xref: /freebsd/sys/arm64/arm64/gdb_machdep.c (revision f242c47ee278e8e119e98b8f4f00e52fbd0d4efd)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2020 The FreeBSD Foundation
5  *
6  * This software was developed by Mitchell Horne under sponsorship from
7  * the FreeBSD Foundation.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kdb.h>
34 #include <sys/kernel.h>
35 #include <sys/proc.h>
36 #include <sys/signal.h>
37 
38 #include <machine/armreg.h>
39 #include <machine/frame.h>
40 #include <machine/gdb_machdep.h>
41 #include <machine/pcb.h>
42 
43 #include <gdb/gdb.h>
44 #include <gdb/gdb_int.h>
45 
46 void *
47 gdb_cpu_getreg(int regnum, size_t *regsz)
48 {
49 
50 	*regsz = gdb_cpu_regsz(regnum);
51 
52 	if (kdb_thread == curthread) {
53 		switch (regnum) {
54 		case GDB_REG_LR:   return (&kdb_frame->tf_lr);
55 		case GDB_REG_SP:   return (&kdb_frame->tf_sp);
56 		case GDB_REG_PC:   return (&kdb_frame->tf_elr);
57 		case GDB_REG_CSPR: return (&kdb_frame->tf_spsr);
58 		default:
59 			if (regnum >= GDB_REG_X0 && regnum <= GDB_REG_X29)
60 				return (&kdb_frame->tf_x[regnum - GDB_REG_X0]);
61 			break;
62 		}
63 	}
64 	switch (regnum) {
65 	case GDB_REG_SP: return (&kdb_thrctx->pcb_sp);
66 	case GDB_REG_PC: /* FALLTHROUGH */
67 	case GDB_REG_LR: return (&kdb_thrctx->pcb_x[PCB_LR]);
68 	default:
69 		if (regnum >= GDB_REG_X19 && regnum <= GDB_REG_X29)
70 			return (&kdb_thrctx->pcb_x[regnum - GDB_REG_X19]);
71 		break;
72 	}
73 
74 	return (NULL);
75 }
76 
77 void
78 gdb_cpu_setreg(int regnum, void *val)
79 {
80 	register_t regval = *(register_t *)val;
81 
82 	/* For curthread, keep the pcb and trapframe in sync. */
83 	if (kdb_thread == curthread) {
84 		switch (regnum) {
85 		case GDB_REG_PC: kdb_frame->tf_elr = regval; break;
86 		case GDB_REG_SP: kdb_frame->tf_sp  = regval; break;
87 		default:
88 			if (regnum >= GDB_REG_X0 && regnum <= GDB_REG_X29) {
89 				kdb_frame->tf_x[regnum] = regval;
90 			}
91 			break;
92 		}
93 	}
94 	switch (regnum) {
95 	case GDB_REG_PC: /* FALLTHROUGH */
96 	case GDB_REG_LR: kdb_thrctx->pcb_x[PCB_LR] = regval; break;
97 	case GDB_REG_SP: kdb_thrctx->pcb_sp = regval; break;
98 	default:
99 		if (regnum >= GDB_REG_X19 && regnum <= GDB_REG_X29) {
100 			kdb_thrctx->pcb_x[regnum - GDB_REG_X19] = regval;
101 		}
102 		break;
103 	}
104 }
105 
106 int
107 gdb_cpu_signal(int type, int code __unused)
108 {
109 
110 	switch (type) {
111 	case EXCP_WATCHPT_EL1:
112 	case EXCP_SOFTSTP_EL1:
113 	case EXCP_BRKPT_EL1:
114 	case EXCP_BRK:
115 		return (SIGTRAP);
116 	}
117 	return (SIGEMT);
118 }
119 
120 void
121 gdb_cpu_stop_reason(int type, int code __unused)
122 {
123 
124 	switch (type) {
125 	case EXCP_WATCHPT_EL1:
126 		gdb_tx_str("watch:");
127 		gdb_tx_varhex((uintmax_t)READ_SPECIALREG(far_el1));
128 		gdb_tx_char(';');
129 		break;
130 	case EXCP_BRKPT_EL1:
131 		gdb_tx_str("hwbreak:;");
132 		break;
133 	default:
134 		break;
135 	}
136 }
137