2 * Copyright (c) 2000 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>
43 #include <kern/counters.h>
44 #include <kern/queue.h>
45 #include <kern/zalloc.h>
46 #include <kern/thread.h>
47 #include <kern/thread_swap.h>
48 #include <kern/task.h>
49 #include <kern/sched_prim.h>
50 #include <kern/misc_protos.h>
53 #include <vm/vm_map.h>
54 #include <vm/vm_kern.h>
56 #include <device/device_types.h>
57 #include <device/device_port.h>
58 #include <device/device_server.h>
60 #include <machine/machparam.h>
63 #include <ppc/mappings.h>
64 #include <ppc/pmap_internals.h>
66 #include <IOKit/IOTypes.h>
72 * Functions in iokit:IOUserClient.cpp
75 extern void iokit_add_reference( io_object_t obj
);
77 extern void iokit_remove_reference( io_object_t obj
);
79 extern ipc_port_t
iokit_port_for_object( io_object_t obj
,
80 ipc_kobject_type_t type
);
82 extern kern_return_t
iokit_client_died( io_object_t obj
,
83 ipc_port_t port
, ipc_kobject_type_t type
, mach_port_mscount_t mscount
);
86 iokit_client_memory_for_type(
90 vm_address_t
* address
,
94 * Lookup a device by its port.
95 * Doesn't consume the naked send right; produces a device reference.
98 iokit_lookup_object_port(
101 register io_object_t obj
;
107 if (ip_active(port
) && (ip_kotype(port
) == IKOT_IOKIT_OBJECT
)) {
108 obj
= (io_object_t
) port
->ip_kobject
;
109 iokit_add_reference( obj
);
119 MIGEXTERN io_object_t
120 iokit_lookup_connect_port(
123 register io_object_t obj
;
129 if (ip_active(port
) && (ip_kotype(port
) == IKOT_IOKIT_CONNECT
)) {
130 obj
= (io_object_t
) port
->ip_kobject
;
131 iokit_add_reference( obj
);
142 iokit_lookup_connect_ref(io_object_t connectRef
, ipc_space_t space
)
144 io_object_t obj
= NULL
;
146 if (connectRef
&& MACH_PORT_VALID((mach_port_name_t
)connectRef
)) {
150 kr
= ipc_object_translate(space
, (mach_port_name_t
)connectRef
, MACH_PORT_RIGHT_SEND
, (ipc_object_t
*)&port
);
152 if (kr
== KERN_SUCCESS
) {
153 assert(IP_VALID(port
));
155 if (ip_active(port
) && (ip_kotype(port
) == IKOT_IOKIT_CONNECT
)) {
156 obj
= (io_object_t
) port
->ip_kobject
;
157 iokit_add_reference(obj
);
168 iokit_lookup_connect_ref_current_task(io_object_t connectRef
)
170 return iokit_lookup_connect_ref(connectRef
, current_space());
174 iokit_retain_port( ipc_port_t port
)
176 ipc_port_reference( port
);
180 iokit_release_port( ipc_port_t port
)
182 ipc_port_release( port
);
186 * Get the port for a device.
187 * Consumes a device reference; produces a naked send right.
190 iokit_make_object_port(
193 register ipc_port_t port
;
194 register ipc_port_t sendPort
;
199 port
= iokit_port_for_object( obj
, IKOT_IOKIT_OBJECT
);
201 sendPort
= ipc_port_make_send( port
);
202 iokit_release_port( port
);
206 iokit_remove_reference( obj
);
212 iokit_make_connect_port(
215 register ipc_port_t port
;
216 register ipc_port_t sendPort
;
221 port
= iokit_port_for_object( obj
, IKOT_IOKIT_CONNECT
);
223 sendPort
= ipc_port_make_send( port
);
224 iokit_release_port( port
);
228 iokit_remove_reference( obj
);
235 iokit_alloc_object_port( io_object_t obj
, ipc_kobject_type_t type
);
240 iokit_alloc_object_port( io_object_t obj
, ipc_kobject_type_t type
)
247 /* Allocate port, keeping a reference for it. */
248 port
= ipc_port_alloc_kernel();
252 /* set kobject & type */
253 // iokit_add_reference( obj );
254 ipc_kobject_set( port
, (ipc_kobject_t
) obj
, type
);
256 /* Request no-senders notifications on the port. */
257 notify
= ipc_port_make_sonce( port
);
259 ipc_port_nsrequest( port
, 1, notify
, ¬ify
);
260 assert( notify
== IP_NULL
);
270 iokit_destroy_object_port( ipc_port_t port
)
272 ipc_kobject_set( port
, IKO_NULL
, IKOT_NONE
);
274 // iokit_remove_reference( obj );
276 ipc_port_dealloc_kernel( port
);
279 return( KERN_SUCCESS
);
282 EXTERN mach_port_name_t
283 iokit_make_send_right( task_t task
, io_object_t obj
, ipc_kobject_type_t type
)
287 mach_port_name_t name
;
290 return MACH_PORT_NULL
;
292 port
= iokit_port_for_object( obj
, type
);
294 sendPort
= ipc_port_make_send( port
);
295 iokit_release_port( port
);
299 if (IP_VALID( sendPort
)) {
301 kr
= ipc_object_copyout( task
->itk_space
, (ipc_object_t
) sendPort
,
302 MACH_MSG_TYPE_PORT_SEND
, TRUE
, &name
);
303 if ( kr
!= KERN_SUCCESS
)
304 name
= MACH_PORT_NULL
;
305 } else if ( sendPort
== IP_NULL
)
306 name
= MACH_PORT_NULL
;
307 else if ( sendPort
== IP_DEAD
)
308 name
= MACH_PORT_DEAD
;
310 iokit_remove_reference( obj
);
316 * Handle the No-More_Senders notification generated from a device port destroy.
317 * Since there are no longer any tasks which hold a send right to this device
318 * port a NMS notification has been generated.
322 iokit_no_senders( mach_no_senders_notification_t
* notification
)
325 io_object_t obj
= NULL
;
326 ipc_kobject_type_t type
;
329 port
= (ipc_port_t
) notification
->not_header
.msgh_remote_port
;
331 // convert a port to io_object_t.
332 if( IP_VALID(port
)) {
334 if( ip_active(port
)) {
335 obj
= (io_object_t
) port
->ip_kobject
;
336 type
= ip_kotype( port
);
337 if( (IKOT_IOKIT_OBJECT
== type
)
338 || (IKOT_IOKIT_CONNECT
== type
))
339 iokit_add_reference( obj
);
347 mach_port_mscount_t mscount
= notification
->not_count
;
349 if( KERN_SUCCESS
!= iokit_client_died( obj
, port
, type
, &mscount
))
351 /* Re-request no-senders notifications on the port. */
352 notify
= ipc_port_make_sonce( port
);
354 ipc_port_nsrequest( port
, mscount
+ 1, notify
, ¬ify
);
355 assert( notify
== IP_NULL
);
357 iokit_remove_reference( obj
);
365 iokit_notify( mach_msg_header_t
* msg
)
367 switch (msg
->msgh_id
) {
368 case MACH_NOTIFY_NO_SENDERS
:
369 iokit_no_senders((mach_no_senders_notification_t
*) msg
);
372 case MACH_NOTIFY_PORT_DELETED
:
373 case MACH_NOTIFY_PORT_DESTROYED
:
374 case MACH_NOTIFY_SEND_ONCE
:
375 case MACH_NOTIFY_DEAD_NAME
:
377 printf("iokit_notify: strange notification %ld\n", msg
->msgh_id
);
383 unsigned int IOTranslateCacheBits(struct phys_entry
*pp
)
386 unsigned int memattr
;
388 /* need to create a pmap function to generalize */
389 memattr
= ((pp
->pte1
& 0x00000078) >> 3);
391 /* NOTE: DEVICE_PAGER_FLAGS are made to line up */
392 flags
= memattr
& VM_WIMG_MASK
;
397 kern_return_t
IOMapPages(vm_map_t map
, vm_offset_t va
, vm_offset_t pa
,
398 vm_size_t length
, unsigned int options
)
403 struct phys_entry
*pp
;
404 pmap_t pmap
= map
->pmap
;
406 prot
= (options
& kIOMapReadOnly
)
407 ? VM_PROT_READ
: (VM_PROT_READ
|VM_PROT_WRITE
);
411 switch(options
& kIOMapCacheMask
) { /* What cache mode do we need? */
413 case kIOMapDefaultCache
:
415 if(pp
= pmap_find_physentry(pa
)) { /* Find physical address */
416 memattr
= ((pp
->pte1
& 0x00000078) >> 3); /* Use physical attributes as default */
418 else { /* If no physical, just hard code attributes */
419 memattr
= PTE_WIMG_UNCACHED_COHERENT_GUARDED
;
423 case kIOMapInhibitCache
:
424 memattr
= PTE_WIMG_UNCACHED_COHERENT_GUARDED
;
427 case kIOMapWriteThruCache
:
428 memattr
= PTE_WIMG_WT_CACHED_COHERENT_GUARDED
;
431 case kIOMapCopybackCache
:
432 memattr
= PTE_WIMG_CB_CACHED_COHERENT
;
436 pmap_map_block(pmap
, va
, pa
, length
, prot
, memattr
, 0); /* Set up a block mapped area */
439 // enter each page's physical address in the target map
440 for (off
= 0; off
< length
; off
+= page_size
) { /* Loop for the whole length */
441 pmap_enter(pmap
, va
+ off
, pa
+ off
, prot
, VM_WIMG_USE_DEFAULT
, TRUE
); /* Map it in */
445 return( KERN_SUCCESS
);
448 kern_return_t
IOUnmapPages(vm_map_t map
, vm_offset_t va
, vm_size_t length
)
450 pmap_t pmap
= map
->pmap
;
452 pmap_remove(pmap
, trunc_page(va
), round_page(va
+ length
));
454 return( KERN_SUCCESS
);
457 void IOGetTime( mach_timespec_t
* clock_time
);
458 void IOGetTime( mach_timespec_t
* clock_time
)
460 *clock_time
= clock_get_system_value();