Page 1 of 1

multitasking problems

Posted: Sat Mar 17, 2007 11:06 am
by iammisc
Hi.

I already have implemented stack-based multitasking in my kernel and it works fine. The problem I am having is that when i do anything big like call the context creation function, when a task switch occurs i get sent to eip=0 instead of the proper location. For some reason my whole stack turns to 0! I don't think this is a problem with my page allocator because the pages are all different. So what else could be going wrong?

Posted: Sat Mar 17, 2007 11:19 am
by AJ
Hi,

When I had this problem it was, indeed, my page allocator in Bochs. I have a few questions that may help:

* What happens on real hardware?
* What does your new task's stack look like before it runs for the first time? Is it all just zero's even after you have just created it (try reading it back)?
* Is your stack aligned? (eg, you are reading a value in to eip that you actually wanted in eax, or similar)
*
* Is the first task switch ok or does eip look wrong even at this point?
* Do you have the correct values in ss0 and esp0 in your tss if you are mixing privileges?
* Are you using different CR3 values? If so, is your new page directory valid?

Cheers,
Adam

Posted: Sat Mar 17, 2007 4:28 pm
by mystran
Also check what happens if you double your stack size. It could be that you are simply recursing too far, or allocating too much local variables, and run out of stack space, hence overwriting some stuff.

Posted: Sat Mar 17, 2007 5:25 pm
by pcmattman
Try disabling the multitasker when you create a new process. I had a problem that whenever I was trying to create a new process and the process creating that process got preempted, the system would crash because of an invalid process table. Moral of the story is, make sure that there is NO access of the process table during creation :D

Posted: Sat Mar 17, 2007 10:28 pm
by iammisc
I think the problem actually might be that my scheduler function is trying to execute the new thread while it is still being created. This would explain eip being 0 as qemu sets all memory to 0, i think. Anyway, I think if I disable the scheduler whilst i am creating the new thread it will work.

I don't think the stack size is an issue because as i said before, i get a page fault. If my stack was too small my os should triple fault because my page fault handler uses the stack.

[EDIT]
pcmattman, yeah I just realized that I had disabled the scheduler in my previous thread creations.

Posted: Sat Mar 17, 2007 10:33 pm
by pcmattman
What I actually meant was that in my OS I do something like this:

Code: Select all

void CreateProcess( ... )
{
    disablemt();
    ...
    enablemt();
}
It just means that your process creation doesn't get preempted and you don't get left with a corrupted process table. Good to hear you got it working, though.

Posted: Sun Mar 18, 2007 12:50 am
by iammisc
no actually i didn't get it working. Uhmm... no matter what type of thread i try to create it always fails if i try to create a new thread within another thread. I can do everything else except create a new thread. I think it has something to do with copying over the page tables.

Posted: Sun Mar 18, 2007 4:29 am
by AJ
Hi,

Assuming that your tasks are contained in some sort of linked list (mine are contained in a 'ring' style list), wouldn't you achieve the same effect by just not inserting your new task in to the list until after you have set up the initial stack? That always seems to work for me.

Adam

Posted: Sun Mar 18, 2007 10:38 am
by iammisc
Yes AJ that is what i do and that is why stopping preemption didn't work.

Posted: Sun Mar 18, 2007 12:01 pm
by iammisc
This was about THE stupidest mistake ever.

I set the page tables wrong.

I set the last entry of the pdt to point to itself so i can access the page tables at 0xFFC00000 and the pdt at 0xFFFFF000. However, in my newly created set of page tables for the new thread, i never set the page tables to the correct physical address. I set it to the old address. But now everything works perfectly.

Posted: Mon Mar 19, 2007 3:06 am
by AJ
:) Glad you've got to the bottom of it. So many of my initial multitasking problems were caused by my mmu. If the mmu is not right, you can never tell whether the problem is with what you are debugging or not :? .

Adam