Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 | /* * Copyright (c) 2007-2016 Apple Inc. All rights reserved. * * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. The rights granted to you under the License * may not be used to create, or enable the creation or redistribution of, * unlawful or unlicensed copies of an Apple operating system, or to * circumvent, violate, or enable the circumvention or violation of, any * terms of an Apple operating system software license agreement. * * Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ */ #include <debug.h> #include <types.h> #include <mach/mach_types.h> #include <mach/thread_status.h> #include <mach/vm_types.h> #include <kern/kern_types.h> #include <kern/task.h> #include <kern/thread.h> #include <kern/misc_protos.h> #include <kern/mach_param.h> #include <kern/spl.h> #include <kern/machine.h> #include <kern/kalloc.h> #include <kern/kpc.h> #if MONOTONIC #include <kern/monotonic.h> #endif /* MONOTONIC */ #include <machine/atomic.h> #include <arm64/proc_reg.h> #include <arm64/machine_machdep.h> #include <arm/cpu_data_internal.h> #include <arm/machdep_call.h> #include <arm/misc_protos.h> #include <arm/cpuid.h> #include <vm/vm_map.h> #include <vm/vm_protos.h> #include <sys/kdebug.h> #define USER_SS_ZONE_ALLOC_SIZE (0x4000) extern int debug_task; zone_t ads_zone; /* zone for debug_state area */ zone_t user_ss_zone; /* zone for user arm_context_t allocations */ /* * Routine: consider_machine_collect * */ void consider_machine_collect(void) { pmap_gc(); } /* * Routine: consider_machine_adjust * */ void consider_machine_adjust(void) { } /* * Routine: machine_switch_context * */ thread_t machine_switch_context( thread_t old, thread_continue_t continuation, thread_t new) { thread_t retval; pmap_t new_pmap; cpu_data_t *cpu_data_ptr; #define machine_switch_context_kprintf(x...) /* kprintf("machine_switch_con * text: " x) */ cpu_data_ptr = getCpuDatap(); if (old == new) panic("machine_switch_context"); kpc_off_cpu(old); new_pmap = new->map->pmap; if (old->map->pmap != new_pmap) pmap_switch(new_pmap); new->machine.CpuDatap = cpu_data_ptr; machine_switch_context_kprintf("old= %x contination = %x new = %x\n", old, continuation, new); retval = Switch_context(old, continuation, new); assert(retval != NULL); return retval; } /* * Routine: machine_thread_create * */ kern_return_t machine_thread_create( thread_t thread, task_t task) { arm_context_t *thread_user_ss = NULL; kern_return_t result = KERN_SUCCESS; #define machine_thread_create_kprintf(x...) /* kprintf("machine_thread_create: " x) */ machine_thread_create_kprintf("thread = %x\n", thread); if (current_thread() != thread) { thread->machine.CpuDatap = (cpu_data_t *)0; } thread->machine.preemption_count = 0; thread->machine.cthread_self = 0; thread->machine.cthread_data = 0; if (task != kernel_task) { /* If this isn't a kernel thread, we'll have userspace state. */ thread->machine.contextData = (arm_context_t *)zalloc(user_ss_zone); if (!thread->machine.contextData) { return KERN_FAILURE; } thread->machine.upcb = &thread->machine.contextData->ss; thread->machine.uNeon = &thread->machine.contextData->ns; if (task_has_64BitAddr(task)) { thread->machine.upcb->ash.flavor = ARM_SAVED_STATE64; thread->machine.upcb->ash.count = ARM_SAVED_STATE64_COUNT; thread->machine.uNeon->nsh.flavor = ARM_NEON_SAVED_STATE64; thread->machine.uNeon->nsh.count = ARM_NEON_SAVED_STATE64_COUNT; } else { thread->machine.upcb->ash.flavor = ARM_SAVED_STATE32; thread->machine.upcb->ash.count = ARM_SAVED_STATE32_COUNT; thread->machine.uNeon->nsh.flavor = ARM_NEON_SAVED_STATE32; thread->machine.uNeon->nsh.count = ARM_NEON_SAVED_STATE32_COUNT; } } else { thread->machine.upcb = NULL; thread->machine.uNeon = NULL; thread->machine.contextData = NULL; } bzero(&thread->machine.perfctrl_state, sizeof(thread->machine.perfctrl_state)); result = machine_thread_state_initialize(thread); if (result != KERN_SUCCESS) { thread_user_ss = thread->machine.contextData; thread->machine.upcb = NULL; thread->machine.uNeon = NULL; thread->machine.contextData = NULL; zfree(user_ss_zone, thread_user_ss); } return result; } /* * Routine: machine_thread_destroy * */ void machine_thread_destroy( thread_t thread) { arm_context_t *thread_user_ss; if (thread->machine.contextData) { /* Disassociate the user save state from the thread before we free it. */ thread_user_ss = thread->machine.contextData; thread->machine.upcb = NULL; thread->machine.uNeon = NULL; thread->machine.contextData = NULL; zfree(user_ss_zone, thread_user_ss); } if (thread->machine.DebugData != NULL) { if (thread->machine.DebugData == getCpuDatap()->cpu_user_debug) { arm_debug_set(NULL); } zfree(ads_zone, thread->machine.DebugData); } } /* * Routine: machine_thread_init * */ void machine_thread_init(void) { ads_zone = zinit(sizeof(arm_debug_state_t), THREAD_CHUNK * (sizeof(arm_debug_state_t)), THREAD_CHUNK * (sizeof(arm_debug_state_t)), "arm debug state"); /* * Create a zone for the user save state. At the time this zone was created, * the user save state was 848 bytes, and the matching kalloc zone was 1024 * bytes, which would result in significant amounts of wasted space if we * simply used kalloc to allocate the user saved state. * * 0x4000 has been chosen as the allocation size, as it results in 272 bytes * of wasted space per chunk, which should correspond to 19 allocations. */ user_ss_zone = zinit(sizeof(arm_context_t), CONFIG_THREAD_MAX * (sizeof(arm_context_t)), USER_SS_ZONE_ALLOC_SIZE, "user save state"); } /* * Routine: get_useraddr * */ user_addr_t get_useraddr() { return (get_saved_state_pc(current_thread()->machine.upcb)); } /* * Routine: machine_stack_detach * */ vm_offset_t machine_stack_detach( thread_t thread) { vm_offset_t stack; KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_SCHED, MACH_STACK_DETACH), (uintptr_t)thread_tid(thread), thread->priority, thread->sched_pri, 0, 0); stack = thread->kernel_stack; thread->kernel_stack = 0; thread->machine.kstackptr = 0; return (stack); } /* * Routine: machine_stack_attach * */ void machine_stack_attach( thread_t thread, vm_offset_t stack) { struct arm_context *context; struct arm_saved_state64 *savestate; #define machine_stack_attach_kprintf(x...) /* kprintf("machine_stack_attach: " x) */ KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_SCHED, MACH_STACK_ATTACH), (uintptr_t)thread_tid(thread), thread->priority, thread->sched_pri, 0, 0); thread->kernel_stack = stack; thread->machine.kstackptr = stack + kernel_stack_size - sizeof(struct thread_kernel_state); thread_initialize_kernel_state(thread); machine_stack_attach_kprintf("kstackptr: %lx\n", (vm_address_t)thread->machine.kstackptr); context = &((thread_kernel_state_t) thread->machine.kstackptr)->machine; savestate = saved_state64(&context->ss); savestate->fp = 0; savestate->lr = (uintptr_t)thread_continue; savestate->sp = thread->machine.kstackptr; savestate->cpsr = PSR64_KERNEL_DEFAULT; machine_stack_attach_kprintf("thread = %x pc = %x, sp = %x\n", thread, savestate->lr, savestate->sp); } /* * Routine: machine_stack_handoff * */ void machine_stack_handoff( thread_t old, thread_t new) { vm_offset_t stack; pmap_t new_pmap; cpu_data_t *cpu_data_ptr; kpc_off_cpu(old); stack = machine_stack_detach(old); cpu_data_ptr = getCpuDatap(); new->kernel_stack = stack; new->machine.kstackptr = stack + kernel_stack_size - sizeof(struct thread_kernel_state); if (stack == old->reserved_stack) { assert(new->reserved_stack); old->reserved_stack = new->reserved_stack; new->reserved_stack = stack; } new_pmap = new->map->pmap; if (old->map->pmap != new_pmap) pmap_switch(new_pmap); new->machine.CpuDatap = cpu_data_ptr; machine_set_current_thread(new); thread_initialize_kernel_state(new); return; } /* * Routine: call_continuation * */ void call_continuation( thread_continue_t continuation, void *parameter, wait_result_t wresult) { #define call_continuation_kprintf(x...) /* kprintf("call_continuation_kprintf:" x) */ call_continuation_kprintf("thread = %p continuation = %p, stack = %p\n", current_thread(), continuation, current_thread()->machine.kstackptr); Call_continuation(continuation, parameter, wresult, current_thread()->machine.kstackptr); } void arm_debug_set32(arm_debug_state_t *debug_state) { struct cpu_data *cpu_data_ptr; arm_debug_info_t *debug_info = arm_debug_info(); volatile uint64_t state; boolean_t intr, set_mde = 0; arm_debug_state_t off_state; uint32_t i; intr = ml_set_interrupts_enabled(FALSE); cpu_data_ptr = getCpuDatap(); // Set current user debug cpu_data_ptr->cpu_user_debug = debug_state; if (NULL == debug_state) { bzero(&off_state, sizeof(off_state)); debug_state = &off_state; } switch (debug_info->num_breakpoint_pairs) { case 16: __asm__ volatile("msr DBGBVR15_EL1, %0" : : "r"((uint64_t)debug_state->uds.ds32.bvr[15])); __asm__ volatile("msr DBGBCR15_EL1, %0" : : "r"((uint64_t)debug_state->uds.ds32.bcr[15])); case 15: __asm__ volatile("msr DBGBVR14_EL1, %0" : : "r"((uint64_t)debug_state->uds.ds32.bvr[14])); __asm__ volatile("msr DBGBCR14_EL1, %0" : : "r"((uint64_t)debug_state->uds.ds32.bcr[14])); case 14: __asm__ volatile("msr DBGBVR13_EL1, %0" : : "r"((uint64_t)debug_state->uds.ds32.bvr[13])); __asm__ volatile("msr DBGBCR13_EL1, %0" : : "r"((uint64_t)debug_state->uds.ds32.bcr[13])); case 13: __asm__ volatile("msr DBGBVR12_EL1, %0" : : "r"((uint64_t)debug_state->uds.ds32.bvr[12])); __asm__ volatile("msr DBGBCR12_EL1, %0" : : "r"((uint64_t)debug_state->uds.ds32.bcr[12])); case 12: __asm__ volatile("msr DBGBVR11_EL1, %0" : : "r"((uint64_t)debug_state->uds.ds32.bvr[11])); __asm__ volatile("msr DBGBCR11_EL1, %0" : : "r"((uint64_t)debug_state->uds.ds32.bcr[11])); case 11: __asm__ volatile("msr DBGBVR10_EL1, %0" : : "r"((uint64_t)debug_state->uds.ds32.bvr[10])); __asm__ volatile("msr DBGBCR10_EL1, %0" : : "r"((uint64_t)debug_state->uds.ds32.bcr[10])); case 10: __asm__ volatile("msr DBGBVR9_EL1, %0" : : "r"((uint64_t)debug_state->uds.ds32.bvr[9])); __asm__ volatile("msr DBGBCR9_EL1, %0" : : "r"((uint64_t)debug_state->uds.ds32.bcr[9])); case 9: __asm__ volatile("msr DBGBVR8_EL1, %0" : : "r"((uint64_t)debug_state->uds.ds32.bvr[8])); __asm__ volatile("msr DBGBCR8_EL1, %0" : : "r"((uint64_t)debug_state->uds.ds32.bcr[8])); case 8: __asm__ volatile("msr DBGBVR7_EL1, %0" : : "r"((uint64_t)debug_state->uds.ds32.bvr[7])); __asm__ volatile("msr DBGBCR7_EL1, %0" : : "r"((uint64_t)debug_state->uds.ds32.bcr[7])); case 7: __asm__ volatile("msr DBGBVR6_EL1, %0" : : "r"((uint64_t)debug_state->uds.ds32.bvr[6])); __asm__ volatile("msr DBGBCR6_EL1, %0" : : "r"((uint64_t)debug_state->uds.ds32.bcr[6])); case 6: __asm__ volatile("msr DBGBVR5_EL1, %0" : : "r"((uint64_t)debug_state->uds.ds32.bvr[5])); __asm__ volatile("msr DBGBCR5_EL1, %0" : : "r"((uint64_t)debug_state->uds.ds32.bcr[5])); case 5: __asm__ volatile("msr DBGBVR4_EL1, %0" : : "r"((uint64_t)debug_state->uds.ds32.bvr[4])); __asm__ volatile("msr DBGBCR4_EL1, %0" : : "r"((uint64_t)debug_state->uds.ds32.bcr[4])); case 4: __asm__ volatile("msr DBGBVR3_EL1, %0" : : "r"((uint64_t)debug_state->uds.ds32.bvr[3])); __asm__ volatile("msr DBGBCR3_EL1, %0" : : "r"((uint64_t)debug_state->uds.ds32.bcr[3])); case 3: __asm__ volatile("msr DBGBVR2_EL1, %0" : : "r"((uint64_t)debug_state->uds.ds32.bvr[2])); __asm__ volatile("msr DBGBCR2_EL1, %0" : : "r"((uint64_t)debug_state->uds.ds32.bcr[2])); case 2: __asm__ volatile("msr DBGBVR1_EL1, %0" : : "r"((uint64_t)debug_state->uds.ds32.bvr[1])); __asm__ volatile("msr DBGBCR1_EL1, %0" : : "r"((uint64_t)debug_state->uds.ds32.bcr[1])); case 1: __asm__ volatile("msr DBGBVR0_EL1, %0" : : "r"((uint64_t)debug_state->uds.ds32.bvr[0])); __asm__ volatile("msr DBGBCR0_EL1, %0" : : "r"((uint64_t)debug_state->uds.ds32.bcr[0])); default: break; } switch (debug_info->num_watchpoint_pairs) { case 16: __asm__ volatile("msr DBGWVR15_EL1, %0" : : "r"((uint64_t)debug_state->uds.ds32.wvr[15])); __asm__ volatile("msr DBGWCR15_EL1, %0" : : "r"((uint64_t)debug_state->uds.ds32.wcr[15])); case 15: __asm__ volatile("msr DBGWVR14_EL1, %0" : : "r"((uint64_t)debug_state->uds.ds32.wvr[14])); __asm__ volatile("msr DBGWCR14_EL1, %0" : : "r"((uint64_t)debug_state->uds.ds32.wcr[14])); case 14: __asm__ volatile("msr DBGWVR13_EL1, %0" : : "r"((uint64_t)debug_state->uds.ds32.wvr[13])); __asm__ volatile("msr DBGWCR13_EL1, %0" : : "r"((uint64_t)debug_state->uds.ds32.wcr[13])); case 13: __asm__ volatile("msr DBGWVR12_EL1, %0" : : "r"((uint64_t)debug_state->uds.ds32.wvr[12])); __asm__ volatile("msr DBGWCR12_EL1, %0" : : "r"((uint64_t)debug_state->uds.ds32.wcr[12])); case 12: __asm__ volatile("msr DBGWVR11_EL1, %0" : : "r"((uint64_t)debug_state->uds.ds32.wvr[11])); __asm__ volatile("msr DBGWCR11_EL1, %0" : : "r"((uint64_t)debug_state->uds.ds32.wcr[11])); case 11: __asm__ volatile("msr DBGWVR10_EL1, %0" : : "r"((uint64_t)debug_state->uds.ds32.wvr[10])); __asm__ volatile("msr DBGWCR10_EL1, %0" : : "r"((uint64_t)debug_state->uds.ds32.wcr[10])); case 10: __asm__ volatile("msr DBGWVR9_EL1, %0" : : "r"((uint64_t)debug_state->uds.ds32.wvr[9])); __asm__ volatile("msr DBGWCR9_EL1, %0" : : "r"((uint64_t)debug_state->uds.ds32.wcr[9])); case 9: __asm__ volatile("msr DBGWVR8_EL1, %0" : : "r"((uint64_t)debug_state->uds.ds32.wvr[8])); __asm__ volatile("msr DBGWCR8_EL1, %0" : : "r"((uint64_t)debug_state->uds.ds32.wcr[8])); case 8: __asm__ volatile("msr DBGWVR7_EL1, %0" : : "r"((uint64_t)debug_state->uds.ds32.wvr[7])); __asm__ volatile("msr DBGWCR7_EL1, %0" : : "r"((uint64_t)debug_state->uds.ds32.wcr[7])); case 7: __asm__ volatile("msr DBGWVR6_EL1, %0" : : "r"((uint64_t)debug_state->uds.ds32.wvr[6])); __asm__ volatile("msr DBGWCR6_EL1, %0" : : "r"((uint64_t)debug_state->uds.ds32.wcr[6])); case 6: __asm__ volatile("msr DBGWVR5_EL1, %0" : : "r"((uint64_t)debug_state->uds.ds32.wvr[5])); __asm__ volatile("msr DBGWCR5_EL1, %0" : : "r"((uint64_t)debug_state->uds.ds32.wcr[5])); case 5: __asm__ volatile("msr DBGWVR4_EL1, %0" : : "r"((uint64_t)debug_state->uds.ds32.wvr[4])); __asm__ volatile("msr DBGWCR4_EL1, %0" : : "r"((uint64_t)debug_state->uds.ds32.wcr[4])); case 4: __asm__ volatile("msr DBGWVR3_EL1, %0" : : "r"((uint64_t)debug_state->uds.ds32.wvr[3])); __asm__ volatile("msr DBGWCR3_EL1, %0" : : "r"((uint64_t)debug_state->uds.ds32.wcr[3])); case 3: __asm__ volatile("msr DBGWVR2_EL1, %0" : : "r"((uint64_t)debug_state->uds.ds32.wvr[2])); __asm__ volatile("msr DBGWCR2_EL1, %0" : : "r"((uint64_t)debug_state->uds.ds32.wcr[2])); case 2: __asm__ volatile("msr DBGWVR1_EL1, %0" : : "r"((uint64_t)debug_state->uds.ds32.wvr[1])); __asm__ volatile("msr DBGWCR1_EL1, %0" : : "r"((uint64_t)debug_state->uds.ds32.wcr[1])); case 1: __asm__ volatile("msr DBGWVR0_EL1, %0" : : "r"((uint64_t)debug_state->uds.ds32.wvr[0])); __asm__ volatile("msr DBGWCR0_EL1, %0" : : "r"((uint64_t)debug_state->uds.ds32.wcr[0])); default: break; } for (i = 0; i < debug_info->num_breakpoint_pairs; i++) { if (0 != debug_state->uds.ds32.bcr[i]) { set_mde = 1; break; } } for (i = 0; i < debug_info->num_watchpoint_pairs; i++) { if (0 != debug_state->uds.ds32.wcr[i]) { set_mde = 1; break; } } /* * Breakpoint/Watchpoint Enable */ if (set_mde) { __asm__ volatile("mrs %0, MDSCR_EL1" : "=r"(state)); state |= 0x8000; // MDSCR_EL1[MDE] __asm__ volatile("msr MDSCR_EL1, %0" : : "r"(state)); } else { __asm__ volatile("mrs %0, MDSCR_EL1" : "=r"(state)); state &= ~0x8000; __asm__ volatile("msr MDSCR_EL1, %0" : : "r"(state)); } /* * Software debug single step enable */ if (debug_state->uds.ds32.mdscr_el1 & 0x1) { __asm__ volatile("mrs %0, MDSCR_EL1" : "=r"(state)); state = (state & ~0x8000) | 0x1; // ~MDE | SS : no brk/watch while single stepping (which we've set) __asm__ volatile("msr MDSCR_EL1, %0" : : "r"(state)); set_saved_state_cpsr((current_thread()->machine.upcb), get_saved_state_cpsr((current_thread()->machine.upcb)) | PSR64_SS); } else { __asm__ volatile("mrs %0, MDSCR_EL1" : "=r"(state)); state &= ~0x1; __asm__ volatile("msr MDSCR_EL1, %0" : : "r"(state)); #if SINGLE_STEP_RETIRE_ERRATA // Workaround for radar 20619637 __builtin_arm_isb(ISB_SY); #endif } (void) ml_set_interrupts_enabled(intr); return; } void arm_debug_set64(arm_debug_state_t *debug_state) { struct cpu_data *cpu_data_ptr; arm_debug_info_t *debug_info = arm_debug_info(); volatile uint64_t state; boolean_t intr, set_mde = 0; arm_debug_state_t off_state; uint32_t i; intr = ml_set_interrupts_enabled(FALSE); cpu_data_ptr = getCpuDatap(); // Set current user debug cpu_data_ptr->cpu_user_debug = debug_state; if (NULL == debug_state) { bzero(&off_state, sizeof(off_state)); debug_state = &off_state; } switch (debug_info->num_breakpoint_pairs) { case 16: __asm__ volatile("msr DBGBVR15_EL1, %0" : : "r"(debug_state->uds.ds64.bvr[15])); __asm__ volatile("msr DBGBCR15_EL1, %0" : : "r"(debug_state->uds.ds64.bcr[15])); case 15: __asm__ volatile("msr DBGBVR14_EL1, %0" : : "r"(debug_state->uds.ds64.bvr[14])); __asm__ volatile("msr DBGBCR14_EL1, %0" : : "r"(debug_state->uds.ds64.bcr[14])); case 14: __asm__ volatile("msr DBGBVR13_EL1, %0" : : "r"(debug_state->uds.ds64.bvr[13])); __asm__ volatile("msr DBGBCR13_EL1, %0" : : "r"(debug_state->uds.ds64.bcr[13])); case 13: __asm__ volatile("msr DBGBVR12_EL1, %0" : : "r"(debug_state->uds.ds64.bvr[12])); __asm__ volatile("msr DBGBCR12_EL1, %0" : : "r"(debug_state->uds.ds64.bcr[12])); case 12: __asm__ volatile("msr DBGBVR11_EL1, %0" : : "r"(debug_state->uds.ds64.bvr[11])); __asm__ volatile("msr DBGBCR11_EL1, %0" : : "r"(debug_state->uds.ds64.bcr[11])); case 11: __asm__ volatile("msr DBGBVR10_EL1, %0" : : "r"(debug_state->uds.ds64.bvr[10])); __asm__ volatile("msr DBGBCR10_EL1, %0" : : "r"(debug_state->uds.ds64.bcr[10])); case 10: __asm__ volatile("msr DBGBVR9_EL1, %0" : : "r"(debug_state->uds.ds64.bvr[9])); __asm__ volatile("msr DBGBCR9_EL1, %0" : : "r"(debug_state->uds.ds64.bcr[9])); case 9: __asm__ volatile("msr DBGBVR8_EL1, %0" : : "r"(debug_state->uds.ds64.bvr[8])); __asm__ volatile("msr DBGBCR8_EL1, %0" : : "r"(debug_state->uds.ds64.bcr[8])); case 8: __asm__ volatile("msr DBGBVR7_EL1, %0" : : "r"(debug_state->uds.ds64.bvr[7])); __asm__ volatile("msr DBGBCR7_EL1, %0" : : "r"(debug_state->uds.ds64.bcr[7])); case 7: __asm__ volatile("msr DBGBVR6_EL1, %0" : : "r"(debug_state->uds.ds64.bvr[6])); __asm__ volatile("msr DBGBCR6_EL1, %0" : : "r"(debug_state->uds.ds64.bcr[6])); case 6: __asm__ volatile("msr DBGBVR5_EL1, %0" : : "r"(debug_state->uds.ds64.bvr[5])); __asm__ volatile("msr DBGBCR5_EL1, %0" : : "r"(debug_state->uds.ds64.bcr[5])); case 5: __asm__ volatile("msr DBGBVR4_EL1, %0" : : "r"(debug_state->uds.ds64.bvr[4])); __asm__ volatile("msr DBGBCR4_EL1, %0" : : "r"(debug_state->uds.ds64.bcr[4])); case 4: __asm__ volatile("msr DBGBVR3_EL1, %0" : : "r"(debug_state->uds.ds64.bvr[3])); __asm__ volatile("msr DBGBCR3_EL1, %0" : : "r"(debug_state->uds.ds64.bcr[3])); case 3: __asm__ volatile("msr DBGBVR2_EL1, %0" : : "r"(debug_state->uds.ds64.bvr[2])); __asm__ volatile("msr DBGBCR2_EL1, %0" : : "r"(debug_state->uds.ds64.bcr[2])); case 2: __asm__ volatile("msr DBGBVR1_EL1, %0" : : "r"(debug_state->uds.ds64.bvr[1])); __asm__ volatile("msr DBGBCR1_EL1, %0" : : "r"(debug_state->uds.ds64.bcr[1])); case 1: __asm__ volatile("msr DBGBVR0_EL1, %0" : : "r"(debug_state->uds.ds64.bvr[0])); __asm__ volatile("msr DBGBCR0_EL1, %0" : : "r"(debug_state->uds.ds64.bcr[0])); default: break; } switch (debug_info->num_watchpoint_pairs) { case 16: __asm__ volatile("msr DBGWVR15_EL1, %0" : : "r"(debug_state->uds.ds64.wvr[15])); __asm__ volatile("msr DBGWCR15_EL1, %0" : : "r"(debug_state->uds.ds64.wcr[15])); case 15: __asm__ volatile("msr DBGWVR14_EL1, %0" : : "r"(debug_state->uds.ds64.wvr[14])); __asm__ volatile("msr DBGWCR14_EL1, %0" : : "r"(debug_state->uds.ds64.wcr[14])); case 14: __asm__ volatile("msr DBGWVR13_EL1, %0" : : "r"(debug_state->uds.ds64.wvr[13])); __asm__ volatile("msr DBGWCR13_EL1, %0" : : "r"(debug_state->uds.ds64.wcr[13])); case 13: __asm__ volatile("msr DBGWVR12_EL1, %0" : : "r"(debug_state->uds.ds64.wvr[12])); __asm__ volatile("msr DBGWCR12_EL1, %0" : : "r"(debug_state->uds.ds64.wcr[12])); case 12: __asm__ volatile("msr DBGWVR11_EL1, %0" : : "r"(debug_state->uds.ds64.wvr[11])); __asm__ volatile("msr DBGWCR11_EL1, %0" : : "r"(debug_state->uds.ds64.wcr[11])); case 11: __asm__ volatile("msr DBGWVR10_EL1, %0" : : "r"(debug_state->uds.ds64.wvr[10])); __asm__ volatile("msr DBGWCR10_EL1, %0" : : "r"(debug_state->uds.ds64.wcr[10])); case 10: __asm__ volatile("msr DBGWVR9_EL1, %0" : : "r"(debug_state->uds.ds64.wvr[9])); __asm__ volatile("msr DBGWCR9_EL1, %0" : : "r"(debug_state->uds.ds64.wcr[9])); case 9: __asm__ volatile("msr DBGWVR8_EL1, %0" : : "r"(debug_state->uds.ds64.wvr[8])); __asm__ volatile("msr DBGWCR8_EL1, %0" : : "r"(debug_state->uds.ds64.wcr[8])); case 8: __asm__ volatile("msr DBGWVR7_EL1, %0" : : "r"(debug_state->uds.ds64.wvr[7])); __asm__ volatile("msr DBGWCR7_EL1, %0" : : "r"(debug_state->uds.ds64.wcr[7])); case 7: __asm__ volatile("msr DBGWVR6_EL1, %0" : : "r"(debug_state->uds.ds64.wvr[6])); __asm__ volatile("msr DBGWCR6_EL1, %0" : : "r"(debug_state->uds.ds64.wcr[6])); case 6: __asm__ volatile("msr DBGWVR5_EL1, %0" : : "r"(debug_state->uds.ds64.wvr[5])); __asm__ volatile("msr DBGWCR5_EL1, %0" : : "r"(debug_state->uds.ds64.wcr[5])); case 5: __asm__ volatile("msr DBGWVR4_EL1, %0" : : "r"(debug_state->uds.ds64.wvr[4])); __asm__ volatile("msr DBGWCR4_EL1, %0" : : "r"(debug_state->uds.ds64.wcr[4])); case 4: __asm__ volatile("msr DBGWVR3_EL1, %0" : : "r"(debug_state->uds.ds64.wvr[3])); __asm__ volatile("msr DBGWCR3_EL1, %0" : : "r"(debug_state->uds.ds64.wcr[3])); case 3: __asm__ volatile("msr DBGWVR2_EL1, %0" : : "r"(debug_state->uds.ds64.wvr[2])); __asm__ volatile("msr DBGWCR2_EL1, %0" : : "r"(debug_state->uds.ds64.wcr[2])); case 2: __asm__ volatile("msr DBGWVR1_EL1, %0" : : "r"(debug_state->uds.ds64.wvr[1])); __asm__ volatile("msr DBGWCR1_EL1, %0" : : "r"(debug_state->uds.ds64.wcr[1])); case 1: __asm__ volatile("msr DBGWVR0_EL1, %0" : : "r"(debug_state->uds.ds64.wvr[0])); __asm__ volatile("msr DBGWCR0_EL1, %0" : : "r"(debug_state->uds.ds64.wcr[0])); default: break; } for (i = 0; i < debug_info->num_breakpoint_pairs; i++) { if (0 != debug_state->uds.ds64.bcr[i]) { set_mde = 1; break; } } for (i = 0; i < debug_info->num_watchpoint_pairs; i++) { if (0 != debug_state->uds.ds64.wcr[i]) { set_mde = 1; break; } } /* * Breakpoint/Watchpoint Enable */ if (set_mde) { __asm__ volatile("mrs %0, MDSCR_EL1" : "=r"(state)); state |= 0x8000; // MDSCR_EL1[MDE] __asm__ volatile("msr MDSCR_EL1, %0" : : "r"(state)); } /* * Software debug single step enable */ if (debug_state->uds.ds64.mdscr_el1 & 0x1) { __asm__ volatile("mrs %0, MDSCR_EL1" : "=r"(state)); state = (state & ~0x8000) | 0x1; // ~MDE | SS : no brk/watch while single stepping (which we've set) __asm__ volatile("msr MDSCR_EL1, %0" : : "r"(state)); set_saved_state_cpsr((current_thread()->machine.upcb), get_saved_state_cpsr((current_thread()->machine.upcb)) | PSR64_SS); } else { __asm__ volatile("mrs %0, MDSCR_EL1" : "=r"(state)); state &= ~0x1; __asm__ volatile("msr MDSCR_EL1, %0" : : "r"(state)); #if SINGLE_STEP_RETIRE_ERRATA // Workaround for radar 20619637 __builtin_arm_isb(ISB_SY); #endif } (void) ml_set_interrupts_enabled(intr); return; } void arm_debug_set(arm_debug_state_t *debug_state) { if (debug_state) { switch (debug_state->dsh.flavor) { case ARM_DEBUG_STATE32: arm_debug_set32(debug_state); break; case ARM_DEBUG_STATE64: arm_debug_set64(debug_state); break; default: panic("arm_debug_set"); break; } } else { if (thread_is_64bit(current_thread())) arm_debug_set64(debug_state); else arm_debug_set32(debug_state); } } #define VM_MAX_ADDRESS32 ((vm_address_t) 0x80000000) boolean_t debug_legacy_state_is_valid(arm_legacy_debug_state_t *debug_state) { arm_debug_info_t *debug_info = arm_debug_info(); uint32_t i; for (i = 0; i < debug_info->num_breakpoint_pairs; i++) { if (0 != debug_state->bcr[i] && VM_MAX_ADDRESS32 <= debug_state->bvr[i]) return FALSE; } for (i = 0; i < debug_info->num_watchpoint_pairs; i++) { if (0 != debug_state->wcr[i] && VM_MAX_ADDRESS32 <= debug_state->wvr[i]) return FALSE; } return TRUE; } boolean_t debug_state_is_valid32(arm_debug_state32_t *debug_state) { arm_debug_info_t *debug_info = arm_debug_info(); uint32_t i; for (i = 0; i < debug_info->num_breakpoint_pairs; i++) { if (0 != debug_state->bcr[i] && VM_MAX_ADDRESS32 <= debug_state->bvr[i]) return FALSE; } for (i = 0; i < debug_info->num_watchpoint_pairs; i++) { if (0 != debug_state->wcr[i] && VM_MAX_ADDRESS32 <= debug_state->wvr[i]) return FALSE; } return TRUE; } boolean_t debug_state_is_valid64(arm_debug_state64_t *debug_state) { arm_debug_info_t *debug_info = arm_debug_info(); uint32_t i; for (i = 0; i < debug_info->num_breakpoint_pairs; i++) { if (0 != debug_state->bcr[i] && MACH_VM_MAX_ADDRESS <= debug_state->bvr[i]) return FALSE; } for (i = 0; i < debug_info->num_watchpoint_pairs; i++) { if (0 != debug_state->wcr[i] && MACH_VM_MAX_ADDRESS <= debug_state->wvr[i]) return FALSE; } return TRUE; } /* * Duplicate one arm_debug_state_t to another. "all" parameter * is ignored in the case of ARM -- Is this the right assumption? */ void copy_legacy_debug_state( arm_legacy_debug_state_t *src, arm_legacy_debug_state_t *target, __unused boolean_t all) { bcopy(src, target, sizeof(arm_legacy_debug_state_t)); } void copy_debug_state32( arm_debug_state32_t *src, arm_debug_state32_t *target, __unused boolean_t all) { bcopy(src, target, sizeof(arm_debug_state32_t)); } void copy_debug_state64( arm_debug_state64_t *src, arm_debug_state64_t *target, __unused boolean_t all) { bcopy(src, target, sizeof(arm_debug_state64_t)); } kern_return_t machine_thread_set_tsd_base( thread_t thread, mach_vm_offset_t tsd_base) { if (thread->task == kernel_task) { return KERN_INVALID_ARGUMENT; } if (tsd_base & MACHDEP_CPUNUM_MASK) { return KERN_INVALID_ARGUMENT; } if (thread_is_64bit(thread)) { if (tsd_base > vm_map_max(thread->map)) tsd_base = 0ULL; } else { if (tsd_base > UINT32_MAX) tsd_base = 0ULL; } thread->machine.cthread_self = tsd_base; /* For current thread, make the TSD base active immediately */ if (thread == current_thread()) { uint64_t cpunum, tpidrro_el0; mp_disable_preemption(); tpidrro_el0 = get_tpidrro(); cpunum = tpidrro_el0 & (MACHDEP_CPUNUM_MASK); set_tpidrro(tsd_base | cpunum); mp_enable_preemption(); } return KERN_SUCCESS; } |