1 // arch/x64/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 switch_thread(Threads::Thread *dest, Threads::Thread *src)
25 assert(dest->aspace == dest->active_aspace);
27 if (dest->aspace != src->active_aspace) {
28 u64 cr3 = Mem::kvirt_to_phys(dest->aspace->
29 page_table->toplevel);
30 asm volatile("movq %0, %%cr3" : : "r" (cr3) : "memory");
33 dest->active_aspace = src->active_aspace;
36 Priv::tss.rsp[0] = reinterpret_cast<u64>(dest);
38 asm volatile("movq %%rsp, (%0);"
45 "jnz x64_new_thread;" :
46 "=a" (dummy1), "=c" (dummy2) :
47 "0" (&src->arch.rsp), "1" (&dest->arch.rsp) :
48 "rbx", "rdx", "rsi", "rdi",
49 "r8", "r9", "r10", "r11", "r12",
50 "r13", "r14", "r15", "memory");