diff --git a/ProjectTree b/ProjectTree index 05361f9..1c063cc 100644 --- a/ProjectTree +++ b/ProjectTree @@ -70,9 +70,11 @@ │   │   ├── buf.h │   │   └── list.h │   ├── mm +│   │   ├── gdt.h │   │   ├── heap.h │   │   ├── malloc.h -│   │   └── mm.h +│   │   ├── map.h +│   │   └── paging.h │   ├── po │   │   └── shtdwn.h │   ├── sh @@ -155,4 +157,4 @@ ├── ProjectTree └── README.md -28 directories, 102 files +28 directories, 104 files diff --git a/README.md b/README.md index 93be98b..6563203 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ make install installdisk=XXX ``` #### Screenshot -![OS/K Started](https://www.os-k.eu/images/screen3.png) +![OS/K Started](https://www.os-k.eu/images/screen6.png) diff --git a/include/mm/mm.h b/include/mm/gdt.h similarity index 62% rename from include/mm/mm.h rename to include/mm/gdt.h index 50657f1..289f911 100644 --- a/include/mm/mm.h +++ b/include/mm/gdt.h @@ -1,7 +1,7 @@ //----------------------------------------------------------------------------// // GNU GPL OS/K // // // -// Desc: Memory manager functions // +// Desc: GDT related functions // // // // // // Copyright © 2018-2019 The OS/K Team // @@ -26,40 +26,15 @@ #include #endif -#ifndef _MM_MM_H -#define _MM_MM_H +#ifndef _MM_GDT_H +#define _MM_GDT_H //----------------------------------------------------------------------------// -#define MINIMUM_RAM_SIZE 16 // Mio, the minimum RAM size. #define IOMAP_SIZE (8 * 1024) -#define AVAILABLE_ZONE 1 // Fully usable RAM zone -#define RESERVED_ZONE 2 // Used by the firmware -#define ACPI_ZONE 3 // Used by ACPI but can be freed -#define NVS_ZONE 4 // Dunno -#define BADRAM_ZONE 5 // Invalid zone because material problem... -#define MAX_ENTRIES 2048 // Max number of memory map entries -#define KPAGESIZE (4 * KB) -#define UPAGESIZE (4 * KB) - //----------------------------------------------------------------------------// -// The entry structure of the map -struct MapEntry_t { - void *addr; - size_t length; // in bytes - uint type; // reserved or not -} __attribute__((__packed__)); - -// the map structure -struct MemoryMap_t { - size_t length; - size_t freeRamSize; - size_t nonfreeRamSize; - MapEntry_t entry[MAX_ENTRIES]; -} __attribute__((__packed__)); - // The gdt entry struct GdtEntry_t { @@ -110,8 +85,6 @@ struct Tss_t } __attribute__ ((packed)) __attribute__((aligned(8))); - - // The gdt pointer struct GdtPtr_t { @@ -121,27 +94,6 @@ struct GdtPtr_t //----------------------------------------------------------------------------// -// -// Initializes the memory map structure -// -void MmInitMemoryMap(void); - -// -// Initializes the memory map structure -// -void MmPrintMemoryMap(void); - -// -// Returns the size of the first available memory zone -// from the start address pointer -// -size_t MmGetAvailZoneSize(void *start); - -// -// Returns the first available memory zone from the start address pointer -// -void *MmGetFirstAvailZone(void *start); - // // Initializes the descriptor table // @@ -158,53 +110,6 @@ extern void MmLoadGdt(GdtPtr_t *gdtPtr, ushort tssOffset); // extern void MmStoreGdt(void); -// -// Paging misc -// -void MmInitPaging(void); - -void MmActivatePageHandler(void); - -// -// Returns the address of the stack guard pages -// -void *MmGetStackGuards(char rank); - -// -// Translate a virtual address into physical address and the opposite -// -void *MmTransVirtToPhyAddr(void*); -void *MmTransPhyToVirtAddr(void* virtualAddr); - -// -// Set flags to a page -// -void MmSetPage(void* virtualAddr, ulong flags); -void MmUnSetPage(void* virtualAddr, ulong flags); - -// -// Map a page -// -void MmMapPage(void* virtualAddr, void* physicalAddr, ulong flags); -void MmUnmapPage(void* virtualAddr); - -// Page table entry -typedef ulong pte_t; - -// Page directory offset -typedef pte_t* pde_t; - -// Page directory pointer offset -typedef pde_t* pdpe_t; - -// Page directory L4 pointer offset -typedef pdpe_t* pml4_t; - -// paging.asm -void MmLoadPML4(void *); -void MmEnableWriteProtect(void); -void MmDisableWriteProtect(void); - //----------------------------------------------------------------------------// #endif diff --git a/include/mm/map.h b/include/mm/map.h new file mode 100644 index 0000000..d7c9ea7 --- /dev/null +++ b/include/mm/map.h @@ -0,0 +1,85 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Desc: Mapping and checking memory related functions // +// // +// // +// Copyright © 2018-2019 The OS/K Team // +// // +// This file is part of OS/K. // +// // +// OS/K is free software: you can redistribute it and/or modify // +// it under the terms of the GNU General Public License as published by // +// the Free Software Foundation, either version 3 of the License, or // +// any later version. // +// // +// OS/K is distributed in the hope that it will be useful, // +// but WITHOUT ANY WARRANTY//without even the implied warranty of // +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // +// GNU General Public License for more details. // +// // +// You should have received a copy of the GNU General Public License // +// along with OS/K. If not, see . // +//----------------------------------------------------------------------------// + +#ifndef _KERNEL_H +#include +#endif + +#ifndef _MM_MAP_H +#define _MM_MAP_H + +//----------------------------------------------------------------------------// + +#define MINIMUM_RAM_SIZE 16 // Mio, the minimum RAM size. + +#define AVAILABLE_ZONE 1 // Fully usable RAM zone +#define RESERVED_ZONE 2 // Used by the firmware +#define ACPI_ZONE 3 // Used by ACPI but can be freed +#define NVS_ZONE 4 // Dunno +#define BADRAM_ZONE 5 // Invalid zone because material problem... +#define MAX_ENTRIES 2048 // Max number of memory map entries + +//----------------------------------------------------------------------------// + +// The entry structure of the map +struct MapEntry_t { + void *addr; + size_t length; // in bytes + uint type; // reserved or not +} __attribute__((__packed__)); + +// the map structure +struct MemoryMap_t { + size_t length; + size_t freeRamSize; + size_t nonfreeRamSize; + MapEntry_t entry[MAX_ENTRIES]; +} __attribute__((__packed__)); + +//----------------------------------------------------------------------------// + +// +// Initializes the memory map structure +// +void MmInitMemoryMap(void); + +// +// Initializes the memory map structure +// +void MmPrintMemoryMap(void); + +// +// Returns the size of the first available memory zone +// from the start address pointer +// +size_t MmGetAvailZoneSize(void *start); + +// +// Returns the first available memory zone from the start address pointer +// +void *MmGetFirstAvailZone(void *start); + +//----------------------------------------------------------------------------// + +#endif diff --git a/include/mm/paging.h b/include/mm/paging.h new file mode 100644 index 0000000..d76f0ec --- /dev/null +++ b/include/mm/paging.h @@ -0,0 +1,93 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Desc: Paging memory related functions // +// // +// // +// Copyright © 2018-2019 The OS/K Team // +// // +// This file is part of OS/K. // +// // +// OS/K is free software: you can redistribute it and/or modify // +// it under the terms of the GNU General Public License as published by // +// the Free Software Foundation, either version 3 of the License, or // +// any later version. // +// // +// OS/K is distributed in the hope that it will be useful, // +// but WITHOUT ANY WARRANTY//without even the implied warranty of // +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // +// GNU General Public License for more details. // +// // +// You should have received a copy of the GNU General Public License // +// along with OS/K. If not, see . // +//----------------------------------------------------------------------------// + +#ifndef _KERNEL_H +#include +#endif + +#ifndef _MM_PAGING_H +#define _MM_PAGING_H + +//----------------------------------------------------------------------------// + +#define KPAGESIZE (4 * KB) +#define UPAGESIZE (4 * KB) +#define USERSPACE 0x80000000 + +//----------------------------------------------------------------------------// + +// Page table entry +typedef ulong pte_t; + +// Page directory offset +typedef pte_t* pde_t; + +// Page directory pointer offset +typedef pde_t* pdpe_t; + +// Page directory L4 pointer offset +typedef pdpe_t* pml4_t; + +//----------------------------------------------------------------------------// + +// +// Paging activation +// +void MmInitPaging(void); + +void MmActivatePageHandler(void); + +// +// Returns the address of the stack guard pages +// +void *MmGetStackGuards(char rank); + +// +// Translate a virtual address into physical address and the opposite +// +void *MmTransVirtToPhyAddr(void*); +void *MmTransPhyToVirtAddr(void* virtualAddr); + +// +// Set flags to a page +// +void MmSetPage(void* virtualAddr, ulong flags); +void MmUnSetPage(void* virtualAddr, ulong flags); + +// +// Map a page +// +void MmMapPage(void* virtualAddr, void* physicalAddr, ulong flags); +void MmUnmapPage(void* virtualAddr); + +// +// Paging misc +// +void MmLoadPML4(void *); +void MmEnableWriteProtect(void); +void MmDisableWriteProtect(void); + +//----------------------------------------------------------------------------// + +#endif diff --git a/include/mm/palloc.h b/include/mm/palloc.h new file mode 100644 index 0000000..bada93f --- /dev/null +++ b/include/mm/palloc.h @@ -0,0 +1,38 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Desc: Page allocator related functions // +// // +// // +// Copyright © 2018-2019 The OS/K Team // +// // +// This file is part of OS/K. // +// // +// OS/K is free software: you can redistribute it and/or modify // +// it under the terms of the GNU General Public License as published by // +// the Free Software Foundation, either version 3 of the License, or // +// any later version. // +// // +// OS/K is distributed in the hope that it will be useful, // +// but WITHOUT ANY WARRANTY//without even the implied warranty of // +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // +// GNU General Public License for more details. // +// // +// You should have received a copy of the GNU General Public License // +// along with OS/K. If not, see . // +//----------------------------------------------------------------------------// + +#ifndef _KERNEL_H +#include +#endif + +#ifndef _MM_PALLOC_H +#define _MM_PALLOC_H + +//----------------------------------------------------------------------------// + + + +//----------------------------------------------------------------------------// + +#endif diff --git a/kaleid/kernel/init/init.c b/kaleid/kernel/init/init.c index 12d3792..3a3144c 100644 --- a/kaleid/kernel/init/init.c +++ b/kaleid/kernel/init/init.c @@ -22,7 +22,9 @@ // along with OS/K. If not, see . // //----------------------------------------------------------------------------// -#include +#include +#include +#include #include #include #include diff --git a/kaleid/kernel/ke/cpuid.c b/kaleid/kernel/ke/cpuid.c index 4c5017b..2682ba2 100644 --- a/kaleid/kernel/ke/cpuid.c +++ b/kaleid/kernel/ke/cpuid.c @@ -44,7 +44,7 @@ void KeGetCpuInfos(void) CpuInfo.frequency = KeGetCpuFrequency(); } - DebugLog("\tCPU %s %#d KHz detected with features %#x\n", + DebugLog("\tCPU %s %#d MHz detected with features %#x\n", CpuInfo.vendorStr, (long)(CpuInfo.frequency / 1000.0), CpuInfo.featureFlag diff --git a/kaleid/kernel/mm/gdt.c b/kaleid/kernel/mm/gdt.c index 8bd426a..6a5c188 100644 --- a/kaleid/kernel/mm/gdt.c +++ b/kaleid/kernel/mm/gdt.c @@ -1,7 +1,7 @@ //----------------------------------------------------------------------------// // GNU GPL OS/K // // // -// Desc: Mapping and checking memory related functions // +// Desc: GDT related functions // // // // // // Copyright © 2018-2019 The OS/K Team // @@ -22,7 +22,8 @@ // along with OS/K. If not, see . // //----------------------------------------------------------------------------// -#include +#include +#include #include GdtPtr_t gdtPtr; diff --git a/kaleid/kernel/mm/heap.c b/kaleid/kernel/mm/heap.c index a968231..b4c0c58 100644 --- a/kaleid/kernel/mm/heap.c +++ b/kaleid/kernel/mm/heap.c @@ -22,7 +22,7 @@ // along with OS/K. If not, see . // //----------------------------------------------------------------------------// -#include +#include #include #include #include diff --git a/kaleid/kernel/mm/map.c b/kaleid/kernel/mm/map.c index f7e164c..64156fe 100644 --- a/kaleid/kernel/mm/map.c +++ b/kaleid/kernel/mm/map.c @@ -22,7 +22,7 @@ // along with OS/K. If not, see . // //----------------------------------------------------------------------------// -#include +#include #include #include #include diff --git a/kaleid/kernel/mm/paging.c b/kaleid/kernel/mm/paging.c index 5a7e77e..6c3487f 100644 --- a/kaleid/kernel/mm/paging.c +++ b/kaleid/kernel/mm/paging.c @@ -1,14 +1,37 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Desc: Paging memory related functions // +// // +// // +// Copyright © 2018-2019 The OS/K Team // +// // +// This file is part of OS/K. // +// // +// OS/K is free software: you can redistribute it and/or modify // +// it under the terms of the GNU General Public License as published by // +// the Free Software Foundation, either version 3 of the License, or // +// any later version. // +// // +// OS/K is distributed in the hope that it will be useful, // +// but WITHOUT ANY WARRANTY//without even the implied warranty of // +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // +// GNU General Public License for more details. // +// // +// You should have received a copy of the GNU General Public License // +// along with OS/K. If not, see . // +//----------------------------------------------------------------------------// + #include #include +#include #include #include -#include -#include +#include +#include #include #include -#define USERSPACE 0x80000000 - //----------- pml4_t MmPageMapLevel4[512] __attribute__((__aligned__(KPAGESIZE))); @@ -21,8 +44,11 @@ extern ulong _rodata_end; extern ulong _data; extern ulong _data_end; +extern MemoryMap_t memoryMap; + ulong MmStackGuards[2] = { 0 }; ulong MmVirtLastAddress = 0; +ulong MmPhysLastKernAddress = 0; enum { @@ -44,7 +70,6 @@ enum // void MmInitPaging(void) { - extern MemoryMap_t memoryMap; pdpe_t *MmPDP = NULL; pde_t *MmPD = NULL; pte_t *MmPT = NULL; @@ -57,8 +82,8 @@ void MmInitPaging(void) ulong phRamSize = memoryMap.freeRamSize + memoryMap.nonfreeRamSize; // Difference between the end of kernel and the begin of userspace - ulong lastKernelAddr = (ulong)(_heap_start + _heap_max); - ulong diffKernUsr = (ulong)USERSPACE - lastKernelAddr - KPAGESIZE; + MmPhysLastKernAddress = (ulong)(_heap_start + _heap_max); + ulong diffKernUsr = (ulong)USERSPACE - MmPhysLastKernAddress - KPAGESIZE; // Maximum VIRTUAL address in memory MmVirtLastAddress = phRamSize + diffKernUsr; @@ -132,13 +157,13 @@ void MmInitPaging(void) MmPT[index] = (ulong)curAddrPT | PRESENT; MmPhysicalPageTable[xedni] = (ulong)curAddrPT; MmStackGuards[0] = (ulong)curAddrPT; - DebugLog("\tStack Guard at %p\n", curAddrPT); + //DebugLog("\tStack Guard at %p\n", curAddrPT); } else if ((ulong)curAddrPT == (ulong)BtLoaderInfo.kernelEndAddr) { MmPT[index] = (ulong)curAddrPT | PRESENT; MmPhysicalPageTable[xedni] = (ulong)curAddrPT; MmStackGuards[1] = (ulong)curAddrPT; - DebugLog("\tStack Guard at %p\n", curAddrPT); + //DebugLog("\tStack Guard at %p\n", curAddrPT); } // SECTION .TEXT PROTECTION else if ((ulong)curAddrPT >= (ulong)&_text && (ulong)curAddrPT <= (ulong)&_text_end) { @@ -159,24 +184,24 @@ void MmInitPaging(void) //DebugLog("\tSection .rodata at %p\n", curAddrPT); } // While we're inside the kernel pages - else if ((ulong)curAddrPT <= lastKernelAddr) { + else if ((ulong)curAddrPT <= MmPhysLastKernAddress) { MmPT[index] = (ulong)curAddrPT | PRESENT | READWRITE; MmPhysicalPageTable[xedni] = (ulong)curAddrPT; - if ((ulong)curAddrPT == lastKernelAddr) { + if ((ulong)curAddrPT == MmPhysLastKernAddress) { //DebugLog("\tLast page of kernel at %p\n", curAddrPT); } } - // While we're inside the userspace pages - else if ((ulong)curAddrPT >= USERSPACE) { - MmPT[index] = ((ulong)curAddrPT - diffKernUsr) | PRESENT; // Not present for instance - xedni = (((ulong)curAddrPT - diffKernUsr) / ((ulong)KPAGESIZE)); - MmPhysicalPageTable[xedni] = (ulong)curAddrPT; + /* // While we're inside the userspace pages */ + /* else if ((ulong)curAddrPT >= USERSPACE) { */ + /* MmPT[index] = ((ulong)curAddrPT - diffKernUsr) | PRESENT; // Not present for instance */ + /* xedni = (((ulong)curAddrPT - diffKernUsr) / ((ulong)KPAGESIZE)); */ + /* //MmPhysicalPageTable[xedni] = (ulong)curAddrPT; */ - if ((ulong)curAddrPT == USERSPACE) { - DebugLog("\tUserspace at %p:%p\n", curAddrPT, curAddrPT - diffKernUsr); - } - } + /* if ((ulong)curAddrPT == USERSPACE) { */ + /* DebugLog("\tUserspace at %p:%p\n", curAddrPT, curAddrPT - diffKernUsr); */ + /* } */ + /* } */ else { MmPT[index] = 0; } @@ -239,7 +264,7 @@ void *MmTransPhyToVirtAddr(void* physicalAddr) ulong phyAddrPage = (ulong)physicalAddr & ( ~(KPAGESIZE - 1)); return (void*)( MmPhysicalPageTable[(ulong)physicalAddr / ((ulong)KPAGESIZE) - + ((ulong)physicalAddr - phyAddrPage) ] ); + ] + ((ulong)physicalAddr - phyAddrPage)); } // @@ -290,28 +315,6 @@ void MmUnmapPage(void* virtualAddr) KeFlushTlbSingle(*page); } -// -// Kernel Page allocator -// -void *MmKAllocPageBlock(void *start) { - pte_t *startPage = MmGetPageDescriptorFromVirtual(start); - - //for (ulong curPage = 0; curPage < ) - - return NULL; -} - -// -// User page allocator -// -void *MmUAllocPageBlock(void *start) { - pte_t *startPage = MmGetPageDescriptorFromVirtual(start); - - //for (ulong curPage = 0; curPage < ) - - return NULL; -} - //----------- // diff --git a/kaleid/kernel/mm/palloc.c b/kaleid/kernel/mm/palloc.c new file mode 100644 index 0000000..22a0fdf --- /dev/null +++ b/kaleid/kernel/mm/palloc.c @@ -0,0 +1,38 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Desc: Page allocator related functions // +// // +// // +// Copyright © 2018-2019 The OS/K Team // +// // +// This file is part of OS/K. // +// // +// OS/K is free software: you can redistribute it and/or modify // +// it under the terms of the GNU General Public License as published by // +// the Free Software Foundation, either version 3 of the License, or // +// any later version. // +// // +// OS/K is distributed in the hope that it will be useful, // +// but WITHOUT ANY WARRANTY//without even the implied warranty of // +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // +// GNU General Public License for more details. // +// // +// You should have received a copy of the GNU General Public License // +// along with OS/K. If not, see . // +//----------------------------------------------------------------------------// + +#include +#include +#include +#include +#include +#include + +//--------- + + + + +//--------- + diff --git a/kaleid/kernel/sh/shcmds.c b/kaleid/kernel/sh/shcmds.c index 0507e59..d74d7fd 100644 --- a/kaleid/kernel/sh/shcmds.c +++ b/kaleid/kernel/sh/shcmds.c @@ -23,7 +23,8 @@ //----------------------------------------------------------------------------// #include -#include +#include +#include #include #include #include diff --git a/kaleid/kernel/sh/testcmds.c b/kaleid/kernel/sh/testcmds.c index f528b71..271de10 100644 --- a/kaleid/kernel/sh/testcmds.c +++ b/kaleid/kernel/sh/testcmds.c @@ -23,7 +23,8 @@ //----------------------------------------------------------------------------// #include -#include +#include +#include #include #include #include @@ -204,6 +205,20 @@ error_t CmdPageTranslateVirtToPhy(int argc, char **argv, char *cmdline) return EOK; } +error_t CmdPageTranslatePhyToVirt(int argc, char **argv, char *cmdline) +{ + void *address = (void*)strtoul(argv[1], NULL, 16); + + /* if (!(void*)atoul(argv[1])) { */ + /* address = (ulong *)0x80000000; */ + /* } */ + + void *translation = MmTransPhyToVirtAddr(address); + + KernLog("Translation of %p is %p\n", address, translation); + return EOK; +} + enum { PRESENT = 1 << 0, @@ -236,29 +251,25 @@ error_t CmdPageUnmap(int argc, char **argv, char *cmdline) return EOK; } -error_t CmdPageTranslatePhyToVirt(int argc, char **argv, char *cmdline) +error_t CmdPageBlock(int argc, char **argv, char *cmdline) { - void *address = (void*)strtoul(argv[1], NULL, 16); + size_t size = (size_t)atoi(argv[1]); + bool usermode = (bool)atoi(argv[2]); - /* if (!(void*)atoul(argv[1])) { */ - /* address = (ulong *)0x80000000; */ - /* } */ + //MmGetPhyPageBlock(size, usermode); - void *translation = MmTransPhyToVirtAddr(address); - - KernLog("Translation of %p is %p\n", address, translation); return EOK; } error_t CmdPF(int argc, char **argv, char *cmdline) { - ulong *address = (ulong*)(ulong)strtoul(argv[1], NULL, 16); + register ulong *address = (ulong*)(ulong)strtoul(argv[1], NULL, 16); - KernLog("Provoking Page Fault at %#x\n", address); + KernLog("Test provoking a fault at %p\n", address); - KernLog("It contained %#x\n", *address); + KernLog("It contained %p\n", *address); *address = 1; - KernLog("Now it contains %#x\n", *address); + KernLog("Now it contains %p\n", *address); KernLog("No page fault : address was valid/present\n"); @@ -316,6 +327,7 @@ static Command_t testcmdtable[] = " virtual address (paging)"}, { "pmap", CmdPageMap, "Map a page to given physical addr" }, { "punmap", CmdPageUnmap, "Unmap a page" }, + { "pblck", CmdPageBlock, "Find a free block of pages" }, { "pf", CmdPF, "Provoke a PF. Usage: pfault
"}, { "shell", CmdShell, "Start a new shell (nested)", }, { "stkov", CmdStackOverflow, "Provoke a stack overflow" },