Page 1 of 1

my new context swtich code (comment plz on its feasibility)

Posted: Sat Aug 08, 2020 4:45 am
by ITchimp
I am doing this in response to comment from this forum that james molloy's context switch code was insane!!

Code: Select all

asm volatile("xchg %%bx, %%bx;"
               "cli;"
               "push %%edi;"
               "push %%esi;"
               "push %%ebx;"
               "push %%ebp;"
              // "pushf;"
               "movl %%esp, %0;" // save old esp
               "movl $1f, %1;"   // save eip 
               "mov %%cr3, %%eax;"
               "movl %%eax, %2;" // save old cr3
               "mov %5, %%eax;" // load new cr3
               "mov %%eax, %%cr3;"
               "mov %4, %%esp;" // load new esp
               "jmp *%3;"
               "1: ;"
               //"mov %3, %%esp;"
               //"popf;"
               "pop %%ebp;"
               "pop %%ebx;"
               "pop %%esi;"
               "pop %%edi;"
               "sti;"
               :"=m"(prev->esp),"=m"(prev->eip),"=m"(prev->page_dir->phy_addr): 
                "m"(current_task->eip), "m"(current_task->esp),"m"(current_task->page_dir->phy_addr)
   );

Re: my new context swtich code (comment plz on its feasibili

Posted: Sat Aug 08, 2020 6:09 am
by iansjack
Have you tested it?

I'd strongly advise against the use of such large inline assembler.

Re: my new context swtich code (comment plz on its feasibili

Posted: Sat Aug 08, 2020 8:58 am
by Octacone
Holy mother of Jesus! That is the worst thing I’ve ever seen. You should never ever write you task switching code in an inline assembly, it will just make your life miserable. Just create a separate assembly file and put all your code in there, and everything else can be done with C.

Re: my new context swtich code (comment plz on its feasibili

Posted: Sat Aug 08, 2020 6:51 pm
by ITchimp
What do you think about the logic? does it look good to you?

Code: Select all

void task_switch2(){
  task_t *next, *prev;
  if (!current_task)
     return;
  if (!current_task->next){
        prev = current_task;
        current_task = ready_queue;
  }

  if (current_task == prev)
     return;
  unsigned long edi, esi, eax, ebx, edx, ecx;
  // save all registers 
  // eax ecx edx ebx esi edi ebp esp 
  // cr3
  print("context switch \n");
 // asm volatile("xchg %bx, %bx;");
 // asm volatile("cli;");

  asm volatile("xchg %%bx, %%bx;"
               "cli;"
               "push %%edi;"
               "push %%esi;"
               "push %%ebx;"
               "push %%ebp;"
              // "pushf;"
               "movl %%esp, %0;" // save old esp
               "movl $1f, %1;"   // save eip 
               "mov %%cr3, %%eax;"
               "movl %%eax, %2;" // save old cr3
               "mov %5, %%eax;" // load new cr3
               "mov %%eax, %%cr3;"
               "mov %4, %%esp;" // load new esp
               "jmp *%3;"
               "1: ;"
               //"mov %3, %%esp;"
               //"popf;"
               "pop %%ebp;"
               "pop %%ebx;"
               "pop %%esi;"
               "pop %%edi;"
               "sti;"
               :"=m"(prev->esp),"=m"(prev->eip),"=m"(prev->page_dir->phy_addr): 
                "m"(current_task->eip), "m"(current_task->esp),"m"(current_task->page_dir->phy_addr)
   );
//asm volatile("sti;");


}

Re: my new context swtich code (comment plz on its feasibili

Posted: Sat Aug 08, 2020 11:35 pm
by iansjack
Does it work or doesn't it?

Re: my new context swtich code (comment plz on its feasibili

Posted: Mon Aug 10, 2020 1:38 am
by ITchimp
no it doesn't, context switch code is hard to get right... for me at my stage of understanding, but I am getting better