process.h 7.0 KB

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