2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_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 License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #include <zone_debug.h>
32 #include <mach/boolean.h>
33 #include <mach/kern_return.h>
34 #include <mach/mig_errors.h>
35 #include <mach/port.h>
36 #include <mach/vm_param.h>
37 #include <mach/notify.h>
38 #include <mach/mach_host_server.h>
39 #include <mach/mach_types.h>
41 #include <machine/machparam.h> /* spl definitions */
43 #include <ipc/ipc_port.h>
44 #include <ipc/ipc_space.h>
46 #include <kern/clock.h>
48 #include <kern/counters.h>
49 #include <kern/queue.h>
50 #include <kern/zalloc.h>
51 #include <kern/thread.h>
52 #include <kern/task.h>
53 #include <kern/sched_prim.h>
54 #include <kern/misc_protos.h>
57 #include <vm/vm_map.h>
58 #include <vm/vm_kern.h>
60 #include <device/device_types.h>
61 #include <device/device_port.h>
62 #include <device/device_server.h>
64 #include <machine/machparam.h>
67 #include <ppc/mappings.h>
70 #include <i386/pmap.h>
72 #include <IOKit/IOTypes.h>
78 * Functions in iokit:IOUserClient.cpp
81 extern void iokit_add_reference( io_object_t obj
);
83 extern ipc_port_t
iokit_port_for_object( io_object_t obj
,
84 ipc_kobject_type_t type
);
86 extern kern_return_t
iokit_client_died( io_object_t obj
,
87 ipc_port_t port
, ipc_kobject_type_t type
, mach_port_mscount_t
* mscount
);
90 iokit_client_memory_for_type(
94 vm_address_t
* address
,
98 extern ppnum_t
IOGetLastPageNumber(void);
101 * Lookup a device by its port.
102 * Doesn't consume the naked send right; produces a device reference.
104 MIGEXTERN io_object_t
105 iokit_lookup_object_port(
108 register io_object_t obj
;
114 if (ip_active(port
) && (ip_kotype(port
) == IKOT_IOKIT_OBJECT
)) {
115 obj
= (io_object_t
) port
->ip_kobject
;
116 iokit_add_reference( obj
);
126 MIGEXTERN io_object_t
127 iokit_lookup_connect_port(
130 register io_object_t obj
;
136 if (ip_active(port
) && (ip_kotype(port
) == IKOT_IOKIT_CONNECT
)) {
137 obj
= (io_object_t
) port
->ip_kobject
;
138 iokit_add_reference( obj
);
149 iokit_lookup_connect_ref(io_object_t connectRef
, ipc_space_t space
)
151 io_object_t obj
= NULL
;
153 if (connectRef
&& MACH_PORT_VALID((mach_port_name_t
)connectRef
)) {
157 kr
= ipc_object_translate(space
, (mach_port_name_t
)connectRef
, MACH_PORT_RIGHT_SEND
, (ipc_object_t
*)&port
);
159 if (kr
== KERN_SUCCESS
) {
160 assert(IP_VALID(port
));
162 if (ip_active(port
) && (ip_kotype(port
) == IKOT_IOKIT_CONNECT
)) {
163 obj
= (io_object_t
) port
->ip_kobject
;
164 iokit_add_reference(obj
);
175 iokit_lookup_connect_ref_current_task(io_object_t connectRef
)
177 return iokit_lookup_connect_ref(connectRef
, current_space());
181 iokit_retain_port( ipc_port_t port
)
183 ipc_port_reference( port
);
187 iokit_release_port( ipc_port_t port
)
189 ipc_port_release( port
);
193 * Get the port for a device.
194 * Consumes a device reference; produces a naked send right.
197 iokit_make_object_port(
200 register ipc_port_t port
;
201 register ipc_port_t sendPort
;
206 port
= iokit_port_for_object( obj
, IKOT_IOKIT_OBJECT
);
208 sendPort
= ipc_port_make_send( port
);
209 iokit_release_port( port
);
213 iokit_remove_reference( obj
);
219 iokit_make_connect_port(
222 register ipc_port_t port
;
223 register ipc_port_t sendPort
;
228 port
= iokit_port_for_object( obj
, IKOT_IOKIT_CONNECT
);
230 sendPort
= ipc_port_make_send( port
);
231 iokit_release_port( port
);
235 iokit_remove_reference( obj
);
242 iokit_alloc_object_port( io_object_t obj
, ipc_kobject_type_t type
);
247 iokit_alloc_object_port( io_object_t obj
, ipc_kobject_type_t type
)
254 /* Allocate port, keeping a reference for it. */
255 port
= ipc_port_alloc_kernel();
259 /* set kobject & type */
260 // iokit_add_reference( obj );
261 ipc_kobject_set( port
, (ipc_kobject_t
) obj
, type
);
263 /* Request no-senders notifications on the port. */
264 notify
= ipc_port_make_sonce( port
);
266 ipc_port_nsrequest( port
, 1, notify
, ¬ify
);
267 assert( notify
== IP_NULL
);
277 iokit_destroy_object_port( ipc_port_t port
)
279 ipc_kobject_set( port
, IKO_NULL
, IKOT_NONE
);
281 // iokit_remove_reference( obj );
283 ipc_port_dealloc_kernel( port
);
286 return( KERN_SUCCESS
);
290 iokit_switch_object_port( ipc_port_t port
, io_object_t obj
, ipc_kobject_type_t type
)
292 ipc_kobject_set( port
, (ipc_kobject_t
) obj
, type
);
294 return( KERN_SUCCESS
);
297 EXTERN mach_port_name_t
298 iokit_make_send_right( task_t task
, io_object_t obj
, ipc_kobject_type_t type
)
302 mach_port_name_t name
;
305 return MACH_PORT_NULL
;
307 port
= iokit_port_for_object( obj
, type
);
309 sendPort
= ipc_port_make_send( port
);
310 iokit_release_port( port
);
314 if (IP_VALID( sendPort
)) {
316 kr
= ipc_object_copyout( task
->itk_space
, (ipc_object_t
) sendPort
,
317 MACH_MSG_TYPE_PORT_SEND
, TRUE
, &name
);
318 if ( kr
!= KERN_SUCCESS
)
319 name
= MACH_PORT_NULL
;
320 } else if ( sendPort
== IP_NULL
)
321 name
= MACH_PORT_NULL
;
322 else if ( sendPort
== IP_DEAD
)
323 name
= MACH_PORT_DEAD
;
325 iokit_remove_reference( obj
);
331 iokit_mod_send_right( task_t task
, mach_port_name_t name
, mach_port_delta_t delta
)
333 return (mach_port_mod_refs( task
->itk_space
, name
, MACH_PORT_RIGHT_SEND
, delta
));
337 * Handle the No-More_Senders notification generated from a device port destroy.
338 * Since there are no longer any tasks which hold a send right to this device
339 * port a NMS notification has been generated.
343 iokit_no_senders( mach_no_senders_notification_t
* notification
)
346 io_object_t obj
= NULL
;
347 ipc_kobject_type_t type
;
350 port
= (ipc_port_t
) notification
->not_header
.msgh_remote_port
;
352 // convert a port to io_object_t.
353 if( IP_VALID(port
)) {
355 if( ip_active(port
)) {
356 obj
= (io_object_t
) port
->ip_kobject
;
357 type
= ip_kotype( port
);
358 if( (IKOT_IOKIT_OBJECT
== type
)
359 || (IKOT_IOKIT_CONNECT
== type
))
360 iokit_add_reference( obj
);
368 mach_port_mscount_t mscount
= notification
->not_count
;
370 if( KERN_SUCCESS
!= iokit_client_died( obj
, port
, type
, &mscount
))
372 /* Re-request no-senders notifications on the port. */
373 notify
= ipc_port_make_sonce( port
);
375 ipc_port_nsrequest( port
, mscount
+ 1, notify
, ¬ify
);
376 assert( notify
== IP_NULL
);
378 iokit_remove_reference( obj
);
386 iokit_notify( mach_msg_header_t
* msg
)
388 switch (msg
->msgh_id
) {
389 case MACH_NOTIFY_NO_SENDERS
:
390 iokit_no_senders((mach_no_senders_notification_t
*) msg
);
393 case MACH_NOTIFY_PORT_DELETED
:
394 case MACH_NOTIFY_PORT_DESTROYED
:
395 case MACH_NOTIFY_SEND_ONCE
:
396 case MACH_NOTIFY_DEAD_NAME
:
398 printf("iokit_notify: strange notification %ld\n", msg
->msgh_id
);
403 /* need to create a pmap function to generalize */
404 unsigned int IODefaultCacheBits(addr64_t pa
)
407 return(pmap_cache_attributes(pa
>> PAGE_SHIFT
));
410 kern_return_t
IOMapPages(vm_map_t map
, mach_vm_address_t va
, mach_vm_address_t pa
,
411 mach_vm_size_t length
, unsigned int options
)
415 pmap_t pmap
= map
->pmap
;
417 prot
= (options
& kIOMapReadOnly
)
418 ? VM_PROT_READ
: (VM_PROT_READ
|VM_PROT_WRITE
);
420 switch(options
& kIOMapCacheMask
) { /* What cache mode do we need? */
422 case kIOMapDefaultCache
:
424 flags
= IODefaultCacheBits(pa
);
427 case kIOMapInhibitCache
:
431 case kIOMapWriteThruCache
:
432 flags
= VM_WIMG_WTHRU
;
435 case kIOMapWriteCombineCache
:
436 flags
= VM_WIMG_WCOMB
;
439 case kIOMapCopybackCache
:
440 flags
= VM_WIMG_COPYBACK
;
444 // Set up a block mapped area
445 pmap_map_block(pmap
, va
, (ppnum_t
)atop_64(pa
), (uint32_t) atop_64(round_page_64(length
)), prot
, flags
, 0);
447 return( KERN_SUCCESS
);
450 kern_return_t
IOUnmapPages(vm_map_t map
, mach_vm_address_t va
, mach_vm_size_t length
)
452 pmap_t pmap
= map
->pmap
;
454 pmap_remove(pmap
, trunc_page_64(va
), round_page_64(va
+ length
));
456 return( KERN_SUCCESS
);
459 kern_return_t
IOProtectCacheMode(vm_map_t map
, mach_vm_address_t va
,
460 mach_vm_size_t length
, unsigned int options
)
465 pmap_t pmap
= map
->pmap
;
467 prot
= (options
& kIOMapReadOnly
)
468 ? VM_PROT_READ
: (VM_PROT_READ
|VM_PROT_WRITE
);
470 switch (options
& kIOMapCacheMask
)
472 /* What cache mode do we need? */
473 case kIOMapDefaultCache
:
475 return (KERN_INVALID_ARGUMENT
);
477 case kIOMapInhibitCache
:
481 case kIOMapWriteThruCache
:
482 flags
= VM_WIMG_WTHRU
;
485 case kIOMapWriteCombineCache
:
486 flags
= VM_WIMG_WCOMB
;
489 case kIOMapCopybackCache
:
490 flags
= VM_WIMG_COPYBACK
;
494 // can't remap block mappings, but ppc doesn't speculative read from WC
497 // enter each page's physical address in the target map
498 for (off
= 0; off
< length
; off
+= page_size
)
500 ppnum_t ppnum
= pmap_find_phys(pmap
, va
+ off
);
502 pmap_enter(pmap
, va
+ off
, ppnum
, prot
, flags
, TRUE
);
507 return (KERN_SUCCESS
);
510 ppnum_t
IOGetLastPageNumber(void)
512 ppnum_t lastPage
, highest
= 0;
516 for (idx
= 0; idx
< pmap_mem_regions_count
; idx
++)
518 lastPage
= pmap_mem_regions
[idx
].mrEnd
;
521 for (idx
= 0; idx
< pmap_memory_region_count
; idx
++)
523 lastPage
= pmap_memory_regions
[idx
].end
- 1;
527 if (lastPage
> highest
)
534 void IOGetTime( mach_timespec_t
* clock_time
);
535 void IOGetTime( mach_timespec_t
* clock_time
)
537 clock_get_system_nanotime(&clock_time
->tv_sec
, &clock_time
->tv_nsec
);