Any C crashes kernel

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.
thewonderidiot
Posts: 19
Joined: Sat May 05, 2007 1:27 pm

Post by thewonderidiot »

I *did* have a 64-bit system set up. 64-bit Linux hates my computer. Every error message I got, for whatever reason, had a u-umlaut in it. So now I'm back to x86. Same problem, though.

Contents of kernel.map:

Code: Select all

Memory Configuration

Name             Origin             Length             Attributes
*default*        0x00000000         0xffffffff

Linker script and memory map

                0x00100000                phys = 0x100000

.text           0x00100000       0x2c
 *(.text)
 .text          0x00100000       0x1c start.o
                0x0010000f                start
 .text          0x0010001c       0x10 kernel.o
                0x0010001c                main

.data           0x0010002c        0x0
 *(.data)
 .data          0x0010002c        0x0 start.o
 .data          0x0010002c        0x0 kernel.o

.bss            0x0010002c     0x2000
 *(.bss)
 .bss           0x0010002c     0x2000 start.o
 .bss           0x0010202c        0x0 kernel.o
LOAD start.o
LOAD kernel.o
OUTPUT(kernel.bin binary)

.comment        0x00000000       0x2e
 .comment       0x00000000       0x2e kernel.o

.note.GNU-stack
                0x00000000        0x0
 .note.GNU-stack
                0x00000000        0x0 kernel.o
And compiling without any std library files didn't change anything.
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Post by Brynet-Inc »

So you were previously linking with your OS's C library? For what possible reason? :roll:
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Post by neon »

I noticed ES was not set in your first post. ES should = DS.

Are you using "main" or "_main"? I see "main" but not the correct
"_main" in your map file.

Have you tried using a debugger? _main is loaded at 0x0010001c,
so insure your jmp _main is jumping to the correct address. If it
isnt, it might be the way you are building the final image.
thewonderidiot
Posts: 19
Joined: Sat May 05, 2007 1:27 pm

Post by thewonderidiot »

Okay, fixed the ES problem. I originally had "_main" in my files, but that produced linker errors, so I switched it to "main", which linked without errors. I noticed you said "jmp main". Should I be jumping to my main method rather than calling it? I haven't tried a debugger yet.

And no, I wasn't linking any standard libraries.
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Post by neon »

Okay, fixed the ES problem. I originally had "_main" in my files, but that produced linker errors, so I switched it to "main", which linked without errors. I noticed you said "jmp main". Should I be jumping to my main method rather than calling it? I haven't tried a debugger yet.
So, are you using _main?

Also: You said you set the stack up before jumping to start. call
pushes EIP into the stack before jumping to _main. I recommend
resetting the stack inside of start before call _main.
thewonderidiot
Posts: 19
Joined: Sat May 05, 2007 1:27 pm

Post by thewonderidiot »

No, I'm not using _main. I'm using main. Start already resets the stack, the very command before the call to main.
User avatar
B.E
Member
Member
Posts: 275
Joined: Sat Oct 21, 2006 5:29 pm
Location: Brisbane Australia
Contact:

Post by B.E »

I'd be more worred about these lines in your bochsout file. That's your problem.

Code: Select all

00024645358i[CPU  ] WARNING: Encountered an unknown instruction (signalling illegal instruction) 
00024645358e[CPU  ] interrupt(): gate descriptor is not valid sys seg
00024645358e[CPU  ] interrupt(): gate descriptor is not valid sys seg 
...
00024645358i[CPU  ] |  ES:ffff( 0000| 0|  0) 000ffff0 0000ffff 0 0 
...
00024645358i[CPU  ] >> (invalid)  : FFFF
Sorry, my mistake, this has already been fixed
Last edited by B.E on Sun May 06, 2007 4:03 pm, edited 1 time in total.
Image
Microsoft: "let everyone run after us. We'll just INNOV~1"
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Post by neon »

Take a quote from t0xin:

Code: Select all

[BITS 32] 
[global start] 
[extern _main] 

start: 
call _main 
cli 
hlt 
And declare main as extern (in a header file):

Code: Select all

extern void main ();
#include the file where main() is defined.

GCC (as most C compilies) append an underscore to all global
symbols.

You wont get an error when attempting to call main. However, main
doesnt exist, hence the triple fault.

You *need* to use _main.

If you still do recieve the error, can you please post the error?

B.E posted what the problem was. Your code is jumping to some
random location in memory, executing an invalid instruction.
This means either a jmp or call is at fault. (or a ret/iret, of course)

I suspect it is you usage of main rather then the defined _main.
I could be wrong though :)
thewonderidiot
Posts: 19
Joined: Sat May 05, 2007 1:27 pm

Post by thewonderidiot »

Code: Select all

start.o:start.o:(.text+0x16): undefined reference to `_main'
LD gives me that when I use _main.
jnc100
Member
Member
Posts: 775
Joined: Mon Apr 09, 2007 12:10 pm
Location: London, UK
Contact:

Post by jnc100 »

If you post all your code (bootloader, start.asm and kernel.c), probably as attachments as I guess the bootloader is quite large, it'll make sorting out your problem easier, as we won't have to rely on asking you for various small snippets.

Regards,
John.
thewonderidiot
Posts: 19
Joined: Sat May 05, 2007 1:27 pm

Post by thewonderidiot »

B.E wrote:I'd be more worred about these lines in your bochsout file. That's your problem.

Code: Select all

00024645358i[CPU  ] WARNING: Encountered an unknown instruction (signalling illegal instruction) 
00024645358e[CPU  ] interrupt(): gate descriptor is not valid sys seg
00024645358e[CPU  ] interrupt(): gate descriptor is not valid sys seg 
...
00024645358i[CPU  ] |  ES:ffff( 0000| 0|  0) 000ffff0 0000ffff 0 0 
...
00024645358i[CPU  ] >> (invalid)  : FFFF
Sorry, my mistake, this has already been fixed
No, it hasn't been fixed. That's still where the restart occurs. Here comes the flood of code:

Code: Select all

[BITS	16]
[ORG	0x7c00]

ea20:
	cli
	call	a20c
	mov	al,0xd1
	out	0x64,al
	call	a20c
	mov	al,0xdf
	out	0x60,al
	call	a20c
	mov	cx,0x5
	.wait:
		nop
		nop
		loop .wait
	mov	al,0xd0
	out	0x64,al
	call	a20f
	in	al,0x60
	bt	ax,2
	jnc	lkrnl
	hlt
lkrnl:
	xor	ax,ax
	int	0x13
	or	ah,ah
	jnz	lkrnl
	mov	ax,0xffff
	mov	es,ax
	mov	bx,0x10
	mov	ah,0x2
	mov	al,0x2
	mov	cx,0x2
	mov	dh,0
	int	0x13
	or	ah,ah
	jnz	lkrnl
egdt:
	lgdt	[descriptor]
	mov	eax,cr0
	or	eax,1
	mov	cr0,eax
	jmp	0x8:eprot
[BITS	32]
eprot:
	mov	ax,0x10
	mov	ss,ax
	mov	ds,ax
	mov	es,ax
	mov	esp,0x9d000
skrnl:
	jmp	0x8:0x100000
	cli
	hlt	
gdt:
gdt_null:
	dd	0
	dd	0
gdt_cs:
	dw	0xffff
	dw	0
	db	0
	db	0x9a
	db	0xcf
	db	0
gdt_ds:
	dw	0xffff
	dw	0
	db	0
	db	0x92
	db	0xcf
	db	0
gdte:
descriptor:
	dw	gdte-gdt-1
	dd	gdt

a20c:
	xor	ax,ax
	in	al,0x64
	bt	ax,1
	jc	a20c
	ret

a20f:
	xor	ax,ax
	in	al,0x64
	bt	ax,0
	jnc	a20f
	ret	

times 510-($-$$) db 0
dw	0xaa55
...And, here's start.asm:

Code: Select all

[BITS	32]
[global start]
[extern	_main]
start:
	sti
	mov	esp, sstack
	call	_main
	cli
	hlt

SECTION .bss
	resb 8192
sstack:
And, finally, the minimal C kernel:

Code: Select all

extern void main()
{
	while(1);
}
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Post by neon »

I notice you are using call without setting up the stack in
your bootloader. Use jmp instead.
thewonderidiot
Posts: 19
Joined: Sat May 05, 2007 1:27 pm

Post by thewonderidiot »

Fixed that. It didn't solve the problem, though.
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Post by neon »

Code: Select all

[BITS   32] 
[global start] 
[extern   _main] 
start: 
   sti 
   mov   esp, sstack 
   call   _main 
   cli 
   hlt 

SECTION .bss 
   resb 8192 
sstack:
Get rid of the sti instruction. Because you dont have an IDT set up,
any interrupt will cause a triple fault in pmode.

After taking the above out, your code ran fine for me. (I used
ELF format for my kernel though)

This is with the code posted here.
Last edited by neon on Sun May 06, 2007 6:00 pm, edited 1 time in total.
thewonderidiot
Posts: 19
Joined: Sat May 05, 2007 1:27 pm

Post by thewonderidiot »

Will you please post exactly how you compiled/linked all of it? Even when I removed that line the emulator still restarted.
Post Reply