1 /* $NetBSD: mach_clock.c,v 1.19 2008/04/28 20:23:44 martin 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 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: mach_clock.c,v 1.19 2008/04/28 20:23:44 martin Exp $");
34
35 #include <sys/types.h>
36 #include <sys/param.h>
37 #include <sys/kernel.h>
38 #include <sys/systm.h>
39 #include <sys/signal.h>
40 #include <sys/proc.h>
41 #include <sys/time.h>
42
43 #include <compat/mach/mach_types.h>
44 #include <compat/mach/mach_message.h>
45 #include <compat/mach/mach_port.h>
46 #include <compat/mach/mach_clock.h>
47 #include <compat/mach/mach_services.h>
48 #include <compat/mach/mach_syscallargs.h>
49
50 int
51 mach_sys_clock_sleep_trap(struct lwp *l, const struct mach_sys_clock_sleep_trap_args *uap, register_t *retval)
52 {
53 /* {
54 syscallarg(mach_clock_port_t) clock_name;
55 syscallarg(mach_sleep_type_t) sleep_type;
56 syscallarg(int) sleep_sec;
57 syscallarg(int) sleep_nsec;
58 syscallarg(mach_timespec_t *) wakeup_time;
59 } */
60 struct timespec mts, cts, tts;
61 mach_timespec_t mcts;
62 int dontcare;
63 int error;
64 int ticks;
65
66 mts.tv_sec = SCARG(uap, sleep_sec);
67 mts.tv_nsec = SCARG(uap, sleep_nsec);
68
69 if (SCARG(uap, sleep_type) == MACH_TIME_ABSOLUTE) {
70 nanotime(&cts);
71 timespecsub(&mts, &cts, &tts);
72 } else {
73 tts.tv_sec = mts.tv_sec;
74 tts.tv_nsec = mts.tv_nsec;
75 }
76
77 ticks = tts.tv_sec * hz;
78 ticks += (tts.tv_nsec * hz) / 100000000L;
79
80 tsleep(&dontcare, PZERO|PCATCH, "sleep", ticks);
81
82 if (SCARG(uap, wakeup_time) != NULL) {
83 nanotime(&cts);
84 mcts.tv_sec = cts.tv_sec;
85 mcts.tv_nsec = cts.tv_nsec;
86 error = copyout(&mcts, SCARG(uap, wakeup_time), sizeof(mcts));
87 if (error != 0)
88 return error;
89 }
90
91 return 0;
92 }
93
94 int
95 mach_sys_timebase_info(struct lwp *l, const struct mach_sys_timebase_info_args *uap, register_t *retval)
96 {
97 /* {
98 syscallarg(mach_timebase_info_t) info;
99 } */
100 int error;
101 struct mach_timebase_info info;
102
103 /* XXX This is probably bus speed, fill it accurately */
104 info.numer = 4000000000UL;
105 info.denom = 75189611UL;
106
107 if ((error = copyout(&info, (void *)SCARG(uap, info),
108 sizeof(info))) != 0)
109 return error;
110
111 return 0;
112 }
113
114
115
116 int
117 mach_clock_get_time(struct mach_trap_args *args)
118 {
119 mach_clock_get_time_request_t *req = args->smsg;
120 mach_clock_get_time_reply_t *rep = args->rmsg;
121 size_t *msglen = args->rsize;
122 struct timeval tv;
123
124 microtime(&tv);
125
126 *msglen = sizeof(*rep);
127 mach_set_header(rep, req, *msglen);
128
129 rep->rep_cur_time.tv_sec = tv.tv_sec;
130 rep->rep_cur_time.tv_nsec = tv.tv_usec * 1000;
131
132 mach_set_trailer(rep, *msglen);
133
134 return 0;
135 }
Cache object: 8a50245e7fbdafb25f94f231c874d53b
|