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 <sys/ioctl.h>
62 #include <machine/machparam.h>
65 #include <ppc/mappings.h>
66 #include <ppc/pmap_internals.h>
68 #include <IOKit/IOTypes.h>
74 * Functions in iokit:IOUserClient.cpp
77 extern void iokit_add_reference( io_object_t obj
);
79 extern void iokit_remove_reference( io_object_t obj
);
81 extern ipc_port_t
iokit_port_for_object( io_object_t obj
,
82 ipc_kobject_type_t type
);
84 extern kern_return_t
iokit_client_died( io_object_t obj
,
85 ipc_port_t port
, ipc_kobject_type_t type
);
88 iokit_client_memory_for_type(
92 vm_address_t
* address
,
96 * Lookup a device by its port.
97 * Doesn't consume the naked send right; produces a device reference.
100 iokit_lookup_object_port(
103 register io_object_t obj
;
109 if (ip_active(port
) && (ip_kotype(port
) == IKOT_IOKIT_OBJECT
)) {
110 obj
= (io_object_t
) port
->ip_kobject
;
111 iokit_add_reference( obj
);
121 MIGEXTERN io_object_t
122 iokit_lookup_connect_port(
125 register io_object_t obj
;
131 if (ip_active(port
) && (ip_kotype(port
) == IKOT_IOKIT_CONNECT
)) {
132 obj
= (io_object_t
) port
->ip_kobject
;
133 iokit_add_reference( obj
);
144 iokit_lookup_connect_ref(io_object_t connectRef
, ipc_space_t space
)
146 io_object_t obj
= NULL
;
148 if (connectRef
&& MACH_PORT_VALID((mach_port_name_t
)connectRef
)) {
152 kr
= ipc_object_translate(space
, (mach_port_name_t
)connectRef
, MACH_PORT_RIGHT_SEND
, (ipc_object_t
*)&port
);
154 if (kr
== KERN_SUCCESS
) {
155 assert(IP_VALID(port
));
157 if (ip_active(port
) && (ip_kotype(port
) == IKOT_IOKIT_CONNECT
)) {
158 obj
= (io_object_t
) port
->ip_kobject
;
159 iokit_add_reference(obj
);
170 iokit_lookup_connect_ref_current_task(io_object_t connectRef
)
172 return iokit_lookup_connect_ref(connectRef
, current_space());
176 * Get the port for a device.
177 * Consumes a device reference; produces a naked send right.
180 iokit_make_object_port(
183 register ipc_port_t port
;
188 port
= iokit_port_for_object( obj
, IKOT_IOKIT_OBJECT
);
190 port
= ipc_port_make_send( port
);
192 iokit_remove_reference( obj
);
198 iokit_make_connect_port(
201 register ipc_port_t port
;
206 port
= iokit_port_for_object( obj
, IKOT_IOKIT_CONNECT
);
208 port
= ipc_port_make_send( port
);
210 iokit_remove_reference( obj
);
217 iokit_alloc_object_port( io_object_t obj
, ipc_kobject_type_t type
);
222 iokit_alloc_object_port( io_object_t obj
, ipc_kobject_type_t type
)
229 /* Allocate port, keeping a reference for it. */
230 port
= ipc_port_alloc_kernel();
234 /* set kobject & type */
235 // iokit_add_reference( obj );
236 ipc_kobject_set( port
, (ipc_kobject_t
) obj
, type
);
238 /* Request no-senders notifications on the port. */
239 notify
= ipc_port_make_sonce( port
);
241 ipc_port_nsrequest( port
, 1, notify
, ¬ify
);
242 assert( notify
== IP_NULL
);
252 iokit_destroy_object_port( ipc_port_t port
)
254 ipc_kobject_set( port
, IKO_NULL
, IKOT_NONE
);
256 // iokit_remove_reference( obj );
258 ipc_port_dealloc_kernel( port
);
261 return( KERN_SUCCESS
);
264 EXTERN mach_port_name_t
265 iokit_make_send_right( task_t task
, io_object_t obj
, ipc_kobject_type_t type
)
269 mach_port_name_t name
;
272 return MACH_PORT_NULL
;
274 port
= iokit_port_for_object( obj
, type
);
276 port
= ipc_port_make_send( port
);
278 return MACH_PORT_NULL
;
280 kr
= ipc_object_copyout( task
->itk_space
, (ipc_object_t
) port
,
281 MACH_MSG_TYPE_PORT_SEND
, TRUE
, &name
);
283 if( kr
!= KERN_SUCCESS
)
284 name
= MACH_PORT_NULL
;
286 iokit_remove_reference( obj
);
292 * Handle the No-More_Senders notification generated from a device port destroy.
293 * Since there are no longer any tasks which hold a send right to this device
294 * port a NMS notification has been generated.
298 iokit_no_senders( mach_no_senders_notification_t
* notification
)
301 io_object_t obj
= NULL
;
302 ipc_kobject_type_t type
;
304 port
= (ipc_port_t
) notification
->not_header
.msgh_remote_port
;
306 // convert a port to io_object_t.
307 if( IP_VALID(port
)) {
309 if( ip_active(port
)) {
310 obj
= (io_object_t
) port
->ip_kobject
;
311 type
= ip_kotype( port
);
312 if( (IKOT_IOKIT_OBJECT
== type
)
313 || (IKOT_IOKIT_CONNECT
== type
))
314 iokit_add_reference( obj
);
321 (void) iokit_client_died( obj
, port
, type
);
322 iokit_remove_reference( obj
);
330 iokit_notify( mach_msg_header_t
* msg
)
332 switch (msg
->msgh_id
) {
333 case MACH_NOTIFY_NO_SENDERS
:
334 iokit_no_senders((mach_no_senders_notification_t
*) msg
);
337 case MACH_NOTIFY_PORT_DELETED
:
338 case MACH_NOTIFY_PORT_DESTROYED
:
339 case MACH_NOTIFY_SEND_ONCE
:
340 case MACH_NOTIFY_DEAD_NAME
:
342 printf("iokit_notify: strange notification %ld\n", msg
->msgh_id
);
347 kern_return_t
IOMapPages(vm_map_t map
, vm_offset_t va
, vm_offset_t pa
,
348 vm_size_t length
, unsigned int options
)
353 struct phys_entry
*pp
;
354 pmap_t pmap
= map
->pmap
;
356 prot
= (options
& kIOMapReadOnly
)
357 ? VM_PROT_READ
: (VM_PROT_READ
|VM_PROT_WRITE
);
361 switch(options
& kIOMapCacheMask
) { /* What cache mode do we need? */
363 case kIOMapDefaultCache
:
365 if(pp
= pmap_find_physentry(pa
)) { /* Find physical address */
366 memattr
= ((pp
->pte1
& 0x00000078) >> 3); /* Use physical attributes as default */
368 else { /* If no physical, just hard code attributes */
369 memattr
= PTE_WIMG_UNCACHED_COHERENT_GUARDED
;
373 case kIOMapInhibitCache
:
374 memattr
= PTE_WIMG_UNCACHED_COHERENT_GUARDED
;
377 case kIOMapWriteThruCache
:
378 memattr
= PTE_WIMG_WT_CACHED_COHERENT_GUARDED
;
381 case kIOMapCopybackCache
:
382 memattr
= PTE_WIMG_CB_CACHED_COHERENT
;
386 pmap_map_block(pmap
, va
, pa
, length
, prot
, memattr
, 0); /* Set up a block mapped area */
389 // enter each page's physical address in the target map
390 for (off
= 0; off
< length
; off
+= page_size
) { /* Loop for the whole length */
391 pmap_enter(pmap
, va
+ off
, pa
+ off
, prot
, TRUE
); /* Map it in */
395 return( KERN_SUCCESS
);
398 void IOGetTime( mach_timespec_t
* clock_time
);
399 void IOGetTime( mach_timespec_t
* clock_time
)
401 *clock_time
= clock_get_system_value();