2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 #include <zone_debug.h>
26 #include <mach/boolean.h>
27 #include <mach/kern_return.h>
28 #include <mach/mig_errors.h>
29 #include <mach/port.h>
30 #include <mach/vm_param.h>
31 #include <mach/notify.h>
32 #include <mach/mach_host_server.h>
33 #include <mach/mach_types.h>
35 #include <machine/machparam.h> /* spl definitions */
37 #include <ipc/ipc_port.h>
38 #include <ipc/ipc_space.h>
40 #include <kern/clock.h>
42 #include <kern/counters.h>
43 #include <kern/queue.h>
44 #include <kern/zalloc.h>
45 #include <kern/thread.h>
46 #include <kern/task.h>
47 #include <kern/sched_prim.h>
48 #include <kern/misc_protos.h>
51 #include <vm/vm_map.h>
52 #include <vm/vm_kern.h>
54 #include <device/device_types.h>
55 #include <device/device_port.h>
56 #include <device/device_server.h>
58 #include <machine/machparam.h>
61 #include <ppc/mappings.h>
64 #include <i386/pmap.h>
66 #include <IOKit/IOTypes.h>
72 * Functions in iokit:IOUserClient.cpp
75 extern void iokit_add_reference( io_object_t obj
);
77 extern ipc_port_t
iokit_port_for_object( io_object_t obj
,
78 ipc_kobject_type_t type
);
80 extern kern_return_t
iokit_client_died( io_object_t obj
,
81 ipc_port_t port
, ipc_kobject_type_t type
, mach_port_mscount_t
* mscount
);
84 iokit_client_memory_for_type(
88 vm_address_t
* address
,
92 * Lookup a device by its port.
93 * Doesn't consume the naked send right; produces a device reference.
96 iokit_lookup_object_port(
99 register io_object_t obj
;
105 if (ip_active(port
) && (ip_kotype(port
) == IKOT_IOKIT_OBJECT
)) {
106 obj
= (io_object_t
) port
->ip_kobject
;
107 iokit_add_reference( obj
);
117 MIGEXTERN io_object_t
118 iokit_lookup_connect_port(
121 register io_object_t obj
;
127 if (ip_active(port
) && (ip_kotype(port
) == IKOT_IOKIT_CONNECT
)) {
128 obj
= (io_object_t
) port
->ip_kobject
;
129 iokit_add_reference( obj
);
140 iokit_lookup_connect_ref(io_object_t connectRef
, ipc_space_t space
)
142 io_object_t obj
= NULL
;
144 if (connectRef
&& MACH_PORT_VALID((mach_port_name_t
)connectRef
)) {
148 kr
= ipc_object_translate(space
, (mach_port_name_t
)connectRef
, MACH_PORT_RIGHT_SEND
, (ipc_object_t
*)&port
);
150 if (kr
== KERN_SUCCESS
) {
151 assert(IP_VALID(port
));
153 if (ip_active(port
) && (ip_kotype(port
) == IKOT_IOKIT_CONNECT
)) {
154 obj
= (io_object_t
) port
->ip_kobject
;
155 iokit_add_reference(obj
);
166 iokit_lookup_connect_ref_current_task(io_object_t connectRef
)
168 return iokit_lookup_connect_ref(connectRef
, current_space());
172 iokit_retain_port( ipc_port_t port
)
174 ipc_port_reference( port
);
178 iokit_release_port( ipc_port_t port
)
180 ipc_port_release( port
);
184 * Get the port for a device.
185 * Consumes a device reference; produces a naked send right.
188 iokit_make_object_port(
191 register ipc_port_t port
;
192 register ipc_port_t sendPort
;
197 port
= iokit_port_for_object( obj
, IKOT_IOKIT_OBJECT
);
199 sendPort
= ipc_port_make_send( port
);
200 iokit_release_port( port
);
204 iokit_remove_reference( obj
);
210 iokit_make_connect_port(
213 register ipc_port_t port
;
214 register ipc_port_t sendPort
;
219 port
= iokit_port_for_object( obj
, IKOT_IOKIT_CONNECT
);
221 sendPort
= ipc_port_make_send( port
);
222 iokit_release_port( port
);
226 iokit_remove_reference( obj
);
233 iokit_alloc_object_port( io_object_t obj
, ipc_kobject_type_t type
);
238 iokit_alloc_object_port( io_object_t obj
, ipc_kobject_type_t type
)
245 /* Allocate port, keeping a reference for it. */
246 port
= ipc_port_alloc_kernel();
250 /* set kobject & type */
251 // iokit_add_reference( obj );
252 ipc_kobject_set( port
, (ipc_kobject_t
) obj
, type
);
254 /* Request no-senders notifications on the port. */
255 notify
= ipc_port_make_sonce( port
);
257 ipc_port_nsrequest( port
, 1, notify
, ¬ify
);
258 assert( notify
== IP_NULL
);
268 iokit_destroy_object_port( ipc_port_t port
)
270 ipc_kobject_set( port
, IKO_NULL
, IKOT_NONE
);
272 // iokit_remove_reference( obj );
274 ipc_port_dealloc_kernel( port
);
277 return( KERN_SUCCESS
);
281 iokit_switch_object_port( ipc_port_t port
, io_object_t obj
, ipc_kobject_type_t type
)
283 ipc_kobject_set( port
, (ipc_kobject_t
) obj
, type
);
285 return( KERN_SUCCESS
);
288 EXTERN mach_port_name_t
289 iokit_make_send_right( task_t task
, io_object_t obj
, ipc_kobject_type_t type
)
293 mach_port_name_t name
;
296 return MACH_PORT_NULL
;
298 port
= iokit_port_for_object( obj
, type
);
300 sendPort
= ipc_port_make_send( port
);
301 iokit_release_port( port
);
305 if (IP_VALID( sendPort
)) {
307 kr
= ipc_object_copyout( task
->itk_space
, (ipc_object_t
) sendPort
,
308 MACH_MSG_TYPE_PORT_SEND
, TRUE
, &name
);
309 if ( kr
!= KERN_SUCCESS
)
310 name
= MACH_PORT_NULL
;
311 } else if ( sendPort
== IP_NULL
)
312 name
= MACH_PORT_NULL
;
313 else if ( sendPort
== IP_DEAD
)
314 name
= MACH_PORT_DEAD
;
316 iokit_remove_reference( obj
);
322 iokit_mod_send_right( task_t task
, mach_port_name_t name
, mach_port_delta_t delta
)
324 return (mach_port_mod_refs( task
->itk_space
, name
, MACH_PORT_RIGHT_SEND
, delta
));
328 * Handle the No-More_Senders notification generated from a device port destroy.
329 * Since there are no longer any tasks which hold a send right to this device
330 * port a NMS notification has been generated.
334 iokit_no_senders( mach_no_senders_notification_t
* notification
)
337 io_object_t obj
= NULL
;
338 ipc_kobject_type_t type
;
341 port
= (ipc_port_t
) notification
->not_header
.msgh_remote_port
;
343 // convert a port to io_object_t.
344 if( IP_VALID(port
)) {
346 if( ip_active(port
)) {
347 obj
= (io_object_t
) port
->ip_kobject
;
348 type
= ip_kotype( port
);
349 if( (IKOT_IOKIT_OBJECT
== type
)
350 || (IKOT_IOKIT_CONNECT
== type
))
351 iokit_add_reference( obj
);
359 mach_port_mscount_t mscount
= notification
->not_count
;
361 if( KERN_SUCCESS
!= iokit_client_died( obj
, port
, type
, &mscount
))
363 /* Re-request no-senders notifications on the port. */
364 notify
= ipc_port_make_sonce( port
);
366 ipc_port_nsrequest( port
, mscount
+ 1, notify
, ¬ify
);
367 assert( notify
== IP_NULL
);
369 iokit_remove_reference( obj
);
377 iokit_notify( mach_msg_header_t
* msg
)
379 switch (msg
->msgh_id
) {
380 case MACH_NOTIFY_NO_SENDERS
:
381 iokit_no_senders((mach_no_senders_notification_t
*) msg
);
384 case MACH_NOTIFY_PORT_DELETED
:
385 case MACH_NOTIFY_PORT_DESTROYED
:
386 case MACH_NOTIFY_SEND_ONCE
:
387 case MACH_NOTIFY_DEAD_NAME
:
389 printf("iokit_notify: strange notification %ld\n", msg
->msgh_id
);
394 /* need to create a pmap function to generalize */
395 unsigned int IODefaultCacheBits(addr64_t pa
)
399 struct phys_entry
* pp
;
401 // Find physical address
402 if ((pp
= pmap_find_physentry(pa
>> 12))) {
403 // Use physical attributes as default
404 // NOTE: DEVICE_PAGER_FLAGS are made to line up
405 flags
= VM_MEM_COHERENT
; /* We only support coherent memory */
406 if(pp
->ppLink
& ppG
) flags
|= VM_MEM_GUARDED
; /* Add in guarded if it is */
407 if(pp
->ppLink
& ppI
) flags
|= VM_MEM_NOT_CACHEABLE
; /* Add in cache inhibited if so */
409 // If no physical, just hard code attributes
412 extern pmap_paddr_t avail_end
;
415 flags
= VM_WIMG_COPYBACK
;
423 kern_return_t
IOMapPages(vm_map_t map
, vm_offset_t va
, vm_offset_t pa
,
424 vm_size_t length
, unsigned int options
)
429 pmap_t pmap
= map
->pmap
;
431 prot
= (options
& kIOMapReadOnly
)
432 ? VM_PROT_READ
: (VM_PROT_READ
|VM_PROT_WRITE
);
434 switch(options
& kIOMapCacheMask
) { /* What cache mode do we need? */
436 case kIOMapDefaultCache
:
438 flags
= IODefaultCacheBits(pa
);
441 case kIOMapInhibitCache
:
445 case kIOMapWriteThruCache
:
446 flags
= VM_WIMG_WTHRU
;
449 case kIOWriteCombineCache
:
450 flags
= VM_WIMG_WCOMB
;
453 case kIOMapCopybackCache
:
454 flags
= VM_WIMG_COPYBACK
;
459 // Set up a block mapped area
460 pmap_map_block(pmap
, (addr64_t
)va
, (ppnum_t
)(pa
>> 12), (uint32_t)(length
>> 12), prot
, flags
, 0);
463 // enter each page's physical address in the target map
465 for (off
= 0; off
< length
; off
+= page_size
)
466 pmap_enter(pmap
, va
+ off
, (pa
+ off
) >> 12, prot
, flags
, TRUE
);
470 return( KERN_SUCCESS
);
473 kern_return_t
IOUnmapPages(vm_map_t map
, vm_offset_t va
, vm_size_t length
)
475 pmap_t pmap
= map
->pmap
;
477 pmap_remove(pmap
, trunc_page_64(va
), round_page_64(va
+ length
));
479 return( KERN_SUCCESS
);
482 void IOGetTime( mach_timespec_t
* clock_time
);
483 void IOGetTime( mach_timespec_t
* clock_time
)
485 clock_get_system_nanotime(&clock_time
->tv_sec
, &clock_time
->tv_nsec
);