1 | ; $Id: bs3-mode-SwitchToRM.asm 59977 2016-03-10 07:58:08Z 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 Nothing (except high 32-bit register parts).
|
---|
43 | ;
|
---|
44 | ; @remarks Obviously returns to 16-bit mode, even if the caller was
|
---|
45 | ; in 32-bit or 64-bit mode.
|
---|
46 | ;
|
---|
47 | ; @remarks Does not require 20h of parameter scratch space in 64-bit mode.
|
---|
48 | ;
|
---|
49 | BS3_PROC_BEGIN_MODE Bs3SwitchToRM
|
---|
50 | %ifdef TMPL_RM
|
---|
51 | ret
|
---|
52 |
|
---|
53 | %elif BS3_MODE_IS_V86(TMPL_MODE)
|
---|
54 | ;
|
---|
55 | ; V8086 - Switch to 16-bit ring-0 and call worker for that mode.
|
---|
56 | ;
|
---|
57 | extern BS3_CMN_NM(Bs3SwitchToRing0)
|
---|
58 | call BS3_CMN_NM(Bs3SwitchToRing0)
|
---|
59 | extern %[BS3_MODE_R0_NM_ %+ TMPL_MODE](Bs3SwitchToRM)
|
---|
60 | jmp %[BS3_MODE_R0_NM_ %+ TMPL_MODE](Bs3SwitchToRM)
|
---|
61 |
|
---|
62 | %else
|
---|
63 | ;
|
---|
64 | ; Protected mode.
|
---|
65 | ; 80286 requirements for PE16 clutters the code a little.
|
---|
66 | ;
|
---|
67 | %if TMPL_MODE == BS3_MODE_PE16
|
---|
68 | mov ax, BS3_SEL_DATA16
|
---|
69 | mov ds, ax ; Bs3EnterMode_rm will set ds, so no need to preserve it
|
---|
70 | cmp byte [BS3_DATA16_WRT(g_uBs3CpuDetected)], BS3CPU_80286
|
---|
71 | ja .do_386_prologue
|
---|
72 | push ax
|
---|
73 | push bx
|
---|
74 | pushf
|
---|
75 | push word 1
|
---|
76 | jmp .done_prologue
|
---|
77 | %endif
|
---|
78 | .do_386_prologue:
|
---|
79 | push sAX
|
---|
80 | push sBX
|
---|
81 | sPUSHF
|
---|
82 | %if TMPL_MODE == BS3_MODE_PE16
|
---|
83 | push word 0
|
---|
84 | %endif
|
---|
85 | .done_prologue:
|
---|
86 |
|
---|
87 | ;
|
---|
88 | ; Get to 16-bit ring-0 and disable interrupts.
|
---|
89 | ;
|
---|
90 | extern BS3_CMN_NM(Bs3SwitchToRing0)
|
---|
91 | call BS3_CMN_NM(Bs3SwitchToRing0)
|
---|
92 |
|
---|
93 | cli
|
---|
94 |
|
---|
95 | %if TMPL_MODE == BS3_MODE_PE16
|
---|
96 | ;
|
---|
97 | ; On 80286 we must reset the CPU to get back to real mode.
|
---|
98 | ;
|
---|
99 | pop ax
|
---|
100 | push ax
|
---|
101 | test ax, ax
|
---|
102 | jz .is_386_or_better
|
---|
103 | .implement_this_later:
|
---|
104 | int3
|
---|
105 | jmp .implement_this_later
|
---|
106 |
|
---|
107 | jmp .reload_cs
|
---|
108 |
|
---|
109 | %elif TMPL_BITS != 16
|
---|
110 | ;
|
---|
111 | ; Must be in 16-bit segment when calling Bs3SwitchTo16Bit.
|
---|
112 | ;
|
---|
113 | jmp .sixteen_bit_segment wrt FLAT
|
---|
114 | BS3_BEGIN_TEXT16
|
---|
115 | BS3_SET_BITS TMPL_BITS
|
---|
116 | .sixteen_bit_segment:
|
---|
117 |
|
---|
118 | extern BS3_CMN_NM(Bs3SwitchTo16Bit)
|
---|
119 | call BS3_CMN_NM(Bs3SwitchTo16Bit)
|
---|
120 | BS3_SET_BITS 16
|
---|
121 | %endif
|
---|
122 |
|
---|
123 | ;
|
---|
124 | ; Exit to real mode.
|
---|
125 | ;
|
---|
126 | .is_386_or_better:
|
---|
127 | mov eax, cr0
|
---|
128 | and eax, X86_CR0_NO_PE_NO_PG
|
---|
129 | mov cr0, eax
|
---|
130 | jmp BS3TEXT16:.reload_cs
|
---|
131 | .reload_cs:
|
---|
132 |
|
---|
133 | ;
|
---|
134 | ; Convert the stack (now 16-bit prot) to real mode.
|
---|
135 | ;
|
---|
136 | mov ax, BS3_SEL_SYSTEM16
|
---|
137 | mov ds, ax
|
---|
138 | mov bx, ss
|
---|
139 | and bx, X86_SEL_MASK ; ASSUMES GDT stack selector
|
---|
140 | mov al, [bx + 4 + Bs3Gdt]
|
---|
141 | mov ah, [bx + 7 + Bs3Gdt]
|
---|
142 | add sp, [bx + 2 + Bs3Gdt] ; ASSUMES not expand down segment.
|
---|
143 | adc ax, 0
|
---|
144 | %ifdef BS3_STRICT
|
---|
145 | test ax, 0fff0h
|
---|
146 | jz .stack_conv_ok
|
---|
147 | int3
|
---|
148 | .stack_conv_ok:
|
---|
149 | %endif
|
---|
150 | shl ax, 12
|
---|
151 | mov ss, ax
|
---|
152 | %if TMPL_BITS != 16
|
---|
153 | and esp, 0ffffh
|
---|
154 | %endif
|
---|
155 |
|
---|
156 | ;
|
---|
157 | ; Call routine for doing mode specific setups.
|
---|
158 | ;
|
---|
159 | extern NAME(Bs3EnteredMode_rm)
|
---|
160 | call NAME(Bs3EnteredMode_rm)
|
---|
161 |
|
---|
162 | %if TMPL_MODE == BS3_MODE_PE16
|
---|
163 | pop ax
|
---|
164 | test ax, ax
|
---|
165 | jz .do_386_epilogue
|
---|
166 | popf
|
---|
167 | pop bx
|
---|
168 | pop ax
|
---|
169 | pop bp
|
---|
170 | %endif
|
---|
171 | .do_386_epilogue:
|
---|
172 | popfd
|
---|
173 | TMPL_ONLY_64BIT_STMT pop ebx
|
---|
174 | pop ebx
|
---|
175 | TMPL_ONLY_64BIT_STMT pop eax
|
---|
176 | pop eax
|
---|
177 | TMPL_ONLY_64BIT_STMT add sp, 4
|
---|
178 | retn (TMPL_BITS - 16) / 8
|
---|
179 |
|
---|
180 | %if TMPL_BITS != 16
|
---|
181 | TMPL_BEGIN_TEXT
|
---|
182 | %endif
|
---|
183 | %endif
|
---|
184 | BS3_PROC_END_MODE Bs3SwitchToRM
|
---|
185 |
|
---|