1 | ; $Id: bs3-cpu-state64-1-asm.asm 96407 2022-08-22 17:43:14Z vboxsync $
|
---|
2 | ;; @file
|
---|
3 | ; BS3Kit - bs3-cpu-state64-1
|
---|
4 | ;
|
---|
5 |
|
---|
6 | ;
|
---|
7 | ; Copyright (C) 2007-2022 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 | ;*********************************************************************************************************************************
|
---|
39 | ;* Header Files *
|
---|
40 | ;*********************************************************************************************************************************
|
---|
41 | %include "bs3kit.mac"
|
---|
42 |
|
---|
43 |
|
---|
44 | ;*********************************************************************************************************************************
|
---|
45 | ;* Global Variables *
|
---|
46 | ;*********************************************************************************************************************************
|
---|
47 | BS3_BEGIN_DATA16
|
---|
48 | BS3_GLOBAL_DATA g_bs3CpuState64CtxCaller, BS3REGCTX_size
|
---|
49 | resb BS3REGCTX_size
|
---|
50 | BS3_GLOBAL_DATA g_bs3CpuState64CtxToLoad, BS3REGCTX_size
|
---|
51 | resb BS3REGCTX_size
|
---|
52 | BS3_GLOBAL_DATA g_bs3CpuState64CtxSaved, BS3REGCTX_size
|
---|
53 | resb BS3REGCTX_size
|
---|
54 |
|
---|
55 | BS3_GLOBAL_DATA g_bs3CpuState64RCX, 8
|
---|
56 | dq 1
|
---|
57 |
|
---|
58 |
|
---|
59 | ;*********************************************************************************************************************************
|
---|
60 | ;* External Symbols *
|
---|
61 | ;*********************************************************************************************************************************
|
---|
62 | BS3_BEGIN_TEXT64
|
---|
63 | EXTERN Bs3RegCtxRestore_c64
|
---|
64 | EXTERN Bs3RegCtxSave_c64
|
---|
65 |
|
---|
66 |
|
---|
67 | BS3_BEGIN_TEXT64
|
---|
68 | BS3_SET_BITS 64
|
---|
69 |
|
---|
70 | ;;
|
---|
71 | ;; Test worker that switches between 64-bit and 16-bit real mode,
|
---|
72 | ;; only trashing RAX, BX, DS, RSP (preseved) and RIP.
|
---|
73 | ;;
|
---|
74 | ;; Caller puts the state to load in g_bs3CpuState64CtxToLoad, this function alters
|
---|
75 | ;; the BX and RIP values before loading it. It then switches to 16-bit real mode,
|
---|
76 | ;; executes the worker given as input, re-enters long mode and saves the state to
|
---|
77 | ;; g_bs3CpuState64CtxSaved.
|
---|
78 | ;;
|
---|
79 | ;; @param rcx Address of worker (16-bit) to invoke while in real-mode.
|
---|
80 | ;;
|
---|
81 | BS3_PROC_BEGIN NAME(bs3CpuState64Worker)
|
---|
82 | push rbp
|
---|
83 | mov rbp, rsp
|
---|
84 | sub rsp, 40h
|
---|
85 | mov [rbp + 16], rcx
|
---|
86 |
|
---|
87 | ;
|
---|
88 | ; Save the current register state so we can return with the exact state we entered.
|
---|
89 | ;
|
---|
90 | lea rcx, [BS3_WRT_RIP(BS3_DATA_NM(g_bs3CpuState64CtxCaller)) wrt FLAT]
|
---|
91 | mov [rsp], rcx
|
---|
92 | call NAME(Bs3RegCtxSave_c64)
|
---|
93 |
|
---|
94 | ;
|
---|
95 | ; Load the context. We modify the state to be loaded so that it fits
|
---|
96 | ; into the code flow here..
|
---|
97 | ;
|
---|
98 | lea rcx, [BS3_WRT_RIP(BS3_DATA_NM(g_bs3CpuState64CtxToLoad)) wrt FLAT]
|
---|
99 | mov [rcx + BS3REGCTX.rsp], rsp
|
---|
100 | ;lea rdx, [BS3_WRT_RIP(.ctx_loaded) wrt FLAT] - absolute address cannot be relative. wtf?
|
---|
101 | mov edx, .ctx_loaded wrt FLAT
|
---|
102 | mov [rcx + BS3REGCTX.rip], rdx
|
---|
103 | mov edx, [rbp + 16] ; Worker address. Putting it in the BX register relative to 16-bit CS.
|
---|
104 | sub edx, BS3_ADDR_BS3TEXT16
|
---|
105 | mov [rcx + BS3REGCTX.rbx], dx
|
---|
106 | mov edx, 0 ; fFlags
|
---|
107 | mov [rsp], rcx
|
---|
108 | mov [rsp + 8], rdx
|
---|
109 | call NAME(Bs3RegCtxRestore_c64)
|
---|
110 | .ctx_loaded:
|
---|
111 |
|
---|
112 | ;
|
---|
113 | ; Disable long mode.
|
---|
114 | ;
|
---|
115 |
|
---|
116 | ; Construct a far return for switching to 16-bit code.
|
---|
117 | push BS3_SEL_R0_CS16
|
---|
118 | push .sixteen_bit_segment wrt CGROUP16
|
---|
119 | xRETF
|
---|
120 | BS3_BEGIN_TEXT16
|
---|
121 | BS3_SET_BITS 16
|
---|
122 | BS3_GLOBAL_LOCAL_LABEL .sixteen_bit_segment
|
---|
123 | ; Make the DS usable from real mode.
|
---|
124 | mov ax, BS3_SEL_R0_DS16
|
---|
125 | mov ds, ax
|
---|
126 |
|
---|
127 | ; Exit to real mode.
|
---|
128 | mov eax, cr0
|
---|
129 | and eax, X86_CR0_NO_PE_NO_PG
|
---|
130 | mov cr0, eax
|
---|
131 | jmp CGROUP16:.reload_cs16
|
---|
132 | BS3_GLOBAL_LOCAL_LABEL .reload_cs16
|
---|
133 |
|
---|
134 | ;
|
---|
135 | ; Jump to the 16-bit worker function that will make state modifications.
|
---|
136 | ;
|
---|
137 | jmp bx
|
---|
138 | BS3_GLOBAL_LOCAL_LABEL .resume16
|
---|
139 |
|
---|
140 | ;
|
---|
141 | ; Re-enter long mode.
|
---|
142 | ;
|
---|
143 | mov eax, cr0
|
---|
144 | or eax, X86_CR0_PE | X86_CR0_PG
|
---|
145 | mov cr0, eax
|
---|
146 | jmp CGROUP16:.reload_cs_long_mode
|
---|
147 | BS3_GLOBAL_LOCAL_LABEL .reload_cs_long_mode
|
---|
148 | ; Construct a far return for switching to 64-bit code.
|
---|
149 | push dword BS3_SEL_R0_CS64
|
---|
150 | push dword .sixtyfour_bit_segment wrt FLAT
|
---|
151 | o32 retf
|
---|
152 | BS3_BEGIN_TEXT64
|
---|
153 | BS3_GLOBAL_LOCAL_LABEL .sixtyfour_bit_segment
|
---|
154 | BS3_SET_BITS 64
|
---|
155 |
|
---|
156 | ;
|
---|
157 | ; We're back in long mode, save the context.
|
---|
158 | ;
|
---|
159 | mov [BS3_WRT_RIP(BS3_DATA_NM(g_bs3CpuState64RCX)) wrt FLAT], rcx
|
---|
160 | lea rcx, [BS3_WRT_RIP(BS3_DATA_NM(g_bs3CpuState64CtxSaved)) wrt FLAT]
|
---|
161 | mov [rsp], rcx
|
---|
162 | call NAME(Bs3RegCtxSave_c64)
|
---|
163 | lea rcx, [BS3_WRT_RIP(BS3_DATA_NM(g_bs3CpuState64CtxSaved)) wrt FLAT]
|
---|
164 | mov rax, [BS3_WRT_RIP(BS3_DATA_NM(g_bs3CpuState64RCX)) wrt FLAT]
|
---|
165 | mov [rcx + BS3REGCTX.rcx], rax
|
---|
166 |
|
---|
167 | ;
|
---|
168 | ; Load the caller's context.
|
---|
169 | ;
|
---|
170 | lea rcx, [BS3_WRT_RIP(BS3_DATA_NM(g_bs3CpuState64CtxCaller)) wrt FLAT]
|
---|
171 | ;lea rdx, [BS3_WRT_RIP(.return_sequence) wrt FLAT] - absolute address cannot be relative. wtf?
|
---|
172 | mov edx, .return_sequence wrt FLAT
|
---|
173 | mov [rcx + BS3REGCTX.rip], rdx
|
---|
174 | mov edx, 0
|
---|
175 | mov [rsp], rcx
|
---|
176 | mov [rsp + 8], rdx
|
---|
177 | call NAME(Bs3RegCtxRestore_c64)
|
---|
178 | .return_sequence:
|
---|
179 |
|
---|
180 | add rsp, 40h
|
---|
181 | pop rbp
|
---|
182 | ret
|
---|
183 | BS3_PROC_END NAME(bs3CpuState64Worker)
|
---|
184 |
|
---|
185 |
|
---|
186 | BS3_BEGIN_TEXT16
|
---|
187 | ;
|
---|
188 | ; Real-mod modification workers for bs3CpuState64Worker.
|
---|
189 | ;
|
---|
190 |
|
---|
191 | BS3_PROC_BEGIN NAME(bs3CpuState64Worker_Nop)
|
---|
192 | nop
|
---|
193 | jmp NAME(bs3CpuState64Worker.resume16)
|
---|
194 | BS3_PROC_END NAME(bs3CpuState64Worker_Nop)
|
---|
195 |
|
---|
196 |
|
---|
197 | BS3_PROC_BEGIN NAME(bs3CpuState64Worker_ModAll32BitGrps)
|
---|
198 | mov eax, 0xc0ffee0d ; C code hardcodes these values too.
|
---|
199 | mov ecx, 0xc0ffee1d
|
---|
200 | mov edx, 0xc0ffee2d
|
---|
201 | mov ebx, 0xc0ffee3d
|
---|
202 | ; leave esp alone for now.
|
---|
203 | mov ebp, 0xc0ffee5d
|
---|
204 | mov esi, 0xc0ffee6d
|
---|
205 | mov edi, 0xc0ffee7d
|
---|
206 | jmp NAME(bs3CpuState64Worker.resume16)
|
---|
207 | BS3_PROC_END NAME(bs3CpuState64Worker_ModAll32BitGrps)
|
---|
208 |
|
---|
209 |
|
---|
210 | BS3_PROC_BEGIN NAME(bs3CpuState64Worker_ModAll16BitGrps)
|
---|
211 | mov ax, 0xfad0 ; C code hardcodes these values too.
|
---|
212 | mov cx, 0xfad1
|
---|
213 | mov dx, 0xfad2
|
---|
214 | mov bx, 0xfad3
|
---|
215 | ; leave esp alone for now.
|
---|
216 | mov bp, 0xfad5
|
---|
217 | mov si, 0xfad6
|
---|
218 | mov di, 0xfad7
|
---|
219 | jmp NAME(bs3CpuState64Worker.resume16)
|
---|
220 | BS3_PROC_END NAME(bs3CpuState64Worker_ModAll16BitGrps)
|
---|
221 |
|
---|
222 |
|
---|
223 | BS3_PROC_BEGIN NAME(bs3CpuState64Worker_ModAll8BitGrps)
|
---|
224 | mov al, 0x10 ; C code hardcodes these values too.
|
---|
225 | mov ah, 0x11
|
---|
226 | mov cl, 0x20
|
---|
227 | mov ch, 0x21
|
---|
228 | mov dl, 0x30
|
---|
229 | mov dh, 0x31
|
---|
230 | mov bl, 0x40
|
---|
231 | mov bh, 0x41
|
---|
232 | jmp NAME(bs3CpuState64Worker.resume16)
|
---|
233 | BS3_PROC_END NAME(bs3CpuState64Worker_ModAll8BitGrps)
|
---|
234 |
|
---|
235 | BS3_PROC_BEGIN NAME(bs3CpuState64Worker_ModCr2)
|
---|
236 | mov eax, 0xf00dface ; C code hardcodes this value too.
|
---|
237 | mov cr2, eax
|
---|
238 | jmp NAME(bs3CpuState64Worker.resume16)
|
---|
239 | BS3_PROC_END NAME(bs3CpuState64Worker_ModCr2)
|
---|
240 |
|
---|
241 | ;; @todo drX registers.
|
---|
242 |
|
---|