process.h 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 last so the PIT hot path offsets stay put. */
  39. struct surface *target;
  40. };
  41. /* User-mode virtual address layout (ring-3 processes). */
  42. #define USER_BASE 0x40000000 /* user program link/load address */
  43. #define USER_STACK_TOP 0x40200000 /* user stack top (grows down) */
  44. #define USER_STACK_PAGES 2 /* user stack size in 4 KB pages */
  45. /* Dynamically-sized process table (grown on demand; see process.c). */
  46. extern struct process *procs;
  47. extern volatile unsigned int current_pid;
  48. void process_init(void);
  49. int process_create(const char *name, void (*entry)(void));
  50. int process_create_arg(const char *name, void (*entry)(const char *),
  51. const char *arg);
  52. int process_create_ptr(const char *name, void (*entry)(void *), void *arg);
  53. /* Load an ELF image into a fresh address space and run it as a ring-3
  54. * process, with `arg` (may be NULL) as the first C parameter of _start.
  55. * Returns the pid, -1 when out of resources, or -2 when the image is not a
  56. * loadable ELF. */
  57. int process_create_user(const char *name, const void *image, unsigned int size,
  58. const char *arg, const char *cwd);
  59. void process_set_screen(struct text_screen *screen);
  60. /* Render target override for the running process (0 = the screen). */
  61. struct surface *process_render_target(void);
  62. void process_set_render_target(struct surface *s);
  63. /* Map an MMIO range into the kernel's page directory and every live process
  64. * copy (and thus every process created later, which clones the kernel map).
  65. * Drivers that map their register window at command time must use this: a
  66. * process runs with its own page-directory copy, so paging_map_mmio() alone
  67. * would only be visible to the kernel bootstrap context. */
  68. void process_map_mmio_all(unsigned int phys_base, unsigned int size);
  69. void process_exit(void);
  70. void schedule(void);
  71. void process_ps(void);
  72. /* Fill *out with a snapshot of the index-th live process (slot order) for
  73. * the `top` command. Returns 1 when found, 0 at the end of the table. */
  74. int process_procinfo(unsigned int index, struct sys_procinfo *out);
  75. int process_kill(unsigned int pid);
  76. int process_is_alive(unsigned int pid);
  77. void process_wait(unsigned int pid);
  78. /* Block the current process for `ms` milliseconds (yields the CPU). */
  79. void process_sleep_ms(unsigned int ms);
  80. #endif