Page 1 of 1

Info tab shows strange pages with PAE

Posted: Sun Feb 27, 2011 9:59 am
by AlfaOmega08
Anyone has any idea of why the following basic PAE code:

Code: Select all

; Multiboot Header Definitions
; We want modules to be page-aligned and memory informations in the info block
MB_MAGIC		EQU	0x1BADB002
MB_MODALIGN		EQU	1 << 0
MB_MEMINFO		EQU	1 << 1
MB_FLAGS		EQU	(MB_MEMINFO | MB_MODALIGN)
MB_CHECKSUM		EQU	-(MB_MAGIC + MB_FLAGS)

[BITS 32]
[SECTION .header]
mbhdr:
	dd MB_MAGIC
	dd MB_FLAGS
	dd MB_CHECKSUM

[SECTION .text]
[GLOBAL _loader]
; Do not use the stack until Paged is called.
; Preserve the content of ebx, which contains a pointer to grub informations
; We have to load a gdt asap, as grub will not give us a valid one.
_loader:
	mov eax, gdtr
	lgdt [eax]
	jmp 0x08:postGdt
	
postGdt:
	; Load segment registers
	mov eax, 0x10
	mov ds, ax
	mov es, ax
	mov fs, ax
	mov gs, ax

	mov eax, Pd
	or eax, 1

	mov edi, Pdpt
	mov [edi], eax

	mov edi, Pd
	mov [edi], DWORD 0x83

	; Load page directory in cr3
	mov eax, Pdpt
	mov cr3, eax

	; Enable PAE
	mov eax, cr4
	or eax, 0x00000030
	mov cr4, eax

	; Enable paging
	mov eax, cr0
	or eax, 0xC0000000
	mov cr0, eax

	jmp $

[SECTION .data]
ALIGN 4096
Pdpt:
	times (0x1000) DB 0

ALIGN 4096
Pd:
	times (0x1000) DB 0

; Temporary GDT set at boot
gdt:
	dd 0, 0
code:
	dw 0xFFFF
	dw 0x0000
	db 0x00
	db 0x9A
	db 0xCF
	db 0x00

data:
	dw 0xFFFF
	dw 0x0000
	db 0x00
	db 0x92
	db 0xCF
	db 0x00

gdtr:
	DW	0x17
	DD	gdt
which basically identity maps the first 2 Mb using PAE paging and then setups CR0/3/4 to enable PAE, produces the following output on the bochs debugger?

Code: Select all

info tab
cr3: 0x0000000000101000
0x00000000-0x001fffff -> 0x0000000000000000-0x00000000001fffff            <-- The only correct one...
0x54a20000-0x54a20fff -> 0x0000003df000e000-0x0000003df000efff
0x54a3c000-0x54a3cfff -> 0x00000000f000f000-0x00000000f000ffff
0x54a8c000-0x54a8cfff -> 0x00000003d4000000-0x00000003d4000fff
0x54a91000-0x54a91fff -> 0x0000000000085000-0x0000000000085fff
0x54a95000-0x54a95fff -> 0x00000000c0003000-0x00000000c0003fff
0x54aa5000-0x54aa5fff -> 0x0000000000000000-0x0000000000000fff
0x54aa8000-0x54aa8fff -> 0x0000000000000000-0x0000000000000fff
0x54aab000-0x54aabfff -> 0x0000000000000000-0x0000000000000fff
0x54aae000-0x54aaefff -> 0x0000000000000000-0x0000000000000fff
0x54ab1000-0x54ab1fff -> 0x0000000000000000-0x0000000000000fff
0x54ae5000-0x54ae5fff -> 0x0000000007cbf000-0x0000000007cbffff
0x54aea000-0x54aeafff -> 0x0000000000000000-0x0000000000000fff
[...]
0x5d4b1000-0x5d4b1fff -> 0x0000000000000000-0x0000000000000fff
0x5d4e5000-0x5d4e5fff -> 0x0000000007cbf000-0x0000000007cbffff
0x5d4ea000-0x5d4eafff -> 0x0000000000000000-0x0000000000000fff
0x94a20000-0x94a20fff -> 0x0000003df000e000-0x0000003df000efff
0x94a3c000-0x94a3cfff -> 0x00000000f000f000-0x00000000f000ffff
0x94a8c000-0x94a8cfff -> 0x00000003d4000000-0x00000003d4000fff
0x94a91000-0x94a91fff -> 0x0000000000085000-0x0000000000085fff
0x94a95000-0x94a95fff -> 0x00000000c0003000-0x00000000c0003fff
0x94aa5000-0x94aa5fff -> 0x0000000000000000-0x0000000000000fff
0x94aa8000-0x94aa8fff -> 0x0000000000000000-0x0000000000000fff
0x94aab000-0x94aabfff -> 0x0000000000000000-0x0000000000000fff
0x94aae000-0x94aaefff -> 0x0000000000000000-0x0000000000000fff
0x94ab1000-0x94ab1fff -> 0x0000000000000000-0x0000000000000fff
0x94ae5000-0x94ae5fff -> 0x0000000007cbf000-0x0000000007cbffff
[...]
0x9d4b1000-0x9d4b1fff -> 0x0000000000000000-0x0000000000000fff
0x9d4e5000-0x9d4e5fff -> 0x0000000007cbf000-0x0000000007cbffff
0x9d4ea000-0x9d4eafff -> 0x0000000000000000-0x0000000000000fff
0xd4a20000-0xd4a20fff -> 0x0000003df000e000-0x0000003df000efff
0xd4a3c000-0xd4a3cfff -> 0x00000000f000f000-0x00000000f000ffff
0xd4a8c000-0xd4a8cfff -> 0x00000003d4000000-0x00000003d4000fff
0xd4a91000-0xd4a91fff -> 0x0000000000085000-0x0000000000085fff
0xd4a95000-0xd4a95fff -> 0x00000000c0003000-0x00000000c0003fff
0xd4aa5000-0xd4aa5fff -> 0x0000000000000000-0x0000000000000fff
0xd4aa8000-0xd4aa8fff -> 0x0000000000000000-0x0000000000000fff
0xd4aab000-0xd4aabfff -> 0x0000000000000000-0x0000000000000fff
[...]
0xdd4e5000-0xdd4e5fff -> 0x0000000007cbf000-0x0000000007cbffff
0xdd4ea000-0xdd4eafff -> 0x0000000000000000-0x0000000000000fff
Edit: I cut out much of the ouput...

Re: Info tab shows strange pages with PAE

Posted: Sun Feb 27, 2011 10:13 am
by Combuster
32-bit PAE has three levels of page tables, not two.

Re: Info tab shows strange pages with PAE

Posted: Sun Feb 27, 2011 10:16 am
by AlfaOmega08
Combuster wrote:32-bit PAE has three levels of page tables, not two.
I skipped the PageTable by setting the first entry of the page directory as a 2mb page :)

Code: Select all

mov edi, Pd
   mov [edi], DWORD 0x83

Re: Info tab shows strange pages with PAE

Posted: Sun Feb 27, 2011 12:40 pm
by AlfaOmega08
Anyone tested? Could it be a bochs fault?

Re: Info tab shows strange pages with PAE

Posted: Sun Feb 27, 2011 5:39 pm
by thepowersgang
@AlfaOmega08
First thing, try not to double post

Secondly, check that the pages are actually mapped yourself (dump the paging structures yourself)

Iirc, PAE's top level structure is only four quadwords long, so you should only have to check that.

Also, I'd suggest setting up your paging structures at compile-time instead of at runtime.

Re: Info tab shows strange pages with PAE

Posted: Mon Feb 28, 2011 1:17 am
by AlfaOmega08
thepowersgang wrote:Iirc, PAE's top level structure is only four quadwords long, so you should only have to check that.
I'd suggest setting up your paging structures at compile-time instead of at runtime.
Here is the new code rewritten using your suggestions:

Code: Select all

; Multiboot Header Definitions
; We want modules to be page-aligned and memory informations in the info block
MB_MAGIC		EQU	0x1BADB002
MB_MODALIGN		EQU	1 << 0
MB_MEMINFO		EQU	1 << 1
MB_FLAGS		EQU	(MB_MEMINFO | MB_MODALIGN)
MB_CHECKSUM		EQU	-(MB_MAGIC + MB_FLAGS)

[BITS 32]
[SECTION .header]
mbhdr:
	dd MB_MAGIC
	dd MB_FLAGS
	dd MB_CHECKSUM

[SECTION .text]
[GLOBAL _loader]
; Main entry point for x86(_64) architecture
; Do not use the stack until Paged is called.
; Preserve the content of ebx, which contains a pointer to grub informations
; We have to load a gdt asap, as grub will not give us a valid one.
_loader:
	mov eax, gdtr
	lgdt [eax]
	jmp 0x08:postGdt
	
postGdt:
	; Load segment registers
	mov eax, 0x10
	mov ds, ax
	mov es, ax
	mov fs, ax
	mov gs, ax
	
	mov edi, Pdpt
	or [edi], DWORD 1

	; Load page directory in cr3
	mov eax, Pdpt
	mov cr3, eax

	; Enable PAE
	mov eax, cr4
	or eax, 0x00000030
	mov cr4, eax

	; Enable paging
	mov eax, cr0
	or eax, 0xC0000000
	mov cr0, eax

	jmp $

[SECTION .data]
; Intel manual says that the PDPT should be at least 32-bytes aligned. We use 4096 for compatibility with x64 architectures.
ALIGN 4096
Pdpt:
	; First entry points to the page directory. The present flag is added at runtime.
	DD	Pd, 0
	times (6) DD 0

ALIGN 4096
Pd:
	; Identity map the first 16Mb
	DD	0x00000083, 0x00000000
	DD	0x00200083, 0x00000000
	DD	0x00400083, 0x00000000
	DD	0x00600083, 0x00000000
	DD	0x00800083, 0x00000000
	DD	0x00A00083, 0x00000000
	DD	0x00C00083, 0x00000000
	DD	0x00E00083, 0x00000000
	times (1008) DD 0

; Temporary GDT set at boot
gdt:
	dd 0, 0
code:
	dw 0xFFFF
	dw 0x0000
	db 0x00
	db 0x9A
	db 0xCF
	db 0x00

data:
	dw 0xFFFF
	dw 0x0000
	db 0x00
	db 0x92
	db 0xCF
	db 0x00

gdtr:
	DW	0x17
	DD	gdt
I dumped the whole pdpt and the first part of the pd using the "x" command of the bochs debbugger. It contains correct data. However "info tab" still shows garbage.
Anyone had similar problems on bochs?

Edit:
I added the following code before jmp $

Code: Select all

mov ebx, 0xdd4ea000
mov eax, [ebx]
0xdd4ea000-0xdd4eafff is mapped (according to the output of info tab) to 0x00000000-0x0000000fff
However it throws a page fault when reading that address. Just as it has to be. So I believe it is definitely a bochs debugger bug.

Edit 2:
Bochs is definitely gone crazy with its debugger. I finally managed to enable long mode and to put my kernel at 0xFFFFFFFF80000000. It runs ok at that address but info tab only shows the identity mapped lower zone...

Re: Info tab shows strange pages with PAE

Posted: Mon Feb 28, 2011 7:15 am
by shikhin
Hi,

I was also having the same problem. I consulted Brendan, he being a Guru in my eyes. According to him, Bochs debugger ignores the present bit, causing every directory and table already not set, to be at 0x00000000.

If you observe the results from 'info tab', you would notice that the entries are repeating, since they are all believed to be at 0x00000000.
Well, that was what I inferred from his reply. If I am wrong anywhere, please the experts and moderators, correct me out. :)

Regards,
Shikhin

Re: Info tab shows strange pages with PAE

Posted: Mon Feb 28, 2011 10:24 am
by AlfaOmega08
yes. I noticed repetitions in the addresses, and the present bit miss is the only rational explaination. Infact if I put the same directory in all the entries of the pdpt, the debugger shows correct values. Has this been signaled to bochs developers? Anyone here with svn write rights? I'm going to correct this in my copy and eventually post a patch here...

Re: Info tab shows strange pages with PAE

Posted: Tue Mar 01, 2011 6:59 pm
by thepowersgang
Well, your problem with the long mode paging is a known issue with Bochs (due to the way it implements `info tab`)

My only guess for the rest is that the garbage is due to the large page bit being ignored.