1 /* $NetBSD: mach_clock.c,v 1.16 2006/11/16 01:32:44 christos 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.16 2006/11/16 01:32:44 christos 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(struct lwp *l, void *v,
59 register_t *retval)
60 {
61 struct mach_sys_clock_sleep_trap_args /* {
62 syscallarg(mach_clock_port_t) clock_name;
63 syscallarg(mach_sleep_type_t) sleep_type;
64 syscallarg(int) sleep_sec;
65 syscallarg(int) sleep_nsec;
66 syscallarg(mach_timespec_t *) wakeup_time;
67 } */ *uap = v;
68 struct timespec mts, cts, tts;
69 mach_timespec_t mcts;
70 int dontcare;
71 int error;
72 int ticks;
73
74 mts.tv_sec = SCARG(uap, sleep_sec);
75 mts.tv_nsec = SCARG(uap, sleep_nsec);
76
77 if (SCARG(uap, sleep_type) == MACH_TIME_ABSOLUTE) {
78 nanotime(&cts);
79 timespecsub(&mts, &cts, &tts);
80 } else {
81 tts.tv_sec = mts.tv_sec;
82 tts.tv_nsec = mts.tv_nsec;
83 }
84
85 ticks = tts.tv_sec * hz;
86 ticks += (tts.tv_nsec * hz) / 100000000L;
87
88 tsleep(&dontcare, PZERO|PCATCH, "sleep", ticks);
89
90 if (SCARG(uap, wakeup_time) != NULL) {
91 nanotime(&cts);
92 mcts.tv_sec = cts.tv_sec;
93 mcts.tv_nsec = cts.tv_nsec;
94 error = copyout(&mcts, SCARG(uap, wakeup_time), sizeof(mcts));
95 if (error != 0)
96 return error;
97 }
98
99 return 0;
100 }
101
102 int
103 mach_sys_timebase_info(struct lwp *l, void *v,
104 register_t *retval)
105 {
106 struct mach_sys_timebase_info_args /*
107 syscallarg(mach_timebase_info_t) info;
108 */ *uap = v;
109 int error;
110 struct mach_timebase_info info;
111
112 /* XXX This is probably bus speed, fill it accurately */
113 info.numer = 4000000000UL;
114 info.denom = 75189611UL;
115
116 if ((error = copyout(&info, (void *)SCARG(uap, info),
117 sizeof(info))) != 0)
118 return error;
119
120 return 0;
121 }
122
123
124
125 int
126 mach_clock_get_time(args)
127 struct mach_trap_args *args;
128 {
129 mach_clock_get_time_request_t *req = args->smsg;
130 mach_clock_get_time_reply_t *rep = args->rmsg;
131 size_t *msglen = args->rsize;
132 struct timeval tv;
133
134 microtime(&tv);
135
136 *msglen = sizeof(*rep);
137 mach_set_header(rep, req, *msglen);
138
139 rep->rep_cur_time.tv_sec = tv.tv_sec;
140 rep->rep_cur_time.tv_nsec = tv.tv_usec * 1000;
141
142 mach_set_trailer(rep, *msglen);
143
144 return 0;
145 }
Cache object: de7feab9f27edfae34a8e180e77a2235
|