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

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
ITchimp
Member
Member
Posts: 134
Joined: Sat Aug 18, 2018 8:44 pm

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

Post 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)
   );
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

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

Post by iansjack »

Have you tested it?

I'd strongly advise against the use of such large inline assembler.
User avatar
Octacone
Member
Member
Posts: 1138
Joined: Fri Aug 07, 2015 6:13 am

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

Post 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.
OS: Basic OS
About: 32 Bit Monolithic Kernel Written in C++ and Assembly, Custom FAT 32 Bootloader
ITchimp
Member
Member
Posts: 134
Joined: Sat Aug 18, 2018 8:44 pm

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

Post 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;");


}
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

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

Post by iansjack »

Does it work or doesn't it?
ITchimp
Member
Member
Posts: 134
Joined: Sat Aug 18, 2018 8:44 pm

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

Post 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
Post Reply