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>
67 #include <ppc/mappings.h>
70 #include <i386/pmap.h>
72 #include <IOKit/IOTypes.h>
78 * Functions in iokit:IOUserClient.cpp
81 extern void iokit_add_reference( io_object_t obj
);
83 extern ipc_port_t
iokit_port_for_object( io_object_t obj
,
84 ipc_kobject_type_t type
);
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
);
90 iokit_client_memory_for_type(
94 vm_address_t
* address
,
98 extern ppnum_t
IOGetLastPageNumber(void);
101 * Functions imported by iokit:IOUserClient.cpp
104 extern ipc_port_t
iokit_alloc_object_port( io_object_t obj
,
105 ipc_kobject_type_t type
);
107 extern kern_return_t
iokit_destroy_object_port( ipc_port_t port
);
109 extern mach_port_name_t
iokit_make_send_right( task_t task
,
110 io_object_t obj
, ipc_kobject_type_t type
);
112 extern kern_return_t
iokit_mod_send_right( task_t task
, mach_port_name_t name
, mach_port_delta_t delta
);
114 extern io_object_t
iokit_lookup_connect_ref(io_object_t clientRef
, ipc_space_t task
);
116 extern io_object_t
iokit_lookup_connect_ref_current_task(io_object_t clientRef
);
118 extern void iokit_retain_port( ipc_port_t port
);
119 extern void iokit_release_port( ipc_port_t port
);
121 extern kern_return_t
iokit_switch_object_port( ipc_port_t port
, io_object_t obj
, ipc_kobject_type_t type
);
124 * Functions imported by iokit:IOMemoryDescriptor.cpp
127 extern kern_return_t
IOMapPages(vm_map_t map
, mach_vm_address_t va
, mach_vm_address_t pa
,
128 mach_vm_size_t length
, unsigned int mapFlags
);
130 extern kern_return_t
IOUnmapPages(vm_map_t map
, mach_vm_address_t va
, mach_vm_size_t length
);
132 extern kern_return_t
IOProtectCacheMode(vm_map_t map
, mach_vm_address_t va
,
133 mach_vm_size_t length
, unsigned int options
);
135 extern unsigned int IODefaultCacheBits(addr64_t pa
);
138 * Lookup a device by its port.
139 * Doesn't consume the naked send right; produces a device reference.
141 MIGEXTERN io_object_t
142 iokit_lookup_object_port(
145 register io_object_t obj
;
151 if (ip_active(port
) && (ip_kotype(port
) == IKOT_IOKIT_OBJECT
)) {
152 obj
= (io_object_t
) port
->ip_kobject
;
153 iokit_add_reference( obj
);
163 MIGEXTERN io_object_t
164 iokit_lookup_connect_port(
167 register io_object_t obj
;
173 if (ip_active(port
) && (ip_kotype(port
) == IKOT_IOKIT_CONNECT
)) {
174 obj
= (io_object_t
) port
->ip_kobject
;
175 iokit_add_reference( obj
);
186 iokit_lookup_connect_ref(io_object_t connectRef
, ipc_space_t space
)
188 io_object_t obj
= NULL
;
190 if (connectRef
&& MACH_PORT_VALID((mach_port_name_t
)connectRef
)) {
194 kr
= ipc_object_translate(space
, (mach_port_name_t
)connectRef
, MACH_PORT_RIGHT_SEND
, (ipc_object_t
*)&port
);
196 if (kr
== KERN_SUCCESS
) {
197 assert(IP_VALID(port
));
199 if (ip_active(port
) && (ip_kotype(port
) == IKOT_IOKIT_CONNECT
)) {
200 obj
= (io_object_t
) port
->ip_kobject
;
201 iokit_add_reference(obj
);
212 iokit_lookup_connect_ref_current_task(io_object_t connectRef
)
214 return iokit_lookup_connect_ref(connectRef
, current_space());
218 iokit_retain_port( ipc_port_t port
)
220 ipc_port_reference( port
);
224 iokit_release_port( ipc_port_t port
)
226 ipc_port_release( port
);
230 * Get the port for a device.
231 * Consumes a device reference; produces a naked send right.
234 iokit_make_object_port(
237 register ipc_port_t port
;
238 register ipc_port_t sendPort
;
243 port
= iokit_port_for_object( obj
, IKOT_IOKIT_OBJECT
);
245 sendPort
= ipc_port_make_send( port
);
246 iokit_release_port( port
);
250 iokit_remove_reference( obj
);
256 iokit_make_connect_port(
259 register ipc_port_t port
;
260 register ipc_port_t sendPort
;
265 port
= iokit_port_for_object( obj
, IKOT_IOKIT_CONNECT
);
267 sendPort
= ipc_port_make_send( port
);
268 iokit_release_port( port
);
272 iokit_remove_reference( obj
);
280 iokit_alloc_object_port( io_object_t obj
, ipc_kobject_type_t type
)
287 /* Allocate port, keeping a reference for it. */
288 port
= ipc_port_alloc_kernel();
292 /* set kobject & type */
293 // iokit_add_reference( obj );
294 ipc_kobject_set( port
, (ipc_kobject_t
) obj
, type
);
296 /* Request no-senders notifications on the port. */
297 notify
= ipc_port_make_sonce( port
);
299 ipc_port_nsrequest( port
, 1, notify
, ¬ify
);
300 assert( notify
== IP_NULL
);
310 iokit_destroy_object_port( ipc_port_t port
)
312 ipc_kobject_set( port
, IKO_NULL
, IKOT_NONE
);
314 // iokit_remove_reference( obj );
316 ipc_port_dealloc_kernel( port
);
319 return( KERN_SUCCESS
);
323 iokit_switch_object_port( ipc_port_t port
, io_object_t obj
, ipc_kobject_type_t type
)
325 ipc_kobject_set( port
, (ipc_kobject_t
) obj
, type
);
327 return( KERN_SUCCESS
);
330 EXTERN mach_port_name_t
331 iokit_make_send_right( task_t task
, io_object_t obj
, ipc_kobject_type_t type
)
335 mach_port_name_t name
;
338 return MACH_PORT_NULL
;
340 port
= iokit_port_for_object( obj
, type
);
342 sendPort
= ipc_port_make_send( port
);
343 iokit_release_port( port
);
347 if (IP_VALID( sendPort
)) {
349 kr
= ipc_object_copyout( task
->itk_space
, (ipc_object_t
) sendPort
,
350 MACH_MSG_TYPE_PORT_SEND
, TRUE
, &name
);
351 if ( kr
!= KERN_SUCCESS
)
352 name
= MACH_PORT_NULL
;
353 } else if ( sendPort
== IP_NULL
)
354 name
= MACH_PORT_NULL
;
355 else if ( sendPort
== IP_DEAD
)
356 name
= MACH_PORT_DEAD
;
358 iokit_remove_reference( obj
);
364 iokit_mod_send_right( task_t task
, mach_port_name_t name
, mach_port_delta_t delta
)
366 return (mach_port_mod_refs( task
->itk_space
, name
, MACH_PORT_RIGHT_SEND
, delta
));
370 * Handle the No-More_Senders notification generated from a device port destroy.
371 * Since there are no longer any tasks which hold a send right to this device
372 * port a NMS notification has been generated.
376 iokit_no_senders( mach_no_senders_notification_t
* notification
)
379 io_object_t obj
= NULL
;
380 ipc_kobject_type_t type
= IKOT_NONE
;
383 port
= (ipc_port_t
) notification
->not_header
.msgh_remote_port
;
385 // convert a port to io_object_t.
386 if( IP_VALID(port
)) {
388 if( ip_active(port
)) {
389 obj
= (io_object_t
) port
->ip_kobject
;
390 type
= ip_kotype( port
);
391 if( (IKOT_IOKIT_OBJECT
== type
)
392 || (IKOT_IOKIT_CONNECT
== type
))
393 iokit_add_reference( obj
);
401 mach_port_mscount_t mscount
= notification
->not_count
;
403 if( KERN_SUCCESS
!= iokit_client_died( obj
, port
, type
, &mscount
))
405 /* Re-request no-senders notifications on the port. */
406 notify
= ipc_port_make_sonce( port
);
408 ipc_port_nsrequest( port
, mscount
+ 1, notify
, ¬ify
);
409 assert( notify
== IP_NULL
);
411 iokit_remove_reference( obj
);
419 iokit_notify( mach_msg_header_t
* msg
)
421 switch (msg
->msgh_id
) {
422 case MACH_NOTIFY_NO_SENDERS
:
423 iokit_no_senders((mach_no_senders_notification_t
*) msg
);
426 case MACH_NOTIFY_PORT_DELETED
:
427 case MACH_NOTIFY_PORT_DESTROYED
:
428 case MACH_NOTIFY_SEND_ONCE
:
429 case MACH_NOTIFY_DEAD_NAME
:
431 printf("iokit_notify: strange notification %d\n", msg
->msgh_id
);
436 /* need to create a pmap function to generalize */
437 unsigned int IODefaultCacheBits(addr64_t pa
)
439 return(pmap_cache_attributes(pa
>> PAGE_SHIFT
));
442 kern_return_t
IOMapPages(vm_map_t map
, mach_vm_address_t va
, mach_vm_address_t pa
,
443 mach_vm_size_t length
, unsigned int options
)
447 pmap_t pmap
= map
->pmap
;
449 prot
= (options
& kIOMapReadOnly
)
450 ? VM_PROT_READ
: (VM_PROT_READ
|VM_PROT_WRITE
);
452 switch(options
& kIOMapCacheMask
) { /* What cache mode do we need? */
454 case kIOMapDefaultCache
:
456 flags
= IODefaultCacheBits(pa
);
459 case kIOMapInhibitCache
:
463 case kIOMapWriteThruCache
:
464 flags
= VM_WIMG_WTHRU
;
467 case kIOMapWriteCombineCache
:
468 flags
= VM_WIMG_WCOMB
;
471 case kIOMapCopybackCache
:
472 flags
= VM_WIMG_COPYBACK
;
476 // Set up a block mapped area
477 pmap_map_block(pmap
, va
, (ppnum_t
)atop_64(pa
), (uint32_t) atop_64(round_page_64(length
)), prot
, flags
, 0);
479 return( KERN_SUCCESS
);
482 kern_return_t
IOUnmapPages(vm_map_t map
, mach_vm_address_t va
, mach_vm_size_t length
)
484 pmap_t pmap
= map
->pmap
;
486 pmap_remove(pmap
, trunc_page_64(va
), round_page_64(va
+ length
));
488 return( KERN_SUCCESS
);
491 kern_return_t
IOProtectCacheMode(vm_map_t __unused map
, mach_vm_address_t __unused va
,
492 mach_vm_size_t __unused length
, unsigned int __unused options
)
495 // can't remap block mappings, but ppc doesn't speculatively read from WC
501 pmap_t pmap
= map
->pmap
;
503 prot
= (options
& kIOMapReadOnly
)
504 ? VM_PROT_READ
: (VM_PROT_READ
|VM_PROT_WRITE
);
506 switch (options
& kIOMapCacheMask
)
508 // what cache mode do we need?
509 case kIOMapDefaultCache
:
511 return (KERN_INVALID_ARGUMENT
);
513 case kIOMapInhibitCache
:
517 case kIOMapWriteThruCache
:
518 flags
= VM_WIMG_WTHRU
;
521 case kIOMapWriteCombineCache
:
522 flags
= VM_WIMG_WCOMB
;
525 case kIOMapCopybackCache
:
526 flags
= VM_WIMG_COPYBACK
;
530 // enter each page's physical address in the target map
531 for (off
= 0; off
< length
; off
+= page_size
)
533 ppnum_t ppnum
= pmap_find_phys(pmap
, va
+ off
);
535 pmap_enter(pmap
, va
+ off
, ppnum
, prot
, flags
, TRUE
);
540 return (KERN_SUCCESS
);
543 ppnum_t
IOGetLastPageNumber(void)
545 ppnum_t lastPage
, highest
= 0;
549 for (idx
= 0; idx
< pmap_mem_regions_count
; idx
++)
551 lastPage
= pmap_mem_regions
[idx
].mrEnd
;
553 for (idx
= 0; idx
< pmap_memory_region_count
; idx
++)
555 lastPage
= pmap_memory_regions
[idx
].end
- 1;
559 if (lastPage
> highest
)
566 void IOGetTime( mach_timespec_t
* clock_time
);
567 void IOGetTime( mach_timespec_t
* clock_time
)
569 clock_get_system_nanotime(&clock_time
->tv_sec
, (uint32_t *) &clock_time
->tv_nsec
);