]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
91447636 | 2 | * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved. |
1c79356b | 3 | * |
8f6c56a5 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
1c79356b | 5 | * |
8f6c56a5 A |
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. The rights granted to you under the License | |
10 | * may not be used to create, or enable the creation or redistribution of, | |
11 | * unlawful or unlicensed copies of an Apple operating system, or to | |
12 | * circumvent, violate, or enable the circumvention or violation of, any | |
13 | * terms of an Apple operating system software license agreement. | |
14 | * | |
15 | * Please obtain a copy of the License at | |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
17 | * | |
18 | * The Original Code and all software distributed under the License are | |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. | |
23 | * Please see the License for the specific language governing rights and | |
8ad349bb | 24 | * limitations under the License. |
8f6c56a5 A |
25 | * |
26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ | |
1c79356b A |
27 | */ |
28 | /* | |
29 | * @OSF_COPYRIGHT@ | |
30 | */ | |
31 | /* | |
32 | * Mach Operating System | |
33 | * Copyright (c) 1991,1990,1989,1988 Carnegie Mellon University | |
34 | * All Rights Reserved. | |
35 | * | |
36 | * Permission to use, copy, modify and distribute this software and its | |
37 | * documentation is hereby granted, provided that both the copyright | |
38 | * notice and this permission notice appear in all copies of the | |
39 | * software, derivative works or modified versions, and any portions | |
40 | * thereof, and that both notices appear in supporting documentation. | |
41 | * | |
42 | * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" | |
43 | * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR | |
44 | * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. | |
45 | * | |
46 | * Carnegie Mellon requests users of this software to return to | |
47 | * | |
48 | * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU | |
49 | * School of Computer Science | |
50 | * Carnegie Mellon University | |
51 | * Pittsburgh PA 15213-3890 | |
52 | * | |
53 | * any improvements or extensions that they make and grant Carnegie Mellon | |
54 | * the rights to redistribute these changes. | |
55 | */ | |
56 | /* | |
57 | */ | |
58 | ||
59 | /* | |
60 | * kern/ipc_host.c | |
61 | * | |
62 | * Routines to implement host ports. | |
63 | */ | |
64 | #include <mach/message.h> | |
65 | #include <mach/mach_traps.h> | |
66 | #include <mach/mach_host_server.h> | |
91447636 | 67 | #include <mach/host_priv_server.h> |
1c79356b A |
68 | #include <kern/host.h> |
69 | #include <kern/processor.h> | |
0b4e3aa0 | 70 | #include <kern/lock.h> |
1c79356b A |
71 | #include <kern/task.h> |
72 | #include <kern/thread.h> | |
73 | #include <kern/ipc_host.h> | |
74 | #include <kern/ipc_kobject.h> | |
75 | #include <kern/misc_protos.h> | |
76 | #include <kern/spl.h> | |
77 | #include <ipc/ipc_port.h> | |
78 | #include <ipc/ipc_space.h> | |
79 | ||
80 | /* | |
81 | * Forward declarations | |
82 | */ | |
83 | ||
84 | void | |
85 | ipc_processor_terminate( | |
86 | processor_t processor); | |
87 | ||
88 | void | |
89 | ipc_processor_disable( | |
90 | processor_t processor); | |
91 | ||
92 | boolean_t | |
93 | ref_pset_port_locked( | |
94 | ipc_port_t port, boolean_t matchn, processor_set_t *ppset); | |
95 | ||
96 | /* | |
97 | * ipc_host_init: set up various things. | |
98 | */ | |
99 | ||
100 | void ipc_host_init(void) | |
101 | { | |
102 | ipc_port_t port; | |
103 | int i; | |
104 | ||
91447636 | 105 | mutex_init(&realhost.lock, 0); |
0b4e3aa0 | 106 | |
1c79356b A |
107 | /* |
108 | * Allocate and set up the two host ports. | |
109 | */ | |
110 | port = ipc_port_alloc_kernel(); | |
111 | if (port == IP_NULL) | |
112 | panic("ipc_host_init"); | |
113 | ||
55e303ae A |
114 | ipc_kobject_set(port, (ipc_kobject_t) &realhost, IKOT_HOST_SECURITY); |
115 | kernel_set_special_port(&realhost, HOST_SECURITY_PORT, | |
116 | ipc_port_make_send(port)); | |
1c79356b A |
117 | |
118 | port = ipc_port_alloc_kernel(); | |
119 | if (port == IP_NULL) | |
120 | panic("ipc_host_init"); | |
121 | ||
55e303ae A |
122 | ipc_kobject_set(port, (ipc_kobject_t) &realhost, IKOT_HOST); |
123 | kernel_set_special_port(&realhost, HOST_PORT, | |
124 | ipc_port_make_send(port)); | |
1c79356b A |
125 | |
126 | port = ipc_port_alloc_kernel(); | |
127 | if (port == IP_NULL) | |
128 | panic("ipc_host_init"); | |
129 | ||
55e303ae A |
130 | ipc_kobject_set(port, (ipc_kobject_t) &realhost, IKOT_HOST_PRIV); |
131 | kernel_set_special_port(&realhost, HOST_PRIV_PORT, | |
132 | ipc_port_make_send(port)); | |
1c79356b | 133 | |
55e303ae | 134 | /* the rest of the special ports will be set up later */ |
1c79356b A |
135 | |
136 | for (i = FIRST_EXCEPTION; i < EXC_TYPES_COUNT; i++) { | |
137 | realhost.exc_actions[i].port = IP_NULL; | |
138 | }/* for */ | |
139 | ||
140 | /* | |
141 | * Set up ipc for default processor set. | |
142 | */ | |
143 | ipc_pset_init(&default_pset); | |
144 | ipc_pset_enable(&default_pset); | |
145 | ||
146 | /* | |
147 | * And for master processor | |
148 | */ | |
149 | ipc_processor_init(master_processor); | |
150 | ipc_processor_enable(master_processor); | |
151 | } | |
152 | ||
153 | /* | |
154 | * Routine: host_self_trap [mach trap] | |
155 | * Purpose: | |
156 | * Give the caller send rights for his own host port. | |
157 | * Conditions: | |
158 | * Nothing locked. | |
159 | * Returns: | |
160 | * MACH_PORT_NULL if there are any resource failures | |
161 | * or other errors. | |
162 | */ | |
163 | ||
164 | mach_port_name_t | |
91447636 A |
165 | host_self_trap( |
166 | __unused struct host_self_trap_args *args) | |
1c79356b A |
167 | { |
168 | ipc_port_t sright; | |
91447636 | 169 | mach_port_name_t name; |
1c79356b A |
170 | |
171 | sright = ipc_port_copy_send(current_task()->itk_host); | |
91447636 A |
172 | name = ipc_port_copyout_send(sright, current_space()); |
173 | return name; | |
1c79356b A |
174 | } |
175 | ||
176 | /* | |
177 | * ipc_processor_init: | |
178 | * | |
179 | * Initialize ipc access to processor by allocating port. | |
180 | */ | |
181 | ||
182 | void | |
183 | ipc_processor_init( | |
184 | processor_t processor) | |
185 | { | |
186 | ipc_port_t port; | |
187 | ||
188 | port = ipc_port_alloc_kernel(); | |
189 | if (port == IP_NULL) | |
190 | panic("ipc_processor_init"); | |
191 | processor->processor_self = port; | |
192 | } | |
193 | ||
194 | /* | |
195 | * ipc_processor_enable: | |
196 | * | |
197 | * Enable ipc control of processor by setting port object. | |
198 | */ | |
199 | void | |
200 | ipc_processor_enable( | |
201 | processor_t processor) | |
202 | { | |
203 | ipc_port_t myport; | |
204 | ||
205 | myport = processor->processor_self; | |
206 | ipc_kobject_set(myport, (ipc_kobject_t) processor, IKOT_PROCESSOR); | |
207 | } | |
208 | ||
209 | /* | |
210 | * ipc_processor_disable: | |
211 | * | |
212 | * Disable ipc control of processor by clearing port object. | |
213 | */ | |
214 | void | |
215 | ipc_processor_disable( | |
216 | processor_t processor) | |
217 | { | |
218 | ipc_port_t myport; | |
219 | ||
220 | myport = processor->processor_self; | |
221 | if (myport == IP_NULL) | |
222 | return; | |
223 | ipc_kobject_set(myport, IKO_NULL, IKOT_NONE); | |
224 | } | |
225 | ||
226 | /* | |
227 | * ipc_processor_terminate: | |
228 | * | |
229 | * Processor is off-line. Destroy ipc control port. | |
230 | */ | |
231 | void | |
232 | ipc_processor_terminate( | |
233 | processor_t processor) | |
234 | { | |
235 | ipc_port_t myport; | |
236 | spl_t s; | |
237 | ||
238 | s = splsched(); | |
239 | processor_lock(processor); | |
240 | myport = processor->processor_self; | |
241 | if (myport == IP_NULL) { | |
242 | processor_unlock(processor); | |
243 | splx(s); | |
244 | return; | |
245 | } | |
246 | ||
247 | processor->processor_self = IP_NULL; | |
248 | processor_unlock(processor); | |
249 | splx(s); | |
250 | ||
251 | ipc_port_dealloc_kernel(myport); | |
252 | } | |
253 | ||
254 | /* | |
255 | * ipc_pset_init: | |
256 | * | |
257 | * Initialize ipc control of a processor set by allocating its ports. | |
258 | */ | |
259 | ||
260 | void | |
261 | ipc_pset_init( | |
262 | processor_set_t pset) | |
263 | { | |
264 | ipc_port_t port; | |
265 | ||
266 | port = ipc_port_alloc_kernel(); | |
267 | if (port == IP_NULL) | |
268 | panic("ipc_pset_init"); | |
269 | pset->pset_self = port; | |
270 | ||
271 | port = ipc_port_alloc_kernel(); | |
272 | if (port == IP_NULL) | |
273 | panic("ipc_pset_init"); | |
274 | pset->pset_name_self = port; | |
275 | } | |
276 | ||
277 | /* | |
278 | * ipc_pset_enable: | |
279 | * | |
280 | * Enable ipc access to a processor set. | |
281 | */ | |
282 | void | |
283 | ipc_pset_enable( | |
284 | processor_set_t pset) | |
285 | { | |
286 | pset_lock(pset); | |
287 | if (pset->active) { | |
288 | ipc_kobject_set(pset->pset_self, | |
289 | (ipc_kobject_t) pset, IKOT_PSET); | |
290 | ipc_kobject_set(pset->pset_name_self, | |
291 | (ipc_kobject_t) pset, IKOT_PSET_NAME); | |
292 | pset->ref_count += 2; | |
293 | } | |
294 | pset_unlock(pset); | |
295 | } | |
296 | ||
297 | /* | |
298 | * ipc_pset_disable: | |
299 | * | |
300 | * Disable ipc access to a processor set by clearing the port objects. | |
301 | * Caller must hold pset lock and a reference to the pset. Ok to | |
302 | * just decrement pset reference count as a result. | |
303 | */ | |
304 | void | |
305 | ipc_pset_disable( | |
306 | processor_set_t pset) | |
307 | { | |
308 | ipc_kobject_set(pset->pset_self, IKO_NULL, IKOT_NONE); | |
309 | ipc_kobject_set(pset->pset_name_self, IKO_NULL, IKOT_NONE); | |
310 | pset->ref_count -= 2; | |
311 | } | |
312 | ||
313 | /* | |
314 | * ipc_pset_terminate: | |
315 | * | |
316 | * Processor set is dead. Deallocate the ipc control structures. | |
317 | */ | |
318 | void | |
319 | ipc_pset_terminate( | |
320 | processor_set_t pset) | |
321 | { | |
322 | ipc_port_dealloc_kernel(pset->pset_self); | |
323 | ipc_port_dealloc_kernel(pset->pset_name_self); | |
324 | } | |
325 | ||
326 | /* | |
327 | * processor_set_default, processor_set_default_priv: | |
328 | * | |
329 | * Return ports for manipulating default_processor set. MiG code | |
330 | * differentiates between these two routines. | |
331 | */ | |
332 | kern_return_t | |
333 | processor_set_default( | |
334 | host_t host, | |
335 | processor_set_t *pset) | |
336 | { | |
337 | if (host == HOST_NULL) | |
338 | return(KERN_INVALID_ARGUMENT); | |
339 | ||
340 | *pset = &default_pset; | |
341 | pset_reference(*pset); | |
342 | return(KERN_SUCCESS); | |
343 | } | |
344 | ||
345 | /* | |
346 | * Routine: convert_port_to_host | |
347 | * Purpose: | |
348 | * Convert from a port to a host. | |
349 | * Doesn't consume the port ref; the host produced may be null. | |
350 | * Conditions: | |
351 | * Nothing locked. | |
352 | */ | |
353 | ||
354 | host_t | |
355 | convert_port_to_host( | |
356 | ipc_port_t port) | |
357 | { | |
358 | host_t host = HOST_NULL; | |
359 | ||
360 | if (IP_VALID(port)) { | |
361 | ip_lock(port); | |
362 | if (ip_active(port) && | |
363 | ((ip_kotype(port) == IKOT_HOST) || | |
364 | (ip_kotype(port) == IKOT_HOST_PRIV) | |
365 | )) | |
366 | host = (host_t) port->ip_kobject; | |
367 | ip_unlock(port); | |
368 | } | |
369 | ||
370 | return host; | |
371 | } | |
372 | ||
373 | /* | |
374 | * Routine: convert_port_to_host_priv | |
375 | * Purpose: | |
376 | * Convert from a port to a host. | |
377 | * Doesn't consume the port ref; the host produced may be null. | |
378 | * Conditions: | |
379 | * Nothing locked. | |
380 | */ | |
381 | ||
382 | host_t | |
383 | convert_port_to_host_priv( | |
384 | ipc_port_t port) | |
385 | { | |
386 | host_t host = HOST_NULL; | |
387 | ||
388 | if (IP_VALID(port)) { | |
389 | ip_lock(port); | |
390 | if (ip_active(port) && | |
391 | (ip_kotype(port) == IKOT_HOST_PRIV)) | |
392 | host = (host_t) port->ip_kobject; | |
393 | ip_unlock(port); | |
394 | } | |
395 | ||
396 | return host; | |
397 | } | |
398 | ||
399 | /* | |
400 | * Routine: convert_port_to_processor | |
401 | * Purpose: | |
402 | * Convert from a port to a processor. | |
403 | * Doesn't consume the port ref; | |
404 | * the processor produced may be null. | |
405 | * Conditions: | |
406 | * Nothing locked. | |
407 | */ | |
408 | ||
409 | processor_t | |
410 | convert_port_to_processor( | |
411 | ipc_port_t port) | |
412 | { | |
413 | processor_t processor = PROCESSOR_NULL; | |
414 | ||
415 | if (IP_VALID(port)) { | |
416 | ip_lock(port); | |
417 | if (ip_active(port) && | |
418 | (ip_kotype(port) == IKOT_PROCESSOR)) | |
419 | processor = (processor_t) port->ip_kobject; | |
420 | ip_unlock(port); | |
421 | } | |
422 | ||
423 | return processor; | |
424 | } | |
425 | ||
426 | /* | |
427 | * Routine: convert_port_to_pset | |
428 | * Purpose: | |
429 | * Convert from a port to a pset. | |
430 | * Doesn't consume the port ref; produces a pset ref, | |
431 | * which may be null. | |
432 | * Conditions: | |
433 | * Nothing locked. | |
434 | */ | |
435 | ||
436 | processor_set_t | |
437 | convert_port_to_pset( | |
438 | ipc_port_t port) | |
439 | { | |
440 | boolean_t r; | |
441 | processor_set_t pset = PROCESSOR_SET_NULL; | |
442 | ||
443 | r = FALSE; | |
444 | while (!r && IP_VALID(port)) { | |
445 | ip_lock(port); | |
446 | r = ref_pset_port_locked(port, FALSE, &pset); | |
447 | /* port unlocked */ | |
448 | } | |
449 | return pset; | |
450 | } | |
451 | ||
452 | /* | |
453 | * Routine: convert_port_to_pset_name | |
454 | * Purpose: | |
455 | * Convert from a port to a pset. | |
456 | * Doesn't consume the port ref; produces a pset ref, | |
457 | * which may be null. | |
458 | * Conditions: | |
459 | * Nothing locked. | |
460 | */ | |
461 | ||
462 | processor_set_name_t | |
463 | convert_port_to_pset_name( | |
464 | ipc_port_t port) | |
465 | { | |
466 | boolean_t r; | |
467 | processor_set_t pset = PROCESSOR_SET_NULL; | |
468 | ||
469 | r = FALSE; | |
470 | while (!r && IP_VALID(port)) { | |
471 | ip_lock(port); | |
472 | r = ref_pset_port_locked(port, TRUE, &pset); | |
473 | /* port unlocked */ | |
474 | } | |
475 | return pset; | |
476 | } | |
477 | ||
478 | boolean_t | |
479 | ref_pset_port_locked(ipc_port_t port, boolean_t matchn, processor_set_t *ppset) | |
480 | { | |
481 | processor_set_t pset; | |
482 | ||
483 | pset = PROCESSOR_SET_NULL; | |
484 | if (ip_active(port) && | |
485 | ((ip_kotype(port) == IKOT_PSET) || | |
486 | (matchn && (ip_kotype(port) == IKOT_PSET_NAME)))) { | |
487 | pset = (processor_set_t) port->ip_kobject; | |
488 | if (!pset_lock_try(pset)) { | |
489 | ip_unlock(port); | |
490 | mutex_pause(); | |
491 | return (FALSE); | |
492 | } | |
493 | pset->ref_count++; | |
494 | pset_unlock(pset); | |
495 | } | |
496 | *ppset = pset; | |
497 | ip_unlock(port); | |
498 | return (TRUE); | |
499 | } | |
500 | ||
501 | /* | |
502 | * Routine: convert_host_to_port | |
503 | * Purpose: | |
504 | * Convert from a host to a port. | |
505 | * Produces a naked send right which may be invalid. | |
506 | * Conditions: | |
507 | * Nothing locked. | |
508 | */ | |
509 | ||
510 | ipc_port_t | |
511 | convert_host_to_port( | |
512 | host_t host) | |
513 | { | |
514 | ipc_port_t port; | |
515 | ||
55e303ae | 516 | host_get_host_port(host, &port); |
1c79356b A |
517 | return port; |
518 | } | |
519 | ||
520 | /* | |
521 | * Routine: convert_processor_to_port | |
522 | * Purpose: | |
523 | * Convert from a processor to a port. | |
524 | * Produces a naked send right which may be invalid. | |
525 | * Conditions: | |
526 | * Nothing locked. | |
527 | */ | |
528 | ||
529 | ipc_port_t | |
530 | convert_processor_to_port( | |
531 | processor_t processor) | |
532 | { | |
533 | ipc_port_t port; | |
534 | spl_t s; | |
535 | ||
536 | s = splsched(); | |
537 | processor_lock(processor); | |
538 | ||
539 | if (processor->processor_self != IP_NULL) | |
540 | port = ipc_port_make_send(processor->processor_self); | |
541 | else | |
542 | port = IP_NULL; | |
543 | ||
544 | processor_unlock(processor); | |
545 | splx(s); | |
546 | ||
547 | return port; | |
548 | } | |
549 | ||
550 | /* | |
551 | * Routine: convert_pset_to_port | |
552 | * Purpose: | |
553 | * Convert from a pset to a port. | |
554 | * Consumes a pset ref; produces a naked send right | |
555 | * which may be invalid. | |
556 | * Conditions: | |
557 | * Nothing locked. | |
558 | */ | |
559 | ||
560 | ipc_port_t | |
561 | convert_pset_to_port( | |
562 | processor_set_t pset) | |
563 | { | |
564 | ipc_port_t port; | |
565 | ||
566 | pset_lock(pset); | |
567 | if (pset->active) | |
568 | port = ipc_port_make_send(pset->pset_self); | |
569 | else | |
570 | port = IP_NULL; | |
571 | pset_unlock(pset); | |
572 | ||
573 | pset_deallocate(pset); | |
574 | return port; | |
575 | } | |
576 | ||
577 | /* | |
578 | * Routine: convert_pset_name_to_port | |
579 | * Purpose: | |
580 | * Convert from a pset to a port. | |
581 | * Consumes a pset ref; produces a naked send right | |
582 | * which may be invalid. | |
583 | * Conditions: | |
584 | * Nothing locked. | |
585 | */ | |
586 | ||
587 | ipc_port_t | |
588 | convert_pset_name_to_port( | |
589 | processor_set_name_t pset) | |
590 | { | |
591 | ipc_port_t port; | |
592 | ||
593 | pset_lock(pset); | |
594 | if (pset->active) | |
595 | port = ipc_port_make_send(pset->pset_name_self); | |
596 | else | |
597 | port = IP_NULL; | |
598 | pset_unlock(pset); | |
599 | ||
600 | pset_deallocate(pset); | |
601 | return port; | |
602 | } | |
603 | ||
604 | /* | |
605 | * Routine: convert_port_to_host_security | |
606 | * Purpose: | |
607 | * Convert from a port to a host security. | |
608 | * Doesn't consume the port ref; the port produced may be null. | |
609 | * Conditions: | |
610 | * Nothing locked. | |
611 | */ | |
612 | ||
613 | host_t | |
614 | convert_port_to_host_security( | |
615 | ipc_port_t port) | |
616 | { | |
617 | host_t host = HOST_NULL; | |
618 | ||
619 | if (IP_VALID(port)) { | |
620 | ip_lock(port); | |
621 | if (ip_active(port) && | |
622 | (ip_kotype(port) == IKOT_HOST_SECURITY)) | |
623 | host = (host_t) port->ip_kobject; | |
624 | ip_unlock(port); | |
625 | } | |
626 | ||
627 | return host; | |
628 | } | |
629 | ||
630 | /* | |
631 | * Routine: host_set_exception_ports [kernel call] | |
632 | * Purpose: | |
633 | * Sets the host exception port, flavor and | |
634 | * behavior for the exception types specified by the mask. | |
635 | * There will be one send right per exception per valid | |
636 | * port. | |
637 | * Conditions: | |
638 | * Nothing locked. If successful, consumes | |
639 | * the supplied send right. | |
640 | * Returns: | |
641 | * KERN_SUCCESS Changed the special port. | |
642 | * KERN_INVALID_ARGUMENT The host_priv is not valid, | |
643 | * Illegal mask bit set. | |
644 | * Illegal exception behavior | |
645 | */ | |
646 | kern_return_t | |
647 | host_set_exception_ports( | |
648 | host_priv_t host_priv, | |
649 | exception_mask_t exception_mask, | |
650 | ipc_port_t new_port, | |
651 | exception_behavior_t new_behavior, | |
652 | thread_state_flavor_t new_flavor) | |
653 | { | |
654 | register int i; | |
655 | ipc_port_t old_port[EXC_TYPES_COUNT]; | |
656 | ||
657 | if (host_priv == HOST_PRIV_NULL) { | |
658 | return KERN_INVALID_ARGUMENT; | |
659 | } | |
660 | ||
661 | assert(host_priv == &realhost); | |
662 | ||
663 | if (exception_mask & ~EXC_MASK_ALL) { | |
664 | return KERN_INVALID_ARGUMENT; | |
665 | } | |
666 | ||
667 | if (IP_VALID(new_port)) { | |
668 | switch (new_behavior) { | |
669 | case EXCEPTION_DEFAULT: | |
670 | case EXCEPTION_STATE: | |
671 | case EXCEPTION_STATE_IDENTITY: | |
672 | break; | |
673 | default: | |
674 | return KERN_INVALID_ARGUMENT; | |
675 | } | |
676 | } | |
677 | /* Cannot easily check "new_flavor", but that just means that | |
678 | * the flavor in the generated exception message might be garbage: | |
679 | * GIGO | |
680 | */ | |
681 | host_lock(host_priv); | |
682 | ||
683 | for (i = FIRST_EXCEPTION; i < EXC_TYPES_COUNT; i++) { | |
684 | if (exception_mask & (1 << i)) { | |
685 | old_port[i] = host_priv->exc_actions[i].port; | |
686 | host_priv->exc_actions[i].port = | |
687 | ipc_port_copy_send(new_port); | |
688 | host_priv->exc_actions[i].behavior = new_behavior; | |
689 | host_priv->exc_actions[i].flavor = new_flavor; | |
690 | } else | |
691 | old_port[i] = IP_NULL; | |
692 | }/* for */ | |
693 | ||
694 | /* | |
695 | * Consume send rights without any lock held. | |
696 | */ | |
697 | host_unlock(host_priv); | |
698 | for (i = FIRST_EXCEPTION; i < EXC_TYPES_COUNT; i++) | |
699 | if (IP_VALID(old_port[i])) | |
700 | ipc_port_release_send(old_port[i]); | |
701 | if (IP_VALID(new_port)) /* consume send right */ | |
702 | ipc_port_release_send(new_port); | |
703 | ||
704 | return KERN_SUCCESS; | |
705 | } | |
706 | ||
707 | /* | |
708 | * Routine: host_get_exception_ports [kernel call] | |
709 | * Purpose: | |
710 | * Clones a send right for each of the host's exception | |
711 | * ports specified in the mask and returns the behaviour | |
712 | * and flavor of said port. | |
713 | * | |
714 | * Returns upto [in} CountCnt elements. | |
715 | * | |
716 | * Conditions: | |
717 | * Nothing locked. | |
718 | * Returns: | |
719 | * KERN_SUCCESS Extracted a send right. | |
720 | * KERN_INVALID_ARGUMENT Invalid host_priv specified, | |
721 | * Invalid special port, | |
722 | * Illegal mask bit set. | |
723 | * KERN_FAILURE The thread is dead. | |
724 | */ | |
725 | kern_return_t | |
726 | host_get_exception_ports( | |
727 | host_priv_t host_priv, | |
728 | exception_mask_t exception_mask, | |
729 | exception_mask_array_t masks, | |
730 | mach_msg_type_number_t * CountCnt, | |
731 | exception_port_array_t ports, | |
732 | exception_behavior_array_t behaviors, | |
733 | thread_state_flavor_array_t flavors ) | |
734 | { | |
91447636 | 735 | unsigned int i, j, count; |
1c79356b A |
736 | |
737 | if (host_priv == HOST_PRIV_NULL) | |
738 | return KERN_INVALID_ARGUMENT; | |
739 | ||
740 | if (exception_mask & ~EXC_MASK_ALL) { | |
741 | return KERN_INVALID_ARGUMENT; | |
742 | } | |
743 | ||
744 | assert (host_priv == &realhost); | |
745 | ||
746 | host_lock(host_priv); | |
747 | ||
748 | count = 0; | |
749 | ||
750 | for (i = FIRST_EXCEPTION; i < EXC_TYPES_COUNT; i++) { | |
751 | if (exception_mask & (1 << i)) { | |
752 | for (j = 0; j < count; j++) { | |
753 | /* | |
754 | * search for an identical entry, if found | |
755 | * set corresponding mask for this exception. | |
756 | */ | |
757 | if (host_priv->exc_actions[i].port == ports[j] && | |
758 | host_priv->exc_actions[i].behavior == behaviors[j] | |
759 | && host_priv->exc_actions[i].flavor == flavors[j]) | |
760 | { | |
761 | masks[j] |= (1 << i); | |
762 | break; | |
763 | } | |
764 | }/* for */ | |
765 | if (j == count) { | |
766 | masks[j] = (1 << i); | |
767 | ports[j] = | |
768 | ipc_port_copy_send(host_priv->exc_actions[i].port); | |
769 | behaviors[j] = host_priv->exc_actions[i].behavior; | |
770 | flavors[j] = host_priv->exc_actions[i].flavor; | |
771 | count++; | |
772 | if (count > *CountCnt) { | |
773 | break; | |
774 | } | |
775 | } | |
776 | } | |
777 | }/* for */ | |
778 | host_unlock(host_priv); | |
779 | ||
780 | *CountCnt = count; | |
781 | return KERN_SUCCESS; | |
782 | } | |
783 | ||
784 | kern_return_t | |
785 | host_swap_exception_ports( | |
786 | host_priv_t host_priv, | |
787 | exception_mask_t exception_mask, | |
788 | ipc_port_t new_port, | |
789 | exception_behavior_t new_behavior, | |
790 | thread_state_flavor_t new_flavor, | |
791 | exception_mask_array_t masks, | |
792 | mach_msg_type_number_t * CountCnt, | |
793 | exception_port_array_t ports, | |
794 | exception_behavior_array_t behaviors, | |
795 | thread_state_flavor_array_t flavors ) | |
796 | { | |
91447636 | 797 | unsigned int i, |
1c79356b A |
798 | j, |
799 | count; | |
800 | ipc_port_t old_port[EXC_TYPES_COUNT]; | |
801 | ||
802 | if (host_priv == HOST_PRIV_NULL) | |
803 | return KERN_INVALID_ARGUMENT; | |
804 | ||
805 | if (exception_mask & ~EXC_MASK_ALL) { | |
806 | return KERN_INVALID_ARGUMENT; | |
807 | } | |
808 | ||
809 | if (IP_VALID(new_port)) { | |
810 | switch (new_behavior) { | |
811 | case EXCEPTION_DEFAULT: | |
812 | case EXCEPTION_STATE: | |
813 | case EXCEPTION_STATE_IDENTITY: | |
814 | break; | |
815 | default: | |
816 | return KERN_INVALID_ARGUMENT; | |
817 | } | |
818 | } | |
819 | /* Cannot easily check "new_flavor", but that just means that | |
820 | * the flavor in the generated exception message might be garbage: | |
821 | * GIGO */ | |
822 | ||
823 | host_lock(host_priv); | |
824 | ||
825 | count = 0; | |
826 | ||
827 | for (i = FIRST_EXCEPTION; i < EXC_TYPES_COUNT; i++) { | |
828 | if (exception_mask & (1 << i)) { | |
829 | for (j = 0; j < count; j++) { | |
830 | /* | |
831 | * search for an identical entry, if found | |
832 | * set corresponding mask for this exception. | |
833 | */ | |
834 | if (host_priv->exc_actions[i].port == ports[j] && | |
835 | host_priv->exc_actions[i].behavior == behaviors[j] | |
836 | && host_priv->exc_actions[i].flavor == flavors[j]) | |
837 | { | |
838 | masks[j] |= (1 << i); | |
839 | break; | |
840 | } | |
841 | }/* for */ | |
842 | if (j == count) { | |
843 | masks[j] = (1 << i); | |
844 | ports[j] = | |
845 | ipc_port_copy_send(host_priv->exc_actions[i].port); | |
846 | behaviors[j] = host_priv->exc_actions[i].behavior; | |
847 | flavors[j] = host_priv->exc_actions[i].flavor; | |
848 | count++; | |
849 | } | |
850 | old_port[i] = host_priv->exc_actions[i].port; | |
851 | host_priv->exc_actions[i].port = | |
852 | ipc_port_copy_send(new_port); | |
853 | host_priv->exc_actions[i].behavior = new_behavior; | |
854 | host_priv->exc_actions[i].flavor = new_flavor; | |
855 | if (count > *CountCnt) { | |
856 | break; | |
857 | } | |
858 | } else | |
859 | old_port[i] = IP_NULL; | |
860 | }/* for */ | |
861 | host_unlock(host_priv); | |
862 | ||
863 | /* | |
864 | * Consume send rights without any lock held. | |
865 | */ | |
866 | for (i = FIRST_EXCEPTION; i < EXC_TYPES_COUNT; i++) | |
867 | if (IP_VALID(old_port[i])) | |
868 | ipc_port_release_send(old_port[i]); | |
869 | if (IP_VALID(new_port)) /* consume send right */ | |
870 | ipc_port_release_send(new_port); | |
871 | *CountCnt = count; | |
872 | ||
873 | return KERN_SUCCESS; | |
874 | } |