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)
45 /** Concatenate non-expanded arguments */
46 #define _C1( x, y ) x ## y
47 /** Concatenate expanded arguments */
48 #define _C2( x, y ) _C1 ( x, y )
50 /** Stringify non-expanded argument */
52 /** Stringify expanded argument */
53 #define _S2( x ) _S1 ( x )
56 * @defgroup symmacros Macros to provide or require explicit symbols
60 /** Provide a symbol within this object file */
62 #define PROVIDE_SYMBOL( _sym ) \
66 #define PROVIDE_SYMBOL( _sym ) \
70 /** Require a symbol within this object file
72 * The symbol is referenced by a relocation in a discarded section, so
73 * if it is not available at link time the link will fail.
76 #define REQUIRE_SYMBOL( _sym ) \
77 .section ".discard", "a", @progbits ; \
82 #define REQUIRE_SYMBOL( _sym ) \
84 static char * _C2 ( _C2 ( __require_, _sym ), _C2 ( _, __LINE__ ) ) \
85 __attribute__ (( section ( ".discard" ), used )) \
89 /** Request that a symbol be available at runtime
91 * The requested symbol is entered as undefined into the symbol table
92 * for this object, so the linker will pull in other object files as
93 * necessary to satisfy the reference. However, the undefined symbol
94 * is not referenced in any relocations, so the link can still succeed
95 * if no file contains it.
97 * A symbol passed to this macro may not be referenced anywhere
98 * else in the file. If you want to do that, see IMPORT_SYMBOL().
101 #define REQUEST_SYMBOL( _sym ) \
102 .equ __need_ ## _sym, _sym
104 #define REQUEST_SYMBOL( _sym ) \
105 __asm__ ( ".equ\t__need_" #_sym ", " #_sym )
106 #endif /* ASSEMBLY */
108 /** Set up a symbol to be usable in another file by IMPORT_SYMBOL()
110 * The symbol must already be marked as global.
112 #define EXPORT_SYMBOL( _sym ) PROVIDE_SYMBOL ( __export_ ## _sym )
114 /** Make a symbol usable to this file if available at link time
116 * If no file passed to the linker contains the symbol, it will have
117 * @c NULL value to future uses. Keep in mind that the symbol value is
118 * really the @e address of a variable or function; see the code
121 * In C using IMPORT_SYMBOL, you must specify the declaration as the
122 * second argument, for instance
125 * IMPORT_SYMBOL ( my_func, int my_func ( int arg ) );
126 * IMPORT_SYMBOL ( my_var, int my_var );
128 * void use_imports ( void ) {
129 * if ( my_func && &my_var )
130 * my_var = my_func ( my_var );
134 * GCC considers a weak declaration to override a strong one no matter
135 * which comes first, so it is safe to include a header file declaring
136 * the imported symbol normally, but providing the declaration to
137 * IMPORT_SYMBOL is still required.
139 * If no EXPORT_SYMBOL declaration exists for the imported symbol in
140 * another file, the behavior will be most likely be identical to that
141 * for an unavailable symbol.
144 #define IMPORT_SYMBOL( _sym ) \
145 REQUEST_SYMBOL ( __export_ ## _sym ) ; \
148 #define IMPORT_SYMBOL( _sym, _decl ) \
149 REQUEST_SYMBOL ( __export_ ## _sym ) ; \
150 extern _decl __attribute__ (( weak ))
156 * @defgroup objmacros Macros to provide or require explicit objects
160 #define PREFIX_OBJECT( _prefix ) _C2 ( _prefix, OBJECT )
161 #define OBJECT_SYMBOL PREFIX_OBJECT ( obj_ )
162 #define REQUEST_EXPANDED( _sym ) REQUEST_SYMBOL ( _sym )
163 #define CONFIG_SYMBOL PREFIX_OBJECT ( obj_config_ )
165 /** Always provide the symbol for the current object (defined by -DOBJECT) */
166 PROVIDE_SYMBOL ( OBJECT_SYMBOL
);
168 /** Pull in an object-specific configuration file if available */
169 REQUEST_EXPANDED ( CONFIG_SYMBOL
);
171 /** Explicitly require another object */
172 #define REQUIRE_OBJECT( _obj ) REQUIRE_SYMBOL ( obj_ ## _obj )
174 /** Pull in another object if it exists */
175 #define REQUEST_OBJECT( _obj ) REQUEST_SYMBOL ( obj_ ## _obj )
179 /** Select file identifier for errno.h (if used) */
180 #define ERRFILE PREFIX_OBJECT ( ERRFILE_ )
184 /** Declare a function as weak (use *before* the definition)
186 * Due to a bug in at least GCC 4.4.4 and earlier, weak symbols may be inlined
187 * if they have hidden visibility (see above for why hidden visibility is
188 * used). This results in the non-weak symbol never being used, so explicitly
189 * mark the function as noinline to prevent inlining.
191 #define __weak __attribute__ (( weak, noinline ))
193 /** Prevent a function from being optimized away without inlining
195 * Calls to functions with void return type that contain no code in their body
196 * may be removed by gcc's optimizer even when inlining is inhibited. Placing
197 * this macro in the body of the function prevents that from occurring.
199 #define __keepme asm("");
203 /** @defgroup dbg Debugging infrastructure
210 * Print a debugging message.
212 * The debug level is set at build time by specifying the @c DEBUG=
213 * parameter on the @c make command line. For example, to enable
214 * debugging for the PCI bus functions (in pci.c) in a @c .dsk image
215 * for the @c rtl8139 card, you could use the command line
219 * make bin/rtl8139.dsk DEBUG=pci
223 * This will enable the debugging statements (DBG()) in pci.c. If
224 * debugging is not enabled, DBG() statements will be ignored.
226 * You can enable debugging in several objects simultaneously by
227 * separating them with commas, as in
231 * make bin/rtl8139.dsk DEBUG=pci,buffer,heap
235 * You can increase the debugging level for an object by specifying it
236 * with @c :N, where @c N is the level, as in
240 * make bin/rtl8139.dsk DEBUG=pci,buffer:2,heap
244 * which would enable debugging for the PCI, buffer-handling and
245 * heap-allocation code, with the buffer-handling code at level 2.
250 * If debug_OBJECT is set to a true value, the macro DBG(...) will
251 * expand to printf(...) when compiling OBJECT, and the symbol
252 * DEBUG_LEVEL will be inserted into the object file.
255 #define DEBUG_SYMBOL PREFIX_OBJECT ( debug_ )
257 /** printf() for debugging
259 * This function exists so that the DBG() macros can expand to
260 * printf() calls without dragging the printf() prototype into scope.
262 * As far as the compiler is concerned, dbg_printf() and printf() are
263 * completely unrelated calls; it's only at the assembly stage that
264 * references to the dbg_printf symbol are collapsed into references
265 * to the printf symbol.
267 extern int __attribute__ (( format ( printf
, 1, 2 ) ))
268 dbg_printf ( const char *fmt
, ... ) asm ( "printf" );
270 extern void dbg_autocolourise ( unsigned long id
);
271 extern void dbg_decolourise ( void );
272 extern void dbg_hex_dump_da ( unsigned long dispaddr
,
273 const void *data
, unsigned long len
);
276 #define DBGLVL_MAX DEBUG_SYMBOL
281 /* Allow for selective disabling of enabled debug levels */
284 #define DBGLVL ( DBGLVL_MAX & ~__debug_disable )
285 #define DBG_DISABLE( level ) do { \
286 __debug_disable |= ( (level) & DBGLVL_MAX ); \
288 #define DBG_ENABLE( level ) do { \
289 __debug_disable &= ~( (level) & DBGLVL_MAX ); \
293 #define DBG_DISABLE( level ) do { } while ( 0 )
294 #define DBG_ENABLE( level ) do { } while ( 0 )
298 #define DBG_LOG ( DBGLVL & DBGLVL_LOG )
299 #define DBGLVL_EXTRA 2
300 #define DBG_EXTRA ( DBGLVL & DBGLVL_EXTRA )
301 #define DBGLVL_PROFILE 4
302 #define DBG_PROFILE ( DBGLVL & DBGLVL_PROFILE )
304 #define DBG_IO ( DBGLVL & DBGLVL_IO )
307 * Print debugging message if we are at a certain debug level
309 * @v level Debug level
310 * @v ... printf() argument list
312 #define DBG_IF( level, ... ) do { \
313 if ( DBG_ ## level ) { \
314 dbg_printf ( __VA_ARGS__ ); \
319 * Print a hex dump if we are at a certain debug level
321 * @v level Debug level
322 * @v dispaddr Display address
323 * @v data Data to print
324 * @v len Length of data
326 #define DBG_HDA_IF( level, dispaddr, data, len ) do { \
327 if ( DBG_ ## level ) { \
330 typeof ( dispaddr ) raw; \
333 dbg_hex_dump_da ( da.ul, data, len ); \
338 * Print a hex dump if we are at a certain debug level
340 * @v level Debug level
341 * @v data Data to print
342 * @v len Length of data
344 #define DBG_HD_IF( level, data, len ) do { \
345 const void *_data = data; \
346 DBG_HDA_IF ( level, _data, _data, len ); \
350 * Select colour for debug messages if we are at a certain debug level
352 * @v level Debug level
353 * @v id Message stream ID
355 #define DBG_AC_IF( level, id ) do { \
356 if ( DBG_ ## level ) { \
361 dbg_stream.raw = id; \
362 dbg_autocolourise ( dbg_stream.ul ); \
367 * Revert colour for debug messages if we are at a certain debug level
369 * @v level Debug level
371 #define DBG_DC_IF( level ) do { \
372 if ( DBG_ ## level ) { \
377 /* Autocolourising versions of the DBGxxx_IF() macros */
379 #define DBGC_IF( level, id, ... ) do { \
380 DBG_AC_IF ( level, id ); \
381 DBG_IF ( level, __VA_ARGS__ ); \
382 DBG_DC_IF ( level ); \
385 #define DBGC_HDA_IF( level, id, ... ) do { \
386 DBG_AC_IF ( level, id ); \
387 DBG_HDA_IF ( level, __VA_ARGS__ ); \
388 DBG_DC_IF ( level ); \
391 #define DBGC_HD_IF( level, id, ... ) do { \
392 DBG_AC_IF ( level, id ); \
393 DBG_HD_IF ( level, __VA_ARGS__ ); \
394 DBG_DC_IF ( level ); \
397 /* Versions of the DBGxxx_IF() macros that imply DBGxxx_IF( LOG, ... )*/
399 #define DBG( ... ) DBG_IF ( LOG, __VA_ARGS__ )
400 #define DBG_HDA( ... ) DBG_HDA_IF ( LOG, __VA_ARGS__ )
401 #define DBG_HD( ... ) DBG_HD_IF ( LOG, __VA_ARGS__ )
402 #define DBGC( ... ) DBGC_IF ( LOG, __VA_ARGS__ )
403 #define DBGC_HDA( ... ) DBGC_HDA_IF ( LOG, __VA_ARGS__ )
404 #define DBGC_HD( ... ) DBGC_HD_IF ( LOG, __VA_ARGS__ )
406 /* Versions of the DBGxxx_IF() macros that imply DBGxxx_IF( EXTRA, ... )*/
408 #define DBG2( ... ) DBG_IF ( EXTRA, __VA_ARGS__ )
409 #define DBG2_HDA( ... ) DBG_HDA_IF ( EXTRA, __VA_ARGS__ )
410 #define DBG2_HD( ... ) DBG_HD_IF ( EXTRA, __VA_ARGS__ )
411 #define DBGC2( ... ) DBGC_IF ( EXTRA, __VA_ARGS__ )
412 #define DBGC2_HDA( ... ) DBGC_HDA_IF ( EXTRA, __VA_ARGS__ )
413 #define DBGC2_HD( ... ) DBGC_HD_IF ( EXTRA, __VA_ARGS__ )
415 /* Versions of the DBGxxx_IF() macros that imply DBGxxx_IF( PROFILE, ... )*/
417 #define DBGP( ... ) DBG_IF ( PROFILE, __VA_ARGS__ )
418 #define DBGP_HDA( ... ) DBG_HDA_IF ( PROFILE, __VA_ARGS__ )
419 #define DBGP_HD( ... ) DBG_HD_IF ( PROFILE, __VA_ARGS__ )
420 #define DBGCP( ... ) DBGC_IF ( PROFILE, __VA_ARGS__ )
421 #define DBGCP_HDA( ... ) DBGC_HDA_IF ( PROFILE, __VA_ARGS__ )
422 #define DBGCP_HD( ... ) DBGC_HD_IF ( PROFILE, __VA_ARGS__ )
424 /* Versions of the DBGxxx_IF() macros that imply DBGxxx_IF( IO, ... )*/
426 #define DBGIO( ... ) DBG_IF ( IO, __VA_ARGS__ )
427 #define DBGIO_HDA( ... ) DBG_HDA_IF ( IO, __VA_ARGS__ )
428 #define DBGIO_HD( ... ) DBG_HD_IF ( IO, __VA_ARGS__ )
429 #define DBGCIO( ... ) DBGC_IF ( IO, __VA_ARGS__ )
430 #define DBGCIO_HDA( ... ) DBGC_HDA_IF ( IO, __VA_ARGS__ )
431 #define DBGCIO_HD( ... ) DBGC_HD_IF ( IO, __VA_ARGS__ )
434 #if DEBUG_SYMBOL == 0
438 #endif /* ASSEMBLY */
441 /** @defgroup attrs Miscellaneous attributes
446 /** Declare a variable or data structure as unused. */
447 #define __unused __attribute__ (( unused ))
450 * Declare a function as pure - i.e. without side effects
452 #define __pure __attribute__ (( pure ))
455 * Declare a function as const - i.e. it does not access global memory
456 * (including dereferencing pointers passed to it) at all.
457 * Must also not call any non-const functions.
459 #define __const __attribute__ (( const ))
462 * Declare a function's pointer parameters as non-null - i.e. force
463 * compiler to check pointers at compile time and enable possible
464 * optimizations based on that fact
466 #define __nonnull __attribute__ (( nonnull ))
469 * Declare a pointer returned by a function as a unique memory address
470 * as returned by malloc-type functions.
472 #define __malloc __attribute__ (( malloc ))
475 * Declare a function as used.
477 * Necessary only if the function is called only from assembler code.
479 #define __used __attribute__ (( used ))
481 /** Declare a data structure to be aligned with 16-byte alignment */
482 #define __aligned __attribute__ (( aligned ( 16 ) ))
484 /** Declare a function to be always inline */
485 #define __always_inline __attribute__ (( always_inline ))
490 * To save space in the binary when multiple-driver images are
491 * compiled, uninitialised data areas can be shared between drivers.
492 * This will typically be used to share statically-allocated receive
493 * and transmit buffers between drivers.
500 * char rx_buf[NUM_RX_BUF][RX_BUF_SIZE];
501 * char tx_buf[TX_BUF_SIZE];
502 * } my_static_data __shared;
507 #define __shared __asm__ ( "_shared_bss" ) __aligned
509 #endif /* ASSEMBLY */
513 * Optimisation barrier
516 #define barrier() __asm__ __volatile__ ( "" : : : "memory" )
517 #endif /* ASSEMBLY */
520 * @defgroup licences Licence declarations
522 * For reasons that are partly historical, various different files
523 * within the gPXE codebase have differing licences.
528 /** Declare a file as being in the public domain
530 * This licence declaration is applicable when a file states itself to
531 * be in the public domain.
533 #define FILE_LICENCE_PUBLIC_DOMAIN \
534 PROVIDE_SYMBOL ( __licence_public_domain )
536 /** Declare a file as being under version 2 (or later) of the GNU GPL
538 * This licence declaration is applicable when a file states itself to
539 * be licensed under the GNU GPL; "either version 2 of the License, or
540 * (at your option) any later version".
542 #define FILE_LICENCE_GPL2_OR_LATER \
543 PROVIDE_SYMBOL ( __licence_gpl2_or_later )
545 /** Declare a file as being under version 2 of the GNU GPL
547 * This licence declaration is applicable when a file states itself to
548 * be licensed under version 2 of the GPL, and does not include the
549 * "or, at your option, any later version" clause.
551 #define FILE_LICENCE_GPL2_ONLY \
552 PROVIDE_SYMBOL ( __licence_gpl2_only )
554 /** Declare a file as being under any version of the GNU GPL
556 * This licence declaration is applicable when a file states itself to
557 * be licensed under the GPL, but does not specify a version.
559 * According to section 9 of the GPLv2, "If the Program does not
560 * specify a version number of this License, you may choose any
561 * version ever published by the Free Software Foundation".
563 #define FILE_LICENCE_GPL_ANY \
564 PROVIDE_SYMBOL ( __licence_gpl_any )
566 /** Declare a file as being under the three-clause BSD licence
568 * This licence declaration is applicable when a file states itself to
569 * be licensed under terms allowing redistribution in source and
570 * binary forms (with or without modification) provided that:
572 * redistributions of source code retain the copyright notice,
573 * list of conditions and any attached disclaimers
575 * redistributions in binary form reproduce the copyright notice,
576 * list of conditions and any attached disclaimers in the
577 * documentation and/or other materials provided with the
580 * the name of the author is not used to endorse or promote
581 * products derived from the software without specific prior
584 * It is not necessary for the file to explicitly state that it is
585 * under a "BSD" licence; only that the licensing terms be
586 * functionally equivalent to the standard three-clause BSD licence.
588 #define FILE_LICENCE_BSD3 \
589 PROVIDE_SYMBOL ( __licence_bsd3 )
591 /** Declare a file as being under the two-clause BSD licence
593 * This licence declaration is applicable when a file states itself to
594 * be licensed under terms allowing redistribution in source and
595 * binary forms (with or without modification) provided that:
597 * redistributions of source code retain the copyright notice,
598 * list of conditions and any attached disclaimers
600 * redistributions in binary form reproduce the copyright notice,
601 * list of conditions and any attached disclaimers in the
602 * documentation and/or other materials provided with the
605 * It is not necessary for the file to explicitly state that it is
606 * under a "BSD" licence; only that the licensing terms be
607 * functionally equivalent to the standard two-clause BSD licence.
609 #define FILE_LICENCE_BSD2 \
610 PROVIDE_SYMBOL ( __licence_bsd2 )
612 /** Declare a file as being under the one-clause MIT-style licence
614 * This licence declaration is applicable when a file states itself to
615 * be licensed under terms allowing redistribution for any purpose
616 * with or without fee, provided that the copyright notice and
617 * permission notice appear in all copies.
619 #define FILE_LICENCE_MIT \
620 PROVIDE_SYMBOL ( __licence_mit )
622 /** Declare a particular licence as applying to a file */
623 #define FILE_LICENCE( _licence ) FILE_LICENCE_ ## _licence
627 /* This file itself is under GPLv2-or-later */
628 FILE_LICENCE ( GPL2_OR_LATER
);
630 #include <bits/compiler.h>
632 #endif /* COMPILER_H */