Long mode initialisation problem

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
neonek
Member
Member
Posts: 38
Joined: Thu Aug 28, 2008 1:53 pm
Location: Białystok - Podlasie, Poland

Long mode initialisation problem

Post by neonek »

Hello!

I'm trying to port my little kernel over to 64-bit. Just from beginning I've got problems. Code assembles just fine, but it doesn't run properly. I'm testing it under VBox, which triple faults and QEMU, which just hangs. Bochs reboots all the time so isn't very helpful. I've got a small request, can somebody check my code? Here it is.

Regards,
Mark
Please correct my English. If you'll find mistake please tell me about it so I can improve my English.
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: Long mode initialisation problem

Post by Combuster »

Bochs (unlike other emus) will print messages to the console when it resets - I suggest you start reading those instead of claiming it doesn't help.
"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 ]
neonek
Member
Member
Posts: 38
Joined: Thu Aug 28, 2008 1:53 pm
Location: Białystok - Podlasie, Poland

Re: Long mode initialisation problem

Post by neonek »

Bochs reboots just after GRUB shows, but I can't do anything and it doesn't print anything to console.

Regards,
Mark
Please correct my English. If you'll find mistake please tell me about it so I can improve my English.
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Re: Long mode initialisation problem

Post by pcmattman »

But it does write a wealth of information to the bochsout file, which you can look at to see where and why it reset.
Gigasoft
Member
Member
Posts: 856
Joined: Sat Nov 21, 2009 5:11 pm

Re: Long mode initialisation problem

Post by Gigasoft »

I can see two problems. The upper part of page table entries aren't being initialized, and the selector you use in the far jump is wrong (being the address of the Code descriptor rather than it's offset in the GDT which is 8).
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: Long mode initialisation problem

Post by AJ »

Hi,
neonek wrote:Bochs reboots just after GRUB shows, but I can't do anything and it doesn't print anything to console.

Code: Select all

cpu: [...], reset_on_triple_fault=0
If you specify a log file, output will go there, otherwise, it will be sent to the console.

Cheers,
Adam
neonek
Member
Member
Posts: 38
Joined: Thu Aug 28, 2008 1:53 pm
Location: Białystok - Podlasie, Poland

Re: Long mode initialisation problem

Post by neonek »

OK. Now I know that bochs reboots due to #PF followed by #GP. I know what's wrong but I can't figure out where bug is. Please, can somebody check my code?

Regards,
Mark
Please correct my English. If you'll find mistake please tell me about it so I can improve my English.
Gigasoft
Member
Member
Posts: 856
Joined: Sat Nov 21, 2009 5:11 pm

Re: Long mode initialisation problem

Post by Gigasoft »

Your PML4, PDPT, PD and PT aren't aligned. Either put them at a fixed 4K aligned address outside the kernel, or make sure that the BSS section has 4K alignment, and move the MBOOT and MBOOT_Magic variables to the end. You should also fix the two other issues I mentioned if you haven't already.
neonek
Member
Member
Posts: 38
Joined: Thu Aug 28, 2008 1:53 pm
Location: Białystok - Podlasie, Poland

Re: Long mode initialisation problem

Post by neonek »

I've aligned my paging tables and moved MBOOT variables after paging tables in .bss. GDT.Code has valid value (0x08). Still I can't figure out how to set up those paging tables. What did you mean by "The upper part of page table entries aren't being initialized" ? And bochs gives me a weird RIP values after jump to long mode code (00000000802000df). Thanks for help.

Regards,
Mark
Please correct my English. If you'll find mistake please tell me about it so I can improve my English.
Gigasoft
Member
Member
Posts: 856
Joined: Sat Nov 21, 2009 5:11 pm

Re: Long mode initialisation problem

Post by Gigasoft »

Chances are that your kernel is linked at 0x200000 instead of 0xffffffff80000000. It should be linked at 0xffffffff80000000. And in the V2P macro, you also need to add the physical address of the kernel (since it's probably not loaded at 0).

Code: Select all

.IdentityMap:
    mov [edi], ebx
    add ebx, 0x1000
    add edi, 0x08
    loop .IdentityMap
In this code, you're only initializing the first half of the page table entries. You should have a and dword [edi+4], byte 0 in there.
neonek
Member
Member
Posts: 38
Joined: Thu Aug 28, 2008 1:53 pm
Location: Białystok - Podlasie, Poland

Re: Long mode initialisation problem

Post by neonek »

My startup section is linked at 0x100000. Other sections are linked at 0xFFFFFFFF80000000. I've changed a bit KERNEL_BASE to 0xFFFFFFFF7FF00000, so V2P gives proper values. I've also added code for clearing upper part of page tables but with no luck. Bochs still page faults at 0x0000000080200***. I don't have any ideas what can be wrong here. Anyway, thank you for your help.

Regards,
Mark
Please correct my English. If you'll find mistake please tell me about it so I can improve my English.
Post Reply