Page 1 of 1

Funky Colors (Paging...)

Posted: Mon Oct 03, 2005 6:34 pm
by Cjmovie
OK, I'm trying to get my second stage bootloader to set up paging so that it can make two mappings:
0 - 4mb where virt. = phys.
0xC0000000 - 0xC0400000 (4mb) map to 0 - 4mb also
(so both point to same 0-4mb physical location)

To do this I clear a page table to 0 and set up two page directorys. Only problem is this: when I run my kernel, it is rather obvious something is wrong:

Image

So it seems that it works, but it's mapping incorrectly. I have below relevant code from stage2. At this point I am in 32-bits with flat-mode addressing (3 gdt entries, Null-Code-Data, 0-4gb).

Code: Select all

;Now, we must do a quick setup - we want to
;use paging to map the kernel to 0xC0000000. We also want to map
;the first 4mb -> phys., and we will also use that area for stack
;First, loop through page directory and set all entries to 0
 mov eax, 0x90000         ;Start at 0x90000
 
.LoopClearDir:
 mov BYTE [ds:eax], 0x00  ;Set that byte to 0
 inc eax                  ;Next byte
 cmp eax, 0x90FFF         ;Done?
 je .DoneClearDir         ;Yes, quit loop
 jmp .LoopClearDir        ;Loop until done
.DoneClearDir:
 
 ;Next, set entries for 0xC0000000 to 0xC0400000 for kernel
 mov eax, 0x91000         ;Location of page table
 mov ebx, 0x110000        ;Next location to map
 
.LoopCreateTable:
 mov ecx, ebx             ;Get next location to map to
 or ecx, 3                ;Supervisor level, read/write, present (011)
 mov [ds:eax], ecx        ;Set it in table
 add ebx, 4096            ;Next page to map
 add eax, 4               ;Next index in page table
 cmp eax, 0x91FFF         ;End of page table?
 je .DoneCreateTable      ;Yes, quit loop.
 jmp .LoopCreateTable     ;Loop until table is full
.DoneCreateTable:
 
 ;Next, set entries for 0x00000000 to 0x00400000 for stack
 mov eax, 0x92000         ;Location of page table (stack area)
 mov ebx, 0x000000        ;Next location to map
 
.LoopCreateTable2:
 mov ecx, ebx             ;Get next location to map to
 or ecx, 3                ;Supervisor level, read/write, present (011)
 mov [ds:eax], ecx        ;Set it in table
 add ebx, 4096            ;Next page to map
 add eax, 4               ;Next index in page table
 cmp eax, 0x91FFF         ;End of page table?
 je .DoneCreateTable2      ;Yes, quit loop.
 jmp .LoopCreateTable2     ;Loop until table is full
.DoneCreateTable2:
 
 ;Now, we set the directory entry (for 0xC0000000) to point to our table (kernel)
 mov eax, 0x91000         ;Address of page table
 or eax, 3                ;Or it with 011 (supervisor, read/write, present)
 mov [ds:0x90C00], eax    ;Set it
 
 ;Now, we set the directory entry (for 0x00000000) to point to our table (stack)
 mov eax, 0x92000         ;Address of page table
 or eax, 3                ;Or it with 011 (supervisor, read/write, present)
 mov [ds:0x90000], eax    ;Set it
 
 mov eax, 0x90000         ;Get location page directory
 mov cr3, eax             ;Put it in CR3
 
 mov eax, CR0             ;Get CR0
 or eax, 0x80000000       ;Or it with 'enable paging' bit
 mov cr0, eax             ;Enable paging!!!!!!!
*edit*
Note, I have also updated my linker script for the new location.

*edit 2*
Sorry, copied one spot wrong somehow....

Re:Funky Colors (Paging...)

Posted: Mon Oct 03, 2005 8:44 pm
by Brendan
Hi,

Your loops are messed up. This is your third loop, with everything removed:

Code: Select all

 mov eax, 0x92000         ;Location of page table
 
.Loop:
 add eax, 4
 cmp eax, 0x91FFF
 je .Done
 jmp .Loop
.Done:
First, EAX will go from 0x92000 until it wraps around to 0x00000, then it'll go from 0x00000 to 0x91FFC, and then it'll be 0x92000 and do it all again. It's an endless loop because EAX never actually equals 0x91FFF.

Instead, you'd want something like:

Code: Select all

   mov eax, 0x92000         ;Location of page table
 
.Loop:
   add eax, 4
   cmp eax, 0x93000
   jb .Loop
Alternatively, you might want to do something like this:

Code: Select all

;Now, we must do a quick setup - we want to
;use paging to map the kernel to 0xC0000000. We also want to map
;the first 4mb -> phys., and we will also use that area for stack
;First, loop through page directory and set all entries to 0

   cld
   mov edi, 0x90000         ;Start at 0x90000
   mov ecx, 1024            ;Do 1024 page directory entries
   xor eax,eax              ;Fill them with zero
   rep stosd                ;Do them all

;Next, set entries for 0xC0000000 to 0xC0400000 for kernel
   mov edi, 0x91000         ;Location of page table
   mov ecx, 1024            ;Do 1024 page table entries
   mov eax, 0x110000 | 3    ;Next location to map with supervisor, read/write and present flags (011)
.nextPTE:
   stosd                    ;Do this page table entry
   add eax,0x1000           ;Next location to map with supervisor, read/write and present flags
   loop .nextPTE            ;Do them all

;Next, set entries for 0x00000000 to 0x00400000 for stack
   mov edi, 0x92000         ;Location of page table
   mov ecx, 1024            ;Do 1024 page table entries
   mov eax, 0x0000 | 3      ;Next location to map with supervisor, read/write and present flags (011)
.nextPTE:
   stosd                    ;Do this page table entry
   add eax,0x1000           ;Next location to map with supervisor, read/write and present flags
   loop .nextPTE            ;Do them all


;Now, we set the directory entry (for 0xC0000000) to point to our table (kernel)
   mov dword [0x90C00], 0x91000 | 3
 
 ;Now, we set the directory entry (for 0x00000000) to point to our table (stack)
   mov dword [0x90000], 0x92000 | 3
 
   mov eax, 0x90000         ;Get location page directory
   mov cr3, eax             ;Put it in CR3
 
   mov eax, CR0             ;Get CR0
   or eax, 0x80000000       ;Or it with 'enable paging' bit
   mov cr0, eax             ;Enable paging!!!!!!!

Cheers,

Brendan

Re:Funky Colors (Paging...)

Posted: Tue Oct 04, 2005 7:22 pm
by Cjmovie
Woah, that would make a lot of sense. To be honest, I had no idea how to use/how were used the 'loop', 'rep', and 'stos' instructions. Only by you writing that and me being forced to look them up in the intel manual.

However, for some reason, the stosd functions kills the stage2.....

Possible I have the flag set to grow the wrong way? I did get it to work, however, replacing the stosd with a mov and add instruction.

Re:Funky Colors (Paging...)

Posted: Tue Oct 04, 2005 8:41 pm
by Brendan
Hi,
Cjmovie wrote:However, for some reason, the stosd functions kills the stage2.....

Possible I have the flag set to grow the wrong way? I did get it to work, however, replacing the stosd with a mov and add instruction.
The only flag that would effect it is the "direction" flag, which is set correctly at the beginning with the "CLD" instruction.

I'm guessing, but is ES set the same as DS? The STOS instruction uses ES not DS and does not allow a segment override (for e.g. "DS: STOSD" won't work), so if ES is different then it'd cause problems...


Cheers,

Brendan