From b5d8ad75599387d11dbc5b46d04366e69c909d31 Mon Sep 17 00:00:00 2001 From: sfja Date: Sun, 22 Nov 2026 01:00:24 +0100 Subject: [PATCH] kalloc works --- common/macros.vc6asm | 14 +- common/memstr.vc6asm | 12 +- kernel/kernel.vc6asm | 524 +++++++++++++++++- kernel/reference/kalloc.c | 242 ++++++++ .../printf.c} | 2 +- src/emu.c | 1 + 6 files changed, 777 insertions(+), 18 deletions(-) create mode 100644 kernel/reference/kalloc.c rename kernel/{printf_reference.c => reference/printf.c} (97%) diff --git a/common/macros.vc6asm b/common/macros.vc6asm index 51476bd..28b5e87 100644 --- a/common/macros.vc6asm +++ b/common/macros.vc6asm @@ -48,15 +48,15 @@ @end @macro maskne 0 - maske ; 00p + ; xxp not fl ; 11q - and fl, FL_EQ ; 00q + maske ; 00q @end @macro maskge 0 - maskl ; 0p0 - not fl ; 1q1 - and fl, FL_LT ; 0q0 + ; xpx + not fl ; xqx + maskl ; 0q0 @end @macro maskle 0 @@ -108,6 +108,10 @@ mov ip, %0 @end +@macro jnz 1 + cmov ip, %0 +@end + @macro je 1 maske cmov ip, %0 diff --git a/common/memstr.vc6asm b/common/memstr.vc6asm index 841525a..c5df988 100644 --- a/common/memstr.vc6asm +++ b/common/memstr.vc6asm @@ -15,8 +15,8 @@ memcpy: add r2, 1 sub r3, 1 .l0_cond: - cmp r3, 0 - jg .l0_body + mov fl, r3 + jnz .l0_body .leave: ret @@ -41,8 +41,8 @@ memmove: sub r2, 1 sub r3, 1 .l0_cond: - cmp r3, 0 - jg .l0_body + mov fl, r3 + jnz .l0_body .leave: ret @@ -57,8 +57,8 @@ memset: add r1, 1 sub r3, 1 .l0_cond: - cmp r3, 0 - jg .l0_body + mov fl, r3 + jnz .l0_body ret ; r1 a diff --git a/kernel/kernel.vc6asm b/kernel/kernel.vc6asm index f1a17f0..3f2f67d 100644 --- a/kernel/kernel.vc6asm +++ b/kernel/kernel.vc6asm @@ -26,6 +26,32 @@ main: mov r1, .ok_str call println + call kalloc_init + + mov r1, 0x120 + call kmalloc + push r0 + + mov r1, 0x120 + call kmalloc + + pop r1 + call kfree + + mov r1, 0x60 + call kmalloc + push r0 + + mov r1, 0x60 + call kmalloc + + mov r1, r0 + call kfree + + pop r1 + mov r2, 0x120 + call krealloc + mov r1, .prompt call puts @@ -51,7 +77,6 @@ main: .prompt: dw "> \0" - keydown_handler: load r1, [KBD_SCANCODE] add r1, 93 @@ -62,6 +87,491 @@ keydown_handler: @include "./vcd_print.vc6asm" @include "../common/memstr.vc6asm" +@const kalloc_table_head_size 0x20 +@const kalloc_entry_size 0x4 +@const kalloc_max_entries 0x100 + +@const kalloc_table_p heap_base +@const kalloc_entries_begin kalloc_table_p + kalloc_table_head_size +@const kalloc_entries_end kalloc_entries_begin + kalloc_entry_size * kalloc_max_entries +@const kalloc_alloc_base kalloc_entries_end +@const kalloc_alloc_end 0x3fff + +@const kalloc_first_p kalloc_table_p +@const kalloc_last_p kalloc_table_p + 1 + +@const kalloc_entry_field_p_prev 0 +@const kalloc_entry_field_p_next 1 +@const kalloc_entry_field_ptr 2 +@const kalloc_entry_field_size 3 + +kalloc_init: + mov r0, 0 + mov [kalloc_first_p], r0 + mov [kalloc_last_p], r0 + + mov r0, [kalloc_last_p] + push r0 + mov r0, [kalloc_first_p] + push r0 + mov r0, kalloc_alloc_base + push r0 + mov r0, kalloc_entries_end + push r0 + mov r0, kalloc_entries_begin + push r0 + mov r0, kalloc_table_p + push r0 + mov r1, .str0 + mov r2, sp + call printf + add sp, 6 + + ret +.str0: + dw "kalloc: Initialized allocator\n p_table = 0x%x\n p_entries_begin = 0x%x\n p_entries_end = 0x%x\n alloc_base = 0x%x\n p_first = 0x%x\n p_last = 0x%x\n\0" + +; r1 size +; --- +; r0 pointer to allocation +kmalloc: + push bp + mov bp, sp + + sub sp, 3 + ; 0 ptr + ; 1 size + ; 2 p_entry + + sto r1, sp, 1 + + call _kalloc_find_ptr_with_enough_space + mov [sp], r0 + + push r0 + mov r1, .str0 + mov r2, sp + call printf + add sp, 1 + + mov r0, [sp] + cmp r0, 0 + je .error_out_of_memory + + call _kalloc_find_empty_entry_in_allocation_table + sto r0, sp, 2 + + push r0 + mov r1, .str1 + mov r2, sp + call printf + add sp, 1 + + ldo r0, sp, 2; p_entry + + ; p_entry->p_prev = table->p_last + mov r2, r0 + add r2, kalloc_entry_field_p_prev + mov r1, [kalloc_last_p] + mov [r2], r1 + + ; p_entry->p_next = 0 + mov r2, r0 + add r2, kalloc_entry_field_p_next + mov r1, 0 + mov [r2], r1 + + ; p_entry->ptr = ptr + mov r2, r0 + add r2, kalloc_entry_field_ptr + mov r1, [sp] + mov [r2], r1 + + ; p_entry->size = size + mov r2, r0 + add r2, kalloc_entry_field_size + ldo r1, sp, 1 + mov [r2], r1 + + ; printf(.str2) + mov r1, r0 + add r1, kalloc_entry_field_size + mov r1, [r1] + push r1 + mov r1, r0 + add r1, kalloc_entry_field_ptr + mov r1, [r1] + push r1 + mov r1, r0 + add r1, kalloc_entry_field_p_next + mov r1, [r1] + push r1 + mov r1, r0 + add r1, kalloc_entry_field_p_prev + mov r1, [r1] + push r1 + mov r1, .str2 + mov r2, sp + call printf + add sp, 4 + + ldo r0, sp, 2; p_entry + mov [kalloc_last_p], r0 + + mov r1, [kalloc_first_p] + cmp r1, 0 + jne .p_first_already_has_a_value + mov [kalloc_first_p], r0 +.p_first_already_has_a_value: + + mov r1, [kalloc_last_p] + push r1 + mov r1, [kalloc_first_p] + push r1 + mov r1, .str3 + mov r2, sp + call printf + add sp, 2 + + ldo r0, sp, 2; p_entry + mov r1, r0 + add r1, kalloc_entry_field_p_prev + mov r1, [r1] ; entry->prev + cmp r1, 0 + je .entry_has_no_prev + + add r1, kalloc_entry_field_p_next + mov [r1], r0 ; prev->next = p_entry +.entry_has_no_prev: + + ldo r1, bp, -2 + push r1 + ldo r1, bp, -3 + push r1 + mov r1, .str4 + mov r2, sp + call printf + add sp, 2 + + ldo r0, bp, -3 +.leave: + mov sp, bp + pop bp + ret +.error_out_of_memory: + mov r1, .error_out_of_memory_str + call printf + mov r0, 0 + jmp .leave +.error_out_of_memory_str: + dw "kmalloc(): Out of memory!\n\0" +.str0: + dw "kmalloc(): ptr = 0x%x\n\0" +.str1: + dw "kmalloc(): p_entry = 0x%x\n\0" +.str2: + dw "kmalloc(): Adding entry\n p_prev = 0x%x\n p_next = 0x%x\n ptr = 0x%x\n size = 0x%x\n\0" +.str3: + dw "kmalloc():\n table->p_first = 0x%x\n table->p_last = 0x%x\n\0" +.str4: + dw "kmalloc(): New allocation at 0x%x of size %u\n\0" + + +; r1 pointer to allocation +kfree: + push bp + mov bp, sp + + sub sp, 2 + ; 0 ptr + ; 1 p_entry + + sto r1, sp, 0 + + push r1 + mov r1, .str0 + mov r2, sp + call printf + add sp, 1 + + ldo r1, sp, 0 + call _kalloc_find_alloc_entry_by_ptr + sto r0, sp, 1 + + push r0 + mov r1, .str1 + mov r2, sp + call printf + add sp, 1 + + ldo r0, sp, 1 + cmp r0, 0 + jne .pointer_is_valid + ret ; Ignore invalid pointer +.pointer_is_valid: + + ldo r1, sp, 1 + call _kalloc_delete_entry + + mov r1, [kalloc_last_p] + push r1 + mov r1, [kalloc_first_p] + push r1 + mov r1, .str2 + mov r2, sp + call printf + add sp, 2 + +.leave: + mov sp, bp + pop bp + ret +.str0: + dw "kfree(): ptr = 0x%x\n\0" +.str1: + dw "kfree(): p_entry = 0x%x\n\0" +.str2: + dw "kfree():\n table->p_first = 0x%x\n table->p_last = 0x%x\n\0" + + +; r1 pointer to allocation +; r2 new size +; --- +; r0 pointer to new allocation +krealloc: + push bp + mov bp, sp + + sub sp, 5 + ; 0 ptr + ; 1 size + ; 2 p_entry + ; 3 move_size + ; 4 new_ptr + + sto r1, sp, 0 + sto r2, sp, 1 + + push r1 + mov r1, .str0 + mov r2, sp + call printf + add sp, 1 + + ldo r1, sp, 0 + call _kalloc_find_alloc_entry_by_ptr + sto r0, sp, 2 + + push r0 + mov r1, .str1 + mov r2, sp + call printf + add sp, 1 + + ldo r0, sp, 1 ; size + ldo r1, sp, 2 + add r1, kalloc_entry_field_size + mov r1, [r1] ; entry->size + cmp r0, r1 + maskl + cmov r0, r1 + sto r0, sp, 3 + + ldo r1, sp, 2 + call _kalloc_delete_entry + + ldo r1, sp, 1 + call kmalloc + sto r0, sp, 4 + + mov r1, r0 + ldo r2, sp, 0 + ldo r3, sp, 3 + call memmove + + ldo r0, sp, 4 +.leave: + mov sp, bp + pop bp + ret + +.str0: + dw "krealloc(): ptr = 0x%x\n\0" +.str1: + dw "krealloc(): p_entry = 0x%x\n\0" + +; r1 size +; --- +; r2 pointer to unoccupied space +_kalloc_find_ptr_with_enough_space: + push bp + mov bp, sp + + sub sp, 3 + ; 0 ptr + ; 1 size + ; 2 p_entry + + mov r0, kalloc_alloc_base + sto r0, sp, 0 + sto r1, sp, 1 + mov r0, [kalloc_first_p] + sto r0, sp, 2 + + jmp .l0_cond +.l0_body: + + ldo r1, sp, 0 ; ptr + ldo r0, sp, 1 ; size + add r0, r1 ; ptr + size + + push r1 + + ldo r1, bp, -1 ; entry + + mov r2, r1 + add r2, kalloc_entry_field_ptr + mov r2, [r2] ; entry->ptr + + ; entry->ptr < ptr + size + cmp r2, r0 + mov r0, fl + + mov r3, r1 + add r3, kalloc_entry_field_size + mov r3, [r3] ; entry->size + + add r2, r3 ; entry->ptr + entry->size + + pop r3 ; ptr + + ; ptr < entry->ptr + entry->size + cmp r3, r2 + and fl, r0 + jge .l0_end + + mov [sp], r2 + + mov r3, r1 + add r3, kalloc_entry_field_p_next + mov r3, [r3] ; p_next + sto r3, bp, -1 ; entry + +.l0_cond: + cmp r1, 0 + jne .l0_body +.l0_end: + + ldo r1, bp, -1 ; entry + + mov r2, r1 + add r2, kalloc_entry_field_ptr + mov r2, [r2] ; entry->ptr + + mov r3, r1 + add r3, kalloc_entry_field_size + mov r3, [r3] ; entry->size + + ; alloc_end < ptr + size + add r2, r3 + mov r3, kalloc_alloc_end + cmp r3, r2 + jl .outside_alloc_zone + mov r0, [sp] + jmp .leave +.outside_alloc_zone: + mov r0, 0 +.leave: + mov sp, bp + pop bp + ret + +; --- +; r0 pointer to empty entry +_kalloc_find_empty_entry_in_allocation_table: + mov r0, kalloc_entries_begin ; p_entry + +.l0_body: + mov r2, r0 + add r2, kalloc_entry_field_ptr + mov r2, [r2] + + cmp r2, 0 + jne .l0_continue + ret +.l0_continue: + + add r0, kalloc_entry_size +.l0_cond: + cmp r0, kalloc_entries_end + jl .l0_body + + mov r0, 0 + ret + +; r1 pointer to look for +; --- +; r0 pointer to entry +_kalloc_find_alloc_entry_by_ptr: + mov r0, kalloc_entries_begin ; p_entry + +.l0_body: + mov r2, r0 + add r2, kalloc_entry_field_ptr + mov r2, [r2] + + cmp r1, r2 + jne .l0_continue + ret +.l0_continue: + + add r0, kalloc_entry_size +.l0_cond: + cmp r0, kalloc_entries_end + jl .l0_body + + mov r0, 0 + ret + + +; r1 pointer to entry +_kalloc_delete_entry: + mov r0, r1 ; entry + + mov r1, r0 + add r1, kalloc_entry_field_p_prev + mov r1, [r1] ; entry->prev + + mov r2, r0 + add r2, kalloc_entry_field_p_next + mov r2, [r2] ; entry->next + + cmp r2, 0 + je .l0_else + + mov r3, r2 + add r3, kalloc_entry_field_p_prev + mov [r3], r1 ; entry->next->prev = entry->prev + + jmp .l0_end +.l0_else: + mov [kalloc_last_p], r1 +.l0_end: + + cmp r1, 0 + je .l1_else + + mov r3, r1 + add r3, kalloc_entry_field_p_next + mov [r3], r2 ; entry->prev->next = entry->next + + jmp .l1_end +.l1_else: + mov [kalloc_first_p], r2 +.l1_end: + ret + + ; r1 block id ; r2 destination address read_block: @@ -71,13 +581,15 @@ read_block: store [DISK_CTL], r0 ret +@const fs_heap_base heap_base + 0x4000 + @const fs_block_size 0x200 -@const fs_superblock heap_base + fs_block_size * 0 -@const fs_bitmap heap_base + fs_block_size * 1 -@const fs_inode_block heap_base + fs_block_size * 2 -@const fs_ind_block heap_base + fs_block_size * 3 -@const fs_data_block heap_base + fs_block_size * 4 +@const fs_superblock fs_heap_base + fs_block_size * 0 +@const fs_bitmap fs_heap_base + fs_block_size * 1 +@const fs_inode_block fs_heap_base + fs_block_size * 2 +@const fs_ind_block fs_heap_base + fs_block_size * 3 +@const fs_data_block fs_heap_base + fs_block_size * 4 @const fs_block_offset 4 diff --git a/kernel/reference/kalloc.c b/kernel/reference/kalloc.c new file mode 100644 index 0000000..7e6ea78 --- /dev/null +++ b/kernel/reference/kalloc.c @@ -0,0 +1,242 @@ +#include +#include +#include +#include +#include + +#define logf(FMT, ...) \ + printf("%s(): " FMT "\n", __FUNCTION__ __VA_OPT__(, ) __VA_ARGS__); + +typedef uint16_t W; + +typedef W WPtr; +typedef W AllocEntryPtr; + +typedef struct { + AllocEntryPtr p_prev; + AllocEntryPtr p_next; + WPtr ptr; + W size; +} AllocEntry; + +typedef struct { + AllocEntryPtr p_entries_begin; + AllocEntryPtr p_entries_end; + AllocEntryPtr p_first; + AllocEntryPtr p_last; + W alloc_base; +} AllocTableHead; + +constexpr W alloc_base = 0x2000; +constexpr W alloc_end = 0x3fff; +constexpr W max_allocs = 0x100; +constexpr W size_of_alloc_table_head = 0x20; +constexpr W size_of_alloc_entry = 0x4; + +W memory[65536]; + +AllocTableHead *table = (AllocTableHead *)&memory[alloc_base]; + +void kmalloc_init(void) +{ + *table = (AllocTableHead) { + .p_entries_begin = alloc_base + size_of_alloc_table_head, + .p_entries_end = alloc_base + size_of_alloc_table_head + + size_of_alloc_entry * max_allocs, + .p_first = 0, + .p_last = 0, + .alloc_base = alloc_base + size_of_alloc_table_head + + size_of_alloc_entry * max_allocs, + }; + + logf("Initialized allocator"); + logf(" p_table = 0x%x", alloc_base); + logf(" p_entries_begin = 0x%x", table->p_entries_begin); + logf(" p_entries_end = 0x%x", table->p_entries_end); + logf(" alloc_base = 0x%x", table->alloc_base); +} + +static WPtr find_ptr_with_space_for_allocation(W size) +{ + WPtr ptr = table->alloc_base; + + AllocEntryPtr p_entry = table->p_first; + + while (p_entry) { + AllocEntry *entry = (AllocEntry *)&memory[p_entry]; + + bool overlapping + = ptr + size > entry->ptr && ptr < entry->ptr + entry->size; + + if (!overlapping) + break; + + ptr = entry->ptr + entry->size; + p_entry = entry->p_next; + } + + if (ptr + size > alloc_end) + return 0; // Out of memory. + + return ptr; +} + +static AllocEntryPtr find_empty_entry_in_allocation_table(void) +{ + for (AllocEntryPtr p_entry = table->p_entries_begin; + p_entry < table->p_entries_end; + p_entry += size_of_alloc_entry) { + + AllocEntry *entry = (AllocEntry *)&memory[p_entry]; + if (!entry->ptr) + return p_entry; + } + + return 0; // Out of entry slots. +} + +WPtr kmalloc(W size) +{ + WPtr ptr = find_ptr_with_space_for_allocation(size); + logf("ptr = 0x%x", ptr); + + if (!ptr) { + logf("Out of memory!"); + return 0; // Out of memory. + } + + AllocEntryPtr p_entry = find_empty_entry_in_allocation_table(); + logf("p_entry = 0x%x", p_entry); + + AllocEntry *entry = (AllocEntry *)&memory[p_entry]; + *entry = (AllocEntry) { + .p_prev = table->p_last, + .p_next = 0, + .ptr = ptr, + .size = size, + }; + + logf("Adding entry"); + logf(" p_prev = 0x%x", entry->p_prev); + logf(" p_next = 0x%x", entry->p_next); + logf(" ptr = 0x%x", entry->ptr); + logf(" size = 0x%x", entry->size); + + table->p_last = p_entry; + if (!table->p_first) + table->p_first = p_entry; + + logf("table->p_first = 0x%x", table->p_first); + logf("table->p_last = 0x%x", table->p_last); + + if (entry->p_prev) { + AllocEntry *prev = (AllocEntry *)&memory[entry->p_prev]; + prev->p_next = p_entry; + } + + logf("New allocation at 0x%x of size %u", ptr, size); + + return ptr; +} + +static AllocEntryPtr find_alloc_entry_by_ptr(WPtr ptr) +{ + for (AllocEntryPtr p_entry = table->p_entries_begin; + p_entry < table->p_entries_end; + p_entry += size_of_alloc_entry) { + + AllocEntry *entry = (AllocEntry *)&memory[p_entry]; + logf("entry->ptr = 0x%x", entry->ptr); + if (entry->ptr == ptr) + return p_entry; + } + + return 0; // Invalid pointer. +} + +static void delete_entry(AllocEntryPtr p_entry) +{ + AllocEntry *entry = (AllocEntry *)&memory[p_entry]; + + if (entry->p_next) { + AllocEntry *next = (AllocEntry *)&memory[entry->p_next]; + next->p_prev = entry->p_prev; + } else { + table->p_last = entry->p_prev; + } + + if (entry->p_prev) { + AllocEntry *prev = (AllocEntry *)&memory[entry->p_prev]; + prev->p_next = prev->p_next; + } else { + table->p_first = entry->p_next; + } +} + +void kfree(WPtr ptr) +{ + logf("ptr = 0x%x", ptr); + AllocEntryPtr p_entry = find_alloc_entry_by_ptr(ptr); + logf("p_entry = 0x%x", p_entry); + + if (!p_entry) + return; // Ignore invalid pointer. + + delete_entry(p_entry); + + logf("table->p_first = 0x%x", table->p_first); + logf("table->p_last = 0x%x", table->p_last); +} + +WPtr krealloc(WPtr ptr, W size) +{ + logf("ptr = 0x%x", ptr); + AllocEntryPtr p_entry = find_alloc_entry_by_ptr(ptr); + logf("p_entry = 0x%x", p_entry); + + AllocEntry *entry = (AllocEntry *)&memory[p_entry]; + W move_size = size < entry->size ? entry->size : size; + + delete_entry(p_entry); + + WPtr new_ptr = kmalloc(size); + memmove(&memory[new_ptr], &memory[ptr], move_size); + + return new_ptr; +} + +int main(void) +{ + logf("Calling kmalloc_init()"); + kmalloc_init(); + + logf("Calling p0 = kmalloc(120)"); + WPtr p0 = kmalloc(120); + + logf("Calling p1 = kmalloc(120)"); + WPtr p1 = kmalloc(120); + assert(p0 != p1); + + logf("Calling p2 = kmalloc(120)"); + WPtr p2 = kmalloc(120); + (void)p2; + + logf("Calling kfree(p1)"); + kfree(p1); + + logf("Calling p3 = kmalloc(60)"); + WPtr p3 = kmalloc(60); + assert(p1 == p3); + + logf("Calling p4 = kmalloc(60)"); + WPtr p4 = kmalloc(60); + + logf("kfree(p4)"); + kfree(p4); + + logf("p5 = krealloc(p3, 120)"); + WPtr p5 = krealloc(p3, 120); + assert(p3 == p5); + + logf("works!"); +} diff --git a/kernel/printf_reference.c b/kernel/reference/printf.c similarity index 97% rename from kernel/printf_reference.c rename to kernel/reference/printf.c index 3118171..32e54e0 100644 --- a/kernel/printf_reference.c +++ b/kernel/reference/printf.c @@ -50,7 +50,7 @@ int print_iword_10(int val) return print_uword_base(-val, 10) + 1; } -int kprintf(const int *fmt, const int args[]) +int my_printf(const int *fmt, const int args[]) { int written = 0; diff --git a/src/emu.c b/src/emu.c index e6b126f..46fa01d 100644 --- a/src/emu.c +++ b/src/emu.c @@ -295,6 +295,7 @@ void vm_run(VM *vm) vm->state = s; mtx_unlock(&vm->mtx); + thrd_sleep(&(struct timespec) { .tv_nsec = 100000 }, NULL); } }