Sorting source tree a bit
[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
43         ; Save CR3
44         mov rsi, [rsp+0x30]     ; Saved version of RSI
45         mov [rsi], rax
46         ; Undo the PUSH_GPR
47         add rsp, 0x80
48         mov rax, .newTask
49         ret
50 .newTask:
51 ;       mov rdi, 0
52 ;       mov rsi, 0x800000000000
53 ;       call MM_DumpTables
54         POP_GPR
55         xor eax, eax
56         ret
57
58 [global SaveState]
59 SaveState:
60         ; Save regs to RSI
61         add rsi, 0x80
62         SAVE_GPR rsi
63         ; Save return addr
64         mov rax, [rsp]
65         mov [rsi], rax
66         ; Return RSI as the RSP value
67         sub rsi, 0x80
68         mov [rdi], rsi
69         ; Check for 
70         mov rax, .restore
71         ret
72 .restore:
73         ; RSP = RSI now
74         POP_GPR
75         mov rax, [rsp]
76         mov rsp, [rsp-0x60]     ; Restore RSP from the saved value
77         mov [rsp], rax  ; Restore return address
78         xor eax, eax
79         ret
80
81 [global SwitchTasks]
82 ; rdi = New RSP
83 ; rsi = Old RSP save loc
84 ; rdx = New RIP
85 ; rcx = Old RIP save loc
86 ; r8 = CR3
87 SwitchTasks:
88         PUSH_GPR
89         
90         ; Save state RIP and RSP
91         lea rax, [rel .restore]
92         mov [rcx], rax
93         mov [rsi], rsp
94
95         ; Change CR3 if requested
96         test r8, r8
97         jz .setState
98         mov cr3, r8
99         
100         ; Make sure the stack is valid before jumping
101         invlpg [rdi-0x1000]
102         invlpg [rdi]
103         invlpg [rdi+0x1000]
104         
105         ; Go to new state
106 .setState:
107         mov rsp, rdi
108         jmp rdx
109
110         ; Restore point for saved state
111 .restore:
112         POP_GPR
113         xor eax, eax    ; Return zero
114         ret
115
116 [global Proc_InitialiseSSE]
117 Proc_InitialiseSSE:
118         mov rax, cr4
119         or ax, (1 << 9)|(1 << 10)       ; Set OSFXSR and OSXMMEXCPT
120         mov cr4, rax
121         mov rax, cr0
122         and ax, ~(1 << 2)       ; Clear EM
123         or rax, (1 << 1)        ; Set MP
124         mov rax, cr0
125         ret
126 [global Proc_DisableSSE]
127 Proc_DisableSSE:
128         mov rax, cr0
129         or ax, 1 << 3   ; Set TS
130         mov cr0, rax
131         ret
132 [global Proc_EnableSSE]
133 Proc_EnableSSE:
134         mov rax, cr0
135         and ax, ~(1 << 3)       ; Clear TS
136         mov cr0, rax
137         ret
138
139 [global Proc_SaveSSE]
140 Proc_SaveSSE:
141         fxsave [rdi]
142         ret
143 [global Proc_RestoreSSE]
144 Proc_RestoreSSE:
145         fxrstor [rdi]
146         ret
147
148 ; vim: ft=nasm

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