123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- /*
- * Copyright (c) 2010-2017 Richard Braun.
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- *
- * Init sections and operations.
- */
- #ifndef KERN_INIT_H
- #define KERN_INIT_H
- /*
- * These sections should contain code and data which can be discarded once
- * kernel initialization is done.
- */
- #define INIT_SECTION .init.text
- #define INIT_DATA_SECTION .init.data
- /*
- * This section must only contain init operation structures, and must be
- * located inside the .init section.
- */
- #define INIT_OPS_SECTION .init.ops
- /*
- * Alignment is important to make sure initialization operations are
- * stored as a C array in the reserved init op section.
- */
- #define INIT_OP_ALIGN 64
- #ifndef __ASSEMBLER__
- #include <errno.h>
- #include <stdalign.h>
- #include <stdbool.h>
- #include <stddef.h>
- #include <kern/macros.h>
- #include <kern/slist_types.h>
- #define __init __section (QUOTE (INIT_SECTION))
- #define __initdata __section (QUOTE (INIT_DATA_SECTION))
- // Boundaries of the .init section.
- extern char _init;
- extern char _init_end;
- // Type for initialization operation functions.
- typedef int (*init_op_fn_t) (void);
- #define __initop __section(QUOTE(INIT_OPS_SECTION))
- #define INIT_OP_STATE_UNLINKED 0
- #define INIT_OP_STATE_PENDING 1
- #define INIT_OP_STATE_COMPLETE 2
- struct init_op
- {
- alignas (INIT_OP_ALIGN) struct slist_node list_node;
- struct slist_node stack_node;
- const char *name;
- init_op_fn_t fn;
- struct init_op_dep *deps;
- int error;
- unsigned char state;
- unsigned char nr_deps;
- unsigned char nr_parents;
- };
- struct init_op_dep
- {
- struct init_op *op;
- bool required;
- };
- #define INIT_OP_DEPS(fn) CONCAT (fn, _init_op_deps)
- #define INIT_OP(fn) CONCAT (fn, _init_op)
- // Forge an init operation declaration.
- #define INIT_OP_DECLARE(fn) extern struct init_op INIT_OP (fn)
- /*
- * Foge an entry suitable as an init operation dependency.
- *
- * If a dependency isn't required, it's still used to determine run
- * order, but its result is ignored, and the operation depending on it
- * behaves as if that dependency succeeded.
- */
- #define INIT_OP_DEP(fn, required) { &INIT_OP (fn), required }
- /*
- * Init operation definition macro.
- *
- * This macro is used to define a structure named after the given function.
- * Init operations are placed in a specific section which doesn't contain
- * any other object type, making it a system-wide array of init operations.
- * There is no need to actively register init operations; this module finds
- * them all from their section. Dependencies are given as a variable-length
- * argument list of entries built with the INIT_OP_DEP() macro.
- */
- #define INIT_OP_DEFINE(_fn, ...) \
- static struct init_op_dep INIT_OP_DEPS(_fn)[] __initdata = \
- { \
- __VA_ARGS__ \
- }; \
- \
- struct init_op INIT_OP(_fn) __initop __used = \
- { \
- .name = QUOTE (_fn), \
- .fn = _fn, \
- .deps = INIT_OP_DEPS (_fn), \
- .error = EAGAIN, \
- .state = INIT_OP_STATE_UNLINKED, \
- .nr_deps = ARRAY_SIZE (INIT_OP_DEPS (_fn)), \
- .nr_parents = 0, \
- }
- /*
- * Initialize the init module.
- *
- * Scan the section containing init operations, resolve all dependencies,
- * and run operations in an appropriate order.
- */
- void init_setup (void);
- #endif // __ASSEMBLER__
- #endif
|