1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 /* 29 * Libkvm Kernel Target Intel 64-bit component 30 * 31 * This file provides the ISA-dependent portion of the libkvm kernel target. 32 * For more details on the implementation refer to mdb_kvm.c. 33 */ 34 35 #include <sys/types.h> 36 #include <sys/reg.h> 37 #include <sys/frame.h> 38 #include <sys/stack.h> 39 #include <sys/sysmacros.h> 40 #include <sys/panic.h> 41 #include <sys/privregs.h> 42 #include <strings.h> 43 44 #include <mdb/mdb_target_impl.h> 45 #include <mdb/mdb_disasm.h> 46 #include <mdb/mdb_modapi.h> 47 #include <mdb/mdb_conf.h> 48 #include <mdb/mdb_kreg_impl.h> 49 #include <mdb/mdb_amd64util.h> 50 #include <mdb/mdb_kvm.h> 51 #include <mdb/mdb_err.h> 52 #include <mdb/mdb_debug.h> 53 #include <mdb/mdb.h> 54 55 static int 56 kt_getareg(mdb_tgt_t *t, mdb_tgt_tid_t tid, 57 const char *rname, mdb_tgt_reg_t *rp) 58 { 59 const mdb_tgt_regdesc_t *rdp; 60 kt_data_t *kt = t->t_data; 61 62 if (tid != kt->k_tid) 63 return (set_errno(EMDB_NOREGS)); 64 65 for (rdp = kt->k_rds; rdp->rd_name != NULL; rdp++) { 66 if (strcmp(rname, rdp->rd_name) == 0) { 67 *rp = kt->k_regs->kregs[rdp->rd_num]; 68 return (0); 69 } 70 } 71 72 return (set_errno(EMDB_BADREG)); 73 } 74 75 static int 76 kt_putareg(mdb_tgt_t *t, mdb_tgt_tid_t tid, const char *rname, mdb_tgt_reg_t r) 77 { 78 const mdb_tgt_regdesc_t *rdp; 79 kt_data_t *kt = t->t_data; 80 81 if (tid != kt->k_tid) 82 return (set_errno(EMDB_NOREGS)); 83 84 for (rdp = kt->k_rds; rdp->rd_name != NULL; rdp++) { 85 if (strcmp(rname, rdp->rd_name) == 0) { 86 kt->k_regs->kregs[rdp->rd_num] = (kreg_t)r; 87 return (0); 88 } 89 } 90 91 return (set_errno(EMDB_BADREG)); 92 } 93 94 /*ARGSUSED*/ 95 int 96 kt_regs(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 97 { 98 kt_data_t *kt = mdb.m_target->t_data; 99 100 if (argc != 0 || (flags & DCMD_ADDRSPEC)) 101 return (DCMD_USAGE); 102 103 mdb_amd64_printregs(kt->k_regs); 104 105 return (DCMD_OK); 106 } 107 108 /* 109 * Return a flag indicating if the specified %eip is likely to have an 110 * interrupt frame on the stack. We do this by comparing the address to the 111 * range of addresses spanned by several well-known routines, and looking 112 * to see if the next and previous %ebp values are "far" apart. Sigh. 113 */ 114 int 115 mdb_kvm_intrframe(mdb_tgt_t *t, uintptr_t pc, uintptr_t fp, 116 uintptr_t prevfp) 117 { 118 kt_data_t *kt = t->t_data; 119 120 return ((pc >= kt->k_intr_sym.st_value && 121 (pc < kt->k_intr_sym.st_value + kt->k_intr_sym.st_size)) || 122 (pc >= kt->k_trap_sym.st_value && 123 (pc < kt->k_trap_sym.st_value + kt->k_trap_sym.st_size)) || 124 (fp >= prevfp + 0x2000) || (fp <= prevfp - 0x2000)); 125 } 126 127 static int 128 kt_stack_common(uintptr_t addr, uint_t flags, int argc, 129 const mdb_arg_t *argv, mdb_tgt_stack_f *func) 130 { 131 kt_data_t *kt = mdb.m_target->t_data; 132 void *arg = (void *)(uintptr_t)mdb.m_nargs; 133 mdb_tgt_gregset_t gregs, *grp; 134 135 if (flags & DCMD_ADDRSPEC) { 136 bzero(&gregs, sizeof (gregs)); 137 gregs.kregs[KREG_RBP] = addr; 138 grp = &gregs; 139 } else 140 grp = kt->k_regs; 141 142 if (argc != 0) { 143 if (argv->a_type == MDB_TYPE_CHAR || argc > 1) 144 return (DCMD_USAGE); 145 146 if (argv->a_type == MDB_TYPE_STRING) 147 arg = (void *)mdb_strtoull(argv->a_un.a_str); 148 else 149 arg = (void *)argv->a_un.a_val; 150 } 151 152 (void) mdb_amd64_kvm_stack_iter(mdb.m_target, grp, func, arg); 153 return (DCMD_OK); 154 } 155 156 static int 157 kt_stack(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 158 { 159 return (kt_stack_common(addr, flags, argc, argv, mdb_amd64_kvm_frame)); 160 } 161 162 static int 163 kt_stackv(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 164 { 165 return (kt_stack_common(addr, flags, argc, argv, mdb_amd64_kvm_framev)); 166 } 167 168 const mdb_tgt_ops_t kt_amd64_ops = { 169 kt_setflags, /* t_setflags */ 170 kt_setcontext, /* t_setcontext */ 171 kt_activate, /* t_activate */ 172 kt_deactivate, /* t_deactivate */ 173 (void (*)()) mdb_tgt_nop, /* t_periodic */ 174 kt_destroy, /* t_destroy */ 175 kt_name, /* t_name */ 176 (const char *(*)()) mdb_conf_isa, /* t_isa */ 177 kt_platform, /* t_platform */ 178 kt_uname, /* t_uname */ 179 kt_dmodel, /* t_dmodel */ 180 kt_aread, /* t_aread */ 181 kt_awrite, /* t_awrite */ 182 kt_vread, /* t_vread */ 183 kt_vwrite, /* t_vwrite */ 184 kt_pread, /* t_pread */ 185 kt_pwrite, /* t_pwrite */ 186 kt_fread, /* t_fread */ 187 kt_fwrite, /* t_fwrite */ 188 (ssize_t (*)()) mdb_tgt_notsup, /* t_ioread */ 189 (ssize_t (*)()) mdb_tgt_notsup, /* t_iowrite */ 190 kt_vtop, /* t_vtop */ 191 kt_lookup_by_name, /* t_lookup_by_name */ 192 kt_lookup_by_addr, /* t_lookup_by_addr */ 193 kt_symbol_iter, /* t_symbol_iter */ 194 kt_mapping_iter, /* t_mapping_iter */ 195 kt_object_iter, /* t_object_iter */ 196 kt_addr_to_map, /* t_addr_to_map */ 197 kt_name_to_map, /* t_name_to_map */ 198 kt_addr_to_ctf, /* t_addr_to_ctf */ 199 kt_name_to_ctf, /* t_name_to_ctf */ 200 kt_status, /* t_status */ 201 (int (*)()) mdb_tgt_notsup, /* t_run */ 202 (int (*)()) mdb_tgt_notsup, /* t_step */ 203 (int (*)()) mdb_tgt_notsup, /* t_step_out */ 204 (int (*)()) mdb_tgt_notsup, /* t_step_branch */ 205 (int (*)()) mdb_tgt_notsup, /* t_next */ 206 (int (*)()) mdb_tgt_notsup, /* t_cont */ 207 (int (*)()) mdb_tgt_notsup, /* t_signal */ 208 (int (*)()) mdb_tgt_null, /* t_add_vbrkpt */ 209 (int (*)()) mdb_tgt_null, /* t_add_sbrkpt */ 210 (int (*)()) mdb_tgt_null, /* t_add_pwapt */ 211 (int (*)()) mdb_tgt_null, /* t_add_vwapt */ 212 (int (*)()) mdb_tgt_null, /* t_add_iowapt */ 213 (int (*)()) mdb_tgt_null, /* t_add_sysenter */ 214 (int (*)()) mdb_tgt_null, /* t_add_sysexit */ 215 (int (*)()) mdb_tgt_null, /* t_add_signal */ 216 (int (*)()) mdb_tgt_null, /* t_add_fault */ 217 kt_getareg, /* t_getareg */ 218 kt_putareg, /* t_putareg */ 219 mdb_amd64_kvm_stack_iter, /* t_stack_iter */ 220 }; 221 222 void 223 kt_amd64_init(mdb_tgt_t *t) 224 { 225 kt_data_t *kt = t->t_data; 226 227 panic_data_t pd; 228 kreg_t *kregs; 229 struct regs regs; 230 uintptr_t addr; 231 232 /* 233 * Initialize the machine-dependent parts of the kernel target 234 * structure. Once this is complete and we fill in the ops 235 * vector, the target is now fully constructed and we can use 236 * the target API itself to perform the rest of our initialization. 237 */ 238 kt->k_rds = mdb_amd64_kregs; 239 kt->k_regs = mdb_zalloc(sizeof (mdb_tgt_gregset_t), UM_SLEEP); 240 kt->k_regsize = sizeof (mdb_tgt_gregset_t); 241 kt->k_dcmd_regs = kt_regs; 242 kt->k_dcmd_stack = kt_stack; 243 kt->k_dcmd_stackv = kt_stackv; 244 kt->k_dcmd_stackr = kt_stackv; 245 246 t->t_ops = &kt_amd64_ops; 247 kregs = kt->k_regs->kregs; 248 249 (void) mdb_dis_select("amd64"); 250 251 /* 252 * Lookup the symbols corresponding to subroutines in locore.s where 253 * we expect a saved regs structure to be pushed on the stack. When 254 * performing stack tracebacks we will attempt to detect interrupt 255 * frames by comparing the %eip value to these symbols. 256 */ 257 (void) mdb_tgt_lookup_by_name(t, MDB_TGT_OBJ_EXEC, 258 "cmnint", &kt->k_intr_sym, NULL); 259 260 (void) mdb_tgt_lookup_by_name(t, MDB_TGT_OBJ_EXEC, 261 "cmntrap", &kt->k_trap_sym, NULL); 262 263 /* 264 * Don't attempt to load any thread or register information if 265 * we're examining the live operating system. 266 */ 267 if (strcmp(kt->k_symfile, "/dev/ksyms") == 0) 268 return; 269 270 /* 271 * If the panicbuf symbol is present and we can consume a panicbuf 272 * header of the appropriate version from this address, then we can 273 * initialize our current register set based on its contents. 274 * Prior to the re-structuring of panicbuf, our only register data 275 * was the panic_regs label_t, into which a setjmp() was performed, 276 * or the panic_reg register pointer, which was only non-zero if 277 * the system panicked as a result of a trap calling die(). 278 */ 279 if (mdb_tgt_readsym(t, MDB_TGT_AS_VIRT, &pd, sizeof (pd), 280 MDB_TGT_OBJ_EXEC, "panicbuf") == sizeof (pd) && 281 pd.pd_version == PANICBUFVERS) { 282 283 size_t pd_size = MIN(PANICBUFSIZE, pd.pd_msgoff); 284 panic_data_t *pdp = mdb_zalloc(pd_size, UM_SLEEP); 285 uint_t i, n; 286 287 (void) mdb_tgt_readsym(t, MDB_TGT_AS_VIRT, pdp, pd_size, 288 MDB_TGT_OBJ_EXEC, "panicbuf"); 289 290 n = (pd_size - (sizeof (panic_data_t) - 291 sizeof (panic_nv_t))) / sizeof (panic_nv_t); 292 293 for (i = 0; i < n; i++) { 294 (void) kt_putareg(t, kt->k_tid, 295 pdp->pd_nvdata[i].pnv_name, 296 pdp->pd_nvdata[i].pnv_value); 297 } 298 299 mdb_free(pdp, pd_size); 300 301 } else if (mdb_tgt_readsym(t, MDB_TGT_AS_VIRT, &addr, sizeof (addr), 302 MDB_TGT_OBJ_EXEC, "panic_reg") == sizeof (addr) && addr != NULL && 303 mdb_tgt_vread(t, ®s, sizeof (regs), addr) == sizeof (regs)) { 304 305 kregs[KREG_SAVFP] = regs.r_savfp; 306 kregs[KREG_SAVPC] = regs.r_savpc; 307 kregs[KREG_RDI] = regs.r_rdi; 308 kregs[KREG_RSI] = regs.r_rsi; 309 kregs[KREG_RDX] = regs.r_rdx; 310 kregs[KREG_RCX] = regs.r_rcx; 311 kregs[KREG_R8] = regs.r_r8; 312 kregs[KREG_R9] = regs.r_r9; 313 kregs[KREG_RAX] = regs.r_rax; 314 kregs[KREG_RBX] = regs.r_rbx; 315 kregs[KREG_RBP] = regs.r_rbp; 316 kregs[KREG_R10] = regs.r_r10; 317 kregs[KREG_R11] = regs.r_r11; 318 kregs[KREG_R12] = regs.r_r12; 319 kregs[KREG_R13] = regs.r_r13; 320 kregs[KREG_R14] = regs.r_r14; 321 kregs[KREG_R15] = regs.r_r15; 322 kregs[KREG_DS] = regs.r_ds; 323 kregs[KREG_ES] = regs.r_es; 324 kregs[KREG_FS] = regs.r_fs; 325 kregs[KREG_GS] = regs.r_gs; 326 kregs[KREG_TRAPNO] = regs.r_trapno; 327 kregs[KREG_ERR] = regs.r_err; 328 kregs[KREG_RIP] = regs.r_rip; 329 kregs[KREG_CS] = regs.r_cs; 330 kregs[KREG_RFLAGS] = regs.r_rfl; 331 kregs[KREG_RSP] = regs.r_rsp; 332 kregs[KREG_SS] = regs.r_ss; 333 334 } else { 335 warn("failed to read panicbuf and panic_reg -- " 336 "current register set will be unavailable\n"); 337 } 338 } 339