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>
65 #include <IOKit/IOTypes.h>
71 * Functions in iokit:IOUserClient.cpp
74 extern void iokit_add_reference( io_object_t obj
);
76 extern void iokit_remove_reference( io_object_t obj
);
78 extern ipc_port_t
iokit_port_for_object( io_object_t obj
,
79 ipc_kobject_type_t type
);
81 extern kern_return_t
iokit_client_died( io_object_t obj
,
82 ipc_port_t port
, ipc_kobject_type_t type
, mach_port_mscount_t
* mscount
);
85 iokit_client_memory_for_type(
89 vm_address_t
* address
,
93 * Lookup a device by its port.
94 * Doesn't consume the naked send right; produces a device reference.
97 iokit_lookup_object_port(
100 register io_object_t obj
;
106 if (ip_active(port
) && (ip_kotype(port
) == IKOT_IOKIT_OBJECT
)) {
107 obj
= (io_object_t
) port
->ip_kobject
;
108 iokit_add_reference( obj
);
118 MIGEXTERN io_object_t
119 iokit_lookup_connect_port(
122 register io_object_t obj
;
128 if (ip_active(port
) && (ip_kotype(port
) == IKOT_IOKIT_CONNECT
)) {
129 obj
= (io_object_t
) port
->ip_kobject
;
130 iokit_add_reference( obj
);
141 iokit_lookup_connect_ref(io_object_t connectRef
, ipc_space_t space
)
143 io_object_t obj
= NULL
;
145 if (connectRef
&& MACH_PORT_VALID((mach_port_name_t
)connectRef
)) {
149 kr
= ipc_object_translate(space
, (mach_port_name_t
)connectRef
, MACH_PORT_RIGHT_SEND
, (ipc_object_t
*)&port
);
151 if (kr
== KERN_SUCCESS
) {
152 assert(IP_VALID(port
));
154 if (ip_active(port
) && (ip_kotype(port
) == IKOT_IOKIT_CONNECT
)) {
155 obj
= (io_object_t
) port
->ip_kobject
;
156 iokit_add_reference(obj
);
167 iokit_lookup_connect_ref_current_task(io_object_t connectRef
)
169 return iokit_lookup_connect_ref(connectRef
, current_space());
173 iokit_retain_port( ipc_port_t port
)
175 ipc_port_reference( port
);
179 iokit_release_port( ipc_port_t port
)
181 ipc_port_release( port
);
185 * Get the port for a device.
186 * Consumes a device reference; produces a naked send right.
189 iokit_make_object_port(
192 register ipc_port_t port
;
193 register ipc_port_t sendPort
;
198 port
= iokit_port_for_object( obj
, IKOT_IOKIT_OBJECT
);
200 sendPort
= ipc_port_make_send( port
);
201 iokit_release_port( port
);
205 iokit_remove_reference( obj
);
211 iokit_make_connect_port(
214 register ipc_port_t port
;
215 register ipc_port_t sendPort
;
220 port
= iokit_port_for_object( obj
, IKOT_IOKIT_CONNECT
);
222 sendPort
= ipc_port_make_send( port
);
223 iokit_release_port( port
);
227 iokit_remove_reference( obj
);
234 iokit_alloc_object_port( io_object_t obj
, ipc_kobject_type_t type
);
239 iokit_alloc_object_port( io_object_t obj
, ipc_kobject_type_t type
)
246 /* Allocate port, keeping a reference for it. */
247 port
= ipc_port_alloc_kernel();
251 /* set kobject & type */
252 // iokit_add_reference( obj );
253 ipc_kobject_set( port
, (ipc_kobject_t
) obj
, type
);
255 /* Request no-senders notifications on the port. */
256 notify
= ipc_port_make_sonce( port
);
258 ipc_port_nsrequest( port
, 1, notify
, ¬ify
);
259 assert( notify
== IP_NULL
);
269 iokit_destroy_object_port( ipc_port_t port
)
271 ipc_kobject_set( port
, IKO_NULL
, IKOT_NONE
);
273 // iokit_remove_reference( obj );
275 ipc_port_dealloc_kernel( port
);
278 return( KERN_SUCCESS
);
282 iokit_switch_object_port( ipc_port_t port
, io_object_t obj
, ipc_kobject_type_t type
)
284 ipc_kobject_set( port
, (ipc_kobject_t
) obj
, type
);
286 return( KERN_SUCCESS
);
289 EXTERN mach_port_name_t
290 iokit_make_send_right( task_t task
, io_object_t obj
, ipc_kobject_type_t type
)
294 mach_port_name_t name
;
297 return MACH_PORT_NULL
;
299 port
= iokit_port_for_object( obj
, type
);
301 sendPort
= ipc_port_make_send( port
);
302 iokit_release_port( port
);
306 if (IP_VALID( sendPort
)) {
308 kr
= ipc_object_copyout( task
->itk_space
, (ipc_object_t
) sendPort
,
309 MACH_MSG_TYPE_PORT_SEND
, TRUE
, &name
);
310 if ( kr
!= KERN_SUCCESS
)
311 name
= MACH_PORT_NULL
;
312 } else if ( sendPort
== IP_NULL
)
313 name
= MACH_PORT_NULL
;
314 else if ( sendPort
== IP_DEAD
)
315 name
= MACH_PORT_DEAD
;
317 iokit_remove_reference( obj
);
323 * Handle the No-More_Senders notification generated from a device port destroy.
324 * Since there are no longer any tasks which hold a send right to this device
325 * port a NMS notification has been generated.
329 iokit_no_senders( mach_no_senders_notification_t
* notification
)
332 io_object_t obj
= NULL
;
333 ipc_kobject_type_t type
;
336 port
= (ipc_port_t
) notification
->not_header
.msgh_remote_port
;
338 // convert a port to io_object_t.
339 if( IP_VALID(port
)) {
341 if( ip_active(port
)) {
342 obj
= (io_object_t
) port
->ip_kobject
;
343 type
= ip_kotype( port
);
344 if( (IKOT_IOKIT_OBJECT
== type
)
345 || (IKOT_IOKIT_CONNECT
== type
))
346 iokit_add_reference( obj
);
354 mach_port_mscount_t mscount
= notification
->not_count
;
356 if( KERN_SUCCESS
!= iokit_client_died( obj
, port
, type
, &mscount
))
358 /* Re-request no-senders notifications on the port. */
359 notify
= ipc_port_make_sonce( port
);
361 ipc_port_nsrequest( port
, mscount
+ 1, notify
, ¬ify
);
362 assert( notify
== IP_NULL
);
364 iokit_remove_reference( obj
);
372 iokit_notify( mach_msg_header_t
* msg
)
374 switch (msg
->msgh_id
) {
375 case MACH_NOTIFY_NO_SENDERS
:
376 iokit_no_senders((mach_no_senders_notification_t
*) msg
);
379 case MACH_NOTIFY_PORT_DELETED
:
380 case MACH_NOTIFY_PORT_DESTROYED
:
381 case MACH_NOTIFY_SEND_ONCE
:
382 case MACH_NOTIFY_DEAD_NAME
:
384 printf("iokit_notify: strange notification %ld\n", msg
->msgh_id
);
389 /* need to create a pmap function to generalize */
390 unsigned int IODefaultCacheBits(addr64_t pa
)
394 struct phys_entry
* pp
;
396 // Find physical address
397 if ((pp
= pmap_find_physentry(pa
>> 12))) {
398 // Use physical attributes as default
399 // NOTE: DEVICE_PAGER_FLAGS are made to line up
400 flags
= VM_MEM_COHERENT
; /* We only support coherent memory */
401 if(pp
->ppLink
& ppG
) flags
|= VM_MEM_GUARDED
; /* Add in guarded if it is */
402 if(pp
->ppLink
& ppI
) flags
|= VM_MEM_NOT_CACHEABLE
; /* Add in cache inhibited if so */
404 // If no physical, just hard code attributes
407 extern vm_offset_t avail_end
;
410 flags
= VM_WIMG_COPYBACK
;
418 kern_return_t
IOMapPages(vm_map_t map
, vm_offset_t va
, vm_offset_t pa
,
419 vm_size_t length
, unsigned int options
)
424 pmap_t pmap
= map
->pmap
;
426 prot
= (options
& kIOMapReadOnly
)
427 ? VM_PROT_READ
: (VM_PROT_READ
|VM_PROT_WRITE
);
429 switch(options
& kIOMapCacheMask
) { /* What cache mode do we need? */
431 case kIOMapDefaultCache
:
433 flags
= IODefaultCacheBits(pa
);
436 case kIOMapInhibitCache
:
440 case kIOMapWriteThruCache
:
441 flags
= VM_WIMG_WTHRU
;
444 case kIOWriteCombineCache
:
445 flags
= VM_WIMG_WCOMB
;
448 case kIOMapCopybackCache
:
449 flags
= VM_WIMG_COPYBACK
;
454 // Set up a block mapped area
455 pmap_map_block(pmap
, (addr64_t
)va
, (ppnum_t
)(pa
>> 12), length
, prot
, flags
, 0);
458 // enter each page's physical address in the target map
460 for (off
= 0; off
< length
; off
+= page_size
)
461 pmap_enter(pmap
, va
+ off
, (pa
+ off
) >> 12, prot
, flags
, TRUE
);
465 return( KERN_SUCCESS
);
468 kern_return_t
IOUnmapPages(vm_map_t map
, vm_offset_t va
, vm_size_t length
)
470 pmap_t pmap
= map
->pmap
;
472 pmap_remove(pmap
, trunc_page_64(va
), round_page_64(va
+ length
));
474 return( KERN_SUCCESS
);
477 void IOGetTime( mach_timespec_t
* clock_time
);
478 void IOGetTime( mach_timespec_t
* clock_time
)
480 clock_get_system_nanotime(&clock_time
->tv_sec
, &clock_time
->tv_nsec
);