sched.h 5.0 KB

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