2 * Copyright (c) 2000-2016 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Mach Operating System
33 * Copyright (c) 1991,1990 Carnegie Mellon University
34 * All Rights Reserved.
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.
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.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
57 #include <mach_debug.h>
58 #include <mach_ldebug.h>
60 #include <sys/kdebug.h>
62 #include <mach/kern_return.h>
63 #include <mach/thread_status.h>
64 #include <mach/vm_param.h>
66 #include <kern/counters.h>
67 #include <kern/kalloc.h>
68 #include <kern/mach_param.h>
69 #include <kern/processor.h>
70 #include <kern/cpu_data.h>
71 #include <kern/cpu_number.h>
72 #include <kern/task.h>
73 #include <kern/thread.h>
74 #include <kern/sched_prim.h>
75 #include <kern/misc_protos.h>
76 #include <kern/assert.h>
78 #include <kern/machine.h>
80 #include <ipc/ipc_port.h>
81 #include <vm/vm_kern.h>
82 #include <vm/vm_map.h>
84 #include <vm/vm_protos.h>
86 #include <i386/cpu_data.h>
87 #include <i386/cpu_number.h>
88 #include <i386/eflags.h>
89 #include <i386/proc_reg.h>
91 #include <i386/misc_protos.h>
92 #include <i386/mp_desc.h>
93 #include <i386/thread.h>
94 #include <i386/machine_routines.h>
95 #include <i386/lapic.h> /* LAPIC_PMC_SWI_VECTOR */
98 #include <kern/hv_support.h>
102 * Maps state flavor to number of words in the state:
104 unsigned int _MachineStateCount
[] = {
105 [x86_THREAD_STATE32
] = x86_THREAD_STATE32_COUNT
,
106 [x86_THREAD_STATE64
] = x86_THREAD_STATE64_COUNT
,
107 [x86_THREAD_STATE
] = x86_THREAD_STATE_COUNT
,
108 [x86_FLOAT_STATE32
] = x86_FLOAT_STATE32_COUNT
,
109 [x86_FLOAT_STATE64
] = x86_FLOAT_STATE64_COUNT
,
110 [x86_FLOAT_STATE
] = x86_FLOAT_STATE_COUNT
,
111 [x86_EXCEPTION_STATE32
] = x86_EXCEPTION_STATE32_COUNT
,
112 [x86_EXCEPTION_STATE64
] = x86_EXCEPTION_STATE64_COUNT
,
113 [x86_EXCEPTION_STATE
] = x86_EXCEPTION_STATE_COUNT
,
114 [x86_DEBUG_STATE32
] = x86_DEBUG_STATE32_COUNT
,
115 [x86_DEBUG_STATE64
] = x86_DEBUG_STATE64_COUNT
,
116 [x86_DEBUG_STATE
] = x86_DEBUG_STATE_COUNT
,
117 [x86_AVX_STATE32
] = x86_AVX_STATE32_COUNT
,
118 [x86_AVX_STATE64
] = x86_AVX_STATE64_COUNT
,
119 [x86_AVX_STATE
] = x86_AVX_STATE_COUNT
,
120 #if !defined(RC_HIDE_XNU_J137)
121 [x86_AVX512_STATE32
] = x86_AVX512_STATE32_COUNT
,
122 [x86_AVX512_STATE64
] = x86_AVX512_STATE64_COUNT
,
123 [x86_AVX512_STATE
] = x86_AVX512_STATE_COUNT
,
124 #endif /* not RC_HIDE_XNU_J137 */
127 zone_t iss_zone
; /* zone for saved_state area */
128 zone_t ids_zone
; /* zone for debug_state area */
132 extern void Thread_continue(void);
133 extern void Load_context(
134 thread_t thread
) __attribute__((noreturn
));
137 get_exception_state32(thread_t thread
, x86_exception_state32_t
*es
);
140 get_exception_state64(thread_t thread
, x86_exception_state64_t
*es
);
143 get_thread_state32(thread_t thread
, x86_thread_state32_t
*ts
);
146 get_thread_state64(thread_t thread
, x86_thread_state64_t
*ts
);
149 set_thread_state32(thread_t thread
, x86_thread_state32_t
*ts
);
152 set_thread_state64(thread_t thread
, x86_thread_state64_t
*ts
);
156 ml_hv_cswitch(thread_t old
, thread_t
new)
158 if (old
->hv_thread_target
)
159 hv_callbacks
.preempt(old
->hv_thread_target
);
161 if (new->hv_thread_target
)
162 hv_callbacks
.dispatch(new->hv_thread_target
);
167 * Don't let an illegal value for the lower 32-bits of dr7 get set.
168 * Specifically, check for undefined settings. Setting these bit patterns
169 * result in undefined behaviour and can lead to an unexpected
173 dr7d_is_valid(uint32_t *dr7d
)
176 uint32_t mask1
, mask2
;
179 * If the DE bit is set in CR4, R/W0-3 can be pattern
180 * "10B" to indicate i/o reads and write
182 if (!(get_cr4() & CR4_DE
))
183 for (i
= 0, mask1
= 0x3<<16, mask2
= 0x2<<16; i
< 4;
184 i
++, mask1
<<= 4, mask2
<<= 4)
185 if ((*dr7d
& mask1
) == mask2
)
189 * if we are doing an instruction execution break (indicated
190 * by r/w[x] being "00B"), then the len[x] must also be set
193 for (i
= 0; i
< 4; i
++)
194 if (((((*dr7d
>> (16 + i
*4))) & 0x3) == 0) &&
195 ((((*dr7d
>> (18 + i
*4))) & 0x3) != 0))
199 * Intel docs have these bits fixed.
201 *dr7d
|= 0x1 << 10; /* set bit 10 to 1 */
202 *dr7d
&= ~(0x1 << 11); /* set bit 11 to 0 */
203 *dr7d
&= ~(0x1 << 12); /* set bit 12 to 0 */
204 *dr7d
&= ~(0x1 << 14); /* set bit 14 to 0 */
205 *dr7d
&= ~(0x1 << 15); /* set bit 15 to 0 */
208 * We don't allow anything to set the global breakpoints.
214 if (*dr7d
& (0x2<<2))
217 if (*dr7d
& (0x2<<4))
220 if (*dr7d
& (0x2<<6))
226 extern void set_64bit_debug_regs(x86_debug_state64_t
*ds
);
229 debug_state_is_valid32(x86_debug_state32_t
*ds
)
231 if (!dr7d_is_valid(&ds
->dr7
))
238 debug_state_is_valid64(x86_debug_state64_t
*ds
)
240 if (!dr7d_is_valid((uint32_t *)&ds
->dr7
))
244 * Don't allow the user to set debug addresses above their max
248 if (ds
->dr0
>= VM_MAX_PAGE_ADDRESS
)
251 if (ds
->dr7
& (0x1<<2))
252 if (ds
->dr1
>= VM_MAX_PAGE_ADDRESS
)
255 if (ds
->dr7
& (0x1<<4))
256 if (ds
->dr2
>= VM_MAX_PAGE_ADDRESS
)
259 if (ds
->dr7
& (0x1<<6))
260 if (ds
->dr3
>= VM_MAX_PAGE_ADDRESS
)
263 /* For x86-64, we must ensure the upper 32-bits of DR7 are clear */
264 ds
->dr7
&= 0xffffffffULL
;
271 set_debug_state32(thread_t thread
, x86_debug_state32_t
*ds
)
273 x86_debug_state32_t
*new_ids
;
276 pcb
= THREAD_TO_PCB(thread
);
278 if (debug_state_is_valid32(ds
) != TRUE
) {
279 return KERN_INVALID_ARGUMENT
;
282 if (pcb
->ids
== NULL
) {
283 new_ids
= zalloc(ids_zone
);
284 bzero(new_ids
, sizeof *new_ids
);
286 simple_lock(&pcb
->lock
);
287 /* make sure it wasn't already alloc()'d elsewhere */
288 if (pcb
->ids
== NULL
) {
290 simple_unlock(&pcb
->lock
);
292 simple_unlock(&pcb
->lock
);
293 zfree(ids_zone
, new_ids
);
298 copy_debug_state32(ds
, pcb
->ids
, FALSE
);
300 return (KERN_SUCCESS
);
304 set_debug_state64(thread_t thread
, x86_debug_state64_t
*ds
)
306 x86_debug_state64_t
*new_ids
;
309 pcb
= THREAD_TO_PCB(thread
);
311 if (debug_state_is_valid64(ds
) != TRUE
) {
312 return KERN_INVALID_ARGUMENT
;
315 if (pcb
->ids
== NULL
) {
316 new_ids
= zalloc(ids_zone
);
317 bzero(new_ids
, sizeof *new_ids
);
320 if (thread
->hv_thread_target
) {
321 hv_callbacks
.volatile_state(thread
->hv_thread_target
,
326 simple_lock(&pcb
->lock
);
327 /* make sure it wasn't already alloc()'d elsewhere */
328 if (pcb
->ids
== NULL
) {
330 simple_unlock(&pcb
->lock
);
332 simple_unlock(&pcb
->lock
);
333 zfree(ids_zone
, new_ids
);
337 copy_debug_state64(ds
, pcb
->ids
, FALSE
);
339 return (KERN_SUCCESS
);
343 get_debug_state32(thread_t thread
, x86_debug_state32_t
*ds
)
345 x86_debug_state32_t
*saved_state
;
347 saved_state
= thread
->machine
.ids
;
350 copy_debug_state32(saved_state
, ds
, TRUE
);
352 bzero(ds
, sizeof *ds
);
356 get_debug_state64(thread_t thread
, x86_debug_state64_t
*ds
)
358 x86_debug_state64_t
*saved_state
;
360 saved_state
= (x86_debug_state64_t
*)thread
->machine
.ids
;
363 copy_debug_state64(saved_state
, ds
, TRUE
);
365 bzero(ds
, sizeof *ds
);
369 * consider_machine_collect:
371 * Try to collect machine-dependent pages
374 consider_machine_collect(void)
379 consider_machine_adjust(void)
384 * Switch to the first thread on a CPU.
387 machine_load_context(
390 new->machine
.specFlags
|= OnProc
;
391 act_machine_switch_pcb(NULL
, new);
395 static inline void pmap_switch_context(thread_t ot
, thread_t nt
, int cnum
) {
396 pmap_assert(ml_get_interrupts_enabled() == FALSE
);
397 vm_map_t nmap
= nt
->map
, omap
= ot
->map
;
398 if ((omap
!= nmap
) || (nmap
->pmap
->pagezero_accessible
)) {
399 PMAP_DEACTIVATE_MAP(omap
, ot
, cnum
);
400 PMAP_ACTIVATE_MAP(nmap
, nt
, cnum
);
405 * Switch to a new thread.
406 * Save the old thread`s kernel state or continuation,
410 machine_switch_context(
412 thread_continue_t continuation
,
415 assert(current_cpu_datap()->cpu_active_stack
== old
->kernel_stack
);
422 * Save FP registers if in use.
424 fpu_switch_context(old
, new);
426 old
->machine
.specFlags
&= ~OnProc
;
427 new->machine
.specFlags
|= OnProc
;
430 * Monitor the stack depth and report new max,
431 * not worrying about races.
433 vm_offset_t depth
= current_stack_depth();
434 if (depth
> kernel_stack_depth_max
) {
435 kernel_stack_depth_max
= depth
;
436 KERNEL_DEBUG_CONSTANT(
437 MACHDBG_CODE(DBG_MACH_SCHED
, MACH_STACK_DEPTH
),
438 (long) depth
, 0, 0, 0, 0);
442 * Switch address maps if need be, even if not switching tasks.
443 * (A server activation may be "borrowing" a client map.)
445 pmap_switch_context(old
, new, cpu_number());
448 * Load the rest of the user state for the new thread
450 act_machine_switch_pcb(old
, new);
453 ml_hv_cswitch(old
, new);
456 return(Switch_context(old
, continuation
, new));
460 machine_processor_shutdown(
462 void (*doshutdown
)(processor_t
),
463 processor_t processor
)
468 fpu_switch_context(thread
, NULL
);
469 pmap_switch_context(thread
, processor
->idle_thread
, cpu_number());
470 return(Shutdown_context(thread
, doshutdown
, processor
));
475 * This is where registers that are not normally specified by the mach-o
476 * file on an execve would be nullified, perhaps to avoid a covert channel.
479 machine_thread_state_initialize(
483 * If there's an fpu save area, free it.
484 * The initialized state will then be lazily faulted-in, if required.
485 * And if we're target, re-arm the no-fpu trap.
487 if (thread
->machine
.ifps
) {
488 (void) fpu_set_fxstate(thread
, NULL
, x86_FLOAT_STATE64
);
490 if (thread
== current_thread())
494 if (thread
->machine
.ids
) {
495 zfree(ids_zone
, thread
->machine
.ids
);
496 thread
->machine
.ids
= NULL
;
503 get_eflags_exportmask(void)
509 * x86_SAVED_STATE32 - internal save/restore general register state on 32/64 bit processors
510 * for 32bit tasks only
511 * x86_SAVED_STATE64 - internal save/restore general register state on 64 bit processors
512 * for 64bit tasks only
513 * x86_THREAD_STATE32 - external set/get general register state on 32/64 bit processors
514 * for 32bit tasks only
515 * x86_THREAD_STATE64 - external set/get general register state on 64 bit processors
516 * for 64bit tasks only
517 * x86_SAVED_STATE - external set/get general register state on 32/64 bit processors
518 * for either 32bit or 64bit tasks
519 * x86_FLOAT_STATE32 - internal/external save/restore float and xmm state on 32/64 bit processors
520 * for 32bit tasks only
521 * x86_FLOAT_STATE64 - internal/external save/restore float and xmm state on 64 bit processors
522 * for 64bit tasks only
523 * x86_FLOAT_STATE - external save/restore float and xmm state on 32/64 bit processors
524 * for either 32bit or 64bit tasks
525 * x86_EXCEPTION_STATE32 - external get exception state on 32/64 bit processors
526 * for 32bit tasks only
527 * x86_EXCEPTION_STATE64 - external get exception state on 64 bit processors
528 * for 64bit tasks only
529 * x86_EXCEPTION_STATE - external get exception state on 323/64 bit processors
530 * for either 32bit or 64bit tasks
535 get_exception_state64(thread_t thread
, x86_exception_state64_t
*es
)
537 x86_saved_state64_t
*saved_state
;
539 saved_state
= USER_REGS64(thread
);
541 es
->trapno
= saved_state
->isf
.trapno
;
542 es
->cpu
= saved_state
->isf
.cpu
;
543 es
->err
= (typeof(es
->err
))saved_state
->isf
.err
;
544 es
->faultvaddr
= saved_state
->cr2
;
548 get_exception_state32(thread_t thread
, x86_exception_state32_t
*es
)
550 x86_saved_state32_t
*saved_state
;
552 saved_state
= USER_REGS32(thread
);
554 es
->trapno
= saved_state
->trapno
;
555 es
->cpu
= saved_state
->cpu
;
556 es
->err
= saved_state
->err
;
557 es
->faultvaddr
= saved_state
->cr2
;
562 set_thread_state32(thread_t thread
, x86_thread_state32_t
*ts
)
564 x86_saved_state32_t
*saved_state
;
566 pal_register_cache_state(thread
, DIRTY
);
568 saved_state
= USER_REGS32(thread
);
571 * Scrub segment selector values:
575 * On a 64 bit kernel, we always override the data segments,
576 * as the actual selector numbers have changed. This also
577 * means that we don't support setting the data segments
584 /* Set GS to CTHREAD only if's been established */
585 ts
->gs
= thread
->machine
.cthread_self
? USER_CTHREAD
: NULL_SEG
;
587 /* Check segment selectors are safe */
588 if (!valid_user_segment_selectors(ts
->cs
,
594 return(KERN_INVALID_ARGUMENT
);
596 saved_state
->eax
= ts
->eax
;
597 saved_state
->ebx
= ts
->ebx
;
598 saved_state
->ecx
= ts
->ecx
;
599 saved_state
->edx
= ts
->edx
;
600 saved_state
->edi
= ts
->edi
;
601 saved_state
->esi
= ts
->esi
;
602 saved_state
->ebp
= ts
->ebp
;
603 saved_state
->uesp
= ts
->esp
;
604 saved_state
->efl
= (ts
->eflags
& ~EFL_USER_CLEAR
) | EFL_USER_SET
;
605 saved_state
->eip
= ts
->eip
;
606 saved_state
->cs
= ts
->cs
;
607 saved_state
->ss
= ts
->ss
;
608 saved_state
->ds
= ts
->ds
;
609 saved_state
->es
= ts
->es
;
610 saved_state
->fs
= ts
->fs
;
611 saved_state
->gs
= ts
->gs
;
614 * If the trace trap bit is being set,
615 * ensure that the user returns via iret
616 * - which is signaled thusly:
618 if ((saved_state
->efl
& EFL_TF
) && saved_state
->cs
== SYSENTER_CS
)
619 saved_state
->cs
= SYSENTER_TF_CS
;
621 return(KERN_SUCCESS
);
625 set_thread_state64(thread_t thread
, x86_thread_state64_t
*ts
)
627 x86_saved_state64_t
*saved_state
;
629 pal_register_cache_state(thread
, DIRTY
);
631 saved_state
= USER_REGS64(thread
);
633 if (!IS_USERADDR64_CANONICAL(ts
->rsp
) ||
634 !IS_USERADDR64_CANONICAL(ts
->rip
))
635 return(KERN_INVALID_ARGUMENT
);
637 saved_state
->r8
= ts
->r8
;
638 saved_state
->r9
= ts
->r9
;
639 saved_state
->r10
= ts
->r10
;
640 saved_state
->r11
= ts
->r11
;
641 saved_state
->r12
= ts
->r12
;
642 saved_state
->r13
= ts
->r13
;
643 saved_state
->r14
= ts
->r14
;
644 saved_state
->r15
= ts
->r15
;
645 saved_state
->rax
= ts
->rax
;
646 saved_state
->rbx
= ts
->rbx
;
647 saved_state
->rcx
= ts
->rcx
;
648 saved_state
->rdx
= ts
->rdx
;
649 saved_state
->rdi
= ts
->rdi
;
650 saved_state
->rsi
= ts
->rsi
;
651 saved_state
->rbp
= ts
->rbp
;
652 saved_state
->isf
.rsp
= ts
->rsp
;
653 saved_state
->isf
.rflags
= (ts
->rflags
& ~EFL_USER_CLEAR
) | EFL_USER_SET
;
654 saved_state
->isf
.rip
= ts
->rip
;
655 saved_state
->isf
.cs
= USER64_CS
;
656 saved_state
->fs
= (uint32_t)ts
->fs
;
657 saved_state
->gs
= (uint32_t)ts
->gs
;
659 return(KERN_SUCCESS
);
665 get_thread_state32(thread_t thread
, x86_thread_state32_t
*ts
)
667 x86_saved_state32_t
*saved_state
;
669 pal_register_cache_state(thread
, VALID
);
671 saved_state
= USER_REGS32(thread
);
673 ts
->eax
= saved_state
->eax
;
674 ts
->ebx
= saved_state
->ebx
;
675 ts
->ecx
= saved_state
->ecx
;
676 ts
->edx
= saved_state
->edx
;
677 ts
->edi
= saved_state
->edi
;
678 ts
->esi
= saved_state
->esi
;
679 ts
->ebp
= saved_state
->ebp
;
680 ts
->esp
= saved_state
->uesp
;
681 ts
->eflags
= saved_state
->efl
;
682 ts
->eip
= saved_state
->eip
;
683 ts
->cs
= saved_state
->cs
;
684 ts
->ss
= saved_state
->ss
;
685 ts
->ds
= saved_state
->ds
;
686 ts
->es
= saved_state
->es
;
687 ts
->fs
= saved_state
->fs
;
688 ts
->gs
= saved_state
->gs
;
693 get_thread_state64(thread_t thread
, x86_thread_state64_t
*ts
)
695 x86_saved_state64_t
*saved_state
;
697 pal_register_cache_state(thread
, VALID
);
699 saved_state
= USER_REGS64(thread
);
701 ts
->r8
= saved_state
->r8
;
702 ts
->r9
= saved_state
->r9
;
703 ts
->r10
= saved_state
->r10
;
704 ts
->r11
= saved_state
->r11
;
705 ts
->r12
= saved_state
->r12
;
706 ts
->r13
= saved_state
->r13
;
707 ts
->r14
= saved_state
->r14
;
708 ts
->r15
= saved_state
->r15
;
709 ts
->rax
= saved_state
->rax
;
710 ts
->rbx
= saved_state
->rbx
;
711 ts
->rcx
= saved_state
->rcx
;
712 ts
->rdx
= saved_state
->rdx
;
713 ts
->rdi
= saved_state
->rdi
;
714 ts
->rsi
= saved_state
->rsi
;
715 ts
->rbp
= saved_state
->rbp
;
716 ts
->rsp
= saved_state
->isf
.rsp
;
717 ts
->rflags
= saved_state
->isf
.rflags
;
718 ts
->rip
= saved_state
->isf
.rip
;
719 ts
->cs
= saved_state
->isf
.cs
;
720 ts
->fs
= saved_state
->fs
;
721 ts
->gs
= saved_state
->gs
;
726 * act_machine_set_state:
728 * Set the status of the specified thread.
732 machine_thread_set_state(
734 thread_flavor_t flavor
,
735 thread_state_t tstate
,
736 mach_msg_type_number_t count
)
739 case x86_SAVED_STATE32
:
741 x86_saved_state32_t
*state
;
742 x86_saved_state32_t
*saved_state
;
744 if (count
< x86_SAVED_STATE32_COUNT
)
745 return(KERN_INVALID_ARGUMENT
);
747 if (thread_is_64bit(thr_act
))
748 return(KERN_INVALID_ARGUMENT
);
750 state
= (x86_saved_state32_t
*) tstate
;
752 /* Check segment selectors are safe */
753 if (!valid_user_segment_selectors(state
->cs
,
759 return KERN_INVALID_ARGUMENT
;
761 pal_register_cache_state(thr_act
, DIRTY
);
763 saved_state
= USER_REGS32(thr_act
);
768 saved_state
->edi
= state
->edi
;
769 saved_state
->esi
= state
->esi
;
770 saved_state
->ebp
= state
->ebp
;
771 saved_state
->uesp
= state
->uesp
;
772 saved_state
->ebx
= state
->ebx
;
773 saved_state
->edx
= state
->edx
;
774 saved_state
->ecx
= state
->ecx
;
775 saved_state
->eax
= state
->eax
;
776 saved_state
->eip
= state
->eip
;
778 saved_state
->efl
= (state
->efl
& ~EFL_USER_CLEAR
) | EFL_USER_SET
;
781 * If the trace trap bit is being set,
782 * ensure that the user returns via iret
783 * - which is signaled thusly:
785 if ((saved_state
->efl
& EFL_TF
) && state
->cs
== SYSENTER_CS
)
786 state
->cs
= SYSENTER_TF_CS
;
789 * User setting segment registers.
790 * Code and stack selectors have already been
791 * checked. Others will be reset by 'iret'
792 * if they are not valid.
794 saved_state
->cs
= state
->cs
;
795 saved_state
->ss
= state
->ss
;
796 saved_state
->ds
= state
->ds
;
797 saved_state
->es
= state
->es
;
798 saved_state
->fs
= state
->fs
;
799 saved_state
->gs
= state
->gs
;
804 case x86_SAVED_STATE64
:
806 x86_saved_state64_t
*state
;
807 x86_saved_state64_t
*saved_state
;
809 if (count
< x86_SAVED_STATE64_COUNT
)
810 return(KERN_INVALID_ARGUMENT
);
812 if (!thread_is_64bit(thr_act
))
813 return(KERN_INVALID_ARGUMENT
);
815 state
= (x86_saved_state64_t
*) tstate
;
817 /* Verify that the supplied code segment selector is
818 * valid. In 64-bit mode, the FS and GS segment overrides
819 * use the FS.base and GS.base MSRs to calculate
820 * base addresses, and the trampolines don't directly
821 * restore the segment registers--hence they are no
822 * longer relevant for validation.
824 if (!valid_user_code_selector(state
->isf
.cs
))
825 return KERN_INVALID_ARGUMENT
;
827 /* Check pc and stack are canonical addresses */
828 if (!IS_USERADDR64_CANONICAL(state
->isf
.rsp
) ||
829 !IS_USERADDR64_CANONICAL(state
->isf
.rip
))
830 return KERN_INVALID_ARGUMENT
;
832 pal_register_cache_state(thr_act
, DIRTY
);
834 saved_state
= USER_REGS64(thr_act
);
839 saved_state
->r8
= state
->r8
;
840 saved_state
->r9
= state
->r9
;
841 saved_state
->r10
= state
->r10
;
842 saved_state
->r11
= state
->r11
;
843 saved_state
->r12
= state
->r12
;
844 saved_state
->r13
= state
->r13
;
845 saved_state
->r14
= state
->r14
;
846 saved_state
->r15
= state
->r15
;
847 saved_state
->rdi
= state
->rdi
;
848 saved_state
->rsi
= state
->rsi
;
849 saved_state
->rbp
= state
->rbp
;
850 saved_state
->rbx
= state
->rbx
;
851 saved_state
->rdx
= state
->rdx
;
852 saved_state
->rcx
= state
->rcx
;
853 saved_state
->rax
= state
->rax
;
854 saved_state
->isf
.rsp
= state
->isf
.rsp
;
855 saved_state
->isf
.rip
= state
->isf
.rip
;
857 saved_state
->isf
.rflags
= (state
->isf
.rflags
& ~EFL_USER_CLEAR
) | EFL_USER_SET
;
860 * User setting segment registers.
861 * Code and stack selectors have already been
862 * checked. Others will be reset by 'sys'
863 * if they are not valid.
865 saved_state
->isf
.cs
= state
->isf
.cs
;
866 saved_state
->isf
.ss
= state
->isf
.ss
;
867 saved_state
->fs
= state
->fs
;
868 saved_state
->gs
= state
->gs
;
873 case x86_FLOAT_STATE32
:
874 case x86_AVX_STATE32
:
875 #if !defined(RC_HIDE_XNU_J137)
876 case x86_AVX512_STATE32
:
877 #endif /* not RC_HIDE_XNU_J137 */
879 if (count
!= _MachineStateCount
[flavor
])
880 return(KERN_INVALID_ARGUMENT
);
882 if (thread_is_64bit(thr_act
))
883 return(KERN_INVALID_ARGUMENT
);
885 return fpu_set_fxstate(thr_act
, tstate
, flavor
);
888 case x86_FLOAT_STATE64
:
889 case x86_AVX_STATE64
:
890 #if !defined(RC_HIDE_XNU_J137)
891 case x86_AVX512_STATE64
:
892 #endif /* not RC_HIDE_XNU_J137 */
894 if (count
!= _MachineStateCount
[flavor
])
895 return(KERN_INVALID_ARGUMENT
);
897 if (!thread_is_64bit(thr_act
))
898 return(KERN_INVALID_ARGUMENT
);
900 return fpu_set_fxstate(thr_act
, tstate
, flavor
);
903 case x86_FLOAT_STATE
:
905 x86_float_state_t
*state
;
907 if (count
!= x86_FLOAT_STATE_COUNT
)
908 return(KERN_INVALID_ARGUMENT
);
910 state
= (x86_float_state_t
*)tstate
;
911 if (state
->fsh
.flavor
== x86_FLOAT_STATE64
&& state
->fsh
.count
== x86_FLOAT_STATE64_COUNT
&&
912 thread_is_64bit(thr_act
)) {
913 return fpu_set_fxstate(thr_act
, (thread_state_t
)&state
->ufs
.fs64
, x86_FLOAT_STATE64
);
915 if (state
->fsh
.flavor
== x86_FLOAT_STATE32
&& state
->fsh
.count
== x86_FLOAT_STATE32_COUNT
&&
916 !thread_is_64bit(thr_act
)) {
917 return fpu_set_fxstate(thr_act
, (thread_state_t
)&state
->ufs
.fs32
, x86_FLOAT_STATE32
);
919 return(KERN_INVALID_ARGUMENT
);
923 #if !defined(RC_HIDE_XNU_J137)
924 case x86_AVX512_STATE
:
927 x86_avx_state_t
*state
;
929 if (count
!= _MachineStateCount
[flavor
])
930 return(KERN_INVALID_ARGUMENT
);
932 state
= (x86_avx_state_t
*)tstate
;
933 /* Flavors are defined to have sequential values: 32-bit, 64-bit, non-specific */
935 if (state
->ash
.flavor
== (flavor
- 1) &&
936 state
->ash
.count
== _MachineStateCount
[flavor
- 1] &&
937 thread_is_64bit(thr_act
)) {
938 return fpu_set_fxstate(thr_act
,
939 (thread_state_t
)&state
->ufs
.as64
,
943 if (state
->ash
.flavor
== (flavor
- 2) &&
944 state
->ash
.count
== _MachineStateCount
[flavor
- 2] &&
945 !thread_is_64bit(thr_act
)) {
946 return fpu_set_fxstate(thr_act
,
947 (thread_state_t
)&state
->ufs
.as32
,
950 return(KERN_INVALID_ARGUMENT
);
953 case x86_THREAD_STATE32
:
955 if (count
!= x86_THREAD_STATE32_COUNT
)
956 return(KERN_INVALID_ARGUMENT
);
958 if (thread_is_64bit(thr_act
))
959 return(KERN_INVALID_ARGUMENT
);
961 return set_thread_state32(thr_act
, (x86_thread_state32_t
*)tstate
);
964 case x86_THREAD_STATE64
:
966 if (count
!= x86_THREAD_STATE64_COUNT
)
967 return(KERN_INVALID_ARGUMENT
);
969 if (!thread_is_64bit(thr_act
))
970 return(KERN_INVALID_ARGUMENT
);
972 return set_thread_state64(thr_act
, (x86_thread_state64_t
*)tstate
);
975 case x86_THREAD_STATE
:
977 x86_thread_state_t
*state
;
979 if (count
!= x86_THREAD_STATE_COUNT
)
980 return(KERN_INVALID_ARGUMENT
);
982 state
= (x86_thread_state_t
*)tstate
;
984 if (state
->tsh
.flavor
== x86_THREAD_STATE64
&&
985 state
->tsh
.count
== x86_THREAD_STATE64_COUNT
&&
986 thread_is_64bit(thr_act
)) {
987 return set_thread_state64(thr_act
, &state
->uts
.ts64
);
988 } else if (state
->tsh
.flavor
== x86_THREAD_STATE32
&&
989 state
->tsh
.count
== x86_THREAD_STATE32_COUNT
&&
990 !thread_is_64bit(thr_act
)) {
991 return set_thread_state32(thr_act
, &state
->uts
.ts32
);
993 return(KERN_INVALID_ARGUMENT
);
995 case x86_DEBUG_STATE32
:
997 x86_debug_state32_t
*state
;
1000 if (thread_is_64bit(thr_act
))
1001 return(KERN_INVALID_ARGUMENT
);
1003 state
= (x86_debug_state32_t
*)tstate
;
1005 ret
= set_debug_state32(thr_act
, state
);
1009 case x86_DEBUG_STATE64
:
1011 x86_debug_state64_t
*state
;
1014 if (!thread_is_64bit(thr_act
))
1015 return(KERN_INVALID_ARGUMENT
);
1017 state
= (x86_debug_state64_t
*)tstate
;
1019 ret
= set_debug_state64(thr_act
, state
);
1023 case x86_DEBUG_STATE
:
1025 x86_debug_state_t
*state
;
1026 kern_return_t ret
= KERN_INVALID_ARGUMENT
;
1028 if (count
!= x86_DEBUG_STATE_COUNT
)
1029 return (KERN_INVALID_ARGUMENT
);
1031 state
= (x86_debug_state_t
*)tstate
;
1032 if (state
->dsh
.flavor
== x86_DEBUG_STATE64
&&
1033 state
->dsh
.count
== x86_DEBUG_STATE64_COUNT
&&
1034 thread_is_64bit(thr_act
)) {
1035 ret
= set_debug_state64(thr_act
, &state
->uds
.ds64
);
1038 if (state
->dsh
.flavor
== x86_DEBUG_STATE32
&&
1039 state
->dsh
.count
== x86_DEBUG_STATE32_COUNT
&&
1040 !thread_is_64bit(thr_act
)) {
1041 ret
= set_debug_state32(thr_act
, &state
->uds
.ds32
);
1046 return(KERN_INVALID_ARGUMENT
);
1049 return(KERN_SUCCESS
);
1057 * Get the status of the specified thread.
1061 machine_thread_get_state(
1063 thread_flavor_t flavor
,
1064 thread_state_t tstate
,
1065 mach_msg_type_number_t
*count
)
1070 case THREAD_STATE_FLAVOR_LIST
:
1073 return (KERN_INVALID_ARGUMENT
);
1075 tstate
[0] = i386_THREAD_STATE
;
1076 tstate
[1] = i386_FLOAT_STATE
;
1077 tstate
[2] = i386_EXCEPTION_STATE
;
1083 case THREAD_STATE_FLAVOR_LIST_NEW
:
1086 return (KERN_INVALID_ARGUMENT
);
1088 tstate
[0] = x86_THREAD_STATE
;
1089 tstate
[1] = x86_FLOAT_STATE
;
1090 tstate
[2] = x86_EXCEPTION_STATE
;
1091 tstate
[3] = x86_DEBUG_STATE
;
1097 case THREAD_STATE_FLAVOR_LIST_10_9
:
1100 return (KERN_INVALID_ARGUMENT
);
1102 tstate
[0] = x86_THREAD_STATE
;
1103 tstate
[1] = x86_FLOAT_STATE
;
1104 tstate
[2] = x86_EXCEPTION_STATE
;
1105 tstate
[3] = x86_DEBUG_STATE
;
1106 tstate
[4] = x86_AVX_STATE
;
1112 #if !defined(RC_HIDE_XNU_J137)
1113 case THREAD_STATE_FLAVOR_LIST_10_13
:
1116 return (KERN_INVALID_ARGUMENT
);
1118 tstate
[0] = x86_THREAD_STATE
;
1119 tstate
[1] = x86_FLOAT_STATE
;
1120 tstate
[2] = x86_EXCEPTION_STATE
;
1121 tstate
[3] = x86_DEBUG_STATE
;
1122 tstate
[4] = x86_AVX_STATE
;
1123 tstate
[5] = x86_AVX512_STATE
;
1130 case x86_SAVED_STATE32
:
1132 x86_saved_state32_t
*state
;
1133 x86_saved_state32_t
*saved_state
;
1135 if (*count
< x86_SAVED_STATE32_COUNT
)
1136 return(KERN_INVALID_ARGUMENT
);
1138 if (thread_is_64bit(thr_act
))
1139 return(KERN_INVALID_ARGUMENT
);
1141 state
= (x86_saved_state32_t
*) tstate
;
1142 saved_state
= USER_REGS32(thr_act
);
1145 * First, copy everything:
1147 *state
= *saved_state
;
1148 state
->ds
= saved_state
->ds
& 0xffff;
1149 state
->es
= saved_state
->es
& 0xffff;
1150 state
->fs
= saved_state
->fs
& 0xffff;
1151 state
->gs
= saved_state
->gs
& 0xffff;
1153 *count
= x86_SAVED_STATE32_COUNT
;
1157 case x86_SAVED_STATE64
:
1159 x86_saved_state64_t
*state
;
1160 x86_saved_state64_t
*saved_state
;
1162 if (*count
< x86_SAVED_STATE64_COUNT
)
1163 return(KERN_INVALID_ARGUMENT
);
1165 if (!thread_is_64bit(thr_act
))
1166 return(KERN_INVALID_ARGUMENT
);
1168 state
= (x86_saved_state64_t
*)tstate
;
1169 saved_state
= USER_REGS64(thr_act
);
1172 * First, copy everything:
1174 *state
= *saved_state
;
1175 state
->fs
= saved_state
->fs
& 0xffff;
1176 state
->gs
= saved_state
->gs
& 0xffff;
1178 *count
= x86_SAVED_STATE64_COUNT
;
1182 case x86_FLOAT_STATE32
:
1184 if (*count
< x86_FLOAT_STATE32_COUNT
)
1185 return(KERN_INVALID_ARGUMENT
);
1187 if (thread_is_64bit(thr_act
))
1188 return(KERN_INVALID_ARGUMENT
);
1190 *count
= x86_FLOAT_STATE32_COUNT
;
1192 return fpu_get_fxstate(thr_act
, tstate
, flavor
);
1195 case x86_FLOAT_STATE64
:
1197 if (*count
< x86_FLOAT_STATE64_COUNT
)
1198 return(KERN_INVALID_ARGUMENT
);
1200 if ( !thread_is_64bit(thr_act
))
1201 return(KERN_INVALID_ARGUMENT
);
1203 *count
= x86_FLOAT_STATE64_COUNT
;
1205 return fpu_get_fxstate(thr_act
, tstate
, flavor
);
1208 case x86_FLOAT_STATE
:
1210 x86_float_state_t
*state
;
1213 if (*count
< x86_FLOAT_STATE_COUNT
)
1214 return(KERN_INVALID_ARGUMENT
);
1216 state
= (x86_float_state_t
*)tstate
;
1219 * no need to bzero... currently
1220 * x86_FLOAT_STATE64_COUNT == x86_FLOAT_STATE32_COUNT
1222 if (thread_is_64bit(thr_act
)) {
1223 state
->fsh
.flavor
= x86_FLOAT_STATE64
;
1224 state
->fsh
.count
= x86_FLOAT_STATE64_COUNT
;
1226 kret
= fpu_get_fxstate(thr_act
, (thread_state_t
)&state
->ufs
.fs64
, x86_FLOAT_STATE64
);
1228 state
->fsh
.flavor
= x86_FLOAT_STATE32
;
1229 state
->fsh
.count
= x86_FLOAT_STATE32_COUNT
;
1231 kret
= fpu_get_fxstate(thr_act
, (thread_state_t
)&state
->ufs
.fs32
, x86_FLOAT_STATE32
);
1233 *count
= x86_FLOAT_STATE_COUNT
;
1238 case x86_AVX_STATE32
:
1239 #if !defined(RC_HIDE_XNU_J137)
1240 case x86_AVX512_STATE32
:
1243 if (*count
!= _MachineStateCount
[flavor
])
1244 return(KERN_INVALID_ARGUMENT
);
1246 if (thread_is_64bit(thr_act
))
1247 return(KERN_INVALID_ARGUMENT
);
1249 *count
= _MachineStateCount
[flavor
];
1251 return fpu_get_fxstate(thr_act
, tstate
, flavor
);
1254 case x86_AVX_STATE64
:
1255 #if !defined(RC_HIDE_XNU_J137)
1256 case x86_AVX512_STATE64
:
1259 if (*count
!= _MachineStateCount
[flavor
])
1260 return(KERN_INVALID_ARGUMENT
);
1262 if ( !thread_is_64bit(thr_act
))
1263 return(KERN_INVALID_ARGUMENT
);
1265 *count
= _MachineStateCount
[flavor
];
1267 return fpu_get_fxstate(thr_act
, tstate
, flavor
);
1271 #if !defined(RC_HIDE_XNU_J137)
1272 case x86_AVX512_STATE
:
1275 x86_avx_state_t
*state
;
1276 thread_state_t fstate
;
1278 if (*count
< _MachineStateCount
[flavor
])
1279 return(KERN_INVALID_ARGUMENT
);
1281 *count
= _MachineStateCount
[flavor
];
1282 state
= (x86_avx_state_t
*)tstate
;
1284 bzero((char *)state
, *count
* sizeof(int));
1286 if (thread_is_64bit(thr_act
)) {
1287 flavor
-= 1; /* 64-bit flavor */
1288 fstate
= (thread_state_t
) &state
->ufs
.as64
;
1290 flavor
-= 2; /* 32-bit flavor */
1291 fstate
= (thread_state_t
) &state
->ufs
.as32
;
1293 state
->ash
.flavor
= flavor
;
1294 state
->ash
.count
= _MachineStateCount
[flavor
];
1296 return fpu_get_fxstate(thr_act
, fstate
, flavor
);
1299 case x86_THREAD_STATE32
:
1301 if (*count
< x86_THREAD_STATE32_COUNT
)
1302 return(KERN_INVALID_ARGUMENT
);
1304 if (thread_is_64bit(thr_act
))
1305 return(KERN_INVALID_ARGUMENT
);
1307 *count
= x86_THREAD_STATE32_COUNT
;
1309 get_thread_state32(thr_act
, (x86_thread_state32_t
*)tstate
);
1313 case x86_THREAD_STATE64
:
1315 if (*count
< x86_THREAD_STATE64_COUNT
)
1316 return(KERN_INVALID_ARGUMENT
);
1318 if ( !thread_is_64bit(thr_act
))
1319 return(KERN_INVALID_ARGUMENT
);
1321 *count
= x86_THREAD_STATE64_COUNT
;
1323 get_thread_state64(thr_act
, (x86_thread_state64_t
*)tstate
);
1327 case x86_THREAD_STATE
:
1329 x86_thread_state_t
*state
;
1331 if (*count
< x86_THREAD_STATE_COUNT
)
1332 return(KERN_INVALID_ARGUMENT
);
1334 state
= (x86_thread_state_t
*)tstate
;
1336 bzero((char *)state
, sizeof(x86_thread_state_t
));
1338 if (thread_is_64bit(thr_act
)) {
1339 state
->tsh
.flavor
= x86_THREAD_STATE64
;
1340 state
->tsh
.count
= x86_THREAD_STATE64_COUNT
;
1342 get_thread_state64(thr_act
, &state
->uts
.ts64
);
1344 state
->tsh
.flavor
= x86_THREAD_STATE32
;
1345 state
->tsh
.count
= x86_THREAD_STATE32_COUNT
;
1347 get_thread_state32(thr_act
, &state
->uts
.ts32
);
1349 *count
= x86_THREAD_STATE_COUNT
;
1355 case x86_EXCEPTION_STATE32
:
1357 if (*count
< x86_EXCEPTION_STATE32_COUNT
)
1358 return(KERN_INVALID_ARGUMENT
);
1360 if (thread_is_64bit(thr_act
))
1361 return(KERN_INVALID_ARGUMENT
);
1363 *count
= x86_EXCEPTION_STATE32_COUNT
;
1365 get_exception_state32(thr_act
, (x86_exception_state32_t
*)tstate
);
1367 * Suppress the cpu number for binary compatibility
1368 * of this deprecated state.
1370 ((x86_exception_state32_t
*)tstate
)->cpu
= 0;
1374 case x86_EXCEPTION_STATE64
:
1376 if (*count
< x86_EXCEPTION_STATE64_COUNT
)
1377 return(KERN_INVALID_ARGUMENT
);
1379 if ( !thread_is_64bit(thr_act
))
1380 return(KERN_INVALID_ARGUMENT
);
1382 *count
= x86_EXCEPTION_STATE64_COUNT
;
1384 get_exception_state64(thr_act
, (x86_exception_state64_t
*)tstate
);
1386 * Suppress the cpu number for binary compatibility
1387 * of this deprecated state.
1389 ((x86_exception_state64_t
*)tstate
)->cpu
= 0;
1393 case x86_EXCEPTION_STATE
:
1395 x86_exception_state_t
*state
;
1397 if (*count
< x86_EXCEPTION_STATE_COUNT
)
1398 return(KERN_INVALID_ARGUMENT
);
1400 state
= (x86_exception_state_t
*)tstate
;
1402 bzero((char *)state
, sizeof(x86_exception_state_t
));
1404 if (thread_is_64bit(thr_act
)) {
1405 state
->esh
.flavor
= x86_EXCEPTION_STATE64
;
1406 state
->esh
.count
= x86_EXCEPTION_STATE64_COUNT
;
1408 get_exception_state64(thr_act
, &state
->ues
.es64
);
1410 state
->esh
.flavor
= x86_EXCEPTION_STATE32
;
1411 state
->esh
.count
= x86_EXCEPTION_STATE32_COUNT
;
1413 get_exception_state32(thr_act
, &state
->ues
.es32
);
1415 *count
= x86_EXCEPTION_STATE_COUNT
;
1419 case x86_DEBUG_STATE32
:
1421 if (*count
< x86_DEBUG_STATE32_COUNT
)
1422 return(KERN_INVALID_ARGUMENT
);
1424 if (thread_is_64bit(thr_act
))
1425 return(KERN_INVALID_ARGUMENT
);
1427 get_debug_state32(thr_act
, (x86_debug_state32_t
*)tstate
);
1429 *count
= x86_DEBUG_STATE32_COUNT
;
1433 case x86_DEBUG_STATE64
:
1435 if (*count
< x86_DEBUG_STATE64_COUNT
)
1436 return(KERN_INVALID_ARGUMENT
);
1438 if (!thread_is_64bit(thr_act
))
1439 return(KERN_INVALID_ARGUMENT
);
1441 get_debug_state64(thr_act
, (x86_debug_state64_t
*)tstate
);
1443 *count
= x86_DEBUG_STATE64_COUNT
;
1447 case x86_DEBUG_STATE
:
1449 x86_debug_state_t
*state
;
1451 if (*count
< x86_DEBUG_STATE_COUNT
)
1452 return(KERN_INVALID_ARGUMENT
);
1454 state
= (x86_debug_state_t
*)tstate
;
1456 bzero(state
, sizeof *state
);
1458 if (thread_is_64bit(thr_act
)) {
1459 state
->dsh
.flavor
= x86_DEBUG_STATE64
;
1460 state
->dsh
.count
= x86_DEBUG_STATE64_COUNT
;
1462 get_debug_state64(thr_act
, &state
->uds
.ds64
);
1464 state
->dsh
.flavor
= x86_DEBUG_STATE32
;
1465 state
->dsh
.count
= x86_DEBUG_STATE32_COUNT
;
1467 get_debug_state32(thr_act
, &state
->uds
.ds32
);
1469 *count
= x86_DEBUG_STATE_COUNT
;
1473 return(KERN_INVALID_ARGUMENT
);
1476 return(KERN_SUCCESS
);
1480 machine_thread_get_kern_state(
1482 thread_flavor_t flavor
,
1483 thread_state_t tstate
,
1484 mach_msg_type_number_t
*count
)
1486 x86_saved_state_t
*int_state
= current_cpu_datap()->cpu_int_state
;
1489 * This works only for an interrupted kernel thread
1491 if (thread
!= current_thread() || int_state
== NULL
)
1492 return KERN_FAILURE
;
1495 case x86_THREAD_STATE32
: {
1496 x86_thread_state32_t
*state
;
1497 x86_saved_state32_t
*saved_state
;
1499 if (!is_saved_state32(int_state
) ||
1500 *count
< x86_THREAD_STATE32_COUNT
)
1501 return (KERN_INVALID_ARGUMENT
);
1503 state
= (x86_thread_state32_t
*) tstate
;
1505 saved_state
= saved_state32(int_state
);
1507 * General registers.
1509 state
->eax
= saved_state
->eax
;
1510 state
->ebx
= saved_state
->ebx
;
1511 state
->ecx
= saved_state
->ecx
;
1512 state
->edx
= saved_state
->edx
;
1513 state
->edi
= saved_state
->edi
;
1514 state
->esi
= saved_state
->esi
;
1515 state
->ebp
= saved_state
->ebp
;
1516 state
->esp
= saved_state
->uesp
;
1517 state
->eflags
= saved_state
->efl
;
1518 state
->eip
= saved_state
->eip
;
1519 state
->cs
= saved_state
->cs
;
1520 state
->ss
= saved_state
->ss
;
1521 state
->ds
= saved_state
->ds
& 0xffff;
1522 state
->es
= saved_state
->es
& 0xffff;
1523 state
->fs
= saved_state
->fs
& 0xffff;
1524 state
->gs
= saved_state
->gs
& 0xffff;
1526 *count
= x86_THREAD_STATE32_COUNT
;
1528 return KERN_SUCCESS
;
1531 case x86_THREAD_STATE64
: {
1532 x86_thread_state64_t
*state
;
1533 x86_saved_state64_t
*saved_state
;
1535 if (!is_saved_state64(int_state
) ||
1536 *count
< x86_THREAD_STATE64_COUNT
)
1537 return (KERN_INVALID_ARGUMENT
);
1539 state
= (x86_thread_state64_t
*) tstate
;
1541 saved_state
= saved_state64(int_state
);
1543 * General registers.
1545 state
->rax
= saved_state
->rax
;
1546 state
->rbx
= saved_state
->rbx
;
1547 state
->rcx
= saved_state
->rcx
;
1548 state
->rdx
= saved_state
->rdx
;
1549 state
->rdi
= saved_state
->rdi
;
1550 state
->rsi
= saved_state
->rsi
;
1551 state
->rbp
= saved_state
->rbp
;
1552 state
->rsp
= saved_state
->isf
.rsp
;
1553 state
->r8
= saved_state
->r8
;
1554 state
->r9
= saved_state
->r9
;
1555 state
->r10
= saved_state
->r10
;
1556 state
->r11
= saved_state
->r11
;
1557 state
->r12
= saved_state
->r12
;
1558 state
->r13
= saved_state
->r13
;
1559 state
->r14
= saved_state
->r14
;
1560 state
->r15
= saved_state
->r15
;
1562 state
->rip
= saved_state
->isf
.rip
;
1563 state
->rflags
= saved_state
->isf
.rflags
;
1564 state
->cs
= saved_state
->isf
.cs
;
1565 state
->fs
= saved_state
->fs
& 0xffff;
1566 state
->gs
= saved_state
->gs
& 0xffff;
1567 *count
= x86_THREAD_STATE64_COUNT
;
1569 return KERN_SUCCESS
;
1572 case x86_THREAD_STATE
: {
1573 x86_thread_state_t
*state
= NULL
;
1575 if (*count
< x86_THREAD_STATE_COUNT
)
1576 return (KERN_INVALID_ARGUMENT
);
1578 state
= (x86_thread_state_t
*) tstate
;
1580 if (is_saved_state32(int_state
)) {
1581 x86_saved_state32_t
*saved_state
= saved_state32(int_state
);
1583 state
->tsh
.flavor
= x86_THREAD_STATE32
;
1584 state
->tsh
.count
= x86_THREAD_STATE32_COUNT
;
1587 * General registers.
1589 state
->uts
.ts32
.eax
= saved_state
->eax
;
1590 state
->uts
.ts32
.ebx
= saved_state
->ebx
;
1591 state
->uts
.ts32
.ecx
= saved_state
->ecx
;
1592 state
->uts
.ts32
.edx
= saved_state
->edx
;
1593 state
->uts
.ts32
.edi
= saved_state
->edi
;
1594 state
->uts
.ts32
.esi
= saved_state
->esi
;
1595 state
->uts
.ts32
.ebp
= saved_state
->ebp
;
1596 state
->uts
.ts32
.esp
= saved_state
->uesp
;
1597 state
->uts
.ts32
.eflags
= saved_state
->efl
;
1598 state
->uts
.ts32
.eip
= saved_state
->eip
;
1599 state
->uts
.ts32
.cs
= saved_state
->cs
;
1600 state
->uts
.ts32
.ss
= saved_state
->ss
;
1601 state
->uts
.ts32
.ds
= saved_state
->ds
& 0xffff;
1602 state
->uts
.ts32
.es
= saved_state
->es
& 0xffff;
1603 state
->uts
.ts32
.fs
= saved_state
->fs
& 0xffff;
1604 state
->uts
.ts32
.gs
= saved_state
->gs
& 0xffff;
1605 } else if (is_saved_state64(int_state
)) {
1606 x86_saved_state64_t
*saved_state
= saved_state64(int_state
);
1608 state
->tsh
.flavor
= x86_THREAD_STATE64
;
1609 state
->tsh
.count
= x86_THREAD_STATE64_COUNT
;
1612 * General registers.
1614 state
->uts
.ts64
.rax
= saved_state
->rax
;
1615 state
->uts
.ts64
.rbx
= saved_state
->rbx
;
1616 state
->uts
.ts64
.rcx
= saved_state
->rcx
;
1617 state
->uts
.ts64
.rdx
= saved_state
->rdx
;
1618 state
->uts
.ts64
.rdi
= saved_state
->rdi
;
1619 state
->uts
.ts64
.rsi
= saved_state
->rsi
;
1620 state
->uts
.ts64
.rbp
= saved_state
->rbp
;
1621 state
->uts
.ts64
.rsp
= saved_state
->isf
.rsp
;
1622 state
->uts
.ts64
.r8
= saved_state
->r8
;
1623 state
->uts
.ts64
.r9
= saved_state
->r9
;
1624 state
->uts
.ts64
.r10
= saved_state
->r10
;
1625 state
->uts
.ts64
.r11
= saved_state
->r11
;
1626 state
->uts
.ts64
.r12
= saved_state
->r12
;
1627 state
->uts
.ts64
.r13
= saved_state
->r13
;
1628 state
->uts
.ts64
.r14
= saved_state
->r14
;
1629 state
->uts
.ts64
.r15
= saved_state
->r15
;
1631 state
->uts
.ts64
.rip
= saved_state
->isf
.rip
;
1632 state
->uts
.ts64
.rflags
= saved_state
->isf
.rflags
;
1633 state
->uts
.ts64
.cs
= saved_state
->isf
.cs
;
1634 state
->uts
.ts64
.fs
= saved_state
->fs
& 0xffff;
1635 state
->uts
.ts64
.gs
= saved_state
->gs
& 0xffff;
1637 panic("unknown thread state");
1640 *count
= x86_THREAD_STATE_COUNT
;
1641 return KERN_SUCCESS
;
1644 return KERN_FAILURE
;
1649 machine_thread_switch_addrmode(thread_t thread
)
1652 * We don't want to be preempted until we're done
1653 * - particularly if we're switching the current thread
1655 disable_preemption();
1658 * Reset the state saveareas. As we're resetting, we anticipate no
1659 * memory allocations in this path.
1661 machine_thread_create(thread
, thread
->task
);
1663 /* Adjust FPU state */
1664 fpu_switch_addrmode(thread
, task_has_64BitAddr(thread
->task
));
1666 /* If we're switching ourselves, reset the pcb addresses etc. */
1667 if (thread
== current_thread()) {
1668 boolean_t istate
= ml_set_interrupts_enabled(FALSE
);
1669 act_machine_switch_pcb(NULL
, thread
);
1670 ml_set_interrupts_enabled(istate
);
1672 enable_preemption();
1678 * This is used to set the current thr_act/thread
1679 * when starting up a new processor
1682 machine_set_current_thread(thread_t thread
)
1684 current_cpu_datap()->cpu_active_thread
= thread
;
1689 * Perform machine-dependent per-thread initializations
1692 machine_thread_init(void)
1694 iss_zone
= zinit(sizeof(x86_saved_state_t
),
1695 thread_max
* sizeof(x86_saved_state_t
),
1696 THREAD_CHUNK
* sizeof(x86_saved_state_t
),
1697 "x86_64 saved state");
1699 ids_zone
= zinit(sizeof(x86_debug_state64_t
),
1700 thread_max
* sizeof(x86_debug_state64_t
),
1701 THREAD_CHUNK
* sizeof(x86_debug_state64_t
),
1702 "x86_64 debug state");
1712 thread_t thr_act
= current_thread();
1714 if (thread_is_64bit(thr_act
)) {
1715 x86_saved_state64_t
*iss64
;
1717 iss64
= USER_REGS64(thr_act
);
1719 return(iss64
->isf
.rip
);
1721 x86_saved_state32_t
*iss32
;
1723 iss32
= USER_REGS32(thr_act
);
1730 * detach and return a kernel stack from a thread
1734 machine_stack_detach(thread_t thread
)
1738 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_SCHED
, MACH_STACK_DETACH
),
1739 (uintptr_t)thread_tid(thread
), thread
->priority
,
1740 thread
->sched_pri
, 0,
1743 stack
= thread
->kernel_stack
;
1744 thread
->kernel_stack
= 0;
1750 * attach a kernel stack to a thread and initialize it
1754 machine_stack_attach(
1758 struct x86_kernel_state
*statep
;
1760 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_SCHED
, MACH_STACK_ATTACH
),
1761 (uintptr_t)thread_tid(thread
), thread
->priority
,
1762 thread
->sched_pri
, 0, 0);
1765 thread
->kernel_stack
= stack
;
1766 thread_initialize_kernel_state(thread
);
1768 statep
= STACK_IKS(stack
);
1769 #if defined(__x86_64__)
1770 statep
->k_rip
= (unsigned long) Thread_continue
;
1771 statep
->k_rbx
= (unsigned long) thread_continue
;
1772 statep
->k_rsp
= (unsigned long) STACK_IKS(stack
);
1774 statep
->k_eip
= (unsigned long) Thread_continue
;
1775 statep
->k_ebx
= (unsigned long) thread_continue
;
1776 statep
->k_esp
= (unsigned long) STACK_IKS(stack
);
1783 * move a stack from old to new thread
1787 machine_stack_handoff(thread_t old
,
1797 stack
= old
->kernel_stack
;
1798 if (stack
== old
->reserved_stack
) {
1799 assert(new->reserved_stack
);
1800 old
->reserved_stack
= new->reserved_stack
;
1801 new->reserved_stack
= stack
;
1803 old
->kernel_stack
= 0;
1805 * A full call to machine_stack_attach() is unnecessry
1806 * because old stack is already initialized.
1808 new->kernel_stack
= stack
;
1810 fpu_switch_context(old
, new);
1812 old
->machine
.specFlags
&= ~OnProc
;
1813 new->machine
.specFlags
|= OnProc
;
1815 pmap_switch_context(old
, new, cpu_number());
1816 act_machine_switch_pcb(old
, new);
1819 ml_hv_cswitch(old
, new);
1822 machine_set_current_thread(new);
1823 thread_initialize_kernel_state(new);
1831 struct x86_act_context32
{
1832 x86_saved_state32_t ss
;
1833 x86_float_state32_t fs
;
1834 x86_debug_state32_t ds
;
1837 struct x86_act_context64
{
1838 x86_saved_state64_t ss
;
1839 x86_float_state64_t fs
;
1840 x86_debug_state64_t ds
;
1846 act_thread_csave(void)
1849 mach_msg_type_number_t val
;
1850 thread_t thr_act
= current_thread();
1852 if (thread_is_64bit(thr_act
)) {
1853 struct x86_act_context64
*ic64
;
1855 ic64
= (struct x86_act_context64
*)kalloc(sizeof(struct x86_act_context64
));
1857 if (ic64
== (struct x86_act_context64
*)NULL
)
1860 val
= x86_SAVED_STATE64_COUNT
;
1861 kret
= machine_thread_get_state(thr_act
, x86_SAVED_STATE64
,
1862 (thread_state_t
) &ic64
->ss
, &val
);
1863 if (kret
!= KERN_SUCCESS
) {
1864 kfree(ic64
, sizeof(struct x86_act_context64
));
1867 val
= x86_FLOAT_STATE64_COUNT
;
1868 kret
= machine_thread_get_state(thr_act
, x86_FLOAT_STATE64
,
1869 (thread_state_t
) &ic64
->fs
, &val
);
1870 if (kret
!= KERN_SUCCESS
) {
1871 kfree(ic64
, sizeof(struct x86_act_context64
));
1875 val
= x86_DEBUG_STATE64_COUNT
;
1876 kret
= machine_thread_get_state(thr_act
,
1878 (thread_state_t
)&ic64
->ds
,
1880 if (kret
!= KERN_SUCCESS
) {
1881 kfree(ic64
, sizeof(struct x86_act_context64
));
1887 struct x86_act_context32
*ic32
;
1889 ic32
= (struct x86_act_context32
*)kalloc(sizeof(struct x86_act_context32
));
1891 if (ic32
== (struct x86_act_context32
*)NULL
)
1894 val
= x86_SAVED_STATE32_COUNT
;
1895 kret
= machine_thread_get_state(thr_act
, x86_SAVED_STATE32
,
1896 (thread_state_t
) &ic32
->ss
, &val
);
1897 if (kret
!= KERN_SUCCESS
) {
1898 kfree(ic32
, sizeof(struct x86_act_context32
));
1901 val
= x86_FLOAT_STATE32_COUNT
;
1902 kret
= machine_thread_get_state(thr_act
, x86_FLOAT_STATE32
,
1903 (thread_state_t
) &ic32
->fs
, &val
);
1904 if (kret
!= KERN_SUCCESS
) {
1905 kfree(ic32
, sizeof(struct x86_act_context32
));
1909 val
= x86_DEBUG_STATE32_COUNT
;
1910 kret
= machine_thread_get_state(thr_act
,
1912 (thread_state_t
)&ic32
->ds
,
1914 if (kret
!= KERN_SUCCESS
) {
1915 kfree(ic32
, sizeof(struct x86_act_context32
));
1924 act_thread_catt(void *ctx
)
1926 thread_t thr_act
= current_thread();
1929 if (ctx
== (void *)NULL
)
1932 if (thread_is_64bit(thr_act
)) {
1933 struct x86_act_context64
*ic64
;
1935 ic64
= (struct x86_act_context64
*)ctx
;
1937 kret
= machine_thread_set_state(thr_act
, x86_SAVED_STATE64
,
1938 (thread_state_t
) &ic64
->ss
, x86_SAVED_STATE64_COUNT
);
1939 if (kret
== KERN_SUCCESS
) {
1940 machine_thread_set_state(thr_act
, x86_FLOAT_STATE64
,
1941 (thread_state_t
) &ic64
->fs
, x86_FLOAT_STATE64_COUNT
);
1943 kfree(ic64
, sizeof(struct x86_act_context64
));
1945 struct x86_act_context32
*ic32
;
1947 ic32
= (struct x86_act_context32
*)ctx
;
1949 kret
= machine_thread_set_state(thr_act
, x86_SAVED_STATE32
,
1950 (thread_state_t
) &ic32
->ss
, x86_SAVED_STATE32_COUNT
);
1951 if (kret
== KERN_SUCCESS
) {
1952 (void) machine_thread_set_state(thr_act
, x86_FLOAT_STATE32
,
1953 (thread_state_t
) &ic32
->fs
, x86_FLOAT_STATE32_COUNT
);
1955 kfree(ic32
, sizeof(struct x86_act_context32
));
1960 void act_thread_cfree(__unused
void *ctx
)
1966 * Duplicate one x86_debug_state32_t to another. "all" parameter
1967 * chooses whether dr4 and dr5 are copied (they are never meant
1968 * to be installed when we do machine_task_set_state() or
1969 * machine_thread_set_state()).
1973 x86_debug_state32_t
*src
,
1974 x86_debug_state32_t
*target
,
1978 target
->dr4
= src
->dr4
;
1979 target
->dr5
= src
->dr5
;
1982 target
->dr0
= src
->dr0
;
1983 target
->dr1
= src
->dr1
;
1984 target
->dr2
= src
->dr2
;
1985 target
->dr3
= src
->dr3
;
1986 target
->dr6
= src
->dr6
;
1987 target
->dr7
= src
->dr7
;
1991 * Duplicate one x86_debug_state64_t to another. "all" parameter
1992 * chooses whether dr4 and dr5 are copied (they are never meant
1993 * to be installed when we do machine_task_set_state() or
1994 * machine_thread_set_state()).
1998 x86_debug_state64_t
*src
,
1999 x86_debug_state64_t
*target
,
2003 target
->dr4
= src
->dr4
;
2004 target
->dr5
= src
->dr5
;
2007 target
->dr0
= src
->dr0
;
2008 target
->dr1
= src
->dr1
;
2009 target
->dr2
= src
->dr2
;
2010 target
->dr3
= src
->dr3
;
2011 target
->dr6
= src
->dr6
;
2012 target
->dr7
= src
->dr7
;