Multi-taskin?

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
Justmin

Multi-taskin?

Post by Justmin »

As far as I have understand, I can use the timer-interrupt for multitasking? My problem is, suppose I have the EIP/All registers stored for a thread. Then I would like to fill the registers with the correct values, no problem. But how do I set the EIP to a special address and then how do I start to execute from there?
B.E

Re:Multi-taskin?

Post by B.E »

When an interrupt is called the cpu pushes the following in order:

EFLAGS
CS
EIP

So to modify the retern address pop EIP and CS off the stack and push CS and newEIP onto the stack.

Code: Select all

Timer_handler:
    ...
    pop oldEIP
    pop oldCS
    push newCS ; now when we retern CS = NewCS
    push newEIP; now when we retern EIP = NewEIP

    IRET
there are other ways of task switching. if you haven't yet read the manuals. you can get them from intel
oswizard

Re:Multi-taskin?

Post by oswizard »

Instead, if your interrupt handler saves all registers (which it must do anyways) simply store an ESP value for each task. Have your timer handler push a return address onto the current stack, switch stacks, and then execute a ret instruction. This will pop the return address off of the new stack. Have a routine such as this one, from my older OS, in Microsoft C++ (pardon me for not using *nix :)):

Code: Select all

EXTERN void TmSwitchTo(IThread *pThreadCur, IThread *pThreadNew)
{
   __asm push esi;
   __asm push edi;
   __asm push ebp;
   __asm mov ecx, pThreadCur;            // ECX = current thread
   __asm mov edx, pThreadNew;            // EDX = new thread
   __asm mov dword ptr [ecx]IThread.SaveESP, esp;
   __asm mov dword ptr [ecx]IThread.SaveEIP, offset tsret;
   __asm mov esp, dword ptr [edx]IThread.SaveESP;
   __asm push dword ptr [edx]IThread.SaveEIP;
   __asm jmp TmSwitchTo2;               // FASTCALL (cur ECX, new EDX)

tsret:
   __asm pop ebp;
   __asm pop edi;
   __asm pop esi;
}

EXTERN void __fastcall TmSwitchTo2(IThread *pThreadCur, IThread *pThreadNew)
{
    // Code here will be called each time a task switches.
    // This would be a good place to switch CR3, etc.
}
I think this is something like what *nix does, but I am not sure. Anyways, this seems to work for me.

Good luck,
Mike
Dex4u

Re:Multi-taskin?

Post by Dex4u »

Here is a simple example of multitasking, that fits in the boot sector of a floppy, less than 512 bytes, its for realmode, but may help ?.
http://512.decard.net/entries/nanoos2.zip

PS: It was a entry in the 512b compo.
Post Reply