process.hpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #pragma once
  2. #include <kernel/interrupt.h>
  3. #include <kernel/mm.hpp>
  4. #include <kernel/task.h>
  5. #include <types/hash_map.hpp>
  6. #include <types/list.hpp>
  7. #include <types/types.h>
  8. typedef size_t pid_t;
  9. class process;
  10. struct thread;
  11. struct process_attr {
  12. uint16_t system : 1;
  13. };
  14. struct thread_attr {
  15. uint32_t system : 1;
  16. uint32_t ready : 1;
  17. uint32_t wait : 1;
  18. };
  19. struct thread {
  20. void* eip;
  21. process* owner;
  22. regs_32 regs;
  23. uint32_t eflags;
  24. thread_attr attr;
  25. };
  26. class process {
  27. public:
  28. mm_list mms;
  29. types::list<thread> thds;
  30. // TODO: allocate a kernel stack for EVERY THREAD
  31. void* k_esp;
  32. process_attr attr;
  33. pid_t pid;
  34. pid_t ppid;
  35. public:
  36. process(process&& val);
  37. process(const process&) = delete;
  38. process(const process& proc, const thread& main_thread);
  39. // only used for system initialization
  40. process(void* start_eip);
  41. };
  42. inline process* volatile current_process;
  43. inline thread* volatile current_thread;
  44. inline typename types::hash_map<pid_t, types::list<pid_t>, types::linux_hasher<pid_t>>* idx_child_processes;
  45. extern "C" void NORETURN init_scheduler();
  46. void do_scheduling(interrupt_stack* intrpt_data);
  47. void thread_context_save(interrupt_stack* int_stack, thread* thd);
  48. void thread_context_load(interrupt_stack* int_stack, thread* thd);
  49. void process_context_save(interrupt_stack*, process*);
  50. void process_context_load(interrupt_stack*, process* proc);
  51. void add_to_process_list(process&& proc);
  52. void add_to_ready_list(thread* thd);
  53. void remove_from_ready_list(thread* thd);
  54. types::list<thread*>::iterator_type query_next_thread(void);
  55. // the function call INVALIDATES iterator
  56. inline void next_task(types::list<thread*>::iterator_type target)
  57. {
  58. auto* ptr = *target;
  59. remove_from_ready_list(ptr);
  60. if (ptr->attr.ready)
  61. add_to_ready_list(ptr);
  62. }
  63. extern "C" void NORETURN to_kernel(interrupt_stack* ret_stack);
  64. extern "C" void NORETURN to_user(interrupt_stack* ret_stack);
  65. inline void NORETURN context_jump(bool system, interrupt_stack* intrpt_stack)
  66. {
  67. if (system)
  68. to_kernel(intrpt_stack);
  69. else
  70. to_user(intrpt_stack);
  71. }
  72. process* findproc(pid_t pid);
  73. void k_new_thread(void (*func)(void*), void* data);