1 | /* $Id: tstX86-1.cpp 36768 2011-04-20 18:33:29Z 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 |
|
---|
25 | #ifdef RT_OS_WINDOWS
|
---|
26 | # include <Windows.h>
|
---|
27 | #else
|
---|
28 | # ifdef RT_OS_DARWIN
|
---|
29 | # define _XOPEN_SOURCE
|
---|
30 | # endif
|
---|
31 | # include <signal.h>
|
---|
32 | # include <ucontext.h>
|
---|
33 | # define USE_SIGNAL
|
---|
34 | #endif
|
---|
35 |
|
---|
36 |
|
---|
37 | /*******************************************************************************
|
---|
38 | * Structures and Typedefs *
|
---|
39 | *******************************************************************************/
|
---|
40 | typedef struct TRAPINFO
|
---|
41 | {
|
---|
42 | uintptr_t uTrapPC;
|
---|
43 | uintptr_t uResumePC;
|
---|
44 | uint8_t u8Trap;
|
---|
45 | uint8_t cbInstr;
|
---|
46 | uint8_t auAlignment[sizeof(uintptr_t) * 2 - 2];
|
---|
47 | } TRAPINFO;
|
---|
48 | typedef TRAPINFO const *PCTRAPINFO;
|
---|
49 |
|
---|
50 | /*******************************************************************************
|
---|
51 | * Global Variables *
|
---|
52 | *******************************************************************************/
|
---|
53 | RT_C_DECLS_BEGIN
|
---|
54 | uint8_t *g_pbEfPage = NULL;
|
---|
55 | extern TRAPINFO g_aTrapInfo[];
|
---|
56 | RT_C_DECLS_END
|
---|
57 |
|
---|
58 |
|
---|
59 | /*******************************************************************************
|
---|
60 | * Internal Functions *
|
---|
61 | *******************************************************************************/
|
---|
62 | DECLASM(int32_t) x861_Test1(void);
|
---|
63 |
|
---|
64 |
|
---|
65 |
|
---|
66 | static PCTRAPINFO findTrapInfo(uintptr_t uTrapPC)
|
---|
67 | {
|
---|
68 | for (unsigned i = 0; g_aTrapInfo[i].uTrapPC; i++)
|
---|
69 | if (g_aTrapInfo[i].uTrapPC == uTrapPC)
|
---|
70 | return &g_aTrapInfo[i];
|
---|
71 |
|
---|
72 | return NULL;
|
---|
73 | }
|
---|
74 |
|
---|
75 | #ifdef USE_SIGNAL
|
---|
76 | static void sigHandler(int iSig, siginfo_t *pSigInfo, void *pvSigCtx)
|
---|
77 | {
|
---|
78 | ucontext_t *pCtx = (ucontext_t *)pvSigCtx;
|
---|
79 | # if defined(RT_ARCH_AMD64) && defined(RT_OS_DARWIN)
|
---|
80 | uintptr_t *puPC = (uintptr_t *)&pCtx->uc_mcontext->__ss.__rip;
|
---|
81 | # elif defined(RT_ARCH_AMD64)
|
---|
82 | uintptr_t *puPC = (uintptr_t *)&pCtx->uc_mcontext.gregs[REG_RIP];
|
---|
83 | # elif defined(RT_ARCH_X86) && defined(RT_OS_DARWIN)
|
---|
84 | uintptr_t *puPC = (uintptr_t *)&pCtx->uc_mcontext->__ss.__eip;
|
---|
85 | # elif defined(RT_ARCH_X86)
|
---|
86 | uintptr_t *puPC = (uintptr_t *)&pCtx->uc_mcontext.gregs[REG_EIP];
|
---|
87 | # else
|
---|
88 | uintptr_t *puPC = NULL;
|
---|
89 | # endif
|
---|
90 |
|
---|
91 | PCTRAPINFO pTrapInfo = findTrapInfo(*puPC);
|
---|
92 | if (pTrapInfo)
|
---|
93 | {
|
---|
94 | /** @todo verify the kind of trap */
|
---|
95 | *puPC = pTrapInfo->uResumePC;
|
---|
96 | return;
|
---|
97 | }
|
---|
98 |
|
---|
99 | /* die */
|
---|
100 | signal(iSig, SIG_IGN);
|
---|
101 | }
|
---|
102 | #else
|
---|
103 |
|
---|
104 | #endif
|
---|
105 |
|
---|
106 | int main()
|
---|
107 | {
|
---|
108 | /*
|
---|
109 | * Set up the test environment.
|
---|
110 | */
|
---|
111 | RTTEST hTest;
|
---|
112 | RTEXITCODE rcExit = RTTestInitAndCreate("tstX86-1", &hTest);
|
---|
113 | if (rcExit != RTEXITCODE_SUCCESS)
|
---|
114 | return rcExit;
|
---|
115 | g_pbEfPage = (uint8_t *)RTTestGuardedAllocTail(hTest, PAGE_SIZE);
|
---|
116 | RTTESTI_CHECK(g_pbEfPage != NULL);
|
---|
117 |
|
---|
118 | #ifdef USE_SIGNAL
|
---|
119 | static int const s_aiSigs[] = { SIGBUS, SIGSEGV, SIGFPE };
|
---|
120 | for (unsigned i = 0; i < RT_ELEMENTS(s_aiSigs); i++)
|
---|
121 | {
|
---|
122 | struct sigaction SigAct;
|
---|
123 | RTTESTI_CHECK_BREAK(sigaction(s_aiSigs[i], NULL, &SigAct) == 0);
|
---|
124 | SigAct.sa_sigaction = sigHandler;
|
---|
125 | SigAct.sa_flags |= SA_SIGINFO;
|
---|
126 | RTTESTI_CHECK(sigaction(s_aiSigs[i], &SigAct, NULL) == 0);
|
---|
127 | }
|
---|
128 | #else
|
---|
129 | /** @todo implement me. */
|
---|
130 | #endif
|
---|
131 |
|
---|
132 |
|
---|
133 | if (!RTTestErrorCount(hTest))
|
---|
134 | {
|
---|
135 |
|
---|
136 | /*
|
---|
137 | * Do the testing.
|
---|
138 | */
|
---|
139 | RTTestSub(hTest, "part 1");
|
---|
140 | int32_t rc = x861_Test1();
|
---|
141 | if (rc != 0)
|
---|
142 | RTTestFailed(hTest, "x861_Test1 -> %d", rc);
|
---|
143 | }
|
---|
144 |
|
---|
145 | return RTTestSummaryAndDestroy(hTest);
|
---|
146 | }
|
---|
147 |
|
---|