main.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. static int do_init_calls(void);
  26. int __init init(void) {
  27. do_init_calls();
  28. kmlog("first usable page is the %d'th page. \n", first_usable_ram - kmem_pages);
  29. return 0;
  30. }
  31. void *__init_data init_call_levels[] = {
  32. &_init_call_arch, &_einit_call_arch,
  33. &_init_call_kernel, &_einit_call_kernel,
  34. &_init_call_modules, &_einit_call_modules
  35. };
  36. /*
  37. * call hooks in those sections those defined with init_call_levels.
  38. *
  39. * that when you use a module_init() macro, you add a new entry into
  40. * that section. when bootstrap, we call them with no argument. if one
  41. * of them returns a non-zero, kernel will halt and do nothing.
  42. * */
  43. typedef int (* __initcall_t) (void);
  44. static int do_init_calls(void) {
  45. int error;
  46. int i;
  47. __archptr_t *start, *end;
  48. __archptr_t fn, *initcalls;
  49. for (i = 0; i < ARRAYSIZE(init_call_levels); i++) {
  50. start = init_call_levels[i];
  51. end = init_call_levels[++i];
  52. int ncalls = end - start;
  53. for (initcalls = start; ncalls; initcalls++, ncalls--) {
  54. fn = *initcalls;
  55. if (!fn)
  56. break;
  57. if ((error = ((__initcall_t) fn)()))
  58. return error;
  59. }
  60. }
  61. return 0;
  62. }