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