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