1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   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  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
  23  * Use is subject to license terms.
  24  */
  25 
  26 #pragma ident   "%Z%%M% %I%     %E% SMI"
  27 
  28 /*
  29  * Intel-specific portions of the DPI
  30  */
  31 
  32 #include <sys/types.h>
  33 #include <sys/trap.h>
  34 
  35 #include <kmdb/kmdb_dpi_impl.h>
  36 #include <kmdb/kmdb_fault.h>
  37 #include <kmdb/kmdb_kdi.h>
  38 #include <mdb/mdb_err.h>
  39 #include <mdb/mdb_debug.h>
  40 #include <mdb/mdb_kreg.h>
  41 #include <mdb/mdb.h>
  42 
  43 void
  44 kmdb_dpi_handle_fault(kreg_t trapno, kreg_t pc, kreg_t sp, int cpuid)
  45 {
  46         kmdb_kdi_system_claim();
  47 
  48         mdb_dprintf(MDB_DBG_DPI, "\ndpi_handle_fault: trapno %u, pc 0x%0?p, "
  49             "sp 0x%0?p\n", (int)trapno, pc, sp);
  50 
  51         switch (trapno) {
  52         case T_GPFLT:
  53                 errno = EACCES;
  54         default:
  55                 errno = EMDB_NOMAP;
  56         }
  57 
  58         if (kmdb_dpi_fault_pcb != NULL) {
  59                 longjmp(*kmdb_dpi_fault_pcb, 1);
  60                 /*NOTREACHED*/
  61         }
  62 
  63         /* Debugger fault */
  64         kmdb_fault(trapno, pc, sp, cpuid);
  65 }
  66 
  67 /*ARGSUSED*/
  68 int
  69 kmdb_dpi_get_register(const char *regname, kreg_t *kregp)
  70 {
  71         return (mdb.m_dpi->dpo_get_register(regname, kregp));
  72 }
  73 
  74 /*ARGSUSED*/
  75 int
  76 kmdb_dpi_set_register(const char *regname, kreg_t kreg)
  77 {
  78         return (mdb.m_dpi->dpo_set_register(regname, kreg));
  79 }
  80 
  81 /*
  82  * Continue/resume handling.  If the target calls kmdb_dpi_resume(), it
  83  * expects that the world will be resumed, and that the call will return
  84  * when the world has stopped again.
  85  *
  86  * For support, we have resume_return(), which is called from main() when
  87  * the continuation has completed (when the world has stopped again).
  88  * set_resume_exit() tells where to jump to actually restart the world.
  89  *
  90  * CAUTION: This routine may be called *after* mdb_destroy.
  91  */
  92 void
  93 kmdb_dpi_resume_common(int cmd)
  94 {
  95         kreg_t pc, trapno;
  96 
  97         ASSERT(kmdb_dpi_resume_requested == 0);
  98 
  99         if (setjmp(kmdb_dpi_resume_pcb) == 0) {
 100                 (void) kmdb_dpi_get_register("pc", &pc);
 101                 mdb_dprintf(MDB_DBG_PROC, "Resume requested, pc is %p\n",
 102                     (void *)pc);
 103 
 104                 if (cmd != KMDB_DPI_CMD_RESUME_UNLOAD)
 105                         kmdb_dpi_resume_requested = 1;
 106 
 107                 longjmp(kmdb_dpi_entry_pcb, cmd);
 108                 /*NOTREACHED*/
 109 
 110         } else {
 111                 (void) kmdb_dpi_get_register("pc", &pc);
 112                 (void) kmdb_dpi_get_register("trapno", &trapno);
 113                 mdb_dprintf(MDB_DBG_PROC, "Back from resume, pc: %p, "
 114                     "trapno: %u\n", (void *)pc, (int)trapno);
 115 
 116                 kmdb_dpi_resume_requested = 0;
 117 
 118                 switch (trapno) {
 119                 case T_BPTFLT:
 120                         kmdb_dpi_set_state(DPI_STATE_FAULTED,
 121                             DPI_STATE_WHY_BKPT);
 122                         break;
 123                 case T_DBGENTR:
 124                         kmdb_dpi_set_state(DPI_STATE_STOPPED, 0);
 125                         break;
 126                 default:
 127                         kmdb_dpi_set_state(DPI_STATE_FAULTED,
 128                             DPI_STATE_WHY_TRAP);
 129                         break;
 130                 }
 131         }
 132 
 133         mdb_dprintf(MDB_DBG_PROC, "returning from resume\n");
 134 }
 135 
 136 void
 137 kmdb_dpi_reboot(void)
 138 {
 139         /*
 140          * We're going to skip all of the niceties we employ in resume_common,
 141          * as we don't plan to ever return.
 142          */
 143         longjmp(kmdb_dpi_entry_pcb, KMDB_DPI_CMD_REBOOT);
 144 }
 145 
 146 void
 147 kmdb_dpi_msr_add(const kdi_msr_t *msrs)
 148 {
 149         mdb.m_dpi->dpo_msr_add(msrs);
 150 }
 151 
 152 uint64_t
 153 kmdb_dpi_msr_get(uint_t msr)
 154 {
 155         return (mdb.m_dpi->dpo_msr_get(DPI_MASTER_CPUID, msr));
 156 }
 157 
 158 uint64_t
 159 kmdb_dpi_msr_get_by_cpu(int cpuid, uint_t msr)
 160 {
 161         return (mdb.m_dpi->dpo_msr_get(cpuid, msr));
 162 }