Home | History | Annotate | Line # | Download | only in sh3
kgdb_machdep.c revision 1.7
      1  1.7     uch /*	$NetBSD: kgdb_machdep.c,v 1.7 2002/04/28 17:10:38 uch Exp $	*/
      2  1.1  itojun 
      3  1.1  itojun /*-
      4  1.6     uch  * Copyright (c) 1997, 2002 The NetBSD Foundation, Inc.
      5  1.1  itojun  * All rights reserved.
      6  1.1  itojun  *
      7  1.1  itojun  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  itojun  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  1.1  itojun  * NASA Ames Research Center.
     10  1.1  itojun  *
     11  1.1  itojun  * Redistribution and use in source and binary forms, with or without
     12  1.1  itojun  * modification, are permitted provided that the following conditions
     13  1.1  itojun  * are met:
     14  1.1  itojun  * 1. Redistributions of source code must retain the above copyright
     15  1.1  itojun  *    notice, this list of conditions and the following disclaimer.
     16  1.1  itojun  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.1  itojun  *    notice, this list of conditions and the following disclaimer in the
     18  1.1  itojun  *    documentation and/or other materials provided with the distribution.
     19  1.1  itojun  * 3. All advertising materials mentioning features or use of this software
     20  1.1  itojun  *    must display the following acknowledgement:
     21  1.1  itojun  *	This product includes software developed by the NetBSD
     22  1.1  itojun  *	Foundation, Inc. and its contributors.
     23  1.1  itojun  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  1.1  itojun  *    contributors may be used to endorse or promote products derived
     25  1.1  itojun  *    from this software without specific prior written permission.
     26  1.1  itojun  *
     27  1.1  itojun  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  1.1  itojun  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  1.1  itojun  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  1.1  itojun  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  1.1  itojun  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  1.1  itojun  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  1.1  itojun  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  1.1  itojun  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  1.1  itojun  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  1.1  itojun  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  1.1  itojun  * POSSIBILITY OF SUCH DAMAGE.
     38  1.1  itojun  */
     39  1.1  itojun 
     40  1.1  itojun /*
     41  1.1  itojun  * Copyright (c) 1996 Matthias Pfaller.
     42  1.1  itojun  * All rights reserved.
     43  1.1  itojun  *
     44  1.1  itojun  * Redistribution and use in source and binary forms, with or without
     45  1.1  itojun  * modification, are permitted provided that the following conditions
     46  1.1  itojun  * are met:
     47  1.1  itojun  * 1. Redistributions of source code must retain the above copyright
     48  1.1  itojun  *    notice, this list of conditions and the following disclaimer.
     49  1.1  itojun  * 2. Redistributions in binary form must reproduce the above copyright
     50  1.1  itojun  *    notice, this list of conditions and the following disclaimer in the
     51  1.1  itojun  *    documentation and/or other materials provided with the distribution.
     52  1.1  itojun  * 3. All advertising materials mentioning features or use of this software
     53  1.1  itojun  *    must display the following acknowledgement:
     54  1.1  itojun  *	This product includes software developed by Matthias Pfaller.
     55  1.1  itojun  * 4. The name of the author may not be used to endorse or promote products
     56  1.1  itojun  *    derived from this software without specific prior written permission
     57  1.1  itojun  *
     58  1.1  itojun  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     59  1.1  itojun  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     60  1.1  itojun  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     61  1.1  itojun  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     62  1.1  itojun  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     63  1.1  itojun  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     64  1.1  itojun  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     65  1.1  itojun  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     66  1.1  itojun  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     67  1.1  itojun  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     68  1.1  itojun  */
     69  1.3   lukem 
     70  1.3   lukem #include "opt_ddb.h"
     71  1.1  itojun 
     72  1.1  itojun #if defined(DDB)
     73  1.1  itojun #error "Can't build DDB and KGDB together."
     74  1.1  itojun #endif
     75  1.1  itojun 
     76  1.1  itojun /*
     77  1.1  itojun  * Machine-dependent functions for remote KGDB.  Originally written
     78  1.1  itojun  * for NetBSD/pc532 by Matthias Pfaller.  Modified for NetBSD/i386
     79  1.6     uch  * by Jason R. Thorpe.  Modified for NetBSD/mips by Ethan Solomita.
     80  1.6     uch  * Modified for NetBSD/sh3 by UCHIYAMA Yasushi.
     81  1.1  itojun  */
     82  1.1  itojun 
     83  1.6     uch #include <sys/types.h>
     84  1.6     uch #include <sys/systm.h>
     85  1.1  itojun #include <sys/param.h>
     86  1.6     uch #include <sys/proc.h>
     87  1.6     uch #include <sys/user.h>
     88  1.6     uch #include <sys/reboot.h>
     89  1.1  itojun #include <sys/kgdb.h>
     90  1.1  itojun 
     91  1.2     mrg #include <uvm/uvm_extern.h>
     92  1.1  itojun 
     93  1.6     uch #include <sh3/cpu.h>
     94  1.6     uch 
     95  1.6     uch #include <machine/db_machdep.h>
     96  1.6     uch #include <ddb/db_access.h>
     97  1.6     uch 
     98  1.6     uch /*
     99  1.6     uch  * Is kva a valid address to access?  This is used by KGDB.
    100  1.6     uch  */
    101  1.6     uch static int
    102  1.6     uch kvacc(vaddr_t kva)
    103  1.6     uch {
    104  1.6     uch 	pt_entry_t *pte;
    105  1.6     uch 
    106  1.6     uch 	if (kva < SH3_P1SEG_BASE)
    107  1.6     uch 		return (0);
    108  1.6     uch 
    109  1.6     uch 	if (kva < SH3_P2SEG_BASE)
    110  1.6     uch 		return (1);
    111  1.7     uch 
    112  1.6     uch 	if (kva >= VM_MAX_KERNEL_ADDRESS)
    113  1.6     uch 		return (0);
    114  1.6     uch 
    115  1.6     uch 	/* check kva is kernel virtual. */
    116  1.6     uch 	if ((kva < VM_MIN_KERNEL_ADDRESS) ||
    117  1.6     uch 	    (kva >= VM_MAX_KERNEL_ADDRESS))
    118  1.6     uch 		return (0);
    119  1.6     uch 
    120  1.6     uch 	/* check page which related kva is valid. */
    121  1.6     uch 	pte = kvtopte(kva);
    122  1.6     uch 	if (!(*pte & PG_V))
    123  1.6     uch 		return (0);
    124  1.1  itojun 
    125  1.6     uch 	return (1);
    126  1.6     uch }
    127  1.1  itojun 
    128  1.1  itojun /*
    129  1.1  itojun  * Determine if the memory at va..(va+len) is valid.
    130  1.1  itojun  */
    131  1.1  itojun int
    132  1.4     uch kgdb_acc(vaddr_t va, size_t len)
    133  1.1  itojun {
    134  1.1  itojun 	vaddr_t last_va;
    135  1.1  itojun 
    136  1.6     uch 	last_va = va + len + NBPG - 1;
    137  1.1  itojun 	va  &= ~PGOFSET;
    138  1.1  itojun 	last_va &= ~PGOFSET;
    139  1.1  itojun 
    140  1.6     uch 	for (; va < last_va; va += NBPG) {
    141  1.6     uch 		if (kvacc(va) == 0)
    142  1.6     uch 			return 0;
    143  1.6     uch 	}
    144  1.1  itojun 
    145  1.1  itojun 	return (1);
    146  1.1  itojun }
    147  1.1  itojun 
    148  1.1  itojun /*
    149  1.1  itojun  * Translate a trap number into a unix compatible signal value.
    150  1.1  itojun  * (gdb only understands unix signal numbers).
    151  1.1  itojun  */
    152  1.7     uch int
    153  1.4     uch kgdb_signal(int type)
    154  1.1  itojun {
    155  1.6     uch 
    156  1.1  itojun 	switch (type) {
    157  1.6     uch 	case T_TLBMISSR:
    158  1.6     uch 	case T_TLBMISSW:
    159  1.6     uch 	case T_INITPAGEWR:
    160  1.6     uch 	case T_TLBPRIVR:
    161  1.6     uch 	case T_TLBPRIVW:
    162  1.6     uch 	case T_ADDRESSERRR:
    163  1.6     uch 	case T_ADDRESSERRW:
    164  1.6     uch 	case T_TLBMISSR+T_USER:
    165  1.6     uch 	case T_TLBMISSW+T_USER:
    166  1.6     uch 	case T_INITPAGEWR+T_USER:
    167  1.6     uch 	case T_TLBPRIVR+T_USER:
    168  1.6     uch 	case T_TLBPRIVW+T_USER:
    169  1.6     uch 	case T_ADDRESSERRR+T_USER:
    170  1.6     uch 	case T_ADDRESSERRW+T_USER:
    171  1.6     uch 		return (SIGSEGV);
    172  1.1  itojun 
    173  1.6     uch 	case T_TRAP:
    174  1.6     uch 	case T_TRAP+T_USER:
    175  1.6     uch 	case T_USERBREAK:
    176  1.6     uch 	case T_USERBREAK+T_USER:
    177  1.1  itojun 		return (SIGTRAP);
    178  1.1  itojun 
    179  1.6     uch 	case T_INVALIDISN:
    180  1.6     uch 	case T_INVALIDSLOT:
    181  1.6     uch 	case T_INVALIDISN+T_USER:
    182  1.6     uch 	case T_INVALIDSLOT+T_USER:
    183  1.6     uch 		return (SIGILL);
    184  1.1  itojun 
    185  1.1  itojun 	default:
    186  1.1  itojun 		return (SIGEMT);
    187  1.1  itojun 	}
    188  1.1  itojun }
    189  1.1  itojun 
    190  1.1  itojun /*
    191  1.6     uch  * Translate the values stored in the db_regs_t struct to the format
    192  1.6     uch  * understood by gdb. (gdb-5.1.1/gdb/config/sh/tm-sh.h)
    193  1.1  itojun  */
    194  1.1  itojun void
    195  1.4     uch kgdb_getregs(db_regs_t *regs, kgdb_reg_t *gdb_regs)
    196  1.1  itojun {
    197  1.6     uch 	u_int32_t r;
    198  1.6     uch 
    199  1.6     uch 	memset(gdb_regs, 0, KGDB_NUMREGS * sizeof(kgdb_reg_t));
    200  1.6     uch 	gdb_regs[ 0] = regs->tf_r0;
    201  1.6     uch 	gdb_regs[ 1] = regs->tf_r1;
    202  1.6     uch 	gdb_regs[ 2] = regs->tf_r2;
    203  1.6     uch 	gdb_regs[ 3] = regs->tf_r3;
    204  1.6     uch 	gdb_regs[ 4] = regs->tf_r4;
    205  1.6     uch 	gdb_regs[ 5] = regs->tf_r5;
    206  1.6     uch 	gdb_regs[ 6] = regs->tf_r6;
    207  1.6     uch 	gdb_regs[ 7] = regs->tf_r7;
    208  1.6     uch 	gdb_regs[ 8] = regs->tf_r8;
    209  1.6     uch 	gdb_regs[ 9] = regs->tf_r9;
    210  1.6     uch 	gdb_regs[10] = regs->tf_r10;
    211  1.6     uch 	gdb_regs[11] = regs->tf_r11;
    212  1.6     uch 	gdb_regs[12] = regs->tf_r12;
    213  1.6     uch 	gdb_regs[13] = regs->tf_r13;
    214  1.6     uch 	gdb_regs[14] = regs->tf_r14;
    215  1.6     uch 	gdb_regs[15] = regs->tf_r15;
    216  1.6     uch 	gdb_regs[16] = regs->tf_spc;
    217  1.6     uch 	gdb_regs[17] = regs->tf_pr;
    218  1.6     uch 	__asm__ __volatile__("stc vbr, %0" : "=r"(r));
    219  1.6     uch 	gdb_regs[19] = r;
    220  1.6     uch 	gdb_regs[20] = regs->tf_mach;
    221  1.6     uch 	gdb_regs[21] = regs->tf_macl;
    222  1.6     uch 	gdb_regs[22] = regs->tf_ssr;
    223  1.1  itojun 
    224  1.6     uch 	/* How treat register bank 1 ? */
    225  1.1  itojun }
    226  1.1  itojun 
    227  1.1  itojun /*
    228  1.1  itojun  * Reverse the above.
    229  1.1  itojun  */
    230  1.1  itojun void
    231  1.4     uch kgdb_setregs(db_regs_t *regs, kgdb_reg_t *gdb_regs)
    232  1.1  itojun {
    233  1.1  itojun 
    234  1.6     uch 	regs->tf_r0	= gdb_regs[ 0];
    235  1.6     uch 	regs->tf_r1	= gdb_regs[ 1];
    236  1.6     uch 	regs->tf_r2	= gdb_regs[ 2];
    237  1.6     uch 	regs->tf_r3	= gdb_regs[ 3];
    238  1.6     uch 	regs->tf_r4	= gdb_regs[ 4];
    239  1.6     uch 	regs->tf_r5	= gdb_regs[ 5];
    240  1.6     uch 	regs->tf_r6	= gdb_regs[ 6];
    241  1.6     uch 	regs->tf_r7	= gdb_regs[ 7];
    242  1.6     uch 	regs->tf_r8	= gdb_regs[ 8];
    243  1.6     uch 	regs->tf_r9	= gdb_regs[ 9];
    244  1.6     uch 	regs->tf_r10	= gdb_regs[10];
    245  1.6     uch 	regs->tf_r11	= gdb_regs[11];
    246  1.6     uch 	regs->tf_r12	= gdb_regs[12];
    247  1.6     uch 	regs->tf_r13	= gdb_regs[13];
    248  1.6     uch 	regs->tf_r14	= gdb_regs[14];
    249  1.6     uch 	regs->tf_r15	= gdb_regs[15];
    250  1.6     uch 	regs->tf_spc	= gdb_regs[16];
    251  1.6     uch 	regs->tf_pr	= gdb_regs[17];
    252  1.6     uch 	__asm__ __volatile__("ldc %0, vbr" :: "r"(gdb_regs[19]));
    253  1.6     uch 	regs->tf_mach	= gdb_regs[20];
    254  1.6     uch 	regs->tf_macl	= gdb_regs[21];
    255  1.6     uch 	regs->tf_ssr	= gdb_regs[22];
    256  1.1  itojun }
    257  1.1  itojun 
    258  1.1  itojun /*
    259  1.1  itojun  * Trap into kgdb to wait for debugger to connect,
    260  1.1  itojun  * noting on the console why nothing else is going on.
    261  1.1  itojun  */
    262  1.1  itojun void
    263  1.4     uch kgdb_connect(int verbose)
    264  1.1  itojun {
    265  1.1  itojun 
    266  1.6     uch 	if (kgdb_dev < 0) {
    267  1.6     uch 		printf("kgdb_dev=%d\n", kgdb_dev);
    268  1.1  itojun 		return;
    269  1.6     uch 	}
    270  1.1  itojun 
    271  1.1  itojun 	if (verbose)
    272  1.1  itojun 		printf("kgdb waiting...");
    273  1.1  itojun 
    274  1.5     uch 	__asm__ __volatile__("trapa #0xc3");
    275  1.1  itojun 
    276  1.1  itojun 	if (verbose)
    277  1.1  itojun 		printf("connected.\n");
    278  1.1  itojun 
    279  1.1  itojun 	kgdb_debug_panic = 1;
    280  1.1  itojun }
    281  1.1  itojun 
    282  1.1  itojun /*
    283  1.1  itojun  * Decide what to do on panic.
    284  1.1  itojun  * (This is called by panic, like Debugger())
    285  1.1  itojun  */
    286  1.1  itojun void
    287  1.1  itojun kgdb_panic()
    288  1.1  itojun {
    289  1.6     uch 
    290  1.1  itojun 	if (kgdb_dev >= 0 && kgdb_debug_panic) {
    291  1.1  itojun 		printf("entering kgdb\n");
    292  1.1  itojun 		kgdb_connect(kgdb_active == 0);
    293  1.1  itojun 	}
    294  1.1  itojun }
    295