1 | /* $Id: tstVMMR0CallHost-1.cpp 18848 2009-04-08 16:28:35Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Testcase for the VMMR0JMPBUF operations.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
18 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
19 | * additional information or have any questions.
|
---|
20 | */
|
---|
21 |
|
---|
22 | /*******************************************************************************
|
---|
23 | * Header Files *
|
---|
24 | *******************************************************************************/
|
---|
25 | #include <iprt/initterm.h>
|
---|
26 | #include <iprt/string.h>
|
---|
27 | #include <iprt/stream.h>
|
---|
28 | #include <iprt/alloca.h>
|
---|
29 | #include <iprt/test.h>
|
---|
30 | #include <VBox/err.h>
|
---|
31 |
|
---|
32 | #define IN_VMM_R0
|
---|
33 | #define IN_RING0 /* pretent we're in Ring-0 to get the prototypes. */
|
---|
34 | #include <VBox/vmm.h>
|
---|
35 | #include "VMMInternal.h"
|
---|
36 |
|
---|
37 |
|
---|
38 | /*******************************************************************************
|
---|
39 | * Defined Constants And Macros *
|
---|
40 | *******************************************************************************/
|
---|
41 | #ifdef RT_OS_DARWIN
|
---|
42 | # define VMM_R0_SWITCH_STACK
|
---|
43 | #endif
|
---|
44 |
|
---|
45 |
|
---|
46 | /*******************************************************************************
|
---|
47 | * Global Variables *
|
---|
48 | *******************************************************************************/
|
---|
49 | /** The jump buffer. */
|
---|
50 | static VMMR0JMPBUF g_Jmp;
|
---|
51 | /** The number of jumps we've done. */
|
---|
52 | static unsigned volatile g_cJmps;
|
---|
53 |
|
---|
54 |
|
---|
55 | int foo(int i, int iZero, int iMinusOne)
|
---|
56 | {
|
---|
57 | /* allocate a buffer which we fill up to the end. */
|
---|
58 | size_t cb = (i % 5555) + 32;
|
---|
59 | char *pv = (char *)alloca(cb);
|
---|
60 | RTStrPrintf(pv, cb, "i=%d%*s\n", i, cb, "");
|
---|
61 |
|
---|
62 | /* Do long jmps every 7th time */
|
---|
63 | if ((i % 7) == 0)
|
---|
64 | {
|
---|
65 | g_cJmps++;
|
---|
66 | int rc = vmmR0CallHostLongJmp(&g_Jmp, 42);
|
---|
67 | if (!rc)
|
---|
68 | return i + 10000;
|
---|
69 | return -1;
|
---|
70 | }
|
---|
71 | NOREF(iMinusOne);
|
---|
72 | return i;
|
---|
73 | }
|
---|
74 |
|
---|
75 |
|
---|
76 | DECLCALLBACK(int) tst2(intptr_t i, intptr_t i2)
|
---|
77 | {
|
---|
78 | RTTESTI_CHECK_MSG_RET(i >= 0 && i <= 8192, ("i=%d is out of range [0..8192]\n", i), 1);
|
---|
79 | RTTESTI_CHECK_MSG_RET(i2 == 0, ("i2=%d is out of range [0]\n", i2), 1);
|
---|
80 | int iExpect = (i % 7) == 0 ? i + 10000 : i;
|
---|
81 | int rc = foo(i, 0, -1);
|
---|
82 | RTTESTI_CHECK_MSG_RET(rc == iExpect, ("i=%d rc=%d expected=%d\n", i, rc, iExpect), 1);
|
---|
83 | return 0;
|
---|
84 | }
|
---|
85 |
|
---|
86 |
|
---|
87 | void tst(int iFrom, int iTo, int iInc)
|
---|
88 | {
|
---|
89 | #ifdef VMM_R0_SWITCH_STACK
|
---|
90 | int const cIterations = iFrom > iTo ? iFrom - iTo : iTo - iFrom;
|
---|
91 | void *pvPrev = alloca(1);
|
---|
92 | #endif
|
---|
93 |
|
---|
94 | g_cJmps = 0;
|
---|
95 | for (int i = iFrom, iItr = 0; i != iTo; i += iInc, iItr++)
|
---|
96 | {
|
---|
97 | int rc = vmmR0CallHostSetJmp(&g_Jmp, (PFNVMMR0SETJMP)tst2, (PVM)i, 0);
|
---|
98 | RTTESTI_CHECK_MSG_RETV(rc == 0 || rc == 42, ("i=%d rc=%d setjmp\n", i, rc));
|
---|
99 |
|
---|
100 | #ifdef VMM_R0_SWITCH_STACK
|
---|
101 | /* Make the stack pointer slide for the second half of the calls. */
|
---|
102 | if (iItr >= cIterations / 2)
|
---|
103 | {
|
---|
104 | /* Note! gcc does funny rounding up of alloca(). */
|
---|
105 | void *pv2 = alloca((i % 63) | 1);
|
---|
106 | size_t cb2 = (uintptr_t)pvPrev - (uintptr_t)pv2;
|
---|
107 | RTTESTI_CHECK_MSG(cb2 >= 16 && cb2 <= 128, ("cb2=%zu pv2=%p pvPrev=%p iAlloca=%d\n", cb2, pv2, pvPrev, iItr));
|
---|
108 | memset(pv2, 0xff, cb2);
|
---|
109 | memset(pvPrev, 0xee, 1);
|
---|
110 | pvPrev = pv2;
|
---|
111 | }
|
---|
112 | #endif
|
---|
113 | }
|
---|
114 | RTTESTI_CHECK_MSG_RETV(g_cJmps, ("No jumps!"));
|
---|
115 | }
|
---|
116 |
|
---|
117 |
|
---|
118 | int main()
|
---|
119 | {
|
---|
120 | /*
|
---|
121 | * Init.
|
---|
122 | */
|
---|
123 | RTTEST hTest;
|
---|
124 | int rc;
|
---|
125 | if ( RT_FAILURE(rc = RTR3Init())
|
---|
126 | || RT_FAILURE(rc = RTTestCreate("tstVMMR0CallHost-1", &hTest)))
|
---|
127 | {
|
---|
128 | RTStrmPrintf(g_pStdErr, "tstVMMR0CallHost-1: Fatal error during init: %Rrc\n", rc);
|
---|
129 | return 1;
|
---|
130 | }
|
---|
131 | RTTestBanner(hTest);
|
---|
132 |
|
---|
133 | g_Jmp.pvSavedStack = (RTR0PTR)RTTestGuardedAllocTail(hTest, 8192);
|
---|
134 |
|
---|
135 | /*
|
---|
136 | * Run two test with about 1000 long jumps each.
|
---|
137 | */
|
---|
138 | RTTestSub(hTest, "Increasing stack usage");
|
---|
139 | tst(0, 7000, 1);
|
---|
140 | RTTestSub(hTest, "Decreasing stack usage");
|
---|
141 | tst(7599, 0, -1);
|
---|
142 |
|
---|
143 | return RTTestSummaryAndDestroy(hTest);
|
---|
144 | }
|
---|