From ca7b1b731a59cc0b18d402ce7b12e6513fa6ae68 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Wed, 27 Mar 2019 17:29:39 +0100 Subject: [PATCH] Big enhancement with memory identity mapping of 8 first gigs --- Makefile | 6 +++--- boot/loader/mem/management.inc | 20 +++++++++++++++----- boot/loader/mem/structures.inc | 2 +- kaleid/include/kernel/mm.h | 9 ++++++++- kaleid/kernel/init/init.c | 4 +--- kaleid/kernel/mm/gdt.c | 23 ++++++++++++++--------- 6 files changed, 42 insertions(+), 22 deletions(-) diff --git a/Makefile b/Makefile index f600141..f14c939 100644 --- a/Makefile +++ b/Makefile @@ -192,18 +192,18 @@ $(KOBJDIR)/kernel/sched.o: $(KERNELDIR)/kernel/proc/sched.c $(KERNELDIR)/include .PHONY: test test: all - @qemu-system-x86_64 -m 5G -mem-prealloc -hda build/bin/disk.img -d cpu_reset,guest_errors,pcall,int -enable-kvm 2> qemu.log & + @qemu-system-x86_64 -m 5G -mem-prealloc -hda build/bin/disk.img -d cpu_reset,guest_errors,pcall,int 2> qemu.log & @ndisasm $(BINDIR)/kaleid -o 0x00100000 -b 64 > loader_disasm64.asm @ndisasm $(BINDIR)/kaleid -o 0x00100000 -b 32 > loader_disasm32.asm .PHONY: test32 test32: all - @qemu-system-i386 -hda build/bin/disk.img -d cpu_reset,guest_errors,pcall,int -enable-kvm 2> qemu.log & + @qemu-system-i386 -hda build/bin/disk.img -d cpu_reset,guest_errors,pcall,int 2> qemu.log & @ndisasm $(BINDIR)/kaleid -o 0x00100000 -b 64 > loader_disasm64.asm @ndisasm $(BINDIR)/kaleid -o 0x00100000 -b 32 > loader_disasm32.asm .PHONY: debug debug: all - @qemu-system-x86_64 -m 5G -hda build/bin/disk.img -d cpu_reset,guest_errors,pcall,int -s -S -enable-kvm 2> qemu.log & + @qemu-system-x86_64 -m 5G -hda build/bin/disk.img -d cpu_reset,guest_errors,pcall,int -s -S 2> qemu.log & @ndisasm $(BINDIR)/kaleid -o 0x00100000 -b 64 > loader_disasm64.asm @ndisasm $(BINDIR)/kaleid -o 0x00100000 -b 32 > loader_disasm32.asm diff --git a/boot/loader/mem/management.inc b/boot/loader/mem/management.inc index cfdf3c4..8e4676d 100644 --- a/boot/loader/mem/management.inc +++ b/boot/loader/mem/management.inc @@ -23,6 +23,8 @@ ; along with OS/K. If not, see . ; ;=----------------------------------------------------------------------------=; +%define MAX_MEMORY 8 ; GiB + [BITS 32] [section .text] ; ---------------------------------------------------------------------------- ; @@ -34,22 +36,28 @@ Setup_paging: or eax, 1 << 1 | 1 << 0 ; present + writable mov [PML4_table], eax - ;; Map the first PDP entry to PD table + ;; Map the first PDP entries to PD table + mov ecx, 0x0 mov eax, PD_table +.map_pdp_table: or eax, 1 << 1 | 1 << 0 ; present + writable - mov [PDP_table], eax + mov [PDP_table + ecx * 8], eax + add eax, 8 + inc ecx + cmp ecx, MAX_MEMORY + jne .map_pdp_table ;; Map each PD entry to a 'huge' 2MiB page mov ecx, 0x0 ; counter variable -.map_p2_table: +.map_pd_table: ;; map ecx-th PD entry to a huge page that starts at address 2MiB*ecx mov eax, 0x200000 mul ecx ; start address of ecx-th page or eax, 1 << 7 | 1 << 1 | 1 << 0 ; present + writable + huge mov [PD_table + ecx * 8], eax inc ecx - cmp ecx, 512 ; PD table is mapped if 512 - jne .map_p2_table ; else map the next entry + cmp ecx, 512 * MAX_MEMORY ; PD table is mapped if 512 + jne .map_pd_table ; else map the next entry ret @@ -125,10 +133,12 @@ InitStack: sar rcx, 1 ; Shift bit 0 into CY jnc $ + 3 stosb + ;; We are word aligned and if bit 1 was on fill another word sar rcx, 1 ; Shift bit 1 into CY jnc $ + 4 stosw + ;; We are dword aligned and if bit 2 was on fill another dword sar rcx, 1 ; Shift bit 2 into CY jnc $ + 3 diff --git a/boot/loader/mem/structures.inc b/boot/loader/mem/structures.inc index dc1c607..baeca97 100644 --- a/boot/loader/mem/structures.inc +++ b/boot/loader/mem/structures.inc @@ -59,4 +59,4 @@ PML4_table: PDP_table: resb 4096 PD_table: - resb 4096 + resb 4096 * MAX_MEMORY diff --git a/kaleid/include/kernel/mm.h b/kaleid/include/kernel/mm.h index e0da6fc..a15a2fa 100644 --- a/kaleid/include/kernel/mm.h +++ b/kaleid/include/kernel/mm.h @@ -67,6 +67,7 @@ struct GdtEntry_t uchar highBase; // last 8 bits } __attribute__((packed)); +// The gdt pointer struct GdtPtr_t { uchar limit; // upper 16 bits @@ -93,7 +94,13 @@ size_t MmGetAvailZoneSize(void *start); void *MmGetFirstAvailZone(void *start); // -// Initialize the descriptor table +// Initializes the descriptor table // void MmInitGdt(void); + +// +// Loads the descriptor table +// +extern void MmLoadGdt(GdtPtr_t *GdtPtr); + // -------------------------------------------------------------------------- // diff --git a/kaleid/kernel/init/init.c b/kaleid/kernel/init/init.c index 1494a13..88fd242 100644 --- a/kaleid/kernel/init/init.c +++ b/kaleid/kernel/init/init.c @@ -150,10 +150,8 @@ noreturn void BtStartKern(multiboot_info_t *mbInfo, int mbMagic) //if(rc)KernLog("error\n"); //KernLog((char*)buf->buf); - uchar *addr = (uchar *)(ullong)(1024*MB - 1); + uchar *addr = (uchar *)(ullong)(1024*5*MB - 7); - KernLog("Test, valeur autour de %p: %hhu\n", addr, *addr); - *addr = 1; KernLog("Test, valeur autour de %p: %hhu\n", addr, *addr); // We're out diff --git a/kaleid/kernel/mm/gdt.c b/kaleid/kernel/mm/gdt.c index ae395a6..e4e5ef3 100644 --- a/kaleid/kernel/mm/gdt.c +++ b/kaleid/kernel/mm/gdt.c @@ -30,21 +30,26 @@ GdtPtr_t gdtPtr; void MmInitGdt(void) { - gdtPtr.limit = (sizeof(gdt_entry_t) * 5) - 1; - gdtPtr.base = (uint)&gdt_entries; + gdtPtr.limit = (sizeof(GdtEntry_t) * 5) - 1; + gdtPtr.base = (uint)(ullong)&gdtEntries; + + + /* XXX set TSS register */ + + //MmLoadGdt(&gdtPtr); } static void MmSetGdtEntry(int index, uint base, uint limit, uchar access, uchar granularity) { - gdtEntries[num].lowBase = (base & 0xFFFF); - gdtEntries[num].middleBase = (base >> 16) & 0xFF; - gdtEntries[num].highBase = (base >> 24) & 0xFF; + gdtEntries[index].lowBase = (base & 0xFFFF); + gdtEntries[index].middleBase = (base >> 16) & 0xFF; + gdtEntries[index].highBase = (base >> 24) & 0xFF; - gdtEntries[num].lowLimit = (limit & 0xFFFF); - gdtEntries[num].granularity = (limit >> 16) & 0x0F; + gdtEntries[index].lowLimit = (limit & 0xFFFF); + gdtEntries[index].granularity = (limit >> 16) & 0x0F; - gdtEntries[num].granularity |= gran & 0xF0; - gdtEntries[num].access = access; + gdtEntries[index].granularity |= granularity & 0xF0; + gdtEntries[index].access = access; }