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