VirtualBox

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

Last change on this file since 40040 was 40024, checked in by vboxsync, 13 years ago

IEM: Some more FPU work underways.

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette