VirtualBox

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

Last change on this file since 9380 was 8155, checked in by vboxsync, 17 years ago

The Big Sun Rebranding Header Change

  • 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 <iprt/runtime.h>
39#include <iprt/stream.h>
40#include <stdlib.h>
41#include <string.h>
42
43
44int main(int argc, char **argv)
45{
46 int rc;
47 int rcRet = 0;
48 RTHCPHYS HCPhys;
49
50 RTR3Init(true, ~0);
51 rc = SUPInit(NULL, ~0);
52 RTPrintf("SUPInit -> rc=%d\n", rc);
53 rcRet += rc != 0;
54 if (!rc)
55 {
56 /*
57 * Simple test.
58 */
59 void *pv;
60 int rc = SUPPageAlloc(1, &pv);
61 AssertRC(rc);
62 RTPrintf("pv=%p\n", pv);
63 SUPPAGE aPages[1];
64 rc = SUPPageLock(pv, 1, &aPages[0]);
65 RTPrintf("rc=%d aPages[0]=%RHp\n", rc, pv, aPages[0]);
66 RTThreadSleep(1500);
67#if 0
68 RTPrintf("Unlocking...\n");
69 RTThreadSleep(250);
70 rc = SUPPageUnlock(pv);
71 RTPrintf("rc=%d\n", rc);
72 RTThreadSleep(1500);
73#endif
74
75 /*
76 * More extensive.
77 */
78 static struct
79 {
80 void *pv;
81 void *pvAligned;
82 SUPPAGE aPages[16];
83 } aPinnings[500];
84 for (unsigned i = 0; i < sizeof(aPinnings) / sizeof(aPinnings[0]); i++)
85 {
86 aPinnings[i].pv = NULL;
87 SUPPageAlloc(0x10000 >> PAGE_SHIFT, &aPinnings[i].pv);
88 aPinnings[i].pvAligned = RT_ALIGN_P(aPinnings[i].pv, PAGE_SIZE);
89 rc = SUPPageLock(aPinnings[i].pvAligned, 0xf000 >> PAGE_SHIFT, &aPinnings[i].aPages[0]);
90 if (!rc)
91 {
92 RTPrintf("i=%d: pvAligned=%p pv=%p:\n", i, aPinnings[i].pvAligned, aPinnings[i].pv);
93 memset(aPinnings[i].pv, 0xfa, 0x10000);
94 unsigned c4GPluss = 0;
95 for (unsigned j = 0; j < (0xf000 >> PAGE_SHIFT); j++)
96 if (aPinnings[i].aPages[j].Phys >= _4G)
97 {
98 RTPrintf("%2d: vrt=%p phys=%VHp\n", j, (char *)aPinnings[i].pvAligned + (j << PAGE_SHIFT), aPinnings[i].aPages[j].Phys);
99 c4GPluss++;
100 }
101 RTPrintf("i=%d: c4GPluss=%d\n", i, c4GPluss);
102 }
103 else
104 {
105 RTPrintf("SUPPageLock -> rc=%d\n", rc);
106 rcRet++;
107 SUPPageFree(aPinnings[i].pv, 0x10000 >> PAGE_SHIFT);
108 aPinnings[i].pv = aPinnings[i].pvAligned = NULL;
109 break;
110 }
111 }
112
113 for (unsigned i = 0; i < sizeof(aPinnings) / sizeof(aPinnings[0]); i += 2)
114 {
115 if (aPinnings[i].pvAligned)
116 {
117 rc = SUPPageUnlock(aPinnings[i].pvAligned);
118 if (rc)
119 {
120 RTPrintf("SUPPageUnlock(%p) -> rc=%d\n", aPinnings[i].pvAligned, rc);
121 rcRet++;
122 }
123 memset(aPinnings[i].pv, 0xaf, 0x10000);
124 }
125 }
126
127 for (unsigned i = 0; i < sizeof(aPinnings) / sizeof(aPinnings[0]); i += 2)
128 {
129 if (aPinnings[i].pv)
130 {
131 memset(aPinnings[i].pv, 0xcc, 0x10000);
132 SUPPageFree(aPinnings[i].pv, 0x10000 >> PAGE_SHIFT);
133 aPinnings[i].pv = NULL;
134 }
135 }
136
137
138 /*
139 * Allocate a bit of contiguous memory.
140 */
141 pv = SUPContAlloc(RT_ALIGN_Z(15003, PAGE_SIZE) >> PAGE_SHIFT, &HCPhys);
142 rcRet += pv == NULL || HCPhys == 0;
143 if (pv && HCPhys)
144 {
145 RTPrintf("SUPContAlloc(15003) -> HCPhys=%llx pv=%p\n", HCPhys, pv);
146 void *pv0 = pv;
147 memset(pv0, 0xaf, 15003);
148 pv = SUPContAlloc(RT_ALIGN_Z(12999, PAGE_SIZE) >> PAGE_SHIFT, &HCPhys);
149 rcRet += pv == NULL || HCPhys == 0;
150 if (pv && HCPhys)
151 {
152 RTPrintf("SUPContAlloc(12999) -> HCPhys=%llx pv=%p\n", HCPhys, pv);
153 memset(pv, 0xbf, 12999);
154 rc = SUPContFree(pv, RT_ALIGN_Z(12999, PAGE_SIZE) >> PAGE_SHIFT);
155 rcRet += rc != 0;
156 if (rc)
157 RTPrintf("SUPContFree failed! rc=%d\n", rc);
158 }
159 else
160 RTPrintf("SUPContAlloc (2nd) failed!\n");
161 memset(pv0, 0xaf, 15003);
162 /* pv0 is intentionally not freed! */
163 }
164 else
165 RTPrintf("SUPContAlloc failed!\n");
166
167 /*
168 * Allocate a big chunk of virtual memory and then lock it.
169 */
170 #define BIG_SIZE 72*1024*1024
171 #define BIG_SIZEPP (BIG_SIZE + PAGE_SIZE)
172 pv = NULL;
173 SUPPageAlloc(BIG_SIZEPP >> PAGE_SHIFT, &pv);
174 if (pv)
175 {
176 static SUPPAGE aPages[BIG_SIZE >> PAGE_SHIFT];
177 void *pvAligned = RT_ALIGN_P(pv, PAGE_SIZE);
178 rc = SUPPageLock(pvAligned, BIG_SIZE >> PAGE_SHIFT, &aPages[0]);
179 if (!rc)
180 {
181 /* dump */
182 RTPrintf("SUPPageLock(%p,%d,) succeeded!\n", pvAligned, BIG_SIZE);
183 memset(pv, 0x42, BIG_SIZEPP);
184 #if 0
185 for (unsigned j = 0; j < (BIG_SIZE >> PAGE_SHIFT); j++)
186 RTPrintf("%2d: vrt=%p phys=%08x\n", j, (char *)pvAligned + (j << PAGE_SHIFT), (uintptr_t)aPages[j].pvPhys);
187 #endif
188
189 /* unlock */
190 rc = SUPPageUnlock(pvAligned);
191 if (rc)
192 {
193 RTPrintf("SUPPageUnlock(%p) -> rc=%d\n", pvAligned, rc);
194 rcRet++;
195 }
196 memset(pv, 0xcc, BIG_SIZEPP);
197 }
198 else
199 {
200 RTPrintf("SUPPageLock(%p) -> rc=%d\n", pvAligned, rc);
201 rcRet++;
202 }
203 SUPPageFree(pv, BIG_SIZEPP >> PAGE_SHIFT);
204 }
205
206 rc = SUPTerm();
207 RTPrintf("SUPTerm -> rc=%d\n", rc);
208 rcRet += rc != 0;
209 }
210
211 return rcRet;
212}
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