VirtualBox

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

Last change on this file since 38324 was 36865, checked in by vboxsync, 14 years ago

build fix

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.7 KB
Line 
1/* $Id: tstX86-1.cpp 36865 2011-04-28 00:59:50Z 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*******************************************************************************/
43typedef 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;
51typedef TRAPINFO const *PCTRAPINFO;
52
53
54/*******************************************************************************
55* Global Variables *
56*******************************************************************************/
57RT_C_DECLS_BEGIN
58uint8_t *g_pbEfPage = NULL;
59uint8_t *g_pbEfExecPage = NULL;
60extern TRAPINFO g_aTrapInfo[];
61RT_C_DECLS_END
62
63
64/*******************************************************************************
65* Internal Functions *
66*******************************************************************************/
67DECLASM(int32_t) x861_Test1(void);
68
69
70
71static 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
88static void sigHandler(int iSig, siginfo_t *pSigInfo, void *pvSigCtx)
89{
90 ucontext_t *pCtx = (ucontext_t *)pvSigCtx;
91
92# if defined(RT_ARCH_AMD64) && defined(RT_OS_DARWIN)
93 uintptr_t *puPC = (uintptr_t *)&pCtx->uc_mcontext->__ss.__rip;
94 uintptr_t *puSP = (uintptr_t *)&pCtx->uc_mcontext->__ss.__rsp;
95 uintptr_t uTrapNo = pCtx->uc_mcontext->__es.__trapno;
96 uintptr_t uErr = pCtx->uc_mcontext->__es.__err;
97
98# elif defined(RT_ARCH_AMD64) && defined(RT_OS_FREEBSD)
99 uintptr_t *puPC = (uintptr_t *)&pCtx->uc_mcontext.mc_rip;
100 uintptr_t *puSP = (uintptr_t *)&pCtx->uc_mcontext.mc_rsp;
101 uintptr_t uTrapNo = ~(uintptr_t)0;
102 uintptr_t uErr = ~(uintptr_t)0;
103
104# elif defined(RT_ARCH_AMD64)
105 uintptr_t *puPC = (uintptr_t *)&pCtx->uc_mcontext.gregs[REG_RIP];
106 uintptr_t *puSP = (uintptr_t *)&pCtx->uc_mcontext.gregs[REG_RSP];
107 uintptr_t uTrapNo = pCtx->uc_mcontext.gregs[REG_TRAPNO];
108 uintptr_t uErr = pCtx->uc_mcontext.gregs[REG_ERR];
109
110# elif defined(RT_ARCH_X86) && defined(RT_OS_DARWIN)
111 uintptr_t *puPC = (uintptr_t *)&pCtx->uc_mcontext->__ss.__eip;
112 uintptr_t *puSP = (uintptr_t *)&pCtx->uc_mcontext->__ss.__esp;
113 uintptr_t uTrapNo = pCtx->uc_mcontext->__es.__trapno;
114 uintptr_t uErr = pCtx->uc_mcontext->__es.__err;
115
116# elif defined(RT_ARCH_X86) && defined(RT_OS_FREEBSD)
117 uintptr_t *puPC = (uintptr_t *)&pCtx->uc_mcontext.mc_eip;
118 uintptr_t *puSP = (uintptr_t *)&pCtx->uc_mcontext.mc_esp;
119 uintptr_t uTrapNo = ~(uintptr_t)0;
120 uintptr_t uErr = ~(uintptr_t)0;
121
122# elif defined(RT_ARCH_X86)
123 uintptr_t *puPC = (uintptr_t *)&pCtx->uc_mcontext.gregs[REG_EIP];
124 uintptr_t *puSP = (uintptr_t *)&pCtx->uc_mcontext.gregs[REG_ESP];
125 uintptr_t uTrapNo = pCtx->uc_mcontext.gregs[REG_TRAPNO];
126 uintptr_t uErr = pCtx->uc_mcontext.gregs[REG_ERR];
127
128# else
129 uintptr_t *puPC = NULL;
130 uintptr_t *puSP = NULL;
131 uintptr_t uTrapNo = ~(uintptr_t)0;
132 uintptr_t uErr = ~(uintptr_t)0;
133# endif
134 RTAssertMsg2("tstX86-1: Trap #%#04x err=%#06x at %p\n", uTrapNo, uErr, *puPC);
135
136 PCTRAPINFO pTrapInfo = findTrapInfo(*puPC, *puSP);
137 if (pTrapInfo)
138 {
139 if (pTrapInfo->u8Trap != uTrapNo && uTrapNo != ~(uintptr_t)0)
140 RTAssertMsg2("tstX86-1: Expected #%#04x, got #%#04x\n", pTrapInfo->u8Trap, uTrapNo);
141 else
142 {
143 if (*puPC != pTrapInfo->uTrapPC)
144 *puSP += sizeof(uintptr_t);
145 *puPC = pTrapInfo->uResumePC;
146 return;
147 }
148 }
149 else
150 RTAssertMsg2("tstX86-1: Unexpected trap!\n");
151
152 /* die */
153 signal(iSig, SIG_IGN);
154}
155#else
156
157#endif
158
159int main()
160{
161 /*
162 * Set up the test environment.
163 */
164 RTTEST hTest;
165 RTEXITCODE rcExit = RTTestInitAndCreate("tstX86-1", &hTest);
166 if (rcExit != RTEXITCODE_SUCCESS)
167 return rcExit;
168 RTTestBanner(hTest);
169
170 g_pbEfPage = (uint8_t *)RTTestGuardedAllocTail(hTest, PAGE_SIZE);
171 RTTESTI_CHECK(g_pbEfPage != NULL);
172
173 g_pbEfExecPage = (uint8_t *)RTMemExecAlloc(PAGE_SIZE*2);
174 RTTESTI_CHECK(g_pbEfExecPage != NULL);
175 RTTESTI_CHECK(!((uintptr_t)g_pbEfExecPage & PAGE_OFFSET_MASK));
176 RTTESTI_CHECK_RC(RTMemProtect(g_pbEfExecPage + PAGE_SIZE, PAGE_SIZE, RTMEM_PROT_NONE), VINF_SUCCESS);
177
178#ifdef USE_SIGNAL
179 static int const s_aiSigs[] = { SIGBUS, SIGSEGV, SIGFPE, SIGILL };
180 for (unsigned i = 0; i < RT_ELEMENTS(s_aiSigs); i++)
181 {
182 struct sigaction SigAct;
183 RTTESTI_CHECK_BREAK(sigaction(s_aiSigs[i], NULL, &SigAct) == 0);
184 SigAct.sa_sigaction = sigHandler;
185 SigAct.sa_flags |= SA_SIGINFO;
186 RTTESTI_CHECK(sigaction(s_aiSigs[i], &SigAct, NULL) == 0);
187 }
188#else
189 /** @todo implement me. */
190#endif
191
192
193 if (!RTTestErrorCount(hTest))
194 {
195 /*
196 * Do the testing.
197 */
198 RTTestSub(hTest, "part 1");
199 int32_t rc = x861_Test1();
200 if (rc != 0)
201 RTTestFailed(hTest, "x861_Test1 -> %d", rc);
202 }
203
204 return RTTestSummaryAndDestroy(hTest);
205}
206
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