process.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. };
  72. /* User-mode virtual address layout (ring-3 processes). */
  73. #define USER_BASE 0x40000000 /* user program link/load address */
  74. #define USER_STACK_TOP 0x40400000 /* user stack top (grows down) */
  75. #define USER_STACK_PAGES 32 /* user stack size in 4 KB pages (128 KB) */
  76. /* Where a canvas window's back buffer is mapped. The mapping is released by
  77. * the reaper without freeing the canvas pages: the window owns those. */
  78. #define USER_WIN_BASE 0x50000000
  79. #define USER_WIN_RANGE 0x00400000 /* one 4 MB slot */
  80. /* Anonymous per-process heap (SYS_MEM_ALLOC/GROW/FREE): one growable arena
  81. * above the WM mappings. Freed by the reaper. */
  82. #define USER_HEAP_BASE 0x60000000u
  83. #define USER_HEAP_MAX (32u * 1024u * 1024u)
  84. /* Stable, sparse process table (see process.c). */
  85. extern struct process *procs[PROC_MAX];
  86. void process_init(void);
  87. /* Prepare an AP idle PCB and stack; returns its kernel stack top, or 0. */
  88. unsigned int process_init_ap(unsigned int cpu);
  89. int process_create(const char *name, void (*entry)(void));
  90. int process_create_arg(const char *name, void (*entry)(const char *),
  91. const char *arg);
  92. int process_create_ptr(const char *name, void (*entry)(void *), void *arg);
  93. /* Load an ELF image into a fresh address space and run it as a ring-3
  94. * process, with `arg` (may be NULL) as the first C parameter of _start.
  95. * Returns the pid, -1 when out of resources, or -2 when the image is not a
  96. * loadable ELF. */
  97. int process_create_user(const char *name, const void *image, unsigned int size,
  98. const char *arg, const char *cwd);
  99. /* Same, but with an explicit tty id (0 = inherit the caller's tty). */
  100. int process_create_user_tty(const char *name, const void *image,
  101. unsigned int size, const char *arg, const char *cwd,
  102. unsigned int tty);
  103. /* Render target override for the running process (0 = the screen). */
  104. struct surface *process_render_target(void);
  105. void process_set_render_target(struct surface *s);
  106. /* Map an MMIO range into the kernel's page directory and every live process
  107. * copy (and thus every process created later, which clones the kernel map).
  108. * Drivers that map their register window at command time must use this: a
  109. * process runs with its own page-directory copy, so paging_map_mmio() alone
  110. * would only be visible to the kernel bootstrap context. */
  111. void process_map_mmio_all(unsigned int phys_base, unsigned int size);
  112. void process_exit(void);
  113. /* Called by the kworker: free switched-out dead processes. */
  114. void process_reap_dead(void);
  115. unsigned int process_zombie_count(void);
  116. /* Caller holds sched_lock. */
  117. unsigned int process_zombie_count_locked(void);
  118. /* Called from schedule()/kill with interrupts disabled: unpublish a dead PCB
  119. * and put it on the kworker's zombie list. */
  120. void process_zombie_add(struct process *p);
  121. void process_ps(void);
  122. /* Fill *out with a snapshot of the index-th live process (slot order) for
  123. * the `top` command. Returns 1 when found, 0 at the end of the table. */
  124. int process_procinfo(unsigned int index, struct sys_procinfo *out);
  125. int process_kill(unsigned int pid);
  126. int process_is_alive(unsigned int pid);
  127. /* 1 while the process is in PROC_SLEEPING (the WM uses this to tell when it
  128. * is safe to reap another window's canvas). */
  129. int process_is_sleeping(unsigned int pid);
  130. void process_set_tty(unsigned int pid, int tty);
  131. void process_wait(unsigned int pid);
  132. /* Adjust the CFS weight of a live process (1..SCHED_WEIGHT_MAX). */
  133. int process_set_weight(unsigned int pid, unsigned int weight);
  134. /* Restrict a live process to a CPU bitmask (0 is ignored). */
  135. int process_set_affinity(unsigned int pid, unsigned int mask);
  136. /* Block the current process for `ms` milliseconds (yields the CPU). */
  137. void process_sleep_ms(unsigned int ms);
  138. #endif