init.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright (c) 2010-2017 Richard Braun.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. *
  18. * Init sections and operations.
  19. */
  20. #ifndef KERN_INIT_H
  21. #define KERN_INIT_H
  22. /*
  23. * These sections should contain code and data which can be discarded once
  24. * kernel initialization is done.
  25. */
  26. #define INIT_SECTION .init.text
  27. #define INIT_DATA_SECTION .init.data
  28. /*
  29. * This section must only contain init operation structures, and must be
  30. * located inside the .init section.
  31. */
  32. #define INIT_OPS_SECTION .init.ops
  33. /*
  34. * Alignment is important to make sure initialization operations are
  35. * stored as a C array in the reserved init op section.
  36. */
  37. #define INIT_OP_ALIGN 64
  38. #ifndef __ASSEMBLER__
  39. #include <errno.h>
  40. #include <kern/macros.h>
  41. #define __init __section(QUOTE(INIT_SECTION))
  42. #define __initdata __section(QUOTE(INIT_DATA_SECTION))
  43. /*
  44. * Boundaries of the .init section.
  45. */
  46. extern char _init;
  47. extern char _init_end;
  48. /*
  49. * Type for initialization operation functions.
  50. */
  51. typedef int (*init_op_fn_t)(void);
  52. #include <kern/init_i.h>
  53. /*
  54. * Forge an init operation declaration.
  55. */
  56. #define INIT_OP_DECLARE(fn) extern struct init_op INIT_OP(fn)
  57. /*
  58. * Foge an entry suitable as an init operation dependency.
  59. *
  60. * If a dependency isn't required, it's still used to determine run
  61. * order, but its result is ignored, and the operation depending on it
  62. * behaves as if that dependency succeeded.
  63. */
  64. #define INIT_OP_DEP(fn, required) { &INIT_OP(fn), required }
  65. /*
  66. * Init operation definition macro.
  67. *
  68. * This macro is used to define a structure named after the given function.
  69. * Init operations are placed in a specific section which doesn't contain
  70. * any other object type, making it a system-wide array of init operations.
  71. * There is no need to actively register init operations; this module finds
  72. * them all from their section. Dependencies are given as a variable-length
  73. * argument list of entries built with the INIT_OP_DEP() macro.
  74. */
  75. #define INIT_OP_DEFINE(_fn, ...) \
  76. static struct init_op_dep INIT_OP_DEPS(_fn)[] __initdata = { \
  77. __VA_ARGS__ \
  78. }; \
  79. \
  80. struct init_op INIT_OP(_fn) __initop __used = { \
  81. .name = QUOTE(_fn), \
  82. .fn = _fn, \
  83. .deps = INIT_OP_DEPS(_fn), \
  84. .error = EAGAIN, \
  85. .state = INIT_OP_STATE_UNLINKED, \
  86. .nr_deps = ARRAY_SIZE(INIT_OP_DEPS(_fn)), \
  87. .nr_parents = 0, \
  88. }
  89. /*
  90. * Initialize the init module.
  91. *
  92. * Scan the section containing init operations, resolve all dependencies,
  93. * and run operations in an appropriate order.
  94. */
  95. void init_setup(void);
  96. #endif /* __ASSEMBLER__ */
  97. #endif /* KERN_INIT_H */