]> git.saurik.com Git - apple/xnu.git/blob - osfmk/device/iokit_rpc.c
xnu-792.21.3.tar.gz
[apple/xnu.git] / osfmk / device / iokit_rpc.c
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
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
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
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>
38 #include <mach/mach_host_server.h>
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>
48 #include <kern/counters.h>
49 #include <kern/queue.h>
50 #include <kern/zalloc.h>
51 #include <kern/thread.h>
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
64 #include <machine/machparam.h>
65
66 #ifdef __ppc__
67 #include <ppc/mappings.h>
68 #endif
69 #ifdef __i386
70 #include <i386/pmap.h>
71 #endif
72 #include <IOKit/IOTypes.h>
73
74 #define EXTERN
75 #define MIGEXTERN
76
77 /*
78 * Functions in iokit:IOUserClient.cpp
79 */
80
81 extern void iokit_add_reference( io_object_t obj );
82
83 extern ipc_port_t iokit_port_for_object( io_object_t obj,
84 ipc_kobject_type_t type );
85
86 extern kern_return_t iokit_client_died( io_object_t obj,
87 ipc_port_t port, ipc_kobject_type_t type, mach_port_mscount_t * mscount );
88
89 extern kern_return_t
90 iokit_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
97 /*
98 * Lookup a device by its port.
99 * Doesn't consume the naked send right; produces a device reference.
100 */
101 MIGEXTERN io_object_t
102 iokit_lookup_object_port(
103 ipc_port_t port)
104 {
105 register io_object_t obj;
106
107 if (!IP_VALID(port))
108 return (NULL);
109
110 ip_lock(port);
111 if (ip_active(port) && (ip_kotype(port) == IKOT_IOKIT_OBJECT)) {
112 obj = (io_object_t) port->ip_kobject;
113 iokit_add_reference( obj );
114 }
115 else
116 obj = NULL;
117
118 ip_unlock(port);
119
120 return( obj );
121 }
122
123 MIGEXTERN io_object_t
124 iokit_lookup_connect_port(
125 ipc_port_t port)
126 {
127 register io_object_t obj;
128
129 if (!IP_VALID(port))
130 return (NULL);
131
132 ip_lock(port);
133 if (ip_active(port) && (ip_kotype(port) == IKOT_IOKIT_CONNECT)) {
134 obj = (io_object_t) port->ip_kobject;
135 iokit_add_reference( obj );
136 }
137 else
138 obj = NULL;
139
140 ip_unlock(port);
141
142 return( obj );
143 }
144
145 EXTERN io_object_t
146 iokit_lookup_connect_ref(io_object_t connectRef, ipc_space_t space)
147 {
148 io_object_t obj = NULL;
149
150 if (connectRef && MACH_PORT_VALID((mach_port_name_t)connectRef)) {
151 ipc_port_t port;
152 kern_return_t kr;
153
154 kr = ipc_object_translate(space, (mach_port_name_t)connectRef, MACH_PORT_RIGHT_SEND, (ipc_object_t *)&port);
155
156 if (kr == KERN_SUCCESS) {
157 assert(IP_VALID(port));
158
159 if (ip_active(port) && (ip_kotype(port) == IKOT_IOKIT_CONNECT)) {
160 obj = (io_object_t) port->ip_kobject;
161 iokit_add_reference(obj);
162 }
163
164 ip_unlock(port);
165 }
166 }
167
168 return obj;
169 }
170
171 EXTERN io_object_t
172 iokit_lookup_connect_ref_current_task(io_object_t connectRef)
173 {
174 return iokit_lookup_connect_ref(connectRef, current_space());
175 }
176
177 EXTERN void
178 iokit_retain_port( ipc_port_t port )
179 {
180 ipc_port_reference( port );
181 }
182
183 EXTERN void
184 iokit_release_port( ipc_port_t port )
185 {
186 ipc_port_release( port );
187 }
188
189 /*
190 * Get the port for a device.
191 * Consumes a device reference; produces a naked send right.
192 */
193 MIGEXTERN ipc_port_t
194 iokit_make_object_port(
195 io_object_t obj )
196 {
197 register ipc_port_t port;
198 register ipc_port_t sendPort;
199
200 if( obj == NULL)
201 return IP_NULL;
202
203 port = iokit_port_for_object( obj, IKOT_IOKIT_OBJECT );
204 if( port) {
205 sendPort = ipc_port_make_send( port);
206 iokit_release_port( port );
207 } else
208 sendPort = IP_NULL;
209
210 iokit_remove_reference( obj );
211
212 return( sendPort);
213 }
214
215 MIGEXTERN ipc_port_t
216 iokit_make_connect_port(
217 io_object_t obj )
218 {
219 register ipc_port_t port;
220 register ipc_port_t sendPort;
221
222 if( obj == NULL)
223 return IP_NULL;
224
225 port = iokit_port_for_object( obj, IKOT_IOKIT_CONNECT );
226 if( port) {
227 sendPort = ipc_port_make_send( port);
228 iokit_release_port( port );
229 } else
230 sendPort = IP_NULL;
231
232 iokit_remove_reference( obj );
233
234 return( sendPort);
235 }
236
237
238 EXTERN ipc_port_t
239 iokit_alloc_object_port( io_object_t obj, ipc_kobject_type_t type );
240
241 int gIOKitPortCount;
242
243 EXTERN ipc_port_t
244 iokit_alloc_object_port( io_object_t obj, ipc_kobject_type_t type )
245 {
246 ipc_port_t notify;
247 ipc_port_t port;
248
249 do {
250
251 /* Allocate port, keeping a reference for it. */
252 port = ipc_port_alloc_kernel();
253 if( port == IP_NULL)
254 continue;
255
256 /* set kobject & type */
257 // iokit_add_reference( obj );
258 ipc_kobject_set( port, (ipc_kobject_t) obj, type);
259
260 /* Request no-senders notifications on the port. */
261 notify = ipc_port_make_sonce( port);
262 ip_lock( port);
263 ipc_port_nsrequest( port, 1, notify, &notify);
264 assert( notify == IP_NULL);
265 gIOKitPortCount++;
266
267 } while( FALSE);
268
269 return( port );
270 }
271
272
273 EXTERN kern_return_t
274 iokit_destroy_object_port( ipc_port_t port )
275 {
276 ipc_kobject_set( port, IKO_NULL, IKOT_NONE);
277
278 // iokit_remove_reference( obj );
279
280 ipc_port_dealloc_kernel( port);
281 gIOKitPortCount--;
282
283 return( KERN_SUCCESS);
284 }
285
286 EXTERN kern_return_t
287 iokit_switch_object_port( ipc_port_t port, io_object_t obj, ipc_kobject_type_t type )
288 {
289 ipc_kobject_set( port, (ipc_kobject_t) obj, type);
290
291 return( KERN_SUCCESS);
292 }
293
294 EXTERN mach_port_name_t
295 iokit_make_send_right( task_t task, io_object_t obj, ipc_kobject_type_t type )
296 {
297 ipc_port_t port;
298 ipc_port_t sendPort;
299 mach_port_name_t name;
300
301 if( obj == NULL)
302 return MACH_PORT_NULL;
303
304 port = iokit_port_for_object( obj, type );
305 if( port) {
306 sendPort = ipc_port_make_send( port);
307 iokit_release_port( port );
308 } else
309 sendPort = IP_NULL;
310
311 if (IP_VALID( sendPort )) {
312 kern_return_t kr;
313 kr = ipc_object_copyout( task->itk_space, (ipc_object_t) sendPort,
314 MACH_MSG_TYPE_PORT_SEND, TRUE, &name);
315 if ( kr != KERN_SUCCESS)
316 name = MACH_PORT_NULL;
317 } else if ( sendPort == IP_NULL)
318 name = MACH_PORT_NULL;
319 else if ( sendPort == IP_DEAD)
320 name = MACH_PORT_DEAD;
321
322 iokit_remove_reference( obj );
323
324 return( name );
325 }
326
327 EXTERN kern_return_t
328 iokit_mod_send_right( task_t task, mach_port_name_t name, mach_port_delta_t delta )
329 {
330 return (mach_port_mod_refs( task->itk_space, name, MACH_PORT_RIGHT_SEND, delta ));
331 }
332
333 /*
334 * Handle the No-More_Senders notification generated from a device port destroy.
335 * Since there are no longer any tasks which hold a send right to this device
336 * port a NMS notification has been generated.
337 */
338
339 static void
340 iokit_no_senders( mach_no_senders_notification_t * notification )
341 {
342 ipc_port_t port;
343 io_object_t obj = NULL;
344 ipc_kobject_type_t type;
345 ipc_port_t notify;
346
347 port = (ipc_port_t) notification->not_header.msgh_remote_port;
348
349 // convert a port to io_object_t.
350 if( IP_VALID(port)) {
351 ip_lock(port);
352 if( ip_active(port)) {
353 obj = (io_object_t) port->ip_kobject;
354 type = ip_kotype( port );
355 if( (IKOT_IOKIT_OBJECT == type)
356 || (IKOT_IOKIT_CONNECT == type))
357 iokit_add_reference( obj );
358 else
359 obj = NULL;
360 }
361 ip_unlock(port);
362
363 if( obj ) {
364
365 mach_port_mscount_t mscount = notification->not_count;
366
367 if( KERN_SUCCESS != iokit_client_died( obj, port, type, &mscount ))
368 {
369 /* Re-request no-senders notifications on the port. */
370 notify = ipc_port_make_sonce( port);
371 ip_lock( port);
372 ipc_port_nsrequest( port, mscount + 1, notify, &notify);
373 assert( notify == IP_NULL);
374 }
375 iokit_remove_reference( obj );
376 }
377 }
378 }
379
380
381 EXTERN
382 boolean_t
383 iokit_notify( mach_msg_header_t * msg )
384 {
385 switch (msg->msgh_id) {
386 case MACH_NOTIFY_NO_SENDERS:
387 iokit_no_senders((mach_no_senders_notification_t *) msg);
388 return TRUE;
389
390 case MACH_NOTIFY_PORT_DELETED:
391 case MACH_NOTIFY_PORT_DESTROYED:
392 case MACH_NOTIFY_SEND_ONCE:
393 case MACH_NOTIFY_DEAD_NAME:
394 default:
395 printf("iokit_notify: strange notification %ld\n", msg->msgh_id);
396 return FALSE;
397 }
398 }
399
400 /* need to create a pmap function to generalize */
401 unsigned int IODefaultCacheBits(addr64_t pa)
402 {
403 unsigned int flags;
404 #ifndef i386
405 struct phys_entry * pp;
406
407 // Find physical address
408 if ((pp = pmap_find_physentry(pa >> 12))) {
409 // Use physical attributes as default
410 // NOTE: DEVICE_PAGER_FLAGS are made to line up
411 flags = VM_MEM_COHERENT; /* We only support coherent memory */
412 if(pp->ppLink & ppG) flags |= VM_MEM_GUARDED; /* Add in guarded if it is */
413 if(pp->ppLink & ppI) flags |= VM_MEM_NOT_CACHEABLE; /* Add in cache inhibited if so */
414 } else
415 // If no physical, just hard code attributes
416 flags = VM_WIMG_IO;
417 #else
418 extern pmap_paddr_t avail_end;
419
420 if (pa < avail_end)
421 flags = VM_WIMG_COPYBACK;
422 else
423 flags = VM_WIMG_IO;
424 #endif
425
426 return flags;
427 }
428
429 kern_return_t IOMapPages(vm_map_t map, vm_offset_t va, vm_offset_t pa,
430 vm_size_t length, unsigned int options)
431 {
432 vm_size_t off;
433 vm_prot_t prot;
434 unsigned int flags;
435 pmap_t pmap = map->pmap;
436
437 prot = (options & kIOMapReadOnly)
438 ? VM_PROT_READ : (VM_PROT_READ|VM_PROT_WRITE);
439
440 switch(options & kIOMapCacheMask ) { /* What cache mode do we need? */
441
442 case kIOMapDefaultCache:
443 default:
444 flags = IODefaultCacheBits(pa);
445 break;
446
447 case kIOMapInhibitCache:
448 flags = VM_WIMG_IO;
449 break;
450
451 case kIOMapWriteThruCache:
452 flags = VM_WIMG_WTHRU;
453 break;
454
455 case kIOWriteCombineCache:
456 flags = VM_WIMG_WCOMB;
457 break;
458
459 case kIOMapCopybackCache:
460 flags = VM_WIMG_COPYBACK;
461 break;
462 }
463 #if __ppc__
464
465 // Set up a block mapped area
466 pmap_map_block(pmap, (addr64_t)va, (ppnum_t)(pa >> 12), (uint32_t)(length >> 12), prot, flags, 0);
467
468 #else
469 // enter each page's physical address in the target map
470
471 for (off = 0; off < length; off += page_size)
472 pmap_enter(pmap, va + off, (pa + off) >> 12, prot, flags, TRUE);
473
474 #endif
475
476 return( KERN_SUCCESS );
477 }
478
479 kern_return_t IOUnmapPages(vm_map_t map, vm_offset_t va, vm_size_t length)
480 {
481 pmap_t pmap = map->pmap;
482
483 pmap_remove(pmap, trunc_page_64(va), round_page_64(va + length));
484
485 return( KERN_SUCCESS );
486 }
487
488 void IOGetTime( mach_timespec_t * clock_time);
489 void IOGetTime( mach_timespec_t * clock_time)
490 {
491 clock_get_system_nanotime(&clock_time->tv_sec, &clock_time->tv_nsec);
492 }