process.hpp 10 KB

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