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 * Copyright 2018 Joyent, Inc.
26 */
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
43void
44kmdb_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		break;
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*/
69int
70kmdb_dpi_get_register(const char *regname, kreg_t *kregp)
71{
72	return (mdb.m_dpi->dpo_get_register(regname, kregp));
73}
74
75/*ARGSUSED*/
76int
77kmdb_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 */
93void
94kmdb_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
137void
138kmdb_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