VirtualBox

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

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

PCI: fixed issues with memory layout mismatch between RC and other contexts, tests added

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