process.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. #pragma once
  2. #include <fcntl.h>
  3. #include <kernel/errno.h>
  4. #include <kernel/event/evtqueue.hpp>
  5. #include <kernel/interrupt.h>
  6. #include <kernel/mm.hpp>
  7. #include <kernel/task.h>
  8. #include <kernel/vfs.hpp>
  9. #include <stdint.h>
  10. #include <types/allocator.hpp>
  11. #include <types/cplusplus.hpp>
  12. #include <types/hash_map.hpp>
  13. #include <types/list.hpp>
  14. #include <types/map.hpp>
  15. #include <types/pair.hpp>
  16. #include <types/status.h>
  17. #include <types/string.hpp>
  18. #include <types/types.h>
  19. typedef size_t pid_t;
  20. class process;
  21. struct thread;
  22. class proclist;
  23. class readyqueue;
  24. inline process* volatile current_process;
  25. inline thread* volatile current_thread;
  26. inline proclist* procs;
  27. inline readyqueue* readythds;
  28. inline tss32_t tss;
  29. struct process_attr {
  30. uint16_t system : 1;
  31. uint16_t zombie : 1 = 0;
  32. };
  33. struct thread_attr {
  34. uint32_t system : 1;
  35. uint32_t ready : 1;
  36. uint32_t wait : 1;
  37. };
  38. struct thread {
  39. private:
  40. inline void alloc_kstack(void)
  41. {
  42. // TODO: alloc low mem
  43. kstack = to_pp(alloc_n_raw_pages(2));
  44. kstack += THREAD_KERNEL_STACK_SIZE;
  45. esp = reinterpret_cast<uint32_t*>(kstack);
  46. }
  47. public:
  48. uint32_t* esp;
  49. pptr_t kstack;
  50. process* owner;
  51. thread_attr attr;
  52. explicit inline thread(process* _owner, bool system)
  53. : owner { _owner }
  54. , attr {
  55. .system = system,
  56. .ready = 1,
  57. .wait = 0,
  58. }
  59. {
  60. alloc_kstack();
  61. }
  62. constexpr thread(thread&& val)
  63. : esp { val.esp }
  64. , kstack { val.kstack }
  65. , owner { val.owner }
  66. , attr { val.attr }
  67. {
  68. val.attr = {};
  69. val.esp = 0;
  70. val.kstack = 0;
  71. val.owner = nullptr;
  72. }
  73. inline thread(const thread& val)
  74. : owner { val.owner }
  75. , attr { val.attr }
  76. {
  77. alloc_kstack();
  78. }
  79. inline thread(const thread& thd, process* new_parent)
  80. : thread { thd }
  81. {
  82. owner = new_parent;
  83. }
  84. constexpr ~thread()
  85. {
  86. if (kstack)
  87. free_n_raw_pages(to_page(kstack - THREAD_KERNEL_STACK_SIZE), 2);
  88. }
  89. };
  90. class thdlist {
  91. public:
  92. using list_type = types::list<thread>;
  93. private:
  94. list_type thds;
  95. public:
  96. constexpr thdlist(const thdlist& obj) = delete;
  97. constexpr thdlist(thdlist&& obj) = delete;
  98. constexpr thdlist& operator=(const thdlist& obj) = delete;
  99. constexpr thdlist& operator=(thdlist&& obj) = delete;
  100. constexpr thdlist(thdlist&& obj, process* new_parent)
  101. : thds { types::move(obj.thds) }
  102. {
  103. for (auto& thd : thds)
  104. thd.owner = new_parent;
  105. }
  106. explicit constexpr thdlist(void)
  107. {
  108. }
  109. // implementation is below
  110. constexpr ~thdlist();
  111. template <typename... Args>
  112. constexpr thread& Emplace(Args&&... args)
  113. {
  114. return *thds.emplace_back(types::forward<Args>(args)...);
  115. }
  116. constexpr size_t size(void) const
  117. {
  118. return thds.size();
  119. }
  120. constexpr list_type& underlying_list(void)
  121. {
  122. return thds;
  123. }
  124. };
  125. class process {
  126. public:
  127. class filearr {
  128. public:
  129. using container_type = types::list<fs::file>;
  130. using array_type = types::map<int, container_type::iterator_type>;
  131. private:
  132. inline static container_type* files;
  133. array_type arr;
  134. int next_fd = 0;
  135. public:
  136. inline static void init_global_file_container(void)
  137. {
  138. files = types::pnew<types::kernel_allocator>(files);
  139. }
  140. private:
  141. // iter should not be nullptr
  142. constexpr void _close(container_type::iterator_type iter)
  143. {
  144. if (iter->ref == 1)
  145. files->erase(iter);
  146. else
  147. --iter->ref;
  148. }
  149. public:
  150. constexpr filearr(const filearr&) = delete;
  151. constexpr filearr& operator=(const filearr&) = delete;
  152. constexpr filearr& operator=(filearr&&) = delete;
  153. constexpr filearr(void) = default;
  154. constexpr filearr(filearr&& val)
  155. : arr { types::move(val.arr) }
  156. , next_fd { val.next_fd }
  157. {
  158. val.next_fd = 0;
  159. }
  160. constexpr void dup(const filearr& orig)
  161. {
  162. if (this->next_fd)
  163. return;
  164. this->next_fd = orig.next_fd;
  165. for (auto iter : orig.arr) {
  166. this->arr.insert(types::make_pair(iter.key, iter.value));
  167. ++iter.value->ref;
  168. }
  169. }
  170. constexpr fs::file* operator[](int i) const
  171. {
  172. auto iter = arr.find(i);
  173. if (!iter)
  174. return nullptr;
  175. else
  176. return &iter->value;
  177. }
  178. // TODO: file opening permissions check
  179. int open(const char* filename, uint32_t flags)
  180. {
  181. auto* dentry = fs::vfs_open(filename);
  182. if (!dentry) {
  183. errno = ENOTFOUND;
  184. return -1;
  185. }
  186. // TODO: unify file, inode, dentry TYPE
  187. fs::file::types type = fs::file::types::regular_file;
  188. if (dentry->ind->flags.in.directory)
  189. type = fs::file::types::directory;
  190. if (dentry->ind->flags.in.special_node)
  191. type = fs::file::types::block_dev;
  192. // check whether dentry is a file if O_DIRECTORY is set
  193. if ((flags & O_DIRECTORY) && type != fs::file::types::directory) {
  194. errno = ENOTDIR;
  195. return -1;
  196. }
  197. auto iter = files->emplace_back(fs::file {
  198. type,
  199. dentry->ind,
  200. dentry->parent,
  201. 0,
  202. 1 });
  203. int fd = next_fd++;
  204. arr.insert(types::make_pair(fd, iter));
  205. return fd;
  206. }
  207. constexpr void close(int fd)
  208. {
  209. auto iter = arr.find(fd);
  210. if (iter) {
  211. _close(iter->value);
  212. arr.erase(iter);
  213. }
  214. }
  215. constexpr void close_all(void)
  216. {
  217. for (auto iter : this->arr)
  218. close(iter.key);
  219. }
  220. constexpr ~filearr()
  221. {
  222. close_all();
  223. }
  224. };
  225. public:
  226. mutable kernel::mm_list mms;
  227. thdlist thds;
  228. kernel::evtqueue wait_lst;
  229. process_attr attr;
  230. pid_t pid;
  231. pid_t ppid;
  232. filearr files;
  233. types::string<> pwd;
  234. public:
  235. process(process&& val);
  236. process(const process&);
  237. explicit process(pid_t ppid, bool system = true, types::string<>&& path = "/");
  238. constexpr bool is_system(void) const
  239. {
  240. return attr.system;
  241. }
  242. constexpr bool is_zombie(void) const
  243. {
  244. return attr.zombie;
  245. }
  246. private:
  247. static inline pid_t max_pid;
  248. static inline pid_t alloc_pid(void)
  249. {
  250. return ++max_pid;
  251. }
  252. };
  253. class proclist final {
  254. public:
  255. using list_type = types::map<pid_t, process>;
  256. using child_index_type = types::hash_map<pid_t, types::list<pid_t>, types::linux_hasher<pid_t>>;
  257. using iterator_type = list_type::iterator_type;
  258. using const_iterator_type = list_type::const_iterator_type;
  259. private:
  260. list_type m_procs;
  261. child_index_type m_child_idx;
  262. public:
  263. template <typename... Args>
  264. iterator_type emplace(Args&&... args)
  265. {
  266. process _proc(types::forward<Args>(args)...);
  267. auto pid = _proc.pid;
  268. auto ppid = _proc.ppid;
  269. auto iter = m_procs.insert(types::make_pair(pid, types::move(_proc)));
  270. auto children = m_child_idx.find(ppid);
  271. if (!children) {
  272. m_child_idx.emplace(ppid, types::list<pid_t> {});
  273. children = m_child_idx.find(ppid);
  274. }
  275. children->value.push_back(pid);
  276. return iter;
  277. }
  278. constexpr void remove(pid_t pid)
  279. {
  280. make_children_orphans(pid);
  281. auto proc_iter = m_procs.find(pid);
  282. auto ppid = proc_iter->value.ppid;
  283. auto& parent_children = m_child_idx.find(ppid)->value;
  284. auto i = parent_children.find(pid);
  285. parent_children.erase(i);
  286. m_procs.erase(proc_iter);
  287. }
  288. constexpr process* find(pid_t pid)
  289. {
  290. return &m_procs.find(pid)->value;
  291. }
  292. constexpr bool has_child(pid_t pid)
  293. {
  294. auto children = m_child_idx.find(pid);
  295. return children && !children->value.empty();
  296. }
  297. constexpr void make_children_orphans(pid_t pid)
  298. {
  299. auto children = m_child_idx.find(pid);
  300. if (children) {
  301. auto init_children = m_child_idx.find(1);
  302. for (auto iter = children->value.begin(); iter != children->value.end(); ++iter) {
  303. init_children->value.push_back(*iter);
  304. this->find(*iter)->ppid = 1;
  305. }
  306. m_child_idx.remove(children);
  307. }
  308. }
  309. void kill(pid_t pid, int exit_code);
  310. };
  311. class readyqueue final {
  312. public:
  313. using list_type = types::list<thread*>;
  314. using iterator_type = list_type::iterator_type;
  315. using const_iterator_type = list_type::const_iterator_type;
  316. private:
  317. list_type m_thds;
  318. private:
  319. readyqueue(const readyqueue&) = delete;
  320. readyqueue(readyqueue&&) = delete;
  321. readyqueue& operator=(const readyqueue&) = delete;
  322. readyqueue& operator=(readyqueue&&) = delete;
  323. ~readyqueue() = delete;
  324. public:
  325. constexpr explicit readyqueue(void) = default;
  326. constexpr void push(thread* thd)
  327. {
  328. m_thds.push_back(thd);
  329. }
  330. constexpr thread* pop(void)
  331. {
  332. auto iter = m_thds.begin();
  333. while (!((*iter)->attr.ready))
  334. iter = m_thds.erase(iter);
  335. auto* ptr = *iter;
  336. m_thds.erase(iter);
  337. return ptr;
  338. }
  339. constexpr thread* query(void)
  340. {
  341. auto* thd = this->pop();
  342. this->push(thd);
  343. return thd;
  344. }
  345. constexpr void remove_all(thread* thd)
  346. {
  347. auto iter = m_thds.find(thd);
  348. while (iter != m_thds.end()) {
  349. m_thds.erase(iter);
  350. iter = m_thds.find(thd);
  351. }
  352. }
  353. };
  354. void NORETURN init_scheduler(void);
  355. void schedule(void);
  356. void NORETURN schedule_noreturn(void);
  357. constexpr uint32_t push_stack(uint32_t** stack, uint32_t val)
  358. {
  359. --*stack;
  360. **stack = val;
  361. return val;
  362. }
  363. // class thdlist
  364. constexpr thdlist::~thdlist()
  365. {
  366. for (auto iter = thds.begin(); iter != thds.end(); ++iter)
  367. readythds->remove_all(&iter);
  368. }
  369. void k_new_thread(void (*func)(void*), void* data);
  370. void NORETURN freeze(void);
  371. void NORETURN kill_current(int exit_code);