]> git.buserror.net Git - polintos/scott/priv.git/blob - kernel/arch/x86/thread.cc
b530d7c3d0e7d354c9df4c7b5b6e305c6a9b6650
[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
19 namespace Arch {
20         void set_aspace(Mem::AddrSpace *aspace)
21         {
22                 u32 cr3 = Mem::kvirt_to_phys(aspace->page_table->toplevel);
23                 asm volatile("movl %0, %%cr3" : : "r" (cr3) : "memory");
24         }
25
26         void switch_thread(Threads::Thread *dest, Threads::Thread *src)
27         {
28                 u32 dummy1, dummy2;
29                 
30                 if (dest->addr_space) {
31                         assert(dest->addr_space == dest->active_addr_space);
32                 
33                         if (dest->addr_space != src->active_addr_space)
34                                 set_aspace(dest->addr_space);
35                 } else {
36                         dest->active_addr_space = src->active_addr_space;
37                 }
38                 
39                 Priv::tss.esp0 = reinterpret_cast<u32>(dest);
40
41                 asm volatile("movl %%esp, (%0);"
42                              "movl %%ebp, 4(%0);"
43                              "movb $0, 8(%0);"
44                              "movb 8(%1), %%al;"
45                              "cmpb $0, %%al;"
46                              "movl (%1), %%esp;"
47                              "movl 4(%1), %%ebp;"
48                              "jnz x86_new_thread;" :
49                              "=a" (dummy1), "=c" (dummy2) :
50                              "0" (&src->arch.esp), "1" (&dest->arch.esp) :
51                              "ebx", "edx", "esi", "edi", "memory");
52         }
53 }