1 #include "devices/timer.h"
6 #include "threads/interrupt.h"
7 #include "threads/io.h"
8 #include "threads/synch.h"
9 #include "threads/thread.h"
11 /* See [8254] for hardware details of the 8254 timer chip. */
14 #error 8254 timer requires TIMER_FREQ >= 19
17 #error TIMER_FREQ <= 1000 recommended
20 /* Number of timer ticks since OS booted. */
23 /* Number of loops per timer tick.
24 Initialized by timer_calibrate(). */
25 static unsigned loops_per_tick;
27 static intr_handler_func timer_interrupt;
28 static bool too_many_loops (unsigned loops);
29 static void busy_wait (int64_t loops);
30 static void real_time_sleep (int64_t num, int32_t denom);
32 /* Sets up the 8254 Programmable Interval Timer (PIT) to
33 interrupt PIT_FREQ times per second, and registers the
34 corresponding interrupt. */
38 /* 8254 input frequency divided by TIMER_FREQ, rounded to
40 uint16_t count = (1193180 + TIMER_FREQ / 2) / TIMER_FREQ;
42 outb (0x43, 0x34); /* CW: counter 0, LSB then MSB, mode 2, binary. */
43 outb (0x40, count & 0xff);
44 outb (0x40, count >> 8);
46 intr_register_ext (0x20, timer_interrupt, "8254 Timer");
49 /* Calibrates loops_per_tick, used to implement brief delays. */
51 timer_calibrate (void)
53 unsigned high_bit, test_bit;
55 ASSERT (intr_get_level () == INTR_ON);
56 printf ("Calibrating timer... ");
58 /* Approximate loops_per_tick as the largest power-of-two
59 still less than one timer tick. */
60 loops_per_tick = 1u << 10;
61 while (!too_many_loops (loops_per_tick << 1))
64 ASSERT (loops_per_tick != 0);
67 /* Refine the next 8 bits of loops_per_tick. */
68 high_bit = loops_per_tick;
69 for (test_bit = high_bit >> 1; test_bit != high_bit >> 10; test_bit >>= 1)
70 if (!too_many_loops (high_bit | test_bit))
71 loops_per_tick |= test_bit;
73 printf ("%'"PRIu64" loops/s.\n", (uint64_t) loops_per_tick * TIMER_FREQ);
76 /* Returns the number of timer ticks since the OS booted. */
80 enum intr_level old_level = intr_disable ();
82 intr_set_level (old_level);
87 /* Returns the number of timer ticks elapsed since THEN, which
88 should be a value once returned by timer_ticks(). */
90 timer_elapsed (int64_t then)
92 return timer_ticks () - then;
95 /* Suspends execution for approximately TICKS timer ticks. */
97 timer_sleep (int64_t ticks)
99 int64_t start = timer_ticks ();
101 ASSERT (intr_get_level () == INTR_ON);
102 while (timer_elapsed (start) < ticks)
106 /* Suspends execution for approximately MS milliseconds. */
108 timer_msleep (int64_t ms)
110 real_time_sleep (ms, 1000);
113 /* Suspends execution for approximately US microseconds. */
115 timer_usleep (int64_t us)
117 real_time_sleep (us, 1000 * 1000);
120 /* Suspends execution for approximately NS nanoseconds. */
122 timer_nsleep (int64_t ns)
124 real_time_sleep (ns, 1000 * 1000 * 1000);
127 /* Prints timer statistics. */
129 timer_print_stats (void)
131 printf ("Timer: %"PRId64" ticks\n", timer_ticks ());
134 /* Timer interrupt handler. */
136 timer_interrupt (struct intr_frame *args UNUSED)
142 /* Returns true if LOOPS iterations waits for more than one timer
143 tick, otherwise false. */
145 too_many_loops (unsigned loops)
147 /* Wait for a timer tick. */
148 int64_t start = ticks;
149 while (ticks == start)
152 /* Run LOOPS loops. */
156 /* If the tick count changed, we iterated too long. */
158 return start != ticks;
161 /* Iterates through a simple loop LOOPS times, for implementing
164 Marked NO_INLINE because code alignment can significantly
165 affect timings, so that if this function was inlined
166 differently in different places the results would be difficult
168 static void NO_INLINE
169 busy_wait (int64_t loops)
175 /* Sleep for approximately NUM/DENOM seconds. */
177 real_time_sleep (int64_t num, int32_t denom)
179 /* Convert NUM/DENOM seconds into timer ticks, rounding down.
182 ---------------------- = NUM * TIMER_FREQ / DENOM ticks.
183 1 s / TIMER_FREQ ticks
185 int64_t ticks = num * TIMER_FREQ / denom;
187 ASSERT (intr_get_level () == INTR_ON);
190 /* We're waiting for at least one full timer tick. Use
191 timer_sleep() because it will yield the CPU to other
197 /* Otherwise, use a busy-wait loop for more accurate
198 sub-tick timing. We scale the numerator and denominator
199 down by 1000 to avoid the possibility of overflow. */
200 ASSERT (denom % 1000 == 0);
201 busy_wait (loops_per_tick * num / 1000 * TIMER_FREQ / (denom / 1000));