Page 1 of 1

Context switching for MT

Posted: Wed Oct 05, 2011 10:01 am
by eltomato
Hi,

I am currently working on a multitasking implementation.
Somebody told me "just switch esp .."

So I wrote an ASM-Subroutine which is:

Code: Select all

global _switch_task
_switch_task:
	pop eax
	pusha
	xchg esp, eax
	popa
	
	ret
and call it like this:

Code: Select all

uint32_t nextESP = ...
switch_task(nextESP)
but this doesn't work :( System stands still ..

I also tried this:

Code: Select all

uint32_t curESP;
asm ( "mov %%esp, %0;" : "=r"(curESP));
switch_task(curESP);
to test if the asm routine is the problem, or a wrong esp .. but this doesn't work either .. :(

Could someone give me a hint, what could be the problem?

Re: Context switching for MT

Posted: Wed Oct 05, 2011 10:31 am
by Luns
That's not going to work. You should understand what it is you're trying to accomplish and how you're going to accomplish that before attempting to write any code. Start by reading http://wiki.osdev.org/Context_Switching.

Re: Context switching for MT

Posted: Wed Oct 05, 2011 1:10 pm
by mutex
I dont know what you mean by MT...

What you say "just swap esp" can work in a cooperative multitasking scheme where each thread yeld() and jumps to next context but thats probably not what you are looking for.

To take advantage of the CPU protection mechanisms, user/kernel mode, interrupt handling etc you actually must use some support from the CPU to get this to work properly.

Read up on task/context switching in the Intel Developer manual or try the Wiki here for a more detailed code centric tutorial on how to proceed.

-
Thomas

Re: Context switching for MT

Posted: Wed Oct 05, 2011 9:34 pm
by thepowersgang
@eltomato
I suggest you think about the stack state when you enter the function, because you don't have the right idea about it at the moment. Look up the cdecl calling convention and then take another look at your "pop eax".

Also, you're going to want to save the original stack pointer so it can be restored later.