From b2547584ec8bf9723cd5e49697130506b5a65ca6 Mon Sep 17 00:00:00 2001 From: Johann Rudloff Date: Fri, 12 Dec 2014 20:06:41 +0100 Subject: [PATCH] Minor improvements, dont hardcode memory map base address. --- boot1.asm | 5 +---- boot2.c | 2 +- entry.asm | 10 ++++++---- include/sys/memory.h | 4 +++- kernel.c | 10 +++++----- kmem.c | 17 +++++++---------- 6 files changed, 23 insertions(+), 25 deletions(-) diff --git a/boot1.asm b/boot1.asm index 0c5cbfc..89e4b92 100644 --- a/boot1.asm +++ b/boot1.asm @@ -107,10 +107,7 @@ protected_main: mov eax, 0x8000 mov esp, eax - call boot2_main - .idle: - hlt - jmp .idle + jmp boot2_main ; pread_sector(unit32_t lba, uint8_t *dest) %define DRIVE_PORT_BASE 0x01f0 diff --git a/boot2.c b/boot2.c index a8b2715..64cae7b 100644 --- a/boot2.c +++ b/boot2.c @@ -78,7 +78,7 @@ void boot2_main(void) { fs_inode_read_block(&fs, &ino_kernel, i, dst); dst += fs.block_size; } - asm volatile ("ljmp $0x08,$0x100000" : : ); + asm volatile ("pushl $0x1000\nljmp $0x08,$0x100000" : : ); } static void diff --git a/entry.asm b/entry.asm index 7667725..b49b36d 100644 --- a/entry.asm +++ b/entry.asm @@ -4,16 +4,14 @@ extern kernel_main %include "assym.asm" start: + mov ebx, [esp] mov ax, 0x10 mov ss, ax mov esp, 0x200000 ; this gives us 2MiB - kernel_size of stack + push ebx call kernel_main - .hang: - hlt - jmp .hang - %define USER_CS 0x1b %define USER_DS 0x23 @@ -21,8 +19,12 @@ global proc_switch proc_switch: mov ebp, [esp + 4] ; struct proc* + mov ebx, cr3 mov eax, PROC_PD(ebp) + cmp eax, ebx + je .have_cr3 ; CR3 is already set, no need to flush TLB mov cr3, eax +.have_cr3: mov ebp, PROC_PCB(ebp) diff --git a/include/sys/memory.h b/include/sys/memory.h index 036ca2f..bcc0388 100644 --- a/include/sys/memory.h +++ b/include/sys/memory.h @@ -5,6 +5,8 @@ #define alloca(size) __builtin_alloca(size) +struct memory_map_entry; + void *kmalloc(size_t size); void *kmalloc_align(size_t alignment, size_t size); void kfree(void *p); @@ -18,6 +20,6 @@ static inline void *memset(void *b, int c, size_t len) { return b; } -void kmem_init(void); +void kmem_init(uint32_t memory_map_entry_count, struct memory_map_entry *entries); #endif /* _MEMORY_H_ */ diff --git a/kernel.c b/kernel.c index 285f280..1a26490 100644 --- a/kernel.c +++ b/kernel.c @@ -45,18 +45,18 @@ static void setup_tss(void); static struct proc *launch(const char *binary, uint32_t mem); -void kernel_main() { +void kernel_main(void *meminfo) { kprint("kernel main entered\n"); console_init(&global_console); + console_clear(&global_console); + console_print(&global_console, "xos kernel loaded\n"); + setup_gdt(); - kmem_init(); + kmem_init(*(uint32_t *)meminfo, meminfo + 4); paging_init(); - console_clear(&global_console); - console_print(&global_console, "xos kernel loaded\n"); - setup_idt(); struct proc *proc1 = launch("/init", 0x70000000); diff --git a/kmem.c b/kmem.c index 6ffd7ab..c0683bd 100644 --- a/kmem.c +++ b/kmem.c @@ -9,7 +9,7 @@ #define KMEM_ARENA_START 0x200000 #define KMEM_ARENA_END 0x400000 -struct detect_mmap_info { +struct memory_map_entry { uint32_t base_low; uint32_t base_high; uint32_t length_low; @@ -28,10 +28,10 @@ struct free_info { static struct kmem kmem_state; -void kmem_detect(void); +void kmem_detect(uint32_t count, struct memory_map_entry *mmap); -void kmem_init(void) { - kmem_detect(); +void kmem_init(uint32_t memory_map_entry_count, struct memory_map_entry *entries) { + kmem_detect(memory_map_entry_count, entries); // begin with the last page void *page = (void *)(KMEM_ARENA_END - PAGE_SIZE); @@ -50,12 +50,9 @@ void kmem_init(void) { } void -kmem_detect(void) { - uint32_t mmap_count = *(uint32_t *)0x1000; - struct detect_mmap_info *mmap = (struct detect_mmap_info *)(0x1000 + 4); - khexprint("mmap count", mmap_count); - khexprint("mmap size", sizeof(struct detect_mmap_info)); - for (int i=0; i