]> git.saurik.com Git - apple/xnu.git/blame - osfmk/device/iokit_rpc.c
xnu-4903.231.4.tar.gz
[apple/xnu.git] / osfmk / device / iokit_rpc.c
CommitLineData
1c79356b 1/*
2d21ac55 2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
1c79356b 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
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.
8f6c56a5 14 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b 27 */
1c79356b 28#include <zone_debug.h>
1c79356b
A
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>
2d21ac55 35//#include <mach/mach_host_server.h>
1c79356b
A
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>
1c79356b
A
45#include <kern/counters.h>
46#include <kern/queue.h>
47#include <kern/zalloc.h>
48#include <kern/thread.h>
1c79356b
A
49#include <kern/task.h>
50#include <kern/sched_prim.h>
51#include <kern/misc_protos.h>
52
53#include <vm/pmap.h>
54#include <vm/vm_map.h>
55#include <vm/vm_kern.h>
56
57#include <device/device_types.h>
58#include <device/device_port.h>
59#include <device/device_server.h>
60
1c79356b
A
61#include <machine/machparam.h>
62
b0d623f7 63#if defined(__i386__) || defined(__x86_64__)
91447636
A
64#include <i386/pmap.h>
65#endif
5ba3f43e
A
66#if defined(__arm__) || defined(__arm64__)
67#include <arm/pmap.h>
68#endif
a39ff7e2 69#include <IOKit/IOKitServer.h>
1c79356b
A
70
71#define EXTERN
72#define MIGEXTERN
73
1c79356b
A
74/*
75 * Lookup a device by its port.
76 * Doesn't consume the naked send right; produces a device reference.
77 */
a39ff7e2
A
78static io_object_t
79iokit_lookup_io_object(ipc_port_t port, ipc_kobject_type_t type)
1c79356b 80{
39037602 81 io_object_t obj;
1c79356b
A
82
83 if (!IP_VALID(port))
84 return (NULL);
85
316670eb 86 iokit_lock_port(port);
a39ff7e2 87 if (ip_active(port) && (ip_kotype(port) == type)) {
1c79356b 88 obj = (io_object_t) port->ip_kobject;
a39ff7e2 89 iokit_add_reference( obj, type );
1c79356b
A
90 }
91 else
92 obj = NULL;
93
316670eb 94 iokit_unlock_port(port);
1c79356b
A
95
96 return( obj );
97}
98
99MIGEXTERN io_object_t
a39ff7e2 100iokit_lookup_object_port(
1c79356b
A
101 ipc_port_t port)
102{
a39ff7e2
A
103 return (iokit_lookup_io_object(port, IKOT_IOKIT_OBJECT));
104}
1c79356b 105
a39ff7e2
A
106MIGEXTERN io_object_t
107iokit_lookup_connect_port(
108 ipc_port_t port)
109{
110 return (iokit_lookup_io_object(port, IKOT_IOKIT_CONNECT));
1c79356b
A
111}
112
a39ff7e2
A
113static io_object_t
114iokit_lookup_object_in_space_with_port_name(mach_port_name_t name, ipc_kobject_type_t type, ipc_space_t space)
1c79356b
A
115{
116 io_object_t obj = NULL;
117
a39ff7e2 118 if (name && MACH_PORT_VALID(name)) {
1c79356b
A
119 ipc_port_t port;
120 kern_return_t kr;
121
a39ff7e2 122 kr = ipc_object_translate(space, name, MACH_PORT_RIGHT_SEND, (ipc_object_t *)&port);
1c79356b
A
123
124 if (kr == KERN_SUCCESS) {
316670eb
A
125 assert(IP_VALID(port));
126
127 ip_reference(port);
128 ip_unlock(port);
129
130 iokit_lock_port(port);
a39ff7e2 131 if (ip_active(port) && (ip_kotype(port) == type)) {
316670eb 132 obj = (io_object_t) port->ip_kobject;
a39ff7e2 133 iokit_add_reference(obj, type);
316670eb
A
134 }
135 iokit_unlock_port(port);
136
137 ip_release(port);
1c79356b
A
138 }
139 }
140
141 return obj;
142}
143
144EXTERN io_object_t
a39ff7e2 145iokit_lookup_object_with_port_name(mach_port_name_t name, ipc_kobject_type_t type, task_t task)
1c79356b 146{
a39ff7e2
A
147 return (iokit_lookup_object_in_space_with_port_name(name, type, task->itk_space));
148}
149
150EXTERN io_object_t
151iokit_lookup_connect_ref_current_task(mach_port_name_t name)
152{
153 return (iokit_lookup_object_in_space_with_port_name(name, IKOT_IOKIT_CONNECT, current_space()));
1c79356b
A
154}
155
9bccf70c
A
156EXTERN void
157iokit_retain_port( ipc_port_t port )
158{
159 ipc_port_reference( port );
160}
161
162EXTERN void
163iokit_release_port( ipc_port_t port )
164{
165 ipc_port_release( port );
166}
167
b0d623f7
A
168EXTERN void
169iokit_release_port_send( ipc_port_t port )
170{
171 ipc_port_release_send( port );
172}
173
316670eb
A
174extern lck_mtx_t iokit_obj_to_port_binding_lock;
175
176EXTERN void
177iokit_lock_port( __unused ipc_port_t port )
178{
179 lck_mtx_lock(&iokit_obj_to_port_binding_lock);
180}
181
182EXTERN void
183iokit_unlock_port( __unused ipc_port_t port )
184{
185 lck_mtx_unlock(&iokit_obj_to_port_binding_lock);
186}
187
1c79356b
A
188/*
189 * Get the port for a device.
190 * Consumes a device reference; produces a naked send right.
191 */
a39ff7e2
A
192
193static ipc_port_t
194iokit_make_port_of_type(io_object_t obj, ipc_kobject_type_t type)
1c79356b 195{
39037602
A
196 ipc_port_t port;
197 ipc_port_t sendPort;
1c79356b
A
198
199 if( obj == NULL)
200 return IP_NULL;
201
a39ff7e2 202 port = iokit_port_for_object( obj, type );
9bccf70c
A
203 if( port) {
204 sendPort = ipc_port_make_send( port);
205 iokit_release_port( port );
206 } else
207 sendPort = IP_NULL;
1c79356b
A
208
209 iokit_remove_reference( obj );
210
9bccf70c 211 return( sendPort);
1c79356b
A
212}
213
214MIGEXTERN ipc_port_t
a39ff7e2 215iokit_make_object_port(
1c79356b
A
216 io_object_t obj )
217{
a39ff7e2
A
218 return (iokit_make_port_of_type(obj, IKOT_IOKIT_OBJECT));
219}
1c79356b 220
a39ff7e2
A
221MIGEXTERN ipc_port_t
222iokit_make_connect_port(
223 io_object_t obj )
224{
225 return (iokit_make_port_of_type(obj, IKOT_IOKIT_CONNECT));
1c79356b
A
226}
227
1c79356b
A
228int gIOKitPortCount;
229
230EXTERN ipc_port_t
231iokit_alloc_object_port( io_object_t obj, ipc_kobject_type_t type )
232{
233 ipc_port_t notify;
234 ipc_port_t port;
235
236 do {
237
238 /* Allocate port, keeping a reference for it. */
239 port = ipc_port_alloc_kernel();
240 if( port == IP_NULL)
241 continue;
242
243 /* set kobject & type */
1c79356b
A
244 ipc_kobject_set( port, (ipc_kobject_t) obj, type);
245
246 /* Request no-senders notifications on the port. */
1c79356b 247 ip_lock( port);
316670eb 248 notify = ipc_port_make_sonce_locked( port);
1c79356b 249 ipc_port_nsrequest( port, 1, notify, &notify);
316670eb 250 /* port unlocked */
1c79356b
A
251 assert( notify == IP_NULL);
252 gIOKitPortCount++;
253
254 } while( FALSE);
255
256 return( port );
257}
258
259
260EXTERN kern_return_t
261iokit_destroy_object_port( ipc_port_t port )
262{
39236c6e
A
263
264 iokit_lock_port(port);
1c79356b
A
265 ipc_kobject_set( port, IKO_NULL, IKOT_NONE);
266
267// iokit_remove_reference( obj );
39236c6e 268 iokit_unlock_port(port);
1c79356b
A
269 ipc_port_dealloc_kernel( port);
270 gIOKitPortCount--;
271
272 return( KERN_SUCCESS);
273}
274
43866e37
A
275EXTERN kern_return_t
276iokit_switch_object_port( ipc_port_t port, io_object_t obj, ipc_kobject_type_t type )
277{
316670eb 278 iokit_lock_port(port);
43866e37 279 ipc_kobject_set( port, (ipc_kobject_t) obj, type);
316670eb 280 iokit_unlock_port(port);
43866e37
A
281
282 return( KERN_SUCCESS);
283}
284
1c79356b
A
285EXTERN mach_port_name_t
286iokit_make_send_right( task_t task, io_object_t obj, ipc_kobject_type_t type )
287{
1c79356b 288 ipc_port_t port;
9bccf70c 289 ipc_port_t sendPort;
39236c6e 290 mach_port_name_t name = 0;
1c79356b
A
291
292 if( obj == NULL)
293 return MACH_PORT_NULL;
294
295 port = iokit_port_for_object( obj, type );
9bccf70c
A
296 if( port) {
297 sendPort = ipc_port_make_send( port);
298 iokit_release_port( port );
299 } else
300 sendPort = IP_NULL;
301
302 if (IP_VALID( sendPort )) {
303 kern_return_t kr;
304 kr = ipc_object_copyout( task->itk_space, (ipc_object_t) sendPort,
1c79356b 305 MACH_MSG_TYPE_PORT_SEND, TRUE, &name);
39236c6e
A
306 if ( kr != KERN_SUCCESS) {
307 ipc_port_release_send( sendPort );
308 name = MACH_PORT_NULL;
309 }
9bccf70c
A
310 } else if ( sendPort == IP_NULL)
311 name = MACH_PORT_NULL;
312 else if ( sendPort == IP_DEAD)
313 name = MACH_PORT_DEAD;
1c79356b 314
1c79356b
A
315 return( name );
316}
317
91447636
A
318EXTERN kern_return_t
319iokit_mod_send_right( task_t task, mach_port_name_t name, mach_port_delta_t delta )
320{
321 return (mach_port_mod_refs( task->itk_space, name, MACH_PORT_RIGHT_SEND, delta ));
322}
323
1c79356b
A
324/*
325 * Handle the No-More_Senders notification generated from a device port destroy.
326 * Since there are no longer any tasks which hold a send right to this device
327 * port a NMS notification has been generated.
328 */
329
330static void
331iokit_no_senders( mach_no_senders_notification_t * notification )
332{
333 ipc_port_t port;
334 io_object_t obj = NULL;
2d21ac55 335 ipc_kobject_type_t type = IKOT_NONE;
9bccf70c 336 ipc_port_t notify;
1c79356b
A
337
338 port = (ipc_port_t) notification->not_header.msgh_remote_port;
339
340 // convert a port to io_object_t.
341 if( IP_VALID(port)) {
316670eb 342 iokit_lock_port(port);
1c79356b
A
343 if( ip_active(port)) {
344 obj = (io_object_t) port->ip_kobject;
345 type = ip_kotype( port );
346 if( (IKOT_IOKIT_OBJECT == type)
a39ff7e2
A
347 || (IKOT_IOKIT_CONNECT == type)
348 || (IKOT_IOKIT_IDENT == type))
349 iokit_add_reference( obj, IKOT_IOKIT_OBJECT );
e3027f41 350 else
1c79356b
A
351 obj = NULL;
352 }
316670eb 353 iokit_unlock_port(port);
1c79356b
A
354
355 if( obj ) {
9bccf70c
A
356
357 mach_port_mscount_t mscount = notification->not_count;
358
359 if( KERN_SUCCESS != iokit_client_died( obj, port, type, &mscount ))
360 {
316670eb
A
361 /* Re-request no-senders notifications on the port (if still active) */
362 ip_lock(port);
363 if (ip_active(port)) {
364 notify = ipc_port_make_sonce_locked(port);
365 ipc_port_nsrequest( port, mscount + 1, notify, &notify);
366 /* port unlocked */
367 if ( notify != IP_NULL)
368 ipc_port_release_sonce(notify);
4b17d6b6
A
369 } else {
370 ip_unlock(port);
316670eb 371 }
9bccf70c 372 }
1c79356b
A
373 iokit_remove_reference( obj );
374 }
375 }
376}
377
378
379EXTERN
380boolean_t
381iokit_notify( mach_msg_header_t * msg )
382{
383 switch (msg->msgh_id) {
384 case MACH_NOTIFY_NO_SENDERS:
385 iokit_no_senders((mach_no_senders_notification_t *) msg);
386 return TRUE;
387
388 case MACH_NOTIFY_PORT_DELETED:
389 case MACH_NOTIFY_PORT_DESTROYED:
390 case MACH_NOTIFY_SEND_ONCE:
391 case MACH_NOTIFY_DEAD_NAME:
392 default:
2d21ac55 393 printf("iokit_notify: strange notification %d\n", msg->msgh_id);
1c79356b
A
394 return FALSE;
395 }
396}
397
55e303ae
A
398/* need to create a pmap function to generalize */
399unsigned int IODefaultCacheBits(addr64_t pa)
de355530 400{
b0d623f7 401 return(pmap_cache_attributes((ppnum_t)(pa >> PAGE_SHIFT)));
9bccf70c 402}
9bccf70c 403
0c530ab8
A
404kern_return_t IOMapPages(vm_map_t map, mach_vm_address_t va, mach_vm_address_t pa,
405 mach_vm_size_t length, unsigned int options)
1c79356b 406{
6d2010ae 407 vm_prot_t prot;
55e303ae 408 unsigned int flags;
6d2010ae 409 ppnum_t pagenum;
55e303ae 410 pmap_t pmap = map->pmap;
1c79356b
A
411
412 prot = (options & kIOMapReadOnly)
413 ? VM_PROT_READ : (VM_PROT_READ|VM_PROT_WRITE);
414
6d2010ae
A
415 pagenum = (ppnum_t)atop_64(pa);
416
55e303ae
A
417 switch(options & kIOMapCacheMask ) { /* What cache mode do we need? */
418
419 case kIOMapDefaultCache:
420 default:
421 flags = IODefaultCacheBits(pa);
422 break;
423
424 case kIOMapInhibitCache:
425 flags = VM_WIMG_IO;
426 break;
427
428 case kIOMapWriteThruCache:
429 flags = VM_WIMG_WTHRU;
430 break;
431
0c530ab8 432 case kIOMapWriteCombineCache:
55e303ae
A
433 flags = VM_WIMG_WCOMB;
434 break;
435
436 case kIOMapCopybackCache:
437 flags = VM_WIMG_COPYBACK;
438 break;
5ba3f43e 439
316670eb
A
440 case kIOMapCopybackInnerCache:
441 flags = VM_WIMG_INNERWBACK;
442 break;
5ba3f43e
A
443
444 case kIOMapPostedWrite:
445 flags = VM_WIMG_POSTED;
446 break;
55e303ae 447 }
1c79356b 448
6d2010ae
A
449 pmap_set_cache_attributes(pagenum, flags);
450
451 vm_map_set_cache_attr(map, (vm_map_offset_t)va);
452
453
55e303ae 454 // Set up a block mapped area
5ba3f43e 455 return pmap_map_block(pmap, va, pagenum, (uint32_t) atop_64(round_page_64(length)), prot, 0, 0);
0c530ab8
A
456}
457
458kern_return_t IOUnmapPages(vm_map_t map, mach_vm_address_t va, mach_vm_size_t length)
459{
460 pmap_t pmap = map->pmap;
461
462 pmap_remove(pmap, trunc_page_64(va), round_page_64(va + length));
463
464 return( KERN_SUCCESS );
465}
466
2d21ac55
A
467kern_return_t IOProtectCacheMode(vm_map_t __unused map, mach_vm_address_t __unused va,
468 mach_vm_size_t __unused length, unsigned int __unused options)
0c530ab8
A
469{
470 mach_vm_size_t off;
471 vm_prot_t prot;
472 unsigned int flags;
473 pmap_t pmap = map->pmap;
39236c6e
A
474 pmap_flush_context pmap_flush_context_storage;
475 boolean_t delayed_pmap_flush = FALSE;
0c530ab8
A
476
477 prot = (options & kIOMapReadOnly)
478 ? VM_PROT_READ : (VM_PROT_READ|VM_PROT_WRITE);
479
480 switch (options & kIOMapCacheMask)
481 {
2d21ac55 482 // what cache mode do we need?
0c530ab8
A
483 case kIOMapDefaultCache:
484 default:
485 return (KERN_INVALID_ARGUMENT);
486
487 case kIOMapInhibitCache:
488 flags = VM_WIMG_IO;
489 break;
490
491 case kIOMapWriteThruCache:
492 flags = VM_WIMG_WTHRU;
493 break;
4452a7af 494
0c530ab8
A
495 case kIOMapWriteCombineCache:
496 flags = VM_WIMG_WCOMB;
497 break;
498
499 case kIOMapCopybackCache:
500 flags = VM_WIMG_COPYBACK;
501 break;
5ba3f43e
A
502
503 case kIOMapCopybackInnerCache:
504 flags = VM_WIMG_INNERWBACK;
505 break;
506
507 case kIOMapPostedWrite:
508 flags = VM_WIMG_POSTED;
509 break;
0c530ab8 510 }
55e303ae 511
39236c6e
A
512 pmap_flush_context_init(&pmap_flush_context_storage);
513 delayed_pmap_flush = FALSE;
514
0c530ab8 515 // enter each page's physical address in the target map
55e303ae 516 for (off = 0; off < length; off += page_size)
0c530ab8
A
517 {
518 ppnum_t ppnum = pmap_find_phys(pmap, va + off);
39236c6e
A
519 if (ppnum) {
520 pmap_enter_options(pmap, va + off, ppnum, prot, VM_PROT_NONE, flags, TRUE,
521 PMAP_OPTIONS_NOFLUSH, (void *)&pmap_flush_context_storage);
522 delayed_pmap_flush = TRUE;
523 }
0c530ab8 524 }
39236c6e
A
525 if (delayed_pmap_flush == TRUE)
526 pmap_flush(&pmap_flush_context_storage);
55e303ae 527
0c530ab8 528 return (KERN_SUCCESS);
1c79356b
A
529}
530
0c530ab8 531ppnum_t IOGetLastPageNumber(void)
e3027f41 532{
6d2010ae
A
533#if __i386__ || __x86_64__
534 ppnum_t lastPage, highest = 0;
535 unsigned int idx;
536
537 for (idx = 0; idx < pmap_memory_region_count; idx++)
538 {
539 lastPage = pmap_memory_regions[idx].end - 1;
540 if (lastPage > highest)
541 highest = lastPage;
542 }
543 return (highest);
5ba3f43e
A
544#elif __arm__ || __arm64__
545 return 0;
0c530ab8 546#else
6d2010ae 547#error unknown arch
0c530ab8 548#endif
6601e61a 549}
4452a7af 550
0c530ab8 551
1c79356b
A
552void IOGetTime( mach_timespec_t * clock_time);
553void IOGetTime( mach_timespec_t * clock_time)
554{
b0d623f7
A
555 clock_sec_t sec;
556 clock_nsec_t nsec;
557 clock_get_system_nanotime(&sec, &nsec);
558 clock_time->tv_sec = (typeof(clock_time->tv_sec))sec;
559 clock_time->tv_nsec = nsec;
1c79356b 560}
0c530ab8 561