main.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /* vim: tabstop=4 shiftwidth=4 noexpandtab
  2. *
  3. * The ToAruOS kernel is released under the terms of the
  4. * University of Illinois / NCSA License.
  5. *
  6. * Copyright (C) 2011-2018 K. Lange. All rights reserved.
  7. * Copyright (C) 2012 Markus Schober
  8. * Copyright (C) 2014 Lioncash
  9. *
  10. * Dedicated to the memory of
  11. * Dennis Ritchie
  12. * 1941-2011
  13. *
  14. * Developed by: ToAruOS Kernel Development Team
  15. * http://gitlab.com/toaruos
  16. *
  17. * Permission is hereby granted, free of charge, to any person obtaining a copy
  18. * of this software and associated documentation files (the "Software"), to
  19. * deal with the Software without restriction, including without limitation the
  20. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  21. * sell copies of the Software, and to permit persons to whom the Software is
  22. * furnished to do so, subject to the following conditions:
  23. * 1. Redistributions of source code must retain the above copyright notice,
  24. * this list of conditions and the following disclaimers.
  25. * 2. Redistributions in binary form must reproduce the above copyright
  26. * notice, this list of conditions and the following disclaimers in the
  27. * documentation and/or other materials provided with the distribution.
  28. * 3. Neither the names of the ToAruOS Kernel Development Team, K. Lange,
  29. * nor the names of its contributors may be used to endorse
  30. * or promote products derived from this Software without specific prior
  31. * written permission.
  32. *
  33. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  34. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  35. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  36. * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  37. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  38. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  39. * WITH THE SOFTWARE.
  40. */
  41. #include <kernel/system.h>
  42. #include <kernel/boot.h>
  43. #include <kernel/ext2.h>
  44. #include <kernel/fs.h>
  45. #include <kernel/logging.h>
  46. #include <kernel/process.h>
  47. #include <kernel/shm.h>
  48. #include <kernel/args.h>
  49. #include <kernel/module.h>
  50. #include <kernel/pci.h>
  51. uintptr_t initial_esp = 0;
  52. fs_node_t * ramdisk_mount(uintptr_t, size_t);
  53. #ifdef EARLY_BOOT_LOG
  54. #define EARLY_LOG_DEVICE 0x3F8
  55. static uint32_t _early_log_write(fs_node_t *node, uint32_t offset, uint32_t size, uint8_t *buffer) {
  56. for (unsigned int i = 0; i < size; ++i) {
  57. outportb(EARLY_LOG_DEVICE, buffer[i]);
  58. }
  59. return size;
  60. }
  61. fs_node_t _early_log = { .write = &_early_log_write };
  62. #define ENABLE_EARLY_BOOT_LOG(level) do { debug_file = &_early_log; debug_level = (level); } while (0)
  63. #define DISABLE_EARLY_BOOT_LOG() do { debug_file = NULL; debug_level = NOTICE; } while (0)
  64. #else
  65. #define ENABLE_EARLY_BOOT_LOG(level)
  66. #define DISABLE_EARLY_BOOT_LOG()
  67. #endif
  68. struct pack_header {
  69. char head[4];
  70. uint32_t region_size;
  71. };
  72. /*
  73. * multiboot i386 (pc) kernel entry point
  74. */
  75. int kmain(struct multiboot *mboot, uint32_t mboot_mag, uintptr_t esp) {
  76. initial_esp = esp;
  77. extern char * cmdline;
  78. uint32_t mboot_mods_count = 0;
  79. mboot_mod_t * mboot_mods = NULL;
  80. mboot_ptr = mboot;
  81. ENABLE_EARLY_BOOT_LOG(0);
  82. assert(mboot_mag == MULTIBOOT_EAX_MAGIC && "Didn't boot with multiboot, not sure how we got here.");
  83. debug_print(NOTICE, "Processing Multiboot information.");
  84. /* Initialize core modules */
  85. gdt_install(); /* Global descriptor table */
  86. idt_install(); /* IDT */
  87. isrs_install(); /* Interrupt service requests */
  88. irq_install(); /* Hardware interrupt requests */
  89. uintptr_t last_mod = (uintptr_t)&end;
  90. if (mboot_ptr->flags & MULTIBOOT_FLAG_MODS) {
  91. debug_print(NOTICE, "There %s %d module%s starting at 0x%x.", mboot_ptr->mods_count == 1 ? "is" : "are", mboot_ptr->mods_count, mboot_ptr->mods_count == 1 ? "" : "s", mboot_ptr->mods_addr);
  92. debug_print(NOTICE, "Current kernel heap start point would be 0x%x.", &end);
  93. if (mboot_ptr->mods_count > 0) {
  94. uint32_t i;
  95. mboot_mods = (mboot_mod_t *)mboot_ptr->mods_addr;
  96. mboot_mods_count = mboot_ptr->mods_count;
  97. for (i = 0; i < mboot_ptr->mods_count; ++i ) {
  98. mboot_mod_t * mod = &mboot_mods[i];
  99. uint32_t module_start = mod->mod_start;
  100. uint32_t module_end = mod->mod_end;
  101. if ((uintptr_t)mod + sizeof(mboot_mod_t) > last_mod) {
  102. /* Just in case some silly person put this *behind* the modules... */
  103. last_mod = (uintptr_t)mod + sizeof(mboot_mod_t);
  104. debug_print(NOTICE, "moving forward to 0x%x", last_mod);
  105. }
  106. debug_print(NOTICE, "Module %d is at 0x%x:0x%x", i, module_start, module_end);
  107. if (last_mod < module_end) {
  108. last_mod = module_end;
  109. }
  110. }
  111. debug_print(NOTICE, "Moving kernel heap start to 0x%x", last_mod);
  112. }
  113. }
  114. if ((uintptr_t)mboot_ptr > last_mod) {
  115. last_mod = (uintptr_t)mboot_ptr + sizeof(struct multiboot);
  116. }
  117. while (last_mod & 0x7FF) last_mod++;
  118. kmalloc_startat(last_mod);
  119. if (mboot_ptr->flags & MULTIBOOT_FLAG_MEM) {
  120. paging_install(mboot_ptr->mem_upper + mboot_ptr->mem_lower);
  121. } else {
  122. debug_print(CRITICAL, "Missing MEM flag in multiboot header\n");
  123. }
  124. if (mboot_ptr->flags & MULTIBOOT_FLAG_MMAP) {
  125. debug_print(NOTICE, "Parsing memory map.");
  126. mboot_memmap_t * mmap = (void *)mboot_ptr->mmap_addr;
  127. while ((uintptr_t)mmap < mboot_ptr->mmap_addr + mboot_ptr->mmap_length) {
  128. if (mmap->type == 2) {
  129. for (unsigned long long int i = 0; i < mmap->length; i += 0x1000) {
  130. if (mmap->base_addr + i > 0xFFFFFFFF) break; /* xxx */
  131. debug_print(INFO, "Marking 0x%x", (uint32_t)(mmap->base_addr + i));
  132. paging_mark_system((mmap->base_addr + i) & 0xFFFFF000);
  133. }
  134. }
  135. mmap = (mboot_memmap_t *) ((uintptr_t)mmap + mmap->size + sizeof(uintptr_t));
  136. }
  137. }
  138. paging_finalize();
  139. {
  140. char cmdline_[1024];
  141. size_t len = strlen((char *)mboot_ptr->cmdline);
  142. memmove(cmdline_, (char *)mboot_ptr->cmdline, len + 1);
  143. /* Relocate the command line */
  144. cmdline = (char *)kmalloc(len + 1);
  145. memcpy(cmdline, cmdline_, len + 1);
  146. }
  147. /* Memory management */
  148. heap_install(); /* Kernel heap */
  149. if (cmdline) {
  150. args_parse(cmdline);
  151. }
  152. vfs_install();
  153. tasking_install(); /* Multi-tasking */
  154. timer_install(); /* PIC driver */
  155. fpu_install(); /* FPU/SSE magic */
  156. syscalls_install(); /* Install the system calls */
  157. shm_install(); /* Install shared memory */
  158. modules_install(); /* Modules! */
  159. pci_remap();
  160. DISABLE_EARLY_BOOT_LOG();
  161. /* Load modules from bootloader */
  162. if (mboot_ptr->flags & MULTIBOOT_FLAG_MODS) {
  163. debug_print(NOTICE, "%d modules to load", mboot_mods_count);
  164. for (unsigned int i = 0; i < mboot_ptr->mods_count; ++i ) {
  165. mboot_mod_t * mod = &mboot_mods[i];
  166. uint32_t module_start = mod->mod_start;
  167. uint32_t module_end = mod->mod_end;
  168. size_t module_size = module_end - module_start;
  169. int check_result = module_quickcheck((void *)module_start);
  170. if (check_result == 1) {
  171. debug_print(NOTICE, "Loading a module: 0x%x:0x%x", module_start, module_end);
  172. module_data_t * mod_info = (module_data_t *)module_load_direct((void *)(module_start), module_size);
  173. if (mod_info) {
  174. debug_print(NOTICE, "Loaded: %s", mod_info->mod_info->name);
  175. }
  176. } else if (check_result == 2) {
  177. /* Mod pack */
  178. debug_print(NOTICE, "Loading modpack. %x", module_start);
  179. struct pack_header * pack_header = (struct pack_header *)module_start;
  180. while (pack_header->region_size) {
  181. void * start = (void *)((uintptr_t)pack_header + 4096);
  182. int result = module_quickcheck(start);
  183. if (result != 1) {
  184. debug_print(WARNING, "Not actually a module?! %x", start);
  185. }
  186. module_data_t * mod_info = (module_data_t *)module_load_direct(start, pack_header->region_size);
  187. if (mod_info) {
  188. debug_print(NOTICE, "Loaded: %s", mod_info->mod_info->name);
  189. }
  190. pack_header = (struct pack_header *)((uintptr_t)start + pack_header->region_size);
  191. }
  192. debug_print(NOTICE, "Done with modpack.");
  193. } else {
  194. debug_print(NOTICE, "Loading ramdisk: 0x%x:0x%x", module_start, module_end);
  195. ramdisk_mount(module_start, module_size);
  196. }
  197. }
  198. }
  199. /* Map /dev to a device mapper */
  200. map_vfs_directory("/dev");
  201. if (args_present("root")) {
  202. char * root_type = "ext2";
  203. if (args_present("root_type")) {
  204. root_type = args_value("root_type");
  205. }
  206. vfs_mount_type(root_type, args_value("root"), "/");
  207. }
  208. if (args_present("start")) {
  209. char * c = args_value("start");
  210. if (!c) {
  211. debug_print(WARNING, "Expected an argument to kernel option `start`. Ignoring.");
  212. } else {
  213. debug_print(NOTICE, "Got start argument: %s", c);
  214. boot_arg = strdup(c);
  215. }
  216. }
  217. if (!fs_root) {
  218. map_vfs_directory("/");
  219. }
  220. char * boot_app = "/bin/init";
  221. if (args_present("init")) {
  222. boot_app = args_value("init");
  223. }
  224. /* Prepare to run /bin/init */
  225. char * argv[] = {
  226. boot_app,
  227. boot_arg,
  228. NULL
  229. };
  230. int argc = 0;
  231. while (argv[argc]) {
  232. argc++;
  233. }
  234. system(argv[0], argc, argv); /* Run init */
  235. debug_print(CRITICAL, "init failed");
  236. switch_task(0);
  237. return 0;
  238. }