process.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /* Preemptive round-robin multitasking for i586, ring-0 only. */
  2. #ifndef PROCESS_H
  3. #define PROCESS_H
  4. #define PROC_STACK_SIZE 0x1000 /* 4 KB per process (= 1 page) */
  5. /* Process stacks are allocated from the frame allocator (mm.c), i.e. above the
  6. * VGA shadow buffer. If a page is unavailable we fall back to a small static
  7. * pool in BSS so the system still boots with fewer processes. */
  8. #define PROC_FALLBACK_STACK_MAX 8
  9. #define PROC_UNUSED 0
  10. #define PROC_READY 1
  11. #define PROC_RUNNING 2
  12. #define PROC_DEAD 3
  13. #define PROC_SLEEPING 4
  14. #define TIMESLICE_TICKS 20 /* context switch every 20 ms (PIT @ 1 kHz) */
  15. #define PROC_NAME_LEN 12
  16. #define PROC_CWD_MAX 64
  17. struct text_screen; /* forward decl (see screen.h) */
  18. struct sys_procinfo; /* forward decl (see syscall.h; `top` snapshot) */
  19. struct surface; /* forward decl (see vga.h; render target override) */
  20. struct process {
  21. unsigned int esp;
  22. unsigned int state;
  23. unsigned int pid;
  24. char name[PROC_NAME_LEN];
  25. struct text_screen *screen; /* output target for io_putchar & co. */
  26. unsigned int stack_base; /* page to free on exit, or 0 (bootstrap/fallback) */
  27. unsigned int cr3; /* physical address of this process's page dir */
  28. unsigned int kstack_top; /* ring-0 stack top (for TSS esp0) */
  29. unsigned int wake_ms; /* tick deadline when PROC_SLEEPING */
  30. char cwd[PROC_CWD_MAX]; /* working directory (ring-3 commands) */
  31. /* CPU accounting for `top`. switch.S credits the running process on
  32. * every PIT tick, so the layout after cwd must match PCB_CPU_TICKS /
  33. * PCB_SIZE there (see the _Static_assert in process.c). */
  34. unsigned int cpu_ticks; /* ticks spent as the running process */
  35. unsigned int start_ticks; /* get_ticks_ms() at creation */
  36. /* Render target override: 0 = the screen. Set while the window manager
  37. * draws into a layer, so another process's drawing can never land in
  38. * it. Kept before tty so the PIT hot path offsets stay put. */
  39. struct surface *target;
  40. /* TTY output id inherited by children (0 = none, use the old screen). */
  41. int tty;
  42. };
  43. /* User-mode virtual address layout (ring-3 processes). */
  44. #define USER_BASE 0x40000000 /* user program link/load address */
  45. #define USER_STACK_TOP 0x40200000 /* user stack top (grows down) */
  46. #define USER_STACK_PAGES 2 /* user stack size in 4 KB pages */
  47. /* Where sys_gfx maps a command's canvas back buffer (see sys_gfx.c). The
  48. * mapping is released by the reaper without freeing the canvas pages: the
  49. * gfx session owns those. */
  50. #define USER_GFX_BASE 0x50000000
  51. #define USER_GFX_RANGE 0x00400000 /* one 4 MB slot */
  52. /* Dynamically-sized process table (grown on demand; see process.c). */
  53. extern struct process *procs;
  54. extern volatile unsigned int current_pid;
  55. void process_init(void);
  56. int process_create(const char *name, void (*entry)(void));
  57. int process_create_arg(const char *name, void (*entry)(const char *),
  58. const char *arg);
  59. int process_create_ptr(const char *name, void (*entry)(void *), void *arg);
  60. /* Load an ELF image into a fresh address space and run it as a ring-3
  61. * process, with `arg` (may be NULL) as the first C parameter of _start.
  62. * Returns the pid, -1 when out of resources, or -2 when the image is not a
  63. * loadable ELF. */
  64. int process_create_user(const char *name, const void *image, unsigned int size,
  65. const char *arg, const char *cwd);
  66. /* Same, but with an explicit tty id (0 = inherit the caller's tty). */
  67. int process_create_user_tty(const char *name, const void *image,
  68. unsigned int size, const char *arg, const char *cwd,
  69. unsigned int tty);
  70. void process_set_screen(struct text_screen *screen);
  71. /* Render target override for the running process (0 = the screen). */
  72. struct surface *process_render_target(void);
  73. void process_set_render_target(struct surface *s);
  74. /* Map an MMIO range into the kernel's page directory and every live process
  75. * copy (and thus every process created later, which clones the kernel map).
  76. * Drivers that map their register window at command time must use this: a
  77. * process runs with its own page-directory copy, so paging_map_mmio() alone
  78. * would only be visible to the kernel bootstrap context. */
  79. void process_map_mmio_all(unsigned int phys_base, unsigned int size);
  80. void process_exit(void);
  81. void schedule(void);
  82. void process_ps(void);
  83. /* Fill *out with a snapshot of the index-th live process (slot order) for
  84. * the `top` command. Returns 1 when found, 0 at the end of the table. */
  85. int process_procinfo(unsigned int index, struct sys_procinfo *out);
  86. int process_kill(unsigned int pid);
  87. int process_is_alive(unsigned int pid);
  88. /* 1 while the process is in PROC_SLEEPING (the WM uses this to tell when it
  89. * is safe to reap another window's canvas). */
  90. int process_is_sleeping(unsigned int pid);
  91. void process_set_tty(unsigned int pid, int tty);
  92. void process_wait(unsigned int pid);
  93. /* Block the current process for `ms` milliseconds (yields the CPU). */
  94. void process_sleep_ms(unsigned int ms);
  95. #endif