Home | History | Annotate | Line # | Download | only in arm32
      1  1.66       mrg /*	$NetBSD: db_interface.c,v 1.66 2025/09/20 06:27:30 mrg Exp $	*/
      2   1.1      matt 
      3  1.50     skrll /*
      4   1.1      matt  * Copyright (c) 1996 Scott K. Stevens
      5   1.1      matt  *
      6   1.1      matt  * Mach Operating System
      7   1.1      matt  * Copyright (c) 1991,1990 Carnegie Mellon University
      8   1.1      matt  * All Rights Reserved.
      9  1.50     skrll  *
     10   1.1      matt  * Permission to use, copy, modify and distribute this software and its
     11   1.1      matt  * documentation is hereby granted, provided that both the copyright
     12   1.1      matt  * notice and this permission notice appear in all copies of the
     13   1.1      matt  * software, derivative works or modified versions, and any portions
     14   1.1      matt  * thereof, and that both notices appear in supporting documentation.
     15  1.50     skrll  *
     16   1.1      matt  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     17   1.1      matt  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
     18   1.1      matt  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     19  1.50     skrll  *
     20   1.1      matt  * Carnegie Mellon requests users of this software to return to
     21  1.50     skrll  *
     22   1.1      matt  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     23   1.1      matt  *  School of Computer Science
     24   1.1      matt  *  Carnegie Mellon University
     25   1.1      matt  *  Pittsburgh PA 15213-3890
     26  1.50     skrll  *
     27   1.1      matt  * any improvements or extensions that they make and grant Carnegie the
     28   1.1      matt  * rights to redistribute these changes.
     29   1.1      matt  *
     30   1.1      matt  *	From: db_interface.c,v 2.4 1991/02/05 17:11:13 mrt (CMU)
     31   1.1      matt  */
     32   1.1      matt 
     33   1.1      matt /*
     34   1.1      matt  * Interface to new debugger.
     35   1.1      matt  */
     36  1.32     lukem 
     37  1.32     lukem #include <sys/cdefs.h>
     38  1.66       mrg __KERNEL_RCSID(0, "$NetBSD: db_interface.c,v 1.66 2025/09/20 06:27:30 mrg Exp $");
     39  1.32     lukem 
     40   1.1      matt #include "opt_ddb.h"
     41  1.24    briggs #include "opt_kgdb.h"
     42  1.54     skrll #include "opt_multiprocessor.h"
     43   1.1      matt 
     44   1.1      matt #include <sys/param.h>
     45  1.60     skrll 
     46  1.60     skrll #include <sys/atomic.h>
     47  1.60     skrll #include <sys/exec.h>
     48  1.60     skrll #include <sys/intr.h>
     49   1.1      matt #include <sys/proc.h>
     50   1.1      matt #include <sys/reboot.h>
     51   1.1      matt #include <sys/systm.h>	/* just for boothowto */
     52   1.1      matt 
     53   1.1      matt #include <uvm/uvm_extern.h>
     54   1.1      matt 
     55  1.13     chris #include <arm/arm32/db_machdep.h>
     56  1.11   thorpej #include <arm/undefined.h>
     57  1.64  riastrad 
     58  1.13     chris #include <ddb/db_access.h>
     59  1.64  riastrad #include <ddb/db_active.h>
     60   1.1      matt #include <ddb/db_command.h>
     61   1.1      matt #include <ddb/db_output.h>
     62   1.1      matt #include <ddb/db_variables.h>
     63   1.1      matt #include <ddb/db_sym.h>
     64   1.1      matt #include <ddb/db_extern.h>
     65   1.1      matt #include <ddb/db_interface.h>
     66  1.64  riastrad 
     67   1.1      matt #include <dev/cons.h>
     68   1.1      matt 
     69  1.24    briggs #if defined(KGDB) || !defined(DDB)
     70  1.24    briggs #define db_printf	printf
     71  1.24    briggs #endif
     72  1.24    briggs 
     73  1.46       dsl u_int db_fetch_reg(int, db_regs_t *);
     74   1.1      matt 
     75  1.46       dsl int db_trapper(u_int, u_int, trapframe_t *, int);
     76  1.13     chris 
     77   1.1      matt int	db_active = 0;
     78  1.52     skrll db_regs_t ddb_regs;	/* register state */
     79  1.52     skrll db_regs_t *ddb_regp;
     80  1.52     skrll 
     81  1.51      matt #ifdef MULTIPROCESSOR
     82  1.51      matt volatile struct cpu_info *db_onproc;
     83  1.51      matt volatile struct cpu_info *db_newcpu;
     84  1.51      matt #endif
     85  1.51      matt 
     86  1.51      matt 
     87   1.1      matt 
     88   1.1      matt 
     89  1.24    briggs #ifdef DDB
     90   1.1      matt /*
     91   1.1      matt  *  kdb_trap - field a TRACE or BPT trap
     92   1.1      matt  */
     93   1.1      matt int
     94  1.15   thorpej kdb_trap(int type, db_regs_t *regs)
     95   1.1      matt {
     96  1.51      matt 	struct cpu_info * const ci = curcpu();
     97  1.52     skrll 	db_regs_t dbreg;
     98   1.1      matt 	int s;
     99   1.1      matt 
    100   1.1      matt 	switch (type) {
    101   1.1      matt 	case T_BREAKPOINT:	/* breakpoint */
    102   1.1      matt 	case -1:		/* keyboard interrupt */
    103   1.1      matt 		break;
    104  1.51      matt #ifdef MULTIPROCESSOR
    105  1.51      matt 	case -2:
    106  1.51      matt 		/*
    107  1.51      matt 		 * We called to enter ddb from another process but by the time
    108  1.51      matt 		 * we got here, no one was in ddb.  So ignore the request.
    109  1.51      matt 		 */
    110  1.51      matt 		if (db_onproc == NULL)
    111  1.51      matt 			return 1;
    112  1.51      matt 		break;
    113  1.51      matt #endif
    114   1.1      matt 	default:
    115   1.1      matt 		if (db_recover != 0) {
    116  1.31   thorpej 			/* This will longjmp back into db_command_loop() */
    117   1.1      matt 			db_error("Faulted in DDB; continuing...\n");
    118   1.1      matt 			/*NOTREACHED*/
    119   1.1      matt 		}
    120   1.1      matt 	}
    121   1.1      matt 
    122   1.1      matt 	/* Should switch to kdb`s own stack here. */
    123   1.1      matt 
    124  1.51      matt #ifdef MULTIPROCESSOR
    125  1.51      matt 	const bool is_mp_p = ncpu > 1;
    126  1.51      matt 	if (is_mp_p) {
    127  1.51      matt 		/*
    128  1.51      matt 		 * Try to take ownership of DDB.  If we do, tell all other
    129  1.51      matt 		 * CPUs to enter DDB too.
    130  1.51      matt 		 */
    131  1.51      matt 		if (atomic_cas_ptr(&db_onproc, NULL, ci) == NULL) {
    132  1.51      matt 			intr_ipi_send(NULL, IPI_DDB);
    133  1.51      matt 		}
    134  1.51      matt 	}
    135  1.51      matt 	for (;;) {
    136  1.51      matt 		if (is_mp_p) {
    137  1.51      matt 			/*
    138  1.51      matt 			 * While we aren't the master, wait until the master
    139  1.51      matt 			 * gives control to us or exits.  If it exited, we
    140  1.55     skrll 			 * just exit too.  Otherwise this cpu will enter DDB.
    141  1.51      matt 			 */
    142  1.51      matt 			membar_consumer();
    143  1.51      matt 			while (db_onproc != ci) {
    144  1.66       mrg 				if (db_onproc == NULL) {
    145  1.66       mrg 					ddb_regp = NULL;
    146  1.51      matt 					return 1;
    147  1.66       mrg 				}
    148  1.51      matt #ifdef _ARM_ARCH_6
    149  1.51      matt 				__asm __volatile("wfe");
    150  1.51      matt 				membar_consumer();
    151  1.51      matt #endif
    152  1.51      matt 				if (db_onproc == ci) {
    153  1.51      matt 					printf("%s: switching to %s\n",
    154  1.51      matt 					    __func__, ci->ci_cpuname);
    155  1.51      matt 				}
    156  1.51      matt 			}
    157  1.51      matt 		}
    158  1.51      matt #endif
    159   1.1      matt 
    160  1.51      matt 		s = splhigh();
    161  1.52     skrll 		ci->ci_ddb_regs = &dbreg;
    162  1.52     skrll 		ddb_regp = &dbreg;
    163  1.52     skrll 		ddb_regs = *regs;
    164  1.52     skrll 
    165  1.51      matt 		atomic_inc_32(&db_active);
    166  1.51      matt 		cnpollc(true);
    167  1.51      matt 		db_trap(type, 0/*code*/);
    168  1.51      matt 		cnpollc(false);
    169  1.51      matt 		atomic_dec_32(&db_active);
    170  1.52     skrll 
    171  1.51      matt 		ci->ci_ddb_regs = NULL;
    172  1.52     skrll 		ddb_regp = &dbreg;
    173  1.52     skrll 		*regs = ddb_regs;
    174  1.51      matt 		splx(s);
    175  1.51      matt 
    176  1.51      matt #ifdef MULTIPROCESSOR
    177  1.51      matt 		if (is_mp_p && db_newcpu != NULL) {
    178  1.51      matt 			db_onproc = db_newcpu;
    179  1.51      matt 			db_newcpu = NULL;
    180  1.63     skrll 			dsb(ishst);
    181  1.63     skrll 			sev();
    182  1.51      matt 			continue;
    183  1.51      matt 		}
    184  1.51      matt 		break;
    185  1.51      matt 	}
    186   1.1      matt 
    187  1.51      matt 	if (is_mp_p) {
    188  1.51      matt 		/*
    189  1.51      matt 		 * We are exiting DDB so there is noone onproc.  Tell
    190  1.51      matt 		 * the other CPUs to exit.
    191  1.51      matt 		 */
    192  1.51      matt 		db_onproc = NULL;
    193  1.63     skrll 		dsb(ishst);
    194  1.63     skrll 		sev();
    195  1.51      matt 	}
    196  1.51      matt #endif
    197   1.1      matt 
    198  1.66       mrg 	ddb_regp = NULL;
    199  1.56     skrll 	return 1;
    200   1.1      matt }
    201  1.24    briggs #endif
    202   1.1      matt 
    203  1.24    briggs int
    204  1.15   thorpej db_validate_address(vaddr_t addr)
    205   1.1      matt {
    206   1.1      matt 	struct proc *p = curproc;
    207  1.14   thorpej 	struct pmap *pmap;
    208   1.1      matt 
    209  1.23       scw 	if (!p || !p->p_vmspace || !p->p_vmspace->vm_map.pmap ||
    210  1.29   thorpej 	    addr >= VM_MIN_KERNEL_ADDRESS
    211  1.29   thorpej 	   )
    212  1.14   thorpej 		pmap = pmap_kernel();
    213   1.1      matt 	else
    214  1.14   thorpej 		pmap = p->p_vmspace->vm_map.pmap;
    215   1.1      matt 
    216  1.61     skrll 	return pmap_extract(pmap, addr, NULL) == false;
    217   1.1      matt }
    218   1.1      matt 
    219   1.1      matt /*
    220   1.1      matt  * Read bytes from kernel address space for debugger.
    221   1.1      matt  */
    222   1.1      matt void
    223  1.47       dsl db_read_bytes(vaddr_t addr, size_t size, char *data)
    224   1.1      matt {
    225  1.30       scw 	char	*src = (char *)addr;
    226   1.1      matt 
    227  1.30       scw 	if (db_validate_address((u_int)src)) {
    228  1.30       scw 		db_printf("address %p is invalid\n", src);
    229  1.30       scw 		return;
    230  1.30       scw 	}
    231  1.30       scw 
    232  1.30       scw 	if (size == 4 && (addr & 3) == 0 && ((uintptr_t)data & 3) == 0) {
    233  1.30       scw 		*((int*)data) = *((int*)src);
    234  1.30       scw 		return;
    235  1.30       scw 	}
    236  1.30       scw 
    237  1.30       scw 	if (size == 2 && (addr & 1) == 0 && ((uintptr_t)data & 1) == 0) {
    238  1.30       scw 		*((short*)data) = *((short*)src);
    239  1.30       scw 		return;
    240  1.30       scw 	}
    241  1.14   thorpej 
    242  1.14   thorpej 	while (size-- > 0) {
    243   1.1      matt 		if (db_validate_address((u_int)src)) {
    244   1.1      matt 			db_printf("address %p is invalid\n", src);
    245   1.1      matt 			return;
    246   1.1      matt 		}
    247   1.1      matt 		*data++ = *src++;
    248   1.1      matt 	}
    249   1.1      matt }
    250   1.1      matt 
    251   1.1      matt static void
    252  1.37       uwe db_write_text(vaddr_t addr, size_t size, const char *data)
    253  1.50     skrll {
    254   1.1      matt 
    255  1.58       chs 	ktext_write((void *)addr, data, size);
    256   1.1      matt }
    257   1.1      matt 
    258   1.1      matt /*
    259   1.1      matt  * Write bytes to kernel address space for debugger.
    260   1.1      matt  */
    261   1.1      matt void
    262  1.37       uwe db_write_bytes(vaddr_t addr, size_t size, const char *data)
    263   1.1      matt {
    264  1.27   thorpej 	extern char kernel_text[];
    265  1.15   thorpej 	extern char etext[];
    266  1.15   thorpej 	char *dst;
    267  1.15   thorpej 	size_t loop;
    268  1.15   thorpej 
    269  1.15   thorpej 	/* If any part is in kernel text, use db_write_text() */
    270  1.27   thorpej 	if (addr >= (vaddr_t) kernel_text && addr < (vaddr_t) etext) {
    271  1.15   thorpej 		db_write_text(addr, size, data);
    272  1.15   thorpej 		return;
    273  1.15   thorpej 	}
    274   1.1      matt 
    275   1.1      matt 	dst = (char *)addr;
    276  1.30       scw 	if (db_validate_address((u_int)dst)) {
    277  1.30       scw 		db_printf("address %p is invalid\n", dst);
    278  1.30       scw 		return;
    279  1.30       scw 	}
    280  1.30       scw 
    281  1.30       scw 	if (size == 4 && (addr & 3) == 0 && ((uintptr_t)data & 3) == 0)
    282  1.65     skrll 		*((int *)dst) = *((const int *)data);
    283  1.30       scw 	else
    284  1.30       scw 	if (size == 2 && (addr & 1) == 0 && ((uintptr_t)data & 1) == 0)
    285  1.65     skrll 		*((short *)dst) = *((const short *)data);
    286  1.30       scw 	else {
    287  1.30       scw 		loop = size;
    288  1.30       scw 		while (loop-- > 0) {
    289  1.30       scw 			if (db_validate_address((u_int)dst)) {
    290  1.30       scw 				db_printf("address %p is invalid\n", dst);
    291  1.30       scw 				return;
    292  1.30       scw 			}
    293  1.30       scw 			*dst++ = *data++;
    294   1.1      matt 		}
    295   1.1      matt 	}
    296  1.30       scw 
    297   1.1      matt 	/* make sure the caches and memory are in sync */
    298  1.17   thorpej 	cpu_icache_sync_range(addr, size);
    299   1.1      matt 
    300   1.1      matt 	/* In case the current page tables have been modified ... */
    301   1.1      matt 	cpu_tlb_flushID();
    302  1.16   thorpej 	cpu_cpwait();
    303   1.1      matt }
    304   1.1      matt 
    305  1.28       bsh #ifdef DDB
    306   1.1      matt void
    307  1.15   thorpej cpu_Debugger(void)
    308   1.1      matt {
    309  1.62       rin #ifdef _ARM_ARCH_BE8
    310  1.62       rin 	__asm(".word	0xffffffe7");
    311  1.62       rin #else
    312  1.39     perry 	__asm(".word	0xe7ffffff");
    313  1.59       rin #endif
    314   1.1      matt }
    315   1.1      matt 
    316   1.1      matt int
    317  1.15   thorpej db_trapper(u_int addr, u_int inst, trapframe_t *frame, int fault_code)
    318   1.1      matt {
    319  1.15   thorpej 
    320   1.1      matt 	if (fault_code == 0) {
    321   1.1      matt 		if ((inst & ~INSN_COND_MASK) == (BKPT_INST & ~INSN_COND_MASK))
    322   1.1      matt 			kdb_trap(T_BREAKPOINT, frame);
    323   1.1      matt 		else
    324   1.1      matt 			kdb_trap(-1, frame);
    325   1.1      matt 	} else
    326  1.56     skrll 		return 1;
    327  1.56     skrll 	return 0;
    328   1.1      matt }
    329   1.1      matt 
    330   1.1      matt extern u_int esym;
    331   1.1      matt extern u_int end;
    332   1.1      matt 
    333   1.3     bjh21 static struct undefined_handler db_uh;
    334   1.3     bjh21 
    335   1.1      matt void
    336  1.15   thorpej db_machine_init(void)
    337   1.1      matt {
    338   1.1      matt 
    339   1.3     bjh21 	/*
    340   1.3     bjh21 	 * We get called before malloc() is available, so supply a static
    341   1.3     bjh21 	 * struct undefined_handler.
    342   1.3     bjh21 	 */
    343   1.3     bjh21 	db_uh.uh_handler = db_trapper;
    344  1.35  rearnsha 	install_coproc_handler_static(CORE_UNKNOWN_HANDLER, &db_uh);
    345   1.1      matt }
    346  1.24    briggs #endif
    347   1.1      matt 
    348   1.1      matt u_int
    349  1.36        he db_fetch_reg(int reg, db_regs_t *regs)
    350   1.1      matt {
    351   1.1      matt 
    352   1.1      matt 	switch (reg) {
    353   1.1      matt 	case 0:
    354  1.56     skrll 		return regs->tf_r0;
    355   1.1      matt 	case 1:
    356  1.56     skrll 		return regs->tf_r1;
    357   1.1      matt 	case 2:
    358  1.56     skrll 		return regs->tf_r2;
    359   1.1      matt 	case 3:
    360  1.56     skrll 		return regs->tf_r3;
    361   1.1      matt 	case 4:
    362  1.56     skrll 		return regs->tf_r4;
    363   1.1      matt 	case 5:
    364  1.56     skrll 		return regs->tf_r5;
    365   1.1      matt 	case 6:
    366  1.56     skrll 		return regs->tf_r6;
    367   1.1      matt 	case 7:
    368  1.56     skrll 		return regs->tf_r7;
    369   1.1      matt 	case 8:
    370  1.56     skrll 		return regs->tf_r8;
    371   1.1      matt 	case 9:
    372  1.56     skrll 		return regs->tf_r9;
    373   1.1      matt 	case 10:
    374  1.56     skrll 		return regs->tf_r10;
    375   1.1      matt 	case 11:
    376  1.56     skrll 		return regs->tf_r11;
    377   1.1      matt 	case 12:
    378  1.56     skrll 		return regs->tf_r12;
    379   1.1      matt 	case 13:
    380  1.56     skrll 		return regs->tf_svc_sp;
    381   1.1      matt 	case 14:
    382  1.56     skrll 		return regs->tf_svc_lr;
    383   1.1      matt 	case 15:
    384  1.56     skrll 		return regs->tf_pc;
    385   1.1      matt 	default:
    386   1.1      matt 		panic("db_fetch_reg: botch");
    387   1.1      matt 	}
    388   1.1      matt }
    389   1.1      matt 
    390   1.1      matt u_int
    391  1.36        he branch_taken(u_int insn, u_int pc, db_regs_t *regs)
    392   1.1      matt {
    393   1.1      matt 	u_int addr, nregs;
    394   1.1      matt 
    395   1.1      matt 	switch ((insn >> 24) & 0xf) {
    396   1.1      matt 	case 0xa:	/* b ... */
    397   1.1      matt 	case 0xb:	/* bl ... */
    398   1.1      matt 		addr = ((insn << 2) & 0x03ffffff);
    399   1.1      matt 		if (addr & 0x02000000)
    400   1.1      matt 			addr |= 0xfc000000;
    401  1.56     skrll 		return pc + 8 + addr;
    402   1.1      matt 	case 0x7:	/* ldr pc, [pc, reg, lsl #2] */
    403  1.36        he 		addr = db_fetch_reg(insn & 0xf, regs);
    404   1.1      matt 		addr = pc + 8 + (addr << 2);
    405   1.1      matt 		db_read_bytes(addr, 4, (char *)&addr);
    406  1.56     skrll 		return addr;
    407  1.41  christos 	case 0x5:	/* ldr pc, [reg] */
    408  1.41  christos 		addr = db_fetch_reg((insn >> 16) & 0xf, regs);
    409  1.41  christos 		db_read_bytes(addr, 4, (char *)&addr);
    410  1.56     skrll 		return addr;
    411   1.1      matt 	case 0x1:	/* mov pc, reg */
    412  1.36        he 		addr = db_fetch_reg(insn & 0xf, regs);
    413  1.56     skrll 		return addr;
    414   1.1      matt 	case 0x8:	/* ldmxx reg, {..., pc} */
    415   1.1      matt 	case 0x9:
    416  1.36        he 		addr = db_fetch_reg((insn >> 16) & 0xf, regs);
    417   1.1      matt 		nregs = (insn  & 0x5555) + ((insn  >> 1) & 0x5555);
    418   1.1      matt 		nregs = (nregs & 0x3333) + ((nregs >> 2) & 0x3333);
    419   1.1      matt 		nregs = (nregs + (nregs >> 4)) & 0x0f0f;
    420   1.1      matt 		nregs = (nregs + (nregs >> 8)) & 0x001f;
    421   1.1      matt 		switch ((insn >> 23) & 0x3) {
    422   1.1      matt 		case 0x0:	/* ldmda */
    423   1.1      matt 			addr = addr - 0;
    424   1.1      matt 			break;
    425   1.1      matt 		case 0x1:	/* ldmia */
    426   1.1      matt 			addr = addr + 0 + ((nregs - 1) << 2);
    427   1.1      matt 			break;
    428   1.1      matt 		case 0x2:	/* ldmdb */
    429   1.1      matt 			addr = addr - 4;
    430   1.1      matt 			break;
    431   1.1      matt 		case 0x3:	/* ldmib */
    432   1.1      matt 			addr = addr + 4 + ((nregs - 1) << 2);
    433   1.1      matt 			break;
    434   1.1      matt 		}
    435   1.1      matt 		db_read_bytes(addr, 4, (char *)&addr);
    436  1.56     skrll 		return addr;
    437   1.1      matt 	default:
    438   1.1      matt 		panic("branch_taken: botch");
    439   1.1      matt 	}
    440   1.1      matt }
    441