From ed84017353c6fc9421b223ff6ec62f8d881d8098 Mon Sep 17 00:00:00 2001 From: Aleksa Vučković Date: Mon, 17 Jan 2022 22:00:14 +0100 Subject: Fixing $(WARNING)s & explicitly declaring function prototypes --- src/c/gdt.c | 35 ++---- src/c/heap.c | 61 +++++---- src/c/idt.c | 39 ++---- src/c/irq.c | 236 ---------------------------------- src/c/irq_handler.c | 237 +++++++++++++++++++++++++++++++++++ src/c/kernel.c | 5 +- src/c/keyboard.c | 5 +- src/c/keymap.c | 3 + src/c/paging.c | 7 +- src/c/stack_protector.c | 3 +- src/c/stdio.c | 15 +-- src/c/string.c | 23 ++-- src/c/timer.c | 7 +- src/c/tty.c | 19 ++- src/c/vga.c | 23 ++-- src/include/asm.h | 6 +- src/include/heap.h | 11 -- src/include/irq.h | 39 ------ src/include/source/gdt.h | 25 ++++ src/include/source/heap.h | 30 +++++ src/include/source/idt.h | 36 ++++++ src/include/source/irq.h | 39 ++++++ src/include/source/irq_handler.h | 37 ++++++ src/include/source/kernel.h | 6 + src/include/source/keyboard.h | 15 +++ src/include/source/keymap.h | 9 ++ src/include/source/paging.h | 10 ++ src/include/source/stack_protector.h | 6 + src/include/source/stdio.h | 6 + src/include/source/string.h | 17 +++ src/include/source/timer.h | 9 ++ src/include/source/tty.h | 19 +++ src/include/source/vga.h | 40 ++++++ src/include/stdio.h | 2 - src/include/string.h | 11 -- src/include/types.h | 4 +- src/include/vga.h | 29 ----- 37 files changed, 648 insertions(+), 476 deletions(-) delete mode 100644 src/c/irq.c create mode 100644 src/c/irq_handler.c delete mode 100644 src/include/heap.h delete mode 100644 src/include/irq.h create mode 100644 src/include/source/gdt.h create mode 100644 src/include/source/heap.h create mode 100644 src/include/source/idt.h create mode 100644 src/include/source/irq.h create mode 100644 src/include/source/irq_handler.h create mode 100644 src/include/source/kernel.h create mode 100644 src/include/source/keyboard.h create mode 100644 src/include/source/keymap.h create mode 100644 src/include/source/paging.h create mode 100644 src/include/source/stack_protector.h create mode 100644 src/include/source/stdio.h create mode 100644 src/include/source/string.h create mode 100644 src/include/source/timer.h create mode 100644 src/include/source/tty.h create mode 100644 src/include/source/vga.h delete mode 100644 src/include/vga.h (limited to 'src') diff --git a/src/c/gdt.c b/src/c/gdt.c index e3d2b4c..8791cbe 100644 --- a/src/c/gdt.c +++ b/src/c/gdt.c @@ -1,46 +1,31 @@ +#include #include -struct gdt_entry -{ - uint16_t limit; - uint16_t base1; - uint8_t base2; - uint8_t access; - uint8_t limit_flags; - uint8_t base3; -} __attribute__((packed)); - -struct gdt_pointer -{ - uint16_t size; - uint32_t offset; -} __attribute__((packed)); - extern void load_gdt(struct gdt_pointer *gdtp); struct gdt_entry gdt[5]; struct gdt_pointer gdtp; -void init_gdt_entry(size_t num, uint32_t limit, uint32_t base, uint8_t access, uint8_t limit_flags) +void init_gdt_entry(size_t num, uint16_t limit, uint32_t base, uint8_t access, uint8_t limit_flags) { gdt[num].limit=limit; gdt[num].base1=(base & 0xffff); - gdt[num].base2=(base & 0xff0000) >> 16; + gdt[num].base2=(uint8_t)((base & 0xff0000) >> 16); gdt[num].access=access; gdt[num].limit_flags=limit_flags; - gdt[num].base3=(base & 0xff000000) >> 24; + gdt[num].base3=(uint8_t)((base & 0xff000000) >> 24); } -void init_gdt_table() +void init_gdt_table(void) { gdtp.size=sizeof(gdt)-1; gdtp.offset=(uint32_t)&gdt; - init_gdt_entry(0,0,0,0,0); // null segment - init_gdt_entry(1,0xffffffff,0,0b10011010,0b11001111); // code segment - init_gdt_entry(2,0xffffffff,0,0b10010010,0b11001111); // data segment - init_gdt_entry(3,0xffffffff,0,0b11111010,0b11001111); // user mode code segment - init_gdt_entry(4,0xffffffff,0,0b11110010,0b11001111); // user mode data segment + init_gdt_entry(0,0,0,0,0); // null segment + init_gdt_entry(1,0xffff,0,0x9a,0xcf); // code segment + init_gdt_entry(2,0xffff,0,0x92,0xcf); // data segment + init_gdt_entry(3,0xffff,0,0xfa,0xcf); // user mode code segment + init_gdt_entry(4,0xffff,0,0xf2,0xcf); // user mode data segment load_gdt(&gdtp); } diff --git a/src/c/heap.c b/src/c/heap.c index 99d0a37..aaa35dc 100644 --- a/src/c/heap.c +++ b/src/c/heap.c @@ -1,22 +1,13 @@ +#include #include -typedef struct _KHEAPBLOCKBM { - struct _KHEAPBLOCKBM *next; - uint32_t size; - uint32_t used; - uint32_t bsize; - uint32_t lfb; -} KHEAPBLOCKBM; - -typedef struct _KHEAPBM { - KHEAPBLOCKBM *fblock; -} KHEAPBM; - -void k_heapBMInit(KHEAPBM *heap) { +void k_heapBMInit(KHEAPBM *heap) +{ heap->fblock = 0; } -int k_heapBMAddBlock(KHEAPBM *heap, uintptr_t addr, uint32_t size, uint32_t bsize) { +int k_heapBMAddBlock(KHEAPBM *heap, uintptr_t addr, uint32_t size, uint32_t bsize) +{ KHEAPBLOCKBM *b; uint32_t bcnt; uint32_t x; @@ -48,13 +39,16 @@ int k_heapBMAddBlock(KHEAPBM *heap, uintptr_t addr, uint32_t size, uint32_t bsiz return 1; } -static uint8_t k_heapBMGetNID(uint8_t a, uint8_t b) { +static uint8_t k_heapBMGetNID(uint8_t a, uint8_t b); +static uint8_t k_heapBMGetNID(uint8_t a, uint8_t b) +{ uint8_t c; - for (c = a + 1; c == b || c == 0; ++c); + for (c=a+1;c==b||c==0;++c); return c; } -void *k_heapBMAlloc(KHEAPBM *heap, uint32_t size) { +void *k_heapBMAlloc(KHEAPBM *heap, uint32_t size) +{ KHEAPBLOCKBM *b; uint8_t *bm; uint32_t bcnt; @@ -63,33 +57,34 @@ void *k_heapBMAlloc(KHEAPBM *heap, uint32_t size) { uint8_t nid; /* iterate blocks */ - for (b = heap->fblock; b; b = b->next) { + for (b = heap->fblock; b; b = b->next) + { /* check if block has enough room */ - if (b->size - (b->used * b->bsize) >= size) { + if (b->size - (b->used * b->bsize) >= size) + { bcnt = b->size / b->bsize; bneed = (size / b->bsize) * b->bsize < size ? size / b->bsize + 1 : size / b->bsize; bm = (uint8_t*)&b[1]; - for (x = (b->lfb + 1 >= bcnt ? 0 : b->lfb + 1); x != b->lfb; ++x) { + for (x = (b->lfb + 1 >= bcnt ? 0 : b->lfb + 1); x != b->lfb; ++x) + { /* just wrap around */ - if (x >= bcnt) { - x = 0; - } + if (x >= bcnt) x = 0; - if (bm[x] == 0) { + if (bm[x] == 0) + { /* count free blocks */ for (y = 0; bm[x + y] == 0 && y < bneed && (x + y) < bcnt; ++y); /* we have enough, now allocate them */ - if (y == bneed) { + if (y == bneed) + { /* find ID that does not match left or right */ nid = k_heapBMGetNID(bm[x - 1], bm[x + y]); /* allocate by setting id */ - for (z = 0; z < y; ++z) { - bm[x + z] = nid; - } + for (z = 0; z < y; ++z) bm[x + z] = nid; /* optimization */ b->lfb = (x + bneed) - 2; @@ -110,7 +105,8 @@ void *k_heapBMAlloc(KHEAPBM *heap, uint32_t size) { return 0; } -void k_heapBMFree(KHEAPBM *heap, void *ptr) { +void k_heapBMFree(KHEAPBM *heap, void *ptr) +{ KHEAPBLOCKBM *b; uintptr_t ptroff; uint32_t bi, x; @@ -118,7 +114,8 @@ void k_heapBMFree(KHEAPBM *heap, void *ptr) { uint8_t id; uint32_t max; - for (b = heap->fblock; b; b = b->next) { + for (b = heap->fblock; b; b = b->next) + { if ((uintptr_t)ptr > (uintptr_t)b && (uintptr_t)ptr < (uintptr_t)b + sizeof(KHEAPBLOCKBM) + b->size) { /* found block */ ptroff = (uintptr_t)ptr - (uintptr_t)&b[1]; /* get offset to get block */ @@ -130,9 +127,7 @@ void k_heapBMFree(KHEAPBM *heap, void *ptr) { id = bm[bi]; /* oddly.. GCC did not optimize this */ max = b->size / b->bsize; - for (x = bi; bm[x] == id && x < max; ++x) { - bm[x] = 0; - } + for (x = bi; bm[x] == id && x < max; ++x) bm[x] = 0; /* update free block count */ b->used -= x - bi; return; diff --git a/src/c/idt.c b/src/c/idt.c index 843c9da..c27743f 100644 --- a/src/c/idt.c +++ b/src/c/idt.c @@ -1,54 +1,29 @@ +#include #include -#include +#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 - -struct idt_entry -{ - uint16_t offset1; - uint16_t selector; - uint8_t zero; - uint8_t type_attr; - uint16_t offset2; -} __attribute__((packed)); - -struct idt_pointer -{ - uint16_t size; - uint32_t offset; -} __attribute__((packed)); - extern void load_idt(struct idt_pointer *idtp); -extern void keyboard_irq(); struct idt_entry idt[256]; struct idt_pointer idtp; void init_idt_entry(size_t num, uint32_t offset, uint16_t selector, uint8_t type_attr) { - idt[num].offset1=(offset & 0xffff); + idt[num].offset1=(uint16_t)(offset & 0xffff); idt[num].selector=selector; idt[num].zero=0; idt[num].type_attr=type_attr; - idt[num].offset2=(offset & 0xffff0000)>>16; + idt[num].offset2=(uint16_t)((offset & 0xffff0000)>>16); } -void add_idt_entry(size_t num,uint32_t offset) +void add_idt_entry(size_t num, uint32_t offset) { init_idt_entry(num,offset,KERNEL_CODE,INTERRUPT_GATE_32); } -void init_pic() +void init_pic(void) { ioport_out(PIC1_COMMAND_PORT, 0x11); ioport_out(PIC2_COMMAND_PORT, 0x11); @@ -63,7 +38,7 @@ void init_pic() ioport_out(PIC1_DATA_PORT, 0xFC); } -void init_idt_table() +void init_idt_table(void) { init_pic(); add_idt_entry(0,(uint32_t)irq0); diff --git a/src/c/irq.c b/src/c/irq.c deleted file mode 100644 index 62b53ee..0000000 --- a/src/c/irq.c +++ /dev/null @@ -1,236 +0,0 @@ -#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() -{ - ioport_out(PIC1_COMMAND_PORT, 0x20); - printf("Interrupt 0.\n"); - printf("Divide-by-zero Error\n"); -} - -void irq1_handler() -{ - ioport_out(PIC1_COMMAND_PORT, 0x20); - printf("Interrupt 1.\n"); - printf("Debug\n"); -} - -void irq2_handler() -{ - ioport_out(PIC1_COMMAND_PORT, 0x20); - printf("Interrupt 2.\n"); - printf("Non-maskable Interrupt\n"); -} - -void irq3_handler() -{ - ioport_out(PIC1_COMMAND_PORT, 0x20); - printf("Interrupt 3.\n"); - printf("Breakpoint\n"); -} - -void irq4_handler() -{ - ioport_out(PIC1_COMMAND_PORT, 0x20); - printf("Interrupt 4.\n"); - printf("Overflow\n"); -} - -void irq5_handler() -{ - ioport_out(PIC1_COMMAND_PORT, 0x20); - printf("Interrupt 5.\n"); - printf("Bound Range Exceeded\n"); -} - -void irq6_handler() -{ - ioport_out(PIC1_COMMAND_PORT, 0x20); - printf("Interrupt 6.\n"); - printf("Invalid Opcode\n"); -} - -void irq7_handler() -{ - ioport_out(PIC1_COMMAND_PORT, 0x20); - printf("Interrupt 7.\n"); - printf("Device Not Available\n"); -} - -void irq8_handler() -{ - ioport_out(PIC1_COMMAND_PORT, 0x20); - printf("Interrupt 8.\n"); - printf("Double Fault\n"); -} - -void irq9_handler() -{ - ioport_out(PIC1_COMMAND_PORT, 0x20); - printf("Interrupt 9.\n"); - printf("Coprocessor Segment Overrun\n"); -} - -void irq10_handler() -{ - ioport_out(PIC1_COMMAND_PORT, 0x20); - printf("Interrupt 10.\n"); - printf("Invalid TSS\n"); -} - -void irq11_handler() -{ - ioport_out(PIC1_COMMAND_PORT, 0x20); - printf("Interrupt 11.\n"); - printf("Segment Not Present\n"); -} - -void irq12_handler() -{ - ioport_out(PIC1_COMMAND_PORT, 0x20); - printf("Interrupt 12.\n"); - printf("Stack-Segment Fault\n"); -} - -void irq13_handler() -{ - ioport_out(PIC1_COMMAND_PORT, 0x20); - printf("Interrupt 13.\n"); - printf("General Protection Fault\n"); -} - -void irq14_handler() -{ - ioport_out(PIC1_COMMAND_PORT, 0x20); - printf("Interrupt 14.\n"); - printf("Page Fault\n"); -} - -void irq15_handler() -{ - ioport_out(PIC1_COMMAND_PORT, 0x20); - printf("Interrupt 15.\n"); - printf("Reserved\n"); -} - -void irq16_handler() -{ - ioport_out(PIC1_COMMAND_PORT, 0x20); - printf("Interrupt 16.\n"); - printf("x87 Floating-Point Exception\n"); -} - -void irq17_handler() -{ - ioport_out(PIC1_COMMAND_PORT, 0x20); - printf("Interrupt 17.\n"); - printf("Alignment Check\n"); -} - -void irq18_handler() -{ - ioport_out(PIC1_COMMAND_PORT, 0x20); - printf("Interrupt 18.\n"); - printf("Machine Check\n"); -} - -void irq19_handler() -{ - ioport_out(PIC1_COMMAND_PORT, 0x20); - printf("Interrupt 19.\n"); - printf("SIMD Floating-Point ExceptionM/#XF\n"); -} - -void irq20_handler() -{ - ioport_out(PIC1_COMMAND_PORT, 0x20); - printf("Interrupt 20.\n"); - printf("Virtualization Exception\n"); -} - -void irq21_handler() -{ - ioport_out(PIC1_COMMAND_PORT, 0x20); - printf("Interrupt 21.\n"); - printf("Reserved\n"); -} - -void irq22_handler() -{ - ioport_out(PIC1_COMMAND_PORT, 0x20); - printf("Interrupt 22.\n"); - printf("Reserved\n"); -} - -void irq23_handler() -{ - ioport_out(PIC1_COMMAND_PORT, 0x20); - printf("Interrupt 23.\n"); - printf("Reserved\n"); -} - -void irq24_handler() -{ - ioport_out(PIC1_COMMAND_PORT, 0x20); - printf("Interrupt 24.\n"); - printf("Reserved\n"); -} - -void irq25_handler() -{ - ioport_out(PIC1_COMMAND_PORT, 0x20); - printf("Interrupt 25.\n"); - printf("Reserved\n"); -} - -void irq26_handler() -{ - ioport_out(PIC1_COMMAND_PORT, 0x20); - printf("Interrupt 26.\n"); - printf("Reserved\n"); -} - -void irq27_handler() -{ - ioport_out(PIC1_COMMAND_PORT, 0x20); - printf("Interrupt 27.\n"); - printf("Reserved\n"); -} - -void irq28_handler() -{ - ioport_out(PIC1_COMMAND_PORT, 0x20); - printf("Interrupt 28.\n"); - printf("Reserved\n"); -} - -void irq29_handler() -{ - ioport_out(PIC1_COMMAND_PORT, 0x20); - printf("Interrupt 29.\n"); - printf("Reserved\n"); -} - -void irq30_handler() -{ - ioport_out(PIC1_COMMAND_PORT, 0x20); - printf("Interrupt 30.\n"); - printf("Security Exception\n"); -} - -void irq31_handler() -{ - ioport_out(PIC1_COMMAND_PORT, 0x20); - printf("Interrupt 31.\n"); - printf("Reserved\n"); -} diff --git a/src/c/irq_handler.c b/src/c/irq_handler.c new file mode 100644 index 0000000..797639d --- /dev/null +++ b/src/c/irq_handler.c @@ -0,0 +1,237 @@ +#include +#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); + printf("Interrupt 0.\n"); + printf("Divide-by-zero Error\n"); +} + +void irq1_handler(void) +{ + ioport_out(PIC1_COMMAND_PORT, 0x20); + printf("Interrupt 1.\n"); + printf("Debug\n"); +} + +void irq2_handler(void) +{ + ioport_out(PIC1_COMMAND_PORT, 0x20); + printf("Interrupt 2.\n"); + printf("Non-maskable Interrupt\n"); +} + +void irq3_handler(void) +{ + ioport_out(PIC1_COMMAND_PORT, 0x20); + printf("Interrupt 3.\n"); + printf("Breakpoint\n"); +} + +void irq4_handler(void) +{ + ioport_out(PIC1_COMMAND_PORT, 0x20); + printf("Interrupt 4.\n"); + printf("Overflow\n"); +} + +void irq5_handler(void) +{ + ioport_out(PIC1_COMMAND_PORT, 0x20); + printf("Interrupt 5.\n"); + printf("Bound Range Exceeded\n"); +} + +void irq6_handler(void) +{ + ioport_out(PIC1_COMMAND_PORT, 0x20); + printf("Interrupt 6.\n"); + printf("Invalid Opcode\n"); +} + +void irq7_handler(void) +{ + ioport_out(PIC1_COMMAND_PORT, 0x20); + printf("Interrupt 7.\n"); + printf("Device Not Available\n"); +} + +void irq8_handler(void) +{ + ioport_out(PIC1_COMMAND_PORT, 0x20); + printf("Interrupt 8.\n"); + printf("Double Fault\n"); +} + +void irq9_handler(void) +{ + ioport_out(PIC1_COMMAND_PORT, 0x20); + printf("Interrupt 9.\n"); + printf("Coprocessor Segment Overrun\n"); +} + +void irq10_handler(void) +{ + ioport_out(PIC1_COMMAND_PORT, 0x20); + printf("Interrupt 10.\n"); + printf("Invalid TSS\n"); +} + +void irq11_handler(void) +{ + ioport_out(PIC1_COMMAND_PORT, 0x20); + printf("Interrupt 11.\n"); + printf("Segment Not Present\n"); +} + +void irq12_handler(void) +{ + ioport_out(PIC1_COMMAND_PORT, 0x20); + printf("Interrupt 12.\n"); + printf("Stack-Segment Fault\n"); +} + +void irq13_handler(void) +{ + ioport_out(PIC1_COMMAND_PORT, 0x20); + printf("Interrupt 13.\n"); + printf("General Protection Fault\n"); +} + +void irq14_handler(void) +{ + ioport_out(PIC1_COMMAND_PORT, 0x20); + printf("Interrupt 14.\n"); + printf("Page Fault\n"); +} + +void irq15_handler(void) +{ + ioport_out(PIC1_COMMAND_PORT, 0x20); + printf("Interrupt 15.\n"); + printf("Reserved\n"); +} + +void irq16_handler(void) +{ + ioport_out(PIC1_COMMAND_PORT, 0x20); + printf("Interrupt 16.\n"); + printf("x87 Floating-Point Exception\n"); +} + +void irq17_handler(void) +{ + ioport_out(PIC1_COMMAND_PORT, 0x20); + printf("Interrupt 17.\n"); + printf("Alignment Check\n"); +} + +void irq18_handler(void) +{ + ioport_out(PIC1_COMMAND_PORT, 0x20); + printf("Interrupt 18.\n"); + printf("Machine Check\n"); +} + +void irq19_handler(void) +{ + ioport_out(PIC1_COMMAND_PORT, 0x20); + printf("Interrupt 19.\n"); + printf("SIMD Floating-Point ExceptionM/#XF\n"); +} + +void irq20_handler(void) +{ + ioport_out(PIC1_COMMAND_PORT, 0x20); + printf("Interrupt 20.\n"); + printf("Virtualization Exception\n"); +} + +void irq21_handler(void) +{ + ioport_out(PIC1_COMMAND_PORT, 0x20); + printf("Interrupt 21.\n"); + printf("Reserved\n"); +} + +void irq22_handler(void) +{ + ioport_out(PIC1_COMMAND_PORT, 0x20); + printf("Interrupt 22.\n"); + printf("Reserved\n"); +} + +void irq23_handler(void) +{ + ioport_out(PIC1_COMMAND_PORT, 0x20); + printf("Interrupt 23.\n"); + printf("Reserved\n"); +} + +void irq24_handler(void) +{ + ioport_out(PIC1_COMMAND_PORT, 0x20); + printf("Interrupt 24.\n"); + printf("Reserved\n"); +} + +void irq25_handler(void) +{ + ioport_out(PIC1_COMMAND_PORT, 0x20); + printf("Interrupt 25.\n"); + printf("Reserved\n"); +} + +void irq26_handler(void) +{ + ioport_out(PIC1_COMMAND_PORT, 0x20); + printf("Interrupt 26.\n"); + printf("Reserved\n"); +} + +void irq27_handler(void) +{ + ioport_out(PIC1_COMMAND_PORT, 0x20); + printf("Interrupt 27.\n"); + printf("Reserved\n"); +} + +void irq28_handler(void) +{ + ioport_out(PIC1_COMMAND_PORT, 0x20); + printf("Interrupt 28.\n"); + printf("Reserved\n"); +} + +void irq29_handler(void) +{ + ioport_out(PIC1_COMMAND_PORT, 0x20); + printf("Interrupt 29.\n"); + printf("Reserved\n"); +} + +void irq30_handler(void) +{ + ioport_out(PIC1_COMMAND_PORT, 0x20); + printf("Interrupt 30.\n"); + printf("Security Exception\n"); +} + +void irq31_handler(void) +{ + ioport_out(PIC1_COMMAND_PORT, 0x20); + printf("Interrupt 31.\n"); + printf("Reserved\n"); +} diff --git a/src/c/kernel.c b/src/c/kernel.c index 849a356..1142a23 100644 --- a/src/c/kernel.c +++ b/src/c/kernel.c @@ -1,11 +1,12 @@ -#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 set_paging(void); void kernel_main(void) { diff --git a/src/c/keyboard.c b/src/c/keyboard.c index 2218f09..8af463b 100644 --- a/src/c/keyboard.c +++ b/src/c/keyboard.c @@ -1,6 +1,7 @@ +#include #include #include -#include +#include #define BUFFER_SIZE 200 #define BUFFER_LOG 200 @@ -21,7 +22,7 @@ size_t buffer_index=0; void previous_field(void); void tty(char *buffer); void prompt(void); -void clear(); +void clear(void); void us_en(char keymap[]); void us_en_shift(char keymap[]); diff --git a/src/c/keymap.c b/src/c/keymap.c index 168d5f9..d9315b6 100644 --- a/src/c/keymap.c +++ b/src/c/keymap.c @@ -1,3 +1,5 @@ +#include + void us_en(char keymap[]) { keymap[0x01]=' '; //escape pressed @@ -171,6 +173,7 @@ void us_en(char keymap[]) keymap[0xD7]=' '; //F11 released keymap[0xD8]=' '; //F12 released } + void us_en_shift(char keymap[]) { keymap[0x01]=' '; //escape pressed diff --git a/src/c/paging.c b/src/c/paging.c index f08530d..f7b6e6a 100644 --- a/src/c/paging.c +++ b/src/c/paging.c @@ -1,11 +1,12 @@ +#include #include extern void loadPageDirectory(uint32_t*); -extern void enablePaging(); +extern void enablePaging(void); uint32_t page_directory[1024] __attribute__((aligned(4096))); -void set_pd() +void set_pd(void) { //set each entry to not present for(size_t i=0;i<1024;i++) @@ -37,7 +38,7 @@ void set_pt(size_t num,uint32_t address) // attributes: supervisor level, read/write, present } -void set_paging() +void set_paging(void) { set_pd(); for(size_t i=0;i<1024;i++) set_pt(i,0x00400000 * i); // all 4GB mapped diff --git a/src/c/stack_protector.c b/src/c/stack_protector.c index 37f7cda..e2d7ab2 100644 --- a/src/c/stack_protector.c +++ b/src/c/stack_protector.c @@ -1,5 +1,6 @@ +#include #include -#include +#include #if UINT32_MAX == UINTPTR_MAX #define STACK_CHK_GUARD 0xe2dee396 diff --git a/src/c/stdio.c b/src/c/stdio.c index 3a51e9a..e47f43d 100644 --- a/src/c/stdio.c +++ b/src/c/stdio.c @@ -1,13 +1,10 @@ +#include #include -#include #include +#include +#include -void terminal_putchar(char c); -void terminal_writestring(char* data); -void terminal_writeint(uint32_t data); -void terminal_writefloat(double num); - -void printf(char *str, ...) +void printf(const char *str, ...) { size_t count=0; for(size_t i=0;str[i]!='\0';i++) if(str[i]=='%') count++; @@ -22,11 +19,11 @@ void printf(char *str, ...) i++; if(str[i]=='c') terminal_putchar((char)va_arg(list,int)); else if(str[i]=='s') terminal_writestring(va_arg(list,char*)); - else if(str[i]=='d') terminal_writeint(va_arg(list,int)); + else if(str[i]=='d') terminal_writeint((uint32_t)va_arg(list,int)); else if(str[i]=='f') terminal_writefloat(va_arg(list,double)); else { - terminal_writestring("wrong format using print function\n"); + terminal_writestring("Wrong format using print function\n"); return; } } diff --git a/src/c/string.c b/src/c/string.c index ce33528..201353f 100644 --- a/src/c/string.c +++ b/src/c/string.c @@ -1,6 +1,7 @@ +#include #include -size_t stringlen(char *str) +size_t stringlen(char* str) { size_t i; @@ -9,7 +10,7 @@ size_t stringlen(char *str) return i; } -bool stringcmp(char *str1,char *str2) +bool stringcmp(const char* str1, const char* str2) { size_t i; for(i=0;str1[i]||str2[i];i++) if(str1[i]!=str2[i]) return 0; @@ -17,7 +18,7 @@ bool stringcmp(char *str1,char *str2) return 0; } -void stringcat(char *str1,char *str2) +void stringcat(char* str1, const char* str2) { char *tmp=str1; while(*tmp) tmp++; @@ -25,7 +26,7 @@ void stringcat(char *str1,char *str2) *tmp=*str2; } -void stringcpy(char *str1,char *str2) +void stringcpy(char *str1, const char *str2) { for(size_t i=0;str2[i]!='\0';i++) str1[i]=str2[i]; } @@ -42,7 +43,7 @@ void stringrev(char *str) } } -uint32_t stoi(const char *str) +uint32_t stoi(const char* str) { uint32_t num=0; @@ -50,26 +51,26 @@ uint32_t stoi(const char *str) { if(str[i]<'0'||str[i]>'9') return num; num*=10; - num+=str[i]-'0'; + num+=(uint32_t)(str[i]-'0'); } return num; } -void itos(uint32_t num,char *str) +void itos(uint32_t num, char* str) { if(num==0) stringcpy(str,"0"); else { size_t i=0; - for(;num>0;num/=10,i++) str[i]='0'+num%10; + for(;num>0;num/=10,i++) str[i]=(char)('0'+num%10); str[i]='\0'; stringrev(str); } } -double stof(const char *str) +double stof(const char* str) { double num=0; @@ -95,7 +96,7 @@ double stof(const char *str) } const size_t decimals=7; -void ftos(double num, char *str) +void ftos(double num, char* str) { itos((uint32_t)num,str); @@ -107,7 +108,7 @@ void ftos(double num, char *str) { num-=(uint32_t)num; num*=10; - c[0]=(uint32_t)num+'0'; + c[0]=(char)(num+'0'); stringcat(str,c); } } diff --git a/src/c/timer.c b/src/c/timer.c index 390e512..412e443 100644 --- a/src/c/timer.c +++ b/src/c/timer.c @@ -1,15 +1,14 @@ +#include #include #include -#include - -void add_idt_entry(size_t num,uint32_t offset); +#include uint32_t tick=0; const uint32_t TICKS_PER_SECOND=50; extern uint32_t time; uint32_t time=0; -void timer_handler() +void timer_handler(void) { tick++; if(tick==TICKS_PER_SECOND) diff --git a/src/c/tty.c b/src/c/tty.c index 62ccd02..a05773f 100644 --- a/src/c/tty.c +++ b/src/c/tty.c @@ -1,11 +1,10 @@ +#include #include -#include -#include -#include +#include +#include +#include -#define CMD_LENGTH 20 -void clear(); extern uint32_t time; size_t pieces(char pieces[][CMD_LENGTH],char *buffer) @@ -48,7 +47,7 @@ void merge(char parts[][CMD_LENGTH]) printf("%s\n",str1); } -void ls() +void ls(void) { printf("Filesystem not implemented yet\n"); } @@ -63,12 +62,12 @@ void number(size_t numberof,char parts[][CMD_LENGTH]) } } -void uptime() +void uptime(void) { printf("System uptime is: %d seconds\n",time); } -void prompt() +void prompt(void) { //printf("[user@myos]$ "); set_color(VGA_COLOR_RED,VGA_COLOR_BLACK); @@ -85,7 +84,7 @@ void prompt() printf("$ "); } -void neofetch() +void neofetch(void) { set_color(VGA_COLOR_WHITE,VGA_COLOR_BLACK); printf(" . "); printf("Dobrodosli u moj \n"); @@ -120,7 +119,7 @@ void neofetch() uptime(); } -void help() +void help(void) { printf("Currently available commands:\n"); printf("clear echo merge ls number uptime neofetch help\n"); diff --git a/src/c/vga.c b/src/c/vga.c index 67c8b02..5294144 100644 --- a/src/c/vga.c +++ b/src/c/vga.c @@ -1,7 +1,7 @@ +#include #include -#include +#include #include -#include size_t terminal_row; size_t terminal_column; @@ -10,15 +10,16 @@ uint16_t* terminal_buffer; void set_color(enum vga_color fg, enum vga_color bg) { - terminal_color = fg | bg << 4; + terminal_color = (uint8_t)(fg|bg<<4); } -static inline uint16_t vga_entry(unsigned char uc, uint8_t color) +static inline uint16_t vga_entry(char uc, uint8_t color); +static inline uint16_t vga_entry(char uc, uint8_t color) { - return (uint16_t) uc | (uint16_t) color << 8; + return (uint16_t)(uc|color<<8); } -void terminal_initialize() +void terminal_initialize(void) { terminal_row=0; terminal_column=0; @@ -40,19 +41,19 @@ void terminal_putentryat(char c, uint8_t color, size_t x, size_t y) terminal_buffer[index]=vga_entry(c, color); } -void movescreen() +void movescreen(void) { terminal_row--; for(size_t i=0;i extern uint8_t ioport_in(uint8_t port); -extern void ioport_out(uint8_t port, char data); +extern void ioport_out(uint8_t port, int data); #endif diff --git a/src/include/heap.h b/src/include/heap.h deleted file mode 100644 index fa32f26..0000000 --- a/src/include/heap.h +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef HEAP_H -#define HEAP_H - -#include - -void kheapinit(); -int kheapaddblock(uintptr_t addr,uint32_t size,uint32_t bsize); -void *kmalloc(uint32_t size); -void kfree(void *ptr); - -#endif diff --git a/src/include/irq.h b/src/include/irq.h deleted file mode 100644 index 58b3cd3..0000000 --- a/src/include/irq.h +++ /dev/null @@ -1,39 +0,0 @@ -#ifndef IRQ_H -#define IRQ_H - -extern void irq0(); -extern void irq1(); -extern void irq2(); -extern void irq3(); -extern void irq4(); -extern void irq5(); -extern void irq6(); -extern void irq7(); -extern void irq8(); -extern void irq9(); -extern void irq10(); -extern void irq11(); -extern void irq12(); -extern void irq13(); -extern void irq14(); -extern void irq15(); -extern void irq16(); -extern void irq17(); -extern void irq18(); -extern void irq19(); -extern void irq20(); -extern void irq21(); -extern void irq22(); -extern void irq23(); -extern void irq24(); -extern void irq25(); -extern void irq26(); -extern void irq27(); -extern void irq28(); -extern void irq29(); -extern void irq30(); -extern void irq31(); -extern void timer_irq(); -extern void keyboard_irq(); - -#endif diff --git a/src/include/source/gdt.h b/src/include/source/gdt.h new file mode 100644 index 0000000..7ed587c --- /dev/null +++ b/src/include/source/gdt.h @@ -0,0 +1,25 @@ +#ifndef SOURCE_GDT_H +#define SOURCE_GDT_H + +#include + +struct gdt_entry +{ + uint16_t limit; + uint16_t base1; + uint8_t base2; + uint8_t access; + uint8_t limit_flags; + uint8_t base3; +} __attribute__((packed)); + +struct gdt_pointer +{ + uint16_t size; + uint32_t offset; +} __attribute__((packed)); + +void init_gdt_entry(size_t num, uint16_t limit, uint32_t base, uint8_t access, uint8_t limit_flags); +void init_gdt_table(void); + +#endif diff --git a/src/include/source/heap.h b/src/include/source/heap.h new file mode 100644 index 0000000..15cba98 --- /dev/null +++ b/src/include/source/heap.h @@ -0,0 +1,30 @@ +#ifndef SOURCE_HEAP_H +#define SOURCE_HEAP_H + +#include + +typedef struct _KHEAPBLOCKBM { + struct _KHEAPBLOCKBM *next; + uint32_t size; + uint32_t used; + uint32_t bsize; + uint32_t lfb; +} KHEAPBLOCKBM; + +typedef struct _KHEAPBM { + KHEAPBLOCKBM *fblock; +} KHEAPBM; + +extern KHEAPBM kheap; + +void k_heapBMInit(KHEAPBM *heap); +int k_heapBMAddBlock(KHEAPBM *heap, uintptr_t addr, uint32_t size, uint32_t bsize); +void *k_heapBMAlloc(KHEAPBM *heap, uint32_t size); +void k_heapBMFree(KHEAPBM *heap, void *ptr); + +void kheapinit(void); +int kheapaddblock(uintptr_t addr,uint32_t size,uint32_t bsize); +void *kmalloc(uint32_t size); +void kfree(void *ptr); + +#endif diff --git a/src/include/source/idt.h b/src/include/source/idt.h new file mode 100644 index 0000000..b150d40 --- /dev/null +++ b/src/include/source/idt.h @@ -0,0 +1,36 @@ +#ifndef SOURCE_IDT_H +#define SOURCE_IDT_H + +#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 + +struct idt_entry +{ + uint16_t offset1; + uint16_t selector; + uint8_t zero; + uint8_t type_attr; + uint16_t offset2; +} __attribute__((packed)); + +struct idt_pointer +{ + uint16_t size; + uint32_t offset; +} __attribute__((packed)); + +void init_idt_entry(size_t num, uint32_t offset, uint16_t selector, uint8_t type_attr); +void add_idt_entry(size_t num,uint32_t offset); +void init_pic(void); +void init_idt_table(void); + +#endif diff --git a/src/include/source/irq.h b/src/include/source/irq.h new file mode 100644 index 0000000..febf3e1 --- /dev/null +++ b/src/include/source/irq.h @@ -0,0 +1,39 @@ +#ifndef SOURCE_IRQ_H +#define SOURCE_IRQ_H + +extern void irq0(void); +extern void irq1(void); +extern void irq2(void); +extern void irq3(void); +extern void irq4(void); +extern void irq5(void); +extern void irq6(void); +extern void irq7(void); +extern void irq8(void); +extern void irq9(void); +extern void irq10(void); +extern void irq11(void); +extern void irq12(void); +extern void irq13(void); +extern void irq14(void); +extern void irq15(void); +extern void irq16(void); +extern void irq17(void); +extern void irq18(void); +extern void irq19(void); +extern void irq20(void); +extern void irq21(void); +extern void irq22(void); +extern void irq23(void); +extern void irq24(void); +extern void irq25(void); +extern void irq26(void); +extern void irq27(void); +extern void irq28(void); +extern void irq29(void); +extern void irq30(void); +extern void irq31(void); +extern void timer_irq(void); +extern void keyboard_irq(void); + +#endif diff --git a/src/include/source/irq_handler.h b/src/include/source/irq_handler.h new file mode 100644 index 0000000..41b4f8c --- /dev/null +++ b/src/include/source/irq_handler.h @@ -0,0 +1,37 @@ +#ifndef SOURCE_IRQ_HANDLER_H +#define SOURCE_IRQ_HANDLER_H + +void irq0_handler(void); +void irq1_handler(void); +void irq2_handler(void); +void irq3_handler(void); +void irq4_handler(void); +void irq5_handler(void); +void irq6_handler(void); +void irq7_handler(void); +void irq8_handler(void); +void irq9_handler(void); +void irq10_handler(void); +void irq11_handler(void); +void irq12_handler(void); +void irq13_handler(void); +void irq14_handler(void); +void irq15_handler(void); +void irq16_handler(void); +void irq17_handler(void); +void irq18_handler(void); +void irq19_handler(void); +void irq20_handler(void); +void irq21_handler(void); +void irq22_handler(void); +void irq23_handler(void); +void irq24_handler(void); +void irq25_handler(void); +void irq26_handler(void); +void irq27_handler(void); +void irq28_handler(void); +void irq29_handler(void); +void irq30_handler(void); +void irq31_handler(void); + +#endif diff --git a/src/include/source/kernel.h b/src/include/source/kernel.h new file mode 100644 index 0000000..0a3903f --- /dev/null +++ b/src/include/source/kernel.h @@ -0,0 +1,6 @@ +#ifndef SOURCE_KERNEL_H +#define SOURCE_KERNEL_H + +void kernel_main(void); + +#endif diff --git a/src/include/source/keyboard.h b/src/include/source/keyboard.h new file mode 100644 index 0000000..28fa104 --- /dev/null +++ b/src/include/source/keyboard.h @@ -0,0 +1,15 @@ +#ifndef SOURCE_KEYBOARD_H +#define SOURCE_KEYBOARD_H + +void init_keyboard(void); +void deletelast(void); +void backspace(void); +void enter(void); +void space(void); +void keyup(void); +void keydown(void); +void keyleft(void); +void keyright(void); +void keyboard_handler(void); + +#endif diff --git a/src/include/source/keymap.h b/src/include/source/keymap.h new file mode 100644 index 0000000..1c474ae --- /dev/null +++ b/src/include/source/keymap.h @@ -0,0 +1,9 @@ +#ifndef SOURCE_KEYMAP_H +#define SOURCE_KEYMAP_H + +#include + +void us_en(char keymap[]); +void us_en_shift(char keymap[]); + +#endif diff --git a/src/include/source/paging.h b/src/include/source/paging.h new file mode 100644 index 0000000..dc4e357 --- /dev/null +++ b/src/include/source/paging.h @@ -0,0 +1,10 @@ +#ifndef SOURCE_PAGING_H +#define SOURCE_PAGING_H + +#include + +void set_pd(void); +void set_pt(size_t num, uint32_t address); +void set_paging(void); + +#endif diff --git a/src/include/source/stack_protector.h b/src/include/source/stack_protector.h new file mode 100644 index 0000000..d5532e2 --- /dev/null +++ b/src/include/source/stack_protector.h @@ -0,0 +1,6 @@ +#ifndef SOURCE_STACK_PROTECTOR_H +#define SOURCE_STACK_PROTECTOR_H + +void __stack_chk_fail(void); + +#endif diff --git a/src/include/source/stdio.h b/src/include/source/stdio.h new file mode 100644 index 0000000..dbf901a --- /dev/null +++ b/src/include/source/stdio.h @@ -0,0 +1,6 @@ +#ifndef SOURCE_STDIO_H +#define SOURCE_STDIO_H + +void printf(const char *str, ...); + +#endif diff --git a/src/include/source/string.h b/src/include/source/string.h new file mode 100644 index 0000000..98aef4f --- /dev/null +++ b/src/include/source/string.h @@ -0,0 +1,17 @@ +#ifndef SOURCE_STRING_H +#define SOURCE_STRING_H + +#include + +size_t stringlen(char* str); +bool stringcmp(const char* str1, const char* str2); +void stringcat(char* str1, const char* str2); +void stringcpy(char* str1, const char* str2); +void stringrev(char* str); + +void itos(uint32_t num, char *str); +uint32_t stoi(const char* str); +double stof(const char* str); +void ftos(double num, char* str); + +#endif diff --git a/src/include/source/timer.h b/src/include/source/timer.h new file mode 100644 index 0000000..bfdc4cd --- /dev/null +++ b/src/include/source/timer.h @@ -0,0 +1,9 @@ +#ifndef SOURCE_TIMER_H +#define SOURCE_TIMER_H + +#include + +void timer_handler(void); +void init_timer(uint32_t frequency); + +#endif diff --git a/src/include/source/tty.h b/src/include/source/tty.h new file mode 100644 index 0000000..2b22eff --- /dev/null +++ b/src/include/source/tty.h @@ -0,0 +1,19 @@ +#ifndef SOURCE_TTY_H +#define SOURCE_TTY_H + +#include + +#define CMD_LENGTH 20 + +size_t pieces(char pieces[][CMD_LENGTH], char *buffer); +void echo(size_t numberof, char parts[][CMD_LENGTH]); +void merge(char parts[][CMD_LENGTH]); +void ls(void); +void number(size_t numberof, char parts[][CMD_LENGTH]); +void uptime(void); +void prompt(void); +void neofetch(void); +void help(void); +void tty(char *buffer); + +#endif diff --git a/src/include/source/vga.h b/src/include/source/vga.h new file mode 100644 index 0000000..69abc44 --- /dev/null +++ b/src/include/source/vga.h @@ -0,0 +1,40 @@ +#ifndef SOURCE_VGA_H +#define SOURCE_VGA_H + +#include + +static const size_t VGA_WIDTH = 80; +static const size_t VGA_HEIGHT = 25; + +enum vga_color { + VGA_COLOR_BLACK = 0, + VGA_COLOR_DARK_BLUE = 1, + VGA_COLOR_GREEN = 2, + VGA_COLOR_TURQUOISE = 3, + VGA_COLOR_RED = 4, + VGA_COLOR_PURPLE = 5, + VGA_COLOR_BROWN = 6, + VGA_COLOR_LIGHT_GREY = 7, + VGA_COLOR_DARK_GREY = 8, + VGA_COLOR_BLUE = 9, + VGA_COLOR_LIGHT_GREEN = 10, + VGA_COLOR_LIGHT_BLUE = 11, + VGA_COLOR_LIGHT_RED = 12, + VGA_COLOR_PINK = 13, + VGA_COLOR_YELLOW = 14, + VGA_COLOR_WHITE = 15, +}; + +void set_color(enum vga_color fg, enum vga_color bg); +void terminal_initialize(void); +void terminal_putentryat(char c, uint8_t color, size_t x, size_t y); +void movescreen(void); +void next_field(void); +void previous_field(void); +void terminal_putchar(char c); +void terminal_writestring(const char* data); +void terminal_writeint(uint32_t num); +void terminal_writefloat(double num); +void clear(void); + +#endif diff --git a/src/include/stdio.h b/src/include/stdio.h index 6bb87be..88f18cc 100644 --- a/src/include/stdio.h +++ b/src/include/stdio.h @@ -20,6 +20,4 @@ size_t fwrite(const void*, size_t, size_t, FILE*); void setbuf(FILE*, char*); int vfprintf(FILE*, const char*, va_list); -void printf(char *str, ...); - #endif diff --git a/src/include/string.h b/src/include/string.h index 78fa956..16da969 100644 --- a/src/include/string.h +++ b/src/include/string.h @@ -8,15 +8,4 @@ void* memset(void*, int, size_t); char* strcpy(char*, const char*); size_t strlen(const char*); -#include - -size_t stringlen(char *str); -bool stringcmp(char *str1,char *str2); -void stringcat(char *str1,char *str2); -void stringrev(char *str); -void itos(uint32_t num,char *str); -uint32_t stoi(const char *str); -double stof(const char *str); -void ftos(double num, char *str); - #endif diff --git a/src/include/types.h b/src/include/types.h index a6d6530..391284d 100644 --- a/src/include/types.h +++ b/src/include/types.h @@ -1,5 +1,5 @@ -#ifndef TYPES_H -#define TYPES_H +#ifndef SOURCE_TYPES_H +#define SOURCE_TYPES_H #include #include diff --git a/src/include/vga.h b/src/include/vga.h deleted file mode 100644 index eb43644..0000000 --- a/src/include/vga.h +++ /dev/null @@ -1,29 +0,0 @@ -#ifndef VGA_H -#define VGA_H - -#include - -static const size_t VGA_WIDTH = 80; -static const size_t VGA_HEIGHT = 25; - -enum vga_color { - VGA_COLOR_BLACK = 0, - VGA_COLOR_DARK_BLUE = 1, - VGA_COLOR_GREEN = 2, - VGA_COLOR_TURQUOISE = 3, - VGA_COLOR_RED = 4, - VGA_COLOR_PURPLE = 5, - VGA_COLOR_BROWN = 6, - VGA_COLOR_LIGHT_GREY = 7, - VGA_COLOR_DARK_GREY = 8, - VGA_COLOR_BLUE = 9, - VGA_COLOR_LIGHT_GREEN = 10, - VGA_COLOR_LIGHT_BLUE = 11, - VGA_COLOR_LIGHT_RED = 12, - VGA_COLOR_PINK = 13, - VGA_COLOR_YELLOW = 14, - VGA_COLOR_WHITE = 15, -}; -void set_color(enum vga_color fg, enum vga_color bg); - -#endif -- cgit v1.2.3