process.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /* Process objects and lifecycle. Scheduling itself lives in sched.c. */
  2. #ifndef PROCESS_H
  3. #define PROCESS_H
  4. #include "sched.h"
  5. #define PROC_STACK_SIZE 0x1000 /* 4 KB per process (= 1 page) */
  6. /* Process stacks are allocated from the frame allocator (mm.c), i.e. above the
  7. * VGA shadow buffer. If a page is unavailable we fall back to a small static
  8. * pool in BSS so the system still boots with fewer processes. */
  9. #define PROC_FALLBACK_STACK_MAX 8
  10. /* Stable process table: slots hold pointers to individually allocated PCBs, so
  11. * a process pointer never moves. Keep the table small; PCBs are heap objects. */
  12. #define PROC_MAX 64
  13. #define PROC_UNUSED 0
  14. #define PROC_READY 1
  15. #define PROC_RUNNING 2
  16. #define PROC_DEAD 3
  17. #define PROC_SLEEPING 4
  18. #define PROC_BLOCKED 5
  19. /* on_cpu when the process is not currently running anywhere. */
  20. #define PROC_NO_CPU 0xFFFFFFFFu
  21. #define TIMESLICE_TICKS 20 /* context switch every 20 ms (PIT @ 1 kHz) */
  22. #define PROC_NAME_LEN 12
  23. #define PROC_CWD_MAX 64
  24. struct sys_procinfo; /* forward decl (see syscall.h; `top` snapshot) */
  25. struct surface; /* forward decl (see vga.h; render target override) */
  26. struct process {
  27. unsigned int esp; /* offset 0: switch.S saves it here */
  28. unsigned int state;
  29. unsigned int pid;
  30. unsigned int slot; /* index in procs[], PROC_MAX for idle */
  31. char name[PROC_NAME_LEN];
  32. unsigned int stack_base; /* page to free on exit, or 0 (bootstrap/fallback) */
  33. unsigned int cr3; /* physical address of this process's page dir */
  34. unsigned int heap_pages; /* pages mapped at USER_HEAP_BASE, or 0 */
  35. unsigned int kstack_top; /* ring-0 stack top (for TSS esp0) */
  36. unsigned int wake_ms; /* tick deadline when PROC_SLEEPING */
  37. char cwd[PROC_CWD_MAX]; /* working directory (ring-3 commands) */
  38. /* CPU accounting for `top`. */
  39. unsigned int cpu_ticks; /* ticks spent as the running process */
  40. unsigned int start_ticks; /* get_ticks_ms() at creation */
  41. /* Render target override: 0 = the screen. Set while the window manager
  42. * draws into a layer, so another process's drawing can never land in
  43. * it. */
  44. struct surface *target;
  45. /* TTY output id inherited by children (0 = none, use the old screen). */
  46. int tty;
  47. /* Per-CPU scaffolding: these are maintained even with one CPU so the
  48. * scheduler can be extended without reshaping the PCB. */
  49. unsigned int on_cpu; /* PROC_NO_CPU when not running */
  50. unsigned int last_cpu;
  51. unsigned int affinity; /* CPU bitmask; all CPUs by default */
  52. /* CFS-lite scheduling entity. vruntime/stride are 64-bit; no 64-bit
  53. * division happens on the tick path (stride is computed in set_weight). */
  54. unsigned int weight; /* 1..SCHED_WEIGHT_MAX */
  55. unsigned int on_runq; /* linked in the ready queue */
  56. unsigned long long vruntime;
  57. unsigned long long stride;
  58. unsigned int slice_start; /* tick when the current slice began */
  59. struct process *run_next, *run_prev;
  60. struct process *sleep_next, *sleep_prev;
  61. struct process *wait_next, *wait_prev;
  62. struct wait_queue *waiting_on;
  63. struct process *zombie_next; /* reaped by the kworker */
  64. unsigned int zombie_queued;
  65. /* Kernel-thread entry: set for processes created with
  66. * process_create*(), whose wrapper takes the BKL. */
  67. void (*kentry)(void);
  68. unsigned int kernel_thread;
  69. /* BKL depth saved when the process blocks and must reacquire it. */
  70. unsigned int bkl_saved;
  71. /* x87 state (FNSAVE image) while switched out; fpu_valid is 0 until
  72. * the first switch-out, and such a process starts from FNINIT. */
  73. unsigned char fpu[108];
  74. unsigned int fpu_valid;
  75. };
  76. /* User-mode virtual address layout (ring-3 processes). */
  77. #define USER_BASE 0x40000000 /* user program link/load address */
  78. #define USER_STACK_TOP 0x40400000 /* user stack top (grows down) */
  79. #define USER_STACK_PAGES 32 /* user stack size in 4 KB pages (128 KB) */
  80. /* Where a canvas window's back buffer is mapped. The mapping is released by
  81. * the reaper without freeing the canvas pages: the window owns those. */
  82. #define USER_WIN_BASE 0x50000000
  83. #define USER_WIN_RANGE 0x00400000 /* one 4 MB slot */
  84. /* Anonymous per-process heap (SYS_MEM_ALLOC/GROW/FREE): one growable arena
  85. * above the WM mappings. Freed by the reaper. */
  86. #define USER_HEAP_BASE 0x60000000u
  87. #define USER_HEAP_MAX (64u * 1024u * 1024u)
  88. /* Stable, sparse process table (see process.c). */
  89. extern struct process *procs[PROC_MAX];
  90. void process_init(void);
  91. /* Prepare an AP idle PCB and stack; returns its kernel stack top, or 0. */
  92. unsigned int process_init_ap(unsigned int cpu);
  93. int process_create(const char *name, void (*entry)(void));
  94. int process_create_arg(const char *name, void (*entry)(const char *),
  95. const char *arg);
  96. int process_create_ptr(const char *name, void (*entry)(void *), void *arg);
  97. /* Load an ELF image into a fresh address space and run it as a ring-3
  98. * process, with `arg` (may be NULL) as the first C parameter of _start.
  99. * Returns the pid, -1 when out of resources, or -2 when the image is not a
  100. * loadable ELF. */
  101. int process_create_user(const char *name, const void *image, unsigned int size,
  102. const char *arg, const char *cwd);
  103. /* Same, but with an explicit tty id (0 = inherit the caller's tty). */
  104. int process_create_user_tty(const char *name, const void *image,
  105. unsigned int size, const char *arg, const char *cwd,
  106. unsigned int tty);
  107. /* Render target override for the running process (0 = the screen). */
  108. struct surface *process_render_target(void);
  109. void process_set_render_target(struct surface *s);
  110. /* Copy kernel page-directory entry `index` into every live process
  111. * directory. paging.c calls it when ioremap() adds a window page table or
  112. * makes a lowmem PDE uncacheable; processes created later clone it. */
  113. void process_sync_kernel_pde(unsigned int index);
  114. void process_exit(void);
  115. /* Called by the kworker: free switched-out dead processes. */
  116. void process_reap_dead(void);
  117. unsigned int process_zombie_count(void);
  118. /* Caller holds sched_lock. */
  119. unsigned int process_zombie_count_locked(void);
  120. /* Called from schedule()/kill with interrupts disabled: unpublish a dead PCB
  121. * and put it on the kworker's zombie list. */
  122. void process_zombie_add(struct process *p);
  123. void process_ps(void);
  124. /* Fill *out with a snapshot of the index-th live process (slot order) for
  125. * the `top` command. Returns 1 when found, 0 at the end of the table. */
  126. int process_procinfo(unsigned int index, struct sys_procinfo *out);
  127. int process_kill(unsigned int pid);
  128. int process_is_alive(unsigned int pid);
  129. /* 1 while the process is in PROC_SLEEPING (the WM uses this to tell when it
  130. * is safe to reap another window's canvas). */
  131. int process_is_sleeping(unsigned int pid);
  132. void process_set_tty(unsigned int pid, int tty);
  133. void process_wait(unsigned int pid);
  134. /* Adjust the CFS weight of a live process (1..SCHED_WEIGHT_MAX). */
  135. int process_set_weight(unsigned int pid, unsigned int weight);
  136. /* Restrict a live process to a CPU bitmask (0 is ignored). */
  137. int process_set_affinity(unsigned int pid, unsigned int mask);
  138. /* Block the current process for `ms` milliseconds (yields the CPU). */
  139. void process_sleep_ms(unsigned int ms);
  140. #endif