2 * Copyright (c) 2007-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@
33 #include <mach/mach_types.h>
34 #include <mach/thread_status.h>
35 #include <mach/vm_types.h>
37 #include <kern/kern_types.h>
38 #include <kern/task.h>
39 #include <kern/thread.h>
40 #include <kern/misc_protos.h>
41 #include <kern/mach_param.h>
43 #include <kern/machine.h>
44 #include <kern/kalloc.h>
48 #include <kern/monotonic.h>
49 #endif /* MONOTONIC */
51 #include <machine/atomic.h>
52 #include <arm64/proc_reg.h>
53 #include <arm64/machine_machdep.h>
54 #include <arm/cpu_data_internal.h>
55 #include <arm/machdep_call.h>
56 #include <arm/misc_protos.h>
57 #include <arm/cpuid.h>
59 #include <vm/vm_map.h>
60 #include <vm/vm_protos.h>
62 #include <sys/kdebug.h>
64 #define USER_SS_ZONE_ALLOC_SIZE (0x4000)
66 extern int debug_task
;
68 zone_t ads_zone
; /* zone for debug_state area */
69 zone_t user_ss_zone
; /* zone for user arm_context_t allocations */
72 * Routine: consider_machine_collect
76 consider_machine_collect(void)
82 * Routine: consider_machine_adjust
86 consider_machine_adjust(void)
91 * Routine: machine_switch_context
95 machine_switch_context(
97 thread_continue_t continuation
,
102 cpu_data_t
*cpu_data_ptr
;
104 #define machine_switch_context_kprintf(x...) /* kprintf("machine_switch_con
107 cpu_data_ptr
= getCpuDatap();
109 panic("machine_switch_context");
114 new_pmap
= new->map
->pmap
;
115 if (old
->map
->pmap
!= new_pmap
)
116 pmap_switch(new_pmap
);
118 new->machine
.CpuDatap
= cpu_data_ptr
;
120 machine_switch_context_kprintf("old= %x contination = %x new = %x\n", old
, continuation
, new);
122 retval
= Switch_context(old
, continuation
, new);
123 assert(retval
!= NULL
);
129 * Routine: machine_thread_create
133 machine_thread_create(
137 arm_context_t
*thread_user_ss
= NULL
;
138 kern_return_t result
= KERN_SUCCESS
;
140 #define machine_thread_create_kprintf(x...) /* kprintf("machine_thread_create: " x) */
142 machine_thread_create_kprintf("thread = %x\n", thread
);
144 if (current_thread() != thread
) {
145 thread
->machine
.CpuDatap
= (cpu_data_t
*)0;
147 thread
->machine
.preemption_count
= 0;
148 thread
->machine
.cthread_self
= 0;
149 thread
->machine
.cthread_data
= 0;
152 if (task
!= kernel_task
) {
153 /* If this isn't a kernel thread, we'll have userspace state. */
154 thread
->machine
.contextData
= (arm_context_t
*)zalloc(user_ss_zone
);
156 if (!thread
->machine
.contextData
) {
160 thread
->machine
.upcb
= &thread
->machine
.contextData
->ss
;
161 thread
->machine
.uNeon
= &thread
->machine
.contextData
->ns
;
163 if (task_has_64BitAddr(task
)) {
164 thread
->machine
.upcb
->ash
.flavor
= ARM_SAVED_STATE64
;
165 thread
->machine
.upcb
->ash
.count
= ARM_SAVED_STATE64_COUNT
;
166 thread
->machine
.uNeon
->nsh
.flavor
= ARM_NEON_SAVED_STATE64
;
167 thread
->machine
.uNeon
->nsh
.count
= ARM_NEON_SAVED_STATE64_COUNT
;
169 thread
->machine
.upcb
->ash
.flavor
= ARM_SAVED_STATE32
;
170 thread
->machine
.upcb
->ash
.count
= ARM_SAVED_STATE32_COUNT
;
171 thread
->machine
.uNeon
->nsh
.flavor
= ARM_NEON_SAVED_STATE32
;
172 thread
->machine
.uNeon
->nsh
.count
= ARM_NEON_SAVED_STATE32_COUNT
;
175 thread
->machine
.upcb
= NULL
;
176 thread
->machine
.uNeon
= NULL
;
177 thread
->machine
.contextData
= NULL
;
180 bzero(&thread
->machine
.perfctrl_state
, sizeof(thread
->machine
.perfctrl_state
));
182 result
= machine_thread_state_initialize(thread
);
184 if (result
!= KERN_SUCCESS
) {
185 thread_user_ss
= thread
->machine
.contextData
;
186 thread
->machine
.upcb
= NULL
;
187 thread
->machine
.uNeon
= NULL
;
188 thread
->machine
.contextData
= NULL
;
189 zfree(user_ss_zone
, thread_user_ss
);
196 * Routine: machine_thread_destroy
200 machine_thread_destroy(
203 arm_context_t
*thread_user_ss
;
205 if (thread
->machine
.contextData
) {
206 /* Disassociate the user save state from the thread before we free it. */
207 thread_user_ss
= thread
->machine
.contextData
;
208 thread
->machine
.upcb
= NULL
;
209 thread
->machine
.uNeon
= NULL
;
210 thread
->machine
.contextData
= NULL
;
211 zfree(user_ss_zone
, thread_user_ss
);
214 if (thread
->machine
.DebugData
!= NULL
) {
215 if (thread
->machine
.DebugData
== getCpuDatap()->cpu_user_debug
) {
219 zfree(ads_zone
, thread
->machine
.DebugData
);
225 * Routine: machine_thread_init
229 machine_thread_init(void)
231 ads_zone
= zinit(sizeof(arm_debug_state_t
),
232 THREAD_CHUNK
* (sizeof(arm_debug_state_t
)),
233 THREAD_CHUNK
* (sizeof(arm_debug_state_t
)),
237 * Create a zone for the user save state. At the time this zone was created,
238 * the user save state was 848 bytes, and the matching kalloc zone was 1024
239 * bytes, which would result in significant amounts of wasted space if we
240 * simply used kalloc to allocate the user saved state.
242 * 0x4000 has been chosen as the allocation size, as it results in 272 bytes
243 * of wasted space per chunk, which should correspond to 19 allocations.
245 user_ss_zone
= zinit(sizeof(arm_context_t
),
246 CONFIG_THREAD_MAX
* (sizeof(arm_context_t
)),
247 USER_SS_ZONE_ALLOC_SIZE
,
253 * Routine: get_useraddr
259 return (get_saved_state_pc(current_thread()->machine
.upcb
));
263 * Routine: machine_stack_detach
267 machine_stack_detach(
272 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_SCHED
, MACH_STACK_DETACH
),
273 (uintptr_t)thread_tid(thread
), thread
->priority
, thread
->sched_pri
, 0, 0);
275 stack
= thread
->kernel_stack
;
276 thread
->kernel_stack
= 0;
277 thread
->machine
.kstackptr
= 0;
284 * Routine: machine_stack_attach
288 machine_stack_attach(
292 struct arm_context
*context
;
293 struct arm_saved_state64
*savestate
;
295 #define machine_stack_attach_kprintf(x...) /* kprintf("machine_stack_attach: " x) */
297 KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_SCHED
, MACH_STACK_ATTACH
),
298 (uintptr_t)thread_tid(thread
), thread
->priority
, thread
->sched_pri
, 0, 0);
300 thread
->kernel_stack
= stack
;
301 thread
->machine
.kstackptr
= stack
+ kernel_stack_size
- sizeof(struct thread_kernel_state
);
302 thread_initialize_kernel_state(thread
);
304 machine_stack_attach_kprintf("kstackptr: %lx\n", (vm_address_t
)thread
->machine
.kstackptr
);
306 context
= &((thread_kernel_state_t
) thread
->machine
.kstackptr
)->machine
;
307 savestate
= saved_state64(&context
->ss
);
309 savestate
->lr
= (uintptr_t)thread_continue
;
310 savestate
->sp
= thread
->machine
.kstackptr
;
311 savestate
->cpsr
= PSR64_KERNEL_DEFAULT
;
312 machine_stack_attach_kprintf("thread = %x pc = %x, sp = %x\n", thread
, savestate
->lr
, savestate
->sp
);
317 * Routine: machine_stack_handoff
321 machine_stack_handoff(
327 cpu_data_t
*cpu_data_ptr
;
331 stack
= machine_stack_detach(old
);
332 cpu_data_ptr
= getCpuDatap();
333 new->kernel_stack
= stack
;
334 new->machine
.kstackptr
= stack
+ kernel_stack_size
- sizeof(struct thread_kernel_state
);
335 if (stack
== old
->reserved_stack
) {
336 assert(new->reserved_stack
);
337 old
->reserved_stack
= new->reserved_stack
;
338 new->reserved_stack
= stack
;
342 new_pmap
= new->map
->pmap
;
343 if (old
->map
->pmap
!= new_pmap
)
344 pmap_switch(new_pmap
);
346 new->machine
.CpuDatap
= cpu_data_ptr
;
347 machine_set_current_thread(new);
348 thread_initialize_kernel_state(new);
355 * Routine: call_continuation
360 thread_continue_t continuation
,
362 wait_result_t wresult
)
364 #define call_continuation_kprintf(x...) /* kprintf("call_continuation_kprintf:" x) */
366 call_continuation_kprintf("thread = %p continuation = %p, stack = %p\n", current_thread(), continuation
, current_thread()->machine
.kstackptr
);
367 Call_continuation(continuation
, parameter
, wresult
, current_thread()->machine
.kstackptr
);
370 /* Setting breakpoints in EL1 is effectively a KTRR bypass. The ability to do so is
371 * controlled by MDSCR.KDE. The MSR to set MDSCR must be present to allow
372 * self-hosted user mode debug. Any checks before the MRS can be skipped with ROP,
373 * so we need to put the checks after the MRS where they can't be skipped. That
374 * still leaves a small window if a breakpoint is set on the instruction
375 * immediately after the MRS. To handle that, we also do a check and then set of
376 * the breakpoint control registers. This allows us to guarantee that a given
377 * core will never have both KDE set and a breakpoint targeting EL1.
379 * If KDE gets set, unset it and then panic */
381 update_mdscr(uint64_t clear
, uint64_t set
)
386 "mrs %[reg], MDSCR_EL1\n"
387 "bic %[reg], %[reg], %[clear]\n"
388 "orr %[reg], %[reg], %[set]\n"
390 "bic %[reg], %[reg], #0x2000\n"
391 "msr MDSCR_EL1, %[reg]\n"
392 #if defined(CONFIG_KERNEL_INTEGRITY)
393 /* verify KDE didn't get set (including via ROP)
394 * If set, clear it and then panic */
395 "ands %[tmp], %[reg], #0x2000\n"
396 "orr %[res], %[res], %[tmp]\n"
399 : [res
] "+r" (result
), [tmp
] "=r" (tmp1
), [reg
] "=r" (tmp2
)
400 : [clear
] "r" (clear
), [set
] "r" (set
) : "x0");
401 #if defined(CONFIG_KERNEL_INTEGRITY)
403 panic("MDSCR.KDE was set: %llx %llx %llx", tmp1
, tmp2
, result
);
407 #define SET_DBGBCRn(n, value, accum) \
409 "msr DBGBCR" #n "_EL1, %[val]\n" \
410 "orr %[result], %[result], %[val]\n" \
411 : [result] "+r"(accum) : [val] "r"((value)))
413 #define SET_DBGBVRn(n, value) \
414 __asm__ volatile("msr DBGBVR" #n "_EL1, %0" : : "r"(value))
416 #define SET_DBGWCRn(n, value, accum) \
418 "msr DBGWCR" #n "_EL1, %[val]\n" \
419 "orr %[result], %[result], %[val]\n" \
420 : [result] "+r"(accum) : [val] "r"((value)))
422 #define SET_DBGWVRn(n, value) \
423 __asm__ volatile("msr DBGWVR" #n "_EL1, %0" : : "r"(value))
425 void arm_debug_set32(arm_debug_state_t
*debug_state
)
427 struct cpu_data
*cpu_data_ptr
;
428 arm_debug_info_t
*debug_info
= arm_debug_info();
429 boolean_t intr
, set_mde
= 0;
430 arm_debug_state_t off_state
;
432 uint64_t all_ctrls
= 0;
434 intr
= ml_set_interrupts_enabled(FALSE
);
435 cpu_data_ptr
= getCpuDatap();
437 // Set current user debug
438 cpu_data_ptr
->cpu_user_debug
= debug_state
;
440 if (NULL
== debug_state
) {
441 bzero(&off_state
, sizeof(off_state
));
442 debug_state
= &off_state
;
445 switch (debug_info
->num_breakpoint_pairs
) {
447 SET_DBGBVRn(15, (uint64_t)debug_state
->uds
.ds32
.bvr
[15]);
448 SET_DBGBCRn(15, (uint64_t)debug_state
->uds
.ds32
.bcr
[15], all_ctrls
);
450 SET_DBGBVRn(14, (uint64_t)debug_state
->uds
.ds32
.bvr
[14]);
451 SET_DBGBCRn(14, (uint64_t)debug_state
->uds
.ds32
.bcr
[14], all_ctrls
);
453 SET_DBGBVRn(13, (uint64_t)debug_state
->uds
.ds32
.bvr
[13]);
454 SET_DBGBCRn(13, (uint64_t)debug_state
->uds
.ds32
.bcr
[13], all_ctrls
);
456 SET_DBGBVRn(12, (uint64_t)debug_state
->uds
.ds32
.bvr
[12]);
457 SET_DBGBCRn(12, (uint64_t)debug_state
->uds
.ds32
.bcr
[12], all_ctrls
);
459 SET_DBGBVRn(11, (uint64_t)debug_state
->uds
.ds32
.bvr
[11]);
460 SET_DBGBCRn(11, (uint64_t)debug_state
->uds
.ds32
.bcr
[11], all_ctrls
);
462 SET_DBGBVRn(10, (uint64_t)debug_state
->uds
.ds32
.bvr
[10]);
463 SET_DBGBCRn(10, (uint64_t)debug_state
->uds
.ds32
.bcr
[10], all_ctrls
);
465 SET_DBGBVRn(9, (uint64_t)debug_state
->uds
.ds32
.bvr
[9]);
466 SET_DBGBCRn(9, (uint64_t)debug_state
->uds
.ds32
.bcr
[9], all_ctrls
);
468 SET_DBGBVRn(8, (uint64_t)debug_state
->uds
.ds32
.bvr
[8]);
469 SET_DBGBCRn(8, (uint64_t)debug_state
->uds
.ds32
.bcr
[8], all_ctrls
);
471 SET_DBGBVRn(7, (uint64_t)debug_state
->uds
.ds32
.bvr
[7]);
472 SET_DBGBCRn(7, (uint64_t)debug_state
->uds
.ds32
.bcr
[7], all_ctrls
);
474 SET_DBGBVRn(6, (uint64_t)debug_state
->uds
.ds32
.bvr
[6]);
475 SET_DBGBCRn(6, (uint64_t)debug_state
->uds
.ds32
.bcr
[6], all_ctrls
);
477 SET_DBGBVRn(5, (uint64_t)debug_state
->uds
.ds32
.bvr
[5]);
478 SET_DBGBCRn(5, (uint64_t)debug_state
->uds
.ds32
.bcr
[5], all_ctrls
);
480 SET_DBGBVRn(4, (uint64_t)debug_state
->uds
.ds32
.bvr
[4]);
481 SET_DBGBCRn(4, (uint64_t)debug_state
->uds
.ds32
.bcr
[4], all_ctrls
);
483 SET_DBGBVRn(3, (uint64_t)debug_state
->uds
.ds32
.bvr
[3]);
484 SET_DBGBCRn(3, (uint64_t)debug_state
->uds
.ds32
.bcr
[3], all_ctrls
);
486 SET_DBGBVRn(2, (uint64_t)debug_state
->uds
.ds32
.bvr
[2]);
487 SET_DBGBCRn(2, (uint64_t)debug_state
->uds
.ds32
.bcr
[2], all_ctrls
);
489 SET_DBGBVRn(1, (uint64_t)debug_state
->uds
.ds32
.bvr
[1]);
490 SET_DBGBCRn(1, (uint64_t)debug_state
->uds
.ds32
.bcr
[1], all_ctrls
);
492 SET_DBGBVRn(0, (uint64_t)debug_state
->uds
.ds32
.bvr
[0]);
493 SET_DBGBCRn(0, (uint64_t)debug_state
->uds
.ds32
.bcr
[0], all_ctrls
);
498 switch (debug_info
->num_watchpoint_pairs
) {
500 SET_DBGWVRn(15, (uint64_t)debug_state
->uds
.ds32
.wvr
[15]);
501 SET_DBGWCRn(15, (uint64_t)debug_state
->uds
.ds32
.wcr
[15], all_ctrls
);
503 SET_DBGWVRn(14, (uint64_t)debug_state
->uds
.ds32
.wvr
[14]);
504 SET_DBGWCRn(14, (uint64_t)debug_state
->uds
.ds32
.wcr
[14], all_ctrls
);
506 SET_DBGWVRn(13, (uint64_t)debug_state
->uds
.ds32
.wvr
[13]);
507 SET_DBGWCRn(13, (uint64_t)debug_state
->uds
.ds32
.wcr
[13], all_ctrls
);
509 SET_DBGWVRn(12, (uint64_t)debug_state
->uds
.ds32
.wvr
[12]);
510 SET_DBGWCRn(12, (uint64_t)debug_state
->uds
.ds32
.wcr
[12], all_ctrls
);
512 SET_DBGWVRn(11, (uint64_t)debug_state
->uds
.ds32
.wvr
[11]);
513 SET_DBGWCRn(11, (uint64_t)debug_state
->uds
.ds32
.wcr
[11], all_ctrls
);
515 SET_DBGWVRn(10, (uint64_t)debug_state
->uds
.ds32
.wvr
[10]);
516 SET_DBGWCRn(10, (uint64_t)debug_state
->uds
.ds32
.wcr
[10], all_ctrls
);
518 SET_DBGWVRn(9, (uint64_t)debug_state
->uds
.ds32
.wvr
[9]);
519 SET_DBGWCRn(9, (uint64_t)debug_state
->uds
.ds32
.wcr
[9], all_ctrls
);
521 SET_DBGWVRn(8, (uint64_t)debug_state
->uds
.ds32
.wvr
[8]);
522 SET_DBGWCRn(8, (uint64_t)debug_state
->uds
.ds32
.wcr
[8], all_ctrls
);
524 SET_DBGWVRn(7, (uint64_t)debug_state
->uds
.ds32
.wvr
[7]);
525 SET_DBGWCRn(7, (uint64_t)debug_state
->uds
.ds32
.wcr
[7], all_ctrls
);
527 SET_DBGWVRn(6, (uint64_t)debug_state
->uds
.ds32
.wvr
[6]);
528 SET_DBGWCRn(6, (uint64_t)debug_state
->uds
.ds32
.wcr
[6], all_ctrls
);
530 SET_DBGWVRn(5, (uint64_t)debug_state
->uds
.ds32
.wvr
[5]);
531 SET_DBGWCRn(5, (uint64_t)debug_state
->uds
.ds32
.wcr
[5], all_ctrls
);
533 SET_DBGWVRn(4, (uint64_t)debug_state
->uds
.ds32
.wvr
[4]);
534 SET_DBGWCRn(4, (uint64_t)debug_state
->uds
.ds32
.wcr
[4], all_ctrls
);
536 SET_DBGWVRn(3, (uint64_t)debug_state
->uds
.ds32
.wvr
[3]);
537 SET_DBGWCRn(3, (uint64_t)debug_state
->uds
.ds32
.wcr
[3], all_ctrls
);
539 SET_DBGWVRn(2, (uint64_t)debug_state
->uds
.ds32
.wvr
[2]);
540 SET_DBGWCRn(2, (uint64_t)debug_state
->uds
.ds32
.wcr
[2], all_ctrls
);
542 SET_DBGWVRn(1, (uint64_t)debug_state
->uds
.ds32
.wvr
[1]);
543 SET_DBGWCRn(1, (uint64_t)debug_state
->uds
.ds32
.wcr
[1], all_ctrls
);
545 SET_DBGWVRn(0, (uint64_t)debug_state
->uds
.ds32
.wvr
[0]);
546 SET_DBGWCRn(0, (uint64_t)debug_state
->uds
.ds32
.wcr
[0], all_ctrls
);
551 #if defined(CONFIG_KERNEL_INTEGRITY)
552 if ((all_ctrls
& (ARM_DBG_CR_MODE_CONTROL_PRIVILEGED
| ARM_DBG_CR_HIGHER_MODE_ENABLE
)) != 0) {
553 panic("sorry, self-hosted debug is not supported: 0x%llx", all_ctrls
);
557 for (i
= 0; i
< debug_info
->num_breakpoint_pairs
; i
++) {
558 if (0 != debug_state
->uds
.ds32
.bcr
[i
]) {
564 for (i
= 0; i
< debug_info
->num_watchpoint_pairs
; i
++) {
565 if (0 != debug_state
->uds
.ds32
.wcr
[i
]) {
572 * Breakpoint/Watchpoint Enable
575 update_mdscr(0, 0x8000); // MDSCR_EL1[MDE]
577 update_mdscr(0x8000, 0);
581 * Software debug single step enable
583 if (debug_state
->uds
.ds32
.mdscr_el1
& 0x1) {
584 update_mdscr(0x8000, 1); // ~MDE | SS : no brk/watch while single stepping (which we've set)
586 set_saved_state_cpsr((current_thread()->machine
.upcb
),
587 get_saved_state_cpsr((current_thread()->machine
.upcb
)) | PSR64_SS
);
591 update_mdscr(0x1, 0);
593 #if SINGLE_STEP_RETIRE_ERRATA
594 // Workaround for radar 20619637
595 __builtin_arm_isb(ISB_SY
);
599 (void) ml_set_interrupts_enabled(intr
);
604 void arm_debug_set64(arm_debug_state_t
*debug_state
)
606 struct cpu_data
*cpu_data_ptr
;
607 arm_debug_info_t
*debug_info
= arm_debug_info();
608 boolean_t intr
, set_mde
= 0;
609 arm_debug_state_t off_state
;
611 uint64_t all_ctrls
= 0;
613 intr
= ml_set_interrupts_enabled(FALSE
);
614 cpu_data_ptr
= getCpuDatap();
616 // Set current user debug
617 cpu_data_ptr
->cpu_user_debug
= debug_state
;
619 if (NULL
== debug_state
) {
620 bzero(&off_state
, sizeof(off_state
));
621 debug_state
= &off_state
;
624 switch (debug_info
->num_breakpoint_pairs
) {
626 SET_DBGBVRn(15, debug_state
->uds
.ds64
.bvr
[15]);
627 SET_DBGBCRn(15, (uint64_t)debug_state
->uds
.ds64
.bcr
[15], all_ctrls
);
629 SET_DBGBVRn(14, debug_state
->uds
.ds64
.bvr
[14]);
630 SET_DBGBCRn(14, (uint64_t)debug_state
->uds
.ds64
.bcr
[14], all_ctrls
);
632 SET_DBGBVRn(13, debug_state
->uds
.ds64
.bvr
[13]);
633 SET_DBGBCRn(13, (uint64_t)debug_state
->uds
.ds64
.bcr
[13], all_ctrls
);
635 SET_DBGBVRn(12, debug_state
->uds
.ds64
.bvr
[12]);
636 SET_DBGBCRn(12, (uint64_t)debug_state
->uds
.ds64
.bcr
[12], all_ctrls
);
638 SET_DBGBVRn(11, debug_state
->uds
.ds64
.bvr
[11]);
639 SET_DBGBCRn(11, (uint64_t)debug_state
->uds
.ds64
.bcr
[11], all_ctrls
);
641 SET_DBGBVRn(10, debug_state
->uds
.ds64
.bvr
[10]);
642 SET_DBGBCRn(10, (uint64_t)debug_state
->uds
.ds64
.bcr
[10], all_ctrls
);
644 SET_DBGBVRn(9, debug_state
->uds
.ds64
.bvr
[9]);
645 SET_DBGBCRn(9, (uint64_t)debug_state
->uds
.ds64
.bcr
[9], all_ctrls
);
647 SET_DBGBVRn(8, debug_state
->uds
.ds64
.bvr
[8]);
648 SET_DBGBCRn(8, (uint64_t)debug_state
->uds
.ds64
.bcr
[8], all_ctrls
);
650 SET_DBGBVRn(7, debug_state
->uds
.ds64
.bvr
[7]);
651 SET_DBGBCRn(7, (uint64_t)debug_state
->uds
.ds64
.bcr
[7], all_ctrls
);
653 SET_DBGBVRn(6, debug_state
->uds
.ds64
.bvr
[6]);
654 SET_DBGBCRn(6, (uint64_t)debug_state
->uds
.ds64
.bcr
[6], all_ctrls
);
656 SET_DBGBVRn(5, debug_state
->uds
.ds64
.bvr
[5]);
657 SET_DBGBCRn(5, (uint64_t)debug_state
->uds
.ds64
.bcr
[5], all_ctrls
);
659 SET_DBGBVRn(4, debug_state
->uds
.ds64
.bvr
[4]);
660 SET_DBGBCRn(4, (uint64_t)debug_state
->uds
.ds64
.bcr
[4], all_ctrls
);
662 SET_DBGBVRn(3, debug_state
->uds
.ds64
.bvr
[3]);
663 SET_DBGBCRn(3, (uint64_t)debug_state
->uds
.ds64
.bcr
[3], all_ctrls
);
665 SET_DBGBVRn(2, debug_state
->uds
.ds64
.bvr
[2]);
666 SET_DBGBCRn(2, (uint64_t)debug_state
->uds
.ds64
.bcr
[2], all_ctrls
);
668 SET_DBGBVRn(1, debug_state
->uds
.ds64
.bvr
[1]);
669 SET_DBGBCRn(1, (uint64_t)debug_state
->uds
.ds64
.bcr
[1], all_ctrls
);
671 SET_DBGBVRn(0, debug_state
->uds
.ds64
.bvr
[0]);
672 SET_DBGBCRn(0, (uint64_t)debug_state
->uds
.ds64
.bcr
[0], all_ctrls
);
677 switch (debug_info
->num_watchpoint_pairs
) {
679 SET_DBGWVRn(15, debug_state
->uds
.ds64
.wvr
[15]);
680 SET_DBGWCRn(15, (uint64_t)debug_state
->uds
.ds64
.wcr
[15], all_ctrls
);
682 SET_DBGWVRn(14, debug_state
->uds
.ds64
.wvr
[14]);
683 SET_DBGWCRn(14, (uint64_t)debug_state
->uds
.ds64
.wcr
[14], all_ctrls
);
685 SET_DBGWVRn(13, debug_state
->uds
.ds64
.wvr
[13]);
686 SET_DBGWCRn(13, (uint64_t)debug_state
->uds
.ds64
.wcr
[13], all_ctrls
);
688 SET_DBGWVRn(12, debug_state
->uds
.ds64
.wvr
[12]);
689 SET_DBGWCRn(12, (uint64_t)debug_state
->uds
.ds64
.wcr
[12], all_ctrls
);
691 SET_DBGWVRn(11, debug_state
->uds
.ds64
.wvr
[11]);
692 SET_DBGWCRn(11, (uint64_t)debug_state
->uds
.ds64
.wcr
[11], all_ctrls
);
694 SET_DBGWVRn(10, debug_state
->uds
.ds64
.wvr
[10]);
695 SET_DBGWCRn(10, (uint64_t)debug_state
->uds
.ds64
.wcr
[10], all_ctrls
);
697 SET_DBGWVRn(9, debug_state
->uds
.ds64
.wvr
[9]);
698 SET_DBGWCRn(9, (uint64_t)debug_state
->uds
.ds64
.wcr
[9], all_ctrls
);
700 SET_DBGWVRn(8, debug_state
->uds
.ds64
.wvr
[8]);
701 SET_DBGWCRn(8, (uint64_t)debug_state
->uds
.ds64
.wcr
[8], all_ctrls
);
703 SET_DBGWVRn(7, debug_state
->uds
.ds64
.wvr
[7]);
704 SET_DBGWCRn(7, (uint64_t)debug_state
->uds
.ds64
.wcr
[7], all_ctrls
);
706 SET_DBGWVRn(6, debug_state
->uds
.ds64
.wvr
[6]);
707 SET_DBGWCRn(6, (uint64_t)debug_state
->uds
.ds64
.wcr
[6], all_ctrls
);
709 SET_DBGWVRn(5, debug_state
->uds
.ds64
.wvr
[5]);
710 SET_DBGWCRn(5, (uint64_t)debug_state
->uds
.ds64
.wcr
[5], all_ctrls
);
712 SET_DBGWVRn(4, debug_state
->uds
.ds64
.wvr
[4]);
713 SET_DBGWCRn(4, (uint64_t)debug_state
->uds
.ds64
.wcr
[4], all_ctrls
);
715 SET_DBGWVRn(3, debug_state
->uds
.ds64
.wvr
[3]);
716 SET_DBGWCRn(3, (uint64_t)debug_state
->uds
.ds64
.wcr
[3], all_ctrls
);
718 SET_DBGWVRn(2, debug_state
->uds
.ds64
.wvr
[2]);
719 SET_DBGWCRn(2, (uint64_t)debug_state
->uds
.ds64
.wcr
[2], all_ctrls
);
721 SET_DBGWVRn(1, debug_state
->uds
.ds64
.wvr
[1]);
722 SET_DBGWCRn(1, (uint64_t)debug_state
->uds
.ds64
.wcr
[1], all_ctrls
);
724 SET_DBGWVRn(0, debug_state
->uds
.ds64
.wvr
[0]);
725 SET_DBGWCRn(0, (uint64_t)debug_state
->uds
.ds64
.wcr
[0], all_ctrls
);
730 #if defined(CONFIG_KERNEL_INTEGRITY)
731 if ((all_ctrls
& (ARM_DBG_CR_MODE_CONTROL_PRIVILEGED
| ARM_DBG_CR_HIGHER_MODE_ENABLE
)) != 0) {
732 panic("sorry, self-hosted debug is not supported: 0x%llx", all_ctrls
);
736 for (i
= 0; i
< debug_info
->num_breakpoint_pairs
; i
++) {
737 if (0 != debug_state
->uds
.ds64
.bcr
[i
]) {
743 for (i
= 0; i
< debug_info
->num_watchpoint_pairs
; i
++) {
744 if (0 != debug_state
->uds
.ds64
.wcr
[i
]) {
751 * Breakpoint/Watchpoint Enable
754 update_mdscr(0, 0x8000); // MDSCR_EL1[MDE]
758 * Software debug single step enable
760 if (debug_state
->uds
.ds64
.mdscr_el1
& 0x1) {
762 update_mdscr(0x8000, 1); // ~MDE | SS : no brk/watch while single stepping (which we've set)
764 set_saved_state_cpsr((current_thread()->machine
.upcb
),
765 get_saved_state_cpsr((current_thread()->machine
.upcb
)) | PSR64_SS
);
769 update_mdscr(0x1, 0);
771 #if SINGLE_STEP_RETIRE_ERRATA
772 // Workaround for radar 20619637
773 __builtin_arm_isb(ISB_SY
);
777 (void) ml_set_interrupts_enabled(intr
);
782 void arm_debug_set(arm_debug_state_t
*debug_state
)
785 switch (debug_state
->dsh
.flavor
) {
786 case ARM_DEBUG_STATE32
:
787 arm_debug_set32(debug_state
);
789 case ARM_DEBUG_STATE64
:
790 arm_debug_set64(debug_state
);
793 panic("arm_debug_set");
797 if (thread_is_64bit(current_thread()))
798 arm_debug_set64(debug_state
);
800 arm_debug_set32(debug_state
);
804 #define VM_MAX_ADDRESS32 ((vm_address_t) 0x80000000)
806 debug_legacy_state_is_valid(arm_legacy_debug_state_t
*debug_state
)
808 arm_debug_info_t
*debug_info
= arm_debug_info();
810 for (i
= 0; i
< debug_info
->num_breakpoint_pairs
; i
++) {
811 if (0 != debug_state
->bcr
[i
] && VM_MAX_ADDRESS32
<= debug_state
->bvr
[i
])
815 for (i
= 0; i
< debug_info
->num_watchpoint_pairs
; i
++) {
816 if (0 != debug_state
->wcr
[i
] && VM_MAX_ADDRESS32
<= debug_state
->wvr
[i
])
823 debug_state_is_valid32(arm_debug_state32_t
*debug_state
)
825 arm_debug_info_t
*debug_info
= arm_debug_info();
827 for (i
= 0; i
< debug_info
->num_breakpoint_pairs
; i
++) {
828 if (0 != debug_state
->bcr
[i
] && VM_MAX_ADDRESS32
<= debug_state
->bvr
[i
])
832 for (i
= 0; i
< debug_info
->num_watchpoint_pairs
; i
++) {
833 if (0 != debug_state
->wcr
[i
] && VM_MAX_ADDRESS32
<= debug_state
->wvr
[i
])
840 debug_state_is_valid64(arm_debug_state64_t
*debug_state
)
842 arm_debug_info_t
*debug_info
= arm_debug_info();
844 for (i
= 0; i
< debug_info
->num_breakpoint_pairs
; i
++) {
845 if (0 != debug_state
->bcr
[i
] && MACH_VM_MAX_ADDRESS
<= debug_state
->bvr
[i
])
849 for (i
= 0; i
< debug_info
->num_watchpoint_pairs
; i
++) {
850 if (0 != debug_state
->wcr
[i
] && MACH_VM_MAX_ADDRESS
<= debug_state
->wvr
[i
])
857 * Duplicate one arm_debug_state_t to another. "all" parameter
858 * is ignored in the case of ARM -- Is this the right assumption?
861 copy_legacy_debug_state(
862 arm_legacy_debug_state_t
*src
,
863 arm_legacy_debug_state_t
*target
,
864 __unused boolean_t all
)
866 bcopy(src
, target
, sizeof(arm_legacy_debug_state_t
));
871 arm_debug_state32_t
*src
,
872 arm_debug_state32_t
*target
,
873 __unused boolean_t all
)
875 bcopy(src
, target
, sizeof(arm_debug_state32_t
));
880 arm_debug_state64_t
*src
,
881 arm_debug_state64_t
*target
,
882 __unused boolean_t all
)
884 bcopy(src
, target
, sizeof(arm_debug_state64_t
));
888 machine_thread_set_tsd_base(
890 mach_vm_offset_t tsd_base
)
893 if (thread
->task
== kernel_task
) {
894 return KERN_INVALID_ARGUMENT
;
897 if (tsd_base
& MACHDEP_CPUNUM_MASK
) {
898 return KERN_INVALID_ARGUMENT
;
901 if (thread_is_64bit(thread
)) {
902 if (tsd_base
> vm_map_max(thread
->map
))
905 if (tsd_base
> UINT32_MAX
)
909 thread
->machine
.cthread_self
= tsd_base
;
911 /* For current thread, make the TSD base active immediately */
912 if (thread
== current_thread()) {
913 uint64_t cpunum
, tpidrro_el0
;
915 mp_disable_preemption();
916 tpidrro_el0
= get_tpidrro();
917 cpunum
= tpidrro_el0
& (MACHDEP_CPUNUM_MASK
);
918 set_tpidrro(tsd_base
| cpunum
);
919 mp_enable_preemption();