From a914c37365967afaf3148293a857c36af6f94ecb Mon Sep 17 00:00:00 2001 From: Aleksa Vučković Date: Fri, 21 Jan 2022 21:44:28 +0100 Subject: Separating assembly, moving #defines to .h & cleaning Makefile --- src/as/boot.s | 30 +----------------------------- src/as/crti.s | 16 ---------------- src/as/crtn.s | 10 ---------- src/as/gdt.s | 6 ++++++ src/as/idt.s | 7 +++++++ src/as/ioport.s | 13 +++++++++++++ src/c/idt.c | 1 - src/c/irq_handler.c | 10 ---------- src/c/kernel.c | 12 ++++++------ src/c/keyboard.c | 24 +++--------------------- src/c/timer.c | 10 ---------- src/crt/crt0.s | 31 +++++++++++++++++++++++++++++++ src/crt/crti.s | 16 ++++++++++++++++ src/crt/crtn.s | 10 ++++++++++ src/include/asm.h | 7 +++++++ src/include/source/irq_handler.h | 6 ++++++ src/include/source/keyboard.h | 6 ++++++ src/include/source/keymap.h | 5 +++++ 18 files changed, 117 insertions(+), 103 deletions(-) delete mode 100644 src/as/crti.s delete mode 100644 src/as/crtn.s create mode 100644 src/as/gdt.s create mode 100644 src/as/idt.s create mode 100644 src/as/ioport.s create mode 100644 src/crt/crt0.s create mode 100644 src/crt/crti.s create mode 100644 src/crt/crtn.s (limited to 'src') diff --git a/src/as/boot.s b/src/as/boot.s index 907cd3c..f65cc72 100644 --- a/src/as/boot.s +++ b/src/as/boot.s @@ -10,35 +10,6 @@ .long FLAGS .long CHECKSUM -.global _start -.global load_gdt -.global load_idt -.global enable_interrupts -.global ioport_in -.global ioport_out - -load_gdt: - movl 4(%esp), %edx - lgdt (%edx) - ret - -load_idt: - movl 4(%esp), %edx - lidt (%edx) - sti - ret - -ioport_in: - movl 4(%esp),%edx - in %dx,%al - ret - -ioport_out: - movl 4(%esp),%edx - movl 8(%esp),%eax - outb %al,%dx - ret - .set CODE_SEGMENT, 0x08 .set DATA_SEGMENT, 0x10 @@ -49,6 +20,7 @@ stack_bottom: stack_top: .section .text +.global _start .type _start, @function _start: call init_gdt_table diff --git a/src/as/crti.s b/src/as/crti.s deleted file mode 100644 index 5894e2d..0000000 --- a/src/as/crti.s +++ /dev/null @@ -1,16 +0,0 @@ -/* x86 crti.s */ -.section .init -.global _init -.type _init, @function -_init: - push %ebp - movl %esp, %ebp - /* gcc will nicely put the contents of crtbegin.o's .init section here. */ - -.section .fini -.global _fini -.type _fini, @function -_fini: - push %ebp - movl %esp, %ebp - /* gcc will nicely put the contents of crtbegin.o's .fini section here. */ diff --git a/src/as/crtn.s b/src/as/crtn.s deleted file mode 100644 index 0e1c314..0000000 --- a/src/as/crtn.s +++ /dev/null @@ -1,10 +0,0 @@ -/* x86 crtn.s */ -.section .init - /* gcc will nicely put the contents of crtend.o's .init section here. */ - popl %ebp - ret - -.section .fini - /* gcc will nicely put the contents of crtend.o's .fini section here. */ - popl %ebp - ret diff --git a/src/as/gdt.s b/src/as/gdt.s new file mode 100644 index 0000000..632bd3e --- /dev/null +++ b/src/as/gdt.s @@ -0,0 +1,6 @@ +.global load_gdt + +load_gdt: + movl 4(%esp), %edx + lgdt (%edx) + ret diff --git a/src/as/idt.s b/src/as/idt.s new file mode 100644 index 0000000..e95b42b --- /dev/null +++ b/src/as/idt.s @@ -0,0 +1,7 @@ +.global load_idt + +load_idt: + movl 4(%esp), %edx + lidt (%edx) + sti + ret diff --git a/src/as/ioport.s b/src/as/ioport.s new file mode 100644 index 0000000..f4b4dbd --- /dev/null +++ b/src/as/ioport.s @@ -0,0 +1,13 @@ +.global ioport_in +.global ioport_out + +ioport_in: + movl 4(%esp),%edx + in %dx,%al + ret + +ioport_out: + movl 4(%esp),%edx + movl 8(%esp),%eax + outb %al,%dx + ret diff --git a/src/c/idt.c b/src/c/idt.c index c27743f..5a84791 100644 --- a/src/c/idt.c +++ b/src/c/idt.c @@ -3,7 +3,6 @@ #include #include - extern void load_idt(struct idt_pointer *idtp); struct idt_entry idt[256]; diff --git a/src/c/irq_handler.c b/src/c/irq_handler.c index 797639d..b687148 100644 --- a/src/c/irq_handler.c +++ b/src/c/irq_handler.c @@ -2,16 +2,6 @@ #include #include -#define INTERRUPT_GATE_32 0x8e - -#define KERNEL_CODE 0x08 -#define KERNEL_DATA 0x10 - -#define PIC1_COMMAND_PORT 0x20 -#define PIC1_DATA_PORT 0x21 -#define PIC2_COMMAND_PORT 0xA0 -#define PIC2_DATA_PORT 0xA1 - void irq0_handler(void) { ioport_out(PIC1_COMMAND_PORT, 0x20); diff --git a/src/c/kernel.c b/src/c/kernel.c index 1142a23..72b2516 100644 --- a/src/c/kernel.c +++ b/src/c/kernel.c @@ -1,12 +1,12 @@ #include +#include +#include +#include +#include #include +#include +#include -void terminal_initialize(void); -void init_idt_table(void); -void init_keyboard(void); -void init_timer(uint32_t frequency); -void prompt(void); -void set_paging(void); void kernel_main(void) { diff --git a/src/c/keyboard.c b/src/c/keyboard.c index 8af463b..a95d399 100644 --- a/src/c/keyboard.c +++ b/src/c/keyboard.c @@ -2,37 +2,19 @@ #include #include #include +#include +#include +#include -#define BUFFER_SIZE 200 -#define BUFFER_LOG 200 char buffer[BUFFER_LOG][BUFFER_SIZE]; size_t buffer_size[BUFFER_LOG]; size_t buffer_current=0; size_t buffer_all=0; size_t buffer_index=0; -#define PIC1_COMMAND_PORT 0x20 -#define PIC1_DATA_PORT 0x21 -#define PIC2_COMMAND_PORT 0xA0 -#define PIC2_DATA_PORT 0xA1 -// IO Ports for Keyboard -#define KEYBOARD_DATA_PORT 0x60 -#define KEYBOARD_STATUS_PORT 0x64 - -void previous_field(void); -void tty(char *buffer); -void prompt(void); -void clear(void); -void us_en(char keymap[]); -void us_en_shift(char keymap[]); - char charcode[256]; char shift_charcode[256]; bool ispressed[128]; -#define lshift 0x2A -#define rshift 0x36 -#define lctrl 0x1D -#define rctrl 0x1D void init_keyboard() { diff --git a/src/c/timer.c b/src/c/timer.c index 412e443..3a8f159 100644 --- a/src/c/timer.c +++ b/src/c/timer.c @@ -13,7 +13,6 @@ void timer_handler(void) tick++; if(tick==TICKS_PER_SECOND) { - //printf("%d seconds passed\n",time); tick=0; time++; } @@ -24,21 +23,12 @@ void timer_handler(void) void init_timer(uint32_t frequency) { - // Firstly, register our timer callback. - - // The value we send to the PIT is the value to divide it's input clock - // (1193180 Hz) by, to get our required frequency. Important to note is - // that the divisor must be small enough to fit into 16-bits. uint32_t divisor = 1193180 / frequency; - - // Send the command byte. ioport_out(0x43, 0x36); - // Divisor has to be sent byte-wise, so split here into upper/lower bytes. uint8_t l = (uint8_t)(divisor & 0xFF); uint8_t h = (uint8_t)( (divisor>>8) & 0xFF ); - // Send the frequency divisor. ioport_out(0x40, l); ioport_out(0x40, h); } diff --git a/src/crt/crt0.s b/src/crt/crt0.s new file mode 100644 index 0000000..8c735f1 --- /dev/null +++ b/src/crt/crt0.s @@ -0,0 +1,31 @@ +.section .text + +.global _start +_start: + # Set up end of the stack frame linked list. + movl $0, %ebp + pushl %ebp # rip=0 + pushl %ebp # rbp=0 + movl %esp, %ebp + + # We need those in a moment when we call main. + pushl %esi + pushl %edi + + # Prepare signals, memory allocation, stdio and such. +# call initialize_standard_library + + # Run the global constructors. + call _init + + # Restore argc and argv. + popl %edi + popl %esi + + # Run main + call main + + # Terminate the process with the exit code. + movl %eax, %edi +# call exit +.size _start, . - _start diff --git a/src/crt/crti.s b/src/crt/crti.s new file mode 100644 index 0000000..5894e2d --- /dev/null +++ b/src/crt/crti.s @@ -0,0 +1,16 @@ +/* x86 crti.s */ +.section .init +.global _init +.type _init, @function +_init: + push %ebp + movl %esp, %ebp + /* gcc will nicely put the contents of crtbegin.o's .init section here. */ + +.section .fini +.global _fini +.type _fini, @function +_fini: + push %ebp + movl %esp, %ebp + /* gcc will nicely put the contents of crtbegin.o's .fini section here. */ diff --git a/src/crt/crtn.s b/src/crt/crtn.s new file mode 100644 index 0000000..0e1c314 --- /dev/null +++ b/src/crt/crtn.s @@ -0,0 +1,10 @@ +/* x86 crtn.s */ +.section .init + /* gcc will nicely put the contents of crtend.o's .init section here. */ + popl %ebp + ret + +.section .fini + /* gcc will nicely put the contents of crtend.o's .fini section here. */ + popl %ebp + ret diff --git a/src/include/asm.h b/src/include/asm.h index e57c35b..ee15f00 100644 --- a/src/include/asm.h +++ b/src/include/asm.h @@ -3,7 +3,14 @@ #include +#define PIC1_COMMAND_PORT 0x20 +#define PIC1_DATA_PORT 0x21 +#define PIC2_COMMAND_PORT 0xA0 +#define PIC2_DATA_PORT 0xA1 + + extern uint8_t ioport_in(uint8_t port); extern void ioport_out(uint8_t port, int data); + #endif diff --git a/src/include/source/irq_handler.h b/src/include/source/irq_handler.h index 41b4f8c..a20ed70 100644 --- a/src/include/source/irq_handler.h +++ b/src/include/source/irq_handler.h @@ -1,6 +1,12 @@ #ifndef SOURCE_IRQ_HANDLER_H #define SOURCE_IRQ_HANDLER_H +#define INTERRUPT_GATE_32 0x8e + +#define KERNEL_CODE 0x08 +#define KERNEL_DATA 0x10 + + void irq0_handler(void); void irq1_handler(void); void irq2_handler(void); diff --git a/src/include/source/keyboard.h b/src/include/source/keyboard.h index 28fa104..a023e66 100644 --- a/src/include/source/keyboard.h +++ b/src/include/source/keyboard.h @@ -1,6 +1,12 @@ #ifndef SOURCE_KEYBOARD_H #define SOURCE_KEYBOARD_H +#define BUFFER_SIZE 200 +#define BUFFER_LOG 200 + +#define KEYBOARD_DATA_PORT 0x60 +#define KEYBOARD_STATUS_PORT 0x64 + void init_keyboard(void); void deletelast(void); void backspace(void); diff --git a/src/include/source/keymap.h b/src/include/source/keymap.h index 1c474ae..7d01291 100644 --- a/src/include/source/keymap.h +++ b/src/include/source/keymap.h @@ -3,6 +3,11 @@ #include +#define lshift 0x2A +#define rshift 0x36 +#define lctrl 0x1D +#define rctrl 0x1D + void us_en(char keymap[]); void us_en_shift(char keymap[]); -- cgit v1.2.3