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
*ids
;
276 pcb
= THREAD_TO_PCB(thread
);
279 if (debug_state_is_valid32(ds
) != TRUE
) {
280 return KERN_INVALID_ARGUMENT
;
284 ids
= zalloc(ids_zone
);
285 bzero(ids
, sizeof *ids
);
287 simple_lock(&pcb
->lock
);
288 /* make sure it wasn't already alloc()'d elsewhere */
289 if (pcb
->ids
== NULL
) {
291 simple_unlock(&pcb
->lock
);
293 simple_unlock(&pcb
->lock
);
294 zfree(ids_zone
, ids
);
299 copy_debug_state32(ds
, ids
, FALSE
);
301 return (KERN_SUCCESS
);
305 set_debug_state64(thread_t thread
, x86_debug_state64_t
*ds
)
307 x86_debug_state64_t
*ids
;
310 pcb
= THREAD_TO_PCB(thread
);
313 if (debug_state_is_valid64(ds
) != TRUE
) {
314 return KERN_INVALID_ARGUMENT
;
318 ids
= zalloc(ids_zone
);
319 bzero(ids
, sizeof *ids
);
322 if (thread
->hv_thread_target
) {
323 hv_callbacks
.volatile_state(thread
->hv_thread_target
,
328 simple_lock(&pcb
->lock
);
329 /* make sure it wasn't already alloc()'d elsewhere */
330 if (pcb
->ids
== NULL
) {
332 simple_unlock(&pcb
->lock
);
334 simple_unlock(&pcb
->lock
);
335 zfree(ids_zone
, ids
);
339 copy_debug_state64(ds
, ids
, FALSE
);
341 return (KERN_SUCCESS
);
345 get_debug_state32(thread_t thread
, x86_debug_state32_t
*ds
)
347 x86_debug_state32_t
*saved_state
;
349 saved_state
= thread
->machine
.ids
;
352 copy_debug_state32(saved_state
, ds
, TRUE
);
354 bzero(ds
, sizeof *ds
);
358 get_debug_state64(thread_t thread
, x86_debug_state64_t
*ds
)
360 x86_debug_state64_t
*saved_state
;
362 saved_state
= (x86_debug_state64_t
*)thread
->machine
.ids
;
365 copy_debug_state64(saved_state
, ds
, TRUE
);
367 bzero(ds
, sizeof *ds
);
371 * consider_machine_collect:
373 * Try to collect machine-dependent pages
376 consider_machine_collect(void)
381 consider_machine_adjust(void)
386 * Switch to the first thread on a CPU.
389 machine_load_context(
392 new->machine
.specFlags
|= OnProc
;
393 act_machine_switch_pcb(NULL
, new);
397 static inline void pmap_switch_context(thread_t ot
, thread_t nt
, int cnum
) {
398 pmap_assert(ml_get_interrupts_enabled() == FALSE
);
399 vm_map_t nmap
= nt
->map
, omap
= ot
->map
;
400 if ((omap
!= nmap
) || (nmap
->pmap
->pagezero_accessible
)) {
401 PMAP_DEACTIVATE_MAP(omap
, ot
, cnum
);
402 PMAP_ACTIVATE_MAP(nmap
, nt
, cnum
);
407 * Switch to a new thread.
408 * Save the old thread`s kernel state or continuation,
412 machine_switch_context(
414 thread_continue_t continuation
,
417 assert(current_cpu_datap()->cpu_active_stack
== old
->kernel_stack
);
424 * Save FP registers if in use.
426 fpu_switch_context(old
, new);
428 old
->machine
.specFlags
&= ~OnProc
;
429 new->machine
.specFlags
|= OnProc
;
432 * Monitor the stack depth and report new max,
433 * not worrying about races.
435 vm_offset_t depth
= current_stack_depth();
436 if (depth
> kernel_stack_depth_max
) {
437 kernel_stack_depth_max
= depth
;
438 KERNEL_DEBUG_CONSTANT(
439 MACHDBG_CODE(DBG_MACH_SCHED
, MACH_STACK_DEPTH
),
440 (long) depth
, 0, 0, 0, 0);
444 * Switch address maps if need be, even if not switching tasks.
445 * (A server activation may be "borrowing" a client map.)
447 pmap_switch_context(old
, new, cpu_number());
450 * Load the rest of the user state for the new thread
452 act_machine_switch_pcb(old
, new);
455 ml_hv_cswitch(old
, new);
458 return(Switch_context(old
, continuation
, new));
462 machine_processor_shutdown(
464 void (*doshutdown
)(processor_t
),
465 processor_t processor
)
470 fpu_switch_context(thread
, NULL
);
471 pmap_switch_context(thread
, processor
->idle_thread
, cpu_number());
472 return(Shutdown_context(thread
, doshutdown
, processor
));
477 * This is where registers that are not normally specified by the mach-o
478 * file on an execve would be nullified, perhaps to avoid a covert channel.
481 machine_thread_state_initialize(
485 * If there's an fpu save area, free it.
486 * The initialized state will then be lazily faulted-in, if required.
487 * And if we're target, re-arm the no-fpu trap.
489 if (thread
->machine
.ifps
) {
490 (void) fpu_set_fxstate(thread
, NULL
, x86_FLOAT_STATE64
);
492 if (thread
== current_thread())
496 if (thread
->machine
.ids
) {
497 zfree(ids_zone
, thread
->machine
.ids
);
498 thread
->machine
.ids
= NULL
;
505 get_eflags_exportmask(void)
511 * x86_SAVED_STATE32 - internal save/restore general register state on 32/64 bit processors
512 * for 32bit tasks only
513 * x86_SAVED_STATE64 - internal save/restore general register state on 64 bit processors
514 * for 64bit tasks only
515 * x86_THREAD_STATE32 - external set/get general register state on 32/64 bit processors
516 * for 32bit tasks only
517 * x86_THREAD_STATE64 - external set/get general register state on 64 bit processors
518 * for 64bit tasks only
519 * x86_SAVED_STATE - external set/get general register state on 32/64 bit processors
520 * for either 32bit or 64bit tasks
521 * x86_FLOAT_STATE32 - internal/external save/restore float and xmm state on 32/64 bit processors
522 * for 32bit tasks only
523 * x86_FLOAT_STATE64 - internal/external save/restore float and xmm state on 64 bit processors
524 * for 64bit tasks only
525 * x86_FLOAT_STATE - external save/restore float and xmm state on 32/64 bit processors
526 * for either 32bit or 64bit tasks
527 * x86_EXCEPTION_STATE32 - external get exception state on 32/64 bit processors
528 * for 32bit tasks only
529 * x86_EXCEPTION_STATE64 - external get exception state on 64 bit processors
530 * for 64bit tasks only
531 * x86_EXCEPTION_STATE - external get exception state on 323/64 bit processors
532 * for either 32bit or 64bit tasks
537 get_exception_state64(thread_t thread
, x86_exception_state64_t
*es
)
539 x86_saved_state64_t
*saved_state
;
541 saved_state
= USER_REGS64(thread
);
543 es
->trapno
= saved_state
->isf
.trapno
;
544 es
->cpu
= saved_state
->isf
.cpu
;
545 es
->err
= (typeof(es
->err
))saved_state
->isf
.err
;
546 es
->faultvaddr
= saved_state
->cr2
;
550 get_exception_state32(thread_t thread
, x86_exception_state32_t
*es
)
552 x86_saved_state32_t
*saved_state
;
554 saved_state
= USER_REGS32(thread
);
556 es
->trapno
= saved_state
->trapno
;
557 es
->cpu
= saved_state
->cpu
;
558 es
->err
= saved_state
->err
;
559 es
->faultvaddr
= saved_state
->cr2
;
564 set_thread_state32(thread_t thread
, x86_thread_state32_t
*ts
)
566 x86_saved_state32_t
*saved_state
;
568 pal_register_cache_state(thread
, DIRTY
);
570 saved_state
= USER_REGS32(thread
);
573 * Scrub segment selector values:
577 * On a 64 bit kernel, we always override the data segments,
578 * as the actual selector numbers have changed. This also
579 * means that we don't support setting the data segments
586 /* Set GS to CTHREAD only if's been established */
587 ts
->gs
= thread
->machine
.cthread_self
? USER_CTHREAD
: NULL_SEG
;
589 /* Check segment selectors are safe */
590 if (!valid_user_segment_selectors(ts
->cs
,
596 return(KERN_INVALID_ARGUMENT
);
598 saved_state
->eax
= ts
->eax
;
599 saved_state
->ebx
= ts
->ebx
;
600 saved_state
->ecx
= ts
->ecx
;
601 saved_state
->edx
= ts
->edx
;
602 saved_state
->edi
= ts
->edi
;
603 saved_state
->esi
= ts
->esi
;
604 saved_state
->ebp
= ts
->ebp
;
605 saved_state
->uesp
= ts
->esp
;
606 saved_state
->efl
= (ts
->eflags
& ~EFL_USER_CLEAR
) | EFL_USER_SET
;
607 saved_state
->eip
= ts
->eip
;
608 saved_state
->cs
= ts
->cs
;
609 saved_state
->ss
= ts
->ss
;
610 saved_state
->ds
= ts
->ds
;
611 saved_state
->es
= ts
->es
;
612 saved_state
->fs
= ts
->fs
;
613 saved_state
->gs
= ts
->gs
;
616 * If the trace trap bit is being set,
617 * ensure that the user returns via iret
618 * - which is signaled thusly:
620 if ((saved_state
->efl
& EFL_TF
) && saved_state
->cs
== SYSENTER_CS
)
621 saved_state
->cs
= SYSENTER_TF_CS
;
623 return(KERN_SUCCESS
);
627 set_thread_state64(thread_t thread
, x86_thread_state64_t
*ts
)
629 x86_saved_state64_t
*saved_state
;
631 pal_register_cache_state(thread
, DIRTY
);
633 saved_state
= USER_REGS64(thread
);
635 if (!IS_USERADDR64_CANONICAL(ts
->rsp
) ||
636 !IS_USERADDR64_CANONICAL(ts
->rip
))
637 return(KERN_INVALID_ARGUMENT
);
639 saved_state
->r8
= ts
->r8
;
640 saved_state
->r9
= ts
->r9
;
641 saved_state
->r10
= ts
->r10
;
642 saved_state
->r11
= ts
->r11
;
643 saved_state
->r12
= ts
->r12
;
644 saved_state
->r13
= ts
->r13
;
645 saved_state
->r14
= ts
->r14
;
646 saved_state
->r15
= ts
->r15
;
647 saved_state
->rax
= ts
->rax
;
648 saved_state
->rbx
= ts
->rbx
;
649 saved_state
->rcx
= ts
->rcx
;
650 saved_state
->rdx
= ts
->rdx
;
651 saved_state
->rdi
= ts
->rdi
;
652 saved_state
->rsi
= ts
->rsi
;
653 saved_state
->rbp
= ts
->rbp
;
654 saved_state
->isf
.rsp
= ts
->rsp
;
655 saved_state
->isf
.rflags
= (ts
->rflags
& ~EFL_USER_CLEAR
) | EFL_USER_SET
;
656 saved_state
->isf
.rip
= ts
->rip
;
657 saved_state
->isf
.cs
= USER64_CS
;
658 saved_state
->fs
= (uint32_t)ts
->fs
;
659 saved_state
->gs
= (uint32_t)ts
->gs
;
661 return(KERN_SUCCESS
);
667 get_thread_state32(thread_t thread
, x86_thread_state32_t
*ts
)
669 x86_saved_state32_t
*saved_state
;
671 pal_register_cache_state(thread
, VALID
);
673 saved_state
= USER_REGS32(thread
);
675 ts
->eax
= saved_state
->eax
;
676 ts
->ebx
= saved_state
->ebx
;
677 ts
->ecx
= saved_state
->ecx
;
678 ts
->edx
= saved_state
->edx
;
679 ts
->edi
= saved_state
->edi
;
680 ts
->esi
= saved_state
->esi
;
681 ts
->ebp
= saved_state
->ebp
;
682 ts
->esp
= saved_state
->uesp
;
683 ts
->eflags
= saved_state
->efl
;
684 ts
->eip
= saved_state
->eip
;
685 ts
->cs
= saved_state
->cs
;
686 ts
->ss
= saved_state
->ss
;
687 ts
->ds
= saved_state
->ds
;
688 ts
->es
= saved_state
->es
;
689 ts
->fs
= saved_state
->fs
;
690 ts
->gs
= saved_state
->gs
;
695 get_thread_state64(thread_t thread
, x86_thread_state64_t
*ts
)
697 x86_saved_state64_t
*saved_state
;
699 pal_register_cache_state(thread
, VALID
);
701 saved_state
= USER_REGS64(thread
);
703 ts
->r8
= saved_state
->r8
;
704 ts
->r9
= saved_state
->r9
;
705 ts
->r10
= saved_state
->r10
;
706 ts
->r11
= saved_state
->r11
;
707 ts
->r12
= saved_state
->r12
;
708 ts
->r13
= saved_state
->r13
;
709 ts
->r14
= saved_state
->r14
;
710 ts
->r15
= saved_state
->r15
;
711 ts
->rax
= saved_state
->rax
;
712 ts
->rbx
= saved_state
->rbx
;
713 ts
->rcx
= saved_state
->rcx
;
714 ts
->rdx
= saved_state
->rdx
;
715 ts
->rdi
= saved_state
->rdi
;
716 ts
->rsi
= saved_state
->rsi
;
717 ts
->rbp
= saved_state
->rbp
;
718 ts
->rsp
= saved_state
->isf
.rsp
;
719 ts
->rflags
= saved_state
->isf
.rflags
;
720 ts
->rip
= saved_state
->isf
.rip
;
721 ts
->cs
= saved_state
->isf
.cs
;
722 ts
->fs
= saved_state
->fs
;
723 ts
->gs
= saved_state
->gs
;
728 * act_machine_set_state:
730 * Set the status of the specified thread.
734 machine_thread_set_state(
736 thread_flavor_t flavor
,
737 thread_state_t tstate
,
738 mach_msg_type_number_t count
)
741 case x86_SAVED_STATE32
:
743 x86_saved_state32_t
*state
;
744 x86_saved_state32_t
*saved_state
;
746 if (count
< x86_SAVED_STATE32_COUNT
)
747 return(KERN_INVALID_ARGUMENT
);
749 if (thread_is_64bit(thr_act
))
750 return(KERN_INVALID_ARGUMENT
);
752 state
= (x86_saved_state32_t
*) tstate
;
754 /* Check segment selectors are safe */
755 if (!valid_user_segment_selectors(state
->cs
,
761 return KERN_INVALID_ARGUMENT
;
763 pal_register_cache_state(thr_act
, DIRTY
);
765 saved_state
= USER_REGS32(thr_act
);
770 saved_state
->edi
= state
->edi
;
771 saved_state
->esi
= state
->esi
;
772 saved_state
->ebp
= state
->ebp
;
773 saved_state
->uesp
= state
->uesp
;
774 saved_state
->ebx
= state
->ebx
;
775 saved_state
->edx
= state
->edx
;
776 saved_state
->ecx
= state
->ecx
;
777 saved_state
->eax
= state
->eax
;
778 saved_state
->eip
= state
->eip
;
780 saved_state
->efl
= (state
->efl
& ~EFL_USER_CLEAR
) | EFL_USER_SET
;
783 * If the trace trap bit is being set,
784 * ensure that the user returns via iret
785 * - which is signaled thusly:
787 if ((saved_state
->efl
& EFL_TF
) && state
->cs
== SYSENTER_CS
)
788 state
->cs
= SYSENTER_TF_CS
;
791 * User setting segment registers.
792 * Code and stack selectors have already been
793 * checked. Others will be reset by 'iret'
794 * if they are not valid.
796 saved_state
->cs
= state
->cs
;
797 saved_state
->ss
= state
->ss
;
798 saved_state
->ds
= state
->ds
;
799 saved_state
->es
= state
->es
;
800 saved_state
->fs
= state
->fs
;
801 saved_state
->gs
= state
->gs
;
806 case x86_SAVED_STATE64
:
808 x86_saved_state64_t
*state
;
809 x86_saved_state64_t
*saved_state
;
811 if (count
< x86_SAVED_STATE64_COUNT
)
812 return(KERN_INVALID_ARGUMENT
);
814 if (!thread_is_64bit(thr_act
))
815 return(KERN_INVALID_ARGUMENT
);
817 state
= (x86_saved_state64_t
*) tstate
;
819 /* Verify that the supplied code segment selector is
820 * valid. In 64-bit mode, the FS and GS segment overrides
821 * use the FS.base and GS.base MSRs to calculate
822 * base addresses, and the trampolines don't directly
823 * restore the segment registers--hence they are no
824 * longer relevant for validation.
826 if (!valid_user_code_selector(state
->isf
.cs
))
827 return KERN_INVALID_ARGUMENT
;
829 /* Check pc and stack are canonical addresses */
830 if (!IS_USERADDR64_CANONICAL(state
->isf
.rsp
) ||
831 !IS_USERADDR64_CANONICAL(state
->isf
.rip
))
832 return KERN_INVALID_ARGUMENT
;
834 pal_register_cache_state(thr_act
, DIRTY
);
836 saved_state
= USER_REGS64(thr_act
);
841 saved_state
->r8
= state
->r8
;
842 saved_state
->r9
= state
->r9
;
843 saved_state
->r10
= state
->r10
;
844 saved_state
->r11
= state
->r11
;
845 saved_state
->r12
= state
->r12
;
846 saved_state
->r13
= state
->r13
;
847 saved_state
->r14
= state
->r14
;
848 saved_state
->r15
= state
->r15
;
849 saved_state
->rdi
= state
->rdi
;
850 saved_state
->rsi
= state
->rsi
;
851 saved_state
->rbp
= state
->rbp
;
852 saved_state
->rbx
= state
->rbx
;
853 saved_state
->rdx
= state
->rdx
;
854 saved_state
->rcx
= state
->rcx
;
855 saved_state
->rax
= state
->rax
;
856 saved_state
->isf
.rsp
= state
->isf
.rsp
;
857 saved_state
->isf
.rip
= state
->isf
.rip
;
859 saved_state
->isf
.rflags
= (state
->isf
.rflags
& ~EFL_USER_CLEAR
) | EFL_USER_SET
;
862 * User setting segment registers.
863 * Code and stack selectors have already been
864 * checked. Others will be reset by 'sys'
865 * if they are not valid.
867 saved_state
->isf
.cs
= state
->isf
.cs
;
868 saved_state
->isf
.ss
= state
->isf
.ss
;
869 saved_state
->fs
= state
->fs
;
870 saved_state
->gs
= state
->gs
;
875 case x86_FLOAT_STATE32
:
876 case x86_AVX_STATE32
:
877 #if !defined(RC_HIDE_XNU_J137)
878 case x86_AVX512_STATE32
:
879 #endif /* not RC_HIDE_XNU_J137 */
881 if (count
!= _MachineStateCount
[flavor
])
882 return(KERN_INVALID_ARGUMENT
);
884 if (thread_is_64bit(thr_act
))
885 return(KERN_INVALID_ARGUMENT
);
887 return fpu_set_fxstate(thr_act
, tstate
, flavor
);
890 case x86_FLOAT_STATE64
:
891 case x86_AVX_STATE64
:
892 #if !defined(RC_HIDE_XNU_J137)
893 case x86_AVX512_STATE64
:
894 #endif /* not RC_HIDE_XNU_J137 */
896 if (count
!= _MachineStateCount
[flavor
])
897 return(KERN_INVALID_ARGUMENT
);
899 if (!thread_is_64bit(thr_act
))
900 return(KERN_INVALID_ARGUMENT
);
902 return fpu_set_fxstate(thr_act
, tstate
, flavor
);
905 case x86_FLOAT_STATE
:
907 x86_float_state_t
*state
;
909 if (count
!= x86_FLOAT_STATE_COUNT
)
910 return(KERN_INVALID_ARGUMENT
);
912 state
= (x86_float_state_t
*)tstate
;
913 if (state
->fsh
.flavor
== x86_FLOAT_STATE64
&& state
->fsh
.count
== x86_FLOAT_STATE64_COUNT
&&
914 thread_is_64bit(thr_act
)) {
915 return fpu_set_fxstate(thr_act
, (thread_state_t
)&state
->ufs
.fs64
, x86_FLOAT_STATE64
);
917 if (state
->fsh
.flavor
== x86_FLOAT_STATE32
&& state
->fsh
.count
== x86_FLOAT_STATE32_COUNT
&&
918 !thread_is_64bit(thr_act
)) {
919 return fpu_set_fxstate(thr_act
, (thread_state_t
)&state
->ufs
.fs32
, x86_FLOAT_STATE32
);
921 return(KERN_INVALID_ARGUMENT
);
925 #if !defined(RC_HIDE_XNU_J137)
926 case x86_AVX512_STATE
:
929 x86_avx_state_t
*state
;
931 if (count
!= _MachineStateCount
[flavor
])
932 return(KERN_INVALID_ARGUMENT
);
934 state
= (x86_avx_state_t
*)tstate
;
935 /* Flavors are defined to have sequential values: 32-bit, 64-bit, non-specific */
937 if (state
->ash
.flavor
== (flavor
- 1) &&
938 state
->ash
.count
== _MachineStateCount
[flavor
- 1] &&
939 thread_is_64bit(thr_act
)) {
940 return fpu_set_fxstate(thr_act
,
941 (thread_state_t
)&state
->ufs
.as64
,
945 if (state
->ash
.flavor
== (flavor
- 2) &&
946 state
->ash
.count
== _MachineStateCount
[flavor
- 2] &&
947 !thread_is_64bit(thr_act
)) {
948 return fpu_set_fxstate(thr_act
,
949 (thread_state_t
)&state
->ufs
.as32
,
952 return(KERN_INVALID_ARGUMENT
);
955 case x86_THREAD_STATE32
:
957 if (count
!= x86_THREAD_STATE32_COUNT
)
958 return(KERN_INVALID_ARGUMENT
);
960 if (thread_is_64bit(thr_act
))
961 return(KERN_INVALID_ARGUMENT
);
963 return set_thread_state32(thr_act
, (x86_thread_state32_t
*)tstate
);
966 case x86_THREAD_STATE64
:
968 if (count
!= x86_THREAD_STATE64_COUNT
)
969 return(KERN_INVALID_ARGUMENT
);
971 if (!thread_is_64bit(thr_act
))
972 return(KERN_INVALID_ARGUMENT
);
974 return set_thread_state64(thr_act
, (x86_thread_state64_t
*)tstate
);
977 case x86_THREAD_STATE
:
979 x86_thread_state_t
*state
;
981 if (count
!= x86_THREAD_STATE_COUNT
)
982 return(KERN_INVALID_ARGUMENT
);
984 state
= (x86_thread_state_t
*)tstate
;
986 if (state
->tsh
.flavor
== x86_THREAD_STATE64
&&
987 state
->tsh
.count
== x86_THREAD_STATE64_COUNT
&&
988 thread_is_64bit(thr_act
)) {
989 return set_thread_state64(thr_act
, &state
->uts
.ts64
);
990 } else if (state
->tsh
.flavor
== x86_THREAD_STATE32
&&
991 state
->tsh
.count
== x86_THREAD_STATE32_COUNT
&&
992 !thread_is_64bit(thr_act
)) {
993 return set_thread_state32(thr_act
, &state
->uts
.ts32
);
995 return(KERN_INVALID_ARGUMENT
);
997 case x86_DEBUG_STATE32
:
999 x86_debug_state32_t
*state
;
1002 if (thread_is_64bit(thr_act
))
1003 return(KERN_INVALID_ARGUMENT
);
1005 state
= (x86_debug_state32_t
*)tstate
;
1007 ret
= set_debug_state32(thr_act
, state
);
1011 case x86_DEBUG_STATE64
:
1013 x86_debug_state64_t
*state
;
1016 if (!thread_is_64bit(thr_act
))
1017 return(KERN_INVALID_ARGUMENT
);
1019 state
= (x86_debug_state64_t
*)tstate
;
1021 ret
= set_debug_state64(thr_act
, state
);
1025 case x86_DEBUG_STATE
:
1027 x86_debug_state_t
*state
;
1028 kern_return_t ret
= KERN_INVALID_ARGUMENT
;
1030 if (count
!= x86_DEBUG_STATE_COUNT
)
1031 return (KERN_INVALID_ARGUMENT
);
1033 state
= (x86_debug_state_t
*)tstate
;
1034 if (state
->dsh
.flavor
== x86_DEBUG_STATE64
&&
1035 state
->dsh
.count
== x86_DEBUG_STATE64_COUNT
&&
1036 thread_is_64bit(thr_act
)) {
1037 ret
= set_debug_state64(thr_act
, &state
->uds
.ds64
);
1040 if (state
->dsh
.flavor
== x86_DEBUG_STATE32
&&
1041 state
->dsh
.count
== x86_DEBUG_STATE32_COUNT
&&
1042 !thread_is_64bit(thr_act
)) {
1043 ret
= set_debug_state32(thr_act
, &state
->uds
.ds32
);
1048 return(KERN_INVALID_ARGUMENT
);
1051 return(KERN_SUCCESS
);
1059 * Get the status of the specified thread.
1063 machine_thread_get_state(
1065 thread_flavor_t flavor
,
1066 thread_state_t tstate
,
1067 mach_msg_type_number_t
*count
)
1072 case THREAD_STATE_FLAVOR_LIST
:
1075 return (KERN_INVALID_ARGUMENT
);
1077 tstate
[0] = i386_THREAD_STATE
;
1078 tstate
[1] = i386_FLOAT_STATE
;
1079 tstate
[2] = i386_EXCEPTION_STATE
;
1085 case THREAD_STATE_FLAVOR_LIST_NEW
:
1088 return (KERN_INVALID_ARGUMENT
);
1090 tstate
[0] = x86_THREAD_STATE
;
1091 tstate
[1] = x86_FLOAT_STATE
;
1092 tstate
[2] = x86_EXCEPTION_STATE
;
1093 tstate
[3] = x86_DEBUG_STATE
;
1099 case THREAD_STATE_FLAVOR_LIST_10_9
:
1102 return (KERN_INVALID_ARGUMENT
);
1104 tstate
[0] = x86_THREAD_STATE
;
1105 tstate
[1] = x86_FLOAT_STATE
;
1106 tstate
[2] = x86_EXCEPTION_STATE
;
1107 tstate
[3] = x86_DEBUG_STATE
;
1108 tstate
[4] = x86_AVX_STATE
;
1114 #if !defined(RC_HIDE_XNU_J137)
1115 case THREAD_STATE_FLAVOR_LIST_10_13
:
1118 return (KERN_INVALID_ARGUMENT
);
1120 tstate
[0] = x86_THREAD_STATE
;
1121 tstate
[1] = x86_FLOAT_STATE
;
1122 tstate
[2] = x86_EXCEPTION_STATE
;
1123 tstate
[3] = x86_DEBUG_STATE
;
1124 tstate
[4] = x86_AVX_STATE
;
1125 tstate
[5] = x86_AVX512_STATE
;
1132 case x86_SAVED_STATE32
:
1134 x86_saved_state32_t
*state
;
1135 x86_saved_state32_t
*saved_state
;
1137 if (*count
< x86_SAVED_STATE32_COUNT
)
1138 return(KERN_INVALID_ARGUMENT
);
1140 if (thread_is_64bit(thr_act
))
1141 return(KERN_INVALID_ARGUMENT
);
1143 state
= (x86_saved_state32_t
*) tstate
;
1144 saved_state
= USER_REGS32(thr_act
);
1147 * First, copy everything:
1149 *state
= *saved_state
;
1150 state
->ds
= saved_state
->ds
& 0xffff;
1151 state
->es
= saved_state
->es
& 0xffff;
1152 state
->fs
= saved_state
->fs
& 0xffff;
1153 state
->gs
= saved_state
->gs
& 0xffff;
1155 *count
= x86_SAVED_STATE32_COUNT
;
1159 case x86_SAVED_STATE64
:
1161 x86_saved_state64_t
*state
;
1162 x86_saved_state64_t
*saved_state
;
1164 if (*count
< x86_SAVED_STATE64_COUNT
)
1165 return(KERN_INVALID_ARGUMENT
);
1167 if (!thread_is_64bit(thr_act
))
1168 return(KERN_INVALID_ARGUMENT
);
1170 state
= (x86_saved_state64_t
*)tstate
;
1171 saved_state
= USER_REGS64(thr_act
);
1174 * First, copy everything:
1176 *state
= *saved_state
;
1177 state
->fs
= saved_state
->fs
& 0xffff;
1178 state
->gs
= saved_state
->gs
& 0xffff;
1180 *count
= x86_SAVED_STATE64_COUNT
;
1184 case x86_FLOAT_STATE32
:
1186 if (*count
< x86_FLOAT_STATE32_COUNT
)
1187 return(KERN_INVALID_ARGUMENT
);
1189 if (thread_is_64bit(thr_act
))
1190 return(KERN_INVALID_ARGUMENT
);
1192 *count
= x86_FLOAT_STATE32_COUNT
;
1194 return fpu_get_fxstate(thr_act
, tstate
, flavor
);
1197 case x86_FLOAT_STATE64
:
1199 if (*count
< x86_FLOAT_STATE64_COUNT
)
1200 return(KERN_INVALID_ARGUMENT
);
1202 if ( !thread_is_64bit(thr_act
))
1203 return(KERN_INVALID_ARGUMENT
);
1205 *count
= x86_FLOAT_STATE64_COUNT
;
1207 return fpu_get_fxstate(thr_act
, tstate
, flavor
);
1210 case x86_FLOAT_STATE
:
1212 x86_float_state_t
*state
;
1215 if (*count
< x86_FLOAT_STATE_COUNT
)
1216 return(KERN_INVALID_ARGUMENT
);
1218 state
= (x86_float_state_t
*)tstate
;
1221 * no need to bzero... currently
1222 * x86_FLOAT_STATE64_COUNT == x86_FLOAT_STATE32_COUNT
1224 if (thread_is_64bit(thr_act
)) {
1225 state
->fsh
.flavor
= x86_FLOAT_STATE64
;
1226 state
->fsh
.count
= x86_FLOAT_STATE64_COUNT
;
1228 kret
= fpu_get_fxstate(thr_act
, (thread_state_t
)&state
->ufs
.fs64
, x86_FLOAT_STATE64
);
1230 state
->fsh
.flavor
= x86_FLOAT_STATE32
;
1231 state
->fsh
.count
= x86_FLOAT_STATE32_COUNT
;
1233 kret
= fpu_get_fxstate(thr_act
, (thread_state_t
)&state
->ufs
.fs32
, x86_FLOAT_STATE32
);
1235 *count
= x86_FLOAT_STATE_COUNT
;
1240 case x86_AVX_STATE32
:
1241 #if !defined(RC_HIDE_XNU_J137)
1242 case x86_AVX512_STATE32
:
1245 if (*count
!= _MachineStateCount
[flavor
])
1246 return(KERN_INVALID_ARGUMENT
);
1248 if (thread_is_64bit(thr_act
))
1249 return(KERN_INVALID_ARGUMENT
);
1251 *count
= _MachineStateCount
[flavor
];
1253 return fpu_get_fxstate(thr_act
, tstate
, flavor
);
1256 case x86_AVX_STATE64
:
1257 #if !defined(RC_HIDE_XNU_J137)
1258 case x86_AVX512_STATE64
:
1261 if (*count
!= _MachineStateCount
[flavor
])
1262 return(KERN_INVALID_ARGUMENT
);
1264 if ( !thread_is_64bit(thr_act
))
1265 return(KERN_INVALID_ARGUMENT
);
1267 *count
= _MachineStateCount
[flavor
];
1269 return fpu_get_fxstate(thr_act
, tstate
, flavor
);
1273 #if !defined(RC_HIDE_XNU_J137)
1274 case x86_AVX512_STATE
:
1277 x86_avx_state_t
*state
;
1278 thread_state_t fstate
;
1280 if (*count
< _MachineStateCount
[flavor
])
1281 return(KERN_INVALID_ARGUMENT
);
1283 *count
= _MachineStateCount
[flavor
];
1284 state
= (x86_avx_state_t
*)tstate
;
1286 bzero((char *)state
, *count
* sizeof(int));
1288 if (thread_is_64bit(thr_act
)) {
1289 flavor
-= 1; /* 64-bit flavor */
1290 fstate
= (thread_state_t
) &state
->ufs
.as64
;
1292 flavor
-= 2; /* 32-bit flavor */
1293 fstate
= (thread_state_t
) &state
->ufs
.as32
;
1295 state
->ash
.flavor
= flavor
;
1296 state
->ash
.count
= _MachineStateCount
[flavor
];
1298 return fpu_get_fxstate(thr_act
, fstate
, flavor
);
1301 case x86_THREAD_STATE32
:
1303 if (*count
< x86_THREAD_STATE32_COUNT
)
1304 return(KERN_INVALID_ARGUMENT
);
1306 if (thread_is_64bit(thr_act
))
1307 return(KERN_INVALID_ARGUMENT
);
1309 *count
= x86_THREAD_STATE32_COUNT
;
1311 get_thread_state32(thr_act
, (x86_thread_state32_t
*)tstate
);
1315 case x86_THREAD_STATE64
:
1317 if (*count
< x86_THREAD_STATE64_COUNT
)
1318 return(KERN_INVALID_ARGUMENT
);
1320 if ( !thread_is_64bit(thr_act
))
1321 return(KERN_INVALID_ARGUMENT
);
1323 *count
= x86_THREAD_STATE64_COUNT
;
1325 get_thread_state64(thr_act
, (x86_thread_state64_t
*)tstate
);
1329 case x86_THREAD_STATE
:
1331 x86_thread_state_t
*state
;
1333 if (*count
< x86_THREAD_STATE_COUNT
)
1334 return(KERN_INVALID_ARGUMENT
);
1336 state
= (x86_thread_state_t
*)tstate
;
1338 bzero((char *)state
, sizeof(x86_thread_state_t
));
1340 if (thread_is_64bit(thr_act
)) {
1341 state
->tsh
.flavor
= x86_THREAD_STATE64
;
1342 state
->tsh
.count
= x86_THREAD_STATE64_COUNT
;
1344 get_thread_state64(thr_act
, &state
->uts
.ts64
);
1346 state
->tsh
.flavor
= x86_THREAD_STATE32
;
1347 state
->tsh
.count
= x86_THREAD_STATE32_COUNT
;
1349 get_thread_state32(thr_act
, &state
->uts
.ts32
);
1351 *count
= x86_THREAD_STATE_COUNT
;
1357 case x86_EXCEPTION_STATE32
:
1359 if (*count
< x86_EXCEPTION_STATE32_COUNT
)
1360 return(KERN_INVALID_ARGUMENT
);
1362 if (thread_is_64bit(thr_act
))
1363 return(KERN_INVALID_ARGUMENT
);
1365 *count
= x86_EXCEPTION_STATE32_COUNT
;
1367 get_exception_state32(thr_act
, (x86_exception_state32_t
*)tstate
);
1369 * Suppress the cpu number for binary compatibility
1370 * of this deprecated state.
1372 ((x86_exception_state32_t
*)tstate
)->cpu
= 0;
1376 case x86_EXCEPTION_STATE64
:
1378 if (*count
< x86_EXCEPTION_STATE64_COUNT
)
1379 return(KERN_INVALID_ARGUMENT
);
1381 if ( !thread_is_64bit(thr_act
))
1382 return(KERN_INVALID_ARGUMENT
);
1384 *count
= x86_EXCEPTION_STATE64_COUNT
;
1386 get_exception_state64(thr_act
, (x86_exception_state64_t
*)tstate
);
1388 * Suppress the cpu number for binary compatibility
1389 * of this deprecated state.
1391 ((x86_exception_state64_t
*)tstate
)->cpu
= 0;
1395 case x86_EXCEPTION_STATE
:
1397 x86_exception_state_t
*state
;
1399 if (*count
< x86_EXCEPTION_STATE_COUNT
)
1400 return(KERN_INVALID_ARGUMENT
);
1402 state
= (x86_exception_state_t
*)tstate
;
1404 bzero((char *)state
, sizeof(x86_exception_state_t
));
1406 if (thread_is_64bit(thr_act
)) {
1407 state
->esh
.flavor
= x86_EXCEPTION_STATE64
;
1408 state
->esh
.count
= x86_EXCEPTION_STATE64_COUNT
;
1410 get_exception_state64(thr_act
, &state
->ues
.es64
);
1412 state
->esh
.flavor
= x86_EXCEPTION_STATE32
;
1413 state
->esh
.count
= x86_EXCEPTION_STATE32_COUNT
;
1415 get_exception_state32(thr_act
, &state
->ues
.es32
);
1417 *count
= x86_EXCEPTION_STATE_COUNT
;
1421 case x86_DEBUG_STATE32
:
1423 if (*count
< x86_DEBUG_STATE32_COUNT
)
1424 return(KERN_INVALID_ARGUMENT
);
1426 if (thread_is_64bit(thr_act
))
1427 return(KERN_INVALID_ARGUMENT
);
1429 get_debug_state32(thr_act
, (x86_debug_state32_t
*)tstate
);
1431 *count
= x86_DEBUG_STATE32_COUNT
;
1435 case x86_DEBUG_STATE64
:
1437 if (*count
< x86_DEBUG_STATE64_COUNT
)
1438 return(KERN_INVALID_ARGUMENT
);
1440 if (!thread_is_64bit(thr_act
))
1441 return(KERN_INVALID_ARGUMENT
);
1443 get_debug_state64(thr_act
, (x86_debug_state64_t
*)tstate
);
1445 *count
= x86_DEBUG_STATE64_COUNT
;
1449 case x86_DEBUG_STATE
:
1451 x86_debug_state_t
*state
;
1453 if (*count
< x86_DEBUG_STATE_COUNT
)
1454 return(KERN_INVALID_ARGUMENT
);
1456 state
= (x86_debug_state_t
*)tstate
;
1458 bzero(state
, sizeof *state
);
1460 if (thread_is_64bit(thr_act
)) {
1461 state
->dsh
.flavor
= x86_DEBUG_STATE64
;
1462 state
->dsh
.count
= x86_DEBUG_STATE64_COUNT
;
1464 get_debug_state64(thr_act
, &state
->uds
.ds64
);
1466 state
->dsh
.flavor
= x86_DEBUG_STATE32
;
1467 state
->dsh
.count
= x86_DEBUG_STATE32_COUNT
;
1469 get_debug_state32(thr_act
, &state
->uds
.ds32
);
1471 *count
= x86_DEBUG_STATE_COUNT
;
1475 return(KERN_INVALID_ARGUMENT
);
1478 return(KERN_SUCCESS
);
1482 machine_thread_get_kern_state(
1484 thread_flavor_t flavor
,
1485 thread_state_t tstate
,
1486 mach_msg_type_number_t
*count
)
1488 x86_saved_state_t
*int_state
= current_cpu_datap()->cpu_int_state
;
1491 * This works only for an interrupted kernel thread
1493 if (thread
!= current_thread() || int_state
== NULL
)
1494 return KERN_FAILURE
;
1497 case x86_THREAD_STATE32
: {
1498 x86_thread_state32_t
*state
;
1499 x86_saved_state32_t
*saved_state
;
1501 if (!is_saved_state32(int_state
) ||
1502 *count
< x86_THREAD_STATE32_COUNT
)
1503 return (KERN_INVALID_ARGUMENT
);
1505 state
= (x86_thread_state32_t
*) tstate
;
1507 saved_state
= saved_state32(int_state
);
1509 * General registers.
1511 state
->eax
= saved_state
->eax
;
1512 state
->ebx
= saved_state
->ebx
;
1513 state
->ecx
= saved_state
->ecx
;
1514 state
->edx
= saved_state
->edx
;
1515 state
->edi
= saved_state
->edi
;
1516 state
->esi
= saved_state
->esi
;
1517 state
->ebp
= saved_state
->ebp
;
1518 state
->esp
= saved_state
->uesp
;
1519 state
->eflags
= saved_state
->efl
;
1520 state
->eip
= saved_state
->eip
;
1521 state
->cs
= saved_state
->cs
;
1522 state
->ss
= saved_state
->ss
;
1523 state
->ds
= saved_state
->ds
& 0xffff;
1524 state
->es
= saved_state
->es
& 0xffff;
1525 state
->fs
= saved_state
->fs
& 0xffff;
1526 state
->gs
= saved_state
->gs
& 0xffff;
1528 *count
= x86_THREAD_STATE32_COUNT
;
1530 return KERN_SUCCESS
;
1533 case x86_THREAD_STATE64
: {
1534 x86_thread_state64_t
*state
;
1535 x86_saved_state64_t
*saved_state
;
1537 if (!is_saved_state64(int_state
) ||
1538 *count
< x86_THREAD_STATE64_COUNT
)
1539 return (KERN_INVALID_ARGUMENT
);
1541 state
= (x86_thread_state64_t
*) tstate
;
1543 saved_state
= saved_state64(int_state
);
1545 * General registers.
1547 state
->rax
= saved_state
->rax
;
1548 state
->rbx
= saved_state
->rbx
;
1549 state
->rcx
= saved_state
->rcx
;
1550 state
->rdx
= saved_state
->rdx
;
1551 state
->rdi
= saved_state
->rdi
;
1552 state
->rsi
= saved_state
->rsi
;
1553 state
->rbp
= saved_state
->rbp
;
1554 state
->rsp
= saved_state
->isf
.rsp
;
1555 state
->r8
= saved_state
->r8
;
1556 state
->r9
= saved_state
->r9
;
1557 state
->r10
= saved_state
->r10
;
1558 state
->r11
= saved_state
->r11
;
1559 state
->r12
= saved_state
->r12
;
1560 state
->r13
= saved_state
->r13
;
1561 state
->r14
= saved_state
->r14
;
1562 state
->r15
= saved_state
->r15
;
1564 state
->rip
= saved_state
->isf
.rip
;
1565 state
->rflags
= saved_state
->isf
.rflags
;
1566 state
->cs
= saved_state
->isf
.cs
;
1567 state
->fs
= saved_state
->fs
& 0xffff;
1568 state
->gs
= saved_state
->gs
& 0xffff;
1569 *count
= x86_THREAD_STATE64_COUNT
;
1571 return KERN_SUCCESS
;
1574 case x86_THREAD_STATE
: {
1575 x86_thread_state_t
*state
= NULL
;
1577 if (*count
< x86_THREAD_STATE_COUNT
)
1578 return (KERN_INVALID_ARGUMENT
);
1580 state
= (x86_thread_state_t
*) tstate
;
1582 if (is_saved_state32(int_state
)) {
1583 x86_saved_state32_t
*saved_state
= saved_state32(int_state
);
1585 state
->tsh
.flavor
= x86_THREAD_STATE32
;
1586 state
->tsh
.count
= x86_THREAD_STATE32_COUNT
;
1589 * General registers.
1591 state
->uts
.ts32
.eax
= saved_state
->eax
;
1592 state
->uts
.ts32
.ebx
= saved_state
->ebx
;
1593 state
->uts
.ts32
.ecx
= saved_state
->ecx
;
1594 state
->uts
.ts32
.edx
= saved_state
->edx
;
1595 state
->uts
.ts32
.edi
= saved_state
->edi
;
1596 state
->uts
.ts32
.esi
= saved_state
->esi
;
1597 state
->uts
.ts32
.ebp
= saved_state
->ebp
;
1598 state
->uts
.ts32
.esp
= saved_state
->uesp
;
1599 state
->uts
.ts32
.eflags
= saved_state
->efl
;
1600 state
->uts
.ts32
.eip
= saved_state
->eip
;
1601 state
->uts
.ts32
.cs
= saved_state
->cs
;
1602 state
->uts
.ts32
.ss
= saved_state
->ss
;
1603 state
->uts
.ts32
.ds
= saved_state
->ds
& 0xffff;
1604 state
->uts
.ts32
.es
= saved_state
->es
& 0xffff;
1605 state
->uts
.ts32
.fs
= saved_state
->fs
& 0xffff;
1606 state
->uts
.ts32
.gs
= saved_state
->gs
& 0xffff;
1607 } else if (is_saved_state64(int_state
)) {
1608 x86_saved_state64_t
*saved_state
= saved_state64(int_state
);
1610 state
->tsh
.flavor
= x86_THREAD_STATE64
;
1611 state
->tsh
.count
= x86_THREAD_STATE64_COUNT
;
1614 * General registers.
1616 state
->uts
.ts64
.rax
= saved_state
->rax
;
1617 state
->uts
.ts64
.rbx
= saved_state
->rbx
;
1618 state
->uts
.ts64
.rcx
= saved_state
->rcx
;
1619 state
->uts
.ts64
.rdx
= saved_state
->rdx
;
1620 state
->uts
.ts64
.rdi
= saved_state
->rdi
;
1621 state
->uts
.ts64
.rsi
= saved_state
->rsi
;
1622 state
->uts
.ts64
.rbp
= saved_state
->rbp
;
1623 state
->uts
.ts64
.rsp
= saved_state
->isf
.rsp
;
1624 state
->uts
.ts64
.r8
= saved_state
->r8
;
1625 state
->uts
.ts64
.r9
= saved_state
->r9
;
1626 state
->uts
.ts64
.r10
= saved_state
->r10
;
1627 state
->uts
.ts64
.r11
= saved_state
->r11
;
1628 state
->uts
.ts64
.r12
= saved_state
->r12
;
1629 state
->uts
.ts64
.r13
= saved_state
->r13
;
1630 state
->uts
.ts64
.r14
= saved_state
->r14
;
1631 state
->uts
.ts64
.r15
= saved_state
->r15
;
1633 state
->uts
.ts64
.rip
= saved_state
->isf
.rip
;
1634 state
->uts
.ts64
.rflags
= saved_state
->isf
.rflags
;
1635 state
->uts
.ts64
.cs
= saved_state
->isf
.cs
;
1636 state
->uts
.ts64
.fs
= saved_state
->fs
& 0xffff;
1637 state
->uts
.ts64
.gs
= saved_state
->gs
& 0xffff;
1639 panic("unknown thread state");
1642 *count
= x86_THREAD_STATE_COUNT
;
1643 return KERN_SUCCESS
;
1646 return KERN_FAILURE
;
1651 machine_thread_switch_addrmode(thread_t thread
)
1654 * We don't want to be preempted until we're done
1655 * - particularly if we're switching the current thread
1657 disable_preemption();
1660 * Reset the state saveareas. As we're resetting, we anticipate no
1661 * memory allocations in this path.
1663 machine_thread_create(thread
, thread
->task
);
1665 /* Adjust FPU state */
1666 fpu_switch_addrmode(thread
, task_has_64BitAddr(thread
->task
));
1668 /* If we're switching ourselves, reset the pcb addresses etc. */
1669 if (thread
== current_thread()) {
1670 boolean_t istate
= ml_set_interrupts_enabled(FALSE
);
1671 act_machine_switch_pcb(NULL
, thread
);
1672 ml_set_interrupts_enabled(istate
);
1674 enable_preemption();
1680 * This is used to set the current thr_act/thread
1681 * when starting up a new processor
1684 machine_set_current_thread(thread_t thread
)
1686 current_cpu_datap()->cpu_active_thread
= thread
;
1691 * Perform machine-dependent per-thread initializations
1694 machine_thread_init(void)
1696 iss_zone
= zinit(sizeof(x86_saved_state_t
),
1697 thread_max
* sizeof(x86_saved_state_t
),
1698 THREAD_CHUNK
* sizeof(x86_saved_state_t
),
1699 "x86_64 saved state");
1701 ids_zone
= zinit(sizeof(x86_debug_state64_t
),
1702 thread_max
* sizeof(x86_debug_state64_t
),
1703 THREAD_CHUNK
* sizeof(x86_debug_state64_t
),
1704 "x86_64 debug state");
1714 thread_t thr_act
= current_thread();
1716 if (thread_is_64bit(thr_act
)) {
1717 x86_saved_state64_t
*iss64
;
1719 iss64
= USER_REGS64(thr_act
);
1721 return(iss64
->isf
.rip
);
1723 x86_saved_state32_t
*iss32
;
1725 iss32
= USER_REGS32(thr_act
);
1732 * detach and return a kernel stack from a thread
1736 machine_stack_detach(thread_t thread
)
1740 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_SCHED
, MACH_STACK_DETACH
),
1741 (uintptr_t)thread_tid(thread
), thread
->priority
,
1742 thread
->sched_pri
, 0,
1745 stack
= thread
->kernel_stack
;
1746 thread
->kernel_stack
= 0;
1752 * attach a kernel stack to a thread and initialize it
1756 machine_stack_attach(
1760 struct x86_kernel_state
*statep
;
1762 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_SCHED
, MACH_STACK_ATTACH
),
1763 (uintptr_t)thread_tid(thread
), thread
->priority
,
1764 thread
->sched_pri
, 0, 0);
1767 thread
->kernel_stack
= stack
;
1768 thread_initialize_kernel_state(thread
);
1770 statep
= STACK_IKS(stack
);
1771 #if defined(__x86_64__)
1772 statep
->k_rip
= (unsigned long) Thread_continue
;
1773 statep
->k_rbx
= (unsigned long) thread_continue
;
1774 statep
->k_rsp
= (unsigned long) STACK_IKS(stack
);
1776 statep
->k_eip
= (unsigned long) Thread_continue
;
1777 statep
->k_ebx
= (unsigned long) thread_continue
;
1778 statep
->k_esp
= (unsigned long) STACK_IKS(stack
);
1785 * move a stack from old to new thread
1789 machine_stack_handoff(thread_t old
,
1799 stack
= old
->kernel_stack
;
1800 if (stack
== old
->reserved_stack
) {
1801 assert(new->reserved_stack
);
1802 old
->reserved_stack
= new->reserved_stack
;
1803 new->reserved_stack
= stack
;
1805 old
->kernel_stack
= 0;
1807 * A full call to machine_stack_attach() is unnecessry
1808 * because old stack is already initialized.
1810 new->kernel_stack
= stack
;
1812 fpu_switch_context(old
, new);
1814 old
->machine
.specFlags
&= ~OnProc
;
1815 new->machine
.specFlags
|= OnProc
;
1817 pmap_switch_context(old
, new, cpu_number());
1818 act_machine_switch_pcb(old
, new);
1821 ml_hv_cswitch(old
, new);
1824 machine_set_current_thread(new);
1825 thread_initialize_kernel_state(new);
1833 struct x86_act_context32
{
1834 x86_saved_state32_t ss
;
1835 x86_float_state32_t fs
;
1836 x86_debug_state32_t ds
;
1839 struct x86_act_context64
{
1840 x86_saved_state64_t ss
;
1841 x86_float_state64_t fs
;
1842 x86_debug_state64_t ds
;
1848 act_thread_csave(void)
1851 mach_msg_type_number_t val
;
1852 thread_t thr_act
= current_thread();
1854 if (thread_is_64bit(thr_act
)) {
1855 struct x86_act_context64
*ic64
;
1857 ic64
= (struct x86_act_context64
*)kalloc(sizeof(struct x86_act_context64
));
1859 if (ic64
== (struct x86_act_context64
*)NULL
)
1862 val
= x86_SAVED_STATE64_COUNT
;
1863 kret
= machine_thread_get_state(thr_act
, x86_SAVED_STATE64
,
1864 (thread_state_t
) &ic64
->ss
, &val
);
1865 if (kret
!= KERN_SUCCESS
) {
1866 kfree(ic64
, sizeof(struct x86_act_context64
));
1869 val
= x86_FLOAT_STATE64_COUNT
;
1870 kret
= machine_thread_get_state(thr_act
, x86_FLOAT_STATE64
,
1871 (thread_state_t
) &ic64
->fs
, &val
);
1872 if (kret
!= KERN_SUCCESS
) {
1873 kfree(ic64
, sizeof(struct x86_act_context64
));
1877 val
= x86_DEBUG_STATE64_COUNT
;
1878 kret
= machine_thread_get_state(thr_act
,
1880 (thread_state_t
)&ic64
->ds
,
1882 if (kret
!= KERN_SUCCESS
) {
1883 kfree(ic64
, sizeof(struct x86_act_context64
));
1889 struct x86_act_context32
*ic32
;
1891 ic32
= (struct x86_act_context32
*)kalloc(sizeof(struct x86_act_context32
));
1893 if (ic32
== (struct x86_act_context32
*)NULL
)
1896 val
= x86_SAVED_STATE32_COUNT
;
1897 kret
= machine_thread_get_state(thr_act
, x86_SAVED_STATE32
,
1898 (thread_state_t
) &ic32
->ss
, &val
);
1899 if (kret
!= KERN_SUCCESS
) {
1900 kfree(ic32
, sizeof(struct x86_act_context32
));
1903 val
= x86_FLOAT_STATE32_COUNT
;
1904 kret
= machine_thread_get_state(thr_act
, x86_FLOAT_STATE32
,
1905 (thread_state_t
) &ic32
->fs
, &val
);
1906 if (kret
!= KERN_SUCCESS
) {
1907 kfree(ic32
, sizeof(struct x86_act_context32
));
1911 val
= x86_DEBUG_STATE32_COUNT
;
1912 kret
= machine_thread_get_state(thr_act
,
1914 (thread_state_t
)&ic32
->ds
,
1916 if (kret
!= KERN_SUCCESS
) {
1917 kfree(ic32
, sizeof(struct x86_act_context32
));
1926 act_thread_catt(void *ctx
)
1928 thread_t thr_act
= current_thread();
1931 if (ctx
== (void *)NULL
)
1934 if (thread_is_64bit(thr_act
)) {
1935 struct x86_act_context64
*ic64
;
1937 ic64
= (struct x86_act_context64
*)ctx
;
1939 kret
= machine_thread_set_state(thr_act
, x86_SAVED_STATE64
,
1940 (thread_state_t
) &ic64
->ss
, x86_SAVED_STATE64_COUNT
);
1941 if (kret
== KERN_SUCCESS
) {
1942 machine_thread_set_state(thr_act
, x86_FLOAT_STATE64
,
1943 (thread_state_t
) &ic64
->fs
, x86_FLOAT_STATE64_COUNT
);
1945 kfree(ic64
, sizeof(struct x86_act_context64
));
1947 struct x86_act_context32
*ic32
;
1949 ic32
= (struct x86_act_context32
*)ctx
;
1951 kret
= machine_thread_set_state(thr_act
, x86_SAVED_STATE32
,
1952 (thread_state_t
) &ic32
->ss
, x86_SAVED_STATE32_COUNT
);
1953 if (kret
== KERN_SUCCESS
) {
1954 (void) machine_thread_set_state(thr_act
, x86_FLOAT_STATE32
,
1955 (thread_state_t
) &ic32
->fs
, x86_FLOAT_STATE32_COUNT
);
1957 kfree(ic32
, sizeof(struct x86_act_context32
));
1962 void act_thread_cfree(__unused
void *ctx
)
1968 * Duplicate one x86_debug_state32_t to another. "all" parameter
1969 * chooses whether dr4 and dr5 are copied (they are never meant
1970 * to be installed when we do machine_task_set_state() or
1971 * machine_thread_set_state()).
1975 x86_debug_state32_t
*src
,
1976 x86_debug_state32_t
*target
,
1980 target
->dr4
= src
->dr4
;
1981 target
->dr5
= src
->dr5
;
1984 target
->dr0
= src
->dr0
;
1985 target
->dr1
= src
->dr1
;
1986 target
->dr2
= src
->dr2
;
1987 target
->dr3
= src
->dr3
;
1988 target
->dr6
= src
->dr6
;
1989 target
->dr7
= src
->dr7
;
1993 * Duplicate one x86_debug_state64_t to another. "all" parameter
1994 * chooses whether dr4 and dr5 are copied (they are never meant
1995 * to be installed when we do machine_task_set_state() or
1996 * machine_thread_set_state()).
2000 x86_debug_state64_t
*src
,
2001 x86_debug_state64_t
*target
,
2005 target
->dr4
= src
->dr4
;
2006 target
->dr5
= src
->dr5
;
2009 target
->dr0
= src
->dr0
;
2010 target
->dr1
= src
->dr1
;
2011 target
->dr2
= src
->dr2
;
2012 target
->dr3
= src
->dr3
;
2013 target
->dr6
= src
->dr6
;
2014 target
->dr7
= src
->dr7
;