From 8e79ceaa7d3995df5a5dcf0ffdbd52ebe4c52163 Mon Sep 17 00:00:00 2001 From: Aleksa Vučković Date: Sun, 9 Jan 2022 18:50:48 +0100 Subject: Indentation --- src/c/gdt.c | 10 +- src/c/heap.c | 265 ++++++++++--------- src/c/idt.c | 3 +- src/c/keyboard.c | 150 ++++++----- src/c/keymap.c | 680 ++++++++++++++++++++++++------------------------ src/c/paging.c | 18 +- src/c/stack_protector.c | 14 +- src/c/stdio.c | 28 +- src/c/string.c | 48 ++-- src/c/timer.c | 6 +- src/c/tty.c | 70 ++--- src/c/vga.c | 25 +- 12 files changed, 663 insertions(+), 654 deletions(-) (limited to 'src/c') diff --git a/src/c/gdt.c b/src/c/gdt.c index a9a745c..e3d2b4c 100644 --- a/src/c/gdt.c +++ b/src/c/gdt.c @@ -36,11 +36,11 @@ void init_gdt_table() 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,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 load_gdt(&gdtp); } diff --git a/src/c/heap.c b/src/c/heap.c index 3e1ba24..99d0a37 100644 --- a/src/c/heap.c +++ b/src/c/heap.c @@ -1,151 +1,146 @@ #include typedef struct _KHEAPBLOCKBM { - struct _KHEAPBLOCKBM *next; - uint32_t size; - uint32_t used; - uint32_t bsize; - uint32_t lfb; + struct _KHEAPBLOCKBM *next; + uint32_t size; + uint32_t used; + uint32_t bsize; + uint32_t lfb; } KHEAPBLOCKBM; - + typedef struct _KHEAPBM { - KHEAPBLOCKBM *fblock; + KHEAPBLOCKBM *fblock; } KHEAPBM; - + void k_heapBMInit(KHEAPBM *heap) { - heap->fblock = 0; + heap->fblock = 0; } - + int k_heapBMAddBlock(KHEAPBM *heap, uintptr_t addr, uint32_t size, uint32_t bsize) { - KHEAPBLOCKBM *b; - uint32_t bcnt; - uint32_t x; - uint8_t *bm; - - b = (KHEAPBLOCKBM*)addr; - b->size = size - sizeof(KHEAPBLOCKBM); - b->bsize = bsize; - - b->next = heap->fblock; - heap->fblock = b; - - bcnt = b->size / b->bsize; - bm = (uint8_t*)&b[1]; - - /* clear bitmap */ - for (x = 0; x < bcnt; ++x) { - bm[x] = 0; - } - - /* reserve room for bitmap */ - bcnt = (bcnt / bsize) * bsize < bcnt ? bcnt / bsize + 1 : bcnt / bsize; - for (x = 0; x < bcnt; ++x) { - bm[x] = 5; - } - - b->lfb = bcnt - 1; - - b->used = bcnt; - - return 1; + KHEAPBLOCKBM *b; + uint32_t bcnt; + uint32_t x; + uint8_t *bm; + + b = (KHEAPBLOCKBM*)addr; + b->size = size - sizeof(KHEAPBLOCKBM); + b->bsize = bsize; + + b->next = heap->fblock; + heap->fblock = b; + + bcnt = b->size / b->bsize; + bm = (uint8_t*)&b[1]; + + /* clear bitmap */ + for (x = 0; x < bcnt; ++x) { + bm[x] = 0; + } + + /* reserve room for bitmap */ + bcnt = (bcnt / bsize) * bsize < bcnt ? bcnt / bsize + 1 : bcnt / bsize; + for (x = 0; x < bcnt; ++x) { + bm[x] = 5; + } + + b->lfb = bcnt - 1; + b->used = bcnt; + return 1; } - + static uint8_t k_heapBMGetNID(uint8_t a, uint8_t b) { - uint8_t c; - for (c = a + 1; c == b || c == 0; ++c); - return c; + uint8_t c; + for (c = a + 1; c == b || c == 0; ++c); + return c; } - + void *k_heapBMAlloc(KHEAPBM *heap, uint32_t size) { - KHEAPBLOCKBM *b; - uint8_t *bm; - uint32_t bcnt; - uint32_t x, y, z; - uint32_t bneed; - uint8_t nid; - - /* iterate blocks */ - for (b = heap->fblock; b; b = b->next) { - //printf("size:%d,used:%d,bsize:%d,lfb:%d\n",b->size,b->used,b->bsize,b->lfb); - /* check if block has enough room */ - 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]; - //printf("bcnt:%d,bneed:%d,bm:%d\n",bcnt,bneed,bm); - - for (x = (b->lfb + 1 >= bcnt ? 0 : b->lfb + 1); x != b->lfb; ++x) { - /* just wrap around */ - if (x >= bcnt) { - 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) { - /* 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; - } - - /* optimization */ - b->lfb = (x + bneed) - 2; - - /* count used blocks NOT bytes */ - b->used += y; - - return (void*)(x * b->bsize + (uintptr_t)&b[1]); - } - - /* x will be incremented by one ONCE more in our FOR loop */ - x += (y - 1); - continue; - } - } - } - } - - return 0; + KHEAPBLOCKBM *b; + uint8_t *bm; + uint32_t bcnt; + uint32_t x, y, z; + uint32_t bneed; + uint8_t nid; + + /* iterate blocks */ + for (b = heap->fblock; b; b = b->next) { + /* check if block has enough room */ + 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) { + /* just wrap around */ + if (x >= bcnt) { + 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) { + /* 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; + } + + /* optimization */ + b->lfb = (x + bneed) - 2; + + /* count used blocks NOT bytes */ + b->used += y; + + return (void*)(x * b->bsize + (uintptr_t)&b[1]); + } + + /* x will be incremented by one ONCE more in our FOR loop */ + x += (y - 1); + continue; + } + } + } + } + return 0; } - + void k_heapBMFree(KHEAPBM *heap, void *ptr) { - KHEAPBLOCKBM *b; - uintptr_t ptroff; - uint32_t bi, x; - uint8_t *bm; - uint8_t id; - uint32_t max; - - 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 */ - /* block offset in BM */ - bi = ptroff / b->bsize; - /* .. */ - bm = (uint8_t*)&b[1]; - /* clear allocation */ - 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; - } - /* update free block count */ - b->used -= x - bi; - return; - } - } - - /* this error needs to be raised or reported somehow */ - return; + KHEAPBLOCKBM *b; + uintptr_t ptroff; + uint32_t bi, x; + uint8_t *bm; + uint8_t id; + uint32_t max; + + 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 */ + /* block offset in BM */ + bi = ptroff / b->bsize; + /* .. */ + bm = (uint8_t*)&b[1]; + /* clear allocation */ + 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; + } + /* update free block count */ + b->used -= x - bi; + return; + } + } + + /* this error needs to be raised or reported somehow */ + return; } KHEAPBM kheap; @@ -154,15 +149,17 @@ void kheapinit() { k_heapBMInit(&kheap); } + int kheapaddblock(uintptr_t addr,uint32_t size,uint32_t bsize) { return k_heapBMAddBlock(&kheap,addr,size,bsize); } + void *kmalloc(uint32_t size) { return k_heapBMAlloc(&kheap,size); - } + void kfree(void *ptr) { k_heapBMFree(&kheap,ptr); diff --git a/src/c/idt.c b/src/c/idt.c index 005a05c..843c9da 100644 --- a/src/c/idt.c +++ b/src/c/idt.c @@ -42,6 +42,7 @@ void init_idt_entry(size_t num, uint32_t offset, uint16_t selector, uint8_t type idt[num].type_attr=type_attr; idt[num].offset2=(offset & 0xffff0000)>>16; } + void add_idt_entry(size_t num,uint32_t offset) { init_idt_entry(num,offset,KERNEL_CODE,INTERRUPT_GATE_32); @@ -59,7 +60,6 @@ void init_pic() ioport_out(PIC2_DATA_PORT, 0x01); ioport_out(PIC1_DATA_PORT, 0xff); ioport_out(PIC2_DATA_PORT, 0xff); - ioport_out(PIC1_DATA_PORT, 0xFC); } @@ -104,6 +104,5 @@ void init_idt_table() idtp.size=sizeof(struct idt_entry)*256-1; idtp.offset=(uint32_t)&idt; - load_idt(&idtp); } diff --git a/src/c/keyboard.c b/src/c/keyboard.c index d082ec5..2218f09 100644 --- a/src/c/keyboard.c +++ b/src/c/keyboard.c @@ -28,6 +28,10 @@ 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() { @@ -56,15 +60,15 @@ void enter() printf("\n"); if(buffer_index>0) { - tty(buffer[buffer_current]); - buffer_size[buffer_current]=buffer_index; - if(buffer_current==buffer_all) buffer_current=(++buffer_all); - else - { - for(size_t i=0;i0) { - buffer_size[buffer_current]=buffer_index; - for(size_t i=0;i #include - + #if UINT32_MAX == UINTPTR_MAX #define STACK_CHK_GUARD 0xe2dee396 #else #define STACK_CHK_GUARD 0x595e9fbd94fda766 #endif - + uintptr_t __stack_chk_guard = STACK_CHK_GUARD; - -//__attribute__((noreturn)) void __stack_chk_fail(void) { -#if __STDC_HOSTED__ - printf("Stack smashing detected\n"); - abort(); -#elif __is_myos_kernel - printf("Stack smashing detected\n"); - panic("Stack smashing detected"); -#endif + printf("Stack smashing detected\n"); } diff --git a/src/c/stdio.c b/src/c/stdio.c index 36fd140..3a51e9a 100644 --- a/src/c/stdio.c +++ b/src/c/stdio.c @@ -17,20 +17,20 @@ void printf(char *str, ...) for(size_t i=0;str[i]!='\0';i++) { - if(str[i]=='%') - { - 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]=='f') terminal_writefloat(va_arg(list,double)); - else - { - terminal_writestring("wrong format using print function\n"); - return; - } - } - else terminal_putchar(str[i]); + if(str[i]=='%') + { + 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]=='f') terminal_writefloat(va_arg(list,double)); + else + { + terminal_writestring("wrong format using print function\n"); + return; + } + } + else terminal_putchar(str[i]); } va_end(list); diff --git a/src/c/string.c b/src/c/string.c index 6a3a96b..ce33528 100644 --- a/src/c/string.c +++ b/src/c/string.c @@ -36,9 +36,9 @@ void stringrev(char *str) for(size_t j=0;j'9') return num; - num*=10; - num+=str[i]-'0'; + if(str[i]<'0'||str[i]>'9') return num; + num*=10; + num+=str[i]-'0'; } return num; @@ -61,11 +61,11 @@ 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; - str[i]='\0'; + size_t i=0; + for(;num>0;num/=10,i++) str[i]='0'+num%10; + str[i]='\0'; - stringrev(str); + stringrev(str); } } @@ -79,16 +79,16 @@ double stof(const char *str) for(;str[i]!='\0';i++) { - if(str[i]=='.') - { - if(point) return num/pow; - point=1; - continue; - } - if(str[i]<'0'||str[i]>'9') return num/pow; - num*=10; - num+=str[i]-'0'; - if(point) pow*=10; + if(str[i]=='.') + { + if(point) return num/pow; + point=1; + continue; + } + if(str[i]<'0'||str[i]>'9') return num/pow; + num*=10; + num+=str[i]-'0'; + if(point) pow*=10; } return num/pow; @@ -105,9 +105,9 @@ void ftos(double num, char *str) for(size_t i=0;i