VirtualBox

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

Last change on this file since 35885 was 35885, checked in by vboxsync, 14 years ago

Main, VMM, vboxshell: more PCI work (persistent settings, logging, more driver API), API consumer in vboxshell

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 11.2 KB
Line 
1/* $Id: tstDeviceStructSize.cpp 35885 2011-02-08 01:20:04Z 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-2010 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* Header Files *
22*******************************************************************************/
23#include <VBox/types.h>
24#include <VBox/x86.h>
25
26
27#define VBOX_WITH_HGCM /* grumble */
28#define VBOX_DEVICE_STRUCT_TESTCASE
29#undef LOG_GROUP
30#include "../Bus/DevPCI.cpp"
31#undef LOG_GROUP
32#include "../Graphics/DevVGA.cpp"
33#undef LOG_GROUP
34#include "../Input/DevPS2.cpp"
35#ifdef VBOX_WITH_E1000
36# undef LOG_GROUP
37# include "../Network/DevE1000.cpp"
38#endif
39#undef LOG_GROUP
40#include "../Network/DevPCNet.cpp"
41#ifdef VBOX_WITH_VIRTIO
42# undef LOG_GROUP
43# include "../Network/DevVirtioNet.cpp"
44#endif
45#undef LOG_GROUP
46#include "../PC/DevACPI.cpp"
47#undef LOG_GROUP
48#include "../PC/DevPIC.cpp"
49#undef LOG_GROUP
50#include "../PC/DevPit-i8254.cpp"
51#undef LOG_GROUP
52#include "../PC/DevRTC.cpp"
53#undef LOG_GROUP
54#include "../PC/DevAPIC.cpp"
55#undef LOG_GROUP
56#include "../PC/DevHPET.cpp"
57#undef LOG_GROUP
58#include "../PC/DevLPC.cpp"
59#undef LOG_GROUP
60#include "../PC/DevSMC.cpp"
61#undef LOG_GROUP
62#include "../Storage/DevATA.cpp"
63#ifdef VBOX_WITH_USB
64# undef LOG_GROUP
65# include "../USB/DevOHCI.cpp"
66# ifdef VBOX_WITH_EHCI
67# include "../USB/DevEHCI.cpp"
68# endif
69#endif
70#undef LOG_GROUP
71#include "../VMMDev/VMMDev.cpp"
72#undef LOG_GROUP
73#include "../Parallel/DevParallel.cpp"
74#undef LOG_GROUP
75#include "../Serial/DevSerial.cpp"
76#ifdef VBOX_WITH_AHCI
77# undef LOG_GROUP
78# include "../Storage/DevAHCI.cpp"
79#endif
80#ifdef VBOX_WITH_BUSLOGIC
81# undef LOG_GROUP
82# include "../Storage/DevBusLogic.cpp"
83#endif
84#ifdef VBOX_WITH_LSILOGIC
85# undef LOG_GROUP
86# include "../Storage/DevLsiLogicSCSI.cpp"
87#endif
88
89#ifdef VBOX_WITH_PCI_PASSTHROUGH
90# undef LOG_GROUP
91# include "../Bus/DevPciRaw.cpp"
92#endif
93
94#include <stdio.h>
95
96
97/*******************************************************************************
98* Defined Constants And Macros *
99*******************************************************************************/
100/**
101 * Checks the offset of a data member.
102 * @param type Type.
103 * @param off Correct offset.
104 * @param m Member name.
105 */
106#define CHECK_OFF(type, off, m) \
107 do { \
108 if (off != RT_OFFSETOF(type, m)) \
109 { \
110 printf("tstDeviceStructSize: error! %#010x %s Off by %d!! (off=%#x)\n", RT_OFFSETOF(type, m), #type "." #m, off - RT_OFFSETOF(type, m), off); \
111 rc++; \
112 } \
113 /*else */ \
114 /*printf("%#08x %s\n", RT_OFFSETOF(type, m), #m);*/ \
115 } while (0)
116
117/**
118 * Checks the size of type.
119 * @param type Type.
120 * @param size Correct size.
121 */
122#define CHECK_SIZE(type, size) \
123 do { \
124 if (size != sizeof(type)) \
125 { \
126 printf("tstDeviceStructSize: error! sizeof(%s): %#x (%d) Off by %d!!\n", #type, (int)sizeof(type), (int)sizeof(type), (int)(sizeof(type) - size)); \
127 rc++; \
128 } \
129 else \
130 printf("tstDeviceStructSize: info: sizeof(%s): %#x (%d)\n", #type, (int)sizeof(type), (int)sizeof(type)); \
131 } while (0)
132
133/**
134 * Checks the alignment of a struct member.
135 */
136#define CHECK_MEMBER_ALIGNMENT(strct, member, align) \
137 do \
138 { \
139 if (RT_OFFSETOF(strct, member) & ((align) - 1) ) \
140 { \
141 printf("tstDeviceStructSize: error! %s::%s offset=%#x (%u) expected alignment %x, meaning %#x (%u) off\n", \
142 #strct, #member, \
143 (unsigned)RT_OFFSETOF(strct, member), \
144 (unsigned)RT_OFFSETOF(strct, member), \
145 (unsigned)(align), \
146 (unsigned)(((align) - RT_OFFSETOF(strct, member)) & ((align) - 1)), \
147 (unsigned)(((align) - RT_OFFSETOF(strct, member)) & ((align) - 1)) ); \
148 rc++; \
149 } \
150 } while (0)
151
152/**
153 * Checks that the size of a type is aligned correctly.
154 */
155#define CHECK_SIZE_ALIGNMENT(type, align) \
156 do { \
157 if (RT_ALIGN_Z(sizeof(type), (align)) != sizeof(type)) \
158 { \
159 printf("tstDeviceStructSize: error! %s size=%#x (%u), align=%#x %#x (%u) bytes off\n", \
160 #type, \
161 (unsigned)sizeof(type), \
162 (unsigned)sizeof(type), \
163 (align), \
164 (unsigned)RT_ALIGN_Z(sizeof(type), align) - (unsigned)sizeof(type), \
165 (unsigned)RT_ALIGN_Z(sizeof(type), align) - (unsigned)sizeof(type)); \
166 rc++; \
167 } \
168 } while (0)
169
170/**
171 * Checks that a internal struct padding is big enough.
172 */
173#define CHECK_PADDING(strct, member, align) \
174 do \
175 { \
176 strct *p = NULL; NOREF(p); \
177 if (sizeof(p->member.s) > sizeof(p->member.padding)) \
178 { \
179 printf("tstDeviceStructSize: error! padding of %s::%s is too small, padding=%d struct=%d correct=%d\n", #strct, #member, \
180 (int)sizeof(p->member.padding), (int)sizeof(p->member.s), (int)RT_ALIGN_Z(sizeof(p->member.s), (align))); \
181 rc++; \
182 } \
183 else if (RT_ALIGN_Z(sizeof(p->member.padding), (align)) != sizeof(p->member.padding)) \
184 { \
185 printf("tstDeviceStructSize: error! padding of %s::%s is misaligned, padding=%d correct=%d\n", #strct, #member, \
186 (int)sizeof(p->member.padding), (int)RT_ALIGN_Z(sizeof(p->member.s), (align))); \
187 rc++; \
188 } \
189 } while (0)
190
191/**
192 * Checks that a internal struct padding is big enough.
193 */
194#define CHECK_PADDING2(strct) \
195 do \
196 { \
197 strct *p = NULL; NOREF(p); \
198 if (sizeof(p->s) > sizeof(p->padding)) \
199 { \
200 printf("tstDeviceStructSize: error! padding of %s is too small, padding=%d struct=%d correct=%d\n", #strct, \
201 (int)sizeof(p->padding), (int)sizeof(p->s), (int)RT_ALIGN_Z(sizeof(p->s), 32)); \
202 rc++; \
203 } \
204 } while (0)
205
206/**
207 * Checks that a internal struct padding is big enough.
208 */
209#define CHECK_PADDING3(strct, member, pad_member) \
210 do \
211 { \
212 strct *p = NULL; NOREF(p); \
213 if (sizeof(p->member) > sizeof(p->pad_member)) \
214 { \
215 printf("tstDeviceStructSize: error! padding of %s::%s is too small, padding=%d struct=%d\n", #strct, #member, \
216 (int)sizeof(p->pad_member), (int)sizeof(p->member)); \
217 rc++; \
218 } \
219 } while (0)
220
221/**
222 * Prints the offset of a struct member.
223 */
224#define PRINT_OFFSET(strct, member) \
225 do \
226 { \
227 printf("tstDeviceStructSize: info: %s::%s offset %d sizeof %d\n", #strct, #member, (int)RT_OFFSETOF(strct, member), (int)RT_SIZEOFMEMB(strct, member)); \
228 } while (0)
229
230
231int main()
232{
233 int rc = 0;
234 printf("tstDeviceStructSize: TESTING\n");
235
236 /* Assert sanity */
237 CHECK_SIZE(uint128_t, 128/8);
238 CHECK_SIZE(int128_t, 128/8);
239 CHECK_SIZE(uint64_t, 64/8);
240 CHECK_SIZE(int64_t, 64/8);
241 CHECK_SIZE(uint32_t, 32/8);
242 CHECK_SIZE(int32_t, 32/8);
243 CHECK_SIZE(uint16_t, 16/8);
244 CHECK_SIZE(int16_t, 16/8);
245 CHECK_SIZE(uint8_t, 8/8);
246 CHECK_SIZE(int8_t, 8/8);
247
248 /* Basic alignment checks. */
249 CHECK_MEMBER_ALIGNMENT(PDMDEVINS, achInstanceData, 64);
250 CHECK_MEMBER_ALIGNMENT(PCIDEVICE, Int.s, 16);
251 CHECK_MEMBER_ALIGNMENT(PCIDEVICE, Int.s.aIORegions, 16);
252
253 /*
254 * Misc alignment checks (keep this somewhat alphabetical).
255 */
256 CHECK_MEMBER_ALIGNMENT(AHCI, lock, 8);
257 CHECK_MEMBER_ALIGNMENT(AHCIPort, StatDMA, 8);
258 CHECK_MEMBER_ALIGNMENT(AHCIATACONTROLLER, lock, 8);
259 CHECK_MEMBER_ALIGNMENT(AHCIATACONTROLLER, StatAsyncOps, 8);
260#ifdef VBOX_WITH_STATISTICS
261 CHECK_MEMBER_ALIGNMENT(APICDeviceInfo, StatMMIOReadGC, 8);
262#endif
263 CHECK_MEMBER_ALIGNMENT(ATADevState, cTotalSectors, 8);
264 CHECK_MEMBER_ALIGNMENT(ATADevState, StatATADMA, 8);
265 CHECK_MEMBER_ALIGNMENT(ATADevState, StatReads, 8);
266 CHECK_MEMBER_ALIGNMENT(ATACONTROLLER, lock, 8);
267 CHECK_MEMBER_ALIGNMENT(ATACONTROLLER, StatAsyncOps, 8);
268 CHECK_MEMBER_ALIGNMENT(BUSLOGIC, CritSectIntr, 8);
269 CHECK_MEMBER_ALIGNMENT(DEVPARALLELSTATE, CritSect, 8);
270#ifdef VBOX_WITH_STATISTICS
271 CHECK_MEMBER_ALIGNMENT(DEVPIC, StatSetIrqGC, 8);
272#endif
273#ifdef VBOX_WITH_E1000
274 CHECK_MEMBER_ALIGNMENT(E1KSTATE, cs, 8);
275 CHECK_MEMBER_ALIGNMENT(E1KSTATE, csRx, 8);
276 CHECK_MEMBER_ALIGNMENT(E1KSTATE, StatReceiveBytes, 8);
277#endif
278#ifdef VBOX_WITH_VIRTIO
279 CHECK_MEMBER_ALIGNMENT(VNETSTATE, StatReceiveBytes, 8);
280#endif
281 //CHECK_MEMBER_ALIGNMENT(E1KSTATE, csTx, 8);
282#ifdef VBOX_WITH_USB
283# ifdef VBOX_WITH_EHCI
284 CHECK_MEMBER_ALIGNMENT(EHCI, RootHub, 8);
285# ifdef VBOX_WITH_STATISTICS
286 CHECK_MEMBER_ALIGNMENT(EHCI, StatCanceledIsocUrbs, 8);
287# endif
288# endif
289#endif
290 CHECK_MEMBER_ALIGNMENT(E1KSTATE, StatReceiveBytes, 8);
291#ifdef VBOX_WITH_STATISTICS
292 CHECK_MEMBER_ALIGNMENT(IOAPICState, StatMMIOReadGC, 8);
293 CHECK_MEMBER_ALIGNMENT(IOAPICState, StatMMIOReadGC, 8);
294#endif
295 CHECK_MEMBER_ALIGNMENT(KBDState, CritSect, 8);
296 CHECK_MEMBER_ALIGNMENT(LSILOGISCSI, ReplyPostQueueCritSect, 8);
297 CHECK_MEMBER_ALIGNMENT(LSILOGISCSI, ReplyFreeQueueCritSect, 8);
298#ifdef VBOX_WITH_USB
299 CHECK_MEMBER_ALIGNMENT(OHCI, RootHub, 8);
300# ifdef VBOX_WITH_STATISTICS
301 CHECK_MEMBER_ALIGNMENT(OHCI, StatCanceledIsocUrbs, 8);
302# endif
303#endif
304 CHECK_MEMBER_ALIGNMENT(PCIBUS, devices, 16);
305 CHECK_MEMBER_ALIGNMENT(PCIBUS, devices, 16);
306 CHECK_MEMBER_ALIGNMENT(PCIGLOBALS, pci_irq_levels, 16);
307 CHECK_MEMBER_ALIGNMENT(PCNetState, u64LastPoll, 8);
308 CHECK_MEMBER_ALIGNMENT(PCNetState, CritSect, 8);
309 CHECK_MEMBER_ALIGNMENT(PCNetState, StatReceiveBytes, 8);
310#ifdef VBOX_WITH_STATISTICS
311 CHECK_MEMBER_ALIGNMENT(PCNetState, StatMMIOReadRZ, 8);
312#endif
313 CHECK_MEMBER_ALIGNMENT(PITState, StatPITIrq, 8);
314 CHECK_MEMBER_ALIGNMENT(SerialState, CritSect, 8);
315 CHECK_MEMBER_ALIGNMENT(VGASTATE, Dev, 8);
316 CHECK_MEMBER_ALIGNMENT(VGASTATE, lock, 8);
317 CHECK_MEMBER_ALIGNMENT(VGASTATE, StatRZMemoryRead, 8);
318 CHECK_MEMBER_ALIGNMENT(VMMDevState, CritSect, 8);
319#ifdef VBOX_WITH_VIRTIO
320 CHECK_MEMBER_ALIGNMENT(VPCISTATE, cs, 8);
321 CHECK_MEMBER_ALIGNMENT(VPCISTATE, led, 4);
322 CHECK_MEMBER_ALIGNMENT(VPCISTATE, Queues, 8);
323#endif
324#ifdef VBOX_WITH_PCI_PASSTHROUGH
325 CHECK_MEMBER_ALIGNMENT(PCIRAWSENDREQ, u.aGetRegionInfo.u64RegionSize, 8);
326#endif
327
328#ifdef VBOX_WITH_RAW_MODE
329 /*
330 * Compare HC and RC.
331 */
332 printf("tstDeviceStructSize: Comparing HC and RC...\n");
333# include "tstDeviceStructSizeRC.h"
334#endif
335
336 /*
337 * Report result.
338 */
339 if (rc)
340 printf("tstDeviceStructSize: FAILURE - %d errors\n", rc);
341 else
342 printf("tstDeviceStructSize: SUCCESS\n");
343 return rc;
344}
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