3b1b086c2153c147791678898061ef2ab117e050
[polintos/scott/priv.git] / kernel / arch / x64 / thread.cc
1 // arch/x64/thread.cc -- Thread switching
2 //
3 // This software is copyright (c) 2006 Scott Wood <scott@buserror.net>.
4 // 
5 // Permission is hereby granted, free of charge, to any person obtaining a copy of
6 // this software and associated documentation files (the "Software"), to deal with
7 // the Software without restriction, including without limitation the rights to
8 // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9 // of the Software, and to permit persons to whom the Software is furnished to do
10 // so, subject to the following conditions:
11 // 
12 //     * Redistributions of source code must retain the above copyright notice,
13 //       this list of conditions and the following disclaimers.
14 // 
15 //     * Redistributions in binary form must reproduce the above copyright notice,
16 //       this list of conditions and the following disclaimers in the
17 //       documentation and/or other materials provided with the distribution.
18 // 
19 //     * The names of the Software's authors and/or contributors
20 //       may not be used to endorse or promote products derived from
21 //       this Software without specific prior written permission.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
25 // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
26 // CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
29 // SOFTWARE.
30
31 #include <kern/thread.h>
32 #include <kern/mem.h>
33 #include <kern/pagetable.h>
34
35 namespace Arch {
36         void switch_thread(Threads::Thread *dest, Threads::Thread *src)
37         {
38                 u64 dummy1, dummy2;
39                 
40                 if (dest->addr_space) {
41                         assert(dest->addr_space == dest->active_addr_space);
42                 
43                         if (dest->addr_space != src->active_addr_space) {
44                                 u64 cr3 = Mem::kvirt_to_phys(dest->addr_space->
45                                                              page_table->toplevel);
46                                 asm volatile("movq %0, %%cr3" : : "r" (cr3) : "memory");
47                         }
48                 } else {
49                         dest->active_addr_space = src->active_addr_space;
50                 }
51                 
52                 Priv::tss.rsp[0] = reinterpret_cast<u64>(dest);
53
54                 asm volatile("movq %%rsp, (%0);"
55                              "movq %%rbp, 8(%0);"
56                              "movb $0, 16(%0);"
57                              "movb 16(%1), %%al;"
58                              "cmpb $0, %%al;"
59                              "movq (%1), %%rsp;"
60                              "movq 8(%1), %%rbp;"
61                              "jnz x64_new_thread;" :
62                              "=a" (dummy1), "=c" (dummy2) :
63                              "0" (&src->arch.rsp), "1" (&dest->arch.rsp) :
64                              "rbx", "rdx", "rsi", "rdi",
65                              "r8", "r9", "r10", "r11", "r12", 
66                              "r13", "r14", "r15", "memory");
67         }
68 }