1 | ; $Id: bs3-c32-Trap32Generic.asm 60132 2016-03-22 09:32:04Z vboxsync $
|
---|
2 | ;; @file
|
---|
3 | ; BS3Kit - Trap, 32-bit assembly handlers.
|
---|
4 | ;
|
---|
5 |
|
---|
6 | ;
|
---|
7 | ; Copyright (C) 2007-2016 Oracle Corporation
|
---|
8 | ;
|
---|
9 | ; This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | ; available from http://www.virtualbox.org. This file is free software;
|
---|
11 | ; you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | ; General Public License (GPL) as published by the Free Software
|
---|
13 | ; Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | ; VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | ; hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | ;
|
---|
17 | ; The contents of this file may alternatively be used under the terms
|
---|
18 | ; of the Common Development and Distribution License Version 1.0
|
---|
19 | ; (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | ; VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | ; CDDL are applicable instead of those of the GPL.
|
---|
22 | ;
|
---|
23 | ; You may elect to license modified versions of this file under the
|
---|
24 | ; terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | ;
|
---|
26 |
|
---|
27 | ;*********************************************************************************************************************************
|
---|
28 | ;* Header Files *
|
---|
29 | ;*********************************************************************************************************************************
|
---|
30 | %include "bs3kit-template-header.mac"
|
---|
31 |
|
---|
32 | %ifndef TMPL_32BIT
|
---|
33 | %error "32-bit only template"
|
---|
34 | %endif
|
---|
35 |
|
---|
36 |
|
---|
37 | ;*********************************************************************************************************************************
|
---|
38 | ;* External Symbols *
|
---|
39 | ;*********************************************************************************************************************************
|
---|
40 | BS3_EXTERN_DATA16 g_bBs3CurrentMode
|
---|
41 | BS3_EXTERN_SYSTEM16 Bs3Gdt
|
---|
42 | TMPL_BEGIN_TEXT
|
---|
43 | BS3_EXTERN_CMN Bs3TrapDefaultHandler
|
---|
44 | BS3_EXTERN_CMN Bs3RegCtxRestore
|
---|
45 | TMPL_BEGIN_TEXT
|
---|
46 |
|
---|
47 |
|
---|
48 | ;*********************************************************************************************************************************
|
---|
49 | ;* Global Variables *
|
---|
50 | ;*********************************************************************************************************************************
|
---|
51 | BS3_BEGIN_DATA16
|
---|
52 | ;; Easy to access flat address of Bs3Trap32GenericEntries.
|
---|
53 | BS3_GLOBAL_DATA g_Bs3Trap32GenericEntriesFlatAddr, 4
|
---|
54 | dd Bs3Trap32GenericEntries wrt FLAT
|
---|
55 | ;; Easy to access flat address of Bs3Trap32DoubleFaultHandler.
|
---|
56 | BS3_GLOBAL_DATA g_Bs3Trap32DoubleFaultHandlerFlatAddr, 4
|
---|
57 | dd Bs3Trap32DoubleFaultHandler wrt FLAT
|
---|
58 |
|
---|
59 | BS3_BEGIN_DATA32
|
---|
60 | ;; Pointer C trap handlers.
|
---|
61 | BS3_GLOBAL_DATA g_apfnBs3TrapHandlers_c32, 1024
|
---|
62 | resd 256
|
---|
63 |
|
---|
64 |
|
---|
65 |
|
---|
66 | ;;
|
---|
67 | ; Generic entry points for IDT handlers, 8 byte spacing.
|
---|
68 | ;
|
---|
69 | BS3_PROC_BEGIN Bs3Trap32GenericEntries
|
---|
70 | %macro Bs3Trap32GenericEntry 1
|
---|
71 | db 06ah, i ; push imm8 - note that this is a signextended value.
|
---|
72 | jmp %1
|
---|
73 | ALIGNCODE(8)
|
---|
74 | %assign i i+1
|
---|
75 | %endmacro
|
---|
76 |
|
---|
77 | %assign i 0 ; start counter.
|
---|
78 | Bs3Trap32GenericEntry bs3Trap32GenericTrapOrInt ; 0
|
---|
79 | Bs3Trap32GenericEntry bs3Trap32GenericTrapOrInt ; 1
|
---|
80 | Bs3Trap32GenericEntry bs3Trap32GenericTrapOrInt ; 2
|
---|
81 | Bs3Trap32GenericEntry bs3Trap32GenericTrapOrInt ; 3
|
---|
82 | Bs3Trap32GenericEntry bs3Trap32GenericTrapOrInt ; 4
|
---|
83 | Bs3Trap32GenericEntry bs3Trap32GenericTrapOrInt ; 5
|
---|
84 | Bs3Trap32GenericEntry bs3Trap32GenericTrapOrInt ; 6
|
---|
85 | Bs3Trap32GenericEntry bs3Trap32GenericTrapOrInt ; 7
|
---|
86 | Bs3Trap32GenericEntry bs3Trap32GenericTrapErrCode ; 8
|
---|
87 | Bs3Trap32GenericEntry bs3Trap32GenericTrapOrInt ; 9
|
---|
88 | Bs3Trap32GenericEntry bs3Trap32GenericTrapErrCode ; a
|
---|
89 | Bs3Trap32GenericEntry bs3Trap32GenericTrapErrCode ; b
|
---|
90 | Bs3Trap32GenericEntry bs3Trap32GenericTrapErrCode ; c
|
---|
91 | Bs3Trap32GenericEntry bs3Trap32GenericTrapErrCode ; d
|
---|
92 | Bs3Trap32GenericEntry bs3Trap32GenericTrapErrCode ; e
|
---|
93 | Bs3Trap32GenericEntry bs3Trap32GenericTrapOrInt ; f (reserved)
|
---|
94 | Bs3Trap32GenericEntry bs3Trap32GenericTrapOrInt ; 10
|
---|
95 | Bs3Trap32GenericEntry bs3Trap32GenericTrapErrCode ; 11
|
---|
96 | Bs3Trap32GenericEntry bs3Trap32GenericTrapOrInt ; 12
|
---|
97 | Bs3Trap32GenericEntry bs3Trap32GenericTrapOrInt ; 13
|
---|
98 | Bs3Trap32GenericEntry bs3Trap32GenericTrapOrInt ; 14
|
---|
99 | Bs3Trap32GenericEntry bs3Trap32GenericTrapOrInt ; 15 (reserved)
|
---|
100 | Bs3Trap32GenericEntry bs3Trap32GenericTrapOrInt ; 16 (reserved)
|
---|
101 | Bs3Trap32GenericEntry bs3Trap32GenericTrapOrInt ; 17 (reserved)
|
---|
102 | Bs3Trap32GenericEntry bs3Trap32GenericTrapOrInt ; 18 (reserved)
|
---|
103 | Bs3Trap32GenericEntry bs3Trap32GenericTrapOrInt ; 19 (reserved)
|
---|
104 | Bs3Trap32GenericEntry bs3Trap32GenericTrapOrInt ; 1a (reserved)
|
---|
105 | Bs3Trap32GenericEntry bs3Trap32GenericTrapOrInt ; 1b (reserved)
|
---|
106 | Bs3Trap32GenericEntry bs3Trap32GenericTrapOrInt ; 1c (reserved)
|
---|
107 | Bs3Trap32GenericEntry bs3Trap32GenericTrapOrInt ; 1d (reserved)
|
---|
108 | Bs3Trap32GenericEntry bs3Trap32GenericTrapErrCode ; 1e
|
---|
109 | Bs3Trap32GenericEntry bs3Trap32GenericTrapOrInt ; 1f (reserved)
|
---|
110 | %rep 224
|
---|
111 | Bs3Trap32GenericEntry bs3Trap32GenericTrapOrInt
|
---|
112 | %endrep
|
---|
113 | BS3_PROC_END Bs3Trap32GenericEntries
|
---|
114 |
|
---|
115 |
|
---|
116 |
|
---|
117 |
|
---|
118 | ;;
|
---|
119 | ; Trap or interrupt (no error code).
|
---|
120 | ;
|
---|
121 | BS3_PROC_BEGIN bs3Trap32GenericTrapOrInt
|
---|
122 | push ebp ; 0
|
---|
123 | mov ebp, esp
|
---|
124 | pushfd ; -04h
|
---|
125 | cli
|
---|
126 | cld
|
---|
127 | push eax ; -08h
|
---|
128 | push edi ; -0ch
|
---|
129 | push ss ; -10h
|
---|
130 | push ds ; -14h
|
---|
131 |
|
---|
132 | ; Make sure we've got a flat DS (ASSUMES ring-0). It makes everything so much simpler.
|
---|
133 | mov ax, BS3_SEL_R0_DS32
|
---|
134 | mov ds, ax
|
---|
135 |
|
---|
136 | ;
|
---|
137 | ; We may be comming from 16-bit code with a 16-bit SS. Thunk it as
|
---|
138 | ; the C code may assume flat SS and we'll mess up by using EBP/ESP/EDI
|
---|
139 | ; instead of BP/SP/SS:DI. ASSUMES standard GDT selector.
|
---|
140 | ;
|
---|
141 | mov ax, ss
|
---|
142 | lar eax, ax
|
---|
143 | test eax, X86LAR_F_D
|
---|
144 | jz .stack_thunk
|
---|
145 | mov ax, BS3_SEL_R0_SS32
|
---|
146 | mov ss, ax
|
---|
147 | jmp .stack_flat
|
---|
148 | .stack_thunk:
|
---|
149 | hlt
|
---|
150 | mov di, ss
|
---|
151 | and edi, X86_SEL_MASK_OFF_RPL
|
---|
152 | mov al, [X86DESCGENERIC_BIT_OFF_BASE_HIGH1 / 8 + edi + Bs3Gdt wrt FLAT]
|
---|
153 | mov ah, [X86DESCGENERIC_BIT_OFF_BASE_HIGH2 / 8 + edi + Bs3Gdt wrt FLAT]
|
---|
154 | shl eax, 16
|
---|
155 | mov ax, [X86DESCGENERIC_BIT_OFF_BASE_LOW / 8 + edi + Bs3Gdt wrt FLAT] ; eax = SS.base
|
---|
156 | movzx ebp, bp ; SS:BP -> flat EBP.
|
---|
157 | add ebp, eax
|
---|
158 | movzx edi, sp ; SS:SP -> flat ESP in EAX.
|
---|
159 | add eax, edi
|
---|
160 | mov di, BS3_SEL_R0_SS32
|
---|
161 | mov ss, di
|
---|
162 | mov esp, eax
|
---|
163 | .stack_flat:
|
---|
164 |
|
---|
165 | ; Reserve space for the the register and trap frame.
|
---|
166 | mov eax, (BS3TRAPFRAME_size + 7) / 8
|
---|
167 | AssertCompileSizeAlignment(BS3TRAPFRAME, 8)
|
---|
168 | .more_zeroed_space:
|
---|
169 | push dword 0
|
---|
170 | push dword 0
|
---|
171 | dec eax
|
---|
172 | jnz .more_zeroed_space
|
---|
173 | mov edi, esp ; edi points to trapframe structure.
|
---|
174 |
|
---|
175 | ; Copy stuff from the stack over.
|
---|
176 | mov al, [ebp + 4]
|
---|
177 | mov [edi + BS3TRAPFRAME.bXcpt], al
|
---|
178 | mov eax, [ebp]
|
---|
179 | mov [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.rbp], eax
|
---|
180 | mov eax, [ebp - 04h]
|
---|
181 | mov [edi + BS3TRAPFRAME.fHandlerRfl], eax
|
---|
182 | mov eax, [ebp - 08h]
|
---|
183 | mov [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.rax], eax
|
---|
184 | mov eax, [ebp - 0ch]
|
---|
185 | mov [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.rdi], eax
|
---|
186 | mov ax, [ebp - 10h]
|
---|
187 | mov [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.ss], ax
|
---|
188 | mov [edi + BS3TRAPFRAME.uHandlerSs], ax
|
---|
189 | mov ax, [ebp - 14h]
|
---|
190 | mov [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.ds], ax
|
---|
191 |
|
---|
192 | lea ebp, [ebp + 4] ; iret - 4 (i.e. ebp frame chain location)
|
---|
193 | jmp bs3Trap32GenericCommon
|
---|
194 | BS3_PROC_END bs3Trap32GenericTrapOrInt
|
---|
195 |
|
---|
196 |
|
---|
197 | ;;
|
---|
198 | ; Trap with error code.
|
---|
199 | ;
|
---|
200 | BS3_PROC_BEGIN bs3Trap32GenericTrapErrCode
|
---|
201 | push ebp ; 0
|
---|
202 | mov ebp, esp
|
---|
203 | pushfd ; -04h
|
---|
204 | cli
|
---|
205 | cld
|
---|
206 | push eax ; -08h
|
---|
207 | push edi ; -0ch
|
---|
208 | push ss ; -10h
|
---|
209 | push ds ; -14h
|
---|
210 |
|
---|
211 | ; Make sure we've got a flat DS (ASSUMES ring-0). It makes everything so much simpler.
|
---|
212 | mov ax, BS3_SEL_R0_DS32
|
---|
213 | mov ds, ax
|
---|
214 |
|
---|
215 | ;
|
---|
216 | ; We may be comming from 16-bit code with a 16-bit SS. Thunk it as
|
---|
217 | ; the C code may assume flat SS and we'll mess up by using EBP/ESP/EDI
|
---|
218 | ; instead of BP/SP/SS:DI. ASSUMES standard GDT selector.
|
---|
219 | ;
|
---|
220 | mov ax, ss
|
---|
221 | lar eax, ax
|
---|
222 | test eax, X86LAR_F_D
|
---|
223 | jz .stack_thunk
|
---|
224 | mov ax, BS3_SEL_R0_SS16
|
---|
225 | mov ss, ax
|
---|
226 | jmp .stack_flat
|
---|
227 | .stack_thunk:
|
---|
228 | mov di, ss
|
---|
229 | and edi, X86_SEL_MASK_OFF_RPL
|
---|
230 | mov al, [X86DESCGENERIC_BIT_OFF_BASE_HIGH1 / 8 + edi + Bs3Gdt wrt FLAT]
|
---|
231 | mov ah, [X86DESCGENERIC_BIT_OFF_BASE_HIGH2 / 8 + edi + Bs3Gdt wrt FLAT]
|
---|
232 | shl eax, 16
|
---|
233 | mov ax, [X86DESCGENERIC_BIT_OFF_BASE_LOW / 8 + edi + Bs3Gdt wrt FLAT] ; eax = SS.base
|
---|
234 | movzx ebp, bp ; SS:BP -> flat EBP.
|
---|
235 | add ebp, eax
|
---|
236 | movzx edi, sp ; SS:SP -> flat ESP in EAX.
|
---|
237 | add eax, edi
|
---|
238 | mov di, BS3_SEL_R0_SS16
|
---|
239 | mov ss, di
|
---|
240 | mov esp, eax
|
---|
241 | .stack_flat:
|
---|
242 |
|
---|
243 | ; Reserve space for the the register and trap frame.
|
---|
244 | mov eax, (BS3TRAPFRAME_size + 7) / 8
|
---|
245 | AssertCompileSizeAlignment(BS3TRAPFRAME, 8)
|
---|
246 | .more_zeroed_space:
|
---|
247 | push dword 0
|
---|
248 | push dword 0
|
---|
249 | dec eax
|
---|
250 | jnz .more_zeroed_space
|
---|
251 | mov edi, esp ; edi points to trapframe structure.
|
---|
252 |
|
---|
253 | ; Copy stuff from the stack over.
|
---|
254 | mov eax, [ebp + 8]
|
---|
255 | ;; @todo Do voodoo checks for 'int xx' or misguided hardware interrupts.
|
---|
256 | mov [edi + BS3TRAPFRAME.uErrCd], eax
|
---|
257 | mov al, [ebp + 4]
|
---|
258 | mov [edi + BS3TRAPFRAME.bXcpt], al
|
---|
259 | mov eax, [ebp]
|
---|
260 | mov [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.rbp], eax
|
---|
261 | mov eax, [ebp - 04h]
|
---|
262 | mov [edi + BS3TRAPFRAME.fHandlerRfl], eax
|
---|
263 | mov eax, [ebp - 08h]
|
---|
264 | mov [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.rax], eax
|
---|
265 | mov eax, [ebp - 0ch]
|
---|
266 | mov [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.rdi], eax
|
---|
267 | mov ax, [ebp - 10h]
|
---|
268 | mov [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.ss], ax
|
---|
269 | mov [edi + BS3TRAPFRAME.uHandlerSs], ax
|
---|
270 | mov ax, [ebp - 14h]
|
---|
271 | mov [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.ds], ax
|
---|
272 |
|
---|
273 | lea ebp, [ebp + 8] ; iret - 4 (i.e. ebp frame chain location)
|
---|
274 | jmp bs3Trap32GenericCommon
|
---|
275 | BS3_PROC_END bs3Trap32GenericTrapErrCode
|
---|
276 |
|
---|
277 |
|
---|
278 | ;;
|
---|
279 | ; Common context saving code and dispatching.
|
---|
280 | ;
|
---|
281 | ; @param edi Pointer to the trap frame. The following members have been
|
---|
282 | ; filled in by the previous code:
|
---|
283 | ; - bXcpt
|
---|
284 | ; - uErrCd
|
---|
285 | ; - fHandlerRFL
|
---|
286 | ; - uHandlerSs
|
---|
287 | ; - Ctx.rax (except upper dword)
|
---|
288 | ; - Ctx.rbp (except upper dword)
|
---|
289 | ; - Ctx.rdi (except upper dword)
|
---|
290 | ; - Ctx.ds
|
---|
291 | ; - Ctx.ss
|
---|
292 | ;
|
---|
293 | ; @param ebp Pointer to the dword before the iret frame, i.e. where ebp
|
---|
294 | ; would be saved if this was a normal call.
|
---|
295 | ;
|
---|
296 | BS3_PROC_BEGIN bs3Trap32GenericCommon
|
---|
297 | ;
|
---|
298 | ; Fake EBP frame.
|
---|
299 | ;
|
---|
300 | mov eax, [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.rbp]
|
---|
301 | mov [ebp], eax
|
---|
302 |
|
---|
303 | ;
|
---|
304 | ; Save the remaining GPRs and segment registers.
|
---|
305 | ;
|
---|
306 | mov [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.rcx], ecx
|
---|
307 | mov [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.rdx], edx
|
---|
308 | mov [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.rbx], ebx
|
---|
309 | mov [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.rsi], esi
|
---|
310 | mov [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.es], es
|
---|
311 | mov [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.fs], fs
|
---|
312 | mov [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.gs], gs
|
---|
313 |
|
---|
314 | ;
|
---|
315 | ; Load 32-bit data selector for the DPL we're executing at into DS and ES.
|
---|
316 | ; Save the handler CS value first.
|
---|
317 | ;
|
---|
318 | mov ax, cs
|
---|
319 | mov [edi + BS3TRAPFRAME.uHandlerCs], ax
|
---|
320 | and ax, 3
|
---|
321 | mov cx, ax
|
---|
322 | shl ax, BS3_SEL_RING_SHIFT
|
---|
323 | or ax, cx
|
---|
324 | add ax, BS3_SEL_R0_DS32
|
---|
325 | mov ds, ax
|
---|
326 | mov es, ax
|
---|
327 |
|
---|
328 | ;
|
---|
329 | ; Copy and update the mode now that we've got a flat DS.
|
---|
330 | ;
|
---|
331 | mov al, [BS3_DATA16_WRT(g_bBs3CurrentMode)]
|
---|
332 | mov [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.bMode], al
|
---|
333 | and al, ~BS3_MODE_CODE_MASK
|
---|
334 | or al, BS3_MODE_CODE_32
|
---|
335 | mov [BS3_DATA16_WRT(g_bBs3CurrentMode)], al
|
---|
336 |
|
---|
337 | ;
|
---|
338 | ; Copy iret info.
|
---|
339 | ;
|
---|
340 | mov ecx, [ebp + 4]
|
---|
341 | mov [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.rip], ecx
|
---|
342 | mov ecx, [ebp + 12]
|
---|
343 | mov [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.rflags], ecx
|
---|
344 | mov cx, [ebp + 8]
|
---|
345 | mov [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.cs], cx
|
---|
346 | test dword [ebp + 12], X86_EFL_VM
|
---|
347 | jnz .iret_frame_v8086
|
---|
348 | mov ax, ss
|
---|
349 | and al, 3
|
---|
350 | and cl, 3
|
---|
351 | mov [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.bCpl], cl
|
---|
352 | cmp cl, al
|
---|
353 | je .iret_frame_same_cpl
|
---|
354 |
|
---|
355 | .iret_frame_different_cpl:
|
---|
356 | mov ecx, [ebp + 16]
|
---|
357 | mov [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.rsp], ecx
|
---|
358 | mov cx, [ebp + 20]
|
---|
359 | mov [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.ss], cx
|
---|
360 | lea eax, [ebp + 24]
|
---|
361 | mov [edi + BS3TRAPFRAME.uHandlerRsp], eax
|
---|
362 | jmp .iret_frame_done
|
---|
363 |
|
---|
364 | .iret_frame_same_cpl:
|
---|
365 | lea ecx, [ebp + 16]
|
---|
366 | mov [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.rsp], ecx
|
---|
367 | mov [edi + BS3TRAPFRAME.uHandlerRsp], ecx
|
---|
368 | jmp .iret_frame_done
|
---|
369 |
|
---|
370 | .iret_frame_v8086:
|
---|
371 | mov byte [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.bCpl], 3
|
---|
372 | or byte [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.bMode], BS3_MODE_CODE_V86 ; paranoia ^ 2
|
---|
373 | movzx ecx, word [ebp + 16]
|
---|
374 | mov [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.rsp], ecx
|
---|
375 | mov cx, [ebp + 20]
|
---|
376 | mov [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.ss], cx
|
---|
377 | mov cx, [ebp + 24]
|
---|
378 | mov [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.es], cx
|
---|
379 | mov cx, [ebp + 28]
|
---|
380 | mov [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.ds], cx
|
---|
381 | mov cx, [ebp + 32]
|
---|
382 | mov [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.fs], cx
|
---|
383 | mov cx, [ebp + 36]
|
---|
384 | mov [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.gs], cx
|
---|
385 | lea eax, [ebp + 40]
|
---|
386 | mov [edi + BS3TRAPFRAME.uHandlerRsp], eax
|
---|
387 | jmp .iret_frame_done
|
---|
388 |
|
---|
389 | .iret_frame_done:
|
---|
390 | ;
|
---|
391 | ; Control registers.
|
---|
392 | ;
|
---|
393 | mov eax, cr0
|
---|
394 | mov [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.cr0], eax
|
---|
395 | mov eax, cr2
|
---|
396 | mov [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.cr2], eax
|
---|
397 | mov eax, cr3
|
---|
398 | mov [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.cr3], eax
|
---|
399 | mov eax, cr4
|
---|
400 | mov [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.cr4], eax
|
---|
401 | str ax
|
---|
402 | mov [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.tr], ax
|
---|
403 | sldt ax
|
---|
404 | mov [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.ldtr], ax
|
---|
405 |
|
---|
406 | ;
|
---|
407 | ; Dispatch it to C code.
|
---|
408 | ;
|
---|
409 | .dispatch_to_handler: ; The double fault code joins us here.
|
---|
410 | movzx ebx, byte [edi + BS3TRAPFRAME.bXcpt]
|
---|
411 | mov eax, [ebx * 4 + BS3_DATA16_WRT(_g_apfnBs3TrapHandlers_c32)]
|
---|
412 | or eax, eax
|
---|
413 | jnz .call_handler
|
---|
414 | mov eax, Bs3TrapDefaultHandler
|
---|
415 | .call_handler:
|
---|
416 | push edi
|
---|
417 | call eax
|
---|
418 |
|
---|
419 | ;
|
---|
420 | ; Resume execution using trap frame.
|
---|
421 | ;
|
---|
422 | push 0
|
---|
423 | add edi, BS3TRAPFRAME.Ctx
|
---|
424 | push edi
|
---|
425 | call Bs3RegCtxRestore
|
---|
426 | .panic:
|
---|
427 | hlt
|
---|
428 | jmp .panic
|
---|
429 | BS3_PROC_END bs3Trap32GenericCommon
|
---|
430 |
|
---|
431 |
|
---|
432 | ;;
|
---|
433 | ; Helper.
|
---|
434 | ;
|
---|
435 | ; @retruns Flat address in eax.
|
---|
436 | ; @param ax
|
---|
437 | ; @uses eax
|
---|
438 | ;
|
---|
439 | bs3Trap32TssInAxToFlatInEax:
|
---|
440 | ; Get the GDT base address and find the descriptor address (EAX)
|
---|
441 | sub esp, 8+2
|
---|
442 | sgdt [esp]
|
---|
443 | and eax, 0fff8h
|
---|
444 | add eax, [esp + 2] ; GDT base address.
|
---|
445 | add esp, 8+2
|
---|
446 |
|
---|
447 | ; Get the flat TSS address from the descriptor.
|
---|
448 | mov al, [eax + (X86DESCGENERIC_BIT_OFF_BASE_HIGH1 / 8)]
|
---|
449 | mov ah, [eax + (X86DESCGENERIC_BIT_OFF_BASE_HIGH2 / 8)]
|
---|
450 | shl eax, 16
|
---|
451 | mov ax, [eax + (X86DESCGENERIC_BIT_OFF_BASE_LOW / 8)]
|
---|
452 | ret
|
---|
453 |
|
---|
454 | ;;
|
---|
455 | ; Double fault handler.
|
---|
456 | ;
|
---|
457 | ; We don't have to load any selectors or clear anything in EFLAGS because the
|
---|
458 | ; TSS specified sane values which got loaded during the task switch.
|
---|
459 | ;
|
---|
460 | BS3_PROC_BEGIN Bs3Trap32DoubleFaultHandler
|
---|
461 | push 0 ; We'll copy the rip from the other TSS here later to create a more sensible call chain.
|
---|
462 | push ebp
|
---|
463 | mov ebp, esp
|
---|
464 |
|
---|
465 | pushfd ; Get handler flags.
|
---|
466 | pop ecx
|
---|
467 |
|
---|
468 | xor edx, edx ; NULL register.
|
---|
469 |
|
---|
470 | ;
|
---|
471 | ; Allocate a zero filled trap frame.
|
---|
472 | ;
|
---|
473 | mov eax, (BS3TRAPFRAME_size + 7) / 8
|
---|
474 | AssertCompileSizeAlignment(BS3TRAPFRAME, 8)
|
---|
475 | .more_zeroed_space:
|
---|
476 | push edx
|
---|
477 | push edx
|
---|
478 | dec eax
|
---|
479 | jz .more_zeroed_space
|
---|
480 | mov edi, esp
|
---|
481 |
|
---|
482 | ;
|
---|
483 | ; Fill in the non-context trap frame bits.
|
---|
484 | ;
|
---|
485 | mov [edi + BS3TRAPFRAME.fHandlerRfl], ecx
|
---|
486 | mov word [edi + BS3TRAPFRAME.bXcpt], X86_XCPT_DF
|
---|
487 | mov [edi + BS3TRAPFRAME.uHandlerCs], cs
|
---|
488 | mov [edi + BS3TRAPFRAME.uHandlerSs], ss
|
---|
489 | lea ecx, [ebp + 12]
|
---|
490 | mov [edi + BS3TRAPFRAME.uHandlerRsp], ecx
|
---|
491 | mov ecx, [ebp + 8]
|
---|
492 | mov [edi + BS3TRAPFRAME.uErrCd], ecx
|
---|
493 |
|
---|
494 | ;
|
---|
495 | ; Copy the register state from the previous task segment.
|
---|
496 | ;
|
---|
497 |
|
---|
498 | ; Find our TSS.
|
---|
499 | str ax
|
---|
500 | call bs3Trap32TssInAxToFlatInEax
|
---|
501 |
|
---|
502 | ; Find the previous TSS.
|
---|
503 | mov ax, [eax + X86TSS32.selPrev]
|
---|
504 | mov [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.tr], ax
|
---|
505 | call bs3Trap32TssInAxToFlatInEax
|
---|
506 |
|
---|
507 | ; Do the copying.
|
---|
508 | mov ecx, [eax + X86TSS32.eax]
|
---|
509 | mov [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.rax], ecx
|
---|
510 | mov ecx, [eax + X86TSS32.ecx]
|
---|
511 | mov [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.rcx], ecx
|
---|
512 | mov ecx, [eax + X86TSS32.edx]
|
---|
513 | mov [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.rdx], ecx
|
---|
514 | mov ecx, [eax + X86TSS32.ebx]
|
---|
515 | mov [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.rbx], ecx
|
---|
516 | mov ecx, [eax + X86TSS32.esp]
|
---|
517 | mov [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.rsp], ecx
|
---|
518 | mov ecx, [eax + X86TSS32.ebp]
|
---|
519 | mov [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.rbp], ecx
|
---|
520 | mov [ebp], ecx ; For better call stacks.
|
---|
521 | mov ecx, [eax + X86TSS32.esi]
|
---|
522 | mov [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.rsi], ecx
|
---|
523 | mov ecx, [eax + X86TSS32.edi]
|
---|
524 | mov [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.rdi], ecx
|
---|
525 | mov ecx, [eax + X86TSS32.esi]
|
---|
526 | mov [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.rsi], ecx
|
---|
527 | mov ecx, [eax + X86TSS32.eflags]
|
---|
528 | mov [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.rflags], ecx
|
---|
529 | mov ecx, [eax + X86TSS32.eip]
|
---|
530 | mov [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.rip], ecx
|
---|
531 | mov [ebp + 4], ecx ; For better call stacks.
|
---|
532 | mov cx, [eax + X86TSS32.cs]
|
---|
533 | mov [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.cs], cx
|
---|
534 | mov cx, [eax + X86TSS32.ds]
|
---|
535 | mov [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.ds], cx
|
---|
536 | mov cx, [eax + X86TSS32.es]
|
---|
537 | mov [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.es], cx
|
---|
538 | mov cx, [eax + X86TSS32.fs]
|
---|
539 | mov [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.fs], cx
|
---|
540 | mov cx, [eax + X86TSS32.gs]
|
---|
541 | mov [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.gs], cx
|
---|
542 | mov cx, [eax + X86TSS32.ss]
|
---|
543 | mov [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.ss], cx
|
---|
544 | mov cx, [eax + X86TSS32.selLdt] ; Note! This isn't necessarily the ldtr at the time of the fault.
|
---|
545 | mov [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.ldtr], cx
|
---|
546 | mov cx, [eax + X86TSS32.cr3] ; Note! This isn't necessarily the cr3 at the time of the fault.
|
---|
547 | mov [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.cr3], ecx
|
---|
548 |
|
---|
549 | ;
|
---|
550 | ; Set CPL; copy and update mode.
|
---|
551 | ;
|
---|
552 | mov cl, [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.ss]
|
---|
553 | and cl, 3
|
---|
554 | mov [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.bCpl], cl
|
---|
555 |
|
---|
556 | mov cl, [BS3_DATA16_WRT(g_bBs3CurrentMode)]
|
---|
557 | mov [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.bMode], cl
|
---|
558 | and cl, ~BS3_MODE_CODE_MASK
|
---|
559 | or cl, BS3_MODE_CODE_32
|
---|
560 | mov [BS3_DATA16_WRT(g_bBs3CurrentMode)], cl
|
---|
561 |
|
---|
562 | ;
|
---|
563 | ; Control registers.
|
---|
564 | ;
|
---|
565 | mov ecx, cr0
|
---|
566 | mov [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.cr0], ecx
|
---|
567 | mov ecx, cr2
|
---|
568 | mov [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.cr2], ecx
|
---|
569 | mov ecx, cr4
|
---|
570 | mov [edi + BS3TRAPFRAME.Ctx + BS3REGCTX.cr4], ecx
|
---|
571 |
|
---|
572 | ;
|
---|
573 | ; Join code paths with the generic handler code.
|
---|
574 | ;
|
---|
575 | jmp bs3Trap32GenericCommon.dispatch_to_handler
|
---|
576 | BS3_PROC_END Bs3Trap32DoubleFaultHandler
|
---|
577 |
|
---|