process.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. #include <asm/port_io.h>
  2. #include <asm/sys.h>
  3. #include <fs/fat.hpp>
  4. #include <kernel/hw/ata.hpp>
  5. #include <kernel/interrupt.h>
  6. #include <kernel/mem.h>
  7. #include <kernel/mm.hpp>
  8. #include <kernel/process.hpp>
  9. #include <kernel/stdio.hpp>
  10. #include <kernel/vfs.hpp>
  11. #include <kernel_main.hpp>
  12. #include <types/allocator.hpp>
  13. #include <types/assert.h>
  14. #include <types/cplusplus.hpp>
  15. #include <types/elf.hpp>
  16. #include <types/hash_map.hpp>
  17. #include <types/list.hpp>
  18. #include <types/lock.hpp>
  19. #include <types/size.h>
  20. #include <types/status.h>
  21. #include <types/stdint.h>
  22. #include <types/types.h>
  23. static void (*volatile kthreadd_new_thd_func)(void*);
  24. static void* volatile kthreadd_new_thd_data;
  25. static types::mutex kthreadd_mtx;
  26. namespace kernel {
  27. struct no_irq_guard {
  28. explicit no_irq_guard()
  29. {
  30. asm_cli();
  31. }
  32. no_irq_guard(const no_irq_guard&) = delete;
  33. no_irq_guard& operator=(const no_irq_guard&) = delete;
  34. ~no_irq_guard()
  35. {
  36. asm_sti();
  37. }
  38. };
  39. } // namespace kernel
  40. process::process(process&& val)
  41. : mms(types::move(val.mms))
  42. , thds { types::move(val.thds), this }
  43. , wait_lst(types::move(val.wait_lst))
  44. , attr { val.attr }
  45. , pid(val.pid)
  46. , ppid(val.ppid)
  47. , files(types::move(val.files))
  48. {
  49. if (current_process == &val)
  50. current_process = this;
  51. val.pid = 0;
  52. val.ppid = 0;
  53. val.attr.system = 0;
  54. val.attr.zombie = 0;
  55. }
  56. process::process(const process& parent)
  57. : process { parent.pid, parent.is_system() }
  58. {
  59. for (auto& area : parent.mms) {
  60. if (area.is_ident())
  61. continue;
  62. mms.mirror_area(area);
  63. }
  64. this->files.dup(parent.files);
  65. }
  66. process::process(pid_t _ppid, bool _system)
  67. : mms(*kernel_mms)
  68. , attr { .system = _system }
  69. , pid { process::alloc_pid() }
  70. , ppid { _ppid }
  71. {
  72. }
  73. inline void NORETURN _noreturn_crash(void)
  74. {
  75. for (;;)
  76. assert(false);
  77. }
  78. void kernel_threadd_main(void)
  79. {
  80. kmsg("kernel thread daemon started\n");
  81. for (;;) {
  82. if (kthreadd_new_thd_func) {
  83. void (*func)(void*) = nullptr;
  84. void* data = nullptr;
  85. {
  86. types::lock_guard lck(kthreadd_mtx);
  87. func = kthreadd_new_thd_func;
  88. data = kthreadd_new_thd_data;
  89. kthreadd_new_thd_func = nullptr;
  90. kthreadd_new_thd_data = nullptr;
  91. }
  92. // TODO
  93. (void)func, (void)data;
  94. assert(false);
  95. // syscall_fork
  96. // int ret = syscall(0x00);
  97. // if (ret == 0) {
  98. // // child process
  99. // func(data);
  100. // // the function shouldn't return here
  101. // assert(false);
  102. // }
  103. }
  104. // TODO: sleep here to wait for new_kernel_thread event
  105. asm_hlt();
  106. }
  107. }
  108. void NORETURN _kernel_init(void)
  109. {
  110. // pid 2 is kernel thread daemon
  111. auto* proc = &procs->emplace(1)->value;
  112. // create thread
  113. thread thd(proc, true);
  114. auto* esp = &thd.esp;
  115. // return(start) address
  116. push_stack(esp, (uint32_t)kernel_threadd_main);
  117. // ebx
  118. push_stack(esp, 0);
  119. // edi
  120. push_stack(esp, 0);
  121. // esi
  122. push_stack(esp, 0);
  123. // ebp
  124. push_stack(esp, 0);
  125. // eflags
  126. push_stack(esp, 0x200);
  127. readythds->push(&proc->thds.Emplace(types::move(thd)));
  128. // ------------------------------------------
  129. asm_sti();
  130. hw::init_ata();
  131. // TODO: parse kernel parameters
  132. auto* _new_fs = fs::register_fs(types::_new<types::kernel_allocator, fs::fat::fat32>(fs::vfs_open("/dev/hda1")->ind));
  133. int ret = fs::fs_root->ind->fs->mount(fs::vfs_open("/mnt"), _new_fs);
  134. assert_likely(ret == GB_OK);
  135. current_process->attr.system = 0;
  136. current_thread->attr.system = 0;
  137. const char* argv[] = { "/mnt/INIT.ELF", nullptr };
  138. types::elf::elf32_load_data d;
  139. d.exec = "/mnt/INIT.ELF";
  140. d.argv = argv;
  141. d.system = false;
  142. assert(types::elf::elf32_load(&d) == GB_OK);
  143. asm volatile(
  144. "movw $0x23, %%ax\n"
  145. "movw %%ax, %%ds\n"
  146. "movw %%ax, %%es\n"
  147. "movw %%ax, %%fs\n"
  148. "movw %%ax, %%gs\n"
  149. "pushl $0x23\n"
  150. "pushl %0\n"
  151. "pushl $0x200\n"
  152. "pushl $0x1b\n"
  153. "pushl %1\n"
  154. "iret\n"
  155. :
  156. : "c"(d.sp), "d"(d.eip)
  157. : "eax", "memory");
  158. _noreturn_crash();
  159. }
  160. void k_new_thread(void (*func)(void*), void* data)
  161. {
  162. types::lock_guard lck(kthreadd_mtx);
  163. kthreadd_new_thd_func = func;
  164. kthreadd_new_thd_data = data;
  165. }
  166. void NORETURN init_scheduler(void)
  167. {
  168. procs = types::pnew<types::kernel_allocator>(procs);
  169. readythds = types::pnew<types::kernel_allocator>(readythds);
  170. // init process has no parent
  171. auto* init = &procs->emplace(0)->value;
  172. init->files.open("/dev/console", 0);
  173. // we need interrupts enabled for cow mapping so now we disable it
  174. // in case timer interrupt mess things up
  175. asm_cli();
  176. current_process = init;
  177. current_thread = &init->thds.Emplace(init, true);
  178. readythds->push(current_thread);
  179. tss.ss0 = KERNEL_DATA_SEGMENT;
  180. tss.esp0 = current_thread->kstack;
  181. asm_switch_pd(current_process->mms.m_pd);
  182. asm volatile(
  183. "movl %0, %%esp\n"
  184. "pushl %=f\n"
  185. "pushl %1\n"
  186. "movw $0x10, %%ax\n"
  187. "movw %%ax, %%ss\n"
  188. "movw %%ax, %%ds\n"
  189. "movw %%ax, %%es\n"
  190. "movw %%ax, %%fs\n"
  191. "movw %%ax, %%gs\n"
  192. "xorl %%ebp, %%ebp\n"
  193. "xorl %%edx, %%edx\n"
  194. "pushl $0x0\n"
  195. "popfl\n"
  196. "ret\n"
  197. "%=:\n"
  198. "ud2"
  199. :
  200. : "a"(current_thread->esp), "c"(_kernel_init)
  201. : "memory");
  202. _noreturn_crash();
  203. }
  204. extern "C" void asm_ctx_switch(uint32_t** curr_esp, uint32_t* next_esp);
  205. void schedule()
  206. {
  207. auto thd = readythds->query();
  208. if (current_thread == thd)
  209. return;
  210. process* proc = thd->owner;
  211. if (current_process != proc) {
  212. asm_switch_pd(proc->mms.m_pd);
  213. current_process = proc;
  214. }
  215. auto* curr_thd = current_thread;
  216. current_thread = thd;
  217. tss.esp0 = current_thread->kstack;
  218. asm_ctx_switch(&curr_thd->esp, thd->esp);
  219. }