1 | ; $Id: bs3-cmn-SwitchTo16Bit.asm 60000 2016-03-11 19:12:05Z vboxsync $
|
---|
2 | ;; @file
|
---|
3 | ; BS3Kit - Bs3SwitchTo16Bit
|
---|
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_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
|
---|
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 | and al, BS3_MODE_CODE_MASK
|
---|
48 | cmp al, BS3_MODE_CODE_V86
|
---|
49 | jne .ret_16bit
|
---|
50 |
|
---|
51 | ; Switch to ring-0 if v8086 mode.
|
---|
52 | mov ax, BS3_SYSCALL_TO_RING0
|
---|
53 | call Bs3Syscall
|
---|
54 |
|
---|
55 | .ret_16bit:
|
---|
56 | pop ds
|
---|
57 | pop ax
|
---|
58 | ret
|
---|
59 |
|
---|
60 | %else
|
---|
61 | push xAX
|
---|
62 | push xBX
|
---|
63 | xPUSHF
|
---|
64 | cli
|
---|
65 |
|
---|
66 | ; Calc new CS.
|
---|
67 | mov ax, cs
|
---|
68 | and xAX, 3
|
---|
69 | shl xAX, BS3_SEL_RING_SHIFT ; ring addend.
|
---|
70 | add xAX, BS3_SEL_R0_CS16
|
---|
71 |
|
---|
72 | ; Construct a far return for switching to 16-bit code.
|
---|
73 | push xAX
|
---|
74 | push .sixteen_bit
|
---|
75 | xRETF
|
---|
76 |
|
---|
77 | BS3_BEGIN_TEXT16
|
---|
78 | .sixteen_bit:
|
---|
79 |
|
---|
80 | ; Load 16-bit segment registers.
|
---|
81 | add ax, BS3_SEL_R0_SS16 - BS3_SEL_R0_CS16
|
---|
82 | mov ss, ax
|
---|
83 |
|
---|
84 | add ax, BS3_SEL_R0_DS16 - BS3_SEL_R0_SS16
|
---|
85 | mov ds, ax
|
---|
86 | mov es, ax
|
---|
87 |
|
---|
88 | ; Thunk the stack if necessary.
|
---|
89 | mov ebx, esp
|
---|
90 | shr ebx, 16
|
---|
91 | jz .stack_ok
|
---|
92 | int3 ; This is for later, just remove this int3 once needed.
|
---|
93 | test ax, X86_SEL_RPL
|
---|
94 | jnz .stack_rpl_must_be_0_for_custom_stacks
|
---|
95 | shl bx, X86_SEL_SHIFT
|
---|
96 | add bx, BS3_SEL_TILED
|
---|
97 | mov ss, bx
|
---|
98 | movzx esp, sp
|
---|
99 | .stack_ok:
|
---|
100 |
|
---|
101 | ; Update globals.
|
---|
102 | and byte [BS3_DATA16_WRT(g_bBs3CurrentMode)], ~BS3_MODE_CODE_MASK
|
---|
103 | or byte [BS3_DATA16_WRT(g_bBs3CurrentMode)], BS3_MODE_CODE_16
|
---|
104 |
|
---|
105 | popfd
|
---|
106 | TMPL_ONLY_64BIT_STMT pop ebx
|
---|
107 | pop ebx
|
---|
108 | TMPL_ONLY_64BIT_STMT pop eax
|
---|
109 | pop eax
|
---|
110 | TMPL_ONLY_64BIT_STMT add sp, 4
|
---|
111 | ret (TMPL_BITS - 16) / 8 ; Return and pop 2 or 6 bytes of "parameters" (unused return value)
|
---|
112 |
|
---|
113 | .stack_rpl_must_be_0_for_custom_stacks:
|
---|
114 | int3
|
---|
115 | jmp .stack_rpl_must_be_0_for_custom_stacks
|
---|
116 | TMPL_BEGIN_TEXT
|
---|
117 | %endif
|
---|
118 | BS3_PROC_END_CMN Bs3SwitchTo16Bit
|
---|
119 |
|
---|