Print this page
9210 remove KMDB branch debugging support
9211 ::crregs could do with cr2/cr3 support
9209 ::ttrace should be able to filter by thread
Reviewed by: Patrick Mooney <patrick.mooney@joyent.com>


   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
  24  * Use is subject to license terms.


  25  */
  26 
  27 #pragma ident   "%Z%%M% %I%     %E% SMI"
  28 
  29 /*
  30  * Debugger entry for both master and slave CPUs
  31  */
  32 
  33 #if defined(__lint)
  34 #include <sys/types.h>
  35 #endif
  36 
  37 #include <sys/segments.h>
  38 #include <sys/asm_linkage.h>
  39 #include <sys/controlregs.h>
  40 #include <sys/x86_archext.h>
  41 #include <sys/privregs.h>
  42 #include <sys/machprivregs.h>
  43 #include <sys/kdi_regs.h>
  44 #include <sys/uadmin.h>
  45 #include <sys/psw.h>
  46 
  47 #ifdef _ASM
  48 


  60         addl    (%edx), %eax
  61 
  62 /*
  63  * Save copies of the IDT and GDT descriptors.  Note that we only save the IDT
  64  * and GDT if the IDT isn't ours, as we may be legitimately re-entering the
  65  * debugger through the trap handler.  We don't want to clobber the saved IDT
  66  * in the process, as we'd end up resuming the world on our IDT.
  67  */
  68 #define SAVE_IDTGDT                             \
  69         movl    %gs:CPU_IDT, %edx;              \
  70         cmpl    $kdi_idt, %edx;                 \
  71         je      1f;                             \
  72         movl    %edx, KRS_IDT(%eax);            \
  73         movl    %gs:CPU_GDT, %edx;              \
  74         movl    %edx, KRS_GDT(%eax);            \
  75 1:
  76 
  77 /*
  78  * Given the address of the current CPU's cpusave area in %edi, the following
  79  * macro restores the debugging state to said CPU.  Restored state includes
  80  * the debug registers from the global %dr variables, and debugging MSRs from
  81  * the CPU save area.  This code would be in a separate routine, but for the
  82  * fact that some of the MSRs are jump-sensitive.  As such, we need to minimize
  83  * the number of jumps taken subsequent to the update of said MSRs.  We can
  84  * remove one jump (the ret) by using a macro instead of a function for the
  85  * debugging state restoration code.
  86  *
  87  * Takes the cpusave area in %edi as a parameter, clobbers %eax-%edx
  88  */     
  89 #define KDI_RESTORE_DEBUGGING_STATE \
  90         leal    kdi_drreg, %ebx;                                \
  91                                                                 \
  92         pushl   DR_CTL(%ebx);                                   \
  93         pushl   $7;                                             \
  94         call    kdi_dreg_set;                                   \
  95         addl    $8, %esp;                                       \
  96                                                                 \
  97         pushl   $KDIREG_DRSTAT_RESERVED;                                \
  98         pushl   $6;                                             \
  99         call    kdi_dreg_set;                                   \
 100         addl    $8, %esp;                                       \
 101                                                                 \
 102         pushl   DRADDR_OFF(0)(%ebx);                            \
 103         pushl   $0;                                             \
 104         call    kdi_dreg_set;                                   \
 105         addl    $8, %esp;                                       \
 106                                                                 \
 107         pushl   DRADDR_OFF(1)(%ebx);                            \
 108         pushl   $1;                                             \
 109         call    kdi_dreg_set;                                   \
 110         addl    $8, %esp;                                       \
 111                                                                 \
 112         pushl   DRADDR_OFF(2)(%ebx);                            \
 113         pushl   $2;                                             \
 114         call    kdi_dreg_set;                                   \
 115         addl    $8, %esp;                                       \
 116                                                                 \
 117         pushl   DRADDR_OFF(3)(%ebx);                            \
 118         pushl   $3;                                             \
 119         call    kdi_dreg_set;                                   \
 120         addl    $8, %esp;                                       \
 121                                                                 \
 122         /*                                                      \
 123          * Write any requested MSRs.                            \
 124          */                                                     \
 125         movl    KRS_MSR(%edi), %ebx;                            \
 126         cmpl    $0, %ebx;                                       \
 127         je      3f;                                             \
 128 1:                                                              \
 129         movl    MSR_NUM(%ebx), %ecx;                            \
 130         cmpl    $0, %ecx;                                       \
 131         je      3f;                                             \
 132                                                                 \
 133         movl    MSR_TYPE(%ebx), %edx;                           \
 134         cmpl    $KDI_MSR_WRITE, %edx;                           \
 135         jne     2f;                                             \
 136                                                                 \
 137         movl    MSR_VALP(%ebx), %edx;                           \
 138         movl    0(%edx), %eax;                                  \
 139         movl    4(%edx), %edx;                                  \
 140         wrmsr;                                                  \
 141 2:                                                              \
 142         addl    $MSR_SIZE, %ebx;                                \
 143         jmp     1b;                                             \
 144 3:                                                              \
 145         /*                                                      \
 146          * We must not branch after re-enabling LBR.  If        \
 147          * kdi_wsr_wrexit_msr is set, it contains the number    \
 148          * of the MSR that controls LBR.  kdi_wsr_wrexit_valp   \
 149          * contains the value that is to be written to enable   \
 150          * LBR.                                                 \
 151          */                                                     \
 152         movl    kdi_msr_wrexit_msr, %ecx;                       \
 153         cmpl    $0, %ecx;                                       \
 154         je      1f;                                             \
 155                                                                 \
 156         movl    kdi_msr_wrexit_valp, %edx;                      \
 157         movl    0(%edx), %eax;                                  \
 158         movl    4(%edx), %edx;                                  \
 159                                                                 \
 160         wrmsr;                                                  \
 161 1:
 162 
 163 #define KDI_RESTORE_REGS() \
 164         /* Discard savfp and savpc */ \
 165         addl    $8, %esp; \
 166         popl    %ss; \
 167         popl    %gs; \
 168         popl    %fs; \
 169         popl    %es; \
 170         popl    %ds; \
 171         popal; \
 172         /* Discard trapno and err */ \
 173         addl    $8, %esp
 174 
 175 /*
 176  * Each cpusave buffer has an area set aside for a ring buffer of breadcrumbs.
 177  * The following macros manage the buffer.
 178  */
 179 
 180 /* Advance the ring buffer */
 181 #define ADVANCE_CRUMB_POINTER(cpusave, tmp1, tmp2) \


 357          */
 358 
 359         jmp     kdi_save_common_state
 360 
 361         SET_SIZE(kdi_master_entry)
 362         SET_SIZE(kdi_cmnint)
 363 
 364 #endif  /* __lint */
 365 
 366 /*
 367  * The cross-call handler for slave CPUs.
 368  *
 369  * The debugger is single-threaded, so only one CPU, called the master, may be
 370  * running it at any given time.  The other CPUs, known as slaves, spin in a
 371  * busy loop until there's something for them to do.  This is the entry point
 372  * for the slaves - they'll be sent here in response to a cross-call sent by the
 373  * master.
 374  */
 375 
 376 #if defined(__lint)
 377 char kdi_slave_entry_patch;
 378 
 379 void
 380 kdi_slave_entry(void)
 381 {
 382 }
 383 #else /* __lint */
 384         .globl  kdi_slave_entry_patch;
 385 
 386         ENTRY_NP(kdi_slave_entry)
 387 
 388         /* kdi_msr_add_clrentry knows where this is */
 389 kdi_slave_entry_patch:
 390         KDI_MSR_PATCH;
 391 
 392         /*
 393          * Cross calls are implemented as function calls, so our stack
 394          * currently looks like one you'd get from a zero-argument function
 395          * call. There's an %eip at %esp, and that's about it.  We want to
 396          * make it look like the master CPU's stack.  By doing this, we can
 397          * use the same resume code for both master and slave.  We need to
 398          * make our stack look like a `struct regs' before we jump into the
 399          * common save routine.
 400          */
 401 
 402         pushl   %cs
 403         pushfl
 404         pushl   $-1             /* A phony trap error code */
 405         pushl   $-1             /* A phony trap number */
 406         pushal
 407         pushl   %ds
 408         pushl   %es
 409         pushl   %fs
 410         pushl   %gs
 411         pushl   %ss


 525         movl    %eax, KRS_DROFF(0)(%edi)
 526 
 527         pushl   $1
 528         call    kdi_dreg_get
 529         addl    $4, %esp
 530         movl    %eax, KRS_DROFF(1)(%edi)
 531 
 532         pushl   $2
 533         call    kdi_dreg_get
 534         addl    $4, %esp
 535         movl    %eax, KRS_DROFF(2)(%edi)
 536 
 537         pushl   $3
 538         call    kdi_dreg_get
 539         addl    $4, %esp
 540         movl    %eax, KRS_DROFF(3)(%edi)
 541 
 542         movl    %edi, %eax
 543         popl    %edi
 544 
 545         /*
 546          * Save any requested MSRs.
 547          */
 548         movl    KRS_MSR(%eax), %ecx
 549         cmpl    $0, %ecx
 550         je      no_msr
 551 
 552         pushl   %eax            /* rdmsr clobbers %eax */
 553         movl    %ecx, %ebx
 554 1:
 555         movl    MSR_NUM(%ebx), %ecx
 556         cmpl    $0, %ecx
 557         je      msr_done
 558 
 559         movl    MSR_TYPE(%ebx), %edx
 560         cmpl    $KDI_MSR_READ, %edx
 561         jne     msr_next
 562 
 563         rdmsr                   /* addr in %ecx, value into %edx:%eax */
 564         movl    %eax, MSR_VAL(%ebx)
 565         movl    %edx, _CONST(MSR_VAL + 4)(%ebx)
 566 
 567 msr_next:
 568         addl    $MSR_SIZE, %ebx
 569         jmp     1b
 570 
 571 msr_done:
 572         popl    %eax
 573 
 574 no_msr:
 575         clr     %ebp            /* stack traces should end here */
 576 
 577         pushl   %eax
 578         call    kdi_debugger_entry
 579         popl    %eax
 580  
 581         jmp     kdi_resume
 582 
 583         SET_SIZE(kdi_save_common_state)
 584 
 585 #endif  /* !__lint */
 586 
 587 /*
 588  * Resume the world.  The code that calls kdi_resume has already
 589  * decided whether or not to restore the IDT.
 590  */
 591 #if defined(__lint)
 592 void
 593 kdi_resume(void)
 594 {




   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
  24  * Use is subject to license terms.
  25  *
  26  * Copyright 2018 Joyent, Inc.
  27  */
  28 


  29 /*
  30  * Debugger entry for both master and slave CPUs
  31  */
  32 
  33 #if defined(__lint)
  34 #include <sys/types.h>
  35 #endif
  36 
  37 #include <sys/segments.h>
  38 #include <sys/asm_linkage.h>
  39 #include <sys/controlregs.h>
  40 #include <sys/x86_archext.h>
  41 #include <sys/privregs.h>
  42 #include <sys/machprivregs.h>
  43 #include <sys/kdi_regs.h>
  44 #include <sys/uadmin.h>
  45 #include <sys/psw.h>
  46 
  47 #ifdef _ASM
  48 


  60         addl    (%edx), %eax
  61 
  62 /*
  63  * Save copies of the IDT and GDT descriptors.  Note that we only save the IDT
  64  * and GDT if the IDT isn't ours, as we may be legitimately re-entering the
  65  * debugger through the trap handler.  We don't want to clobber the saved IDT
  66  * in the process, as we'd end up resuming the world on our IDT.
  67  */
  68 #define SAVE_IDTGDT                             \
  69         movl    %gs:CPU_IDT, %edx;              \
  70         cmpl    $kdi_idt, %edx;                 \
  71         je      1f;                             \
  72         movl    %edx, KRS_IDT(%eax);            \
  73         movl    %gs:CPU_GDT, %edx;              \
  74         movl    %edx, KRS_GDT(%eax);            \
  75 1:
  76 
  77 /*
  78  * Given the address of the current CPU's cpusave area in %edi, the following
  79  * macro restores the debugging state to said CPU.  Restored state includes
  80  * the debug registers from the global %dr variables.







  81  */
  82 #define KDI_RESTORE_DEBUGGING_STATE \
  83         leal    kdi_drreg, %ebx;                                \
  84                                                                 \
  85         pushl   DR_CTL(%ebx);                                   \
  86         pushl   $7;                                             \
  87         call    kdi_dreg_set;                                   \
  88         addl    $8, %esp;                                       \
  89                                                                 \
  90         pushl   $KDIREG_DRSTAT_RESERVED;                                \
  91         pushl   $6;                                             \
  92         call    kdi_dreg_set;                                   \
  93         addl    $8, %esp;                                       \
  94                                                                 \
  95         pushl   DRADDR_OFF(0)(%ebx);                            \
  96         pushl   $0;                                             \
  97         call    kdi_dreg_set;                                   \
  98         addl    $8, %esp;                                       \
  99                                                                 \
 100         pushl   DRADDR_OFF(1)(%ebx);                            \
 101         pushl   $1;                                             \
 102         call    kdi_dreg_set;                                   \
 103         addl    $8, %esp;                                       \
 104                                                                 \
 105         pushl   DRADDR_OFF(2)(%ebx);                            \
 106         pushl   $2;                                             \
 107         call    kdi_dreg_set;                                   \
 108         addl    $8, %esp;                                       \
 109                                                                 \
 110         pushl   DRADDR_OFF(3)(%ebx);                            \
 111         pushl   $3;                                             \
 112         call    kdi_dreg_set;                                   \
 113         addl    $8, %esp;









































 114 
 115 #define KDI_RESTORE_REGS() \
 116         /* Discard savfp and savpc */ \
 117         addl    $8, %esp; \
 118         popl    %ss; \
 119         popl    %gs; \
 120         popl    %fs; \
 121         popl    %es; \
 122         popl    %ds; \
 123         popal; \
 124         /* Discard trapno and err */ \
 125         addl    $8, %esp
 126 
 127 /*
 128  * Each cpusave buffer has an area set aside for a ring buffer of breadcrumbs.
 129  * The following macros manage the buffer.
 130  */
 131 
 132 /* Advance the ring buffer */
 133 #define ADVANCE_CRUMB_POINTER(cpusave, tmp1, tmp2) \


 309          */
 310 
 311         jmp     kdi_save_common_state
 312 
 313         SET_SIZE(kdi_master_entry)
 314         SET_SIZE(kdi_cmnint)
 315 
 316 #endif  /* __lint */
 317 
 318 /*
 319  * The cross-call handler for slave CPUs.
 320  *
 321  * The debugger is single-threaded, so only one CPU, called the master, may be
 322  * running it at any given time.  The other CPUs, known as slaves, spin in a
 323  * busy loop until there's something for them to do.  This is the entry point
 324  * for the slaves - they'll be sent here in response to a cross-call sent by the
 325  * master.
 326  */
 327 
 328 #if defined(__lint)


 329 void
 330 kdi_slave_entry(void)
 331 {
 332 }
 333 #else /* __lint */


 334         ENTRY_NP(kdi_slave_entry)
 335 




 336         /*
 337          * Cross calls are implemented as function calls, so our stack
 338          * currently looks like one you'd get from a zero-argument function
 339          * call. There's an %eip at %esp, and that's about it.  We want to
 340          * make it look like the master CPU's stack.  By doing this, we can
 341          * use the same resume code for both master and slave.  We need to
 342          * make our stack look like a `struct regs' before we jump into the
 343          * common save routine.
 344          */
 345 
 346         pushl   %cs
 347         pushfl
 348         pushl   $-1             /* A phony trap error code */
 349         pushl   $-1             /* A phony trap number */
 350         pushal
 351         pushl   %ds
 352         pushl   %es
 353         pushl   %fs
 354         pushl   %gs
 355         pushl   %ss


 469         movl    %eax, KRS_DROFF(0)(%edi)
 470 
 471         pushl   $1
 472         call    kdi_dreg_get
 473         addl    $4, %esp
 474         movl    %eax, KRS_DROFF(1)(%edi)
 475 
 476         pushl   $2
 477         call    kdi_dreg_get
 478         addl    $4, %esp
 479         movl    %eax, KRS_DROFF(2)(%edi)
 480 
 481         pushl   $3
 482         call    kdi_dreg_get
 483         addl    $4, %esp
 484         movl    %eax, KRS_DROFF(3)(%edi)
 485 
 486         movl    %edi, %eax
 487         popl    %edi
 488 






























 489         clr     %ebp            /* stack traces should end here */
 490 
 491         pushl   %eax
 492         call    kdi_debugger_entry
 493         popl    %eax
 494 
 495         jmp     kdi_resume
 496 
 497         SET_SIZE(kdi_save_common_state)
 498 
 499 #endif  /* !__lint */
 500 
 501 /*
 502  * Resume the world.  The code that calls kdi_resume has already
 503  * decided whether or not to restore the IDT.
 504  */
 505 #if defined(__lint)
 506 void
 507 kdi_resume(void)
 508 {