-
Notifications
You must be signed in to change notification settings - Fork 4
/
paging.c
90 lines (67 loc) · 1.91 KB
/
paging.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/**************************************************************************************************
*
* File: paging.c
*
* Copyright (c) 2003, 2004 Erik Dubbelboer
*
* Contents: paging functions.
*
*************************************************************************************************/
#include "kernel.h"
#include "paging.h"
/**
* Set up the Boot Page-Directory and Tables, and enable Paging.
*
* This function is executed at the physicallKernel address.
* Therefore it can not use global variables or switch() statements.
**/
void paging_init(void)
{
uint index;
uint loop;
uint* table;
//
// Boot Page-Directory
//
uint* dir = (uint*)0x124000;
index = 0;
// The 1e Page-Table.
dir[index++] = 0x125000 | X86_PAGE_PRESENT | X86_PAGE_WRITE;
// The rest of the tables till 3GB are not present.
for (loop = 0; loop < 768 - 1; loop++)
dir[index++] = 0; // Not present.
// The 2e Page-Table.
dir[index++] = 0x126000 | X86_PAGE_PRESENT | X86_PAGE_WRITE;
// The rest of the tables is not present.
for (loop = 0; loop < 256 - 1; loop++)
dir[index++] = 0;
//
// Page-Table 1.
//
table = (uint*)0x125000;
index = 0;
// Map the First 2MB (512 Pages)
for (loop = 0; loop < 512; loop++)
table[index++] = (PAGE_SIZE * loop) | X86_PAGE_PRESENT | X86_PAGE_WRITE;
// The rest is not present.
for (loop = 0; loop < 512; loop++)
table[index++] = 0;
//
// Page-Table 2.
//
table = (uint*)0x126000;
index = 0;
// Map the First 2MB (512 Pages)
for (loop = 0; loop < 512; loop++)
table[index++] = (PAGE_SIZE * loop) | X86_PAGE_PRESENT | X86_PAGE_WRITE;
// The rest is not present.
for (loop = 0; loop < 512; loop++)
table[index++] = 0;
// load cr3 (Page-Directory Base Register) with the Page-Directory we are going to use,
// which is the Page-Directory from the Kernel process.
set_cr3((uint)dir);
//
// Enable paging.
//
set_cr0(get_cr0() | 0x80000000); // Set the paging bit.
}