1 | ; $Id: bs3-mode-SwitchToPE32.asm 64694 2016-11-17 17:10:47Z vboxsync $
|
---|
2 | ;; @file
|
---|
3 | ; BS3Kit - Bs3SwitchToPE32
|
---|
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 |
|
---|
30 | ;;
|
---|
31 | ; Switch to 32-bit unpaged protected mode from any other mode.
|
---|
32 | ;
|
---|
33 | ; @cproto BS3_DECL(void) Bs3SwitchToPE32(void);
|
---|
34 | ;
|
---|
35 | ; @uses Nothing (except high 32-bit register parts), upper part of ESP is
|
---|
36 | ; cleared if caller is in 16-bit mode.
|
---|
37 | ;
|
---|
38 | ; @remarks Obviously returns to 32-bit mode, even if the caller was
|
---|
39 | ; in 16-bit or 64-bit mode.
|
---|
40 | ;
|
---|
41 | ; @remarks Does not require 20h of parameter scratch space in 64-bit mode.
|
---|
42 | ;
|
---|
43 | BS3_GLOBAL_NAME_EX TMPL_NM(Bs3SwitchToPE32_Safe), function, 0
|
---|
44 | BS3_PROC_BEGIN_MODE Bs3SwitchToPE32, BS3_PBC_NEAR
|
---|
45 | %ifdef TMPL_PE32
|
---|
46 | ret
|
---|
47 |
|
---|
48 | %elif BS3_MODE_IS_V86(TMPL_MODE)
|
---|
49 | ;
|
---|
50 | ; V8086 - Switch to 16-bit ring-0 and call worker for that mode.
|
---|
51 | ;
|
---|
52 | extern BS3_CMN_NM(Bs3SwitchToRing0)
|
---|
53 | call BS3_CMN_NM(Bs3SwitchToRing0)
|
---|
54 | extern %[BS3_MODE_R0_NM_ %+ TMPL_MODE](Bs3SwitchToPE32)
|
---|
55 | jmp %[BS3_MODE_R0_NM_ %+ TMPL_MODE](Bs3SwitchToPE32)
|
---|
56 |
|
---|
57 | %else
|
---|
58 | ;
|
---|
59 | ; Switch to real mode.
|
---|
60 | ;
|
---|
61 | %if TMPL_BITS != 32
|
---|
62 | %if TMPL_BITS > 32
|
---|
63 | shl xPRE [xSP], 32 ; Adjust the return address from 64-bit to 32-bit.
|
---|
64 | add rsp, xCB - 4
|
---|
65 | %else
|
---|
66 | push word 0 ; Reserve space to expand the return address.
|
---|
67 | %endif
|
---|
68 | %endif
|
---|
69 | %if TMPL_BITS != 16
|
---|
70 | ; Must be in 16-bit segment when calling Bs3SwitchTo16Bit.
|
---|
71 | jmp .sixteen_bit_segment
|
---|
72 | BS3_BEGIN_TEXT16
|
---|
73 | BS3_SET_BITS TMPL_BITS
|
---|
74 | BS3_GLOBAL_LOCAL_LABEL .sixteen_bit_segment
|
---|
75 | %endif
|
---|
76 | ;
|
---|
77 | ; Switch to real mode.
|
---|
78 | ;
|
---|
79 | extern TMPL_NM(Bs3SwitchToRM)
|
---|
80 | call TMPL_NM(Bs3SwitchToRM)
|
---|
81 | BS3_SET_BITS 16
|
---|
82 |
|
---|
83 | push eax
|
---|
84 | pushfd
|
---|
85 | cli
|
---|
86 |
|
---|
87 | ;
|
---|
88 | ; Load the GDT and enable PE32.
|
---|
89 | ;
|
---|
90 | BS3_EXTERN_SYSTEM16 Bs3LgdtDef_Gdt
|
---|
91 | BS3_EXTERN_SYSTEM16 Bs3Lgdt_Gdt
|
---|
92 | BS3_BEGIN_TEXT16
|
---|
93 | mov ax, BS3SYSTEM16
|
---|
94 | mov ds, ax
|
---|
95 | lgdt [Bs3LgdtDef_Gdt] ; Will only load 24-bit base!
|
---|
96 |
|
---|
97 | mov eax, cr0
|
---|
98 | or eax, X86_CR0_PE
|
---|
99 | mov cr0, eax
|
---|
100 | jmp BS3_SEL_R0_CS32:dword .thirty_two_bit wrt FLAT
|
---|
101 | BS3_BEGIN_TEXT32
|
---|
102 | BS3_GLOBAL_LOCAL_LABEL .thirty_two_bit
|
---|
103 |
|
---|
104 | ;
|
---|
105 | ; Convert the (now) real mode stack pointer to 32-bit flat.
|
---|
106 | ;
|
---|
107 | xor eax, eax
|
---|
108 | mov ax, ss
|
---|
109 | shl eax, 4
|
---|
110 | and esp, 0ffffh
|
---|
111 | add esp, eax
|
---|
112 |
|
---|
113 | mov ax, BS3_SEL_R0_SS32
|
---|
114 | mov ss, ax
|
---|
115 |
|
---|
116 | ;
|
---|
117 | ; Call rountine for doing mode specific setups.
|
---|
118 | ;
|
---|
119 | extern NAME(Bs3EnteredMode_pe32)
|
---|
120 | call NAME(Bs3EnteredMode_pe32)
|
---|
121 |
|
---|
122 | ; Load full 32-bit GDT base address.
|
---|
123 | lgdt [Bs3Lgdt_Gdt wrt FLAT]
|
---|
124 |
|
---|
125 | ;
|
---|
126 | ; Restore eax and flags (IF).
|
---|
127 | ;
|
---|
128 | %if TMPL_BITS < 32
|
---|
129 | and esp, 0ffffh ; Make sure the high word is zero.
|
---|
130 | movzx eax, word [esp + 8 + 2] ; Load return address.
|
---|
131 | add eax, BS3_ADDR_BS3TEXT16 ; Convert it to a flat address.
|
---|
132 | mov [esp + 8], eax ; Store it in the place right for 32-bit returns.
|
---|
133 | %endif
|
---|
134 | popfd
|
---|
135 | pop eax
|
---|
136 | ret
|
---|
137 |
|
---|
138 | %if TMPL_BITS != 32
|
---|
139 | TMPL_BEGIN_TEXT
|
---|
140 | %endif
|
---|
141 | %endif
|
---|
142 | BS3_PROC_END_MODE Bs3SwitchToPE32
|
---|
143 |
|
---|
144 |
|
---|
145 | %if TMPL_BITS == 16
|
---|
146 | ;;
|
---|
147 | ; Custom far stub.
|
---|
148 | BS3_BEGIN_TEXT16_FARSTUBS
|
---|
149 | BS3_PROC_BEGIN_MODE Bs3SwitchToPE32, BS3_PBC_FAR
|
---|
150 | inc bp
|
---|
151 | push bp
|
---|
152 | mov bp, sp
|
---|
153 |
|
---|
154 | ; Call the real thing.
|
---|
155 | call TMPL_NM(Bs3SwitchToPE32)
|
---|
156 | BS3_SET_BITS 32
|
---|
157 |
|
---|
158 | ; Jmp to common code for the tedious conversion.
|
---|
159 | %if BS3_MODE_IS_RM_OR_V86(TMPL_MODE)
|
---|
160 | extern _Bs3SwitchHlpConvRealModeRetfPopBpDecBpAndReturn_c32
|
---|
161 | jmp _Bs3SwitchHlpConvRealModeRetfPopBpDecBpAndReturn_c32
|
---|
162 | %else
|
---|
163 | extern _Bs3SwitchHlpConvProtModeRetfPopBpDecBpAndReturn_c32
|
---|
164 | jmp _Bs3SwitchHlpConvProtModeRetfPopBpDecBpAndReturn_c32
|
---|
165 | %endif
|
---|
166 | BS3_SET_BITS 16
|
---|
167 | BS3_PROC_END_MODE Bs3SwitchToPE32
|
---|
168 | %endif
|
---|
169 |
|
---|