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@
29 #include <zone_debug.h>
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>
41 #include <machine/machparam.h> /* spl definitions */
43 #include <ipc/ipc_port.h>
44 #include <ipc/ipc_space.h>
46 #include <kern/clock.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>
57 #include <vm/vm_map.h>
58 #include <vm/vm_kern.h>
60 #include <device/device_types.h>
61 #include <device/device_port.h>
62 #include <device/device_server.h>
64 #include <machine/machparam.h>
66 #if defined(__i386__) || defined(__x86_64__)
67 #include <i386/pmap.h>
69 #include <IOKit/IOTypes.h>
75 * Functions in iokit:IOUserClient.cpp
78 extern void iokit_add_reference( io_object_t obj
);
80 extern ipc_port_t
iokit_port_for_object( io_object_t obj
,
81 ipc_kobject_type_t type
);
83 extern kern_return_t
iokit_client_died( io_object_t obj
,
84 ipc_port_t port
, ipc_kobject_type_t type
, mach_port_mscount_t
* mscount
);
87 iokit_client_memory_for_type(
91 vm_address_t
* address
,
95 extern ppnum_t
IOGetLastPageNumber(void);
98 * Functions imported by iokit:IOUserClient.cpp
101 extern ipc_port_t
iokit_alloc_object_port( io_object_t obj
,
102 ipc_kobject_type_t type
);
104 extern kern_return_t
iokit_destroy_object_port( ipc_port_t port
);
106 extern mach_port_name_t
iokit_make_send_right( task_t task
,
107 io_object_t obj
, ipc_kobject_type_t type
);
109 extern kern_return_t
iokit_mod_send_right( task_t task
, mach_port_name_t name
, mach_port_delta_t delta
);
111 extern io_object_t
iokit_lookup_connect_ref(io_object_t clientRef
, ipc_space_t task
);
113 extern io_object_t
iokit_lookup_connect_ref_current_task(io_object_t clientRef
);
115 extern void iokit_retain_port( ipc_port_t port
);
116 extern void iokit_release_port( ipc_port_t port
);
117 extern void iokit_release_port_send( ipc_port_t port
);
119 extern kern_return_t
iokit_switch_object_port( ipc_port_t port
, io_object_t obj
, ipc_kobject_type_t type
);
122 * Functions imported by iokit:IOMemoryDescriptor.cpp
125 extern kern_return_t
IOMapPages(vm_map_t map
, mach_vm_address_t va
, mach_vm_address_t pa
,
126 mach_vm_size_t length
, unsigned int mapFlags
);
128 extern kern_return_t
IOUnmapPages(vm_map_t map
, mach_vm_address_t va
, mach_vm_size_t length
);
130 extern kern_return_t
IOProtectCacheMode(vm_map_t map
, mach_vm_address_t va
,
131 mach_vm_size_t length
, unsigned int options
);
133 extern unsigned int IODefaultCacheBits(addr64_t pa
);
136 * Lookup a device by its port.
137 * Doesn't consume the naked send right; produces a device reference.
139 MIGEXTERN io_object_t
140 iokit_lookup_object_port(
143 register io_object_t obj
;
149 if (ip_active(port
) && (ip_kotype(port
) == IKOT_IOKIT_OBJECT
)) {
150 obj
= (io_object_t
) port
->ip_kobject
;
151 iokit_add_reference( obj
);
161 MIGEXTERN io_object_t
162 iokit_lookup_connect_port(
165 register io_object_t obj
;
171 if (ip_active(port
) && (ip_kotype(port
) == IKOT_IOKIT_CONNECT
)) {
172 obj
= (io_object_t
) port
->ip_kobject
;
173 iokit_add_reference( obj
);
184 iokit_lookup_connect_ref(io_object_t connectRef
, ipc_space_t space
)
186 io_object_t obj
= NULL
;
188 if (connectRef
&& MACH_PORT_VALID(CAST_MACH_PORT_TO_NAME(connectRef
))) {
192 kr
= ipc_object_translate(space
, CAST_MACH_PORT_TO_NAME(connectRef
), MACH_PORT_RIGHT_SEND
, (ipc_object_t
*)&port
);
194 if (kr
== KERN_SUCCESS
) {
195 assert(IP_VALID(port
));
197 if (ip_active(port
) && (ip_kotype(port
) == IKOT_IOKIT_CONNECT
)) {
198 obj
= (io_object_t
) port
->ip_kobject
;
199 iokit_add_reference(obj
);
210 iokit_lookup_connect_ref_current_task(io_object_t connectRef
)
212 return iokit_lookup_connect_ref(connectRef
, current_space());
216 iokit_retain_port( ipc_port_t port
)
218 ipc_port_reference( port
);
222 iokit_release_port( ipc_port_t port
)
224 ipc_port_release( port
);
228 iokit_release_port_send( ipc_port_t port
)
230 ipc_port_release_send( port
);
234 * Get the port for a device.
235 * Consumes a device reference; produces a naked send right.
238 iokit_make_object_port(
241 register ipc_port_t port
;
242 register ipc_port_t sendPort
;
247 port
= iokit_port_for_object( obj
, IKOT_IOKIT_OBJECT
);
249 sendPort
= ipc_port_make_send( port
);
250 iokit_release_port( port
);
254 iokit_remove_reference( obj
);
260 iokit_make_connect_port(
263 register ipc_port_t port
;
264 register ipc_port_t sendPort
;
269 port
= iokit_port_for_object( obj
, IKOT_IOKIT_CONNECT
);
271 sendPort
= ipc_port_make_send( port
);
272 iokit_release_port( port
);
276 iokit_remove_reference( obj
);
284 iokit_alloc_object_port( io_object_t obj
, ipc_kobject_type_t type
)
291 /* Allocate port, keeping a reference for it. */
292 port
= ipc_port_alloc_kernel();
296 /* set kobject & type */
297 // iokit_add_reference( obj );
298 ipc_kobject_set( port
, (ipc_kobject_t
) obj
, type
);
300 /* Request no-senders notifications on the port. */
301 notify
= ipc_port_make_sonce( port
);
303 ipc_port_nsrequest( port
, 1, notify
, ¬ify
);
304 assert( notify
== IP_NULL
);
314 iokit_destroy_object_port( ipc_port_t port
)
316 ipc_kobject_set( port
, IKO_NULL
, IKOT_NONE
);
318 // iokit_remove_reference( obj );
320 ipc_port_dealloc_kernel( port
);
323 return( KERN_SUCCESS
);
327 iokit_switch_object_port( ipc_port_t port
, io_object_t obj
, ipc_kobject_type_t type
)
329 ipc_kobject_set( port
, (ipc_kobject_t
) obj
, type
);
331 return( KERN_SUCCESS
);
334 EXTERN mach_port_name_t
335 iokit_make_send_right( task_t task
, io_object_t obj
, ipc_kobject_type_t type
)
339 mach_port_name_t name
;
342 return MACH_PORT_NULL
;
344 port
= iokit_port_for_object( obj
, type
);
346 sendPort
= ipc_port_make_send( port
);
347 iokit_release_port( port
);
351 if (IP_VALID( sendPort
)) {
353 kr
= ipc_object_copyout( task
->itk_space
, (ipc_object_t
) sendPort
,
354 MACH_MSG_TYPE_PORT_SEND
, TRUE
, &name
);
355 if ( kr
!= KERN_SUCCESS
)
356 name
= MACH_PORT_NULL
;
357 } else if ( sendPort
== IP_NULL
)
358 name
= MACH_PORT_NULL
;
359 else if ( sendPort
== IP_DEAD
)
360 name
= MACH_PORT_DEAD
;
362 iokit_remove_reference( obj
);
368 iokit_mod_send_right( task_t task
, mach_port_name_t name
, mach_port_delta_t delta
)
370 return (mach_port_mod_refs( task
->itk_space
, name
, MACH_PORT_RIGHT_SEND
, delta
));
374 * Handle the No-More_Senders notification generated from a device port destroy.
375 * Since there are no longer any tasks which hold a send right to this device
376 * port a NMS notification has been generated.
380 iokit_no_senders( mach_no_senders_notification_t
* notification
)
383 io_object_t obj
= NULL
;
384 ipc_kobject_type_t type
= IKOT_NONE
;
387 port
= (ipc_port_t
) notification
->not_header
.msgh_remote_port
;
389 // convert a port to io_object_t.
390 if( IP_VALID(port
)) {
392 if( ip_active(port
)) {
393 obj
= (io_object_t
) port
->ip_kobject
;
394 type
= ip_kotype( port
);
395 if( (IKOT_IOKIT_OBJECT
== type
)
396 || (IKOT_IOKIT_CONNECT
== type
))
397 iokit_add_reference( obj
);
405 mach_port_mscount_t mscount
= notification
->not_count
;
407 if( KERN_SUCCESS
!= iokit_client_died( obj
, port
, type
, &mscount
))
409 /* Re-request no-senders notifications on the port. */
410 notify
= ipc_port_make_sonce( port
);
412 ipc_port_nsrequest( port
, mscount
+ 1, notify
, ¬ify
);
413 assert( notify
== IP_NULL
);
415 iokit_remove_reference( obj
);
423 iokit_notify( mach_msg_header_t
* msg
)
425 switch (msg
->msgh_id
) {
426 case MACH_NOTIFY_NO_SENDERS
:
427 iokit_no_senders((mach_no_senders_notification_t
*) msg
);
430 case MACH_NOTIFY_PORT_DELETED
:
431 case MACH_NOTIFY_PORT_DESTROYED
:
432 case MACH_NOTIFY_SEND_ONCE
:
433 case MACH_NOTIFY_DEAD_NAME
:
435 printf("iokit_notify: strange notification %d\n", msg
->msgh_id
);
440 /* need to create a pmap function to generalize */
441 unsigned int IODefaultCacheBits(addr64_t pa
)
443 return(pmap_cache_attributes((ppnum_t
)(pa
>> PAGE_SHIFT
)));
446 kern_return_t
IOMapPages(vm_map_t map
, mach_vm_address_t va
, mach_vm_address_t pa
,
447 mach_vm_size_t length
, unsigned int options
)
452 pmap_t pmap
= map
->pmap
;
454 prot
= (options
& kIOMapReadOnly
)
455 ? VM_PROT_READ
: (VM_PROT_READ
|VM_PROT_WRITE
);
457 pagenum
= (ppnum_t
)atop_64(pa
);
459 switch(options
& kIOMapCacheMask
) { /* What cache mode do we need? */
461 case kIOMapDefaultCache
:
463 flags
= IODefaultCacheBits(pa
);
466 case kIOMapInhibitCache
:
470 case kIOMapWriteThruCache
:
471 flags
= VM_WIMG_WTHRU
;
474 case kIOMapWriteCombineCache
:
475 flags
= VM_WIMG_WCOMB
;
478 case kIOMapCopybackCache
:
479 flags
= VM_WIMG_COPYBACK
;
483 pmap_set_cache_attributes(pagenum
, flags
);
485 vm_map_set_cache_attr(map
, (vm_map_offset_t
)va
);
488 // Set up a block mapped area
489 pmap_map_block(pmap
, va
, pagenum
, (uint32_t) atop_64(round_page_64(length
)), prot
, 0, 0);
491 return( KERN_SUCCESS
);
494 kern_return_t
IOUnmapPages(vm_map_t map
, mach_vm_address_t va
, mach_vm_size_t length
)
496 pmap_t pmap
= map
->pmap
;
498 pmap_remove(pmap
, trunc_page_64(va
), round_page_64(va
+ length
));
500 return( KERN_SUCCESS
);
503 kern_return_t
IOProtectCacheMode(vm_map_t __unused map
, mach_vm_address_t __unused va
,
504 mach_vm_size_t __unused length
, unsigned int __unused options
)
509 pmap_t pmap
= map
->pmap
;
511 prot
= (options
& kIOMapReadOnly
)
512 ? VM_PROT_READ
: (VM_PROT_READ
|VM_PROT_WRITE
);
514 switch (options
& kIOMapCacheMask
)
516 // what cache mode do we need?
517 case kIOMapDefaultCache
:
519 return (KERN_INVALID_ARGUMENT
);
521 case kIOMapInhibitCache
:
525 case kIOMapWriteThruCache
:
526 flags
= VM_WIMG_WTHRU
;
529 case kIOMapWriteCombineCache
:
530 flags
= VM_WIMG_WCOMB
;
533 case kIOMapCopybackCache
:
534 flags
= VM_WIMG_COPYBACK
;
538 // enter each page's physical address in the target map
539 for (off
= 0; off
< length
; off
+= page_size
)
541 ppnum_t ppnum
= pmap_find_phys(pmap
, va
+ off
);
543 pmap_enter(pmap
, va
+ off
, ppnum
, prot
, flags
, TRUE
);
546 return (KERN_SUCCESS
);
549 ppnum_t
IOGetLastPageNumber(void)
551 #if __i386__ || __x86_64__
552 ppnum_t lastPage
, highest
= 0;
555 for (idx
= 0; idx
< pmap_memory_region_count
; idx
++)
557 lastPage
= pmap_memory_regions
[idx
].end
- 1;
558 if (lastPage
> highest
)
568 void IOGetTime( mach_timespec_t
* clock_time
);
569 void IOGetTime( mach_timespec_t
* clock_time
)
573 clock_get_system_nanotime(&sec
, &nsec
);
574 clock_time
->tv_sec
= (typeof(clock_time
->tv_sec
))sec
;
575 clock_time
->tv_nsec
= nsec
;