1 | /* $Id: tstVMMR0CallHost-1.cpp 41965 2012-06-29 02:52:49Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Testcase for the VMMR0JMPBUF operations.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 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 | * Header Files *
|
---|
20 | *******************************************************************************/
|
---|
21 | #include <VBox/err.h>
|
---|
22 | #include <VBox/param.h>
|
---|
23 | #include <iprt/alloca.h>
|
---|
24 | #include <iprt/initterm.h>
|
---|
25 | #include <iprt/string.h>
|
---|
26 | #include <iprt/stream.h>
|
---|
27 | #include <iprt/test.h>
|
---|
28 |
|
---|
29 | #define IN_VMM_R0
|
---|
30 | #define IN_RING0 /* pretent we're in Ring-0 to get the prototypes. */
|
---|
31 | #include <VBox/vmm/vmm.h>
|
---|
32 | #include "VMMInternal.h"
|
---|
33 |
|
---|
34 |
|
---|
35 | /*******************************************************************************
|
---|
36 | * Defined Constants And Macros *
|
---|
37 | *******************************************************************************/
|
---|
38 | #if !defined(VMM_R0_SWITCH_STACK) && !defined(VMM_R0_NO_SWITCH_STACK)
|
---|
39 | # error "VMM_R0_SWITCH_STACK or VMM_R0_NO_SWITCH_STACK has to be defined."
|
---|
40 | #endif
|
---|
41 |
|
---|
42 |
|
---|
43 | /*******************************************************************************
|
---|
44 | * Global Variables *
|
---|
45 | *******************************************************************************/
|
---|
46 | /** The jump buffer. */
|
---|
47 | static VMMR0JMPBUF g_Jmp;
|
---|
48 | /** The number of jumps we've done. */
|
---|
49 | static unsigned volatile g_cJmps;
|
---|
50 | /** Number of bytes allocated last time we called foo(). */
|
---|
51 | static size_t volatile g_cbFoo;
|
---|
52 | /** Number of bytes used last time we called foo(). */
|
---|
53 | static intptr_t volatile g_cbFooUsed;
|
---|
54 |
|
---|
55 |
|
---|
56 | int foo(int i, int iZero, int iMinusOne)
|
---|
57 | {
|
---|
58 | NOREF(iZero);
|
---|
59 |
|
---|
60 | /* allocate a buffer which we fill up to the end. */
|
---|
61 | size_t cb = (i % 1555) + 32;
|
---|
62 | g_cbFoo = cb;
|
---|
63 | char *pv = (char *)alloca(cb);
|
---|
64 | RTStrPrintf(pv, cb, "i=%d%*s\n", i, cb, "");
|
---|
65 | #ifdef VMM_R0_SWITCH_STACK
|
---|
66 | g_cbFooUsed = VMM_STACK_SIZE - ((uintptr_t)pv - (uintptr_t)g_Jmp.pvSavedStack);
|
---|
67 | RTTESTI_CHECK_MSG_RET(g_cbFooUsed < (intptr_t)VMM_STACK_SIZE - 128, ("%#x - (%p - %p) -> %#x; cb=%#x i=%d\n", VMM_STACK_SIZE, pv, g_Jmp.pvSavedStack, g_cbFooUsed, cb, i), -15);
|
---|
68 | #elif defined(RT_ARCH_AMD64)
|
---|
69 | g_cbFooUsed = (uintptr_t)g_Jmp.rsp - (uintptr_t)pv;
|
---|
70 | RTTESTI_CHECK_MSG_RET(g_cbFooUsed < VMM_STACK_SIZE - 128, ("%p - %p -> %#x; cb=%#x i=%d\n", g_Jmp.rsp, pv, g_cbFooUsed, cb, i), -15);
|
---|
71 | #elif defined(RT_ARCH_X86)
|
---|
72 | g_cbFooUsed = (uintptr_t)g_Jmp.esp - (uintptr_t)pv;
|
---|
73 | RTTESTI_CHECK_MSG_RET(g_cbFooUsed < (intptr_t)VMM_STACK_SIZE - 128, ("%p - %p -> %#x; cb=%#x i=%d\n", g_Jmp.esp, pv, g_cbFooUsed, cb, i), -15);
|
---|
74 | #endif
|
---|
75 |
|
---|
76 | /* Do long jmps every 7th time */
|
---|
77 | if ((i % 7) == 0)
|
---|
78 | {
|
---|
79 | g_cJmps++;
|
---|
80 | int rc = vmmR0CallRing3LongJmp(&g_Jmp, 42);
|
---|
81 | if (!rc)
|
---|
82 | return i + 10000;
|
---|
83 | return -1;
|
---|
84 | }
|
---|
85 | NOREF(iMinusOne);
|
---|
86 | return i;
|
---|
87 | }
|
---|
88 |
|
---|
89 |
|
---|
90 | DECLCALLBACK(int) tst2(intptr_t i, intptr_t i2)
|
---|
91 | {
|
---|
92 | RTTESTI_CHECK_MSG_RET(i >= 0 && i <= 8192, ("i=%d is out of range [0..8192]\n", i), 1);
|
---|
93 | RTTESTI_CHECK_MSG_RET(i2 == 0, ("i2=%d is out of range [0]\n", i2), 1);
|
---|
94 | int iExpect = (i % 7) == 0 ? i + 10000 : i;
|
---|
95 | int rc = foo(i, 0, -1);
|
---|
96 | RTTESTI_CHECK_MSG_RET(rc == iExpect, ("i=%d rc=%d expected=%d\n", i, rc, iExpect), 1);
|
---|
97 | return 0;
|
---|
98 | }
|
---|
99 |
|
---|
100 |
|
---|
101 | void tst(int iFrom, int iTo, int iInc)
|
---|
102 | {
|
---|
103 | #ifdef VMM_R0_SWITCH_STACK
|
---|
104 | int const cIterations = iFrom > iTo ? iFrom - iTo : iTo - iFrom;
|
---|
105 | void *pvPrev = alloca(1);
|
---|
106 | #endif
|
---|
107 |
|
---|
108 | RTR0PTR R0PtrSaved = g_Jmp.pvSavedStack;
|
---|
109 | RT_ZERO(g_Jmp);
|
---|
110 | g_Jmp.pvSavedStack = R0PtrSaved;
|
---|
111 | memset((void *)g_Jmp.pvSavedStack, '\0', VMM_STACK_SIZE);
|
---|
112 | g_cbFoo = 0;
|
---|
113 | g_cJmps = 0;
|
---|
114 | g_cbFooUsed = 0;
|
---|
115 |
|
---|
116 | for (int i = iFrom, iItr = 0; i != iTo; i += iInc, iItr++)
|
---|
117 | {
|
---|
118 | int rc = vmmR0CallRing3SetJmp(&g_Jmp, (PFNVMMR0SETJMP)tst2, (PVM)i, 0);
|
---|
119 | RTTESTI_CHECK_MSG_RETV(rc == 0 || rc == 42, ("i=%d rc=%d setjmp; cbFoo=%#x cbFooUsed=%#x\n", i, rc, g_cbFoo, g_cbFooUsed));
|
---|
120 |
|
---|
121 | #ifdef VMM_R0_SWITCH_STACK
|
---|
122 | /* Make the stack pointer slide for the second half of the calls. */
|
---|
123 | if (iItr >= cIterations / 2)
|
---|
124 | {
|
---|
125 | /* Note! gcc does funny rounding up of alloca(). */
|
---|
126 | void *pv2 = alloca((i % 63) | 1);
|
---|
127 | size_t cb2 = (uintptr_t)pvPrev - (uintptr_t)pv2;
|
---|
128 | RTTESTI_CHECK_MSG(cb2 >= 16 && cb2 <= 128, ("cb2=%zu pv2=%p pvPrev=%p iAlloca=%d\n", cb2, pv2, pvPrev, iItr));
|
---|
129 | memset(pv2, 0xff, cb2);
|
---|
130 | memset(pvPrev, 0xee, 1);
|
---|
131 | pvPrev = pv2;
|
---|
132 | }
|
---|
133 | #endif
|
---|
134 | }
|
---|
135 | RTTESTI_CHECK_MSG_RETV(g_cJmps, ("No jumps!"));
|
---|
136 | if (g_Jmp.cbUsedAvg || g_Jmp.cUsedTotal)
|
---|
137 | RTTestIPrintf(RTTESTLVL_ALWAYS, "cbUsedAvg=%#x cbUsedMax=%#x cUsedTotal=%#llx\n",
|
---|
138 | g_Jmp.cbUsedAvg, g_Jmp.cbUsedMax, g_Jmp.cUsedTotal);
|
---|
139 | }
|
---|
140 |
|
---|
141 |
|
---|
142 | int main()
|
---|
143 | {
|
---|
144 | /*
|
---|
145 | * Init.
|
---|
146 | */
|
---|
147 | RTTEST hTest;
|
---|
148 | int rc;
|
---|
149 | if ( RT_FAILURE(rc = RTR3InitExeNoArguments(0))
|
---|
150 | || RT_FAILURE(rc = RTTestCreate("tstVMMR0CallHost-1", &hTest)))
|
---|
151 | {
|
---|
152 | RTStrmPrintf(g_pStdErr, "tstVMMR0CallHost-1: Fatal error during init: %Rrc\n", rc);
|
---|
153 | return 1;
|
---|
154 | }
|
---|
155 | RTTestBanner(hTest);
|
---|
156 |
|
---|
157 | g_Jmp.pvSavedStack = (RTR0PTR)RTTestGuardedAllocTail(hTest, VMM_STACK_SIZE);
|
---|
158 |
|
---|
159 | /*
|
---|
160 | * Run two test with about 1000 long jumps each.
|
---|
161 | */
|
---|
162 | RTTestSub(hTest, "Increasing stack usage");
|
---|
163 | tst(0, 7000, 1);
|
---|
164 | RTTestSub(hTest, "Decreasing stack usage");
|
---|
165 | tst(7599, 0, -1);
|
---|
166 |
|
---|
167 | return RTTestSummaryAndDestroy(hTest);
|
---|
168 | }
|
---|