1178172Simp/*	$NetBSD: kgdb_machdep.c,v 1.11 2005/12/24 22:45:35 perry Exp $	*/
2178172Simp
3178172Simp/*-
4178172Simp * Copyright (c) 2004 Marcel Moolenaar
5178172Simp * All rights reserved.
6178172Simp *
7178172Simp * Redistribution and use in source and binary forms, with or without
8178172Simp * modification, are permitted provided that the following conditions
9178172Simp * are met:
10178172Simp *
11178172Simp * 1. Redistributions of source code must retain the above copyright
12178172Simp *    notice, this list of conditions and the following disclaimer.
13178172Simp * 2. Redistributions in binary form must reproduce the above copyright
14178172Simp *    notice, this list of conditions and the following disclaimer in the
15178172Simp *    documentation and/or other materials provided with the distribution.
16178172Simp *
17178172Simp * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
18178172Simp * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19178172Simp * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20178172Simp * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21178172Simp * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22178172Simp * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23178172Simp * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24178172Simp * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25178172Simp * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26178172Simp * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27178172Simp */
28178172Simp/*-
29178172Simp * Copyright (c) 1997 The NetBSD Foundation, Inc.
30178172Simp * All rights reserved.
31178172Simp *
32178172Simp * This code is derived from software contributed to The NetBSD Foundation
33178172Simp * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
34178172Simp * NASA Ames Research Center.
35178172Simp *
36178172Simp * Redistribution and use in source and binary forms, with or without
37178172Simp * modification, are permitted provided that the following conditions
38178172Simp * are met:
39178172Simp * 1. Redistributions of source code must retain the above copyright
40178172Simp *    notice, this list of conditions and the following disclaimer.
41178172Simp * 2. Redistributions in binary form must reproduce the above copyright
42178172Simp *    notice, this list of conditions and the following disclaimer in the
43178172Simp *    documentation and/or other materials provided with the distribution.
44178172Simp *
45178172Simp * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
46178172Simp * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
47178172Simp * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
48178172Simp * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
49178172Simp * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
50178172Simp * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
51178172Simp * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
52178172Simp * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
53178172Simp * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
54178172Simp * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
55178172Simp * POSSIBILITY OF SUCH DAMAGE.
56178172Simp */
57178172Simp
58178172Simp/*
59178172Simp * Copyright (c) 1996 Matthias Pfaller.
60178172Simp * All rights reserved.
61178172Simp *
62178172Simp * Redistribution and use in source and binary forms, with or without
63178172Simp * modification, are permitted provided that the following conditions
64178172Simp * are met:
65178172Simp * 1. Redistributions of source code must retain the above copyright
66178172Simp *    notice, this list of conditions and the following disclaimer.
67178172Simp * 2. Redistributions in binary form must reproduce the above copyright
68178172Simp *    notice, this list of conditions and the following disclaimer in the
69178172Simp *    documentation and/or other materials provided with the distribution.
70178172Simp * 3. All advertising materials mentioning features or use of this software
71178172Simp *    must display the following acknowledgement:
72178172Simp *	This product includes software developed by Matthias Pfaller.
73178172Simp * 4. The name of the author may not be used to endorse or promote products
74178172Simp *    derived from this software without specific prior written permission
75178172Simp *
76178172Simp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
77178172Simp * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
78178172Simp * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
79178172Simp * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
80178172Simp * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
81178172Simp * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
82178172Simp * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
83178172Simp * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
84178172Simp * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
85178172Simp * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
86178172Simp *
87178172Simp *	JNPR: gdb_machdep.c,v 1.1 2007/08/09 12:25:25 katta
88178172Simp * $FreeBSD$
89178172Simp */
90178172Simp
91178172Simp#include <sys/cdefs.h>
92178172Simp
93178172Simp#include <sys/param.h>
94178172Simp#include <sys/systm.h>
95178172Simp#include <sys/kdb.h>
96178172Simp#include <sys/kernel.h>
97178172Simp#include <sys/signal.h>
98178172Simp#include <sys/pcpu.h>
99178172Simp
100178172Simp#include <machine/gdb_machdep.h>
101178172Simp#include <machine/pcb.h>
102178172Simp#include <machine/reg.h>
103178172Simp#include <machine/trap.h>
104178172Simp
105178172Simp#include <gdb/gdb.h>
106178172Simp
107178172Simpvoid *
108178172Simpgdb_cpu_getreg(int regnum, size_t *regsz)
109178172Simp{
110178172Simp
111178172Simp 	*regsz = gdb_cpu_regsz(regnum);
112228962Sjhb 	if (kdb_thread == curthread) {
113214965Sgonzo		register_t *zero_ptr = &kdb_frame->zero;
114214965Sgonzo		return zero_ptr + regnum;
115178172Simp	}
116214965Sgonzo
117178172Simp	switch (regnum) {
118214965Sgonzo	/*
119214965Sgonzo	 * S0..S7
120214965Sgonzo	 */
121214965Sgonzo	case 16:
122214965Sgonzo	case 17:
123214965Sgonzo	case 18:
124214965Sgonzo	case 19:
125214965Sgonzo	case 20:
126214965Sgonzo	case 21:
127214965Sgonzo	case 22:
128214965Sgonzo	case 23:
129214965Sgonzo 		return (&kdb_thrctx->pcb_context[PCB_REG_S0 + regnum - 16]);
130214965Sgonzo	case 28:
131214965Sgonzo		return (&kdb_thrctx->pcb_context[PCB_REG_GP]);
132214965Sgonzo	case 29:
133214965Sgonzo		return (&kdb_thrctx->pcb_context[PCB_REG_SP]);
134214965Sgonzo	case 30:
135214965Sgonzo		return (&kdb_thrctx->pcb_context[PCB_REG_S8]);
136214965Sgonzo	case 31:
137214965Sgonzo		return (&kdb_thrctx->pcb_context[PCB_REG_RA]);
138214965Sgonzo	case 37:
139214965Sgonzo		return (&kdb_thrctx->pcb_context[PCB_REG_PC]);
140178172Simp	}
141178172Simp	return (NULL);
142178172Simp}
143178172Simp
144178172Simpvoid
145178172Simpgdb_cpu_setreg(int regnum, void *val)
146178172Simp{
147178172Simp	switch (regnum) {
148178172Simp	case GDB_REG_PC:
149202046Simp		kdb_thrctx->pcb_context[10] = *(register_t *)val;
150228962Sjhb		if (kdb_thread == curthread)
151178172Simp			kdb_frame->pc = *(register_t *)val;
152178172Simp	}
153178172Simp}
154178172Simp
155178172Simpint
156178172Simpgdb_cpu_signal(int entry, int code)
157178172Simp{
158178172Simp	switch (entry) {
159178172Simp	case T_TLB_MOD:
160178172Simp	case T_TLB_MOD+T_USER:
161178172Simp	case T_TLB_LD_MISS:
162178172Simp	case T_TLB_ST_MISS:
163178172Simp	case T_TLB_LD_MISS+T_USER:
164178172Simp	case T_TLB_ST_MISS+T_USER:
165178172Simp	case T_ADDR_ERR_LD:		/* misaligned access */
166178172Simp	case T_ADDR_ERR_ST:		/* misaligned access */
167178172Simp	case T_BUS_ERR_LD_ST:		/* BERR asserted to CPU */
168178172Simp	case T_ADDR_ERR_LD+T_USER:	/* misaligned or kseg access */
169178172Simp	case T_ADDR_ERR_ST+T_USER:	/* misaligned or kseg access */
170178172Simp	case T_BUS_ERR_IFETCH+T_USER:	/* BERR asserted to CPU */
171178172Simp	case T_BUS_ERR_LD_ST+T_USER:	/* BERR asserted to CPU */
172178172Simp		return (SIGSEGV);
173178172Simp
174178172Simp	case T_BREAK:
175178172Simp	case T_BREAK+T_USER:
176178172Simp		return (SIGTRAP);
177178172Simp
178178172Simp	case T_RES_INST+T_USER:
179178172Simp	case T_COP_UNUSABLE+T_USER:
180178172Simp		return (SIGILL);
181178172Simp
182178172Simp	case T_FPE+T_USER:
183178172Simp	case T_OVFLOW+T_USER:
184178172Simp		return (SIGFPE);
185178172Simp
186178172Simp	default:
187178172Simp		return (SIGEMT);
188178172Simp	}
189178172Simp}
190