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>
67 #include <ppc/pmap_internals.h>
69 #include <IOKit/IOTypes.h>
75 * Functions in iokit:IOUserClient.cpp
78 extern void iokit_add_reference( io_object_t obj
);
80 extern void iokit_remove_reference( io_object_t obj
);
82 extern ipc_port_t
iokit_port_for_object( io_object_t obj
,
83 ipc_kobject_type_t type
);
85 extern kern_return_t
iokit_client_died( io_object_t obj
,
86 ipc_port_t port
, ipc_kobject_type_t type
, mach_port_mscount_t mscount
);
89 iokit_client_memory_for_type(
93 vm_address_t
* address
,
97 * Lookup a device by its port.
98 * Doesn't consume the naked send right; produces a device reference.
100 MIGEXTERN io_object_t
101 iokit_lookup_object_port(
104 register io_object_t obj
;
110 if (ip_active(port
) && (ip_kotype(port
) == IKOT_IOKIT_OBJECT
)) {
111 obj
= (io_object_t
) port
->ip_kobject
;
112 iokit_add_reference( obj
);
122 MIGEXTERN io_object_t
123 iokit_lookup_connect_port(
126 register io_object_t obj
;
132 if (ip_active(port
) && (ip_kotype(port
) == IKOT_IOKIT_CONNECT
)) {
133 obj
= (io_object_t
) port
->ip_kobject
;
134 iokit_add_reference( obj
);
145 iokit_lookup_connect_ref(io_object_t connectRef
, ipc_space_t space
)
147 io_object_t obj
= NULL
;
149 if (connectRef
&& MACH_PORT_VALID((mach_port_name_t
)connectRef
)) {
153 kr
= ipc_object_translate(space
, (mach_port_name_t
)connectRef
, MACH_PORT_RIGHT_SEND
, (ipc_object_t
*)&port
);
155 if (kr
== KERN_SUCCESS
) {
156 assert(IP_VALID(port
));
158 if (ip_active(port
) && (ip_kotype(port
) == IKOT_IOKIT_CONNECT
)) {
159 obj
= (io_object_t
) port
->ip_kobject
;
160 iokit_add_reference(obj
);
171 iokit_lookup_connect_ref_current_task(io_object_t connectRef
)
173 return iokit_lookup_connect_ref(connectRef
, current_space());
177 iokit_retain_port( ipc_port_t port
)
179 ipc_port_reference( port
);
183 iokit_release_port( ipc_port_t port
)
185 ipc_port_release( port
);
189 * Get the port for a device.
190 * Consumes a device reference; produces a naked send right.
193 iokit_make_object_port(
196 register ipc_port_t port
;
197 register ipc_port_t sendPort
;
202 port
= iokit_port_for_object( obj
, IKOT_IOKIT_OBJECT
);
204 sendPort
= ipc_port_make_send( port
);
205 iokit_release_port( port
);
209 iokit_remove_reference( obj
);
215 iokit_make_connect_port(
218 register ipc_port_t port
;
219 register ipc_port_t sendPort
;
224 port
= iokit_port_for_object( obj
, IKOT_IOKIT_CONNECT
);
226 sendPort
= ipc_port_make_send( port
);
227 iokit_release_port( port
);
231 iokit_remove_reference( obj
);
238 iokit_alloc_object_port( io_object_t obj
, ipc_kobject_type_t type
);
243 iokit_alloc_object_port( io_object_t obj
, ipc_kobject_type_t type
)
250 /* Allocate port, keeping a reference for it. */
251 port
= ipc_port_alloc_kernel();
255 /* set kobject & type */
256 // iokit_add_reference( obj );
257 ipc_kobject_set( port
, (ipc_kobject_t
) obj
, type
);
259 /* Request no-senders notifications on the port. */
260 notify
= ipc_port_make_sonce( port
);
262 ipc_port_nsrequest( port
, 1, notify
, ¬ify
);
263 assert( notify
== IP_NULL
);
273 iokit_destroy_object_port( ipc_port_t port
)
275 ipc_kobject_set( port
, IKO_NULL
, IKOT_NONE
);
277 // iokit_remove_reference( obj );
279 ipc_port_dealloc_kernel( port
);
282 return( KERN_SUCCESS
);
286 iokit_switch_object_port( ipc_port_t port
, io_object_t obj
, ipc_kobject_type_t type
)
288 ipc_kobject_set( port
, (ipc_kobject_t
) obj
, type
);
290 return( KERN_SUCCESS
);
293 EXTERN mach_port_name_t
294 iokit_make_send_right( task_t task
, io_object_t obj
, ipc_kobject_type_t type
)
298 mach_port_name_t name
;
301 return MACH_PORT_NULL
;
303 port
= iokit_port_for_object( obj
, type
);
305 sendPort
= ipc_port_make_send( port
);
306 iokit_release_port( port
);
310 if (IP_VALID( sendPort
)) {
312 kr
= ipc_object_copyout( task
->itk_space
, (ipc_object_t
) sendPort
,
313 MACH_MSG_TYPE_PORT_SEND
, TRUE
, &name
);
314 if ( kr
!= KERN_SUCCESS
)
315 name
= MACH_PORT_NULL
;
316 } else if ( sendPort
== IP_NULL
)
317 name
= MACH_PORT_NULL
;
318 else if ( sendPort
== IP_DEAD
)
319 name
= MACH_PORT_DEAD
;
321 iokit_remove_reference( obj
);
327 * Handle the No-More_Senders notification generated from a device port destroy.
328 * Since there are no longer any tasks which hold a send right to this device
329 * port a NMS notification has been generated.
333 iokit_no_senders( mach_no_senders_notification_t
* notification
)
336 io_object_t obj
= NULL
;
337 ipc_kobject_type_t type
;
340 port
= (ipc_port_t
) notification
->not_header
.msgh_remote_port
;
342 // convert a port to io_object_t.
343 if( IP_VALID(port
)) {
345 if( ip_active(port
)) {
346 obj
= (io_object_t
) port
->ip_kobject
;
347 type
= ip_kotype( port
);
348 if( (IKOT_IOKIT_OBJECT
== type
)
349 || (IKOT_IOKIT_CONNECT
== type
))
350 iokit_add_reference( obj
);
358 mach_port_mscount_t mscount
= notification
->not_count
;
360 if( KERN_SUCCESS
!= iokit_client_died( obj
, port
, type
, &mscount
))
362 /* Re-request no-senders notifications on the port. */
363 notify
= ipc_port_make_sonce( port
);
365 ipc_port_nsrequest( port
, mscount
+ 1, notify
, ¬ify
);
366 assert( notify
== IP_NULL
);
368 iokit_remove_reference( obj
);
376 iokit_notify( mach_msg_header_t
* msg
)
378 switch (msg
->msgh_id
) {
379 case MACH_NOTIFY_NO_SENDERS
:
380 iokit_no_senders((mach_no_senders_notification_t
*) msg
);
383 case MACH_NOTIFY_PORT_DELETED
:
384 case MACH_NOTIFY_PORT_DESTROYED
:
385 case MACH_NOTIFY_SEND_ONCE
:
386 case MACH_NOTIFY_DEAD_NAME
:
388 printf("iokit_notify: strange notification %ld\n", msg
->msgh_id
);
394 unsigned int IOTranslateCacheBits(struct phys_entry
*pp
)
397 unsigned int memattr
;
399 /* need to create a pmap function to generalize */
400 memattr
= ((pp
->pte1
& 0x00000078) >> 3);
402 /* NOTE: DEVICE_PAGER_FLAGS are made to line up */
403 flags
= memattr
& VM_WIMG_MASK
;
408 kern_return_t
IOMapPages(vm_map_t map
, vm_offset_t va
, vm_offset_t pa
,
409 vm_size_t length
, unsigned int options
)
414 struct phys_entry
*pp
;
415 pmap_t pmap
= map
->pmap
;
417 prot
= (options
& kIOMapReadOnly
)
418 ? VM_PROT_READ
: (VM_PROT_READ
|VM_PROT_WRITE
);
422 switch(options
& kIOMapCacheMask
) { /* What cache mode do we need? */
424 case kIOMapDefaultCache
:
426 if(pp
= pmap_find_physentry(pa
)) { /* Find physical address */
427 memattr
= ((pp
->pte1
& 0x00000078) >> 3); /* Use physical attributes as default */
429 else { /* If no physical, just hard code attributes */
430 memattr
= PTE_WIMG_UNCACHED_COHERENT_GUARDED
;
434 case kIOMapInhibitCache
:
435 memattr
= PTE_WIMG_UNCACHED_COHERENT_GUARDED
;
438 case kIOMapWriteThruCache
:
439 memattr
= PTE_WIMG_WT_CACHED_COHERENT_GUARDED
;
442 case kIOMapCopybackCache
:
443 memattr
= PTE_WIMG_CB_CACHED_COHERENT
;
447 pmap_map_block(pmap
, va
, pa
, length
, prot
, memattr
, 0); /* Set up a block mapped area */
450 // enter each page's physical address in the target map
451 for (off
= 0; off
< length
; off
+= page_size
) { /* Loop for the whole length */
452 pmap_enter(pmap
, va
+ off
, pa
+ off
, prot
, VM_WIMG_USE_DEFAULT
, TRUE
); /* Map it in */
456 return( KERN_SUCCESS
);
459 kern_return_t
IOUnmapPages(vm_map_t map
, vm_offset_t va
, vm_size_t length
)
461 pmap_t pmap
= map
->pmap
;
463 pmap_remove(pmap
, trunc_page(va
), round_page(va
+ length
));
465 return( KERN_SUCCESS
);
468 void IOGetTime( mach_timespec_t
* clock_time
);
469 void IOGetTime( mach_timespec_t
* clock_time
)
471 *clock_time
= clock_get_system_value();