xref: /freebsd/sys/arm/arm/gdb_machdep.c (revision 4b9d6057)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2006 Olivier Houchard
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kdb.h>
32 #include <sys/kernel.h>
33 #include <sys/proc.h>
34 #include <sys/signal.h>
35 
36 #include <machine/gdb_machdep.h>
37 #include <machine/db_machdep.h>
38 #include <machine/vmparam.h>
39 #include <machine/pcb.h>
40 #include <machine/trap.h>
41 #include <machine/frame.h>
42 #include <machine/endian.h>
43 
44 #include <gdb/gdb.h>
45 
46 static register_t stacktest;
47 
48 void *
49 gdb_cpu_getreg(int regnum, size_t *regsz)
50 {
51 
52 	*regsz = gdb_cpu_regsz(regnum);
53 
54 	if (kdb_thread == curthread) {
55 		if (regnum < 13)
56 			return (&kdb_frame->tf_r0 + regnum);
57 		if (regnum == 13)
58 			return (&kdb_frame->tf_svc_sp);
59 		if (regnum == 14)
60 			return (&kdb_frame->tf_svc_lr);
61 		if (regnum == 15)
62 			return (&kdb_frame->tf_pc);
63 		if (regnum == 25)
64 			return (&kdb_frame->tf_spsr);
65 	}
66 
67 	switch (regnum) {
68 	case 4:  return (&kdb_thrctx->pcb_regs.sf_r4);
69 	case 5:  return (&kdb_thrctx->pcb_regs.sf_r5);
70 	case 6:  return (&kdb_thrctx->pcb_regs.sf_r6);
71 	case 7:  return (&kdb_thrctx->pcb_regs.sf_r7);
72 	case 8:  return (&kdb_thrctx->pcb_regs.sf_r8);
73 	case 9:  return (&kdb_thrctx->pcb_regs.sf_r9);
74 	case 10:  return (&kdb_thrctx->pcb_regs.sf_r10);
75 	case 11:  return (&kdb_thrctx->pcb_regs.sf_r11);
76 	case 12:  return (&kdb_thrctx->pcb_regs.sf_r12);
77 	case 13:  stacktest = kdb_thrctx->pcb_regs.sf_sp + 5 * 4;
78 		  return (&stacktest);
79 	case 15:
80 		  /*
81 		   * On context switch, the PC is not put in the PCB, but
82 		   * we can retrieve it from the stack.
83 		   */
84 		  if (kdb_thrctx->pcb_regs.sf_sp > KERNBASE) {
85 			  kdb_thrctx->pcb_regs.sf_pc = *(register_t *)
86 			      (kdb_thrctx->pcb_regs.sf_sp + 4 * 4);
87 			  return (&kdb_thrctx->pcb_regs.sf_pc);
88 		  }
89 	}
90 
91 	return (NULL);
92 }
93 
94 void
95 gdb_cpu_setreg(int regnum, void *val)
96 {
97 	if (kdb_thread != curthread)
98 		return;
99 
100 	switch (regnum) {
101 	case GDB_REG_PC:
102 		kdb_frame->tf_pc = *(register_t *)val;
103 		break;
104 	case GDB_REG_SP:
105 		kdb_frame->tf_svc_sp = *(register_t *)val;
106 		break;
107 	case GDB_REG_LR:
108 		kdb_frame->tf_svc_lr = *(register_t *)val;
109 		break;
110 	default:
111 		/* Write to the general purpose registers r0-r12. */
112 		if (regnum >= 0 && regnum <= 12) {
113 			*(&kdb_frame->tf_r0 + regnum) = *(register_t *)val;
114 		}
115 		break;
116 	}
117 }
118 
119 int
120 gdb_cpu_signal(int type, int code)
121 {
122 
123 	switch (type) {
124 	case T_BREAKPOINT: return (SIGTRAP);
125 	}
126 	return (SIGEMT);
127 }
128