Correcting dynamic paging

This commit is contained in:
Adrien Bourmault 2019-05-16 23:56:23 +02:00
parent 4e32aa157d
commit 4f3bda6ec2
5 changed files with 46 additions and 28 deletions

View File

@ -29,7 +29,7 @@
# Debug # Debug
mode ?= debug mode ?= debug
ram ?= 8G ram ?= 4G
# Programs # Programs
ASM=nasm ASM=nasm
@ -203,7 +203,7 @@ $(KOBJDIR)/%.o: %.c | $(KOBJDIR)
@echo ${CL2}[$@] ${CL}Compiled.${CL3} @echo ${CL2}[$@] ${CL}Compiled.${CL3}
test: all test: all
@qemu-system-x86_64 -cpu core2duo -soundhw pcspk -rtc base=localtime -m $(ram) -hda $(BUILDDIR)/bin/disk.img \ @qemu-system-x86_64 -cpu core2duo -soundhw pcspk -s -rtc base=localtime -m $(ram) -hda $(BUILDDIR)/bin/disk.img \
-d cpu_reset,guest_errors,pcall,int 2> $(BUILDDIR)/qemu.log & -d cpu_reset,guest_errors,pcall,int 2> $(BUILDDIR)/qemu.log &
test32: all test32: all

View File

@ -23,7 +23,7 @@
; along with OS/K. If not, see <https://www.gnu.org/licenses/>. ; ; along with OS/K. If not, see <https://www.gnu.org/licenses/>. ;
;=----------------------------------------------------------------------------=; ;=----------------------------------------------------------------------------=;
%define MAX_MEMORY 4 ; GiB %define MAX_MEMORY 1 ; GiB
[BITS 32] [BITS 32]
[section .text] [section .text]

View File

@ -48,8 +48,8 @@ noreturn void BtStartKern(multiboot_info_t *mbInfo, uint mbMagic, void *codeSeg)
// Memory // Memory
MmInitMemoryMap(); MmInitMemoryMap();
MmInitHeap();
MmInitPaging(); MmInitPaging();
MmInitHeap();
// Interrupts launching // Interrupts launching
KeSetupIDT(); KeSetupIDT();

View File

@ -33,7 +33,7 @@ enum
MF_NX = 1 << 31 MF_NX = 1 << 31
}; };
#define RAM_MAX 16 #define RAM_MAX 32
#define NB_4K 150 #define NB_4K 150
// * 2 MB // * 2 MB
@ -54,56 +54,55 @@ volatile ulong MmStackGuards[2] = { 0 };
void MmInitPaging(void) void MmInitPaging(void)
{ {
extern MemoryMap_t memoryMap; extern MemoryMap_t memoryMap;
uint phRamSize = memoryMap.freeRamSize + memoryMap.nonfreeRamSize; ulong phRamSize = memoryMap.freeRamSize + memoryMap.nonfreeRamSize;
memzero((void *)&MmPML4[0], sizeof(MmPML4)); memzero((void *)&MmPML4[0], sizeof(MmPML4));
memzero((void *)&MmPDP[0], sizeof(MmPDP)); memzero((void *)&MmPDP[0], sizeof(MmPDP));
memzero((void *)&MmPD[0], sizeof(MmPD)); memzero((void *)&MmPD[0], sizeof(MmPD));
memzero((void *)&MmPT[0], sizeof(MmPT)); memzero((void *)&MmPT[0], sizeof(MmPT));
for (int i = 0; i < 512 * NB_4K; i++) { for (volatile ulong i = 0; i < 512 * NB_4K; i++) {
// STACK GUARD PAGE // STACK GUARD PAGE
if ((ulong)i*4096 == (ulong)BtLoaderInfo.stackEndAddr) { if ((ulong)(i*4096) == (ulong)BtLoaderInfo.stackEndAddr) {
MmPT[i] = ((ulong)i * 4096); MmPT[i] = ((ulong)(i*4096));
MmStackGuards[0] = i; MmStackGuards[0] = i;
continue; continue;
} }
// ENOMEM like // ENOMEM like
if ((ulong)i*4096 > (ulong)phRamSize) { if ((ulong)(i*4096) > (ulong)phRamSize) {
MmPT[i] = ((ulong)i * 4096) | MF_READWRITE;// | MF_NX; break;
continue;
} }
// STACK GARD PAGE // STACK GARD PAGE
if ((ulong)i*4096 == (ulong)BtLoaderInfo.kernelEndAddr) { if ((ulong)(i*4096) == (ulong)BtLoaderInfo.kernelEndAddr) {
MmPT[i] = ((ulong)i * 4096); MmPT[i] = ((ulong)(i*4096));
MmStackGuards[1] = i; MmStackGuards[1] = i;
continue; continue;
} }
MmPT[i] = ((ulong)i * 4096) | MF_PRESENT | MF_READWRITE; MmPT[i] = ((ulong)(i*4096)) | MF_PRESENT | MF_READWRITE;
} }
for (int i = 0; i < NB_4K; i++) { for (volatile ulong i = 0; i < NB_4K; i++) {
MmPD[i] = (ulong)(&MmPT[i*512])| MF_PRESENT | MF_READWRITE; MmPD[i] = (ulong)(&MmPT[i*512])| MF_PRESENT | MF_READWRITE;
} }
for (int i = NB_4K; i < 512 * RAM_MAX; i++) { for (volatile ulong i = NB_4K; i < 512 * RAM_MAX; i++) {
// ENOMEM like // ENOMEM like
if ((ulong)i* 2048 * 1024 > (ulong)phRamSize) { if ((ulong)(i* 2048 * 1024) > (ulong)phRamSize) {
MmPD[i] = ((ulong)i * 2048 * 1024) | MF_READWRITE;// | MF_NX; break;
continue;
} }
MmPD[i] = ((ulong)i * 2048 * 1024) | MF_PRESENT | MF_READWRITE | MF_HUGE; MmPD[i] = 0;
MmPD[i] = ((ulong)(i* 2048 * 1024)) | MF_PRESENT | MF_READWRITE | MF_HUGE;
} }
for (int i = 0; i < RAM_MAX; i++) { for (volatile int i = 0; i < RAM_MAX; i++) {
MmPDP[i] = (ulong)(&MmPD[i*512])| MF_PRESENT | MF_READWRITE; MmPDP[i] = (ulong)(&MmPD[i*512])| MF_PRESENT | MF_READWRITE;
} }
MmPML4[0] = (ulong)(&MmPDP[0])| MF_PRESENT | MF_READWRITE; MmPML4[0] = (ulong)(&MmPDP[0])| MF_PRESENT | MF_READWRITE;
//MmLoadPML4((void *)MmPML4); MmLoadPML4((void *)MmPML4);
} }

View File

@ -224,13 +224,31 @@ error_t CmdDie(int argc, char **argv, char *cmdline)
error_t CmdPF(int argc, char **argv, char *cmdline) error_t CmdPF(int argc, char **argv, char *cmdline)
{ {
if (argc != 2) /* if (argc != 2) */
return EINVAL; /* return EINVAL; */
ulong address = atoul(argv[1]); /* ulong address = atoul(argv[1]); */
KernLog("print: %s, %d\n", argv[1], address); /* KernLog("print: %s, %d\n", argv[1], address); */
*((char*)address) += 1; /* *((char*)address) += 1; */
*((char*)(1*GB)) += 1;
KernLog("1GB ");
*((char*)(2*GB)) += 1;
KernLog("2GB ");
*((char*)(3*GB)) += 1;
KernLog("3GB ");
*((char*)(4*GB)) += 1;
KernLog("4GB ");
return EOK;
}
void MmInitPaging(void);
error_t CmdReloadPage(int argc, char **argv, char *cmdline)
{
MmInitPaging();
return EOK; return EOK;
} }
@ -251,6 +269,7 @@ Command_t cmdtable[] =
{ "die", CmdDie, "Die painfully" }, { "die", CmdDie, "Die painfully" },
{ "exit", CmdQuit, "Initiate shutdown" }, { "exit", CmdQuit, "Initiate shutdown" },
{ "help", CmdHelp, "Show this message" }, { "help", CmdHelp, "Show this message" },
{ "rpag", CmdReloadPage, "Reload the pages directory" },
{ "march", CmdStarWars, "Play the Imperial March"}, { "march", CmdStarWars, "Play the Imperial March"},
{ "mmap", CmdMemMap, "Show memory map" }, { "mmap", CmdMemMap, "Show memory map" },
{ "musage", CmdMemUsage, "Show memory statistics" }, { "musage", CmdMemUsage, "Show memory statistics" },