1 // arch/x86/thread.cc -- Thread switching
3 // This software is copyright (c) 2006 Scott Wood <scott@buserror.net>.
5 // This software is provided 'as-is', without any express or implied warranty.
6 // In no event will the authors or contributors be held liable for any damages
7 // arising from the use of this software.
9 // Permission is hereby granted to everyone, free of charge, to use, copy,
10 // modify, prepare derivative works of, publish, distribute, perform,
11 // sublicense, and/or sell copies of the Software, provided that the above
12 // copyright notice and disclaimer of warranty be included in all copies or
13 // substantial portions of this software.
15 #include <kern/thread.h>
17 #include <kern/pagetable.h>
20 void set_aspace(Mem::ProcAddrSpace *aspace)
22 u32 cr3 = Mem::kvirt_to_phys(aspace->page_table->toplevel);
23 asm volatile("movl %0, %%cr3" : : "r" (cr3) : "memory");
26 void switch_thread(Threads::Thread *dest, Threads::Thread *src)
30 if (dest->addr_space) {
31 assert(dest->addr_space == dest->active_addr_space);
33 if (dest->addr_space != src->active_addr_space)
34 set_aspace(dest->addr_space);
36 dest->active_addr_space = src->active_addr_space;
39 Priv::tss.esp0 = reinterpret_cast<u32>(dest);
41 asm volatile("movl %%esp, (%0);"
48 "jnz x86_new_thread;" :
49 "=a" (dummy1), "=c" (dummy2) :
50 "0" (&src->arch.esp), "1" (&dest->arch.esp) :
51 "ebx", "edx", "esi", "edi", "memory");