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