forked from yhzhang0128/egos-2000
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.h
130 lines (116 loc) · 4.75 KB
/
init.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* Copyright 2019 SiFive Inc. */
/* SPDX-License-Identifier: Apache-2.0 */
#ifndef METAL_INIT
#define METAL_INIT
/*!
* @file init.h
* API for Metal constructors and destructors
*/
typedef void (*metal_constructor_t)(void);
typedef void (*metal_destructor_t)(void);
#define METAL_INIT_HIGHEST_PRIORITY 0
#define METAL_INIT_DEFAULT_PRIORITY 5000
#define METAL_INIT_LOWEST_PRIORITY 9999
/*! @def METAL_CONSTRUCTOR
* @brief Define a Metal constructor
*
* Functions defined with METAL_CONSTRUCTOR will be added to the list of
* Metal constructors. By default, these functions are called before main by
* the metal_init() function.
*/
#define METAL_CONSTRUCTOR(function_name) \
METAL_CONSTRUCTOR_PRIO(function_name, METAL_INIT_DEFAULT_PRIORITY)
/*! @def METAL_CONSTRUCTOR_PRIO
* @brief Define a Metal constructor with a given priority
*
* The priority argument should be an integer between 0 and 9999, where 0
* is the highest priority (runs first) and 9999 is the lowest priority
* (runs last).
*
* Functions defined with METAL_CONSTRUCTOR_PRIO will be added to the list of
* Metal constructors. By default, these functions are called before main by
* the metal_init() function.
*/
#define METAL_CONSTRUCTOR_PRIO(function_name, priority) \
__METAL_CONSTRUCTOR_PRIO(function_name, priority)
/* We use this wrapper for METAL_CONSTRUCTOR_PRIORITY so that macros passed
* as 'priority' are expanded before being stringified by the # operator.
* If we don't do this, then
* METAL_CONSTRUCTOR(my_fn_name, METAL_INIT_DEFAULT_PRIORITY)
* results in .metal.init_array.METAL_INIT_DEFAULT_PRIORITY instead of
* .metal.init_array.5000 */
#define __METAL_CONSTRUCTOR_PRIO(function_name, priority) \
__attribute__((section(".metal.ctors"))) void function_name(void); \
__attribute__((section(".metal.init_array." #priority))) \
metal_constructor_t _##function_name##_ptr = &function_name; \
void function_name(void)
/*! @def METAL_DESTRUCTOR
* @brief Define a Metal destructor
*
* Functions defined with METAL_DESTRUCTOR will be added to the list of
* Metal destructors. By default, these functions are called on exit by
* the metal_fini() function.
*/
#define METAL_DESTRUCTOR(function_name) \
METAL_DESTRUCTOR_PRIO(function_name, METAL_INIT_DEFAULT_PRIORITY)
/*! @def METAL_DESTRUCTOR_PRIO
* @brief Define a Metal destructor with a given priority
*
* The priority argument should be an integer between 0 and 9999, where 0
* is the highest priority (runs first) and 9999 is the lowest priority
* (runs last).
*
* Functions defined with METAL_DESTRUCTOR_PRIO will be added to the list of
* Metal destructors. By default, these functions are called on exit by
* the metal_fini() function.
*/
#define METAL_DESTRUCTOR_PRIO(function_name, priority) \
__METAL_DESTRUCTOR_PRIO(function_name, priority)
#define __METAL_DESTRUCTOR_PRIO(function_name, priority) \
__attribute__((section(".metal.dtors"))) void function_name(void); \
__attribute__((section(".metal.fini_array." #priority))) \
metal_destructor_t _##function_name##_ptr = &function_name; \
void function_name(void)
/*!
* @brief Call all Metal constructors
*
* Devices supported by Metal may define Metal constructors to perform
* initialization before main. This function iterates over the constructors
* and calls them in turn.
*
* You can add your own constructors to the functions called by metal_init()
* by defining functions with the METAL_CONSTRUCTOR() macro.
*
* This function is called before main by default by metal_init_run().
*/
void metal_init(void);
/*!
* @brief Call all Metal destructors
*
* Devices supported by Metal may define Metal destructors to perform
* initialization on exit. This function iterates over the destructors
* and calls them in turn.
*
* You can add your own destructors to the functions called by metal_fini()
* by defining functions with the METAL_DESTRUCTOR() macro.
*
* This function is called on exit by default by metal_fini_run().
*/
void metal_fini(void);
/*!
* @brief Weak function to call metal_init() before main
*
* This function calls metal_init() before main by default. If you wish to
* replace or augment this call to the Metal constructors, you can redefine
* metal_init_run()
*/
void metal_init_run(void);
/*!
* @brief Weak function to call metal_fini() before main
*
* This function calls metal_fini() at exit by default. If you wish to
* replace or augment this call to the Metal destructors, you can redefine
* metal_fini_run()
*/
void metal_fini_run(void);
#endif /* METAL_INIT */