]> git.saurik.com Git - apple/xnu.git/blob - osfmk/device/iokit_rpc.c
637047f7ecc7ccdc7595bd444ea6db571c086ae1
[apple/xnu.git] / osfmk / device / iokit_rpc.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 #include <mach_kdb.h>
23 #include <zone_debug.h>
24 #include <mach_kdb.h>
25
26 #include <mach/boolean.h>
27 #include <mach/kern_return.h>
28 #include <mach/mig_errors.h>
29 #include <mach/port.h>
30 #include <mach/vm_param.h>
31 #include <mach/notify.h>
32 #include <mach/mach_host_server.h>
33 #include <mach/mach_types.h>
34
35 #include <machine/machparam.h> /* spl definitions */
36
37 #include <ipc/ipc_port.h>
38 #include <ipc/ipc_space.h>
39
40 #include <kern/clock.h>
41 #include <kern/spl.h>
42 #include <kern/ast.h>
43 #include <kern/counters.h>
44 #include <kern/queue.h>
45 #include <kern/zalloc.h>
46 #include <kern/thread.h>
47 #include <kern/thread_swap.h>
48 #include <kern/task.h>
49 #include <kern/sched_prim.h>
50 #include <kern/misc_protos.h>
51
52 #include <vm/pmap.h>
53 #include <vm/vm_map.h>
54 #include <vm/vm_kern.h>
55
56 #include <device/device_types.h>
57 #include <device/device_port.h>
58 #include <device/device_server.h>
59
60 #include <sys/ioctl.h>
61
62 #include <machine/machparam.h>
63
64 #ifdef __ppc__
65 #include <ppc/mappings.h>
66 #include <ppc/pmap_internals.h>
67 #endif
68 #include <IOKit/IOTypes.h>
69
70 #define EXTERN
71 #define MIGEXTERN
72
73 /*
74 * Functions in iokit:IOUserClient.cpp
75 */
76
77 extern void iokit_add_reference( io_object_t obj );
78
79 extern void iokit_remove_reference( io_object_t obj );
80
81 extern ipc_port_t iokit_port_for_object( io_object_t obj,
82 ipc_kobject_type_t type );
83
84 extern kern_return_t iokit_client_died( io_object_t obj,
85 ipc_port_t port, ipc_kobject_type_t type );
86
87 extern kern_return_t
88 iokit_client_memory_for_type(
89 io_object_t connect,
90 unsigned int type,
91 unsigned int * flags,
92 vm_address_t * address,
93 vm_size_t * size );
94
95 /*
96 * Lookup a device by its port.
97 * Doesn't consume the naked send right; produces a device reference.
98 */
99 MIGEXTERN io_object_t
100 iokit_lookup_object_port(
101 ipc_port_t port)
102 {
103 register io_object_t obj;
104
105 if (!IP_VALID(port))
106 return (NULL);
107
108 ip_lock(port);
109 if (ip_active(port) && (ip_kotype(port) == IKOT_IOKIT_OBJECT)) {
110 obj = (io_object_t) port->ip_kobject;
111 iokit_add_reference( obj );
112 }
113 else
114 obj = NULL;
115
116 ip_unlock(port);
117
118 return( obj );
119 }
120
121 MIGEXTERN io_object_t
122 iokit_lookup_connect_port(
123 ipc_port_t port)
124 {
125 register io_object_t obj;
126
127 if (!IP_VALID(port))
128 return (NULL);
129
130 ip_lock(port);
131 if (ip_active(port) && (ip_kotype(port) == IKOT_IOKIT_CONNECT)) {
132 obj = (io_object_t) port->ip_kobject;
133 iokit_add_reference( obj );
134 }
135 else
136 obj = NULL;
137
138 ip_unlock(port);
139
140 return( obj );
141 }
142
143 EXTERN io_object_t
144 iokit_lookup_connect_ref(io_object_t connectRef, ipc_space_t space)
145 {
146 io_object_t obj = NULL;
147
148 if (connectRef && MACH_PORT_VALID((mach_port_name_t)connectRef)) {
149 ipc_port_t port;
150 kern_return_t kr;
151
152 kr = ipc_object_translate(space, (mach_port_name_t)connectRef, MACH_PORT_RIGHT_SEND, (ipc_object_t *)&port);
153
154 if (kr == KERN_SUCCESS) {
155 assert(IP_VALID(port));
156
157 if (ip_active(port) && (ip_kotype(port) == IKOT_IOKIT_CONNECT)) {
158 obj = (io_object_t) port->ip_kobject;
159 iokit_add_reference(obj);
160 }
161
162 ip_unlock(port);
163 }
164 }
165
166 return obj;
167 }
168
169 EXTERN io_object_t
170 iokit_lookup_connect_ref_current_task(io_object_t connectRef)
171 {
172 return iokit_lookup_connect_ref(connectRef, current_space());
173 }
174
175 /*
176 * Get the port for a device.
177 * Consumes a device reference; produces a naked send right.
178 */
179 MIGEXTERN ipc_port_t
180 iokit_make_object_port(
181 io_object_t obj )
182 {
183 register ipc_port_t port;
184
185 if( obj == NULL)
186 return IP_NULL;
187
188 port = iokit_port_for_object( obj, IKOT_IOKIT_OBJECT );
189 if( port)
190 port = ipc_port_make_send( port);
191
192 iokit_remove_reference( obj );
193
194 return( port);
195 }
196
197 MIGEXTERN ipc_port_t
198 iokit_make_connect_port(
199 io_object_t obj )
200 {
201 register ipc_port_t port;
202
203 if( obj == NULL)
204 return IP_NULL;
205
206 port = iokit_port_for_object( obj, IKOT_IOKIT_CONNECT );
207 if( port)
208 port = ipc_port_make_send( port);
209
210 iokit_remove_reference( obj );
211
212 return( port);
213 }
214
215
216 EXTERN ipc_port_t
217 iokit_alloc_object_port( io_object_t obj, ipc_kobject_type_t type );
218
219 int gIOKitPortCount;
220
221 EXTERN ipc_port_t
222 iokit_alloc_object_port( io_object_t obj, ipc_kobject_type_t type )
223 {
224 ipc_port_t notify;
225 ipc_port_t port;
226
227 do {
228
229 /* Allocate port, keeping a reference for it. */
230 port = ipc_port_alloc_kernel();
231 if( port == IP_NULL)
232 continue;
233
234 /* set kobject & type */
235 // iokit_add_reference( obj );
236 ipc_kobject_set( port, (ipc_kobject_t) obj, type);
237
238 /* Request no-senders notifications on the port. */
239 notify = ipc_port_make_sonce( port);
240 ip_lock( port);
241 ipc_port_nsrequest( port, 1, notify, &notify);
242 assert( notify == IP_NULL);
243 gIOKitPortCount++;
244
245 } while( FALSE);
246
247 return( port );
248 }
249
250
251 EXTERN kern_return_t
252 iokit_destroy_object_port( ipc_port_t port )
253 {
254 ipc_kobject_set( port, IKO_NULL, IKOT_NONE);
255
256 // iokit_remove_reference( obj );
257
258 ipc_port_dealloc_kernel( port);
259 gIOKitPortCount--;
260
261 return( KERN_SUCCESS);
262 }
263
264 EXTERN mach_port_name_t
265 iokit_make_send_right( task_t task, io_object_t obj, ipc_kobject_type_t type )
266 {
267 kern_return_t kr;
268 ipc_port_t port;
269 mach_port_name_t name;
270
271 if( obj == NULL)
272 return MACH_PORT_NULL;
273
274 port = iokit_port_for_object( obj, type );
275 if( port)
276 port = ipc_port_make_send( port);
277 if( port == IP_NULL)
278 return MACH_PORT_NULL;
279
280 kr = ipc_object_copyout( task->itk_space, (ipc_object_t) port,
281 MACH_MSG_TYPE_PORT_SEND, TRUE, &name);
282
283 if( kr != KERN_SUCCESS)
284 name = MACH_PORT_NULL;
285
286 iokit_remove_reference( obj );
287
288 return( name );
289 }
290
291 /*
292 * Handle the No-More_Senders notification generated from a device port destroy.
293 * Since there are no longer any tasks which hold a send right to this device
294 * port a NMS notification has been generated.
295 */
296
297 static void
298 iokit_no_senders( mach_no_senders_notification_t * notification )
299 {
300 ipc_port_t port;
301 io_object_t obj = NULL;
302 ipc_kobject_type_t type;
303
304 port = (ipc_port_t) notification->not_header.msgh_remote_port;
305
306 // convert a port to io_object_t.
307 if( IP_VALID(port)) {
308 ip_lock(port);
309 if( ip_active(port)) {
310 obj = (io_object_t) port->ip_kobject;
311 type = ip_kotype( port );
312 if( (IKOT_IOKIT_OBJECT == type)
313 || (IKOT_IOKIT_CONNECT == type))
314 iokit_add_reference( obj );
315 else
316 obj = NULL;
317 }
318 ip_unlock(port);
319
320 if( obj ) {
321 (void) iokit_client_died( obj, port, type );
322 iokit_remove_reference( obj );
323 }
324 }
325 }
326
327
328 EXTERN
329 boolean_t
330 iokit_notify( mach_msg_header_t * msg )
331 {
332 switch (msg->msgh_id) {
333 case MACH_NOTIFY_NO_SENDERS:
334 iokit_no_senders((mach_no_senders_notification_t *) msg);
335 return TRUE;
336
337 case MACH_NOTIFY_PORT_DELETED:
338 case MACH_NOTIFY_PORT_DESTROYED:
339 case MACH_NOTIFY_SEND_ONCE:
340 case MACH_NOTIFY_DEAD_NAME:
341 default:
342 printf("iokit_notify: strange notification %ld\n", msg->msgh_id);
343 return FALSE;
344 }
345 }
346
347 kern_return_t IOMapPages(vm_map_t map, vm_offset_t va, vm_offset_t pa,
348 vm_size_t length, unsigned int options)
349 {
350 vm_size_t off;
351 vm_prot_t prot;
352 int memattr;
353 struct phys_entry *pp;
354 pmap_t pmap = map->pmap;
355
356 prot = (options & kIOMapReadOnly)
357 ? VM_PROT_READ : (VM_PROT_READ|VM_PROT_WRITE);
358
359 #if __ppc__
360
361 switch(options & kIOMapCacheMask ) { /* What cache mode do we need? */
362
363 case kIOMapDefaultCache:
364 default:
365 if(pp = pmap_find_physentry(pa)) { /* Find physical address */
366 memattr = ((pp->pte1 & 0x00000078) >> 3); /* Use physical attributes as default */
367 }
368 else { /* If no physical, just hard code attributes */
369 memattr = PTE_WIMG_UNCACHED_COHERENT_GUARDED;
370 }
371 break;
372
373 case kIOMapInhibitCache:
374 memattr = PTE_WIMG_UNCACHED_COHERENT_GUARDED;
375 break;
376
377 case kIOMapWriteThruCache:
378 memattr = PTE_WIMG_WT_CACHED_COHERENT_GUARDED;
379 break;
380
381 case kIOMapCopybackCache:
382 memattr = PTE_WIMG_CB_CACHED_COHERENT;
383 break;
384 }
385
386 pmap_map_block(pmap, va, pa, length, prot, memattr, 0); /* Set up a block mapped area */
387
388 #else
389 // enter each page's physical address in the target map
390 for (off = 0; off < length; off += page_size) { /* Loop for the whole length */
391 pmap_enter(pmap, va + off, pa + off, prot, TRUE); /* Map it in */
392 }
393 #endif
394
395 return( KERN_SUCCESS );
396 }
397
398 void IOGetTime( mach_timespec_t * clock_time);
399 void IOGetTime( mach_timespec_t * clock_time)
400 {
401 *clock_time = clock_get_system_value();
402 }