| [4] | 1 | /* This file contains the clock task, which handles time related functions. | 
|---|
|  | 2 | * Important events that are handled by the CLOCK include setting and | 
|---|
|  | 3 | * monitoring alarm timers and deciding when to (re)schedule processes. | 
|---|
|  | 4 | * The CLOCK offers a direct interface to kernel processes. System services | 
|---|
|  | 5 | * can access its services through system calls, such as sys_setalarm(). The | 
|---|
|  | 6 | * CLOCK task thus is hidden from the outside world. | 
|---|
|  | 7 | * | 
|---|
|  | 8 | * Changes: | 
|---|
|  | 9 | *   Oct 08, 2005   reordering and comment editing (A. S. Woodhull) | 
|---|
|  | 10 | *   Mar 18, 2004   clock interface moved to SYSTEM task (Jorrit N. Herder) | 
|---|
|  | 11 | *   Sep 30, 2004   source code documentation updated  (Jorrit N. Herder) | 
|---|
|  | 12 | *   Sep 24, 2004   redesigned alarm timers  (Jorrit N. Herder) | 
|---|
|  | 13 | * | 
|---|
|  | 14 | * The function do_clocktick() is triggered by the clock's interrupt | 
|---|
|  | 15 | * handler when a watchdog timer has expired or a process must be scheduled. | 
|---|
|  | 16 | * | 
|---|
|  | 17 | * In addition to the main clock_task() entry point, which starts the main | 
|---|
|  | 18 | * loop, there are several other minor entry points: | 
|---|
|  | 19 | *   clock_stop:        called just before MINIX shutdown | 
|---|
|  | 20 | *   get_uptime:        get realtime since boot in clock ticks | 
|---|
|  | 21 | *   set_timer:         set a watchdog timer (+) | 
|---|
|  | 22 | *   reset_timer:       reset a watchdog timer (+) | 
|---|
|  | 23 | *   read_clock:        read the counter of channel 0 of the 8253A timer | 
|---|
|  | 24 | * | 
|---|
|  | 25 | * (+) The CLOCK task keeps tracks of watchdog timers for the entire kernel. | 
|---|
|  | 26 | * The watchdog functions of expired timers are executed in do_clocktick(). | 
|---|
|  | 27 | * It is crucial that watchdog functions not block, or the CLOCK task may | 
|---|
|  | 28 | * be blocked. Do not send() a message when the receiver is not expecting it. | 
|---|
|  | 29 | * Instead, notify(), which always returns, should be used. | 
|---|
|  | 30 | */ | 
|---|
|  | 31 |  | 
|---|
|  | 32 | #include "kernel.h" | 
|---|
|  | 33 | #include "proc.h" | 
|---|
|  | 34 | #include <signal.h> | 
|---|
|  | 35 | #include <minix/com.h> | 
|---|
|  | 36 |  | 
|---|
|  | 37 | /* Function prototype for PRIVATE functions. */ | 
|---|
|  | 38 | FORWARD _PROTOTYPE( void init_clock, (void) ); | 
|---|
|  | 39 | FORWARD _PROTOTYPE( int clock_handler, (irq_hook_t *hook) ); | 
|---|
|  | 40 | FORWARD _PROTOTYPE( int do_clocktick, (message *m_ptr) ); | 
|---|
|  | 41 |  | 
|---|
|  | 42 | /* Clock parameters. */ | 
|---|
|  | 43 | #define COUNTER_FREQ (2*TIMER_FREQ) /* counter frequency using square wave */ | 
|---|
|  | 44 | #define LATCH_COUNT     0x00    /* cc00xxxx, c = channel, x = any */ | 
|---|
|  | 45 | #define SQUARE_WAVE     0x36    /* ccaammmb, a = access, m = mode, b = BCD */ | 
|---|
|  | 46 | /*   11x11, 11 = LSB then MSB, x11 = sq wave */ | 
|---|
|  | 47 | #define TIMER_COUNT ((unsigned) (TIMER_FREQ/HZ)) /* initial value for counter*/ | 
|---|
|  | 48 | #define TIMER_FREQ  1193182L    /* clock frequency for timer in PC and AT */ | 
|---|
|  | 49 |  | 
|---|
|  | 50 | #define CLOCK_ACK_BIT   0x80    /* PS/2 clock interrupt acknowledge bit */ | 
|---|
|  | 51 |  | 
|---|
|  | 52 | /* The CLOCK's timers queue. The functions in <timers.h> operate on this. | 
|---|
|  | 53 | * Each system process possesses a single synchronous alarm timer. If other | 
|---|
|  | 54 | * kernel parts want to use additional timers, they must declare their own | 
|---|
|  | 55 | * persistent (static) timer structure, which can be passed to the clock | 
|---|
|  | 56 | * via (re)set_timer(). | 
|---|
|  | 57 | * When a timer expires its watchdog function is run by the CLOCK task. | 
|---|
|  | 58 | */ | 
|---|
|  | 59 | PRIVATE timer_t *clock_timers;          /* queue of CLOCK timers */ | 
|---|
|  | 60 | PRIVATE clock_t next_timeout;           /* realtime that next timer expires */ | 
|---|
|  | 61 |  | 
|---|
|  | 62 | /* The time is incremented by the interrupt handler on each clock tick. */ | 
|---|
|  | 63 | PRIVATE clock_t realtime;               /* real time clock */ | 
|---|
|  | 64 | PRIVATE irq_hook_t clock_hook;          /* interrupt handler hook */ | 
|---|
|  | 65 |  | 
|---|
|  | 66 | /*===========================================================================* | 
|---|
|  | 67 | *                              clock_task                                   * | 
|---|
|  | 68 | *===========================================================================*/ | 
|---|
|  | 69 | PUBLIC void clock_task() | 
|---|
|  | 70 | { | 
|---|
|  | 71 | /* Main program of clock task. If the call is not HARD_INT it is an error. | 
|---|
|  | 72 | */ | 
|---|
|  | 73 | message m;                    /* message buffer for both input and output */ | 
|---|
|  | 74 | int result;                   /* result returned by the handler */ | 
|---|
|  | 75 |  | 
|---|
|  | 76 | init_clock();                 /* initialize clock task */ | 
|---|
|  | 77 |  | 
|---|
|  | 78 | /* Main loop of the clock task.  Get work, process it. Never reply. */ | 
|---|
|  | 79 | while (TRUE) { | 
|---|
|  | 80 |  | 
|---|
|  | 81 | /* Go get a message. */ | 
|---|
|  | 82 | receive(ANY, &m); | 
|---|
|  | 83 |  | 
|---|
|  | 84 | /* Handle the request. Only clock ticks are expected. */ | 
|---|
|  | 85 | switch (m.m_type) { | 
|---|
|  | 86 | case HARD_INT: | 
|---|
|  | 87 | result = do_clocktick(&m);    /* handle clock tick */ | 
|---|
|  | 88 | break; | 
|---|
|  | 89 | default:                          /* illegal request type */ | 
|---|
|  | 90 | kprintf("CLOCK: illegal request %d from %d.\n", m.m_type,m.m_source); | 
|---|
|  | 91 | } | 
|---|
|  | 92 | } | 
|---|
|  | 93 | } | 
|---|
|  | 94 |  | 
|---|
|  | 95 | /*===========================================================================* | 
|---|
|  | 96 | *                              do_clocktick                                 * | 
|---|
|  | 97 | *===========================================================================*/ | 
|---|
|  | 98 | PRIVATE int do_clocktick(m_ptr) | 
|---|
|  | 99 | message *m_ptr;                         /* pointer to request message */ | 
|---|
|  | 100 | { | 
|---|
|  | 101 | /* Despite its name, this routine is not called on every clock tick. It | 
|---|
|  | 102 | * is called on those clock ticks when a lot of work needs to be done. | 
|---|
|  | 103 | */ | 
|---|
|  | 104 |  | 
|---|
|  | 105 | /* A process used up a full quantum. The interrupt handler stored this | 
|---|
|  | 106 | * process in 'prev_ptr'.  First make sure that the process is not on the | 
|---|
|  | 107 | * scheduling queues.  Then announce the process ready again. Since it has | 
|---|
|  | 108 | * no more time left, it gets a new quantum and is inserted at the right | 
|---|
|  | 109 | * place in the queues.  As a side-effect a new process will be scheduled. | 
|---|
|  | 110 | */ | 
|---|
|  | 111 | if (prev_ptr->p_ticks_left <= 0 && priv(prev_ptr)->s_flags & PREEMPTIBLE) { | 
|---|
|  | 112 | lock_dequeue(prev_ptr);           /* take it off the queues */ | 
|---|
|  | 113 | lock_enqueue(prev_ptr);           /* and reinsert it again */ | 
|---|
|  | 114 | } | 
|---|
|  | 115 |  | 
|---|
|  | 116 | /* Check if a clock timer expired and run its watchdog function. */ | 
|---|
|  | 117 | if (next_timeout <= realtime) { | 
|---|
|  | 118 | tmrs_exptimers(&clock_timers, realtime, NULL); | 
|---|
|  | 119 | next_timeout = clock_timers == NULL ? | 
|---|
|  | 120 | TMR_NEVER : clock_timers->tmr_exp_time; | 
|---|
|  | 121 | } | 
|---|
|  | 122 |  | 
|---|
|  | 123 | /* Inhibit sending a reply. */ | 
|---|
|  | 124 | return(EDONTREPLY); | 
|---|
|  | 125 | } | 
|---|
|  | 126 |  | 
|---|
|  | 127 | /*===========================================================================* | 
|---|
|  | 128 | *                              init_clock                                   * | 
|---|
|  | 129 | *===========================================================================*/ | 
|---|
|  | 130 | PRIVATE void init_clock() | 
|---|
|  | 131 | { | 
|---|
|  | 132 | /* Initialize the CLOCK's interrupt hook. */ | 
|---|
|  | 133 | clock_hook.proc_nr = CLOCK; | 
|---|
|  | 134 |  | 
|---|
|  | 135 | /* Initialize channel 0 of the 8253A timer to, e.g., 60 Hz. */ | 
|---|
|  | 136 | outb(TIMER_MODE, SQUARE_WAVE);        /* set timer to run continuously */ | 
|---|
|  | 137 | outb(TIMER0, TIMER_COUNT);            /* load timer low byte */ | 
|---|
|  | 138 | outb(TIMER0, TIMER_COUNT >> 8);       /* load timer high byte */ | 
|---|
|  | 139 | put_irq_handler(&clock_hook, CLOCK_IRQ, clock_handler);/* register handler */ | 
|---|
|  | 140 | enable_irq(&clock_hook);              /* ready for clock interrupts */ | 
|---|
|  | 141 | } | 
|---|
|  | 142 |  | 
|---|
|  | 143 | /*===========================================================================* | 
|---|
|  | 144 | *                              clock_stop                                   * | 
|---|
|  | 145 | *===========================================================================*/ | 
|---|
|  | 146 | PUBLIC void clock_stop() | 
|---|
|  | 147 | { | 
|---|
|  | 148 | /* Reset the clock to the BIOS rate. (For rebooting) */ | 
|---|
|  | 149 | outb(TIMER_MODE, 0x36); | 
|---|
|  | 150 | outb(TIMER0, 0); | 
|---|
|  | 151 | outb(TIMER0, 0); | 
|---|
|  | 152 | } | 
|---|
|  | 153 |  | 
|---|
|  | 154 | /*===========================================================================* | 
|---|
|  | 155 | *                              clock_handler                                * | 
|---|
|  | 156 | *===========================================================================*/ | 
|---|
|  | 157 | PRIVATE int clock_handler(hook) | 
|---|
|  | 158 | irq_hook_t *hook; | 
|---|
|  | 159 | { | 
|---|
|  | 160 | /* This executes on each clock tick (i.e., every time the timer chip generates | 
|---|
|  | 161 | * an interrupt). It does a little bit of work so the clock task does not have | 
|---|
|  | 162 | * to be called on every tick.  The clock task is called when: | 
|---|
|  | 163 | * | 
|---|
|  | 164 | *      (1) the scheduling quantum of the running process has expired, or | 
|---|
|  | 165 | *      (2) a timer has expired and the watchdog function should be run. | 
|---|
|  | 166 | * | 
|---|
|  | 167 | * Many global global and static variables are accessed here.  The safety of | 
|---|
|  | 168 | * this must be justified. All scheduling and message passing code acquires a | 
|---|
|  | 169 | * lock by temporarily disabling interrupts, so no conflicts with calls from | 
|---|
|  | 170 | * the task level can occur. Furthermore, interrupts are not reentrant, the | 
|---|
|  | 171 | * interrupt handler cannot be bothered by other interrupts. | 
|---|
|  | 172 | * | 
|---|
|  | 173 | * Variables that are updated in the clock's interrupt handler: | 
|---|
|  | 174 | *      lost_ticks: | 
|---|
|  | 175 | *              Clock ticks counted outside the clock task. This for example | 
|---|
|  | 176 | *              is used when the boot monitor processes a real mode interrupt. | 
|---|
|  | 177 | *      realtime: | 
|---|
|  | 178 | *              The current uptime is incremented with all outstanding ticks. | 
|---|
|  | 179 | *      proc_ptr, bill_ptr: | 
|---|
|  | 180 | *              These are used for accounting.  It does not matter if proc.c | 
|---|
|  | 181 | *              is changing them, provided they are always valid pointers, | 
|---|
|  | 182 | *              since at worst the previous process would be billed. | 
|---|
|  | 183 | */ | 
|---|
|  | 184 | register unsigned ticks; | 
|---|
|  | 185 |  | 
|---|
|  | 186 | /* Acknowledge the PS/2 clock interrupt. */ | 
|---|
|  | 187 | if (machine.ps_mca) outb(PORT_B, inb(PORT_B) | CLOCK_ACK_BIT); | 
|---|
|  | 188 |  | 
|---|
|  | 189 | /* Get number of ticks and update realtime. */ | 
|---|
|  | 190 | ticks = lost_ticks + 1; | 
|---|
|  | 191 | lost_ticks = 0; | 
|---|
|  | 192 | realtime += ticks; | 
|---|
|  | 193 |  | 
|---|
|  | 194 | /* Update user and system accounting times. Charge the current process for | 
|---|
|  | 195 | * user time. If the current process is not billable, that is, if a non-user | 
|---|
|  | 196 | * process is running, charge the billable process for system time as well. | 
|---|
|  | 197 | * Thus the unbillable process' user time is the billable user's system time. | 
|---|
|  | 198 | */ | 
|---|
|  | 199 | proc_ptr->p_user_time += ticks; | 
|---|
|  | 200 | if (priv(proc_ptr)->s_flags & PREEMPTIBLE) { | 
|---|
|  | 201 | proc_ptr->p_ticks_left -= ticks; | 
|---|
|  | 202 | } | 
|---|
|  | 203 | if (! (priv(proc_ptr)->s_flags & BILLABLE)) { | 
|---|
|  | 204 | bill_ptr->p_sys_time += ticks; | 
|---|
|  | 205 | bill_ptr->p_ticks_left -= ticks; | 
|---|
|  | 206 | } | 
|---|
|  | 207 |  | 
|---|
|  | 208 | /* Check if do_clocktick() must be called. Done for alarms and scheduling. | 
|---|
|  | 209 | * Some processes, such as the kernel tasks, cannot be preempted. | 
|---|
|  | 210 | */ | 
|---|
|  | 211 | if ((next_timeout <= realtime) || (proc_ptr->p_ticks_left <= 0)) { | 
|---|
|  | 212 | prev_ptr = proc_ptr;                      /* store running process */ | 
|---|
|  | 213 | lock_notify(HARDWARE, CLOCK);             /* send notification */ | 
|---|
|  | 214 | } | 
|---|
|  | 215 | return(1);                                    /* reenable interrupts */ | 
|---|
|  | 216 | } | 
|---|
|  | 217 |  | 
|---|
|  | 218 | /*===========================================================================* | 
|---|
|  | 219 | *                              get_uptime                                   * | 
|---|
|  | 220 | *===========================================================================*/ | 
|---|
|  | 221 | PUBLIC clock_t get_uptime() | 
|---|
|  | 222 | { | 
|---|
|  | 223 | /* Get and return the current clock uptime in ticks. */ | 
|---|
|  | 224 | return(realtime); | 
|---|
|  | 225 | } | 
|---|
|  | 226 |  | 
|---|
|  | 227 | /*===========================================================================* | 
|---|
|  | 228 | *                              set_timer                                    * | 
|---|
|  | 229 | *===========================================================================*/ | 
|---|
|  | 230 | PUBLIC void set_timer(tp, exp_time, watchdog) | 
|---|
|  | 231 | struct timer *tp;               /* pointer to timer structure */ | 
|---|
|  | 232 | clock_t exp_time;               /* expiration realtime */ | 
|---|
|  | 233 | tmr_func_t watchdog;            /* watchdog to be called */ | 
|---|
|  | 234 | { | 
|---|
|  | 235 | /* Insert the new timer in the active timers list. Always update the | 
|---|
|  | 236 | * next timeout time by setting it to the front of the active list. | 
|---|
|  | 237 | */ | 
|---|
|  | 238 | tmrs_settimer(&clock_timers, tp, exp_time, watchdog, NULL); | 
|---|
|  | 239 | next_timeout = clock_timers->tmr_exp_time; | 
|---|
|  | 240 | } | 
|---|
|  | 241 |  | 
|---|
|  | 242 | /*===========================================================================* | 
|---|
|  | 243 | *                              reset_timer                                  * | 
|---|
|  | 244 | *===========================================================================*/ | 
|---|
|  | 245 | PUBLIC void reset_timer(tp) | 
|---|
|  | 246 | struct timer *tp;               /* pointer to timer structure */ | 
|---|
|  | 247 | { | 
|---|
|  | 248 | /* The timer pointed to by 'tp' is no longer needed. Remove it from both the | 
|---|
|  | 249 | * active and expired lists. Always update the next timeout time by setting | 
|---|
|  | 250 | * it to the front of the active list. | 
|---|
|  | 251 | */ | 
|---|
|  | 252 | tmrs_clrtimer(&clock_timers, tp, NULL); | 
|---|
|  | 253 | next_timeout = (clock_timers == NULL) ? | 
|---|
|  | 254 | TMR_NEVER : clock_timers->tmr_exp_time; | 
|---|
|  | 255 | } | 
|---|
|  | 256 |  | 
|---|
|  | 257 | /*===========================================================================* | 
|---|
|  | 258 | *                              read_clock                                   * | 
|---|
|  | 259 | *===========================================================================*/ | 
|---|
|  | 260 | PUBLIC unsigned long read_clock() | 
|---|
|  | 261 | { | 
|---|
|  | 262 | /* Read the counter of channel 0 of the 8253A timer.  This counter counts | 
|---|
|  | 263 | * down at a rate of TIMER_FREQ and restarts at TIMER_COUNT-1 when it | 
|---|
|  | 264 | * reaches zero. A hardware interrupt (clock tick) occurs when the counter | 
|---|
|  | 265 | * gets to zero and restarts its cycle. | 
|---|
|  | 266 | */ | 
|---|
|  | 267 | unsigned count; | 
|---|
|  | 268 |  | 
|---|
|  | 269 | outb(TIMER_MODE, LATCH_COUNT); | 
|---|
|  | 270 | count = inb(TIMER0); | 
|---|
|  | 271 | count |= (inb(TIMER0) << 8); | 
|---|
|  | 272 |  | 
|---|
|  | 273 | return count; | 
|---|
|  | 274 | } | 
|---|