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 (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved. 23 */ 24 25 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 26 /* All Rights Reserved */ 27 /* 28 * Copyright (c) 2018, Joyent, Inc. 29 * Copyright 2012 Nexenta Systems, Inc. All rights reserved. 30 */ 31 32 #include <sys/param.h> 33 #include <sys/types.h> 34 #include <sys/vmparam.h> 35 #include <sys/systm.h> 36 #include <sys/signal.h> 37 #include <sys/stack.h> 38 #include <sys/regset.h> 39 #include <sys/privregs.h> 40 #include <sys/frame.h> 41 #include <sys/proc.h> 42 #include <sys/psw.h> 43 #include <sys/siginfo.h> 44 #include <sys/cpuvar.h> 45 #include <sys/asm_linkage.h> 46 #include <sys/kmem.h> 47 #include <sys/errno.h> 48 #include <sys/bootconf.h> 49 #include <sys/archsystm.h> 50 #include <sys/debug.h> 51 #include <sys/elf.h> 52 #include <sys/spl.h> 53 #include <sys/time.h> 54 #include <sys/atomic.h> 55 #include <sys/sysmacros.h> 56 #include <sys/cmn_err.h> 57 #include <sys/modctl.h> 58 #include <sys/kobj.h> 59 #include <sys/panic.h> 60 #include <sys/reboot.h> 61 #include <sys/time.h> 62 #include <sys/fp.h> 63 #include <sys/x86_archext.h> 64 #include <sys/auxv.h> 65 #include <sys/auxv_386.h> 66 #include <sys/dtrace.h> 67 #include <sys/brand.h> 68 #include <sys/machbrand.h> 69 #include <sys/cmn_err.h> 70 71 /* 72 * Map an fnsave-formatted save area into an fxsave-formatted save area. 73 * 74 * Most fields are the same width, content and semantics. However 75 * the tag word is compressed. 76 */ 77 static void 78 fnsave_to_fxsave(const struct fnsave_state *fn, struct fxsave_state *fx) 79 { 80 uint_t i, tagbits; 81 82 fx->fx_fcw = fn->f_fcw; 83 fx->fx_fsw = fn->f_fsw; 84 85 /* 86 * copy element by element (because of holes) 87 */ 88 for (i = 0; i < 8; i++) 89 bcopy(&fn->f_st[i].fpr_16[0], &fx->fx_st[i].fpr_16[0], 90 sizeof (fn->f_st[0].fpr_16)); /* 80-bit x87-style floats */ 91 92 /* 93 * synthesize compressed tag bits 94 */ 95 fx->fx_fctw = 0; 96 for (tagbits = fn->f_ftw, i = 0; i < 8; i++, tagbits >>= 2) 97 if ((tagbits & 3) != 3) 98 fx->fx_fctw |= (1 << i); 99 100 fx->fx_fop = fn->f_fop; 101 102 fx->fx_rip = (uint64_t)fn->f_eip; 103 fx->fx_rdp = (uint64_t)fn->f_dp; 104 } 105 106 /* 107 * Map from an fxsave-format save area to an fnsave-format save area. 108 */ 109 static void 110 fxsave_to_fnsave(const struct fxsave_state *fx, struct fnsave_state *fn) 111 { 112 uint_t i, top, tagbits; 113 114 fn->f_fcw = fx->fx_fcw; 115 fn->__f_ign0 = 0; 116 fn->f_fsw = fx->fx_fsw; 117 fn->__f_ign1 = 0; 118 119 top = (fx->fx_fsw & FPS_TOP) >> 11; 120 121 /* 122 * copy element by element (because of holes) 123 */ 124 for (i = 0; i < 8; i++) 125 bcopy(&fx->fx_st[i].fpr_16[0], &fn->f_st[i].fpr_16[0], 126 sizeof (fn->f_st[0].fpr_16)); /* 80-bit x87-style floats */ 127 128 /* 129 * synthesize uncompressed tag bits 130 */ 131 fn->f_ftw = 0; 132 for (tagbits = fx->fx_fctw, i = 0; i < 8; i++, tagbits >>= 1) { 133 uint_t ibit, expo; 134 const uint16_t *fpp; 135 static const uint16_t zero[5] = { 0, 0, 0, 0, 0 }; 136 137 if ((tagbits & 1) == 0) { 138 fn->f_ftw |= 3 << (i << 1); /* empty */ 139 continue; 140 } 141 142 /* 143 * (tags refer to *physical* registers) 144 */ 145 fpp = &fx->fx_st[(i - top + 8) & 7].fpr_16[0]; 146 ibit = fpp[3] >> 15; 147 expo = fpp[4] & 0x7fff; 148 149 if (ibit && expo != 0 && expo != 0x7fff) 150 continue; /* valid fp number */ 151 152 if (bcmp(fpp, &zero, sizeof (zero))) 153 fn->f_ftw |= 2 << (i << 1); /* NaN */ 154 else 155 fn->f_ftw |= 1 << (i << 1); /* fp zero */ 156 } 157 158 fn->f_fop = fx->fx_fop; 159 160 fn->__f_ign2 = 0; 161 fn->f_eip = (uint32_t)fx->fx_rip; 162 fn->f_cs = U32CS_SEL; 163 fn->f_dp = (uint32_t)fx->fx_rdp; 164 fn->f_ds = UDS_SEL; 165 fn->__f_ign3 = 0; 166 } 167 168 /* 169 * Map from an fpregset_t into an fxsave-format save area 170 */ 171 static void 172 fpregset_to_fxsave(const fpregset_t *fp, struct fxsave_state *fx) 173 { 174 bcopy(fp, fx, sizeof (*fx)); 175 /* 176 * avoid useless #gp exceptions - mask reserved bits 177 */ 178 fx->fx_mxcsr &= sse_mxcsr_mask; 179 } 180 181 /* 182 * Map from an fxsave-format save area into a fpregset_t 183 */ 184 static void 185 fxsave_to_fpregset(const struct fxsave_state *fx, fpregset_t *fp) 186 { 187 bcopy(fx, fp, sizeof (*fx)); 188 } 189 190 #if defined(_SYSCALL32_IMPL) 191 static void 192 fpregset32_to_fxsave(const fpregset32_t *fp, struct fxsave_state *fx) 193 { 194 const struct fpchip32_state *fc = &fp->fp_reg_set.fpchip_state; 195 196 fnsave_to_fxsave((const struct fnsave_state *)fc, fx); 197 /* 198 * avoid useless #gp exceptions - mask reserved bits 199 */ 200 fx->fx_mxcsr = sse_mxcsr_mask & fc->mxcsr; 201 bcopy(&fc->xmm[0], &fx->fx_xmm[0], sizeof (fc->xmm)); 202 } 203 204 static void 205 fxsave_to_fpregset32(const struct fxsave_state *fx, fpregset32_t *fp) 206 { 207 struct fpchip32_state *fc = &fp->fp_reg_set.fpchip_state; 208 209 fxsave_to_fnsave(fx, (struct fnsave_state *)fc); 210 fc->mxcsr = fx->fx_mxcsr; 211 bcopy(&fx->fx_xmm[0], &fc->xmm[0], sizeof (fc->xmm)); 212 } 213 214 static void 215 fpregset_nto32(const fpregset_t *src, fpregset32_t *dst) 216 { 217 fxsave_to_fpregset32((struct fxsave_state *)src, dst); 218 dst->fp_reg_set.fpchip_state.status = 219 src->fp_reg_set.fpchip_state.status; 220 dst->fp_reg_set.fpchip_state.xstatus = 221 src->fp_reg_set.fpchip_state.xstatus; 222 } 223 224 static void 225 fpregset_32ton(const fpregset32_t *src, fpregset_t *dst) 226 { 227 fpregset32_to_fxsave(src, (struct fxsave_state *)dst); 228 dst->fp_reg_set.fpchip_state.status = 229 src->fp_reg_set.fpchip_state.status; 230 dst->fp_reg_set.fpchip_state.xstatus = 231 src->fp_reg_set.fpchip_state.xstatus; 232 } 233 #endif 234 235 /* 236 * Set floating-point registers from a native fpregset_t. 237 */ 238 void 239 setfpregs(klwp_t *lwp, fpregset_t *fp) 240 { 241 struct fpu_ctx *fpu = &lwp->lwp_pcb.pcb_fpu; 242 243 if (fpu->fpu_flags & FPU_EN) { 244 if (!(fpu->fpu_flags & FPU_VALID)) { 245 /* 246 * FPU context is still active, release the 247 * ownership. 248 */ 249 fp_free(fpu, 0); 250 } 251 } 252 /* 253 * Else: if we are trying to change the FPU state of a thread which 254 * hasn't yet initialized floating point, store the state in 255 * the pcb and indicate that the state is valid. When the 256 * thread enables floating point, it will use this state instead 257 * of the default state. 258 */ 259 260 switch (fp_save_mech) { 261 case FP_FXSAVE: 262 fpregset_to_fxsave(fp, fpu->fpu_regs.kfpu_u.kfpu_fx); 263 fpu->fpu_regs.kfpu_xstatus = 264 fp->fp_reg_set.fpchip_state.xstatus; 265 break; 266 267 case FP_XSAVE: 268 fpregset_to_fxsave(fp, 269 &fpu->fpu_regs.kfpu_u.kfpu_xs->xs_fxsave); 270 fpu->fpu_regs.kfpu_xstatus = 271 fp->fp_reg_set.fpchip_state.xstatus; 272 fpu->fpu_regs.kfpu_u.kfpu_xs->xs_xstate_bv |= 273 (XFEATURE_LEGACY_FP | XFEATURE_SSE); 274 break; 275 default: 276 panic("Invalid fp_save_mech"); 277 /*NOTREACHED*/ 278 } 279 280 fpu->fpu_regs.kfpu_status = fp->fp_reg_set.fpchip_state.status; 281 fpu->fpu_flags |= FPU_VALID; 282 PCB_SET_UPDATE_FPU(&lwp->lwp_pcb); 283 } 284 285 /* 286 * Get floating-point registers into a native fpregset_t. 287 */ 288 void 289 getfpregs(klwp_t *lwp, fpregset_t *fp) 290 { 291 struct fpu_ctx *fpu = &lwp->lwp_pcb.pcb_fpu; 292 293 kpreempt_disable(); 294 if (fpu->fpu_flags & FPU_EN) { 295 /* 296 * If we have FPU hw and the thread's pcb doesn't have 297 * a valid FPU state then get the state from the hw. 298 */ 299 if (fpu_exists && ttolwp(curthread) == lwp && 300 !(fpu->fpu_flags & FPU_VALID)) 301 fp_save(fpu); /* get the current FPU state */ 302 } 303 304 /* 305 * There are 3 possible cases we have to be aware of here: 306 * 307 * 1. FPU is enabled. FPU state is stored in the current LWP. 308 * 309 * 2. FPU is not enabled, and there have been no intervening /proc 310 * modifications. Return initial FPU state. 311 * 312 * 3. FPU is not enabled, but a /proc consumer has modified FPU state. 313 * FPU state is stored in the current LWP. 314 */ 315 if ((fpu->fpu_flags & FPU_EN) || (fpu->fpu_flags & FPU_VALID)) { 316 /* 317 * Cases 1 and 3. 318 */ 319 switch (fp_save_mech) { 320 case FP_FXSAVE: 321 fxsave_to_fpregset(fpu->fpu_regs.kfpu_u.kfpu_fx, fp); 322 fp->fp_reg_set.fpchip_state.xstatus = 323 fpu->fpu_regs.kfpu_xstatus; 324 break; 325 case FP_XSAVE: 326 fxsave_to_fpregset( 327 &fpu->fpu_regs.kfpu_u.kfpu_xs->xs_fxsave, fp); 328 fp->fp_reg_set.fpchip_state.xstatus = 329 fpu->fpu_regs.kfpu_xstatus; 330 break; 331 default: 332 panic("Invalid fp_save_mech"); 333 /*NOTREACHED*/ 334 } 335 fp->fp_reg_set.fpchip_state.status = fpu->fpu_regs.kfpu_status; 336 } else { 337 /* 338 * Case 2. 339 */ 340 switch (fp_save_mech) { 341 case FP_FXSAVE: 342 case FP_XSAVE: 343 /* 344 * For now, we don't have any AVX specific field in ABI. 345 * If we add any in the future, we need to initial them 346 * as well. 347 */ 348 fxsave_to_fpregset(&sse_initial, fp); 349 fp->fp_reg_set.fpchip_state.xstatus = 350 fpu->fpu_regs.kfpu_xstatus; 351 break; 352 default: 353 panic("Invalid fp_save_mech"); 354 /*NOTREACHED*/ 355 } 356 fp->fp_reg_set.fpchip_state.status = fpu->fpu_regs.kfpu_status; 357 } 358 kpreempt_enable(); 359 } 360 361 #if defined(_SYSCALL32_IMPL) 362 363 /* 364 * Set floating-point registers from an fpregset32_t. 365 */ 366 void 367 setfpregs32(klwp_t *lwp, fpregset32_t *fp) 368 { 369 fpregset_t fpregs; 370 371 fpregset_32ton(fp, &fpregs); 372 setfpregs(lwp, &fpregs); 373 } 374 375 /* 376 * Get floating-point registers into an fpregset32_t. 377 */ 378 void 379 getfpregs32(klwp_t *lwp, fpregset32_t *fp) 380 { 381 fpregset_t fpregs; 382 383 getfpregs(lwp, &fpregs); 384 fpregset_nto32(&fpregs, fp); 385 } 386 387 #endif /* _SYSCALL32_IMPL */ 388 389 /* 390 * Return the general registers 391 */ 392 void 393 getgregs(klwp_t *lwp, gregset_t grp) 394 { 395 struct regs *rp = lwptoregs(lwp); 396 struct pcb *pcb = &lwp->lwp_pcb; 397 int thisthread = lwptot(lwp) == curthread; 398 399 grp[REG_RDI] = rp->r_rdi; 400 grp[REG_RSI] = rp->r_rsi; 401 grp[REG_RDX] = rp->r_rdx; 402 grp[REG_RCX] = rp->r_rcx; 403 grp[REG_R8] = rp->r_r8; 404 grp[REG_R9] = rp->r_r9; 405 grp[REG_RAX] = rp->r_rax; 406 grp[REG_RBX] = rp->r_rbx; 407 grp[REG_RBP] = rp->r_rbp; 408 grp[REG_R10] = rp->r_r10; 409 grp[REG_R11] = rp->r_r11; 410 grp[REG_R12] = rp->r_r12; 411 grp[REG_R13] = rp->r_r13; 412 grp[REG_R14] = rp->r_r14; 413 grp[REG_R15] = rp->r_r15; 414 grp[REG_FSBASE] = pcb->pcb_fsbase; 415 grp[REG_GSBASE] = pcb->pcb_gsbase; 416 if (thisthread) 417 kpreempt_disable(); 418 if (PCB_NEED_UPDATE_SEGS(pcb)) { 419 grp[REG_DS] = pcb->pcb_ds; 420 grp[REG_ES] = pcb->pcb_es; 421 grp[REG_FS] = pcb->pcb_fs; 422 grp[REG_GS] = pcb->pcb_gs; 423 } else { 424 grp[REG_DS] = rp->r_ds; 425 grp[REG_ES] = rp->r_es; 426 grp[REG_FS] = rp->r_fs; 427 grp[REG_GS] = rp->r_gs; 428 } 429 if (thisthread) 430 kpreempt_enable(); 431 grp[REG_TRAPNO] = rp->r_trapno; 432 grp[REG_ERR] = rp->r_err; 433 grp[REG_RIP] = rp->r_rip; 434 grp[REG_CS] = rp->r_cs; 435 grp[REG_SS] = rp->r_ss; 436 grp[REG_RFL] = rp->r_rfl; 437 grp[REG_RSP] = rp->r_rsp; 438 } 439 440 #if defined(_SYSCALL32_IMPL) 441 442 void 443 getgregs32(klwp_t *lwp, gregset32_t grp) 444 { 445 struct regs *rp = lwptoregs(lwp); 446 struct pcb *pcb = &lwp->lwp_pcb; 447 int thisthread = lwptot(lwp) == curthread; 448 449 if (thisthread) 450 kpreempt_disable(); 451 if (PCB_NEED_UPDATE_SEGS(pcb)) { 452 grp[GS] = (uint16_t)pcb->pcb_gs; 453 grp[FS] = (uint16_t)pcb->pcb_fs; 454 grp[DS] = (uint16_t)pcb->pcb_ds; 455 grp[ES] = (uint16_t)pcb->pcb_es; 456 } else { 457 grp[GS] = (uint16_t)rp->r_gs; 458 grp[FS] = (uint16_t)rp->r_fs; 459 grp[DS] = (uint16_t)rp->r_ds; 460 grp[ES] = (uint16_t)rp->r_es; 461 } 462 if (thisthread) 463 kpreempt_enable(); 464 grp[EDI] = (greg32_t)rp->r_rdi; 465 grp[ESI] = (greg32_t)rp->r_rsi; 466 grp[EBP] = (greg32_t)rp->r_rbp; 467 grp[ESP] = 0; 468 grp[EBX] = (greg32_t)rp->r_rbx; 469 grp[EDX] = (greg32_t)rp->r_rdx; 470 grp[ECX] = (greg32_t)rp->r_rcx; 471 grp[EAX] = (greg32_t)rp->r_rax; 472 grp[TRAPNO] = (greg32_t)rp->r_trapno; 473 grp[ERR] = (greg32_t)rp->r_err; 474 grp[EIP] = (greg32_t)rp->r_rip; 475 grp[CS] = (uint16_t)rp->r_cs; 476 grp[EFL] = (greg32_t)rp->r_rfl; 477 grp[UESP] = (greg32_t)rp->r_rsp; 478 grp[SS] = (uint16_t)rp->r_ss; 479 } 480 481 void 482 ucontext_32ton(const ucontext32_t *src, ucontext_t *dst) 483 { 484 mcontext_t *dmc = &dst->uc_mcontext; 485 const mcontext32_t *smc = &src->uc_mcontext; 486 487 bzero(dst, sizeof (*dst)); 488 dst->uc_flags = src->uc_flags; 489 dst->uc_link = (ucontext_t *)(uintptr_t)src->uc_link; 490 491 bcopy(&src->uc_sigmask, &dst->uc_sigmask, sizeof (dst->uc_sigmask)); 492 493 dst->uc_stack.ss_sp = (void *)(uintptr_t)src->uc_stack.ss_sp; 494 dst->uc_stack.ss_size = (size_t)src->uc_stack.ss_size; 495 dst->uc_stack.ss_flags = src->uc_stack.ss_flags; 496 497 dmc->gregs[REG_GS] = (greg_t)(uint32_t)smc->gregs[GS]; 498 dmc->gregs[REG_FS] = (greg_t)(uint32_t)smc->gregs[FS]; 499 dmc->gregs[REG_ES] = (greg_t)(uint32_t)smc->gregs[ES]; 500 dmc->gregs[REG_DS] = (greg_t)(uint32_t)smc->gregs[DS]; 501 dmc->gregs[REG_RDI] = (greg_t)(uint32_t)smc->gregs[EDI]; 502 dmc->gregs[REG_RSI] = (greg_t)(uint32_t)smc->gregs[ESI]; 503 dmc->gregs[REG_RBP] = (greg_t)(uint32_t)smc->gregs[EBP]; 504 dmc->gregs[REG_RBX] = (greg_t)(uint32_t)smc->gregs[EBX]; 505 dmc->gregs[REG_RDX] = (greg_t)(uint32_t)smc->gregs[EDX]; 506 dmc->gregs[REG_RCX] = (greg_t)(uint32_t)smc->gregs[ECX]; 507 dmc->gregs[REG_RAX] = (greg_t)(uint32_t)smc->gregs[EAX]; 508 dmc->gregs[REG_TRAPNO] = (greg_t)(uint32_t)smc->gregs[TRAPNO]; 509 dmc->gregs[REG_ERR] = (greg_t)(uint32_t)smc->gregs[ERR]; 510 dmc->gregs[REG_RIP] = (greg_t)(uint32_t)smc->gregs[EIP]; 511 dmc->gregs[REG_CS] = (greg_t)(uint32_t)smc->gregs[CS]; 512 dmc->gregs[REG_RFL] = (greg_t)(uint32_t)smc->gregs[EFL]; 513 dmc->gregs[REG_RSP] = (greg_t)(uint32_t)smc->gregs[UESP]; 514 dmc->gregs[REG_SS] = (greg_t)(uint32_t)smc->gregs[SS]; 515 516 /* 517 * A valid fpregs is only copied in if uc.uc_flags has UC_FPU set 518 * otherwise there is no guarantee that anything in fpregs is valid. 519 */ 520 if (src->uc_flags & UC_FPU) 521 fpregset_32ton(&src->uc_mcontext.fpregs, 522 &dst->uc_mcontext.fpregs); 523 } 524 525 #endif /* _SYSCALL32_IMPL */ 526 527 /* 528 * Return the user-level PC. 529 * If in a system call, return the address of the syscall trap. 530 */ 531 greg_t 532 getuserpc() 533 { 534 greg_t upc = lwptoregs(ttolwp(curthread))->r_pc; 535 uint32_t insn; 536 537 if (curthread->t_sysnum == 0) 538 return (upc); 539 540 /* 541 * We might've gotten here from sysenter (0xf 0x34), 542 * syscall (0xf 0x5) or lcall (0x9a 0 0 0 0 0x27 0). 543 * 544 * Go peek at the binary to figure it out.. 545 */ 546 if (fuword32((void *)(upc - 2), &insn) != -1 && 547 (insn & 0xffff) == 0x340f || (insn & 0xffff) == 0x050f) 548 return (upc - 2); 549 return (upc - 7); 550 } 551 552 /* 553 * Protect segment registers from non-user privilege levels and GDT selectors 554 * other than USER_CS, USER_DS and lwp FS and GS values. If the segment 555 * selector is non-null and not USER_CS/USER_DS, we make sure that the 556 * TI bit is set to point into the LDT and that the RPL is set to 3. 557 * 558 * Since struct regs stores each 16-bit segment register as a 32-bit greg_t, we 559 * also explicitly zero the top 16 bits since they may be coming from the 560 * user's address space via setcontext(2) or /proc. 561 * 562 * Note about null selector. When running on the hypervisor if we allow a 563 * process to set its %cs to null selector with RPL of 0 the hypervisor will 564 * crash the domain. If running on bare metal we would get a #gp fault and 565 * be able to kill the process and continue on. Therefore we make sure to 566 * force RPL to SEL_UPL even for null selector when setting %cs. 567 */ 568 569 #if defined(IS_CS) || defined(IS_NOT_CS) 570 #error "IS_CS and IS_NOT_CS already defined" 571 #endif 572 573 #define IS_CS 1 574 #define IS_NOT_CS 0 575 576 /*ARGSUSED*/ 577 static greg_t 578 fix_segreg(greg_t sr, int iscs, model_t datamodel) 579 { 580 switch (sr &= 0xffff) { 581 582 case 0: 583 if (iscs == IS_CS) 584 return (0 | SEL_UPL); 585 else 586 return (0); 587 588 /* 589 * If lwp attempts to switch data model then force their 590 * code selector to be null selector. 591 */ 592 case U32CS_SEL: 593 if (datamodel == DATAMODEL_NATIVE) 594 return (0 | SEL_UPL); 595 else 596 return (sr); 597 598 case UCS_SEL: 599 if (datamodel == DATAMODEL_ILP32) 600 return (0 | SEL_UPL); 601 /*FALLTHROUGH*/ 602 case UDS_SEL: 603 case LWPFS_SEL: 604 case LWPGS_SEL: 605 case SEL_UPL: 606 return (sr); 607 default: 608 break; 609 } 610 611 /* 612 * Force it into the LDT in ring 3 for 32-bit processes, which by 613 * default do not have an LDT, so that any attempt to use an invalid 614 * selector will reference the (non-existant) LDT, and cause a #gp 615 * fault for the process. 616 * 617 * 64-bit processes get the null gdt selector since they 618 * are not allowed to have a private LDT. 619 */ 620 if (datamodel == DATAMODEL_ILP32) { 621 return (sr | SEL_TI_LDT | SEL_UPL); 622 } else { 623 if (iscs == IS_CS) 624 return (0 | SEL_UPL); 625 else 626 return (0); 627 } 628 629 } 630 631 /* 632 * Set general registers. 633 */ 634 void 635 setgregs(klwp_t *lwp, gregset_t grp) 636 { 637 struct regs *rp = lwptoregs(lwp); 638 model_t datamodel = lwp_getdatamodel(lwp); 639 640 struct pcb *pcb = &lwp->lwp_pcb; 641 int thisthread = lwptot(lwp) == curthread; 642 643 if (datamodel == DATAMODEL_NATIVE) { 644 if (thisthread) 645 (void) save_syscall_args(); /* copy the args */ 646 647 rp->r_rdi = grp[REG_RDI]; 648 rp->r_rsi = grp[REG_RSI]; 649 rp->r_rdx = grp[REG_RDX]; 650 rp->r_rcx = grp[REG_RCX]; 651 rp->r_r8 = grp[REG_R8]; 652 rp->r_r9 = grp[REG_R9]; 653 rp->r_rax = grp[REG_RAX]; 654 rp->r_rbx = grp[REG_RBX]; 655 rp->r_rbp = grp[REG_RBP]; 656 rp->r_r10 = grp[REG_R10]; 657 rp->r_r11 = grp[REG_R11]; 658 rp->r_r12 = grp[REG_R12]; 659 rp->r_r13 = grp[REG_R13]; 660 rp->r_r14 = grp[REG_R14]; 661 rp->r_r15 = grp[REG_R15]; 662 rp->r_trapno = grp[REG_TRAPNO]; 663 rp->r_err = grp[REG_ERR]; 664 rp->r_rip = grp[REG_RIP]; 665 /* 666 * Setting %cs or %ss to anything else is quietly but 667 * quite definitely forbidden! 668 */ 669 rp->r_cs = UCS_SEL; 670 rp->r_ss = UDS_SEL; 671 rp->r_rsp = grp[REG_RSP]; 672 673 if (thisthread) 674 kpreempt_disable(); 675 676 pcb->pcb_ds = UDS_SEL; 677 pcb->pcb_es = UDS_SEL; 678 679 /* 680 * 64-bit processes -are- allowed to set their fsbase/gsbase 681 * values directly, but only if they're using the segment 682 * selectors that allow that semantic. 683 * 684 * (32-bit processes must use lwp_set_private().) 685 */ 686 pcb->pcb_fsbase = grp[REG_FSBASE]; 687 pcb->pcb_gsbase = grp[REG_GSBASE]; 688 pcb->pcb_fs = fix_segreg(grp[REG_FS], IS_NOT_CS, datamodel); 689 pcb->pcb_gs = fix_segreg(grp[REG_GS], IS_NOT_CS, datamodel); 690 691 /* 692 * Ensure that we go out via update_sregs 693 */ 694 PCB_SET_UPDATE_SEGS(pcb); 695 lwptot(lwp)->t_post_sys = 1; 696 if (thisthread) 697 kpreempt_enable(); 698 #if defined(_SYSCALL32_IMPL) 699 } else { 700 rp->r_rdi = (uint32_t)grp[REG_RDI]; 701 rp->r_rsi = (uint32_t)grp[REG_RSI]; 702 rp->r_rdx = (uint32_t)grp[REG_RDX]; 703 rp->r_rcx = (uint32_t)grp[REG_RCX]; 704 rp->r_rax = (uint32_t)grp[REG_RAX]; 705 rp->r_rbx = (uint32_t)grp[REG_RBX]; 706 rp->r_rbp = (uint32_t)grp[REG_RBP]; 707 rp->r_trapno = (uint32_t)grp[REG_TRAPNO]; 708 rp->r_err = (uint32_t)grp[REG_ERR]; 709 rp->r_rip = (uint32_t)grp[REG_RIP]; 710 711 rp->r_cs = fix_segreg(grp[REG_CS], IS_CS, datamodel); 712 rp->r_ss = fix_segreg(grp[REG_DS], IS_NOT_CS, datamodel); 713 714 rp->r_rsp = (uint32_t)grp[REG_RSP]; 715 716 if (thisthread) 717 kpreempt_disable(); 718 719 pcb->pcb_ds = fix_segreg(grp[REG_DS], IS_NOT_CS, datamodel); 720 pcb->pcb_es = fix_segreg(grp[REG_ES], IS_NOT_CS, datamodel); 721 722 /* 723 * (See fsbase/gsbase commentary above) 724 */ 725 pcb->pcb_fs = fix_segreg(grp[REG_FS], IS_NOT_CS, datamodel); 726 pcb->pcb_gs = fix_segreg(grp[REG_GS], IS_NOT_CS, datamodel); 727 728 /* 729 * Ensure that we go out via update_sregs 730 */ 731 PCB_SET_UPDATE_SEGS(pcb); 732 lwptot(lwp)->t_post_sys = 1; 733 if (thisthread) 734 kpreempt_enable(); 735 #endif 736 } 737 738 /* 739 * Only certain bits of the flags register can be modified. 740 */ 741 rp->r_rfl = (rp->r_rfl & ~PSL_USERMASK) | 742 (grp[REG_RFL] & PSL_USERMASK); 743 } 744 745 /* 746 * Determine whether eip is likely to have an interrupt frame 747 * on the stack. We do this by comparing the address to the 748 * range of addresses spanned by several well-known routines. 749 */ 750 extern void _interrupt(); 751 extern void _allsyscalls(); 752 extern void _cmntrap(); 753 extern void fakesoftint(); 754 755 extern size_t _interrupt_size; 756 extern size_t _allsyscalls_size; 757 extern size_t _cmntrap_size; 758 extern size_t _fakesoftint_size; 759 760 /* 761 * Get a pc-only stacktrace. Used for kmem_alloc() buffer ownership tracking. 762 * Returns MIN(current stack depth, pcstack_limit). 763 */ 764 int 765 getpcstack(pc_t *pcstack, int pcstack_limit) 766 { 767 struct frame *fp = (struct frame *)getfp(); 768 struct frame *nextfp, *minfp, *stacktop; 769 int depth = 0; 770 int on_intr; 771 uintptr_t pc; 772 773 if ((on_intr = CPU_ON_INTR(CPU)) != 0) 774 stacktop = (struct frame *)(CPU->cpu_intr_stack + SA(MINFRAME)); 775 else 776 stacktop = (struct frame *)curthread->t_stk; 777 minfp = fp; 778 779 pc = ((struct regs *)fp)->r_pc; 780 781 while (depth < pcstack_limit) { 782 nextfp = (struct frame *)fp->fr_savfp; 783 pc = fp->fr_savpc; 784 if (nextfp <= minfp || nextfp >= stacktop) { 785 if (on_intr) { 786 /* 787 * Hop from interrupt stack to thread stack. 788 */ 789 stacktop = (struct frame *)curthread->t_stk; 790 minfp = (struct frame *)curthread->t_stkbase; 791 on_intr = 0; 792 continue; 793 } 794 break; 795 } 796 pcstack[depth++] = (pc_t)pc; 797 fp = nextfp; 798 minfp = fp; 799 } 800 return (depth); 801 } 802 803 /* 804 * The following ELF header fields are defined as processor-specific 805 * in the V8 ABI: 806 * 807 * e_ident[EI_DATA] encoding of the processor-specific 808 * data in the object file 809 * e_machine processor identification 810 * e_flags processor-specific flags associated 811 * with the file 812 */ 813 814 /* 815 * The value of at_flags reflects a platform's cpu module support. 816 * at_flags is used to check for allowing a binary to execute and 817 * is passed as the value of the AT_FLAGS auxiliary vector. 818 */ 819 int at_flags = 0; 820 821 /* 822 * Check the processor-specific fields of an ELF header. 823 * 824 * returns 1 if the fields are valid, 0 otherwise 825 */ 826 /*ARGSUSED2*/ 827 int 828 elfheadcheck( 829 unsigned char e_data, 830 Elf32_Half e_machine, 831 Elf32_Word e_flags) 832 { 833 if (e_data != ELFDATA2LSB) 834 return (0); 835 if (e_machine == EM_AMD64) 836 return (1); 837 return (e_machine == EM_386); 838 } 839 840 uint_t auxv_hwcap_include = 0; /* patch to enable unrecognized features */ 841 uint_t auxv_hwcap_include_2 = 0; /* second word */ 842 uint_t auxv_hwcap_exclude = 0; /* patch for broken cpus, debugging */ 843 uint_t auxv_hwcap_exclude_2 = 0; /* second word */ 844 #if defined(_SYSCALL32_IMPL) 845 uint_t auxv_hwcap32_include = 0; /* ditto for 32-bit apps */ 846 uint_t auxv_hwcap32_include_2 = 0; /* ditto for 32-bit apps */ 847 uint_t auxv_hwcap32_exclude = 0; /* ditto for 32-bit apps */ 848 uint_t auxv_hwcap32_exclude_2 = 0; /* ditto for 32-bit apps */ 849 #endif 850 851 /* 852 * Gather information about the processor and place it into auxv_hwcap 853 * so that it can be exported to the linker via the aux vector. 854 * 855 * We use this seemingly complicated mechanism so that we can ensure 856 * that /etc/system can be used to override what the system can or 857 * cannot discover for itself. 858 */ 859 void 860 bind_hwcap(void) 861 { 862 uint_t cpu_hwcap_flags[2]; 863 cpuid_pass4(NULL, cpu_hwcap_flags); 864 865 auxv_hwcap = (auxv_hwcap_include | cpu_hwcap_flags[0]) & 866 ~auxv_hwcap_exclude; 867 auxv_hwcap_2 = (auxv_hwcap_include_2 | cpu_hwcap_flags[1]) & 868 ~auxv_hwcap_exclude_2; 869 870 /* 871 * On AMD processors, sysenter just doesn't work at all 872 * when the kernel is in long mode. On IA-32e processors 873 * it does, but there's no real point in all the alternate 874 * mechanism when syscall works on both. 875 * 876 * Besides, the kernel's sysenter handler is expecting a 877 * 32-bit lwp ... 878 */ 879 auxv_hwcap &= ~AV_386_SEP; 880 881 if (auxv_hwcap_include || auxv_hwcap_exclude || auxv_hwcap_include_2 || 882 auxv_hwcap_exclude_2) { 883 /* 884 * The below assignment is regrettably required to get lint 885 * to accept the validity of our format string. The format 886 * string is in fact valid, but whatever intelligence in lint 887 * understands the cmn_err()-specific %b appears to have an 888 * off-by-one error: it (mistakenly) complains about bit 889 * number 32 (even though this is explicitly permitted). 890 * Normally, one would will away such warnings with a "LINTED" 891 * directive, but for reasons unclear and unknown, lint 892 * refuses to be assuaged in this case. Fortunately, lint 893 * doesn't pretend to have solved the Halting Problem -- 894 * and as soon as the format string is programmatic, it 895 * knows enough to shut up. 896 */ 897 char *fmt = "?user ABI extensions: %b\n"; 898 cmn_err(CE_CONT, fmt, auxv_hwcap, FMT_AV_386); 899 fmt = "?user ABI extensions (word 2): %b\n"; 900 cmn_err(CE_CONT, fmt, auxv_hwcap_2, FMT_AV_386_2); 901 } 902 903 #if defined(_SYSCALL32_IMPL) 904 auxv_hwcap32 = (auxv_hwcap32_include | cpu_hwcap_flags[0]) & 905 ~auxv_hwcap32_exclude; 906 auxv_hwcap32_2 = (auxv_hwcap32_include_2 | cpu_hwcap_flags[1]) & 907 ~auxv_hwcap32_exclude_2; 908 909 /* 910 * If this is an amd64 architecture machine from Intel, then 911 * syscall -doesn't- work in compatibility mode, only sysenter does. 912 * 913 * Sigh. 914 */ 915 if (!cpuid_syscall32_insn(NULL)) 916 auxv_hwcap32 &= ~AV_386_AMD_SYSC; 917 918 /* 919 * 32-bit processes can -always- use the lahf/sahf instructions 920 */ 921 auxv_hwcap32 |= AV_386_AHF; 922 923 /* 924 * 32-bit processes can -never- use fsgsbase instructions. 925 */ 926 auxv_hwcap32_2 &= ~AV_386_2_FSGSBASE; 927 928 if (auxv_hwcap32_include || auxv_hwcap32_exclude || 929 auxv_hwcap32_include_2 || auxv_hwcap32_exclude_2) { 930 /* 931 * See the block comment in the cmn_err() of auxv_hwcap, above. 932 */ 933 char *fmt = "?32-bit user ABI extensions: %b\n"; 934 cmn_err(CE_CONT, fmt, auxv_hwcap32, FMT_AV_386); 935 fmt = "?32-bit user ABI extensions (word 2): %b\n"; 936 cmn_err(CE_CONT, fmt, auxv_hwcap32_2, FMT_AV_386_2); 937 } 938 #endif 939 } 940 941 /* 942 * sync_icache() - this is called 943 * in proc/fs/prusrio.c. x86 has an unified cache and therefore 944 * this is a nop. 945 */ 946 /* ARGSUSED */ 947 void 948 sync_icache(caddr_t addr, uint_t len) 949 { 950 /* Do nothing for now */ 951 } 952 953 /*ARGSUSED*/ 954 void 955 sync_data_memory(caddr_t va, size_t len) 956 { 957 /* Not implemented for this platform */ 958 } 959 960 int 961 __ipltospl(int ipl) 962 { 963 return (ipltospl(ipl)); 964 } 965 966 /* 967 * The panic code invokes panic_saveregs() to record the contents of a 968 * regs structure into the specified panic_data structure for debuggers. 969 */ 970 void 971 panic_saveregs(panic_data_t *pdp, struct regs *rp) 972 { 973 panic_nv_t *pnv = PANICNVGET(pdp); 974 975 struct cregs creg; 976 977 getcregs(&creg); 978 979 PANICNVADD(pnv, "rdi", rp->r_rdi); 980 PANICNVADD(pnv, "rsi", rp->r_rsi); 981 PANICNVADD(pnv, "rdx", rp->r_rdx); 982 PANICNVADD(pnv, "rcx", rp->r_rcx); 983 PANICNVADD(pnv, "r8", rp->r_r8); 984 PANICNVADD(pnv, "r9", rp->r_r9); 985 PANICNVADD(pnv, "rax", rp->r_rax); 986 PANICNVADD(pnv, "rbx", rp->r_rbx); 987 PANICNVADD(pnv, "rbp", rp->r_rbp); 988 PANICNVADD(pnv, "r10", rp->r_r10); 989 PANICNVADD(pnv, "r11", rp->r_r11); 990 PANICNVADD(pnv, "r12", rp->r_r12); 991 PANICNVADD(pnv, "r13", rp->r_r13); 992 PANICNVADD(pnv, "r14", rp->r_r14); 993 PANICNVADD(pnv, "r15", rp->r_r15); 994 PANICNVADD(pnv, "fsbase", rdmsr(MSR_AMD_FSBASE)); 995 PANICNVADD(pnv, "gsbase", rdmsr(MSR_AMD_GSBASE)); 996 PANICNVADD(pnv, "ds", rp->r_ds); 997 PANICNVADD(pnv, "es", rp->r_es); 998 PANICNVADD(pnv, "fs", rp->r_fs); 999 PANICNVADD(pnv, "gs", rp->r_gs); 1000 PANICNVADD(pnv, "trapno", rp->r_trapno); 1001 PANICNVADD(pnv, "err", rp->r_err); 1002 PANICNVADD(pnv, "rip", rp->r_rip); 1003 PANICNVADD(pnv, "cs", rp->r_cs); 1004 PANICNVADD(pnv, "rflags", rp->r_rfl); 1005 PANICNVADD(pnv, "rsp", rp->r_rsp); 1006 PANICNVADD(pnv, "ss", rp->r_ss); 1007 PANICNVADD(pnv, "gdt_hi", (uint64_t)(creg.cr_gdt._l[3])); 1008 PANICNVADD(pnv, "gdt_lo", (uint64_t)(creg.cr_gdt._l[0])); 1009 PANICNVADD(pnv, "idt_hi", (uint64_t)(creg.cr_idt._l[3])); 1010 PANICNVADD(pnv, "idt_lo", (uint64_t)(creg.cr_idt._l[0])); 1011 1012 PANICNVADD(pnv, "ldt", creg.cr_ldt); 1013 PANICNVADD(pnv, "task", creg.cr_task); 1014 PANICNVADD(pnv, "cr0", creg.cr_cr0); 1015 PANICNVADD(pnv, "cr2", creg.cr_cr2); 1016 PANICNVADD(pnv, "cr3", creg.cr_cr3); 1017 if (creg.cr_cr4) 1018 PANICNVADD(pnv, "cr4", creg.cr_cr4); 1019 1020 PANICNVSET(pdp, pnv); 1021 } 1022 1023 #define TR_ARG_MAX 6 /* Max args to print, same as SPARC */ 1024 1025 1026 /* 1027 * Print a stack backtrace using the specified frame pointer. We delay two 1028 * seconds before continuing, unless this is the panic traceback. 1029 * If we are in the process of panicking, we also attempt to write the 1030 * stack backtrace to a staticly assigned buffer, to allow the panic 1031 * code to find it and write it in to uncompressed pages within the 1032 * system crash dump. 1033 * Note that the frame for the starting stack pointer value is omitted because 1034 * the corresponding %eip is not known. 1035 */ 1036 1037 extern char *dump_stack_scratch; 1038 1039 1040 void 1041 traceback(caddr_t fpreg) 1042 { 1043 struct frame *fp = (struct frame *)fpreg; 1044 struct frame *nextfp; 1045 uintptr_t pc, nextpc; 1046 ulong_t off; 1047 char args[TR_ARG_MAX * 2 + 16], *sym; 1048 uint_t offset = 0; 1049 uint_t next_offset = 0; 1050 char stack_buffer[1024]; 1051 1052 if (!panicstr) 1053 printf("traceback: %%fp = %p\n", (void *)fp); 1054 1055 if (panicstr && !dump_stack_scratch) { 1056 printf("Warning - stack not written to the dump buffer\n"); 1057 } 1058 1059 fp = (struct frame *)plat_traceback(fpreg); 1060 if ((uintptr_t)fp < KERNELBASE) 1061 goto out; 1062 1063 pc = fp->fr_savpc; 1064 fp = (struct frame *)fp->fr_savfp; 1065 1066 while ((uintptr_t)fp >= KERNELBASE) { 1067 /* 1068 * XX64 Until port is complete tolerate 8-byte aligned 1069 * frame pointers but flag with a warning so they can 1070 * be fixed. 1071 */ 1072 if (((uintptr_t)fp & (STACK_ALIGN - 1)) != 0) { 1073 if (((uintptr_t)fp & (8 - 1)) == 0) { 1074 printf(" >> warning! 8-byte" 1075 " aligned %%fp = %p\n", (void *)fp); 1076 } else { 1077 printf( 1078 " >> mis-aligned %%fp = %p\n", (void *)fp); 1079 break; 1080 } 1081 } 1082 1083 args[0] = '\0'; 1084 nextpc = (uintptr_t)fp->fr_savpc; 1085 nextfp = (struct frame *)fp->fr_savfp; 1086 if ((sym = kobj_getsymname(pc, &off)) != NULL) { 1087 printf("%016lx %s:%s+%lx (%s)\n", (uintptr_t)fp, 1088 mod_containing_pc((caddr_t)pc), sym, off, args); 1089 (void) snprintf(stack_buffer, sizeof (stack_buffer), 1090 "%s:%s+%lx (%s) | ", 1091 mod_containing_pc((caddr_t)pc), sym, off, args); 1092 } else { 1093 printf("%016lx %lx (%s)\n", 1094 (uintptr_t)fp, pc, args); 1095 (void) snprintf(stack_buffer, sizeof (stack_buffer), 1096 "%lx (%s) | ", pc, args); 1097 } 1098 1099 if (panicstr && dump_stack_scratch) { 1100 next_offset = offset + strlen(stack_buffer); 1101 if (next_offset < STACK_BUF_SIZE) { 1102 bcopy(stack_buffer, dump_stack_scratch + offset, 1103 strlen(stack_buffer)); 1104 offset = next_offset; 1105 } else { 1106 /* 1107 * In attempting to save the panic stack 1108 * to the dumpbuf we have overflowed that area. 1109 * Print a warning and continue to printf the 1110 * stack to the msgbuf 1111 */ 1112 printf("Warning: stack in the dump buffer" 1113 " may be incomplete\n"); 1114 offset = next_offset; 1115 } 1116 } 1117 1118 pc = nextpc; 1119 fp = nextfp; 1120 } 1121 out: 1122 if (!panicstr) { 1123 printf("end of traceback\n"); 1124 DELAY(2 * MICROSEC); 1125 } else if (dump_stack_scratch) { 1126 dump_stack_scratch[offset] = '\0'; 1127 } 1128 } 1129 1130 1131 /* 1132 * Generate a stack backtrace from a saved register set. 1133 */ 1134 void 1135 traceregs(struct regs *rp) 1136 { 1137 traceback((caddr_t)rp->r_fp); 1138 } 1139 1140 void 1141 exec_set_sp(size_t stksize) 1142 { 1143 klwp_t *lwp = ttolwp(curthread); 1144 1145 lwptoregs(lwp)->r_sp = (uintptr_t)curproc->p_usrstack - stksize; 1146 } 1147 1148 hrtime_t 1149 gethrtime_waitfree(void) 1150 { 1151 return (dtrace_gethrtime()); 1152 } 1153 1154 hrtime_t 1155 gethrtime(void) 1156 { 1157 return (gethrtimef()); 1158 } 1159 1160 hrtime_t 1161 gethrtime_unscaled(void) 1162 { 1163 return (gethrtimeunscaledf()); 1164 } 1165 1166 void 1167 scalehrtime(hrtime_t *hrt) 1168 { 1169 scalehrtimef(hrt); 1170 } 1171 1172 uint64_t 1173 unscalehrtime(hrtime_t nsecs) 1174 { 1175 return (unscalehrtimef(nsecs)); 1176 } 1177 1178 void 1179 gethrestime(timespec_t *tp) 1180 { 1181 gethrestimef(tp); 1182 } 1183 1184 /* 1185 * Part of the implementation of hres_tick(); this routine is 1186 * easier in C than assembler .. called with the hres_lock held. 1187 * 1188 * XX64 Many of these timekeeping variables need to be extern'ed in a header 1189 */ 1190 1191 #include <sys/time.h> 1192 #include <sys/machlock.h> 1193 1194 extern int one_sec; 1195 extern int max_hres_adj; 1196 1197 void 1198 __adj_hrestime(void) 1199 { 1200 long long adj; 1201 1202 if (hrestime_adj == 0) 1203 adj = 0; 1204 else if (hrestime_adj > 0) { 1205 if (hrestime_adj < max_hres_adj) 1206 adj = hrestime_adj; 1207 else 1208 adj = max_hres_adj; 1209 } else { 1210 if (hrestime_adj < -max_hres_adj) 1211 adj = -max_hres_adj; 1212 else 1213 adj = hrestime_adj; 1214 } 1215 1216 timedelta -= adj; 1217 hrestime_adj = timedelta; 1218 hrestime.tv_nsec += adj; 1219 1220 while (hrestime.tv_nsec >= NANOSEC) { 1221 one_sec++; 1222 hrestime.tv_sec++; 1223 hrestime.tv_nsec -= NANOSEC; 1224 } 1225 } 1226 1227 /* 1228 * Wrapper functions to maintain backwards compability 1229 */ 1230 int 1231 xcopyin(const void *uaddr, void *kaddr, size_t count) 1232 { 1233 return (xcopyin_nta(uaddr, kaddr, count, UIO_COPY_CACHED)); 1234 } 1235 1236 int 1237 xcopyout(const void *kaddr, void *uaddr, size_t count) 1238 { 1239 return (xcopyout_nta(kaddr, uaddr, count, UIO_COPY_CACHED)); 1240 } 1241