2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 #include <zone_debug.h>
29 #include <mach/boolean.h>
30 #include <mach/kern_return.h>
31 #include <mach/mig_errors.h>
32 #include <mach/port.h>
33 #include <mach/vm_param.h>
34 #include <mach/notify.h>
35 #include <mach/mach_host_server.h>
36 #include <mach/mach_types.h>
38 #include <machine/machparam.h> /* spl definitions */
40 #include <ipc/ipc_port.h>
41 #include <ipc/ipc_space.h>
43 #include <kern/clock.h>
46 #include <kern/counters.h>
47 #include <kern/queue.h>
48 #include <kern/zalloc.h>
49 #include <kern/thread.h>
50 #include <kern/thread_swap.h>
51 #include <kern/task.h>
52 #include <kern/sched_prim.h>
53 #include <kern/misc_protos.h>
56 #include <vm/vm_map.h>
57 #include <vm/vm_kern.h>
59 #include <device/device_types.h>
60 #include <device/device_port.h>
61 #include <device/device_server.h>
63 #include <machine/machparam.h>
66 #include <ppc/mappings.h>
68 #include <IOKit/IOTypes.h>
74 * Functions in iokit:IOUserClient.cpp
77 extern void iokit_add_reference( io_object_t obj
);
79 extern void iokit_remove_reference( io_object_t obj
);
81 extern ipc_port_t
iokit_port_for_object( io_object_t obj
,
82 ipc_kobject_type_t type
);
84 extern kern_return_t
iokit_client_died( io_object_t obj
,
85 ipc_port_t port
, ipc_kobject_type_t type
, mach_port_mscount_t
* mscount
);
88 iokit_client_memory_for_type(
92 vm_address_t
* address
,
96 * Lookup a device by its port.
97 * Doesn't consume the naked send right; produces a device reference.
100 iokit_lookup_object_port(
103 register io_object_t obj
;
109 if (ip_active(port
) && (ip_kotype(port
) == IKOT_IOKIT_OBJECT
)) {
110 obj
= (io_object_t
) port
->ip_kobject
;
111 iokit_add_reference( obj
);
121 MIGEXTERN io_object_t
122 iokit_lookup_connect_port(
125 register io_object_t obj
;
131 if (ip_active(port
) && (ip_kotype(port
) == IKOT_IOKIT_CONNECT
)) {
132 obj
= (io_object_t
) port
->ip_kobject
;
133 iokit_add_reference( obj
);
144 iokit_lookup_connect_ref(io_object_t connectRef
, ipc_space_t space
)
146 io_object_t obj
= NULL
;
148 if (connectRef
&& MACH_PORT_VALID((mach_port_name_t
)connectRef
)) {
152 kr
= ipc_object_translate(space
, (mach_port_name_t
)connectRef
, MACH_PORT_RIGHT_SEND
, (ipc_object_t
*)&port
);
154 if (kr
== KERN_SUCCESS
) {
155 assert(IP_VALID(port
));
157 if (ip_active(port
) && (ip_kotype(port
) == IKOT_IOKIT_CONNECT
)) {
158 obj
= (io_object_t
) port
->ip_kobject
;
159 iokit_add_reference(obj
);
170 iokit_lookup_connect_ref_current_task(io_object_t connectRef
)
172 return iokit_lookup_connect_ref(connectRef
, current_space());
176 iokit_retain_port( ipc_port_t port
)
178 ipc_port_reference( port
);
182 iokit_release_port( ipc_port_t port
)
184 ipc_port_release( port
);
188 * Get the port for a device.
189 * Consumes a device reference; produces a naked send right.
192 iokit_make_object_port(
195 register ipc_port_t port
;
196 register ipc_port_t sendPort
;
201 port
= iokit_port_for_object( obj
, IKOT_IOKIT_OBJECT
);
203 sendPort
= ipc_port_make_send( port
);
204 iokit_release_port( port
);
208 iokit_remove_reference( obj
);
214 iokit_make_connect_port(
217 register ipc_port_t port
;
218 register ipc_port_t sendPort
;
223 port
= iokit_port_for_object( obj
, IKOT_IOKIT_CONNECT
);
225 sendPort
= ipc_port_make_send( port
);
226 iokit_release_port( port
);
230 iokit_remove_reference( obj
);
237 iokit_alloc_object_port( io_object_t obj
, ipc_kobject_type_t type
);
242 iokit_alloc_object_port( io_object_t obj
, ipc_kobject_type_t type
)
249 /* Allocate port, keeping a reference for it. */
250 port
= ipc_port_alloc_kernel();
254 /* set kobject & type */
255 // iokit_add_reference( obj );
256 ipc_kobject_set( port
, (ipc_kobject_t
) obj
, type
);
258 /* Request no-senders notifications on the port. */
259 notify
= ipc_port_make_sonce( port
);
261 ipc_port_nsrequest( port
, 1, notify
, ¬ify
);
262 assert( notify
== IP_NULL
);
272 iokit_destroy_object_port( ipc_port_t port
)
274 ipc_kobject_set( port
, IKO_NULL
, IKOT_NONE
);
276 // iokit_remove_reference( obj );
278 ipc_port_dealloc_kernel( port
);
281 return( KERN_SUCCESS
);
285 iokit_switch_object_port( ipc_port_t port
, io_object_t obj
, ipc_kobject_type_t type
)
287 ipc_kobject_set( port
, (ipc_kobject_t
) obj
, type
);
289 return( KERN_SUCCESS
);
292 EXTERN mach_port_name_t
293 iokit_make_send_right( task_t task
, io_object_t obj
, ipc_kobject_type_t type
)
297 mach_port_name_t name
;
300 return MACH_PORT_NULL
;
302 port
= iokit_port_for_object( obj
, type
);
304 sendPort
= ipc_port_make_send( port
);
305 iokit_release_port( port
);
309 if (IP_VALID( sendPort
)) {
311 kr
= ipc_object_copyout( task
->itk_space
, (ipc_object_t
) sendPort
,
312 MACH_MSG_TYPE_PORT_SEND
, TRUE
, &name
);
313 if ( kr
!= KERN_SUCCESS
)
314 name
= MACH_PORT_NULL
;
315 } else if ( sendPort
== IP_NULL
)
316 name
= MACH_PORT_NULL
;
317 else if ( sendPort
== IP_DEAD
)
318 name
= MACH_PORT_DEAD
;
320 iokit_remove_reference( obj
);
326 * Handle the No-More_Senders notification generated from a device port destroy.
327 * Since there are no longer any tasks which hold a send right to this device
328 * port a NMS notification has been generated.
332 iokit_no_senders( mach_no_senders_notification_t
* notification
)
335 io_object_t obj
= NULL
;
336 ipc_kobject_type_t type
;
339 port
= (ipc_port_t
) notification
->not_header
.msgh_remote_port
;
341 // convert a port to io_object_t.
342 if( IP_VALID(port
)) {
344 if( ip_active(port
)) {
345 obj
= (io_object_t
) port
->ip_kobject
;
346 type
= ip_kotype( port
);
347 if( (IKOT_IOKIT_OBJECT
== type
)
348 || (IKOT_IOKIT_CONNECT
== type
))
349 iokit_add_reference( obj
);
357 mach_port_mscount_t mscount
= notification
->not_count
;
359 if( KERN_SUCCESS
!= iokit_client_died( obj
, port
, type
, &mscount
))
361 /* Re-request no-senders notifications on the port. */
362 notify
= ipc_port_make_sonce( port
);
364 ipc_port_nsrequest( port
, mscount
+ 1, notify
, ¬ify
);
365 assert( notify
== IP_NULL
);
367 iokit_remove_reference( obj
);
375 iokit_notify( mach_msg_header_t
* msg
)
377 switch (msg
->msgh_id
) {
378 case MACH_NOTIFY_NO_SENDERS
:
379 iokit_no_senders((mach_no_senders_notification_t
*) msg
);
382 case MACH_NOTIFY_PORT_DELETED
:
383 case MACH_NOTIFY_PORT_DESTROYED
:
384 case MACH_NOTIFY_SEND_ONCE
:
385 case MACH_NOTIFY_DEAD_NAME
:
387 printf("iokit_notify: strange notification %ld\n", msg
->msgh_id
);
392 /* need to create a pmap function to generalize */
393 unsigned int IODefaultCacheBits(addr64_t pa
)
397 struct phys_entry
* pp
;
399 // Find physical address
400 if ((pp
= pmap_find_physentry(pa
>> 12))) {
401 // Use physical attributes as default
402 // NOTE: DEVICE_PAGER_FLAGS are made to line up
403 flags
= VM_MEM_COHERENT
; /* We only support coherent memory */
404 if(pp
->ppLink
& ppG
) flags
|= VM_MEM_GUARDED
; /* Add in guarded if it is */
405 if(pp
->ppLink
& ppI
) flags
|= VM_MEM_NOT_CACHEABLE
; /* Add in cache inhibited if so */
407 // If no physical, just hard code attributes
410 extern vm_offset_t avail_end
;
413 flags
= VM_WIMG_COPYBACK
;
421 kern_return_t
IOMapPages(vm_map_t map
, vm_offset_t va
, vm_offset_t pa
,
422 vm_size_t length
, unsigned int options
)
427 pmap_t pmap
= map
->pmap
;
429 prot
= (options
& kIOMapReadOnly
)
430 ? VM_PROT_READ
: (VM_PROT_READ
|VM_PROT_WRITE
);
432 switch(options
& kIOMapCacheMask
) { /* What cache mode do we need? */
434 case kIOMapDefaultCache
:
436 flags
= IODefaultCacheBits(pa
);
439 case kIOMapInhibitCache
:
443 case kIOMapWriteThruCache
:
444 flags
= VM_WIMG_WTHRU
;
447 case kIOWriteCombineCache
:
448 flags
= VM_WIMG_WCOMB
;
451 case kIOMapCopybackCache
:
452 flags
= VM_WIMG_COPYBACK
;
457 // Set up a block mapped area
458 pmap_map_block(pmap
, (addr64_t
)va
, (ppnum_t
)(pa
>> 12), length
, prot
, flags
, 0);
461 // enter each page's physical address in the target map
463 for (off
= 0; off
< length
; off
+= page_size
)
464 pmap_enter(pmap
, va
+ off
, (pa
+ off
) >> 12, prot
, flags
, TRUE
);
468 return( KERN_SUCCESS
);
471 kern_return_t
IOUnmapPages(vm_map_t map
, vm_offset_t va
, vm_size_t length
)
473 pmap_t pmap
= map
->pmap
;
475 pmap_remove(pmap
, trunc_page_64(va
), round_page_64(va
+ length
));
477 return( KERN_SUCCESS
);
480 void IOGetTime( mach_timespec_t
* clock_time
);
481 void IOGetTime( mach_timespec_t
* clock_time
)
483 clock_get_system_nanotime(&clock_time
->tv_sec
, &clock_time
->tv_nsec
);