1 | /* $Id: bs3-cmn-Trap16Init.c 106061 2024-09-16 14:03:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * BS3Kit - Bs3Trap16Init
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2007-2024 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #include "bs3kit-template-header.h"
|
---|
42 |
|
---|
43 |
|
---|
44 | /*********************************************************************************************************************************
|
---|
45 | * Global Variables *
|
---|
46 | *********************************************************************************************************************************/
|
---|
47 | /* We ASSUME that BS3CLASS16CODE is 64KB aligned, so the low 16-bit of the
|
---|
48 | flat address matches. Also, these symbols are defined both with
|
---|
49 | and without underscore prefixes. */
|
---|
50 | extern BS3_DECL(void) BS3_FAR_CODE Bs3Trap16DoubleFaultHandler80386(void);
|
---|
51 | extern BS3_DECL(void) BS3_FAR_CODE Bs3Trap16DoubleFaultHandler80286(void);
|
---|
52 | extern BS3_DECL(void) BS3_FAR_CODE Bs3Trap16GenericEntries(void);
|
---|
53 |
|
---|
54 | /* These two are ugly. Need data access for patching purposes. */
|
---|
55 | extern uint8_t BS3_FAR_DATA bs3Trap16GenericTrapOrInt[];
|
---|
56 |
|
---|
57 |
|
---|
58 | #undef Bs3Trap16InitEx
|
---|
59 | BS3_CMN_DEF(void, Bs3Trap16InitEx,(bool f386Plus))
|
---|
60 | {
|
---|
61 | X86TSS16 BS3_FAR *pTss;
|
---|
62 | unsigned iIdt;
|
---|
63 |
|
---|
64 | /*
|
---|
65 | * If 386 or later, patch the trap handler code to not jump to the 80286
|
---|
66 | * code but continue with the next instruction (the 386+ code).
|
---|
67 | */
|
---|
68 | if (f386Plus)
|
---|
69 | {
|
---|
70 | uint8_t BS3_FAR_DATA *pbFunction = &bs3Trap16GenericTrapOrInt[0];
|
---|
71 | #if ARCH_BITS == 16
|
---|
72 | if (g_bBs3CurrentMode != BS3_MODE_RM)
|
---|
73 | pbFunction = (uint8_t BS3_FAR_DATA *)BS3_FP_MAKE(BS3_SEL_TILED + 1, BS3_FP_OFF(pbFunction));
|
---|
74 | #endif
|
---|
75 | pbFunction[1] = 0;
|
---|
76 | pbFunction[2] = 0;
|
---|
77 | }
|
---|
78 |
|
---|
79 | /*
|
---|
80 | * IDT entries, except the system call gate.
|
---|
81 | */
|
---|
82 | for (iIdt = 0; iIdt < 256; iIdt++)
|
---|
83 | if (iIdt != BS3_TRAP_SYSCALL)
|
---|
84 | Bs3Trap16SetGate(iIdt, X86_SEL_TYPE_SYS_286_INT_GATE, 0 /*bDpl*/,
|
---|
85 | BS3_SEL_R0_CS16, (uint16_t)(uintptr_t)Bs3Trap16GenericEntries + iIdt * 8, 0 /*cParams*/);
|
---|
86 |
|
---|
87 | /*
|
---|
88 | * Initialize the normal TSS so we can do ring transitions via the IDT.
|
---|
89 | */
|
---|
90 | pTss = &Bs3Tss16;
|
---|
91 | Bs3MemZero(pTss, sizeof(*pTss));
|
---|
92 | pTss->sp0 = BS3_ADDR_STACK_R0;
|
---|
93 | pTss->ss0 = BS3_SEL_R0_SS16;
|
---|
94 | pTss->sp1 = BS3_ADDR_STACK_R1;
|
---|
95 | pTss->ss1 = BS3_SEL_R1_SS16 | 1;
|
---|
96 | pTss->sp2 = BS3_ADDR_STACK_R2;
|
---|
97 | pTss->ss2 = BS3_SEL_R2_SS16 | 2;
|
---|
98 |
|
---|
99 | /*
|
---|
100 | * Initialize the double fault TSS.
|
---|
101 | * cr3 is filled in by switcher code, when needed.
|
---|
102 | */
|
---|
103 | pTss = &Bs3Tss16DoubleFault;
|
---|
104 | Bs3MemZero(pTss, sizeof(*pTss));
|
---|
105 | pTss->sp0 = BS3_ADDR_STACK_R0;
|
---|
106 | pTss->ss0 = BS3_SEL_R0_SS16;
|
---|
107 | pTss->sp1 = BS3_ADDR_STACK_R1;
|
---|
108 | pTss->ss1 = BS3_SEL_R1_SS16 | 1;
|
---|
109 | pTss->sp2 = BS3_ADDR_STACK_R2;
|
---|
110 | pTss->ss2 = BS3_SEL_R2_SS16 | 2;
|
---|
111 | pTss->ip = (uint16_t)(uintptr_t)(f386Plus ? &Bs3Trap16DoubleFaultHandler80386 : &Bs3Trap16DoubleFaultHandler80286);
|
---|
112 | pTss->flags = X86_EFL_1;
|
---|
113 | pTss->sp = BS3_ADDR_STACK_R0_IST1;
|
---|
114 | pTss->es = BS3_SEL_R0_DS16;
|
---|
115 | pTss->ds = BS3_SEL_R0_DS16;
|
---|
116 | pTss->cs = BS3_SEL_R0_CS16;
|
---|
117 | pTss->ss = BS3_SEL_R0_SS16;
|
---|
118 | pTss->dx = f386Plus;
|
---|
119 |
|
---|
120 | Bs3Trap16SetGate(X86_XCPT_DF, X86_SEL_TYPE_SYS_TASK_GATE, 0 /*bDpl*/, BS3_SEL_TSS16_DF, 0, 0 /*cParams*/);
|
---|
121 | }
|
---|
122 |
|
---|
123 |
|
---|
124 | #undef Bs3Trap16Init
|
---|
125 | BS3_CMN_DEF(void, Bs3Trap16Init,(void))
|
---|
126 | {
|
---|
127 | BS3_CMN_NM(Bs3Trap16InitEx)((g_uBs3CpuDetected & BS3CPU_TYPE_MASK) >= BS3CPU_80386);
|
---|
128 | }
|
---|
129 |
|
---|