process.h 6.5 KB

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