123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440 |
- #pragma once
- #include <map>
- #include <list>
- #include <memory>
- #include <queue>
- #include <set>
- #include <tuple>
- #include <utility>
- #include <errno.h>
- #include <fcntl.h>
- #include <kernel/event/evtqueue.hpp>
- #include <kernel/interrupt.h>
- #include <kernel/mm.hpp>
- #include <kernel/signal.hpp>
- #include <kernel/task.h>
- #include <kernel/tty.hpp>
- #include <kernel/vfs.hpp>
- #include <stdint.h>
- #include <sys/types.h>
- #include <types/allocator.hpp>
- #include <types/cplusplus.hpp>
- #include <types/hash_map.hpp>
- #include <types/path.hpp>
- #include <types/status.h>
- #include <types/string.hpp>
- #include <types/types.h>
- class process;
- namespace kernel::tasks {
- struct thread;
- } // namespace kernel::tasks
- class proclist;
- class readyqueue;
- inline process* volatile current_process;
- inline kernel::tasks::thread* volatile current_thread;
- inline proclist* procs;
- inline readyqueue* readythds;
- inline tss32_t tss;
- struct process_attr {
- uint16_t system : 1;
- uint16_t zombie : 1 = 0;
- };
- struct thread_attr {
- uint32_t system : 1;
- uint32_t ready : 1;
- uint32_t wait : 1;
- };
- namespace kernel::tasks {
- using tid_t = uint32_t;
- struct thread {
- private:
- void alloc_kstack(void);
- void free_kstack(uint32_t p);
- public:
- uint32_t* esp;
- uint32_t pkstack;
- pid_t owner;
- thread_attr attr;
- int* __user set_child_tid {};
- int* __user clear_child_tid {};
- types::string<> name {};
- explicit inline thread(types::string<> name, pid_t owner)
- : owner { owner }
- , attr { .system = 1, .ready = 1, .wait = 0, }
- , name { name }
- {
- alloc_kstack();
- }
- inline thread(const thread& val, pid_t owner)
- : owner { owner }, attr { val.attr }, name { val.name }
- {
- alloc_kstack();
- }
- constexpr thread(thread&& val) = default;
- inline ~thread() { free_kstack(pkstack); }
- constexpr tid_t tid() const { return pkstack; }
- constexpr bool operator==(const thread& rhs) const
- { return pkstack == rhs.pkstack; }
- constexpr bool operator<(const thread& rhs) const
- { return pkstack < rhs.pkstack; }
- };
- }
- class filearr {
- public:
- using array_type = std::map<int, std::shared_ptr<fs::file>>;
- private:
- array_type arr;
- std::priority_queue<int, std::vector<int>, std::greater<int>> _fds;
- int _greatest_fd;
- private:
- constexpr int next_fd()
- {
- if (_fds.empty())
- return _greatest_fd++;
- int retval = _fds.top();
- _fds.pop();
- return retval;
- }
- public:
- constexpr filearr(const filearr&) = delete;
- constexpr filearr& operator=(const filearr&) = delete;
- constexpr filearr& operator=(filearr&&) = delete;
- constexpr filearr(void) = default;
- constexpr filearr(filearr&& val) = default;
- constexpr int dup(int old_fd)
- {
- return dup2(old_fd, next_fd());
- }
- // TODO: the third parameter should be int flags
- // determining whether the fd should be closed
- // after exec() (FD_CLOEXEC)
- constexpr int dup2(int old_fd, int new_fd)
- {
- close(new_fd);
- auto iter = arr.find(old_fd);
- if (!iter)
- return -EBADF;
- this->arr.emplace(new_fd, iter->second);
- return new_fd;
- }
- constexpr void dup_all(const filearr& orig)
- {
- this->_fds = orig._fds;
- this->_greatest_fd = orig._greatest_fd;
- for (auto [ fd, fp ] : orig.arr)
- this->arr.emplace(fd, fp);
- }
- constexpr fs::file* operator[](int i) const
- {
- auto iter = arr.find(i);
- if (!iter)
- return nullptr;
- return iter->second.get();
- }
- int pipe(int pipefd[2])
- {
- std::shared_ptr<fs::pipe> ppipe { new fs::pipe };
- bool inserted = false;
- int fd = next_fd();
- std::tie(std::ignore, inserted) = arr.emplace(fd,
- std::shared_ptr<fs::file> {
- new fs::fifo_file(nullptr, {
- .read = 1,
- .write = 0,
- .close_on_exec = 0,
- }, ppipe),
- });
- assert(inserted);
- // TODO: use copy_to_user()
- pipefd[0] = fd;
- fd = next_fd();
- std::tie(std::ignore, inserted) = arr.emplace(fd,
- std::shared_ptr<fs::file> {
- new fs::fifo_file(nullptr, {
- .read = 0,
- .write = 1,
- .close_on_exec = 0,
- }, ppipe),
- });
- assert(inserted);
- // TODO: use copy_to_user()
- pipefd[1] = fd;
- return 0;
- }
- int open(const process& current, const types::path& filepath, int flags, mode_t mode);
- constexpr void close(int fd)
- {
- auto iter = arr.find(fd);
- if (!iter)
- return;
- iter->second->close();
- _fds.push(fd);
- arr.erase(iter);
- }
- constexpr void onexec()
- {
- for (auto&& [ fd, fp ] : arr) {
- if (fp->flags.close_on_exec)
- close(fd);
- }
- }
- constexpr void close_all(void)
- {
- for (auto&& [ fd, fp ] : arr) {
- fp->close();
- _fds.push(fd);
- }
- arr.clear();
- }
- constexpr ~filearr()
- {
- close_all();
- }
- };
- class process {
- public:
- struct wait_obj {
- pid_t pid;
- int code;
- };
- public:
- kernel::memory::mm_list mms {};
- std::set<kernel::tasks::thread> thds;
- kernel::cond_var cv_wait;
- std::list<wait_obj> waitlist;
- process_attr attr {};
- filearr files;
- types::path pwd;
- kernel::signal_list signals;
- mode_t umask { 0022 };
- pid_t pid {};
- pid_t ppid {};
- pid_t pgid {};
- pid_t sid {};
- tty* control_tty {};
- fs::vfs::dentry* root { fs::fs_root };
- std::set<pid_t> children;
- public:
- process(const process&) = delete;
- explicit process(const process& parent, pid_t pid);
- // this function is used for system initialization
- // DO NOT use this after the system is on
- explicit process(pid_t pid, pid_t ppid);
- constexpr bool is_system(void) const
- { return attr.system; }
- constexpr bool is_zombie(void) const
- { return attr.zombie; }
- };
- class proclist final {
- public:
- using list_type = std::map<pid_t, process>;
- using iterator = list_type::iterator;
- using const_iterator = list_type::const_iterator;
- private:
- list_type m_procs;
- pid_t m_nextpid = 1;
- constexpr pid_t next_pid() { return m_nextpid++; }
- public:
- process& emplace(pid_t ppid)
- {
- pid_t pid = next_pid();
- auto [ iter, inserted ] = m_procs.try_emplace(pid, pid, ppid);
- assert(inserted);
- if (try_find(ppid)) {
- bool success = false;
- std::tie(std::ignore, success) =
- find(ppid).children.insert(pid);
- assert(success);
- }
- return iter->second;
- }
- process& copy_from(process& proc)
- {
- pid_t pid = next_pid();
- auto [ iter, inserted ] = m_procs.try_emplace(pid, proc, pid);
- assert(inserted);
- proc.children.insert(pid);
- return iter->second;
- }
- constexpr void remove(pid_t pid)
- {
- make_children_orphans(pid);
- auto proc_iter = m_procs.find(pid);
- auto ppid = proc_iter->second.ppid;
- find(ppid).children.erase(pid);
- m_procs.erase(proc_iter);
- }
- constexpr bool try_find(pid_t pid) const
- { return m_procs.find(pid); }
- // if process doesn't exist, the behavior is undefined
- constexpr process& find(pid_t pid)
- {
- auto iter = m_procs.find(pid);
- assert(iter);
- return iter->second;
- }
- constexpr bool has_child(pid_t pid)
- {
- auto& proc = find(pid);
- return !proc.children.empty();
- }
- constexpr void make_children_orphans(pid_t pid)
- {
- auto& children = find(pid).children;
- auto& init_children = find(1).children;
- for (auto item : children) {
- init_children.insert(item);
- find(item).ppid = 1;
- }
- children.clear();
- }
- // the process MUST exist, or the behavior is undefined
- void send_signal(pid_t pid, kernel::sig_t signal)
- {
- auto& proc = this->find(pid);
- proc.signals.set(signal);
- }
- void send_signal_grp(pid_t pgid, kernel::sig_t signal)
- {
- for (auto& [ pid, proc ] : m_procs) {
- if (proc.pgid == pgid)
- proc.signals.set(signal);
- }
- }
- void kill(pid_t pid, int exit_code);
- };
- // TODO: lock and unlock
- class readyqueue final {
- public:
- using thread = kernel::tasks::thread;
- using list_type = std::list<thread*>;
- private:
- list_type m_thds;
- private:
- readyqueue(const readyqueue&) = delete;
- readyqueue(readyqueue&&) = delete;
- readyqueue& operator=(const readyqueue&) = delete;
- readyqueue& operator=(readyqueue&&) = delete;
- ~readyqueue() = delete;
- public:
- constexpr explicit readyqueue(void) = default;
- constexpr void push(thread* thd)
- { m_thds.push_back(thd); }
- constexpr thread* pop(void)
- {
- m_thds.remove_if([](thread* item) {
- return !item->attr.ready;
- });
- auto* retval = m_thds.front();
- m_thds.pop_front();
- return retval;
- }
- constexpr thread* query(void)
- {
- auto* thd = this->pop();
- this->push(thd);
- return thd;
- }
- constexpr void remove_all(thread* thd)
- { m_thds.remove(thd); }
- };
- void NORETURN init_scheduler(void);
- /// @return true if returned normally, false if being interrupted
- bool schedule(void);
- void NORETURN schedule_noreturn(void);
- constexpr uint32_t push_stack(uint32_t** stack, uint32_t val)
- {
- --*stack;
- **stack = val;
- return val;
- }
- void k_new_thread(void (*func)(void*), void* data);
- void NORETURN freeze(void);
- void NORETURN kill_current(int exit_code);
- void check_signal(void);
|