Virtual Memory Tips (using brokenthorn.com guide)
Posted: Sun Nov 24, 2013 5:48 pm
Hi mates,
after some days of study, I've implemented the virtual memory manager using brokenthorn.com guide (that is GREAT!).
Well the problem is that in this guide, a custom bootloader is used, and the paging is enable there..
I'm using grub as bootloader, so what I did to solve this problem was to just add that code in my bootstrap.asm (before to call the kernel main function).
Apparently it works.. but I'm not so happy with this solution... I'm sure that there is a better (and perhaps more elegant) way to do this..
Here is my bootstrap asm code:
But is Virtual Memory really working ?? Well I'd say yes, since I don't get any page fault and I read the cr0 register to be sure it is set correctly..
Sorry for my bad english, it is late night here and i'm very tired
after some days of study, I've implemented the virtual memory manager using brokenthorn.com guide (that is GREAT!).
Well the problem is that in this guide, a custom bootloader is used, and the paging is enable there..
I'm using grub as bootloader, so what I did to solve this problem was to just add that code in my bootstrap.asm (before to call the kernel main function).
Apparently it works.. but I'm not so happy with this solution... I'm sure that there is a better (and perhaps more elegant) way to do this..
Here is my bootstrap asm code:
Code: Select all
;
; bootstrap.asm -- Kernel start location. Also defines multiboot header.
; Based on Bran's kernel development tutorial file start.asm
;
MBOOT_PAGE_ALIGN equ 1<<0 ; Load kernel and modules on a page boundary
MBOOT_MEM_INFO equ 1<<1 ; Provide your kernel with memory info
MBOOT_HEADER_MAGIC equ 0x1BADB002 ; Multiboot Magic value
; NOTE: We do not use MBOOT_AOUT_KLUDGE. It means that GRUB does not
; pass us a symbol table.
MBOOT_HEADER_FLAGS equ MBOOT_PAGE_ALIGN | MBOOT_MEM_INFO
MBOOT_CHECKSUM equ -(MBOOT_HEADER_MAGIC + MBOOT_HEADER_FLAGS)
[BITS 32] ; All instructions should be 32-bit.
[GLOBAL mboot] ; Make 'mboot' accessible from C.
[EXTERN code] ; Start of the '.text' section.
[EXTERN bss] ; Start of the .bss section.
[EXTERN end] ; End of the last loadable section.
mboot:
dd MBOOT_HEADER_MAGIC ; GRUB will search for this value on each
; 4-byte boundary in your kernel file
dd MBOOT_HEADER_FLAGS ; How GRUB should load your file / settings
dd MBOOT_CHECKSUM ; To ensure that the above values are correct
dd mboot ; Location of this descriptor
dd code ; Start of kernel '.text' (code) section.
dd bss ; End of kernel '.data' section.
dd end ; End of kernel.
dd start ; Kernel entry point (initial EIP).
[GLOBAL start] ; Kernel entry point.
[EXTERN kmain] ; This is the entry point of our C code
%define PAGE_DIR 0x9C000
%define PAGE_TABLE_0 0x9D000
%define PAGE_TABLE_768 0x9E000
%define PAGE_TABLE_ENTRIES 1024
%define PRIV 3 ; attributes (page is present;page is writable; supervisor mode)
start:
pusha
mov eax, PAGE_TABLE_0
mov ebx, 0x0 | PRIV
mov ecx, PAGE_TABLE_ENTRIES
.loop:
mov dword [eax], ebx
add eax, 4
add ebx, 4096
loop .loop
mov eax, PAGE_TABLE_0 | PRIV
mov dword [PAGE_DIR], eax
mov eax, PAGE_TABLE_768 | PRIV
mov dword [PAGE_DIR+(768*4)], eax
mov eax, PAGE_DIR
mov cr3, eax
mov eax, cr0
or eax, 0x80000000
mov cr0, eax ;enable paging
mov eax, PAGE_TABLE_768
mov ebx, 0x100000 | PRIV
mov ecx, PAGE_TABLE_ENTRIES
.loop2:
mov dword [eax], ebx
add eax, 4
add ebx, 4096
loop .loop2
popa
; Load multiboot information:
push ebx
; Execute the kernel:
cli ; Disable interrupts.
call kmain ; call our main() function.
jmp $ ; Enter an infinite loop, to stop the processor
; executing whatever rubbish is in the memory
; after our kernel!
But is Virtual Memory really working ?? Well I'd say yes, since I don't get any page fault and I read the cr0 register to be sure it is set correctly..
Sorry for my bad english, it is late night here and i'm very tired