]> git.buserror.net Git - polintos/scott/priv.git/blob - kernel/arch/x86/thread.cc
d7e445c5e65fffa933019c6434765c6de86bdcd2
[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 // 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.
8 // 
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.
14
15 #include <kern/thread.h>
16 #include <kern/mem.h>
17 #include <kern/pagetable.h>
18 #include <kern/process.h>
19
20 namespace Arch {
21         void set_aspace(Mem::ProcAddrSpace *aspace)
22         {
23                 u32 cr3 = Mem::kvirt_to_phys(aspace->page_table->toplevel);
24                 asm volatile("movl %0, %%cr3" : : "r" (cr3) : "memory");
25         }
26
27         void switch_thread(Threads::Thread *dest, Threads::Thread *src)
28         {
29                 u32 dummy1, dummy2;
30                 
31                 if (dest->addr_space) {
32                         assert(dest->addr_space == dest->active_addr_space);
33                 
34                         if (dest->addr_space != src->active_addr_space)
35                                 set_aspace(dest->addr_space);
36                 } else {
37                         dest->active_addr_space = src->active_addr_space;
38                 }
39                 
40                 Priv::tss.esp0 = reinterpret_cast<u32>(dest);
41
42                 asm volatile("movl %%esp, (%0);"
43                              "movl %%ebp, 4(%0);"
44                              "movb $0, 8(%0);"
45                              "movb 8(%1), %%al;"
46                              "cmpb $0, %%al;"
47                              "movl (%1), %%esp;"
48                              "movl 4(%1), %%ebp;"
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");
53         }
54 }