1 | /* $Id: bs3-cmn-Trap16Init.c 60527 2016-04-18 09:11:04Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * BS3Kit - Bs3Trap16Init
|
---|
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 | /*********************************************************************************************************************************
|
---|
28 | * Header Files *
|
---|
29 | *********************************************************************************************************************************/
|
---|
30 | #include "bs3kit-template-header.h"
|
---|
31 |
|
---|
32 |
|
---|
33 | /*********************************************************************************************************************************
|
---|
34 | * Global Variables *
|
---|
35 | *********************************************************************************************************************************/
|
---|
36 | /* We ASSUME that BS3CLASS16CODE is 64KB aligned, so the low 16-bit of the
|
---|
37 | flat address matches. Also, these symbols are defined both with
|
---|
38 | and without underscore prefixes. */
|
---|
39 | extern BS3_DECL(void) BS3_FAR_CODE Bs3Trap16DoubleFaultHandler80386(void);
|
---|
40 | extern BS3_DECL(void) BS3_FAR_CODE Bs3Trap16DoubleFaultHandler80286(void);
|
---|
41 | extern BS3_DECL(void) BS3_FAR_CODE Bs3Trap16GenericEntries(void);
|
---|
42 |
|
---|
43 | /* These two are ugly. Need data access for patching purposes. */
|
---|
44 | extern uint8_t BS3_FAR_DATA bs3Trap16GenericTrapOrInt[];
|
---|
45 |
|
---|
46 |
|
---|
47 | #undef Bs3Trap16InitEx
|
---|
48 | BS3_CMN_DEF(void, Bs3Trap16InitEx,(bool f386Plus))
|
---|
49 | {
|
---|
50 | X86TSS16 BS3_FAR *pTss;
|
---|
51 | unsigned iIdt;
|
---|
52 |
|
---|
53 | /*
|
---|
54 | * If 386 or later, patch the trap handler code to not jump to the 80286
|
---|
55 | * code but continue with the next instruction (the 386+ code).
|
---|
56 | */
|
---|
57 | if (f386Plus)
|
---|
58 | {
|
---|
59 | uint8_t BS3_FAR_DATA *pbFunction = &bs3Trap16GenericTrapOrInt[0];
|
---|
60 | #if ARCH_BITS == 16
|
---|
61 | if (g_bBs3CurrentMode != BS3_MODE_RM)
|
---|
62 | pbFunction = (uint8_t BS3_FAR_DATA *)BS3_FP_MAKE(BS3_SEL_TILED + 1, BS3_FP_OFF(pbFunction));
|
---|
63 | #endif
|
---|
64 | pbFunction[1] = 0;
|
---|
65 | pbFunction[2] = 0;
|
---|
66 | }
|
---|
67 |
|
---|
68 | /*
|
---|
69 | * IDT entries, except the system call gate.
|
---|
70 | */
|
---|
71 | for (iIdt = 0; iIdt < 256; iIdt++)
|
---|
72 | if (iIdt != BS3_TRAP_SYSCALL)
|
---|
73 | Bs3Trap16SetGate(iIdt, X86_SEL_TYPE_SYS_286_INT_GATE, 0 /*bDpl*/,
|
---|
74 | BS3_SEL_R0_CS16, (uint16_t)(uintptr_t)Bs3Trap16GenericEntries + iIdt * 8, 0 /*cParams*/);
|
---|
75 |
|
---|
76 | /*
|
---|
77 | * Initialize the normal TSS so we can do ring transitions via the IDT.
|
---|
78 | */
|
---|
79 | pTss = &Bs3Tss16;
|
---|
80 | Bs3MemZero(pTss, sizeof(*pTss));
|
---|
81 | pTss->sp0 = BS3_ADDR_STACK_R0;
|
---|
82 | pTss->ss0 = BS3_SEL_R0_SS16;
|
---|
83 | pTss->sp1 = BS3_ADDR_STACK_R1;
|
---|
84 | pTss->ss1 = BS3_SEL_R1_SS16 | 1;
|
---|
85 | pTss->sp2 = BS3_ADDR_STACK_R2;
|
---|
86 | pTss->ss2 = BS3_SEL_R2_SS16 | 2;
|
---|
87 |
|
---|
88 | /*
|
---|
89 | * Initialize the double fault TSS.
|
---|
90 | * cr3 is filled in by switcher code, when needed.
|
---|
91 | */
|
---|
92 | pTss = &Bs3Tss16DoubleFault;
|
---|
93 | Bs3MemZero(pTss, sizeof(*pTss));
|
---|
94 | pTss->sp0 = BS3_ADDR_STACK_R0;
|
---|
95 | pTss->ss0 = BS3_SEL_R0_SS16;
|
---|
96 | pTss->sp1 = BS3_ADDR_STACK_R1;
|
---|
97 | pTss->ss1 = BS3_SEL_R1_SS16 | 1;
|
---|
98 | pTss->sp2 = BS3_ADDR_STACK_R2;
|
---|
99 | pTss->ss2 = BS3_SEL_R2_SS16 | 2;
|
---|
100 | pTss->ip = (uint16_t)(uintptr_t)(f386Plus ? &Bs3Trap16DoubleFaultHandler80386 : &Bs3Trap16DoubleFaultHandler80286);
|
---|
101 | pTss->flags = X86_EFL_1;
|
---|
102 | pTss->sp = BS3_ADDR_STACK_R0_IST1;
|
---|
103 | pTss->es = BS3_SEL_R0_DS16;
|
---|
104 | pTss->ds = BS3_SEL_R0_DS16;
|
---|
105 | pTss->cs = BS3_SEL_R0_CS16;
|
---|
106 | pTss->ss = BS3_SEL_R0_SS16;
|
---|
107 | pTss->dx = f386Plus;
|
---|
108 |
|
---|
109 | Bs3Trap16SetGate(X86_XCPT_DF, X86_SEL_TYPE_SYS_TASK_GATE, 0 /*bDpl*/, BS3_SEL_TSS16_DF, 0, 0 /*cParams*/);
|
---|
110 | }
|
---|
111 |
|
---|
112 |
|
---|
113 | #undef Bs3Trap16Init
|
---|
114 | BS3_CMN_DEF(void, Bs3Trap16Init,(void))
|
---|
115 | {
|
---|
116 | BS3_CMN_NM(Bs3Trap16InitEx)((g_uBs3CpuDetected & BS3CPU_TYPE_MASK) >= BS3CPU_80386);
|
---|
117 | }
|
---|
118 |
|
---|