sched.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* Scheduler core: per-CPU state, the running process, and the tick entry.
  2. *
  3. * Only CPU 0 exists today, but everything is shaped for more CPUs later:
  4. * current is per-CPU, reached through a per-CPU GS segment, and the timer
  5. * path goes through sched_tick_cpu-style code rather than a global pid index.
  6. */
  7. #ifndef SCHED_H
  8. #define SCHED_H
  9. #define MAX_CPUS 8
  10. #define SCHED_WEIGHT_DEFAULT 8
  11. #define SCHED_WEIGHT_MAX 16
  12. #define SCHED_SLICE_TICKS 10 /* PIT ticks (1 kHz) per timeslice */
  13. struct process;
  14. struct tss;
  15. struct sys_schedinfo;
  16. struct wait_queue {
  17. struct process *head;
  18. struct process *tail;
  19. };
  20. struct cpu {
  21. struct process *current; /* offset 0, read by switch.S (%gs:0) */
  22. struct process *idle;
  23. struct tss *tss; /* this CPU's TSS; schedule() updates esp0 */
  24. struct cpu *self; /* this_cpu() reads it through %gs */
  25. unsigned int id;
  26. unsigned int lapic_id;
  27. unsigned int need_resched;
  28. unsigned int preempt_count;
  29. unsigned int idle_ticks;
  30. unsigned int switches;
  31. };
  32. extern struct cpu cpus[MAX_CPUS];
  33. extern unsigned int cpu_count;
  34. extern unsigned int cpu_gs_ready;
  35. /* The GS descriptor points at this CPU's struct cpu. Before the GDT has
  36. * been loaded (early boot, or a fault before gdt_tss_init), fall back to
  37. * the BSP entry so panic/console code can still run safely. */
  38. static inline struct cpu *this_cpu(void)
  39. {
  40. struct cpu *c;
  41. if (!cpu_gs_ready)
  42. return &cpus[0];
  43. __asm__ volatile ("movl %%gs:%c1, %0"
  44. : "=r"(c)
  45. : "i"(__builtin_offsetof(struct cpu, self)));
  46. return c;
  47. }
  48. static inline struct process *current_proc(void)
  49. {
  50. return this_cpu()->current;
  51. }
  52. void sched_init(void);
  53. void schedule(void);
  54. int sched_tick_bsp(void);
  55. /* Voluntary reschedule through the int 0x81 gate. Must not be called with
  56. * preempt_count set; blocking helpers below handle that. */
  57. void sched_yield(void);
  58. /* Block the current process until its deadline, or add it to a ready queue.
  59. * Both are the entry points process.c uses. */
  60. void sched_sleep_ms(unsigned int ms);
  61. void sched_add_ready(struct process *p);
  62. void sched_unlink(struct process *p);
  63. /* Wait queues. wait_wake_* may be called from any process context; the
  64. * block helpers handle interrupts themselves. */
  65. void wait_queue_init(struct wait_queue *wq);
  66. void sched_block_on(struct wait_queue *wq);
  67. void sched_block_on_locked(struct wait_queue *wq);
  68. void sched_block_timeout(struct wait_queue *wq, unsigned int ms);
  69. void wait_wake_one(struct wait_queue *wq);
  70. void wait_wake_all(struct wait_queue *wq);
  71. /* Use with interrupts disabled. sched_set_weight_locked accounts the current
  72. * slice with the old weight before changing the stride. */
  73. void sched_set_weight_locked(struct process *p, unsigned int weight);
  74. /* Per-CPU non-preemptible sections (the FAT driver and the shadow flush).
  75. * The PIT still increments ticks and wakes sleepers, but does not switch. */
  76. void preempt_disable(void);
  77. void preempt_enable(void);
  78. /* Snapshot for SYS_SCHEDINFO and the `sched` command. */
  79. int sched_snapshot(struct sys_schedinfo *out);
  80. void sched_print(void);
  81. #endif