Page 1 of 2

Computer crashes once bootloader tries to jump to kernel cod

Posted: Mon Apr 18, 2005 11:20 pm
by kofrad
I am following a tutorial at http://www.osdever.net/tutorials/brunmar/ . The tutorial is designed to leave you with a simple bootloader that will read the kernel code off of the disk and then start executing kernel code. I am at the end of the tutorial, I have all my source done and set up and everything. However, the code dosent work. I am able to read the kernel off of the boot disk, enter protected mode and do everything else just fine, but the computer crashes ones I try to jump into kernel code. The only thing I can think of is that the address I am using in the jmp opcode may be wrong. I dont know what it should be, as the author has several discrepancies in the tutorial with segment addresses. Here is the output from bochs where it crashes:

Code: Select all

(0).[7628740] [0x00001fbc] 0008:00001fbc (unk. ctxt): add byte ptr ds:[eax], al ; 0000
<!   ... last message repeated 25 times ... />
(0).[7628766] [0x00001ff0] 0008:00001ff0 (unk. ctxt): add byte ptr ds:[eax], al ; 0000
(0).[7628767] [0x00001ff2] 0008:00001ff2 (unk. ctxt): add byte ptr ds:[eax], al ; 0000
(0).[7628768] [0x00001ff4] 0008:00001ff4 (unk. ctxt): add byte ptr ds:[eax], al ; 0000
(0).[7628769] [0x00001ff6] 0008:00001ff6 (unk. ctxt): add byte ptr ds:[eax], al ; 0000
(0).[7628770] [0x00001ff8] 0008:00001ff8 (unk. ctxt): add byte ptr ds:[eax], al ; 0000
(0).[7628771] [0x00001ffa] 0008:00001ffa (unk. ctxt): add byte ptr ds:[eax], al ; 0000
(0).[7628772] [0x00001ffc] 0008:00001ffc (unk. ctxt): add byte ptr ds:[eax], al ; 0000
(0).[7628773] [0x00001ffe] 0008:00001ffe (unk. ctxt): add byte ptr ds:[eax], al ; 0000
(0).[7628774] [0x00002000] 0008:00002000 (unk. ctxt): push ebp                  ; 55
(0).[7628775] [0x00002001] 0008:00002001 (unk. ctxt): mov ebp, esp              ; 89e5
(0).[7628776] [0x00002003] 0008:00002003 (unk. ctxt): sub esp, 0x8              ; 83ec08
(0).[7628777] [0x00002006] 0008:00002006 (unk. ctxt): and esp, 0xfffffff0       ; 83e4f0
(0).[7628778] [0x00002009] 0008:00002009 (unk. ctxt): mov eax, 0x0              ; b800000000
(0).[7628779] [0x0000200e] 0008:0000200e (unk. ctxt): sub esp, eax              ; 29c4
(0).[7628780] [0x00002010] 0008:00002010 (unk. ctxt): call 0x2011               ; e8fcffffff
(0).[7628781] [0x00002011] 0008:00002011 (unk. ctxt): cld                       ; fc
(0).[7628782] [0x00002012] 0008:00002012 (unk. ctxt): (invalid)                 ; ffff

I dont have the entire output, as my terminal cut it off after a while. I did only include here the bottom, all that I have of the top, is basically the same, adding bytes everywhere. I dont get this at all. If anyone could help me out, I would be very thankful. If you need me too, I will post a tarball of my sources. Thanks.

Re:Computer crashes once bootloader tries to jump to kernel

Posted: Tue Apr 19, 2005 12:28 am
by distantvoices
[chicken]*poooook pok pok pok pooooook* [/chicken]

chances are that you send your cpu to nirvana with that jump.

Check, where you load the kernel image to and check what's the entry point looks like. If it isn't on the correct address, your cpu gonna choke on that.

Best for the beginning is to have flat descriptors for kernel and user space in the Global descriptor table. Keep the Interrupt descriptor table register empty for the beginning, take care not to cause exceptions and shut interrupts away. Only you and your cpu. *gg*

If still in doubt, don't bother to attach your code to a post and let us have a look.

Have a nice day. Today I'm gonna have either a good ride on my bicicle or my feet bring me round a good amount of laps in some park nearby.

Re:Computer crashes once bootloader tries to jump to kernel

Posted: Tue Apr 19, 2005 1:48 am
by Candy
1. The jump is wrong (you jump in the middle of a bunch of 0's, which isn't the final problem this time but is kind of bad and not good)
2. The kernel is not linked (calling -4 is usually a sign that no linker adjusted it for you to the right place).

Re:Computer crashes once bootloader tries to jump to kernel

Posted: Tue Apr 19, 2005 4:23 am
by kofrad
I'm not sure what the kernel entry point or anything is, so I am attaching a zip with my sources. Sorry if I am troubling anyone, its just this is really new to me still and its hard to find good documentation on certain aspects of this. Thank you.

Re:Computer crashes once bootloader tries to jump to kernel

Posted: Tue Apr 19, 2005 4:29 am
by Pype.Clicker
i'd guess you have misused the program entry point and jump to offset 0 in the image while the program is somewhere else.

Re:Computer crashes once bootloader tries to jump to kernel

Posted: Tue Apr 19, 2005 6:26 pm
by kofrad
Pype.Clicker wrote: i'd guess you have misused the program entry point and jump to offset 0 in the image while the program is somewhere else.
Would you happen to know how I could work this problem out?

Re:Computer crashes once bootloader tries to jump to kernel

Posted: Wed Apr 20, 2005 12:07 am
by kofrad
I just thought of something, could this problem be because of the A20 gate? I dont really know what the A20 gate does, but isnt it required to access memory over the 1mb limit? If this is the case, my kernel is loaded into memory at 1mb, could that cause these problems? For some reason though, bochs will seem to crash in a different spot sometimes. It has crashed right when I start to set parameters for the floppy read, and other times when i try to jmp into the kernel.

Re:Computer crashes once bootloader tries to jump to kernel

Posted: Wed Apr 20, 2005 12:20 am
by AR
The A20 is what allows you to actually use memory above 1MB, if it's off whenever you read/write at 0x100000 it is actually being put at 0x0 (which is where the Interrupt Vector Table is).

Re:Computer crashes once bootloader tries to jump to kernel

Posted: Wed Apr 20, 2005 12:33 am
by kofrad
Might I be able to assume that the kernel is overwriting my bootloader in memory by my bochs output? I tried some things, and in the code in different spots, I have text output to screen. I was able to see all three text outputs on screen, but bochs crashed on the code that outputs the second string. The second text output is right before you switch over to protected mode too. It looks to me that the bootloader is sort of killing itself by reading my kernel from the floppy and copying it over its own code or something. How do I know for sure that my kernel code was read into the proper location in memory.

Re:Computer crashes once bootloader tries to jump to kernel

Posted: Wed Apr 20, 2005 2:15 am
by Pype.Clicker
kofrad wrote:
Pype.Clicker wrote: i'd guess you have misused the program entry point and jump to offset 0 in the image while the program is somewhere else.
Would you happen to know how I could work this problem out?
Yes. Write an assembler 'stub' that you'll force to be ahead of your kernel binaries (e.g. by the mean of a proper "ld" statement, or (better, imho) by a linker script. See BareBones in the FAQ for additionnal info.

Oh, and just in case you might want to know, you're *not* loading your kernel over 1MB at all. You load it at 4K. That means if your kernel.bin grows enough, it'll eventually erase the bootloader. I'd suggest you load it at es=1000, bx=0000 instead.

Re:Computer crashes once bootloader tries to jump to kernel

Posted: Wed Apr 20, 2005 8:54 am
by kofrad
Pype.Clicker wrote:
kofrad wrote:
Pype.Clicker wrote: i'd guess you have misused the program entry point and jump to offset 0 in the image while the program is somewhere else.
Would you happen to know how I could work this problem out?
Yes. Write an assembler 'stub' that you'll force to be ahead of your kernel binaries (e.g. by the mean of a proper "ld" statement, or (better, imho) by a linker script. See BareBones in the FAQ for additionnal info.

Oh, and just in case you might want to know, you're *not* loading your kernel over 1MB at all. You load it at 4K. That means if your kernel.bin grows enough, it'll eventually erase the bootloader. I'd suggest you load it at es=1000, bx=0000 instead.
Sorry to keep everyone bothered with this, but I dont really understand whats on that BareBones page. I dont know how to use it either, as the tutorial I was following uses its own bootloader. So, when I load in the kernel at 4K, that will leave me with 1020K space for the kernel before it wraps around?

Re:Computer crashes once bootloader tries to jump to kernel

Posted: Wed Apr 20, 2005 9:09 am
by Pype.Clicker
I pointed out BareBones because of the linker script and the commands. If you cannot get your bootloader "smart enough" to find the entry point in your kernel image, you still have the option of tweaking the image generation so that it starts with code (preferably asm) that will jump to the entry point.

The simplest thing you could have is

Code: Select all

; - - 8< - - stub.asm - - 8< - - 
section asmstub
call _start_C_kernel_initialization
; hang the computer if _start_C_kernel_initialization returns
.here:
jmp .here
Assemble that and use a linker script to have it at the very start of your kernel. E.g.

Code: Select all

SECTIONS
{
    . = 0x0010000; 
    // kernel will be loaded at 64K.
    
    .text :
    {
        stub.o(asmstub);
        *(.text)
    }

    .rodata ALIGN (0x1000) :
    {
        *(.rodata)
    }

    .data ALIGN (0x1000) :
    {
        *(.data)
    }

    .bss :
    {
        _sbss = .;
        *(COMMON)
        *(.bss)
        _ebss = .;
    }
}
keep in mind that you have to create a binary file, rather than an ELF (since your bootloader is not elf-aware).

Re:Computer crashes once bootloader tries to jump to kernel

Posted: Wed Apr 20, 2005 12:06 pm
by kofrad
Pype.Clicker wrote: I pointed out BareBones because of the linker script and the commands. If you cannot get your bootloader "smart enough" to find the entry point in your kernel image, you still have the option of tweaking the image generation so that it starts with code (preferably asm) that will jump to the entry point.
<code/>

Assemble that and use a linker script to have it at the very start of your kernel. E.g.
<morecode/>

keep in mind that you have to create a binary file, rather than an ELF (since your bootloader is not elf-aware).


I think I'm getting somewhere now. Bochs still crashes, it seems like I may not have linked properly. If I look at the trace output from bochs, it appears that the stub was sucessfully jmp'ed into, and it looks like it tries to either run kmain() and crashes then. Here is the output from bochs after it jmp's to the stub

Code: Select all

(0).[7621597] [0x00007c6c] 0008:00007c6c (unk. ctxt): jmp 0008:00001000         ; ea001000000800
(0).[7621598] [0x00001000] 0008:00001000 (unk. ctxt): call 0x1001               ; e8fcffffff
(0).[7621599] [0x00001001] 0008:00001001 (unk. ctxt): cld                       ; fc
(0).[7621600] [0x00001002] 0008:00001002 (unk. ctxt): (invalid)                 ; ffff
I also think it could be a link problem, but have no idea how to fix it. If it is a link problem, I am sure it is in the strange way I get everything compiled and linked. This was the only setup I could get to work with everything properly.

Code: Select all

sailos.img: kernel.bin bootsect.bin
        cat bootsect.bin kernel.bin > sailos.img

bootsect.bin: bootsect.asm
        nasm -f bin bootsect.asm -o bootsect.bin

kernel.bin: kernel.o
        objcopy -R .note -R .comment -S -O binary kernel.o kernel.bin

kernel.o: stub.o main.o video.o ports.o
        ld -e kmain -T link.ld -o kernel.o stub.o main.o video.o ports.o
        ld -i -e kmain -T link.ld -o kernel.o stub.o main.o video.o ports.o

stub.o: stub.asm
        nasm -f elf stub.asm -o stub.o
#       objcopy -R .note -R .comment -S -O binary stub.o stub.bin

main.o: main.c
        gcc -I./include -ffreestanding -c main.c -o main.o

video.o: video.c
        gcc -I./include -c video.c -o video.o

ports.o: ports.c
        gcc -I./include -c ports.c -o ports.o

clean:
        rm *.o *.bin *.img
Once again, thank you much for helping me get this far :)

Re:Computer crashes once bootloader tries to jump to kernel

Posted: Wed Apr 20, 2005 9:47 pm
by kofrad
I think I've pretty much solved the problem now, after learning how to create a map file using the linker, I realised that my link.ld had a typo in it :-[ I had told it to align the kernel at 0x10000 instead of 0x1000. I ended up with some troubles with the kernel stuck in another endless jmp'ing spree, but I have traced it down to something gone wary in my print() function. I'm going to find the tutorial again and double check my code for that function. Thanks a bunch to Pype.Clicker for helping me get this far. Your advice has helped me a lot and helped me to find new ways of debugging my kernel. Thanks!

Re:Computer crashes once bootloader tries to jump to kernel

Posted: Thu Apr 21, 2005 2:35 am
by Pype.Clicker
<tears>
once again, please, load your kernel at 64K rather than 4K.
</tears>

Really, your bootloader stands at 0x7C00 and you want to load your kernel at 0x1000. Tell me, what is likely to occur when your kernel will grow larger than 0x6C00 bytes (that is still less than 32K. You'll hit that earlier than you could think) ?