Kernel/x86_64 - Fix task switching
[tpg/acess2.git] / KernelLand / Kernel / arch / x86_64 / proc.asm
1 ;
2 ;
3 ;
4 %include "arch/x86_64/include/common.inc.asm"
5 [BITS 64]
6 [section .text]
7
8 [extern Threads_Exit]
9 [extern glThreadListLock]
10 [extern SHORTREL]
11
12 [global GetRIP]
13 GetRIP:
14         mov rax, [rsp]
15         ret
16
17 [global NewTaskHeader]
18 NewTaskHeader:
19         ; [rsp+0x00]: Thread
20         ; [rsp+0x08]: Function
21         ; [rsp+0x10]: Argument
22
23         mov rdi, glThreadListLock
24         call SHORTREL
25         
26         mov rdi, [rsp+0x10]
27         mov rax, [rsp+0x8]
28         add rsp, 0x10   ; Reclaim stack space (thread/fcn)
29         ;xchg bx, bx
30         call rax
31         
32         ; Quit thread with RAX as the return code
33         xor rdi, rdi
34         mov rsi, rax
35         call Threads_Exit
36
37 .hlt:
38         jmp .hlt
39
40 [extern MM_Clone]
41 [extern MM_DumpTables]
42 [global Proc_CloneInt]
43 Proc_CloneInt:
44         PUSH_GPR
45         ; Save RSP
46         mov [rdi], rsp
47         ; Call MM_Clone (with bNoUserCopy flag)
48         mov rdi, rdx
49         call MM_Clone
50         ; Save CR3
51         mov rsi, [rsp+0x30]     ; Saved version of RSI
52         mov [rsi], rax
53         ; Undo the PUSH_GPR
54         add rsp, 0x80
55         mov rax, .newTask
56         ret
57 .newTask:
58 ;       mov rdi, 0
59 ;       mov rsi, 0x800000000000
60 ;       call MM_DumpTables
61         POP_GPR
62         xor eax, eax
63         ret
64
65 [global SaveState]
66 SaveState:
67         ; Save regs to RSI
68         add rsi, 0x80
69         SAVE_GPR rsi
70         ; Save return addr
71         mov rax, [rsp]
72         mov [rsi], rax
73         ; Return RSI as the RSP value
74         sub rsi, 0x80
75         mov [rdi], rsi
76         ; Check for 
77         mov rax, .restore
78         ret
79 .restore:
80         ; RSP = RSI now
81         POP_GPR
82         mov rax, [rsp]
83         mov rsp, [rsp-0x60]     ; Restore RSP from the saved value
84         mov [rsp], rax  ; Restore return address
85         xor eax, eax
86         ret
87
88 [global SwitchTasks]
89 ; rdi = New RSP
90 ; rsi = Old RSP save loc
91 ; rdx = New RIP
92 ; rcx = Old RIP save loc
93 ; r8 = CR3
94 SwitchTasks:
95         PUSH_GPR
96         
97         ; Save state RIP and RSP
98         lea rax, [rel .restore]
99         mov [rcx], rax
100         mov [rsi], rsp
101
102         ; Change CR3 if requested
103         test r8, r8
104         jz .setState
105         mov cr3, r8
106         
107         ; Make sure the stack is valid before jumping
108         invlpg [rdi-0x1000]
109         invlpg [rdi]
110         invlpg [rdi+0x1000]
111         
112         ; Go to new state
113 .setState:
114         mov rsp, rdi
115         jmp rdx
116
117         ; Restore point for saved state
118 .restore:
119         POP_GPR
120         xor eax, eax    ; Return zero
121         ret
122
123 [global Proc_InitialiseSSE]
124 Proc_InitialiseSSE:
125         mov rax, cr4
126         or ax, (1 << 9)|(1 << 10)       ; Set OSFXSR and OSXMMEXCPT
127         mov cr4, rax
128         mov rax, cr0
129         and ax, ~(1 << 2)       ; Clear EM
130         or rax, (1 << 1)        ; Set MP
131         mov rax, cr0
132         ret
133 [global Proc_DisableSSE]
134 Proc_DisableSSE:
135         mov rax, cr0
136         or ax, 1 << 3   ; Set TS
137         mov cr0, rax
138         ret
139 [global Proc_EnableSSE]
140 Proc_EnableSSE:
141         mov rax, cr0
142         and ax, ~(1 << 3)       ; Clear TS
143         mov cr0, rax
144         ret
145
146 [global Proc_SaveSSE]
147 Proc_SaveSSE:
148         fxsave [rdi]
149         ret
150 [global Proc_RestoreSSE]
151 Proc_RestoreSSE:
152         fxrstor [rdi]
153         ret
154
155 [section .usertext]
156
157 [global User_Signal_Kill]
158 User_Signal_Kill:
159         xor rax, rax
160         mov bx, di
161         mov bh, 0x02
162         int 0xAC
163         jmp $
164
165
166 ; vim: ft=nasm

UCC git Repository :: git.ucc.asn.au