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 | FORWARD _PROTOTYPE( void load_update, (void));
|
---|
42 |
|
---|
43 | /* Clock parameters. */
|
---|
44 | #define COUNTER_FREQ (2*TIMER_FREQ) /* counter frequency using square wave */
|
---|
45 | #define LATCH_COUNT 0x00 /* cc00xxxx, c = channel, x = any */
|
---|
46 | #define SQUARE_WAVE 0x36 /* ccaammmb, a = access, m = mode, b = BCD */
|
---|
47 | /* 11x11, 11 = LSB then MSB, x11 = sq wave */
|
---|
48 | #define TIMER_COUNT ((unsigned) (TIMER_FREQ/HZ)) /* initial value for counter*/
|
---|
49 | #define TIMER_FREQ 1193182L /* clock frequency for timer in PC and AT */
|
---|
50 |
|
---|
51 | #define CLOCK_ACK_BIT 0x80 /* PS/2 clock interrupt acknowledge bit */
|
---|
52 |
|
---|
53 | /* The CLOCK's timers queue. The functions in <timers.h> operate on this.
|
---|
54 | * Each system process possesses a single synchronous alarm timer. If other
|
---|
55 | * kernel parts want to use additional timers, they must declare their own
|
---|
56 | * persistent (static) timer structure, which can be passed to the clock
|
---|
57 | * via (re)set_timer().
|
---|
58 | * When a timer expires its watchdog function is run by the CLOCK task.
|
---|
59 | */
|
---|
60 | PRIVATE timer_t *clock_timers; /* queue of CLOCK timers */
|
---|
61 | PRIVATE clock_t next_timeout; /* realtime that next timer expires */
|
---|
62 |
|
---|
63 | /* The time is incremented by the interrupt handler on each clock tick. */
|
---|
64 | PRIVATE clock_t realtime; /* real time clock */
|
---|
65 | PRIVATE irq_hook_t clock_hook; /* interrupt handler hook */
|
---|
66 |
|
---|
67 | /*===========================================================================*
|
---|
68 | * clock_task *
|
---|
69 | *===========================================================================*/
|
---|
70 | PUBLIC void clock_task()
|
---|
71 | {
|
---|
72 | /* Main program of clock task. If the call is not HARD_INT it is an error.
|
---|
73 | */
|
---|
74 | message m; /* message buffer for both input and output */
|
---|
75 | int result; /* result returned by the handler */
|
---|
76 |
|
---|
77 | init_clock(); /* initialize clock task */
|
---|
78 |
|
---|
79 | /* Main loop of the clock task. Get work, process it. Never reply. */
|
---|
80 | while (TRUE) {
|
---|
81 |
|
---|
82 | /* Go get a message. */
|
---|
83 | receive(ANY, &m);
|
---|
84 |
|
---|
85 | /* Handle the request. Only clock ticks are expected. */
|
---|
86 | switch (m.m_type) {
|
---|
87 | case HARD_INT:
|
---|
88 | result = do_clocktick(&m); /* handle clock tick */
|
---|
89 | break;
|
---|
90 | default: /* illegal request type */
|
---|
91 | kprintf("CLOCK: illegal request %d from %d.\n", m.m_type,m.m_source);
|
---|
92 | }
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | /*===========================================================================*
|
---|
97 | * do_clocktick *
|
---|
98 | *===========================================================================*/
|
---|
99 | PRIVATE int do_clocktick(m_ptr)
|
---|
100 | message *m_ptr; /* pointer to request message */
|
---|
101 | {
|
---|
102 | /* Despite its name, this routine is not called on every clock tick. It
|
---|
103 | * is called on those clock ticks when a lot of work needs to be done.
|
---|
104 | */
|
---|
105 |
|
---|
106 | /* A process used up a full quantum. The interrupt handler stored this
|
---|
107 | * process in 'prev_ptr'. First make sure that the process is not on the
|
---|
108 | * scheduling queues. Then announce the process ready again. Since it has
|
---|
109 | * no more time left, it gets a new quantum and is inserted at the right
|
---|
110 | * place in the queues. As a side-effect a new process will be scheduled.
|
---|
111 | */
|
---|
112 | if (prev_ptr->p_ticks_left <= 0 && priv(prev_ptr)->s_flags & PREEMPTIBLE) {
|
---|
113 | lock_dequeue(prev_ptr); /* take it off the queues */
|
---|
114 | lock_enqueue(prev_ptr); /* and reinsert it again */
|
---|
115 | }
|
---|
116 |
|
---|
117 | /* Check if a clock timer expired and run its watchdog function. */
|
---|
118 | if (next_timeout <= realtime) {
|
---|
119 | tmrs_exptimers(&clock_timers, realtime, NULL);
|
---|
120 | next_timeout = clock_timers == NULL ?
|
---|
121 | TMR_NEVER : clock_timers->tmr_exp_time;
|
---|
122 | }
|
---|
123 |
|
---|
124 | /* Inhibit sending a reply. */
|
---|
125 | return(EDONTREPLY);
|
---|
126 | }
|
---|
127 |
|
---|
128 | /*===========================================================================*
|
---|
129 | * init_clock *
|
---|
130 | *===========================================================================*/
|
---|
131 | PRIVATE void init_clock()
|
---|
132 | {
|
---|
133 | /* Initialize the CLOCK's interrupt hook. */
|
---|
134 | clock_hook.proc_nr_e = CLOCK;
|
---|
135 |
|
---|
136 | /* Initialize channel 0 of the 8253A timer to, e.g., 60 Hz, and register
|
---|
137 | * the CLOCK task's interrupt handler to be run on every clock tick.
|
---|
138 | */
|
---|
139 | outb(TIMER_MODE, SQUARE_WAVE); /* set timer to run continuously */
|
---|
140 | outb(TIMER0, TIMER_COUNT); /* load timer low byte */
|
---|
141 | outb(TIMER0, TIMER_COUNT >> 8); /* load timer high byte */
|
---|
142 | put_irq_handler(&clock_hook, CLOCK_IRQ, clock_handler);
|
---|
143 | enable_irq(&clock_hook); /* ready for clock interrupts */
|
---|
144 |
|
---|
145 | /* Set a watchdog timer to periodically balance the scheduling queues. */
|
---|
146 | balance_queues(NULL); /* side-effect sets new timer */
|
---|
147 | }
|
---|
148 |
|
---|
149 | /*===========================================================================*
|
---|
150 | * clock_stop *
|
---|
151 | *===========================================================================*/
|
---|
152 | PUBLIC void clock_stop()
|
---|
153 | {
|
---|
154 | /* Reset the clock to the BIOS rate. (For rebooting.) */
|
---|
155 | outb(TIMER_MODE, 0x36);
|
---|
156 | outb(TIMER0, 0);
|
---|
157 | outb(TIMER0, 0);
|
---|
158 | }
|
---|
159 |
|
---|
160 | /*===========================================================================*
|
---|
161 | * clock_handler *
|
---|
162 | *===========================================================================*/
|
---|
163 | PRIVATE int clock_handler(hook)
|
---|
164 | irq_hook_t *hook;
|
---|
165 | {
|
---|
166 | /* This executes on each clock tick (i.e., every time the timer chip generates
|
---|
167 | * an interrupt). It does a little bit of work so the clock task does not have
|
---|
168 | * to be called on every tick. The clock task is called when:
|
---|
169 | *
|
---|
170 | * (1) the scheduling quantum of the running process has expired, or
|
---|
171 | * (2) a timer has expired and the watchdog function should be run.
|
---|
172 | *
|
---|
173 | * Many global global and static variables are accessed here. The safety of
|
---|
174 | * this must be justified. All scheduling and message passing code acquires a
|
---|
175 | * lock by temporarily disabling interrupts, so no conflicts with calls from
|
---|
176 | * the task level can occur. Furthermore, interrupts are not reentrant, the
|
---|
177 | * interrupt handler cannot be bothered by other interrupts.
|
---|
178 | *
|
---|
179 | * Variables that are updated in the clock's interrupt handler:
|
---|
180 | * lost_ticks:
|
---|
181 | * Clock ticks counted outside the clock task. This for example
|
---|
182 | * is used when the boot monitor processes a real mode interrupt.
|
---|
183 | * realtime:
|
---|
184 | * The current uptime is incremented with all outstanding ticks.
|
---|
185 | * proc_ptr, bill_ptr:
|
---|
186 | * These are used for accounting. It does not matter if proc.c
|
---|
187 | * is changing them, provided they are always valid pointers,
|
---|
188 | * since at worst the previous process would be billed.
|
---|
189 | */
|
---|
190 | register unsigned ticks;
|
---|
191 |
|
---|
192 | /* Acknowledge the PS/2 clock interrupt. */
|
---|
193 | if (machine.ps_mca) outb(PORT_B, inb(PORT_B) | CLOCK_ACK_BIT);
|
---|
194 |
|
---|
195 | /* Get number of ticks and update realtime. */
|
---|
196 | ticks = lost_ticks + 1;
|
---|
197 | lost_ticks = 0;
|
---|
198 | realtime += ticks;
|
---|
199 |
|
---|
200 | /* Update user and system accounting times. Charge the current process for
|
---|
201 | * user time. If the current process is not billable, that is, if a non-user
|
---|
202 | * process is running, charge the billable process for system time as well.
|
---|
203 | * Thus the unbillable process' user time is the billable user's system time.
|
---|
204 | */
|
---|
205 | proc_ptr->p_user_time += ticks;
|
---|
206 | if (priv(proc_ptr)->s_flags & PREEMPTIBLE) {
|
---|
207 | proc_ptr->p_ticks_left -= ticks;
|
---|
208 | }
|
---|
209 | if (! (priv(proc_ptr)->s_flags & BILLABLE)) {
|
---|
210 | bill_ptr->p_sys_time += ticks;
|
---|
211 | bill_ptr->p_ticks_left -= ticks;
|
---|
212 | }
|
---|
213 |
|
---|
214 | /* Update load average. */
|
---|
215 | load_update();
|
---|
216 |
|
---|
217 | /* Check if do_clocktick() must be called. Done for alarms and scheduling.
|
---|
218 | * Some processes, such as the kernel tasks, cannot be preempted.
|
---|
219 | */
|
---|
220 | if ((next_timeout <= realtime) || (proc_ptr->p_ticks_left <= 0)) {
|
---|
221 | prev_ptr = proc_ptr; /* store running process */
|
---|
222 | lock_notify(HARDWARE, CLOCK); /* send notification */
|
---|
223 | }
|
---|
224 | return(1); /* reenable interrupts */
|
---|
225 | }
|
---|
226 |
|
---|
227 | /*===========================================================================*
|
---|
228 | * get_uptime *
|
---|
229 | *===========================================================================*/
|
---|
230 | PUBLIC clock_t get_uptime()
|
---|
231 | {
|
---|
232 | /* Get and return the current clock uptime in ticks. */
|
---|
233 | return(realtime);
|
---|
234 | }
|
---|
235 |
|
---|
236 | /*===========================================================================*
|
---|
237 | * set_timer *
|
---|
238 | *===========================================================================*/
|
---|
239 | PUBLIC void set_timer(tp, exp_time, watchdog)
|
---|
240 | struct timer *tp; /* pointer to timer structure */
|
---|
241 | clock_t exp_time; /* expiration realtime */
|
---|
242 | tmr_func_t watchdog; /* watchdog to be called */
|
---|
243 | {
|
---|
244 | /* Insert the new timer in the active timers list. Always update the
|
---|
245 | * next timeout time by setting it to the front of the active list.
|
---|
246 | */
|
---|
247 | tmrs_settimer(&clock_timers, tp, exp_time, watchdog, NULL);
|
---|
248 | next_timeout = clock_timers->tmr_exp_time;
|
---|
249 | }
|
---|
250 |
|
---|
251 | /*===========================================================================*
|
---|
252 | * reset_timer *
|
---|
253 | *===========================================================================*/
|
---|
254 | PUBLIC void reset_timer(tp)
|
---|
255 | struct timer *tp; /* pointer to timer structure */
|
---|
256 | {
|
---|
257 | /* The timer pointed to by 'tp' is no longer needed. Remove it from both the
|
---|
258 | * active and expired lists. Always update the next timeout time by setting
|
---|
259 | * it to the front of the active list.
|
---|
260 | */
|
---|
261 | tmrs_clrtimer(&clock_timers, tp, NULL);
|
---|
262 | next_timeout = (clock_timers == NULL) ?
|
---|
263 | TMR_NEVER : clock_timers->tmr_exp_time;
|
---|
264 | }
|
---|
265 |
|
---|
266 | /*===========================================================================*
|
---|
267 | * read_clock *
|
---|
268 | *===========================================================================*/
|
---|
269 | PUBLIC unsigned long read_clock()
|
---|
270 | {
|
---|
271 | /* Read the counter of channel 0 of the 8253A timer. This counter counts
|
---|
272 | * down at a rate of TIMER_FREQ and restarts at TIMER_COUNT-1 when it
|
---|
273 | * reaches zero. A hardware interrupt (clock tick) occurs when the counter
|
---|
274 | * gets to zero and restarts its cycle.
|
---|
275 | */
|
---|
276 | unsigned count;
|
---|
277 |
|
---|
278 | outb(TIMER_MODE, LATCH_COUNT);
|
---|
279 | count = inb(TIMER0);
|
---|
280 | count |= (inb(TIMER0) << 8);
|
---|
281 |
|
---|
282 | return count;
|
---|
283 | }
|
---|
284 |
|
---|
285 | /*===========================================================================*
|
---|
286 | * load_update *
|
---|
287 | *===========================================================================*/
|
---|
288 | PRIVATE void load_update(void)
|
---|
289 | {
|
---|
290 | u16_t slot;
|
---|
291 | int enqueued = -1, q; /* -1: special compensation for IDLE. */
|
---|
292 | struct proc *p;
|
---|
293 |
|
---|
294 | /* Load average data is stored as a list of numbers in a circular
|
---|
295 | * buffer. Each slot accumulates _LOAD_UNIT_SECS of samples of
|
---|
296 | * the number of runnable processes. Computations can then
|
---|
297 | * be made of the load average over variable periods, in the
|
---|
298 | * user library (see getloadavg(3)).
|
---|
299 | */
|
---|
300 | slot = (realtime / HZ / _LOAD_UNIT_SECS) % _LOAD_HISTORY;
|
---|
301 | if(slot != kloadinfo.proc_last_slot) {
|
---|
302 | kloadinfo.proc_load_history[slot] = 0;
|
---|
303 | kloadinfo.proc_last_slot = slot;
|
---|
304 | }
|
---|
305 |
|
---|
306 | /* Cumulation. How many processes are ready now? */
|
---|
307 | for(q = 0; q < NR_SCHED_QUEUES; q++)
|
---|
308 | for(p = rdy_head[q]; p != NIL_PROC; p = p->p_nextready)
|
---|
309 | enqueued++;
|
---|
310 |
|
---|
311 | kloadinfo.proc_load_history[slot] += enqueued;
|
---|
312 |
|
---|
313 | /* Up-to-dateness. */
|
---|
314 | kloadinfo.last_clock = realtime;
|
---|
315 | }
|
---|
316 |
|
---|