process.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 text_screen; /* forward decl (see screen.h) */
  25. struct sys_procinfo; /* forward decl (see syscall.h; `top` snapshot) */
  26. struct surface; /* forward decl (see vga.h; render target override) */
  27. struct process {
  28. unsigned int esp; /* offset 0: switch.S saves it here */
  29. unsigned int state;
  30. unsigned int pid;
  31. unsigned int slot; /* index in procs[], PROC_MAX for idle */
  32. char name[PROC_NAME_LEN];
  33. struct text_screen *screen; /* output target for io_putchar & co. */
  34. unsigned int stack_base; /* page to free on exit, or 0 (bootstrap/fallback) */
  35. unsigned int cr3; /* physical address of this process's page dir */
  36. unsigned int heap_pages; /* pages mapped at USER_HEAP_BASE, or 0 */
  37. unsigned int kstack_top; /* ring-0 stack top (for TSS esp0) */
  38. unsigned int wake_ms; /* tick deadline when PROC_SLEEPING */
  39. char cwd[PROC_CWD_MAX]; /* working directory (ring-3 commands) */
  40. /* CPU accounting for `top`. */
  41. unsigned int cpu_ticks; /* ticks spent as the running process */
  42. unsigned int start_ticks; /* get_ticks_ms() at creation */
  43. /* Render target override: 0 = the screen. Set while the window manager
  44. * draws into a layer, so another process's drawing can never land in
  45. * it. */
  46. struct surface *target;
  47. /* TTY output id inherited by children (0 = none, use the old screen). */
  48. int tty;
  49. /* Per-CPU scaffolding: these are maintained even with one CPU so the
  50. * scheduler can be extended without reshaping the PCB. */
  51. unsigned int on_cpu; /* PROC_NO_CPU when not running */
  52. unsigned int last_cpu;
  53. unsigned int affinity; /* CPU bitmask; all CPUs by default */
  54. /* CFS-lite scheduling entity. vruntime/stride are 64-bit; no 64-bit
  55. * division happens on the tick path (stride is computed in set_weight). */
  56. unsigned int weight; /* 1..SCHED_WEIGHT_MAX */
  57. unsigned int on_runq; /* linked in the ready queue */
  58. unsigned long long vruntime;
  59. unsigned long long stride;
  60. unsigned int slice_start; /* tick when the current slice began */
  61. struct process *run_next, *run_prev;
  62. struct process *sleep_next, *sleep_prev;
  63. struct process *wait_next, *wait_prev;
  64. struct wait_queue *waiting_on;
  65. struct process *zombie_next; /* reaped by the kworker */
  66. unsigned int zombie_queued;
  67. /* Kernel-thread entry: set for processes created with
  68. * process_create*(), whose wrapper takes the BKL. */
  69. void (*kentry)(void);
  70. unsigned int kernel_thread;
  71. /* BKL depth saved when the process blocks and must reacquire it. */
  72. unsigned int bkl_saved;
  73. };
  74. /* User-mode virtual address layout (ring-3 processes). */
  75. #define USER_BASE 0x40000000 /* user program link/load address */
  76. #define USER_STACK_TOP 0x40200000 /* user stack top (grows down) */
  77. #define USER_STACK_PAGES 2 /* user stack size in 4 KB pages */
  78. /* Where sys_gfx maps a command's canvas back buffer (see sys_gfx.c). The
  79. * mapping is released by the reaper without freeing the canvas pages: the
  80. * gfx session owns those. */
  81. #define USER_GFX_BASE 0x50000000
  82. #define USER_GFX_RANGE 0x00400000 /* one 4 MB slot */
  83. /* Anonymous per-process heap (SYS_MEM_ALLOC/GROW/FREE): one growable arena
  84. * above the WM mappings. Freed by the reaper. */
  85. #define USER_HEAP_BASE 0x60000000u
  86. #define USER_HEAP_MAX (32u * 1024u * 1024u)
  87. /* Stable, sparse process table (see process.c). */
  88. extern struct process *procs[PROC_MAX];
  89. void process_init(void);
  90. /* Prepare an AP idle PCB and stack; returns its kernel stack top, or 0. */
  91. unsigned int process_init_ap(unsigned int cpu);
  92. int process_create(const char *name, void (*entry)(void));
  93. int process_create_arg(const char *name, void (*entry)(const char *),
  94. const char *arg);
  95. int process_create_ptr(const char *name, void (*entry)(void *), void *arg);
  96. /* Load an ELF image into a fresh address space and run it as a ring-3
  97. * process, with `arg` (may be NULL) as the first C parameter of _start.
  98. * Returns the pid, -1 when out of resources, or -2 when the image is not a
  99. * loadable ELF. */
  100. int process_create_user(const char *name, const void *image, unsigned int size,
  101. const char *arg, const char *cwd);
  102. /* Same, but with an explicit tty id (0 = inherit the caller's tty). */
  103. int process_create_user_tty(const char *name, const void *image,
  104. unsigned int size, const char *arg, const char *cwd,
  105. unsigned int tty);
  106. void process_set_screen(struct text_screen *screen);
  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. /* Map an MMIO range into the kernel's page directory and every live process
  111. * copy (and thus every process created later, which clones the kernel map).
  112. * Drivers that map their register window at command time must use this: a
  113. * process runs with its own page-directory copy, so paging_map_mmio() alone
  114. * would only be visible to the kernel bootstrap context. */
  115. void process_map_mmio_all(unsigned int phys_base, unsigned int size);
  116. void process_exit(void);
  117. /* Called by the kworker: free switched-out dead processes. */
  118. void process_reap_dead(void);
  119. unsigned int process_zombie_count(void);
  120. /* Caller holds sched_lock. */
  121. unsigned int process_zombie_count_locked(void);
  122. /* Called from schedule()/kill with interrupts disabled: unpublish a dead PCB
  123. * and put it on the kworker's zombie list. */
  124. void process_zombie_add(struct process *p);
  125. void process_ps(void);
  126. /* Fill *out with a snapshot of the index-th live process (slot order) for
  127. * the `top` command. Returns 1 when found, 0 at the end of the table. */
  128. int process_procinfo(unsigned int index, struct sys_procinfo *out);
  129. int process_kill(unsigned int pid);
  130. int process_is_alive(unsigned int pid);
  131. /* 1 while the process is in PROC_SLEEPING (the WM uses this to tell when it
  132. * is safe to reap another window's canvas). */
  133. int process_is_sleeping(unsigned int pid);
  134. void process_set_tty(unsigned int pid, int tty);
  135. void process_wait(unsigned int pid);
  136. /* Adjust the CFS weight of a live process (1..SCHED_WEIGHT_MAX). */
  137. int process_set_weight(unsigned int pid, unsigned int weight);
  138. /* Restrict a live process to a CPU bitmask (0 is ignored). */
  139. int process_set_affinity(unsigned int pid, unsigned int mask);
  140. /* Block the current process for `ms` milliseconds (yields the CPU). */
  141. void process_sleep_ms(unsigned int ms);
  142. #endif