Merge branch 'BetterMemory' of github.com:os-k-team/os-k into BetterMemory
This commit is contained in:
commit
aeb1ca721b
2
Makefile
2
Makefile
|
@ -115,7 +115,7 @@ KernSources = kernel/ke/cpuid.c kernel/mm/paging.c \
|
||||||
kernel/sh/shell.c kernel/sh/shcmds.c \
|
kernel/sh/shell.c kernel/sh/shcmds.c \
|
||||||
kernel/sh/musage.c kernel/io/ata.c \
|
kernel/sh/musage.c kernel/io/ata.c \
|
||||||
kernel/sh/argv.c kernel/ke/pit.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))
|
KernObj=$(patsubst %.c,$(KOBJDIR)/%.o,$(KernSources))
|
||||||
KernDep=$(patsubst %.c,$(KOBJDIR)/%.d,$(KernSources))
|
KernDep=$(patsubst %.c,$(KOBJDIR)/%.d,$(KernSources))
|
||||||
|
|
|
@ -98,7 +98,6 @@
|
||||||
│ │ │ ├── ata.c
|
│ │ │ ├── ata.c
|
||||||
│ │ │ ├── cursor.c
|
│ │ │ ├── cursor.c
|
||||||
│ │ │ ├── keyb.c
|
│ │ │ ├── keyb.c
|
||||||
│ │ │ ├── scan.c
|
|
||||||
│ │ │ ├── spkr.c
|
│ │ │ ├── spkr.c
|
||||||
│ │ │ └── vga.c
|
│ │ │ └── vga.c
|
||||||
│ │ ├── ke
|
│ │ ├── ke
|
||||||
|
@ -160,4 +159,4 @@
|
||||||
├── ProjectTree
|
├── ProjectTree
|
||||||
└── README.md
|
└── README.md
|
||||||
|
|
||||||
28 directories, 107 files
|
28 directories, 106 files
|
||||||
|
|
|
@ -33,7 +33,7 @@
|
||||||
|
|
||||||
#define KPAGESIZE (4 * KB)
|
#define KPAGESIZE (4 * KB)
|
||||||
#define UPAGESIZE (4 * KB)
|
#define UPAGESIZE (4 * KB)
|
||||||
#define USERSPACE 0x80000000
|
#define USERSPACE 0x100000000
|
||||||
|
|
||||||
//----------------------------------------------------------------------------//
|
//----------------------------------------------------------------------------//
|
||||||
|
|
||||||
|
|
|
@ -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);
|
||||||
|
|
||||||
//----------------------------------------------------------------------------//
|
//----------------------------------------------------------------------------//
|
||||||
|
|
||||||
|
|
|
@ -78,6 +78,8 @@ void MmInitPaging(void)
|
||||||
ulong lastDirectoryAddr = 0;
|
ulong lastDirectoryAddr = 0;
|
||||||
ulong phDirSize = 0;
|
ulong phDirSize = 0;
|
||||||
|
|
||||||
|
KernLog("\tActivating paging...\n");
|
||||||
|
|
||||||
// Maximum PHYSICAL address in memory
|
// Maximum PHYSICAL address in memory
|
||||||
ulong phRamSize = memoryMap.freeRamSize + memoryMap.nonfreeRamSize;
|
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);
|
//DebugLog("\tPaging gap : %u MB (%p)\n\tLast virtual address %p\n", diffKernUsr / MB, diffKernUsr, MmVirtLastAddress);
|
||||||
|
|
||||||
memzero((void *)&MmPageMapLevel4[0], sizeof(MmPageMapLevel4));
|
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);
|
MmPhysicalPageTable = (ulong*)malloc(phDirSize);
|
||||||
//DebugLog("\t\tRam %u MB, pagesize %u KB, size %u MB\n", phRamSize / MB, KPAGESIZE / KB, phDirSize / MB);
|
//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
|
// SECTION .RODATA PROTECTION
|
||||||
else if ((ulong)curAddrPT >= (ulong)&_rodata && (ulong)curAddrPT <= (ulong)&_rodata_end) {
|
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;
|
MmPhysicalPageTable[xedni] = (ulong)curAddrPT;
|
||||||
//DebugLog("\tSection .rodata at %p\n", curAddrPT);
|
//DebugLog("\tSection .rodata at %p\n", curAddrPT);
|
||||||
}
|
}
|
||||||
|
@ -214,7 +216,7 @@ void MmInitPaging(void)
|
||||||
lastDirectoryAddr = (ulong)MmPT;
|
lastDirectoryAddr = (ulong)MmPT;
|
||||||
|
|
||||||
MmLoadPML4((void *)MmPageMapLevel4);
|
MmLoadPML4((void *)MmPageMapLevel4);
|
||||||
//MmEnableWriteProtect();
|
MmEnableWriteProtect();
|
||||||
|
|
||||||
DebugLog("\tPage table size : %u MB\n", (lastDirectoryAddr - firstDirectoryAddr + phDirSize)/MB);
|
DebugLog("\tPage table size : %u MB\n", (lastDirectoryAddr - firstDirectoryAddr + phDirSize)/MB);
|
||||||
}
|
}
|
||||||
|
@ -224,17 +226,17 @@ void MmInitPaging(void)
|
||||||
//
|
//
|
||||||
static pte_t *MmGetPageDescriptorFromVirtual(void *virtualAddr)
|
static pte_t *MmGetPageDescriptorFromVirtual(void *virtualAddr)
|
||||||
{
|
{
|
||||||
ulong virtAddrPage = (ulong)virtualAddr & ( ~(KPAGESIZE - 1));
|
ulong virtAddrPage = (ulong)virtualAddr & ( ~((KPAGESIZE - 1) | NX));
|
||||||
|
|
||||||
if (virtAddrPage > MmVirtLastAddress) {
|
if (virtAddrPage > MmVirtLastAddress) {
|
||||||
KeStartPanic("MmSetPage() Out of bound of the address space !");
|
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);
|
//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);
|
//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);
|
//DebugLog("pt\t: %p\n", pt);
|
||||||
|
|
||||||
pte_t *page = &pt[(virtAddrPage / ((ulong)KPAGESIZE)) % 512];
|
pte_t *page = &pt[(virtAddrPage / ((ulong)KPAGESIZE)) % 512];
|
||||||
|
@ -249,19 +251,19 @@ static pte_t *MmGetPageDescriptorFromVirtual(void *virtualAddr)
|
||||||
//
|
//
|
||||||
void *MmTransVirtToPhyAddr(void* virtualAddr)
|
void *MmTransVirtToPhyAddr(void* virtualAddr)
|
||||||
{
|
{
|
||||||
ulong virtAddrPage = (ulong)virtualAddr & ( ~(KPAGESIZE - 1));
|
ulong virtAddrPage = (ulong)virtualAddr & ( ~((KPAGESIZE - 1) | NX));
|
||||||
pte_t *page = MmGetPageDescriptorFromVirtual(virtualAddr);
|
pte_t *page = MmGetPageDescriptorFromVirtual(virtualAddr);
|
||||||
|
|
||||||
if (*page == (*page & ~(KPAGESIZE - 1))) {
|
if (*page == (*page & ~((KPAGESIZE - 1) | NX))) {
|
||||||
return NULL;
|
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)
|
void *MmTransPhyToVirtAddr(void* physicalAddr)
|
||||||
{
|
{
|
||||||
ulong phyAddrPage = (ulong)physicalAddr & ( ~(KPAGESIZE - 1));
|
ulong phyAddrPage = (ulong)physicalAddr & ( ~((KPAGESIZE - 1) | NX));
|
||||||
return (void*)( MmPhysicalPageTable[(ulong)physicalAddr
|
return (void*)( MmPhysicalPageTable[(ulong)physicalAddr
|
||||||
/ ((ulong)KPAGESIZE)
|
/ ((ulong)KPAGESIZE)
|
||||||
] + ((ulong)physicalAddr - phyAddrPage));
|
] + ((ulong)physicalAddr - phyAddrPage));
|
||||||
|
@ -298,7 +300,7 @@ void MmMapPage(void* virtualAddr, void* physicalAddr, ulong flags)
|
||||||
{
|
{
|
||||||
pte_t *page = MmGetPageDescriptorFromVirtual(virtualAddr);
|
pte_t *page = MmGetPageDescriptorFromVirtual(virtualAddr);
|
||||||
|
|
||||||
*page = ((ulong)physicalAddr & ~(KPAGESIZE - 1)) | flags;
|
*page = ((ulong)physicalAddr & ~((KPAGESIZE - 1) | NX)) | flags;
|
||||||
|
|
||||||
KeFlushTlbSingle(*page);
|
KeFlushTlbSingle(*page);
|
||||||
}
|
}
|
||||||
|
@ -415,5 +417,5 @@ static void PagingHandler(ISRFrame_t *regs)
|
||||||
void MmActivatePageHandler(void)
|
void MmActivatePageHandler(void)
|
||||||
{
|
{
|
||||||
KeRegisterISR(PagingHandler, 0xe);
|
KeRegisterISR(PagingHandler, 0xe);
|
||||||
DebugLog("\tPaging activated\n");
|
//DebugLog("\tPage handler activated\n");
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -24,6 +24,7 @@
|
||||||
|
|
||||||
#include <vers.h>
|
#include <vers.h>
|
||||||
#include <mm/paging.h>
|
#include <mm/paging.h>
|
||||||
|
#include <mm/palloc.h>
|
||||||
#include <mm/map.h>
|
#include <mm/map.h>
|
||||||
#include <io/ata.h>
|
#include <io/ata.h>
|
||||||
#include <io/vga.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]);
|
size_t size = (size_t)atoi(argv[1]);
|
||||||
bool usermode = (bool)atoi(argv[2]);
|
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)
|
error_t CmdPF(int argc, char **argv, char *cmdline)
|
||||||
|
|
Loading…
Reference in New Issue