process.hpp 10 KB

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