]> git.saurik.com Git - apple/xnu.git/blob - osfmk/device/iokit_rpc.c
a04a511a1fa6864005e02e0c220700eeab26a849
[apple/xnu.git] / osfmk / device / iokit_rpc.c
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 #include <mach_kdb.h>
29 #include <zone_debug.h>
30 #include <mach_kdb.h>
31
32 #include <mach/boolean.h>
33 #include <mach/kern_return.h>
34 #include <mach/mig_errors.h>
35 #include <mach/port.h>
36 #include <mach/vm_param.h>
37 #include <mach/notify.h>
38 #include <mach/mach_host_server.h>
39 #include <mach/mach_types.h>
40
41 #include <machine/machparam.h> /* spl definitions */
42
43 #include <ipc/ipc_port.h>
44 #include <ipc/ipc_space.h>
45
46 #include <kern/clock.h>
47 #include <kern/spl.h>
48 #include <kern/counters.h>
49 #include <kern/queue.h>
50 #include <kern/zalloc.h>
51 #include <kern/thread.h>
52 #include <kern/task.h>
53 #include <kern/sched_prim.h>
54 #include <kern/misc_protos.h>
55
56 #include <vm/pmap.h>
57 #include <vm/vm_map.h>
58 #include <vm/vm_kern.h>
59
60 #include <device/device_types.h>
61 #include <device/device_port.h>
62 #include <device/device_server.h>
63
64 #include <machine/machparam.h>
65
66 #ifdef __ppc__
67 #include <ppc/mappings.h>
68 #endif
69 #ifdef __i386
70 #include <i386/pmap.h>
71 #endif
72 #include <IOKit/IOTypes.h>
73
74 #define EXTERN
75 #define MIGEXTERN
76
77 /*
78 * Functions in iokit:IOUserClient.cpp
79 */
80
81 extern void iokit_add_reference( io_object_t obj );
82
83 extern ipc_port_t iokit_port_for_object( io_object_t obj,
84 ipc_kobject_type_t type );
85
86 extern kern_return_t iokit_client_died( io_object_t obj,
87 ipc_port_t port, ipc_kobject_type_t type, mach_port_mscount_t * mscount );
88
89 extern kern_return_t
90 iokit_client_memory_for_type(
91 io_object_t connect,
92 unsigned int type,
93 unsigned int * flags,
94 vm_address_t * address,
95 vm_size_t * size );
96
97
98 extern ppnum_t IOGetLastPageNumber(void);
99
100 /*
101 * Lookup a device by its port.
102 * Doesn't consume the naked send right; produces a device reference.
103 */
104 MIGEXTERN io_object_t
105 iokit_lookup_object_port(
106 ipc_port_t port)
107 {
108 register io_object_t obj;
109
110 if (!IP_VALID(port))
111 return (NULL);
112
113 ip_lock(port);
114 if (ip_active(port) && (ip_kotype(port) == IKOT_IOKIT_OBJECT)) {
115 obj = (io_object_t) port->ip_kobject;
116 iokit_add_reference( obj );
117 }
118 else
119 obj = NULL;
120
121 ip_unlock(port);
122
123 return( obj );
124 }
125
126 MIGEXTERN io_object_t
127 iokit_lookup_connect_port(
128 ipc_port_t port)
129 {
130 register io_object_t obj;
131
132 if (!IP_VALID(port))
133 return (NULL);
134
135 ip_lock(port);
136 if (ip_active(port) && (ip_kotype(port) == IKOT_IOKIT_CONNECT)) {
137 obj = (io_object_t) port->ip_kobject;
138 iokit_add_reference( obj );
139 }
140 else
141 obj = NULL;
142
143 ip_unlock(port);
144
145 return( obj );
146 }
147
148 EXTERN io_object_t
149 iokit_lookup_connect_ref(io_object_t connectRef, ipc_space_t space)
150 {
151 io_object_t obj = NULL;
152
153 if (connectRef && MACH_PORT_VALID((mach_port_name_t)connectRef)) {
154 ipc_port_t port;
155 kern_return_t kr;
156
157 kr = ipc_object_translate(space, (mach_port_name_t)connectRef, MACH_PORT_RIGHT_SEND, (ipc_object_t *)&port);
158
159 if (kr == KERN_SUCCESS) {
160 assert(IP_VALID(port));
161
162 if (ip_active(port) && (ip_kotype(port) == IKOT_IOKIT_CONNECT)) {
163 obj = (io_object_t) port->ip_kobject;
164 iokit_add_reference(obj);
165 }
166
167 ip_unlock(port);
168 }
169 }
170
171 return obj;
172 }
173
174 EXTERN io_object_t
175 iokit_lookup_connect_ref_current_task(io_object_t connectRef)
176 {
177 return iokit_lookup_connect_ref(connectRef, current_space());
178 }
179
180 EXTERN void
181 iokit_retain_port( ipc_port_t port )
182 {
183 ipc_port_reference( port );
184 }
185
186 EXTERN void
187 iokit_release_port( ipc_port_t port )
188 {
189 ipc_port_release( port );
190 }
191
192 /*
193 * Get the port for a device.
194 * Consumes a device reference; produces a naked send right.
195 */
196 MIGEXTERN ipc_port_t
197 iokit_make_object_port(
198 io_object_t obj )
199 {
200 register ipc_port_t port;
201 register ipc_port_t sendPort;
202
203 if( obj == NULL)
204 return IP_NULL;
205
206 port = iokit_port_for_object( obj, IKOT_IOKIT_OBJECT );
207 if( port) {
208 sendPort = ipc_port_make_send( port);
209 iokit_release_port( port );
210 } else
211 sendPort = IP_NULL;
212
213 iokit_remove_reference( obj );
214
215 return( sendPort);
216 }
217
218 MIGEXTERN ipc_port_t
219 iokit_make_connect_port(
220 io_object_t obj )
221 {
222 register ipc_port_t port;
223 register ipc_port_t sendPort;
224
225 if( obj == NULL)
226 return IP_NULL;
227
228 port = iokit_port_for_object( obj, IKOT_IOKIT_CONNECT );
229 if( port) {
230 sendPort = ipc_port_make_send( port);
231 iokit_release_port( port );
232 } else
233 sendPort = IP_NULL;
234
235 iokit_remove_reference( obj );
236
237 return( sendPort);
238 }
239
240
241 EXTERN ipc_port_t
242 iokit_alloc_object_port( io_object_t obj, ipc_kobject_type_t type );
243
244 int gIOKitPortCount;
245
246 EXTERN ipc_port_t
247 iokit_alloc_object_port( io_object_t obj, ipc_kobject_type_t type )
248 {
249 ipc_port_t notify;
250 ipc_port_t port;
251
252 do {
253
254 /* Allocate port, keeping a reference for it. */
255 port = ipc_port_alloc_kernel();
256 if( port == IP_NULL)
257 continue;
258
259 /* set kobject & type */
260 // iokit_add_reference( obj );
261 ipc_kobject_set( port, (ipc_kobject_t) obj, type);
262
263 /* Request no-senders notifications on the port. */
264 notify = ipc_port_make_sonce( port);
265 ip_lock( port);
266 ipc_port_nsrequest( port, 1, notify, &notify);
267 assert( notify == IP_NULL);
268 gIOKitPortCount++;
269
270 } while( FALSE);
271
272 return( port );
273 }
274
275
276 EXTERN kern_return_t
277 iokit_destroy_object_port( ipc_port_t port )
278 {
279 ipc_kobject_set( port, IKO_NULL, IKOT_NONE);
280
281 // iokit_remove_reference( obj );
282
283 ipc_port_dealloc_kernel( port);
284 gIOKitPortCount--;
285
286 return( KERN_SUCCESS);
287 }
288
289 EXTERN kern_return_t
290 iokit_switch_object_port( ipc_port_t port, io_object_t obj, ipc_kobject_type_t type )
291 {
292 ipc_kobject_set( port, (ipc_kobject_t) obj, type);
293
294 return( KERN_SUCCESS);
295 }
296
297 EXTERN mach_port_name_t
298 iokit_make_send_right( task_t task, io_object_t obj, ipc_kobject_type_t type )
299 {
300 ipc_port_t port;
301 ipc_port_t sendPort;
302 mach_port_name_t name;
303
304 if( obj == NULL)
305 return MACH_PORT_NULL;
306
307 port = iokit_port_for_object( obj, type );
308 if( port) {
309 sendPort = ipc_port_make_send( port);
310 iokit_release_port( port );
311 } else
312 sendPort = IP_NULL;
313
314 if (IP_VALID( sendPort )) {
315 kern_return_t kr;
316 kr = ipc_object_copyout( task->itk_space, (ipc_object_t) sendPort,
317 MACH_MSG_TYPE_PORT_SEND, TRUE, &name);
318 if ( kr != KERN_SUCCESS)
319 name = MACH_PORT_NULL;
320 } else if ( sendPort == IP_NULL)
321 name = MACH_PORT_NULL;
322 else if ( sendPort == IP_DEAD)
323 name = MACH_PORT_DEAD;
324
325 iokit_remove_reference( obj );
326
327 return( name );
328 }
329
330 EXTERN kern_return_t
331 iokit_mod_send_right( task_t task, mach_port_name_t name, mach_port_delta_t delta )
332 {
333 return (mach_port_mod_refs( task->itk_space, name, MACH_PORT_RIGHT_SEND, delta ));
334 }
335
336 /*
337 * Handle the No-More_Senders notification generated from a device port destroy.
338 * Since there are no longer any tasks which hold a send right to this device
339 * port a NMS notification has been generated.
340 */
341
342 static void
343 iokit_no_senders( mach_no_senders_notification_t * notification )
344 {
345 ipc_port_t port;
346 io_object_t obj = NULL;
347 ipc_kobject_type_t type;
348 ipc_port_t notify;
349
350 port = (ipc_port_t) notification->not_header.msgh_remote_port;
351
352 // convert a port to io_object_t.
353 if( IP_VALID(port)) {
354 ip_lock(port);
355 if( ip_active(port)) {
356 obj = (io_object_t) port->ip_kobject;
357 type = ip_kotype( port );
358 if( (IKOT_IOKIT_OBJECT == type)
359 || (IKOT_IOKIT_CONNECT == type))
360 iokit_add_reference( obj );
361 else
362 obj = NULL;
363 }
364 ip_unlock(port);
365
366 if( obj ) {
367
368 mach_port_mscount_t mscount = notification->not_count;
369
370 if( KERN_SUCCESS != iokit_client_died( obj, port, type, &mscount ))
371 {
372 /* Re-request no-senders notifications on the port. */
373 notify = ipc_port_make_sonce( port);
374 ip_lock( port);
375 ipc_port_nsrequest( port, mscount + 1, notify, &notify);
376 assert( notify == IP_NULL);
377 }
378 iokit_remove_reference( obj );
379 }
380 }
381 }
382
383
384 EXTERN
385 boolean_t
386 iokit_notify( mach_msg_header_t * msg )
387 {
388 switch (msg->msgh_id) {
389 case MACH_NOTIFY_NO_SENDERS:
390 iokit_no_senders((mach_no_senders_notification_t *) msg);
391 return TRUE;
392
393 case MACH_NOTIFY_PORT_DELETED:
394 case MACH_NOTIFY_PORT_DESTROYED:
395 case MACH_NOTIFY_SEND_ONCE:
396 case MACH_NOTIFY_DEAD_NAME:
397 default:
398 printf("iokit_notify: strange notification %ld\n", msg->msgh_id);
399 return FALSE;
400 }
401 }
402
403 /* need to create a pmap function to generalize */
404 unsigned int IODefaultCacheBits(addr64_t pa)
405 {
406
407 return(pmap_cache_attributes(pa >> PAGE_SHIFT));
408 }
409
410 kern_return_t IOMapPages(vm_map_t map, mach_vm_address_t va, mach_vm_address_t pa,
411 mach_vm_size_t length, unsigned int options)
412 {
413 vm_prot_t prot;
414 unsigned int flags;
415 pmap_t pmap = map->pmap;
416
417 prot = (options & kIOMapReadOnly)
418 ? VM_PROT_READ : (VM_PROT_READ|VM_PROT_WRITE);
419
420 switch(options & kIOMapCacheMask ) { /* What cache mode do we need? */
421
422 case kIOMapDefaultCache:
423 default:
424 flags = IODefaultCacheBits(pa);
425 break;
426
427 case kIOMapInhibitCache:
428 flags = VM_WIMG_IO;
429 break;
430
431 case kIOMapWriteThruCache:
432 flags = VM_WIMG_WTHRU;
433 break;
434
435 case kIOMapWriteCombineCache:
436 flags = VM_WIMG_WCOMB;
437 break;
438
439 case kIOMapCopybackCache:
440 flags = VM_WIMG_COPYBACK;
441 break;
442 }
443
444 // Set up a block mapped area
445 pmap_map_block(pmap, va, (ppnum_t)atop_64(pa), (uint32_t) atop_64(round_page_64(length)), prot, flags, 0);
446
447 return( KERN_SUCCESS );
448 }
449
450 kern_return_t IOUnmapPages(vm_map_t map, mach_vm_address_t va, mach_vm_size_t length)
451 {
452 pmap_t pmap = map->pmap;
453
454 pmap_remove(pmap, trunc_page_64(va), round_page_64(va + length));
455
456 return( KERN_SUCCESS );
457 }
458
459 kern_return_t IOProtectCacheMode(vm_map_t map, mach_vm_address_t va,
460 mach_vm_size_t length, unsigned int options)
461 {
462 mach_vm_size_t off;
463 vm_prot_t prot;
464 unsigned int flags;
465 pmap_t pmap = map->pmap;
466
467 prot = (options & kIOMapReadOnly)
468 ? VM_PROT_READ : (VM_PROT_READ|VM_PROT_WRITE);
469
470 switch (options & kIOMapCacheMask)
471 {
472 /* What cache mode do we need? */
473 case kIOMapDefaultCache:
474 default:
475 return (KERN_INVALID_ARGUMENT);
476
477 case kIOMapInhibitCache:
478 flags = VM_WIMG_IO;
479 break;
480
481 case kIOMapWriteThruCache:
482 flags = VM_WIMG_WTHRU;
483 break;
484
485 case kIOMapWriteCombineCache:
486 flags = VM_WIMG_WCOMB;
487 break;
488
489 case kIOMapCopybackCache:
490 flags = VM_WIMG_COPYBACK;
491 break;
492 }
493 #if __ppc__
494 // can't remap block mappings, but ppc doesn't speculative read from WC
495 #else
496
497 // enter each page's physical address in the target map
498 for (off = 0; off < length; off += page_size)
499 {
500 ppnum_t ppnum = pmap_find_phys(pmap, va + off);
501 if (ppnum)
502 pmap_enter(pmap, va + off, ppnum, prot, flags, TRUE);
503 }
504
505 #endif
506
507 return (KERN_SUCCESS);
508 }
509
510 ppnum_t IOGetLastPageNumber(void)
511 {
512 ppnum_t lastPage, highest = 0;
513
514 #if __ppc__
515 int idx;
516 for (idx = 0; idx < pmap_mem_regions_count; idx++)
517 {
518 lastPage = pmap_mem_regions[idx].mrEnd;
519 #elif __i386__
520 unsigned int idx;
521 for (idx = 0; idx < pmap_memory_region_count; idx++)
522 {
523 lastPage = pmap_memory_regions[idx].end - 1;
524 #else
525 #error arch
526 #endif
527 if (lastPage > highest)
528 highest = lastPage;
529 }
530 return (highest);
531 }
532
533
534 void IOGetTime( mach_timespec_t * clock_time);
535 void IOGetTime( mach_timespec_t * clock_time)
536 {
537 clock_get_system_nanotime(&clock_time->tv_sec, &clock_time->tv_nsec);
538 }
539