main.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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/mm.h>
  22. #include <arch/archinfo.h>
  23. #include <arch/mm.h>
  24. #include <sys/types.h>
  25. int init_state;
  26. static int do_init_calls(void);
  27. int __init init(void) {
  28. struct page *p;
  29. init_state = STATE_INIT_CALLS;
  30. do_init_calls();
  31. init_state = STATE_READY;
  32. p = getfreepage(0, 0);
  33. freepage(p, 0);
  34. p = getfreepage(0, 0);
  35. freepage(p, 0);
  36. p = getfreepage(0, 0);
  37. freepage(p, 0);
  38. p = getfreepage(0, 0);
  39. kmlog("page number %d\n", page_nr(p));
  40. return 0;
  41. }
  42. void *__init_data init_call_levels[] = {
  43. &_init_call_arch, &_einit_call_arch,
  44. &_init_call_kernel, &_einit_call_kernel,
  45. &_init_call_modules, &_einit_call_modules
  46. };
  47. /*
  48. * call hooks in those sections those defined with init_call_levels.
  49. *
  50. * that when you use a module_init() macro, you add a new entry into
  51. * that section. when bootstrap, we call them with no argument. if one
  52. * of them returns a non-zero, kernel will halt and do nothing.
  53. * */
  54. typedef int (* __initcall_t) (void);
  55. static int do_init_calls(void) {
  56. int error;
  57. int i;
  58. __archptr_t *start, *end;
  59. __archptr_t fn, *initcalls;
  60. for (i = 0; i < ARRAYSIZE(init_call_levels); i++) {
  61. start = init_call_levels[i];
  62. end = init_call_levels[++i];
  63. int ncalls = end - start;
  64. for (initcalls = start; ncalls; initcalls++, ncalls--) {
  65. fn = *initcalls;
  66. if (!fn)
  67. break;
  68. if ((error = ((__initcall_t) fn)()))
  69. return error;
  70. }
  71. }
  72. return 0;
  73. }