]> git.saurik.com Git - apple/xnu.git/blame - osfmk/device/iokit_rpc.c
xnu-792.13.8.tar.gz
[apple/xnu.git] / osfmk / device / iokit_rpc.c
CommitLineData
1c79356b 1/*
91447636 2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
1c79356b 3 *
8ad349bb 4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
1c79356b 5 *
8ad349bb
A
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
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
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.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
1c79356b
A
29 */
30#include <mach_kdb.h>
31#include <zone_debug.h>
32#include <mach_kdb.h>
33
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>
42
43#include <machine/machparam.h> /* spl definitions */
44
45#include <ipc/ipc_port.h>
46#include <ipc/ipc_space.h>
47
48#include <kern/clock.h>
49#include <kern/spl.h>
1c79356b
A
50#include <kern/counters.h>
51#include <kern/queue.h>
52#include <kern/zalloc.h>
53#include <kern/thread.h>
1c79356b
A
54#include <kern/task.h>
55#include <kern/sched_prim.h>
56#include <kern/misc_protos.h>
57
58#include <vm/pmap.h>
59#include <vm/vm_map.h>
60#include <vm/vm_kern.h>
61
62#include <device/device_types.h>
63#include <device/device_port.h>
64#include <device/device_server.h>
65
1c79356b
A
66#include <machine/machparam.h>
67
68#ifdef __ppc__
69#include <ppc/mappings.h>
1c79356b 70#endif
91447636
A
71#ifdef __i386
72#include <i386/pmap.h>
73#endif
1c79356b
A
74#include <IOKit/IOTypes.h>
75
76#define EXTERN
77#define MIGEXTERN
78
79/*
80 * Functions in iokit:IOUserClient.cpp
81 */
82
83extern void iokit_add_reference( io_object_t obj );
84
1c79356b
A
85extern ipc_port_t iokit_port_for_object( io_object_t obj,
86 ipc_kobject_type_t type );
87
88extern kern_return_t iokit_client_died( io_object_t obj,
55e303ae 89 ipc_port_t port, ipc_kobject_type_t type, mach_port_mscount_t * mscount );
1c79356b
A
90
91extern kern_return_t
92iokit_client_memory_for_type(
93 io_object_t connect,
94 unsigned int type,
95 unsigned int * flags,
96 vm_address_t * address,
97 vm_size_t * size );
98
5d5c5d0d
A
99
100extern ppnum_t IOGetLastPageNumber(void);
101
1c79356b
A
102/*
103 * Lookup a device by its port.
104 * Doesn't consume the naked send right; produces a device reference.
105 */
106MIGEXTERN io_object_t
107iokit_lookup_object_port(
108 ipc_port_t port)
109{
110 register io_object_t obj;
111
112 if (!IP_VALID(port))
113 return (NULL);
114
115 ip_lock(port);
116 if (ip_active(port) && (ip_kotype(port) == IKOT_IOKIT_OBJECT)) {
117 obj = (io_object_t) port->ip_kobject;
118 iokit_add_reference( obj );
119 }
120 else
121 obj = NULL;
122
123 ip_unlock(port);
124
125 return( obj );
126}
127
128MIGEXTERN io_object_t
129iokit_lookup_connect_port(
130 ipc_port_t port)
131{
132 register io_object_t obj;
133
134 if (!IP_VALID(port))
135 return (NULL);
136
137 ip_lock(port);
138 if (ip_active(port) && (ip_kotype(port) == IKOT_IOKIT_CONNECT)) {
139 obj = (io_object_t) port->ip_kobject;
140 iokit_add_reference( obj );
141 }
142 else
143 obj = NULL;
144
145 ip_unlock(port);
146
147 return( obj );
148}
149
150EXTERN io_object_t
151iokit_lookup_connect_ref(io_object_t connectRef, ipc_space_t space)
152{
153 io_object_t obj = NULL;
154
155 if (connectRef && MACH_PORT_VALID((mach_port_name_t)connectRef)) {
156 ipc_port_t port;
157 kern_return_t kr;
158
159 kr = ipc_object_translate(space, (mach_port_name_t)connectRef, MACH_PORT_RIGHT_SEND, (ipc_object_t *)&port);
160
161 if (kr == KERN_SUCCESS) {
162 assert(IP_VALID(port));
163
164 if (ip_active(port) && (ip_kotype(port) == IKOT_IOKIT_CONNECT)) {
165 obj = (io_object_t) port->ip_kobject;
166 iokit_add_reference(obj);
167 }
168
169 ip_unlock(port);
170 }
171 }
172
173 return obj;
174}
175
176EXTERN io_object_t
177iokit_lookup_connect_ref_current_task(io_object_t connectRef)
178{
179 return iokit_lookup_connect_ref(connectRef, current_space());
180}
181
9bccf70c
A
182EXTERN void
183iokit_retain_port( ipc_port_t port )
184{
185 ipc_port_reference( port );
186}
187
188EXTERN void
189iokit_release_port( ipc_port_t port )
190{
191 ipc_port_release( port );
192}
193
1c79356b
A
194/*
195 * Get the port for a device.
196 * Consumes a device reference; produces a naked send right.
197 */
198MIGEXTERN ipc_port_t
199iokit_make_object_port(
200 io_object_t obj )
201{
202 register ipc_port_t port;
9bccf70c 203 register ipc_port_t sendPort;
1c79356b
A
204
205 if( obj == NULL)
206 return IP_NULL;
207
208 port = iokit_port_for_object( obj, IKOT_IOKIT_OBJECT );
9bccf70c
A
209 if( port) {
210 sendPort = ipc_port_make_send( port);
211 iokit_release_port( port );
212 } else
213 sendPort = IP_NULL;
1c79356b
A
214
215 iokit_remove_reference( obj );
216
9bccf70c 217 return( sendPort);
1c79356b
A
218}
219
220MIGEXTERN ipc_port_t
221iokit_make_connect_port(
222 io_object_t obj )
223{
224 register ipc_port_t port;
9bccf70c 225 register ipc_port_t sendPort;
1c79356b
A
226
227 if( obj == NULL)
228 return IP_NULL;
229
230 port = iokit_port_for_object( obj, IKOT_IOKIT_CONNECT );
9bccf70c
A
231 if( port) {
232 sendPort = ipc_port_make_send( port);
233 iokit_release_port( port );
234 } else
235 sendPort = IP_NULL;
1c79356b
A
236
237 iokit_remove_reference( obj );
238
9bccf70c 239 return( sendPort);
1c79356b
A
240}
241
242
243EXTERN ipc_port_t
244iokit_alloc_object_port( io_object_t obj, ipc_kobject_type_t type );
245
246int gIOKitPortCount;
247
248EXTERN ipc_port_t
249iokit_alloc_object_port( io_object_t obj, ipc_kobject_type_t type )
250{
251 ipc_port_t notify;
252 ipc_port_t port;
253
254 do {
255
256 /* Allocate port, keeping a reference for it. */
257 port = ipc_port_alloc_kernel();
258 if( port == IP_NULL)
259 continue;
260
261 /* set kobject & type */
262// iokit_add_reference( obj );
263 ipc_kobject_set( port, (ipc_kobject_t) obj, type);
264
265 /* Request no-senders notifications on the port. */
266 notify = ipc_port_make_sonce( port);
267 ip_lock( port);
268 ipc_port_nsrequest( port, 1, notify, &notify);
269 assert( notify == IP_NULL);
270 gIOKitPortCount++;
271
272 } while( FALSE);
273
274 return( port );
275}
276
277
278EXTERN kern_return_t
279iokit_destroy_object_port( ipc_port_t port )
280{
281 ipc_kobject_set( port, IKO_NULL, IKOT_NONE);
282
283// iokit_remove_reference( obj );
284
285 ipc_port_dealloc_kernel( port);
286 gIOKitPortCount--;
287
288 return( KERN_SUCCESS);
289}
290
43866e37
A
291EXTERN kern_return_t
292iokit_switch_object_port( ipc_port_t port, io_object_t obj, ipc_kobject_type_t type )
293{
294 ipc_kobject_set( port, (ipc_kobject_t) obj, type);
295
296 return( KERN_SUCCESS);
297}
298
1c79356b
A
299EXTERN mach_port_name_t
300iokit_make_send_right( task_t task, io_object_t obj, ipc_kobject_type_t type )
301{
1c79356b 302 ipc_port_t port;
9bccf70c 303 ipc_port_t sendPort;
1c79356b
A
304 mach_port_name_t name;
305
306 if( obj == NULL)
307 return MACH_PORT_NULL;
308
309 port = iokit_port_for_object( obj, type );
9bccf70c
A
310 if( port) {
311 sendPort = ipc_port_make_send( port);
312 iokit_release_port( port );
313 } else
314 sendPort = IP_NULL;
315
316 if (IP_VALID( sendPort )) {
317 kern_return_t kr;
318 kr = ipc_object_copyout( task->itk_space, (ipc_object_t) sendPort,
1c79356b 319 MACH_MSG_TYPE_PORT_SEND, TRUE, &name);
9bccf70c
A
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;
1c79356b
A
326
327 iokit_remove_reference( obj );
328
329 return( name );
330}
331
91447636
A
332EXTERN kern_return_t
333iokit_mod_send_right( task_t task, mach_port_name_t name, mach_port_delta_t delta )
334{
335 return (mach_port_mod_refs( task->itk_space, name, MACH_PORT_RIGHT_SEND, delta ));
336}
337
1c79356b
A
338/*
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.
342 */
343
344static void
345iokit_no_senders( mach_no_senders_notification_t * notification )
346{
347 ipc_port_t port;
348 io_object_t obj = NULL;
349 ipc_kobject_type_t type;
9bccf70c 350 ipc_port_t notify;
1c79356b
A
351
352 port = (ipc_port_t) notification->not_header.msgh_remote_port;
353
354 // convert a port to io_object_t.
355 if( IP_VALID(port)) {
356 ip_lock(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 );
e3027f41 363 else
1c79356b
A
364 obj = NULL;
365 }
366 ip_unlock(port);
367
368 if( obj ) {
9bccf70c
A
369
370 mach_port_mscount_t mscount = notification->not_count;
371
372 if( KERN_SUCCESS != iokit_client_died( obj, port, type, &mscount ))
373 {
374 /* Re-request no-senders notifications on the port. */
375 notify = ipc_port_make_sonce( port);
376 ip_lock( port);
377 ipc_port_nsrequest( port, mscount + 1, notify, &notify);
378 assert( notify == IP_NULL);
379 }
1c79356b
A
380 iokit_remove_reference( obj );
381 }
382 }
383}
384
385
386EXTERN
387boolean_t
388iokit_notify( mach_msg_header_t * msg )
389{
390 switch (msg->msgh_id) {
391 case MACH_NOTIFY_NO_SENDERS:
392 iokit_no_senders((mach_no_senders_notification_t *) msg);
393 return TRUE;
394
395 case MACH_NOTIFY_PORT_DELETED:
396 case MACH_NOTIFY_PORT_DESTROYED:
397 case MACH_NOTIFY_SEND_ONCE:
398 case MACH_NOTIFY_DEAD_NAME:
399 default:
400 printf("iokit_notify: strange notification %ld\n", msg->msgh_id);
401 return FALSE;
402 }
403}
404
55e303ae
A
405/* need to create a pmap function to generalize */
406unsigned int IODefaultCacheBits(addr64_t pa)
de355530 407{
8ad349bb 408
5d5c5d0d 409 return(pmap_cache_attributes(pa >> PAGE_SHIFT));
9bccf70c 410}
9bccf70c 411
5d5c5d0d
A
412kern_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)
1c79356b 414{
1c79356b 415 vm_prot_t prot;
55e303ae
A
416 unsigned int flags;
417 pmap_t pmap = map->pmap;
1c79356b
A
418
419 prot = (options & kIOMapReadOnly)
420 ? VM_PROT_READ : (VM_PROT_READ|VM_PROT_WRITE);
421
55e303ae
A
422 switch(options & kIOMapCacheMask ) { /* What cache mode do we need? */
423
424 case kIOMapDefaultCache:
425 default:
426 flags = IODefaultCacheBits(pa);
427 break;
428
429 case kIOMapInhibitCache:
430 flags = VM_WIMG_IO;
431 break;
432
433 case kIOMapWriteThruCache:
434 flags = VM_WIMG_WTHRU;
435 break;
436
5d5c5d0d 437 case kIOMapWriteCombineCache:
55e303ae
A
438 flags = VM_WIMG_WCOMB;
439 break;
440
441 case kIOMapCopybackCache:
442 flags = VM_WIMG_COPYBACK;
443 break;
444 }
1c79356b 445
55e303ae 446 // Set up a block mapped area
5d5c5d0d
A
447 pmap_map_block(pmap, va, (ppnum_t)atop_64(pa), (uint32_t) atop_64(round_page_64(length)), prot, flags, 0);
448
449 return( KERN_SUCCESS );
450}
451
452kern_return_t IOUnmapPages(vm_map_t map, mach_vm_address_t va, mach_vm_size_t length)
453{
454 pmap_t pmap = map->pmap;
455
456 pmap_remove(pmap, trunc_page_64(va), round_page_64(va + length));
457
458 return( KERN_SUCCESS );
459}
460
461kern_return_t IOProtectCacheMode(vm_map_t map, mach_vm_address_t va,
462 mach_vm_size_t length, unsigned int options)
463{
464 mach_vm_size_t off;
465 vm_prot_t prot;
466 unsigned int flags;
467 pmap_t pmap = map->pmap;
468
469 prot = (options & kIOMapReadOnly)
470 ? VM_PROT_READ : (VM_PROT_READ|VM_PROT_WRITE);
471
472 switch (options & kIOMapCacheMask)
473 {
474 /* What cache mode do we need? */
475 case kIOMapDefaultCache:
476 default:
477 return (KERN_INVALID_ARGUMENT);
478
479 case kIOMapInhibitCache:
480 flags = VM_WIMG_IO;
481 break;
482
483 case kIOMapWriteThruCache:
484 flags = VM_WIMG_WTHRU;
485 break;
c0fea474 486
5d5c5d0d
A
487 case kIOMapWriteCombineCache:
488 flags = VM_WIMG_WCOMB;
489 break;
490
491 case kIOMapCopybackCache:
492 flags = VM_WIMG_COPYBACK;
493 break;
494 }
495#if __ppc__
496 // can't remap block mappings, but ppc doesn't speculative read from WC
1c79356b 497#else
55e303ae 498
5d5c5d0d 499 // enter each page's physical address in the target map
55e303ae 500 for (off = 0; off < length; off += page_size)
5d5c5d0d
A
501 {
502 ppnum_t ppnum = pmap_find_phys(pmap, va + off);
503 if (ppnum)
504 pmap_enter(pmap, va + off, ppnum, prot, flags, TRUE);
505 }
55e303ae 506
1c79356b
A
507#endif
508
5d5c5d0d 509 return (KERN_SUCCESS);
1c79356b
A
510}
511
5d5c5d0d 512ppnum_t IOGetLastPageNumber(void)
e3027f41 513{
5d5c5d0d 514 ppnum_t lastPage, highest = 0;
e3027f41 515
5d5c5d0d
A
516#if __ppc__
517 int idx;
518 for (idx = 0; idx < pmap_mem_regions_count; idx++)
519 {
520 lastPage = pmap_mem_regions[idx].mrEnd;
521#elif __i386__
522 unsigned int idx;
523 for (idx = 0; idx < pmap_memory_region_count; idx++)
524 {
525 lastPage = pmap_memory_regions[idx].end - 1;
526#else
527#error arch
528#endif
529 if (lastPage > highest)
530 highest = lastPage;
531 }
532 return (highest);
8ad349bb 533}
c0fea474 534
5d5c5d0d 535
1c79356b
A
536void IOGetTime( mach_timespec_t * clock_time);
537void IOGetTime( mach_timespec_t * clock_time)
538{
55e303ae 539 clock_get_system_nanotime(&clock_time->tv_sec, &clock_time->tv_nsec);
1c79356b 540}
5d5c5d0d 541