Page 1 of 1

tss ~OR~ context swithing

Posted: Mon Aug 25, 2003 6:19 am
by Adek336
so, which way is better? with a tss or a context-switch (this is when I swao stacks, yes?)

speak out!

cheers,
Adrian

Re:tss ~OR~ context swithing

Posted: Mon Aug 25, 2003 6:29 am
by Solar
Well... that depends on what you want, no?

1) a software context switch is not only about switching stacks. You will also have to save the registers...

2) I overheard somewhere (best I can give since I'm not yet at the point of adding task switches) that the TSS approach is better if you are frequently switching protection levels... but I couldn't find the reference when I looked for it. Any takers?

Re:tss ~OR~ context swithing

Posted: Mon Aug 25, 2003 6:59 am
by Pype.Clicker
no winner to claim.

software-switch defenders will tell you software switch is faster, easier and more portable, while hardware-switch defenders will tell you hardware switch (i.e. TSS) is faster, easier and deals better with address space, IO priviledge and local descriptor table changes.

The truth is somewhere between the two, easiness highly depending on your existing exceptions-trapping code and on the kind of protection mechanisms you want to offer. Speed of the switch method will greatly depend on the inter-process vs intra-process switch frequency (software switches theorically offering better switch-times when switching between threads in the same process).

Re:tss ~OR~ context swithing

Posted: Mon Aug 25, 2003 7:12 am
by Solar
Which belches up the question, is it possible to mix the two - i.e., TSS for process, software switching for threads?

Re:tss ~OR~ context swithing

Posted: Mon Aug 25, 2003 7:29 am
by Pype.Clicker
It is possible and Clicker "XLR8" implements it :p

I have one TSS per process-x-cpu, and switching from Process1.Thread1 to Process1.Thread2 just implies a stack switch while Process1.Threadx to Process2.Thready implies a TSS jump.

Code: Select all


static void do_switch(kDispatcher *me, kThread *from, kThread* to)
{
  static kThread *checkme;

  checkme=from;
  dumpDispatcher();
  if (from!=to && to!=NULL) {

   switching++;

   kSaveCpuCtx(&(from->cpuState),from->ptrTss);
   kRestoreCpuCtx(to->ptrTss,&(to->cpuState));

   if (!(to->stat & KT_TSSOWNER))
       to->ptrTss->eip=(dword)resume_switch;


      if (from->TSS_ID != to->TSS_ID) {
          task_switch(0,to->TSS_ID,&(from->cpuState));
      } else {
          stack_switch(&(from->cpuState),&(to->cpuState));
      }
      switching--;
  }
}

_task_switch:
   push ebp
   mov ebp, esp
   pushfd
   push fs
   pushad
   mov eax,[ebp+16]
   mov [eax],esp
   mov [eax+4],ss

   jmp far [ebp+8]
_resume_switch:
   popad
   pop fs
   popfd
   pop ebp
   ret


;;--------------------------------------------------------------------------
;; performs a stack-switch for multi-threading.
;; cs:   don't need to be saved: =0x08 for every thread in kernel mode.
;; ds,es,gs:    don't need to be saved: =0x10/0x18 for every thread.
;; fs:   this holds the selector for user data segment. same for all threads
;;   within a given process, but should better be saved in case a thread
;;   changed it for special purpose
;; eax:   return value, don't need to be saved.
;; edx,ecx: callee-clobbered register: the compiler will assume we overwrote
;;   them after function returns.
;; -------------------------------------------------------------------------
;;; cpu_context* stack_switch(cpu_context *saveto, cpu_context *getfrom)
_stack_switch:
   push ebp
   mov ebp,esp
   pushfd
   push fs
   pushad

   mov eax,[ebp+8]
   mov edx,[ebp+12]
   mov [eax],esp
   mov [eax+4],ss
   lss esp,[edx]

   popad
   pop fs
   popfd
   pop ebp
   ret

Look at dispatch.c and control.asm to learn more about it :p

Re:tss ~OR~ context swithing

Posted: Mon Aug 25, 2003 7:30 am
by Adek336
I?m into context switching, because I already have a mini scheduler. Now I?m making a create-task which creates a task from scratch.

Anyways, which way do you plan your oses - tss, context, both or monotasking?

Cheers,
Adrian