1 /*- 2 * Copyright (c) 2015 The FreeBSD Foundation 3 * 4 * This software was developed by Semihalf under 5 * the sponsorship of the FreeBSD Foundation. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 #include <sys/param.h> 32 #include <sys/proc.h> 33 #include <vm/vm.h> 34 #include <vm/pmap.h> 35 #include <vm/vm_map.h> 36 37 #ifdef KDB 38 #include <sys/kdb.h> 39 #endif 40 41 #include <ddb/ddb.h> 42 #include <ddb/db_variables.h> 43 44 #include <machine/cpu.h> 45 #include <machine/pcb.h> 46 #include <machine/stack.h> 47 #include <machine/vmparam.h> 48 49 static int 50 db_frame(struct db_variable *vp, db_expr_t *valuep, int op) 51 { 52 long *reg; 53 54 if (kdb_frame == NULL) 55 return (0); 56 57 reg = (long *)((uintptr_t)kdb_frame + (db_expr_t)vp->valuep); 58 if (op == DB_VAR_GET) 59 *valuep = *reg; 60 else 61 *reg = *valuep; 62 return (1); 63 } 64 65 #define DB_OFFSET(x) (db_expr_t *)offsetof(struct trapframe, x) 66 struct db_variable db_regs[] = { 67 { "ra", DB_OFFSET(tf_ra), db_frame }, 68 { "sp", DB_OFFSET(tf_sp), db_frame }, 69 { "gp", DB_OFFSET(tf_gp), db_frame }, 70 { "tp", DB_OFFSET(tf_tp), db_frame }, 71 { "t0", DB_OFFSET(tf_t[0]), db_frame }, 72 { "t1", DB_OFFSET(tf_t[1]), db_frame }, 73 { "t2", DB_OFFSET(tf_t[2]), db_frame }, 74 { "t3", DB_OFFSET(tf_t[3]), db_frame }, 75 { "t4", DB_OFFSET(tf_t[4]), db_frame }, 76 { "t5", DB_OFFSET(tf_t[5]), db_frame }, 77 { "t6", DB_OFFSET(tf_t[6]), db_frame }, 78 { "s0", DB_OFFSET(tf_s[0]), db_frame }, 79 { "s1", DB_OFFSET(tf_s[1]), db_frame }, 80 { "s2", DB_OFFSET(tf_s[2]), db_frame }, 81 { "s3", DB_OFFSET(tf_s[3]), db_frame }, 82 { "s4", DB_OFFSET(tf_s[4]), db_frame }, 83 { "s5", DB_OFFSET(tf_s[5]), db_frame }, 84 { "s6", DB_OFFSET(tf_s[6]), db_frame }, 85 { "s7", DB_OFFSET(tf_s[7]), db_frame }, 86 { "s8", DB_OFFSET(tf_s[8]), db_frame }, 87 { "s9", DB_OFFSET(tf_s[9]), db_frame }, 88 { "s10", DB_OFFSET(tf_s[10]), db_frame }, 89 { "s11", DB_OFFSET(tf_s[11]), db_frame }, 90 { "a0", DB_OFFSET(tf_a[0]), db_frame }, 91 { "a1", DB_OFFSET(tf_a[1]), db_frame }, 92 { "a2", DB_OFFSET(tf_a[2]), db_frame }, 93 { "a3", DB_OFFSET(tf_a[3]), db_frame }, 94 { "a4", DB_OFFSET(tf_a[4]), db_frame }, 95 { "a5", DB_OFFSET(tf_a[5]), db_frame }, 96 { "a6", DB_OFFSET(tf_a[6]), db_frame }, 97 { "a7", DB_OFFSET(tf_a[7]), db_frame }, 98 { "sepc", DB_OFFSET(tf_sepc), db_frame }, 99 { "sstatus", DB_OFFSET(tf_sstatus), db_frame }, 100 { "stval", DB_OFFSET(tf_stval), db_frame }, 101 { "scause", DB_OFFSET(tf_scause), db_frame }, 102 }; 103 104 struct db_variable *db_eregs = db_regs + nitems(db_regs); 105 106 void 107 db_show_mdpcpu(struct pcpu *pc) 108 { 109 db_printf("curpmap = %p\n", pc->pc_curpmap); 110 db_printf("pending_ipis = %x\n", pc->pc_pending_ipis); 111 db_printf("hart = %u\n", pc->pc_hart); 112 } 113 114 /* 115 * Read bytes from kernel address space for debugger. 116 */ 117 int 118 db_read_bytes(vm_offset_t addr, size_t size, char *data) 119 { 120 jmp_buf jb; 121 void *prev_jb; 122 const char *src; 123 int ret; 124 125 prev_jb = kdb_jmpbuf(jb); 126 ret = setjmp(jb); 127 128 if (ret == 0) { 129 src = (const char *)addr; 130 while (size-- > 0) 131 *data++ = *src++; 132 } 133 (void)kdb_jmpbuf(prev_jb); 134 135 return (ret); 136 } 137 138 /* 139 * Write bytes to kernel address space for debugger. 140 */ 141 int 142 db_write_bytes(vm_offset_t addr, size_t size, char *data) 143 { 144 jmp_buf jb; 145 void *prev_jb; 146 char *dst; 147 int ret; 148 149 prev_jb = kdb_jmpbuf(jb); 150 ret = setjmp(jb); 151 if (ret == 0) { 152 dst = (char *)addr; 153 while (size-- > 0) 154 *dst++ = *data++; 155 156 /* Invalidate I-cache */ 157 fence_i(); 158 } 159 (void)kdb_jmpbuf(prev_jb); 160 161 return (ret); 162 } 163