process.cpp 11 KB

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