Skip to content

Commit

Permalink
fix(kernel: mmu): offset map the first 1MiB
Browse files Browse the repository at this point in the history
We previously identity mapped the first 1MiB of our address
space because it contains physical addresses linked to hardware
devices (VGA buffer).

Problem is this first 1MiB of address space was not duplicated in
each newly created page directory after that. Now, instead of going
through the hassle of duplicating it, we offset_map it alongside the
kernel's code during startup, and modified all references to it to
now use the offseted address instead.

Test: Use the VGA buffer without crashing, and test if VMM structures
      (that now use the first MiB) can be allocated correctly.
  • Loading branch information
d4ilyrun committed Nov 26, 2024
1 parent f89cf88 commit b4a3d1e
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 16 deletions.
11 changes: 4 additions & 7 deletions include/kernel/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,8 @@
* | |
* | |
* | |
* 0x0110_0000 |------------------|
* 0x0010_0000 |------------------|
* | VMM Reserved |
* 0x0100_0000 |------------------|
* | Reserved |
* 0x0000_0000 --------------------
*
* @{
Expand Down Expand Up @@ -100,7 +98,7 @@
* Any byte written after this address **WILL** overwrite our kernel's
* executable binary.
*
* @info this address is defined inside the kernel's linker scrpit.
* @note this address is defined inside the kernel's linker scrpit.
*/
extern u32 _kernel_code_start;
#define KERNEL_CODE_START ((u32)&_kernel_code_start)
Expand All @@ -111,7 +109,7 @@ extern u32 _kernel_code_start;
* Any byte written after this address will not overwrite our kernel's
* executable binary.
*
* @info this address is defined inside the kernel's linker scrpit.
* @note this address is defined inside the kernel's linker scrpit.
*/
extern u32 _kernel_code_end;
#define KERNEL_CODE_END ((u32)&_kernel_code_end)
Expand All @@ -138,12 +136,11 @@ extern u32 _kernel_code_end;
* @brief Size of the area reserved fo rallovation memory management structures
*
* 1MiB Virtual memory range reserved for allocating VMA structures.
* We place this area first thing first thing after our bootstrap code.
*
* @see @ref vmm
*/
#define VMM_RESERVED_SIZE 0x100000UL
#define VMM_RESERVED_START KERNEL_HIGHER_HALF_PHYSICAL(KERNEL_CODE_START)
#define VMM_RESERVED_START 0x00000000
#define VMM_RESERVED_END (VMM_RESERVED_START + VMM_RESERVED_SIZE)

#endif /* KERNEL_MEMORY_H */
9 changes: 1 addition & 8 deletions kernel/arch/i686/mmu.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,16 +218,9 @@ bool mmu_init(void)
// we keep it as is, since it is the only way for us to test
// our userland implementation until we port our first programs
// (soon hopefully)
mmu_offset_map(KERNEL_HIGHER_HALF_PHYSICAL(KERNEL_CODE_START),
KERNEL_HIGHER_HALF_PHYSICAL(KERNEL_CODE_END),
mmu_offset_map(0, KERNEL_HIGHER_HALF_PHYSICAL(KERNEL_CODE_END),
KERNEL_HIGHER_HALF_OFFSET, PROT_EXEC | PROT_READ);

// Identity map the first MB, since it contains hardcoded addresses we still
// use (console buffer for example).
//
// TODO: Check for possible alternatives? (MMIO?, map only what we need?)
mmu_identity_map(0x0, 0x100000, PROT_READ | PROT_WRITE | PROT_KERNEL);

mmu_load_page_directory(kernel_startup_process.context.cr3);

// According to 4.3, to activate 32-bit mode paging we must:
Expand Down
3 changes: 2 additions & 1 deletion kernel/arch/i686/terminal.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <kernel/memory.h>
#include <kernel/terminal.h>

#include <stddef.h>
Expand Down Expand Up @@ -61,7 +62,7 @@ void tty_init(void)
.column = 0,
.row = 0,
.color = vga_entry_color(VGA_COLOR_LIGHT_GREY, VGA_COLOR_BLACK),
.buffer = TTY_BUFFER_START,
.buffer = (uint16_t *)KERNEL_HIGHER_HALF_VIRTUAL(TTY_BUFFER_START),
};

// Fill the whole buffer with a ' ' character, written grey on black.
Expand Down

0 comments on commit b4a3d1e

Please sign in to comment.