1
0
mirror of https://gitlab.os-k.eu/os-k-team/os-k.git synced 2023-08-25 14:03:10 +02:00

Merge branch 'BetterMemory' of github.com:os-k-team/os-k into BetterMemory

This commit is contained in:
Adrien Bourmault 2020-01-13 21:18:36 +01:00
commit aeb1ca721b
7 changed files with 66 additions and 21 deletions

View File

@ -115,7 +115,7 @@ KernSources = kernel/ke/cpuid.c kernel/mm/paging.c \
kernel/sh/shell.c kernel/sh/shcmds.c \
kernel/sh/musage.c kernel/io/ata.c \
kernel/sh/argv.c kernel/ke/pit.c \
kernel/sh/testcmds.c
kernel/sh/testcmds.c kernel/mm/palloc.c
KernObj=$(patsubst %.c,$(KOBJDIR)/%.o,$(KernSources))
KernDep=$(patsubst %.c,$(KOBJDIR)/%.d,$(KernSources))

View File

@ -98,7 +98,6 @@
│   │   │   ├── ata.c
│   │   │   ├── cursor.c
│   │   │   ├── keyb.c
│   │   │   ├── scan.c
│   │   │   ├── spkr.c
│   │   │   └── vga.c
│   │   ├── ke
@ -160,4 +159,4 @@
├── ProjectTree
└── README.md
28 directories, 107 files
28 directories, 106 files

View File

@ -33,7 +33,7 @@
#define KPAGESIZE (4 * KB)
#define UPAGESIZE (4 * KB)
#define USERSPACE 0x80000000
#define USERSPACE 0x100000000
//----------------------------------------------------------------------------//

View File

@ -31,7 +31,15 @@
//----------------------------------------------------------------------------//
typedef struct AllocatedPage_t{
void *phyAddress;
ulong id;
struct AllocatedPage_t *next;
} AllocatedPage_t;
//----------------------------------------------------------------------------//
error_t MmGetFreePageFrame(void **framePtr, size_t *pageNumber, size_t size);
//----------------------------------------------------------------------------//

View File

@ -78,6 +78,8 @@ void MmInitPaging(void)
ulong lastDirectoryAddr = 0;
ulong phDirSize = 0;
KernLog("\tActivating paging...\n");
// Maximum PHYSICAL address in memory
ulong phRamSize = memoryMap.freeRamSize + memoryMap.nonfreeRamSize;
@ -91,7 +93,7 @@ void MmInitPaging(void)
//DebugLog("\tPaging gap : %u MB (%p)\n\tLast virtual address %p\n", diffKernUsr / MB, diffKernUsr, MmVirtLastAddress);
memzero((void *)&MmPageMapLevel4[0], sizeof(MmPageMapLevel4));
phDirSize = ((phRamSize / KPAGESIZE)*sizeof(ulong) + KPAGESIZE) & ( ~(KPAGESIZE - 1));
phDirSize = ((phRamSize / KPAGESIZE)*sizeof(ulong) + KPAGESIZE) & ( ~((KPAGESIZE - 1) | NX));
MmPhysicalPageTable = (ulong*)malloc(phDirSize);
//DebugLog("\t\tRam %u MB, pagesize %u KB, size %u MB\n", phRamSize / MB, KPAGESIZE / KB, phDirSize / MB);
@ -179,7 +181,7 @@ void MmInitPaging(void)
}
// SECTION .RODATA PROTECTION
else if ((ulong)curAddrPT >= (ulong)&_rodata && (ulong)curAddrPT <= (ulong)&_rodata_end) {
MmPT[index] = (ulong)curAddrPT | PRESENT | WRITETHR | NX;
MmPT[index] = (ulong)curAddrPT | PRESENT | NX;
MmPhysicalPageTable[xedni] = (ulong)curAddrPT;
//DebugLog("\tSection .rodata at %p\n", curAddrPT);
}
@ -214,7 +216,7 @@ void MmInitPaging(void)
lastDirectoryAddr = (ulong)MmPT;
MmLoadPML4((void *)MmPageMapLevel4);
//MmEnableWriteProtect();
MmEnableWriteProtect();
DebugLog("\tPage table size : %u MB\n", (lastDirectoryAddr - firstDirectoryAddr + phDirSize)/MB);
}
@ -224,17 +226,17 @@ void MmInitPaging(void)
//
static pte_t *MmGetPageDescriptorFromVirtual(void *virtualAddr)
{
ulong virtAddrPage = (ulong)virtualAddr & ( ~(KPAGESIZE - 1));
ulong virtAddrPage = (ulong)virtualAddr & ( ~((KPAGESIZE - 1) | NX));
if (virtAddrPage > MmVirtLastAddress) {
KeStartPanic("MmSetPage() Out of bound of the address space !");
}
pdpe_t *pdp = (pdpe_t*)((ulong)MmPageMapLevel4[(virtAddrPage / ((ulong)KPAGESIZE * 0x8000000)) % 512] & ~(KPAGESIZE - 1));
pdpe_t *pdp = (pdpe_t*)((ulong)MmPageMapLevel4[(virtAddrPage / ((ulong)KPAGESIZE * 0x8000000)) % 512] & ~((KPAGESIZE - 1) | NX | NX));
//DebugLog("pdp\t: %p\n", pdp);
pde_t *pd = (pde_t*)( (ulong)pdp[(virtAddrPage / ((ulong)KPAGESIZE * 0x40000)) % 512] & ~(KPAGESIZE - 1));
pde_t *pd = (pde_t*)( (ulong)pdp[(virtAddrPage / ((ulong)KPAGESIZE * 0x40000)) % 512] & ~((KPAGESIZE - 1) | NX));
//DebugLog("pd\t: %p\n", pd);
pte_t *pt = (pte_t*)( (ulong)pd[(virtAddrPage / ((ulong)KPAGESIZE * 0x200)) % 512] & ~(KPAGESIZE - 1));
pte_t *pt = (pte_t*)( (ulong)pd[(virtAddrPage / ((ulong)KPAGESIZE * 0x200)) % 512] & ~((KPAGESIZE - 1) | NX));
//DebugLog("pt\t: %p\n", pt);
pte_t *page = &pt[(virtAddrPage / ((ulong)KPAGESIZE)) % 512];
@ -249,19 +251,19 @@ static pte_t *MmGetPageDescriptorFromVirtual(void *virtualAddr)
//
void *MmTransVirtToPhyAddr(void* virtualAddr)
{
ulong virtAddrPage = (ulong)virtualAddr & ( ~(KPAGESIZE - 1));
ulong virtAddrPage = (ulong)virtualAddr & ( ~((KPAGESIZE - 1) | NX));
pte_t *page = MmGetPageDescriptorFromVirtual(virtualAddr);
if (*page == (*page & ~(KPAGESIZE - 1))) {
if (*page == (*page & ~((KPAGESIZE - 1) | NX))) {
return NULL;
}
return (void*)((*page & ~(KPAGESIZE - 1))+ ((ulong)virtualAddr - (ulong)virtAddrPage));
return (void*)((*page & ~((KPAGESIZE - 1) | NX))+ ((ulong)virtualAddr - (ulong)virtAddrPage));
}
void *MmTransPhyToVirtAddr(void* physicalAddr)
{
ulong phyAddrPage = (ulong)physicalAddr & ( ~(KPAGESIZE - 1));
ulong phyAddrPage = (ulong)physicalAddr & ( ~((KPAGESIZE - 1) | NX));
return (void*)( MmPhysicalPageTable[(ulong)physicalAddr
/ ((ulong)KPAGESIZE)
] + ((ulong)physicalAddr - phyAddrPage));
@ -298,7 +300,7 @@ void MmMapPage(void* virtualAddr, void* physicalAddr, ulong flags)
{
pte_t *page = MmGetPageDescriptorFromVirtual(virtualAddr);
*page = ((ulong)physicalAddr & ~(KPAGESIZE - 1)) | flags;
*page = ((ulong)physicalAddr & ~((KPAGESIZE - 1) | NX)) | flags;
KeFlushTlbSingle(*page);
}
@ -415,5 +417,5 @@ static void PagingHandler(ISRFrame_t *regs)
void MmActivatePageHandler(void)
{
KeRegisterISR(PagingHandler, 0xe);
DebugLog("\tPaging activated\n");
//DebugLog("\tPage handler activated\n");
}

View File

@ -31,8 +31,42 @@
//---------
enum
{
Whatever = 1UL << 52,
Whatever2 = 1UL << 62
};
static AllocatedPage_t busyPagesList = { (void*)0, 0, (AllocatedPage_t*)0 };
//---------
static bool MmIsPageBusy(void *phyAddr) {
AllocatedPage_t *busyPage = &busyPagesList;
bool isBusy = false;
while(busyPage->next) {
busyPages = busyPage->next;
DebugLog("Busy page at %p\n", busyPage->phyAddress);
if (phyPageAddr == busyPage->phyAddress) {
isBusy = true;
break;
}
}
return isBusy;
}
//
// Returns a structure that describes a pageframe
//
error_t MmGetFreePageFrame(void **framePtr, size_t *pageNumber, size_t size) {
*pageNumber = (ulong)size / KPAGESIZE;
return EOK;
}

View File

@ -24,6 +24,7 @@
#include <vers.h>
#include <mm/paging.h>
#include <mm/palloc.h>
#include <mm/map.h>
#include <io/ata.h>
#include <io/vga.h>
@ -256,9 +257,10 @@ error_t CmdPageBlock(int argc, char **argv, char *cmdline)
size_t size = (size_t)atoi(argv[1]);
bool usermode = (bool)atoi(argv[2]);
//MmGetPhyPageBlock(size, usermode);
size_t pageNum = 0;
return EOK;
error_t err = MmGetFreePageFrame((void**)0x12345678, &pageNum, (size_t)4096);
return err;
}
error_t CmdPF(int argc, char **argv, char *cmdline)