process.cpp 7.1 KB

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