paging.c:
Code: Select all
/** @file paging.c
* Copyright (c) 2024 FigaSystems
* Memory Paging
* @author Daniil Dunaef
* @date 14-Aug-2024
* @bug No current bugs found
*/
#include "mm/paging.h"
#include "FOSdef.h"
extern void PGInstallPageDirectory();
extern void PGEnablePaging();
unsigned int PageDirectory[1024] __attribute__((aligned(4096)));
unsigned int PageTable[1024] __attribute((aligned(4096)));
FOSKERNELAPI
VOID
PGInitializePageDirectory(
CHAR Present,
CHAR ReadWrite,
CHAR UserSuperior,
CHAR WriteThrough,
CHAR CacheDisable,
CHAR Accessed,
CHAR PageSize
)
{
for (int i = 0; i < 1024; i++)
PageDirectory[i] = Present | ReadWrite << 1 | UserSuperior << 2 | WriteThrough << 3 | CacheDisable << 4 | Accessed << 5 | PageSize << 6;
}
FOSKERNELAPI
VOID
PGInitializePageTable(
CHAR Present,
CHAR ReadWrite,
CHAR UserSuperior,
CHAR WriteThrough,
CHAR CacheDisable,
CHAR Accessed,
CHAR Dirty,
CHAR PageAttributeTable,
CHAR Global
)
{
for (int i = 0; i < 1024; i++)
PageTable[i] = (i * 0x1000) | Present | ReadWrite << 1 | UserSuperior << 2 | WriteThrough << 3 | CacheDisable << 4 | Accessed << 5 | Dirty << 6 | PageAttributeTable << 7 | Global << 8;
PageDirectory[0] |= ((unsigned int)PageTable);
}
FOSKERNELAPI
VOID
PGSetPageTableEntry(
INT PageTableIndex,
INT Address,
CHAR Present,
CHAR ReadWrite,
CHAR UserSuperior,
CHAR WriteThrough,
CHAR CacheDisable,
CHAR Accessed,
CHAR Dirty,
CHAR PageAttributeTable,
CHAR Global
)
{
PageTable[PageTableIndex] = Address << 11 | Present | ReadWrite << 1 | UserSuperior << 2 | WriteThrough << 3 | CacheDisable << 4 | Accessed << 5 | Dirty << 6 | PageAttributeTable << 7 | Global << 8;
}
FOSKERNELAPI
VOID
PGInitializePaging(
VOID
)
{
PGInitializePageDirectory(0, 1, 0, 0, 0, 0, 0);
PGInitializePageTable(1, 1, 0, 0, 0, 1, 0, 0, 0);
PGInstallPageDirectory();
PGEnablePaging();
}
Code: Select all
; File: paging.asm
; Copyright (c) 2024 Figasystems
; Paging
; Author: Daniil Dunaef
; Date: 14-Aug-2024
; Bugs no Bugs
global PGInstallPageDirectory
global PGEnablePaging
extern PageDirectory
PGInstallPageDirectory:
push ebp
mov ebp, esp
mov eax, [PageDirectory]
mov cr3, eax
mov ebp, esp
pop ebp
ret
PGEnablePaging:
push ebp
mov ebp, esp
mov eax, cr0
or eax, 0x80000000
mov cr0, eax
ret
Code: Select all
Servicing hardware INT=0x08
check_exception old: 0xffffffff new 0xe
0: v=0e e=0000 i=0 cpl=0 IP=0008:00101aad pc=00101aad SP=0010:00106f08 CR2=00101aad
EAX=80000011 EBX=00010000 ECX=00000000 EDX=00108000
ESI=00000000 EDI=00000000 EBP=00106f08 ESP=00106f08
EIP=00101aad EFL=00000086 [--S--P-] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =0010 00000000 ffffffff 00cf9300 DPL=0 DS [-WA]
CS =0008 00000000 ffffffff 00cf9a00 DPL=0 CS32 [-R-]
SS =0010 00000000 ffffffff 00cf9300 DPL=0 DS [-WA]
DS =0010 00000000 ffffffff 00cf9300 DPL=0 DS [-WA]
FS =0010 00000000 ffffffff 00cf9300 DPL=0 DS [-WA]
GS =0010 00000000 ffffffff 00cf9300 DPL=0 DS [-WA]
LDT=0000 00000000 0000ffff 00008200 DPL=0 LDT
TR =0000 00000000 0000ffff 00008b00 DPL=0 TSS32-busy
GDT= 00116140 0000002f
IDT= 00116180 000007ff
CR0=80000011 CR2=00101aad CR3=00108002 CR4=00000000
DR0=00000000 DR1=00000000 DR2=00000000 DR3=00000000
DR6=ffff0ff0 DR7=00000400
CCS=00000084 CCD=80000011 CCO=EFLAGS
EFER=0000000000000000
check_exception old: 0xe new 0xe
1: v=08 e=0000 i=0 cpl=0 IP=0008:00101aad pc=00101aad SP=0010:00106f08 env->regs[R_EAX]=80000011
EAX=80000011 EBX=00010000 ECX=00000000 EDX=00108000
ESI=00000000 EDI=00000000 EBP=00106f08 ESP=00106f08
EIP=00101aad EFL=00000086 [--S--P-] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =0010 00000000 ffffffff 00cf9300 DPL=0 DS [-WA]
CS =0008 00000000 ffffffff 00cf9a00 DPL=0 CS32 [-R-]
SS =0010 00000000 ffffffff 00cf9300 DPL=0 DS [-WA]
DS =0010 00000000 ffffffff 00cf9300 DPL=0 DS [-WA]
FS =0010 00000000 ffffffff 00cf9300 DPL=0 DS [-WA]
GS =0010 00000000 ffffffff 00cf9300 DPL=0 DS [-WA]
LDT=0000 00000000 0000ffff 00008200 DPL=0 LDT
TR =0000 00000000 0000ffff 00008b00 DPL=0 TSS32-busy
GDT= 00116140 0000002f
IDT= 00116180 000007ff
CR0=80000011 CR2=001161f0 CR3=00108002 CR4=00000000
DR0=00000000 DR1=00000000 DR2=00000000 DR3=00000000
DR6=ffff0ff0 DR7=00000400
CCS=00000084 CCD=80000011 CCO=EFLAGS
EFER=0000000000000000
check_exception old: 0x8 new 0xe
Triple fault