Switching threads causing triple fault.

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
AndyB
Posts: 15
Joined: Fri Dec 17, 2010 5:43 pm

Switching threads causing triple fault.

Post by AndyB »

Ive been trying to implement very simple kernel threads as per the JamesM multitasking tutorial. Just to get a basic understanding of the concept before jumping into anything more complicated like full processes.

Obviously, my "multitasking" code is pretty much based off the code from the tutorial.

Unfortunately once tasking is initialized (but nothing yet forked), the second call to kthread_switch causes a triple fault.
The code for kthread_switch: http://pastebin.com/zf7YRJfX

Upon stepping through the code in GDB (Qemu), it is apparent that in the second call to kthread_switch() the if statement is the problem.

Code: Select all

 if(!current_thread) return;
Looking at things more closely stepping through instruction my instruction in Bochs Debugger, the if statement is disassembled as:

Code: Select all

mov eax, dword ptr ds:0xC002101C
test eax, eax
jz 0xC000172B

mov ebx, esp
mov dword ptr ss:[ebp-12],ebx
Everything appears to run as expected until the JZ instruction, at which point the ESP reg gets trashed and always has the value: 0x5244C5B. The next step jumps to 0xFFFFFFF0 and causes a triple fault (No exception).
I find it odd that it is only at the JZ instruction, the "mov ebx, esp" never executes, but somehow ESP is being trashed?

Its always at the second call to kthread_switch and I cant understand why the jump would cause this?

Anyone notice something I dont? I would very much appreciate any input or advice.
MDenham
Member
Member
Posts: 62
Joined: Sat Nov 10, 2012 1:16 pm

Re: Switching threads causing triple fault.

Post by MDenham »

You'd need to trace the actual handling of those faults, because you don't get a triple fault out of nowhere, but I suspect the jz is failing due to either a GPF or a page fault, which is faulting again, and the double fault handler is faulting again.

I'm leaning towards it being a page fault, BTW.

(EDIT: For additional help, a stack dump after stepping into the JZ instruction would be useful here, especially because you could then see what error codes are making it onto the stack.)
greyOne
Member
Member
Posts: 58
Joined: Sun Feb 03, 2013 10:38 pm
Location: Canada

Re: Switching threads causing triple fault.

Post by greyOne »

MDenham wrote: I'm leaning towards it being a page fault, BTW.
Aye.
If your threads have page directories of their own,
Make sure the kernel (and thereby your fault handler) is mapped in said directories.

I will however mention that said multitasking tutorial is far from ideal.
Threads should have their own stacks so as not to interfere,
Which may be the cause of the issue.

If you wish to follow a tutorial on the subject,
This one demonstrates a more complete method.

http://code.google.com/p/onyxkernel/wik ... ltitasking
Post Reply