Kernel Crashes at Function Call

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
User avatar
AaronMiller
Member
Member
Posts: 81
Joined: Thu Mar 06, 2008 1:26 pm
Location: Roseville, California (USA)
Contact:

Kernel Crashes at Function Call

Post by AaronMiller »

I'm having a very strange problem with the kernel. :-S I was just wondering if anyone would mind taking a few minutes of their time to help me out, to figure out why it's occuring and how to fix it?

Here's the main happenings of the problem.
QEMU crashes when calling "KeHalt" at first line of KeInitGDT code, QEMU says:

Code: Select all

Could not open '\\.\kqemu' - QEMU acceleration layer not activated
qemu: fatal: Trying to execute code outside RAM or ROM at 0xb0000092

EAX=00000001 EBX=00009000 ECX=00000001 EDX=00000002
ESI=ffff7cd1 EDI=0004ffde EBP=630001bf ESP=0001bfbd
EIP=b0000092 EFL=00000002 [-------] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =0010 00000000 ffffffff 00cf9300
CS =0008 00000000 ffffffff 00cf9a00
SS =0010 00000000 ffffffff 00cf9300
DS =0010 00000000 ffffffff 00cf9300
FS =0010 00000000 ffffffff 00cf9300
GS =0010 00000000 ffffffff 00cf9300
LDT=0000 00000000 0000ffff 00008000
TR =0000 00000000 0000ffff 00008000
GDT=     00007cb6 00007ccd
IDT=     00000000 000003ff
CR0=60000011 CR2=00000000 CR3=00000000 CR4=00000000
CCS=00000000 CCD=0001bfa5 CCO=INCL
FCW=037f FSW=0000 [ST=0] FTW=00 MXCSR=00001f80
FPR0=0000000000000000 0000 FPR1=0000000000000000 0000
FPR2=0000000000000000 0000 FPR3=0000000000000000 0000
FPR4=0000000000000000 0000 FPR5=0000000000000000 0000
FPR6=0000000000000000 0000 FPR7=0000000000000000 0000
XMM00=00000000000000000000000000000000 XMM01=00000000000000000000000000000000
XMM02=00000000000000000000000000000000 XMM03=00000000000000000000000000000000
XMM04=00000000000000000000000000000000 XMM05=00000000000000000000000000000000
XMM06=00000000000000000000000000000000 XMM07=00000000000000000000000000000000

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
Function is:

Code: Select all

// Initialize the GDT function set
NOEXPORT void KRNLAPI KeInitGDT(void)
{
	// Setup the GDT pointer and limit
	KeHalt(); // Halt the kernel to try and trap a bug
	g_pGdt.limit	= (sizeof(GDT_ENTRY) * 3) - 1;
	g_pGdt.base		= (u32)&g_gdt;
	// Setup the null descriptor
	GdtSetGate(0, 0, 0, 0, 0);
	// Setup the code segment (Base = 0, Limit = 4GB, Granularity = 4KB, 32-bits)
	GdtSetGate(1, 0, 0xffffffff, 0x9a, 0xcf);
	// Setup the data segment (Base = 0, Limit = 4GB, Granularity = 4KB, 32-bits)
	GdtSetGate(2, 0, 0xffffffff, 0x92, 0xcf);
	// Setup the GDT
	GdtFlush();
}
Moving KeHalt() to RIGHT before calling KeInitGDT fixes the problem?! :-S

I've got no idea why this is happening: I've tried the __cdecl and __stdcall calling conventions and both yield the same result, it would appear that it's trying to jump to an area of memory that doesn't exist, yet I can't see *why* it would... I think the above is sufficient enough information to query an answer, but in case it's not I'll upload my entire build environment (portions subject to the GPL). Credit goes out to all the various tutorials on osdever.net and osdev.org :)

Btw: This is a port of my old C kernel, the boot loader is the same, some ASM differences in the code.

Cheers,

-naota

Download the Development Environment + Source Code Here
Last edited by AaronMiller on Tue Jun 24, 2008 6:35 pm, edited 1 time in total.
midir
Member
Member
Posts: 46
Joined: Fri Jun 13, 2008 4:09 pm

Re: Kernel Crashes at Function Call

Post by midir »

It sounds like a problem with your linker not putting the KeHalt function where it's supposed to be. 0xb00000092 (the value in EIP) is nearly three gigabytes into physical memory unless you set up paging or segmentation to map it somewhere else.

If you're compiling with GCC passing the -Map file option onto LD will save a map of where all parts of the kernel are in memory to a file. If you're not using a separate LD command line I think passing -Xlinker -M onto GCC's command line works too. You can use a linker script to remap the kernel to the right place. I've been using the following very basic linker script with LD to put the kernel at 0x200000 (2 megabytes):

Code: Select all

SECTIONS
{
	. = 0x00200000;
	
	.text : {
		*(.text)
		*(.rdata)
		*(.rodata)
		*(.data)
	}
	
	.bss : {
		*(.bss)
	}
}
User avatar
AaronMiller
Member
Member
Posts: 81
Joined: Thu Mar 06, 2008 1:26 pm
Location: Roseville, California (USA)
Contact:

Re: Kernel Crashes at Function Call

Post by AaronMiller »

Oh dear, it would appear I forgot to upload the kernel assembly dump, I added comments in it which explained where every function was. :/

Uploaded to this post. :)
Attachments
kernel assembly dump.zip
(16.65 KiB) Downloaded 84 times
Post Reply