Rename ram_pages to init_ram_pages.
[pintos-anon] / src / devices / timer.c
1 #include "devices/timer.h"
2 #include <debug.h>
3 #include <inttypes.h>
4 #include <round.h>
5 #include <stdio.h>
6 #include "threads/interrupt.h"
7 #include "threads/io.h"
8 #include "threads/synch.h"
9 #include "threads/thread.h"
10   
11 /* See [8254] for hardware details of the 8254 timer chip. */
12
13 #if TIMER_FREQ < 19
14 #error 8254 timer requires TIMER_FREQ >= 19
15 #endif
16 #if TIMER_FREQ > 1000
17 #error TIMER_FREQ <= 1000 recommended
18 #endif
19
20 /* Number of timer ticks since OS booted. */
21 static int64_t ticks;
22
23 /* Number of loops per timer tick.
24    Initialized by timer_calibrate(). */
25 static unsigned loops_per_tick;
26
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);
31 static void real_time_delay (int64_t num, int32_t denom);
32
33 /* Sets up the 8254 Programmable Interval Timer (PIT) to
34    interrupt PIT_FREQ times per second, and registers the
35    corresponding interrupt. */
36 void
37 timer_init (void) 
38 {
39   /* 8254 input frequency divided by TIMER_FREQ, rounded to
40      nearest. */
41   uint16_t count = (1193180 + TIMER_FREQ / 2) / TIMER_FREQ;
42
43   outb (0x43, 0x34);    /* CW: counter 0, LSB then MSB, mode 2, binary. */
44   outb (0x40, count & 0xff);
45   outb (0x40, count >> 8);
46
47   intr_register_ext (0x20, timer_interrupt, "8254 Timer");
48 }
49
50 /* Calibrates loops_per_tick, used to implement brief delays. */
51 void
52 timer_calibrate (void) 
53 {
54   unsigned high_bit, test_bit;
55
56   ASSERT (intr_get_level () == INTR_ON);
57   printf ("Calibrating timer...  ");
58
59   /* Approximate loops_per_tick as the largest power-of-two
60      still less than one timer tick. */
61   loops_per_tick = 1u << 10;
62   while (!too_many_loops (loops_per_tick << 1)) 
63     {
64       loops_per_tick <<= 1;
65       ASSERT (loops_per_tick != 0);
66     }
67
68   /* Refine the next 8 bits of loops_per_tick. */
69   high_bit = loops_per_tick;
70   for (test_bit = high_bit >> 1; test_bit != high_bit >> 10; test_bit >>= 1)
71     if (!too_many_loops (high_bit | test_bit))
72       loops_per_tick |= test_bit;
73
74   printf ("%'"PRIu64" loops/s.\n", (uint64_t) loops_per_tick * TIMER_FREQ);
75 }
76
77 /* Returns the number of timer ticks since the OS booted. */
78 int64_t
79 timer_ticks (void) 
80 {
81   enum intr_level old_level = intr_disable ();
82   int64_t t = ticks;
83   intr_set_level (old_level);
84   barrier ();
85   return t;
86 }
87
88 /* Returns the number of timer ticks elapsed since THEN, which
89    should be a value once returned by timer_ticks(). */
90 int64_t
91 timer_elapsed (int64_t then) 
92 {
93   return timer_ticks () - then;
94 }
95
96 /* Sleeps for approximately TICKS timer ticks.  Interrupts must
97    be turned on. */
98 void
99 timer_sleep (int64_t ticks) 
100 {
101   int64_t start = timer_ticks ();
102
103   ASSERT (intr_get_level () == INTR_ON);
104   while (timer_elapsed (start) < ticks) 
105     thread_yield ();
106 }
107
108 /* Sleeps for approximately MS milliseconds.  Interrupts must be
109    turned on. */
110 void
111 timer_msleep (int64_t ms) 
112 {
113   real_time_sleep (ms, 1000);
114 }
115
116 /* Sleeps for approximately US microseconds.  Interrupts must be
117    turned on. */
118 void
119 timer_usleep (int64_t us) 
120 {
121   real_time_sleep (us, 1000 * 1000);
122 }
123
124 /* Sleeps for approximately NS nanoseconds.  Interrupts must be
125    turned on. */
126 void
127 timer_nsleep (int64_t ns) 
128 {
129   real_time_sleep (ns, 1000 * 1000 * 1000);
130 }
131
132 /* Busy-waits for approximately MS milliseconds.  Interrupts need
133    not be turned on.
134
135    Busy waiting wastes CPU cycles, and busy waiting with
136    interrupts off for the interval between timer ticks or longer
137    will cause timer ticks to be lost.  Thus, use timer_msleep()
138    instead if interrupts are enabled. */
139 void
140 timer_mdelay (int64_t ms) 
141 {
142   real_time_delay (ms, 1000);
143 }
144
145 /* Sleeps for approximately US microseconds.  Interrupts need not
146    be turned on.
147
148    Busy waiting wastes CPU cycles, and busy waiting with
149    interrupts off for the interval between timer ticks or longer
150    will cause timer ticks to be lost.  Thus, use timer_usleep()
151    instead if interrupts are enabled. */
152 void
153 timer_udelay (int64_t us) 
154 {
155   real_time_delay (us, 1000 * 1000);
156 }
157
158 /* Sleeps execution for approximately NS nanoseconds.  Interrupts
159    need not be turned on.
160
161    Busy waiting wastes CPU cycles, and busy waiting with
162    interrupts off for the interval between timer ticks or longer
163    will cause timer ticks to be lost.  Thus, use timer_nsleep()
164    instead if interrupts are enabled.*/
165 void
166 timer_ndelay (int64_t ns) 
167 {
168   real_time_delay (ns, 1000 * 1000 * 1000);
169 }
170
171 /* Prints timer statistics. */
172 void
173 timer_print_stats (void) 
174 {
175   printf ("Timer: %"PRId64" ticks\n", timer_ticks ());
176 }
177 \f
178 /* Timer interrupt handler. */
179 static void
180 timer_interrupt (struct intr_frame *args UNUSED)
181 {
182   ticks++;
183   thread_tick ();
184 }
185
186 /* Returns true if LOOPS iterations waits for more than one timer
187    tick, otherwise false. */
188 static bool
189 too_many_loops (unsigned loops) 
190 {
191   /* Wait for a timer tick. */
192   int64_t start = ticks;
193   while (ticks == start)
194     barrier ();
195
196   /* Run LOOPS loops. */
197   start = ticks;
198   busy_wait (loops);
199
200   /* If the tick count changed, we iterated too long. */
201   barrier ();
202   return start != ticks;
203 }
204
205 /* Iterates through a simple loop LOOPS times, for implementing
206    brief delays.
207
208    Marked NO_INLINE because code alignment can significantly
209    affect timings, so that if this function was inlined
210    differently in different places the results would be difficult
211    to predict. */
212 static void NO_INLINE
213 busy_wait (int64_t loops) 
214 {
215   while (loops-- > 0)
216     barrier ();
217 }
218
219 /* Sleep for approximately NUM/DENOM seconds. */
220 static void
221 real_time_sleep (int64_t num, int32_t denom) 
222 {
223   /* Convert NUM/DENOM seconds into timer ticks, rounding down.
224           
225         (NUM / DENOM) s          
226      ---------------------- = NUM * TIMER_FREQ / DENOM ticks. 
227      1 s / TIMER_FREQ ticks
228   */
229   int64_t ticks = num * TIMER_FREQ / denom;
230
231   ASSERT (intr_get_level () == INTR_ON);
232   if (ticks > 0)
233     {
234       /* We're waiting for at least one full timer tick.  Use
235          timer_sleep() because it will yield the CPU to other
236          processes. */                
237       timer_sleep (ticks); 
238     }
239   else 
240     {
241       /* Otherwise, use a busy-wait loop for more accurate
242          sub-tick timing. */
243       real_time_delay (num, denom); 
244     }
245 }
246
247 /* Busy-wait for approximately NUM/DENOM seconds. */
248 static void
249 real_time_delay (int64_t num, int32_t denom)
250 {
251   /* Scale the numerator and denominator down by 1000 to avoid
252      the possibility of overflow. */
253   ASSERT (denom % 1000 == 0);
254   busy_wait (loops_per_tick * num / 1000 * TIMER_FREQ / (denom / 1000)); 
255 }