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