1 | /* $Id: tstX86-1.cpp 39084 2011-10-22 00:37:15Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * X86 instruction set exploration/testcase #1.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2011 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 |
|
---|
18 |
|
---|
19 | /*******************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *******************************************************************************/
|
---|
22 | #include <iprt/test.h>
|
---|
23 | #include <iprt/param.h>
|
---|
24 | #include <iprt/mem.h>
|
---|
25 | #include <iprt/err.h>
|
---|
26 | #include <iprt/assert.h>
|
---|
27 |
|
---|
28 | #ifdef RT_OS_WINDOWS
|
---|
29 | # include <Windows.h>
|
---|
30 | #else
|
---|
31 | # ifdef RT_OS_DARWIN
|
---|
32 | # define _XOPEN_SOURCE
|
---|
33 | # endif
|
---|
34 | # include <signal.h>
|
---|
35 | # include <ucontext.h>
|
---|
36 | # define USE_SIGNAL
|
---|
37 | #endif
|
---|
38 |
|
---|
39 |
|
---|
40 | /*******************************************************************************
|
---|
41 | * Structures and Typedefs *
|
---|
42 | *******************************************************************************/
|
---|
43 | typedef struct TRAPINFO
|
---|
44 | {
|
---|
45 | uintptr_t uTrapPC;
|
---|
46 | uintptr_t uResumePC;
|
---|
47 | uint8_t u8Trap;
|
---|
48 | uint8_t cbInstr;
|
---|
49 | uint8_t auAlignment[sizeof(uintptr_t) * 2 - 2];
|
---|
50 | } TRAPINFO;
|
---|
51 | typedef TRAPINFO const *PCTRAPINFO;
|
---|
52 |
|
---|
53 |
|
---|
54 | /*******************************************************************************
|
---|
55 | * Global Variables *
|
---|
56 | *******************************************************************************/
|
---|
57 | RT_C_DECLS_BEGIN
|
---|
58 | uint8_t *g_pbEfPage = NULL;
|
---|
59 | uint8_t *g_pbEfExecPage = NULL;
|
---|
60 | extern TRAPINFO g_aTrapInfo[];
|
---|
61 | RT_C_DECLS_END
|
---|
62 |
|
---|
63 |
|
---|
64 | /*******************************************************************************
|
---|
65 | * Internal Functions *
|
---|
66 | *******************************************************************************/
|
---|
67 | DECLASM(int32_t) x861_Test1(void);
|
---|
68 |
|
---|
69 |
|
---|
70 |
|
---|
71 | static PCTRAPINFO findTrapInfo(uintptr_t uTrapPC, uintptr_t uTrapSP)
|
---|
72 | {
|
---|
73 | /* Search by trap program counter. */
|
---|
74 | for (unsigned i = 0; g_aTrapInfo[i].uTrapPC; i++)
|
---|
75 | if (g_aTrapInfo[i].uTrapPC == uTrapPC)
|
---|
76 | return &g_aTrapInfo[i];
|
---|
77 |
|
---|
78 | /* Search by return address. */
|
---|
79 | uintptr_t uReturn = *(uintptr_t *)uTrapSP;
|
---|
80 | for (unsigned i = 0; g_aTrapInfo[i].uTrapPC; i++)
|
---|
81 | if (g_aTrapInfo[i].uTrapPC + g_aTrapInfo[i].cbInstr == uReturn)
|
---|
82 | return &g_aTrapInfo[i];
|
---|
83 |
|
---|
84 | return NULL;
|
---|
85 | }
|
---|
86 |
|
---|
87 | #ifdef USE_SIGNAL
|
---|
88 | static void sigHandler(int iSig, siginfo_t *pSigInfo, void *pvSigCtx)
|
---|
89 | {
|
---|
90 | ucontext_t *pCtx = (ucontext_t *)pvSigCtx;
|
---|
91 | NOREF(pSigInfo);
|
---|
92 |
|
---|
93 | # if defined(RT_ARCH_AMD64) && defined(RT_OS_DARWIN)
|
---|
94 | uintptr_t *puPC = (uintptr_t *)&pCtx->uc_mcontext->__ss.__rip;
|
---|
95 | uintptr_t *puSP = (uintptr_t *)&pCtx->uc_mcontext->__ss.__rsp;
|
---|
96 | uintptr_t uTrapNo = pCtx->uc_mcontext->__es.__trapno;
|
---|
97 | uintptr_t uErr = pCtx->uc_mcontext->__es.__err;
|
---|
98 |
|
---|
99 | # elif defined(RT_ARCH_AMD64) && defined(RT_OS_FREEBSD)
|
---|
100 | uintptr_t *puPC = (uintptr_t *)&pCtx->uc_mcontext.mc_rip;
|
---|
101 | uintptr_t *puSP = (uintptr_t *)&pCtx->uc_mcontext.mc_rsp;
|
---|
102 | uintptr_t uTrapNo = ~(uintptr_t)0;
|
---|
103 | uintptr_t uErr = ~(uintptr_t)0;
|
---|
104 |
|
---|
105 | # elif defined(RT_ARCH_AMD64)
|
---|
106 | uintptr_t *puPC = (uintptr_t *)&pCtx->uc_mcontext.gregs[REG_RIP];
|
---|
107 | uintptr_t *puSP = (uintptr_t *)&pCtx->uc_mcontext.gregs[REG_RSP];
|
---|
108 | uintptr_t uTrapNo = pCtx->uc_mcontext.gregs[REG_TRAPNO];
|
---|
109 | uintptr_t uErr = pCtx->uc_mcontext.gregs[REG_ERR];
|
---|
110 |
|
---|
111 | # elif defined(RT_ARCH_X86) && defined(RT_OS_DARWIN)
|
---|
112 | uintptr_t *puPC = (uintptr_t *)&pCtx->uc_mcontext->__ss.__eip;
|
---|
113 | uintptr_t *puSP = (uintptr_t *)&pCtx->uc_mcontext->__ss.__esp;
|
---|
114 | uintptr_t uTrapNo = pCtx->uc_mcontext->__es.__trapno;
|
---|
115 | uintptr_t uErr = pCtx->uc_mcontext->__es.__err;
|
---|
116 |
|
---|
117 | # elif defined(RT_ARCH_X86) && defined(RT_OS_FREEBSD)
|
---|
118 | uintptr_t *puPC = (uintptr_t *)&pCtx->uc_mcontext.mc_eip;
|
---|
119 | uintptr_t *puSP = (uintptr_t *)&pCtx->uc_mcontext.mc_esp;
|
---|
120 | uintptr_t uTrapNo = ~(uintptr_t)0;
|
---|
121 | uintptr_t uErr = ~(uintptr_t)0;
|
---|
122 |
|
---|
123 | # elif defined(RT_ARCH_X86)
|
---|
124 | uintptr_t *puPC = (uintptr_t *)&pCtx->uc_mcontext.gregs[REG_EIP];
|
---|
125 | uintptr_t *puSP = (uintptr_t *)&pCtx->uc_mcontext.gregs[REG_ESP];
|
---|
126 | uintptr_t uTrapNo = pCtx->uc_mcontext.gregs[REG_TRAPNO];
|
---|
127 | uintptr_t uErr = pCtx->uc_mcontext.gregs[REG_ERR];
|
---|
128 |
|
---|
129 | # else
|
---|
130 | uintptr_t *puPC = NULL;
|
---|
131 | uintptr_t *puSP = NULL;
|
---|
132 | uintptr_t uTrapNo = ~(uintptr_t)0;
|
---|
133 | uintptr_t uErr = ~(uintptr_t)0;
|
---|
134 | # endif
|
---|
135 | RTAssertMsg2("tstX86-1: Trap #%#04x err=%#06x at %p\n", uTrapNo, uErr, *puPC);
|
---|
136 |
|
---|
137 | PCTRAPINFO pTrapInfo = findTrapInfo(*puPC, *puSP);
|
---|
138 | if (pTrapInfo)
|
---|
139 | {
|
---|
140 | if (pTrapInfo->u8Trap != uTrapNo && uTrapNo != ~(uintptr_t)0)
|
---|
141 | RTAssertMsg2("tstX86-1: Expected #%#04x, got #%#04x\n", pTrapInfo->u8Trap, uTrapNo);
|
---|
142 | else
|
---|
143 | {
|
---|
144 | if (*puPC != pTrapInfo->uTrapPC)
|
---|
145 | *puSP += sizeof(uintptr_t);
|
---|
146 | *puPC = pTrapInfo->uResumePC;
|
---|
147 | return;
|
---|
148 | }
|
---|
149 | }
|
---|
150 | else
|
---|
151 | RTAssertMsg2("tstX86-1: Unexpected trap!\n");
|
---|
152 |
|
---|
153 | /* die */
|
---|
154 | signal(iSig, SIG_IGN);
|
---|
155 | }
|
---|
156 | #else
|
---|
157 |
|
---|
158 | #endif
|
---|
159 |
|
---|
160 | int main()
|
---|
161 | {
|
---|
162 | /*
|
---|
163 | * Set up the test environment.
|
---|
164 | */
|
---|
165 | RTTEST hTest;
|
---|
166 | RTEXITCODE rcExit = RTTestInitAndCreate("tstX86-1", &hTest);
|
---|
167 | if (rcExit != RTEXITCODE_SUCCESS)
|
---|
168 | return rcExit;
|
---|
169 | RTTestBanner(hTest);
|
---|
170 |
|
---|
171 | g_pbEfPage = (uint8_t *)RTTestGuardedAllocTail(hTest, PAGE_SIZE);
|
---|
172 | RTTESTI_CHECK(g_pbEfPage != NULL);
|
---|
173 |
|
---|
174 | g_pbEfExecPage = (uint8_t *)RTMemExecAlloc(PAGE_SIZE*2);
|
---|
175 | RTTESTI_CHECK(g_pbEfExecPage != NULL);
|
---|
176 | RTTESTI_CHECK(!((uintptr_t)g_pbEfExecPage & PAGE_OFFSET_MASK));
|
---|
177 | RTTESTI_CHECK_RC(RTMemProtect(g_pbEfExecPage + PAGE_SIZE, PAGE_SIZE, RTMEM_PROT_NONE), VINF_SUCCESS);
|
---|
178 |
|
---|
179 | #ifdef USE_SIGNAL
|
---|
180 | static int const s_aiSigs[] = { SIGBUS, SIGSEGV, SIGFPE, SIGILL };
|
---|
181 | for (unsigned i = 0; i < RT_ELEMENTS(s_aiSigs); i++)
|
---|
182 | {
|
---|
183 | struct sigaction SigAct;
|
---|
184 | RTTESTI_CHECK_BREAK(sigaction(s_aiSigs[i], NULL, &SigAct) == 0);
|
---|
185 | SigAct.sa_sigaction = sigHandler;
|
---|
186 | SigAct.sa_flags |= SA_SIGINFO;
|
---|
187 | RTTESTI_CHECK(sigaction(s_aiSigs[i], &SigAct, NULL) == 0);
|
---|
188 | }
|
---|
189 | #else
|
---|
190 | /** @todo implement me. */
|
---|
191 | #endif
|
---|
192 |
|
---|
193 |
|
---|
194 | if (!RTTestErrorCount(hTest))
|
---|
195 | {
|
---|
196 | /*
|
---|
197 | * Do the testing.
|
---|
198 | */
|
---|
199 | RTTestSub(hTest, "part 1");
|
---|
200 | int32_t rc = x861_Test1();
|
---|
201 | if (rc != 0)
|
---|
202 | RTTestFailed(hTest, "x861_Test1 -> %d", rc);
|
---|
203 | }
|
---|
204 |
|
---|
205 | return RTTestSummaryAndDestroy(hTest);
|
---|
206 | }
|
---|
207 |
|
---|