main.c 2.3 KB

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