next up previous contents
Next: Trimedia Up: Context Initialization Previous: Context Initialization   Contents

x86

For GNU C compiler uses the convention of passing all the arguements to a function, through the stack. The return addresse of a function is also stored in the stack. Hence in CPU_Context_Initialize both the entry_point and the arguements to entry_point are stored in the stack. Context switch is carried out by first loading all the registers and then executing a ret. On a ret instruction, the processor pops the return address from the stack (which in this case is entry_point) and then jumps to that. The function entry_point gets its arguement from the (stack - 4) & (stack - 8).

#define CPU_EFLAGS_INTERRUPTS_ON  0x00003202

void CPU_Context_Initialize(struct reg_context *the_context,void *stack_base,\
                            oskit_u32_t stack_size,
                            void *(*entry_point)(void *,void *),
                            void *(*thread_fun)(void *),
                            void *thread_arg)
{
  oskit_u32_t stack;
  stack=((int)stack_base)+stack_size-16;
  *(int *)stack=entry_point;
  *(int *)(stack+4)=0;
  *(int *)(stack+8)=thread_fun;
  *(int *)(stack+12)=thread_arg;
  (the_context)->eflags = CPU_EFLAGS_INTERRUPTS_ON;
  (the_context)->ebp     = (void *)stack; 
  (the_context)->esp     = (void *)stack;
  (the_context)->eax = 0;
  (the_context)->ebx = 0;
  (the_context)->ecx = 0;
  (the_context)->edx = 0;
  (the_context)->esi = 0;
  (the_context)->edi = 0;
}



Soumyadeb Mitra 2002-08-07