]> git.saurik.com Git - apple/xnu.git/blob - osfmk/device/iokit_rpc.c
0662600fe5f2734d15ed8b331a47969243f15ef2
[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 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 #include <mach_kdb.h>
26 #include <zone_debug.h>
27 #include <mach_kdb.h>
28
29 #include <mach/boolean.h>
30 #include <mach/kern_return.h>
31 #include <mach/mig_errors.h>
32 #include <mach/port.h>
33 #include <mach/vm_param.h>
34 #include <mach/notify.h>
35 #include <mach/mach_host_server.h>
36 #include <mach/mach_types.h>
37
38 #include <machine/machparam.h> /* spl definitions */
39
40 #include <ipc/ipc_port.h>
41 #include <ipc/ipc_space.h>
42
43 #include <kern/clock.h>
44 #include <kern/spl.h>
45 #include <kern/ast.h>
46 #include <kern/counters.h>
47 #include <kern/queue.h>
48 #include <kern/zalloc.h>
49 #include <kern/thread.h>
50 #include <kern/thread_swap.h>
51 #include <kern/task.h>
52 #include <kern/sched_prim.h>
53 #include <kern/misc_protos.h>
54
55 #include <vm/pmap.h>
56 #include <vm/vm_map.h>
57 #include <vm/vm_kern.h>
58
59 #include <device/device_types.h>
60 #include <device/device_port.h>
61 #include <device/device_server.h>
62
63 #include <machine/machparam.h>
64
65 #ifdef __ppc__
66 #include <ppc/mappings.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, mach_port_mscount_t * mscount );
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 EXTERN void
176 iokit_retain_port( ipc_port_t port )
177 {
178 ipc_port_reference( port );
179 }
180
181 EXTERN void
182 iokit_release_port( ipc_port_t port )
183 {
184 ipc_port_release( port );
185 }
186
187 /*
188 * Get the port for a device.
189 * Consumes a device reference; produces a naked send right.
190 */
191 MIGEXTERN ipc_port_t
192 iokit_make_object_port(
193 io_object_t obj )
194 {
195 register ipc_port_t port;
196 register ipc_port_t sendPort;
197
198 if( obj == NULL)
199 return IP_NULL;
200
201 port = iokit_port_for_object( obj, IKOT_IOKIT_OBJECT );
202 if( port) {
203 sendPort = ipc_port_make_send( port);
204 iokit_release_port( port );
205 } else
206 sendPort = IP_NULL;
207
208 iokit_remove_reference( obj );
209
210 return( sendPort);
211 }
212
213 MIGEXTERN ipc_port_t
214 iokit_make_connect_port(
215 io_object_t obj )
216 {
217 register ipc_port_t port;
218 register ipc_port_t sendPort;
219
220 if( obj == NULL)
221 return IP_NULL;
222
223 port = iokit_port_for_object( obj, IKOT_IOKIT_CONNECT );
224 if( port) {
225 sendPort = ipc_port_make_send( port);
226 iokit_release_port( port );
227 } else
228 sendPort = IP_NULL;
229
230 iokit_remove_reference( obj );
231
232 return( sendPort);
233 }
234
235
236 EXTERN ipc_port_t
237 iokit_alloc_object_port( io_object_t obj, ipc_kobject_type_t type );
238
239 int gIOKitPortCount;
240
241 EXTERN ipc_port_t
242 iokit_alloc_object_port( io_object_t obj, ipc_kobject_type_t type )
243 {
244 ipc_port_t notify;
245 ipc_port_t port;
246
247 do {
248
249 /* Allocate port, keeping a reference for it. */
250 port = ipc_port_alloc_kernel();
251 if( port == IP_NULL)
252 continue;
253
254 /* set kobject & type */
255 // iokit_add_reference( obj );
256 ipc_kobject_set( port, (ipc_kobject_t) obj, type);
257
258 /* Request no-senders notifications on the port. */
259 notify = ipc_port_make_sonce( port);
260 ip_lock( port);
261 ipc_port_nsrequest( port, 1, notify, &notify);
262 assert( notify == IP_NULL);
263 gIOKitPortCount++;
264
265 } while( FALSE);
266
267 return( port );
268 }
269
270
271 EXTERN kern_return_t
272 iokit_destroy_object_port( ipc_port_t port )
273 {
274 ipc_kobject_set( port, IKO_NULL, IKOT_NONE);
275
276 // iokit_remove_reference( obj );
277
278 ipc_port_dealloc_kernel( port);
279 gIOKitPortCount--;
280
281 return( KERN_SUCCESS);
282 }
283
284 EXTERN kern_return_t
285 iokit_switch_object_port( ipc_port_t port, io_object_t obj, ipc_kobject_type_t type )
286 {
287 ipc_kobject_set( port, (ipc_kobject_t) obj, type);
288
289 return( KERN_SUCCESS);
290 }
291
292 EXTERN mach_port_name_t
293 iokit_make_send_right( task_t task, io_object_t obj, ipc_kobject_type_t type )
294 {
295 ipc_port_t port;
296 ipc_port_t sendPort;
297 mach_port_name_t name;
298
299 if( obj == NULL)
300 return MACH_PORT_NULL;
301
302 port = iokit_port_for_object( obj, type );
303 if( port) {
304 sendPort = ipc_port_make_send( port);
305 iokit_release_port( port );
306 } else
307 sendPort = IP_NULL;
308
309 if (IP_VALID( sendPort )) {
310 kern_return_t kr;
311 kr = ipc_object_copyout( task->itk_space, (ipc_object_t) sendPort,
312 MACH_MSG_TYPE_PORT_SEND, TRUE, &name);
313 if ( kr != KERN_SUCCESS)
314 name = MACH_PORT_NULL;
315 } else if ( sendPort == IP_NULL)
316 name = MACH_PORT_NULL;
317 else if ( sendPort == IP_DEAD)
318 name = MACH_PORT_DEAD;
319
320 iokit_remove_reference( obj );
321
322 return( name );
323 }
324
325 /*
326 * Handle the No-More_Senders notification generated from a device port destroy.
327 * Since there are no longer any tasks which hold a send right to this device
328 * port a NMS notification has been generated.
329 */
330
331 static void
332 iokit_no_senders( mach_no_senders_notification_t * notification )
333 {
334 ipc_port_t port;
335 io_object_t obj = NULL;
336 ipc_kobject_type_t type;
337 ipc_port_t notify;
338
339 port = (ipc_port_t) notification->not_header.msgh_remote_port;
340
341 // convert a port to io_object_t.
342 if( IP_VALID(port)) {
343 ip_lock(port);
344 if( ip_active(port)) {
345 obj = (io_object_t) port->ip_kobject;
346 type = ip_kotype( port );
347 if( (IKOT_IOKIT_OBJECT == type)
348 || (IKOT_IOKIT_CONNECT == type))
349 iokit_add_reference( obj );
350 else
351 obj = NULL;
352 }
353 ip_unlock(port);
354
355 if( obj ) {
356
357 mach_port_mscount_t mscount = notification->not_count;
358
359 if( KERN_SUCCESS != iokit_client_died( obj, port, type, &mscount ))
360 {
361 /* Re-request no-senders notifications on the port. */
362 notify = ipc_port_make_sonce( port);
363 ip_lock( port);
364 ipc_port_nsrequest( port, mscount + 1, notify, &notify);
365 assert( notify == IP_NULL);
366 }
367 iokit_remove_reference( obj );
368 }
369 }
370 }
371
372
373 EXTERN
374 boolean_t
375 iokit_notify( mach_msg_header_t * msg )
376 {
377 switch (msg->msgh_id) {
378 case MACH_NOTIFY_NO_SENDERS:
379 iokit_no_senders((mach_no_senders_notification_t *) msg);
380 return TRUE;
381
382 case MACH_NOTIFY_PORT_DELETED:
383 case MACH_NOTIFY_PORT_DESTROYED:
384 case MACH_NOTIFY_SEND_ONCE:
385 case MACH_NOTIFY_DEAD_NAME:
386 default:
387 printf("iokit_notify: strange notification %ld\n", msg->msgh_id);
388 return FALSE;
389 }
390 }
391
392 /* need to create a pmap function to generalize */
393 unsigned int IODefaultCacheBits(addr64_t pa)
394 {
395 unsigned int flags;
396 #ifndef i386
397 struct phys_entry * pp;
398
399 // Find physical address
400 if ((pp = pmap_find_physentry(pa >> 12))) {
401 // Use physical attributes as default
402 // NOTE: DEVICE_PAGER_FLAGS are made to line up
403 flags = VM_MEM_COHERENT; /* We only support coherent memory */
404 if(pp->ppLink & ppG) flags |= VM_MEM_GUARDED; /* Add in guarded if it is */
405 if(pp->ppLink & ppI) flags |= VM_MEM_NOT_CACHEABLE; /* Add in cache inhibited if so */
406 } else
407 // If no physical, just hard code attributes
408 flags = VM_WIMG_IO;
409 #else
410 extern vm_offset_t avail_end;
411
412 if (pa < avail_end)
413 flags = VM_WIMG_COPYBACK;
414 else
415 flags = VM_WIMG_IO;
416 #endif
417
418 return flags;
419 }
420
421 kern_return_t IOMapPages(vm_map_t map, vm_offset_t va, vm_offset_t pa,
422 vm_size_t length, unsigned int options)
423 {
424 vm_size_t off;
425 vm_prot_t prot;
426 unsigned int flags;
427 pmap_t pmap = map->pmap;
428
429 prot = (options & kIOMapReadOnly)
430 ? VM_PROT_READ : (VM_PROT_READ|VM_PROT_WRITE);
431
432 switch(options & kIOMapCacheMask ) { /* What cache mode do we need? */
433
434 case kIOMapDefaultCache:
435 default:
436 flags = IODefaultCacheBits(pa);
437 break;
438
439 case kIOMapInhibitCache:
440 flags = VM_WIMG_IO;
441 break;
442
443 case kIOMapWriteThruCache:
444 flags = VM_WIMG_WTHRU;
445 break;
446
447 case kIOWriteCombineCache:
448 flags = VM_WIMG_WCOMB;
449 break;
450
451 case kIOMapCopybackCache:
452 flags = VM_WIMG_COPYBACK;
453 break;
454 }
455 #if __ppc__
456
457 // Set up a block mapped area
458 pmap_map_block(pmap, (addr64_t)va, (ppnum_t)(pa >> 12), length, prot, flags, 0);
459
460 #else
461 // enter each page's physical address in the target map
462
463 for (off = 0; off < length; off += page_size)
464 pmap_enter(pmap, va + off, (pa + off) >> 12, prot, flags, TRUE);
465
466 #endif
467
468 return( KERN_SUCCESS );
469 }
470
471 kern_return_t IOUnmapPages(vm_map_t map, vm_offset_t va, vm_size_t length)
472 {
473 pmap_t pmap = map->pmap;
474
475 pmap_remove(pmap, trunc_page_64(va), round_page_64(va + length));
476
477 return( KERN_SUCCESS );
478 }
479
480 void IOGetTime( mach_timespec_t * clock_time);
481 void IOGetTime( mach_timespec_t * clock_time)
482 {
483 clock_get_system_nanotime(&clock_time->tv_sec, &clock_time->tv_nsec);
484 }