Software task switching . How?

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.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Software task switching . How?

Post by Pype.Clicker »

Woow ... this will hardly depend on the way you're going to handle tasks, but i guess you should at least
1. allocate some task descriptor
2. possibly link it to some process structure, increment identifier context, etc.
3. prepare the stack frame so that it matches the 'pops' in stack_switch (we want to cheat the dispatcher and make it believe that the new spawned task is just another task from which it switched out earlier :)

Code: Select all

  dword* sp=(dword*) me->cpuState.esp;  
/* we assume we have a kernel stack aligned on the datasegment. */
  sign("preparing core->kernel return\n");
  *(--sp)=(dword)entry;   /* sets up the return address to the entry point */
  *(--sp)=0;              /* starts with a nul %ebp value */
  *(--sp)=IA32_INITFLAGS|IA32_INTERRUPT; /* stores the flags */
  *(--sp)=0;              /* FS selector will be null */
  sp=sp-8;        /* skip 8 values for generic registers (should be null). */
  me->cpuState.esp=(dword) sp;
  sign("final esp value = %x\n",me->cpuState.esp);
Note that threadCreate(...) of kernel/mtask/thread.c is way more complicated because it has logic to handle a lot of cases, so that it can allocate stacks if you don't provide some, get entries for special-owned TSS, setup stack areas for thread-local variables and the like ...
Post Reply