1 /* 2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. 3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without modification, 6 * are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this list of 9 * conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list 12 * of conditions and the following disclaimer in the documentation and/or other materials 13 * provided with the distribution. 14 * 15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used 16 * to endorse or promote products derived from this software without specific prior written 17 * permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #ifndef _LOS_INIT_INFO_H 33 #define _LOS_INIT_INFO_H 34 35 #include "stdalign.h" 36 #include "los_toolchain.h" 37 #include "los_typedef.h" 38 39 /** 40 * @ingroup los_init_info 41 * Macro LOS_INIT_DEBUG needs to be defined here, used to debug the init framework. 42 * #define LOS_INIT_DEBUG 43 */ 44 45 /** 46 * @ingroup los_init_info 47 * Macro LOS_INIT_STATISTICS needs to be defined here, used to count the kernel startup time. 48 * @attention 49 * <ul> 50 * <li> if uses, the macro LOS_INIT_DEBUG must be undefined. </li> 51 * </ul> 52 * #define LOS_INIT_STATISTICS 53 */ 54 55 #if defined(LOS_INIT_STATISTICS) && defined(LOS_INIT_DEBUG) 56 #error "LOS_INIT_STATISTICS needs LOS_INIT_DEBUG to be undefined" 57 #endif 58 59 #define INIT_SECTION(_type, _level, _hook) __attribute__((section(".rodata.init."#_type"."#_level"."#_hook))) 60 #define INIT_ALIGN __attribute__((aligned(alignof(struct ModuleInitInfo)))) 61 62 typedef UINT32 (*OsInitHook)(VOID); 63 64 struct ModuleInitInfo { 65 OsInitHook hook; 66 #ifdef LOS_INIT_DEBUG 67 const CHAR *name; 68 #endif 69 }; 70 71 #ifdef LOS_INIT_DEBUG 72 #define SET_MODULE_NAME(_hook) .name = #_hook, 73 #else 74 #define SET_MODULE_NAME(_hook) 75 #endif 76 77 /** 78 * @ingroup los_init_info 79 * @brief Add a registration module to the specified level in a startup framework. 80 * 81 * @par Description: 82 * This API is used to add a registration module to the specified level in a startup framework. 83 * @attention 84 * <ul> 85 * <li>It is not recommended to call directly, it is recommended that each startup framework 86 * encapsulate a layer of interface in los_init.h.</li> 87 * </ul> 88 * 89 * @param _type [IN] Type name of startup framework. 90 * @param _hook [IN] Register function. 91 * @param _level [IN] At which _level do you want to register. 92 * 93 * @retval None 94 * @par Dependency: 95 * <ul><li>los_task_info.h: the header file that contains the API declaration.</li></ul> 96 * @see 97 */ 98 #define OS_INIT_HOOK_REG(_type, _hook, _level) \ 99 STATIC const struct ModuleInitInfo ModuleInitInfo_##_hook \ 100 USED INIT_SECTION(_type, _level, _hook) INIT_ALIGN = { \ 101 .hook = (UINT32 (*)(VOID))&_hook, \ 102 SET_MODULE_NAME(_hook) \ 103 }; 104 105 #define EXTERN_LABEL(_type, _level) extern struct ModuleInitInfo __##_type##_init_level_##_level; 106 #define GET_LABEL(_type, _level) &__##_type##_init_level_##_level, 107 108 #define INIT_LABEL_REG_0(_op, _type) \ 109 _op(_type, 0) 110 #define INIT_LABEL_REG_1(_op, _type) \ 111 INIT_LABEL_REG_0(_op, _type) \ 112 _op(_type, 1) 113 #define INIT_LABEL_REG_2(_op, _type) \ 114 INIT_LABEL_REG_1(_op, _type) \ 115 _op(_type, 2) 116 #define INIT_LABEL_REG_3(_op, _type) \ 117 INIT_LABEL_REG_2(_op, _type) \ 118 _op(_type, 3) 119 #define INIT_LABEL_REG_4(_op, _type) \ 120 INIT_LABEL_REG_3(_op, _type) \ 121 _op(_type, 4) 122 #define INIT_LABEL_REG_5(_op, _type) \ 123 INIT_LABEL_REG_4(_op, _type) \ 124 _op(_type, 5) 125 #define INIT_LABEL_REG_6(_op, _type) \ 126 INIT_LABEL_REG_5(_op, _type) \ 127 _op(_type, 6) 128 #define INIT_LABEL_REG_7(_op, _type) \ 129 INIT_LABEL_REG_6(_op, _type) \ 130 _op(_type, 7) 131 #define INIT_LABEL_REG_8(_op, _type) \ 132 INIT_LABEL_REG_7(_op, _type) \ 133 _op(_type, 8) 134 #define INIT_LABEL_REG_9(_op, _type) \ 135 INIT_LABEL_REG_8(_op, _type) \ 136 _op(_type, 9) 137 #define INIT_LABEL_REG_10(_op, _type) \ 138 INIT_LABEL_REG_9(_op, _type) \ 139 _op(_type, 10) 140 141 /** 142 * @ingroup los_init_info 143 * @brief Define a set of levels and initialize the labels of each level. 144 * 145 * @par Description: 146 * This API is used to define a set of levels and initialize the labels of each level. 147 * @attention 148 * <ul> 149 * <li>This interface is used to add a new startup framework.</li> 150 * <li>To use this interface, you need to add a corresponding section description in 151 * the liteos.ld and liteos_llvm.ld files to match </li> 152 * </ul> 153 * 154 * @param _type [IN] Type name of startup framework. 155 * @param _num [IN] The maximum effective level of the startup framework, the level range is [0, _num]. 156 * @param _list [IN] Static global array, used to manage labels at all levels. 157 * 158 * @retval None 159 * @par Dependency: 160 * <ul><li>los_task_info.h: the header file that contains the API declaration.</li></ul> 161 * @see 162 */ 163 #define OS_INIT_LEVEL_REG(_type, _num, _list) \ 164 INIT_LABEL_REG_##_num(EXTERN_LABEL, _type) \ 165 STATIC struct ModuleInitInfo *_list[] = { \ 166 INIT_LABEL_REG_##_num(GET_LABEL, _type) \ 167 } 168 169 #endif /* _LOS_INIT_INFO_H */ 170