1 | ; $Id: bs3-c64-Trap64Generic.asm 106061 2024-09-16 14:03:52Z vboxsync $
|
---|
2 | ;; @file
|
---|
3 | ; BS3Kit - Trap, 64-bit assembly handlers.
|
---|
4 | ;
|
---|
5 |
|
---|
6 | ;
|
---|
7 | ; Copyright (C) 2007-2024 Oracle and/or its affiliates.
|
---|
8 | ;
|
---|
9 | ; This file is part of VirtualBox base platform packages, as
|
---|
10 | ; available from https://www.virtualbox.org.
|
---|
11 | ;
|
---|
12 | ; This program is free software; you can redistribute it and/or
|
---|
13 | ; modify it under the terms of the GNU General Public License
|
---|
14 | ; as published by the Free Software Foundation, in version 3 of the
|
---|
15 | ; License.
|
---|
16 | ;
|
---|
17 | ; This program is distributed in the hope that it will be useful, but
|
---|
18 | ; WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | ; General Public License for more details.
|
---|
21 | ;
|
---|
22 | ; You should have received a copy of the GNU General Public License
|
---|
23 | ; along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | ;
|
---|
25 | ; The contents of this file may alternatively be used under the terms
|
---|
26 | ; of the Common Development and Distribution License Version 1.0
|
---|
27 | ; (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | ; in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | ; CDDL are applicable instead of those of the GPL.
|
---|
30 | ;
|
---|
31 | ; You may elect to license modified versions of this file under the
|
---|
32 | ; terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | ;
|
---|
34 | ; SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | ;
|
---|
36 |
|
---|
37 | ;*********************************************************************************************************************************
|
---|
38 | ;* Header Files *
|
---|
39 | ;*********************************************************************************************************************************
|
---|
40 | %include "bs3kit-template-header.mac"
|
---|
41 |
|
---|
42 | %ifndef TMPL_64BIT
|
---|
43 | %error "64-bit only template"
|
---|
44 | %endif
|
---|
45 |
|
---|
46 |
|
---|
47 | ;*********************************************************************************************************************************
|
---|
48 | ;* External Symbols *
|
---|
49 | ;*********************************************************************************************************************************
|
---|
50 | BS3_EXTERN_DATA16 g_bBs3CurrentMode
|
---|
51 | BS3_EXTERN_DATA16 g_apfnBs3TrapHandlers_c64
|
---|
52 | TMPL_BEGIN_TEXT
|
---|
53 | BS3_EXTERN_CMN Bs3TrapDefaultHandler
|
---|
54 | BS3_EXTERN_CMN Bs3RegCtxRestore
|
---|
55 | TMPL_BEGIN_TEXT
|
---|
56 |
|
---|
57 |
|
---|
58 | ;*********************************************************************************************************************************
|
---|
59 | ;* Global Variables *
|
---|
60 | ;*********************************************************************************************************************************
|
---|
61 | BS3_BEGIN_DATA16
|
---|
62 | ;; Easy to access flat address of Bs3Trap64GenericEntries.
|
---|
63 | BS3_GLOBAL_DATA g_Bs3Trap64GenericEntriesFlatAddr, 4
|
---|
64 | dd Bs3Trap64GenericEntries wrt FLAT
|
---|
65 |
|
---|
66 |
|
---|
67 | TMPL_BEGIN_TEXT
|
---|
68 |
|
---|
69 | ;;
|
---|
70 | ; Generic entry points for IDT handlers, 8 byte spacing.
|
---|
71 | ;
|
---|
72 | BS3_PROC_BEGIN Bs3Trap64GenericEntries
|
---|
73 | %macro Bs3Trap64GenericEntry 1
|
---|
74 | db 06ah, i ; push imm8 - note that this is a signextended value.
|
---|
75 | jmp %1
|
---|
76 | ALIGNCODE(8)
|
---|
77 | %assign i i+1
|
---|
78 | %endmacro
|
---|
79 |
|
---|
80 | %assign i 0 ; start counter.
|
---|
81 | Bs3Trap64GenericEntry Bs3Trap64GenericTrapOrInt ; 0
|
---|
82 | Bs3Trap64GenericEntry Bs3Trap64GenericTrapOrInt ; 1
|
---|
83 | Bs3Trap64GenericEntry Bs3Trap64GenericTrapOrInt ; 2
|
---|
84 | Bs3Trap64GenericEntry Bs3Trap64GenericTrapOrInt ; 3
|
---|
85 | Bs3Trap64GenericEntry Bs3Trap64GenericTrapOrInt ; 4
|
---|
86 | Bs3Trap64GenericEntry Bs3Trap64GenericTrapOrInt ; 5
|
---|
87 | Bs3Trap64GenericEntry Bs3Trap64GenericTrapOrInt ; 6
|
---|
88 | Bs3Trap64GenericEntry Bs3Trap64GenericTrapOrInt ; 7
|
---|
89 | Bs3Trap64GenericEntry Bs3Trap64GenericTrapErrCode ; 8
|
---|
90 | Bs3Trap64GenericEntry Bs3Trap64GenericTrapOrInt ; 9
|
---|
91 | Bs3Trap64GenericEntry Bs3Trap64GenericTrapErrCode ; a
|
---|
92 | Bs3Trap64GenericEntry Bs3Trap64GenericTrapErrCode ; b
|
---|
93 | Bs3Trap64GenericEntry Bs3Trap64GenericTrapErrCode ; c
|
---|
94 | Bs3Trap64GenericEntry Bs3Trap64GenericTrapErrCode ; d
|
---|
95 | Bs3Trap64GenericEntry Bs3Trap64GenericTrapErrCode ; e
|
---|
96 | Bs3Trap64GenericEntry Bs3Trap64GenericTrapOrInt ; f (reserved)
|
---|
97 | Bs3Trap64GenericEntry Bs3Trap64GenericTrapOrInt ; 10
|
---|
98 | Bs3Trap64GenericEntry Bs3Trap64GenericTrapErrCode ; 11
|
---|
99 | Bs3Trap64GenericEntry Bs3Trap64GenericTrapOrInt ; 12
|
---|
100 | Bs3Trap64GenericEntry Bs3Trap64GenericTrapOrInt ; 13
|
---|
101 | Bs3Trap64GenericEntry Bs3Trap64GenericTrapOrInt ; 14
|
---|
102 | Bs3Trap64GenericEntry Bs3Trap64GenericTrapOrInt ; 15 (reserved)
|
---|
103 | Bs3Trap64GenericEntry Bs3Trap64GenericTrapOrInt ; 16 (reserved)
|
---|
104 | Bs3Trap64GenericEntry Bs3Trap64GenericTrapOrInt ; 17 (reserved)
|
---|
105 | Bs3Trap64GenericEntry Bs3Trap64GenericTrapOrInt ; 18 (reserved)
|
---|
106 | Bs3Trap64GenericEntry Bs3Trap64GenericTrapOrInt ; 19 (reserved)
|
---|
107 | Bs3Trap64GenericEntry Bs3Trap64GenericTrapOrInt ; 1a (reserved)
|
---|
108 | Bs3Trap64GenericEntry Bs3Trap64GenericTrapOrInt ; 1b (reserved)
|
---|
109 | Bs3Trap64GenericEntry Bs3Trap64GenericTrapOrInt ; 1c (reserved)
|
---|
110 | Bs3Trap64GenericEntry Bs3Trap64GenericTrapOrInt ; 1d (reserved)
|
---|
111 | Bs3Trap64GenericEntry Bs3Trap64GenericTrapErrCode ; 1e
|
---|
112 | Bs3Trap64GenericEntry Bs3Trap64GenericTrapOrInt ; 1f (reserved)
|
---|
113 | %rep 224
|
---|
114 | Bs3Trap64GenericEntry Bs3Trap64GenericTrapOrInt
|
---|
115 | %endrep
|
---|
116 | BS3_PROC_END Bs3Trap64GenericEntries
|
---|
117 |
|
---|
118 |
|
---|
119 |
|
---|
120 |
|
---|
121 | ;;
|
---|
122 | ; Trap or interrupt (no error code).
|
---|
123 | ;
|
---|
124 | BS3_PROC_BEGIN Bs3Trap64GenericTrapOrInt
|
---|
125 | push rbp ; 0
|
---|
126 | mov rbp, rsp
|
---|
127 | pushfq ; -08h
|
---|
128 | cld
|
---|
129 | push rdi
|
---|
130 |
|
---|
131 | ; Reserve space for the register and trap frame.
|
---|
132 | mov edi, (BS3TRAPFRAME_size + 15) / 16
|
---|
133 | .more_zeroed_space:
|
---|
134 | push qword 0
|
---|
135 | push qword 0
|
---|
136 | dec edi
|
---|
137 | jnz .more_zeroed_space
|
---|
138 | mov rdi, rsp ; rdi points to trapframe structure.
|
---|
139 |
|
---|
140 | ; Free up rax.
|
---|
141 | mov [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.rax], rax
|
---|
142 |
|
---|
143 | ; Copy stuff from the stack over.
|
---|
144 | mov al, [rbp + 08h]
|
---|
145 | mov [rdi + BS3TRAPFRAME.bXcpt], al
|
---|
146 | mov rax, [rbp]
|
---|
147 | mov [rdi + BS3TRAPFRAME.Ctx + BS3REGCTX.rbp], rax
|
---|
148 | mov rax, [rbp - 08h]
|
---|
149 | mov [rdi + BS3TRAPFRAME.fHandlerRfl], rax
|
---|
150 | mov rax, [rbp - 10h]
|
---|
151 | mov [rdi + BS3TRAPFRAME.Ctx + BS3REGCTX.rdi], rax
|
---|
152 |
|
---|
153 | lea rbp, [rbp + 08h] ; iret - 8 (i.e. rbp frame chain location)
|
---|
154 | jmp Bs3Trap64GenericCommon
|
---|
155 | BS3_PROC_END Bs3Trap64GenericTrapOrInt
|
---|
156 |
|
---|
157 |
|
---|
158 | ;;
|
---|
159 | ; Trap with error code.
|
---|
160 | ;
|
---|
161 | BS3_PROC_BEGIN Bs3Trap64GenericTrapErrCode
|
---|
162 | push rbp ; 0
|
---|
163 | mov rbp, rsp
|
---|
164 | pushfq ; -08h
|
---|
165 | cld
|
---|
166 | push rdi
|
---|
167 |
|
---|
168 | ; Reserve space for the register and trap frame.
|
---|
169 | mov edi, (BS3TRAPFRAME_size + 15) / 16
|
---|
170 | .more_zeroed_space:
|
---|
171 | push qword 0
|
---|
172 | push qword 0
|
---|
173 | dec edi
|
---|
174 | jnz .more_zeroed_space
|
---|
175 | mov rdi, rsp ; rdi points to trapframe structure.
|
---|
176 |
|
---|
177 | ; Free up rax.
|
---|
178 | mov [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.rax], rax
|
---|
179 |
|
---|
180 | ; Copy stuff from the stack over.
|
---|
181 | mov rax, [rbp + 10h]
|
---|
182 | mov [rdi + BS3TRAPFRAME.uErrCd], rax
|
---|
183 | mov al, [rbp + 08h]
|
---|
184 | mov [rdi + BS3TRAPFRAME.bXcpt], al
|
---|
185 | mov rax, [rbp]
|
---|
186 | mov [rdi + BS3TRAPFRAME.Ctx + BS3REGCTX.rbp], rax
|
---|
187 | mov rax, [rbp - 08h]
|
---|
188 | mov [rdi + BS3TRAPFRAME.fHandlerRfl], rax
|
---|
189 | mov rax, [rbp - 10h]
|
---|
190 | mov [rdi + BS3TRAPFRAME.Ctx + BS3REGCTX.rdi], rax
|
---|
191 |
|
---|
192 | lea rbp, [rbp + 10h] ; iret - 8 (i.e. rbp frame chain location)
|
---|
193 | jmp Bs3Trap64GenericCommon
|
---|
194 | BS3_PROC_END Bs3Trap64GenericTrapErrCode
|
---|
195 |
|
---|
196 |
|
---|
197 | ;;
|
---|
198 | ; Common context saving code and dispatching.
|
---|
199 | ;
|
---|
200 | ; @param rdi Pointer to the trap frame. The following members have been
|
---|
201 | ; filled in by the previous code:
|
---|
202 | ; - bXcpt
|
---|
203 | ; - uErrCd
|
---|
204 | ; - fHandlerRfl
|
---|
205 | ; - Ctx.rax
|
---|
206 | ; - Ctx.rbp
|
---|
207 | ; - Ctx.rdi
|
---|
208 | ;
|
---|
209 | ; @param rbp Pointer to the dword before the iret frame, i.e. where rbp
|
---|
210 | ; would be saved if this was a normal call.
|
---|
211 | ;
|
---|
212 | BS3_PROC_BEGIN Bs3Trap64GenericCommon
|
---|
213 | ;
|
---|
214 | ; Fake RBP frame.
|
---|
215 | ;
|
---|
216 | mov rax, [rdi + BS3TRAPFRAME.Ctx + BS3REGCTX.rbp]
|
---|
217 | mov [rbp], rax
|
---|
218 |
|
---|
219 | ;
|
---|
220 | ; Save the remaining GPRs and segment registers.
|
---|
221 | ;
|
---|
222 | mov [rdi + BS3TRAPFRAME.Ctx + BS3REGCTX.rcx], rcx
|
---|
223 | mov [rdi + BS3TRAPFRAME.Ctx + BS3REGCTX.rdx], rdx
|
---|
224 | mov [rdi + BS3TRAPFRAME.Ctx + BS3REGCTX.rbx], rbx
|
---|
225 | mov [rdi + BS3TRAPFRAME.Ctx + BS3REGCTX.rsi], rsi
|
---|
226 | mov [rdi + BS3TRAPFRAME.Ctx + BS3REGCTX.r8 ], r8
|
---|
227 | mov [rdi + BS3TRAPFRAME.Ctx + BS3REGCTX.r9 ], r9
|
---|
228 | mov [rdi + BS3TRAPFRAME.Ctx + BS3REGCTX.r10], r10
|
---|
229 | mov [rdi + BS3TRAPFRAME.Ctx + BS3REGCTX.r11], r11
|
---|
230 | mov [rdi + BS3TRAPFRAME.Ctx + BS3REGCTX.r12], r12
|
---|
231 | mov [rdi + BS3TRAPFRAME.Ctx + BS3REGCTX.r13], r13
|
---|
232 | mov [rdi + BS3TRAPFRAME.Ctx + BS3REGCTX.r14], r14
|
---|
233 | mov [rdi + BS3TRAPFRAME.Ctx + BS3REGCTX.r15], r15
|
---|
234 | mov [rdi + BS3TRAPFRAME.Ctx + BS3REGCTX.ds], ds
|
---|
235 | mov [rdi + BS3TRAPFRAME.Ctx + BS3REGCTX.es], es
|
---|
236 | mov [rdi + BS3TRAPFRAME.Ctx + BS3REGCTX.fs], fs
|
---|
237 | mov [rdi + BS3TRAPFRAME.Ctx + BS3REGCTX.gs], gs
|
---|
238 | lea rax, [rbp + 8h]
|
---|
239 | mov [rdi + BS3TRAPFRAME.uHandlerRsp], rax
|
---|
240 | mov [rdi + BS3TRAPFRAME.uHandlerSs], ss
|
---|
241 |
|
---|
242 | ;
|
---|
243 | ; Load 32-bit data selector for the DPL we're executing at into DS, ES and SS.
|
---|
244 | ; Save the handler CS value first.
|
---|
245 | ;
|
---|
246 | mov ax, cs
|
---|
247 | mov [rdi + BS3TRAPFRAME.uHandlerCs], ax
|
---|
248 | AssertCompile(BS3_SEL_RING_SHIFT == 8)
|
---|
249 | and al, 3
|
---|
250 | mov ah, al
|
---|
251 | add ax, BS3_SEL_R0_DS64
|
---|
252 | mov ds, ax
|
---|
253 | mov es, ax
|
---|
254 | mov ss, ax
|
---|
255 |
|
---|
256 | ;
|
---|
257 | ; Copy and update the mode.
|
---|
258 | ;
|
---|
259 | mov al, [BS3_DATA16_WRT(g_bBs3CurrentMode)]
|
---|
260 | mov [rdi + BS3TRAPFRAME.Ctx + BS3REGCTX.bMode], al
|
---|
261 | and al, ~BS3_MODE_CODE_MASK
|
---|
262 | or al, BS3_MODE_CODE_64
|
---|
263 | mov [BS3_DATA16_WRT(g_bBs3CurrentMode)], al
|
---|
264 |
|
---|
265 | ;
|
---|
266 | ; Copy iret info. Bless AMD for only doing one 64-bit iret frame layout.
|
---|
267 | ;
|
---|
268 | mov rcx, [rbp + 08]
|
---|
269 | mov [rdi + BS3TRAPFRAME.Ctx + BS3REGCTX.rip], rcx
|
---|
270 | mov cx, [rbp + 10h]
|
---|
271 | mov [rdi + BS3TRAPFRAME.Ctx + BS3REGCTX.cs], cx
|
---|
272 | and cl, 3
|
---|
273 | mov [rdi + BS3TRAPFRAME.Ctx + BS3REGCTX.bCpl], cl
|
---|
274 | mov rcx, [rbp + 18h]
|
---|
275 | mov [rdi + BS3TRAPFRAME.Ctx + BS3REGCTX.rflags], rcx
|
---|
276 | mov rcx, [rbp + 20h]
|
---|
277 | mov [rdi + BS3TRAPFRAME.Ctx + BS3REGCTX.rsp], rcx
|
---|
278 | mov cx, [rbp + 28h]
|
---|
279 | mov [rdi + BS3TRAPFRAME.Ctx + BS3REGCTX.ss], cx
|
---|
280 | mov byte [rdi + BS3TRAPFRAME.cbIretFrame], 5*8
|
---|
281 |
|
---|
282 | ;
|
---|
283 | ; Control registers.
|
---|
284 | ;
|
---|
285 | str ax
|
---|
286 | mov [rdi + BS3TRAPFRAME.Ctx + BS3REGCTX.tr], ax
|
---|
287 | sldt ax
|
---|
288 | mov [rdi + BS3TRAPFRAME.Ctx + BS3REGCTX.ldtr], ax
|
---|
289 |
|
---|
290 | mov ax, ss
|
---|
291 | test al, 3
|
---|
292 | jnz .skip_crX_because_cpl_not_0
|
---|
293 |
|
---|
294 | mov rax, cr0
|
---|
295 | mov [rdi + BS3TRAPFRAME.Ctx + BS3REGCTX.cr0], rax
|
---|
296 | mov rax, cr2
|
---|
297 | mov [rdi + BS3TRAPFRAME.Ctx + BS3REGCTX.cr2], rax
|
---|
298 | mov rax, cr3
|
---|
299 | mov [rdi + BS3TRAPFRAME.Ctx + BS3REGCTX.cr3], rax
|
---|
300 | mov rax, cr4
|
---|
301 | mov [rdi + BS3TRAPFRAME.Ctx + BS3REGCTX.cr4], rax
|
---|
302 | jmp .dispatch_to_handler
|
---|
303 |
|
---|
304 | .skip_crX_because_cpl_not_0:
|
---|
305 | or byte [rdi + BS3TRAPFRAME.Ctx + BS3REGCTX.fbFlags], \
|
---|
306 | BS3REG_CTX_F_NO_CR0_IS_MSW | BS3REG_CTX_F_NO_CR2_CR3 | BS3REG_CTX_F_NO_CR4
|
---|
307 | smsw [rdi + BS3TRAPFRAME.Ctx + BS3REGCTX.cr0]
|
---|
308 |
|
---|
309 | ;
|
---|
310 | ; Dispatch it to C code.
|
---|
311 | ;
|
---|
312 | .dispatch_to_handler: ; The double fault code joins us here.
|
---|
313 | movzx ebx, byte [rdi + BS3TRAPFRAME.bXcpt]
|
---|
314 | lea rax, [BS3_DATA16_WRT(_g_apfnBs3TrapHandlers_c64)]
|
---|
315 | mov rax, [rax + rbx * 8]
|
---|
316 | or rax, rax
|
---|
317 | jnz .call_handler
|
---|
318 | lea rax, [BS3_WRT_RIP(Bs3TrapDefaultHandler)]
|
---|
319 | .call_handler:
|
---|
320 | sub rsp, 20h
|
---|
321 | mov [rsp], rdi
|
---|
322 | mov rcx, rdi
|
---|
323 | call rax
|
---|
324 |
|
---|
325 | ;
|
---|
326 | ; Resume execution using trap frame.
|
---|
327 | ;
|
---|
328 | xor edx, edx ; fFlags
|
---|
329 | mov [rsp + 8], rdx
|
---|
330 | lea rcx, [rdi + BS3TRAPFRAME.Ctx] ; pCtx
|
---|
331 | mov [rsp], rcx
|
---|
332 | call Bs3RegCtxRestore
|
---|
333 | .panic:
|
---|
334 | hlt
|
---|
335 | jmp .panic
|
---|
336 | BS3_PROC_END Bs3Trap64GenericCommon
|
---|
337 |
|
---|