VirtualBox

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

Last change on this file since 4858 was 4825, checked in by vboxsync, 17 years ago

warning

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.6 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 innotek GmbH
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 as published by the Free Software Foundation,
14 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
15 * distribution. VirtualBox OSE is distributed in the hope that it will
16 * be useful, but WITHOUT ANY WARRANTY of any kind.
17 */
18
19
20/*******************************************************************************
21* Header Files *
22*******************************************************************************/
23#include <VBox/sup.h>
24#include <VBox/param.h>
25#include <iprt/runtime.h>
26#include <iprt/stream.h>
27#include <stdlib.h>
28#include <string.h>
29
30
31int main(int argc, char **argv)
32{
33 int rc;
34 int rcRet = 0;
35 RTHCPHYS HCPhys;
36
37 RTR3Init(true, ~0);
38 rc = SUPInit(NULL, ~0);
39 RTPrintf("SUPInit -> rc=%d\n", rc);
40 rcRet += rc != 0;
41 if (!rc)
42 {
43 /*
44 * Simple test.
45 */
46 void *pv;
47 int rc = SUPPageAlloc(1, &pv);
48 AssertRC(rc);
49 RTPrintf("pv=%p\n", pv);
50 SUPPAGE aPages[1];
51 rc = SUPPageLock(pv, 1, &aPages[0]);
52 RTPrintf("rc=%d aPages[0]=%RHp\n", rc, pv, aPages[0]);
53 RTThreadSleep(1500);
54#if 0
55 RTPrintf("Unlocking...\n");
56 RTThreadSleep(250);
57 rc = SUPPageUnlock(pv);
58 RTPrintf("rc=%d\n", rc);
59 RTThreadSleep(1500);
60#endif
61
62 /*
63 * More extensive.
64 */
65 static struct
66 {
67 void *pv;
68 void *pvAligned;
69 SUPPAGE aPages[16];
70 } aPinnings[500];
71 for (unsigned i = 0; i < sizeof(aPinnings) / sizeof(aPinnings[0]); i++)
72 {
73 aPinnings[i].pv = NULL;
74 SUPPageAlloc(0x10000 >> PAGE_SHIFT, &aPinnings[i].pv);
75 aPinnings[i].pvAligned = ALIGNP(aPinnings[i].pv, PAGE_SIZE);
76 rc = SUPPageLock(aPinnings[i].pvAligned, 0xf000 >> PAGE_SHIFT, &aPinnings[i].aPages[0]);
77 if (!rc)
78 {
79 RTPrintf("i=%d: pvAligned=%p pv=%p:\n", i, aPinnings[i].pvAligned, aPinnings[i].pv);
80 memset(aPinnings[i].pv, 0xfa, 0x10000);
81 unsigned c4GPluss = 0;
82 for (unsigned j = 0; j < (0xf000 >> PAGE_SHIFT); j++)
83 if (aPinnings[i].aPages[j].Phys >= _4G)
84 {
85 RTPrintf("%2d: vrt=%p phys=%VHp\n", j, (char *)aPinnings[i].pvAligned + (j << PAGE_SHIFT), aPinnings[i].aPages[j].Phys);
86 c4GPluss++;
87 }
88 RTPrintf("i=%d: c4GPluss=%d\n", i, c4GPluss);
89 }
90 else
91 {
92 RTPrintf("SUPPageLock -> rc=%d\n", rc);
93 rcRet++;
94 SUPPageFree(aPinnings[i].pv, 0x10000 >> PAGE_SHIFT);
95 aPinnings[i].pv = aPinnings[i].pvAligned = NULL;
96 break;
97 }
98 }
99
100 for (unsigned i = 0; i < sizeof(aPinnings) / sizeof(aPinnings[0]); i += 2)
101 {
102 if (aPinnings[i].pvAligned)
103 {
104 rc = SUPPageUnlock(aPinnings[i].pvAligned);
105 if (rc)
106 {
107 RTPrintf("SUPPageUnlock(%p) -> rc=%d\n", aPinnings[i].pvAligned, rc);
108 rcRet++;
109 }
110 memset(aPinnings[i].pv, 0xaf, 0x10000);
111 }
112 }
113
114 for (unsigned i = 0; i < sizeof(aPinnings) / sizeof(aPinnings[0]); i += 2)
115 {
116 if (aPinnings[i].pv)
117 {
118 memset(aPinnings[i].pv, 0xcc, 0x10000);
119 SUPPageFree(aPinnings[i].pv, 0x10000 >> PAGE_SHIFT);
120 aPinnings[i].pv = NULL;
121 }
122 }
123
124
125 /*
126 * Allocate a bit of contiguous memory.
127 */
128 pv = SUPContAlloc(RT_ALIGN_Z(15003, PAGE_SIZE) >> PAGE_SHIFT, &HCPhys);
129 rcRet += pv == NULL || HCPhys == 0;
130 if (pv && HCPhys)
131 {
132 RTPrintf("SUPContAlloc(15003) -> HCPhys=%llx pv=%p\n", HCPhys, pv);
133 void *pv0 = pv;
134 memset(pv0, 0xaf, 15003);
135 pv = SUPContAlloc(RT_ALIGN_Z(12999, PAGE_SIZE) >> PAGE_SHIFT, &HCPhys);
136 rcRet += pv == NULL || HCPhys == 0;
137 if (pv && HCPhys)
138 {
139 RTPrintf("SUPContAlloc(12999) -> HCPhys=%llx pv=%p\n", HCPhys, pv);
140 memset(pv, 0xbf, 12999);
141 rc = SUPContFree(pv, RT_ALIGN_Z(12999, PAGE_SIZE) >> PAGE_SHIFT);
142 rcRet += rc != 0;
143 if (rc)
144 RTPrintf("SUPContFree failed! rc=%d\n", rc);
145 }
146 else
147 RTPrintf("SUPContAlloc (2nd) failed!\n");
148 memset(pv0, 0xaf, 15003);
149 /* pv0 is intentionally not freed! */
150 }
151 else
152 RTPrintf("SUPContAlloc failed!\n");
153
154 /*
155 * Allocate a big chunk of virtual memory and then lock it.
156 */
157 #define BIG_SIZE 72*1024*1024
158 #define BIG_SIZEPP (BIG_SIZE + PAGE_SIZE)
159 pv = NULL;
160 SUPPageAlloc(BIG_SIZEPP >> PAGE_SHIFT, &pv);
161 if (pv)
162 {
163 static SUPPAGE aPages[BIG_SIZE >> PAGE_SHIFT];
164 void *pvAligned = RT_ALIGN_P(pv, PAGE_SIZE);
165 rc = SUPPageLock(pvAligned, BIG_SIZE >> PAGE_SHIFT, &aPages[0]);
166 if (!rc)
167 {
168 /* dump */
169 RTPrintf("SUPPageLock(%p,%d,) succeeded!\n", pvAligned, BIG_SIZE);
170 memset(pv, 0x42, BIG_SIZEPP);
171 #if 0
172 for (unsigned j = 0; j < (BIG_SIZE >> PAGE_SHIFT); j++)
173 RTPrintf("%2d: vrt=%p phys=%08x\n", j, (char *)pvAligned + (j << PAGE_SHIFT), (uintptr_t)aPages[j].pvPhys);
174 #endif
175
176 /* unlock */
177 rc = SUPPageUnlock(pvAligned);
178 if (rc)
179 {
180 RTPrintf("SUPPageUnlock(%p) -> rc=%d\n", pvAligned, rc);
181 rcRet++;
182 }
183 memset(pv, 0xcc, BIG_SIZEPP);
184 }
185 else
186 {
187 RTPrintf("SUPPageLock(%p) -> rc=%d\n", pvAligned, rc);
188 rcRet++;
189 }
190 SUPPageFree(pv, BIG_SIZEPP >> PAGE_SHIFT);
191 }
192
193 rc = SUPTerm();
194 RTPrintf("SUPTerm -> rc=%d\n", rc);
195 rcRet += rc != 0;
196 }
197
198 return rcRet;
199}
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