xref: /netbsd/sys/arch/x68k/x68k/kgdb_glue.c (revision bf9ec67e)
1 /*	$NetBSD: kgdb_glue.c,v 1.3 2001/05/30 15:24:40 lukem Exp $	*/
2 
3 /*
4  * Copyright (c) 1991, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This software was developed by the Computer Systems Engineering group
8  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9  * contributed to Berkeley.
10  *
11  * All advertising materials mentioning features or use of this software
12  * must display the following acknowledgement:
13  *	This product includes software developed by the University of
14  *	California, Lawrence Berkeley Laboratories.
15  *
16  * Redistribution and use in source and binary forms, with or without
17  * modification, are permitted provided that the following conditions
18  * are met:
19  * 1. Redistributions of source code must retain the above copyright
20  *    notice, this list of conditions and the following disclaimer.
21  * 2. Redistributions in binary form must reproduce the above copyright
22  *    notice, this list of conditions and the following disclaimer in the
23  *    documentation and/or other materials provided with the distribution.
24  * 3. All advertising materials mentioning features or use of this software
25  *    must display the following acknowledgement:
26  *	This product includes software developed by the University of
27  *	California, Berkeley and its contributors.
28  * 4. Neither the name of the University nor the names of its contributors
29  *    may be used to endorse or promote products derived from this software
30  *    without specific prior written permission.
31  *
32  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
33  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
36  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42  * SUCH DAMAGE.
43  *
44  *	@(#)kgdb_glue.c	8.2 (Berkeley) 1/12/94
45  */
46 
47 /*
48  * This file must be compiled with gcc -fno-defer-pop.
49  */
50 
51 #include "opt_kgdb.h"
52 
53 #ifdef KGDB
54 
55 #include <sys/param.h>
56 
57 #include <machine/frame.h>
58 #include <machine/reg.h>
59 
60 #ifndef lint
61 static char rcsid[] = "$NetBSD: kgdb_glue.c,v 1.3 2001/05/30 15:24:40 lukem Exp $";
62 #endif
63 
64 #define KGDB_STACKSIZE 0x800
65 #define KGDB_STACKWORDS (KGDB_STACKSIZE / sizeof(u_long))
66 
67 u_long kgdb_stack[KGDB_STACKWORDS];
68 
69 #define getsp(v) asm("movl %%sp, %0" : "=r" (v))
70 #define setsp(v) asm("movl %0, %%sp" :: "r" (v))
71 
72 static inline void
73 copywords(src, dst, nbytes)
74 	register u_long *src, *dst;
75 	register u_int nbytes;
76 {
77 	u_long *limit = src + (nbytes / sizeof(u_long));
78 
79 	do {
80 		*dst++ = *src++;
81 	} while (src < limit);
82 	if (nbytes & 2)
83 		*(u_short *)dst = *(u_short *)src;
84 }
85 
86 kgdb_trap_glue(type, frame)
87 	int type;
88 	struct frame frame;
89 {
90 	u_long osp, nsp;
91 	u_int fsize, s;
92 	extern short exframesize[];
93 
94 	/*
95 	 * After a kernel mode trap, the saved sp doesn't point to the right
96 	 * place.  The correct value is the top of the frame (i.e. before the
97 	 * KGDB trap).
98 	 *
99 	 * XXX this may have to change if we implement an interrupt stack.
100 	 */
101 	fsize = sizeof(frame) - sizeof(frame.F_u) + exframesize[frame.f_format];
102 	frame.f_regs[SP] = (u_long)&frame + fsize;
103 
104 	/*
105 	 * Copy the interrupt context and frame to the new stack.
106 	 * We're throwing away trap()'s frame since we're going to do
107 	 * our own rte.
108 	 */
109 	nsp = (u_long)&kgdb_stack[KGDB_STACKWORDS] -
110 	      roundup(fsize, sizeof(u_long));
111 
112 	copywords((u_long *)&frame, (u_long *)nsp, fsize);
113 
114 	s = splhigh();
115 
116 	getsp(osp);
117 	setsp(nsp);
118 
119 	if (kgdb_trap(type, (struct frame *)nsp) == 0) {
120 		/*
121 		 * Get back on kernel stack.  This thread of control
122 		 * will return back up through trap().  If kgdb_trap()
123 		 * returns 0, it didn't handle the trap at all so
124 		 * the stack is still intact and everything will
125 		 * unwind okay from here up.
126 		 */
127 		setsp(osp);
128 		splx(s);
129 		return 0;
130 	}
131 	/*
132 	 * Copy back context, which has possibly changed.  Even the
133 	 * sp might have changed.
134 	 */
135 	osp = ((struct frame *)nsp)->f_regs[SP] - fsize;
136 	copywords((u_long *)nsp, (u_long *)osp, fsize);
137 	setsp(osp);
138 
139 	/*
140 	 * Restore the possible new context from frame, pop the
141 	 * unneeded usp (we trapped from kernel mode) and pad word,
142 	 * and return to the trapped thread.
143 	 */
144 	asm("moveml %sp@+,#0x7FFF; addql #8,sp; rte");
145 }
146 
147 int kgdb_testval;
148 
149 kgdb_test(i)
150 	int i;
151 {
152         ++kgdb_testval;
153         return (i + 1);
154 }
155 #endif /* KGDB */
156