1/*	$NetBSD: kgdb_machdep.c,v 1.11 2005/12/24 22:45:35 perry Exp $	*/
2
3/*-
4 * Copyright (c) 2004 Marcel Moolenaar
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 * Copyright (c) 1997 The NetBSD Foundation, Inc.
30 * All rights reserved.
31 *
32 * This code is derived from software contributed to The NetBSD Foundation
33 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
34 * NASA Ames Research Center.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 *    notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 *    notice, this list of conditions and the following disclaimer in the
43 *    documentation and/or other materials provided with the distribution.
44 *
45 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
46 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
47 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
48 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
49 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
50 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
51 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
52 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
53 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
54 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
55 * POSSIBILITY OF SUCH DAMAGE.
56 */
57
58/*
59 * Copyright (c) 1996 Matthias Pfaller.
60 * All rights reserved.
61 *
62 * Redistribution and use in source and binary forms, with or without
63 * modification, are permitted provided that the following conditions
64 * are met:
65 * 1. Redistributions of source code must retain the above copyright
66 *    notice, this list of conditions and the following disclaimer.
67 * 2. Redistributions in binary form must reproduce the above copyright
68 *    notice, this list of conditions and the following disclaimer in the
69 *    documentation and/or other materials provided with the distribution.
70 * 3. All advertising materials mentioning features or use of this software
71 *    must display the following acknowledgement:
72 *	This product includes software developed by Matthias Pfaller.
73 * 4. The name of the author may not be used to endorse or promote products
74 *    derived from this software without specific prior written permission
75 *
76 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
77 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
78 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
79 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
80 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
81 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
82 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
83 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
84 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
85 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
86 *
87 *	JNPR: gdb_machdep.c,v 1.1 2007/08/09 12:25:25 katta
88 * $FreeBSD$
89 */
90
91#include <sys/cdefs.h>
92
93#include <sys/param.h>
94#include <sys/systm.h>
95#include <sys/kdb.h>
96#include <sys/kernel.h>
97#include <sys/signal.h>
98#include <sys/pcpu.h>
99
100#include <machine/gdb_machdep.h>
101#include <machine/pcb.h>
102#include <machine/reg.h>
103#include <machine/trap.h>
104
105#include <gdb/gdb.h>
106
107void *
108gdb_cpu_getreg(int regnum, size_t *regsz)
109{
110
111 	*regsz = gdb_cpu_regsz(regnum);
112 	if (kdb_thread == curthread) {
113		register_t *zero_ptr = &kdb_frame->zero;
114		return zero_ptr + regnum;
115	}
116
117	switch (regnum) {
118	/*
119	 * S0..S7
120	 */
121	case 16:
122	case 17:
123	case 18:
124	case 19:
125	case 20:
126	case 21:
127	case 22:
128	case 23:
129 		return (&kdb_thrctx->pcb_context[PCB_REG_S0 + regnum - 16]);
130	case 28:
131		return (&kdb_thrctx->pcb_context[PCB_REG_GP]);
132	case 29:
133		return (&kdb_thrctx->pcb_context[PCB_REG_SP]);
134	case 30:
135		return (&kdb_thrctx->pcb_context[PCB_REG_S8]);
136	case 31:
137		return (&kdb_thrctx->pcb_context[PCB_REG_RA]);
138	case 37:
139		return (&kdb_thrctx->pcb_context[PCB_REG_PC]);
140	}
141	return (NULL);
142}
143
144void
145gdb_cpu_setreg(int regnum, void *val)
146{
147	switch (regnum) {
148	case GDB_REG_PC:
149		kdb_thrctx->pcb_context[10] = *(register_t *)val;
150		if (kdb_thread == curthread)
151			kdb_frame->pc = *(register_t *)val;
152	}
153}
154
155int
156gdb_cpu_signal(int entry, int code)
157{
158	switch (entry) {
159	case T_TLB_MOD:
160	case T_TLB_MOD+T_USER:
161	case T_TLB_LD_MISS:
162	case T_TLB_ST_MISS:
163	case T_TLB_LD_MISS+T_USER:
164	case T_TLB_ST_MISS+T_USER:
165	case T_ADDR_ERR_LD:		/* misaligned access */
166	case T_ADDR_ERR_ST:		/* misaligned access */
167	case T_BUS_ERR_LD_ST:		/* BERR asserted to CPU */
168	case T_ADDR_ERR_LD+T_USER:	/* misaligned or kseg access */
169	case T_ADDR_ERR_ST+T_USER:	/* misaligned or kseg access */
170	case T_BUS_ERR_IFETCH+T_USER:	/* BERR asserted to CPU */
171	case T_BUS_ERR_LD_ST+T_USER:	/* BERR asserted to CPU */
172		return (SIGSEGV);
173
174	case T_BREAK:
175	case T_BREAK+T_USER:
176		return (SIGTRAP);
177
178	case T_RES_INST+T_USER:
179	case T_COP_UNUSABLE+T_USER:
180		return (SIGILL);
181
182	case T_FPE+T_USER:
183	case T_OVFLOW+T_USER:
184		return (SIGFPE);
185
186	default:
187		return (SIGEMT);
188	}
189}
190