]> git.buserror.net Git - polintos/scott/priv.git/blob - kernel/arch/x86/thread.cc
update
[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->aspace) {
32                         assert(dest->aspace == dest->active_aspace);
33                 
34                         if (dest->aspace != src->active_aspace)
35                                 set_aspace(dest->aspace);
36                 } else {
37                         dest->active_aspace = src->active_aspace;
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
55         void ArchThread::init(void *entry, void *arg1, void *arg2)
56         {
57                 void **stack = reinterpret_cast<void **>(this);
58                 
59                 *--stack = arg2;
60                 *--stack = arg1;
61                 *--stack = entry;
62                 
63                 esp = stack;
64                 ebp = 0;
65                 jump_to_init = 1;
66         }
67 }