process.hpp 10 KB

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