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 ts
)
99 int64_t deadline
= ts
+ timer_ticks();
100 thread_wait(thread_current(),deadline
);
102 while(deadline
> timer_ticks()){
103 thread_wait(thread_current(),deadline
);
109 /* Suspends execution for approximately MS milliseconds. */
111 timer_msleep (int64_t ms
)
113 real_time_sleep (ms
, 1000);
116 /* Suspends execution for approximately US microseconds. */
118 timer_usleep (int64_t us
)
120 real_time_sleep (us
, 1000 * 1000);
123 /* Suspends execution for approximately NS nanoseconds. */
125 timer_nsleep (int64_t ns
)
127 real_time_sleep (ns
, 1000 * 1000 * 1000);
130 /* Prints timer statistics. */
132 timer_print_stats (void)
134 printf ("Timer: %"PRId64
" ticks\n", timer_ticks ());
137 /* Timer interrupt handler. */
139 timer_interrupt (struct intr_frame
*args UNUSED
)
145 /* Returns true if LOOPS iterations waits for more than one timer
146 tick, otherwise false. */
148 too_many_loops (unsigned loops
)
150 /* Wait for a timer tick. */
151 int64_t start
= ticks
;
152 while (ticks
== start
)
155 /* Run LOOPS loops. */
159 /* If the tick count changed, we iterated too long. */
161 return start
!= ticks
;
164 /* Iterates through a simple loop LOOPS times, for implementing
167 Marked NO_INLINE because code alignment can significantly
168 affect timings, so that if this function was inlined
169 differently in different places the results would be difficult
171 static void NO_INLINE
172 busy_wait (int64_t loops
)
178 /* Sleep for approximately NUM/DENOM seconds. */
180 real_time_sleep (int64_t num
, int32_t denom
)
182 /* Convert NUM/DENOM seconds into timer ticks, rounding down.
185 ---------------------- = NUM * TIMER_FREQ / DENOM ticks.
186 1 s / TIMER_FREQ ticks
188 int64_t ticks
= num
* TIMER_FREQ
/ denom
;
190 ASSERT (intr_get_level () == INTR_ON
);
193 /* We're waiting for at least one full timer tick. Use
194 timer_sleep() because it will yield the CPU to other
200 /* Otherwise, use a busy-wait loop for more accurate
201 sub-tick timing. We scale the numerator and denominator
202 down by 1000 to avoid the possibility of overflow. */
203 ASSERT (denom
% 1000 == 0);
204 busy_wait (loops_per_tick
* num
/ 1000 * TIMER_FREQ
/ (denom
/ 1000));