1 | ; $Id: bs3-cmn-SwitchTo32Bit.asm 106061 2024-09-16 14:03:52Z vboxsync $
|
---|
2 | ;; @file
|
---|
3 | ; BS3Kit - Bs3SwitchTo32Bit
|
---|
4 | ;
|
---|
5 |
|
---|
6 | ;
|
---|
7 | ; Copyright (C) 2007-2024 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 | %include "bs3kit-template-header.mac"
|
---|
38 |
|
---|
39 |
|
---|
40 | %if TMPL_BITS == 16
|
---|
41 | BS3_EXTERN_CMN Bs3SelProtFar32ToFlat32
|
---|
42 | BS3_EXTERN_CMN Bs3Syscall
|
---|
43 | %endif
|
---|
44 | %if TMPL_BITS != 32
|
---|
45 | BS3_EXTERN_DATA16 g_bBs3CurrentMode
|
---|
46 | TMPL_BEGIN_TEXT
|
---|
47 | %endif
|
---|
48 |
|
---|
49 |
|
---|
50 | ;;
|
---|
51 | ; @cproto BS3_DECL(void) Bs3SwitchTo32Bit(void);
|
---|
52 | ;
|
---|
53 | ; @remarks Does not require 20h of parameter scratch space in 64-bit mode.
|
---|
54 | ;
|
---|
55 | BS3_PROC_BEGIN_CMN Bs3SwitchTo32Bit, BS3_PBC_NEAR
|
---|
56 | %if TMPL_BITS == 32
|
---|
57 | ret
|
---|
58 | %else
|
---|
59 | %if TMPL_BITS == 16
|
---|
60 | push ax ; Reserve space for larger return value (adjusted in 32-bit code).
|
---|
61 | push eax
|
---|
62 | pushfd
|
---|
63 | push edx
|
---|
64 | %else
|
---|
65 | pushfq
|
---|
66 | mov [rsp + 4], eax
|
---|
67 | %endif
|
---|
68 | cli
|
---|
69 |
|
---|
70 | %if TMPL_BITS == 16
|
---|
71 | ; Check for v8086 mode, we need to exit it to enter 32-bit mode.
|
---|
72 | mov ax, seg g_bBs3CurrentMode
|
---|
73 | mov ds, ax
|
---|
74 | mov al, [BS3_DATA16_WRT(g_bBs3CurrentMode)]
|
---|
75 | test al, BS3_MODE_CODE_V86
|
---|
76 | jz .not_v8086
|
---|
77 |
|
---|
78 | ; Calc flat stack into edx.
|
---|
79 | mov dx, ss
|
---|
80 | movzx edx, dx
|
---|
81 | shl edx, 4
|
---|
82 | add dx, sp
|
---|
83 | adc edx, 0 ; edx = flat stack address corresponding to ss:sp
|
---|
84 |
|
---|
85 | ; Switch to 16-bit ring0 and go on to do the far jump to 32-bit code.
|
---|
86 | mov ax, BS3_SYSCALL_TO_RING0
|
---|
87 | call Bs3Syscall
|
---|
88 |
|
---|
89 | mov xAX, BS3_SEL_R0_CS32
|
---|
90 | jmp .do_far_jump
|
---|
91 | %endif
|
---|
92 |
|
---|
93 | .not_v8086:
|
---|
94 | %if TMPL_BITS == 16
|
---|
95 | ; Calc flat stack into edx.
|
---|
96 | movzx eax, sp
|
---|
97 | push ecx
|
---|
98 | push ebx
|
---|
99 | push ss
|
---|
100 | push eax
|
---|
101 | call Bs3SelProtFar32ToFlat32
|
---|
102 | add sp, 6
|
---|
103 | shl edx, 16
|
---|
104 | mov dx, ax ; edx = flat stack address corresponding to ss:sp
|
---|
105 | pop ebx
|
---|
106 | pop ecx
|
---|
107 | %endif
|
---|
108 |
|
---|
109 | ; Calc ring addend.
|
---|
110 | mov ax, cs
|
---|
111 | and xAX, 3
|
---|
112 | shl xAX, BS3_SEL_RING_SHIFT
|
---|
113 | add xAX, BS3_SEL_R0_CS32
|
---|
114 |
|
---|
115 | ; Create far return for switching to 32-bit mode.
|
---|
116 | .do_far_jump:
|
---|
117 | push sAX
|
---|
118 | %if TMPL_BITS == 16
|
---|
119 | push dword .thirty_two_bit wrt FLAT
|
---|
120 | o32 retf
|
---|
121 | %else
|
---|
122 | push .thirty_two_bit
|
---|
123 | o64 retf
|
---|
124 | %endif
|
---|
125 |
|
---|
126 | BS3_SET_BITS 32
|
---|
127 | .thirty_two_bit:
|
---|
128 | ; Load 32-bit segment registers.
|
---|
129 | add eax, BS3_SEL_R0_SS32 - BS3_SEL_R0_CS32
|
---|
130 | mov ss, ax
|
---|
131 | %if TMPL_BITS == 16
|
---|
132 | mov esp, edx ; Load flat stack address.
|
---|
133 | %endif
|
---|
134 |
|
---|
135 | add eax, BS3_SEL_R0_DS32 - BS3_SEL_R0_SS32
|
---|
136 | mov ds, ax
|
---|
137 | mov es, ax
|
---|
138 |
|
---|
139 | ; Update globals.
|
---|
140 | and byte [BS3_DATA16_WRT(g_bBs3CurrentMode)], ~BS3_MODE_CODE_MASK
|
---|
141 | or byte [BS3_DATA16_WRT(g_bBs3CurrentMode)], BS3_MODE_CODE_32
|
---|
142 |
|
---|
143 | %if TMPL_BITS == 16
|
---|
144 | ; Adjust the return address.
|
---|
145 | movzx eax, word [esp + 4*3 + 2]
|
---|
146 | add eax, BS3_ADDR_BS3TEXT16
|
---|
147 | mov [esp + 4*3], eax
|
---|
148 | %endif
|
---|
149 |
|
---|
150 | ; Restore and return.
|
---|
151 | %if TMPL_BITS == 16
|
---|
152 | pop edx
|
---|
153 | %endif
|
---|
154 | popfd
|
---|
155 | pop eax
|
---|
156 | TONLY64 ret 4
|
---|
157 | TNOT64 ret
|
---|
158 | %endif
|
---|
159 | BS3_PROC_END_CMN Bs3SwitchTo32Bit
|
---|
160 |
|
---|
161 | ;; @todo far 16-bit variant.
|
---|
162 |
|
---|