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