You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Chapter-8/README.md
+17-6Lines changed: 17 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,4 @@
1
-
## Chapter 8: Memory management: physical and virtual
1
+
## Chapter 8: Theory: physical and virtual memory
2
2
3
3
In the chapter related to the GDT, we saw that using segmentation a physical memory address is calculated using a segment selector and an offset.
4
4
@@ -41,14 +41,14 @@ The two types of entries (table and directory) look like the same. Only the fiel
41
41
*`A`: indicate if the page or table was accessed
42
42
*`D`: (only for pages table) indicate if the page was written
43
43
*`PS` (only for pages directory) indicate the size of pages:
44
-
* 0 = 4ko
45
-
* 1 = 4mo
44
+
* 0 = 4kb
45
+
* 1 = 4mb
46
46
47
-
**Note:** Physical addresses in the pages diretcory or pages table are written using 20 bits because these addresses are aligned on 4ko, so the last 12bits should be equal to 0.
47
+
**Note:** Physical addresses in the pages diretcory or pages table are written using 20 bits because these addresses are aligned on 4kb, so the last 12bits should be equal to 0.
48
48
49
49
* A pages directory or pages table used 1024*4 = 4096 bytes = 4k
50
-
* A pages table can address 1024 * 4k = 4 Mo
51
-
* A pages directory can address 1024 * (1024 * 4k) = 4 Go
50
+
* A pages table can address 1024 * 4k = 4 Mb
51
+
* A pages directory can address 1024 * (1024 * 4k) = 4 Gb
52
52
53
53
#### How to enable pagination?
54
54
@@ -63,5 +63,16 @@ asm(" mov %%cr0, %%eax; \
63
63
64
64
But before, we need to initialize our pages directory with at least one pages table.
65
65
66
+
#### Identity Mapping
67
+
68
+
With the identity mapping model, the page will apply only to the kernel as the first 4 MB of virtual memory coincide with the first 4 MB of physical memory:
69
+
70
+

71
+
72
+
This model is simple: the first virtual memory page coincide to the first page in physical memory, the second page coincide to the second page on physical memory and so on ...
The kernel knows the size of the physical memory available thanks to [GRUB](../Chapter-3/README.md).
4
+
5
+
In our implementation, the first 8 megabytes of physical memory will be reserved for use by the kernel and will contain:
6
+
7
+
- The kernel
8
+
- GDT, IDT et TSS
9
+
- Kernel Stack
10
+
- Some space reserved to hardware (video memory, ...)
11
+
- Page directory and pages table for the kernel
12
+
13
+
The rest of the physical memory is freely available to the kernel and applications.
14
+
15
+

16
+
17
+
18
+
### Virtual Memory Mapping
19
+
20
+
The address space between the beginning of memory and `0x40000000` address is the kernel space, while the space between the address `0x40000000` and the end of the memory corresponds to user space:
21
+
22
+

23
+
24
+
The kernel space in virtual memory, which is using 1Gb of virtual memory, is common to all tasks (kernel and user).
25
+
26
+
This is implemented by pointing the first 256 entries of the task page directory to the kernel page directory (In [vmm.cc](https://github.com/SamyPesse/How-to-Make-a-Computer-Operating-System/blob/master/src/kernel/arch/x86/vmm.cc#L204)):
27
+
28
+
```cpp
29
+
/*
30
+
* Kernel Space. v_addr < USER_OFFSET are addressed by the kernel pages table
0 commit comments