VirtualBox

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

Last change on this file since 91998 was 89590, checked in by vboxsync, 4 years ago

Intel IOMMU: bugref:9967 Added to device struct size and alignment tests.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 15.2 KB
Line 
1/* $Id: tstDeviceStructSize.cpp 89590 2021-06-10 08:43:08Z 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-2020 Oracle Corporation
10 *
11 * This file is part of VirtualBox Open Source Edition (OSE), as
12 * available from http://www.virtualbox.org. This file is free software;
13 * you can redistribute it and/or modify it under the terms of the GNU
14 * General Public License (GPL) as published by the Free Software
15 * Foundation, in version 2 as it comes in the "COPYING" file of the
16 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
17 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
18 */
19
20
21/*********************************************************************************************************************************
22* Header Files *
23*********************************************************************************************************************************/
24#include <VBox/types.h>
25#include <iprt/x86.h>
26
27#define VBOX_WITH_HGCM /* grumble */
28#define VBOX_DEVICE_STRUCT_TESTCASE
29
30/* Check that important preprocessor macros does not get redefined: */
31#include <VBox/cdefs.h>
32#include <VBox/log.h>
33#ifdef DEBUG
34# define VBOX_DEVICE_STRUCT_TESTCASE_CHECK_DEBUG
35#else
36# undef VBOX_DEVICE_STRUCT_TESTCASE_CHECK_DEBUG
37#endif
38#ifdef LOG_ENABLED
39# define VBOX_DEVICE_STRUCT_TESTCASE_CHECK_LOG_ENABLED
40#else
41# undef VBOX_DEVICE_STRUCT_TESTCASE_CHECK_LOG_ENABLED
42#endif
43#ifdef VBOX_STRICT
44# define VBOX_DEVICE_STRUCT_TESTCASE_CHECK_VBOX_STRICT
45#else
46# undef VBOX_DEVICE_STRUCT_TESTCASE_CHECK_VBOX_STRICT
47#endif
48#ifdef RT_STRICT
49# define VBOX_DEVICE_STRUCT_TESTCASE_CHECK_RT_STRICT
50#else
51# undef VBOX_DEVICE_STRUCT_TESTCASE_CHECK_RT_STRICT
52#endif
53
54/* The structures we're checking: */
55#undef LOG_GROUP
56#include "../Bus/DevPciInternal.h"
57#undef LOG_GROUP
58#include "../Graphics/DevVGA.cpp"
59#undef LOG_GROUP
60#include "../Input/DevPS2.h"
61#ifdef VBOX_WITH_E1000
62# undef LOG_GROUP
63# include "../Network/DevE1000.cpp"
64#endif
65#undef LOG_GROUP
66#include "../Network/DevPCNet.cpp"
67#ifdef VBOX_WITH_VIRTIO
68# undef LOG_GROUP
69# include "../Network/DevVirtioNet.cpp"
70#endif
71#undef LOG_GROUP
72#include "../PC/DevACPI.cpp"
73#undef LOG_GROUP
74#include "../PC/DevPIC.cpp"
75#undef LOG_GROUP
76#include "../PC/DevPit-i8254.cpp"
77#undef LOG_GROUP
78#include "../PC/DevRTC.cpp"
79# undef LOG_GROUP
80# include "../../VMM/VMMR3/APIC.cpp"
81#undef LOG_GROUP
82#include "../PC/DevIoApic.cpp"
83#undef LOG_GROUP
84#include "../PC/DevHPET.cpp"
85#undef LOG_GROUP
86#include "../PC/DevDMA.cpp"
87#undef LOG_GROUP
88#include "../EFI/DevSmc.cpp"
89#undef LOG_GROUP
90#include "../Storage/DevATA.cpp"
91#ifdef VBOX_WITH_USB
92# undef LOG_GROUP
93# include "../USB/DevOHCI.cpp"
94# ifdef VBOX_WITH_EHCI_IMPL
95# undef LOG_GROUP
96# include "../USB/DevEHCI.cpp"
97# endif
98# ifdef VBOX_WITH_XHCI_IMPL
99# undef LOG_GROUP
100# include "../USB/DevXHCI.cpp"
101# endif
102#endif
103#undef LOG_GROUP
104#include "../VMMDev/VMMDev.cpp"
105#undef LOG_GROUP
106#include "../Parallel/DevParallel.cpp"
107#undef LOG_GROUP
108#include "../Serial/DevSerial.cpp"
109#undef LOG_GROUP
110#include "../Serial/DevOxPcie958.cpp"
111#ifdef VBOX_WITH_AHCI
112# undef LOG_GROUP
113# include "../Storage/DevAHCI.cpp"
114#endif
115#ifdef VBOX_WITH_BUSLOGIC
116# undef LOG_GROUP
117# include "../Storage/DevBusLogic.cpp"
118#endif
119#ifdef VBOX_WITH_LSILOGIC
120# undef LOG_GROUP
121# include "../Storage/DevLsiLogicSCSI.cpp"
122#endif
123#ifdef VBOX_WITH_NVME_IMPL
124# undef LOG_GROUP
125# include "../Storage/DevNVMe.cpp"
126#endif
127
128#ifdef VBOX_WITH_PCI_PASSTHROUGH_IMPL
129# undef LOG_GROUP
130# include "../Bus/DevPciRaw.cpp"
131#endif
132
133#ifdef VBOX_WITH_IOMMU_AMD
134# undef LOG_GROUP
135# include "../Bus/DevIommuAmd.cpp"
136#endif
137#ifdef VBOX_WITH_IOMMU_INTEL
138# undef LOG_GROUP
139# include "../Bus/DevIommuIntel.cpp"
140#endif
141
142#include <VBox/vmm/pdmaudioifs.h>
143
144#undef LOG_GROUP
145#include "../Audio/DevIchAc97.cpp"
146#undef LOG_GROUP
147#include "../Audio/DevHda.h"
148
149
150/* Check that important preprocessor macros didn't get redefined: */
151#if defined(DEBUG) != defined(VBOX_DEVICE_STRUCT_TESTCASE_CHECK_DEBUG)
152# error "DEBUG was modified! This may throw off structure tests."
153#endif
154#if defined(LOG_ENABLED) != defined(VBOX_DEVICE_STRUCT_TESTCASE_CHECK_LOG_ENABLED)
155# error "LOG_ENABLED was modified! This may throw off structure tests."
156#endif
157#if defined(RT_STRICT) != defined(VBOX_DEVICE_STRUCT_TESTCASE_CHECK_RT_STRICT)
158# error "RT_STRICT was modified! This may throw off structure tests."
159#endif
160#if defined(VBOX_STRICT) != defined(VBOX_DEVICE_STRUCT_TESTCASE_CHECK_VBOX_STRICT)
161# error "VBOX_STRICT was modified! This may throw off structure tests."
162#endif
163
164
165#include <stdio.h>
166
167
168/*********************************************************************************************************************************
169* Defined Constants And Macros *
170*********************************************************************************************************************************/
171/**
172 * Checks the offset of a data member.
173 * @param type Type.
174 * @param off Correct offset.
175 * @param m Member name.
176 */
177#define CHECK_OFF(type, off, m) \
178 do { \
179 if (off != RT_OFFSETOF(type, m)) \
180 { \
181 printf("tstDeviceStructSize: Error! %#010x %s Member offset wrong by %d (should be %d -- but is %d)\n", \
182 RT_OFFSETOF(type, m), #type "." #m, off - RT_OFFSETOF(type, m), off, RT_OFFSETOF(type, m)); \
183 rc++; \
184 } \
185 else \
186 printf("%#08x (%d) %s\n", RT_OFFSETOF(type, m), RT_OFFSETOF(type, m), #type "." #m); \
187 } while (0)
188
189/**
190 * Checks the size of type.
191 * @param type Type.
192 * @param size Correct size.
193 */
194#define CHECK_SIZE(type, size) \
195 do { \
196 if (size != sizeof(type)) \
197 { \
198 printf("tstDeviceStructSize: Error! sizeof(%s): %#x (%d) Size wrong by %d (should be %d -- but is %d)\n", \
199 #type, (int)sizeof(type), (int)sizeof(type), (int)sizeof(type) - (int)size, (int)size, (int)sizeof(type)); \
200 rc++; \
201 } \
202 else \
203 printf("tstDeviceStructSize: info: sizeof(%s): %#x (%d)\n", #type, (int)sizeof(type), (int)sizeof(type)); \
204 } while (0)
205
206/**
207 * Checks the alignment of a struct member.
208 */
209#define CHECK_MEMBER_ALIGNMENT(strct, member, align) \
210 do \
211 { \
212 if (RT_OFFSETOF(strct, member) & ((align) - 1) ) \
213 { \
214 printf("tstDeviceStructSize: error! %s::%s offset=%#x (%u) expected alignment %#x, meaning %#x (%u) off\n", \
215 #strct, #member, \
216 (unsigned)RT_OFFSETOF(strct, member), \
217 (unsigned)RT_OFFSETOF(strct, member), \
218 (unsigned)(align), \
219 (unsigned)(((align) - RT_OFFSETOF(strct, member)) & ((align) - 1)), \
220 (unsigned)(((align) - RT_OFFSETOF(strct, member)) & ((align) - 1)) ); \
221 rc++; \
222 } \
223 } while (0)
224
225/**
226 * Checks that the size of a type is aligned correctly.
227 */
228#define CHECK_SIZE_ALIGNMENT(type, align) \
229 do { \
230 if (RT_ALIGN_Z(sizeof(type), (align)) != sizeof(type)) \
231 { \
232 printf("tstDeviceStructSize: error! %s size=%#x (%u), align=%#x %#x (%u) bytes off\n", \
233 #type, \
234 (unsigned)sizeof(type), \
235 (unsigned)sizeof(type), \
236 (align), \
237 (unsigned)RT_ALIGN_Z(sizeof(type), align) - (unsigned)sizeof(type), \
238 (unsigned)RT_ALIGN_Z(sizeof(type), align) - (unsigned)sizeof(type)); \
239 rc++; \
240 } \
241 } while (0)
242
243/**
244 * Checks that a internal struct padding is big enough.
245 */
246#define CHECK_PADDING(strct, member, align) \
247 do \
248 { \
249 strct *p = NULL; NOREF(p); \
250 if (sizeof(p->member.s) > sizeof(p->member.padding)) \
251 { \
252 printf("tstDeviceStructSize: error! padding of %s::%s is too small, padding=%d struct=%d correct=%d\n", #strct, #member, \
253 (int)sizeof(p->member.padding), (int)sizeof(p->member.s), (int)RT_ALIGN_Z(sizeof(p->member.s), (align))); \
254 rc++; \
255 } \
256 else if (RT_ALIGN_Z(sizeof(p->member.padding), (align)) != sizeof(p->member.padding)) \
257 { \
258 printf("tstDeviceStructSize: error! padding of %s::%s is misaligned, padding=%d correct=%d\n", #strct, #member, \
259 (int)sizeof(p->member.padding), (int)RT_ALIGN_Z(sizeof(p->member.s), (align))); \
260 rc++; \
261 } \
262 } while (0)
263
264/**
265 * Checks that a internal struct padding is big enough.
266 */
267#define CHECK_PADDING2(strct) \
268 do \
269 { \
270 strct *p = NULL; NOREF(p); \
271 if (sizeof(p->s) > sizeof(p->padding)) \
272 { \
273 printf("tstDeviceStructSize: error! padding of %s is too small, padding=%d struct=%d correct=%d\n", #strct, \
274 (int)sizeof(p->padding), (int)sizeof(p->s), (int)RT_ALIGN_Z(sizeof(p->s), 32)); \
275 rc++; \
276 } \
277 } while (0)
278
279/**
280 * Checks that a internal struct padding is big enough.
281 */
282#define CHECK_PADDING3(strct, member, pad_member) \
283 do \
284 { \
285 strct *p = NULL; NOREF(p); \
286 if (sizeof(p->member) > sizeof(p->pad_member)) \
287 { \
288 printf("tstDeviceStructSize: error! padding of %s::%s is too small, padding=%d struct=%d\n", #strct, #member, \
289 (int)sizeof(p->pad_member), (int)sizeof(p->member)); \
290 rc++; \
291 } \
292 } while (0)
293
294/**
295 * Prints the offset of a struct member.
296 */
297#define PRINT_OFFSET(strct, member) \
298 do \
299 { \
300 printf("tstDeviceStructSize: info: %s::%s offset %d sizeof %d\n", #strct, #member, (int)RT_OFFSETOF(strct, member), (int)RT_SIZEOFMEMB(strct, member)); \
301 } while (0)
302
303
304int main()
305{
306 int rc = 0;
307 printf("tstDeviceStructSize: TESTING\n");
308
309 /* Assert sanity */
310 CHECK_SIZE(uint128_t, 128/8);
311 CHECK_SIZE(int128_t, 128/8);
312 CHECK_SIZE(uint64_t, 64/8);
313 CHECK_SIZE(int64_t, 64/8);
314 CHECK_SIZE(uint32_t, 32/8);
315 CHECK_SIZE(int32_t, 32/8);
316 CHECK_SIZE(uint16_t, 16/8);
317 CHECK_SIZE(int16_t, 16/8);
318 CHECK_SIZE(uint8_t, 8/8);
319 CHECK_SIZE(int8_t, 8/8);
320
321 /* Basic alignment checks. */
322 CHECK_MEMBER_ALIGNMENT(PDMDEVINS, achInstanceData, 64);
323 CHECK_MEMBER_ALIGNMENT(PDMPCIDEV, Int.s, 16);
324 CHECK_MEMBER_ALIGNMENT(PDMPCIDEV, Int.s.aIORegions, 16);
325
326 /*
327 * Misc alignment checks (keep this somewhat alphabetical).
328 */
329 CHECK_MEMBER_ALIGNMENT(AC97STATE, CritSect, 8);
330
331 CHECK_MEMBER_ALIGNMENT(AHCI, lock, 8);
332 CHECK_MEMBER_ALIGNMENT(AHCI, aPorts[0], 8);
333 CHECK_MEMBER_ALIGNMENT(AHCIR3, aPorts[0], 8);
334
335 CHECK_MEMBER_ALIGNMENT(ATADEVSTATE, cTotalSectors, 8);
336 CHECK_MEMBER_ALIGNMENT(ATADEVSTATE, StatATADMA, 8);
337 CHECK_MEMBER_ALIGNMENT(ATADEVSTATE, StatReads, 8);
338 CHECK_MEMBER_ALIGNMENT(ATACONTROLLER, lock, 8);
339 CHECK_MEMBER_ALIGNMENT(ATACONTROLLER, StatAsyncOps, 8);
340 CHECK_MEMBER_ALIGNMENT(BUSLOGIC, CritSectIntr, 8);
341#ifdef VBOX_WITH_STATISTICS
342 CHECK_MEMBER_ALIGNMENT(DEVPIC, StatSetIrqRZ, 8);
343#endif
344#ifdef VBOX_WITH_E1000
345 CHECK_MEMBER_ALIGNMENT(E1KSTATE, cs, 8);
346 CHECK_MEMBER_ALIGNMENT(E1KSTATE, csRx, 8);
347 CHECK_MEMBER_ALIGNMENT(E1KSTATE, StatReceiveBytes, 8);
348#endif
349#ifdef VBOX_WITH_VIRTIO
350 CHECK_MEMBER_ALIGNMENT(VNETSTATE, StatReceiveBytes, 8);
351#endif
352 //CHECK_MEMBER_ALIGNMENT(E1KSTATE, csTx, 8);
353#ifdef VBOX_WITH_USB
354# ifdef VBOX_WITH_EHCI_IMPL
355 CHECK_MEMBER_ALIGNMENT(EHCI, RootHub, 8);
356# endif
357# ifdef VBOX_WITH_XHCI_IMPL
358 CHECK_MEMBER_ALIGNMENT(XHCI, aPorts, 8);
359 CHECK_MEMBER_ALIGNMENT(XHCI, aInterrupters, 8);
360 CHECK_MEMBER_ALIGNMENT(XHCI, aInterrupters[0].lock, 8);
361 CHECK_MEMBER_ALIGNMENT(XHCI, aInterrupters[1].lock, 8);
362 CHECK_MEMBER_ALIGNMENT(XHCI, cmdr_dqp, 8);
363 CHECK_MEMBER_ALIGNMENT(XHCI, hMmio, 8);
364# ifdef VBOX_WITH_STATISTICS
365 CHECK_MEMBER_ALIGNMENT(XHCI, StatErrorIsocUrbs, 8);
366 CHECK_MEMBER_ALIGNMENT(XHCI, StatIntrsCleared, 8);
367# endif
368# endif
369#endif
370 CHECK_MEMBER_ALIGNMENT(E1KSTATE, StatReceiveBytes, 8);
371 CHECK_MEMBER_ALIGNMENT(IOAPIC, au64RedirTable, 8);
372# ifdef VBOX_WITH_STATISTICS
373 CHECK_MEMBER_ALIGNMENT(IOAPIC, StatMmioReadRZ, 8);
374# endif
375 CHECK_MEMBER_ALIGNMENT(LSILOGICSCSI, aMessage, 8);
376 CHECK_MEMBER_ALIGNMENT(LSILOGICSCSI, ReplyPostQueueCritSect, 8);
377 CHECK_MEMBER_ALIGNMENT(LSILOGICSCSI, ReplyFreeQueueCritSect, 8);
378 CHECK_MEMBER_ALIGNMENT(LSILOGICSCSI, uReplyFreeQueueNextEntryFreeWrite, 8);
379#ifdef VBOX_WITH_USB
380 CHECK_MEMBER_ALIGNMENT(OHCI, RootHub, 8);
381# ifdef VBOX_WITH_STATISTICS
382 CHECK_MEMBER_ALIGNMENT(OHCI, StatCanceledIsocUrbs, 8);
383# endif
384#endif
385 CHECK_MEMBER_ALIGNMENT(DEVPCIBUS, apDevices, 64);
386 CHECK_MEMBER_ALIGNMENT(DEVPCIROOT, auPciApicIrqLevels, 16);
387 CHECK_MEMBER_ALIGNMENT(DEVPCIROOT, Piix3.auPciLegacyIrqLevels, 16);
388 CHECK_MEMBER_ALIGNMENT(PCNETSTATE, u64LastPoll, 8);
389 CHECK_MEMBER_ALIGNMENT(PCNETSTATE, CritSect, 8);
390 CHECK_MEMBER_ALIGNMENT(PCNETSTATE, StatReceiveBytes, 8);
391#ifdef VBOX_WITH_STATISTICS
392 CHECK_MEMBER_ALIGNMENT(PCNETSTATE, StatMMIOReadRZ, 8);
393#endif
394 CHECK_MEMBER_ALIGNMENT(PITSTATE, StatPITIrq, 8);
395 CHECK_MEMBER_ALIGNMENT(DEVSERIAL, UartCore, 8);
396 CHECK_MEMBER_ALIGNMENT(UARTCORE, CritSect, 8);
397#ifdef VBOX_WITH_VMSVGA
398 CHECK_SIZE(VMSVGAState, RT_ALIGN_Z(sizeof(VMSVGAState), 8));
399 CHECK_MEMBER_ALIGNMENT(VGASTATE, svga, 8);
400 CHECK_MEMBER_ALIGNMENT(VGASTATE, svga.au32ScratchRegion, 8);
401 CHECK_MEMBER_ALIGNMENT(VGASTATE, svga.StatRegBitsPerPixelWr, 8);
402#endif
403 CHECK_MEMBER_ALIGNMENT(VGASTATE, cMonitors, 8);
404 CHECK_MEMBER_ALIGNMENT(VGASTATE, GCPhysVRAM, 8);
405 CHECK_MEMBER_ALIGNMENT(VGASTATE, CritSect, 8);
406 CHECK_MEMBER_ALIGNMENT(VGASTATE, StatRZMemoryRead, 8);
407 CHECK_MEMBER_ALIGNMENT(VGASTATE, CritSectIRQ, 8);
408 CHECK_MEMBER_ALIGNMENT(VMMDEV, CritSect, 8);
409#ifdef VBOX_WITH_VIRTIO
410 CHECK_MEMBER_ALIGNMENT(VPCISTATE, cs, 8);
411 CHECK_MEMBER_ALIGNMENT(VPCISTATE, led, 4);
412 CHECK_MEMBER_ALIGNMENT(VPCISTATE, Queues, 8);
413#endif
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 printf("tstDeviceStructSize: Comparing HC and RC...\n");
443# include "tstDeviceStructSizeRC.h"
444#endif
445
446 /*
447 * Report result.
448 */
449 if (rc)
450 printf("tstDeviceStructSize: FAILURE - %d errors\n", rc);
451 else
452 printf("tstDeviceStructSize: SUCCESS\n");
453 return rc;
454}
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