2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 #include <zone_debug.h>
34 #include <mach/boolean.h>
35 #include <mach/kern_return.h>
36 #include <mach/mig_errors.h>
37 #include <mach/port.h>
38 #include <mach/vm_param.h>
39 #include <mach/notify.h>
40 #include <mach/mach_host_server.h>
41 #include <mach/mach_types.h>
43 #include <machine/machparam.h> /* spl definitions */
45 #include <ipc/ipc_port.h>
46 #include <ipc/ipc_space.h>
48 #include <kern/clock.h>
50 #include <kern/counters.h>
51 #include <kern/queue.h>
52 #include <kern/zalloc.h>
53 #include <kern/thread.h>
54 #include <kern/task.h>
55 #include <kern/sched_prim.h>
56 #include <kern/misc_protos.h>
59 #include <vm/vm_map.h>
60 #include <vm/vm_kern.h>
62 #include <device/device_types.h>
63 #include <device/device_port.h>
64 #include <device/device_server.h>
66 #include <machine/machparam.h>
69 #include <ppc/mappings.h>
72 #include <i386/pmap.h>
74 #include <IOKit/IOTypes.h>
80 * Functions in iokit:IOUserClient.cpp
83 extern void iokit_add_reference( io_object_t obj
);
85 extern ipc_port_t
iokit_port_for_object( io_object_t obj
,
86 ipc_kobject_type_t type
);
88 extern kern_return_t
iokit_client_died( io_object_t obj
,
89 ipc_port_t port
, ipc_kobject_type_t type
, mach_port_mscount_t
* mscount
);
92 iokit_client_memory_for_type(
96 vm_address_t
* address
,
100 * Lookup a device by its port.
101 * Doesn't consume the naked send right; produces a device reference.
103 MIGEXTERN io_object_t
104 iokit_lookup_object_port(
107 register io_object_t obj
;
113 if (ip_active(port
) && (ip_kotype(port
) == IKOT_IOKIT_OBJECT
)) {
114 obj
= (io_object_t
) port
->ip_kobject
;
115 iokit_add_reference( obj
);
125 MIGEXTERN io_object_t
126 iokit_lookup_connect_port(
129 register io_object_t obj
;
135 if (ip_active(port
) && (ip_kotype(port
) == IKOT_IOKIT_CONNECT
)) {
136 obj
= (io_object_t
) port
->ip_kobject
;
137 iokit_add_reference( obj
);
148 iokit_lookup_connect_ref(io_object_t connectRef
, ipc_space_t space
)
150 io_object_t obj
= NULL
;
152 if (connectRef
&& MACH_PORT_VALID((mach_port_name_t
)connectRef
)) {
156 kr
= ipc_object_translate(space
, (mach_port_name_t
)connectRef
, MACH_PORT_RIGHT_SEND
, (ipc_object_t
*)&port
);
158 if (kr
== KERN_SUCCESS
) {
159 assert(IP_VALID(port
));
161 if (ip_active(port
) && (ip_kotype(port
) == IKOT_IOKIT_CONNECT
)) {
162 obj
= (io_object_t
) port
->ip_kobject
;
163 iokit_add_reference(obj
);
174 iokit_lookup_connect_ref_current_task(io_object_t connectRef
)
176 return iokit_lookup_connect_ref(connectRef
, current_space());
180 iokit_retain_port( ipc_port_t port
)
182 ipc_port_reference( port
);
186 iokit_release_port( ipc_port_t port
)
188 ipc_port_release( port
);
192 * Get the port for a device.
193 * Consumes a device reference; produces a naked send right.
196 iokit_make_object_port(
199 register ipc_port_t port
;
200 register ipc_port_t sendPort
;
205 port
= iokit_port_for_object( obj
, IKOT_IOKIT_OBJECT
);
207 sendPort
= ipc_port_make_send( port
);
208 iokit_release_port( port
);
212 iokit_remove_reference( obj
);
218 iokit_make_connect_port(
221 register ipc_port_t port
;
222 register ipc_port_t sendPort
;
227 port
= iokit_port_for_object( obj
, IKOT_IOKIT_CONNECT
);
229 sendPort
= ipc_port_make_send( port
);
230 iokit_release_port( port
);
234 iokit_remove_reference( obj
);
241 iokit_alloc_object_port( io_object_t obj
, ipc_kobject_type_t type
);
246 iokit_alloc_object_port( io_object_t obj
, ipc_kobject_type_t type
)
253 /* Allocate port, keeping a reference for it. */
254 port
= ipc_port_alloc_kernel();
258 /* set kobject & type */
259 // iokit_add_reference( obj );
260 ipc_kobject_set( port
, (ipc_kobject_t
) obj
, type
);
262 /* Request no-senders notifications on the port. */
263 notify
= ipc_port_make_sonce( port
);
265 ipc_port_nsrequest( port
, 1, notify
, ¬ify
);
266 assert( notify
== IP_NULL
);
276 iokit_destroy_object_port( ipc_port_t port
)
278 ipc_kobject_set( port
, IKO_NULL
, IKOT_NONE
);
280 // iokit_remove_reference( obj );
282 ipc_port_dealloc_kernel( port
);
285 return( KERN_SUCCESS
);
289 iokit_switch_object_port( ipc_port_t port
, io_object_t obj
, ipc_kobject_type_t type
)
291 ipc_kobject_set( port
, (ipc_kobject_t
) obj
, type
);
293 return( KERN_SUCCESS
);
296 EXTERN mach_port_name_t
297 iokit_make_send_right( task_t task
, io_object_t obj
, ipc_kobject_type_t type
)
301 mach_port_name_t name
;
304 return MACH_PORT_NULL
;
306 port
= iokit_port_for_object( obj
, type
);
308 sendPort
= ipc_port_make_send( port
);
309 iokit_release_port( port
);
313 if (IP_VALID( sendPort
)) {
315 kr
= ipc_object_copyout( task
->itk_space
, (ipc_object_t
) sendPort
,
316 MACH_MSG_TYPE_PORT_SEND
, TRUE
, &name
);
317 if ( kr
!= KERN_SUCCESS
)
318 name
= MACH_PORT_NULL
;
319 } else if ( sendPort
== IP_NULL
)
320 name
= MACH_PORT_NULL
;
321 else if ( sendPort
== IP_DEAD
)
322 name
= MACH_PORT_DEAD
;
324 iokit_remove_reference( obj
);
330 iokit_mod_send_right( task_t task
, mach_port_name_t name
, mach_port_delta_t delta
)
332 return (mach_port_mod_refs( task
->itk_space
, name
, MACH_PORT_RIGHT_SEND
, delta
));
336 * Handle the No-More_Senders notification generated from a device port destroy.
337 * Since there are no longer any tasks which hold a send right to this device
338 * port a NMS notification has been generated.
342 iokit_no_senders( mach_no_senders_notification_t
* notification
)
345 io_object_t obj
= NULL
;
346 ipc_kobject_type_t type
;
349 port
= (ipc_port_t
) notification
->not_header
.msgh_remote_port
;
351 // convert a port to io_object_t.
352 if( IP_VALID(port
)) {
354 if( ip_active(port
)) {
355 obj
= (io_object_t
) port
->ip_kobject
;
356 type
= ip_kotype( port
);
357 if( (IKOT_IOKIT_OBJECT
== type
)
358 || (IKOT_IOKIT_CONNECT
== type
))
359 iokit_add_reference( obj
);
367 mach_port_mscount_t mscount
= notification
->not_count
;
369 if( KERN_SUCCESS
!= iokit_client_died( obj
, port
, type
, &mscount
))
371 /* Re-request no-senders notifications on the port. */
372 notify
= ipc_port_make_sonce( port
);
374 ipc_port_nsrequest( port
, mscount
+ 1, notify
, ¬ify
);
375 assert( notify
== IP_NULL
);
377 iokit_remove_reference( obj
);
385 iokit_notify( mach_msg_header_t
* msg
)
387 switch (msg
->msgh_id
) {
388 case MACH_NOTIFY_NO_SENDERS
:
389 iokit_no_senders((mach_no_senders_notification_t
*) msg
);
392 case MACH_NOTIFY_PORT_DELETED
:
393 case MACH_NOTIFY_PORT_DESTROYED
:
394 case MACH_NOTIFY_SEND_ONCE
:
395 case MACH_NOTIFY_DEAD_NAME
:
397 printf("iokit_notify: strange notification %ld\n", msg
->msgh_id
);
402 /* need to create a pmap function to generalize */
403 unsigned int IODefaultCacheBits(addr64_t pa
)
407 struct phys_entry
* pp
;
409 // Find physical address
410 if ((pp
= pmap_find_physentry(pa
>> 12))) {
411 // Use physical attributes as default
412 // NOTE: DEVICE_PAGER_FLAGS are made to line up
413 flags
= VM_MEM_COHERENT
; /* We only support coherent memory */
414 if(pp
->ppLink
& ppG
) flags
|= VM_MEM_GUARDED
; /* Add in guarded if it is */
415 if(pp
->ppLink
& ppI
) flags
|= VM_MEM_NOT_CACHEABLE
; /* Add in cache inhibited if so */
417 // If no physical, just hard code attributes
420 extern pmap_paddr_t avail_end
;
423 flags
= VM_WIMG_COPYBACK
;
431 kern_return_t
IOMapPages(vm_map_t map
, vm_offset_t va
, vm_offset_t pa
,
432 vm_size_t length
, unsigned int options
)
437 pmap_t pmap
= map
->pmap
;
439 prot
= (options
& kIOMapReadOnly
)
440 ? VM_PROT_READ
: (VM_PROT_READ
|VM_PROT_WRITE
);
442 switch(options
& kIOMapCacheMask
) { /* What cache mode do we need? */
444 case kIOMapDefaultCache
:
446 flags
= IODefaultCacheBits(pa
);
449 case kIOMapInhibitCache
:
453 case kIOMapWriteThruCache
:
454 flags
= VM_WIMG_WTHRU
;
457 case kIOWriteCombineCache
:
458 flags
= VM_WIMG_WCOMB
;
461 case kIOMapCopybackCache
:
462 flags
= VM_WIMG_COPYBACK
;
467 // Set up a block mapped area
468 pmap_map_block(pmap
, (addr64_t
)va
, (ppnum_t
)(pa
>> 12), (uint32_t)(length
>> 12), prot
, flags
, 0);
471 // enter each page's physical address in the target map
473 for (off
= 0; off
< length
; off
+= page_size
)
474 pmap_enter(pmap
, va
+ off
, (pa
+ off
) >> 12, prot
, flags
, TRUE
);
478 return( KERN_SUCCESS
);
481 kern_return_t
IOUnmapPages(vm_map_t map
, vm_offset_t va
, vm_size_t length
)
483 pmap_t pmap
= map
->pmap
;
485 pmap_remove(pmap
, trunc_page_64(va
), round_page_64(va
+ length
));
487 return( KERN_SUCCESS
);
490 void IOGetTime( mach_timespec_t
* clock_time
);
491 void IOGetTime( mach_timespec_t
* clock_time
)
493 clock_get_system_nanotime(&clock_time
->tv_sec
, &clock_time
->tv_nsec
);