VirtualBox

source: vbox/trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-RegCtxSave.asm@ 60600

Last change on this file since 60600 was 60527, checked in by vboxsync, 9 years ago

bs3kit: Far updates.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.3 KB
Line 
1; $Id: bs3-cmn-RegCtxSave.asm 60527 2016-04-18 09:11:04Z vboxsync $
2;; @file
3; BS3Kit - Bs3RegCtxSave.
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%include "bs3kit-template-header.mac"
28
29
30BS3_EXTERN_SYSTEM16 Bs3Gdt
31BS3_EXTERN_DATA16 g_bBs3CurrentMode
32%if TMPL_BITS != 64
33BS3_EXTERN_DATA16 g_uBs3CpuDetected
34%endif
35TMPL_BEGIN_TEXT
36
37
38
39;;
40; Saves the current register context.
41;
42; @param pRegCtx
43; @uses None.
44;
45BS3_PROC_BEGIN_CMN Bs3RegCtxSave, BS3_PBC_HYBRID_SAFE
46 TMPL_ONLY_16BIT_STMT CPU 8086
47 BS3_CALL_CONV_PROLOG 1
48 push xBP
49 mov xBP, xSP
50 xPUSHF ; xBP - xCB*1: save the incoming flags exactly.
51 push xAX ; xBP - xCB*2: save incoming xAX
52 push xCX ; xBP - xCB*3: save incoming xCX
53 push xDI ; xBP - xCB*4: save incoming xDI
54 BS3_ONLY_16BIT_STMT push es ; xBP - xCB*5
55 BS3_ONLY_16BIT_STMT push ds ; xBP - xCB*6
56
57 ;
58 ; Clear the whole structure first.
59 ;
60 xor xAX, xAX
61 cld
62 AssertCompileSizeAlignment(BS3REGCTX, 4)
63%if TMPL_BITS == 16
64 les xDI, [xBP + xCB + cbCurRetAddr]
65 mov xCX, BS3REGCTX_size / 2
66 rep stosw
67%else
68 mov xDI, [xBP + xCB + cbCurRetAddr]
69 mov xCX, BS3REGCTX_size / 4
70 rep stosd
71%endif
72 mov xDI, [xBP + xCB + cbCurRetAddr]
73
74 ;
75 ; Save the current mode.
76 ;
77 mov cl, [BS3_DATA16_WRT(g_bBs3CurrentMode)]
78 mov [BS3_ONLY_16BIT(es:) xDI + BS3REGCTX.bMode], cl
79%if TMPL_BITS == 16
80
81 ;
82 ; In 16-bit mode we could be running on really ancient CPUs, so check
83 ; mode and detected CPU and proceed with care.
84 ;
85 cmp cl, BS3_MODE_PP16
86 jae .save_full
87
88 mov cl, [BS3_DATA16_WRT(g_uBs3CpuDetected)]
89 cmp cl, BS3CPU_80386
90 jae .save_full
91
92 ; load ES into DS so we can save some segment prefix bytes.
93 push es
94 pop ds
95
96 ; 16-bit GPRs not on the stack.
97 mov [xDI + BS3REGCTX.rdx], dx
98 mov [xDI + BS3REGCTX.rbx], bx
99 mov [xDI + BS3REGCTX.rsi], si
100
101 ; Join the common code.
102 cmp cl, BS3CPU_80286
103 jb .common_ancient
104 CPU 286
105
106 smsw [xDI + BS3REGCTX.cr0]
107 jmp .common_80286
108
109 CPU 386
110%endif
111
112
113.save_full:
114 ;
115 ; 80386 or later.
116 ;
117%if TMPL_BITS != 64
118 ; Check for CR4 here while we've got a working DS in all contexts.
119 test byte [1 + BS3_DATA16_WRT(g_uBs3CpuDetected)], (BS3CPU_F_CPUID >> 8)
120 jnz .save_full_have_cr4
121 or byte [BS3_ONLY_16BIT(es:) xDI + BS3REGCTX.fbFlags], BS3REG_CTX_F_NO_CR4
122.save_full_have_cr4:
123%endif
124%if TMPL_BITS == 16
125 ; Load es into ds so we can save ourselves some segment prefix bytes.
126 push es
127 pop ds
128%endif
129
130 ; GPRs first.
131 mov [xDI + BS3REGCTX.rdx], sDX
132 mov [xDI + BS3REGCTX.rbx], sBX
133 mov [xDI + BS3REGCTX.rsi], sSI
134%if TMPL_BITS == 64
135 mov [xDI + BS3REGCTX.r8], r8
136 mov [xDI + BS3REGCTX.r9], r9
137 mov [xDI + BS3REGCTX.r10], r10
138 mov [xDI + BS3REGCTX.r11], r11
139 mov [xDI + BS3REGCTX.r12], r12
140 mov [xDI + BS3REGCTX.r13], r13
141 mov [xDI + BS3REGCTX.r14], r14
142 mov [xDI + BS3REGCTX.r15], r15
143%else
144 or byte [xDI + BS3REGCTX.fbFlags], BS3REG_CTX_F_NO_AMD64
145%endif
146%if TMPL_BITS == 16 ; Save high bits.
147 mov [xDI + BS3REGCTX.rax], eax
148 mov [xDI + BS3REGCTX.rcx], ecx
149 mov [xDI + BS3REGCTX.rdi], edi
150 mov [xDI + BS3REGCTX.rbp], ebp
151 mov [xDI + BS3REGCTX.rsp], esp
152 pushfd
153 pop dword [xDI + BS3REGCTX.rflags]
154%endif
155
156 ; 386 segment registers.
157 mov [xDI + BS3REGCTX.fs], fs
158 mov [xDI + BS3REGCTX.gs], gs
159
160%if TMPL_BITS == 16 ; v8086 and real mode woes.
161 mov cl, [xDI + BS3REGCTX.bMode]
162 cmp cl, BS3_MODE_RM
163 je .common_full_control_regs
164 test cl, BS3_MODE_CODE_V86
165 jnz .common_full_no_control_regs
166%endif
167 mov ax, ss
168 test al, 3
169 jnz .common_full_no_control_regs
170
171 ; Control registers (ring-0 and real-mode only).
172.common_full_control_regs:
173 mov sAX, cr0
174 mov [xDI + BS3REGCTX.cr0], sAX
175 mov sAX, cr2
176 mov [xDI + BS3REGCTX.cr2], sAX
177 mov sAX, cr3
178 mov [xDI + BS3REGCTX.cr3], sAX
179%if TMPL_BITS != 64
180 test byte [xDI + BS3REGCTX.fbFlags], BS3REG_CTX_F_NO_CR4
181 jnz .common_80286
182%endif
183 mov sAX, cr4
184 mov [xDI + BS3REGCTX.cr4], sAX
185 jmp .common_80286
186
187.common_full_no_control_regs:
188 or byte [xDI + BS3REGCTX.fbFlags], BS3REG_CTX_F_NO_CR
189
190 ; 80286 control registers.
191.common_80286:
192 TMPL_ONLY_16BIT_STMT CPU 286
193 cmp cl, BS3_MODE_RM
194 je .common_ancient
195 str [xDI + BS3REGCTX.tr]
196 sldt [xDI + BS3REGCTX.ldtr]
197
198 ; Common stuff - stuff on the stack, 286 segment registers.
199.common_ancient:
200 TMPL_ONLY_16BIT_STMT CPU 8086
201 mov xAX, [xBP - xCB*1]
202 mov [xDI + BS3REGCTX.rflags], xAX
203 mov xAX, [xBP - xCB*2]
204 mov [xDI + BS3REGCTX.rax], xAX
205 mov xAX, [xBP - xCB*3]
206 mov [xDI + BS3REGCTX.rcx], xAX
207 mov xAX, [xBP - xCB*4]
208 mov [xDI + BS3REGCTX.rdi], xAX
209 mov xAX, [xBP]
210 mov [xDI + BS3REGCTX.rbp], xAX
211 mov xAX, [xBP + xCB]
212 mov [xDI + BS3REGCTX.rip], xAX
213 lea xAX, [xBP + xCB + cbCurRetAddr]
214 mov [xDI + BS3REGCTX.rsp], xAX
215
216%if TMPL_BITS == 16
217 mov ax, [xBP + xCB + 2]
218 mov [xDI + BS3REGCTX.cs], ax
219 mov ax, [xBP - xCB*6]
220 mov [xDI + BS3REGCTX.ds], ax
221 mov ax, [xBP - xCB*5]
222 mov [xDI + BS3REGCTX.es], ax
223%else
224 mov [xDI + BS3REGCTX.cs], cs
225 mov [xDI + BS3REGCTX.ds], ds
226 mov [xDI + BS3REGCTX.es], es
227%endif
228 mov [xDI + BS3REGCTX.ss], ss
229
230 ;
231 ; Return.
232 ;
233.return:
234 BS3_ONLY_16BIT_STMT pop ds
235 BS3_ONLY_16BIT_STMT pop es
236 pop xDI
237 pop xCX
238 pop xAX
239 xPOPF
240 pop xBP
241 BS3_HYBRID_RET
242BS3_PROC_END_CMN Bs3RegCtxSave
243
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette