Page 2 of 2

Re: Triple fault upon call to extern function

Posted: Thu Apr 15, 2010 10:22 am
by Gigasoft
Well, this code certainly isn't loading anything anywhere. It just resets the disk system.

Re: Triple fault upon call to extern function

Posted: Thu Apr 15, 2010 10:33 am
by gravaera
Benjamin1996 wrote:Hello everyone!
My problem is that when I try to execute an extern function call from my bootloader, my CPU triple faults and resets.
This is my 32 bit "protectedMode" function, located in bootloader.asm:

Code: Select all

[bits 32]
[extern kernel]
protectedMode:
    mov ax, 0x0010
    mov ds, ax ;Point ds to the code descriptor
    mov ss, ax ;Point ss to the code descriptor
    mov esp, 0x90000 ;Set the stack base to 0x90000
    call kernel
And this is kernel.asm:

Code: Select all

[bits 32]
[global kernel]
kernel:
    mov edi, 0xb8000 ;Point edi to the color video memory
    mov byte [edi + 800], 'K'
    mov byte [edi + 801], 0x0004 ;Display a red 'K' on a black background.
    jmp $  ;Hang the stub-kernel.
And this is my build.bat file:

Code: Select all

@echo off
nasm -f elf bootloader.asm -o bootloader.elf
nasm -f elf kernel.asm -o kernel.elf
ld -e kernel -o kernel.o kernel.elf
ld -Ttext 0x7c00 -o os.o bootloader.elf kernel.elf
objcopy -R .note -R .comment -S -O binary os.o os.bin
echo os.bin created!
rawwrite.exe
For testing, I'm using Bochs on Windows, and this is the message printed in bochsout.txt upon triple fault:

Code: Select all

00025746133i[CPU0 ] 0x000000000000fda8>> int1  : F1
00025746133e[CPU0 ] exception(): 3rd (13) exception with no resolution, shutdown status is 00h, resetting
I would love some help :).

Best regards,
-Benjamin.
Hi:

Apparently you have no idea how to debug code. Allow me to help you. The first thing you need to know is the exact point at which execution faults. To do that, you should place infinite loops at strategic points in the code. The idea is: "If running the code to its natural conclusion causes a triple fault, then running to code to a partial extent using infinite loops as delimiters should enable me to locate the faulting instruction."

You have made the assumption that the call to the external function is what causes the triple fault. This assumption could be wrong. And is likely to be, Regardless, when you trace the fault to its primitive, the next step is to find out why the instruction fails.

You have the benefit of being able to use a debugger, so you can run an "objdump -x mykernel.bin | grep '^0x' | sort" to lookup the address that bochs tells you the instruction fault was at, and see which function, and exactly which instruction therein causes the fault. Look at that.

There can be any number of reasons for an instruction to fail. One of the most common is your page table mappings, assuming you're using paging. If not, and you get exception 13, which it seems you are getting, then the problem has to do with access permissions in your GDT. Fix your GDT.

But by no means is the information you provided complete. Nobody even knows whether or not you even know what's faulting your own code. So we're blind, and you're stunted. Technically, Bochs can't help you with this problem much more than the Intel Manuals can. So read them. Most of the time you don't need the Bochs debugger, GDB or any of that fancy stuff. Most of the time you can more than manage with printf(), for(;;){} and objdump.

Know your code. Know your architecture. Know your stuff.

--Good luck,
gravaera

Re: Triple fault upon call to extern function

Posted: Thu Apr 15, 2010 11:24 am
by Benjamin1996
Alright thanks gravaera.
By using your infinite loop technique I found out that the fault does probably happen doing the jmp instruction (as it could execute the "mov esp, 0x90000" instruction, but not the "jmp 0x0008:0x7e00" or the first instruction in the "kernel" function).
Also, I don't have paging enabled, so according to what you said it can only be something with the access properties of my GDT. - But I don't understand that.
This is the access byte in my code descriptor:

Code: Select all

10011010
This is my translation of this binary number:
Bit 0: This is the access bit for virtual memory (I'm not using virtual memory, so this is 0).
Bit 1: This is the readable/writable bit (it's 1 so that I can read and execute data as code).
Bit 2: This is the expansion direction bit (I don't know what this is for, so I disabled it).
Bit 3: This is the code or data descriptor bit (1 means code descriptor, right?).
Bit 4: Pretty much the same as above, right? (hence, 1).
Bit 5 - 6: These are the privilege level bits (they're both 0 as an OS is a ring 0 program).
Bit 7: An indicator that this segment is in memory (I guess it is, so 1).
If I interpreted this right, am I violating any of these restrictions?

Re: Triple fault upon call to extern function

Posted: Thu Apr 15, 2010 1:44 pm
by Gigasoft
As I said, your code doesn't load anything into 0x7e00, so that's why it doesn't work.

Re: Triple fault upon call to extern function

Posted: Thu Apr 15, 2010 11:32 pm
by Benjamin1996
Gigasoft wrote:As I said, your code doesn't load anything into 0x7e00, so that's why it doesn't work.
That's kind off odd, because I remember once when I didn't want it to execute any sub-kernel first, but just jump directly into the C code. Code almost identical to this one worked then. The only difference was that I placed the compiled and linked C sources at 0x1000 with the -Ttext 0x1000 command in LD...

Re: Triple fault upon call to extern function

Posted: Fri Apr 16, 2010 12:07 am
by neon
Hello,

There is nothing in your code that loads anything so I am uncertain whats odd about it. Also, I do personally recommend against going into protected mode in your bootstrap (Stage 1) code do to its size limitation.

Re: Triple fault upon call to extern function

Posted: Fri Apr 16, 2010 5:40 am
by Benjamin1996
neon wrote:Hello,

There is nothing in your code that loads anything so I am uncertain whats odd about it. Also, I do personally recommend against going into protected mode in your bootstrap (Stage 1) code do to its size limitation.
Looking at your signature I assume that you're the one that wrote the OS Development Series (at http://www.brokenthorn.com).
I remember trying your tutorial once, but once I got to the part loading the KRNLDR.sys file, it failed with a "MISSING OR CURRUPT KRNLDR. Press Any Key to Reboot" message, when I was running it in Bochs. But again, it worked in Microsoft Virtual PC and on real hardware, except it didn't execute the stage 2 file..
Even though KRNLDR.sys was on the floppy disk, AND in Bochs' default directory, it failed.

By the way, if my assumption that you're the one who wrote the OS Development Series is correct, then great job, very helpful series of tutorials :).

Re: Triple fault upon call to extern function

Posted: Fri Apr 16, 2010 10:20 am
by neon
Hello,

We are getting a little off topic now. If it didnt execute the "Stage 2" program in Virtual PC and real hardware, then, technically, it didnt work even though it didnt crash :) Bochs differs from Virtual PC and real hardware in different ways.

(Also I appreciate your compliment on the series, I am indeed the author of it.)

Re: Triple fault upon call to extern function

Posted: Fri Apr 16, 2010 11:14 am
by Benjamin1996
neon wrote:Hello,

We are getting a little off topic now. If it didnt execute the "Stage 2" program in Virtual PC and real hardware, then, technically, it didnt work even though it didnt crash :) Bochs differs from Virtual PC and real hardware in different ways.

(Also I appreciate your compliment on the series, I am indeed the author of it.)
Well, have you got any ideas why I won't execute stage 2 then?

Re: Triple fault upon call to extern function

Posted: Fri Apr 16, 2010 11:51 am
by neon
Hello,

That is an off-topic question. Nontheless, you need to provide much more information. Are you using the demo code "as-is"? (ie, running both BUILD.bat scripts without modifying anything?) Are you using a real floppy disk or VFD? What does your Bochs configuration file look like? What version of Bochs?

Re: Triple fault upon call to extern function

Posted: Fri Apr 16, 2010 11:59 am
by Gigasoft
Well, have you got any ideas why I won't execute stage 2 then?
Impossible to say without stepping through it in a debugger. Maybe you didn't format the filename correctly (as in "KRNLDR SYS" with two spaces). Anyway, you don't have to implement FAT. You could just put the kernel at sector 2 onwards, and load the sectors from there. It's easiest to read one sector at a time in a loop, otherwise you have to make sure you don't try to read multiple tracks in a single read operation.

Re: Triple fault upon call to extern function

Posted: Sat Apr 17, 2010 2:31 am
by Benjamin1996
neon wrote:Hello,

That is an off-topic question. Nontheless, you need to provide much more information. Are you using the demo code "as-is"? (ie, running both BUILD.bat scripts without modifying anything?) Are you using a real floppy disk or VFD? What does your Bochs configuration file look like? What version of Bochs?
I'm using the demo as it is, I'm using a real floppy disk, and this is my bochsrc.txt:

Code: Select all

#romimage: file=BIOS-bochs-latest, address=0xf0000
#vgaromimage: VGABIOS-elpin-2.40
megs: 32

floppya: 1_44="C:\Users\Benjamin\Desktop\OS\bootloader.bin", status=inserted
boot: a

log: bochsout.txt
panic: action=ask
error: action=report
info: action=report
debug: action=ignore

vga_update_interval: 300000
keyboard_serial_delay: 250
keyboard_paste_delay: 100000
#floppy_command_delay: 500
#ips: 1000000
mouse: enabled=0
private_colormap: enabled=0
fullscreen: enabled=0
screenmode: name="sample"
keyboard_mapping: enabled=0, map=
And finally, I'm using Bochs version 2.4.2..

Re: Triple fault upon call to extern function

Posted: Sat Apr 17, 2010 7:37 am
by neon
Hello,

Change your floppya line to:

Code: Select all

floppya: 1_44=a:, status=inserted

Re: Triple fault upon call to extern function

Posted: Sat Apr 17, 2010 7:47 am
by Benjamin1996
neon wrote:Hello,

Change your floppya line to:

Code: Select all

floppya: 1_44=a:, status=inserted
Thanks a lot, the demo works now :). I'll try to implement it myself now as well, and then I'll report back in this thread, if it works or not.. :)
EDIT: My own implementation of it works great as well now, thanks a lot :D.