VirtualBox

source: vbox/trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-mode-SwitchToPE16.asm@ 66112

Last change on this file since 66112 was 64694, checked in by vboxsync, 8 years ago

bs3kit: Added Bs3SwitchTo32BitAndCallC and fixed a few problems switching from PE32 to other mode and back again.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.3 KB
Line 
1; $Id: bs3-mode-SwitchToPE16.asm 64694 2016-11-17 17:10:47Z vboxsync $
2;; @file
3; BS3Kit - Bs3SwitchToPE16
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 16-bit unpaged protected mode from any other mode.
32;
33; @cproto BS3_DECL(void) Bs3SwitchToPE16(void);
34;
35; @uses Nothing (except high 32-bit register parts).
36;
37; @remarks Obviously returns to 16-bit mode, even if the caller was
38; in 32-bit or 64-bit mode.
39;
40; @remarks Does not require 20h of parameter scratch space in 64-bit mode.
41;
42%if TMPL_BITS == 16
43BS3_GLOBAL_NAME_EX TMPL_NM(Bs3SwitchToPE16_Safe), function , 0
44%endif
45BS3_PROC_BEGIN_MODE Bs3SwitchToPE16, BS3_PBC_NEAR
46%ifdef TMPL_PE16
47 extern BS3_CMN_NM(Bs3SwitchToRing0)
48 call BS3_CMN_NM(Bs3SwitchToRing0)
49 push ax
50 mov ax, BS3_SEL_R0_DS16
51 mov ds, ax
52 mov es, ax
53 pop ax
54 ret
55
56%elif BS3_MODE_IS_V86(TMPL_MODE)
57 ;
58 ; V8086 - Switch to 16-bit ring-0 and call worker for that mode.
59 ;
60 extern BS3_CMN_NM(Bs3SwitchToRing0)
61 call BS3_CMN_NM(Bs3SwitchToRing0)
62 extern %[BS3_MODE_R0_NM_ %+ TMPL_MODE](Bs3SwitchToPE16)
63 jmp %[BS3_MODE_R0_NM_ %+ TMPL_MODE](Bs3SwitchToPE16)
64
65%else
66 ;
67 ; Switch to 16-bit mode and prepare for returning in 16-bit mode.
68 ;
69 %if TMPL_BITS != 16
70 shl xPRE [xSP], TMPL_BITS - 16 ; Adjust the return address.
71 add xSP, xCB - 2
72
73 ; Must be in 16-bit segment when calling Bs3SwitchTo16Bit.
74 jmp .sixteen_bit_segment
75BS3_BEGIN_TEXT16
76 BS3_SET_BITS TMPL_BITS
77BS3_GLOBAL_LOCAL_LABEL .sixteen_bit_segment
78 %endif
79
80 ;
81 ; Switch to real mode.
82 ;
83 extern TMPL_NM(Bs3SwitchToRM)
84 call TMPL_NM(Bs3SwitchToRM)
85 BS3_SET_BITS 16
86
87 push ax
88 push cx
89 pushf
90 cli
91
92 ;
93 ; Load the GDT and enable PE16.
94 ;
95BS3_EXTERN_SYSTEM16 Bs3Lgdt_Gdt
96BS3_EXTERN_SYSTEM16 Bs3LgdtDef_Gdt
97BS3_BEGIN_TEXT16
98 mov ax, BS3SYSTEM16
99 mov ds, ax
100 lgdt [Bs3LgdtDef_Gdt] ; Will only load 24-bit base!
101
102 smsw ax
103 or ax, X86_CR0_PE
104 lmsw ax
105
106 ;
107 ; Convert from real mode stack to protected mode stack.
108 ;
109 mov ax, .p16_stack
110 extern NAME(Bs3ConvertRMStackToP16UsingCxReturnToAx_c16)
111 jmp NAME(Bs3ConvertRMStackToP16UsingCxReturnToAx_c16)
112.p16_stack:
113
114 ;
115 ; Call routine for doing mode specific setups.
116 ;
117 extern NAME(Bs3EnteredMode_pe16)
118 call NAME(Bs3EnteredMode_pe16)
119
120 ;
121 ; Load full 32-bit GDT base address from 32-bit segment, if 386+ CPU.
122 ;
123 BS3_EXTERN_DATA16 g_uBs3CpuDetected
124 BS3_BEGIN_TEXT16
125 cmp byte [g_uBs3CpuDetected], BS3CPU_80386
126 jb .old_cpu_skip_32bit_lgdt
127 push ds
128 mov ax, BS3_SEL_SYSTEM16
129 mov ds, ax
130 jmp dword BS3_SEL_R0_CS32:.load_full_gdt_base wrt FLAT
131.load_full_gdt_base:
132 BS3_SET_BITS 32
133 lgdt [Bs3Lgdt_Gdt wrt BS3SYSTEM16]
134 jmp BS3_SEL_R0_CS16:.back_to_16bit
135.back_to_16bit:
136 BS3_SET_BITS 16
137 pop ds
138.old_cpu_skip_32bit_lgdt:
139
140 popf
141 pop cx
142 pop ax
143 ret
144
145 %if TMPL_BITS != 16
146TMPL_BEGIN_TEXT
147 %endif
148%endif
149BS3_PROC_END_MODE Bs3SwitchToPE16
150
151
152%if TMPL_BITS == 16
153;;
154; Custom far stub.
155BS3_BEGIN_TEXT16_FARSTUBS
156BS3_PROC_BEGIN_MODE Bs3SwitchToPE16, BS3_PBC_FAR
157 inc bp
158 push bp
159 mov bp, sp
160
161 ; Call the real thing.
162 call TMPL_NM(Bs3SwitchToPE16)
163
164 %if BS3_MODE_IS_RM_OR_V86(TMPL_MODE)
165 ; Jmp to common code for the tedious conversion.
166 BS3_EXTERN_CMN Bs3SwitchHlpConvRealModeRetfPopBpDecBpAndReturn
167 jmp Bs3SwitchHlpConvRealModeRetfPopBpDecBpAndReturn
168 %else
169 pop bp
170 dec bp
171 retf
172 %endif
173BS3_PROC_END_MODE Bs3SwitchToPE16
174
175%else
176;;
177; Safe far return to non-BS3TEXT16 code.
178BS3_EXTERN_CMN Bs3SwitchHlpConvFlatRetToRetfProtMode
179BS3_BEGIN_TEXT16
180BS3_SET_BITS TMPL_BITS
181BS3_PROC_BEGIN_MODE Bs3SwitchToPE16_Safe, BS3_PBC_NEAR
182 call Bs3SwitchHlpConvFlatRetToRetfProtMode ; Special internal function. Uses nothing, but modifies the stack.
183 call TMPL_NM(Bs3SwitchToPE16)
184 BS3_SET_BITS 16
185 retf
186BS3_PROC_END_MODE Bs3SwitchToPE16_Safe
187%endif
188
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette