PML4 on Core2Duo or VirtualBox issue

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.
gusc
Member
Member
Posts: 50
Joined: Tue Feb 05, 2013 1:42 pm

PML4 on Core2Duo or VirtualBox issue

Post by gusc »

Hello!

I've been writing my silly OS project for some time and I've finally managed to setup Long-mode, but it only works in Bosch and Qemu. VirtualBox on my Core2Duo in Windows XP host hangs when I try to enable paging after I've set up everything else. Full source can be found here, but the code snippet I teared down that hangs is here (in bbp/boot/boot.asm):

Code: Select all

  ; Setup long mode.
  mov eax, cr0                ; read from CR0
  and eax, 0x7FFFFFFF         ; clear paging bit
  mov cr0, eax                ; write to CR0
  
  lgdt [gdt64_ptr]            ; load 64bit GDT pointer
  
  mov eax, cr4                ; read from CR4
  or eax, 0x000000A0          ; set the PAE and PGE bit
  mov cr4, eax                ; write to CR4
  
  mov eax, [pml4_ptr32]       ; point eax to PML4 pointer location
  or eax, 0x00000008          ; enable Page write-through
  mov cr3, eax                ; save PML4 pointer into CR3
  
  mov ecx, 0xC0000080         ; read from the EFER MSR
  rdmsr                       ; read MSR
  or eax, 0x00000101          ; set the LME and SYSCALL/SYSRET bits
  wrmsr                       ; write MSR
  
  mov eax, cr0                ; read from CR0
  or eax,0x80000000           ; set paging bit
; Here it stops, right after I write into CR0  
  mov cr0, eax                ; write to CR0
  
  jmp 0x08:start64            ; do the magic jump to Long Mode
I've enabled PAE, VT-x and Nested paging in VirtualBox.
I can't find any references to which version of Intel's CPUs started to support PML4, could this be the issue or is it that I'm using a 32bit OS as the host? I know that I should probably do a proper CPUID check, but some "feature reference tables" would be nice to have here on OSDev Wiki.
User avatar
iansjack
Member
Member
Posts: 4711
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: PML4 on Core2Duo or VirtualBox issue

Post by iansjack »

Presuming that you have enabled the hardware virtualization features in BIOS, VirtualBox should be able to run a 64-bit guest on a 32-bit host. This also assumes that you have set everything correctly in VirtualBox; the easiest way to do this is to use the "Create VM" wizard to set up a 64-bit guest. Try installing another 64-bit OS, say a 64-bit Linux, and see if it works. All Core 2 processors support 64-bit, but not all support hardware virtualization.
gusc
Member
Member
Posts: 50
Joined: Tue Feb 05, 2013 1:42 pm

Re: PML4 on Core2Duo or VirtualBox issue

Post by gusc »

Well that might be a problem, as I don't see any hardware virtualization options in my BIOS (I have to PCs with Core2Duo and none of them have).

Although with CPU-Z I can see that VT-x is available and also I found on Intel's site that both of my CPUs should support VT-x. Maybe it's something to do with a motherboard's chipset? I have the Nvidia nForce 650i SLI and Intel GM965 on the other PC.
User avatar
iansjack
Member
Member
Posts: 4711
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: PML4 on Core2Duo or VirtualBox issue

Post by iansjack »

Rather than guessing, why not try my practical suggestion. Try installing a 64-bit Linux distro in a VM. Or, you could always look at the VM log to see why it is failing.
Opcode
Member
Member
Posts: 29
Joined: Mon Apr 01, 2013 2:50 pm

Re: PML4 on Core2Duo or VirtualBox issue

Post by Opcode »

gusc wrote:I can't find any references to which version of Intel's CPUs started to support PML4...
Since you were referring to finally being in Longmode, I can assume you're targeting the x86-64 architecture. If that's the case then you can assume PML4 support.
gusc
Member
Member
Posts: 50
Joined: Tue Feb 05, 2013 1:42 pm

Re: PML4 on Core2Duo or VirtualBox issue

Post by gusc »

iansjack wrote:Rather than guessing, why not try my practical suggestion. Try installing a 64-bit Linux distro in a VM. Or, you could always look at the VM log to see why it is failing.
Working on it. :) And no, the VM log does not say anything about failing, it just stops (like an infinite loop).
Opcode wrote:Since you were referring to finally being in Longmode, I can assume you're targeting the x86-64 architecture. If that's the case then you can assume PML4 support.
Can I assume, from you answer, that every CPU that supports x-86-64 also supports PML4 paging? My kernel is targeting long mode, and it runs fine in Bochs and Qemu, but currently I'm unable to test it in VirtualBox.

Maybe it's my old PC after all. Well I'll see in a few hours once I'll download 64bit Ubuntu image from the net.
User avatar
Griwes
Member
Member
Posts: 374
Joined: Sat Jul 30, 2011 10:07 am
Libera.chat IRC: Griwes
Location: Wrocław/Racibórz, Poland
Contact:

Re: PML4 on Core2Duo or VirtualBox issue

Post by Griwes »

Can I assume, from you answer, that every CPU that supports x-86-64 also supports PML4 paging?
You really need to RTFM.
Reaver Project :: Repository :: Ohloh project page
<klange> This is a horror story about what happens when you need a hammer and all you have is the skulls of the damned.
<drake1> as long as the lock is read and modified by atomic operations
Opcode
Member
Member
Posts: 29
Joined: Mon Apr 01, 2013 2:50 pm

Re: PML4 on Core2Duo or VirtualBox issue

Post by Opcode »

gusc wrote:
Opcode wrote:Since you were referring to finally being in Longmode, I can assume you're targeting the x86-64 architecture. If that's the case then you can assume PML4 support.
Can I assume, from you answer, that every CPU that supports x-86-64 also supports PML4 paging? My kernel is targeting long mode, and it runs fine in Bochs and Qemu, but currently I'm unable to test it in VirtualBox.
Yeap, all 64bit CPUs have PML4. There are some features that you do need to check (e.g. not all 64bit CPUs support 1GB page frames). You can check the Intel manual #3 chapter 4 for further information.
gusc
Member
Member
Posts: 50
Joined: Tue Feb 05, 2013 1:42 pm

Re: PML4 on Core2Duo or VirtualBox issue

Post by gusc »

Griwes wrote:You really need to RTFM.
My head is spinning around and I'm starting to get cross-eyed by reading that 3k page manual :) But, yeah, I cross checked and I couldn't get a clear answer, that "Yes, starting from architecture codenamed "X" we have 100% PML4 support".

Intel's manual can be taunting sometimes, as it's referencing a lot of things by different names + I am a noob :) I'm still not sure whether PAE, EM64T, IA-32e paging and PML4 support means the same thing or not (well, OK, PML4 is the name of page table).

So I'm sorry if I'm asking dumb questions here, please forgive me :)
User avatar
Griwes
Member
Member
Posts: 374
Joined: Sat Jul 30, 2011 10:07 am
Libera.chat IRC: Griwes
Location: Wrocław/Racibórz, Poland
Contact:

Re: PML4 on Core2Duo or VirtualBox issue

Post by Griwes »

There is no clear "starting with [...], we have full PML4 support", because there never was a partial PML4 support and PML4 is required to even be in long mode. No-one wants you to read the 3k page manual; you are supposed to be able to use indexes and search in PDFs if you want to get anything hobby OS oriented done.
Reaver Project :: Repository :: Ohloh project page
<klange> This is a horror story about what happens when you need a hammer and all you have is the skulls of the damned.
<drake1> as long as the lock is read and modified by atomic operations
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: PML4 on Core2Duo or VirtualBox issue

Post by Combuster »

Although, I can recommend reading volume 3A cover-to-cover once when you got the basics running, just so that you know what's there and what you might find interesting or necessary to use.
"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 ]
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: PML4 on Core2Duo or VirtualBox issue

Post by Brendan »

Hi,
gusc wrote:I've been writing my silly OS project for some time and I've finally managed to setup Long-mode, but it only works in Bosch and Qemu. VirtualBox on my Core2Duo in Windows XP host hangs when I try to enable paging after I've set up everything else.
gusc wrote:I've enabled PAE, VT-x and Nested paging in VirtualBox.
When you create the virtual machine, there's a drop-down list of "guest OSs". I think you need to select a 64-bit guest from this list.

From the VirtualBox manual:
VirtualBox supports 64-bit guest operating systems, even on 32-bit host operating systems, provided that the following conditions are met:
  • You need a 64-bit processor with hardware virtualization support (see the section called “Hardware vs. software virtualization”).
  • You must enable hardware virtualization for the particular VM for which you want 64-bit support; software virtualization is not supported for 64-bit VMs.
  • If you want to use 64-bit guest support on a 32-bit host operating system, you must also select a 64-bit operating system for the particular VM. Since supporting 64 bits on 32-bit hosts incurs additional overhead, VirtualBox only enables this support upon explicit request.
Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
gusc
Member
Member
Posts: 50
Joined: Tue Feb 05, 2013 1:42 pm

Re: PML4 on Core2Duo or VirtualBox issue

Post by gusc »

So, yesterday I downloaded this image which is Linux Mint 64bit version and it was running fine on the same PC I tried to run my own tiny kernel.

My OS ... still nothing. So I was wondering, Brendan, what is meant by:
Brendan wrote:
  • If you want to use 64-bit guest support on a 32-bit host operating system, you must also select a 64-bit operating system for the particular VM. Since supporting 64 bits on 32-bit hosts incurs additional overhead, VirtualBox only enables this support upon explicit request.
An explict request - does it mean some kind of configuration setting in the *.vbox file or there are more things to do than MOV to CR0 with paging bit set, like some kind of custom instruction that tells VirtualBox that this OS should be run in 64bits?
gusc
Member
Member
Posts: 50
Joined: Tue Feb 05, 2013 1:42 pm

Re: PML4 on Core2Duo or VirtualBox issue

Post by gusc »

I've also done some trial runs with different settings, like creating a new VM using Ubuntu 64-bit presets or FreeBSD 64-bit presets and none of them work - they all hang on the same instruction. To me it looks like I'm the one to blame (i.e. there is a bug in my code), but I can not figure out what and where.
gusc
Member
Member
Posts: 50
Joined: Tue Feb 05, 2013 1:42 pm

Re: PML4 on Core2Duo or VirtualBox issue

Post by gusc »

I was digging around and I came to conclusion - maybe I'm debugging in the wrong direction. So I read an article about VirtualBox Log files and it's funny, but it's not your regular log file that just writes anything that's happening right now. Which also sounds reasonable, as it's not an emulator.

Anyway, there's this point that you can't see what's actually happening until you shut down the VM. It's then when it logs all the registers and information about your OS's state. And this is where I found that my OS was actually in Long mode:

Code: Select all

00:00:07.201562 Guest paging mode:  AMD64 (changed 6 times), A20 disabled (changed 1 times)
00:00:07.201562 Shadow paging mode: AMD64
00:00:07.201562 Host paging mode:   PAE+G
So that means - the jump is working fine. So why I don't see anything on the screen? Are there any known issues with int 10 mode 3 (80x25 text mode in long mode)? Maybe that's why I get no response on the screen?
Post Reply