1 /* $NetBSD: mach_clock.c,v 1.13 2005/02/26 23:10:19 perry Exp $ */
2
3 /*-
4 * Copyright (c) 2002-2003 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Emmanuel Dreyfus
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: mach_clock.c,v 1.13 2005/02/26 23:10:19 perry Exp $");
41
42 #include <sys/types.h>
43 #include <sys/param.h>
44 #include <sys/kernel.h>
45 #include <sys/systm.h>
46 #include <sys/signal.h>
47 #include <sys/proc.h>
48 #include <sys/time.h>
49
50 #include <compat/mach/mach_types.h>
51 #include <compat/mach/mach_message.h>
52 #include <compat/mach/mach_port.h>
53 #include <compat/mach/mach_clock.h>
54 #include <compat/mach/mach_services.h>
55 #include <compat/mach/mach_syscallargs.h>
56
57 int
58 mach_sys_clock_sleep_trap(l, v, retval)
59 struct lwp *l;
60 void *v;
61 register_t *retval;
62 {
63 struct mach_sys_clock_sleep_trap_args /* {
64 syscallarg(mach_clock_port_t) clock_name;
65 syscallarg(mach_sleep_type_t) sleep_type;
66 syscallarg(int) sleep_sec;
67 syscallarg(int) sleep_nsec;
68 syscallarg(mach_timespec_t *) wakeup_time;
69 } */ *uap = v;
70 struct timespec mts, cts, tts;
71 struct timeval now;
72 mach_timespec_t mcts;
73 int dontcare;
74 int error;
75 int ticks;
76
77 mts.tv_sec = SCARG(uap, sleep_sec);
78 mts.tv_nsec = SCARG(uap, sleep_nsec);
79
80 if (SCARG(uap, sleep_type) == MACH_TIME_ABSOLUTE) {
81 microtime(&now);
82 TIMEVAL_TO_TIMESPEC(&now, &cts);
83 timespecsub(&mts, &cts, &tts);
84 } else {
85 tts.tv_sec = mts.tv_sec;
86 tts.tv_nsec = mts.tv_nsec;
87 }
88
89 ticks = tts.tv_sec * hz;
90 ticks += (tts.tv_nsec * hz) / 100000000L;
91
92 tsleep(&dontcare, PZERO|PCATCH, "sleep", ticks);
93
94 if (SCARG(uap, wakeup_time) != NULL) {
95 microtime(&now);
96 TIMEVAL_TO_TIMESPEC(&now, &cts);
97 mcts.tv_sec = cts.tv_sec;
98 mcts.tv_nsec = cts.tv_nsec;
99 error = copyout(&mcts, SCARG(uap, wakeup_time), sizeof(mcts));
100 if (error != 0)
101 return error;
102 }
103
104 return 0;
105 }
106
107 int
108 mach_sys_timebase_info(l, v, retval)
109 struct lwp *l;
110 void *v;
111 register_t *retval;
112 {
113 struct mach_sys_timebase_info_args /*
114 syscallarg(mach_timebase_info_t) info;
115 */ *uap = v;
116 int error;
117 struct mach_timebase_info info;
118
119 /* XXX This is probably bus speed, fill it accurately */
120 info.numer = 4000000000UL;
121 info.denom = 75189611UL;
122
123 if ((error = copyout(&info, (void *)SCARG(uap, info),
124 sizeof(info))) != 0)
125 return error;
126
127 return 0;
128 }
129
130
131
132 int
133 mach_clock_get_time(args)
134 struct mach_trap_args *args;
135 {
136 mach_clock_get_time_request_t *req = args->smsg;
137 mach_clock_get_time_reply_t *rep = args->rmsg;
138 size_t *msglen = args->rsize;
139 struct timeval tv;
140
141 microtime(&tv);
142
143 *msglen = sizeof(*rep);
144 mach_set_header(rep, req, *msglen);
145
146 rep->rep_cur_time.tv_sec = tv.tv_sec;
147 rep->rep_cur_time.tv_nsec = tv.tv_usec * 1000;
148
149 mach_set_trailer(rep, *msglen);
150
151 return 0;
152 }
Cache object: fb6433c87a99df6faaca2998df1493bc
|