Page 1 of 1

Difficultly with Long Mode

Posted: Thu Apr 08, 2010 5:54 am
by proxy
So I've been toying with my 32-bit OS for quite some time and am trying to get into 64-bit development. But I am having a bit of difficulty getting into long mode on all my tests. Currently, what I have works in bochs, but gives a stack fault in vmware and stalls qemu. I am booting with grub, the intention is to have a higher half kernel, but currently I am just 1:1 mapping the first 6 MEG (physical load address is 2MB). Here is the code:

Code: Select all

#define ASM
#include "multiboot.h"

#define KERNEL_VMA 0xffffffff80000000

.code32
.align 4
multiboot_header:
	.long   MULTIBOOT_HEADER_MAGIC								/* magic */
	.long   MULTIBOOT_HEADER_FLAGS								/* flags */
	.long   -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)	/* checksum */
	.long   (multiboot_header - KERNEL_VMA)	/* header_addr */
	.long   (_start - KERNEL_VMA)			/* load_addr */
	.long   (_edata - KERNEL_VMA)			/* load_end_addr */
	.long   (_end - KERNEL_VMA)				/* bss_end_addr */
	.long   (bootstrap - KERNEL_VMA)		/* entry_addr */


.globl  bootstrap
bootstrap:
	/* make sure interrupts are off */
	cli
	
	/* load the GDT */
	lgdt (GDT64_PTR - KERNEL_VMA)

        /* this may not be necessary if since I use grub, just trying to be sure */
	mov $0x10, %ax
	mov %ax, %ds
	mov %ax, %es
	mov %ax, %fs
	mov %ax, %gs
	mov %ax, %ss
	/* give ourselfs a functional stack */
	movl $(init_stack_end - KERNEL_VMA), %esp
	
        /* once again, trying to find the issue, I don't think this is necessary */
	ljmp $0x08, $(boot32 - KERNEL_VMA)
boot32:

	/* ensure that paging is off */
	movl	%cr0, %eax
	btr		$31, %eax
	movl	%eax, %cr0
	
	/* reset EFLAGS. */
	pushl	$2
	popf
	
	/* load a level4 PD */
	movl	$(pml4 - KERNEL_VMA), %eax
	mov		%eax, %cr3
	
	/* enable CR4.PAE */
	movl	%cr4, %eax
	bts		$5, %eax
	movl	%eax, %cr4

	/* set IA32_EFER.LME */
	movl	$0xc0000080, %ecx
	rdmsr
	orl		$0x00000101, %eax
	wrmsr

	/* **** DOESN'T GET HERE IN VMWARE , Stack Fault Error! *** */

	/* enable paging */
	movl	%cr0, %eax
	bts		$31, %eax
	movl	%eax, %cr0

	/* at this point we should be in IA-32e mode, let's go all the way to long mode :-) */
	ljmp $0x18, $(boot64 - KERNEL_VMA)
	
GDT64:
	.quad 0x0000000000000000 // 0x00 NULL
	.quad 0x00cf9a000000ffff // 0x08 CODE32
	.quad 0x00cf92000000ffff // 0x10 DATA32
	.quad 0x002098000000ffff // 0x18 CODE64
	.quad 0x000090000000ffff // 0x20 DATA64
	
.align 16
GDT64_PTR:
	.word . - GDT64 - 1
	.quad GDT64 - KERNEL_VMA

	
.align 0x1000
pml4:
	.quad pdp - KERNEL_VMA + 3
	.fill 511,8,0

pdp:
	.quad pd - KERNEL_VMA + 3
	.fill 511,8,0
	

pd:
	/* ident map of first 6 megs */
	.quad 0x0000000000000083
	.quad 0x0000000000200083
	.quad 0x0000000000400083
	.fill 509,8,0
	
.align 0x1000
init_stack:
	.fill 1024,1,0
init_stack_end:
	
.code64
boot64:
	/* Hurray, we made it, a simple clear the screen with blue code */
	mov $0xb8000, %rdi
	mov $0x1f201f201f201f20, %rax
	mov $500, %rcx
	
cls:
	mov %rax, (%rdi)
	add $8, %rdi
	loop cls

	hlt
If anyone has any clues as to what could be going wrong, please let me know.

Re: Difficultly with Long Mode

Posted: Thu Apr 08, 2010 9:56 am
by lemonyii
hi
if you use bochs,use bochsdbg,and you may find which instruction got you a fault.
and i suggest you try set ss to null not 0x10.
i am a fish,but i spent a long time in this.
and if anything you think i can do for you, feel free to tell me.

Cheers!

Re: Difficultly with Long Mode

Posted: Thu Apr 08, 2010 10:26 am
by proxy
The problem is that it works in bochs, but not in qemu and vmware :-(. bochs shows no errors at all.

Re: Difficultly with Long Mode

Posted: Thu Apr 08, 2010 10:48 am
by stlw
proxy wrote:The problem is that it works in bochs, but not in qemu and vmware :-(. bochs shows no errors at all.
Which Bochs version do you use ?
Many issues like missed faults is fixed in Bochs every release.

Which IP you stack failure ? On which instruction you have a problem ?
What is the error code pushed to stack on #SS?

Stanislav

Re: Difficultly with Long Mode

Posted: Thu Apr 08, 2010 11:09 am
by proxy
I am using bochs 2.4.1. Unfortunately vmware doesn't have any good OS debugging tools (that I am aware of) and qemu with gdb attached only gives me so much information. VMWare litterally just says:
*** Virtual machine kernel stack fault (hardware reset) ***
The virtual machine just suffered a stack fault in kernel mode. On a real computer, this would amount to a reset of the processor. It can be caused by an incorrect configuration of the virtual machine, a bug in the operating system, or a problem in the VMware Workstation software. Press OK to reboot virtual machine or Cancel to shut it down.
No useful information for debugging.

Anyway, by doing breadcrumb debugging (inserting instructions which yield a different result and moving them around until i get the error again...) I've narrowed the actual fault down to the "wrmsr" instruction when running in vmware. I don't see why enabled the LME bit would cause a stack fault, but then again it might be vmware being funny and giving me an inaccurate message.

Re: Difficultly with Long Mode

Posted: Thu Apr 08, 2010 1:08 pm
by proxy
So the qemu issue was a matter of running qemu-system-x86_64 (I think that's what solved that one). It now gets a cleared blue screen as well.

However, vmware still gives me a stack fault error during boot. This is very frustrating, as far as I can tell I've followed the intel docs exactly!

Re: Difficultly with Long Mode

Posted: Thu Apr 08, 2010 1:19 pm
by proxy
OK, got vmware working and this is pretty stupid. I didn't have the VM configured for an "Other 64-bit". Once I did that, I had to enable the Intel VT in my BIOS (apparently vmware can't do 64-bit guests without the hardware extensions).

Once all of this was done, vmware works as well. I finally have a 64-bit image to start working with and porting my old project OS!

Re: Difficultly with Long Mode

Posted: Thu Apr 08, 2010 3:48 pm
by quok
Perhaps you should make sure long mode is actually supported by the CPU before attempting to use it. :roll:

Re: Difficultly with Long Mode

Posted: Thu Apr 08, 2010 10:22 pm
by lemonyii
quok wrote:Perhaps you should make sure long mode is actually supported by the CPU before attempting to use it. :roll:
i agree.everything in bochs is totally emulated,but not in vmware.if your computer does not support 64bit mode,forget it.

Re: Difficultly with Long Mode

Posted: Fri Apr 09, 2010 2:10 pm
by proxy
well the I knew the CPU did support it since I am running a 64-bit build of linux. It was simply a configuration issue with my vmware setup. Apparently, it requires VT extensions to be enabled and you must specify that the guest is 64-bit. I always just figured that vmware would emulate the same processor as you run natively automatically, but I guess it is a bit more involved than that.

Clearly now that the code is working, I should add some basic checks that CPU is what I think it is. But now that I have a good test environment, that's relatively low on the totem poll :-).