2020-01-12 16:53:23 +01:00
|
|
|
//----------------------------------------------------------------------------//
|
2020-09-27 17:33:48 +02:00
|
|
|
// OS on Kaleid //
|
2020-01-12 16:53:23 +01:00
|
|
|
// //
|
|
|
|
// Desc: Paging memory related functions //
|
|
|
|
// //
|
|
|
|
// //
|
2020-02-06 14:23:26 +01:00
|
|
|
// Copyright © 2018-2020 The OS/K Team //
|
2020-01-12 16:53:23 +01:00
|
|
|
// //
|
|
|
|
// 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/>. //
|
2020-01-12 17:21:12 +01:00
|
|
|
//----------------------------------------------------------------------------//
|
2020-01-12 16:53:23 +01:00
|
|
|
|
2019-05-14 14:39:35 +02:00
|
|
|
#include <kernel.h>
|
2019-05-15 15:55:57 +02:00
|
|
|
#include <init/boot.h>
|
2020-01-12 17:21:12 +01:00
|
|
|
#include <ke/idt.h>
|
2019-05-15 19:11:47 +02:00
|
|
|
#include <ex/malloc.h>
|
2020-01-09 18:19:49 +01:00
|
|
|
#include <mm/heap.h>
|
2020-01-12 16:53:23 +01:00
|
|
|
#include <mm/paging.h>
|
|
|
|
#include <mm/map.h>
|
2020-02-19 22:19:58 +01:00
|
|
|
#include <libbuf.h>
|
2019-05-19 00:07:01 +02:00
|
|
|
#include <io/vga.h>
|
2020-01-18 13:40:56 +01:00
|
|
|
#include <ke/time.h>
|
2019-05-19 00:07:01 +02:00
|
|
|
|
2019-05-14 14:39:35 +02:00
|
|
|
//-----------
|
|
|
|
|
2020-01-17 14:09:45 +01:00
|
|
|
static pml4_t MmPageMapLevel4[512] __attribute__((__aligned__(KPAGESIZE)));
|
|
|
|
static ulong *MmPhysicalPageTable __attribute__((__aligned__(KPAGESIZE)));
|
2019-05-14 14:39:35 +02:00
|
|
|
|
2019-12-21 13:55:02 +01:00
|
|
|
extern ulong _text;
|
|
|
|
extern ulong _text_end;
|
|
|
|
extern ulong _rodata;
|
|
|
|
extern ulong _rodata_end;
|
|
|
|
extern ulong _data;
|
|
|
|
extern ulong _data_end;
|
|
|
|
|
2020-01-11 00:05:34 +01:00
|
|
|
extern MemoryMap_t memoryMap;
|
|
|
|
|
2020-01-14 16:54:47 +01:00
|
|
|
static ulong MmStackGuards[2] = { 0 };
|
2020-01-09 18:19:49 +01:00
|
|
|
ulong MmVirtLastAddress = 0;
|
2020-01-11 00:05:34 +01:00
|
|
|
ulong MmPhysLastKernAddress = 0;
|
2020-01-09 18:19:49 +01:00
|
|
|
|
2020-01-09 22:34:38 +01:00
|
|
|
//-----------
|
|
|
|
|
2019-05-15 15:55:57 +02:00
|
|
|
//
|
|
|
|
// Creates our new page table structure and loads it
|
2019-05-18 13:39:58 +02:00
|
|
|
//
|
2019-05-14 14:39:35 +02:00
|
|
|
void MmInitPaging(void)
|
|
|
|
{
|
2020-01-09 20:58:57 +01:00
|
|
|
pdpe_t *MmPDP = NULL;
|
|
|
|
pde_t *MmPD = NULL;
|
|
|
|
pte_t *MmPT = NULL;
|
2020-01-16 15:51:03 +01:00
|
|
|
ulong index, xedni;
|
|
|
|
ulong curAddrPML4;
|
|
|
|
ulong curAddrPDP;
|
|
|
|
ulong curAddrPD;
|
|
|
|
ulong curAddrPT;
|
|
|
|
|
2020-01-09 18:19:49 +01:00
|
|
|
ulong firstDirectoryAddr = 0;
|
|
|
|
ulong lastDirectoryAddr = 0;
|
2020-01-10 13:36:33 +01:00
|
|
|
ulong phDirSize = 0;
|
2020-01-09 18:19:49 +01:00
|
|
|
|
2020-02-06 13:18:22 +01:00
|
|
|
DebugLog("Activating paging...\n");
|
2020-01-13 20:48:24 +01:00
|
|
|
|
2020-01-09 18:19:49 +01:00
|
|
|
// Maximum PHYSICAL address in memory
|
2019-05-16 23:56:23 +02:00
|
|
|
ulong phRamSize = memoryMap.freeRamSize + memoryMap.nonfreeRamSize;
|
2019-05-15 21:02:16 +02:00
|
|
|
|
2020-01-09 18:19:49 +01:00
|
|
|
// Difference between the end of kernel and the begin of userspace
|
2020-01-11 00:05:34 +01:00
|
|
|
MmPhysLastKernAddress = (ulong)(_heap_start + _heap_max);
|
2020-01-09 18:19:49 +01:00
|
|
|
|
2020-01-16 00:08:04 +01:00
|
|
|
// Size of physical table
|
|
|
|
phDirSize = (((phRamSize + KPAGESIZE) / KPAGESIZE)*sizeof(ulong));
|
|
|
|
|
2020-01-09 18:19:49 +01:00
|
|
|
// Maximum VIRTUAL address in memory
|
2020-01-19 01:44:29 +01:00
|
|
|
MmVirtLastAddress = phRamSize;
|
2020-01-09 18:19:49 +01:00
|
|
|
|
2020-01-16 00:08:04 +01:00
|
|
|
// Alloc structures
|
|
|
|
memzero((void *)&MmPageMapLevel4[0], 512*sizeof(ulong));
|
2020-01-20 19:38:27 +01:00
|
|
|
MmPhysicalPageTable = memalign(phDirSize, KPAGESIZE);
|
2020-01-16 15:51:03 +01:00
|
|
|
|
2020-01-18 00:48:05 +01:00
|
|
|
//DebugLog("\t\t\t\tPhysical map addr : %p\n", MmPhysicalPageTable);
|
2020-01-10 13:36:33 +01:00
|
|
|
|
2020-01-16 15:51:03 +01:00
|
|
|
for (curAddrPML4 = 0;
|
|
|
|
curAddrPML4 < 512 * KPAGESIZE * 0x8000000;
|
2020-01-09 18:19:49 +01:00
|
|
|
curAddrPML4 += ((ulong)KPAGESIZE * 0x8000000)) {
|
|
|
|
// Create an entry in PML4 each 512GB
|
|
|
|
// 0x8000000 = 512 ^ 3
|
|
|
|
|
2020-01-16 15:51:03 +01:00
|
|
|
index = (curAddrPML4 / ((ulong)KPAGESIZE * 0x8000000)) % 512;
|
|
|
|
|
2020-01-19 01:44:29 +01:00
|
|
|
if (curAddrPML4 > MmPhysLastKernAddress) {
|
2020-01-16 15:51:03 +01:00
|
|
|
MmPageMapLevel4[index] = (pdpe_t *)0;
|
2020-01-19 01:44:29 +01:00
|
|
|
////DebugLog("PML4 %d\n", index);
|
2020-01-16 15:51:03 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-01-20 19:38:27 +01:00
|
|
|
MmPDP = memalign(512*sizeof(pde_t), KPAGESIZE);
|
2020-01-09 18:19:49 +01:00
|
|
|
|
|
|
|
if (!firstDirectoryAddr) {
|
|
|
|
firstDirectoryAddr = (ulong)MmPDP;
|
|
|
|
}
|
|
|
|
|
|
|
|
//DebugLog("\t\t\t\tPDP %d : %p\n", index, MmPDP);
|
2020-01-20 20:05:02 +01:00
|
|
|
MmPageMapLevel4[index] = (pdpe_t *)((ulong)MmPDP | PRESENT
|
|
|
|
| READWRITE);
|
2020-01-09 18:19:49 +01:00
|
|
|
|
2020-01-16 15:51:03 +01:00
|
|
|
for (curAddrPDP = curAddrPML4;
|
|
|
|
curAddrPDP < (curAddrPML4 + ((ulong)KPAGESIZE * 0x8000000));
|
2020-01-09 18:19:49 +01:00
|
|
|
curAddrPDP += ((ulong)KPAGESIZE * 0x40000)) {
|
|
|
|
// Create an intry in PDP each 1GB
|
|
|
|
// 0x40000 = 512 ^ 2
|
|
|
|
|
2020-01-16 15:51:03 +01:00
|
|
|
index = (curAddrPDP / ((ulong)KPAGESIZE * 0x40000)) % 512;
|
|
|
|
|
2020-01-19 01:44:29 +01:00
|
|
|
if (curAddrPDP > MmPhysLastKernAddress) {
|
2020-01-16 15:51:03 +01:00
|
|
|
MmPDP[index] = (pde_t *)0;
|
|
|
|
//DebugLog("PDP %d\n", index);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-01-20 19:38:27 +01:00
|
|
|
MmPD = memalign(512*sizeof(pde_t), KPAGESIZE);
|
2020-01-09 18:19:49 +01:00
|
|
|
|
|
|
|
index = (curAddrPDP / ((ulong)KPAGESIZE * 0x40000)) % 512;
|
|
|
|
|
|
|
|
//DebugLog("\t\t\t\tPD %d : %p\n", index, MmPD);
|
|
|
|
MmPDP[index] = (pde_t *)((ulong)MmPD | PRESENT | READWRITE);
|
|
|
|
|
2020-01-16 15:51:03 +01:00
|
|
|
for (curAddrPD = curAddrPDP;
|
|
|
|
curAddrPD < (curAddrPDP + ((ulong)KPAGESIZE * 0x40000));
|
2020-01-09 18:19:49 +01:00
|
|
|
curAddrPD += ((ulong)KPAGESIZE * 0x200)) {
|
|
|
|
// Create an intry in PD each 2MB
|
|
|
|
// 0x200 = 512
|
|
|
|
|
2020-01-20 20:05:02 +01:00
|
|
|
index = (curAddrPD / ((ulong)KPAGESIZE * 0x200))
|
|
|
|
% 512;
|
2020-01-09 18:19:49 +01:00
|
|
|
|
2020-01-19 01:44:29 +01:00
|
|
|
if (curAddrPD > MmPhysLastKernAddress) {
|
2020-01-16 15:51:03 +01:00
|
|
|
MmPD[index] = (pte_t *)0;
|
|
|
|
//DebugLog("PD %d\n", index);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-01-20 19:38:27 +01:00
|
|
|
MmPT = memalign(512*sizeof(pte_t), KPAGESIZE);
|
2020-01-16 15:51:03 +01:00
|
|
|
|
2020-01-09 18:19:49 +01:00
|
|
|
//DebugLog("\t\t\t\tPT %d : %p\n", index, MmPT);
|
2020-01-20 20:05:02 +01:00
|
|
|
MmPD[index] = (pte_t *)((ulong)MmPT | PRESENT
|
|
|
|
| READWRITE);
|
2020-01-09 18:19:49 +01:00
|
|
|
|
2020-01-16 15:51:03 +01:00
|
|
|
for (curAddrPT = curAddrPD;
|
|
|
|
curAddrPT < (curAddrPD + ((ulong)KPAGESIZE * 0x200));
|
2020-01-09 18:19:49 +01:00
|
|
|
curAddrPT += (ulong)KPAGESIZE) {
|
|
|
|
// Create an entry in PT each page of 4KB
|
|
|
|
|
|
|
|
index = (curAddrPT / ((ulong)KPAGESIZE)) % 512;
|
2020-01-10 19:25:33 +01:00
|
|
|
xedni = (curAddrPT / ((ulong)KPAGESIZE));
|
2020-01-09 18:19:49 +01:00
|
|
|
|
|
|
|
// STACK GUARD PAGE */
|
2020-01-20 20:05:02 +01:00
|
|
|
if ((ulong)curAddrPT ==
|
|
|
|
(ulong)BtLoaderInfo.stackEndAddr) {
|
2020-01-09 18:19:49 +01:00
|
|
|
MmPT[index] = (ulong)curAddrPT | PRESENT;
|
2020-01-10 19:25:33 +01:00
|
|
|
MmPhysicalPageTable[xedni] = (ulong)curAddrPT;
|
2020-01-09 18:19:49 +01:00
|
|
|
MmStackGuards[0] = (ulong)curAddrPT;
|
2020-02-06 13:18:22 +01:00
|
|
|
DebugLog("Stack Guard at %p\n", curAddrPT);
|
2020-01-09 18:19:49 +01:00
|
|
|
}
|
2020-01-20 20:05:02 +01:00
|
|
|
else if ((ulong)curAddrPT ==
|
|
|
|
(ulong)BtLoaderInfo.kernelEndAddr) {
|
2020-01-09 18:19:49 +01:00
|
|
|
MmPT[index] = (ulong)curAddrPT | PRESENT;
|
2020-01-10 19:25:33 +01:00
|
|
|
MmPhysicalPageTable[xedni] = (ulong)curAddrPT;
|
2020-01-09 18:19:49 +01:00
|
|
|
MmStackGuards[1] = (ulong)curAddrPT;
|
2020-02-06 13:18:22 +01:00
|
|
|
DebugLog("Stack Guard at %p\n", curAddrPT);
|
2020-01-09 18:19:49 +01:00
|
|
|
}
|
|
|
|
// SECTION .TEXT PROTECTION
|
2020-01-20 20:05:02 +01:00
|
|
|
else if ((ulong)curAddrPT >= (ulong)&_text
|
|
|
|
&& (ulong)curAddrPT <= (ulong)&_text_end) {
|
2020-01-09 18:19:49 +01:00
|
|
|
MmPT[index] = (ulong)curAddrPT | PRESENT;
|
2020-01-10 19:25:33 +01:00
|
|
|
MmPhysicalPageTable[xedni] = (ulong)curAddrPT;
|
2020-02-06 13:18:22 +01:00
|
|
|
DebugLog("Section .text at %p\n", curAddrPT);
|
2020-01-09 18:19:49 +01:00
|
|
|
}
|
|
|
|
// SECTION .DATA PROTECTION
|
2020-01-20 20:05:02 +01:00
|
|
|
else if ((ulong)curAddrPT >= (ulong)&_data
|
|
|
|
&& (ulong)curAddrPT <= (ulong)&_data_end) {
|
|
|
|
MmPT[index] = (ulong)curAddrPT | PRESENT
|
|
|
|
| WRITETHR
|
|
|
|
| READWRITE
|
|
|
|
| NX;
|
2020-01-10 19:25:33 +01:00
|
|
|
MmPhysicalPageTable[xedni] = (ulong)curAddrPT;
|
2020-02-06 13:18:22 +01:00
|
|
|
DebugLog("Section .data at %p\n", curAddrPT);
|
2020-01-09 18:19:49 +01:00
|
|
|
}
|
|
|
|
// SECTION .RODATA PROTECTION
|
2020-01-20 20:05:02 +01:00
|
|
|
else if ((ulong)curAddrPT >= (ulong)&_rodata
|
|
|
|
&& (ulong)curAddrPT <= (ulong)&_rodata_end) {
|
2020-01-13 16:12:30 +01:00
|
|
|
MmPT[index] = (ulong)curAddrPT | PRESENT | NX;
|
2020-01-10 19:25:33 +01:00
|
|
|
MmPhysicalPageTable[xedni] = (ulong)curAddrPT;
|
2020-02-06 13:18:22 +01:00
|
|
|
DebugLog("Section .rodata at %p\n", curAddrPT);
|
2020-01-09 18:19:49 +01:00
|
|
|
}
|
2020-01-09 20:58:57 +01:00
|
|
|
// While we're inside the kernel pages
|
2020-01-11 00:05:34 +01:00
|
|
|
else if ((ulong)curAddrPT <= MmPhysLastKernAddress) {
|
2020-01-20 20:05:02 +01:00
|
|
|
MmPT[index] = (ulong)curAddrPT | PRESENT
|
|
|
|
| READWRITE;
|
2020-01-10 19:25:33 +01:00
|
|
|
MmPhysicalPageTable[xedni] = (ulong)curAddrPT;
|
2020-01-09 18:19:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-15 11:15:21 +02:00
|
|
|
}
|
2020-01-09 20:58:57 +01:00
|
|
|
lastDirectoryAddr = (ulong)MmPT;
|
2019-05-15 11:15:21 +02:00
|
|
|
|
2020-01-09 18:19:49 +01:00
|
|
|
MmLoadPML4((void *)MmPageMapLevel4);
|
2020-01-13 20:48:24 +01:00
|
|
|
MmEnableWriteProtect();
|
2020-01-09 20:58:57 +01:00
|
|
|
|
2020-02-12 17:51:14 +01:00
|
|
|
DebugLog("Page table size : %u MB\n",
|
|
|
|
(lastDirectoryAddr - firstDirectoryAddr + phDirSize)/MB);
|
2019-05-18 13:39:58 +02:00
|
|
|
}
|
|
|
|
|
2020-01-09 20:58:57 +01:00
|
|
|
//
|
2020-01-09 22:34:38 +01:00
|
|
|
// Get a page from an address
|
2020-01-09 20:58:57 +01:00
|
|
|
//
|
2020-01-20 01:17:10 +01:00
|
|
|
ulong *MmGetPageDescriptorFromVirtual(void *virtualAddr)
|
2020-01-09 20:58:57 +01:00
|
|
|
{
|
2020-01-20 20:05:02 +01:00
|
|
|
// Select bit from 39 to 47
|
|
|
|
register ulong pml4Index = ((ulong)virtualAddr & 0xFF8000000000) >> 39;
|
|
|
|
// Select bit from 30 to 39
|
|
|
|
register ulong pdpIndex = ((ulong)virtualAddr & 0x7FC0000000) >> 30;
|
|
|
|
// etc etc
|
|
|
|
register ulong pdIndex = ((ulong)virtualAddr & 0x3FE00000) >> 21;
|
|
|
|
// etc
|
|
|
|
register ulong ptIndex = ((ulong)virtualAddr & 0x1FF000) >> 12;
|
|
|
|
|
2020-01-20 01:17:10 +01:00
|
|
|
pdpe_t *pdp = NULL;
|
|
|
|
pde_t *pd = NULL;
|
|
|
|
pte_t *pt = NULL;
|
|
|
|
|
2020-01-20 20:05:02 +01:00
|
|
|
//DebugLog("PML4[%d], PDP[%d], PD[%d], PT[%d]\n",
|
|
|
|
// pml4Index, pdpIndex, pdIndex, ptIndex);
|
2020-01-20 01:17:10 +01:00
|
|
|
|
2020-01-20 20:05:02 +01:00
|
|
|
// Select bit from 12 to 51
|
|
|
|
if (!((ulong)MmPageMapLevel4[pml4Index] & 0xFFFFFFFFFF000)) {
|
2020-01-20 19:09:42 +01:00
|
|
|
|
|
|
|
// Alloc space
|
2020-01-20 19:38:27 +01:00
|
|
|
MmPageMapLevel4[pml4Index] = memalign(512*sizeof(pdpe_t), KPAGESIZE);
|
2020-01-20 19:09:42 +01:00
|
|
|
|
|
|
|
// Set present
|
2020-01-20 20:05:02 +01:00
|
|
|
MmPageMapLevel4[pml4Index] =
|
|
|
|
(pml4_t)((ulong)MmPageMapLevel4[pml4Index] | PRESENT | READWRITE);
|
2020-01-20 19:09:42 +01:00
|
|
|
|
2020-01-20 01:17:10 +01:00
|
|
|
pdp = (pdpe_t *)((ulong)MmPageMapLevel4[pml4Index] & 0xFFFFFFFFFF000);
|
2020-01-20 20:05:02 +01:00
|
|
|
//DebugLog("\tCreate PDP at %p\n", MmPageMapLevel4[pml4Index]);
|
2020-01-20 19:09:42 +01:00
|
|
|
|
2020-01-20 01:17:10 +01:00
|
|
|
} else {
|
|
|
|
pdp = (pdpe_t *)((ulong)MmPageMapLevel4[pml4Index] & 0xFFFFFFFFFF000);
|
2020-01-09 20:58:57 +01:00
|
|
|
}
|
|
|
|
|
2020-01-20 20:05:02 +01:00
|
|
|
//DebugLog("\tPDP[%d] = %p\n", pdpIndex, pdp[pdpIndex]);
|
2020-01-20 01:17:10 +01:00
|
|
|
|
2020-01-20 20:05:02 +01:00
|
|
|
// Select bit from 12 to 51
|
|
|
|
if (!((ulong)pdp[pdpIndex] & 0xFFFFFFFFFF000)) {
|
2020-01-20 19:09:42 +01:00
|
|
|
|
2020-01-20 19:38:27 +01:00
|
|
|
pdp[pdpIndex] = memalign(512*sizeof(pde_t), KPAGESIZE);
|
2020-01-20 19:09:42 +01:00
|
|
|
|
|
|
|
pdp[pdpIndex] = (pdpe_t)((ulong)pdp[pdpIndex] | PRESENT | READWRITE);
|
|
|
|
|
2020-01-20 01:17:10 +01:00
|
|
|
pd = (pde_t *)((ulong)pdp[pdpIndex] & 0xFFFFFFFFFF000);
|
2020-01-20 20:05:02 +01:00
|
|
|
//DebugLog("\tCreate PD at %p\n", (ulong)pdp[pdpIndex]);
|
2020-01-20 19:09:42 +01:00
|
|
|
|
2020-01-20 01:17:10 +01:00
|
|
|
} else {
|
|
|
|
pd = (pde_t *)((ulong)pdp[pdpIndex] & 0xFFFFFFFFFF000);
|
|
|
|
}
|
|
|
|
|
2020-01-21 01:08:28 +01:00
|
|
|
//DebugLog("\tPD[%d] = %p\n", pdIndex, pd[pdIndex]);
|
2020-01-20 01:17:10 +01:00
|
|
|
|
2020-01-20 20:05:02 +01:00
|
|
|
// Select bit from 12 to 51
|
|
|
|
if (!((ulong)pd[pdIndex] & 0xFFFFFFFFFF000)) {
|
2020-01-20 19:38:27 +01:00
|
|
|
|
|
|
|
pd[pdIndex] = memalign(512*sizeof(pte_t), KPAGESIZE);
|
2020-01-20 19:09:42 +01:00
|
|
|
|
|
|
|
pd[pdIndex] = (pde_t)((ulong)pd[pdIndex] | PRESENT | READWRITE);
|
|
|
|
|
2020-01-20 01:17:10 +01:00
|
|
|
pt = (pte_t *)((ulong)pd[pdIndex] & 0xFFFFFFFFFF000);
|
2020-01-20 20:05:02 +01:00
|
|
|
//DebugLog("\tCreate PT at %p\n", (ulong)pd[pdIndex]);
|
2020-01-20 19:09:42 +01:00
|
|
|
|
2020-01-20 01:17:10 +01:00
|
|
|
} else {
|
|
|
|
pt = (pte_t *)((ulong)pd[pdIndex] & 0xFFFFFFFFFF000);
|
|
|
|
}
|
|
|
|
|
2020-01-20 20:05:02 +01:00
|
|
|
//DebugLog("\tPT[%d] = %p\n", ptIndex, pt[ptIndex]);
|
2020-01-20 01:17:10 +01:00
|
|
|
|
|
|
|
MmLoadPML4((void *)MmPageMapLevel4);
|
|
|
|
|
|
|
|
return &pt[ptIndex];
|
2020-01-09 22:34:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Translates a virtual address to its physical equivalent
|
|
|
|
//
|
|
|
|
void *MmTransVirtToPhyAddr(void* virtualAddr)
|
|
|
|
{
|
2020-01-19 01:44:29 +01:00
|
|
|
ulong virtAddrPage = (ulong)virtualAddr & ( ~(KPAGESIZE - 1));
|
2020-01-18 13:40:56 +01:00
|
|
|
ulong *page = MmGetPageDescriptorFromVirtual(virtualAddr);
|
2020-01-09 20:58:57 +01:00
|
|
|
|
2020-01-20 01:17:10 +01:00
|
|
|
if (!(page)) {
|
2020-01-09 20:58:57 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-02-12 17:51:14 +01:00
|
|
|
return (void*)(((ulong)*page & 0xFFFFFFFFFF000)+
|
|
|
|
((ulong)virtualAddr - (ulong)virtAddrPage));
|
2020-01-09 20:58:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void *MmTransPhyToVirtAddr(void* physicalAddr)
|
|
|
|
{
|
2020-01-13 16:12:30 +01:00
|
|
|
ulong phyAddrPage = (ulong)physicalAddr & ( ~((KPAGESIZE - 1) | NX));
|
2020-01-10 19:25:33 +01:00
|
|
|
return (void*)( MmPhysicalPageTable[(ulong)physicalAddr
|
|
|
|
/ ((ulong)KPAGESIZE)
|
2020-01-11 00:05:34 +01:00
|
|
|
] + ((ulong)physicalAddr - phyAddrPage));
|
2020-01-09 20:58:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Add flags to a page
|
|
|
|
//
|
|
|
|
void MmSetPage(void* virtualAddr, ulong flags)
|
|
|
|
{
|
2020-01-18 13:40:56 +01:00
|
|
|
ulong *page = MmGetPageDescriptorFromVirtual(virtualAddr);
|
2020-01-09 20:58:57 +01:00
|
|
|
|
2020-01-20 20:05:02 +01:00
|
|
|
*page |= flags;
|
|
|
|
|
2020-01-21 09:16:42 +01:00
|
|
|
KeFlushTlbSingle((ulong)virtualAddr);
|
2020-01-09 20:58:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Remove flags of a page
|
|
|
|
//
|
|
|
|
void MmUnsetPage(void* virtualAddr, ulong flags)
|
|
|
|
{
|
2020-01-18 13:40:56 +01:00
|
|
|
ulong *page = MmGetPageDescriptorFromVirtual(virtualAddr);
|
2020-01-09 20:58:57 +01:00
|
|
|
|
2020-01-20 20:05:02 +01:00
|
|
|
*page |= flags;
|
|
|
|
|
2020-01-21 09:16:42 +01:00
|
|
|
KeFlushTlbSingle((ulong)virtualAddr);
|
2020-01-09 22:34:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Map a page in memory
|
|
|
|
//
|
|
|
|
void MmMapPage(void* virtualAddr, void* physicalAddr, ulong flags)
|
|
|
|
{
|
2020-01-19 18:54:33 +01:00
|
|
|
//DebugLog("Request %p:%p with %lu\n", virtualAddr, physicalAddr, flags);
|
|
|
|
|
2020-01-20 01:17:10 +01:00
|
|
|
ulong *page = MmGetPageDescriptorFromVirtual(virtualAddr);
|
2020-01-19 18:54:33 +01:00
|
|
|
|
2020-01-20 01:17:10 +01:00
|
|
|
*page = (ulong)physicalAddr | flags;
|
2020-01-09 22:42:41 +01:00
|
|
|
|
2020-01-15 16:12:13 +01:00
|
|
|
MmPhysicalPageTable[(ulong)physicalAddr
|
2020-01-20 01:17:10 +01:00
|
|
|
/ ((ulong)KPAGESIZE)
|
|
|
|
] = (ulong)virtualAddr;
|
2020-01-15 16:12:13 +01:00
|
|
|
|
2020-01-21 09:16:42 +01:00
|
|
|
KeFlushTlbSingle((ulong)virtualAddr);
|
2020-01-16 00:08:04 +01:00
|
|
|
|
2020-01-19 01:44:29 +01:00
|
|
|
//DebugLog("Done %p at page %p\n", *page, page);
|
|
|
|
|
2020-01-16 15:51:03 +01:00
|
|
|
if ((ulong)virtualAddr > MmVirtLastAddress)
|
2020-01-19 01:44:29 +01:00
|
|
|
MmVirtLastAddress = (ulong)virtualAddr + KPAGESIZE;
|
2020-01-09 22:34:38 +01:00
|
|
|
}
|
2020-01-09 20:58:57 +01:00
|
|
|
|
2020-01-09 22:34:38 +01:00
|
|
|
//
|
|
|
|
// Unmap a page in memory
|
|
|
|
//
|
|
|
|
void MmUnmapPage(void* virtualAddr)
|
|
|
|
{
|
2020-01-18 13:40:56 +01:00
|
|
|
ulong *page = MmGetPageDescriptorFromVirtual(virtualAddr);
|
2020-01-10 23:56:07 +01:00
|
|
|
|
2020-01-21 09:16:42 +01:00
|
|
|
//DebugLog("Request %p:%p with %lu\n", virtualAddr, physicalAddr, flags);
|
|
|
|
|
|
|
|
MmPhysicalPageTable[(ulong)MmTransVirtToPhyAddr (virtualAddr)
|
|
|
|
/ ((ulong)KPAGESIZE)
|
|
|
|
] = (ulong)0;
|
|
|
|
|
|
|
|
*page = (ulong)0;
|
|
|
|
|
|
|
|
//DebugLog("Done %p at page %p\n", *page, page);
|
2020-01-15 16:12:13 +01:00
|
|
|
|
2020-01-21 09:16:42 +01:00
|
|
|
if ((ulong)virtualAddr == MmVirtLastAddress)
|
|
|
|
MmVirtLastAddress = (ulong)virtualAddr - KPAGESIZE;
|
2020-01-10 23:56:07 +01:00
|
|
|
|
2020-01-21 09:16:42 +01:00
|
|
|
KeFlushTlbSingle((ulong)virtualAddr);
|
2020-01-09 20:58:57 +01:00
|
|
|
}
|
|
|
|
|
2020-01-09 22:34:38 +01:00
|
|
|
//-----------
|
|
|
|
|
2020-01-09 20:58:57 +01:00
|
|
|
//
|
2019-05-22 08:11:50 +02:00
|
|
|
// Returns the rank of the Stack Guards
|
2020-01-09 20:58:57 +01:00
|
|
|
//
|
2019-05-22 08:11:50 +02:00
|
|
|
void *MmGetStackGuards(char rank)
|
|
|
|
{
|
|
|
|
return (void *)MmStackGuards[(int)rank];
|
|
|
|
}
|
|
|
|
|
2019-05-18 13:39:58 +02:00
|
|
|
//
|
|
|
|
// Page fault handler
|
|
|
|
//
|
|
|
|
static void PagingHandler(ISRFrame_t *regs)
|
|
|
|
{
|
2019-05-22 08:35:58 +02:00
|
|
|
ulong StackGuardOne = (ulong)MmGetStackGuards(0);
|
2020-01-07 16:56:46 +01:00
|
|
|
ulong StackGuardTwo = (ulong)MmGetStackGuards(1);
|
2020-02-12 17:51:14 +01:00
|
|
|
if ((regs->cr2 >= StackGuardOne) && (regs->cr2 <= StackGuardOne + KPAGESIZE)
|
|
|
|
&& (regs->rsp <= regs->cr2)) {
|
2019-05-22 08:35:58 +02:00
|
|
|
bprintf(BStdOut,
|
|
|
|
"\n\n%CPANIC\n[ISR 0x8] Irrecoverable Kernel Stack Underflow\n\n"
|
2020-01-09 18:19:49 +01:00
|
|
|
" Page Fault Error code : %#x (%b)\n"
|
2019-05-22 08:35:58 +02:00
|
|
|
" Stack Guard bypassed : %#x",
|
|
|
|
|
|
|
|
VGA_COLOR_LIGHT_RED,
|
|
|
|
regs->ErrorCode,
|
|
|
|
regs->ErrorCode,
|
|
|
|
StackGuardOne
|
|
|
|
);
|
2020-02-12 17:51:14 +01:00
|
|
|
} else if ((regs->cr2 >= StackGuardTwo)
|
|
|
|
&& (regs->cr2 <= StackGuardTwo + KPAGESIZE)
|
|
|
|
&& (regs->rsp >= regs->cr2)) {
|
2020-01-07 16:56:46 +01:00
|
|
|
bprintf(BStdOut,
|
|
|
|
"\n\n%CPANIC\n[ISR 0x8] Irrecoverable Kernel Stack Overflow\n\n"
|
2020-01-09 18:19:49 +01:00
|
|
|
" Page Fault Error code : %#x (%b)\n"
|
2020-01-07 16:56:46 +01:00
|
|
|
" Stack Guard bypassed : %#x",
|
|
|
|
|
|
|
|
VGA_COLOR_LIGHT_RED,
|
|
|
|
regs->ErrorCode,
|
|
|
|
regs->ErrorCode,
|
|
|
|
StackGuardTwo
|
|
|
|
);
|
2020-01-09 18:19:49 +01:00
|
|
|
} else if (regs->cr2 == 0) {
|
|
|
|
bprintf(BStdOut,
|
|
|
|
"\n\n%CPANIC\n[ISR 0x8] Null vector exception !\n\n"
|
|
|
|
" Page Fault Error code : %#x (%b)\n",
|
|
|
|
|
|
|
|
VGA_COLOR_LIGHT_RED,
|
|
|
|
regs->intNo,
|
|
|
|
regs->ErrorCode,
|
|
|
|
regs->ErrorCode
|
|
|
|
);
|
|
|
|
} else if (regs->cr2 >= MmVirtLastAddress || regs->cr2 <= 0) {
|
|
|
|
bprintf(BStdOut,
|
2020-02-12 17:51:14 +01:00
|
|
|
"\n\n%CPANIC\n[ISR 0x8] Out of bound of the address space at %p !\n\n"
|
2020-01-09 18:19:49 +01:00
|
|
|
" End of the address space : %p\n"
|
|
|
|
" Page Fault Error code : %#x (%b)\n",
|
|
|
|
|
|
|
|
VGA_COLOR_LIGHT_RED,
|
|
|
|
regs->cr2,
|
|
|
|
MmVirtLastAddress,
|
|
|
|
regs->ErrorCode,
|
|
|
|
regs->ErrorCode
|
|
|
|
);
|
2019-05-22 08:35:58 +02:00
|
|
|
} else {
|
2019-11-16 22:41:46 +01:00
|
|
|
//XXX page fault
|
2020-02-12 17:51:14 +01:00
|
|
|
bprintf(BStdOut,
|
|
|
|
"\n\n%CPANIC\n[ISR 0x8] Irrecoverable Page Fault at %p\n\n"
|
2019-05-19 01:33:16 +02:00
|
|
|
" Error code : 0x%x (%b)",
|
2019-05-18 20:31:02 +02:00
|
|
|
|
2019-05-19 00:07:01 +02:00
|
|
|
VGA_COLOR_LIGHT_RED,
|
2019-05-18 20:31:02 +02:00
|
|
|
regs->cr2,
|
2019-05-18 13:39:58 +02:00
|
|
|
regs->ErrorCode,
|
2019-05-19 01:33:16 +02:00
|
|
|
regs->ErrorCode
|
2019-05-18 13:39:58 +02:00
|
|
|
);
|
2019-05-22 08:35:58 +02:00
|
|
|
}
|
2019-05-19 00:07:01 +02:00
|
|
|
|
2020-01-09 22:17:44 +01:00
|
|
|
bprintf(BStdOut, "\n Description : ");
|
|
|
|
|
|
|
|
if (regs->ErrorCode & PRESENT) {
|
|
|
|
bprintf(BStdOut, "Page-protection violation ");
|
|
|
|
} else {
|
|
|
|
bprintf(BStdOut, "Non present page ");
|
|
|
|
}
|
|
|
|
if (regs->ErrorCode & READWRITE) {
|
|
|
|
bprintf(BStdOut, "during write access ");
|
|
|
|
} else {
|
|
|
|
bprintf(BStdOut, "during read access ");
|
|
|
|
}
|
|
|
|
if (regs->ErrorCode & (1 << 3))
|
|
|
|
bprintf(BStdOut, "from userspace ");
|
|
|
|
if (regs->ErrorCode & (1 << 4))
|
|
|
|
bprintf(BStdOut, "after instruction fetching ");
|
|
|
|
|
2019-05-19 01:33:16 +02:00
|
|
|
KeBrkDumpRegisters(regs);
|
|
|
|
|
2019-05-19 00:07:01 +02:00
|
|
|
BStdOut->flusher(BStdOut);
|
|
|
|
|
|
|
|
KeHaltCPU();
|
2019-05-18 13:39:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MmActivatePageHandler(void)
|
|
|
|
{
|
|
|
|
KeRegisterISR(PagingHandler, 0xe);
|
2020-02-06 17:45:44 +01:00
|
|
|
DebugLog("Page handler activated\n");
|
2019-05-15 02:26:55 +02:00
|
|
|
}
|