Comments.
[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/thread.h"
9   
10 /* See [8254] for hardware details of the 8254 timer chip. */
11
12 #if TIMER_FREQ < 19
13 #error 8254 timer requires TIMER_FREQ >= 19
14 #endif
15 #if TIMER_FREQ > 1000
16 #error TIMER_FREQ <= 1000 recommended
17 #endif
18
19 /* Number of timer ticks that a process gets before being
20    preempted. */
21 #define TIME_SLICE 1
22
23 /* Number of timer ticks since OS booted. */
24 static volatile int64_t ticks;
25
26 static intr_handler_func timer_interrupt;
27
28 /* Sets up the 8254 Programmable Interval Timer (PIT) to
29    interrupt PIT_FREQ times per second, and registers the
30    corresponding interrupt. */
31 void
32 timer_init (void) 
33 {
34   /* 8254 input frequency divided by TIMER_FREQ, rounded to
35      nearest. */
36   uint16_t count = (1193180 + TIMER_FREQ / 2) / TIMER_FREQ;
37
38   outb (0x43, 0x34);    /* CW: counter 0, LSB then MSB, mode 2, binary. */
39   outb (0x40, count & 0xff);
40   outb (0x40, count >> 8);
41
42   intr_register (0x20, 0, INTR_OFF, timer_interrupt, "8254 Timer");
43 }
44
45 /* Returns the number of timer ticks since the OS booted. */
46 int64_t
47 timer_ticks (void) 
48 {
49   enum intr_level old_level = intr_disable ();
50   int64_t t = ticks;
51   intr_set_level (old_level);
52   return t;
53 }
54
55 /* Returns the number of timer ticks elapsed since THEN, which
56    should be a value once returned by timer_ticks(). */
57 int64_t
58 timer_elapsed (int64_t then) 
59 {
60   return timer_ticks () - then;
61 }
62
63 /* Suspends execution for approximately TICKS timer ticks. */
64 void
65 timer_sleep (int64_t ticks) 
66 {
67   int64_t start = timer_ticks ();
68
69   while (timer_elapsed (start) < ticks) 
70     if (intr_get_level () == INTR_ON)
71       thread_yield ();
72 }
73
74 /* Returns MS milliseconds in timer ticks, rounding up. */
75 int64_t
76 timer_ms2ticks (int64_t ms) 
77 {
78   /*       MS / 1000 s          
79      ------------------------ = MS * TIMER_FREQ / 1000 ticks. 
80      (1 / TIMER_FREQ) ticks/s
81   */
82   return DIV_ROUND_UP (ms * TIMER_FREQ, 1000);
83 }
84
85 /* Returns US microseconds in timer ticks, rounding up. */
86 int64_t
87 timer_us2ticks (int64_t us) 
88 {
89   return DIV_ROUND_UP (us * TIMER_FREQ, 1000000);
90 }
91
92 /* Returns NS nanoseconds in timer ticks, rounding up. */
93 int64_t
94 timer_ns2ticks (int64_t ns) 
95 {
96   return DIV_ROUND_UP (ns * TIMER_FREQ, 1000000000);
97 }
98
99 /* Prints timer statistics. */
100 void
101 timer_print_stats (void) 
102 {
103   printf ("Timer: %"PRId64" ticks\n", ticks);
104 }
105 \f
106 /* Timer interrupt handler. */
107 static void
108 timer_interrupt (struct intr_frame *args UNUSED)
109 {
110   ticks++;
111   thread_tick ();
112   if (ticks % TIME_SLICE == 0)
113     intr_yield_on_return ();
114 }