VirtualBox

source: vbox/trunk/src/VBox/Devices/testcase/tstDeviceStructSize.cpp

Last change on this file was 99822, checked in by vboxsync, 13 months ago

Devices/Bus: Implement interrupt forwarding for the PCIe bus, bugref:10445 [forgotten file]

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 15.3 KB
RevLine 
[735]1/* $Id: tstDeviceStructSize.cpp 99822 2023-05-17 07:51:49Z vboxsync $ */
2/** @file
3 * tstDeviceStructSize - testcase for check structure sizes/alignment
[25946]4 * and to verify that HC and RC uses the same
[735]5 * representation of the structures.
6 */
7
8/*
[98103]9 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
[735]10 *
[96407]11 * This file is part of VirtualBox base platform packages, as
12 * available from https://www.virtualbox.org.
13 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation, in version 3 of the
17 * License.
18 *
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, see <https://www.gnu.org/licenses>.
26 *
27 * SPDX-License-Identifier: GPL-3.0-only
[735]28 */
29
[57358]30
31/*********************************************************************************************************************************
32* Header Files *
33*********************************************************************************************************************************/
[735]34#include <VBox/types.h>
[37955]35#include <iprt/x86.h>
[735]36
[30772]37#define VBOX_WITH_HGCM /* grumble */
[735]38#define VBOX_DEVICE_STRUCT_TESTCASE
[71732]39
40/* Check that important preprocessor macros does not get redefined: */
41#include <VBox/cdefs.h>
42#include <VBox/log.h>
43#ifdef DEBUG
44# define VBOX_DEVICE_STRUCT_TESTCASE_CHECK_DEBUG
45#else
46# undef VBOX_DEVICE_STRUCT_TESTCASE_CHECK_DEBUG
47#endif
48#ifdef LOG_ENABLED
49# define VBOX_DEVICE_STRUCT_TESTCASE_CHECK_LOG_ENABLED
50#else
51# undef VBOX_DEVICE_STRUCT_TESTCASE_CHECK_LOG_ENABLED
52#endif
53#ifdef VBOX_STRICT
54# define VBOX_DEVICE_STRUCT_TESTCASE_CHECK_VBOX_STRICT
55#else
56# undef VBOX_DEVICE_STRUCT_TESTCASE_CHECK_VBOX_STRICT
57#endif
58#ifdef RT_STRICT
59# define VBOX_DEVICE_STRUCT_TESTCASE_CHECK_RT_STRICT
60#else
61# undef VBOX_DEVICE_STRUCT_TESTCASE_CHECK_RT_STRICT
62#endif
63
64/* The structures we're checking: */
[735]65#undef LOG_GROUP
[64419]66#include "../Bus/DevPciInternal.h"
[735]67#undef LOG_GROUP
[11270]68#include "../Graphics/DevVGA.cpp"
[735]69#undef LOG_GROUP
[82173]70#include "../Input/DevPS2.h"
[23814]71#ifdef VBOX_WITH_E1000
72# undef LOG_GROUP
73# include "../Network/DevE1000.cpp"
74#endif
[735]75#undef LOG_GROUP
[11270]76#include "../Network/DevPCNet.cpp"
[735]77#undef LOG_GROUP
[11270]78#include "../PC/DevACPI.cpp"
[735]79#undef LOG_GROUP
[11270]80#include "../PC/DevPIC.cpp"
[735]81#undef LOG_GROUP
[11270]82#include "../PC/DevPit-i8254.cpp"
[735]83#undef LOG_GROUP
[11270]84#include "../PC/DevRTC.cpp"
[60307]85# undef LOG_GROUP
86# include "../../VMM/VMMR3/APIC.cpp"
[735]87#undef LOG_GROUP
[65338]88#include "../PC/DevIoApic.cpp"
[37477]89#undef LOG_GROUP
[32471]90#include "../PC/DevHPET.cpp"
91#undef LOG_GROUP
[71210]92#include "../PC/DevDMA.cpp"
93#undef LOG_GROUP
[52508]94#include "../EFI/DevSmc.cpp"
[32471]95#undef LOG_GROUP
[11270]96#include "../Storage/DevATA.cpp"
[735]97#ifdef VBOX_WITH_USB
98# undef LOG_GROUP
[11270]99# include "../USB/DevOHCI.cpp"
[37752]100# ifdef VBOX_WITH_EHCI_IMPL
[50533]101# undef LOG_GROUP
[31901]102# include "../USB/DevEHCI.cpp"
103# endif
[52259]104# ifdef VBOX_WITH_XHCI_IMPL
105# undef LOG_GROUP
106# include "../USB/DevXHCI.cpp"
107# endif
[735]108#endif
109#undef LOG_GROUP
[21232]110#include "../VMMDev/VMMDev.cpp"
[1723]111#undef LOG_GROUP
[11270]112#include "../Parallel/DevParallel.cpp"
113#undef LOG_GROUP
[73766]114#include "../Serial/DevSerial.cpp"
115#undef LOG_GROUP
116#include "../Serial/DevOxPcie958.cpp"
[5687]117#ifdef VBOX_WITH_AHCI
[11145]118# undef LOG_GROUP
[11270]119# include "../Storage/DevAHCI.cpp"
[5687]120#endif
[11556]121#ifdef VBOX_WITH_BUSLOGIC
122# undef LOG_GROUP
123# include "../Storage/DevBusLogic.cpp"
124#endif
[15475]125#ifdef VBOX_WITH_LSILOGIC
126# undef LOG_GROUP
127# include "../Storage/DevLsiLogicSCSI.cpp"
128#endif
[57537]129#ifdef VBOX_WITH_NVME_IMPL
130# undef LOG_GROUP
131# include "../Storage/DevNVMe.cpp"
132#endif
[735]133
[37781]134#ifdef VBOX_WITH_PCI_PASSTHROUGH_IMPL
[35885]135# undef LOG_GROUP
136# include "../Bus/DevPciRaw.cpp"
137#endif
138
[88624]139#ifdef VBOX_WITH_IOMMU_AMD
140# undef LOG_GROUP
141# include "../Bus/DevIommuAmd.cpp"
142#endif
[89590]143#ifdef VBOX_WITH_IOMMU_INTEL
144# undef LOG_GROUP
145# include "../Bus/DevIommuIntel.cpp"
146#endif
[88624]147
[56085]148#include <VBox/vmm/pdmaudioifs.h>
149
[71247]150#undef LOG_GROUP
151#include "../Audio/DevIchAc97.cpp"
152#undef LOG_GROUP
[88229]153#include "../Audio/DevHda.h"
[46272]154
[71732]155
156/* Check that important preprocessor macros didn't get redefined: */
157#if defined(DEBUG) != defined(VBOX_DEVICE_STRUCT_TESTCASE_CHECK_DEBUG)
158# error "DEBUG was modified! This may throw off structure tests."
159#endif
160#if defined(LOG_ENABLED) != defined(VBOX_DEVICE_STRUCT_TESTCASE_CHECK_LOG_ENABLED)
161# error "LOG_ENABLED was modified! This may throw off structure tests."
162#endif
163#if defined(RT_STRICT) != defined(VBOX_DEVICE_STRUCT_TESTCASE_CHECK_RT_STRICT)
164# error "RT_STRICT was modified! This may throw off structure tests."
165#endif
166#if defined(VBOX_STRICT) != defined(VBOX_DEVICE_STRUCT_TESTCASE_CHECK_VBOX_STRICT)
167# error "VBOX_STRICT was modified! This may throw off structure tests."
168#endif
169
170
[96512]171#include <iprt/stream.h>
[735]172
173
[57358]174/*********************************************************************************************************************************
175* Defined Constants And Macros *
176*********************************************************************************************************************************/
[735]177/**
178 * Checks the offset of a data member.
179 * @param type Type.
180 * @param off Correct offset.
181 * @param m Member name.
182 */
183#define CHECK_OFF(type, off, m) \
184 do { \
185 if (off != RT_OFFSETOF(type, m)) \
186 { \
[96512]187 RTPrintf("tstDeviceStructSize: Error! %#010x %s Member offset wrong by %d (should be %d -- but is %d)\n", \
188 RT_OFFSETOF(type, m), #type "." #m, off - RT_OFFSETOF(type, m), off, RT_OFFSETOF(type, m)); \
[735]189 rc++; \
190 } \
[53831]191 else \
[96512]192 RTPrintf("%#08x (%d) %s\n", RT_OFFSETOF(type, m), RT_OFFSETOF(type, m), #type "." #m); \
[735]193 } while (0)
194
195/**
196 * Checks the size of type.
197 * @param type Type.
198 * @param size Correct size.
199 */
200#define CHECK_SIZE(type, size) \
201 do { \
202 if (size != sizeof(type)) \
203 { \
[96512]204 RTPrintf("tstDeviceStructSize: Error! sizeof(%s): %#x (%d) Size wrong by %d (should be %d -- but is %d)\n", \
205 #type, (int)sizeof(type), (int)sizeof(type), (int)sizeof(type) - (int)size, (int)size, (int)sizeof(type)); \
[735]206 rc++; \
207 } \
208 else \
[96512]209 RTPrintf("tstDeviceStructSize: info: sizeof(%s): %#x (%d)\n", #type, (int)sizeof(type), (int)sizeof(type)); \
[735]210 } while (0)
211
212/**
213 * Checks the alignment of a struct member.
214 */
215#define CHECK_MEMBER_ALIGNMENT(strct, member, align) \
216 do \
217 { \
[22885]218 if (RT_OFFSETOF(strct, member) & ((align) - 1) ) \
[735]219 { \
[96512]220 RTPrintf("tstDeviceStructSize: error! %s::%s offset=%#x (%u) expected alignment %#x, meaning %#x (%u) off\n", \
221 #strct, #member, \
222 (unsigned)RT_OFFSETOF(strct, member), \
223 (unsigned)RT_OFFSETOF(strct, member), \
224 (unsigned)(align), \
225 (unsigned)(((align) - RT_OFFSETOF(strct, member)) & ((align) - 1)), \
226 (unsigned)(((align) - RT_OFFSETOF(strct, member)) & ((align) - 1)) ); \
[735]227 rc++; \
228 } \
229 } while (0)
230
231/**
232 * Checks that the size of a type is aligned correctly.
233 */
234#define CHECK_SIZE_ALIGNMENT(type, align) \
235 do { \
236 if (RT_ALIGN_Z(sizeof(type), (align)) != sizeof(type)) \
237 { \
[96512]238 RTPrintf("tstDeviceStructSize: error! %s size=%#x (%u), align=%#x %#x (%u) bytes off\n", \
239 #type, \
240 (unsigned)sizeof(type), \
241 (unsigned)sizeof(type), \
242 (align), \
243 (unsigned)RT_ALIGN_Z(sizeof(type), align) - (unsigned)sizeof(type), \
244 (unsigned)RT_ALIGN_Z(sizeof(type), align) - (unsigned)sizeof(type)); \
[735]245 rc++; \
246 } \
247 } while (0)
248
249/**
250 * Checks that a internal struct padding is big enough.
251 */
[22885]252#define CHECK_PADDING(strct, member, align) \
[735]253 do \
254 { \
[30222]255 strct *p = NULL; NOREF(p); \
[735]256 if (sizeof(p->member.s) > sizeof(p->member.padding)) \
257 { \
[96512]258 RTPrintf("tstDeviceStructSize: error! padding of %s::%s is too small, padding=%d struct=%d correct=%d\n", #strct, #member, \
259 (int)sizeof(p->member.padding), (int)sizeof(p->member.s), (int)RT_ALIGN_Z(sizeof(p->member.s), (align))); \
[735]260 rc++; \
261 } \
[22885]262 else if (RT_ALIGN_Z(sizeof(p->member.padding), (align)) != sizeof(p->member.padding)) \
263 { \
[96512]264 RTPrintf("tstDeviceStructSize: error! padding of %s::%s is misaligned, padding=%d correct=%d\n", #strct, #member, \
265 (int)sizeof(p->member.padding), (int)RT_ALIGN_Z(sizeof(p->member.s), (align))); \
[22885]266 rc++; \
267 } \
[735]268 } while (0)
269
270/**
271 * Checks that a internal struct padding is big enough.
272 */
273#define CHECK_PADDING2(strct) \
274 do \
275 { \
[30222]276 strct *p = NULL; NOREF(p); \
[735]277 if (sizeof(p->s) > sizeof(p->padding)) \
278 { \
[96512]279 RTPrintf("tstDeviceStructSize: error! padding of %s is too small, padding=%d struct=%d correct=%d\n", #strct, \
280 (int)sizeof(p->padding), (int)sizeof(p->s), (int)RT_ALIGN_Z(sizeof(p->s), 32)); \
[735]281 rc++; \
282 } \
283 } while (0)
284
285/**
286 * Checks that a internal struct padding is big enough.
287 */
288#define CHECK_PADDING3(strct, member, pad_member) \
289 do \
290 { \
[30222]291 strct *p = NULL; NOREF(p); \
[735]292 if (sizeof(p->member) > sizeof(p->pad_member)) \
293 { \
[96512]294 RTPrintf("tstDeviceStructSize: error! padding of %s::%s is too small, padding=%d struct=%d\n", #strct, #member, \
295 (int)sizeof(p->pad_member), (int)sizeof(p->member)); \
[735]296 rc++; \
297 } \
298 } while (0)
299
300/**
301 * Prints the offset of a struct member.
302 */
303#define PRINT_OFFSET(strct, member) \
304 do \
305 { \
[96512]306 RTPrintf("tstDeviceStructSize: info: %s::%s offset %d sizeof %d\n", #strct, #member, (int)RT_OFFSETOF(strct, member), (int)RT_SIZEOFMEMB(strct, member)); \
[735]307 } while (0)
308
309
310int main()
311{
312 int rc = 0;
[96512]313 RTPrintf("tstDeviceStructSize: TESTING\n");
[735]314
315 /* Assert sanity */
316 CHECK_SIZE(uint128_t, 128/8);
317 CHECK_SIZE(int128_t, 128/8);
318 CHECK_SIZE(uint64_t, 64/8);
319 CHECK_SIZE(int64_t, 64/8);
320 CHECK_SIZE(uint32_t, 32/8);
321 CHECK_SIZE(int32_t, 32/8);
322 CHECK_SIZE(uint16_t, 16/8);
323 CHECK_SIZE(int16_t, 16/8);
324 CHECK_SIZE(uint8_t, 8/8);
325 CHECK_SIZE(int8_t, 8/8);
326
[23814]327 /* Basic alignment checks. */
[12969]328 CHECK_MEMBER_ALIGNMENT(PDMDEVINS, achInstanceData, 64);
[64387]329 CHECK_MEMBER_ALIGNMENT(PDMPCIDEV, Int.s, 16);
330 CHECK_MEMBER_ALIGNMENT(PDMPCIDEV, Int.s.aIORegions, 16);
[23814]331
332 /*
333 * Misc alignment checks (keep this somewhat alphabetical).
334 */
[71247]335 CHECK_MEMBER_ALIGNMENT(AC97STATE, CritSect, 8);
336
[23814]337 CHECK_MEMBER_ALIGNMENT(AHCI, lock, 8);
[81887]338 CHECK_MEMBER_ALIGNMENT(AHCI, aPorts[0], 8);
339 CHECK_MEMBER_ALIGNMENT(AHCIR3, aPorts[0], 8);
[64626]340
[81867]341 CHECK_MEMBER_ALIGNMENT(ATADEVSTATE, cTotalSectors, 8);
342 CHECK_MEMBER_ALIGNMENT(ATADEVSTATE, StatATADMA, 8);
343 CHECK_MEMBER_ALIGNMENT(ATADEVSTATE, StatReads, 8);
[23814]344 CHECK_MEMBER_ALIGNMENT(ATACONTROLLER, lock, 8);
[735]345 CHECK_MEMBER_ALIGNMENT(ATACONTROLLER, StatAsyncOps, 8);
[30597]346 CHECK_MEMBER_ALIGNMENT(BUSLOGIC, CritSectIntr, 8);
[23814]347#ifdef VBOX_WITH_STATISTICS
[81911]348 CHECK_MEMBER_ALIGNMENT(DEVPIC, StatSetIrqRZ, 8);
[23814]349#endif
[26932]350#ifdef VBOX_WITH_E1000
[23814]351 CHECK_MEMBER_ALIGNMENT(E1KSTATE, cs, 8);
352 CHECK_MEMBER_ALIGNMENT(E1KSTATE, csRx, 8);
[29435]353 CHECK_MEMBER_ALIGNMENT(E1KSTATE, StatReceiveBytes, 8);
[26932]354#endif
[23814]355 //CHECK_MEMBER_ALIGNMENT(E1KSTATE, csTx, 8);
[735]356#ifdef VBOX_WITH_USB
[37752]357# ifdef VBOX_WITH_EHCI_IMPL
[5516]358 CHECK_MEMBER_ALIGNMENT(EHCI, RootHub, 8);
359# endif
[52259]360# ifdef VBOX_WITH_XHCI_IMPL
[81566]361 CHECK_MEMBER_ALIGNMENT(XHCI, aPorts, 8);
362 CHECK_MEMBER_ALIGNMENT(XHCI, aInterrupters, 8);
363 CHECK_MEMBER_ALIGNMENT(XHCI, aInterrupters[0].lock, 8);
364 CHECK_MEMBER_ALIGNMENT(XHCI, aInterrupters[1].lock, 8);
[52259]365 CHECK_MEMBER_ALIGNMENT(XHCI, cmdr_dqp, 8);
[81566]366 CHECK_MEMBER_ALIGNMENT(XHCI, hMmio, 8);
[52259]367# ifdef VBOX_WITH_STATISTICS
[64761]368 CHECK_MEMBER_ALIGNMENT(XHCI, StatErrorIsocUrbs, 8);
[52259]369 CHECK_MEMBER_ALIGNMENT(XHCI, StatIntrsCleared, 8);
370# endif
371# endif
[735]372#endif
[28196]373 CHECK_MEMBER_ALIGNMENT(E1KSTATE, StatReceiveBytes, 8);
[61339]374 CHECK_MEMBER_ALIGNMENT(IOAPIC, au64RedirTable, 8);
375# ifdef VBOX_WITH_STATISTICS
[61847]376 CHECK_MEMBER_ALIGNMENT(IOAPIC, StatMmioReadRZ, 8);
[61339]377# endif
[81782]378 CHECK_MEMBER_ALIGNMENT(LSILOGICSCSI, aMessage, 8);
379 CHECK_MEMBER_ALIGNMENT(LSILOGICSCSI, ReplyPostQueueCritSect, 8);
380 CHECK_MEMBER_ALIGNMENT(LSILOGICSCSI, ReplyFreeQueueCritSect, 8);
381 CHECK_MEMBER_ALIGNMENT(LSILOGICSCSI, uReplyFreeQueueNextEntryFreeWrite, 8);
[23814]382#ifdef VBOX_WITH_USB
383 CHECK_MEMBER_ALIGNMENT(OHCI, RootHub, 8);
384# ifdef VBOX_WITH_STATISTICS
385 CHECK_MEMBER_ALIGNMENT(OHCI, StatCanceledIsocUrbs, 8);
386# endif
387#endif
[64414]388 CHECK_MEMBER_ALIGNMENT(DEVPCIBUS, apDevices, 64);
[64419]389 CHECK_MEMBER_ALIGNMENT(DEVPCIROOT, auPciApicIrqLevels, 16);
[99822]390 CHECK_MEMBER_ALIGNMENT(DEVPCIROOT, u.Piix3.auPciLegacyIrqLevels, 16);
[44892]391 CHECK_MEMBER_ALIGNMENT(PCNETSTATE, u64LastPoll, 8);
392 CHECK_MEMBER_ALIGNMENT(PCNETSTATE, CritSect, 8);
393 CHECK_MEMBER_ALIGNMENT(PCNETSTATE, StatReceiveBytes, 8);
[23814]394#ifdef VBOX_WITH_STATISTICS
[44892]395 CHECK_MEMBER_ALIGNMENT(PCNETSTATE, StatMMIOReadRZ, 8);
[23814]396#endif
[44804]397 CHECK_MEMBER_ALIGNMENT(PITSTATE, StatPITIrq, 8);
[73135]398 CHECK_MEMBER_ALIGNMENT(DEVSERIAL, UartCore, 8);
399 CHECK_MEMBER_ALIGNMENT(UARTCORE, CritSect, 8);
[50126]400#ifdef VBOX_WITH_VMSVGA
[57410]401 CHECK_SIZE(VMSVGAState, RT_ALIGN_Z(sizeof(VMSVGAState), 8));
402 CHECK_MEMBER_ALIGNMENT(VGASTATE, svga, 8);
[65303]403 CHECK_MEMBER_ALIGNMENT(VGASTATE, svga.au32ScratchRegion, 8);
404 CHECK_MEMBER_ALIGNMENT(VGASTATE, svga.StatRegBitsPerPixelWr, 8);
[50126]405#endif
[57410]406 CHECK_MEMBER_ALIGNMENT(VGASTATE, cMonitors, 8);
[50126]407 CHECK_MEMBER_ALIGNMENT(VGASTATE, GCPhysVRAM, 8);
[44876]408 CHECK_MEMBER_ALIGNMENT(VGASTATE, CritSect, 8);
[23814]409 CHECK_MEMBER_ALIGNMENT(VGASTATE, StatRZMemoryRead, 8);
[62032]410 CHECK_MEMBER_ALIGNMENT(VGASTATE, CritSectIRQ, 8);
[98065]411 CHECK_MEMBER_ALIGNMENT(VGASTATE, bmDirtyBitmap, 8);
412 CHECK_MEMBER_ALIGNMENT(VGASTATE, pciRegions, 8);
[81571]413 CHECK_MEMBER_ALIGNMENT(VMMDEV, CritSect, 8);
[37781]414#ifdef VBOX_WITH_PCI_PASSTHROUGH_IMPL
415 CHECK_MEMBER_ALIGNMENT(PCIRAWSENDREQ, u.aGetRegionInfo.u64RegionSize, 8);
[35885]416#endif
[88624]417#ifdef VBOX_WITH_IOMMU_AMD
418 CHECK_MEMBER_ALIGNMENT(IOMMU, IommuBar, 8);
419 CHECK_MEMBER_ALIGNMENT(IOMMU, aDevTabBaseAddrs, 8);
420 CHECK_MEMBER_ALIGNMENT(IOMMU, CmdBufHeadPtr, 8);
421 CHECK_MEMBER_ALIGNMENT(IOMMU, Status, 8);
422# ifdef VBOX_WITH_STATISTICS
423 CHECK_MEMBER_ALIGNMENT(IOMMU, StatMmioReadR3, 8);
424# endif
425#endif
[89590]426#ifdef VBOX_WITH_IOMMU_INTEL
427 CHECK_MEMBER_ALIGNMENT(DMAR, abRegs0, 8);
428 CHECK_MEMBER_ALIGNMENT(DMAR, abRegs1, 8);
429 CHECK_MEMBER_ALIGNMENT(DMAR, uIrtaReg, 8);
430 CHECK_MEMBER_ALIGNMENT(DMAR, uRtaddrReg, 8);
431 CHECK_MEMBER_ALIGNMENT(DMAR, hEvtInvQueue, 8);
432# ifdef VBOX_WITH_STATISTICS
433 CHECK_MEMBER_ALIGNMENT(DMAR, StatMmioReadR3, 8);
434 CHECK_MEMBER_ALIGNMENT(DMAR, StatPasidDevtlbInvDsc, 8);
435# endif
436#endif
[735]437
[25946]438#ifdef VBOX_WITH_RAW_MODE
[735]439 /*
[25946]440 * Compare HC and RC.
[735]441 */
[96512]442 RTPrintf("tstDeviceStructSize: Comparing HC and RC...\n");
[25946]443# include "tstDeviceStructSizeRC.h"
444#endif
[735]445
446 /*
447 * Report result.
448 */
449 if (rc)
[96512]450 RTPrintf("tstDeviceStructSize: FAILURE - %d errors\n", rc);
[735]451 else
[96512]452 RTPrintf("tstDeviceStructSize: SUCCESS\n");
[735]453 return rc;
454}
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use