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.
ITchimp
Member
Posts: 134 Joined: Sat Aug 18, 2018 8:44 pm
Post
by ITchimp » Sat Aug 08, 2020 4:45 am
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)
);
iansjack
Member
Posts: 4703 Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK
Post
by iansjack » Sat Aug 08, 2020 6:09 am
Have you tested it?
I'd strongly advise against the use of such large inline assembler.
Octacone
Member
Posts: 1138 Joined: Fri Aug 07, 2015 6:13 am
Post
by Octacone » Sat Aug 08, 2020 8:58 am
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
Posts: 134 Joined: Sat Aug 18, 2018 8:44 pm
Post
by ITchimp » Sat Aug 08, 2020 6:51 pm
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;");
}
iansjack
Member
Posts: 4703 Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK
Post
by iansjack » Sat Aug 08, 2020 11:35 pm
Does it work or doesn't it?
ITchimp
Member
Posts: 134 Joined: Sat Aug 18, 2018 8:44 pm
Post
by ITchimp » Mon Aug 10, 2020 1:38 am
no it doesn't, context switch code is hard to get right... for me at my stage of understanding, but I am getting better