process.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. #include <utility>
  2. #include <asm/port_io.h>
  3. #include <asm/sys.h>
  4. #include <assert.h>
  5. #include <fs/fat.hpp>
  6. #include <kernel/hw/ata.hpp>
  7. #include <kernel/interrupt.h>
  8. #include <kernel/log.hpp>
  9. #include <kernel/mem.h>
  10. #include <kernel/mm.hpp>
  11. #include <kernel/process.hpp>
  12. #include <kernel/signal.hpp>
  13. #include <kernel/vfs.hpp>
  14. #include <stdint.h>
  15. #include <stdio.h>
  16. #include <types/allocator.hpp>
  17. #include <types/bitmap.hpp>
  18. #include <types/cplusplus.hpp>
  19. #include <types/elf.hpp>
  20. #include <types/hash_map.hpp>
  21. #include <types/list.hpp>
  22. #include <types/lock.hpp>
  23. #include <types/size.h>
  24. #include <types/status.h>
  25. #include <types/types.h>
  26. static void (*volatile kthreadd_new_thd_func)(void*);
  27. static void* volatile kthreadd_new_thd_data;
  28. static types::mutex kthreadd_mtx;
  29. namespace kernel {
  30. struct no_irq_guard {
  31. explicit no_irq_guard()
  32. {
  33. asm_cli();
  34. }
  35. no_irq_guard(const no_irq_guard&) = delete;
  36. no_irq_guard& operator=(const no_irq_guard&) = delete;
  37. ~no_irq_guard()
  38. {
  39. asm_sti();
  40. }
  41. };
  42. } // namespace kernel
  43. static types::bitmap* pkstack_bmp;
  44. void kernel::tasks::thread::alloc_kstack(void)
  45. {
  46. static int __allocated;
  47. if (!pkstack_bmp)
  48. pkstack_bmp = new types::bitmap((0x1000000 - 0xc00000) / 0x2000);
  49. for (int i = 0; i < __allocated; ++i) {
  50. if (pkstack_bmp->test(i) == 0) {
  51. pkstack = 0xffc00000 + THREAD_KERNEL_STACK_SIZE * (i + 1);
  52. esp = reinterpret_cast<uint32_t*>(pkstack);
  53. pkstack_bmp->set(i);
  54. return;
  55. }
  56. }
  57. // kernel stack pt is at page#0x00005
  58. kernel::paccess pa(0x00005);
  59. auto pt = (pt_t)pa.ptr();
  60. assert(pt);
  61. pte_t* pte = *pt + __allocated * 2;
  62. pte[0].v = 0x3;
  63. pte[0].in.page = __alloc_raw_page();
  64. pte[1].v = 0x3;
  65. pte[1].in.page = __alloc_raw_page();
  66. pkstack = 0xffc00000 + THREAD_KERNEL_STACK_SIZE * (__allocated + 1);
  67. esp = reinterpret_cast<uint32_t*>(pkstack);
  68. pkstack_bmp->set(__allocated);
  69. ++__allocated;
  70. }
  71. void kernel::tasks::thread::free_kstack(uint32_t p)
  72. {
  73. p -= 0xffc00000;
  74. p /= THREAD_KERNEL_STACK_SIZE;
  75. p -= 1;
  76. pkstack_bmp->clear(p);
  77. }
  78. process::process(const process& parent, pid_t pid)
  79. : mms { *kernel_mms }
  80. , attr { parent.attr } , pwd { parent.pwd }
  81. , signals { parent.signals } , pid { pid }
  82. , ppid { parent.pid } , pgid { parent.pgid } , sid { parent.sid }
  83. , control_tty { parent.control_tty } , children {}
  84. {
  85. for (auto& area : parent.mms) {
  86. if (area.is_kernel_space() || area.attr.in.system)
  87. continue;
  88. mms.mirror_area(area);
  89. }
  90. this->files.dup_all(parent.files);
  91. }
  92. process::process(pid_t pid, pid_t ppid)
  93. : mms(*kernel_mms)
  94. , attr { .system = true }
  95. , pwd { "/" }
  96. , pid { pid }
  97. , ppid { ppid }
  98. , pgid {} , sid {} , control_tty {}
  99. , children {} { }
  100. void proclist::kill(pid_t pid, int exit_code)
  101. {
  102. auto& proc = this->find(pid);
  103. // remove threads from ready list
  104. for (auto& thd : proc.thds) {
  105. thd.attr.ready = 0;
  106. readythds->remove_all(&thd);
  107. }
  108. // write back mmap'ped files and close them
  109. proc.files.close_all();
  110. // unmap all user memory areas
  111. proc.mms.clear_user();
  112. // init should never exit
  113. if (proc.ppid == 0) {
  114. console->print("kernel panic: init exited!\n");
  115. assert(false);
  116. }
  117. // make child processes orphans (children of init)
  118. this->make_children_orphans(pid);
  119. proc.attr.zombie = 1;
  120. // notify parent process and init
  121. auto& parent = this->find(proc.ppid);
  122. auto& init = this->find(1);
  123. bool flag = false;
  124. {
  125. auto& mtx = init.cv_wait.mtx();
  126. types::lock_guard lck(mtx);
  127. {
  128. auto& mtx = proc.cv_wait.mtx();
  129. types::lock_guard lck(mtx);
  130. for (const auto& item : proc.waitlist) {
  131. init.waitlist.push_back(item);
  132. flag = true;
  133. }
  134. proc.waitlist.clear();
  135. }
  136. }
  137. if (flag)
  138. init.cv_wait.notify();
  139. {
  140. auto& mtx = parent.cv_wait.mtx();
  141. types::lock_guard lck(mtx);
  142. parent.waitlist.push_back({ pid, exit_code });
  143. }
  144. parent.cv_wait.notify();
  145. }
  146. void kernel_threadd_main(void)
  147. {
  148. kmsg("kernel thread daemon started\n");
  149. for (;;) {
  150. if (kthreadd_new_thd_func) {
  151. void (*func)(void*) = nullptr;
  152. void* data = nullptr;
  153. {
  154. types::lock_guard lck(kthreadd_mtx);
  155. func = std::exchange(kthreadd_new_thd_func, nullptr);
  156. data = std::exchange(kthreadd_new_thd_data, nullptr);
  157. }
  158. // TODO
  159. (void)func, (void)data;
  160. assert(false);
  161. // syscall_fork
  162. // int ret = syscall(0x00);
  163. // if (ret == 0) {
  164. // // child process
  165. // func(data);
  166. // // the function shouldn't return here
  167. // assert(false);
  168. // }
  169. }
  170. // TODO: sleep here to wait for new_kernel_thread event
  171. asm_hlt();
  172. }
  173. }
  174. void NORETURN _kernel_init(void)
  175. {
  176. // pid 2 is kernel thread daemon
  177. auto& proc = procs->emplace(1);
  178. assert(proc.pid == 2);
  179. // create thread
  180. auto [ iter_thd, inserted] = proc.thds.emplace(proc.pid);
  181. assert(inserted);
  182. auto& thd = *iter_thd;
  183. auto* esp = &thd.esp;
  184. // return(start) address
  185. push_stack(esp, (uint32_t)kernel_threadd_main);
  186. // ebx
  187. push_stack(esp, 0);
  188. // edi
  189. push_stack(esp, 0);
  190. // esi
  191. push_stack(esp, 0);
  192. // ebp
  193. push_stack(esp, 0);
  194. // eflags
  195. push_stack(esp, 0x200);
  196. readythds->push(&thd);
  197. // ------------------------------------------
  198. asm_sti();
  199. hw::init_ata();
  200. // TODO: parse kernel parameters
  201. auto* drive = fs::vfs_open("/dev/hda1");
  202. assert(drive);
  203. auto* _new_fs = fs::register_fs(new fs::fat::fat32(drive->ind));
  204. auto* mnt = fs::vfs_open("/mnt");
  205. assert(mnt);
  206. int ret = fs::fs_root->ind->fs->mount(mnt, _new_fs);
  207. assert(ret == GB_OK);
  208. current_process->attr.system = 0;
  209. current_thread->attr.system = 0;
  210. const char* argv[] = { "/mnt/init", "/mnt/sh", nullptr };
  211. const char* envp[] = { nullptr };
  212. types::elf::elf32_load_data d;
  213. d.exec = "/mnt/init";
  214. d.argv = argv;
  215. d.envp = envp;
  216. d.system = false;
  217. ret = types::elf::elf32_load(&d);
  218. assert(ret == GB_OK);
  219. asm volatile(
  220. "movw $0x23, %%ax\n"
  221. "movw %%ax, %%ds\n"
  222. "movw %%ax, %%es\n"
  223. "movw %%ax, %%fs\n"
  224. "movw %%ax, %%gs\n"
  225. "pushl $0x23\n"
  226. "pushl %0\n"
  227. "pushl $0x200\n"
  228. "pushl $0x1b\n"
  229. "pushl %1\n"
  230. "iret\n"
  231. :
  232. : "c"(d.sp), "d"(d.eip)
  233. : "eax", "memory");
  234. freeze();
  235. }
  236. void k_new_thread(void (*func)(void*), void* data)
  237. {
  238. types::lock_guard lck(kthreadd_mtx);
  239. kthreadd_new_thd_func = func;
  240. kthreadd_new_thd_data = data;
  241. }
  242. void NORETURN init_scheduler(void)
  243. {
  244. {
  245. extern char __stage1_start[];
  246. extern char __kinit_end[];
  247. kernel::paccess pa(EARLY_KERNEL_PD_PAGE);
  248. auto pd = (pd_t)pa.ptr();
  249. assert(pd);
  250. (*pd)[0].v = 0;
  251. // free pt#0
  252. __free_raw_page(0x00002);
  253. // free .stage1 and .kinit
  254. for (uint32_t i = ((uint32_t)__stage1_start >> 12);
  255. i < ((uint32_t)__kinit_end >> 12); ++i) {
  256. __free_raw_page(i);
  257. }
  258. }
  259. procs = new proclist;
  260. readythds = new readyqueue;
  261. filearr::init_global_file_container();
  262. // init process has no parent
  263. auto& init = procs->emplace(0);
  264. assert(init.pid == 1);
  265. auto [ iter_thd, inserted ] = init.thds.emplace(init.pid);
  266. assert(inserted);
  267. auto& thd = *iter_thd;
  268. init.files.open("/dev/console", O_RDONLY);
  269. init.files.open("/dev/console", O_WRONLY);
  270. init.files.open("/dev/console", O_WRONLY);
  271. // we need interrupts enabled for cow mapping so now we disable it
  272. // in case timer interrupt mess things up
  273. asm_cli();
  274. current_process = &init;
  275. current_thread = &thd;
  276. readythds->push(current_thread);
  277. tss.ss0 = KERNEL_DATA_SEGMENT;
  278. tss.esp0 = current_thread->pkstack;
  279. asm_switch_pd(current_process->mms.m_pd);
  280. asm volatile(
  281. "movl %0, %%esp\n"
  282. "pushl %=f\n"
  283. "pushl %1\n"
  284. "movw $0x10, %%ax\n"
  285. "movw %%ax, %%ss\n"
  286. "movw %%ax, %%ds\n"
  287. "movw %%ax, %%es\n"
  288. "movw %%ax, %%fs\n"
  289. "movw %%ax, %%gs\n"
  290. "xorl %%ebp, %%ebp\n"
  291. "xorl %%edx, %%edx\n"
  292. "pushl $0x0\n"
  293. "popfl\n"
  294. "ret\n"
  295. "%=:\n"
  296. "ud2"
  297. :
  298. : "a"(current_thread->esp), "c"(_kernel_init)
  299. : "memory");
  300. freeze();
  301. }
  302. extern "C" void asm_ctx_switch(uint32_t** curr_esp, uint32_t* next_esp);
  303. bool schedule()
  304. {
  305. auto thd = readythds->query();
  306. process* proc = nullptr;
  307. kernel::tasks::thread* curr_thd = nullptr;
  308. if (current_thread == thd)
  309. goto _end;
  310. proc = &procs->find(thd->owner);
  311. if (current_process != proc) {
  312. asm_switch_pd(proc->mms.m_pd);
  313. current_process = proc;
  314. }
  315. curr_thd = current_thread;
  316. current_thread = thd;
  317. tss.esp0 = current_thread->pkstack;
  318. asm_ctx_switch(&curr_thd->esp, thd->esp);
  319. _end:
  320. return current_process->signals.empty();
  321. }
  322. void NORETURN schedule_noreturn(void)
  323. {
  324. schedule();
  325. freeze();
  326. }
  327. void NORETURN freeze(void)
  328. {
  329. asm_cli();
  330. asm_hlt();
  331. for (;;)
  332. ;
  333. }
  334. void NORETURN kill_current(int exit_code)
  335. {
  336. procs->kill(current_process->pid, exit_code);
  337. schedule_noreturn();
  338. }
  339. void check_signal()
  340. {
  341. switch (current_process->signals.pop()) {
  342. case kernel::SIGINT:
  343. case kernel::SIGQUIT:
  344. case kernel::SIGPIPE:
  345. case kernel::SIGSTOP:
  346. kill_current(-1);
  347. break;
  348. case 0:
  349. break;
  350. }
  351. }