You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

compiler.h 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. /* Not quite sure why cpp requires two levels of macro call in order
  25. * to actually expand OBJECT...
  26. */
  27. #undef _H1
  28. #define _H1( x, y ) x ## y
  29. #undef _H2
  30. #define _H2( x, y ) _H1 ( x, y )
  31. #define PREFIX_OBJECT(prefix) _H2 ( prefix, OBJECT )
  32. #define OBJECT_SYMBOL PREFIX_OBJECT(obj_)
  33. #undef _STR
  34. #define _STR(s) #s
  35. #undef _XSTR
  36. #define _XSTR(s) _STR(s)
  37. #define OBJECT_SYMBOL_STR _XSTR ( OBJECT_SYMBOL )
  38. #ifdef ASSEMBLY
  39. .globl OBJECT_SYMBOL
  40. .equ OBJECT_SYMBOL, 0
  41. #else /* ASSEMBLY */
  42. __asm__ ( ".globl\t" OBJECT_SYMBOL_STR );
  43. __asm__ ( ".equ\t" OBJECT_SYMBOL_STR ", 0" );
  44. /**
  45. * Drag in an object by object name.
  46. *
  47. * Macro to allow objects to explicitly drag in other objects by
  48. * object name. Used by config.c.
  49. *
  50. */
  51. #define REQUIRE_OBJECT(object) \
  52. __asm__ ( ".equ\tneed_" #object ", obj_" #object );
  53. /* Force visibility of all symbols to "hidden", i.e. inform gcc that
  54. * all symbol references resolve strictly within our final binary.
  55. * This avoids unnecessary PLT/GOT entries on x86_64.
  56. *
  57. * This is a stronger claim than specifying "-fvisibility=hidden",
  58. * since it also affects symbols marked with "extern".
  59. */
  60. #if __GNUC__ >= 4
  61. #pragma GCC visibility push(hidden)
  62. #endif
  63. /** @def DBG
  64. *
  65. * Print a debugging message.
  66. *
  67. * The debug level is set at build time by specifying the @c DEBUG=
  68. * parameter on the @c make command line. For example, to enable
  69. * debugging for the PCI bus functions (in pci.c) in a @c .dsk image
  70. * for the @c rtl8139 card, you could use the command line
  71. *
  72. * @code
  73. *
  74. * make bin/rtl8139.dsk DEBUG=pci
  75. *
  76. * @endcode
  77. *
  78. * This will enable the debugging statements (DBG()) in pci.c. If
  79. * debugging is not enabled, DBG() statements will be ignored.
  80. *
  81. * You can enable debugging in several objects simultaneously by
  82. * separating them with commas, as in
  83. *
  84. * @code
  85. *
  86. * make bin/rtl8139.dsk DEBUG=pci,buffer,heap
  87. *
  88. * @endcode
  89. *
  90. * You can increase the debugging level for an object by specifying it
  91. * with @c :N, where @c N is the level, as in
  92. *
  93. * @code
  94. *
  95. * make bin/rtl8139.dsk DEBUG=pci,buffer:2,heap
  96. *
  97. * @endcode
  98. *
  99. * which would enable debugging for the PCI, buffer-handling and
  100. * heap-allocation code, with the buffer-handling code at level 2.
  101. *
  102. */
  103. /*
  104. * If debug_OBJECT is set to a true value, the macro DBG(...) will
  105. * expand to printf(...) when compiling OBJECT, and the symbol
  106. * DEBUG_LEVEL will be inserted into the object file.
  107. *
  108. */
  109. #define DEBUG_SYMBOL PREFIX_OBJECT(debug_)
  110. #if DEBUG_SYMBOL
  111. #define DEBUG_SYMBOL_STR _XSTR ( DEBUG_SYMBOL )
  112. __asm__ ( ".equ\tDBGLVL, " DEBUG_SYMBOL_STR );
  113. #endif
  114. /** printf() for debugging
  115. *
  116. * This function exists so that the DBG() macros can expand to
  117. * printf() calls without dragging the printf() prototype into scope.
  118. *
  119. * As far as the compiler is concerned, dbg_printf() and printf() are
  120. * completely unrelated calls; it's only at the assembly stage that
  121. * references to the dbg_printf symbol are collapsed into references
  122. * to the printf symbol.
  123. */
  124. extern int __attribute__ (( format ( printf, 1, 2 ) ))
  125. dbg_printf ( const char *fmt, ... ) asm ( "printf" );
  126. extern void dbg_autocolourise ( unsigned long id );
  127. extern void dbg_decolourise ( void );
  128. extern void dbg_hex_dump_da ( unsigned long dispaddr,
  129. const void *data, unsigned long len );
  130. #if DEBUG_SYMBOL
  131. #define DBGLVL_MAX DEBUG_SYMBOL
  132. #else
  133. #define DBGLVL_MAX 0
  134. #endif
  135. /* Allow for selective disabling of enabled debug levels */
  136. #if DBGLVL_MAX
  137. int __debug_disable;
  138. #define DBGLVL ( DBGLVL_MAX & ~__debug_disable )
  139. #define DBG_DISABLE( level ) do { \
  140. __debug_disable |= ( (level) & DBGLVL_MAX ); \
  141. } while ( 0 )
  142. #define DBG_ENABLE( level ) do { \
  143. __debug_disable &= ~( (level) & DBGLVL_MAX ); \
  144. } while ( 0 )
  145. #else
  146. #define DBGLVL 0
  147. #define DBG_DISABLE( level ) do { } while ( 0 )
  148. #define DBG_ENABLE( level ) do { } while ( 0 )
  149. #endif
  150. #define DBGLVL_LOG 1
  151. #define DBG_LOG ( DBGLVL & DBGLVL_LOG )
  152. #define DBGLVL_EXTRA 2
  153. #define DBG_EXTRA ( DBGLVL & DBGLVL_EXTRA )
  154. #define DBGLVL_PROFILE 4
  155. #define DBG_PROFILE ( DBGLVL & DBGLVL_PROFILE )
  156. #define DBGLVL_IO 8
  157. #define DBG_IO ( DBGLVL & DBGLVL_IO )
  158. /**
  159. * Print debugging message if we are at a certain debug level
  160. *
  161. * @v level Debug level
  162. * @v ... printf() argument list
  163. */
  164. #define DBG_IF( level, ... ) do { \
  165. if ( DBG_ ## level ) { \
  166. dbg_printf ( __VA_ARGS__ ); \
  167. } \
  168. } while ( 0 )
  169. /**
  170. * Print a hex dump if we are at a certain debug level
  171. *
  172. * @v level Debug level
  173. * @v dispaddr Display address
  174. * @v data Data to print
  175. * @v len Length of data
  176. */
  177. #define DBG_HDA_IF( level, dispaddr, data, len ) do { \
  178. if ( DBG_ ## level ) { \
  179. union { \
  180. unsigned long ul; \
  181. typeof ( dispaddr ) raw; \
  182. } da; \
  183. da.raw = dispaddr; \
  184. dbg_hex_dump_da ( da.ul, data, len ); \
  185. } \
  186. } while ( 0 )
  187. /**
  188. * Print a hex dump if we are at a certain debug level
  189. *
  190. * @v level Debug level
  191. * @v data Data to print
  192. * @v len Length of data
  193. */
  194. #define DBG_HD_IF( level, data, len ) do { \
  195. const void *_data = data; \
  196. DBG_HDA_IF ( level, _data, _data, len ); \
  197. } while ( 0 )
  198. /**
  199. * Select colour for debug messages if we are at a certain debug level
  200. *
  201. * @v level Debug level
  202. * @v id Message stream ID
  203. */
  204. #define DBG_AC_IF( level, id ) do { \
  205. if ( DBG_ ## level ) { \
  206. union { \
  207. unsigned long ul; \
  208. typeof ( id ) raw; \
  209. } dbg_stream; \
  210. dbg_stream.raw = id; \
  211. dbg_autocolourise ( dbg_stream.ul ); \
  212. } \
  213. } while ( 0 )
  214. /**
  215. * Revert colour for debug messages if we are at a certain debug level
  216. *
  217. * @v level Debug level
  218. */
  219. #define DBG_DC_IF( level ) do { \
  220. if ( DBG_ ## level ) { \
  221. dbg_decolourise(); \
  222. } \
  223. } while ( 0 )
  224. /* Autocolourising versions of the DBGxxx_IF() macros */
  225. #define DBGC_IF( level, id, ... ) do { \
  226. DBG_AC_IF ( level, id ); \
  227. DBG_IF ( level, __VA_ARGS__ ); \
  228. DBG_DC_IF ( level ); \
  229. } while ( 0 )
  230. #define DBGC_HDA_IF( level, id, ... ) do { \
  231. DBG_AC_IF ( level, id ); \
  232. DBG_HDA_IF ( level, __VA_ARGS__ ); \
  233. DBG_DC_IF ( level ); \
  234. } while ( 0 )
  235. #define DBGC_HD_IF( level, id, ... ) do { \
  236. DBG_AC_IF ( level, id ); \
  237. DBG_HD_IF ( level, __VA_ARGS__ ); \
  238. DBG_DC_IF ( level ); \
  239. } while ( 0 )
  240. /* Versions of the DBGxxx_IF() macros that imply DBGxxx_IF( LOG, ... )*/
  241. #define DBG( ... ) DBG_IF ( LOG, __VA_ARGS__ )
  242. #define DBG_HDA( ... ) DBG_HDA_IF ( LOG, __VA_ARGS__ )
  243. #define DBG_HD( ... ) DBG_HD_IF ( LOG, __VA_ARGS__ )
  244. #define DBGC( ... ) DBGC_IF ( LOG, __VA_ARGS__ )
  245. #define DBGC_HDA( ... ) DBGC_HDA_IF ( LOG, __VA_ARGS__ )
  246. #define DBGC_HD( ... ) DBGC_HD_IF ( LOG, __VA_ARGS__ )
  247. /* Versions of the DBGxxx_IF() macros that imply DBGxxx_IF( EXTRA, ... )*/
  248. #define DBG2( ... ) DBG_IF ( EXTRA, __VA_ARGS__ )
  249. #define DBG2_HDA( ... ) DBG_HDA_IF ( EXTRA, __VA_ARGS__ )
  250. #define DBG2_HD( ... ) DBG_HD_IF ( EXTRA, __VA_ARGS__ )
  251. #define DBGC2( ... ) DBGC_IF ( EXTRA, __VA_ARGS__ )
  252. #define DBGC2_HDA( ... ) DBGC_HDA_IF ( EXTRA, __VA_ARGS__ )
  253. #define DBGC2_HD( ... ) DBGC_HD_IF ( EXTRA, __VA_ARGS__ )
  254. /* Versions of the DBGxxx_IF() macros that imply DBGxxx_IF( PROFILE, ... )*/
  255. #define DBGP( ... ) DBG_IF ( PROFILE, __VA_ARGS__ )
  256. #define DBGP_HDA( ... ) DBG_HDA_IF ( PROFILE, __VA_ARGS__ )
  257. #define DBGP_HD( ... ) DBG_HD_IF ( PROFILE, __VA_ARGS__ )
  258. #define DBGCP( ... ) DBGC_IF ( PROFILE, __VA_ARGS__ )
  259. #define DBGCP_HDA( ... ) DBGC_HDA_IF ( PROFILE, __VA_ARGS__ )
  260. #define DBGCP_HD( ... ) DBGC_HD_IF ( PROFILE, __VA_ARGS__ )
  261. /* Versions of the DBGxxx_IF() macros that imply DBGxxx_IF( IO, ... )*/
  262. #define DBGIO( ... ) DBG_IF ( IO, __VA_ARGS__ )
  263. #define DBGIO_HDA( ... ) DBG_HDA_IF ( IO, __VA_ARGS__ )
  264. #define DBGIO_HD( ... ) DBG_HD_IF ( IO, __VA_ARGS__ )
  265. #define DBGCIO( ... ) DBGC_IF ( IO, __VA_ARGS__ )
  266. #define DBGCIO_HDA( ... ) DBGC_HDA_IF ( IO, __VA_ARGS__ )
  267. #define DBGCIO_HD( ... ) DBGC_HD_IF ( IO, __VA_ARGS__ )
  268. #if DEBUG_SYMBOL == 0
  269. #define NDEBUG
  270. #endif
  271. /** Select file identifier for errno.h (if used) */
  272. #define ERRFILE PREFIX_OBJECT ( ERRFILE_ )
  273. /** Declare a data structure as packed. */
  274. #define PACKED __attribute__ (( packed ))
  275. /** Declare a variable or data structure as unused. */
  276. #define __unused __attribute__ (( unused ))
  277. /**
  278. * Declare a function as pure - i.e. without side effects
  279. */
  280. #define __pure __attribute__ (( pure ))
  281. /**
  282. * Declare a function as const - i.e. it does not access global memory
  283. * (including dereferencing pointers passed to it) at all.
  284. * Must also not call any non-const functions.
  285. */
  286. #define __const __attribute__ (( const ))
  287. /**
  288. * Declare a function's pointer parameters as non-null - i.e. force
  289. * compiler to check pointers at compile time and enable possible
  290. * optimizations based on that fact
  291. */
  292. #define __nonnull __attribute__ (( nonnull ))
  293. /**
  294. * Declare a pointer returned by a function as a unique memory address
  295. * as returned by malloc-type functions.
  296. */
  297. #define __malloc __attribute__ (( malloc ))
  298. /**
  299. * Declare a function as used.
  300. *
  301. * Necessary only if the function is called only from assembler code.
  302. */
  303. #define __used __attribute__ (( used ))
  304. /** Declare a data structure to be aligned with 16-byte alignment */
  305. #define __aligned __attribute__ (( aligned ( 16 ) ))
  306. /** Declare a function to be always inline */
  307. #define __always_inline __attribute__ (( always_inline ))
  308. /**
  309. * Shared data.
  310. *
  311. * To save space in the binary when multiple-driver images are
  312. * compiled, uninitialised data areas can be shared between drivers.
  313. * This will typically be used to share statically-allocated receive
  314. * and transmit buffers between drivers.
  315. *
  316. * Use as e.g.
  317. *
  318. * @code
  319. *
  320. * struct {
  321. * char rx_buf[NUM_RX_BUF][RX_BUF_SIZE];
  322. * char tx_buf[TX_BUF_SIZE];
  323. * } my_static_data __shared;
  324. *
  325. * @endcode
  326. *
  327. */
  328. #define __shared __asm__ ( "_shared_bss" ) __aligned
  329. /**
  330. * Optimisation barrier
  331. */
  332. #define barrier() __asm__ __volatile__ ( "" : : : "memory" )
  333. #endif /* ASSEMBLY */
  334. #include <bits/compiler.h>
  335. #endif /* COMPILER_H */