VirtualBox

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

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

fpu explorations.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.3 KB
Line 
1/* $Id: tstX86-1.cpp 40057 2012-02-10 00:21:23Z 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);
73DECLASM(int32_t) x861_Test6(void);
74
75
76
77static PCTRAPINFO findTrapInfo(uintptr_t uTrapPC, uintptr_t uTrapSP)
78{
79 /* Search by trap program counter. */
80 for (unsigned i = 0; g_aTrapInfo[i].uTrapPC; i++)
81 if (g_aTrapInfo[i].uTrapPC == uTrapPC)
82 return &g_aTrapInfo[i];
83
84 /* Search by return address. */
85 uintptr_t uReturn = *(uintptr_t *)uTrapSP;
86 for (unsigned i = 0; g_aTrapInfo[i].uTrapPC; i++)
87 if (g_aTrapInfo[i].uTrapPC + g_aTrapInfo[i].cbInstr == uReturn)
88 return &g_aTrapInfo[i];
89
90 return NULL;
91}
92
93#ifdef USE_SIGNAL
94static void sigHandler(int iSig, siginfo_t *pSigInfo, void *pvSigCtx)
95{
96 ucontext_t *pCtx = (ucontext_t *)pvSigCtx;
97 NOREF(pSigInfo);
98
99# if defined(RT_ARCH_AMD64) && defined(RT_OS_DARWIN)
100 uintptr_t *puPC = (uintptr_t *)&pCtx->uc_mcontext->__ss.__rip;
101 uintptr_t *puSP = (uintptr_t *)&pCtx->uc_mcontext->__ss.__rsp;
102 uintptr_t uTrapNo = pCtx->uc_mcontext->__es.__trapno;
103 uintptr_t uErr = pCtx->uc_mcontext->__es.__err;
104 uintptr_t uCr2 = pCtx->uc_mcontext->__es.__faultvaddr;
105
106# elif defined(RT_ARCH_AMD64) && defined(RT_OS_FREEBSD)
107 uintptr_t *puPC = (uintptr_t *)&pCtx->uc_mcontext.mc_rip;
108 uintptr_t *puSP = (uintptr_t *)&pCtx->uc_mcontext.mc_rsp;
109 uintptr_t uTrapNo = ~(uintptr_t)0;
110 uintptr_t uErr = ~(uintptr_t)0;
111 uintptr_t uCr2 = ~(uintptr_t)0;
112
113# elif defined(RT_ARCH_AMD64)
114 uintptr_t *puPC = (uintptr_t *)&pCtx->uc_mcontext.gregs[REG_RIP];
115 uintptr_t *puSP = (uintptr_t *)&pCtx->uc_mcontext.gregs[REG_RSP];
116 uintptr_t uTrapNo = pCtx->uc_mcontext.gregs[REG_TRAPNO];
117 uintptr_t uErr = pCtx->uc_mcontext.gregs[REG_ERR];
118 uintptr_t uCr2 = pCtx->uc_mcontext.gregs[REG_CR2];
119
120# elif defined(RT_ARCH_X86) && defined(RT_OS_DARWIN)
121 uintptr_t *puPC = (uintptr_t *)&pCtx->uc_mcontext->__ss.__eip;
122 uintptr_t *puSP = (uintptr_t *)&pCtx->uc_mcontext->__ss.__esp;
123 uintptr_t uTrapNo = pCtx->uc_mcontext->__es.__trapno;
124 uintptr_t uErr = pCtx->uc_mcontext->__es.__err;
125 uintptr_t uCr2 = pCtx->uc_mcontext->__es.__faultvaddr;
126
127# elif defined(RT_ARCH_X86) && defined(RT_OS_FREEBSD)
128 uintptr_t *puPC = (uintptr_t *)&pCtx->uc_mcontext.mc_eip;
129 uintptr_t *puSP = (uintptr_t *)&pCtx->uc_mcontext.mc_esp;
130 uintptr_t uTrapNo = ~(uintptr_t)0;
131 uintptr_t uErr = ~(uintptr_t)0;
132 uintptr_t uCr2 = ~(uintptr_t)0;
133
134# elif defined(RT_ARCH_X86)
135 uintptr_t *puPC = (uintptr_t *)&pCtx->uc_mcontext.gregs[REG_EIP];
136 uintptr_t *puSP = (uintptr_t *)&pCtx->uc_mcontext.gregs[REG_ESP];
137 uintptr_t uTrapNo = pCtx->uc_mcontext.gregs[REG_TRAPNO];
138 uintptr_t uErr = pCtx->uc_mcontext.gregs[REG_ERR];
139# ifdef REG_CR2 /** @todo ... */
140 uintptr_t uCr2 = pCtx->uc_mcontext.gregs[REG_CR2];
141# else
142 uintptr_t uCr2 = ~(uintptr_t)0;
143# endif
144
145# else
146 uintptr_t *puPC = NULL;
147 uintptr_t *puSP = NULL;
148 uintptr_t uTrapNo = ~(uintptr_t)0;
149 uintptr_t uErr = ~(uintptr_t)0;
150 uintptr_t uCr2 = ~(uintptr_t)0;
151# endif
152 if (uTrapNo == X86_XCPT_PF)
153 RTAssertMsg2("tstX86-1: Trap #%#04x err=%#06x at %p / %p\n", uTrapNo, uErr, *puPC, uCr2);
154 else
155 RTAssertMsg2("tstX86-1: Trap #%#04x err=%#06x at %p\n", uTrapNo, uErr, *puPC);
156
157 PCTRAPINFO pTrapInfo = findTrapInfo(*puPC, *puSP);
158 if (pTrapInfo)
159 {
160 if (pTrapInfo->u8Trap != uTrapNo && uTrapNo != ~(uintptr_t)0)
161 RTAssertMsg2("tstX86-1: Expected #%#04x, got #%#04x\n", pTrapInfo->u8Trap, uTrapNo);
162 else
163 {
164 if (*puPC != pTrapInfo->uTrapPC)
165 *puSP += sizeof(uintptr_t);
166 *puPC = pTrapInfo->uResumePC;
167 return;
168 }
169 }
170 else
171 RTAssertMsg2("tstX86-1: Unexpected trap!\n");
172
173 /* die */
174 signal(iSig, SIG_IGN);
175}
176#else
177
178#endif
179
180
181int main()
182{
183 /*
184 * Set up the test environment.
185 */
186 RTTEST hTest;
187 RTEXITCODE rcExit = RTTestInitAndCreate("tstX86-1", &hTest);
188 if (rcExit != RTEXITCODE_SUCCESS)
189 return rcExit;
190 RTTestBanner(hTest);
191
192 g_pbEfPage = (uint8_t *)RTTestGuardedAllocTail(hTest, PAGE_SIZE);
193 RTTESTI_CHECK(g_pbEfPage != NULL);
194
195 g_pbEfExecPage = (uint8_t *)RTMemExecAlloc(PAGE_SIZE*2);
196 RTTESTI_CHECK(g_pbEfExecPage != NULL);
197 RTTESTI_CHECK(!((uintptr_t)g_pbEfExecPage & PAGE_OFFSET_MASK));
198 RTTESTI_CHECK_RC(RTMemProtect(g_pbEfExecPage + PAGE_SIZE, PAGE_SIZE, RTMEM_PROT_NONE), VINF_SUCCESS);
199
200#ifdef USE_SIGNAL
201 static int const s_aiSigs[] = { SIGBUS, SIGSEGV, SIGFPE, SIGILL };
202 for (unsigned i = 0; i < RT_ELEMENTS(s_aiSigs); i++)
203 {
204 struct sigaction SigAct;
205 RTTESTI_CHECK_BREAK(sigaction(s_aiSigs[i], NULL, &SigAct) == 0);
206 SigAct.sa_sigaction = sigHandler;
207 SigAct.sa_flags |= SA_SIGINFO;
208 RTTESTI_CHECK(sigaction(s_aiSigs[i], &SigAct, NULL) == 0);
209 }
210#else
211 /** @todo implement me. */
212#endif
213
214
215 if (!RTTestErrorCount(hTest))
216 {
217 /*
218 * Do the testing.
219 */
220 int32_t rc;
221#if 0
222 RTTestSub(hTest, "Misc 1");
223 rc = x861_Test1();
224 if (rc != 0)
225 RTTestFailed(hTest, "x861_Test1 -> %d", rc);
226
227 RTTestSub(hTest, "Prefixes and groups");
228 rc = x861_Test2();
229 if (rc != 0)
230 RTTestFailed(hTest, "x861_Test2 -> %d", rc);
231
232 RTTestSub(hTest, "fxsave / fxrstor and #PFs");
233 rc = x861_Test3();
234 if (rc != 0)
235 RTTestFailed(hTest, "x861_Test3 -> %d", rc);
236
237 RTTestSub(hTest, "Multibyte NOPs");
238 rc = x861_Test4();
239 if (rc != 0)
240 RTTestFailed(hTest, "x861_Test4 -> %d", rc);
241
242 RTTestSub(hTest, "Odd floating point encodings");
243 rc = x861_Test5();
244 if (rc != 0)
245 RTTestFailed(hTest, "x861_Test5 -> %d", rc);
246#endif
247
248 RTTestSub(hTest, "Floating point exceptions ++");
249 rc = x861_Test6();
250 if (rc != 0)
251 RTTestFailed(hTest, "x861_Test6 -> %d", rc);
252 }
253
254 return RTTestSummaryAndDestroy(hTest);
255}
256
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