VirtualBox

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

Last change on this file was 99822, checked in by vboxsync, 12 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
Line 
1/* $Id: tstDeviceStructSize.cpp 99822 2023-05-17 07:51:49Z vboxsync $ */
2/** @file
3 * tstDeviceStructSize - testcase for check structure sizes/alignment
4 * and to verify that HC and RC uses the same
5 * representation of the structures.
6 */
7
8/*
9 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
10 *
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
28 */
29
30
31/*********************************************************************************************************************************
32* Header Files *
33*********************************************************************************************************************************/
34#include <VBox/types.h>
35#include <iprt/x86.h>
36
37#define VBOX_WITH_HGCM /* grumble */
38#define VBOX_DEVICE_STRUCT_TESTCASE
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: */
65#undef LOG_GROUP
66#include "../Bus/DevPciInternal.h"
67#undef LOG_GROUP
68#include "../Graphics/DevVGA.cpp"
69#undef LOG_GROUP
70#include "../Input/DevPS2.h"
71#ifdef VBOX_WITH_E1000
72# undef LOG_GROUP
73# include "../Network/DevE1000.cpp"
74#endif
75#undef LOG_GROUP
76#include "../Network/DevPCNet.cpp"
77#undef LOG_GROUP
78#include "../PC/DevACPI.cpp"
79#undef LOG_GROUP
80#include "../PC/DevPIC.cpp"
81#undef LOG_GROUP
82#include "../PC/DevPit-i8254.cpp"
83#undef LOG_GROUP
84#include "../PC/DevRTC.cpp"
85# undef LOG_GROUP
86# include "../../VMM/VMMR3/APIC.cpp"
87#undef LOG_GROUP
88#include "../PC/DevIoApic.cpp"
89#undef LOG_GROUP
90#include "../PC/DevHPET.cpp"
91#undef LOG_GROUP
92#include "../PC/DevDMA.cpp"
93#undef LOG_GROUP
94#include "../EFI/DevSmc.cpp"
95#undef LOG_GROUP
96#include "../Storage/DevATA.cpp"
97#ifdef VBOX_WITH_USB
98# undef LOG_GROUP
99# include "../USB/DevOHCI.cpp"
100# ifdef VBOX_WITH_EHCI_IMPL
101# undef LOG_GROUP
102# include "../USB/DevEHCI.cpp"
103# endif
104# ifdef VBOX_WITH_XHCI_IMPL
105# undef LOG_GROUP
106# include "../USB/DevXHCI.cpp"
107# endif
108#endif
109#undef LOG_GROUP
110#include "../VMMDev/VMMDev.cpp"
111#undef LOG_GROUP
112#include "../Parallel/DevParallel.cpp"
113#undef LOG_GROUP
114#include "../Serial/DevSerial.cpp"
115#undef LOG_GROUP
116#include "../Serial/DevOxPcie958.cpp"
117#ifdef VBOX_WITH_AHCI
118# undef LOG_GROUP
119# include "../Storage/DevAHCI.cpp"
120#endif
121#ifdef VBOX_WITH_BUSLOGIC
122# undef LOG_GROUP
123# include "../Storage/DevBusLogic.cpp"
124#endif
125#ifdef VBOX_WITH_LSILOGIC
126# undef LOG_GROUP
127# include "../Storage/DevLsiLogicSCSI.cpp"
128#endif
129#ifdef VBOX_WITH_NVME_IMPL
130# undef LOG_GROUP
131# include "../Storage/DevNVMe.cpp"
132#endif
133
134#ifdef VBOX_WITH_PCI_PASSTHROUGH_IMPL
135# undef LOG_GROUP
136# include "../Bus/DevPciRaw.cpp"
137#endif
138
139#ifdef VBOX_WITH_IOMMU_AMD
140# undef LOG_GROUP
141# include "../Bus/DevIommuAmd.cpp"
142#endif
143#ifdef VBOX_WITH_IOMMU_INTEL
144# undef LOG_GROUP
145# include "../Bus/DevIommuIntel.cpp"
146#endif
147
148#include <VBox/vmm/pdmaudioifs.h>
149
150#undef LOG_GROUP
151#include "../Audio/DevIchAc97.cpp"
152#undef LOG_GROUP
153#include "../Audio/DevHda.h"
154
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
171#include <iprt/stream.h>
172
173
174/*********************************************************************************************************************************
175* Defined Constants And Macros *
176*********************************************************************************************************************************/
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 { \
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)); \
189 rc++; \
190 } \
191 else \
192 RTPrintf("%#08x (%d) %s\n", RT_OFFSETOF(type, m), RT_OFFSETOF(type, m), #type "." #m); \
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 { \
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)); \
206 rc++; \
207 } \
208 else \
209 RTPrintf("tstDeviceStructSize: info: sizeof(%s): %#x (%d)\n", #type, (int)sizeof(type), (int)sizeof(type)); \
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 { \
218 if (RT_OFFSETOF(strct, member) & ((align) - 1) ) \
219 { \
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)) ); \
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 { \
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)); \
245 rc++; \
246 } \
247 } while (0)
248
249/**
250 * Checks that a internal struct padding is big enough.
251 */
252#define CHECK_PADDING(strct, member, align) \
253 do \
254 { \
255 strct *p = NULL; NOREF(p); \
256 if (sizeof(p->member.s) > sizeof(p->member.padding)) \
257 { \
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))); \
260 rc++; \
261 } \
262 else if (RT_ALIGN_Z(sizeof(p->member.padding), (align)) != sizeof(p->member.padding)) \
263 { \
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))); \
266 rc++; \
267 } \
268 } while (0)
269
270/**
271 * Checks that a internal struct padding is big enough.
272 */
273#define CHECK_PADDING2(strct) \
274 do \
275 { \
276 strct *p = NULL; NOREF(p); \
277 if (sizeof(p->s) > sizeof(p->padding)) \
278 { \
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)); \
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 { \
291 strct *p = NULL; NOREF(p); \
292 if (sizeof(p->member) > sizeof(p->pad_member)) \
293 { \
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)); \
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 { \
306 RTPrintf("tstDeviceStructSize: info: %s::%s offset %d sizeof %d\n", #strct, #member, (int)RT_OFFSETOF(strct, member), (int)RT_SIZEOFMEMB(strct, member)); \
307 } while (0)
308
309
310int main()
311{
312 int rc = 0;
313 RTPrintf("tstDeviceStructSize: TESTING\n");
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
327 /* Basic alignment checks. */
328 CHECK_MEMBER_ALIGNMENT(PDMDEVINS, achInstanceData, 64);
329 CHECK_MEMBER_ALIGNMENT(PDMPCIDEV, Int.s, 16);
330 CHECK_MEMBER_ALIGNMENT(PDMPCIDEV, Int.s.aIORegions, 16);
331
332 /*
333 * Misc alignment checks (keep this somewhat alphabetical).
334 */
335 CHECK_MEMBER_ALIGNMENT(AC97STATE, CritSect, 8);
336
337 CHECK_MEMBER_ALIGNMENT(AHCI, lock, 8);
338 CHECK_MEMBER_ALIGNMENT(AHCI, aPorts[0], 8);
339 CHECK_MEMBER_ALIGNMENT(AHCIR3, aPorts[0], 8);
340
341 CHECK_MEMBER_ALIGNMENT(ATADEVSTATE, cTotalSectors, 8);
342 CHECK_MEMBER_ALIGNMENT(ATADEVSTATE, StatATADMA, 8);
343 CHECK_MEMBER_ALIGNMENT(ATADEVSTATE, StatReads, 8);
344 CHECK_MEMBER_ALIGNMENT(ATACONTROLLER, lock, 8);
345 CHECK_MEMBER_ALIGNMENT(ATACONTROLLER, StatAsyncOps, 8);
346 CHECK_MEMBER_ALIGNMENT(BUSLOGIC, CritSectIntr, 8);
347#ifdef VBOX_WITH_STATISTICS
348 CHECK_MEMBER_ALIGNMENT(DEVPIC, StatSetIrqRZ, 8);
349#endif
350#ifdef VBOX_WITH_E1000
351 CHECK_MEMBER_ALIGNMENT(E1KSTATE, cs, 8);
352 CHECK_MEMBER_ALIGNMENT(E1KSTATE, csRx, 8);
353 CHECK_MEMBER_ALIGNMENT(E1KSTATE, StatReceiveBytes, 8);
354#endif
355 //CHECK_MEMBER_ALIGNMENT(E1KSTATE, csTx, 8);
356#ifdef VBOX_WITH_USB
357# ifdef VBOX_WITH_EHCI_IMPL
358 CHECK_MEMBER_ALIGNMENT(EHCI, RootHub, 8);
359# endif
360# ifdef VBOX_WITH_XHCI_IMPL
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);
365 CHECK_MEMBER_ALIGNMENT(XHCI, cmdr_dqp, 8);
366 CHECK_MEMBER_ALIGNMENT(XHCI, hMmio, 8);
367# ifdef VBOX_WITH_STATISTICS
368 CHECK_MEMBER_ALIGNMENT(XHCI, StatErrorIsocUrbs, 8);
369 CHECK_MEMBER_ALIGNMENT(XHCI, StatIntrsCleared, 8);
370# endif
371# endif
372#endif
373 CHECK_MEMBER_ALIGNMENT(E1KSTATE, StatReceiveBytes, 8);
374 CHECK_MEMBER_ALIGNMENT(IOAPIC, au64RedirTable, 8);
375# ifdef VBOX_WITH_STATISTICS
376 CHECK_MEMBER_ALIGNMENT(IOAPIC, StatMmioReadRZ, 8);
377# endif
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);
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
388 CHECK_MEMBER_ALIGNMENT(DEVPCIBUS, apDevices, 64);
389 CHECK_MEMBER_ALIGNMENT(DEVPCIROOT, auPciApicIrqLevels, 16);
390 CHECK_MEMBER_ALIGNMENT(DEVPCIROOT, u.Piix3.auPciLegacyIrqLevels, 16);
391 CHECK_MEMBER_ALIGNMENT(PCNETSTATE, u64LastPoll, 8);
392 CHECK_MEMBER_ALIGNMENT(PCNETSTATE, CritSect, 8);
393 CHECK_MEMBER_ALIGNMENT(PCNETSTATE, StatReceiveBytes, 8);
394#ifdef VBOX_WITH_STATISTICS
395 CHECK_MEMBER_ALIGNMENT(PCNETSTATE, StatMMIOReadRZ, 8);
396#endif
397 CHECK_MEMBER_ALIGNMENT(PITSTATE, StatPITIrq, 8);
398 CHECK_MEMBER_ALIGNMENT(DEVSERIAL, UartCore, 8);
399 CHECK_MEMBER_ALIGNMENT(UARTCORE, CritSect, 8);
400#ifdef VBOX_WITH_VMSVGA
401 CHECK_SIZE(VMSVGAState, RT_ALIGN_Z(sizeof(VMSVGAState), 8));
402 CHECK_MEMBER_ALIGNMENT(VGASTATE, svga, 8);
403 CHECK_MEMBER_ALIGNMENT(VGASTATE, svga.au32ScratchRegion, 8);
404 CHECK_MEMBER_ALIGNMENT(VGASTATE, svga.StatRegBitsPerPixelWr, 8);
405#endif
406 CHECK_MEMBER_ALIGNMENT(VGASTATE, cMonitors, 8);
407 CHECK_MEMBER_ALIGNMENT(VGASTATE, GCPhysVRAM, 8);
408 CHECK_MEMBER_ALIGNMENT(VGASTATE, CritSect, 8);
409 CHECK_MEMBER_ALIGNMENT(VGASTATE, StatRZMemoryRead, 8);
410 CHECK_MEMBER_ALIGNMENT(VGASTATE, CritSectIRQ, 8);
411 CHECK_MEMBER_ALIGNMENT(VGASTATE, bmDirtyBitmap, 8);
412 CHECK_MEMBER_ALIGNMENT(VGASTATE, pciRegions, 8);
413 CHECK_MEMBER_ALIGNMENT(VMMDEV, CritSect, 8);
414#ifdef VBOX_WITH_PCI_PASSTHROUGH_IMPL
415 CHECK_MEMBER_ALIGNMENT(PCIRAWSENDREQ, u.aGetRegionInfo.u64RegionSize, 8);
416#endif
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
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
437
438#ifdef VBOX_WITH_RAW_MODE
439 /*
440 * Compare HC and RC.
441 */
442 RTPrintf("tstDeviceStructSize: Comparing HC and RC...\n");
443# include "tstDeviceStructSizeRC.h"
444#endif
445
446 /*
447 * Report result.
448 */
449 if (rc)
450 RTPrintf("tstDeviceStructSize: FAILURE - %d errors\n", rc);
451 else
452 RTPrintf("tstDeviceStructSize: SUCCESS\n");
453 return rc;
454}
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use