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