main.c 2.0 KB

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