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 extern ppnum_t
IOGetLastPageNumber(void);
95 * Lookup a device by its port.
96 * Doesn't consume the naked send right; produces a device reference.
99 iokit_lookup_object_port(
102 register io_object_t obj
;
108 if (ip_active(port
) && (ip_kotype(port
) == IKOT_IOKIT_OBJECT
)) {
109 obj
= (io_object_t
) port
->ip_kobject
;
110 iokit_add_reference( obj
);
120 MIGEXTERN io_object_t
121 iokit_lookup_connect_port(
124 register io_object_t obj
;
130 if (ip_active(port
) && (ip_kotype(port
) == IKOT_IOKIT_CONNECT
)) {
131 obj
= (io_object_t
) port
->ip_kobject
;
132 iokit_add_reference( obj
);
143 iokit_lookup_connect_ref(io_object_t connectRef
, ipc_space_t space
)
145 io_object_t obj
= NULL
;
147 if (connectRef
&& MACH_PORT_VALID((mach_port_name_t
)connectRef
)) {
151 kr
= ipc_object_translate(space
, (mach_port_name_t
)connectRef
, MACH_PORT_RIGHT_SEND
, (ipc_object_t
*)&port
);
153 if (kr
== KERN_SUCCESS
) {
154 assert(IP_VALID(port
));
156 if (ip_active(port
) && (ip_kotype(port
) == IKOT_IOKIT_CONNECT
)) {
157 obj
= (io_object_t
) port
->ip_kobject
;
158 iokit_add_reference(obj
);
169 iokit_lookup_connect_ref_current_task(io_object_t connectRef
)
171 return iokit_lookup_connect_ref(connectRef
, current_space());
175 iokit_retain_port( ipc_port_t port
)
177 ipc_port_reference( port
);
181 iokit_release_port( ipc_port_t port
)
183 ipc_port_release( port
);
187 * Get the port for a device.
188 * Consumes a device reference; produces a naked send right.
191 iokit_make_object_port(
194 register ipc_port_t port
;
195 register ipc_port_t sendPort
;
200 port
= iokit_port_for_object( obj
, IKOT_IOKIT_OBJECT
);
202 sendPort
= ipc_port_make_send( port
);
203 iokit_release_port( port
);
207 iokit_remove_reference( obj
);
213 iokit_make_connect_port(
216 register ipc_port_t port
;
217 register ipc_port_t sendPort
;
222 port
= iokit_port_for_object( obj
, IKOT_IOKIT_CONNECT
);
224 sendPort
= ipc_port_make_send( port
);
225 iokit_release_port( port
);
229 iokit_remove_reference( obj
);
236 iokit_alloc_object_port( io_object_t obj
, ipc_kobject_type_t type
);
241 iokit_alloc_object_port( io_object_t obj
, ipc_kobject_type_t type
)
248 /* Allocate port, keeping a reference for it. */
249 port
= ipc_port_alloc_kernel();
253 /* set kobject & type */
254 // iokit_add_reference( obj );
255 ipc_kobject_set( port
, (ipc_kobject_t
) obj
, type
);
257 /* Request no-senders notifications on the port. */
258 notify
= ipc_port_make_sonce( port
);
260 ipc_port_nsrequest( port
, 1, notify
, ¬ify
);
261 assert( notify
== IP_NULL
);
271 iokit_destroy_object_port( ipc_port_t port
)
273 ipc_kobject_set( port
, IKO_NULL
, IKOT_NONE
);
275 // iokit_remove_reference( obj );
277 ipc_port_dealloc_kernel( port
);
280 return( KERN_SUCCESS
);
284 iokit_switch_object_port( ipc_port_t port
, io_object_t obj
, ipc_kobject_type_t type
)
286 ipc_kobject_set( port
, (ipc_kobject_t
) obj
, type
);
288 return( KERN_SUCCESS
);
291 EXTERN mach_port_name_t
292 iokit_make_send_right( task_t task
, io_object_t obj
, ipc_kobject_type_t type
)
296 mach_port_name_t name
;
299 return MACH_PORT_NULL
;
301 port
= iokit_port_for_object( obj
, type
);
303 sendPort
= ipc_port_make_send( port
);
304 iokit_release_port( port
);
308 if (IP_VALID( sendPort
)) {
310 kr
= ipc_object_copyout( task
->itk_space
, (ipc_object_t
) sendPort
,
311 MACH_MSG_TYPE_PORT_SEND
, TRUE
, &name
);
312 if ( kr
!= KERN_SUCCESS
)
313 name
= MACH_PORT_NULL
;
314 } else if ( sendPort
== IP_NULL
)
315 name
= MACH_PORT_NULL
;
316 else if ( sendPort
== IP_DEAD
)
317 name
= MACH_PORT_DEAD
;
319 iokit_remove_reference( obj
);
325 iokit_mod_send_right( task_t task
, mach_port_name_t name
, mach_port_delta_t delta
)
327 return (mach_port_mod_refs( task
->itk_space
, name
, MACH_PORT_RIGHT_SEND
, delta
));
331 * Handle the No-More_Senders notification generated from a device port destroy.
332 * Since there are no longer any tasks which hold a send right to this device
333 * port a NMS notification has been generated.
337 iokit_no_senders( mach_no_senders_notification_t
* notification
)
340 io_object_t obj
= NULL
;
341 ipc_kobject_type_t type
;
344 port
= (ipc_port_t
) notification
->not_header
.msgh_remote_port
;
346 // convert a port to io_object_t.
347 if( IP_VALID(port
)) {
349 if( ip_active(port
)) {
350 obj
= (io_object_t
) port
->ip_kobject
;
351 type
= ip_kotype( port
);
352 if( (IKOT_IOKIT_OBJECT
== type
)
353 || (IKOT_IOKIT_CONNECT
== type
))
354 iokit_add_reference( obj
);
362 mach_port_mscount_t mscount
= notification
->not_count
;
364 if( KERN_SUCCESS
!= iokit_client_died( obj
, port
, type
, &mscount
))
366 /* Re-request no-senders notifications on the port. */
367 notify
= ipc_port_make_sonce( port
);
369 ipc_port_nsrequest( port
, mscount
+ 1, notify
, ¬ify
);
370 assert( notify
== IP_NULL
);
372 iokit_remove_reference( obj
);
380 iokit_notify( mach_msg_header_t
* msg
)
382 switch (msg
->msgh_id
) {
383 case MACH_NOTIFY_NO_SENDERS
:
384 iokit_no_senders((mach_no_senders_notification_t
*) msg
);
387 case MACH_NOTIFY_PORT_DELETED
:
388 case MACH_NOTIFY_PORT_DESTROYED
:
389 case MACH_NOTIFY_SEND_ONCE
:
390 case MACH_NOTIFY_DEAD_NAME
:
392 printf("iokit_notify: strange notification %ld\n", msg
->msgh_id
);
397 /* need to create a pmap function to generalize */
398 unsigned int IODefaultCacheBits(addr64_t pa
)
401 return(pmap_cache_attributes(pa
>> PAGE_SHIFT
));
404 kern_return_t
IOMapPages(vm_map_t map
, mach_vm_address_t va
, mach_vm_address_t pa
,
405 mach_vm_size_t length
, unsigned int options
)
409 pmap_t pmap
= map
->pmap
;
411 prot
= (options
& kIOMapReadOnly
)
412 ? VM_PROT_READ
: (VM_PROT_READ
|VM_PROT_WRITE
);
414 switch(options
& kIOMapCacheMask
) { /* What cache mode do we need? */
416 case kIOMapDefaultCache
:
418 flags
= IODefaultCacheBits(pa
);
421 case kIOMapInhibitCache
:
425 case kIOMapWriteThruCache
:
426 flags
= VM_WIMG_WTHRU
;
429 case kIOMapWriteCombineCache
:
430 flags
= VM_WIMG_WCOMB
;
433 case kIOMapCopybackCache
:
434 flags
= VM_WIMG_COPYBACK
;
438 // Set up a block mapped area
439 pmap_map_block(pmap
, va
, (ppnum_t
)atop_64(pa
), (uint32_t) atop_64(round_page_64(length
)), prot
, flags
, 0);
441 return( KERN_SUCCESS
);
444 kern_return_t
IOUnmapPages(vm_map_t map
, mach_vm_address_t va
, mach_vm_size_t length
)
446 pmap_t pmap
= map
->pmap
;
448 pmap_remove(pmap
, trunc_page_64(va
), round_page_64(va
+ length
));
450 return( KERN_SUCCESS
);
453 kern_return_t
IOProtectCacheMode(vm_map_t map
, mach_vm_address_t va
,
454 mach_vm_size_t length
, unsigned int options
)
459 pmap_t pmap
= map
->pmap
;
461 prot
= (options
& kIOMapReadOnly
)
462 ? VM_PROT_READ
: (VM_PROT_READ
|VM_PROT_WRITE
);
464 switch (options
& kIOMapCacheMask
)
466 /* What cache mode do we need? */
467 case kIOMapDefaultCache
:
469 return (KERN_INVALID_ARGUMENT
);
471 case kIOMapInhibitCache
:
475 case kIOMapWriteThruCache
:
476 flags
= VM_WIMG_WTHRU
;
479 case kIOMapWriteCombineCache
:
480 flags
= VM_WIMG_WCOMB
;
483 case kIOMapCopybackCache
:
484 flags
= VM_WIMG_COPYBACK
;
488 // can't remap block mappings, but ppc doesn't speculative read from WC
491 // enter each page's physical address in the target map
492 for (off
= 0; off
< length
; off
+= page_size
)
494 ppnum_t ppnum
= pmap_find_phys(pmap
, va
+ off
);
496 pmap_enter(pmap
, va
+ off
, ppnum
, prot
, flags
, TRUE
);
501 return (KERN_SUCCESS
);
504 ppnum_t
IOGetLastPageNumber(void)
506 ppnum_t lastPage
, highest
= 0;
510 for (idx
= 0; idx
< pmap_mem_regions_count
; idx
++)
512 lastPage
= pmap_mem_regions
[idx
].mrEnd
;
515 for (idx
= 0; idx
< pmap_memory_region_count
; idx
++)
517 lastPage
= pmap_memory_regions
[idx
].end
- 1;
521 if (lastPage
> highest
)
528 void IOGetTime( mach_timespec_t
* clock_time
);
529 void IOGetTime( mach_timespec_t
* clock_time
)
531 clock_get_system_nanotime(&clock_time
->tv_sec
, &clock_time
->tv_nsec
);