1 | ; $Id: bs3-cmn-SwitchTo16Bit.asm 64694 2016-11-17 17:10:47Z vboxsync $
|
---|
2 | ;; @file
|
---|
3 | ; BS3Kit - Bs3SwitchTo16Bit
|
---|
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 | BS3_EXTERN_DATA16 g_bBs3CurrentMode
|
---|
30 | %if TMPL_BITS == 16
|
---|
31 | BS3_EXTERN_CMN Bs3Syscall
|
---|
32 | %endif
|
---|
33 | TMPL_BEGIN_TEXT
|
---|
34 |
|
---|
35 |
|
---|
36 | ;;
|
---|
37 | ; @cproto BS3_DECL(void) Bs3SwitchTo16Bit(void);
|
---|
38 | ; @remarks Does not require 20h of parameter scratch space in 64-bit mode.
|
---|
39 | ;
|
---|
40 | BS3_PROC_BEGIN_CMN Bs3SwitchTo16Bit, BS3_PBC_NEAR
|
---|
41 | %if TMPL_BITS == 16
|
---|
42 | push ax
|
---|
43 | push ds
|
---|
44 |
|
---|
45 | ; Check g_bBs3CurrentMode whether we're in v8086 mode or not.
|
---|
46 | mov al, [BS3_DATA16_WRT(g_bBs3CurrentMode)]
|
---|
47 | test al, BS3_MODE_CODE_V86
|
---|
48 | jz .ret_16bit
|
---|
49 |
|
---|
50 | ; Switch to ring-0 if v8086 mode.
|
---|
51 | mov ax, BS3_SYSCALL_TO_RING0
|
---|
52 | call Bs3Syscall
|
---|
53 |
|
---|
54 | .ret_16bit:
|
---|
55 | pop ds
|
---|
56 | pop ax
|
---|
57 | ret
|
---|
58 |
|
---|
59 | %else
|
---|
60 | push xAX
|
---|
61 | push xBX
|
---|
62 | xPUSHF
|
---|
63 | cli
|
---|
64 |
|
---|
65 | ; Calc new CS.
|
---|
66 | mov ax, cs
|
---|
67 | and xAX, 3
|
---|
68 | shl xAX, BS3_SEL_RING_SHIFT ; ring addend.
|
---|
69 | add xAX, BS3_SEL_R0_CS16
|
---|
70 |
|
---|
71 | ; Construct a far return for switching to 16-bit code.
|
---|
72 | push xAX
|
---|
73 | push .sixteen_bit
|
---|
74 | xRETF
|
---|
75 |
|
---|
76 | BS3_BEGIN_TEXT16
|
---|
77 | BS3_GLOBAL_LOCAL_LABEL .sixteen_bit
|
---|
78 |
|
---|
79 | ; Load 16-bit segment registers.
|
---|
80 | add ax, BS3_SEL_R0_SS16 - BS3_SEL_R0_CS16
|
---|
81 | mov ss, ax
|
---|
82 |
|
---|
83 | add ax, BS3_SEL_R0_DS16 - BS3_SEL_R0_SS16
|
---|
84 | mov ds, ax
|
---|
85 | mov es, ax
|
---|
86 |
|
---|
87 | ; Thunk the stack if necessary.
|
---|
88 | mov ebx, esp
|
---|
89 | shr ebx, 16
|
---|
90 | jz .stack_ok
|
---|
91 | int3 ; This is for later, just remove this int3 once needed.
|
---|
92 | test ax, X86_SEL_RPL
|
---|
93 | jnz .stack_rpl_must_be_0_for_custom_stacks
|
---|
94 | shl bx, X86_SEL_SHIFT
|
---|
95 | add bx, BS3_SEL_TILED
|
---|
96 | mov ss, bx
|
---|
97 | movzx esp, sp
|
---|
98 | .stack_ok:
|
---|
99 |
|
---|
100 | ; Update globals.
|
---|
101 | and byte [BS3_DATA16_WRT(g_bBs3CurrentMode)], ~BS3_MODE_CODE_MASK
|
---|
102 | or byte [BS3_DATA16_WRT(g_bBs3CurrentMode)], BS3_MODE_CODE_16
|
---|
103 |
|
---|
104 | popfd
|
---|
105 | TONLY64 pop ebx
|
---|
106 | pop ebx
|
---|
107 | TONLY64 pop eax
|
---|
108 | pop eax
|
---|
109 | TONLY64 add sp, 4
|
---|
110 | ret (TMPL_BITS - 16) / 8 ; Return and pop 2 or 6 bytes of "parameters" (unused return value)
|
---|
111 |
|
---|
112 | .stack_rpl_must_be_0_for_custom_stacks:
|
---|
113 | int3
|
---|
114 | jmp .stack_rpl_must_be_0_for_custom_stacks
|
---|
115 | TMPL_BEGIN_TEXT
|
---|
116 | %endif
|
---|
117 | BS3_PROC_END_CMN Bs3SwitchTo16Bit
|
---|
118 |
|
---|
119 | ;; @todo far 16-bit variant.
|
---|
120 |
|
---|