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>
18 #include <kern/process.h>
21 void set_aspace(Mem::ProcAddrSpace *aspace)
23 u32 cr3 = Mem::kvirt_to_phys(aspace->page_table->toplevel);
24 asm volatile("movl %0, %%cr3" : : "r" (cr3) : "memory");
27 void switch_thread(Threads::Thread *dest, Threads::Thread *src)
31 if (dest->addr_space) {
32 assert(dest->addr_space == dest->active_addr_space);
34 if (dest->addr_space != src->active_addr_space)
35 set_aspace(dest->addr_space);
37 dest->active_addr_space = src->active_addr_space;
40 Priv::tss.esp0 = reinterpret_cast<u32>(dest);
42 asm volatile("movl %%esp, (%0);"
49 "jnz x86_new_thread;" :
50 "=a" (dummy1), "=c" (dummy2) :
51 "0" (&src->arch.esp), "1" (&dest->arch.esp) :
52 "ebx", "edx", "esi", "edi", "memory");