Skip to content

Commit 3bc191f

Browse files
committed
Merge chapter/process into 0096079
2 parents 0096079 + 1b96237 commit 3bc191f

File tree

6 files changed

+57
-9
lines changed

6 files changed

+57
-9
lines changed

Chapter-8/README.md

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## Chapter 8: Memory management: physical and virtual
1+
## Chapter 8: Theory: physical and virtual memory
22

33
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.
44

@@ -41,14 +41,14 @@ The two types of entries (table and directory) look like the same. Only the fiel
4141
* `A`: indicate if the page or table was accessed
4242
* `D`: (only for pages table) indicate if the page was written
4343
* `PS` (only for pages directory) indicate the size of pages:
44-
* 0 = 4ko
45-
* 1 = 4mo
44+
* 0 = 4kb
45+
* 1 = 4mb
4646

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.
4848

4949
* 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
5252

5353
#### How to enable pagination?
5454

@@ -63,5 +63,16 @@ asm(" mov %%cr0, %%eax; \
6363

6464
But before, we need to initialize our pages directory with at least one pages table.
6565

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+
![Identity Mapping](identitymapping.png)
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 ...
73+
74+
75+
76+
6677

6778

Chapter-8/identitymapping.png

7.39 KB
Loading

SUMMARY.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
### Summary
1+
# Summary
22

3+
* [Introduction](README.md)
34
* [Introduction about the x86 architecture and about our OS](Chapter-1/README.md)
45
* [Setup the development environment](Chapter-2/README.md)
56
* [First boot with GRUB](Chapter-3/README.md)
67
* [Backbone of the OS and C++ runtime](Chapter-4/README.md)
78
* [Base classes for managing x86 architecture](Chapter-5/README.md)
89
* [GDT](Chapter-6/README.md)
910
* [IDT and interrupts](Chapter-7/README.md)
10-
* [Memory management: physical and virtual](Chapter-8/README.md)
11+
* [Theory: physical and virtual memory](Chapter-8/README.md)
12+
* [Memory management: physical and virtual](chapter9/README.md)
1113
* Process management and multitasking
1214
* External program execution: ELF files
1315
* Userland and syscalls
@@ -18,4 +20,5 @@
1820
* EXT2 read-only filesystems
1921
* Standard C library (libC)
2022
* UNIX basic tools: sh, cat
21-
* Lua interpreter
23+
* Lua interpreter
24+

chapter9/README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Memory management: physical and virtual
2+
3+
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+
![Physical Memory](physicalmemory.png)
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+
![Virtual Memory](virtualmemory.png)
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
31+
*/
32+
for (i=0; i<256; i++)
33+
pdir[i] = pd0[i];
34+
```

chapter9/physicalmemory.png

10.2 KB
Loading

chapter9/virtualmemory.png

14.3 KB
Loading

0 commit comments

Comments
 (0)