main.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* init/main.c
  2. *
  3. * This file is part of evx.
  4. *
  5. * evx is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * evx is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with evx. If not, see <https://www.gnu.org/licenses/>
  17. */
  18. #include <kernel/init.h>
  19. #include <kernel/module.h>
  20. #include <kernel/kernel.h>
  21. #include <kernel/kmlog.h>
  22. #include <kernel/mm.h>
  23. #include <arch/archinfo.h>
  24. #include <arch/asm.h>
  25. #include <arch/mm.h>
  26. #include <sys/types.h>
  27. int init_state;
  28. static int do_init_calls(void);
  29. int __init init(void) {
  30. init_state = STATE_INIT_CALLS;
  31. do_init_calls();
  32. init_state = STATE_READY;
  33. return 0;
  34. }
  35. void *__init_data init_call_levels[] = {
  36. &_init_call_arch, &_einit_call_arch,
  37. &_init_call_kernel, &_einit_call_kernel,
  38. &_init_call_modules, &_einit_call_modules
  39. };
  40. /*
  41. * call hooks in those sections those defined with init_call_levels.
  42. *
  43. * that when you use a module_init() macro, you add a new entry into
  44. * that section. when bootstrap, we call them with no argument. if one
  45. * of them returns a non-zero, kernel will halt and do nothing.
  46. * */
  47. typedef int (* __initcall_t) (void);
  48. static int do_init_calls(void) {
  49. int error;
  50. int i;
  51. __archptr_t *start, *end;
  52. __archptr_t fn, *initcalls;
  53. for (i = 0; i < ARRAYSIZE(init_call_levels); i++) {
  54. start = init_call_levels[i];
  55. end = init_call_levels[++i];
  56. int ncalls = end - start;
  57. for (initcalls = start; ncalls; initcalls++, ncalls--) {
  58. fn = *initcalls;
  59. if (!fn)
  60. break;
  61. if ((error = ((__initcall_t) fn)()))
  62. return error;
  63. }
  64. }
  65. return 0;
  66. }