kgdb_glue.c revision 1.2 1 /* $NetBSD: kgdb_glue.c,v 1.2 2001/03/22 12:46:20 minoura 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 #ifdef KGDB
52
53 #include <sys/param.h>
54
55 #include <machine/frame.h>
56 #include <machine/reg.h>
57
58 #ifndef lint
59 static char rcsid[] = "$NetBSD: kgdb_glue.c,v 1.2 2001/03/22 12:46:20 minoura Exp $";
60 #endif
61
62 #define KGDB_STACKSIZE 0x800
63 #define KGDB_STACKWORDS (KGDB_STACKSIZE / sizeof(u_long))
64
65 u_long kgdb_stack[KGDB_STACKWORDS];
66
67 #define getsp(v) asm("movl %%sp, %0" : "=r" (v))
68 #define setsp(v) asm("movl %0, %%sp" :: "r" (v))
69
70 static inline void
71 copywords(src, dst, nbytes)
72 register u_long *src, *dst;
73 register u_int nbytes;
74 {
75 u_long *limit = src + (nbytes / sizeof(u_long));
76
77 do {
78 *dst++ = *src++;
79 } while (src < limit);
80 if (nbytes & 2)
81 *(u_short *)dst = *(u_short *)src;
82 }
83
84 kgdb_trap_glue(type, frame)
85 int type;
86 struct frame frame;
87 {
88 u_long osp, nsp;
89 u_int fsize, s;
90 extern short exframesize[];
91
92 /*
93 * After a kernel mode trap, the saved sp doesn't point to the right
94 * place. The correct value is the top of the frame (i.e. before the
95 * KGDB trap).
96 *
97 * XXX this may have to change if we implement an interrupt stack.
98 */
99 fsize = sizeof(frame) - sizeof(frame.F_u) + exframesize[frame.f_format];
100 frame.f_regs[SP] = (u_long)&frame + fsize;
101
102 /*
103 * Copy the interrupt context and frame to the new stack.
104 * We're throwing away trap()'s frame since we're going to do
105 * our own rte.
106 */
107 nsp = (u_long)&kgdb_stack[KGDB_STACKWORDS] -
108 roundup(fsize, sizeof(u_long));
109
110 copywords((u_long *)&frame, (u_long *)nsp, fsize);
111
112 s = splhigh();
113
114 getsp(osp);
115 setsp(nsp);
116
117 if (kgdb_trap(type, (struct frame *)nsp) == 0) {
118 /*
119 * Get back on kernel stack. This thread of control
120 * will return back up through trap(). If kgdb_trap()
121 * returns 0, it didn't handle the trap at all so
122 * the stack is still intact and everything will
123 * unwind okay from here up.
124 */
125 setsp(osp);
126 splx(s);
127 return 0;
128 }
129 /*
130 * Copy back context, which has possibly changed. Even the
131 * sp might have changed.
132 */
133 osp = ((struct frame *)nsp)->f_regs[SP] - fsize;
134 copywords((u_long *)nsp, (u_long *)osp, fsize);
135 setsp(osp);
136
137 /*
138 * Restore the possible new context from frame, pop the
139 * unneeded usp (we trapped from kernel mode) and pad word,
140 * and return to the trapped thread.
141 */
142 asm("moveml %sp@+,#0x7FFF; addql #8,sp; rte");
143 }
144
145 int kgdb_testval;
146
147 kgdb_test(i)
148 int i;
149 {
150 ++kgdb_testval;
151 return (i + 1);
152 }
153 #endif /* KGDB */
154