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