Home | History | Annotate | Line # | Download | only in sh3
kgdb_machdep.c revision 1.5
      1 /*	$NetBSD: kgdb_machdep.c,v 1.5 2002/02/19 17:22:34 uch Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * Copyright (c) 1996 Matthias Pfaller.
     42  * All rights reserved.
     43  *
     44  * Redistribution and use in source and binary forms, with or without
     45  * modification, are permitted provided that the following conditions
     46  * are met:
     47  * 1. Redistributions of source code must retain the above copyright
     48  *    notice, this list of conditions and the following disclaimer.
     49  * 2. Redistributions in binary form must reproduce the above copyright
     50  *    notice, this list of conditions and the following disclaimer in the
     51  *    documentation and/or other materials provided with the distribution.
     52  * 3. All advertising materials mentioning features or use of this software
     53  *    must display the following acknowledgement:
     54  *	This product includes software developed by Matthias Pfaller.
     55  * 4. The name of the author may not be used to endorse or promote products
     56  *    derived from this software without specific prior written permission
     57  *
     58  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     59  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     60  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     61  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     62  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     63  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     64  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     65  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     66  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     67  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     68  */
     69 
     70 #include "opt_ddb.h"
     71 #include "opt_kgdb.h"
     72 
     73 #if defined(DDB)
     74 #error "Can't build DDB and KGDB together."
     75 #endif
     76 
     77 /*
     78  * Machine-dependent functions for remote KGDB.  Originally written
     79  * for NetBSD/pc532 by Matthias Pfaller.  Modified for NetBSD/i386
     80  * by Jason R. Thorpe.
     81  */
     82 
     83 #include <sys/param.h>
     84 #include <sys/kgdb.h>
     85 #include <sys/systm.h>
     86 
     87 #include <uvm/uvm_extern.h>
     88 
     89 #include <machine/pte.h>
     90 #include <machine/reg.h>
     91 #include <machine/trap.h>
     92 
     93 /* XXX Should be in <machine/pmap.h> */
     94 pt_entry_t *pmap_pte(pmap_t, vaddr_t);
     95 
     96 /*
     97  * Determine if the memory at va..(va+len) is valid.
     98  */
     99 int
    100 kgdb_acc(vaddr_t va, size_t len)
    101 {
    102 	vaddr_t last_va;
    103 	pt_entry_t pte;
    104 
    105 	last_va = va + len;
    106 	va  &= ~PGOFSET;
    107 	last_va &= ~PGOFSET;
    108 
    109 	do {
    110 		pte = *(pt_entry_t *)pmap_pte(pmap_kernel(), va);
    111 		if ((pte & PG_V) == 0)
    112 			return (0);
    113 		va  += NBPG;
    114 	} while (va < last_va);
    115 
    116 	return (1);
    117 }
    118 
    119 /*
    120  * Translate a trap number into a unix compatible signal value.
    121  * (gdb only understands unix signal numbers).
    122  */
    123 int
    124 kgdb_signal(int type)
    125 {
    126 	switch (type) {
    127 	case T_NMI:
    128 		return (SIGINT);
    129 
    130 	case T_ALIGNFLT:
    131 		return (SIGILL);
    132 
    133 	case T_BPTFLT:
    134 	case T_TRCTRAP:
    135 		return (SIGTRAP);
    136 
    137 	case T_ASTFLT:
    138 	case T_DOUBLEFLT:
    139 		return (SIGEMT);
    140 
    141 	case T_ARITHTRAP:
    142 	case T_DIVIDE:
    143 	case T_OFLOW:
    144 	case T_DNA:
    145 	case T_FPOPFLT:
    146 		return (SIGFPE);
    147 
    148 	case T_PRIVINFLT:
    149 	case T_PROTFLT:
    150 	case T_PAGEFLT:
    151 	case T_TSSFLT:
    152 	case T_SEGNPFLT:
    153 	case T_STKFLT:
    154 		return (SIGSEGV);
    155 
    156 	case T_BOUND:
    157 		return (SIGURG);
    158 
    159 	default:
    160 		return (SIGEMT);
    161 	}
    162 }
    163 
    164 /*
    165  * Translate the values stored in the kernel regs struct to the format
    166  * understood by gdb.
    167  */
    168 void
    169 kgdb_getregs(db_regs_t *regs, kgdb_reg_t *gdb_regs)
    170 {
    171 
    172 	gdb_regs[ 0] = regs->tf_eax;
    173 	gdb_regs[ 1] = regs->tf_ecx;
    174 	gdb_regs[ 2] = regs->tf_edx;
    175 	gdb_regs[ 3] = regs->tf_ebx;
    176 	gdb_regs[ 5] = regs->tf_ebp;
    177 	gdb_regs[ 6] = regs->tf_esi;
    178 	gdb_regs[ 7] = regs->tf_edi;
    179 	gdb_regs[ 8] = regs->tf_eip;
    180 	gdb_regs[ 9] = regs->tf_eflags;
    181 	gdb_regs[10] = regs->tf_cs;
    182 	gdb_regs[12] = regs->tf_ds;
    183 	gdb_regs[13] = regs->tf_es;
    184 
    185 	if (KERNELMODE(regs->tf_cs, regs->tf_eflags)) {
    186 		/*
    187 		 * Kernel mode - esp and ss not saved.
    188 		 */
    189 		gdb_regs[ 4] = (kgdb_reg_t)&regs->tf_esp; /* kernel stack
    190 							     pointer */
    191 		__asm __volatile("movw %%ss,%w0" : "=r" (gdb_regs[11]));
    192 	}
    193 }
    194 
    195 /*
    196  * Reverse the above.
    197  */
    198 void
    199 kgdb_setregs(db_regs_t *regs, kgdb_reg_t *gdb_regs)
    200 {
    201 
    202 	regs->tf_eax    = gdb_regs[ 0];
    203 	regs->tf_ecx    = gdb_regs[ 1];
    204 	regs->tf_edx    = gdb_regs[ 2];
    205 	regs->tf_ebx    = gdb_regs[ 3];
    206 	regs->tf_ebp    = gdb_regs[ 5];
    207 	regs->tf_esi    = gdb_regs[ 6];
    208 	regs->tf_edi    = gdb_regs[ 7];
    209 	regs->tf_eip    = gdb_regs[ 8];
    210 	regs->tf_eflags = gdb_regs[ 9];
    211 	regs->tf_cs     = gdb_regs[10];
    212 	regs->tf_ds     = gdb_regs[12];
    213 	regs->tf_es     = gdb_regs[13];
    214 
    215 	if (KERNELMODE(regs->tf_cs, regs->tf_eflags) == 0) {
    216 		/*
    217 		 * Trapped in user mode - restore esp and ss.
    218 		 */
    219 		regs->tf_esp = gdb_regs[ 4];
    220 		regs->tf_ss  = gdb_regs[11];
    221 	}
    222 }
    223 
    224 /*
    225  * Trap into kgdb to wait for debugger to connect,
    226  * noting on the console why nothing else is going on.
    227  */
    228 void
    229 kgdb_connect(int verbose)
    230 {
    231 
    232 	if (kgdb_dev < 0)
    233 		return;
    234 
    235 	if (verbose)
    236 		printf("kgdb waiting...");
    237 
    238 	__asm__ __volatile__("trapa #0xc3");
    239 
    240 	if (verbose)
    241 		printf("connected.\n");
    242 
    243 	kgdb_debug_panic = 1;
    244 }
    245 
    246 /*
    247  * Decide what to do on panic.
    248  * (This is called by panic, like Debugger())
    249  */
    250 void
    251 kgdb_panic()
    252 {
    253 	if (kgdb_dev >= 0 && kgdb_debug_panic) {
    254 		printf("entering kgdb\n");
    255 		kgdb_connect(kgdb_active == 0);
    256 	}
    257 }
    258