process.hpp 10 KB

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