main.c 2.2 KB

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