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

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