]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/ipc_clock.c
xnu-792.12.6.tar.gz
[apple/xnu.git] / osfmk / kern / ipc_clock.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30 /*
31 * @OSF_COPYRIGHT@
32 */
33 /*
34 * File: kern/ipc_clock.c
35 * Purpose: Routines to support ipc semantics of new kernel
36 * alarm clock facility.
37 */
38
39 #include <mach/message.h>
40 #include <kern/host.h>
41 #include <kern/processor.h>
42 #include <kern/task.h>
43 #include <kern/thread.h>
44 #include <kern/ipc_host.h>
45 #include <kern/ipc_kobject.h>
46 #include <kern/clock.h>
47 #include <kern/misc_protos.h>
48 #include <ipc/ipc_port.h>
49 #include <ipc/ipc_space.h>
50
51 /*
52 * Routine: ipc_clock_init
53 * Purpose:
54 * Initialize ipc control of a clock.
55 */
56 void
57 ipc_clock_init(
58 clock_t clock)
59 {
60 ipc_port_t port;
61
62 port = ipc_port_alloc_kernel();
63 if (port == IP_NULL)
64 panic("ipc_clock_init");
65 clock->cl_service = port;
66
67 port = ipc_port_alloc_kernel();
68 if (port == IP_NULL)
69 panic("ipc_clock_init");
70 clock->cl_control = port;
71 }
72
73 /*
74 * Routine: ipc_clock_enable
75 * Purpose:
76 * Enable ipc access to a clock.
77 */
78 void
79 ipc_clock_enable(
80 clock_t clock)
81 {
82 ipc_kobject_set(clock->cl_service,
83 (ipc_kobject_t) clock, IKOT_CLOCK);
84 ipc_kobject_set(clock->cl_control,
85 (ipc_kobject_t) clock, IKOT_CLOCK_CTRL);
86 }
87
88 /*
89 * Routine: convert_port_to_clock
90 * Purpose:
91 * Convert from a port to a clock.
92 * Doesn't consume the port ref; produces a clock ref,
93 * which may be null.
94 * Conditions:
95 * Nothing locked.
96 */
97 clock_t
98 convert_port_to_clock(
99 ipc_port_t port)
100 {
101 clock_t clock = CLOCK_NULL;
102
103 if (IP_VALID(port)) {
104 ip_lock(port);
105 if (ip_active(port) &&
106 ((ip_kotype(port) == IKOT_CLOCK) ||
107 (ip_kotype(port) == IKOT_CLOCK_CTRL))) {
108 clock = (clock_t) port->ip_kobject;
109 }
110 ip_unlock(port);
111 }
112 return (clock);
113 }
114
115 /*
116 * Routine: convert_port_to_clock_ctrl
117 * Purpose:
118 * Convert from a port to a clock.
119 * Doesn't consume the port ref; produces a clock ref,
120 * which may be null.
121 * Conditions:
122 * Nothing locked.
123 */
124 clock_t
125 convert_port_to_clock_ctrl(
126 ipc_port_t port)
127 {
128 clock_t clock = CLOCK_NULL;
129
130 if (IP_VALID(port)) {
131 ip_lock(port);
132 if (ip_active(port) &&
133 (ip_kotype(port) == IKOT_CLOCK_CTRL)) {
134 clock = (clock_t) port->ip_kobject;
135 }
136 ip_unlock(port);
137 }
138 return (clock);
139 }
140
141 /*
142 * Routine: convert_clock_to_port
143 * Purpose:
144 * Convert from a clock to a port.
145 * Produces a naked send right which may be invalid.
146 * Conditions:
147 * Nothing locked.
148 */
149 ipc_port_t
150 convert_clock_to_port(
151 clock_t clock)
152 {
153 ipc_port_t port;
154
155 port = ipc_port_make_send(clock->cl_service);
156 return (port);
157 }
158
159 /*
160 * Routine: convert_clock_ctrl_to_port
161 * Purpose:
162 * Convert from a clock to a port.
163 * Produces a naked send right which may be invalid.
164 * Conditions:
165 * Nothing locked.
166 */
167 ipc_port_t
168 convert_clock_ctrl_to_port(
169 clock_t clock)
170 {
171 ipc_port_t port;
172
173 port = ipc_port_make_send(clock->cl_control);
174 return (port);
175 }
176
177 /*
178 * Routine: port_name_to_clock
179 * Purpose:
180 * Convert from a clock name to a clock pointer.
181 */
182 clock_t
183 port_name_to_clock(
184 mach_port_name_t clock_name)
185 {
186 clock_t clock = CLOCK_NULL;
187 ipc_space_t space;
188 ipc_port_t port;
189
190 if (clock_name == 0)
191 return (clock);
192 space = current_space();
193 if (ipc_port_translate_send(space, clock_name, &port) != KERN_SUCCESS)
194 return (clock);
195 if (ip_active(port) && (ip_kotype(port) == IKOT_CLOCK))
196 clock = (clock_t) port->ip_kobject;
197 ip_unlock(port);
198 return (clock);
199 }