From 8cd22309667cf3da9c357e5b7dca43e8b6a2f9c0 Mon Sep 17 00:00:00 2001 From: Danny Holman Date: Fri, 21 Jan 2022 12:10:05 -0600 Subject: arch: i386: move non-critial files out of boot Move all files not needed for the bootstrap process out of boot and into the main x86 source directory. Signed-off-by: Danny Holman --- arch/i386/boot/pic.c | 67 ----------------------------- arch/i386/boot/syscall.c | 21 --------- arch/i386/boot/syscall.h | 9 ---- arch/i386/boot/tss.c | 26 ----------- arch/i386/boot/tss.h | 39 ----------------- arch/i386/boot/tty.c | 87 ------------------------------------- arch/i386/boot/vga.h | 33 -------------- arch/i386/include/kernel/syscall.h | 13 ++++++ arch/i386/include/kernel/vga.h | 33 ++++++++++++++ arch/i386/kernel/pic.c | 67 +++++++++++++++++++++++++++++ arch/i386/kernel/syscall.c | 42 ++++++++++++++++++ arch/i386/kernel/tty.c | 88 ++++++++++++++++++++++++++++++++++++++ arch/i386/make.config | 6 +-- 13 files changed, 246 insertions(+), 285 deletions(-) delete mode 100644 arch/i386/boot/pic.c delete mode 100644 arch/i386/boot/syscall.c delete mode 100644 arch/i386/boot/syscall.h delete mode 100644 arch/i386/boot/tss.c delete mode 100644 arch/i386/boot/tss.h delete mode 100644 arch/i386/boot/tty.c delete mode 100644 arch/i386/boot/vga.h create mode 100644 arch/i386/include/kernel/syscall.h create mode 100644 arch/i386/include/kernel/vga.h create mode 100644 arch/i386/kernel/pic.c create mode 100644 arch/i386/kernel/syscall.c create mode 100644 arch/i386/kernel/tty.c diff --git a/arch/i386/boot/pic.c b/arch/i386/boot/pic.c deleted file mode 100644 index 69a0785..0000000 --- a/arch/i386/boot/pic.c +++ /dev/null @@ -1,67 +0,0 @@ -#include - -void pic_eoi(unsigned char irq) { - if (irq >= 8) - outb(PIC2_COMMAND, 0x20); - outb(PIC1_COMMAND, 0x20); -} - -void pic_remap(void) { - unsigned char a1 = inb(PIC1_DATA); - unsigned char a2 = inb(PIC2_DATA); - - outb(PIC1_COMMAND, ICW1_INIT | ICW1_ICW4); - outb(PIC2_COMMAND, ICW1_INIT | ICW1_ICW4); - outb(PIC1_DATA, PIC1); - outb(PIC2_DATA, PIC2); - outb(PIC1_DATA, 4); - outb(PIC2_DATA, 2); - outb(PIC1_DATA, ICW4_8086); - outb(PIC2_DATA, ICW4_8086); - outb(PIC1_DATA, a1); - outb(PIC2_DATA, a2); -} - -static uint16_t __pic_get_irq_reg(int ocw3) { - outb(PIC1_COMMAND, ocw3); - outb(PIC2_COMMAND, ocw3); - return (inb(PIC2_COMMAND) << 8) | inb(PIC1_COMMAND); -} - -uint16_t pic_get_irr(void) { - return __pic_get_irq_reg(PIC_READ_IRR); -} - -uint16_t pic_get_isr(void) { - return __pic_get_irq_reg(PIC_READ_ISR); -} - -void irq_set_mask(uint8_t irq) { - uint16_t port; - uint8_t data; - - if (irq < 8) { - port = PIC1_DATA; - } else { - port = PIC2_DATA; - irq -= 8; - } - - data = inb(port) | (1 << irq); - outb(port, data); -} - -void irq_clear_mask(uint8_t irq) { - uint16_t port; - uint8_t data; - - if (irq < 8) { - port = PIC1_DATA; - } else { - port = PIC2_DATA; - irq -= 8; - } - - data = inb(port) & ~(1 << irq); - outb(port, data); -} diff --git a/arch/i386/boot/syscall.c b/arch/i386/boot/syscall.c deleted file mode 100644 index fb613b3..0000000 --- a/arch/i386/boot/syscall.c +++ /dev/null @@ -1,21 +0,0 @@ -#include "syscall.h" -#include -#include - -void *(*syscall_handlers[30])(struct isr_frame *frame); - -void syscall_dispatch(struct isr_frame *frame) { - if (syscall_handlers[frame->eax] != NULL) - syscall_handlers[frame->eax](frame); - else - kprintf("Error: Invalid system call number: %d\n", frame->eax); - __asm__ volatile("cli;hlt"); -} - -void register_syscall(void *handler(struct isr_frame*), int num) { - syscall_handlers[num] = handler; -} - -void print_hello(struct isr_frame *frame) { - kprintf("Hello syscall\n"); -} diff --git a/arch/i386/boot/syscall.h b/arch/i386/boot/syscall.h deleted file mode 100644 index 376fb7c..0000000 --- a/arch/i386/boot/syscall.h +++ /dev/null @@ -1,9 +0,0 @@ -#ifndef I386_SYSCALL_H -#define I386_SYSCALL_H - -#include - -void syscall_dispatch(struct isr_frame *frame); -void register_syscall(void *handler(struct isr_frame*), int num); - -#endif diff --git a/arch/i386/boot/tss.c b/arch/i386/boot/tss.c deleted file mode 100644 index 2edfa76..0000000 --- a/arch/i386/boot/tss.c +++ /dev/null @@ -1,26 +0,0 @@ -#include "tss.h" -#include - -struct tss tss_entry; - -uint64_t create_tss_entry(uint16_t ss0, uint32_t esp0) { - uint32_t base = (uint32_t)&tss_entry; - uint32_t limit = base + sizeof(&tss_entry); - uint64_t ret = create_gdt_entry(base, limit, 0xE9); - - memset(&tss_entry, 0, sizeof(struct tss)); - tss_entry.ss0 = ss0; - tss_entry.esp0 = esp0; - tss_entry.cs = 0x08; - tss_entry.ss = 0x13; - tss_entry.ds = 0x13; - tss_entry.es = 0x13; - tss_entry.fs = 0x13; - tss_entry.gs = 0x13; - tss_entry.io_offset = sizeof(struct tss) & 0xFFFF; - return ret; -} - -void set_kernel_stack(uint32_t stack) { - tss_entry.esp0 = stack; -} diff --git a/arch/i386/boot/tss.h b/arch/i386/boot/tss.h deleted file mode 100644 index 28bae6e..0000000 --- a/arch/i386/boot/tss.h +++ /dev/null @@ -1,39 +0,0 @@ -#ifndef I386_TSS_H -#define I386_TSS_H - -#include - -struct tss { - uint32_t link; - uint32_t esp0; - uint32_t ss0; - uint32_t esp1; - uint32_t ss2; - uint32_t cr3; - uint32_t eip; - uint32_t eflags; - uint32_t eax; - uint32_t ecx; - uint32_t edx; - uint32_t ebx; - uint32_t esp; - uint32_t ebp; - uint32_t esi; - uint32_t edi; - uint32_t es; - uint32_t cs; - uint32_t ss; - uint32_t ds; - uint32_t fs; - uint32_t gs; - uint32_t ldtr; - uint32_t io_offset; -}__attribute__((packed)); - -void flush_tss(void); - -uint64_t create_gdt_entry(uint32_t base, uint32_t limit, uint16_t flag); -uint64_t create_tss_entry(uint16_t ss0, uint32_t esp0); -void set_kernel_stack(uint32_t stack); - -#endif diff --git a/arch/i386/boot/tty.c b/arch/i386/boot/tty.c deleted file mode 100644 index aa77736..0000000 --- a/arch/i386/boot/tty.c +++ /dev/null @@ -1,87 +0,0 @@ -#include "vga.h" -#include -#include -#include -#include - -static const size_t VGA_WIDTH = 80; -static const size_t VGA_HEIGHT = 25; -static uint16_t *const VGA_MEMORY = (uint16_t*)0xC03FF000; - -static size_t terminal_row; -static size_t terminal_column; -static uint8_t terminal_color; -static uint16_t *terminal_buffer; - -void tty_init(void) { - terminal_row = 0; - terminal_column = 0; - terminal_color = vga_entry_color(VGA_COLOR_LIGHT_GREY, VGA_COLOR_BLACK); - terminal_buffer = VGA_MEMORY; - for (size_t y = 0; y < VGA_HEIGHT; y++) { - for (size_t x = 0; x < VGA_WIDTH; x++) { - const size_t index = y * VGA_WIDTH + x; - terminal_buffer[index] = vga_entry(' ', terminal_color); - } - } -} - -void tty_setcolor(uint8_t color) { - terminal_color = color; -} - -void tty_putentryat(unsigned char c, uint8_t color, size_t x, size_t y) { - const size_t index = y * VGA_WIDTH + x; - terminal_buffer[index] = vga_entry(c, color); -} - -void terminal_scroll(void) { - for (size_t i = 0; i < VGA_HEIGHT; i++) { - for (size_t j = 0; j < VGA_WIDTH; j++) - terminal_buffer[i * VGA_WIDTH + j] = terminal_buffer[(i+1) * VGA_WIDTH + j]; - } -} - -void tty_putchar(char c) { - unsigned char uc; - - uc = c; - switch (uc) { - case '\n': - terminal_column = 0; - if (++terminal_row == VGA_HEIGHT) { - terminal_row--; - terminal_scroll(); - } - break; - case '\t': - terminal_column += 4; - if (++terminal_column == VGA_WIDTH) { - terminal_column = 4; - if (++terminal_row == VGA_HEIGHT) { - terminal_row--; - terminal_scroll(); - } - } - break; - default: - tty_putentryat(uc, terminal_color, terminal_column, terminal_row); - if (++terminal_column == VGA_WIDTH) { - terminal_column = 0; - if (++terminal_row == VGA_HEIGHT) { - terminal_row--; - terminal_scroll(); - } - } - break; - } -} - -void tty_write(const char *data, size_t size) { - for (size_t i = 0; i < size; i++) - tty_putchar(data[i]); -} - -void tty_writestring(const char *data) { - tty_write(data, strlen(data)); -} diff --git a/arch/i386/boot/vga.h b/arch/i386/boot/vga.h deleted file mode 100644 index e89f202..0000000 --- a/arch/i386/boot/vga.h +++ /dev/null @@ -1,33 +0,0 @@ -#ifndef I386_VGA_H -#define I386_VGA_H - -#include - -enum vga_color { - VGA_COLOR_BLACK = 0, - VGA_COLOR_BLUE = 1, - VGA_COLOR_GREEN = 2, - VGA_COLOR_CYAN = 3, - VGA_COLOR_RED = 4, - VGA_COLOR_MAGENTA = 5, - VGA_COLOR_BROWN = 6, - VGA_COLOR_LIGHT_GREY = 7, - VGA_COLOR_DARK_GREY = 8, - VGA_COLOR_LIGHT_BLUE = 9, - VGA_COLOR_LIGHT_GREEN = 10, - VGA_COLOR_LIGHT_CYAN = 11, - VGA_COLOR_LIGHT_RED = 12, - VGA_COLOR_LIGHT_MAGENTA = 13, - VGA_COLOR_LIGHT_BROWN = 14, - VGA_COLOR_LIGHT_WHITE = 15, -}; - -static inline uint8_t vga_entry_color(enum vga_color fg, enum vga_color bg) { - return fg | bg << 4; -} - -static inline uint16_t vga_entry(unsigned char uc, uint8_t color) { - return (uint16_t)uc | (uint16_t)color << 8; -} - -#endif diff --git a/arch/i386/include/kernel/syscall.h b/arch/i386/include/kernel/syscall.h new file mode 100644 index 0000000..cded732 --- /dev/null +++ b/arch/i386/include/kernel/syscall.h @@ -0,0 +1,13 @@ +#ifndef I386_SYSCALL_H +#define I386_SYSCALL_H + +#include + +void syscall_dispatch(struct isr_frame *frame); +void register_syscall(void *handler(struct isr_frame*), int num); +void sys_stop(struct isr_frame *frame); +void sys_status(struct isr_frame *frame); + +void dump_reg(struct isr_frame *frame); + +#endif diff --git a/arch/i386/include/kernel/vga.h b/arch/i386/include/kernel/vga.h new file mode 100644 index 0000000..e89f202 --- /dev/null +++ b/arch/i386/include/kernel/vga.h @@ -0,0 +1,33 @@ +#ifndef I386_VGA_H +#define I386_VGA_H + +#include + +enum vga_color { + VGA_COLOR_BLACK = 0, + VGA_COLOR_BLUE = 1, + VGA_COLOR_GREEN = 2, + VGA_COLOR_CYAN = 3, + VGA_COLOR_RED = 4, + VGA_COLOR_MAGENTA = 5, + VGA_COLOR_BROWN = 6, + VGA_COLOR_LIGHT_GREY = 7, + VGA_COLOR_DARK_GREY = 8, + VGA_COLOR_LIGHT_BLUE = 9, + VGA_COLOR_LIGHT_GREEN = 10, + VGA_COLOR_LIGHT_CYAN = 11, + VGA_COLOR_LIGHT_RED = 12, + VGA_COLOR_LIGHT_MAGENTA = 13, + VGA_COLOR_LIGHT_BROWN = 14, + VGA_COLOR_LIGHT_WHITE = 15, +}; + +static inline uint8_t vga_entry_color(enum vga_color fg, enum vga_color bg) { + return fg | bg << 4; +} + +static inline uint16_t vga_entry(unsigned char uc, uint8_t color) { + return (uint16_t)uc | (uint16_t)color << 8; +} + +#endif diff --git a/arch/i386/kernel/pic.c b/arch/i386/kernel/pic.c new file mode 100644 index 0000000..69a0785 --- /dev/null +++ b/arch/i386/kernel/pic.c @@ -0,0 +1,67 @@ +#include + +void pic_eoi(unsigned char irq) { + if (irq >= 8) + outb(PIC2_COMMAND, 0x20); + outb(PIC1_COMMAND, 0x20); +} + +void pic_remap(void) { + unsigned char a1 = inb(PIC1_DATA); + unsigned char a2 = inb(PIC2_DATA); + + outb(PIC1_COMMAND, ICW1_INIT | ICW1_ICW4); + outb(PIC2_COMMAND, ICW1_INIT | ICW1_ICW4); + outb(PIC1_DATA, PIC1); + outb(PIC2_DATA, PIC2); + outb(PIC1_DATA, 4); + outb(PIC2_DATA, 2); + outb(PIC1_DATA, ICW4_8086); + outb(PIC2_DATA, ICW4_8086); + outb(PIC1_DATA, a1); + outb(PIC2_DATA, a2); +} + +static uint16_t __pic_get_irq_reg(int ocw3) { + outb(PIC1_COMMAND, ocw3); + outb(PIC2_COMMAND, ocw3); + return (inb(PIC2_COMMAND) << 8) | inb(PIC1_COMMAND); +} + +uint16_t pic_get_irr(void) { + return __pic_get_irq_reg(PIC_READ_IRR); +} + +uint16_t pic_get_isr(void) { + return __pic_get_irq_reg(PIC_READ_ISR); +} + +void irq_set_mask(uint8_t irq) { + uint16_t port; + uint8_t data; + + if (irq < 8) { + port = PIC1_DATA; + } else { + port = PIC2_DATA; + irq -= 8; + } + + data = inb(port) | (1 << irq); + outb(port, data); +} + +void irq_clear_mask(uint8_t irq) { + uint16_t port; + uint8_t data; + + if (irq < 8) { + port = PIC1_DATA; + } else { + port = PIC2_DATA; + irq -= 8; + } + + data = inb(port) & ~(1 << irq); + outb(port, data); +} diff --git a/arch/i386/kernel/syscall.c b/arch/i386/kernel/syscall.c new file mode 100644 index 0000000..570f0de --- /dev/null +++ b/arch/i386/kernel/syscall.c @@ -0,0 +1,42 @@ +#include +#include +#include + +void *(*syscall_handlers[30])(struct isr_frame *frame); + +void syscall_dispatch(struct isr_frame *frame) { + if (syscall_handlers[frame->eax] != NULL) + syscall_handlers[frame->eax](frame); + else + kprintf("Error: Invalid system call number: %d\n", frame->eax); + __asm__ volatile("cli;hlt"); +} + +void register_syscall(void *handler(struct isr_frame*), int num) { + syscall_handlers[num] = handler; +} + +void sys_stop(struct isr_frame *frame) { + kprintf("SYSTEM CALL: STOP\n"); + halt_catch_fire(frame); +} + +void sys_status(struct isr_frame *frame) { + kprintf("SYSTEM CALL: STATUS\n"); + dump_reg(frame); +} + +void dump_reg(struct isr_frame *frame) { + kprintf("Registers at interrupt:\n"); + kprintf("\tEAX = %x\n", frame->eax); + kprintf("\tEBX = %x\n", frame->ebx); + kprintf("\tECX = %x\n", frame->ecx); + kprintf("\tEDX = %x\n", frame->edx); + kprintf("\tESI = %x\n", frame->esi); + kprintf("\tEDI = %x\n", frame->edi); + kprintf("\tESP = %x\n", frame->esp); + kprintf("\tEBP = %x\n", frame->ebp); + kprintf("\tEIP = %x\n", frame->eip); + kprintf("\tEFLAGS = %x\n", frame->eflags); + kprintf("Current code selector: %d\n", frame->cs); +} diff --git a/arch/i386/kernel/tty.c b/arch/i386/kernel/tty.c new file mode 100644 index 0000000..f929408 --- /dev/null +++ b/arch/i386/kernel/tty.c @@ -0,0 +1,88 @@ +#include +#include +#include +#include +#include + +static const size_t VGA_WIDTH = 80; +static const size_t VGA_HEIGHT = 25; +//static uint16_t *const VGA_MEMORY = (uint16_t*)0xC03FF000; +static uint16_t *const VGA_MEMORY = (uint16_t*)0xB8000; + +static size_t terminal_row; +static size_t terminal_column; +static uint8_t terminal_color; +static uint16_t *terminal_buffer; + +void tty_init(void) { + terminal_row = 0; + terminal_column = 0; + terminal_color = vga_entry_color(VGA_COLOR_LIGHT_GREY, VGA_COLOR_BLACK); + terminal_buffer = VGA_MEMORY; + for (size_t y = 0; y < VGA_HEIGHT; y++) { + for (size_t x = 0; x < VGA_WIDTH; x++) { + const size_t index = y * VGA_WIDTH + x; + terminal_buffer[index] = vga_entry(' ', terminal_color); + } + } +} + +void tty_setcolor(uint8_t color) { + terminal_color = color; +} + +void tty_putentryat(unsigned char c, uint8_t color, size_t x, size_t y) { + const size_t index = y * VGA_WIDTH + x; + terminal_buffer[index] = vga_entry(c, color); +} + +void terminal_scroll(void) { + for (size_t i = 0; i < VGA_HEIGHT; i++) { + for (size_t j = 0; j < VGA_WIDTH; j++) + terminal_buffer[i * VGA_WIDTH + j] = terminal_buffer[(i+1) * VGA_WIDTH + j]; + } +} + +void tty_putchar(char c) { + unsigned char uc; + + uc = c; + switch (uc) { + case '\n': + terminal_column = 0; + if (++terminal_row == VGA_HEIGHT) { + terminal_row--; + terminal_scroll(); + } + break; + case '\t': + terminal_column += 4; + if (++terminal_column == VGA_WIDTH) { + terminal_column = 4; + if (++terminal_row == VGA_HEIGHT) { + terminal_row--; + terminal_scroll(); + } + } + break; + default: + tty_putentryat(uc, terminal_color, terminal_column, terminal_row); + if (++terminal_column == VGA_WIDTH) { + terminal_column = 0; + if (++terminal_row == VGA_HEIGHT) { + terminal_row--; + terminal_scroll(); + } + } + break; + } +} + +void tty_write(const char *data, size_t size) { + for (size_t i = 0; i < size; i++) + tty_putchar(data[i]); +} + +void tty_writestring(const char *data) { + tty_write(data, strlen(data)); +} diff --git a/arch/i386/make.config b/arch/i386/make.config index 82e8faf..ec9c631 100644 --- a/arch/i386/make.config +++ b/arch/i386/make.config @@ -5,9 +5,9 @@ KERNEL_ARCH_LIBS= KERNEL_ARCH_OBJS=$(ARCHDIR)/boot/boot.o \ $(ARCHDIR)/boot/isr.o \ - $(ARCHDIR)/boot/tty.o \ - $(ARCHDIR)/boot/pic.o \ $(ARCHDIR)/boot/idt.o \ - $(ARCHDIR)/boot/syscall.o \ $(ARCHDIR)/boot/gdt.o \ + $(ARCHDIR)/kernel/tty.o \ $(ARCHDIR)/kernel/serial.o \ + $(ARCHDIR)/kernel/pic.o \ + $(ARCHDIR)/kernel/syscall.o \ -- cgit v1.2.3