From 353647418912b5b6b94473b15bee312ddc4d64a9 Mon Sep 17 00:00:00 2001 From: dzwdz Date: Wed, 25 Jan 2023 22:05:35 +0100 Subject: kernel: move /mem/alloc to /malloc and linker.ld to arch/amd64/ --- src/kernel/arch/amd64/boot.c | 2 +- src/kernel/arch/amd64/linker.ld | 42 ++++++++++ src/kernel/arch/amd64/pagedir.c | 2 +- src/kernel/execbuf.c | 2 +- src/kernel/handle.c | 2 +- src/kernel/linker.ld | 42 ---------- src/kernel/malloc.c | 177 ++++++++++++++++++++++++++++++++++++++++ src/kernel/malloc.h | 29 +++++++ src/kernel/mem/alloc.c | 177 ---------------------------------------- src/kernel/mem/alloc.h | 29 ------- src/kernel/proc.c | 2 +- src/kernel/ring.c | 0 src/kernel/syscalls.c | 2 +- src/kernel/vfs/mount.c | 2 +- src/kernel/vfs/procfs.c | 2 +- src/kernel/vfs/request.c | 2 +- 16 files changed, 257 insertions(+), 257 deletions(-) create mode 100644 src/kernel/arch/amd64/linker.ld delete mode 100644 src/kernel/linker.ld create mode 100644 src/kernel/malloc.c create mode 100644 src/kernel/malloc.h delete mode 100644 src/kernel/mem/alloc.c delete mode 100644 src/kernel/mem/alloc.h delete mode 100644 src/kernel/ring.c (limited to 'src') diff --git a/src/kernel/arch/amd64/boot.c b/src/kernel/arch/amd64/boot.c index c645fb6..337d23b 100644 --- a/src/kernel/arch/amd64/boot.c +++ b/src/kernel/arch/amd64/boot.c @@ -6,7 +6,7 @@ #include #include #include -#include +#include #include #include diff --git a/src/kernel/arch/amd64/linker.ld b/src/kernel/arch/amd64/linker.ld new file mode 100644 index 0000000..a910d4e --- /dev/null +++ b/src/kernel/arch/amd64/linker.ld @@ -0,0 +1,42 @@ +ENTRY(_start) + +SECTIONS +{ + . = 0; + .shared BLOCK(4K) : ALIGN(4K) + { + *(.multiboot) + *(.shared) + _shared_len = .; + } + + . = 1M; + .text BLOCK(4K) : ALIGN(4K) + { + _kern_start = .; + *(.text) + } + .rodata BLOCK(4K) : ALIGN(4K) + { + *(.rodata) + } + .data BLOCK(4K) : ALIGN(4K) + { + *(.data) + } + _data_end = .; + .bss BLOCK(4K) : ALIGN(4K) + { + *(COMMON) + *(.bss) + + . += 16K; + _isr_big_stack = .; + + . += 16K; + _stack_top = .; + } + _bss_end = (. + 0xFFF) & ~0xFFF; /* aligned to 4K */ + pbitmap = _bss_end; + pbitmap_start = _bss_end; +} diff --git a/src/kernel/arch/amd64/pagedir.c b/src/kernel/arch/amd64/pagedir.c index 9250a01..d56a548 100644 --- a/src/kernel/arch/amd64/pagedir.c +++ b/src/kernel/arch/amd64/pagedir.c @@ -1,6 +1,6 @@ #include #include -#include +#include #include #include #include diff --git a/src/kernel/execbuf.c b/src/kernel/execbuf.c index 8f7e22e..33bb94a 100644 --- a/src/kernel/execbuf.c +++ b/src/kernel/execbuf.c @@ -1,7 +1,7 @@ #include #include #include -#include +#include #include #include diff --git a/src/kernel/handle.c b/src/kernel/handle.c index 156be12..51a95be 100644 --- a/src/kernel/handle.c +++ b/src/kernel/handle.c @@ -1,5 +1,5 @@ #include -#include +#include #include #include #include diff --git a/src/kernel/linker.ld b/src/kernel/linker.ld deleted file mode 100644 index a910d4e..0000000 --- a/src/kernel/linker.ld +++ /dev/null @@ -1,42 +0,0 @@ -ENTRY(_start) - -SECTIONS -{ - . = 0; - .shared BLOCK(4K) : ALIGN(4K) - { - *(.multiboot) - *(.shared) - _shared_len = .; - } - - . = 1M; - .text BLOCK(4K) : ALIGN(4K) - { - _kern_start = .; - *(.text) - } - .rodata BLOCK(4K) : ALIGN(4K) - { - *(.rodata) - } - .data BLOCK(4K) : ALIGN(4K) - { - *(.data) - } - _data_end = .; - .bss BLOCK(4K) : ALIGN(4K) - { - *(COMMON) - *(.bss) - - . += 16K; - _isr_big_stack = .; - - . += 16K; - _stack_top = .; - } - _bss_end = (. + 0xFFF) & ~0xFFF; /* aligned to 4K */ - pbitmap = _bss_end; - pbitmap_start = _bss_end; -} diff --git a/src/kernel/malloc.c b/src/kernel/malloc.c new file mode 100644 index 0000000..8c52b6f --- /dev/null +++ b/src/kernel/malloc.c @@ -0,0 +1,177 @@ +#include +#include +#include +#include +#include +#include +#include + +#define MALLOC_MAGIC 0xA770C666 + +struct malloc_hdr { + uint32_t magic; + uint32_t page_amt; + struct malloc_hdr *next, *prev; + void *stacktrace[4]; +}; + +struct malloc_hdr *malloc_last = NULL; + +extern uint8_t pbitmap[], pbitmap_start[]; /* linker.ld */ +static size_t pbitmap_len; +static size_t pbitmap_searchstart = 0; +static size_t pbitmap_taken = 0; + + +static bool bitmap_get(size_t i) { + size_t b = i / 8; + assert(b < pbitmap_len); + return 0 < (pbitmap[b] & (1 << (i&7))); +} + +static void bitmap_set(size_t i, bool v) { + size_t b = i / 8; + uint8_t m = 1 << (i&7); + assert(b < pbitmap_len); + if ((pbitmap[b] & m) ^ v) { + /* value changes */ + if (v) pbitmap_taken++; + else pbitmap_taken--; + } + if (v) pbitmap[b] |= m; + else pbitmap[b] &= ~m; +} + +void mem_init(void *memtop) { + kprintf("memory %8x -> %8x\n", &_bss_end, memtop); + size_t pageamt = ((uintptr_t)memtop - (uintptr_t)pbitmap_start) / PAGE_SIZE; + pbitmap_len = pageamt / 8; + memset(pbitmap, 0, pbitmap_len); + mem_reserve(pbitmap, pbitmap_len); +} + +void mem_reserve(void *addr, size_t len) { + kprintf("reserved %8x -> %8x\n", addr, addr + len); + + /* align to the previous page */ + size_t off = (uintptr_t)addr & PAGE_MASK; + addr -= off; + len += off; + size_t first = ((uintptr_t)addr - (uintptr_t)pbitmap_start) / PAGE_SIZE; + for (size_t i = 0; i * PAGE_SIZE < len; i++) { + if (first + i >= pbitmap_len) break; + if (bitmap_get(first + i)) + panic_invalid_state(); + bitmap_set(first + i, true); + } +} + +void mem_debugprint(void) { + size_t total = 0; + kprintf("current kmallocs:\n"); + kprintf("addr pages\n"); + for (struct malloc_hdr *iter = malloc_last; iter; iter = iter->prev) { + kprintf("%08x %05x", iter, iter->page_amt); + for (size_t i = 0; i < 4; i++) + kprintf(" k/%08x", iter->stacktrace[i]); + kprintf("\n peek 0x%x\n", *(uint32_t*)(&iter[1])); + total++; + } + kprintf(" total 0x%x\n", total); + kprintf("pbitmap usage %u/%u\n", pbitmap_taken, pbitmap_len * 8); +} + +void *page_alloc(size_t pages) { + /* i do realize how painfully slow this is */ + size_t streak = 0; + for (size_t i = pbitmap_searchstart; i < pbitmap_len * 8; i++) { + if (bitmap_get(i)) { + streak = 0; + continue; + } + if (++streak >= pages) { + /* found hole big enough for this allocation */ + i = i + 1 - streak; + for (size_t j = 0; j < streak; j++) + bitmap_set(i + j, true); + pbitmap_searchstart = i + streak - 1; + return pbitmap_start + i * PAGE_SIZE; + } + } + kprintf("we ran out of memory :(\ngoodbye.\n"); + panic_unimplemented(); +} + +void *page_zalloc(size_t pages) { + void *p = page_alloc(pages); + memset(p, 0, pages * PAGE_SIZE); + return p; +} + +// frees `pages` consecutive pages starting from *first +void page_free(void *first_addr, size_t pages) { + assert(first_addr >= (void*)pbitmap_start); + size_t first = ((uintptr_t)first_addr - (uintptr_t)pbitmap_start) / PAGE_SIZE; + if (pbitmap_searchstart > first) + pbitmap_searchstart = first; + for (size_t i = 0; i < pages; i++) { + assert(bitmap_get(first + i)); + bitmap_set(first + i, false); + } +} + +void kmalloc_sanity(const void *addr) { + assert(addr); + const struct malloc_hdr *hdr = addr - sizeof(struct malloc_hdr); + assert(hdr->magic == MALLOC_MAGIC); + if (hdr->next) assert(hdr->next->prev == hdr); + if (hdr->prev) assert(hdr->prev->next == hdr); +} + +void *kmalloc(size_t len) { + // TODO better kmalloc + + struct malloc_hdr *hdr; + void *addr; + uint32_t pages; + + len += sizeof(struct malloc_hdr); + pages = len / PAGE_SIZE + 1; + + hdr = page_alloc(pages); + hdr->magic = MALLOC_MAGIC; + hdr->page_amt = pages; + + hdr->next = NULL; + hdr->prev = malloc_last; + if (hdr->prev) { + assert(!hdr->prev->next); + hdr->prev->next = hdr; + } + + for (size_t i = 0; i < 4; i++) + hdr->stacktrace[i] = debug_caller(i); + + malloc_last = hdr; + + addr = (void*)hdr + sizeof(struct malloc_hdr); + kmalloc_sanity(addr); + return addr; +} + +void kfree(void *ptr) { + struct malloc_hdr *hdr; + if (ptr == NULL) return; + + hdr = ptr - sizeof(struct malloc_hdr); + kmalloc_sanity(ptr); + + hdr->magic = ~MALLOC_MAGIC; // (hopefully) detect double frees + if (hdr->next) + hdr->next->prev = hdr->prev; + if (hdr->prev) + hdr->prev->next = hdr->next; + if (malloc_last == hdr) + malloc_last = hdr->prev; + page_free(hdr, hdr->page_amt); +} diff --git a/src/kernel/malloc.h b/src/kernel/malloc.h new file mode 100644 index 0000000..671a468 --- /dev/null +++ b/src/kernel/malloc.h @@ -0,0 +1,29 @@ +#pragma once +#include +#include +#include + +extern struct malloc_hdr *malloc_last; + +void mem_init(void *memtop); +void mem_reserve(void *addr, size_t len); +void mem_debugprint(void); + +// allocates `pages` consecutive pages +// TODO deprecate +void *page_alloc(size_t pages); +// zeroes the allocated pages +void *page_zalloc(size_t pages); + +// frees `pages` consecutive pages starting from *first +void page_free(void *first, size_t pages); + +void kmalloc_sanity(const void *addr); +void *kmalloc(size_t len); +void kfree(void *ptr); + +static inline void *kzalloc(size_t len) { + void *b = kmalloc(len); + memset(b, 0, len); + return b; +} diff --git a/src/kernel/mem/alloc.c b/src/kernel/mem/alloc.c deleted file mode 100644 index a8e5aed..0000000 --- a/src/kernel/mem/alloc.c +++ /dev/null @@ -1,177 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include - -#define MALLOC_MAGIC 0xACAB1312 - -struct malloc_hdr { - uint32_t magic; - uint32_t page_amt; - struct malloc_hdr *next, *prev; - void *stacktrace[4]; -}; - -struct malloc_hdr *malloc_last = NULL; - -extern uint8_t pbitmap[], pbitmap_start[]; /* linker.ld */ -static size_t pbitmap_len; -static size_t pbitmap_searchstart = 0; -static size_t pbitmap_taken = 0; - - -static bool bitmap_get(size_t i) { - size_t b = i / 8; - assert(b < pbitmap_len); - return 0 < (pbitmap[b] & (1 << (i&7))); -} - -static void bitmap_set(size_t i, bool v) { - size_t b = i / 8; - uint8_t m = 1 << (i&7); - assert(b < pbitmap_len); - if ((pbitmap[b] & m) ^ v) { - /* value changes */ - if (v) pbitmap_taken++; - else pbitmap_taken--; - } - if (v) pbitmap[b] |= m; - else pbitmap[b] &= ~m; -} - -void mem_init(void *memtop) { - kprintf("memory %8x -> %8x\n", &_bss_end, memtop); - size_t pageamt = ((uintptr_t)memtop - (uintptr_t)pbitmap_start) / PAGE_SIZE; - pbitmap_len = pageamt / 8; - memset(pbitmap, 0, pbitmap_len); - mem_reserve(pbitmap, pbitmap_len); -} - -void mem_reserve(void *addr, size_t len) { - kprintf("reserved %8x -> %8x\n", addr, addr + len); - - /* align to the previous page */ - size_t off = (uintptr_t)addr & PAGE_MASK; - addr -= off; - len += off; - size_t first = ((uintptr_t)addr - (uintptr_t)pbitmap_start) / PAGE_SIZE; - for (size_t i = 0; i * PAGE_SIZE < len; i++) { - if (first + i >= pbitmap_len) break; - if (bitmap_get(first + i)) - panic_invalid_state(); - bitmap_set(first + i, true); - } -} - -void mem_debugprint(void) { - size_t total = 0; - kprintf("current kmallocs:\n"); - kprintf("addr pages\n"); - for (struct malloc_hdr *iter = malloc_last; iter; iter = iter->prev) { - kprintf("%08x %05x", iter, iter->page_amt); - for (size_t i = 0; i < 4; i++) - kprintf(" k/%08x", iter->stacktrace[i]); - kprintf("\n peek 0x%x\n", *(uint32_t*)(&iter[1])); - total++; - } - kprintf(" total 0x%x\n", total); - kprintf("pbitmap usage %u/%u\n", pbitmap_taken, pbitmap_len * 8); -} - -void *page_alloc(size_t pages) { - /* i do realize how painfully slow this is */ - size_t streak = 0; - for (size_t i = pbitmap_searchstart; i < pbitmap_len * 8; i++) { - if (bitmap_get(i)) { - streak = 0; - continue; - } - if (++streak >= pages) { - /* found hole big enough for this allocation */ - i = i + 1 - streak; - for (size_t j = 0; j < streak; j++) - bitmap_set(i + j, true); - pbitmap_searchstart = i + streak - 1; - return pbitmap_start + i * PAGE_SIZE; - } - } - kprintf("we ran out of memory :(\ngoodbye.\n"); - panic_unimplemented(); -} - -void *page_zalloc(size_t pages) { - void *p = page_alloc(pages); - memset(p, 0, pages * PAGE_SIZE); - return p; -} - -// frees `pages` consecutive pages starting from *first -void page_free(void *first_addr, size_t pages) { - assert(first_addr >= (void*)pbitmap_start); - size_t first = ((uintptr_t)first_addr - (uintptr_t)pbitmap_start) / PAGE_SIZE; - if (pbitmap_searchstart > first) - pbitmap_searchstart = first; - for (size_t i = 0; i < pages; i++) { - assert(bitmap_get(first + i)); - bitmap_set(first + i, false); - } -} - -void kmalloc_sanity(const void *addr) { - assert(addr); - const struct malloc_hdr *hdr = addr - sizeof(struct malloc_hdr); - assert(hdr->magic == MALLOC_MAGIC); - if (hdr->next) assert(hdr->next->prev == hdr); - if (hdr->prev) assert(hdr->prev->next == hdr); -} - -void *kmalloc(size_t len) { - // TODO better kmalloc - - struct malloc_hdr *hdr; - void *addr; - uint32_t pages; - - len += sizeof(struct malloc_hdr); - pages = len / PAGE_SIZE + 1; - - hdr = page_alloc(pages); - hdr->magic = MALLOC_MAGIC; - hdr->page_amt = pages; - - hdr->next = NULL; - hdr->prev = malloc_last; - if (hdr->prev) { - assert(!hdr->prev->next); - hdr->prev->next = hdr; - } - - for (size_t i = 0; i < 4; i++) - hdr->stacktrace[i] = debug_caller(i); - - malloc_last = hdr; - - addr = (void*)hdr + sizeof(struct malloc_hdr); - kmalloc_sanity(addr); - return addr; -} - -void kfree(void *ptr) { - struct malloc_hdr *hdr; - if (ptr == NULL) return; - - hdr = ptr - sizeof(struct malloc_hdr); - kmalloc_sanity(ptr); - - hdr->magic = ~MALLOC_MAGIC; // (hopefully) detect double frees - if (hdr->next) - hdr->next->prev = hdr->prev; - if (hdr->prev) - hdr->prev->next = hdr->next; - if (malloc_last == hdr) - malloc_last = hdr->prev; - page_free(hdr, hdr->page_amt); -} diff --git a/src/kernel/mem/alloc.h b/src/kernel/mem/alloc.h deleted file mode 100644 index 671a468..0000000 --- a/src/kernel/mem/alloc.h +++ /dev/null @@ -1,29 +0,0 @@ -#pragma once -#include -#include -#include - -extern struct malloc_hdr *malloc_last; - -void mem_init(void *memtop); -void mem_reserve(void *addr, size_t len); -void mem_debugprint(void); - -// allocates `pages` consecutive pages -// TODO deprecate -void *page_alloc(size_t pages); -// zeroes the allocated pages -void *page_zalloc(size_t pages); - -// frees `pages` consecutive pages starting from *first -void page_free(void *first, size_t pages); - -void kmalloc_sanity(const void *addr); -void *kmalloc(size_t len); -void kfree(void *ptr); - -static inline void *kzalloc(size_t len) { - void *b = kmalloc(len); - memset(b, 0, len); - return b; -} diff --git a/src/kernel/proc.c b/src/kernel/proc.c index 836490b..65cbd81 100644 --- a/src/kernel/proc.c +++ b/src/kernel/proc.c @@ -2,7 +2,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/kernel/ring.c b/src/kernel/ring.c deleted file mode 100644 index e69de29..0000000 diff --git a/src/kernel/syscalls.c b/src/kernel/syscalls.c index 72d19f0..259b59f 100644 --- a/src/kernel/syscalls.c +++ b/src/kernel/syscalls.c @@ -4,7 +4,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/kernel/vfs/mount.c b/src/kernel/vfs/mount.c index 2518989..b4f5b0f 100644 --- a/src/kernel/vfs/mount.c +++ b/src/kernel/vfs/mount.c @@ -1,4 +1,4 @@ -#include +#include #include #include #include diff --git a/src/kernel/vfs/procfs.c b/src/kernel/vfs/procfs.c index ecca839..4f2bbd1 100644 --- a/src/kernel/vfs/procfs.c +++ b/src/kernel/vfs/procfs.c @@ -1,5 +1,5 @@ #include -#include +#include #include #include #include diff --git a/src/kernel/vfs/request.c b/src/kernel/vfs/request.c index 35df29c..410e41e 100644 --- a/src/kernel/vfs/request.c +++ b/src/kernel/vfs/request.c @@ -1,6 +1,6 @@ #include #include -#include +#include #include #include #include -- cgit v1.2.3