From 04211e27739a13527c23a9f42336d540af222c26 Mon Sep 17 00:00:00 2001 From: Starnakin Date: Wed, 30 Oct 2024 19:05:58 +0100 Subject: [PATCH] add: documentation: memory --- README.md | 72 +++---------------------- documentation/installation.md | 73 ++++++++++++++++++++++++++ documentation/memory.md | 5 ++ documentation/memory/frame.md | 3 ++ documentation/memory/mmu.md | 19 +++++++ documentation/memory/page.md | 3 ++ documentation/memory/page_directory.md | 7 +++ documentation/memory/page_table.md | 12 +++++ src/kernel.c | 5 ++ 9 files changed, 134 insertions(+), 65 deletions(-) create mode 100644 documentation/installation.md create mode 100644 documentation/memory.md create mode 100644 documentation/memory/frame.md create mode 100644 documentation/memory/mmu.md create mode 100644 documentation/memory/page.md create mode 100644 documentation/memory/page_directory.md create mode 100644 documentation/memory/page_table.md diff --git a/README.md b/README.md index f51eeb3..b3045dc 100644 --- a/README.md +++ b/README.md @@ -2,68 +2,10 @@ This project aims to explore how a kernel works by building our own, gaining hands-on experience with core concepts like memory management, interrupt, keyboard driver, etc -## INSTALLATION - -### Cross compile the compile -[THE DOCUMENTATION](https://wiki.osdev.org/GCC_Cross-Compiler) -- install requirement follow [THE DOCUMENTATION](https://wiki.osdev.org/GCC_Cross-Compiler) -- Setup the shell -``` sh -export PREFIX="$HOME/opt/cross" -export TARGET=i386-elf -export PATH="$PREFIX/bin:$PATH" -mkdir $HOME/src -cd $HOME/src -``` -- Download [binutils](https://ftp.gnu.org/gnu/binutils/?C=M;O=D) -- Download [gcc](https://ftp.gnu.org/gnu/gcc/?C=M;O=D) -- Download [gdb](https://ftp.gnu.org/gnu/gdb/?C=M;O=D) -- extract archives -``` sh -tar xf [your binutils archive] -tar xf [your gcc archive] -tar xf [your gdb archive] -``` -- (protips use -j [nb core]) -- Compile binutils -``` sh -cd $HOME/src -mkdir build-binutils - -cd build-binutils -../binutils-x.y.z/configure --target=$TARGET --prefix="$PREFIX" --with-sysroot --disable-nls --disable-werror -make -make install -``` -- Compile gdb -``` sh -cd $HOME/src -mkdir build-gdb - -cd build-gdb -../gdb.x.y.z/configure --target=$TARGET --prefix="$PREFIX" --disable-werror -make all-gdb -make install-gdb -make -make install -``` -- Compile gcc -``` sh -cd $HOME/src - -# The $PREFIX/bin dir _must_ be in the PATH. We did that above. -which -- $TARGET-as || echo $TARGET-as is not in the PATH - -mkdir build-gcc -cd build-gcc -../gcc-x.y.z/configure --target=$TARGET --prefix="$PREFIX" --disable-nls --enable-languages=c,c++ --without-headers -make all-gcc -make all-target-libgcc -make install-gcc -make install-target-libgcc -``` - -### Compile and the start the kernel -``` sh -make run -``` \ No newline at end of file +## Currently supported +- screen driver +- keyboard driver +- [memory pagination](./documentation/memory.md) +- interruption +- power management (reboot, halt, shutdown) +- simple shell \ No newline at end of file diff --git a/documentation/installation.md b/documentation/installation.md new file mode 100644 index 0000000..d134c53 --- /dev/null +++ b/documentation/installation.md @@ -0,0 +1,73 @@ +# INSTALLATION + +## Cross compile the compile + +### WHY ?? +Why cross compile the gcc ? + +We must cross compile gcc cause our kernel is developped for i386 cpu but your current cpu is probally not an i386 so we need to create an i386 compiler + +### HOW ? + +[THE DOCUMENTATION](https://wiki.osdev.org/GCC_Cross-Compiler) +- install requirement follow [THE DOCUMENTATION](https://wiki.osdev.org/GCC_Cross-Compiler) +- Setup the shell +``` sh +export PREFIX="$HOME/opt/cross" +export TARGET=i386-elf +export PATH="$PREFIX/bin:$PATH" +mkdir $HOME/src +cd $HOME/src +``` +- Download [binutils](https://ftp.gnu.org/gnu/binutils/?C=M;O=D) +- Download [gcc](https://ftp.gnu.org/gnu/gcc/?C=M;O=D) +- Download [gdb](https://ftp.gnu.org/gnu/gdb/?C=M;O=D) +- extract archives +``` sh +tar xf [your binutils archive] +tar xf [your gcc archive] +tar xf [your gdb archive] +``` +- (protips use -j [nb core]) +- Compile binutils +``` sh +cd $HOME/src +mkdir build-binutils + +cd build-binutils +../binutils-x.y.z/configure --target=$TARGET --prefix="$PREFIX" --with-sysroot --disable-nls --disable-werror +make +make install +``` +- Compile gdb +``` sh +cd $HOME/src +mkdir build-gdb + +cd build-gdb +../gdb.x.y.z/configure --target=$TARGET --prefix="$PREFIX" --disable-werror +make all-gdb +make install-gdb +make +make install +``` +- Compile gcc +``` sh +cd $HOME/src + +# The $PREFIX/bin dir _must_ be in the PATH. We did that above. +which -- $TARGET-as || echo $TARGET-as is not in the PATH + +mkdir build-gcc +cd build-gcc +../gcc-x.y.z/configure --target=$TARGET --prefix="$PREFIX" --disable-nls --enable-languages=c,c++ --without-headers +make all-gcc +make all-target-libgcc +make install-gcc +make install-target-libgcc +``` + +## Compile and the start the kernel +``` sh +make run +``` \ No newline at end of file diff --git a/documentation/memory.md b/documentation/memory.md new file mode 100644 index 0000000..51cf8c9 --- /dev/null +++ b/documentation/memory.md @@ -0,0 +1,5 @@ +# MEMOMY + +## Pagination + +To activate pagination you must put the physic [pd](./memory/page_directory.md) address in the cr3 register and 0x80000000 in the cr0 register. Now the [MMU](./memory/mmu.md) will translate your address \ No newline at end of file diff --git a/documentation/memory/frame.md b/documentation/memory/frame.md new file mode 100644 index 0000000..06ffe8b --- /dev/null +++ b/documentation/memory/frame.md @@ -0,0 +1,3 @@ +# FRAME + +Like [page](./page.md) but in physical memory space \ No newline at end of file diff --git a/documentation/memory/mmu.md b/documentation/memory/mmu.md new file mode 100644 index 0000000..c4d3fa0 --- /dev/null +++ b/documentation/memory/mmu.md @@ -0,0 +1,19 @@ +# Memory Management Unit (MMU) +The MMU is a chip which translate virtual memory to physical memory. When you try to access to an address (/!\ only when you try to access) the MMU will get the physical address from the [pd](./page_directory.md). + +## How translate an address ? +The address will be divid in 3 parts: +- Page Directory index (10bit (cause 1024)) +- Page Table index (10bit (cause 1024 too)) +- offset (12bit cause page size = 4096) + +### Example + +**address**: 0b000100000011101011111011101101 + +| Base | PD index | PT index | offset | +|--------|----------|------------|--------------| +| Binary | 00010000 | 0011101011 | 111011101101 | +| Decimal| 16 | 235 | 3821 | + +So to translate this address the MMU will get the 20 first bit of the value store at the 235th PTE of the 16th PDE and will replace the 20 first bit of the virtually address. \ No newline at end of file diff --git a/documentation/memory/page.md b/documentation/memory/page.md new file mode 100644 index 0000000..5efec19 --- /dev/null +++ b/documentation/memory/page.md @@ -0,0 +1,3 @@ +# PAGE + +A page is a memory space with with a specific size (default 4096byte). \ No newline at end of file diff --git a/documentation/memory/page_directory.md b/documentation/memory/page_directory.md new file mode 100644 index 0000000..3564250 --- /dev/null +++ b/documentation/memory/page_directory.md @@ -0,0 +1,7 @@ +# PAGE DIRECTORY + +The page directory (or pd) is + + +## Indexed Size +A PD can store 1024 PT, each PT index 1024 * 4096Byte = 4MB of address, so a PD can index 1024 * 4MB = 4GB. \ No newline at end of file diff --git a/documentation/memory/page_table.md b/documentation/memory/page_table.md new file mode 100644 index 0000000..ffb283e --- /dev/null +++ b/documentation/memory/page_table.md @@ -0,0 +1,12 @@ +# PAGE TABLE + +A page table (or PT) is an 1024 array of 32bit value(Page Table Entry (or PTE)). The address of the PT is store in the [PD](./page_directory.md). + +## Page Table Entry (PTE) +The value is a 20 first bits of the [frame](./frame.md) address and the 12 last bit is for the flag. + +### Not used PTE value +To say to the [MMU](./mmu.md) the page is currently not attribued to a frame you should put the (PTE index << 12) + +## Indexed Size +Cause our kernel use 4096byte page and a PT can store 1024 value, each PT index 1024 * 4096Byte = 4MB of address. \ No newline at end of file diff --git a/src/kernel.c b/src/kernel.c index 8ec87eb..c336db2 100644 --- a/src/kernel.c +++ b/src/kernel.c @@ -36,6 +36,11 @@ void kernel_main(void) "I see no way to confuse an array of 256 seg:off pairs with a " "complex 8*unknown quantity -byte descriptor table. -- Troy " "Martin 03:50, 22 March 2009 (UTC)\n"); + while (true) { + void *ptr = alloc_pages(4096); + memset(ptr, ~0, 4096); + PRINT_PTR(ptr); + } /* int i = 0; */ /* while (vmalloc(10)) */ /* ; */