2 * Copyright (c) 2000-2004 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 * Lookup a device by its port.
99 * Doesn't consume the naked send right; produces a device reference.
101 MIGEXTERN io_object_t
102 iokit_lookup_object_port(
105 register io_object_t obj
;
111 if (ip_active(port
) && (ip_kotype(port
) == IKOT_IOKIT_OBJECT
)) {
112 obj
= (io_object_t
) port
->ip_kobject
;
113 iokit_add_reference( obj
);
123 MIGEXTERN io_object_t
124 iokit_lookup_connect_port(
127 register io_object_t obj
;
133 if (ip_active(port
) && (ip_kotype(port
) == IKOT_IOKIT_CONNECT
)) {
134 obj
= (io_object_t
) port
->ip_kobject
;
135 iokit_add_reference( obj
);
146 iokit_lookup_connect_ref(io_object_t connectRef
, ipc_space_t space
)
148 io_object_t obj
= NULL
;
150 if (connectRef
&& MACH_PORT_VALID((mach_port_name_t
)connectRef
)) {
154 kr
= ipc_object_translate(space
, (mach_port_name_t
)connectRef
, MACH_PORT_RIGHT_SEND
, (ipc_object_t
*)&port
);
156 if (kr
== KERN_SUCCESS
) {
157 assert(IP_VALID(port
));
159 if (ip_active(port
) && (ip_kotype(port
) == IKOT_IOKIT_CONNECT
)) {
160 obj
= (io_object_t
) port
->ip_kobject
;
161 iokit_add_reference(obj
);
172 iokit_lookup_connect_ref_current_task(io_object_t connectRef
)
174 return iokit_lookup_connect_ref(connectRef
, current_space());
178 iokit_retain_port( ipc_port_t port
)
180 ipc_port_reference( port
);
184 iokit_release_port( ipc_port_t port
)
186 ipc_port_release( port
);
190 * Get the port for a device.
191 * Consumes a device reference; produces a naked send right.
194 iokit_make_object_port(
197 register ipc_port_t port
;
198 register ipc_port_t sendPort
;
203 port
= iokit_port_for_object( obj
, IKOT_IOKIT_OBJECT
);
205 sendPort
= ipc_port_make_send( port
);
206 iokit_release_port( port
);
210 iokit_remove_reference( obj
);
216 iokit_make_connect_port(
219 register ipc_port_t port
;
220 register ipc_port_t sendPort
;
225 port
= iokit_port_for_object( obj
, IKOT_IOKIT_CONNECT
);
227 sendPort
= ipc_port_make_send( port
);
228 iokit_release_port( port
);
232 iokit_remove_reference( obj
);
239 iokit_alloc_object_port( io_object_t obj
, ipc_kobject_type_t type
);
244 iokit_alloc_object_port( io_object_t obj
, ipc_kobject_type_t type
)
251 /* Allocate port, keeping a reference for it. */
252 port
= ipc_port_alloc_kernel();
256 /* set kobject & type */
257 // iokit_add_reference( obj );
258 ipc_kobject_set( port
, (ipc_kobject_t
) obj
, type
);
260 /* Request no-senders notifications on the port. */
261 notify
= ipc_port_make_sonce( port
);
263 ipc_port_nsrequest( port
, 1, notify
, ¬ify
);
264 assert( notify
== IP_NULL
);
274 iokit_destroy_object_port( ipc_port_t port
)
276 ipc_kobject_set( port
, IKO_NULL
, IKOT_NONE
);
278 // iokit_remove_reference( obj );
280 ipc_port_dealloc_kernel( port
);
283 return( KERN_SUCCESS
);
287 iokit_switch_object_port( ipc_port_t port
, io_object_t obj
, ipc_kobject_type_t type
)
289 ipc_kobject_set( port
, (ipc_kobject_t
) obj
, type
);
291 return( KERN_SUCCESS
);
294 EXTERN mach_port_name_t
295 iokit_make_send_right( task_t task
, io_object_t obj
, ipc_kobject_type_t type
)
299 mach_port_name_t name
;
302 return MACH_PORT_NULL
;
304 port
= iokit_port_for_object( obj
, type
);
306 sendPort
= ipc_port_make_send( port
);
307 iokit_release_port( port
);
311 if (IP_VALID( sendPort
)) {
313 kr
= ipc_object_copyout( task
->itk_space
, (ipc_object_t
) sendPort
,
314 MACH_MSG_TYPE_PORT_SEND
, TRUE
, &name
);
315 if ( kr
!= KERN_SUCCESS
)
316 name
= MACH_PORT_NULL
;
317 } else if ( sendPort
== IP_NULL
)
318 name
= MACH_PORT_NULL
;
319 else if ( sendPort
== IP_DEAD
)
320 name
= MACH_PORT_DEAD
;
322 iokit_remove_reference( obj
);
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
;
347 port
= (ipc_port_t
) notification
->not_header
.msgh_remote_port
;
349 // convert a port to io_object_t.
350 if( IP_VALID(port
)) {
352 if( ip_active(port
)) {
353 obj
= (io_object_t
) port
->ip_kobject
;
354 type
= ip_kotype( port
);
355 if( (IKOT_IOKIT_OBJECT
== type
)
356 || (IKOT_IOKIT_CONNECT
== type
))
357 iokit_add_reference( obj
);
365 mach_port_mscount_t mscount
= notification
->not_count
;
367 if( KERN_SUCCESS
!= iokit_client_died( obj
, port
, type
, &mscount
))
369 /* Re-request no-senders notifications on the port. */
370 notify
= ipc_port_make_sonce( port
);
372 ipc_port_nsrequest( port
, mscount
+ 1, notify
, ¬ify
);
373 assert( notify
== IP_NULL
);
375 iokit_remove_reference( obj
);
383 iokit_notify( mach_msg_header_t
* msg
)
385 switch (msg
->msgh_id
) {
386 case MACH_NOTIFY_NO_SENDERS
:
387 iokit_no_senders((mach_no_senders_notification_t
*) msg
);
390 case MACH_NOTIFY_PORT_DELETED
:
391 case MACH_NOTIFY_PORT_DESTROYED
:
392 case MACH_NOTIFY_SEND_ONCE
:
393 case MACH_NOTIFY_DEAD_NAME
:
395 printf("iokit_notify: strange notification %ld\n", msg
->msgh_id
);
400 /* need to create a pmap function to generalize */
401 unsigned int IODefaultCacheBits(addr64_t pa
)
405 struct phys_entry
* pp
;
407 // Find physical address
408 if ((pp
= pmap_find_physentry(pa
>> 12))) {
409 // Use physical attributes as default
410 // NOTE: DEVICE_PAGER_FLAGS are made to line up
411 flags
= VM_MEM_COHERENT
; /* We only support coherent memory */
412 if(pp
->ppLink
& ppG
) flags
|= VM_MEM_GUARDED
; /* Add in guarded if it is */
413 if(pp
->ppLink
& ppI
) flags
|= VM_MEM_NOT_CACHEABLE
; /* Add in cache inhibited if so */
415 // If no physical, just hard code attributes
418 extern pmap_paddr_t avail_end
;
421 flags
= VM_WIMG_COPYBACK
;
429 kern_return_t
IOMapPages(vm_map_t map
, vm_offset_t va
, vm_offset_t pa
,
430 vm_size_t length
, unsigned int options
)
435 pmap_t pmap
= map
->pmap
;
437 prot
= (options
& kIOMapReadOnly
)
438 ? VM_PROT_READ
: (VM_PROT_READ
|VM_PROT_WRITE
);
440 switch(options
& kIOMapCacheMask
) { /* What cache mode do we need? */
442 case kIOMapDefaultCache
:
444 flags
= IODefaultCacheBits(pa
);
447 case kIOMapInhibitCache
:
451 case kIOMapWriteThruCache
:
452 flags
= VM_WIMG_WTHRU
;
455 case kIOWriteCombineCache
:
456 flags
= VM_WIMG_WCOMB
;
459 case kIOMapCopybackCache
:
460 flags
= VM_WIMG_COPYBACK
;
465 // Set up a block mapped area
466 pmap_map_block(pmap
, (addr64_t
)va
, (ppnum_t
)(pa
>> 12), (uint32_t)(length
>> 12), prot
, flags
, 0);
469 // enter each page's physical address in the target map
471 for (off
= 0; off
< length
; off
+= page_size
)
472 pmap_enter(pmap
, va
+ off
, (pa
+ off
) >> 12, prot
, flags
, TRUE
);
476 return( KERN_SUCCESS
);
479 kern_return_t
IOUnmapPages(vm_map_t map
, vm_offset_t va
, vm_size_t length
)
481 pmap_t pmap
= map
->pmap
;
483 pmap_remove(pmap
, trunc_page_64(va
), round_page_64(va
+ length
));
485 return( KERN_SUCCESS
);
488 void IOGetTime( mach_timespec_t
* clock_time
);
489 void IOGetTime( mach_timespec_t
* clock_time
)
491 clock_get_system_nanotime(&clock_time
->tv_sec
, &clock_time
->tv_nsec
);