2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 #include <mach/boolean.h>
29 #include <mach/kern_return.h>
30 #include <mach/mig_errors.h>
31 #include <mach/port.h>
32 #include <mach/vm_param.h>
33 #include <mach/notify.h>
34 //#include <mach/mach_host_server.h>
35 #include <mach/mach_types.h>
37 #include <machine/machparam.h> /* spl definitions */
39 #include <ipc/ipc_port.h>
40 #include <ipc/ipc_space.h>
42 #include <kern/clock.h>
44 #include <kern/counters.h>
45 #include <kern/queue.h>
46 #include <kern/zalloc.h>
47 #include <kern/thread.h>
48 #include <kern/task.h>
49 #include <kern/sched_prim.h>
50 #include <kern/misc_protos.h>
53 #include <vm/vm_map.h>
54 #include <vm/vm_kern.h>
56 #include <device/device_types.h>
57 #include <device/device_port.h>
58 #include <device/device_server.h>
60 #include <machine/machparam.h>
62 #if defined(__i386__) || defined(__x86_64__)
63 #include <i386/pmap.h>
65 #if defined(__arm__) || defined(__arm64__)
68 #include <IOKit/IOKitServer.h>
73 LCK_GRP_DECLARE(dev_lck_grp
, "device");
74 LCK_MTX_DECLARE(iokit_obj_to_port_binding_lock
, &dev_lck_grp
);
77 * Lookup a device by its port.
78 * Doesn't consume the naked send right; produces a device reference.
81 iokit_lookup_io_object(ipc_port_t port
, ipc_kobject_type_t type
)
85 if (!IP_VALID(port
)) {
89 iokit_lock_port(port
);
90 if (ip_active(port
) && (ip_kotype(port
) == type
)) {
91 obj
= (io_object_t
) ip_get_kobject(port
);
92 iokit_add_reference( obj
, type
);
97 iokit_unlock_port(port
);
102 MIGEXTERN io_object_t
103 iokit_lookup_object_port(
106 return iokit_lookup_io_object(port
, IKOT_IOKIT_OBJECT
);
109 MIGEXTERN io_object_t
110 iokit_lookup_connect_port(
113 return iokit_lookup_io_object(port
, IKOT_IOKIT_CONNECT
);
116 MIGEXTERN io_object_t
117 iokit_lookup_uext_object_port(
120 return iokit_lookup_io_object(port
, IKOT_UEXT_OBJECT
);
124 iokit_lookup_object_in_space_with_port_name(mach_port_name_t name
, ipc_kobject_type_t type
, ipc_space_t space
)
126 io_object_t obj
= NULL
;
128 if (name
&& MACH_PORT_VALID(name
)) {
132 kr
= ipc_port_translate_send(space
, name
, &port
);
134 if (kr
== KERN_SUCCESS
) {
135 assert(IP_VALID(port
));
136 require_ip_active(port
);
140 iokit_lock_port(port
);
141 if (ip_kotype(port
) == type
) {
142 obj
= (io_object_t
) ip_get_kobject(port
);
143 iokit_add_reference(obj
, type
);
145 iokit_unlock_port(port
);
155 iokit_lookup_object_with_port_name(mach_port_name_t name
, ipc_kobject_type_t type
, task_t task
)
157 return iokit_lookup_object_in_space_with_port_name(name
, type
, task
->itk_space
);
161 iokit_lookup_connect_ref_current_task(mach_port_name_t name
)
163 return iokit_lookup_object_in_space_with_port_name(name
, IKOT_IOKIT_CONNECT
, current_space());
167 iokit_lookup_uext_ref_current_task(mach_port_name_t name
)
169 return iokit_lookup_object_in_space_with_port_name(name
, IKOT_UEXT_OBJECT
, current_space());
173 iokit_retain_port( ipc_port_t port
)
175 ipc_port_reference( port
);
179 iokit_release_port( ipc_port_t port
)
181 ipc_port_release( port
);
185 iokit_release_port_send( ipc_port_t port
)
187 ipc_port_release_send( port
);
191 iokit_lock_port( __unused ipc_port_t port
)
193 lck_mtx_lock(&iokit_obj_to_port_binding_lock
);
197 iokit_unlock_port( __unused ipc_port_t port
)
199 lck_mtx_unlock(&iokit_obj_to_port_binding_lock
);
203 * Get the port for a device.
204 * Consumes a device reference; produces a naked send right.
208 iokit_make_port_of_type(io_object_t obj
, ipc_kobject_type_t type
)
217 port
= iokit_port_for_object( obj
, type
);
219 sendPort
= ipc_port_make_send( port
);
220 iokit_release_port( port
);
225 iokit_remove_reference( obj
);
231 iokit_make_object_port(
234 return iokit_make_port_of_type(obj
, IKOT_IOKIT_OBJECT
);
238 iokit_make_connect_port(
241 return iokit_make_port_of_type(obj
, IKOT_IOKIT_CONNECT
);
247 iokit_alloc_object_port( io_object_t obj
, ipc_kobject_type_t type
)
249 /* Allocate port, keeping a reference for it. */
251 ipc_kobject_alloc_options_t options
= IPC_KOBJECT_ALLOC_NSREQUEST
;
252 if (type
== IKOT_IOKIT_CONNECT
) {
253 options
|= IPC_KOBJECT_ALLOC_IMMOVABLE_SEND
;
255 if (type
== IKOT_UEXT_OBJECT
) {
256 ipc_label_t label
= IPC_LABEL_DEXT
;
257 return ipc_kobject_alloc_labeled_port((ipc_kobject_t
) obj
, type
, label
, options
);
259 return ipc_kobject_alloc_port((ipc_kobject_t
) obj
, type
, options
);
264 iokit_destroy_object_port( ipc_port_t port
)
266 iokit_lock_port(port
);
267 ipc_kobject_set( port
, IKO_NULL
, IKOT_NONE
);
269 // iokit_remove_reference( obj );
270 iokit_unlock_port(port
);
271 ipc_port_dealloc_kernel( port
);
278 iokit_switch_object_port( ipc_port_t port
, io_object_t obj
, ipc_kobject_type_t type
)
280 iokit_lock_port(port
);
281 ipc_kobject_set( port
, (ipc_kobject_t
) obj
, type
);
282 iokit_unlock_port(port
);
287 EXTERN mach_port_name_t
288 iokit_make_send_right( task_t task
, io_object_t obj
, ipc_kobject_type_t type
)
292 mach_port_name_t name
= 0;
295 return MACH_PORT_NULL
;
298 port
= iokit_port_for_object( obj
, type
);
300 sendPort
= ipc_port_make_send( port
);
301 iokit_release_port( port
);
306 if (IP_VALID( sendPort
)) {
308 // Remove once <rdar://problem/45522961> is fixed.
309 // We need to make ith_knote NULL as ipc_object_copyout() uses
310 // thread-argument-passing and its value should not be garbage
311 current_thread()->ith_knote
= ITH_KNOTE_NULL
;
312 kr
= ipc_object_copyout( task
->itk_space
, ip_to_object(sendPort
),
313 MACH_MSG_TYPE_PORT_SEND
, NULL
, NULL
, &name
);
314 if (kr
!= KERN_SUCCESS
) {
315 ipc_port_release_send( sendPort
);
316 name
= MACH_PORT_NULL
;
318 } else if (sendPort
== IP_NULL
) {
319 name
= MACH_PORT_NULL
;
320 } else if (sendPort
== IP_DEAD
) {
321 name
= MACH_PORT_DEAD
;
328 iokit_mod_send_right( task_t task
, mach_port_name_t name
, mach_port_delta_t delta
)
330 return mach_port_mod_refs( task
->itk_space
, name
, MACH_PORT_RIGHT_SEND
, delta
);
334 * Handle the No-More_Senders notification generated from a device port destroy.
335 * Since there are no longer any tasks which hold a send right to this device
336 * port a NMS notification has been generated.
340 iokit_no_senders( mach_no_senders_notification_t
* notification
)
343 io_object_t obj
= NULL
;
344 ipc_kobject_type_t type
= IKOT_NONE
;
347 port
= notification
->not_header
.msgh_remote_port
;
349 // convert a port to io_object_t.
350 if (IP_VALID(port
)) {
351 iokit_lock_port(port
);
352 if (ip_active(port
)) {
353 obj
= (io_object_t
) ip_get_kobject(port
);
354 type
= ip_kotype( port
);
355 if ((IKOT_IOKIT_OBJECT
== type
)
356 || (IKOT_IOKIT_CONNECT
== type
)
357 || (IKOT_IOKIT_IDENT
== type
)
358 || (IKOT_UEXT_OBJECT
== type
)) {
359 iokit_add_reference( obj
, IKOT_IOKIT_OBJECT
);
364 iokit_unlock_port(port
);
367 mach_port_mscount_t mscount
= notification
->not_count
;
369 if (KERN_SUCCESS
!= iokit_client_died( obj
, port
, type
, &mscount
)) {
370 /* Re-request no-senders notifications on the port (if still active) */
372 if (ip_active(port
)) {
373 notify
= ipc_port_make_sonce_locked(port
);
374 ipc_port_nsrequest( port
, mscount
+ 1, notify
, ¬ify
);
376 if (notify
!= IP_NULL
) {
377 ipc_port_release_sonce(notify
);
383 iokit_remove_reference( obj
);
391 iokit_notify( mach_msg_header_t
* msg
)
393 switch (msg
->msgh_id
) {
394 case MACH_NOTIFY_NO_SENDERS
:
395 iokit_no_senders((mach_no_senders_notification_t
*) msg
);
398 case MACH_NOTIFY_PORT_DELETED
:
399 case MACH_NOTIFY_PORT_DESTROYED
:
400 case MACH_NOTIFY_SEND_ONCE
:
401 case MACH_NOTIFY_DEAD_NAME
:
403 printf("iokit_notify: strange notification %d\n", msg
->msgh_id
);
409 iokit_label_dext_task(task_t task
)
411 return ipc_space_add_label(task
->itk_space
, IPC_LABEL_DEXT
);
414 /* need to create a pmap function to generalize */
416 IODefaultCacheBits(addr64_t pa
)
418 return pmap_cache_attributes((ppnum_t
)(pa
>> PAGE_SHIFT
));
422 IOMapPages(vm_map_t map
, mach_vm_address_t va
, mach_vm_address_t pa
,
423 mach_vm_size_t length
, unsigned int options
)
428 pmap_t pmap
= map
->pmap
;
430 prot
= (options
& kIOMapReadOnly
)
431 ? VM_PROT_READ
: (VM_PROT_READ
| VM_PROT_WRITE
);
433 pagenum
= (ppnum_t
)atop_64(pa
);
435 switch (options
& kIOMapCacheMask
) { /* What cache mode do we need? */
436 case kIOMapDefaultCache
:
438 flags
= IODefaultCacheBits(pa
);
441 case kIOMapInhibitCache
:
445 case kIOMapWriteThruCache
:
446 flags
= VM_WIMG_WTHRU
;
449 case kIOMapWriteCombineCache
:
450 flags
= VM_WIMG_WCOMB
;
453 case kIOMapCopybackCache
:
454 flags
= VM_WIMG_COPYBACK
;
457 case kIOMapCopybackInnerCache
:
458 flags
= VM_WIMG_INNERWBACK
;
461 case kIOMapPostedWrite
:
462 flags
= VM_WIMG_POSTED
;
465 case kIOMapRealTimeCache
:
470 pmap_set_cache_attributes(pagenum
, flags
);
472 vm_map_set_cache_attr(map
, (vm_map_offset_t
)va
);
475 // Set up a block mapped area
476 return pmap_map_block(pmap
, va
, pagenum
, (uint32_t) atop_64(round_page_64(length
)), prot
, 0, 0);
480 IOUnmapPages(vm_map_t map
, mach_vm_address_t va
, mach_vm_size_t length
)
482 pmap_t pmap
= map
->pmap
;
484 pmap_remove(pmap
, trunc_page_64(va
), round_page_64(va
+ length
));
490 IOProtectCacheMode(vm_map_t __unused map
, mach_vm_address_t __unused va
,
491 mach_vm_size_t __unused length
, unsigned int __unused options
)
496 pmap_t pmap
= map
->pmap
;
497 pmap_flush_context pmap_flush_context_storage
;
498 boolean_t delayed_pmap_flush
= FALSE
;
500 prot
= (options
& kIOMapReadOnly
)
501 ? VM_PROT_READ
: (VM_PROT_READ
| VM_PROT_WRITE
);
503 switch (options
& kIOMapCacheMask
) {
504 // what cache mode do we need?
505 case kIOMapDefaultCache
:
507 return KERN_INVALID_ARGUMENT
;
509 case kIOMapInhibitCache
:
513 case kIOMapWriteThruCache
:
514 flags
= VM_WIMG_WTHRU
;
517 case kIOMapWriteCombineCache
:
518 flags
= VM_WIMG_WCOMB
;
521 case kIOMapCopybackCache
:
522 flags
= VM_WIMG_COPYBACK
;
525 case kIOMapCopybackInnerCache
:
526 flags
= VM_WIMG_INNERWBACK
;
529 case kIOMapPostedWrite
:
530 flags
= VM_WIMG_POSTED
;
533 case kIOMapRealTimeCache
:
538 pmap_flush_context_init(&pmap_flush_context_storage
);
539 delayed_pmap_flush
= FALSE
;
541 // enter each page's physical address in the target map
542 for (off
= 0; off
< length
; off
+= page_size
) {
543 ppnum_t ppnum
= pmap_find_phys(pmap
, va
+ off
);
545 pmap_enter_options(pmap
, va
+ off
, ppnum
, prot
, VM_PROT_NONE
, flags
, TRUE
,
546 PMAP_OPTIONS_NOFLUSH
, (void *)&pmap_flush_context_storage
);
547 delayed_pmap_flush
= TRUE
;
550 if (delayed_pmap_flush
== TRUE
) {
551 pmap_flush(&pmap_flush_context_storage
);
558 IOGetLastPageNumber(void)
560 #if __i386__ || __x86_64__
561 ppnum_t lastPage
, highest
= 0;
564 for (idx
= 0; idx
< pmap_memory_region_count
; idx
++) {
565 lastPage
= pmap_memory_regions
[idx
].end
- 1;
566 if (lastPage
> highest
) {
571 #elif __arm__ || __arm64__
579 void IOGetTime( mach_timespec_t
* clock_time
);
581 IOGetTime( mach_timespec_t
* clock_time
)
585 clock_get_system_nanotime(&sec
, &nsec
);
586 clock_time
->tv_sec
= (typeof(clock_time
->tv_sec
))sec
;
587 clock_time
->tv_nsec
= nsec
;