]> git.buserror.net Git - polintos/scott/priv.git/blob - kernel/arch/x86/thread.cc
Initial checkin from Perforce.
[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 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 set_aspace(Mem::AddrSpace *aspace)
37         {
38                 u32 cr3 = Mem::kvirt_to_phys(aspace->page_table->toplevel);
39                 asm volatile("movl %0, %%cr3" : : "r" (cr3) : "memory");
40         }
41
42         void switch_thread(Threads::Thread *dest, Threads::Thread *src)
43         {
44                 u32 dummy1, dummy2;
45                 
46                 if (dest->addr_space) {
47                         assert(dest->addr_space == dest->active_addr_space);
48                 
49                         if (dest->addr_space != src->active_addr_space)
50                                 set_aspace(dest->addr_space);
51                 } else {
52                         dest->active_addr_space = src->active_addr_space;
53                 }
54                 
55                 Priv::tss.esp0 = reinterpret_cast<u32>(dest);
56
57                 asm volatile("movl %%esp, (%0);"
58                              "movl %%ebp, 4(%0);"
59                              "movb $0, 8(%0);"
60                              "movb 8(%1), %%al;"
61                              "cmpb $0, %%al;"
62                              "movl (%1), %%esp;"
63                              "movl 4(%1), %%ebp;"
64                              "jnz x86_new_thread;" :
65                              "=a" (dummy1), "=c" (dummy2) :
66                              "0" (&src->arch.esp), "1" (&dest->arch.esp) :
67                              "ebx", "edx", "esi", "edi", "memory");
68         }
69 }