Page 2 of 2
Re: Clarifying some points regarding paging
Posted: Fri Jun 03, 2011 11:39 am
by Luns
That makes sense, thanks for the response.
I'm having some trouble implementing it though. I've just copied the linker script from the
Higher Half Bare Bones wiki article, but when I try starting the kernel in QEMU it crashes - "Trying to execute code outside RAM or ROM at 0xC010000c." I don't think it has anything to do with my code - I tried moving a value into ecx on the very first line but according to QEMU's register dump that value never gets there. Does anyone know what the problem might be?
Re: Clarifying some points regarding paging
Posted: Fri Jun 03, 2011 12:11 pm
by Luns
How do I give it the correct mappings? It crashes before it gets to my code, is there something I need to do in my linker script?
I do 'qemu -m 64 -kernel kernel.bin', where kernel.bin is what ld gives me after linking all my object file together. Is that how I'm supposed to be using QEMU?
Re: Clarifying some points regarding paging
Posted: Fri Jun 03, 2011 6:16 pm
by Luns
Alright, so I made a floppy image with GRUB on it and it seems to boot. My kernel seems to triple-fault, but that's probably a problem with how I map the first bit of memory - I'll take a closer look at it tonight...
Now that I have to use GRUB though, does anyone know how I can tell GRUB before hand how big the kernel it is, and then have it automatically load the kernel and boot? So I wouldn't have to type kernel '200+x, boot' every time I test out my kernel?
And thanks very much for the help thus far
Re: Clarifying some points regarding paging
Posted: Fri Jun 03, 2011 8:41 pm
by Owen
Use an actual file system. I suggest using a CD-ROM image
Re: Clarifying some points regarding paging
Posted: Sat Jun 04, 2011 10:40 am
by Luns
Awesome, thanks
I took another look at my code, but I'm still not sure what's wrong. I'm just trying to fill a page table for the first 4 MB of memory, put that into a page directory, then load that into cr3 and enable paging. As soon as I enable paging though, it triple-faults. I checked out the values in the page table and they seem correct (3, 4099, ...), the first value in my page directory contains the correct address of my page table, as does (what I think is) the 768th page directory entry. Could anyone take a look and see if they notice what's wrong?
Code: Select all
global loader ; making entry point visible to linker
; setting up the Multiboot header - see GRUB docs for details
MODULEALIGN equ 1<<0 ; align loaded modules on page boundaries
MEMINFO equ 1<<1 ; provide memory map
FLAGS equ MODULEALIGN | MEMINFO ; this is the Multiboot 'flag' field
MAGIC equ 0x1BADB002 ; 'magic number' lets bootloader find the header
CHECKSUM equ -(MAGIC + FLAGS) ; checksum required
align 4096
pageDirectory equ (0x00100000 + 25600) & 0xFFFFF000 ;pretty much directly after kernel, making sure it's page-aligned
pageTable equ pageDirectory + 0x1000
section .text
align 4
MultiBootHeader:
dd MAGIC
dd FLAGS
dd CHECKSUM
; reserve initial kernel stack space
STACKSIZE equ 0x4000 ; that's 16k.
loader:
mov edi, 0xb8000 ;make sure we're alive
mov [edi], byte 'a'
inc edi
mov [edi], byte 0x07
mov ecx, 3 ;each entry will be a multiple of 4096, +3 (for kernel-mode, r/w, present)
mov eax, pageTable
.idMap:
mov [eax], ecx ;move value in ecx into address of eax
add ecx, 0x1000 ;add 4096 to ecx
add eax, 4 ;advance address in eax by one dword
cmp ecx, (1024*0x1000) ;if we've filled the table with 1024 entries, we're done
jl .idMap
mov eax, pageTable ;move address of pageTable into eax
mov ecx, pageDirectory ;and the address of pageDirectory into ecx
mov [ecx], eax ;move address of pageTable into pageDirectory [0]
mov edx, pageDirectory ;move address of pageDirectory into edx
add edx, (768*4) ;and advance edx by 768 dwords
mov [edx], eax ;and put address of pageTable there too (into pageDirectory [768], which translates to virtual 0xC0000000
mov cr3, ecx ;ecx still contains pageDirectory address, put it into cr3
mov ecx, cr0 ;and enable paging...
or ecx, 0x80010000
mov cr0, ecx
jmp $ ;loop forever
Re: Clarifying some points regarding paging
Posted: Sat Jun 04, 2011 11:51 am
by Combuster
Remember to set P/W/U for the page directory entry as well as the page table entries
Re: Clarifying some points regarding paging
Posted: Sat Jun 04, 2011 10:24 pm
by Luns
Oh, right! That got me past that code, thanks.
I still triple-fault though. After I do all this stuff in my very first loader file, I shouldn't have to change anything else in the rest of my kernel right? Or do I need to change my memory addresses (like is text video memory still at 0xB8000), or something else?
Re: Clarifying some points regarding paging
Posted: Fri Jun 10, 2011 9:43 am
by amn
Pardon me for interrupting, but shouldn't this be moved into "Design & Theory" folder?