1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate /* Copyright (c) 1990, 1991 UNIX System Laboratories, Inc. */
23*0Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T */
24*0Sstevel@tonic-gate /* All Rights Reserved */
25*0Sstevel@tonic-gate
26*0Sstevel@tonic-gate /*
27*0Sstevel@tonic-gate * Copyright (c) 1997, by Sun Microsystems, Inc.
28*0Sstevel@tonic-gate * All rights reserved.
29*0Sstevel@tonic-gate */
30*0Sstevel@tonic-gate
31*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
32*0Sstevel@tonic-gate
33*0Sstevel@tonic-gate #include <sys/param.h>
34*0Sstevel@tonic-gate #include <sys/types.h>
35*0Sstevel@tonic-gate #include <sys/sysmacros.h>
36*0Sstevel@tonic-gate #include <sys/systm.h>
37*0Sstevel@tonic-gate #include <sys/hrtcntl.h>
38*0Sstevel@tonic-gate #include <sys/errno.h>
39*0Sstevel@tonic-gate #include <sys/hrtsys.h>
40*0Sstevel@tonic-gate #include <sys/time.h>
41*0Sstevel@tonic-gate #include <sys/timer.h>
42*0Sstevel@tonic-gate #include <sys/cmn_err.h>
43*0Sstevel@tonic-gate
44*0Sstevel@tonic-gate /*
45*0Sstevel@tonic-gate * This file contains the code that manages the hardware clocks and
46*0Sstevel@tonic-gate * timers. We must provide UNIX with a HZ resolution clock and give
47*0Sstevel@tonic-gate * the user an interface to the timers through system calls.
48*0Sstevel@tonic-gate */
49*0Sstevel@tonic-gate
50*0Sstevel@tonic-gate static int hrt_checkres(ulong res);
51*0Sstevel@tonic-gate static int hrt_bsd_cancel(int clock);
52*0Sstevel@tonic-gate static int hrt_checkclock(register int clock);
53*0Sstevel@tonic-gate
54*0Sstevel@tonic-gate /*
55*0Sstevel@tonic-gate * Argument vectors for the various flavors of hrtsys().
56*0Sstevel@tonic-gate */
57*0Sstevel@tonic-gate
58*0Sstevel@tonic-gate #define HRTCNTL 0
59*0Sstevel@tonic-gate #define HRTALARM 1
60*0Sstevel@tonic-gate #define HRTSLEEP 2
61*0Sstevel@tonic-gate #define HRTCANCEL 3
62*0Sstevel@tonic-gate
63*0Sstevel@tonic-gate struct hrtsysa {
64*0Sstevel@tonic-gate int opcode;
65*0Sstevel@tonic-gate };
66*0Sstevel@tonic-gate
67*0Sstevel@tonic-gate struct hrtcntla {
68*0Sstevel@tonic-gate int opcode;
69*0Sstevel@tonic-gate int cmd;
70*0Sstevel@tonic-gate int clk;
71*0Sstevel@tonic-gate interval_t *intp;
72*0Sstevel@tonic-gate hrtimes_t *hrtp;
73*0Sstevel@tonic-gate };
74*0Sstevel@tonic-gate
75*0Sstevel@tonic-gate struct hrtalarma {
76*0Sstevel@tonic-gate int opcode;
77*0Sstevel@tonic-gate hrtcmd_t *cmdp;
78*0Sstevel@tonic-gate int cmds;
79*0Sstevel@tonic-gate };
80*0Sstevel@tonic-gate
81*0Sstevel@tonic-gate
82*0Sstevel@tonic-gate /*
83*0Sstevel@tonic-gate * Hrtcntl (time control) system call.
84*0Sstevel@tonic-gate */
85*0Sstevel@tonic-gate
86*0Sstevel@tonic-gate
87*0Sstevel@tonic-gate /*ARGSUSED1*/
88*0Sstevel@tonic-gate int
hrtcntl(uap,rvp)89*0Sstevel@tonic-gate hrtcntl(uap, rvp)
90*0Sstevel@tonic-gate register struct hrtcntla *uap;
91*0Sstevel@tonic-gate rval_t *rvp;
92*0Sstevel@tonic-gate {
93*0Sstevel@tonic-gate register int error = 0;
94*0Sstevel@tonic-gate hrtimes_t temptofd;
95*0Sstevel@tonic-gate
96*0Sstevel@tonic-gate switch (uap->cmd) {
97*0Sstevel@tonic-gate
98*0Sstevel@tonic-gate case HRT_TOFD: /* Get the time of day */
99*0Sstevel@tonic-gate
100*0Sstevel@tonic-gate if (uap->clk != CLK_STD) {
101*0Sstevel@tonic-gate error = EINVAL;
102*0Sstevel@tonic-gate break;
103*0Sstevel@tonic-gate }
104*0Sstevel@tonic-gate
105*0Sstevel@tonic-gate if (copyin((caddr_t)uap->hrtp,
106*0Sstevel@tonic-gate (caddr_t)&temptofd, sizeof (hrtimes_t))) {
107*0Sstevel@tonic-gate error = EFAULT;
108*0Sstevel@tonic-gate break;
109*0Sstevel@tonic-gate }
110*0Sstevel@tonic-gate
111*0Sstevel@tonic-gate if ((error = hrt_checkres(temptofd.hrt_res)))
112*0Sstevel@tonic-gate break;
113*0Sstevel@tonic-gate
114*0Sstevel@tonic-gate hrt_gettofd(&temptofd);
115*0Sstevel@tonic-gate
116*0Sstevel@tonic-gate if (copyout((caddr_t)&temptofd,
117*0Sstevel@tonic-gate (caddr_t)uap->hrtp, sizeof (hrtimes_t)))
118*0Sstevel@tonic-gate error = EFAULT;
119*0Sstevel@tonic-gate
120*0Sstevel@tonic-gate break;
121*0Sstevel@tonic-gate
122*0Sstevel@tonic-gate default:
123*0Sstevel@tonic-gate error = EINVAL;
124*0Sstevel@tonic-gate break;
125*0Sstevel@tonic-gate }
126*0Sstevel@tonic-gate return (error);
127*0Sstevel@tonic-gate }
128*0Sstevel@tonic-gate
129*0Sstevel@tonic-gate /*
130*0Sstevel@tonic-gate * Hrtalarm (start one or more alarms) system call.
131*0Sstevel@tonic-gate */
132*0Sstevel@tonic-gate
133*0Sstevel@tonic-gate int
hrtalarm(uap,rvp)134*0Sstevel@tonic-gate hrtalarm(uap, rvp)
135*0Sstevel@tonic-gate register struct hrtalarma *uap;
136*0Sstevel@tonic-gate rval_t *rvp;
137*0Sstevel@tonic-gate {
138*0Sstevel@tonic-gate register hrtcmd_t *cp;
139*0Sstevel@tonic-gate hrtcmd_t *hrcmdp;
140*0Sstevel@tonic-gate uint alarm_cnt;
141*0Sstevel@tonic-gate int cnt;
142*0Sstevel@tonic-gate int error = 0;
143*0Sstevel@tonic-gate int cmd;
144*0Sstevel@tonic-gate hrtcmd_t timecmd;
145*0Sstevel@tonic-gate hrtimes_t delay_ht;
146*0Sstevel@tonic-gate
147*0Sstevel@tonic-gate
148*0Sstevel@tonic-gate /*
149*0Sstevel@tonic-gate * Return EINVAL for negative and zero counts.
150*0Sstevel@tonic-gate */
151*0Sstevel@tonic-gate
152*0Sstevel@tonic-gate if (uap->cmds <= 0)
153*0Sstevel@tonic-gate return (EINVAL);
154*0Sstevel@tonic-gate
155*0Sstevel@tonic-gate cp = &timecmd;
156*0Sstevel@tonic-gate hrcmdp = uap->cmdp;
157*0Sstevel@tonic-gate alarm_cnt = 0;
158*0Sstevel@tonic-gate
159*0Sstevel@tonic-gate /* Loop through and process each command. */
160*0Sstevel@tonic-gate
161*0Sstevel@tonic-gate for (cnt = 0; cnt < uap->cmds; cnt++, hrcmdp++) {
162*0Sstevel@tonic-gate
163*0Sstevel@tonic-gate if (copyin((caddr_t)hrcmdp, (caddr_t)cp, sizeof (hrtcmd_t)))
164*0Sstevel@tonic-gate return (EFAULT);
165*0Sstevel@tonic-gate
166*0Sstevel@tonic-gate cmd = cp->hrtc_cmd;
167*0Sstevel@tonic-gate
168*0Sstevel@tonic-gate /*
169*0Sstevel@tonic-gate * If we try to post a Berkley Timer remove
170*0Sstevel@tonic-gate * previous timers.
171*0Sstevel@tonic-gate */
172*0Sstevel@tonic-gate
173*0Sstevel@tonic-gate if (cmd == HRT_BSD || cmd == HRT_BSD_REP)
174*0Sstevel@tonic-gate (void) hrt_bsd_cancel(cp->hrtc_clk);
175*0Sstevel@tonic-gate
176*0Sstevel@tonic-gate /* See what kind of command we have. */
177*0Sstevel@tonic-gate
178*0Sstevel@tonic-gate switch (cmd) {
179*0Sstevel@tonic-gate case HRT_BSD: /* one-shot timer */
180*0Sstevel@tonic-gate {
181*0Sstevel@tonic-gate struct itimerval itv;
182*0Sstevel@tonic-gate u_int which;
183*0Sstevel@tonic-gate
184*0Sstevel@tonic-gate if (error = hrt_checkclock(cp->hrtc_clk))
185*0Sstevel@tonic-gate break;
186*0Sstevel@tonic-gate switch (cp->hrtc_clk) {
187*0Sstevel@tonic-gate case CLK_STD:
188*0Sstevel@tonic-gate which = ITIMER_REAL;
189*0Sstevel@tonic-gate break;
190*0Sstevel@tonic-gate case CLK_USERVIRT:
191*0Sstevel@tonic-gate which = ITIMER_VIRTUAL;
192*0Sstevel@tonic-gate break;
193*0Sstevel@tonic-gate case CLK_PROCVIRT:
194*0Sstevel@tonic-gate which = ITIMER_PROF;
195*0Sstevel@tonic-gate break;
196*0Sstevel@tonic-gate default:
197*0Sstevel@tonic-gate error = EINVAL;
198*0Sstevel@tonic-gate goto bad;
199*0Sstevel@tonic-gate }
200*0Sstevel@tonic-gate itv.it_value.tv_sec = cp->hrtc_int.hrt_secs;
201*0Sstevel@tonic-gate itv.it_value.tv_usec = cp->hrtc_int.hrt_rem;
202*0Sstevel@tonic-gate itv.it_interval.tv_sec = 0;
203*0Sstevel@tonic-gate itv.it_interval.tv_usec = 0;
204*0Sstevel@tonic-gate (void) xsetitimer(which, &itv, 1);
205*0Sstevel@tonic-gate
206*0Sstevel@tonic-gate break;
207*0Sstevel@tonic-gate }
208*0Sstevel@tonic-gate
209*0Sstevel@tonic-gate case HRT_BSD_REP:
210*0Sstevel@tonic-gate {
211*0Sstevel@tonic-gate struct itimerval itv;
212*0Sstevel@tonic-gate u_int which;
213*0Sstevel@tonic-gate
214*0Sstevel@tonic-gate switch (cp->hrtc_clk) {
215*0Sstevel@tonic-gate case CLK_STD:
216*0Sstevel@tonic-gate which = ITIMER_REAL;
217*0Sstevel@tonic-gate break;
218*0Sstevel@tonic-gate case CLK_USERVIRT:
219*0Sstevel@tonic-gate which = ITIMER_VIRTUAL;
220*0Sstevel@tonic-gate break;
221*0Sstevel@tonic-gate case CLK_PROCVIRT:
222*0Sstevel@tonic-gate which = ITIMER_PROF;
223*0Sstevel@tonic-gate break;
224*0Sstevel@tonic-gate default:
225*0Sstevel@tonic-gate error = EINVAL;
226*0Sstevel@tonic-gate goto bad;
227*0Sstevel@tonic-gate }
228*0Sstevel@tonic-gate itv.it_value.tv_sec = cp->hrtc_tod.hrt_secs;
229*0Sstevel@tonic-gate itv.it_value.tv_usec = cp->hrtc_tod.hrt_rem;
230*0Sstevel@tonic-gate itv.it_interval.tv_sec = cp->hrtc_int.hrt_secs;
231*0Sstevel@tonic-gate itv.it_interval.tv_usec = cp->hrtc_int.hrt_rem;
232*0Sstevel@tonic-gate (void) xsetitimer(which, &itv, 1);
233*0Sstevel@tonic-gate
234*0Sstevel@tonic-gate break;
235*0Sstevel@tonic-gate }
236*0Sstevel@tonic-gate
237*0Sstevel@tonic-gate case HRT_BSD_PEND:
238*0Sstevel@tonic-gate {
239*0Sstevel@tonic-gate struct itimerval itv;
240*0Sstevel@tonic-gate u_int which;
241*0Sstevel@tonic-gate
242*0Sstevel@tonic-gate switch (cp->hrtc_clk) {
243*0Sstevel@tonic-gate case CLK_STD:
244*0Sstevel@tonic-gate which = ITIMER_REAL;
245*0Sstevel@tonic-gate break;
246*0Sstevel@tonic-gate case CLK_USERVIRT:
247*0Sstevel@tonic-gate which = ITIMER_VIRTUAL;
248*0Sstevel@tonic-gate break;
249*0Sstevel@tonic-gate case CLK_PROCVIRT:
250*0Sstevel@tonic-gate which = ITIMER_PROF;
251*0Sstevel@tonic-gate break;
252*0Sstevel@tonic-gate default:
253*0Sstevel@tonic-gate error = EINVAL;
254*0Sstevel@tonic-gate goto bad;
255*0Sstevel@tonic-gate }
256*0Sstevel@tonic-gate (void) xgetitimer(which, &itv, 1);
257*0Sstevel@tonic-gate delay_ht.hrt_secs = itv.it_value.tv_sec;
258*0Sstevel@tonic-gate delay_ht.hrt_rem = itv.it_value.tv_usec;
259*0Sstevel@tonic-gate }
260*0Sstevel@tonic-gate
261*0Sstevel@tonic-gate if (copyout((caddr_t)&delay_ht,
262*0Sstevel@tonic-gate (caddr_t)&hrcmdp->hrtc_int, sizeof (hrtimes_t)))
263*0Sstevel@tonic-gate error = EFAULT;
264*0Sstevel@tonic-gate
265*0Sstevel@tonic-gate break;
266*0Sstevel@tonic-gate
267*0Sstevel@tonic-gate case HRT_BSD_CANCEL:
268*0Sstevel@tonic-gate if (error = hrt_checkclock(cp->hrtc_clk))
269*0Sstevel@tonic-gate break;
270*0Sstevel@tonic-gate
271*0Sstevel@tonic-gate error = hrt_bsd_cancel(cp->hrtc_clk);
272*0Sstevel@tonic-gate
273*0Sstevel@tonic-gate break;
274*0Sstevel@tonic-gate
275*0Sstevel@tonic-gate default :
276*0Sstevel@tonic-gate error = EINVAL;
277*0Sstevel@tonic-gate break;
278*0Sstevel@tonic-gate }
279*0Sstevel@tonic-gate bad:
280*0Sstevel@tonic-gate if (error) {
281*0Sstevel@tonic-gate cp->hrtc_flags |= HRTF_ERROR;
282*0Sstevel@tonic-gate cp->hrtc_error = error;
283*0Sstevel@tonic-gate } else {
284*0Sstevel@tonic-gate cp->hrtc_flags |= HRTF_DONE;
285*0Sstevel@tonic-gate cp->hrtc_error = 0;
286*0Sstevel@tonic-gate alarm_cnt++;
287*0Sstevel@tonic-gate }
288*0Sstevel@tonic-gate if (copyout((caddr_t)&cp->hrtc_flags,
289*0Sstevel@tonic-gate (caddr_t)&hrcmdp->hrtc_flags,
290*0Sstevel@tonic-gate sizeof (cp->hrtc_flags) + sizeof (cp->hrtc_error))) {
291*0Sstevel@tonic-gate error = EFAULT;
292*0Sstevel@tonic-gate return (error);
293*0Sstevel@tonic-gate }
294*0Sstevel@tonic-gate }
295*0Sstevel@tonic-gate rvp->r_val1 = alarm_cnt;
296*0Sstevel@tonic-gate return (0);
297*0Sstevel@tonic-gate }
298*0Sstevel@tonic-gate
299*0Sstevel@tonic-gate
300*0Sstevel@tonic-gate /*
301*0Sstevel@tonic-gate * Cancel BSD timers
302*0Sstevel@tonic-gate */
303*0Sstevel@tonic-gate
304*0Sstevel@tonic-gate static int
hrt_bsd_cancel(int clock)305*0Sstevel@tonic-gate hrt_bsd_cancel(int clock)
306*0Sstevel@tonic-gate {
307*0Sstevel@tonic-gate struct itimerval itv;
308*0Sstevel@tonic-gate u_int which;
309*0Sstevel@tonic-gate
310*0Sstevel@tonic-gate switch (clock) {
311*0Sstevel@tonic-gate case CLK_STD:
312*0Sstevel@tonic-gate which = ITIMER_REAL;
313*0Sstevel@tonic-gate break;
314*0Sstevel@tonic-gate case CLK_USERVIRT:
315*0Sstevel@tonic-gate which = ITIMER_VIRTUAL;
316*0Sstevel@tonic-gate break;
317*0Sstevel@tonic-gate case CLK_PROCVIRT:
318*0Sstevel@tonic-gate which = ITIMER_PROF;
319*0Sstevel@tonic-gate break;
320*0Sstevel@tonic-gate default:
321*0Sstevel@tonic-gate return (EINVAL);
322*0Sstevel@tonic-gate }
323*0Sstevel@tonic-gate itv.it_value.tv_sec = 0;
324*0Sstevel@tonic-gate itv.it_value.tv_usec = 0;
325*0Sstevel@tonic-gate (void) xsetitimer(which, &itv, 1);
326*0Sstevel@tonic-gate return (0);
327*0Sstevel@tonic-gate }
328*0Sstevel@tonic-gate
329*0Sstevel@tonic-gate
330*0Sstevel@tonic-gate /*
331*0Sstevel@tonic-gate * Return 0 if "res" is a legal resolution. Otherwise,
332*0Sstevel@tonic-gate * return an error code, ERANGE.
333*0Sstevel@tonic-gate */
334*0Sstevel@tonic-gate
335*0Sstevel@tonic-gate static int
hrt_checkres(ulong res)336*0Sstevel@tonic-gate hrt_checkres(ulong res)
337*0Sstevel@tonic-gate {
338*0Sstevel@tonic-gate if (res == 0 || res > NANOSEC)
339*0Sstevel@tonic-gate return (ERANGE);
340*0Sstevel@tonic-gate return (0);
341*0Sstevel@tonic-gate }
342*0Sstevel@tonic-gate
343*0Sstevel@tonic-gate /*
344*0Sstevel@tonic-gate * Return 0 if "clock" is a valid clock. Otherwise,
345*0Sstevel@tonic-gate * return an error code, EINVAL.
346*0Sstevel@tonic-gate */
347*0Sstevel@tonic-gate
348*0Sstevel@tonic-gate static int
hrt_checkclock(register int clock)349*0Sstevel@tonic-gate hrt_checkclock(register int clock)
350*0Sstevel@tonic-gate {
351*0Sstevel@tonic-gate switch (clock)
352*0Sstevel@tonic-gate case CLK_STD:
353*0Sstevel@tonic-gate case CLK_USERVIRT:
354*0Sstevel@tonic-gate case CLK_PROCVIRT:
355*0Sstevel@tonic-gate return (0);
356*0Sstevel@tonic-gate
357*0Sstevel@tonic-gate return (EINVAL);
358*0Sstevel@tonic-gate }
359*0Sstevel@tonic-gate
360*0Sstevel@tonic-gate
361*0Sstevel@tonic-gate /*
362*0Sstevel@tonic-gate * Set the current time of day in a specified resolution into
363*0Sstevel@tonic-gate * a hrtimes_t structure.
364*0Sstevel@tonic-gate */
365*0Sstevel@tonic-gate void
hrt_gettofd(hrtimes_t * td)366*0Sstevel@tonic-gate hrt_gettofd(hrtimes_t *td)
367*0Sstevel@tonic-gate {
368*0Sstevel@tonic-gate ulong new_res = td->hrt_res;
369*0Sstevel@tonic-gate timestruc_t ts;
370*0Sstevel@tonic-gate
371*0Sstevel@tonic-gate gethrestime(&ts);
372*0Sstevel@tonic-gate td->hrt_secs = ts.tv_sec;
373*0Sstevel@tonic-gate td->hrt_rem = ts.tv_nsec;
374*0Sstevel@tonic-gate td->hrt_res = NANOSEC;
375*0Sstevel@tonic-gate
376*0Sstevel@tonic-gate if (new_res != td->hrt_res) {
377*0Sstevel@tonic-gate td->hrt_rem /= NANOSEC / new_res;
378*0Sstevel@tonic-gate td->hrt_res = new_res;
379*0Sstevel@tonic-gate }
380*0Sstevel@tonic-gate }
381*0Sstevel@tonic-gate
382*0Sstevel@tonic-gate
383*0Sstevel@tonic-gate /*
384*0Sstevel@tonic-gate * System entry point for hrtcntl, hrtalarm
385*0Sstevel@tonic-gate * system calls.
386*0Sstevel@tonic-gate */
387*0Sstevel@tonic-gate
388*0Sstevel@tonic-gate int
hrtsys(uap,rvp)389*0Sstevel@tonic-gate hrtsys(uap, rvp)
390*0Sstevel@tonic-gate register struct hrtsysa *uap;
391*0Sstevel@tonic-gate rval_t *rvp;
392*0Sstevel@tonic-gate {
393*0Sstevel@tonic-gate register int error;
394*0Sstevel@tonic-gate
395*0Sstevel@tonic-gate switch (uap->opcode) {
396*0Sstevel@tonic-gate case HRTCNTL:
397*0Sstevel@tonic-gate error = hrtcntl((struct hrtcntla *)uap, rvp);
398*0Sstevel@tonic-gate break;
399*0Sstevel@tonic-gate case HRTALARM:
400*0Sstevel@tonic-gate error = hrtalarm((struct hrtalarma *)uap, rvp);
401*0Sstevel@tonic-gate break;
402*0Sstevel@tonic-gate default:
403*0Sstevel@tonic-gate error = EINVAL;
404*0Sstevel@tonic-gate break;
405*0Sstevel@tonic-gate }
406*0Sstevel@tonic-gate
407*0Sstevel@tonic-gate return (error);
408*0Sstevel@tonic-gate }
409