]> git.buserror.net Git - polintos/scott/priv.git/blob - kernel/arch/x86/thread.cc
Switch to a simple X11-style license.
[polintos/scott/priv.git] / kernel / arch / x86 / thread.cc
1 // arch/x86/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 condition:
11 // 
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 // 
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
18 // CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
21 // SOFTWARE.
22
23 #include <kern/thread.h>
24 #include <kern/mem.h>
25 #include <kern/pagetable.h>
26
27 namespace Arch {
28         void set_aspace(Mem::AddrSpace *aspace)
29         {
30                 u32 cr3 = Mem::kvirt_to_phys(aspace->page_table->toplevel);
31                 asm volatile("movl %0, %%cr3" : : "r" (cr3) : "memory");
32         }
33
34         void switch_thread(Threads::Thread *dest, Threads::Thread *src)
35         {
36                 u32 dummy1, dummy2;
37                 
38                 if (dest->addr_space) {
39                         assert(dest->addr_space == dest->active_addr_space);
40                 
41                         if (dest->addr_space != src->active_addr_space)
42                                 set_aspace(dest->addr_space);
43                 } else {
44                         dest->active_addr_space = src->active_addr_space;
45                 }
46                 
47                 Priv::tss.esp0 = reinterpret_cast<u32>(dest);
48
49                 asm volatile("movl %%esp, (%0);"
50                              "movl %%ebp, 4(%0);"
51                              "movb $0, 8(%0);"
52                              "movb 8(%1), %%al;"
53                              "cmpb $0, %%al;"
54                              "movl (%1), %%esp;"
55                              "movl 4(%1), %%ebp;"
56                              "jnz x86_new_thread;" :
57                              "=a" (dummy1), "=c" (dummy2) :
58                              "0" (&src->arch.esp), "1" (&dest->arch.esp) :
59                              "ebx", "edx", "esi", "edi", "memory");
60         }
61 }