Process.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. #pragma once
  2. #include "types.h"
  3. #include "TSS.h"
  4. #include "i386.h"
  5. #include "TTY.h"
  6. #include "Syscall.h"
  7. #include "GUITypes.h"
  8. #include <VirtualFileSystem/VirtualFileSystem.h>
  9. #include <VirtualFileSystem/UnixTypes.h>
  10. #include <AK/InlineLinkedList.h>
  11. #include <AK/AKString.h>
  12. #include <AK/Vector.h>
  13. #include <AK/WeakPtr.h>
  14. #include <AK/Lock.h>
  15. class FileDescriptor;
  16. class PageDirectory;
  17. class Region;
  18. class VMObject;
  19. class Zone;
  20. class WSWindow;
  21. #define COOL_GLOBALS
  22. #ifdef COOL_GLOBALS
  23. struct CoolGlobals {
  24. pid_t current_pid;
  25. };
  26. extern CoolGlobals* g_cool_globals;
  27. #endif
  28. struct SignalActionData {
  29. LinearAddress handler_or_sigaction;
  30. dword mask { 0 };
  31. int flags { 0 };
  32. LinearAddress restorer;
  33. };
  34. struct DisplayInfo {
  35. unsigned width;
  36. unsigned height;
  37. unsigned bpp;
  38. unsigned pitch;
  39. byte* framebuffer;
  40. };
  41. class Process : public InlineLinkedListNode<Process> {
  42. friend class InlineLinkedListNode<Process>;
  43. friend class WSWindowManager; // FIXME: Make a better API for allocate_region().
  44. friend class GraphicsBitmap; // FIXME: Make a better API for allocate_region().
  45. public:
  46. static Process* create_kernel_process(String&& name, void (*entry)());
  47. static Process* create_user_process(const String& path, uid_t, gid_t, pid_t ppid, int& error, Vector<String>&& arguments = Vector<String>(), Vector<String>&& environment = Vector<String>(), TTY* = nullptr);
  48. ~Process();
  49. static Vector<Process*> allProcesses();
  50. enum State {
  51. Invalid = 0,
  52. Runnable,
  53. Running,
  54. Skip1SchedulerPass,
  55. Skip0SchedulerPasses,
  56. Dead,
  57. BeingInspected,
  58. BlockedSleep,
  59. BlockedWait,
  60. BlockedRead,
  61. BlockedWrite,
  62. BlockedSignal,
  63. BlockedSelect,
  64. };
  65. enum RingLevel {
  66. Ring0 = 0,
  67. Ring3 = 3,
  68. };
  69. bool isRing0() const { return m_ring == Ring0; }
  70. bool isRing3() const { return m_ring == Ring3; }
  71. bool is_blocked() const
  72. {
  73. return m_state == BlockedSleep || m_state == BlockedWait || m_state == BlockedRead || m_state == BlockedSignal || m_state == BlockedSelect;
  74. }
  75. PageDirectory& page_directory() { return *m_page_directory; }
  76. const PageDirectory& page_directory() const { return *m_page_directory; }
  77. bool in_kernel() const { return (m_tss.cs & 0x03) == 0; }
  78. static Process* from_pid(pid_t);
  79. const String& name() const { return m_name; }
  80. pid_t pid() const { return m_pid; }
  81. pid_t sid() const { return m_sid; }
  82. pid_t pgid() const { return m_pgid; }
  83. dword ticks() const { return m_ticks; }
  84. word selector() const { return m_farPtr.selector; }
  85. TSS32& tss() { return m_tss; }
  86. State state() const { return m_state; }
  87. uid_t uid() const { return m_uid; }
  88. gid_t gid() const { return m_gid; }
  89. uid_t euid() const { return m_euid; }
  90. gid_t egid() const { return m_egid; }
  91. pid_t ppid() const { return m_ppid; }
  92. const FarPtr& farPtr() const { return m_farPtr; }
  93. FileDescriptor* file_descriptor(int fd);
  94. const FileDescriptor* file_descriptor(int fd) const;
  95. void block(Process::State);
  96. void unblock();
  97. void setWakeupTime(dword t) { m_wakeupTime = t; }
  98. dword wakeupTime() const { return m_wakeupTime; }
  99. template<typename Callback> static void for_each(Callback);
  100. template<typename Callback> static void for_each_in_pgrp(pid_t, Callback);
  101. template<typename Callback> static void for_each_in_state(State, Callback);
  102. template<typename Callback> static void for_each_not_in_state(State, Callback);
  103. template<typename Callback> void for_each_child(Callback);
  104. bool tick() { ++m_ticks; return --m_ticksLeft; }
  105. void set_ticks_left(dword t) { m_ticksLeft = t; }
  106. void setSelector(word s) { m_farPtr.selector = s; }
  107. void set_state(State s) { m_state = s; }
  108. pid_t sys$setsid();
  109. pid_t sys$getsid(pid_t);
  110. int sys$setpgid(pid_t pid, pid_t pgid);
  111. pid_t sys$getpgrp();
  112. pid_t sys$getpgid(pid_t);
  113. uid_t sys$getuid();
  114. gid_t sys$getgid();
  115. uid_t sys$geteuid();
  116. gid_t sys$getegid();
  117. pid_t sys$getpid();
  118. pid_t sys$getppid();
  119. mode_t sys$umask(mode_t);
  120. int sys$open(const char* path, int options);
  121. int sys$close(int fd);
  122. ssize_t sys$read(int fd, void* outbuf, size_t nread);
  123. ssize_t sys$write(int fd, const void*, size_t);
  124. int sys$fstat(int fd, Unix::stat*);
  125. int sys$lstat(const char*, Unix::stat*);
  126. int sys$stat(const char*, Unix::stat*);
  127. int sys$lseek(int fd, off_t, int whence);
  128. int sys$kill(pid_t pid, int sig);
  129. int sys$geterror() { return m_error; }
  130. void sys$exit(int status) NORETURN;
  131. void sys$sigreturn() NORETURN;
  132. pid_t sys$waitpid(pid_t, int* wstatus, int options);
  133. void* sys$mmap(const Syscall::SC_mmap_params*);
  134. int sys$munmap(void*, size_t size);
  135. int sys$set_mmap_name(void*, size_t, const char*);
  136. int sys$select(const Syscall::SC_select_params*);
  137. ssize_t sys$get_dir_entries(int fd, void*, size_t);
  138. int sys$getcwd(char*, size_t);
  139. int sys$chdir(const char*);
  140. int sys$sleep(unsigned seconds);
  141. int sys$gettimeofday(timeval*);
  142. int sys$gethostname(char* name, size_t length);
  143. int sys$get_arguments(int* argc, char*** argv);
  144. int sys$get_environment(char*** environ);
  145. int sys$uname(utsname*);
  146. int sys$readlink(const char*, char*, size_t);
  147. int sys$ttyname_r(int fd, char*, size_t);
  148. int sys$ptsname_r(int fd, char*, size_t);
  149. pid_t sys$fork(RegisterDump&);
  150. int sys$execve(const char* filename, const char** argv, const char** envp);
  151. int sys$isatty(int fd);
  152. int sys$getdtablesize();
  153. int sys$dup(int oldfd);
  154. int sys$dup2(int oldfd, int newfd);
  155. int sys$sigaction(int signum, const Unix::sigaction* act, Unix::sigaction* old_act);
  156. int sys$sigprocmask(int how, const Unix::sigset_t* set, Unix::sigset_t* old_set);
  157. int sys$sigpending(Unix::sigset_t*);
  158. int sys$getgroups(int size, gid_t*);
  159. int sys$setgroups(size_t, const gid_t*);
  160. int sys$pipe(int* pipefd);
  161. int sys$killpg(int pgrp, int sig);
  162. int sys$setgid(gid_t);
  163. int sys$setuid(uid_t);
  164. unsigned sys$alarm(unsigned seconds);
  165. int sys$access(const char* pathname, int mode);
  166. int sys$fcntl(int fd, int cmd, dword extra_arg);
  167. int sys$ioctl(int fd, unsigned request, unsigned arg);
  168. int sys$mkdir(const char* pathname, mode_t mode);
  169. Unix::clock_t sys$times(Unix::tms*);
  170. int sys$utime(const char* pathname, const struct Unix::utimbuf*);
  171. int gui$create_window(const GUI_CreateWindowParameters*);
  172. int gui$destroy_window(int window_id);
  173. int gui$get_window_backing_store(int window_id, GUI_WindowBackingStoreInfo*);
  174. int gui$invalidate_window(int window_id);
  175. DisplayInfo get_display_info();
  176. static void initialize();
  177. static void initialize_gui_statics();
  178. int make_window_id();
  179. void crash() NORETURN;
  180. static int reap(Process&) WARN_UNUSED_RESULT;
  181. const TTY* tty() const { return m_tty; }
  182. void set_tty(TTY* tty) { m_tty = tty; }
  183. size_t regionCount() const { return m_regions.size(); }
  184. const Vector<RetainPtr<Region>>& regions() const { return m_regions; }
  185. void dumpRegions();
  186. void did_schedule() { ++m_timesScheduled; }
  187. dword timesScheduled() const { return m_timesScheduled; }
  188. dword m_ticks_in_user { 0 };
  189. dword m_ticks_in_kernel { 0 };
  190. dword m_ticks_in_user_for_dead_children { 0 };
  191. dword m_ticks_in_kernel_for_dead_children { 0 };
  192. pid_t waitee_pid() const { return m_waitee_pid; }
  193. dword framePtr() const { return m_tss.ebp; }
  194. dword stackPtr() const { return m_tss.esp; }
  195. dword stackTop() const { return m_tss.ss == 0x10 ? m_stackTop0 : m_stackTop3; }
  196. bool validate_read_from_kernel(LinearAddress) const;
  197. bool validate_read(const void*, size_t) const;
  198. bool validate_write(void*, size_t) const;
  199. bool validate_read_str(const char* str) { return validate_read(str, strlen(str) + 1); }
  200. template<typename T> bool validate_read_typed(T* value, size_t count = 1) { return validate_read(value, sizeof(T) * count); }
  201. template<typename T> bool validate_write_typed(T* value, size_t count = 1) { return validate_write(value, sizeof(T) * count); }
  202. Inode* cwd_inode();
  203. Inode* executable_inode() { return m_executable.ptr(); }
  204. size_t number_of_open_file_descriptors() const;
  205. size_t max_open_file_descriptors() const { return m_max_open_file_descriptors; }
  206. void send_signal(byte signal, Process* sender);
  207. bool dispatch_one_pending_signal();
  208. bool dispatch_signal(byte signal);
  209. bool has_unmasked_pending_signals() const;
  210. void terminate_due_to_signal(byte signal);
  211. Process* fork(RegisterDump&);
  212. int exec(const String& path, Vector<String>&& arguments, Vector<String>&& environment);
  213. bool is_root() const { return m_euid == 0; }
  214. Vector<GUI_Event>& gui_events() { return m_gui_events; }
  215. SpinLock& gui_events_lock() { return m_gui_events_lock; }
  216. bool wakeup_requested() { return m_wakeup_requested; }
  217. void request_wakeup() { m_wakeup_requested = true; }
  218. private:
  219. friend class MemoryManager;
  220. friend class Scheduler;
  221. friend class Region;
  222. Process(String&& name, uid_t, gid_t, pid_t ppid, RingLevel, RetainPtr<Inode>&& cwd = nullptr, RetainPtr<Inode>&& executable = nullptr, TTY* = nullptr, Process* fork_parent = nullptr);
  223. int do_exec(const String& path, Vector<String>&& arguments, Vector<String>&& environment);
  224. void push_value_on_stack(dword);
  225. int alloc_fd();
  226. OwnPtr<PageDirectory> m_page_directory;
  227. Process* m_prev { nullptr };
  228. Process* m_next { nullptr };
  229. String m_name;
  230. void (*m_entry)() { nullptr };
  231. pid_t m_pid { 0 };
  232. uid_t m_uid { 0 };
  233. gid_t m_gid { 0 };
  234. uid_t m_euid { 0 };
  235. gid_t m_egid { 0 };
  236. pid_t m_sid { 0 };
  237. pid_t m_pgid { 0 };
  238. dword m_ticks { 0 };
  239. dword m_ticksLeft { 0 };
  240. dword m_stackTop0 { 0 };
  241. dword m_stackTop3 { 0 };
  242. FarPtr m_farPtr;
  243. State m_state { Invalid };
  244. dword m_wakeupTime { 0 };
  245. TSS32 m_tss;
  246. TSS32 m_tss_to_resume_kernel;
  247. struct FileDescriptorAndFlags {
  248. operator bool() const { return !!descriptor; }
  249. void clear() { descriptor = nullptr; flags = 0; }
  250. void set(RetainPtr<FileDescriptor>&& d, dword f = 0) { descriptor = move(d), flags = f; }
  251. RetainPtr<FileDescriptor> descriptor;
  252. dword flags { 0 };
  253. };
  254. Vector<FileDescriptorAndFlags> m_fds;
  255. RingLevel m_ring { Ring0 };
  256. int m_error { 0 };
  257. void* m_kernelStack { nullptr };
  258. dword m_timesScheduled { 0 };
  259. pid_t m_waitee_pid { -1 };
  260. int m_blocked_fd { -1 };
  261. Vector<int> m_select_read_fds;
  262. Vector<int> m_select_write_fds;
  263. size_t m_max_open_file_descriptors { 16 };
  264. SignalActionData m_signal_action_data[32];
  265. dword m_pending_signals { 0 };
  266. dword m_signal_mask { 0xffffffff };
  267. byte m_termination_status { 0 };
  268. byte m_termination_signal { 0 };
  269. RetainPtr<Inode> m_cwd;
  270. RetainPtr<Inode> m_executable;
  271. TTY* m_tty { nullptr };
  272. Region* allocate_region(LinearAddress, size_t, String&& name, bool is_readable = true, bool is_writable = true, bool commit = true);
  273. Region* allocate_file_backed_region(LinearAddress, size_t, RetainPtr<Inode>&&, String&& name, bool is_readable, bool is_writable);
  274. Region* allocate_region_with_vmo(LinearAddress, size_t, RetainPtr<VMObject>&&, size_t offset_in_vmo, String&& name, bool is_readable, bool is_writable);
  275. bool deallocate_region(Region& region);
  276. Region* regionFromRange(LinearAddress, size_t);
  277. Vector<RetainPtr<Region>> m_regions;
  278. // FIXME: Implement some kind of ASLR?
  279. LinearAddress m_nextRegion;
  280. LinearAddress m_return_to_ring3_from_signal_trampoline;
  281. LinearAddress m_return_to_ring0_from_signal_trampoline;
  282. pid_t m_ppid { 0 };
  283. mode_t m_umask { 022 };
  284. bool m_was_interrupted_while_blocked { false };
  285. static void notify_waiters(pid_t waitee, int exit_status, int signal);
  286. Vector<String> m_arguments;
  287. Vector<String> m_initialEnvironment;
  288. HashTable<gid_t> m_gids;
  289. Region* m_stack_region { nullptr };
  290. Region* m_signal_stack_user_region { nullptr };
  291. Region* m_signal_stack_kernel_region { nullptr };
  292. RetainPtr<Region> m_display_framebuffer_region;
  293. HashMap<int, OwnPtr<WSWindow>> m_windows;
  294. Vector<GUI_Event> m_gui_events;
  295. SpinLock m_gui_events_lock;
  296. int m_next_window_id { 1 };
  297. dword m_wakeup_requested { false };
  298. };
  299. extern Process* current;
  300. class ProcessInspectionHandle {
  301. public:
  302. ProcessInspectionHandle(Process& process)
  303. : m_process(process)
  304. , m_original_state(process.state())
  305. {
  306. if (&process != current)
  307. m_process.set_state(Process::BeingInspected);
  308. }
  309. ~ProcessInspectionHandle()
  310. {
  311. m_process.set_state(m_original_state);
  312. }
  313. Process* operator->() { return &m_process; }
  314. Process& operator*() { return m_process; }
  315. private:
  316. Process& m_process;
  317. Process::State m_original_state { Process::Invalid };
  318. };
  319. static inline const char* toString(Process::State state)
  320. {
  321. switch (state) {
  322. case Process::Invalid: return "Invalid";
  323. case Process::Runnable: return "Runnable";
  324. case Process::Running: return "Running";
  325. case Process::Dead: return "Dead";
  326. case Process::Skip1SchedulerPass: return "Skip1";
  327. case Process::Skip0SchedulerPasses: return "Skip0";
  328. case Process::BlockedSleep: return "Sleep";
  329. case Process::BlockedWait: return "Wait";
  330. case Process::BlockedRead: return "Read";
  331. case Process::BlockedWrite: return "Write";
  332. case Process::BlockedSignal: return "Signal";
  333. case Process::BlockedSelect: return "Select";
  334. case Process::BeingInspected: return "Inspect";
  335. }
  336. ASSERT_NOT_REACHED();
  337. return nullptr;
  338. }
  339. extern void block(Process::State);
  340. extern void sleep(dword ticks);
  341. extern InlineLinkedList<Process>* g_processes;
  342. template<typename Callback>
  343. inline void Process::for_each(Callback callback)
  344. {
  345. ASSERT_INTERRUPTS_DISABLED();
  346. for (auto* process = g_processes->head(); process;) {
  347. auto* next_process = process->next();
  348. if (!callback(*process))
  349. break;
  350. process = next_process;
  351. }
  352. }
  353. template<typename Callback>
  354. inline void Process::for_each_child(Callback callback)
  355. {
  356. ASSERT_INTERRUPTS_DISABLED();
  357. pid_t my_pid = pid();
  358. for (auto* process = g_processes->head(); process;) {
  359. auto* next_process = process->next();
  360. if (process->ppid() == my_pid) {
  361. if (!callback(*process))
  362. break;
  363. }
  364. process = next_process;
  365. }
  366. }
  367. template<typename Callback>
  368. inline void Process::for_each_in_pgrp(pid_t pgid, Callback callback)
  369. {
  370. ASSERT_INTERRUPTS_DISABLED();
  371. for (auto* process = g_processes->head(); process;) {
  372. auto* next_process = process->next();
  373. if (process->pgid() == pgid) {
  374. if (!callback(*process))
  375. break;
  376. }
  377. process = next_process;
  378. }
  379. }
  380. template<typename Callback>
  381. inline void Process::for_each_in_state(State state, Callback callback)
  382. {
  383. ASSERT_INTERRUPTS_DISABLED();
  384. for (auto* process = g_processes->head(); process;) {
  385. auto* next_process = process->next();
  386. if (process->state() == state)
  387. callback(*process);
  388. process = next_process;
  389. }
  390. }
  391. template<typename Callback>
  392. inline void Process::for_each_not_in_state(State state, Callback callback)
  393. {
  394. ASSERT_INTERRUPTS_DISABLED();
  395. for (auto* process = g_processes->head(); process;) {
  396. auto* next_process = process->next();
  397. if (process->state() != state)
  398. callback(*process);
  399. process = next_process;
  400. }
  401. }