Trying to make my Kernel an Executable

If you are new to OS Development, plan on spending some time here first before going into the other forums.

Moderator:Moderators

Andyhhp
Moderator
Posts:387
Joined:Tue Oct 23, 2007 10:05 am
Location:127.0.0.1
Contact:
Re: Trying to make my Kernel an Executable

Post by Andyhhp » Tue Sep 01, 2009 8:19 pm

almost certainly a problem with interrupts :P
Image

oib111
Posts:38
Joined:Sat Aug 29, 2009 6:44 am

Re: Trying to make my Kernel an Executable

Post by oib111 » Tue Sep 01, 2009 8:38 pm

What do you mean? Like I didn't disable interrupts and an interrupt occurred so I triple faulted? I don't think it is. I disable interrupts before going into pmode, and I also use the cli instruction in my kernel_entry function just in case. Plus, bochs resets when I try loading the demo's kernel, and bochs resets when I use the demo's bootloader to load my kernel. So I think it's a bad project setting and a problem with my bootloader (I suppose there could be a problem in my code as well)?

User avatar
Mike
Site Admin
Posts:465
Joined:Sat Oct 20, 2007 7:58 pm
Contact:

Re: Trying to make my Kernel an Executable

Post by Mike » Tue Sep 01, 2009 10:37 pm

Hello,

If its a bad project setting the first place I would check is Linker->Command Line and insure /ALIGN:512 is in it. (Its pretty common to omit it.)

Perhaps posting your bochs crash log might help? (Please not the full log.)
Lead Programmer for BrokenThorn Entertainment, Co.
Website: http://www.brokenthorn.com
Email: webmaster@brokenthorn.com

oib111
Posts:38
Joined:Sat Aug 29, 2009 6:44 am

Re: Trying to make my Kernel an Executable

Post by oib111 » Wed Sep 02, 2009 12:01 am

No I have /ALIGN:512 set. Hm...I just redid everything (recopied boot1.bin into MBR, reassembled bloader2.asm and copied BOOT2.SYS into E:\, and copied my built kernel into E:\), and I got:

Code: Select all

Error: OIBKRNL.exe is corrupt.
Apparently it's corrupt? I made a PE Validator program and it says it's a valid PE...so I'm a bit confused. I tested with the bootloader in the demo and it said it was corrupt as well...some conflicting stuff there. Perhaps it's a problem with Visual Studio?

EDIT:

Looking at the PE File Format I realized the e_lfanew is really at the 36th byte not the 60th (no idea where you got the 60th byte, no offense, but for some reason it works for your kernel...). Anyway I changed it to

Code: Select all

mov ebx, [PIMAGE_MODE_BASE+36]
But now triple faults are happening again...so I'm gonna look at Boch's error log.
00014041774i[BIOS ] Booting from 0000:7c00
00015752153i[BIOS ] *** int 15h function AX=2043, BX=0206 not yet supported!
00018001015e[CPU0 ] interrupt(): gate descriptor is not valid sys seg (vector=0x06)
00018001015e[CPU0 ] interrupt(): gate not present
00018001015e[CPU0 ] interrupt(): gate descriptor is not valid sys seg (vector=0x08)
00018001015i[CPU0 ] CPU is in protected mode (active)
00018001015i[CPU0 ] CS.d_b = 32 bit
00018001015i[CPU0 ] SS.d_b = 32 bit
00018001015i[CPU0 ] EFER = 0x00000000
00018001015i[CPU0 ] | RAX=0000000000000004 RBX=000000000000087d
00018001015i[CPU0 ] | RCX=0000000000000004 RDX=0000000000000000
00018001015i[CPU0 ] | RSP=0000000000090008 RBP=0000000000000a62
00018001015i[CPU0 ] | RSI=0000000000100002 RDI=0000000000000819
00018001015i[CPU0 ] | R8=0000000000000000 R9=0000000000000000
00018001015i[CPU0 ] | R10=0000000000000000 R11=0000000000000000
00018001015i[CPU0 ] | R12=0000000000000000 R13=0000000000000000
00018001015i[CPU0 ] | R14=0000000000000000 R15=0000000000000000
00018001015i[CPU0 ] | IOPL=0 id vip vif ac vm RF nt of df if tf sf zf af pf cf
00018001015i[CPU0 ] | SEG selector base limit G D
00018001015i[CPU0 ] | SEG sltr(index|ti|rpl) base limit G D
00018001015i[CPU0 ] | CS:0008( 0001| 0| 0) 00000000 ffffffff 1 1
00018001015i[CPU0 ] | DS:0010( 0002| 0| 0) 00000000 ffffffff 1 1
00018001015i[CPU0 ] | SS:0010( 0002| 0| 0) 00000000 ffffffff 1 1
00018001015i[CPU0 ] | ES:0010( 0002| 0| 0) 00000000 ffffffff 1 1
00018001015i[CPU0 ] | FS:07c0( 0005| 0| 0) 00007c00 0000ffff 0 0
00018001015i[CPU0 ] | GS:07c0( 0005| 0| 0) 00007c00 0000ffff 0 0
00018001015i[CPU0 ] | MSR_FS_BASE:0000000000007c00
00018001015i[CPU0 ] | MSR_GS_BASE:0000000000007c00
00018001015i[CPU0 ] | RIP=000000000000000c (000000000000000c)
00018001015i[CPU0 ] | CR0=0x60000011 CR2=0x0000000000000000
00018001015i[CPU0 ] | CR3=0x00000000 CR4=0x00000000
00018001015i[CPU0 ] 0x000000000000000c>> (invalid) : FFFF
00018001015e[CPU0 ] exception(): 3rd (13) exception with no resolution, shutdown status is 00h, resetting
00018001015i[SYS ] bx_pc_system_c::Reset(HARDWARE) called
00018001015i[CPU0 ] cpu hardware reset
00018001015i[APIC0] allocate APIC id=0 (MMIO enabled) to 0xfee00000
00018001015i[ ] reset of 'unmapped' plugin device by virtual method
00018001015i[ ] reset of 'biosdev' plugin device by virtual method
00018001015i[ ] reset of 'speaker' plugin device by virtual method
00018001015i[ ] reset of 'extfpuirq' plugin device by virtual method
00018001015i[ ] reset of 'gameport' plugin device by virtual method
00018001015i[ ] reset of 'pci_ide' plugin device by virtual method
00018001015i[ ] reset of 'acpi' plugin device by virtual method
00018001015i[ ] reset of 'ioapic' plugin device by virtual method
00018001015i[ ] reset of 'keyboard' plugin device by virtual method
00018001015i[ ] reset of 'harddrv' plugin device by virtual method
00018001015i[ ] reset of 'serial' plugin device by virtual method
00018001015i[ ] reset of 'parallel' plugin device by virtual method

Andyhhp
Moderator
Posts:387
Joined:Tue Oct 23, 2007 10:05 am
Location:127.0.0.1
Contact:

Re: Trying to make my Kernel an Executable

Post by Andyhhp » Wed Sep 02, 2009 11:39 am

00015752153i[BIOS ] *** int 15h function AX=2043, BX=0206 not yet supported!
00018001015e[CPU0 ] interrupt(): gate descriptor is not valid sys seg (vector=0x06)
00018001015e[CPU0 ] interrupt(): gate not present
00018001015e[CPU0 ] interrupt(): gate descriptor is not valid sys seg (vector=0x08)
First of all you want to edit the call to int 15h/2043 call. I cant see any reason to be making it.

As for the exceptions - that means you have not succesfully set up the IDT
Image

oib111
Posts:38
Joined:Sat Aug 29, 2009 6:44 am

Re: Trying to make my Kernel an Executable

Post by oib111 » Wed Sep 02, 2009 5:32 pm

Ok, removed. Also, wouldn't I have to have interrupts enabled for exceptions to be a problem?

EDIT:

I started enabling the A20 gate through the output port and now it's working again...well not working. It doesn't triple fault but it still thinks my kernel is corrupt. I tried switching between:

Code: Select all

mov ebx, [IMAGE_PMODE_BASE+60]

; and

mov ebx, [IMAGE_PMODE_BASE+36]
But both failed.

Hm...can I do EDIT of EDIT lol? Well anyway, it is a valid PE as my PE validator program says. Unfortunately for some reason the bootloader isn't thinking that. But jumping to EXECUTE_KERNEL without validating works no problem...Still wanna know why it doesn't think it's valid though :S

Andyhhp
Moderator
Posts:387
Joined:Tue Oct 23, 2007 10:05 am
Location:127.0.0.1
Contact:

Re: Trying to make my Kernel an Executable

Post by Andyhhp » Wed Sep 02, 2009 6:29 pm

how are you attempting to validate the PE in stage 2?
Image

oib111
Posts:38
Joined:Sat Aug 29, 2009 6:44 am

Re: Trying to make my Kernel an Executable

Post by oib111 » Wed Sep 02, 2009 6:44 pm

Code: Select all

	;-------------------------------;
	;   Validate Kernel is ok    ;
	;-------------------------------;
	
	mov ebx, [IMAGE_PMODE_BASE+60]
	add ebx, IMAGE_PMODE_BASE			; ebx now points to PE header sig (PE00)
	mov esi, ebx
	mov edi, ImageSig
	cmpsw								; compare the sigs
	je EXECUTE_KERNEL					; if equal start executing kernel
	mov ebx, Corrupt					
	call Puts32							; OIBKRNL is corrupted, print error message
	cli
	hlt
Since e_lfanew does seem to be located at the 60th byte. I don't get how that works though? Unsigned shorts are 2-bytes long, and there are 18 USHORTs in IMAGE_DOS_HEADER. That means e_lfanew should be at the 36th byte shouldn't it?

Andyhhp
Moderator
Posts:387
Joined:Tue Oct 23, 2007 10:05 am
Location:127.0.0.1
Contact:

Re: Trying to make my Kernel an Executable

Post by Andyhhp » Wed Sep 02, 2009 6:53 pm

whats your definition of "ImageSig"? I think that might be the problem.

Also, unless I am mistaken, you want to do cmpsd because its a 4 byte value not a 2 byte value :P
Image

oib111
Posts:38
Joined:Sat Aug 29, 2009 6:44 am

Re: Trying to make my Kernel an Executable

Post by oib111 » Wed Sep 02, 2009 7:57 pm

Huh...moved the definition from common.inc (unsure of why I had it in there in the first place) into bloader2.asm and it works now. That's odd but as long as it works right :)? But back to that other problem of:

Code: Select all

------ Build started: Project: Kernel, Configuration: Debug Win32 ------
Compiling...
main.c
.\main.c(74) : error C2224: left of '.base' must have struct/union type
.\main.c(75) : error C2109: subscript requires array or pointer type
.\main.c(75) : error C2109: subscript requires array or pointer type
.\main.c(75) : error C2109: subscript requires array or pointer type
.\main.c(77) : error C2109: subscript requires array or pointer type
.\main.c(79) : error C2109: subscript requires array or pointer type
.\main.c(79) : error C2109: subscript requires array or pointer type
.\main.c(83) : error C2109: subscript requires array or pointer type
.\main.c(83) : error C2109: subscript requires array or pointer type
.\main.c(85) : error C2109: subscript requires array or pointer type
.\main.c(85) : error C2109: subscript requires array or pointer type
.\main.c(85) : error C2109: subscript requires array or pointer type
.\main.c(86) : error C2109: subscript requires array or pointer type
.\main.c(88) : error C2109: subscript requires array or pointer type
.\main.c(88) : error C2109: subscript requires array or pointer type
.\main.c(92) : error C2109: subscript requires array or pointer type
.\main.c(92) : error C2109: subscript requires array or pointer type
.\main.c(95) : error C2224: left of '.base' must have struct/union type
Build log was saved at "file://c:\Documents and Settings\OIB\My Documents\Visual Studio 2008\Projects\Core\Core\Debug\BuildLog.htm"
Kernel - 18 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Anybody know what's going on there?

Andyhhp
Moderator
Posts:387
Joined:Tue Oct 23, 2007 10:05 am
Location:127.0.0.1
Contact:

Re: Trying to make my Kernel an Executable

Post by Andyhhp » Wed Sep 02, 2009 8:08 pm

error C2109: http://msdn.microsoft.com/en-us/library ... 71%29.aspx

it means that "gdt" isnt a pointer or an array

error C2224: means gp is not a structure
Image

oib111
Posts:38
Joined:Sat Aug 29, 2009 6:44 am

Re: Trying to make my Kernel an Executable

Post by oib111 » Wed Sep 02, 2009 8:25 pm

gdt is an array of structures, and gp is a structure. Here are the definitions of all of them:

Code: Select all

//in gdt.c

// pointer to gdt (needed for lgdt instruction)
struct gdt_ptr
{
    unsigned short limit;
    unsigned int base;
};

// define three entries (NULL entry, code descriptor, data descriptor)
struct gdt_entry gdt[3];
struct gdt_ptr gp;

//in idt.c

struct idt_ptr
{
    unsigned short limit;
    unsigned int base;
};

// make an 256 idt entries
struct idt_entry idt[256];
struct idt_ptr idtp;

Andyhhp
Moderator
Posts:387
Joined:Tue Oct 23, 2007 10:05 am
Location:127.0.0.1
Contact:

Re: Trying to make my Kernel an Executable

Post by Andyhhp » Wed Sep 02, 2009 8:46 pm

that would be your problem.

the struct declarations need to go in header files, while the instanciations go in the source files.

if not, then the other source files (everything that isnt gdt.c) have no idea what type of variable gdt or gp are. In this case, C just assums they are ints, hense your errors.
Image

oib111
Posts:38
Joined:Sat Aug 29, 2009 6:44 am

Re: Trying to make my Kernel an Executable

Post by oib111 » Wed Sep 02, 2009 8:59 pm

Ah...is there anyway to explicitly say what they are or do I have to make some header files?

EDIT:

That didn't do anything...added structure definitions to gdt.h and idt.h but I'm still getting the same errors.

User avatar
Mike
Site Admin
Posts:465
Joined:Sat Oct 20, 2007 7:58 pm
Contact:

Re: Trying to make my Kernel an Executable

Post by Mike » Wed Sep 02, 2009 10:54 pm

Hello,

You can externally define the variables in the header files that they are in (extern keyword) and define them in the source files. This will allow other files to be able to reference those variables defined in the source file.

With regards to the errors - Please post, in your main.c file, the function that the errors are occurring in.
Lead Programmer for BrokenThorn Entertainment, Co.
Website: http://www.brokenthorn.com
Email: webmaster@brokenthorn.com

Post Reply