VirtualBox

source: vbox/trunk/src/VBox/VMM/testcase/tstX86-1.cpp@ 96860

Last change on this file since 96860 was 96407, checked in by vboxsync, 22 months ago

scm copyright and license note update

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.6 KB
Line 
1/* $Id: tstX86-1.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
2/** @file
3 * X86 instruction set exploration/testcase #1.
4 */
5
6/*
7 * Copyright (C) 2011-2022 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 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28
29/*********************************************************************************************************************************
30* Header Files *
31*********************************************************************************************************************************/
32#include <iprt/test.h>
33#include <VBox/param.h>
34#include <iprt/mem.h>
35#include <iprt/errcore.h>
36#include <iprt/assert.h>
37#include <iprt/x86.h>
38
39#ifdef RT_OS_WINDOWS
40# include <iprt/win/windows.h>
41#else
42# ifdef RT_OS_DARWIN
43# define _XOPEN_SOURCE
44# endif
45# include <signal.h>
46# include <ucontext.h>
47# define USE_SIGNAL
48#endif
49
50
51/*********************************************************************************************************************************
52* Structures and Typedefs *
53*********************************************************************************************************************************/
54typedef struct TRAPINFO
55{
56 uintptr_t uTrapPC;
57 uintptr_t uResumePC;
58 uint8_t u8Trap;
59 uint8_t cbInstr;
60 uint8_t auAlignment[sizeof(uintptr_t) * 2 - 2];
61} TRAPINFO;
62typedef TRAPINFO const *PCTRAPINFO;
63
64
65/*********************************************************************************************************************************
66* Global Variables *
67*********************************************************************************************************************************/
68RT_C_DECLS_BEGIN
69uint8_t *g_pbEfPage = NULL;
70uint8_t *g_pbEfExecPage = NULL;
71extern TRAPINFO g_aTrapInfo[];
72RT_C_DECLS_END
73
74
75/*********************************************************************************************************************************
76* Internal Functions *
77*********************************************************************************************************************************/
78DECLASM(int32_t) x861_Test1(void);
79DECLASM(int32_t) x861_Test2(void);
80DECLASM(int32_t) x861_Test3(void);
81DECLASM(int32_t) x861_Test4(void);
82DECLASM(int32_t) x861_Test5(void);
83DECLASM(int32_t) x861_Test6(void);
84DECLASM(int32_t) x861_Test7(void);
85DECLASM(int32_t) x861_TestFPUInstr1(void);
86
87
88
89static PCTRAPINFO findTrapInfo(uintptr_t uTrapPC, uintptr_t uTrapSP)
90{
91 /* Search by trap program counter. */
92 for (unsigned i = 0; g_aTrapInfo[i].uTrapPC; i++)
93 if (g_aTrapInfo[i].uTrapPC == uTrapPC)
94 return &g_aTrapInfo[i];
95
96 /* Search by return address. */
97 uintptr_t uReturn = *(uintptr_t *)uTrapSP;
98 for (unsigned i = 0; g_aTrapInfo[i].uTrapPC; i++)
99 if (g_aTrapInfo[i].uTrapPC + g_aTrapInfo[i].cbInstr == uReturn)
100 return &g_aTrapInfo[i];
101
102 return NULL;
103}
104
105#ifdef USE_SIGNAL
106static void sigHandler(int iSig, siginfo_t *pSigInfo, void *pvSigCtx)
107{
108 ucontext_t *pCtx = (ucontext_t *)pvSigCtx;
109 NOREF(pSigInfo);
110
111# if defined(RT_ARCH_AMD64) && defined(RT_OS_DARWIN)
112 uintptr_t *puPC = (uintptr_t *)&pCtx->uc_mcontext->__ss.__rip;
113 uintptr_t *puSP = (uintptr_t *)&pCtx->uc_mcontext->__ss.__rsp;
114 uintptr_t uTrapNo = pCtx->uc_mcontext->__es.__trapno;
115 uintptr_t uErr = pCtx->uc_mcontext->__es.__err;
116 uintptr_t uCr2 = pCtx->uc_mcontext->__es.__faultvaddr;
117
118# elif defined(RT_ARCH_AMD64) && defined(RT_OS_FREEBSD)
119 uintptr_t *puPC = (uintptr_t *)&pCtx->uc_mcontext.mc_rip;
120 uintptr_t *puSP = (uintptr_t *)&pCtx->uc_mcontext.mc_rsp;
121 uintptr_t uTrapNo = ~(uintptr_t)0;
122 uintptr_t uErr = ~(uintptr_t)0;
123 uintptr_t uCr2 = ~(uintptr_t)0;
124
125# elif defined(RT_ARCH_AMD64)
126 uintptr_t *puPC = (uintptr_t *)&pCtx->uc_mcontext.gregs[REG_RIP];
127 uintptr_t *puSP = (uintptr_t *)&pCtx->uc_mcontext.gregs[REG_RSP];
128 uintptr_t uTrapNo = pCtx->uc_mcontext.gregs[REG_TRAPNO];
129 uintptr_t uErr = pCtx->uc_mcontext.gregs[REG_ERR];
130 uintptr_t uCr2 = pCtx->uc_mcontext.gregs[REG_CR2];
131
132# elif defined(RT_ARCH_X86) && defined(RT_OS_DARWIN)
133 uintptr_t *puPC = (uintptr_t *)&pCtx->uc_mcontext->__ss.__eip;
134 uintptr_t *puSP = (uintptr_t *)&pCtx->uc_mcontext->__ss.__esp;
135 uintptr_t uTrapNo = pCtx->uc_mcontext->__es.__trapno;
136 uintptr_t uErr = pCtx->uc_mcontext->__es.__err;
137 uintptr_t uCr2 = pCtx->uc_mcontext->__es.__faultvaddr;
138
139# elif defined(RT_ARCH_X86) && defined(RT_OS_FREEBSD)
140 uintptr_t *puPC = (uintptr_t *)&pCtx->uc_mcontext.mc_eip;
141 uintptr_t *puSP = (uintptr_t *)&pCtx->uc_mcontext.mc_esp;
142 uintptr_t uTrapNo = ~(uintptr_t)0;
143 uintptr_t uErr = ~(uintptr_t)0;
144 uintptr_t uCr2 = ~(uintptr_t)0;
145
146# elif defined(RT_ARCH_X86)
147 uintptr_t *puPC = (uintptr_t *)&pCtx->uc_mcontext.gregs[REG_EIP];
148 uintptr_t *puSP = (uintptr_t *)&pCtx->uc_mcontext.gregs[REG_ESP];
149 uintptr_t uTrapNo = pCtx->uc_mcontext.gregs[REG_TRAPNO];
150 uintptr_t uErr = pCtx->uc_mcontext.gregs[REG_ERR];
151# ifdef REG_CR2 /** @todo ... */
152 uintptr_t uCr2 = pCtx->uc_mcontext.gregs[REG_CR2];
153# else
154 uintptr_t uCr2 = ~(uintptr_t)0;
155# endif
156
157# else
158 uintptr_t *puPC = NULL;
159 uintptr_t *puSP = NULL;
160 uintptr_t uTrapNo = ~(uintptr_t)0;
161 uintptr_t uErr = ~(uintptr_t)0;
162 uintptr_t uCr2 = ~(uintptr_t)0;
163# endif
164 if (uTrapNo == X86_XCPT_PF)
165 RTAssertMsg2("tstX86-1: Trap #%#04x err=%#06x at %p / %p\n", uTrapNo, uErr, *puPC, uCr2);
166 else
167 RTAssertMsg2("tstX86-1: Trap #%#04x err=%#06x at %p\n", uTrapNo, uErr, *puPC);
168
169 PCTRAPINFO pTrapInfo = findTrapInfo(*puPC, *puSP);
170 if (pTrapInfo)
171 {
172 if (pTrapInfo->u8Trap != uTrapNo && uTrapNo != ~(uintptr_t)0)
173 RTAssertMsg2("tstX86-1: Expected #%#04x, got #%#04x\n", pTrapInfo->u8Trap, uTrapNo);
174 else
175 {
176 if (*puPC != pTrapInfo->uTrapPC)
177 *puSP += sizeof(uintptr_t);
178 *puPC = pTrapInfo->uResumePC;
179 return;
180 }
181 }
182 else
183 RTAssertMsg2("tstX86-1: Unexpected trap!\n");
184
185 /* die */
186 signal(iSig, SIG_IGN);
187}
188#else
189
190#endif
191
192
193
194int main()
195{
196 /*
197 * Set up the test environment.
198 */
199 RTTEST hTest;
200 RTEXITCODE rcExit = RTTestInitAndCreate("tstX86-1", &hTest);
201 if (rcExit != RTEXITCODE_SUCCESS)
202 return rcExit;
203 RTTestBanner(hTest);
204
205 g_pbEfPage = (uint8_t *)RTTestGuardedAllocTail(hTest, HOST_PAGE_SIZE);
206 RTTESTI_CHECK(g_pbEfPage != NULL);
207
208 g_pbEfExecPage = (uint8_t *)RTMemExecAlloc(HOST_PAGE_SIZE*2);
209 RTTESTI_CHECK(g_pbEfExecPage != NULL);
210 RTTESTI_CHECK(!((uintptr_t)g_pbEfExecPage & HOST_PAGE_OFFSET_MASK));
211 RTTESTI_CHECK_RC(RTMemProtect(g_pbEfExecPage + HOST_PAGE_SIZE, HOST_PAGE_SIZE, RTMEM_PROT_NONE), VINF_SUCCESS);
212
213#ifdef USE_SIGNAL
214 static int const s_aiSigs[] = { SIGBUS, SIGSEGV, SIGFPE, SIGILL };
215 for (unsigned i = 0; i < RT_ELEMENTS(s_aiSigs); i++)
216 {
217 struct sigaction SigAct;
218 RTTESTI_CHECK_BREAK(sigaction(s_aiSigs[i], NULL, &SigAct) == 0);
219 SigAct.sa_sigaction = sigHandler;
220 SigAct.sa_flags |= SA_SIGINFO;
221 RTTESTI_CHECK(sigaction(s_aiSigs[i], &SigAct, NULL) == 0);
222 }
223#else
224 /** @todo implement me. */
225#endif
226
227
228 if (!RTTestErrorCount(hTest))
229 {
230 /*
231 * Do the testing.
232 */
233 int32_t rc;
234#if 0
235 RTTestSub(hTest, "Misc 1");
236 rc = x861_Test1();
237 if (rc != 0)
238 RTTestFailed(hTest, "x861_Test1 -> %d", rc);
239
240 RTTestSub(hTest, "Prefixes and groups");
241 rc = x861_Test2();
242 if (rc != 0)
243 RTTestFailed(hTest, "x861_Test2 -> %d", rc);
244
245 RTTestSub(hTest, "fxsave / fxrstor and #PFs");
246 rc = x861_Test3();
247 if (rc != 0)
248 RTTestFailed(hTest, "x861_Test3 -> %d", rc);
249
250 RTTestSub(hTest, "Multibyte NOPs");
251 rc = x861_Test4();
252 if (rc != 0)
253 RTTestFailed(hTest, "x861_Test4 -> %d", rc);
254//#endif
255
256 RTTestSub(hTest, "Odd encodings and odd ends");
257 rc = x861_Test5();
258 if (rc != 0)
259 RTTestFailed(hTest, "x861_Test5 -> %d", rc);
260
261//#if 0
262 RTTestSub(hTest, "Odd floating point encodings");
263 rc = x861_Test6();
264 if (rc != 0)
265 RTTestFailed(hTest, "x861_Test5 -> %d", rc);
266
267 RTTestSub(hTest, "Floating point exceptions ++");
268 rc = x861_Test7();
269 if (rc != 0)
270 RTTestFailed(hTest, "x861_Test6 -> %d", rc);
271#endif
272
273 rc = x861_TestFPUInstr1();
274 if (rc != 0)
275 RTTestFailed(hTest, "x861_TestFPUInstr1 -> %d", rc);
276 }
277
278 return RTTestSummaryAndDestroy(hTest);
279}
280
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use