1 #include "devices/timer.h"
6 #include "threads/interrupt.h"
7 #include "threads/io.h"
8 #include "threads/thread.h"
10 /* See [8254] for hardware details of the 8254 timer chip. */
13 #error 8254 timer requires TIMER_FREQ >= 19
16 #error TIMER_FREQ <= 1000 recommended
19 /* Number of timer ticks that a process gets before being
23 /* Number of timer ticks since OS booted. */
24 static volatile int64_t ticks;
26 /* Number of loops per timer tick.
27 Initialized by timer_calibrate(). */
28 static unsigned loops_per_tick;
30 static intr_handler_func timer_interrupt;
31 static bool too_many_loops (unsigned loops);
32 static void busy_wait (int64_t loops);
33 static void real_time_sleep (int64_t num, int32_t denom);
35 /* Sets up the 8254 Programmable Interval Timer (PIT) to
36 interrupt PIT_FREQ times per second, and registers the
37 corresponding interrupt. */
41 /* 8254 input frequency divided by TIMER_FREQ, rounded to
43 uint16_t count = (1193180 + TIMER_FREQ / 2) / TIMER_FREQ;
45 outb (0x43, 0x34); /* CW: counter 0, LSB then MSB, mode 2, binary. */
46 outb (0x40, count & 0xff);
47 outb (0x40, count >> 8);
49 intr_register (0x20, 0, INTR_OFF, timer_interrupt, "8254 Timer");
52 /* Calibrates loops_per_tick, used to implement brief delays. */
54 timer_calibrate (void)
56 unsigned high_bit, test_bit;
58 ASSERT (intr_get_level () == INTR_ON);
59 printf ("Calibrating timer... ");
61 /* Approximate loops_per_tick as the largest power-of-two
62 still less than one timer tick. */
63 loops_per_tick = 1u << 10;
64 while (!too_many_loops (loops_per_tick << 1))
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);
86 /* Returns the number of timer ticks elapsed since THEN, which
87 should be a value once returned by timer_ticks(). */
89 timer_elapsed (int64_t then)
91 return timer_ticks () - then;
94 /* Suspends execution for approximately TICKS timer ticks. */
96 timer_sleep (int64_t ticks)
98 int64_t start = timer_ticks ();
100 ASSERT (intr_get_level () == INTR_ON);
101 while (timer_elapsed (start) < ticks)
105 /* Suspends execution for approximately MS milliseconds. */
107 timer_msleep (int64_t ms)
109 real_time_sleep (ms, 1000);
112 /* Suspends execution for approximately US microseconds. */
114 timer_usleep (int64_t us)
116 real_time_sleep (us, 1000 * 1000);
119 /* Suspends execution for approximately NS nanoseconds. */
121 timer_nsleep (int64_t ns)
123 real_time_sleep (ns, 1000 * 1000 * 1000);
126 /* Prints timer statistics. */
128 timer_print_stats (void)
130 printf ("Timer: %"PRId64" ticks\n", ticks);
133 /* Timer interrupt handler. */
135 timer_interrupt (struct intr_frame *args UNUSED)
139 if (ticks % TIME_SLICE == 0)
140 intr_yield_on_return ();
143 /* Returns true if LOOPS iterations waits for more than one timer
144 tick, otherwise false. */
146 too_many_loops (unsigned loops)
150 /* Wait for a timer tick. */
152 while (ticks == start)
155 /* Run LOOPS loops. */
159 /* If the tick count changed, we iterated too long. */
160 return start != ticks;
163 /* Iterates through a simple loop LOOPS times, for implementing
166 Marked NO_INLINE because code alignment can significantly
167 affect timings, so that if this function was inlined
168 differently in different places the results would be difficult
170 static void NO_INLINE
171 busy_wait (int64_t loops)
177 /* Sleep for approximately NUM/DENOM seconds. */
179 real_time_sleep (int64_t num, int32_t denom)
181 /* Convert NUM/DENOM seconds into timer ticks, rounding down.
184 ---------------------- = NUM * TIMER_FREQ / DENOM ticks.
185 1 s / TIMER_FREQ ticks
187 int64_t ticks = num * TIMER_FREQ / denom;
189 ASSERT (intr_get_level () == INTR_ON);
192 /* We're waiting for at least one full timer tick. Use
193 timer_sleep() because it will yield the CPU to other
199 /* Otherwise, use a busy-wait loop for more accurate
200 sub-tick timing. We scale the numerator and denominator
201 down by 1000 to avoid the possibility of overflow. */
202 ASSERT (denom % 1000 == 0);
203 busy_wait (loops_per_tick * num / 1000 * TIMER_FREQ / (denom / 1000));