Clarifying some points regarding paging

Discussions on more advanced topics such as monolithic vs micro-kernels, transactional memory models, and paging vs segmentation should go here. Use this forum to expand and improve the wiki!
Luns
Member
Member
Posts: 56
Joined: Sun May 01, 2011 12:15 am

Re: Clarifying some points regarding paging

Post 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?
Luns
Member
Member
Posts: 56
Joined: Sun May 01, 2011 12:15 am

Re: Clarifying some points regarding paging

Post 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?
Luns
Member
Member
Posts: 56
Joined: Sun May 01, 2011 12:15 am

Re: Clarifying some points regarding paging

Post 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 :)
User avatar
Owen
Member
Member
Posts: 1700
Joined: Fri Jun 13, 2008 3:21 pm
Location: Cambridge, United Kingdom
Contact:

Re: Clarifying some points regarding paging

Post by Owen »

Use an actual file system. I suggest using a CD-ROM image
Luns
Member
Member
Posts: 56
Joined: Sun May 01, 2011 12:15 am

Re: Clarifying some points regarding paging

Post by Luns »

Awesome, thanks :D

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
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Clarifying some points regarding paging

Post by Combuster »

Remember to set P/W/U for the page directory entry as well as the page table entries
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Luns
Member
Member
Posts: 56
Joined: Sun May 01, 2011 12:15 am

Re: Clarifying some points regarding paging

Post 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?
amn
Posts: 23
Joined: Fri Oct 03, 2008 10:14 am

Re: Clarifying some points regarding paging

Post by amn »

Pardon me for interrupting, but shouldn't this be moved into "Design & Theory" folder?
Post Reply