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