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 #include <sys/types.h> 30 #include <sys/reg.h> 31 #include <sys/privregs.h> 32 #include <sys/stack.h> 33 #include <sys/frame.h> 34 35 #include <mdb/mdb_target_impl.h> 36 #include <mdb/mdb_kreg_impl.h> 37 #include <mdb/mdb_debug.h> 38 #include <mdb/mdb_modapi.h> 39 #include <mdb/mdb_amd64util.h> 40 #include <mdb/mdb_ctf.h> 41 #include <mdb/mdb_err.h> 42 #include <mdb/mdb.h> 43 44 /* 45 * This array is used by the getareg and putareg entry points, and also by our 46 * register variable discipline. 47 */ 48 49 const mdb_tgt_regdesc_t mdb_amd64_kregs[] = { 50 { "savfp", KREG_SAVFP, MDB_TGT_R_EXPORT }, 51 { "savpc", KREG_SAVPC, MDB_TGT_R_EXPORT }, 52 { "rdi", KREG_RDI, MDB_TGT_R_EXPORT }, 53 { "rsi", KREG_RSI, MDB_TGT_R_EXPORT }, 54 { "rdx", KREG_RDX, MDB_TGT_R_EXPORT }, 55 { "rcx", KREG_RCX, MDB_TGT_R_EXPORT }, 56 { "r8", KREG_R8, MDB_TGT_R_EXPORT }, 57 { "r9", KREG_R9, MDB_TGT_R_EXPORT }, 58 { "rax", KREG_RAX, MDB_TGT_R_EXPORT }, 59 { "rbx", KREG_RBX, MDB_TGT_R_EXPORT }, 60 { "rbp", KREG_RBP, MDB_TGT_R_EXPORT }, 61 { "r10", KREG_R10, MDB_TGT_R_EXPORT }, 62 { "r11", KREG_R11, MDB_TGT_R_EXPORT }, 63 { "r12", KREG_R12, MDB_TGT_R_EXPORT }, 64 { "r13", KREG_R13, MDB_TGT_R_EXPORT }, 65 { "r14", KREG_R14, MDB_TGT_R_EXPORT }, 66 { "r15", KREG_R15, MDB_TGT_R_EXPORT }, 67 { "fsbase", KREG_FSBASE, MDB_TGT_R_EXPORT | MDB_TGT_R_PRIV }, 68 { "gsbase", KREG_GSBASE, MDB_TGT_R_EXPORT | MDB_TGT_R_PRIV }, 69 { "kgsbase", KREG_KGSBASE, MDB_TGT_R_EXPORT | MDB_TGT_R_PRIV }, 70 { "ds", KREG_DS, MDB_TGT_R_EXPORT }, 71 { "es", KREG_ES, MDB_TGT_R_EXPORT }, 72 { "fs", KREG_FS, MDB_TGT_R_EXPORT }, 73 { "gs", KREG_GS, MDB_TGT_R_EXPORT }, 74 { "trapno", KREG_TRAPNO, MDB_TGT_R_EXPORT | MDB_TGT_R_PRIV }, 75 { "err", KREG_ERR, MDB_TGT_R_EXPORT | MDB_TGT_R_PRIV }, 76 { "rip", KREG_RIP, MDB_TGT_R_EXPORT }, 77 { "cs", KREG_CS, MDB_TGT_R_EXPORT }, 78 { "rflags", KREG_RFLAGS, MDB_TGT_R_EXPORT }, 79 { "rsp", KREG_RSP, MDB_TGT_R_EXPORT }, 80 { "ss", KREG_SS, MDB_TGT_R_EXPORT }, 81 { NULL, 0, 0 } 82 }; 83 84 void 85 mdb_amd64_printregs(const mdb_tgt_gregset_t *gregs) 86 { 87 const kreg_t *kregs = &gregs->kregs[0]; 88 kreg_t rflags = kregs[KREG_RFLAGS]; 89 90 #define GETREG2(x) ((uintptr_t)kregs[(x)]), ((uintptr_t)kregs[(x)]) 91 92 mdb_printf("%%rax = 0x%0?p %15A %%r9 = 0x%0?p %A\n", 93 GETREG2(KREG_RAX), GETREG2(KREG_R9)); 94 mdb_printf("%%rbx = 0x%0?p %15A %%r10 = 0x%0?p %A\n", 95 GETREG2(KREG_RBX), GETREG2(KREG_R10)); 96 mdb_printf("%%rcx = 0x%0?p %15A %%r11 = 0x%0?p %A\n", 97 GETREG2(KREG_RCX), GETREG2(KREG_R11)); 98 mdb_printf("%%rdx = 0x%0?p %15A %%r12 = 0x%0?p %A\n", 99 GETREG2(KREG_RDX), GETREG2(KREG_R12)); 100 mdb_printf("%%rsi = 0x%0?p %15A %%r13 = 0x%0?p %A\n", 101 GETREG2(KREG_RSI), GETREG2(KREG_R13)); 102 mdb_printf("%%rdi = 0x%0?p %15A %%r14 = 0x%0?p %A\n", 103 GETREG2(KREG_RDI), GETREG2(KREG_R14)); 104 mdb_printf("%%r8 = 0x%0?p %15A %%r15 = 0x%0?p %A\n\n", 105 GETREG2(KREG_R8), GETREG2(KREG_R15)); 106 107 mdb_printf("%%rip = 0x%0?p %A\n", GETREG2(KREG_RIP)); 108 mdb_printf("%%rbp = 0x%0?p\n", kregs[KREG_RBP]); 109 mdb_printf("%%rsp = 0x%0?p\n", kregs[KREG_RSP]); 110 111 mdb_printf("%%rflags = 0x%08x\n", rflags); 112 113 mdb_printf(" id=%u vip=%u vif=%u ac=%u vm=%u rf=%u nt=%u iopl=0x%x\n", 114 (rflags & KREG_EFLAGS_ID_MASK) >> KREG_EFLAGS_ID_SHIFT, 115 (rflags & KREG_EFLAGS_VIP_MASK) >> KREG_EFLAGS_VIP_SHIFT, 116 (rflags & KREG_EFLAGS_VIF_MASK) >> KREG_EFLAGS_VIF_SHIFT, 117 (rflags & KREG_EFLAGS_AC_MASK) >> KREG_EFLAGS_AC_SHIFT, 118 (rflags & KREG_EFLAGS_VM_MASK) >> KREG_EFLAGS_VM_SHIFT, 119 (rflags & KREG_EFLAGS_RF_MASK) >> KREG_EFLAGS_RF_SHIFT, 120 (rflags & KREG_EFLAGS_NT_MASK) >> KREG_EFLAGS_NT_SHIFT, 121 (rflags & KREG_EFLAGS_IOPL_MASK) >> KREG_EFLAGS_IOPL_SHIFT); 122 123 mdb_printf(" status=<%s,%s,%s,%s,%s,%s,%s,%s,%s>\n\n", 124 (rflags & KREG_EFLAGS_OF_MASK) ? "OF" : "of", 125 (rflags & KREG_EFLAGS_DF_MASK) ? "DF" : "df", 126 (rflags & KREG_EFLAGS_IF_MASK) ? "IF" : "if", 127 (rflags & KREG_EFLAGS_TF_MASK) ? "TF" : "tf", 128 (rflags & KREG_EFLAGS_SF_MASK) ? "SF" : "sf", 129 (rflags & KREG_EFLAGS_ZF_MASK) ? "ZF" : "zf", 130 (rflags & KREG_EFLAGS_AF_MASK) ? "AF" : "af", 131 (rflags & KREG_EFLAGS_PF_MASK) ? "PF" : "pf", 132 (rflags & KREG_EFLAGS_CF_MASK) ? "CF" : "cf"); 133 134 mdb_printf("%24s%%cs = 0x%04x\t%%ds = 0x%04x\t%%es = 0x%04x\n", 135 " ", kregs[KREG_CS], kregs[KREG_DS], kregs[KREG_ES]); 136 137 mdb_printf("%%trapno = 0x%x\t\t%%fs = 0x%04x\tfsbase = 0x%0?p\n", 138 kregs[KREG_TRAPNO], (kregs[KREG_FS] & 0xffff), kregs[KREG_FSBASE]); 139 mdb_printf(" %%err = 0x%x\t\t%%gs = 0x%04x\tgsbase = 0x%0?p\n", 140 kregs[KREG_ERR], (kregs[KREG_GS] & 0xffff), kregs[KREG_GSBASE]); 141 } 142 143 144 145 /* 146 * Sun Studio 10 patch compiler and gcc 3.4.3 Sun branch implemented a 147 * "-save_args" option on amd64. When the option is specified, INTEGER 148 * type function arguments passed via registers will be saved on the stack 149 * immediately after %rbp, and will not be modified through out the life 150 * of the routine. 151 * 152 * +--------+ 153 * %rbp --> | %rbp | 154 * +--------+ 155 * -0x8(%rbp) | %rdi | 156 * +--------+ 157 * -0x10(%rbp) | %rsi | 158 * +--------+ 159 * -0x18(%rbp) | %rdx | 160 * +--------+ 161 * -0x20(%rbp) | %rcx | 162 * +--------+ 163 * -0x28(%rbp) | %r8 | 164 * +--------+ 165 * -0x30(%rbp) | %r9 | 166 * +--------+ 167 * 168 * 169 * For example, for the following function, 170 * 171 * void 172 * foo(int a1, int a2, int a3, int a4, int a5, int a6, int a7) 173 * { 174 * ... 175 * } 176 * 177 * Disassembled code will look something like the following: 178 * 179 * pushq %rbp 180 * movq %rsp, %rbp 181 * subq $imm8, %rsp ** 182 * movq %rdi, -0x8(%rbp) 183 * movq %rsi, -0x10(%rbp) 184 * movq %rdx, -0x18(%rbp) 185 * movq %rcx, -0x20(%rbp) 186 * movq %r8, -0x28(%rbp) 187 * movq %r9, -0x30(%rbp) 188 * ... 189 * or 190 * pushq %rbp 191 * movq %rsp, %rbp 192 * subq $imm8, %rsp ** 193 * movq %r9, -0x30(%rbp) 194 * movq %r8, -0x28(%rbp) 195 * movq %rcx, -0x20(%rbp) 196 * movq %rdx, -0x18(%rbp) 197 * movq %rsi, -0x10(%rbp) 198 * movq %rdi, -0x8(%rbp) 199 * ... 200 * 201 * **: The space being reserved is in addition to what the current 202 * function prolog already reserves. 203 * 204 * If there are odd number of arguments to a function, additional space is 205 * reserved on the stack to maintain 16-byte alignment. For example, 206 * 207 * argc == 0: no argument saving. 208 * argc == 3: save 3, but space for 4 is reserved 209 * argc == 7: save 6. 210 */ 211 212 /* 213 * The longest instruction sequence in bytes before all 6 arguments are 214 * saved on the stack. This value depends on compiler implementation, 215 * therefore it should be examined periodically to guarantee accuracy. 216 */ 217 #define SEQ_LEN 80 218 219 /* 220 * Size of the instruction sequence arrays. It should correspond to 221 * the maximum number of arguments passed via registers. 222 */ 223 #define INSTR_ARRAY_SIZE 6 224 225 #define INSTR4(ins, off) \ 226 (ins[(off)] + (ins[(off) + 1] << 8) + (ins[(off + 2)] << 16) + \ 227 (ins[(off) + 3] << 24)) 228 229 /* 230 * Sun Studio 10 patch implementation saves %rdi first; 231 * GCC 3.4.3 Sun branch implementation saves them in reverse order. 232 */ 233 static const uint32_t save_instr[INSTR_ARRAY_SIZE] = { 234 0xf87d8948, /* movq %rdi, -0x8(%rbp) */ 235 0xf0758948, /* movq %rsi, -0x10(%rbp) */ 236 0xe8558948, /* movq %rdx, -0x18(%rbp) */ 237 0xe04d8948, /* movq %rcx, -0x20(%rbp) */ 238 0xd845894c, /* movq %r8, -0x28(%rbp) */ 239 0xd04d894c /* movq %r9, -0x30(%rbp) */ 240 }; 241 242 static const uint32_t save_fp_instr[2] = { 243 0xe5894855, /* pushq %rbp; movq %rsp,%rbp, encoding 1 */ 244 0xec8b4855 /* pushq %rbp; movq %rsp,%rbp, encoding 2 */ 245 }; 246 247 /* 248 * Look for the above instruction sequences as indicators for register 249 * arguments being available on the stack. 250 */ 251 static int 252 is_argsaved(mdb_tgt_t *t, uintptr_t fstart, uint64_t size, uint_t argc, 253 int start_index) 254 { 255 uint8_t ins[SEQ_LEN]; 256 int i, j; 257 uint32_t n; 258 259 size = MIN(size, SEQ_LEN); 260 argc = MIN((start_index + argc), INSTR_ARRAY_SIZE); 261 262 if (mdb_tgt_vread(t, ins, size, fstart) != size) 263 return (0); 264 265 /* 266 * Make sure framepointer has been saved. 267 */ 268 n = INSTR4(ins, 0); 269 for (i = 0; i < 2; i++) { 270 if (n == save_fp_instr[i]) 271 break; 272 } 273 274 if (i >= 2) 275 return (0); 276 277 /* 278 * Compare against Sun Studio implementation 279 */ 280 for (i = 8, j = start_index; i < size - 4; i++) { 281 n = INSTR4(ins, i); 282 283 if (n == save_instr[j]) { 284 i += 3; 285 if (++j >= argc) 286 return (1); 287 } 288 } 289 290 /* 291 * Compare against GCC implementation 292 */ 293 for (i = 8, j = argc - 1; i < size - 4; i++) { 294 n = INSTR4(ins, i); 295 296 if (n == save_instr[j]) { 297 i += 3; 298 if (--j < start_index) 299 return (1); 300 } 301 } 302 303 return (0); 304 } 305 306 int 307 mdb_amd64_kvm_stack_iter(mdb_tgt_t *t, const mdb_tgt_gregset_t *gsp, 308 mdb_tgt_stack_f *func, void *arg) 309 { 310 mdb_tgt_gregset_t gregs; 311 kreg_t *kregs = &gregs.kregs[0]; 312 int got_pc = (gsp->kregs[KREG_RIP] != 0); 313 uint_t argc, reg_argc; 314 long fr_argv[32]; 315 int start_index; /* index to save_instr where to start comparison */ 316 int i; 317 318 struct { 319 uintptr_t fr_savfp; 320 uintptr_t fr_savpc; 321 } fr; 322 323 uintptr_t fp = gsp->kregs[KREG_RBP]; 324 uintptr_t pc = gsp->kregs[KREG_RIP]; 325 uintptr_t curpc; 326 327 ssize_t size; 328 329 GElf_Sym s; 330 mdb_syminfo_t sip; 331 mdb_ctf_funcinfo_t mfp; 332 333 bcopy(gsp, &gregs, sizeof (gregs)); 334 335 while (fp != 0) { 336 337 curpc = pc; 338 339 if (fp & (STACK_ALIGN - 1)) 340 return (set_errno(EMDB_STKALIGN)); 341 342 if (mdb_tgt_vread(t, &fr, sizeof (fr), fp) != sizeof (fr)) 343 return (-1); /* errno has been set for us */ 344 345 if ((mdb_tgt_lookup_by_addr(t, pc, MDB_TGT_SYM_FUZZY, 346 NULL, 0, &s, &sip) == 0) && 347 (mdb_ctf_func_info(&s, &sip, &mfp) == 0)) { 348 int return_type = mdb_ctf_type_kind(mfp.mtf_return); 349 argc = mfp.mtf_argc; 350 /* 351 * If the function returns a structure or union, 352 * %rdi contains the address in which to store the 353 * return value rather than for an argument. 354 */ 355 if (return_type == CTF_K_STRUCT || 356 return_type == CTF_K_UNION) 357 start_index = 1; 358 else 359 start_index = 0; 360 } else { 361 argc = 0; 362 } 363 364 if (argc != 0 && is_argsaved(t, s.st_value, s.st_size, 365 argc, start_index)) { 366 367 /* Upto to 6 arguments are passed via registers */ 368 reg_argc = MIN(6, mfp.mtf_argc); 369 size = reg_argc * sizeof (long); 370 371 if (mdb_tgt_vread(t, fr_argv, size, (fp - size)) 372 != size) 373 return (-1); /* errno has been set for us */ 374 375 /* 376 * Arrange the arguments in the right order for 377 * printing. 378 */ 379 for (i = 0; i < (reg_argc >> 1); i++) { 380 long t = fr_argv[i]; 381 382 fr_argv[i] = fr_argv[reg_argc - i - 1]; 383 fr_argv[reg_argc - i - 1] = t; 384 } 385 386 if (argc > 6) { 387 size = (argc - 6) * sizeof (long); 388 if (mdb_tgt_vread(t, &fr_argv[6], size, 389 fp + sizeof (fr)) != size) 390 return (-1); /* errno has been set */ 391 } 392 } else 393 argc = 0; 394 395 if (got_pc && func(arg, pc, argc, fr_argv, &gregs) != 0) 396 break; 397 398 kregs[KREG_RSP] = kregs[KREG_RBP]; 399 400 kregs[KREG_RBP] = fp = fr.fr_savfp; 401 kregs[KREG_RIP] = pc = fr.fr_savpc; 402 403 if (curpc == pc) 404 break; 405 406 got_pc = (pc != 0); 407 } 408 409 return (0); 410 } 411 412 /* 413 * Determine the return address for the current frame. Typically this is the 414 * fr_savpc value from the current frame, but we also perform some special 415 * handling to see if we are stopped on one of the first two instructions of 416 * a typical function prologue, in which case %rbp will not be set up yet. 417 */ 418 int 419 mdb_amd64_step_out(mdb_tgt_t *t, uintptr_t *p, kreg_t pc, kreg_t fp, kreg_t sp, 420 mdb_instr_t curinstr) 421 { 422 struct frame fr; 423 GElf_Sym s; 424 char buf[1]; 425 426 enum { 427 M_PUSHQ_RBP = 0x55, /* pushq %rbp */ 428 M_REX_W = 0x48, /* REX prefix with only W set */ 429 M_MOVL_RBP = 0x8b /* movq %rsp, %rbp with prefix */ 430 }; 431 432 if (mdb_tgt_lookup_by_addr(t, pc, MDB_TGT_SYM_FUZZY, 433 buf, 0, &s, NULL) == 0) { 434 if (pc == s.st_value && curinstr == M_PUSHQ_RBP) 435 fp = sp - 8; 436 else if (pc == s.st_value + 1 && curinstr == M_REX_W) { 437 if (mdb_tgt_vread(t, &curinstr, sizeof (curinstr), 438 pc + 1) == sizeof (curinstr) && curinstr == 439 M_MOVL_RBP) 440 fp = sp; 441 } 442 } 443 444 if (mdb_tgt_vread(t, &fr, sizeof (fr), fp) == sizeof (fr)) { 445 *p = fr.fr_savpc; 446 return (0); 447 } 448 449 return (-1); /* errno is set for us */ 450 } 451 452 /*ARGSUSED*/ 453 int 454 mdb_amd64_next(mdb_tgt_t *t, uintptr_t *p, kreg_t pc, mdb_instr_t curinstr) 455 { 456 mdb_tgt_addr_t npc; 457 458 enum { 459 M_CALL_REL = 0xe8, /* call near with relative displacement */ 460 M_CALL_REG = 0xff, /* call near indirect or call far register */ 461 462 M_REX_LO = 0x40, 463 M_REX_HI = 0x4f 464 }; 465 466 /* 467 * If the opcode is a near call with relative displacement, assume the 468 * displacement is a rel32 from the next instruction. 469 */ 470 if (curinstr == M_CALL_REL) { 471 *p = pc + sizeof (mdb_instr_t) + sizeof (uint32_t); 472 return (0); 473 } 474 475 /* Skip the rex prefix, if any */ 476 if (curinstr >= M_REX_LO && curinstr <= M_REX_HI && 477 mdb_tgt_vread(t, &curinstr, sizeof (curinstr), pc) != 478 sizeof (curinstr)) 479 return (-1); /* errno is set for us */ 480 481 if (curinstr != M_CALL_REG) { 482 /* It's not a call */ 483 return (set_errno(EAGAIN)); 484 } 485 486 if ((npc = mdb_dis_nextins(mdb.m_disasm, t, MDB_TGT_AS_VIRT, pc)) == pc) 487 return (-1); /* errno is set for us */ 488 489 *p = npc; 490 return (0); 491 } 492 493 /*ARGSUSED*/ 494 int 495 mdb_amd64_kvm_frame(void *arglim, uintptr_t pc, uint_t argc, const long *argv, 496 const mdb_tgt_gregset_t *gregs) 497 { 498 argc = MIN(argc, (uintptr_t)arglim); 499 mdb_printf("%a(", pc); 500 501 if (argc != 0) { 502 mdb_printf("%lr", *argv++); 503 for (argc--; argc != 0; argc--) 504 mdb_printf(", %lr", *argv++); 505 } 506 507 mdb_printf(")\n"); 508 return (0); 509 } 510 511 int 512 mdb_amd64_kvm_framev(void *arglim, uintptr_t pc, uint_t argc, const long *argv, 513 const mdb_tgt_gregset_t *gregs) 514 { 515 /* 516 * Historically adb limited stack trace argument display to a fixed- 517 * size number of arguments since no symbolic debugging info existed. 518 * On amd64 we can detect the true number of saved arguments so only 519 * respect an arglim of zero; otherwise display the entire argv[]. 520 */ 521 if (arglim == 0) 522 argc = 0; 523 524 mdb_printf("%0?lr %a(", gregs->kregs[KREG_RBP], pc); 525 526 if (argc != 0) { 527 mdb_printf("%lr", *argv++); 528 for (argc--; argc != 0; argc--) 529 mdb_printf(", %lr", *argv++); 530 } 531 532 mdb_printf(")\n"); 533 return (0); 534 } 535