2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 #include <zone_debug.h>
27 #include <mach/boolean.h>
28 #include <mach/kern_return.h>
29 #include <mach/mig_errors.h>
30 #include <mach/port.h>
31 #include <mach/vm_param.h>
32 #include <mach/notify.h>
33 #include <mach/mach_host_server.h>
34 #include <mach/mach_types.h>
36 #include <machine/machparam.h> /* spl definitions */
38 #include <ipc/ipc_port.h>
39 #include <ipc/ipc_space.h>
41 #include <kern/clock.h>
43 #include <kern/counters.h>
44 #include <kern/queue.h>
45 #include <kern/zalloc.h>
46 #include <kern/thread.h>
47 #include <kern/task.h>
48 #include <kern/sched_prim.h>
49 #include <kern/misc_protos.h>
52 #include <vm/vm_map.h>
53 #include <vm/vm_kern.h>
55 #include <device/device_types.h>
56 #include <device/device_port.h>
57 #include <device/device_server.h>
59 #include <machine/machparam.h>
62 #include <ppc/mappings.h>
65 #include <i386/pmap.h>
67 #include <IOKit/IOTypes.h>
73 * Functions in iokit:IOUserClient.cpp
76 extern void iokit_add_reference( io_object_t obj
);
78 extern ipc_port_t
iokit_port_for_object( io_object_t obj
,
79 ipc_kobject_type_t type
);
81 extern kern_return_t
iokit_client_died( io_object_t obj
,
82 ipc_port_t port
, ipc_kobject_type_t type
, mach_port_mscount_t
* mscount
);
85 iokit_client_memory_for_type(
89 vm_address_t
* address
,
93 * Lookup a device by its port.
94 * Doesn't consume the naked send right; produces a device reference.
97 iokit_lookup_object_port(
100 register io_object_t obj
;
106 if (ip_active(port
) && (ip_kotype(port
) == IKOT_IOKIT_OBJECT
)) {
107 obj
= (io_object_t
) port
->ip_kobject
;
108 iokit_add_reference( obj
);
118 MIGEXTERN io_object_t
119 iokit_lookup_connect_port(
122 register io_object_t obj
;
128 if (ip_active(port
) && (ip_kotype(port
) == IKOT_IOKIT_CONNECT
)) {
129 obj
= (io_object_t
) port
->ip_kobject
;
130 iokit_add_reference( obj
);
141 iokit_lookup_connect_ref(io_object_t connectRef
, ipc_space_t space
)
143 io_object_t obj
= NULL
;
145 if (connectRef
&& MACH_PORT_VALID((mach_port_name_t
)connectRef
)) {
149 kr
= ipc_object_translate(space
, (mach_port_name_t
)connectRef
, MACH_PORT_RIGHT_SEND
, (ipc_object_t
*)&port
);
151 if (kr
== KERN_SUCCESS
) {
152 assert(IP_VALID(port
));
154 if (ip_active(port
) && (ip_kotype(port
) == IKOT_IOKIT_CONNECT
)) {
155 obj
= (io_object_t
) port
->ip_kobject
;
156 iokit_add_reference(obj
);
167 iokit_lookup_connect_ref_current_task(io_object_t connectRef
)
169 return iokit_lookup_connect_ref(connectRef
, 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 * Get the port for a device.
186 * Consumes a device reference; produces a naked send right.
189 iokit_make_object_port(
192 register ipc_port_t port
;
193 register ipc_port_t sendPort
;
198 port
= iokit_port_for_object( obj
, IKOT_IOKIT_OBJECT
);
200 sendPort
= ipc_port_make_send( port
);
201 iokit_release_port( port
);
205 iokit_remove_reference( obj
);
211 iokit_make_connect_port(
214 register ipc_port_t port
;
215 register ipc_port_t sendPort
;
220 port
= iokit_port_for_object( obj
, IKOT_IOKIT_CONNECT
);
222 sendPort
= ipc_port_make_send( port
);
223 iokit_release_port( port
);
227 iokit_remove_reference( obj
);
234 iokit_alloc_object_port( io_object_t obj
, ipc_kobject_type_t type
);
239 iokit_alloc_object_port( io_object_t obj
, ipc_kobject_type_t type
)
246 /* Allocate port, keeping a reference for it. */
247 port
= ipc_port_alloc_kernel();
251 /* set kobject & type */
252 // iokit_add_reference( obj );
253 ipc_kobject_set( port
, (ipc_kobject_t
) obj
, type
);
255 /* Request no-senders notifications on the port. */
256 notify
= ipc_port_make_sonce( port
);
258 ipc_port_nsrequest( port
, 1, notify
, ¬ify
);
259 assert( notify
== IP_NULL
);
269 iokit_destroy_object_port( ipc_port_t port
)
271 ipc_kobject_set( port
, IKO_NULL
, IKOT_NONE
);
273 // iokit_remove_reference( obj );
275 ipc_port_dealloc_kernel( port
);
278 return( KERN_SUCCESS
);
282 iokit_switch_object_port( ipc_port_t port
, io_object_t obj
, ipc_kobject_type_t type
)
284 ipc_kobject_set( port
, (ipc_kobject_t
) obj
, type
);
286 return( KERN_SUCCESS
);
289 EXTERN mach_port_name_t
290 iokit_make_send_right( task_t task
, io_object_t obj
, ipc_kobject_type_t type
)
294 mach_port_name_t name
;
297 return MACH_PORT_NULL
;
299 port
= iokit_port_for_object( obj
, type
);
301 sendPort
= ipc_port_make_send( port
);
302 iokit_release_port( port
);
306 if (IP_VALID( sendPort
)) {
308 kr
= ipc_object_copyout( task
->itk_space
, (ipc_object_t
) sendPort
,
309 MACH_MSG_TYPE_PORT_SEND
, TRUE
, &name
);
310 if ( kr
!= KERN_SUCCESS
)
311 name
= MACH_PORT_NULL
;
312 } else if ( sendPort
== IP_NULL
)
313 name
= MACH_PORT_NULL
;
314 else if ( sendPort
== IP_DEAD
)
315 name
= MACH_PORT_DEAD
;
317 iokit_remove_reference( obj
);
323 iokit_mod_send_right( task_t task
, mach_port_name_t name
, mach_port_delta_t delta
)
325 return (mach_port_mod_refs( task
->itk_space
, name
, MACH_PORT_RIGHT_SEND
, delta
));
329 * Handle the No-More_Senders notification generated from a device port destroy.
330 * Since there are no longer any tasks which hold a send right to this device
331 * port a NMS notification has been generated.
335 iokit_no_senders( mach_no_senders_notification_t
* notification
)
338 io_object_t obj
= NULL
;
339 ipc_kobject_type_t type
;
342 port
= (ipc_port_t
) notification
->not_header
.msgh_remote_port
;
344 // convert a port to io_object_t.
345 if( IP_VALID(port
)) {
347 if( ip_active(port
)) {
348 obj
= (io_object_t
) port
->ip_kobject
;
349 type
= ip_kotype( port
);
350 if( (IKOT_IOKIT_OBJECT
== type
)
351 || (IKOT_IOKIT_CONNECT
== type
))
352 iokit_add_reference( obj
);
360 mach_port_mscount_t mscount
= notification
->not_count
;
362 if( KERN_SUCCESS
!= iokit_client_died( obj
, port
, type
, &mscount
))
364 /* Re-request no-senders notifications on the port. */
365 notify
= ipc_port_make_sonce( port
);
367 ipc_port_nsrequest( port
, mscount
+ 1, notify
, ¬ify
);
368 assert( notify
== IP_NULL
);
370 iokit_remove_reference( obj
);
378 iokit_notify( mach_msg_header_t
* msg
)
380 switch (msg
->msgh_id
) {
381 case MACH_NOTIFY_NO_SENDERS
:
382 iokit_no_senders((mach_no_senders_notification_t
*) msg
);
385 case MACH_NOTIFY_PORT_DELETED
:
386 case MACH_NOTIFY_PORT_DESTROYED
:
387 case MACH_NOTIFY_SEND_ONCE
:
388 case MACH_NOTIFY_DEAD_NAME
:
390 printf("iokit_notify: strange notification %ld\n", msg
->msgh_id
);
395 /* need to create a pmap function to generalize */
396 unsigned int IODefaultCacheBits(addr64_t pa
)
400 struct phys_entry
* pp
;
402 // Find physical address
403 if ((pp
= pmap_find_physentry(pa
>> 12))) {
404 // Use physical attributes as default
405 // NOTE: DEVICE_PAGER_FLAGS are made to line up
406 flags
= VM_MEM_COHERENT
; /* We only support coherent memory */
407 if(pp
->ppLink
& ppG
) flags
|= VM_MEM_GUARDED
; /* Add in guarded if it is */
408 if(pp
->ppLink
& ppI
) flags
|= VM_MEM_NOT_CACHEABLE
; /* Add in cache inhibited if so */
410 // If no physical, just hard code attributes
413 extern pmap_paddr_t avail_end
;
416 flags
= VM_WIMG_COPYBACK
;
424 kern_return_t
IOMapPages(vm_map_t map
, vm_offset_t va
, vm_offset_t pa
,
425 vm_size_t length
, unsigned int options
)
430 pmap_t pmap
= map
->pmap
;
432 prot
= (options
& kIOMapReadOnly
)
433 ? VM_PROT_READ
: (VM_PROT_READ
|VM_PROT_WRITE
);
435 switch(options
& kIOMapCacheMask
) { /* What cache mode do we need? */
437 case kIOMapDefaultCache
:
439 flags
= IODefaultCacheBits(pa
);
442 case kIOMapInhibitCache
:
446 case kIOMapWriteThruCache
:
447 flags
= VM_WIMG_WTHRU
;
450 case kIOWriteCombineCache
:
451 flags
= VM_WIMG_WCOMB
;
454 case kIOMapCopybackCache
:
455 flags
= VM_WIMG_COPYBACK
;
460 // Set up a block mapped area
461 pmap_map_block(pmap
, (addr64_t
)va
, (ppnum_t
)(pa
>> 12), (uint32_t)(length
>> 12), prot
, flags
, 0);
464 // enter each page's physical address in the target map
466 for (off
= 0; off
< length
; off
+= page_size
)
467 pmap_enter(pmap
, va
+ off
, (pa
+ off
) >> 12, prot
, flags
, TRUE
);
471 return( KERN_SUCCESS
);
474 kern_return_t
IOUnmapPages(vm_map_t map
, vm_offset_t va
, vm_size_t length
)
476 pmap_t pmap
= map
->pmap
;
478 pmap_remove(pmap
, trunc_page_64(va
), round_page_64(va
+ length
));
480 return( KERN_SUCCESS
);
483 void IOGetTime( mach_timespec_t
* clock_time
);
484 void IOGetTime( mach_timespec_t
* clock_time
)
486 clock_get_system_nanotime(&clock_time
->tv_sec
, &clock_time
->tv_nsec
);