VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/Support/testcase/tstPin.cpp@ 18499

Last change on this file since 18499 was 14831, checked in by vboxsync, 16 years ago

whole bunch: avoid runtime.h, include individual headers indead.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.3 KB
Line 
1/** @file
2 *
3 * VBox host drivers - Ring-0 support drivers - Testcases:
4 * Test the memory locking interface
5 */
6
7/*
8 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.virtualbox.org. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License (GPL) as published by the Free Software
14 * Foundation, in version 2 as it comes in the "COPYING" file of the
15 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
16 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
17 *
18 * The contents of this file may alternatively be used under the terms
19 * of the Common Development and Distribution License Version 1.0
20 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
21 * VirtualBox OSE distribution, in which case the provisions of the
22 * CDDL are applicable instead of those of the GPL.
23 *
24 * You may elect to license modified versions of this file under the
25 * terms and conditions of either the GPL or the CDDL or both.
26 *
27 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
28 * Clara, CA 95054 USA or visit http://www.sun.com if you need
29 * additional information or have any questions.
30 */
31
32
33/*******************************************************************************
34* Header Files *
35*******************************************************************************/
36#include <VBox/sup.h>
37#include <VBox/param.h>
38#include <VBox/err.h>
39#include <iprt/initterm.h>
40#include <iprt/stream.h>
41#include <iprt/thread.h>
42#include <iprt/string.h>
43
44
45int main(int argc, char **argv)
46{
47 int rc;
48 int rcRet = 0;
49 RTHCPHYS HCPhys;
50
51 RTR3InitAndSUPLib();
52 rc = SUPR3Init(NULL);
53 RTPrintf("SUPR3Init -> rc=%d\n", rc);
54 rcRet += rc != 0;
55 if (!rc)
56 {
57 /*
58 * Simple test.
59 */
60 void *pv;
61 int rc = SUPPageAlloc(1, &pv);
62 AssertRC(rc);
63 RTPrintf("pv=%p\n", pv);
64 SUPPAGE aPages[1];
65 rc = SUPPageLock(pv, 1, &aPages[0]);
66 RTPrintf("rc=%d aPages[0]=%RHp\n", rc, pv, aPages[0]);
67 RTThreadSleep(1500);
68#if 0
69 RTPrintf("Unlocking...\n");
70 RTThreadSleep(250);
71 rc = SUPPageUnlock(pv);
72 RTPrintf("rc=%d\n", rc);
73 RTThreadSleep(1500);
74#endif
75
76 /*
77 * More extensive.
78 */
79 static struct
80 {
81 void *pv;
82 void *pvAligned;
83 SUPPAGE aPages[16];
84 } aPinnings[500];
85 for (unsigned i = 0; i < sizeof(aPinnings) / sizeof(aPinnings[0]); i++)
86 {
87 aPinnings[i].pv = NULL;
88 SUPPageAlloc(0x10000 >> PAGE_SHIFT, &aPinnings[i].pv);
89 aPinnings[i].pvAligned = RT_ALIGN_P(aPinnings[i].pv, PAGE_SIZE);
90 rc = SUPPageLock(aPinnings[i].pvAligned, 0xf000 >> PAGE_SHIFT, &aPinnings[i].aPages[0]);
91 if (!rc)
92 {
93 RTPrintf("i=%d: pvAligned=%p pv=%p:\n", i, aPinnings[i].pvAligned, aPinnings[i].pv);
94 memset(aPinnings[i].pv, 0xfa, 0x10000);
95 unsigned c4GPluss = 0;
96 for (unsigned j = 0; j < (0xf000 >> PAGE_SHIFT); j++)
97 if (aPinnings[i].aPages[j].Phys >= _4G)
98 {
99 RTPrintf("%2d: vrt=%p phys=%RHp\n", j, (char *)aPinnings[i].pvAligned + (j << PAGE_SHIFT), aPinnings[i].aPages[j].Phys);
100 c4GPluss++;
101 }
102 RTPrintf("i=%d: c4GPluss=%d\n", i, c4GPluss);
103 }
104 else
105 {
106 RTPrintf("SUPPageLock -> rc=%d\n", rc);
107 rcRet++;
108 SUPPageFree(aPinnings[i].pv, 0x10000 >> PAGE_SHIFT);
109 aPinnings[i].pv = aPinnings[i].pvAligned = NULL;
110 break;
111 }
112 }
113
114 for (unsigned i = 0; i < sizeof(aPinnings) / sizeof(aPinnings[0]); i += 2)
115 {
116 if (aPinnings[i].pvAligned)
117 {
118 rc = SUPPageUnlock(aPinnings[i].pvAligned);
119 if (rc)
120 {
121 RTPrintf("SUPPageUnlock(%p) -> rc=%d\n", aPinnings[i].pvAligned, rc);
122 rcRet++;
123 }
124 memset(aPinnings[i].pv, 0xaf, 0x10000);
125 }
126 }
127
128 for (unsigned i = 0; i < sizeof(aPinnings) / sizeof(aPinnings[0]); i += 2)
129 {
130 if (aPinnings[i].pv)
131 {
132 memset(aPinnings[i].pv, 0xcc, 0x10000);
133 SUPPageFree(aPinnings[i].pv, 0x10000 >> PAGE_SHIFT);
134 aPinnings[i].pv = NULL;
135 }
136 }
137
138
139 /*
140 * Allocate a bit of contiguous memory.
141 */
142 pv = SUPContAlloc(RT_ALIGN_Z(15003, PAGE_SIZE) >> PAGE_SHIFT, &HCPhys);
143 rcRet += pv == NULL || HCPhys == 0;
144 if (pv && HCPhys)
145 {
146 RTPrintf("SUPContAlloc(15003) -> HCPhys=%llx pv=%p\n", HCPhys, pv);
147 void *pv0 = pv;
148 memset(pv0, 0xaf, 15003);
149 pv = SUPContAlloc(RT_ALIGN_Z(12999, PAGE_SIZE) >> PAGE_SHIFT, &HCPhys);
150 rcRet += pv == NULL || HCPhys == 0;
151 if (pv && HCPhys)
152 {
153 RTPrintf("SUPContAlloc(12999) -> HCPhys=%llx pv=%p\n", HCPhys, pv);
154 memset(pv, 0xbf, 12999);
155 rc = SUPContFree(pv, RT_ALIGN_Z(12999, PAGE_SIZE) >> PAGE_SHIFT);
156 rcRet += rc != 0;
157 if (rc)
158 RTPrintf("SUPContFree failed! rc=%d\n", rc);
159 }
160 else
161 RTPrintf("SUPContAlloc (2nd) failed!\n");
162 memset(pv0, 0xaf, 15003);
163 /* pv0 is intentionally not freed! */
164 }
165 else
166 RTPrintf("SUPContAlloc failed!\n");
167
168 /*
169 * Allocate a big chunk of virtual memory and then lock it.
170 */
171 #define BIG_SIZE 72*1024*1024
172 #define BIG_SIZEPP (BIG_SIZE + PAGE_SIZE)
173 pv = NULL;
174 SUPPageAlloc(BIG_SIZEPP >> PAGE_SHIFT, &pv);
175 if (pv)
176 {
177 static SUPPAGE aPages[BIG_SIZE >> PAGE_SHIFT];
178 void *pvAligned = RT_ALIGN_P(pv, PAGE_SIZE);
179 rc = SUPPageLock(pvAligned, BIG_SIZE >> PAGE_SHIFT, &aPages[0]);
180 if (!rc)
181 {
182 /* dump */
183 RTPrintf("SUPPageLock(%p,%d,) succeeded!\n", pvAligned, BIG_SIZE);
184 memset(pv, 0x42, BIG_SIZEPP);
185 #if 0
186 for (unsigned j = 0; j < (BIG_SIZE >> PAGE_SHIFT); j++)
187 RTPrintf("%2d: vrt=%p phys=%08x\n", j, (char *)pvAligned + (j << PAGE_SHIFT), (uintptr_t)aPages[j].pvPhys);
188 #endif
189
190 /* unlock */
191 rc = SUPPageUnlock(pvAligned);
192 if (rc)
193 {
194 RTPrintf("SUPPageUnlock(%p) -> rc=%d\n", pvAligned, rc);
195 rcRet++;
196 }
197 memset(pv, 0xcc, BIG_SIZEPP);
198 }
199 else
200 {
201 RTPrintf("SUPPageLock(%p) -> rc=%d\n", pvAligned, rc);
202 rcRet++;
203 }
204 SUPPageFree(pv, BIG_SIZEPP >> PAGE_SHIFT);
205 }
206
207 rc = SUPTerm();
208 RTPrintf("SUPTerm -> rc=%d\n", rc);
209 rcRet += rc != 0;
210 }
211
212 return rcRet;
213}
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use