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 /* Force visibility of all symbols to "hidden", i.e. inform gcc that
28 * all symbol references resolve strictly within our final binary.
29 * This avoids unnecessary PLT/GOT entries on x86_64.
31 * This is a stronger claim than specifying "-fvisibility=hidden",
32 * since it also affects symbols marked with "extern".
36 #pragma GCC visibility push(hidden)
41 * @defgroup symmacros Macros to provide or require explicit symbols
45 /** Provide a symbol within this object file */
47 #define PROVIDE_SYMBOL( _sym ) \
51 #define PROVIDE_SYMBOL( _sym ) \
55 /** Require a symbol within this object file */
57 #define REQUIRE_SYMBOL( _sym ) \
58 .equ __need_ # _sym, _sym
60 #define REQUIRE_SYMBOL( _sym ) \
61 __asm__ ( ".equ\t__need_" #_sym ", " #_sym )
67 * @defgroup objmacros Macros to provide or require explicit objects
71 /* Not quite sure why cpp requires two levels of macro call in order
72 * to actually expand OBJECT...
75 #define _H1( x, y ) x ## y
77 #define _H2( x, y ) _H1 ( x, y )
78 #define PREFIX_OBJECT( _prefix ) _H2 ( _prefix, OBJECT )
79 #define OBJECT_SYMBOL PREFIX_OBJECT ( obj_ )
81 /** Always provide the symbol for the current object (defined by -DOBJECT) */
82 PROVIDE_SYMBOL ( OBJECT_SYMBOL );
84 /** Explicitly require another object */
85 #define REQUIRE_OBJECT( _obj ) REQUIRE_SYMBOL ( obj_ ## _obj )
89 /** Select file identifier for errno.h (if used) */
90 #define ERRFILE PREFIX_OBJECT ( ERRFILE_ )
92 /** @defgroup dbg Debugging infrastructure
99 * Print a debugging message.
101 * The debug level is set at build time by specifying the @c DEBUG=
102 * parameter on the @c make command line. For example, to enable
103 * debugging for the PCI bus functions (in pci.c) in a @c .dsk image
104 * for the @c rtl8139 card, you could use the command line
108 * make bin/rtl8139.dsk DEBUG=pci
112 * This will enable the debugging statements (DBG()) in pci.c. If
113 * debugging is not enabled, DBG() statements will be ignored.
115 * You can enable debugging in several objects simultaneously by
116 * separating them with commas, as in
120 * make bin/rtl8139.dsk DEBUG=pci,buffer,heap
124 * You can increase the debugging level for an object by specifying it
125 * with @c :N, where @c N is the level, as in
129 * make bin/rtl8139.dsk DEBUG=pci,buffer:2,heap
133 * which would enable debugging for the PCI, buffer-handling and
134 * heap-allocation code, with the buffer-handling code at level 2.
139 * If debug_OBJECT is set to a true value, the macro DBG(...) will
140 * expand to printf(...) when compiling OBJECT, and the symbol
141 * DEBUG_LEVEL will be inserted into the object file.
144 #define DEBUG_SYMBOL PREFIX_OBJECT ( debug_ )
146 /** printf() for debugging
148 * This function exists so that the DBG() macros can expand to
149 * printf() calls without dragging the printf() prototype into scope.
151 * As far as the compiler is concerned, dbg_printf() and printf() are
152 * completely unrelated calls; it's only at the assembly stage that
153 * references to the dbg_printf symbol are collapsed into references
154 * to the printf symbol.
156 extern int __attribute__ (( format ( printf, 1, 2 ) ))
157 dbg_printf ( const char *fmt, ... ) asm ( "printf" );
159 extern void dbg_autocolourise ( unsigned long id );
160 extern void dbg_decolourise ( void );
161 extern void dbg_hex_dump_da ( unsigned long dispaddr,
162 const void *data, unsigned long len );
165 #define DBGLVL_MAX DEBUG_SYMBOL
170 /* Allow for selective disabling of enabled debug levels */
173 #define DBGLVL ( DBGLVL_MAX & ~__debug_disable )
174 #define DBG_DISABLE( level ) do { \
175 __debug_disable |= ( (level) & DBGLVL_MAX ); \
177 #define DBG_ENABLE( level ) do { \
178 __debug_disable &= ~( (level) & DBGLVL_MAX ); \
182 #define DBG_DISABLE( level ) do { } while ( 0 )
183 #define DBG_ENABLE( level ) do { } while ( 0 )
187 #define DBG_LOG ( DBGLVL & DBGLVL_LOG )
188 #define DBGLVL_EXTRA 2
189 #define DBG_EXTRA ( DBGLVL & DBGLVL_EXTRA )
190 #define DBGLVL_PROFILE 4
191 #define DBG_PROFILE ( DBGLVL & DBGLVL_PROFILE )
193 #define DBG_IO ( DBGLVL & DBGLVL_IO )
196 * Print debugging message if we are at a certain debug level
198 * @v level Debug level
199 * @v ... printf() argument list
201 #define DBG_IF( level, ... ) do { \
202 if ( DBG_ ## level ) { \
203 dbg_printf ( __VA_ARGS__ ); \
208 * Print a hex dump if we are at a certain debug level
210 * @v level Debug level
211 * @v dispaddr Display address
212 * @v data Data to print
213 * @v len Length of data
215 #define DBG_HDA_IF( level, dispaddr, data, len ) do { \
216 if ( DBG_ ## level ) { \
219 typeof ( dispaddr ) raw; \
222 dbg_hex_dump_da ( da.ul, data, len ); \
227 * Print a hex dump if we are at a certain debug level
229 * @v level Debug level
230 * @v data Data to print
231 * @v len Length of data
233 #define DBG_HD_IF( level, data, len ) do { \
234 const void *_data = data; \
235 DBG_HDA_IF ( level, _data, _data, len ); \
239 * Select colour for debug messages if we are at a certain debug level
241 * @v level Debug level
242 * @v id Message stream ID
244 #define DBG_AC_IF( level, id ) do { \
245 if ( DBG_ ## level ) { \
250 dbg_stream.raw = id; \
251 dbg_autocolourise ( dbg_stream.ul ); \
256 * Revert colour for debug messages if we are at a certain debug level
258 * @v level Debug level
260 #define DBG_DC_IF( level ) do { \
261 if ( DBG_ ## level ) { \
266 /* Autocolourising versions of the DBGxxx_IF() macros */
268 #define DBGC_IF( level, id, ... ) do { \
269 DBG_AC_IF ( level, id ); \
270 DBG_IF ( level, __VA_ARGS__ ); \
271 DBG_DC_IF ( level ); \
274 #define DBGC_HDA_IF( level, id, ... ) do { \
275 DBG_AC_IF ( level, id ); \
276 DBG_HDA_IF ( level, __VA_ARGS__ ); \
277 DBG_DC_IF ( level ); \
280 #define DBGC_HD_IF( level, id, ... ) do { \
281 DBG_AC_IF ( level, id ); \
282 DBG_HD_IF ( level, __VA_ARGS__ ); \
283 DBG_DC_IF ( level ); \
286 /* Versions of the DBGxxx_IF() macros that imply DBGxxx_IF( LOG, ... )*/
288 #define DBG( ... ) DBG_IF ( LOG, __VA_ARGS__ )
289 #define DBG_HDA( ... ) DBG_HDA_IF ( LOG, __VA_ARGS__ )
290 #define DBG_HD( ... ) DBG_HD_IF ( LOG, __VA_ARGS__ )
291 #define DBGC( ... ) DBGC_IF ( LOG, __VA_ARGS__ )
292 #define DBGC_HDA( ... ) DBGC_HDA_IF ( LOG, __VA_ARGS__ )
293 #define DBGC_HD( ... ) DBGC_HD_IF ( LOG, __VA_ARGS__ )
295 /* Versions of the DBGxxx_IF() macros that imply DBGxxx_IF( EXTRA, ... )*/
297 #define DBG2( ... ) DBG_IF ( EXTRA, __VA_ARGS__ )
298 #define DBG2_HDA( ... ) DBG_HDA_IF ( EXTRA, __VA_ARGS__ )
299 #define DBG2_HD( ... ) DBG_HD_IF ( EXTRA, __VA_ARGS__ )
300 #define DBGC2( ... ) DBGC_IF ( EXTRA, __VA_ARGS__ )
301 #define DBGC2_HDA( ... ) DBGC_HDA_IF ( EXTRA, __VA_ARGS__ )
302 #define DBGC2_HD( ... ) DBGC_HD_IF ( EXTRA, __VA_ARGS__ )
304 /* Versions of the DBGxxx_IF() macros that imply DBGxxx_IF( PROFILE, ... )*/
306 #define DBGP( ... ) DBG_IF ( PROFILE, __VA_ARGS__ )
307 #define DBGP_HDA( ... ) DBG_HDA_IF ( PROFILE, __VA_ARGS__ )
308 #define DBGP_HD( ... ) DBG_HD_IF ( PROFILE, __VA_ARGS__ )
309 #define DBGCP( ... ) DBGC_IF ( PROFILE, __VA_ARGS__ )
310 #define DBGCP_HDA( ... ) DBGC_HDA_IF ( PROFILE, __VA_ARGS__ )
311 #define DBGCP_HD( ... ) DBGC_HD_IF ( PROFILE, __VA_ARGS__ )
313 /* Versions of the DBGxxx_IF() macros that imply DBGxxx_IF( IO, ... )*/
315 #define DBGIO( ... ) DBG_IF ( IO, __VA_ARGS__ )
316 #define DBGIO_HDA( ... ) DBG_HDA_IF ( IO, __VA_ARGS__ )
317 #define DBGIO_HD( ... ) DBG_HD_IF ( IO, __VA_ARGS__ )
318 #define DBGCIO( ... ) DBGC_IF ( IO, __VA_ARGS__ )
319 #define DBGCIO_HDA( ... ) DBGC_HDA_IF ( IO, __VA_ARGS__ )
320 #define DBGCIO_HD( ... ) DBGC_HD_IF ( IO, __VA_ARGS__ )
323 #if DEBUG_SYMBOL == 0
327 #endif /* ASSEMBLY */
330 /** @defgroup attrs Miscellaneous attributes
335 /** Declare a data structure as packed. */
336 #define PACKED __attribute__ (( packed ))
338 /** Declare a variable or data structure as unused. */
339 #define __unused __attribute__ (( unused ))
342 * Declare a function as pure - i.e. without side effects
344 #define __pure __attribute__ (( pure ))
347 * Declare a function as const - i.e. it does not access global memory
348 * (including dereferencing pointers passed to it) at all.
349 * Must also not call any non-const functions.
351 #define __const __attribute__ (( const ))
354 * Declare a function's pointer parameters as non-null - i.e. force
355 * compiler to check pointers at compile time and enable possible
356 * optimizations based on that fact
358 #define __nonnull __attribute__ (( nonnull ))
361 * Declare a pointer returned by a function as a unique memory address
362 * as returned by malloc-type functions.
364 #define __malloc __attribute__ (( malloc ))
367 * Declare a function as used.
369 * Necessary only if the function is called only from assembler code.
371 #define __used __attribute__ (( used ))
373 /** Declare a data structure to be aligned with 16-byte alignment */
374 #define __aligned __attribute__ (( aligned ( 16 ) ))
376 /** Declare a function to be always inline */
377 #define __always_inline __attribute__ (( always_inline ))
382 * To save space in the binary when multiple-driver images are
383 * compiled, uninitialised data areas can be shared between drivers.
384 * This will typically be used to share statically-allocated receive
385 * and transmit buffers between drivers.
392 * char rx_buf[NUM_RX_BUF][RX_BUF_SIZE];
393 * char tx_buf[TX_BUF_SIZE];
394 * } my_static_data __shared;
399 #define __shared __asm__ ( "_shared_bss" ) __aligned
401 #endif /* ASSEMBLY */
405 * Optimisation barrier
408 #define barrier() __asm__ __volatile__ ( "" : : : "memory" )
409 #endif /* ASSEMBLY */
412 * @defgroup licences Licence declarations
414 * For reasons that are partly historical, various different files
415 * within the gPXE codebase have differing licences.
420 /** Declare a file as being in the public domain
422 * This licence declaration is applicable when a file states itself to
423 * be in the public domain.
425 #define FILE_LICENCE_PUBLIC_DOMAIN \
426 PROVIDE_SYMBOL ( __licence_public_domain )
428 /** Declare a file as being under version 2 (or later) of the GNU GPL
430 * This licence declaration is applicable when a file states itself to
431 * be licensed under the GNU GPL; "either version 2 of the License, or
432 * (at your option) any later version".
434 #define FILE_LICENCE_GPL2_OR_LATER \
435 PROVIDE_SYMBOL ( __licence_gpl2_or_later )
437 /** Declare a file as being under version 2 of the GNU GPL
439 * This licence declaration is applicable when a file states itself to
440 * be licensed under version 2 of the GPL, and does not include the
441 * "or, at your option, any later version" clause.
443 #define FILE_LICENCE_GPL2_ONLY \
444 PROVIDE_SYMBOL ( __licence_gpl2_only )
446 /** Declare a file as being under any version of the GNU GPL
448 * This licence declaration is applicable when a file states itself to
449 * be licensed under the GPL, but does not specify a version.
451 * According to section 9 of the GPLv2, "If the Program does not
452 * specify a version number of this License, you may choose any
453 * version ever published by the Free Software Foundation".
455 #define FILE_LICENCE_GPL_ANY \
456 PROVIDE_SYMBOL ( __licence_gpl_any )
458 /** Declare a file as being under the three-clause BSD licence
460 * This licence declaration is applicable when a file states itself to
461 * be licensed under terms allowing redistribution in source and
462 * binary forms (with or without modification) provided that:
464 * redistributions of source code retain the copyright notice,
465 * list of conditions and any attached disclaimers
467 * redistributions in binary form reproduce the copyright notice,
468 * list of conditions and any attached disclaimers in the
469 * documentation and/or other materials provided with the
472 * the name of the author is not used to endorse or promote
473 * products derived from the software without specific prior
476 * It is not necessary for the file to explicitly state that it is
477 * under a "BSD" licence; only that the licensing terms be
478 * functionally equivalent to the standard three-clause BSD licence.
480 #define FILE_LICENCE_BSD3 \
481 PROVIDE_SYMBOL ( __licence_bsd3 )
483 /** Declare a file as being under the two-clause BSD licence
485 * This licence declaration is applicable when a file states itself to
486 * be licensed under terms allowing redistribution in source and
487 * binary forms (with or without modification) provided that:
489 * redistributions of source code retain the copyright notice,
490 * list of conditions and any attached disclaimers
492 * redistributions in binary form reproduce the copyright notice,
493 * list of conditions and any attached disclaimers in the
494 * documentation and/or other materials provided with the
497 * It is not necessary for the file to explicitly state that it is
498 * under a "BSD" licence; only that the licensing terms be
499 * functionally equivalent to the standard two-clause BSD licence.
501 #define FILE_LICENCE_BSD2 \
502 PROVIDE_SYMBOL ( __licence_bsd2 )
504 /** Declare a file as being under the one-clause MIT-style licence
506 * This licence declaration is applicable when a file states itself to
507 * be licensed under terms allowing redistribution for any purpose
508 * with or without fee, provided that the copyright notice and
509 * permission notice appear in all copies.
511 #define FILE_LICENCE_MIT \
512 PROVIDE_SYMBOL ( __licence_mit )
514 /** Declare a particular licence as applying to a file */
515 #define FILE_LICENCE( _licence ) FILE_LICENCE_ ## _licence
519 /* This file itself is under GPLv2-or-later */
520 FILE_LICENCE ( GPL2_OR_LATER );
522 #include <bits/compiler.h>
524 #endif /* COMPILER_H */