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