From ed7634ba1d402b3b4d7536899842ae7e5dd67992 Mon Sep 17 00:00:00 2001 From: Julian Barathieu Date: Tue, 14 May 2019 14:39:35 +0200 Subject: [PATCH 1/9] pushing bugs --- Makefile | 13 ++++++-- kaleid/kernel/init/init.c | 3 ++ kaleid/kernel/mm/paging.asm | 24 ++++++++++++++ kaleid/kernel/mm/paging.c | 65 +++++++++++++++++++++++++++++++++++++ 4 files changed, 103 insertions(+), 2 deletions(-) create mode 100644 kaleid/kernel/mm/paging.asm create mode 100644 kaleid/kernel/mm/paging.c diff --git a/Makefile b/Makefile index 86a11ad..809e8ee 100644 --- a/Makefile +++ b/Makefile @@ -91,8 +91,8 @@ LibCDep=$(patsubst %.c,$(KOBJDIR)/%.d,$(LibCSources)) LibCObj += $(KOBJDIR)/libc/atoi.o $(KOBJDIR)/libc/itoa.o # Kernel sources -KernSources = kernel/ke/cpuid.c \ - kernel/ke/idt.c kernel/init/init.c \ +KernSources = kernel/ke/cpuid.c kernel/mm/paging.c \ + kernel/ke/idt.c kernel/init/init.c \ kernel/init/table.c kernel/io/cursor.c \ kernel/ke/log.c kernel/io/vga.c \ kernel/ke/panic.c kernel/mm/map.c \ @@ -161,6 +161,15 @@ $(KOBJDIR)/kernel/ke/idt.o: $(KALEIDDIR)/kernel/ke/idt.c \ @rm -f $@.1 $@.2 @echo ${CL2}[$@] ${CL}Compiled.${CL3} +$(KOBJDIR)/kernel/mm/paging.o: $(KALEIDDIR)/kernel/mm/paging.c \ + $(KALEIDDIR)/kernel/mm/paging.asm | $(KOBJDIR) + @mkdir -p $(shell dirname $@) + @$(ASM) $(ASMFLAGS) $(KALEIDDIR)/kernel/mm/paging.asm -o $@.1 + @$(KCC) $< -o $@.2 + @$(LD) $(LDFLAGS) -r $@.1 $@.2 -o $@ + @rm -f $@.1 $@.2 + @echo ${CL2}[$@] ${CL}Compiled.${CL3} + $(KOBJDIR)/kernel/mm/gdt.o: $(KALEIDDIR)/kernel/mm/gdt.c \ $(KALEIDDIR)/kernel/mm/gdt.asm | $(KOBJDIR) @mkdir -p $(shell dirname $@) diff --git a/kaleid/kernel/init/init.c b/kaleid/kernel/init/init.c index 3d775d2..9cf71bf 100644 --- a/kaleid/kernel/init/init.c +++ b/kaleid/kernel/init/init.c @@ -24,6 +24,8 @@ #include "init.h" +void MmInitPaging(void); + // // Entry point of the Kaleid kernel // @@ -52,6 +54,7 @@ noreturn void BtStartKern(multiboot_info_t *mbInfo, uint mbMagic, void *codeSeg) KeEnableIRQs(); // Several inits + MmInitPaging(); MmInitHeap(); // Start drivers diff --git a/kaleid/kernel/mm/paging.asm b/kaleid/kernel/mm/paging.asm new file mode 100644 index 0000000..222a83d --- /dev/null +++ b/kaleid/kernel/mm/paging.asm @@ -0,0 +1,24 @@ + +global MmEnableWriteProtect +global MmDisableWriteProtect +global MmLoadPML4 + +MmEnableWriteProtect: + push rax + mov rax, cr0 + or rax, 1<<16 + mov cr0, rax + pop rax + ret + +MmDisableWriteProtect: + push rax + mov rax, cr0 + and rax, ~(1<<16) + mov cr0, rax + pop rax + ret + +MmLoadPML4: + mov cr3, rsi + ret diff --git a/kaleid/kernel/mm/paging.c b/kaleid/kernel/mm/paging.c new file mode 100644 index 0000000..aff8118 --- /dev/null +++ b/kaleid/kernel/mm/paging.c @@ -0,0 +1,65 @@ +#include + +#define PAGESIZE (4 * KB) +#define PAGEALIGNED __attribute__((__aligned__(4096))) + +// Page directory pointer offset +typedef uint pdpe_t; + +// Page directory offset +typedef uint pde_t; + +// Page table entry +typedef uint pte_t; + +// paging.asm +void MmLoadPML4(void *); +void MmEnableWriteProtect(void); +void MmDisableWriteProtect(void); + +enum +{ + MF_PRESENT = 1 << 0, + MF_READWRITE = 1 << 1, + MF_USERMODE = 1 << 2, + MF_WRITETHR = 1 << 3, + MF_CACHEDIS = 1 << 4, + MF_ACCESSED = 1 << 5, + MF_DIRTY = 1 << 6 +}; + +//----------- + +pdpe_t pml4[1024] ; + +// First PDPE of our pml4 +pde_t first_pdpe[1024] PAGEALIGNED; + +// First PDP of first_pdpe +pte_t first_pde[1024] PAGEALIGNED; + +// First PTE of first_pde +uint first_pte[1024] PAGEALIGNED; + +void MmInitPaging(void) +{ + size_t i; + + // Set all PDPEs to kernel-mode not present + for (i = 0; i < 1024; i++) pml4[i] = MF_READWRITE; + for (i = 0; i < 1024; i++) first_pdpe[i] = MF_READWRITE; + for (i = 0; i < 1024; i++) first_pde[i] = MF_READWRITE; + + // Set all pages in first_pte to kernel-mode present + for (i = 0; i < 1024; i++) { + first_pte[i] = (i * PAGESIZE) | (MF_READWRITE | MF_PRESENT); + } + + // Install the first PTE + first_pde[0] = (uint)(ulong)first_pte | (MF_READWRITE | MF_PRESENT); + first_pdpe[0] = (uint)(ulong)first_pde | (MF_READWRITE | MF_PRESENT); + pml4[0] = (uint)(ulong)first_pdpe | (MF_READWRITE | MF_PRESENT); + + MmLoadPML4(pml4); +} + From b3240ec3249cb60aec747bb0d819920bd0908fce Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Wed, 15 May 2019 02:26:55 +0200 Subject: [PATCH 2/9] Paging now controlled by the kernel --- Makefile | 17 +++++++------ boot/loader/mem/management.inc | 5 ++-- kaleid/kernel/init/init.c | 3 ++- kaleid/kernel/mm/paging.asm | 5 +++- kaleid/kernel/mm/paging.c | 46 ++++++++++++++++------------------ 5 files changed, 40 insertions(+), 36 deletions(-) diff --git a/Makefile b/Makefile index 809e8ee..cc6223b 100644 --- a/Makefile +++ b/Makefile @@ -203,20 +203,23 @@ $(KOBJDIR)/%.o: %.c | $(KOBJDIR) test: all @qemu-system-x86_64 -cpu core2duo -soundhw pcspk -rtc base=localtime -m 4G -hda $(BUILDDIR)/bin/disk.img \ - -d cpu_reset,guest_errors,pcall,int 2> $(BUILDDIR)/qemu.log & + -d cpu_reset,guest_errors,pcall,int 2> $(BUILDDIR)/qemu.log & test32: all - @qemu-system-i386 -hda $(BUILDDIR)/bin/disk.img -d cpu_reset,guest_errors,pcall,int 2> $(BUILDDIR)/qemu.log & + @qemu-system-i386 -hda $(BUILDDIR)/bin/disk.img -d \ + cpu_reset,guest_errors,pcall,int 2> $(BUILDDIR)/qemu.log & debug: all - @qemu-system-x86_64 -soundhw pcspk -rtc base=localtime -m 64M -hda $(BUILDDIR)/bin/disk.img -no-reboot \ - -no-shutdown -d cpu_reset,guest_errors,pcall,int 2> $(BUILDDIR)/qemu.log & + @setsid qemu-system-x86_64 -soundhw pcspk -rtc base=localtime -m 64M \ + -hda $(BUILDDIR)/bin/disk.img -no-reboot -no-shutdown -d \ + cpu_reset,guest_errors,pcall,int 2> $(BUILDDIR)/qemu.log & @ndisasm $(BINDIR)/kaleid -o 0x00100000 -b 64 > $(BUILDDIR)/kaleid64_disasm.asm @ndisasm $(BINDIR)/kaleid -o 0x00100000 -b 32 > $(BUILDDIR)/kaleid32_disasm.asm gdb: all - @qemu-system-x86_64 -m 64M -soundhw pcspk -rtc base=localtime -hda $(BUILDDIR)/bin/disk.img -no-reboot \ - -no-shutdown -d cpu_reset,guest_errors,pcall,int -s -S 2> $(BUILDDIR)/qemu.log & + @setsid qemu-system-x86_64 -m 64M -soundhw pcspk -rtc base=localtime \ + -hda $(BUILDDIR)/bin/disk.img -no-reboot -no-shutdown -d \ + cpu_reset,guest_errors,pcall,int -s -S 2> $(BUILDDIR)/qemu.log & @ndisasm $(BINDIR)/kaleid -o 0x00100000 -b 64 > kaleid64_disasm.asm @ndisasm $(BINDIR)/kaleid -o 0x00100000 -b 32 > kaleid32_disasm.asm @gdb \ @@ -226,7 +229,7 @@ gdb: all -ex "break BtStartKern" \ ddd: all - @qemu-system-x86_64 -m 64M -hda $(BUILDDIR)/bin/disk.img -no-reboot -soundhw pcspk \ + @setsid qemu-system-x86_64 -m 64M -hda $(BUILDDIR)/bin/disk.img -no-reboot -soundhw pcspk \ -no-shutdown -d cpu_reset,guest_errors,pcall,int -s 2> $(BUILDDIR)/qemu.log & @ndisasm $(BINDIR)/kaleid -o 0x00100000 -b 64 > kaleid64_disasm.asm @ndisasm $(BINDIR)/kaleid -o 0x00100000 -b 32 > kaleid32_disasm.asm diff --git a/boot/loader/mem/management.inc b/boot/loader/mem/management.inc index e29a2b1..9f2f8c3 100644 --- a/boot/loader/mem/management.inc +++ b/boot/loader/mem/management.inc @@ -48,11 +48,11 @@ Setup_paging: cmp ecx, MAX_MEMORY ; PDP table is mapped if MAX_MEMORY jne .map_pdp_table ; else map the next entry - ;; Map each PD entry to a 'huge' 2MiB page + ;; Map each PD entry to a 'huge' 4MiB page mov ecx, 0x0 ; counter variable .map_pd_table: - ;; map ecx-th PD entry to a huge page that starts at address 2MiB*ecx + ;; map ecx-th PD entry to a huge page that starts at address 4MiB*ecx mov eax, 0x200000 mul ecx ; start address of ecx-th page or eax, 1 << 7 | 1 << 1 | 1 << 0 ; present + writable + huge @@ -60,6 +60,7 @@ Setup_paging: inc ecx cmp ecx, 512 * MAX_MEMORY ; PD table is mapped if 512 jne .map_pd_table ; else map the next entry + ret ; ---------------------------------------------------------------------------- ; ; Enable long mode and paging ; diff --git a/kaleid/kernel/init/init.c b/kaleid/kernel/init/init.c index 9cf71bf..0875b1e 100644 --- a/kaleid/kernel/init/init.c +++ b/kaleid/kernel/init/init.c @@ -48,13 +48,14 @@ noreturn void BtStartKern(multiboot_info_t *mbInfo, uint mbMagic, void *codeSeg) // Memory MmInitMemoryMap(); + MmInitPaging(); + // Interrupts launching KeSetupIDT(); KeEnableIRQs(); // Several inits - MmInitPaging(); MmInitHeap(); // Start drivers diff --git a/kaleid/kernel/mm/paging.asm b/kaleid/kernel/mm/paging.asm index 222a83d..8b57010 100644 --- a/kaleid/kernel/mm/paging.asm +++ b/kaleid/kernel/mm/paging.asm @@ -20,5 +20,8 @@ MmDisableWriteProtect: ret MmLoadPML4: - mov cr3, rsi + push rax + mov rax, rdi + mov cr3, rax + pop rax ret diff --git a/kaleid/kernel/mm/paging.c b/kaleid/kernel/mm/paging.c index aff8118..37f7d0a 100644 --- a/kaleid/kernel/mm/paging.c +++ b/kaleid/kernel/mm/paging.c @@ -1,16 +1,16 @@ #include #define PAGESIZE (4 * KB) -#define PAGEALIGNED __attribute__((__aligned__(4096))) + // Page directory pointer offset -typedef uint pdpe_t; +typedef ulong pdpe_t; // Page directory offset -typedef uint pde_t; +typedef ulong pde_t; // Page table entry -typedef uint pte_t; +typedef ulong pte_t; // paging.asm void MmLoadPML4(void *); @@ -28,38 +28,34 @@ enum MF_DIRTY = 1 << 6 }; +#define RAM 8 + //----------- -pdpe_t pml4[1024] ; +volatile pdpe_t PML4[512] __attribute__((__aligned__(4096))); // First PDPE of our pml4 -pde_t first_pdpe[1024] PAGEALIGNED; +volatile pde_t PDP[512] __attribute__((__aligned__(4096))); // First PDP of first_pdpe -pte_t first_pde[1024] PAGEALIGNED; +volatile pte_t PD[512 * RAM] __attribute__((__aligned__(4096))); -// First PTE of first_pde -uint first_pte[1024] PAGEALIGNED; void MmInitPaging(void) { - size_t i; + memzero((void *)&PML4[0], sizeof(PML4)); + memzero((void *)&PDP[0], sizeof(PDP)); + memzero((void *)&PD[0], sizeof(PD)); - // Set all PDPEs to kernel-mode not present - for (i = 0; i < 1024; i++) pml4[i] = MF_READWRITE; - for (i = 0; i < 1024; i++) first_pdpe[i] = MF_READWRITE; - for (i = 0; i < 1024; i++) first_pde[i] = MF_READWRITE; - - // Set all pages in first_pte to kernel-mode present - for (i = 0; i < 1024; i++) { - first_pte[i] = (i * PAGESIZE) | (MF_READWRITE | MF_PRESENT); + for (int i = 0; i < 512 * RAM; i++) { + PD[i] = ((ulong)i * 2048 * 1024) | MF_PRESENT | MF_READWRITE | 1 << 7; } - - // Install the first PTE - first_pde[0] = (uint)(ulong)first_pte | (MF_READWRITE | MF_PRESENT); - first_pdpe[0] = (uint)(ulong)first_pde | (MF_READWRITE | MF_PRESENT); - pml4[0] = (uint)(ulong)first_pdpe | (MF_READWRITE | MF_PRESENT); - MmLoadPML4(pml4); + for (int i = 0; i < RAM; i++) { + PDP[i] = (ulong)(&PD[i*512])| MF_PRESENT | MF_READWRITE; + } + + PML4[0] = (ulong)(&PDP[0])| MF_PRESENT | MF_READWRITE; + + MmLoadPML4((void *)PML4); } - From aaf25bd7e9e683bb6f084314eb59abc1a4021547 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Wed, 15 May 2019 11:15:21 +0200 Subject: [PATCH 3/9] Working static paging --- kaleid/kernel/mm/paging.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/kaleid/kernel/mm/paging.c b/kaleid/kernel/mm/paging.c index 37f7d0a..640898a 100644 --- a/kaleid/kernel/mm/paging.c +++ b/kaleid/kernel/mm/paging.c @@ -25,30 +25,40 @@ enum MF_WRITETHR = 1 << 3, MF_CACHEDIS = 1 << 4, MF_ACCESSED = 1 << 5, - MF_DIRTY = 1 << 6 + MF_DIRTY = 1 << 6, + MF_HUGE = 1 << 7 }; #define RAM 8 +#define NB_4K 2 //----------- volatile pdpe_t PML4[512] __attribute__((__aligned__(4096))); -// First PDPE of our pml4 volatile pde_t PDP[512] __attribute__((__aligned__(4096))); -// First PDP of first_pdpe -volatile pte_t PD[512 * RAM] __attribute__((__aligned__(4096))); +volatile pde_t PD[512 * RAM] __attribute__((__aligned__(4096))); +volatile pte_t PT[512 * NB_4K] __attribute__((__aligned__(4096))); void MmInitPaging(void) { memzero((void *)&PML4[0], sizeof(PML4)); memzero((void *)&PDP[0], sizeof(PDP)); memzero((void *)&PD[0], sizeof(PD)); + memzero((void *)&PT[0], sizeof(PT)); - for (int i = 0; i < 512 * RAM; i++) { - PD[i] = ((ulong)i * 2048 * 1024) | MF_PRESENT | MF_READWRITE | 1 << 7; + for (int i = 0; i < 512 * NB_4K; i++) { + PT[i] = ((ulong)i * 4096) | MF_PRESENT | MF_READWRITE; + } + + for (int i = 0; i < NB_4K; i++) { + PD[i] = (ulong)(&PT[i*512])| MF_PRESENT | MF_READWRITE; + } + + for (int i = NB_4K; i < 512 * RAM; i++) { + PD[i] = ((ulong)i * 2048 * 1024) | MF_PRESENT | MF_READWRITE | MF_HUGE; } for (int i = 0; i < RAM; i++) { From 2c7967f772f069b55c8a88b794c73aeb73cc57e0 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Wed, 15 May 2019 15:55:57 +0200 Subject: [PATCH 4/9] Working static paging --- boot/loader/loader.asm | 2 +- boot/loader/mem/management.inc | 28 ++++++++++------ boot/loader/mem/structures.inc | 2 +- kaleid/kernel/mm/map.c | 4 +-- kaleid/kernel/mm/paging.c | 61 +++++++++++++++++++++++++--------- kaleid/kernel/sh/shcmds.c | 34 +++++++++---------- 6 files changed, 84 insertions(+), 47 deletions(-) diff --git a/boot/loader/loader.asm b/boot/loader/loader.asm index 2312c73..fdb856f 100644 --- a/boot/loader/loader.asm +++ b/boot/loader/loader.asm @@ -54,7 +54,7 @@ MB_header: ;;MULTIBOOT POINT ENTRY FOR GRUB -------------------------------------------- ;; MB_start: - mov esp, KERNEL_STACK ; Setup the stack + mov esp, kernelEnd + 16*1024 ; Setup the stack push 0 ; Reset EFLAGS popf mov [mbInfo], ebx diff --git a/boot/loader/mem/management.inc b/boot/loader/mem/management.inc index 9f2f8c3..f3ae8d6 100644 --- a/boot/loader/mem/management.inc +++ b/boot/loader/mem/management.inc @@ -80,6 +80,8 @@ Go64: mov ecx, 0xC0000080 ; Address of MSR rdmsr ; Read MSR or eax, 1 << 8 ; LME = 1. (Long Mode Enable) + or eax, 1 << 11 ; NXE = 1 (No execute bit) + wrmsr ; Write MSR ;; Enable paging @@ -129,16 +131,16 @@ InitStack: push rdi ;; Begin address to fill and length - mov qword [newStackEnd], KERNEL_STACK - mov qword [newKernelEnd], kernelEnd + mov rdi, kernelEnd - mov qword [kernelEnd], qword 0xbad0bad - mov rdi, kernelEnd + 16 - mov rcx, (KERNEL_STACK - (kernelEnd + 16)) ; The Stack can begin at - ; kernelEnd + 16 in order to not overwrite the - ; kernel by pushing values (grows downward) + ;; Alignes it to 4096o / FUTURE PAGE FRAME + shr rdi, 12 + shl rdi, 12 + add rdi, 0x1000 - ;; XXX : align the stack to 16bytes + ;; Passing info to kernel + mov qword [newKernelEnd], rdi + mov rcx, KERNEL_STACK ; counter ;; If bit 0 is on, fill one byte sar rcx, 1 ; Shift bit 0 into CY @@ -151,12 +153,16 @@ InitStack: stosw ;; We are dword aligned and if bit 2 was on fill another dword - sar rcx, 1 ; Shift bit 2 into CY - jnc $ + 3 + sar rcx, 1 ; Shift bit 2 into CY + jnc $ + 3 stosd ;; RCX now equals the number of qwords to fill - repnz stosq ; Finish by writing RCX qwords. + repnz stosq ; Finish by writing RCX qwords. + + ;; New info for the kernel + mov qword [newStackEnd], rdi + mov rsp, rdi pop rdi pop rcx diff --git a/boot/loader/mem/structures.inc b/boot/loader/mem/structures.inc index 397cb4a..e538bea 100644 --- a/boot/loader/mem/structures.inc +++ b/boot/loader/mem/structures.inc @@ -30,7 +30,7 @@ global newKernelEnd global newStackEnd [section .text] -KERNEL_STACK equ (kernelEnd + 16) + 16 * 1024 ; 16KB of stack +KERNEL_STACK equ 16 * 1024 ; 16KB of stack newKernelEnd dq 0x0 newStackEnd dq 0x0 diff --git a/kaleid/kernel/mm/map.c b/kaleid/kernel/mm/map.c index 2768b5f..b71de25 100644 --- a/kaleid/kernel/mm/map.c +++ b/kaleid/kernel/mm/map.c @@ -141,8 +141,8 @@ void *MmGetFirstAvailZone(void *start) { void *current = 0; // Because the kernel is the kernel - if ((ulong)start < (ulong)BtLoaderInfo.stackEndAddr+16) { - return MmGetFirstAvailZone(BtLoaderInfo.stackEndAddr+16); + if ((ulong)start < (ulong)BtLoaderInfo.stackEndAddr+4096) { + return MmGetFirstAvailZone(BtLoaderInfo.stackEndAddr+4096); } // Search the zone where the start address is diff --git a/kaleid/kernel/mm/paging.c b/kaleid/kernel/mm/paging.c index 640898a..d9827dd 100644 --- a/kaleid/kernel/mm/paging.c +++ b/kaleid/kernel/mm/paging.c @@ -1,4 +1,5 @@ #include +#include #define PAGESIZE (4 * KB) @@ -26,7 +27,8 @@ enum MF_CACHEDIS = 1 << 4, MF_ACCESSED = 1 << 5, MF_DIRTY = 1 << 6, - MF_HUGE = 1 << 7 + MF_HUGE = 1 << 7, + MF_NX = 1 << 31 }; #define RAM 8 @@ -34,38 +36,67 @@ enum //----------- -volatile pdpe_t PML4[512] __attribute__((__aligned__(4096))); +volatile pdpe_t MmPML4[512] __attribute__((__aligned__(4096))); -volatile pde_t PDP[512] __attribute__((__aligned__(4096))); +volatile pde_t MmPDP[512] __attribute__((__aligned__(4096))); -volatile pde_t PD[512 * RAM] __attribute__((__aligned__(4096))); +volatile pde_t MmPD[512 * RAM] __attribute__((__aligned__(4096))); -volatile pte_t PT[512 * NB_4K] __attribute__((__aligned__(4096))); +volatile pte_t MmPT[512 * NB_4K] __attribute__((__aligned__(4096))); +volatile ulong MmStackGuards[2] = { 0 }; + + +// +// Creates our new page table structure and loads it void MmInitPaging(void) { - memzero((void *)&PML4[0], sizeof(PML4)); - memzero((void *)&PDP[0], sizeof(PDP)); - memzero((void *)&PD[0], sizeof(PD)); - memzero((void *)&PT[0], sizeof(PT)); + memzero((void *)&MmPML4[0], sizeof(MmPML4)); + memzero((void *)&MmPDP[0], sizeof(MmPDP)); + memzero((void *)&MmPD[0], sizeof(MmPD)); + memzero((void *)&MmPT[0], sizeof(MmPT)); for (int i = 0; i < 512 * NB_4K; i++) { - PT[i] = ((ulong)i * 4096) | MF_PRESENT | MF_READWRITE; + + // STACK GUARD PAGE + if ((ulong)i*4096 == (ulong)BtLoaderInfo.stackEndAddr) { + MmPT[i] = ((ulong)i * 4096) | MF_PRESENT; + MmStackGuards[0] = i; + continue; + } + + // STACK PAGES + if ( + (ulong)i*4096 < (ulong)BtLoaderInfo.stackEndAddr && + (ulong)i*4096 > (ulong)BtLoaderInfo.kernelEndAddr + ) { + MmPT[i] = ((ulong)i * 4096) | MF_PRESENT | MF_READWRITE;// | MF_NX; + continue; + } + + // STACK GARD PAGE + if ((ulong)i*4096 == (ulong)BtLoaderInfo.kernelEndAddr) { + MmPT[i] = ((ulong)i * 4096) | MF_PRESENT; + MmStackGuards[1] = i; + continue; + } + + MmPT[i] = ((ulong)i * 4096) | MF_PRESENT | MF_READWRITE; } for (int i = 0; i < NB_4K; i++) { - PD[i] = (ulong)(&PT[i*512])| MF_PRESENT | MF_READWRITE; + MmPD[i] = (ulong)(&MmPT[i*512])| MF_PRESENT | MF_READWRITE; } for (int i = NB_4K; i < 512 * RAM; i++) { - PD[i] = ((ulong)i * 2048 * 1024) | MF_PRESENT | MF_READWRITE | MF_HUGE; + MmPD[i] = ((ulong)i * 2048 * 1024) | MF_PRESENT | MF_READWRITE | MF_HUGE; } for (int i = 0; i < RAM; i++) { - PDP[i] = (ulong)(&PD[i*512])| MF_PRESENT | MF_READWRITE; + MmPDP[i] = (ulong)(&MmPD[i*512])| MF_PRESENT | MF_READWRITE; } - PML4[0] = (ulong)(&PDP[0])| MF_PRESENT | MF_READWRITE; + MmPML4[0] = (ulong)(&MmPDP[0])| MF_PRESENT | MF_READWRITE; - MmLoadPML4((void *)PML4); + MmLoadPML4((void *)MmPML4); } diff --git a/kaleid/kernel/sh/shcmds.c b/kaleid/kernel/sh/shcmds.c index 3098467..bf89436 100644 --- a/kaleid/kernel/sh/shcmds.c +++ b/kaleid/kernel/sh/shcmds.c @@ -146,7 +146,7 @@ error_t CmdMemUsage(int argc, char **argv, char *cmdline) KernLog("Kernel stack\n"); KernLog("\tstarts at:\t\t%p (%4luMB + %4luKB + %4luB)\n", - BtLoaderInfo.kernelEndAddr, + BtLoaderInfo.stackEndAddr, _ADDR_TO_MB((size_t)BtLoaderInfo.stackEndAddr), _ADDR_TO_KB((size_t)BtLoaderInfo.stackEndAddr), _ADDR_TO_B((size_t)BtLoaderInfo.stackEndAddr)); @@ -229,9 +229,9 @@ error_t CmdPF(int argc, char **argv, char *cmdline) } extern void KeStartShell(void); -error_t CmdShell(int argc, char **argv, char *cmdline) +error_t CmdStackOverflow(int argc, char **argv, char *cmdline) { - KeStartShell(); + CmdStackOverflow(0, 0, 0); return EOK; } @@ -239,20 +239,20 @@ error_t CmdShell(int argc, char **argv, char *cmdline) Command_t cmdtable[] = { - { "beep", CmdBeep, "Make a beep" }, - { "cls", CmdClear, "Clears standard output" }, - { "date", CmdDate, "Print date" }, - { "die", CmdDie, "Die painfully" }, - { "exit", CmdQuit, "Initiate shutdown" }, - { "help", CmdHelp, "Show this message" }, - { "march", CmdStarWars, "Play the Imperial March"}, - { "mmap", CmdMemMap, "Show memory map" }, - { "musage", CmdMemUsage, "Show memory statistics" }, - { "pfault", CmdPF, "Provokes a PF" }, - { "pstest", CmdPsTest, "Scheduler test routine" }, - { "quit", CmdQuit, "Alias for 'exit'" }, - { "shell", CmdShell, "New shell instance" }, - { "time", CmdTime, "Print time" }, + { "beep", CmdBeep, "Make a beep" }, + { "cls", CmdClear, "Clears standard output" }, + { "date", CmdDate, "Print date" }, + { "die", CmdDie, "Die painfully" }, + { "exit", CmdQuit, "Initiate shutdown" }, + { "help", CmdHelp, "Show this message" }, + { "march", CmdStarWars, "Play the Imperial March"}, + { "mmap", CmdMemMap, "Show memory map" }, + { "musage", CmdMemUsage, "Show memory statistics" }, + { "pfault", CmdPF, "Provoke a PF" }, + { "pstest", CmdPsTest, "Scheduler test routine" }, + { "quit", CmdQuit, "Alias for 'exit'" }, + { "so", CmdStackOverflow, "Provoke a stack overflow" }, + { "time", CmdTime, "Print time" }, { NULL, NULL, NULL } }; From 9152ac341f4c4780ad8238cc3b3f6ecbf6e22f5e Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Wed, 15 May 2019 16:38:57 +0200 Subject: [PATCH 5/9] Working static paging --- kaleid/kernel/init/init.c | 1 - kaleid/kernel/mm/paging.c | 12 ++++++------ kaleid/kernel/sh/shcmds.c | 2 +- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/kaleid/kernel/init/init.c b/kaleid/kernel/init/init.c index 0875b1e..5c2a9c2 100644 --- a/kaleid/kernel/init/init.c +++ b/kaleid/kernel/init/init.c @@ -50,7 +50,6 @@ noreturn void BtStartKern(multiboot_info_t *mbInfo, uint mbMagic, void *codeSeg) MmInitMemoryMap(); MmInitPaging(); - // Interrupts launching KeSetupIDT(); KeEnableIRQs(); diff --git a/kaleid/kernel/mm/paging.c b/kaleid/kernel/mm/paging.c index d9827dd..b87920f 100644 --- a/kaleid/kernel/mm/paging.c +++ b/kaleid/kernel/mm/paging.c @@ -31,7 +31,7 @@ enum MF_NX = 1 << 31 }; -#define RAM 8 +#define RAM_MAX 16 #define NB_4K 2 //----------- @@ -40,7 +40,7 @@ volatile pdpe_t MmPML4[512] __attribute__((__aligned__(4096))); volatile pde_t MmPDP[512] __attribute__((__aligned__(4096))); -volatile pde_t MmPD[512 * RAM] __attribute__((__aligned__(4096))); +volatile pde_t MmPD[512 * RAM_MAX] __attribute__((__aligned__(4096))); volatile pte_t MmPT[512 * NB_4K] __attribute__((__aligned__(4096))); @@ -60,7 +60,7 @@ void MmInitPaging(void) // STACK GUARD PAGE if ((ulong)i*4096 == (ulong)BtLoaderInfo.stackEndAddr) { - MmPT[i] = ((ulong)i * 4096) | MF_PRESENT; + MmPT[i] = ((ulong)i * 4096); MmStackGuards[0] = i; continue; } @@ -76,7 +76,7 @@ void MmInitPaging(void) // STACK GARD PAGE if ((ulong)i*4096 == (ulong)BtLoaderInfo.kernelEndAddr) { - MmPT[i] = ((ulong)i * 4096) | MF_PRESENT; + MmPT[i] = ((ulong)i * 4096); MmStackGuards[1] = i; continue; } @@ -88,11 +88,11 @@ void MmInitPaging(void) MmPD[i] = (ulong)(&MmPT[i*512])| MF_PRESENT | MF_READWRITE; } - for (int i = NB_4K; i < 512 * RAM; i++) { + for (int i = NB_4K; i < 512 * RAM_MAX; i++) { MmPD[i] = ((ulong)i * 2048 * 1024) | MF_PRESENT | MF_READWRITE | MF_HUGE; } - for (int i = 0; i < RAM; i++) { + for (int i = 0; i < RAM_MAX; i++) { MmPDP[i] = (ulong)(&MmPD[i*512])| MF_PRESENT | MF_READWRITE; } diff --git a/kaleid/kernel/sh/shcmds.c b/kaleid/kernel/sh/shcmds.c index bf89436..b48e50a 100644 --- a/kaleid/kernel/sh/shcmds.c +++ b/kaleid/kernel/sh/shcmds.c @@ -224,7 +224,7 @@ error_t CmdDie(int argc, char **argv, char *cmdline) error_t CmdPF(int argc, char **argv, char *cmdline) { - *((char*)0xDEADBEEF0) = 1; + *((char*)BtLoaderInfo.stackEndAddr + 16) = 1; return EOK; } From 945c215b72af4c9e89495d18ba93018c0feccc02 Mon Sep 17 00:00:00 2001 From: Julian Barathieu Date: Wed, 15 May 2019 16:39:20 +0200 Subject: [PATCH 6/9] malloc() fix --- kaleid/libc/mem.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/kaleid/libc/mem.c b/kaleid/libc/mem.c index ae8f2d5..5fe84db 100644 --- a/kaleid/libc/mem.c +++ b/kaleid/libc/mem.c @@ -36,7 +36,11 @@ void *malloc(size_t n) void *ptr; error_t rc; +#ifndef _KALEID_KERNEL rc = KalAllocMemory(&ptr, n); +#else + rc = KalAllocMemoryEx(&ptr, n, M_ZEROED, 0) +#endif if (rc > 0) seterrno(rc); return ptr; From 602b4361130accb411c794912e19aefd1f745333 Mon Sep 17 00:00:00 2001 From: Julian Barathieu Date: Wed, 15 May 2019 16:39:36 +0200 Subject: [PATCH 7/9] malloc() fix #2 --- kaleid/libc/mem.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kaleid/libc/mem.c b/kaleid/libc/mem.c index 5fe84db..7107bd0 100644 --- a/kaleid/libc/mem.c +++ b/kaleid/libc/mem.c @@ -39,7 +39,7 @@ void *malloc(size_t n) #ifndef _KALEID_KERNEL rc = KalAllocMemory(&ptr, n); #else - rc = KalAllocMemoryEx(&ptr, n, M_ZEROED, 0) + rc = KalAllocMemoryEx(&ptr, n, M_ZEROED, 0); #endif if (rc > 0) seterrno(rc); From 96dd593b881707b18412b3727e86ff1500a85c06 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Wed, 15 May 2019 19:11:47 +0200 Subject: [PATCH 8/9] Working static paging --- kaleid/kernel/init/init.c | 4 +--- kaleid/kernel/mm/paging.c | 26 +++++++++++++++++--------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/kaleid/kernel/init/init.c b/kaleid/kernel/init/init.c index 5c2a9c2..15f3333 100644 --- a/kaleid/kernel/init/init.c +++ b/kaleid/kernel/init/init.c @@ -48,15 +48,13 @@ noreturn void BtStartKern(multiboot_info_t *mbInfo, uint mbMagic, void *codeSeg) // Memory MmInitMemoryMap(); + MmInitHeap(); MmInitPaging(); // Interrupts launching KeSetupIDT(); KeEnableIRQs(); - // Several inits - MmInitHeap(); - // Start drivers KeEnableRTC(); IoEnableKeyb(); diff --git a/kaleid/kernel/mm/paging.c b/kaleid/kernel/mm/paging.c index b87920f..84f1dc6 100644 --- a/kaleid/kernel/mm/paging.c +++ b/kaleid/kernel/mm/paging.c @@ -1,5 +1,7 @@ #include #include +#include +#include #define PAGESIZE (4 * KB) @@ -32,7 +34,8 @@ enum }; #define RAM_MAX 16 -#define NB_4K 2 +#define NB_4K 150 + // * 2 MB //----------- @@ -40,9 +43,9 @@ volatile pdpe_t MmPML4[512] __attribute__((__aligned__(4096))); volatile pde_t MmPDP[512] __attribute__((__aligned__(4096))); -volatile pde_t MmPD[512 * RAM_MAX] __attribute__((__aligned__(4096))); +volatile pde_t MmPD[512 * RAM_MAX] __attribute__((__aligned__(4096)));; -volatile pte_t MmPT[512 * NB_4K] __attribute__((__aligned__(4096))); +volatile pte_t MmPT[512 * NB_4K] __attribute__((__aligned__(4096)));; volatile ulong MmStackGuards[2] = { 0 }; @@ -51,6 +54,8 @@ volatile ulong MmStackGuards[2] = { 0 }; // Creates our new page table structure and loads it void MmInitPaging(void) { + extern MemoryMap_t memoryMap; + uint phRamSize = memoryMap.freeRamSize + memoryMap.nonfreeRamSize; memzero((void *)&MmPML4[0], sizeof(MmPML4)); memzero((void *)&MmPDP[0], sizeof(MmPDP)); memzero((void *)&MmPD[0], sizeof(MmPD)); @@ -65,12 +70,9 @@ void MmInitPaging(void) continue; } - // STACK PAGES - if ( - (ulong)i*4096 < (ulong)BtLoaderInfo.stackEndAddr && - (ulong)i*4096 > (ulong)BtLoaderInfo.kernelEndAddr - ) { - MmPT[i] = ((ulong)i * 4096) | MF_PRESENT | MF_READWRITE;// | MF_NX; + // ENOMEM like + if ((ulong)i*4096 > (ulong)phRamSize) { + MmPT[i] = ((ulong)i * 4096) | MF_READWRITE;// | MF_NX; continue; } @@ -89,6 +91,12 @@ void MmInitPaging(void) } for (int i = NB_4K; i < 512 * RAM_MAX; i++) { + // ENOMEM like + if ((ulong)i* 2048 * 1024 > (ulong)phRamSize) { + MmPT[i] = ((ulong)i * 4096) | MF_READWRITE;// | MF_NX; + continue; + } + MmPD[i] = ((ulong)i * 2048 * 1024) | MF_PRESENT | MF_READWRITE | MF_HUGE; } From a023b91b01dc72729fd0c085c48915e6f9f7073f Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Wed, 15 May 2019 21:02:16 +0200 Subject: [PATCH 9/9] Page Fault test --- Makefile | 20 +++++--------------- kaleid/kernel/mm/paging.c | 7 +++---- kaleid/kernel/sh/shcmds.c | 10 ++++++++-- 3 files changed, 16 insertions(+), 21 deletions(-) diff --git a/Makefile b/Makefile index cc6223b..7e44091 100644 --- a/Makefile +++ b/Makefile @@ -29,6 +29,7 @@ # Debug mode ?= debug +ram ?= 8G # Programs ASM=nasm @@ -202,26 +203,17 @@ $(KOBJDIR)/%.o: %.c | $(KOBJDIR) @echo ${CL2}[$@] ${CL}Compiled.${CL3} test: all - @qemu-system-x86_64 -cpu core2duo -soundhw pcspk -rtc base=localtime -m 4G -hda $(BUILDDIR)/bin/disk.img \ + @qemu-system-x86_64 -cpu core2duo -soundhw pcspk -rtc base=localtime -m $(ram) -hda $(BUILDDIR)/bin/disk.img \ -d cpu_reset,guest_errors,pcall,int 2> $(BUILDDIR)/qemu.log & test32: all - @qemu-system-i386 -hda $(BUILDDIR)/bin/disk.img -d \ + @qemu-system-i386 -m $(ram) -hda $(BUILDDIR)/bin/disk.img -d \ cpu_reset,guest_errors,pcall,int 2> $(BUILDDIR)/qemu.log & -debug: all - @setsid qemu-system-x86_64 -soundhw pcspk -rtc base=localtime -m 64M \ - -hda $(BUILDDIR)/bin/disk.img -no-reboot -no-shutdown -d \ - cpu_reset,guest_errors,pcall,int 2> $(BUILDDIR)/qemu.log & - @ndisasm $(BINDIR)/kaleid -o 0x00100000 -b 64 > $(BUILDDIR)/kaleid64_disasm.asm - @ndisasm $(BINDIR)/kaleid -o 0x00100000 -b 32 > $(BUILDDIR)/kaleid32_disasm.asm - gdb: all - @setsid qemu-system-x86_64 -m 64M -soundhw pcspk -rtc base=localtime \ + @setsid qemu-system-x86_64 -m $(ram) -soundhw pcspk -rtc base=localtime \ -hda $(BUILDDIR)/bin/disk.img -no-reboot -no-shutdown -d \ cpu_reset,guest_errors,pcall,int -s -S 2> $(BUILDDIR)/qemu.log & - @ndisasm $(BINDIR)/kaleid -o 0x00100000 -b 64 > kaleid64_disasm.asm - @ndisasm $(BINDIR)/kaleid -o 0x00100000 -b 32 > kaleid32_disasm.asm @gdb \ -ex "set arch i386:x86-64:intel" \ -ex "target remote localhost:1234" \ @@ -229,10 +221,8 @@ gdb: all -ex "break BtStartKern" \ ddd: all - @setsid qemu-system-x86_64 -m 64M -hda $(BUILDDIR)/bin/disk.img -no-reboot -soundhw pcspk \ + @setsid qemu-system-x86_64 -m $(ram) -hda $(BUILDDIR)/bin/disk.img -no-reboot -soundhw pcspk \ -no-shutdown -d cpu_reset,guest_errors,pcall,int -s 2> $(BUILDDIR)/qemu.log & - @ndisasm $(BINDIR)/kaleid -o 0x00100000 -b 64 > kaleid64_disasm.asm - @ndisasm $(BINDIR)/kaleid -o 0x00100000 -b 32 > kaleid32_disasm.asm @ddd install_mbr: $(BINDIR)/disk.img $(MBRDIR)/grub.cfg diff --git a/kaleid/kernel/mm/paging.c b/kaleid/kernel/mm/paging.c index 84f1dc6..10ddbe0 100644 --- a/kaleid/kernel/mm/paging.c +++ b/kaleid/kernel/mm/paging.c @@ -49,20 +49,19 @@ volatile pte_t MmPT[512 * NB_4K] __attribute__((__aligned__(4096)));; volatile ulong MmStackGuards[2] = { 0 }; - // // Creates our new page table structure and loads it void MmInitPaging(void) { extern MemoryMap_t memoryMap; uint phRamSize = memoryMap.freeRamSize + memoryMap.nonfreeRamSize; + memzero((void *)&MmPML4[0], sizeof(MmPML4)); memzero((void *)&MmPDP[0], sizeof(MmPDP)); memzero((void *)&MmPD[0], sizeof(MmPD)); memzero((void *)&MmPT[0], sizeof(MmPT)); for (int i = 0; i < 512 * NB_4K; i++) { - // STACK GUARD PAGE if ((ulong)i*4096 == (ulong)BtLoaderInfo.stackEndAddr) { MmPT[i] = ((ulong)i * 4096); @@ -93,7 +92,7 @@ void MmInitPaging(void) for (int i = NB_4K; i < 512 * RAM_MAX; i++) { // ENOMEM like if ((ulong)i* 2048 * 1024 > (ulong)phRamSize) { - MmPT[i] = ((ulong)i * 4096) | MF_READWRITE;// | MF_NX; + MmPD[i] = ((ulong)i * 2048 * 1024) | MF_READWRITE;// | MF_NX; continue; } @@ -106,5 +105,5 @@ void MmInitPaging(void) MmPML4[0] = (ulong)(&MmPDP[0])| MF_PRESENT | MF_READWRITE; - MmLoadPML4((void *)MmPML4); + //MmLoadPML4((void *)MmPML4); } diff --git a/kaleid/kernel/sh/shcmds.c b/kaleid/kernel/sh/shcmds.c index b48e50a..eaed746 100644 --- a/kaleid/kernel/sh/shcmds.c +++ b/kaleid/kernel/sh/shcmds.c @@ -224,7 +224,13 @@ error_t CmdDie(int argc, char **argv, char *cmdline) error_t CmdPF(int argc, char **argv, char *cmdline) { - *((char*)BtLoaderInfo.stackEndAddr + 16) = 1; + if (argc != 2) + return EINVAL; + + ulong address = atoul(argv[1]); + KernLog("print: %s, %d\n", argv[1], address); + + *((char*)address) += 1; return EOK; } @@ -248,7 +254,7 @@ Command_t cmdtable[] = { "march", CmdStarWars, "Play the Imperial March"}, { "mmap", CmdMemMap, "Show memory map" }, { "musage", CmdMemUsage, "Show memory statistics" }, - { "pfault", CmdPF, "Provoke a PF" }, + { "pfault", CmdPF, "Provoke a PF. Usage : pfault
" }, { "pstest", CmdPsTest, "Scheduler test routine" }, { "quit", CmdQuit, "Alias for 'exit'" }, { "so", CmdStackOverflow, "Provoke a stack overflow" },