VirtualBox

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

Last change on this file since 60462 was 60439, checked in by vboxsync, 9 years ago

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

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