Page 1 of 1

Long mode initialisation problem

Posted: Mon Feb 22, 2010 1:38 pm
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

Re: Long mode initialisation problem

Posted: Mon Feb 22, 2010 1:41 pm
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.

Re: Long mode initialisation problem

Posted: Mon Feb 22, 2010 2:32 pm
by neonek
Bochs reboots just after GRUB shows, but I can't do anything and it doesn't print anything to console.

Regards,
Mark

Re: Long mode initialisation problem

Posted: Mon Feb 22, 2010 2:56 pm
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.

Re: Long mode initialisation problem

Posted: Mon Feb 22, 2010 4:33 pm
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).

Re: Long mode initialisation problem

Posted: Tue Feb 23, 2010 2:42 am
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

Re: Long mode initialisation problem

Posted: Tue Feb 23, 2010 12:13 pm
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

Re: Long mode initialisation problem

Posted: Tue Feb 23, 2010 1:57 pm
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.

Re: Long mode initialisation problem

Posted: Wed Feb 24, 2010 12:59 pm
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

Re: Long mode initialisation problem

Posted: Wed Feb 24, 2010 4:25 pm
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.

Re: Long mode initialisation problem

Posted: Thu Feb 25, 2010 4:05 am
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