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