1 | ; $Id: bs3-mode-SwitchToRM.asm 60000 2016-03-11 19:12:05Z vboxsync $
|
---|
2 | ;; @file
|
---|
3 | ; BS3Kit - Bs3SwitchToRM
|
---|
4 | ;
|
---|
5 |
|
---|
6 | ;
|
---|
7 | ; Copyright (C) 2007-2015 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 | BS3_EXTERN_SYSTEM16 Bs3Gdt
|
---|
30 | %if TMPL_MODE == BS3_MODE_PE16
|
---|
31 | BS3_EXTERN_DATA16 g_uBs3CpuDetected
|
---|
32 | %endif
|
---|
33 |
|
---|
34 | TMPL_BEGIN_TEXT
|
---|
35 |
|
---|
36 |
|
---|
37 | ;;
|
---|
38 | ; Switch to real mode from any other mode.
|
---|
39 | ;
|
---|
40 | ; @cproto BS3_DECL(void) Bs3SwitchToRM(void);
|
---|
41 | ;
|
---|
42 | ; @uses GPRs and EFLAGS are unchanged (except high 32-bit register (AMD64) parts).
|
---|
43 | ; CS is loaded with BS3TEXT16.
|
---|
44 | ; SS:[RE]SP is converted to real mode address.
|
---|
45 | ; DS and ES are loaded with BS3DATA16_GROUP.
|
---|
46 | ; FS and GS are loaded with zero if present.
|
---|
47 | ;
|
---|
48 | ; @remarks Obviously returns to 16-bit mode, even if the caller was
|
---|
49 | ; in 32-bit or 64-bit mode.
|
---|
50 | ;
|
---|
51 | ; @remarks Does not require 20h of parameter scratch space in 64-bit mode.
|
---|
52 | ;
|
---|
53 | BS3_PROC_BEGIN_MODE Bs3SwitchToRM
|
---|
54 | %ifdef TMPL_RM
|
---|
55 | ret
|
---|
56 |
|
---|
57 | %elif BS3_MODE_IS_V86(TMPL_MODE)
|
---|
58 | ;
|
---|
59 | ; V8086 - Switch to 16-bit ring-0 and call worker for that mode.
|
---|
60 | ;
|
---|
61 | extern BS3_CMN_NM(Bs3SwitchToRing0)
|
---|
62 | call BS3_CMN_NM(Bs3SwitchToRing0)
|
---|
63 | extern %[BS3_MODE_R0_NM_ %+ TMPL_MODE](Bs3SwitchToRM)
|
---|
64 | jmp %[BS3_MODE_R0_NM_ %+ TMPL_MODE](Bs3SwitchToRM)
|
---|
65 |
|
---|
66 | %else
|
---|
67 | ;
|
---|
68 | ; Protected mode.
|
---|
69 | ; 80286 requirements for PE16 clutters the code a little.
|
---|
70 | ;
|
---|
71 | %if TMPL_MODE == BS3_MODE_PE16
|
---|
72 | mov ax, BS3_SEL_DATA16
|
---|
73 | mov ds, ax ; Bs3EnterMode_rm will set ds, so no need to preserve it
|
---|
74 | cmp byte [BS3_DATA16_WRT(g_uBs3CpuDetected)], BS3CPU_80286
|
---|
75 | ja .do_386_prologue
|
---|
76 | push ax
|
---|
77 | push bx
|
---|
78 | pushf
|
---|
79 | push word 1
|
---|
80 | jmp .done_prologue
|
---|
81 | %endif
|
---|
82 | .do_386_prologue:
|
---|
83 | push sAX
|
---|
84 | push sBX
|
---|
85 | sPUSHF
|
---|
86 | %if TMPL_MODE == BS3_MODE_PE16
|
---|
87 | push word 0
|
---|
88 | %endif
|
---|
89 | .done_prologue:
|
---|
90 |
|
---|
91 | ;
|
---|
92 | ; Get to 16-bit ring-0 and disable interrupts.
|
---|
93 | ;
|
---|
94 | extern BS3_CMN_NM(Bs3SwitchToRing0)
|
---|
95 | call BS3_CMN_NM(Bs3SwitchToRing0)
|
---|
96 |
|
---|
97 | cli
|
---|
98 |
|
---|
99 | %if TMPL_MODE == BS3_MODE_PE16
|
---|
100 | ;
|
---|
101 | ; On 80286 we must reset the CPU to get back to real mode.
|
---|
102 | ;
|
---|
103 | pop ax
|
---|
104 | push ax
|
---|
105 | test ax, ax
|
---|
106 | jz .is_386_or_better
|
---|
107 | .implement_this_later:
|
---|
108 | int3
|
---|
109 | jmp .implement_this_later
|
---|
110 |
|
---|
111 | jmp .reload_cs
|
---|
112 |
|
---|
113 | %elif TMPL_BITS != 16
|
---|
114 | ;
|
---|
115 | ; Must be in 16-bit segment when calling Bs3SwitchTo16Bit.
|
---|
116 | ;
|
---|
117 | jmp .sixteen_bit_segment wrt FLAT
|
---|
118 | BS3_BEGIN_TEXT16
|
---|
119 | BS3_SET_BITS TMPL_BITS
|
---|
120 | .sixteen_bit_segment:
|
---|
121 |
|
---|
122 | extern BS3_CMN_NM(Bs3SwitchTo16Bit)
|
---|
123 | call BS3_CMN_NM(Bs3SwitchTo16Bit)
|
---|
124 | BS3_SET_BITS 16
|
---|
125 | %endif
|
---|
126 | ;
|
---|
127 | ; Before exiting to real mode we must load sensible selectors into the
|
---|
128 | ; segment registers so the hidden parts (which doesn't get reloaded in
|
---|
129 | ; real mode) are real mode compatible.
|
---|
130 | ;
|
---|
131 | .is_386_or_better:
|
---|
132 | ;; @todo Testcase: Experiment leaving weird stuff in the hidden segment registers.
|
---|
133 | mov ax, BS3_SEL_R0_DS16
|
---|
134 | mov ds, ax
|
---|
135 | mov es, ax
|
---|
136 | mov fs, ax
|
---|
137 | mov gs, ax
|
---|
138 |
|
---|
139 | ;
|
---|
140 | ; Exit to real mode.
|
---|
141 | ;
|
---|
142 | mov eax, cr0
|
---|
143 | and eax, X86_CR0_NO_PE_NO_PG
|
---|
144 | mov cr0, eax
|
---|
145 | jmp BS3TEXT16:.reload_cs
|
---|
146 | .reload_cs:
|
---|
147 |
|
---|
148 | ;
|
---|
149 | ; Convert the stack (now 16-bit prot) to real mode.
|
---|
150 | ;
|
---|
151 | mov ax, BS3_SEL_SYSTEM16
|
---|
152 | mov ds, ax
|
---|
153 | mov bx, ss
|
---|
154 | and bx, X86_SEL_MASK ; ASSUMES GDT stack selector
|
---|
155 | mov al, [bx + 4 + Bs3Gdt]
|
---|
156 | mov ah, [bx + 7 + Bs3Gdt]
|
---|
157 | add sp, [bx + 2 + Bs3Gdt] ; ASSUMES not expand down segment.
|
---|
158 | adc ax, 0
|
---|
159 | %ifdef BS3_STRICT
|
---|
160 | test ax, 0fff0h
|
---|
161 | jz .stack_conv_ok
|
---|
162 | int3
|
---|
163 | .stack_conv_ok:
|
---|
164 | %endif
|
---|
165 | shl ax, 12
|
---|
166 | mov ss, ax
|
---|
167 | %if TMPL_BITS != 16
|
---|
168 | and esp, 0ffffh
|
---|
169 | %endif
|
---|
170 |
|
---|
171 | ;
|
---|
172 | ; Call routine for doing mode specific setups.
|
---|
173 | ;
|
---|
174 | extern NAME(Bs3EnteredMode_rm)
|
---|
175 | call NAME(Bs3EnteredMode_rm)
|
---|
176 |
|
---|
177 | %if TMPL_MODE == BS3_MODE_PE16
|
---|
178 | pop ax
|
---|
179 | test ax, ax
|
---|
180 | jz .do_386_epilogue
|
---|
181 | popf
|
---|
182 | pop bx
|
---|
183 | pop ax
|
---|
184 | pop bp
|
---|
185 | %endif
|
---|
186 | .do_386_epilogue:
|
---|
187 | popfd
|
---|
188 | TMPL_ONLY_64BIT_STMT pop ebx
|
---|
189 | pop ebx
|
---|
190 | TMPL_ONLY_64BIT_STMT pop eax
|
---|
191 | pop eax
|
---|
192 | TMPL_ONLY_64BIT_STMT add sp, 4
|
---|
193 | retn (TMPL_BITS - 16) / 8
|
---|
194 |
|
---|
195 | %if TMPL_BITS != 16
|
---|
196 | TMPL_BEGIN_TEXT
|
---|
197 | %endif
|
---|
198 | %endif
|
---|
199 | BS3_PROC_END_MODE Bs3SwitchToRM
|
---|
200 |
|
---|