1 | /* $Id: bs3-cpu-basic-2-template.c 60098 2016-03-18 13:15:39Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * BS3Kit - bs3-cpu-basic-2, C code template.
|
---|
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.215389.xyz. 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 | #ifdef BS3_INSTANTIATING_MODE
|
---|
29 |
|
---|
30 | #include <iprt/asm.h>
|
---|
31 |
|
---|
32 |
|
---|
33 | extern BS3_DECL(void) TMPL_NM(bs3CpuBasic2_TssGateEsp_IntXx)(void);
|
---|
34 |
|
---|
35 | # if TMPL_MODE == BS3_MODE_PE16 \
|
---|
36 | || TMPL_MODE == BS3_MODE_PE16_32
|
---|
37 |
|
---|
38 | static void bs3CpuBasic2_CompareTrapCtx1(PCBS3TRAPFRAME pTrapCtx, PCBS3REGCTX pStartCtx, uint16_t cbIpAdjust, uint8_t bXcpt,
|
---|
39 | unsigned uStep)
|
---|
40 | {
|
---|
41 | const char *pszMode = BS3_DATA_NM(TMPL_NM(g_szBs3ModeName));
|
---|
42 | uint16_t cErrorsBefore = Bs3TestSubErrorCount();
|
---|
43 |
|
---|
44 | #define CHECK_MEMBER(a_szName, a_szFmt, a_Actual, a_Expected) \
|
---|
45 | do \
|
---|
46 | { \
|
---|
47 | if ((a_Actual) == (a_Expected)) { /* likely */ } \
|
---|
48 | else Bs3TestFailedF("%u - %s: " a_szName "=" a_szFmt " expected " a_szFmt, uStep, pszMode, (a_Actual), (a_Expected)); \
|
---|
49 | } while (0)
|
---|
50 |
|
---|
51 | CHECK_MEMBER("bXcpt", "%#04x", pTrapCtx->bXcpt, bXcpt);
|
---|
52 | Bs3TestCheckRegCtxEx(&pTrapCtx->Ctx, pStartCtx, cbIpAdjust, 0 /*cbSpAdjust*/, pszMode, uStep);
|
---|
53 | if (Bs3TestSubErrorCount() != cErrorsBefore)
|
---|
54 | Bs3TrapPrintFrame(pTrapCtx);
|
---|
55 | }
|
---|
56 | #endif
|
---|
57 |
|
---|
58 |
|
---|
59 | BS3_DECL(uint8_t) TMPL_NM(bs3CpuBasic2_TssGateEsp)(uint8_t bMode)
|
---|
60 | {
|
---|
61 | uint8_t bRet = 0;
|
---|
62 | BS3TRAPFRAME TrapCtx;
|
---|
63 | BS3REGCTX Ctx, Ctx2;
|
---|
64 | uint8_t *pbTmp;
|
---|
65 |
|
---|
66 | pbTmp = NULL; NOREF(pbTmp);
|
---|
67 |
|
---|
68 | /* make sure they're allocated */
|
---|
69 | Bs3MemZero(&Ctx, sizeof(Ctx));
|
---|
70 | Bs3MemZero(&Ctx2, sizeof(Ctx2));
|
---|
71 | Bs3MemZero(&TrapCtx, sizeof(TrapCtx));
|
---|
72 |
|
---|
73 | # if TMPL_MODE == BS3_MODE_PE16 \
|
---|
74 | || TMPL_MODE == BS3_MODE_PE16_32
|
---|
75 |
|
---|
76 | Bs3RegCtxSave(&Ctx);
|
---|
77 | Ctx.rsp.u -= 0x80;
|
---|
78 | Ctx.rip.u = (uintptr_t)BS3_FP_OFF(&TMPL_NM(bs3CpuBasic2_TssGateEsp_IntXx));
|
---|
79 | # if TMPL_BITS == 32
|
---|
80 | BS3_DATA_NM(g_uBs3TrapEipHint) = Ctx.rip.u32;
|
---|
81 | # endif
|
---|
82 |
|
---|
83 | /*
|
---|
84 | * Check that the stuff works first.
|
---|
85 | */
|
---|
86 | if (Bs3TrapSetJmp(&TrapCtx))
|
---|
87 | Bs3RegCtxRestore(&Ctx, 0); /* (does not return) */
|
---|
88 | bs3CpuBasic2_CompareTrapCtx1(&TrapCtx, &Ctx, 2 /*int 80h*/, 0x80 /*bXcpt*/, 1/*bStep*/);
|
---|
89 |
|
---|
90 | /*
|
---|
91 | * Check that the upper part of ESP is preserved when doing .
|
---|
92 | */
|
---|
93 | if ((BS3_DATA_NM(g_uBs3CpuDetected) & BS3CPU_TYPE_MASK) >= BS3CPU_80386)
|
---|
94 | {
|
---|
95 | size_t const cbAltStack = _8K;
|
---|
96 | uint8_t *pbAltStack = Bs3MemAllocZ(BS3MEMKIND_TILED, cbAltStack);
|
---|
97 | if (pbAltStack)
|
---|
98 | {
|
---|
99 | Bs3MemCpy(&Ctx2, &Ctx, sizeof(Ctx2));
|
---|
100 | Ctx2.rsp.u = Bs3SelPtrToFlat(pbAltStack + 0x1980);
|
---|
101 | if (Bs3TrapSetJmp(&TrapCtx))
|
---|
102 | Bs3RegCtxRestore(&Ctx2, 0); /* (does not return) */
|
---|
103 | bs3CpuBasic2_CompareTrapCtx1(&TrapCtx, &Ctx2, 2 /*int 80h*/, 0x80 /*bXcpt*/, 2/*bStep*/);
|
---|
104 | # if TMPL_BITS == 16
|
---|
105 | if ((pbTmp = (uint8_t *)ASMMemFirstNonZero(pbAltStack, cbAltStack)) != NULL)
|
---|
106 | Bs3TestFailedF("%s: someone touched the alt stack (%p) with CS:ESP=%04x:%#RX32: %p=%02x\n",
|
---|
107 | BS3_DATA_NM(TMPL_NM(g_szBs3ModeName)), pbAltStack, Ctx2.ss, Ctx2.rsp.u32, pbTmp, *pbTmp);
|
---|
108 | # else
|
---|
109 | if (ASMMemIsZero(pbAltStack, cbAltStack))
|
---|
110 | Bs3TestFailedF("%s: alt stack wasn't used despite CS:ESP=%04x:%#RX32\n",
|
---|
111 | BS3_DATA_NM(TMPL_NM(g_szBs3ModeName)), Ctx2.ss, Ctx2.rsp.u32);
|
---|
112 | # endif
|
---|
113 | Bs3MemFree(pbAltStack, cbAltStack);
|
---|
114 | }
|
---|
115 | else
|
---|
116 | Bs3TestPrintf("%s: Skipping ESP check, alloc failed\n", BS3_DATA_NM(TMPL_NM(g_szBs3ModeName)));
|
---|
117 | }
|
---|
118 | else
|
---|
119 | Bs3TestPrintf("%s: Skipping ESP check, CPU too old\n", BS3_DATA_NM(TMPL_NM(g_szBs3ModeName)));
|
---|
120 |
|
---|
121 | # else
|
---|
122 | bRet = BS3TESTDOMODE_SKIPPED;
|
---|
123 | # endif
|
---|
124 |
|
---|
125 | /*
|
---|
126 | * Re-initialize the IDT.
|
---|
127 | */
|
---|
128 | # if BS3_MODE_IS_16BIT_SYS(TMPL_MODE)
|
---|
129 | Bs3Trap16Init();
|
---|
130 | # elif BS3_MODE_IS_32BIT_SYS(TMPL_MODE)
|
---|
131 | Bs3Trap32Init();
|
---|
132 | # elif BS3_MODE_IS_32BIT_SYS(TMPL_MODE)
|
---|
133 | Bs3Trap64Init();
|
---|
134 | # endif
|
---|
135 |
|
---|
136 | return bRet;
|
---|
137 | }
|
---|
138 |
|
---|
139 |
|
---|
140 | #endif /* BS3_INSTANTIATING_MODE */
|
---|