Context switching for MT

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
eltomato
Posts: 7
Joined: Wed Oct 05, 2011 9:49 am
Location: Bonn, Germany

Context switching for MT

Post 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?
Luns
Member
Member
Posts: 56
Joined: Sun May 01, 2011 12:15 am

Re: Context switching for MT

Post 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.
User avatar
mutex
Member
Member
Posts: 131
Joined: Sat Jul 07, 2007 7:49 pm

Re: Context switching for MT

Post 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
User avatar
thepowersgang
Member
Member
Posts: 734
Joined: Tue Dec 25, 2007 6:03 am
Libera.chat IRC: thePowersGang
Location: Perth, Western Australia
Contact:

Re: Context switching for MT

Post 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.
Kernel Development, It's the brain surgery of programming.
Acess2 OS (c) | Tifflin OS (rust) | mrustc - Rust compiler
Currently Working on: mrustc
Post Reply