5 * Doxygen can't cope with some of the more esoteric areas of C, so we
6 * make its life simpler.
10 #define __attribute__(x)
15 * Global compiler definitions.
17 * This file is implicitly included by every @c .c file in Etherboot.
18 * It defines global macros such as DBG().
20 * We arrange for each object to export the symbol @c obj_OBJECT
21 * (where @c OBJECT is the object name, e.g. @c rtl8139) as a global
22 * symbol, so that the linker can drag in selected object files from
23 * the library using <tt> -u obj_OBJECT </tt>.
27 /* Not quite sure why cpp requires two levels of macro call in order
28 * to actually expand OBJECT...
31 #define _H1( x, y ) x ## y
33 #define _H2( x, y ) _H1 ( x, y )
34 #define PREFIX_OBJECT(prefix) _H2 ( prefix, OBJECT )
35 #define OBJECT_SYMBOL PREFIX_OBJECT(obj_)
39 #define _XSTR(s) _STR(s)
40 #define OBJECT_SYMBOL_STR _XSTR ( OBJECT_SYMBOL )
49 __asm__ ( ".globl\t" OBJECT_SYMBOL_STR );
50 __asm__ ( ".equ\t" OBJECT_SYMBOL_STR ", 0" );
53 * Drag in an object by object name.
55 * Macro to allow objects to explicitly drag in other objects by
56 * object name. Used by config.c.
59 #define REQUIRE_OBJECT(object) \
60 __asm__ ( ".equ\tneed_" #object ", obj_" #object );
62 /* Force visibility of all symbols to "hidden", i.e. inform gcc that
63 * all symbol references resolve strictly within our final binary.
64 * This avoids unnecessary PLT/GOT entries on x86_64.
66 * This is a stronger claim than specifying "-fvisibility=hidden",
67 * since it also affects symbols marked with "extern".
70 #pragma GCC visibility push(hidden)
75 * Print a debugging message.
77 * The debug level is set at build time by specifying the @c DEBUG=
78 * parameter on the @c make command line. For example, to enable
79 * debugging for the PCI bus functions (in pci.c) in a @c .dsk image
80 * for the @c rtl8139 card, you could use the command line
84 * make bin/rtl8139.dsk DEBUG=pci
88 * This will enable the debugging statements (DBG()) in pci.c. If
89 * debugging is not enabled, DBG() statements will be ignored.
91 * You can enable debugging in several objects simultaneously by
92 * separating them with commas, as in
96 * make bin/rtl8139.dsk DEBUG=pci,buffer,heap
100 * You can increase the debugging level for an object by specifying it
101 * with @c :N, where @c N is the level, as in
105 * make bin/rtl8139.dsk DEBUG=pci,buffer:2,heap
109 * which would enable debugging for the PCI, buffer-handling and
110 * heap-allocation code, with the buffer-handling code at level 2.
115 * If debug_OBJECT is set to a true value, the macro DBG(...) will
116 * expand to printf(...) when compiling OBJECT, and the symbol
117 * DEBUG_LEVEL will be inserted into the object file.
120 #define DEBUG_SYMBOL PREFIX_OBJECT(debug_)
123 #define DEBUG_SYMBOL_STR _XSTR ( DEBUG_SYMBOL )
124 __asm__ ( ".equ\tDBGLVL, " DEBUG_SYMBOL_STR );
127 /** printf() for debugging
129 * This function exists so that the DBG() macros can expand to
130 * printf() calls without dragging the printf() prototype into scope.
132 * As far as the compiler is concerned, dbg_printf() and printf() are
133 * completely unrelated calls; it's only at the assembly stage that
134 * references to the dbg_printf symbol are collapsed into references
135 * to the printf symbol.
137 extern int __attribute__ (( format ( printf, 1, 2 ) ))
138 dbg_printf ( const char *fmt, ... ) asm ( "printf" );
140 extern void dbg_autocolourise ( unsigned long id );
141 extern void dbg_decolourise ( void );
142 extern void dbg_hex_dump_da ( unsigned long dispaddr,
143 const void *data, unsigned long len );
146 #define DBGLVL_MAX DEBUG_SYMBOL
151 /* Allow for selective disabling of enabled debug levels */
154 #define DBGLVL ( DBGLVL_MAX & ~__debug_disable )
155 #define DBG_DISABLE( level ) do { \
156 __debug_disable |= ( (level) & DBGLVL_MAX ); \
158 #define DBG_ENABLE( level ) do { \
159 __debug_disable &= ~( (level) & DBGLVL_MAX ); \
163 #define DBG_DISABLE( level ) do { } while ( 0 )
164 #define DBG_ENABLE( level ) do { } while ( 0 )
168 #define DBG_LOG ( DBGLVL & DBGLVL_LOG )
169 #define DBGLVL_EXTRA 2
170 #define DBG_EXTRA ( DBGLVL & DBGLVL_EXTRA )
171 #define DBGLVL_PROFILE 4
172 #define DBG_PROFILE ( DBGLVL & DBGLVL_PROFILE )
174 #define DBG_IO ( DBGLVL & DBGLVL_IO )
177 * Print debugging message if we are at a certain debug level
179 * @v level Debug level
180 * @v ... printf() argument list
182 #define DBG_IF( level, ... ) do { \
183 if ( DBG_ ## level ) { \
184 dbg_printf ( __VA_ARGS__ ); \
189 * Print a hex dump if we are at a certain debug level
191 * @v level Debug level
192 * @v dispaddr Display address
193 * @v data Data to print
194 * @v len Length of data
196 #define DBG_HDA_IF( level, dispaddr, data, len ) do { \
197 if ( DBG_ ## level ) { \
200 typeof ( dispaddr ) raw; \
203 dbg_hex_dump_da ( da.ul, data, len ); \
208 * Print a hex dump if we are at a certain debug level
210 * @v level Debug level
211 * @v data Data to print
212 * @v len Length of data
214 #define DBG_HD_IF( level, data, len ) do { \
215 DBG_HDA_IF ( level, data, data, len ); \
219 * Select colour for debug messages if we are at a certain debug level
221 * @v level Debug level
222 * @v id Message stream ID
224 #define DBG_AC_IF( level, id ) do { \
225 if ( DBG_ ## level ) { \
230 dbg_stream.raw = id; \
231 dbg_autocolourise ( dbg_stream.ul ); \
236 * Revert colour for debug messages if we are at a certain debug level
238 * @v level Debug level
240 #define DBG_DC_IF( level ) do { \
241 if ( DBG_ ## level ) { \
246 /* Autocolourising versions of the DBGxxx_IF() macros */
248 #define DBGC_IF( level, id, ... ) do { \
249 DBG_AC_IF ( level, id ); \
250 DBG_IF ( level, __VA_ARGS__ ); \
251 DBG_DC_IF ( level ); \
254 #define DBGC_HDA_IF( level, id, ... ) do { \
255 DBG_AC_IF ( level, id ); \
256 DBG_HDA_IF ( level, __VA_ARGS__ ); \
257 DBG_DC_IF ( level ); \
260 #define DBGC_HD_IF( level, id, ... ) do { \
261 DBG_AC_IF ( level, id ); \
262 DBG_HD_IF ( level, __VA_ARGS__ ); \
263 DBG_DC_IF ( level ); \
266 /* Versions of the DBGxxx_IF() macros that imply DBGxxx_IF( LOG, ... )*/
268 #define DBG( ... ) DBG_IF ( LOG, __VA_ARGS__ )
269 #define DBG_HDA( ... ) DBG_HDA_IF ( LOG, __VA_ARGS__ )
270 #define DBG_HD( ... ) DBG_HD_IF ( LOG, __VA_ARGS__ )
271 #define DBGC( ... ) DBGC_IF ( LOG, __VA_ARGS__ )
272 #define DBGC_HDA( ... ) DBGC_HDA_IF ( LOG, __VA_ARGS__ )
273 #define DBGC_HD( ... ) DBGC_HD_IF ( LOG, __VA_ARGS__ )
275 /* Versions of the DBGxxx_IF() macros that imply DBGxxx_IF( EXTRA, ... )*/
277 #define DBG2( ... ) DBG_IF ( EXTRA, __VA_ARGS__ )
278 #define DBG2_HDA( ... ) DBG_HDA_IF ( EXTRA, __VA_ARGS__ )
279 #define DBG2_HD( ... ) DBG_HD_IF ( EXTRA, __VA_ARGS__ )
280 #define DBGC2( ... ) DBGC_IF ( EXTRA, __VA_ARGS__ )
281 #define DBGC2_HDA( ... ) DBGC_HDA_IF ( EXTRA, __VA_ARGS__ )
282 #define DBGC2_HD( ... ) DBGC_HD_IF ( EXTRA, __VA_ARGS__ )
284 /* Versions of the DBGxxx_IF() macros that imply DBGxxx_IF( PROFILE, ... )*/
286 #define DBGP( ... ) DBG_IF ( PROFILE, __VA_ARGS__ )
287 #define DBGP_HDA( ... ) DBG_HDA_IF ( PROFILE, __VA_ARGS__ )
288 #define DBGP_HD( ... ) DBG_HD_IF ( PROFILE, __VA_ARGS__ )
289 #define DBGCP( ... ) DBGC_IF ( PROFILE, __VA_ARGS__ )
290 #define DBGCP_HDA( ... ) DBGC_HDA_IF ( PROFILE, __VA_ARGS__ )
291 #define DBGCP_HD( ... ) DBGC_HD_IF ( PROFILE, __VA_ARGS__ )
293 /* Versions of the DBGxxx_IF() macros that imply DBGxxx_IF( IO, ... )*/
295 #define DBGIO( ... ) DBG_IF ( IO, __VA_ARGS__ )
296 #define DBGIO_HDA( ... ) DBG_HDA_IF ( IO, __VA_ARGS__ )
297 #define DBGIO_HD( ... ) DBG_HD_IF ( IO, __VA_ARGS__ )
298 #define DBGCIO( ... ) DBGC_IF ( IO, __VA_ARGS__ )
299 #define DBGCIO_HDA( ... ) DBGC_HDA_IF ( IO, __VA_ARGS__ )
300 #define DBGCIO_HD( ... ) DBGC_HD_IF ( IO, __VA_ARGS__ )
303 #if DEBUG_SYMBOL == 0
307 /** Select file identifier for errno.h (if used) */
308 #define ERRFILE PREFIX_OBJECT ( ERRFILE_ )
310 /** Declare a data structure as packed. */
311 #define PACKED __attribute__ (( packed ))
313 /** Declare a variable or data structure as unused. */
314 #define __unused __attribute__ (( unused ))
317 * Declare a function as pure - i.e. without side effects
319 #define __pure __attribute__ (( pure ))
322 * Declare a function as const - i.e. it does not access global memory
323 * (including dereferencing pointers passed to it) at all.
324 * Must also not call any non-const functions.
326 #define __const __attribute__ (( const ))
329 * Declare a function's pointer parameters as non-null - i.e. force
330 * compiler to check pointers at compile time and enable possible
331 * optimizations based on that fact
333 #define __nonnull __attribute__ (( nonnull ))
336 * Declare a pointer returned by a function as a unique memory address
337 * as returned by malloc-type functions.
339 #define __malloc __attribute__ (( malloc ))
342 * Declare a function as used.
344 * Necessary only if the function is called only from assembler code.
346 #define __used __attribute__ (( used ))
348 /** Declare a data structure to be aligned with 16-byte alignment */
349 #define __aligned __attribute__ (( aligned ( 16 ) ))
351 /** Declare a function to be always inline */
352 #define __always_inline __attribute__ (( always_inline ))
357 * To save space in the binary when multiple-driver images are
358 * compiled, uninitialised data areas can be shared between drivers.
359 * This will typically be used to share statically-allocated receive
360 * and transmit buffers between drivers.
367 * char rx_buf[NUM_RX_BUF][RX_BUF_SIZE];
368 * char tx_buf[TX_BUF_SIZE];
369 * } my_static_data __shared;
374 #define __shared __asm__ ( "_shared_bss" ) __aligned
377 * Optimisation barrier
379 #define barrier() __asm__ __volatile__ ( "" : : : "memory" )
381 #endif /* ASSEMBLY */
383 #include <bits/compiler.h>
385 #endif /* COMPILER_H */