]> git.saurik.com Git - apple/xnu.git/blame - osfmk/device/iokit_rpc.c
xnu-517.11.1.tar.gz
[apple/xnu.git] / osfmk / device / iokit_rpc.c
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
e5568f75
A
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.
1c79356b 11 *
e5568f75
A
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
1c79356b
A
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
e5568f75
A
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.
1c79356b
A
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/ast.h>
43#include <kern/counters.h>
44#include <kern/queue.h>
45#include <kern/zalloc.h>
46#include <kern/thread.h>
47#include <kern/thread_swap.h>
48#include <kern/task.h>
49#include <kern/sched_prim.h>
50#include <kern/misc_protos.h>
51
52#include <vm/pmap.h>
53#include <vm/vm_map.h>
54#include <vm/vm_kern.h>
55
56#include <device/device_types.h>
57#include <device/device_port.h>
58#include <device/device_server.h>
59
1c79356b
A
60#include <machine/machparam.h>
61
62#ifdef __ppc__
63#include <ppc/mappings.h>
1c79356b
A
64#endif
65#include <IOKit/IOTypes.h>
66
67#define EXTERN
68#define MIGEXTERN
69
70/*
71 * Functions in iokit:IOUserClient.cpp
72 */
73
74extern void iokit_add_reference( io_object_t obj );
75
76extern void iokit_remove_reference( io_object_t obj );
77
78extern ipc_port_t iokit_port_for_object( io_object_t obj,
79 ipc_kobject_type_t type );
80
81extern kern_return_t iokit_client_died( io_object_t obj,
55e303ae 82 ipc_port_t port, ipc_kobject_type_t type, mach_port_mscount_t * mscount );
1c79356b
A
83
84extern kern_return_t
85iokit_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 */
96MIGEXTERN io_object_t
97iokit_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
118MIGEXTERN io_object_t
119iokit_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
140EXTERN io_object_t
141iokit_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
166EXTERN io_object_t
167iokit_lookup_connect_ref_current_task(io_object_t connectRef)
168{
169 return iokit_lookup_connect_ref(connectRef, current_space());
170}
171
9bccf70c
A
172EXTERN void
173iokit_retain_port( ipc_port_t port )
174{
175 ipc_port_reference( port );
176}
177
178EXTERN void
179iokit_release_port( ipc_port_t port )
180{
181 ipc_port_release( port );
182}
183
1c79356b
A
184/*
185 * Get the port for a device.
186 * Consumes a device reference; produces a naked send right.
187 */
188MIGEXTERN ipc_port_t
189iokit_make_object_port(
190 io_object_t obj )
191{
192 register ipc_port_t port;
9bccf70c 193 register ipc_port_t sendPort;
1c79356b
A
194
195 if( obj == NULL)
196 return IP_NULL;
197
198 port = iokit_port_for_object( obj, IKOT_IOKIT_OBJECT );
9bccf70c
A
199 if( port) {
200 sendPort = ipc_port_make_send( port);
201 iokit_release_port( port );
202 } else
203 sendPort = IP_NULL;
1c79356b
A
204
205 iokit_remove_reference( obj );
206
9bccf70c 207 return( sendPort);
1c79356b
A
208}
209
210MIGEXTERN ipc_port_t
211iokit_make_connect_port(
212 io_object_t obj )
213{
214 register ipc_port_t port;
9bccf70c 215 register ipc_port_t sendPort;
1c79356b
A
216
217 if( obj == NULL)
218 return IP_NULL;
219
220 port = iokit_port_for_object( obj, IKOT_IOKIT_CONNECT );
9bccf70c
A
221 if( port) {
222 sendPort = ipc_port_make_send( port);
223 iokit_release_port( port );
224 } else
225 sendPort = IP_NULL;
1c79356b
A
226
227 iokit_remove_reference( obj );
228
9bccf70c 229 return( sendPort);
1c79356b
A
230}
231
232
233EXTERN ipc_port_t
234iokit_alloc_object_port( io_object_t obj, ipc_kobject_type_t type );
235
236int gIOKitPortCount;
237
238EXTERN ipc_port_t
239iokit_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
268EXTERN kern_return_t
269iokit_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
43866e37
A
281EXTERN kern_return_t
282iokit_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
1c79356b
A
289EXTERN mach_port_name_t
290iokit_make_send_right( task_t task, io_object_t obj, ipc_kobject_type_t type )
291{
1c79356b 292 ipc_port_t port;
9bccf70c 293 ipc_port_t sendPort;
1c79356b
A
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 );
9bccf70c
A
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,
1c79356b 309 MACH_MSG_TYPE_PORT_SEND, TRUE, &name);
9bccf70c
A
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;
1c79356b
A
316
317 iokit_remove_reference( obj );
318
319 return( name );
320}
321
322/*
323 * Handle the No-More_Senders notification generated from a device port destroy.
324 * Since there are no longer any tasks which hold a send right to this device
325 * port a NMS notification has been generated.
326 */
327
328static void
329iokit_no_senders( mach_no_senders_notification_t * notification )
330{
331 ipc_port_t port;
332 io_object_t obj = NULL;
333 ipc_kobject_type_t type;
9bccf70c 334 ipc_port_t notify;
1c79356b
A
335
336 port = (ipc_port_t) notification->not_header.msgh_remote_port;
337
338 // convert a port to io_object_t.
339 if( IP_VALID(port)) {
340 ip_lock(port);
341 if( ip_active(port)) {
342 obj = (io_object_t) port->ip_kobject;
343 type = ip_kotype( port );
344 if( (IKOT_IOKIT_OBJECT == type)
345 || (IKOT_IOKIT_CONNECT == type))
346 iokit_add_reference( obj );
e3027f41 347 else
1c79356b
A
348 obj = NULL;
349 }
350 ip_unlock(port);
351
352 if( obj ) {
9bccf70c
A
353
354 mach_port_mscount_t mscount = notification->not_count;
355
356 if( KERN_SUCCESS != iokit_client_died( obj, port, type, &mscount ))
357 {
358 /* Re-request no-senders notifications on the port. */
359 notify = ipc_port_make_sonce( port);
360 ip_lock( port);
361 ipc_port_nsrequest( port, mscount + 1, notify, &notify);
362 assert( notify == IP_NULL);
363 }
1c79356b
A
364 iokit_remove_reference( obj );
365 }
366 }
367}
368
369
370EXTERN
371boolean_t
372iokit_notify( mach_msg_header_t * msg )
373{
374 switch (msg->msgh_id) {
375 case MACH_NOTIFY_NO_SENDERS:
376 iokit_no_senders((mach_no_senders_notification_t *) msg);
377 return TRUE;
378
379 case MACH_NOTIFY_PORT_DELETED:
380 case MACH_NOTIFY_PORT_DESTROYED:
381 case MACH_NOTIFY_SEND_ONCE:
382 case MACH_NOTIFY_DEAD_NAME:
383 default:
384 printf("iokit_notify: strange notification %ld\n", msg->msgh_id);
385 return FALSE;
386 }
387}
388
55e303ae
A
389/* need to create a pmap function to generalize */
390unsigned int IODefaultCacheBits(addr64_t pa)
de355530 391{
55e303ae
A
392 unsigned int flags;
393#ifndef i386
394 struct phys_entry * pp;
395
396 // Find physical address
397 if ((pp = pmap_find_physentry(pa >> 12))) {
398 // Use physical attributes as default
399 // NOTE: DEVICE_PAGER_FLAGS are made to line up
400 flags = VM_MEM_COHERENT; /* We only support coherent memory */
401 if(pp->ppLink & ppG) flags |= VM_MEM_GUARDED; /* Add in guarded if it is */
402 if(pp->ppLink & ppI) flags |= VM_MEM_NOT_CACHEABLE; /* Add in cache inhibited if so */
403 } else
404 // If no physical, just hard code attributes
405 flags = VM_WIMG_IO;
406#else
407 extern vm_offset_t avail_end;
9bccf70c 408
55e303ae
A
409 if (pa < avail_end)
410 flags = VM_WIMG_COPYBACK;
411 else
412 flags = VM_WIMG_IO;
413#endif
9bccf70c 414
55e303ae 415 return flags;
9bccf70c 416}
9bccf70c 417
1c79356b
A
418kern_return_t IOMapPages(vm_map_t map, vm_offset_t va, vm_offset_t pa,
419 vm_size_t length, unsigned int options)
420{
421 vm_size_t off;
422 vm_prot_t prot;
55e303ae
A
423 unsigned int flags;
424 pmap_t pmap = map->pmap;
1c79356b
A
425
426 prot = (options & kIOMapReadOnly)
427 ? VM_PROT_READ : (VM_PROT_READ|VM_PROT_WRITE);
428
55e303ae
A
429 switch(options & kIOMapCacheMask ) { /* What cache mode do we need? */
430
431 case kIOMapDefaultCache:
432 default:
433 flags = IODefaultCacheBits(pa);
434 break;
435
436 case kIOMapInhibitCache:
437 flags = VM_WIMG_IO;
438 break;
439
440 case kIOMapWriteThruCache:
441 flags = VM_WIMG_WTHRU;
442 break;
443
444 case kIOWriteCombineCache:
445 flags = VM_WIMG_WCOMB;
446 break;
447
448 case kIOMapCopybackCache:
449 flags = VM_WIMG_COPYBACK;
450 break;
451 }
1c79356b
A
452#if __ppc__
453
55e303ae
A
454 // Set up a block mapped area
455 pmap_map_block(pmap, (addr64_t)va, (ppnum_t)(pa >> 12), length, prot, flags, 0);
1c79356b 456
1c79356b 457#else
55e303ae
A
458// enter each page's physical address in the target map
459
460 for (off = 0; off < length; off += page_size)
461 pmap_enter(pmap, va + off, (pa + off) >> 12, prot, flags, TRUE);
462
1c79356b
A
463#endif
464
465 return( KERN_SUCCESS );
466}
467
e3027f41
A
468kern_return_t IOUnmapPages(vm_map_t map, vm_offset_t va, vm_size_t length)
469{
470 pmap_t pmap = map->pmap;
e3027f41 471
55e303ae 472 pmap_remove(pmap, trunc_page_64(va), round_page_64(va + length));
e3027f41 473
9bccf70c 474 return( KERN_SUCCESS );
e3027f41
A
475}
476
1c79356b
A
477void IOGetTime( mach_timespec_t * clock_time);
478void IOGetTime( mach_timespec_t * clock_time)
479{
55e303ae 480 clock_get_system_nanotime(&clock_time->tv_sec, &clock_time->tv_nsec);
1c79356b 481}