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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2006 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  * Intel-specific portions of the DPI
31  */
32 
33 #include <sys/types.h>
34 #include <sys/trap.h>
35 
36 #include <kmdb/kmdb_dpi_impl.h>
37 #include <kmdb/kmdb_fault.h>
38 #include <kmdb/kmdb_kdi.h>
39 #include <mdb/mdb_err.h>
40 #include <mdb/mdb_debug.h>
41 #include <mdb/mdb_kreg.h>
42 #include <mdb/mdb.h>
43 
44 void
45 kmdb_dpi_handle_fault(kreg_t trapno, kreg_t pc, kreg_t sp, int cpuid)
46 {
47 	kmdb_kdi_system_claim();
48 
49 	mdb_dprintf(MDB_DBG_DPI, "\ndpi_handle_fault: trapno %u, pc 0x%0?p, "
50 	    "sp 0x%0?p\n", (int)trapno, pc, sp);
51 
52 	switch (trapno) {
53 	case T_GPFLT:
54 		errno = EACCES;
55 	default:
56 		errno = EMDB_NOMAP;
57 	}
58 
59 	if (kmdb_dpi_fault_pcb != NULL) {
60 		longjmp(*kmdb_dpi_fault_pcb, 1);
61 		/*NOTREACHED*/
62 	}
63 
64 	/* Debugger fault */
65 	kmdb_fault(trapno, pc, sp, cpuid);
66 }
67 
68 /*ARGSUSED*/
69 int
70 kmdb_dpi_get_register(const char *regname, kreg_t *kregp)
71 {
72 	return (mdb.m_dpi->dpo_get_register(regname, kregp));
73 }
74 
75 /*ARGSUSED*/
76 int
77 kmdb_dpi_set_register(const char *regname, kreg_t kreg)
78 {
79 	return (mdb.m_dpi->dpo_set_register(regname, kreg));
80 }
81 
82 /*
83  * Continue/resume handling.  If the target calls kmdb_dpi_resume(), it
84  * expects that the world will be resumed, and that the call will return
85  * when the world has stopped again.
86  *
87  * For support, we have resume_return(), which is called from main() when
88  * the continuation has completed (when the world has stopped again).
89  * set_resume_exit() tells where to jump to actually restart the world.
90  *
91  * CAUTION: This routine may be called *after* mdb_destroy.
92  */
93 void
94 kmdb_dpi_resume_common(int cmd)
95 {
96 	kreg_t pc, trapno;
97 
98 	ASSERT(kmdb_dpi_resume_requested == 0);
99 
100 	if (setjmp(kmdb_dpi_resume_pcb) == 0) {
101 		(void) kmdb_dpi_get_register("pc", &pc);
102 		mdb_dprintf(MDB_DBG_PROC, "Resume requested, pc is %p\n",
103 		    (void *)pc);
104 
105 		if (cmd != KMDB_DPI_CMD_RESUME_UNLOAD)
106 			kmdb_dpi_resume_requested = 1;
107 
108 		longjmp(kmdb_dpi_entry_pcb, cmd);
109 		/*NOTREACHED*/
110 
111 	} else {
112 		(void) kmdb_dpi_get_register("pc", &pc);
113 		(void) kmdb_dpi_get_register("trapno", &trapno);
114 		mdb_dprintf(MDB_DBG_PROC, "Back from resume, pc: %p, "
115 		    "trapno: %u\n", (void *)pc, (int)trapno);
116 
117 		kmdb_dpi_resume_requested = 0;
118 
119 		switch (trapno) {
120 		case T_BPTFLT:
121 			kmdb_dpi_set_state(DPI_STATE_FAULTED,
122 			    DPI_STATE_WHY_BKPT);
123 			break;
124 		case T_DBGENTR:
125 			kmdb_dpi_set_state(DPI_STATE_STOPPED, 0);
126 			break;
127 		default:
128 			kmdb_dpi_set_state(DPI_STATE_FAULTED,
129 			    DPI_STATE_WHY_TRAP);
130 			break;
131 		}
132 	}
133 
134 	mdb_dprintf(MDB_DBG_PROC, "returning from resume\n");
135 }
136 
137 void
138 kmdb_dpi_reboot(void)
139 {
140 	/*
141 	 * We're going to skip all of the niceties we employ in resume_common,
142 	 * as we don't plan to ever return.
143 	 */
144 	longjmp(kmdb_dpi_entry_pcb, KMDB_DPI_CMD_REBOOT);
145 }
146 
147 void
148 kmdb_dpi_msr_add(const kmdb_msr_t *msrs)
149 {
150 	mdb.m_dpi->dpo_msr_add(msrs);
151 }
152 
153 uint64_t
154 kmdb_dpi_msr_get(uint_t msr)
155 {
156 	return (mdb.m_dpi->dpo_msr_get(DPI_MASTER_CPUID, msr));
157 }
158 
159 uint64_t
160 kmdb_dpi_msr_get_by_cpu(int cpuid, uint_t msr)
161 {
162 	return (mdb.m_dpi->dpo_msr_get(cpuid, msr));
163 }
164