> For the complete documentation index, see [llms.txt](https://samypesse.gitbook.io/how-to-create-an-operating-system/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://samypesse.gitbook.io/how-to-create-an-operating-system/chapter9.md).

# Memory management: physical and virtual

The kernel knows the size of the physical memory available thanks to [GRUB](/how-to-create-an-operating-system/chapter-3.md).

In our implementation, the first 8 megabytes of physical memory will be reserved for use by the kernel and will contain:

* The kernel
* GDT, IDT et TSS
* Kernel Stack
* Some space reserved to hardware (video memory, ...)
* Page directory and pages table for the kernel

The rest of the physical memory is freely available to the kernel and applications.

![Physical Memory](/files/-LYkbouNZMvh8eAi4rjs)

## Virtual Memory Mapping

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:

![Virtual Memory](/files/-LYkbouP-kv2KCGLnCwM)

The kernel space in virtual memory, which is using 1Gb of virtual memory, is common to all tasks (kernel and user).

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)):

```cpp
/* 
 * Kernel Space. v_addr < USER_OFFSET are addressed by the kernel pages table
 */
for (i=0; i<256; i++) 
    pdir[i] = pd0[i];
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://samypesse.gitbook.io/how-to-create-an-operating-system/chapter9.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
