VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/Support/testcase/tstLow.cpp@ 108658

Last change on this file since 108658 was 108658, checked in by vboxsync, 7 weeks ago

HostDrivers/Support/testcase/tstLow.cpp: Make it work for hosts where we don't know the host page size when building, bugref:10391

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.6 KB
Line 
1/* $Id: tstLow.cpp 108658 2025-03-20 15:33:39Z vboxsync $ */
2/** @file
3 * SUP Testcase - Low (<4GB) Memory Allocate interface (ring 3).
4 */
5
6/*
7 * Copyright (C) 2006-2024 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include <VBox/sup.h>
42#include <VBox/param.h>
43#include <iprt/errcore.h>
44#include <iprt/initterm.h>
45#include <iprt/stream.h>
46#include <iprt/string.h>
47#include <iprt/system.h>
48
49
50int main(int argc, char **argv)
51{
52 int rc;
53 int rcRet = 0;
54
55 RTR3InitExe(argc, &argv, 0);
56 RTPrintf("tstLow: TESTING...\n");
57
58 rc = SUPR3Init(NULL);
59 if (RT_SUCCESS(rc))
60 {
61 uint32_t const cbPage = RTSystemGetPageSize();
62 uintptr_t const offPageMask = RTSystemGetPageOffsetMask();
63
64 /*
65 * Allocate a bit of contiguous memory.
66 */
67 SUPPAGE aPages0[128];
68 void *pvPages0 = (void *)0x77777777;
69 memset(&aPages0[0], 0x8f, sizeof(aPages0));
70 rc = SUPR3LowAlloc(RT_ELEMENTS(aPages0), &pvPages0, NULL, aPages0);
71 if (RT_SUCCESS(rc))
72 {
73 /* check that the pages are below 4GB and valid. */
74 for (unsigned iPage = 0; iPage < RT_ELEMENTS(aPages0); iPage++)
75 {
76 RTPrintf("%-4d: Phys=%RHp Reserved=%p\n", iPage, aPages0[iPage].Phys, aPages0[iPage].uReserved);
77 if (aPages0[iPage].uReserved != 0)
78 {
79 rcRet++;
80 RTPrintf("tstLow: error: aPages0[%d].uReserved=%#x expected 0!\n", iPage, aPages0[iPage].uReserved);
81 }
82 if ( aPages0[iPage].Phys >= _4G
83 || (aPages0[iPage].Phys & offPageMask))
84 {
85 rcRet++;
86 RTPrintf("tstLow: error: aPages0[%d].Phys=%RHp!\n", iPage, aPages0[iPage].Phys);
87 }
88 }
89 if (!rcRet)
90 {
91 for (unsigned iPage = 0; iPage < RT_ELEMENTS(aPages0); iPage++)
92 memset((char *)pvPages0 + iPage * cbPage, iPage, cbPage);
93 for (unsigned iPage = 0; iPage < RT_ELEMENTS(aPages0); iPage++)
94 for (uint8_t *pu8 = (uint8_t *)pvPages0 + iPage * cbPage, *pu8End = pu8 + cbPage; pu8 < pu8End; pu8++)
95 if (*pu8 != (uint8_t)iPage)
96 {
97 RTPrintf("tstLow: error: invalid page content %02x != %02x. iPage=%u off=%#x\n",
98 *pu8, (uint8_t)iPage, iPage, (uintptr_t)pu8 & offPageMask);
99 rcRet++;
100 }
101 }
102 SUPR3LowFree(pvPages0, RT_ELEMENTS(aPages0));
103 }
104 else
105 {
106 RTPrintf("SUPR3LowAlloc(%d,,) failed -> rc=%Rrc\n", RT_ELEMENTS(aPages0), rc);
107 rcRet++;
108 }
109
110 /*
111 * Allocate odd amounts in from 1 to 127.
112 */
113 for (unsigned cPages = 1; cPages <= 127; cPages++)
114 {
115 SUPPAGE aPages1[128];
116 void *pvPages1 = (void *)0x77777777;
117 memset(&aPages1[0], 0x8f, sizeof(aPages1));
118 rc = SUPR3LowAlloc(cPages, &pvPages1, NULL, aPages1);
119 if (RT_SUCCESS(rc))
120 {
121 /* check that the pages are below 4GB and valid. */
122 for (unsigned iPage = 0; iPage < cPages; iPage++)
123 {
124 RTPrintf("%-4d::%-4d: Phys=%RHp Reserved=%p\n", cPages, iPage, aPages1[iPage].Phys, aPages1[iPage].uReserved);
125 if (aPages1[iPage].uReserved != 0)
126 {
127 rcRet++;
128 RTPrintf("tstLow: error: aPages1[%d].uReserved=%#x expected 0!\n", iPage, aPages1[iPage].uReserved);
129 }
130 if ( aPages1[iPage].Phys >= _4G
131 || (aPages1[iPage].Phys & offPageMask))
132 {
133 rcRet++;
134 RTPrintf("tstLow: error: aPages1[%d].Phys=%RHp!\n", iPage, aPages1[iPage].Phys);
135 }
136 }
137 if (!rcRet)
138 {
139 for (unsigned iPage = 0; iPage < cPages; iPage++)
140 memset((char *)pvPages1 + iPage * cbPage, iPage, cbPage);
141 for (unsigned iPage = 0; iPage < cPages; iPage++)
142 for (uint8_t *pu8 = (uint8_t *)pvPages1 + iPage * cbPage, *pu8End = pu8 + cbPage; pu8 < pu8End; pu8++)
143 if (*pu8 != (uint8_t)iPage)
144 {
145 RTPrintf("tstLow: error: invalid page content %02x != %02x. iPage=%p off=%#x\n",
146 *pu8, (uint8_t)iPage, iPage, (uintptr_t)pu8 & offPageMask);
147 rcRet++;
148 }
149 }
150 SUPR3LowFree(pvPages1, cPages);
151 }
152 else
153 {
154 RTPrintf("SUPR3LowAlloc(%d,,) failed -> rc=%Rrc\n", cPages, rc);
155 rcRet++;
156 }
157 }
158
159 }
160 else
161 {
162 RTPrintf("SUPR3Init -> rc=%Rrc\n", rc);
163 rcRet++;
164 }
165
166
167 return rcRet;
168}
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette