2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 #include <zone_debug.h>
34 #include <mach/boolean.h>
35 #include <mach/kern_return.h>
36 #include <mach/mig_errors.h>
37 #include <mach/port.h>
38 #include <mach/vm_param.h>
39 #include <mach/notify.h>
40 #include <mach/mach_host_server.h>
41 #include <mach/mach_types.h>
43 #include <machine/machparam.h> /* spl definitions */
45 #include <ipc/ipc_port.h>
46 #include <ipc/ipc_space.h>
48 #include <kern/clock.h>
50 #include <kern/counters.h>
51 #include <kern/queue.h>
52 #include <kern/zalloc.h>
53 #include <kern/thread.h>
54 #include <kern/task.h>
55 #include <kern/sched_prim.h>
56 #include <kern/misc_protos.h>
59 #include <vm/vm_map.h>
60 #include <vm/vm_kern.h>
62 #include <device/device_types.h>
63 #include <device/device_port.h>
64 #include <device/device_server.h>
66 #include <machine/machparam.h>
69 #include <ppc/mappings.h>
72 #include <i386/pmap.h>
74 #include <IOKit/IOTypes.h>
80 * Functions in iokit:IOUserClient.cpp
83 extern void iokit_add_reference( io_object_t obj
);
85 extern ipc_port_t
iokit_port_for_object( io_object_t obj
,
86 ipc_kobject_type_t type
);
88 extern kern_return_t
iokit_client_died( io_object_t obj
,
89 ipc_port_t port
, ipc_kobject_type_t type
, mach_port_mscount_t
* mscount
);
92 iokit_client_memory_for_type(
96 vm_address_t
* address
,
100 extern ppnum_t
IOGetLastPageNumber(void);
103 * Lookup a device by its port.
104 * Doesn't consume the naked send right; produces a device reference.
106 MIGEXTERN io_object_t
107 iokit_lookup_object_port(
110 register io_object_t obj
;
116 if (ip_active(port
) && (ip_kotype(port
) == IKOT_IOKIT_OBJECT
)) {
117 obj
= (io_object_t
) port
->ip_kobject
;
118 iokit_add_reference( obj
);
128 MIGEXTERN io_object_t
129 iokit_lookup_connect_port(
132 register io_object_t obj
;
138 if (ip_active(port
) && (ip_kotype(port
) == IKOT_IOKIT_CONNECT
)) {
139 obj
= (io_object_t
) port
->ip_kobject
;
140 iokit_add_reference( obj
);
151 iokit_lookup_connect_ref(io_object_t connectRef
, ipc_space_t space
)
153 io_object_t obj
= NULL
;
155 if (connectRef
&& MACH_PORT_VALID((mach_port_name_t
)connectRef
)) {
159 kr
= ipc_object_translate(space
, (mach_port_name_t
)connectRef
, MACH_PORT_RIGHT_SEND
, (ipc_object_t
*)&port
);
161 if (kr
== KERN_SUCCESS
) {
162 assert(IP_VALID(port
));
164 if (ip_active(port
) && (ip_kotype(port
) == IKOT_IOKIT_CONNECT
)) {
165 obj
= (io_object_t
) port
->ip_kobject
;
166 iokit_add_reference(obj
);
177 iokit_lookup_connect_ref_current_task(io_object_t connectRef
)
179 return iokit_lookup_connect_ref(connectRef
, current_space());
183 iokit_retain_port( ipc_port_t port
)
185 ipc_port_reference( port
);
189 iokit_release_port( ipc_port_t port
)
191 ipc_port_release( port
);
195 * Get the port for a device.
196 * Consumes a device reference; produces a naked send right.
199 iokit_make_object_port(
202 register ipc_port_t port
;
203 register ipc_port_t sendPort
;
208 port
= iokit_port_for_object( obj
, IKOT_IOKIT_OBJECT
);
210 sendPort
= ipc_port_make_send( port
);
211 iokit_release_port( port
);
215 iokit_remove_reference( obj
);
221 iokit_make_connect_port(
224 register ipc_port_t port
;
225 register ipc_port_t sendPort
;
230 port
= iokit_port_for_object( obj
, IKOT_IOKIT_CONNECT
);
232 sendPort
= ipc_port_make_send( port
);
233 iokit_release_port( port
);
237 iokit_remove_reference( obj
);
244 iokit_alloc_object_port( io_object_t obj
, ipc_kobject_type_t type
);
249 iokit_alloc_object_port( io_object_t obj
, ipc_kobject_type_t type
)
256 /* Allocate port, keeping a reference for it. */
257 port
= ipc_port_alloc_kernel();
261 /* set kobject & type */
262 // iokit_add_reference( obj );
263 ipc_kobject_set( port
, (ipc_kobject_t
) obj
, type
);
265 /* Request no-senders notifications on the port. */
266 notify
= ipc_port_make_sonce( port
);
268 ipc_port_nsrequest( port
, 1, notify
, ¬ify
);
269 assert( notify
== IP_NULL
);
279 iokit_destroy_object_port( ipc_port_t port
)
281 ipc_kobject_set( port
, IKO_NULL
, IKOT_NONE
);
283 // iokit_remove_reference( obj );
285 ipc_port_dealloc_kernel( port
);
288 return( KERN_SUCCESS
);
292 iokit_switch_object_port( ipc_port_t port
, io_object_t obj
, ipc_kobject_type_t type
)
294 ipc_kobject_set( port
, (ipc_kobject_t
) obj
, type
);
296 return( KERN_SUCCESS
);
299 EXTERN mach_port_name_t
300 iokit_make_send_right( task_t task
, io_object_t obj
, ipc_kobject_type_t type
)
304 mach_port_name_t name
;
307 return MACH_PORT_NULL
;
309 port
= iokit_port_for_object( obj
, type
);
311 sendPort
= ipc_port_make_send( port
);
312 iokit_release_port( port
);
316 if (IP_VALID( sendPort
)) {
318 kr
= ipc_object_copyout( task
->itk_space
, (ipc_object_t
) sendPort
,
319 MACH_MSG_TYPE_PORT_SEND
, TRUE
, &name
);
320 if ( kr
!= KERN_SUCCESS
)
321 name
= MACH_PORT_NULL
;
322 } else if ( sendPort
== IP_NULL
)
323 name
= MACH_PORT_NULL
;
324 else if ( sendPort
== IP_DEAD
)
325 name
= MACH_PORT_DEAD
;
327 iokit_remove_reference( obj
);
333 iokit_mod_send_right( task_t task
, mach_port_name_t name
, mach_port_delta_t delta
)
335 return (mach_port_mod_refs( task
->itk_space
, name
, MACH_PORT_RIGHT_SEND
, delta
));
339 * Handle the No-More_Senders notification generated from a device port destroy.
340 * Since there are no longer any tasks which hold a send right to this device
341 * port a NMS notification has been generated.
345 iokit_no_senders( mach_no_senders_notification_t
* notification
)
348 io_object_t obj
= NULL
;
349 ipc_kobject_type_t type
;
352 port
= (ipc_port_t
) notification
->not_header
.msgh_remote_port
;
354 // convert a port to io_object_t.
355 if( IP_VALID(port
)) {
357 if( ip_active(port
)) {
358 obj
= (io_object_t
) port
->ip_kobject
;
359 type
= ip_kotype( port
);
360 if( (IKOT_IOKIT_OBJECT
== type
)
361 || (IKOT_IOKIT_CONNECT
== type
))
362 iokit_add_reference( obj
);
370 mach_port_mscount_t mscount
= notification
->not_count
;
372 if( KERN_SUCCESS
!= iokit_client_died( obj
, port
, type
, &mscount
))
374 /* Re-request no-senders notifications on the port. */
375 notify
= ipc_port_make_sonce( port
);
377 ipc_port_nsrequest( port
, mscount
+ 1, notify
, ¬ify
);
378 assert( notify
== IP_NULL
);
380 iokit_remove_reference( obj
);
388 iokit_notify( mach_msg_header_t
* msg
)
390 switch (msg
->msgh_id
) {
391 case MACH_NOTIFY_NO_SENDERS
:
392 iokit_no_senders((mach_no_senders_notification_t
*) msg
);
395 case MACH_NOTIFY_PORT_DELETED
:
396 case MACH_NOTIFY_PORT_DESTROYED
:
397 case MACH_NOTIFY_SEND_ONCE
:
398 case MACH_NOTIFY_DEAD_NAME
:
400 printf("iokit_notify: strange notification %ld\n", msg
->msgh_id
);
405 /* need to create a pmap function to generalize */
406 unsigned int IODefaultCacheBits(addr64_t pa
)
409 return(pmap_cache_attributes(pa
>> PAGE_SHIFT
));
412 kern_return_t
IOMapPages(vm_map_t map
, mach_vm_address_t va
, mach_vm_address_t pa
,
413 mach_vm_size_t length
, unsigned int options
)
417 pmap_t pmap
= map
->pmap
;
419 prot
= (options
& kIOMapReadOnly
)
420 ? VM_PROT_READ
: (VM_PROT_READ
|VM_PROT_WRITE
);
422 switch(options
& kIOMapCacheMask
) { /* What cache mode do we need? */
424 case kIOMapDefaultCache
:
426 flags
= IODefaultCacheBits(pa
);
429 case kIOMapInhibitCache
:
433 case kIOMapWriteThruCache
:
434 flags
= VM_WIMG_WTHRU
;
437 case kIOMapWriteCombineCache
:
438 flags
= VM_WIMG_WCOMB
;
441 case kIOMapCopybackCache
:
442 flags
= VM_WIMG_COPYBACK
;
446 // Set up a block mapped area
447 pmap_map_block(pmap
, va
, (ppnum_t
)atop_64(pa
), (uint32_t) atop_64(round_page_64(length
)), prot
, flags
, 0);
449 return( KERN_SUCCESS
);
452 kern_return_t
IOUnmapPages(vm_map_t map
, mach_vm_address_t va
, mach_vm_size_t length
)
454 pmap_t pmap
= map
->pmap
;
456 pmap_remove(pmap
, trunc_page_64(va
), round_page_64(va
+ length
));
458 return( KERN_SUCCESS
);
461 kern_return_t
IOProtectCacheMode(vm_map_t map
, mach_vm_address_t va
,
462 mach_vm_size_t length
, unsigned int options
)
467 pmap_t pmap
= map
->pmap
;
469 prot
= (options
& kIOMapReadOnly
)
470 ? VM_PROT_READ
: (VM_PROT_READ
|VM_PROT_WRITE
);
472 switch (options
& kIOMapCacheMask
)
474 /* What cache mode do we need? */
475 case kIOMapDefaultCache
:
477 return (KERN_INVALID_ARGUMENT
);
479 case kIOMapInhibitCache
:
483 case kIOMapWriteThruCache
:
484 flags
= VM_WIMG_WTHRU
;
487 case kIOMapWriteCombineCache
:
488 flags
= VM_WIMG_WCOMB
;
491 case kIOMapCopybackCache
:
492 flags
= VM_WIMG_COPYBACK
;
496 // can't remap block mappings, but ppc doesn't speculative read from WC
499 // enter each page's physical address in the target map
500 for (off
= 0; off
< length
; off
+= page_size
)
502 ppnum_t ppnum
= pmap_find_phys(pmap
, va
+ off
);
504 pmap_enter(pmap
, va
+ off
, ppnum
, prot
, flags
, TRUE
);
509 return (KERN_SUCCESS
);
512 ppnum_t
IOGetLastPageNumber(void)
514 ppnum_t lastPage
, highest
= 0;
518 for (idx
= 0; idx
< pmap_mem_regions_count
; idx
++)
520 lastPage
= pmap_mem_regions
[idx
].mrEnd
;
523 for (idx
= 0; idx
< pmap_memory_region_count
; idx
++)
525 lastPage
= pmap_memory_regions
[idx
].end
- 1;
529 if (lastPage
> highest
)
536 void IOGetTime( mach_timespec_t
* clock_time
);
537 void IOGetTime( mach_timespec_t
* clock_time
)
539 clock_get_system_nanotime(&clock_time
->tv_sec
, &clock_time
->tv_nsec
);