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