sched.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* Scheduler core: per-CPU state, the running process, and the tick entries.
  2. *
  3. * Every online CPU has its own current/idle/TSS via a per-CPU GS segment.
  4. * The ready/sleep/wait queues are global and protected by sched_lock; the
  5. * BSP PIT is the global clock, while APs run their own LAPIC timer.
  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. /* Dead process this CPU just switched away from. It is queued only
  32. * after the new stack is live (sched_finish_switch), so the reaper can
  33. * never free a stack or page directory still in use. */
  34. struct process *pending_zombie;
  35. /* schedule() keeps the scheduler lock until switch.S has loaded the
  36. * new stack; sched_finish_switch() then drops it. Without this the
  37. * old process could be picked by another CPU while this CPU still runs
  38. * on its kernel stack. */
  39. unsigned int deferred_unlock;
  40. unsigned int sched_flags;
  41. };
  42. extern struct cpu cpus[MAX_CPUS];
  43. extern unsigned int cpu_count;
  44. extern unsigned int cpu_gs_ready;
  45. /* The GS descriptor points at this CPU's struct cpu. Before the GDT has
  46. * been loaded (early boot, or a fault before gdt_tss_init), fall back to
  47. * the BSP entry so panic/console code can still run safely. */
  48. static inline struct cpu *this_cpu(void)
  49. {
  50. struct cpu *c;
  51. if (!cpu_gs_ready)
  52. return &cpus[0];
  53. __asm__ volatile ("movl %%gs:%c1, %0"
  54. : "=r"(c)
  55. : "i"(__builtin_offsetof(struct cpu, self)));
  56. return c;
  57. }
  58. static inline struct process *current_proc(void)
  59. {
  60. return this_cpu()->current;
  61. }
  62. void sched_init(void);
  63. void schedule(void);
  64. int sched_tick_bsp(void);
  65. int sched_tick_ap(void);
  66. /* Queue the previous process after the assembly stub has loaded the new
  67. * kernel stack (called from switch.S). */
  68. void sched_finish_switch(void);
  69. /* The global ready/sleep/wait/process locks. Callers get the old interrupt
  70. * state back and must pass it to sched_lock_release(). */
  71. unsigned int sched_lock_acquire(void);
  72. void sched_lock_release(unsigned int flags);
  73. /* Reinitialize the lock after the APs have been parked (an INIT can stop an
  74. * AP while it holds the lock; the old run queue no longer matters). */
  75. void sched_lock_reset(void);
  76. /* Big kernel lock: a sleeping mutex serializing kernel execution. Held for
  77. * the duration of a syscall or kernel-thread work item, dropped around
  78. * voluntary blocking so the other CPU can make progress. */
  79. void bkl_init(void);
  80. void bkl_lock(void);
  81. void bkl_unlock(void);
  82. int bkl_trylock(void);
  83. /* Drop/reacquire the BKL around a long wait that does not touch kernel
  84. * state (serial receive, device polling). */
  85. void bkl_pause(void);
  86. void bkl_resume(void);
  87. /* Voluntary reschedule through the int 0x81 gate. Must not be called with
  88. * preempt_count set; blocking helpers below handle that. */
  89. void sched_yield(void);
  90. /* Block the current process until its deadline, or add it to a ready queue.
  91. * Both are the entry points process.c uses. */
  92. void sched_sleep_ms(unsigned int ms);
  93. void sched_add_ready(struct process *p);
  94. void sched_unlink(struct process *p);
  95. /* Caller holds sched_lock. */
  96. void sched_unlink_locked(struct process *p);
  97. /* Wait queues. wait_wake_* may be called from any process context; the
  98. * block helpers handle interrupts themselves. */
  99. void wait_queue_init(struct wait_queue *wq);
  100. void sched_block_on(struct wait_queue *wq);
  101. void sched_block_on_locked(struct wait_queue *wq);
  102. void sched_block_timeout(struct wait_queue *wq, unsigned int ms);
  103. void wait_wake_one(struct wait_queue *wq);
  104. void wait_wake_all(struct wait_queue *wq);
  105. /* Use with interrupts disabled. sched_set_weight_locked accounts the current
  106. * slice with the old weight before changing the stride. */
  107. void sched_set_weight_locked(struct process *p, unsigned int weight);
  108. /* Per-CPU non-preemptible sections (the FAT driver and the shadow flush).
  109. * The PIT still increments ticks and wakes sleepers, but does not switch. */
  110. void preempt_disable(void);
  111. void preempt_enable(void);
  112. /* Snapshot for SYS_SCHEDINFO and the `sched` command. */
  113. int sched_snapshot(struct sys_schedinfo *out);
  114. void sched_print(void);
  115. #endif