您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

compiler.h 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. #ifndef COMPILER_H
  2. #define COMPILER_H
  3. /*
  4. * Doxygen can't cope with some of the more esoteric areas of C, so we
  5. * make its life simpler.
  6. *
  7. */
  8. #ifdef DOXYGEN
  9. #define __attribute__(x)
  10. #endif
  11. /** @file
  12. *
  13. * Global compiler definitions.
  14. *
  15. * This file is implicitly included by every @c .c file in Etherboot.
  16. * It defines global macros such as DBG().
  17. *
  18. * We arrange for each object to export the symbol @c obj_OBJECT
  19. * (where @c OBJECT is the object name, e.g. @c rtl8139) as a global
  20. * symbol, so that the linker can drag in selected object files from
  21. * the library using <tt> -u obj_OBJECT </tt>.
  22. *
  23. */
  24. /* Force visibility of all symbols to "hidden", i.e. inform gcc that
  25. * all symbol references resolve strictly within our final binary.
  26. * This avoids unnecessary PLT/GOT entries on x86_64.
  27. *
  28. * This is a stronger claim than specifying "-fvisibility=hidden",
  29. * since it also affects symbols marked with "extern".
  30. */
  31. #ifndef ASSEMBLY
  32. #if __GNUC__ >= 4
  33. #pragma GCC visibility push(hidden)
  34. #endif
  35. #endif /* ASSEMBLY */
  36. /**
  37. * @defgroup symmacros Macros to provide or require explicit symbols
  38. * @{
  39. */
  40. /** Provide a symbol within this object file */
  41. #ifdef ASSEMBLY
  42. #define PROVIDE_SYMBOL( _sym ) \
  43. .globl _sym ; \
  44. .comm _sym, 0
  45. #else /* ASSEMBLY */
  46. #define PROVIDE_SYMBOL( _sym ) \
  47. char _sym[0]
  48. #endif /* ASSEMBLY */
  49. /** Require a symbol within this object file */
  50. #ifdef ASSEMBLY
  51. #define REQUIRE_SYMBOL( _sym ) \
  52. .equ __need_ # _sym, _sym
  53. #else /* ASSEMBLY */
  54. #define REQUIRE_SYMBOL( _sym ) \
  55. __asm__ ( ".equ\t__need_" #_sym ", " #_sym )
  56. #endif /* ASSEMBLY */
  57. /** @} */
  58. /**
  59. * @defgroup objmacros Macros to provide or require explicit objects
  60. * @{
  61. */
  62. /* Not quite sure why cpp requires two levels of macro call in order
  63. * to actually expand OBJECT...
  64. */
  65. #undef _H1
  66. #define _H1( x, y ) x ## y
  67. #undef _H2
  68. #define _H2( x, y ) _H1 ( x, y )
  69. #define PREFIX_OBJECT( _prefix ) _H2 ( _prefix, OBJECT )
  70. #define OBJECT_SYMBOL PREFIX_OBJECT ( obj_ )
  71. /** Always provide the symbol for the current object (defined by -DOBJECT) */
  72. PROVIDE_SYMBOL ( OBJECT_SYMBOL );
  73. /** Explicitly require another object */
  74. #define REQUIRE_OBJECT( _obj ) REQUIRE_SYMBOL ( obj_ ## _obj )
  75. /** @} */
  76. /** Select file identifier for errno.h (if used) */
  77. #define ERRFILE PREFIX_OBJECT ( ERRFILE_ )
  78. /** @defgroup dbg Debugging infrastructure
  79. * @{
  80. */
  81. #ifndef ASSEMBLY
  82. /** @def DBG
  83. *
  84. * Print a debugging message.
  85. *
  86. * The debug level is set at build time by specifying the @c DEBUG=
  87. * parameter on the @c make command line. For example, to enable
  88. * debugging for the PCI bus functions (in pci.c) in a @c .dsk image
  89. * for the @c rtl8139 card, you could use the command line
  90. *
  91. * @code
  92. *
  93. * make bin/rtl8139.dsk DEBUG=pci
  94. *
  95. * @endcode
  96. *
  97. * This will enable the debugging statements (DBG()) in pci.c. If
  98. * debugging is not enabled, DBG() statements will be ignored.
  99. *
  100. * You can enable debugging in several objects simultaneously by
  101. * separating them with commas, as in
  102. *
  103. * @code
  104. *
  105. * make bin/rtl8139.dsk DEBUG=pci,buffer,heap
  106. *
  107. * @endcode
  108. *
  109. * You can increase the debugging level for an object by specifying it
  110. * with @c :N, where @c N is the level, as in
  111. *
  112. * @code
  113. *
  114. * make bin/rtl8139.dsk DEBUG=pci,buffer:2,heap
  115. *
  116. * @endcode
  117. *
  118. * which would enable debugging for the PCI, buffer-handling and
  119. * heap-allocation code, with the buffer-handling code at level 2.
  120. *
  121. */
  122. /*
  123. * If debug_OBJECT is set to a true value, the macro DBG(...) will
  124. * expand to printf(...) when compiling OBJECT, and the symbol
  125. * DEBUG_LEVEL will be inserted into the object file.
  126. *
  127. */
  128. #define DEBUG_SYMBOL PREFIX_OBJECT ( debug_ )
  129. /** printf() for debugging
  130. *
  131. * This function exists so that the DBG() macros can expand to
  132. * printf() calls without dragging the printf() prototype into scope.
  133. *
  134. * As far as the compiler is concerned, dbg_printf() and printf() are
  135. * completely unrelated calls; it's only at the assembly stage that
  136. * references to the dbg_printf symbol are collapsed into references
  137. * to the printf symbol.
  138. */
  139. extern int __attribute__ (( format ( printf, 1, 2 ) ))
  140. dbg_printf ( const char *fmt, ... ) asm ( "printf" );
  141. extern void dbg_autocolourise ( unsigned long id );
  142. extern void dbg_decolourise ( void );
  143. extern void dbg_hex_dump_da ( unsigned long dispaddr,
  144. const void *data, unsigned long len );
  145. #if DEBUG_SYMBOL
  146. #define DBGLVL_MAX DEBUG_SYMBOL
  147. #else
  148. #define DBGLVL_MAX 0
  149. #endif
  150. /* Allow for selective disabling of enabled debug levels */
  151. #if DBGLVL_MAX
  152. int __debug_disable;
  153. #define DBGLVL ( DBGLVL_MAX & ~__debug_disable )
  154. #define DBG_DISABLE( level ) do { \
  155. __debug_disable |= ( (level) & DBGLVL_MAX ); \
  156. } while ( 0 )
  157. #define DBG_ENABLE( level ) do { \
  158. __debug_disable &= ~( (level) & DBGLVL_MAX ); \
  159. } while ( 0 )
  160. #else
  161. #define DBGLVL 0
  162. #define DBG_DISABLE( level ) do { } while ( 0 )
  163. #define DBG_ENABLE( level ) do { } while ( 0 )
  164. #endif
  165. #define DBGLVL_LOG 1
  166. #define DBG_LOG ( DBGLVL & DBGLVL_LOG )
  167. #define DBGLVL_EXTRA 2
  168. #define DBG_EXTRA ( DBGLVL & DBGLVL_EXTRA )
  169. #define DBGLVL_PROFILE 4
  170. #define DBG_PROFILE ( DBGLVL & DBGLVL_PROFILE )
  171. #define DBGLVL_IO 8
  172. #define DBG_IO ( DBGLVL & DBGLVL_IO )
  173. /**
  174. * Print debugging message if we are at a certain debug level
  175. *
  176. * @v level Debug level
  177. * @v ... printf() argument list
  178. */
  179. #define DBG_IF( level, ... ) do { \
  180. if ( DBG_ ## level ) { \
  181. dbg_printf ( __VA_ARGS__ ); \
  182. } \
  183. } while ( 0 )
  184. /**
  185. * Print a hex dump if we are at a certain debug level
  186. *
  187. * @v level Debug level
  188. * @v dispaddr Display address
  189. * @v data Data to print
  190. * @v len Length of data
  191. */
  192. #define DBG_HDA_IF( level, dispaddr, data, len ) do { \
  193. if ( DBG_ ## level ) { \
  194. union { \
  195. unsigned long ul; \
  196. typeof ( dispaddr ) raw; \
  197. } da; \
  198. da.raw = dispaddr; \
  199. dbg_hex_dump_da ( da.ul, data, len ); \
  200. } \
  201. } while ( 0 )
  202. /**
  203. * Print a hex dump if we are at a certain debug level
  204. *
  205. * @v level Debug level
  206. * @v data Data to print
  207. * @v len Length of data
  208. */
  209. #define DBG_HD_IF( level, data, len ) do { \
  210. const void *_data = data; \
  211. DBG_HDA_IF ( level, _data, _data, len ); \
  212. } while ( 0 )
  213. /**
  214. * Select colour for debug messages if we are at a certain debug level
  215. *
  216. * @v level Debug level
  217. * @v id Message stream ID
  218. */
  219. #define DBG_AC_IF( level, id ) do { \
  220. if ( DBG_ ## level ) { \
  221. union { \
  222. unsigned long ul; \
  223. typeof ( id ) raw; \
  224. } dbg_stream; \
  225. dbg_stream.raw = id; \
  226. dbg_autocolourise ( dbg_stream.ul ); \
  227. } \
  228. } while ( 0 )
  229. /**
  230. * Revert colour for debug messages if we are at a certain debug level
  231. *
  232. * @v level Debug level
  233. */
  234. #define DBG_DC_IF( level ) do { \
  235. if ( DBG_ ## level ) { \
  236. dbg_decolourise(); \
  237. } \
  238. } while ( 0 )
  239. /* Autocolourising versions of the DBGxxx_IF() macros */
  240. #define DBGC_IF( level, id, ... ) do { \
  241. DBG_AC_IF ( level, id ); \
  242. DBG_IF ( level, __VA_ARGS__ ); \
  243. DBG_DC_IF ( level ); \
  244. } while ( 0 )
  245. #define DBGC_HDA_IF( level, id, ... ) do { \
  246. DBG_AC_IF ( level, id ); \
  247. DBG_HDA_IF ( level, __VA_ARGS__ ); \
  248. DBG_DC_IF ( level ); \
  249. } while ( 0 )
  250. #define DBGC_HD_IF( level, id, ... ) do { \
  251. DBG_AC_IF ( level, id ); \
  252. DBG_HD_IF ( level, __VA_ARGS__ ); \
  253. DBG_DC_IF ( level ); \
  254. } while ( 0 )
  255. /* Versions of the DBGxxx_IF() macros that imply DBGxxx_IF( LOG, ... )*/
  256. #define DBG( ... ) DBG_IF ( LOG, __VA_ARGS__ )
  257. #define DBG_HDA( ... ) DBG_HDA_IF ( LOG, __VA_ARGS__ )
  258. #define DBG_HD( ... ) DBG_HD_IF ( LOG, __VA_ARGS__ )
  259. #define DBGC( ... ) DBGC_IF ( LOG, __VA_ARGS__ )
  260. #define DBGC_HDA( ... ) DBGC_HDA_IF ( LOG, __VA_ARGS__ )
  261. #define DBGC_HD( ... ) DBGC_HD_IF ( LOG, __VA_ARGS__ )
  262. /* Versions of the DBGxxx_IF() macros that imply DBGxxx_IF( EXTRA, ... )*/
  263. #define DBG2( ... ) DBG_IF ( EXTRA, __VA_ARGS__ )
  264. #define DBG2_HDA( ... ) DBG_HDA_IF ( EXTRA, __VA_ARGS__ )
  265. #define DBG2_HD( ... ) DBG_HD_IF ( EXTRA, __VA_ARGS__ )
  266. #define DBGC2( ... ) DBGC_IF ( EXTRA, __VA_ARGS__ )
  267. #define DBGC2_HDA( ... ) DBGC_HDA_IF ( EXTRA, __VA_ARGS__ )
  268. #define DBGC2_HD( ... ) DBGC_HD_IF ( EXTRA, __VA_ARGS__ )
  269. /* Versions of the DBGxxx_IF() macros that imply DBGxxx_IF( PROFILE, ... )*/
  270. #define DBGP( ... ) DBG_IF ( PROFILE, __VA_ARGS__ )
  271. #define DBGP_HDA( ... ) DBG_HDA_IF ( PROFILE, __VA_ARGS__ )
  272. #define DBGP_HD( ... ) DBG_HD_IF ( PROFILE, __VA_ARGS__ )
  273. #define DBGCP( ... ) DBGC_IF ( PROFILE, __VA_ARGS__ )
  274. #define DBGCP_HDA( ... ) DBGC_HDA_IF ( PROFILE, __VA_ARGS__ )
  275. #define DBGCP_HD( ... ) DBGC_HD_IF ( PROFILE, __VA_ARGS__ )
  276. /* Versions of the DBGxxx_IF() macros that imply DBGxxx_IF( IO, ... )*/
  277. #define DBGIO( ... ) DBG_IF ( IO, __VA_ARGS__ )
  278. #define DBGIO_HDA( ... ) DBG_HDA_IF ( IO, __VA_ARGS__ )
  279. #define DBGIO_HD( ... ) DBG_HD_IF ( IO, __VA_ARGS__ )
  280. #define DBGCIO( ... ) DBGC_IF ( IO, __VA_ARGS__ )
  281. #define DBGCIO_HDA( ... ) DBGC_HDA_IF ( IO, __VA_ARGS__ )
  282. #define DBGCIO_HD( ... ) DBGC_HD_IF ( IO, __VA_ARGS__ )
  283. #if DEBUG_SYMBOL == 0
  284. #define NDEBUG
  285. #endif
  286. #endif /* ASSEMBLY */
  287. /** @} */
  288. /** @defgroup attrs Miscellaneous attributes
  289. * @{
  290. */
  291. #ifndef ASSEMBLY
  292. /** Declare a data structure as packed. */
  293. #define PACKED __attribute__ (( packed ))
  294. /** Declare a variable or data structure as unused. */
  295. #define __unused __attribute__ (( unused ))
  296. /**
  297. * Declare a function as pure - i.e. without side effects
  298. */
  299. #define __pure __attribute__ (( pure ))
  300. /**
  301. * Declare a function as const - i.e. it does not access global memory
  302. * (including dereferencing pointers passed to it) at all.
  303. * Must also not call any non-const functions.
  304. */
  305. #define __const __attribute__ (( const ))
  306. /**
  307. * Declare a function's pointer parameters as non-null - i.e. force
  308. * compiler to check pointers at compile time and enable possible
  309. * optimizations based on that fact
  310. */
  311. #define __nonnull __attribute__ (( nonnull ))
  312. /**
  313. * Declare a pointer returned by a function as a unique memory address
  314. * as returned by malloc-type functions.
  315. */
  316. #define __malloc __attribute__ (( malloc ))
  317. /**
  318. * Declare a function as used.
  319. *
  320. * Necessary only if the function is called only from assembler code.
  321. */
  322. #define __used __attribute__ (( used ))
  323. /** Declare a data structure to be aligned with 16-byte alignment */
  324. #define __aligned __attribute__ (( aligned ( 16 ) ))
  325. /** Declare a function to be always inline */
  326. #define __always_inline __attribute__ (( always_inline ))
  327. /**
  328. * Shared data.
  329. *
  330. * To save space in the binary when multiple-driver images are
  331. * compiled, uninitialised data areas can be shared between drivers.
  332. * This will typically be used to share statically-allocated receive
  333. * and transmit buffers between drivers.
  334. *
  335. * Use as e.g.
  336. *
  337. * @code
  338. *
  339. * struct {
  340. * char rx_buf[NUM_RX_BUF][RX_BUF_SIZE];
  341. * char tx_buf[TX_BUF_SIZE];
  342. * } my_static_data __shared;
  343. *
  344. * @endcode
  345. *
  346. */
  347. #define __shared __asm__ ( "_shared_bss" ) __aligned
  348. #endif /* ASSEMBLY */
  349. /** @} */
  350. /**
  351. * Optimisation barrier
  352. */
  353. #ifndef ASSEMBLY
  354. #define barrier() __asm__ __volatile__ ( "" : : : "memory" )
  355. #endif /* ASSEMBLY */
  356. /**
  357. * @defgroup licences Licence declarations
  358. *
  359. * For reasons that are partly historical, various different files
  360. * within the gPXE codebase have differing licences.
  361. *
  362. * @{
  363. */
  364. /** Declare a file as being in the public domain
  365. *
  366. * This licence declaration is applicable when a file states itself to
  367. * be in the public domain.
  368. */
  369. #define FILE_LICENCE_PUBLIC_DOMAIN \
  370. PROVIDE_SYMBOL ( __licence_public_domain )
  371. /** Declare a file as being under version 2 (or later) of the GNU GPL
  372. *
  373. * This licence declaration is applicable when a file states itself to
  374. * be licensed under the GNU GPL; "either version 2 of the License, or
  375. * (at your option) any later version".
  376. */
  377. #define FILE_LICENCE_GPL2_OR_LATER \
  378. PROVIDE_SYMBOL ( __licence_gpl2_or_later )
  379. /** Declare a file as being under version 2 of the GNU GPL
  380. *
  381. * This licence declaration is applicable when a file states itself to
  382. * be licensed under version 2 of the GPL, and does not include the
  383. * "or, at your option, any later version" clause.
  384. */
  385. #define FILE_LICENCE_GPL2_ONLY \
  386. PROVIDE_SYMBOL ( __licence_gpl2_only )
  387. /** Declare a file as being under any version of the GNU GPL
  388. *
  389. * This licence declaration is applicable when a file states itself to
  390. * be licensed under the GPL, but does not specify a version.
  391. *
  392. * According to section 9 of the GPLv2, "If the Program does not
  393. * specify a version number of this License, you may choose any
  394. * version ever published by the Free Software Foundation".
  395. */
  396. #define FILE_LICENCE_GPL_ANY \
  397. PROVIDE_SYMBOL ( __licence_gpl_any )
  398. /** Declare a file as being under the three-clause BSD licence
  399. *
  400. * This licence declaration is applicable when a file states itself to
  401. * be licensed under terms allowing redistribution in source and
  402. * binary forms (with or without modification) provided that:
  403. *
  404. * redistributions of source code retain the copyright notice,
  405. * list of conditions and any attached disclaimers
  406. *
  407. * redistributions in binary form reproduce the copyright notice,
  408. * list of conditions and any attached disclaimers in the
  409. * documentation and/or other materials provided with the
  410. * distribution
  411. *
  412. * the name of the author is not used to endorse or promote
  413. * products derived from the software without specific prior
  414. * written permission
  415. *
  416. * It is not necessary for the file to explicitly state that it is
  417. * under a "BSD" licence; only that the licensing terms be
  418. * functionally equivalent to the standard three-clause BSD licence.
  419. */
  420. #define FILE_LICENCE_BSD3 \
  421. PROVIDE_SYMBOL ( __licence_bsd3 )
  422. /** Declare a file as being under the two-clause BSD licence
  423. *
  424. * This licence declaration is applicable when a file states itself to
  425. * be licensed under terms allowing redistribution in source and
  426. * binary forms (with or without modification) provided that:
  427. *
  428. * redistributions of source code retain the copyright notice,
  429. * list of conditions and any attached disclaimers
  430. *
  431. * redistributions in binary form reproduce the copyright notice,
  432. * list of conditions and any attached disclaimers in the
  433. * documentation and/or other materials provided with the
  434. * distribution
  435. *
  436. * It is not necessary for the file to explicitly state that it is
  437. * under a "BSD" licence; only that the licensing terms be
  438. * functionally equivalent to the standard two-clause BSD licence.
  439. */
  440. #define FILE_LICENCE_BSD2 \
  441. PROVIDE_SYMBOL ( __licence_bsd2 )
  442. /** Declare a file as being under the one-clause MIT-style licence
  443. *
  444. * This licence declaration is applicable when a file states itself to
  445. * be licensed under terms allowing redistribution for any purpose
  446. * with or without fee, provided that the copyright notice and
  447. * permission notice appear in all copies.
  448. */
  449. #define FILE_LICENCE_MIT \
  450. PROVIDE_SYMBOL ( __licence_mit )
  451. /** Declare a particular licence as applying to a file */
  452. #define FILE_LICENCE( _licence ) FILE_LICENCE_ ## _licence
  453. /** @} */
  454. /* This file itself is under GPLv2-or-later */
  455. FILE_LICENCE ( GPL2_OR_LATER );
  456. #include <bits/compiler.h>
  457. #endif /* COMPILER_H */