Merge branch 'BetterMemory'

This commit is contained in:
Adrien Bourmault 2020-01-12 17:30:49 +01:00
commit ac72e556b3
15 changed files with 344 additions and 164 deletions

View File

@ -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

View File

@ -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)

View File

@ -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 <kernel.h>
#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

85
include/mm/map.h Normal file
View File

@ -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 <https://www.gnu.org/licenses/>. //
//----------------------------------------------------------------------------//
#ifndef _KERNEL_H
#include <kernel.h>
#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

93
include/mm/paging.h Normal file
View File

@ -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 <https://www.gnu.org/licenses/>. //
//----------------------------------------------------------------------------//
#ifndef _KERNEL_H
#include <kernel.h>
#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

38
include/mm/palloc.h Normal file
View File

@ -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 <https://www.gnu.org/licenses/>. //
//----------------------------------------------------------------------------//
#ifndef _KERNEL_H
#include <kernel.h>
#endif
#ifndef _MM_PALLOC_H
#define _MM_PALLOC_H
//----------------------------------------------------------------------------//
//----------------------------------------------------------------------------//
#endif

View File

@ -22,7 +22,9 @@
// along with OS/K. If not, see <https://www.gnu.org/licenses/>. //
//----------------------------------------------------------------------------//
#include <mm/mm.h>
#include <mm/gdt.h>
#include <mm/map.h>
#include <mm/paging.h>
#include <mm/heap.h>
#include <ke/idt.h>
#include <ke/time.h>

View File

@ -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

View File

@ -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 <https://www.gnu.org/licenses/>. //
//----------------------------------------------------------------------------//
#include <mm/mm.h>
#include <mm/gdt.h>
#include <mm/paging.h>
#include <init/boot.h>
GdtPtr_t gdtPtr;

View File

@ -22,7 +22,7 @@
// along with OS/K. If not, see <https://www.gnu.org/licenses/>. //
//----------------------------------------------------------------------------//
#include <mm/mm.h>
#include <mm/map.h>
#include <mm/heap.h>
#include <ex/lock.h>
#include <init/boot.h>

View File

@ -22,7 +22,7 @@
// along with OS/K. If not, see <https://www.gnu.org/licenses/>. //
//----------------------------------------------------------------------------//
#include <mm/mm.h>
#include <mm/map.h>
#include <init/boot.h>
#include <init/mboot.h>
#include <io/vga.h>

View File

@ -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 <https://www.gnu.org/licenses/>. //
//----------------------------------------------------------------------------//
#include <kernel.h>
#include <init/boot.h>
#include <ke/idt.h>
#include <ex/malloc.h>
#include <mm/heap.h>
#include <mm/mm.h>
#include <ke/idt.h>
#include <mm/paging.h>
#include <mm/map.h>
#include <lib/buf.h>
#include <io/vga.h>
#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;
}
//-----------
//

38
kaleid/kernel/mm/palloc.c Normal file
View File

@ -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 <https://www.gnu.org/licenses/>. //
//----------------------------------------------------------------------------//
#include <kernel.h>
#include <init/boot.h>
#include <ex/malloc.h>
#include <mm/paging.h>
#include <mm/palloc.h>
#include <io/vga.h>
//---------
//---------

View File

@ -23,7 +23,8 @@
//----------------------------------------------------------------------------//
#include <vers.h>
#include <mm/mm.h>
#include <mm/paging.h>
#include <mm/map.h>
#include <io/ata.h>
#include <io/vga.h>
#include <io/spkr.h>

View File

@ -23,7 +23,8 @@
//----------------------------------------------------------------------------//
#include <vers.h>
#include <mm/mm.h>
#include <mm/paging.h>
#include <mm/map.h>
#include <io/ata.h>
#include <io/vga.h>
#include <io/spkr.h>
@ -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 <address>"},
{ "shell", CmdShell, "Start a new shell (nested)", },
{ "stkov", CmdStackOverflow, "Provoke a stack overflow" },