10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 70Sstevel@tonic-gate * with the License. 80Sstevel@tonic-gate * 90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 110Sstevel@tonic-gate * See the License for the specific language governing permissions 120Sstevel@tonic-gate * and limitations under the License. 130Sstevel@tonic-gate * 140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 190Sstevel@tonic-gate * 200Sstevel@tonic-gate * CDDL HEADER END 210Sstevel@tonic-gate */ 220Sstevel@tonic-gate /* 230Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 280Sstevel@tonic-gate 290Sstevel@tonic-gate /* common code with bug fixes from original version in trap.c */ 300Sstevel@tonic-gate 310Sstevel@tonic-gate #include <sys/param.h> 320Sstevel@tonic-gate #include <sys/types.h> 330Sstevel@tonic-gate #include <sys/systm.h> 340Sstevel@tonic-gate #include <sys/archsystm.h> 350Sstevel@tonic-gate #include <sys/vmsystm.h> 360Sstevel@tonic-gate #include <sys/fpu/fpusystm.h> 370Sstevel@tonic-gate #include <sys/fpu/fpu_simulator.h> 380Sstevel@tonic-gate #include <sys/inline.h> 390Sstevel@tonic-gate #include <sys/debug.h> 400Sstevel@tonic-gate #include <sys/privregs.h> 410Sstevel@tonic-gate #include <sys/machpcb.h> 420Sstevel@tonic-gate #include <sys/simulate.h> 430Sstevel@tonic-gate #include <sys/proc.h> 440Sstevel@tonic-gate #include <sys/cmn_err.h> 450Sstevel@tonic-gate #include <sys/stack.h> 460Sstevel@tonic-gate #include <sys/watchpoint.h> 470Sstevel@tonic-gate #include <sys/trap.h> 480Sstevel@tonic-gate #include <sys/machtrap.h> 490Sstevel@tonic-gate #include <sys/mman.h> 500Sstevel@tonic-gate #include <sys/asi.h> 510Sstevel@tonic-gate #include <sys/copyops.h> 520Sstevel@tonic-gate #include <vm/as.h> 530Sstevel@tonic-gate #include <vm/page.h> 540Sstevel@tonic-gate #include <sys/model.h> 550Sstevel@tonic-gate #include <vm/seg_vn.h> 56*518Swsm #include <sys/byteorder.h> 570Sstevel@tonic-gate 580Sstevel@tonic-gate #define IS_IBIT_SET(x) (x & 0x2000) 590Sstevel@tonic-gate #define IS_VIS1(op, op3)(op == 2 && op3 == 0x36) 600Sstevel@tonic-gate #define IS_PARTIAL_OR_SHORT_FLOAT_LD_ST(op, op3, asi) \ 610Sstevel@tonic-gate (op == 3 && (op3 == IOP_V8_LDDFA || \ 620Sstevel@tonic-gate op3 == IOP_V8_STDFA) && asi > ASI_SNFL) 630Sstevel@tonic-gate 640Sstevel@tonic-gate static int aligndebug = 0; 650Sstevel@tonic-gate 660Sstevel@tonic-gate /* 670Sstevel@tonic-gate * For the sake of those who must be compatible with unaligned 680Sstevel@tonic-gate * architectures, users can link their programs to use a 690Sstevel@tonic-gate * corrective trap handler that will fix unaligned references 700Sstevel@tonic-gate * a special trap #6 (T_FIX_ALIGN) enables this 'feature'. 710Sstevel@tonic-gate * Returns 1 for success, 0 for failure. 720Sstevel@tonic-gate */ 730Sstevel@tonic-gate 740Sstevel@tonic-gate int 750Sstevel@tonic-gate do_unaligned(struct regs *rp, caddr_t *badaddr) 760Sstevel@tonic-gate { 770Sstevel@tonic-gate uint_t inst, op3, asi = 0; 780Sstevel@tonic-gate uint_t rd, rs1, rs2; 790Sstevel@tonic-gate int sz, nf = 0, ltlend = 0; 800Sstevel@tonic-gate int floatflg; 810Sstevel@tonic-gate int fsrflg; 820Sstevel@tonic-gate int immflg; 830Sstevel@tonic-gate int lddstdflg; 840Sstevel@tonic-gate caddr_t addr; 850Sstevel@tonic-gate uint64_t val; 860Sstevel@tonic-gate union { 870Sstevel@tonic-gate uint64_t l[2]; 880Sstevel@tonic-gate uint32_t i[4]; 890Sstevel@tonic-gate uint16_t s[8]; 900Sstevel@tonic-gate uint8_t c[16]; 910Sstevel@tonic-gate } data; 920Sstevel@tonic-gate 930Sstevel@tonic-gate ASSERT(USERMODE(rp->r_tstate)); 940Sstevel@tonic-gate inst = fetch_user_instr((caddr_t)rp->r_pc); 950Sstevel@tonic-gate 960Sstevel@tonic-gate op3 = (inst >> 19) & 0x3f; 970Sstevel@tonic-gate rd = (inst >> 25) & 0x1f; 980Sstevel@tonic-gate rs1 = (inst >> 14) & 0x1f; 990Sstevel@tonic-gate rs2 = inst & 0x1f; 1000Sstevel@tonic-gate floatflg = (inst >> 24) & 1; 1010Sstevel@tonic-gate immflg = (inst >> 13) & 1; 1020Sstevel@tonic-gate lddstdflg = fsrflg = 0; 1030Sstevel@tonic-gate 1040Sstevel@tonic-gate /* if not load or store do nothing */ 1050Sstevel@tonic-gate if ((inst >> 30) != 3) 1060Sstevel@tonic-gate return (0); 1070Sstevel@tonic-gate 1080Sstevel@tonic-gate /* if ldstub or swap, do nothing */ 1090Sstevel@tonic-gate if ((inst & 0xc1680000) == 0xc0680000) 1100Sstevel@tonic-gate return (0); 1110Sstevel@tonic-gate 1120Sstevel@tonic-gate /* if cas/casx, do nothing */ 1130Sstevel@tonic-gate if ((inst & 0xc1e00000) == 0xc1e00000) 1140Sstevel@tonic-gate return (0); 1150Sstevel@tonic-gate 1160Sstevel@tonic-gate if (floatflg) { 1170Sstevel@tonic-gate switch ((inst >> 19) & 3) { /* map size bits to a number */ 1180Sstevel@tonic-gate case 0: sz = 4; 1190Sstevel@tonic-gate break; /* ldf{a}/stf{a} */ 1200Sstevel@tonic-gate case 1: fsrflg = 1; 1210Sstevel@tonic-gate if (rd == 0) 1220Sstevel@tonic-gate sz = 4; /* ldfsr/stfsr */ 1230Sstevel@tonic-gate else if (rd == 1) 1240Sstevel@tonic-gate sz = 8; /* ldxfsr/stxfsr */ 1250Sstevel@tonic-gate else 1260Sstevel@tonic-gate return (SIMU_ILLEGAL); 1270Sstevel@tonic-gate break; 1280Sstevel@tonic-gate case 2: sz = 16; 1290Sstevel@tonic-gate break; /* ldqf{a}/stqf{a} */ 1300Sstevel@tonic-gate case 3: sz = 8; 1310Sstevel@tonic-gate break; /* lddf{a}/stdf{a} */ 1320Sstevel@tonic-gate } 1330Sstevel@tonic-gate /* 1340Sstevel@tonic-gate * Fix to access extra double register encoding plus 1350Sstevel@tonic-gate * compensate to access the correct fpu_dreg. 1360Sstevel@tonic-gate */ 1370Sstevel@tonic-gate if ((sz > 4) && (fsrflg == 0)) { 1380Sstevel@tonic-gate if ((rd & 1) == 1) 1390Sstevel@tonic-gate rd = (rd & 0x1e) | 0x20; 1400Sstevel@tonic-gate rd = rd >> 1; 1410Sstevel@tonic-gate if ((sz == 16) && ((rd & 0x1) != 0)) 1420Sstevel@tonic-gate return (SIMU_ILLEGAL); 1430Sstevel@tonic-gate } 1440Sstevel@tonic-gate } else { 1450Sstevel@tonic-gate int sz_bits = (inst >> 19) & 0xf; 1460Sstevel@tonic-gate switch (sz_bits) { /* map size bits to a number */ 1470Sstevel@tonic-gate case 0: /* lduw{a} */ 1480Sstevel@tonic-gate case 4: /* stw{a} */ 1490Sstevel@tonic-gate case 8: /* ldsw{a} */ 1500Sstevel@tonic-gate case 0xf: /* swap */ 1510Sstevel@tonic-gate sz = 4; break; 1520Sstevel@tonic-gate case 1: /* ldub{a} */ 1530Sstevel@tonic-gate case 5: /* stb{a} */ 1540Sstevel@tonic-gate case 9: /* ldsb{a} */ 1550Sstevel@tonic-gate case 0xd: /* ldstub */ 1560Sstevel@tonic-gate sz = 1; break; 1570Sstevel@tonic-gate case 2: /* lduh{a} */ 1580Sstevel@tonic-gate case 6: /* sth{a} */ 1590Sstevel@tonic-gate case 0xa: /* ldsh{a} */ 1600Sstevel@tonic-gate sz = 2; break; 1610Sstevel@tonic-gate case 3: /* ldd{a} */ 1620Sstevel@tonic-gate case 7: /* std{a} */ 1630Sstevel@tonic-gate lddstdflg = 1; 1640Sstevel@tonic-gate sz = 8; break; 1650Sstevel@tonic-gate case 0xb: /* ldx{a} */ 1660Sstevel@tonic-gate case 0xe: /* stx{a} */ 1670Sstevel@tonic-gate sz = 8; break; 1680Sstevel@tonic-gate } 1690Sstevel@tonic-gate } 1700Sstevel@tonic-gate 1710Sstevel@tonic-gate 1720Sstevel@tonic-gate /* only support primary and secondary asi's */ 1730Sstevel@tonic-gate if ((op3 >> 4) & 1) { 1740Sstevel@tonic-gate if (immflg) { 1750Sstevel@tonic-gate asi = (uint_t)(rp->r_tstate >> TSTATE_ASI_SHIFT) & 1760Sstevel@tonic-gate TSTATE_ASI_MASK; 1770Sstevel@tonic-gate } else { 1780Sstevel@tonic-gate asi = (inst >> 5) & 0xff; 1790Sstevel@tonic-gate } 1800Sstevel@tonic-gate switch (asi) { 1810Sstevel@tonic-gate case ASI_P: 1820Sstevel@tonic-gate case ASI_S: 1830Sstevel@tonic-gate break; 1840Sstevel@tonic-gate case ASI_PNF: 1850Sstevel@tonic-gate case ASI_SNF: 1860Sstevel@tonic-gate nf = 1; 1870Sstevel@tonic-gate break; 1880Sstevel@tonic-gate case ASI_PL: 1890Sstevel@tonic-gate case ASI_SL: 1900Sstevel@tonic-gate ltlend = 1; 1910Sstevel@tonic-gate break; 1920Sstevel@tonic-gate case ASI_PNFL: 1930Sstevel@tonic-gate case ASI_SNFL: 1940Sstevel@tonic-gate ltlend = 1; 1950Sstevel@tonic-gate nf = 1; 1960Sstevel@tonic-gate break; 1970Sstevel@tonic-gate default: 1980Sstevel@tonic-gate return (0); 1990Sstevel@tonic-gate } 2000Sstevel@tonic-gate /* 2010Sstevel@tonic-gate * Non-faulting stores generate a data_access_exception trap, 2020Sstevel@tonic-gate * according to the Spitfire manual, which should be signaled 2030Sstevel@tonic-gate * as an illegal instruction trap, because it can't be fixed. 2040Sstevel@tonic-gate */ 2050Sstevel@tonic-gate if ((nf) && ((op3 == IOP_V8_STQFA) || (op3 == IOP_V8_STDFA))) 2060Sstevel@tonic-gate return (SIMU_ILLEGAL); 2070Sstevel@tonic-gate } 2080Sstevel@tonic-gate 2090Sstevel@tonic-gate if (aligndebug) { 2100Sstevel@tonic-gate printf("unaligned access at %p, instruction: 0x%x\n", 2110Sstevel@tonic-gate (void *)rp->r_pc, inst); 2120Sstevel@tonic-gate printf("type %s", (((inst >> 21) & 1) ? "st" : "ld")); 2130Sstevel@tonic-gate if (((inst >> 21) & 1) == 0) 2140Sstevel@tonic-gate printf(" %s", (((inst >> 22) & 1) ? "signed" : "unsigned")); 2150Sstevel@tonic-gate printf(" asi 0x%x size %d immflg %d\n", asi, sz, immflg); 2160Sstevel@tonic-gate printf("rd = %d, op3 = 0x%x, rs1 = %d, rs2 = %d, imm13=0x%x\n", 2170Sstevel@tonic-gate rd, op3, rs1, rs2, (inst & 0x1fff)); 2180Sstevel@tonic-gate } 2190Sstevel@tonic-gate 2200Sstevel@tonic-gate (void) flush_user_windows_to_stack(NULL); 2210Sstevel@tonic-gate if (getreg(rp, rs1, &val, badaddr)) 2220Sstevel@tonic-gate return (SIMU_FAULT); 2230Sstevel@tonic-gate addr = (caddr_t)val; /* convert to 32/64 bit address */ 2240Sstevel@tonic-gate if (aligndebug) 2250Sstevel@tonic-gate printf("addr 1 = %p\n", (void *)addr); 2260Sstevel@tonic-gate 2270Sstevel@tonic-gate /* check immediate bit and use immediate field or reg (rs2) */ 2280Sstevel@tonic-gate if (immflg) { 2290Sstevel@tonic-gate int imm; 2300Sstevel@tonic-gate imm = inst & 0x1fff; /* mask out immediate field */ 2310Sstevel@tonic-gate imm <<= 19; /* sign extend it */ 2320Sstevel@tonic-gate imm >>= 19; 2330Sstevel@tonic-gate addr += imm; /* compute address */ 2340Sstevel@tonic-gate } else { 2350Sstevel@tonic-gate if (getreg(rp, rs2, &val, badaddr)) 2360Sstevel@tonic-gate return (SIMU_FAULT); 2370Sstevel@tonic-gate addr += val; 2380Sstevel@tonic-gate } 2390Sstevel@tonic-gate 2400Sstevel@tonic-gate /* 2410Sstevel@tonic-gate * If this is a 32-bit program, chop the address accordingly. 2420Sstevel@tonic-gate */ 2430Sstevel@tonic-gate if (curproc->p_model == DATAMODEL_ILP32) 2440Sstevel@tonic-gate addr = (caddr_t)(caddr32_t)addr; 2450Sstevel@tonic-gate 2460Sstevel@tonic-gate if (aligndebug) 2470Sstevel@tonic-gate printf("addr 2 = %p\n", (void *)addr); 2480Sstevel@tonic-gate 2490Sstevel@tonic-gate if (addr >= curproc->p_as->a_userlimit) { 2500Sstevel@tonic-gate *badaddr = addr; 2510Sstevel@tonic-gate goto badret; 2520Sstevel@tonic-gate } 2530Sstevel@tonic-gate 2540Sstevel@tonic-gate /* a single bit differentiates ld and st */ 2550Sstevel@tonic-gate if ((inst >> 21) & 1) { /* store */ 2560Sstevel@tonic-gate if (floatflg) { 2570Sstevel@tonic-gate klwp_id_t lwp = ttolwp(curthread); 2580Sstevel@tonic-gate kfpu_t *fp = lwptofpu(lwp); 2590Sstevel@tonic-gate /* Ensure fp has been enabled */ 2600Sstevel@tonic-gate if (fpu_exists) { 2610Sstevel@tonic-gate if (!(_fp_read_fprs() & FPRS_FEF)) 2620Sstevel@tonic-gate fp_enable(); 2630Sstevel@tonic-gate } else { 2640Sstevel@tonic-gate if (!fp->fpu_en) 2650Sstevel@tonic-gate fp_enable(); 2660Sstevel@tonic-gate } 2670Sstevel@tonic-gate /* if fpu_exists read fpu reg */ 2680Sstevel@tonic-gate if (fpu_exists) { 2690Sstevel@tonic-gate if (fsrflg) { 2700Sstevel@tonic-gate _fp_read_pfsr(&data.l[0]); 2710Sstevel@tonic-gate } else { 2720Sstevel@tonic-gate if (sz == 4) { 2730Sstevel@tonic-gate data.i[0] = 0; 2740Sstevel@tonic-gate _fp_read_pfreg( 2750Sstevel@tonic-gate (unsigned *)&data.i[1], rd); 2760Sstevel@tonic-gate } 2770Sstevel@tonic-gate if (sz >= 8) 2780Sstevel@tonic-gate _fp_read_pdreg( 2790Sstevel@tonic-gate &data.l[0], rd); 2800Sstevel@tonic-gate if (sz == 16) 2810Sstevel@tonic-gate _fp_read_pdreg( 2820Sstevel@tonic-gate &data.l[1], rd+1); 2830Sstevel@tonic-gate } 2840Sstevel@tonic-gate } else { 2850Sstevel@tonic-gate if (fsrflg) { 2860Sstevel@tonic-gate /* Clear reserved bits, set version=7 */ 2870Sstevel@tonic-gate fp->fpu_fsr &= ~0x30301000; 2880Sstevel@tonic-gate fp->fpu_fsr |= 0xE0000; 2890Sstevel@tonic-gate data.l[0] = fp->fpu_fsr; 2900Sstevel@tonic-gate } else { 2910Sstevel@tonic-gate if (sz == 4) { 2920Sstevel@tonic-gate data.i[0] = 0; 2930Sstevel@tonic-gate data.i[1] = 2940Sstevel@tonic-gate (unsigned)fp->fpu_fr.fpu_regs[rd]; 2950Sstevel@tonic-gate } 2960Sstevel@tonic-gate if (sz >= 8) 2970Sstevel@tonic-gate data.l[0] = 2980Sstevel@tonic-gate fp->fpu_fr.fpu_dregs[rd]; 2990Sstevel@tonic-gate if (sz == 16) 3000Sstevel@tonic-gate data.l[1] = 3010Sstevel@tonic-gate fp->fpu_fr.fpu_dregs[rd+1]; 3020Sstevel@tonic-gate } 3030Sstevel@tonic-gate } 3040Sstevel@tonic-gate } else { 305*518Swsm if (lddstdflg) { /* combine the data */ 3060Sstevel@tonic-gate if (getreg(rp, rd, &data.l[0], badaddr)) 3070Sstevel@tonic-gate return (SIMU_FAULT); 3080Sstevel@tonic-gate if (getreg(rp, rd+1, &data.l[1], badaddr)) 3090Sstevel@tonic-gate return (SIMU_FAULT); 310*518Swsm if (ltlend) { 311*518Swsm /* 312*518Swsm * For STD, each 32-bit word is byte- 313*518Swsm * swapped individually. For 314*518Swsm * simplicity we don't want to do that 315*518Swsm * below, so we swap the words now to 316*518Swsm * get the desired result in the end. 317*518Swsm */ 318*518Swsm data.i[0] = data.i[3]; 319*518Swsm } else { 320*518Swsm data.i[0] = data.i[1]; 321*518Swsm data.i[1] = data.i[3]; 322*518Swsm } 3230Sstevel@tonic-gate } else { 3240Sstevel@tonic-gate if (getreg(rp, rd, &data.l[0], badaddr)) 3250Sstevel@tonic-gate return (SIMU_FAULT); 3260Sstevel@tonic-gate } 3270Sstevel@tonic-gate } 3280Sstevel@tonic-gate 3290Sstevel@tonic-gate if (aligndebug) { 3300Sstevel@tonic-gate if (sz == 16) { 3310Sstevel@tonic-gate printf("data %x %x %x %x\n", 3320Sstevel@tonic-gate data.i[0], data.i[1], data.i[2], data.c[3]); 3330Sstevel@tonic-gate } else { 3340Sstevel@tonic-gate printf("data %x %x %x %x %x %x %x %x\n", 3350Sstevel@tonic-gate data.c[0], data.c[1], data.c[2], data.c[3], 3360Sstevel@tonic-gate data.c[4], data.c[5], data.c[6], data.c[7]); 3370Sstevel@tonic-gate } 3380Sstevel@tonic-gate } 3390Sstevel@tonic-gate 3400Sstevel@tonic-gate if (ltlend) { 3410Sstevel@tonic-gate if (sz == 1) { 3420Sstevel@tonic-gate if (xcopyout_little(&data.c[7], addr, 3430Sstevel@tonic-gate (size_t)sz) != 0) 3440Sstevel@tonic-gate goto badret; 3450Sstevel@tonic-gate } else if (sz == 2) { 3460Sstevel@tonic-gate if (xcopyout_little(&data.s[3], addr, 3470Sstevel@tonic-gate (size_t)sz) != 0) 3480Sstevel@tonic-gate goto badret; 3490Sstevel@tonic-gate } else if (sz == 4) { 3500Sstevel@tonic-gate if (xcopyout_little(&data.i[1], addr, 3510Sstevel@tonic-gate (size_t)sz) != 0) 3520Sstevel@tonic-gate goto badret; 3530Sstevel@tonic-gate } else { 3540Sstevel@tonic-gate if (xcopyout_little(&data.l[0], addr, 3550Sstevel@tonic-gate (size_t)sz) != 0) 3560Sstevel@tonic-gate goto badret; 3570Sstevel@tonic-gate } 3580Sstevel@tonic-gate } else { 3590Sstevel@tonic-gate if (sz == 1) { 3600Sstevel@tonic-gate if (copyout(&data.c[7], addr, (size_t)sz) == -1) 3610Sstevel@tonic-gate goto badret; 3620Sstevel@tonic-gate } else if (sz == 2) { 3630Sstevel@tonic-gate if (copyout(&data.s[3], addr, (size_t)sz) == -1) 3640Sstevel@tonic-gate goto badret; 3650Sstevel@tonic-gate } else if (sz == 4) { 3660Sstevel@tonic-gate if (copyout(&data.i[1], addr, (size_t)sz) == -1) 3670Sstevel@tonic-gate goto badret; 3680Sstevel@tonic-gate } else { 3690Sstevel@tonic-gate if (copyout(&data.l[0], addr, (size_t)sz) == -1) 3700Sstevel@tonic-gate goto badret; 3710Sstevel@tonic-gate } 3720Sstevel@tonic-gate } 3730Sstevel@tonic-gate } else { /* load */ 3740Sstevel@tonic-gate if (sz == 1) { 3750Sstevel@tonic-gate if (ltlend) { 3760Sstevel@tonic-gate if (xcopyin_little(addr, &data.c[7], 3770Sstevel@tonic-gate (size_t)sz) != 0) { 3780Sstevel@tonic-gate if (nf) 3790Sstevel@tonic-gate data.c[7] = 0; 3800Sstevel@tonic-gate else 3810Sstevel@tonic-gate goto badret; 3820Sstevel@tonic-gate } 3830Sstevel@tonic-gate } else { 3840Sstevel@tonic-gate if (copyin(addr, &data.c[7], 3850Sstevel@tonic-gate (size_t)sz) == -1) { 3860Sstevel@tonic-gate if (nf) 3870Sstevel@tonic-gate data.c[7] = 0; 3880Sstevel@tonic-gate else 3890Sstevel@tonic-gate goto badret; 3900Sstevel@tonic-gate } 3910Sstevel@tonic-gate } 3920Sstevel@tonic-gate /* if signed and the sign bit is set extend it */ 3930Sstevel@tonic-gate if (((inst >> 22) & 1) && ((data.c[7] >> 7) & 1)) { 3940Sstevel@tonic-gate data.i[0] = (uint_t)-1; /* extend sign bit */ 3950Sstevel@tonic-gate data.s[2] = (ushort_t)-1; 3960Sstevel@tonic-gate data.c[6] = (uchar_t)-1; 3970Sstevel@tonic-gate } else { 3980Sstevel@tonic-gate data.i[0] = 0; /* clear upper 32+24 bits */ 3990Sstevel@tonic-gate data.s[2] = 0; 4000Sstevel@tonic-gate data.c[6] = 0; 4010Sstevel@tonic-gate } 4020Sstevel@tonic-gate } else if (sz == 2) { 4030Sstevel@tonic-gate if (ltlend) { 4040Sstevel@tonic-gate if (xcopyin_little(addr, &data.s[3], 4050Sstevel@tonic-gate (size_t)sz) != 0) { 4060Sstevel@tonic-gate if (nf) 4070Sstevel@tonic-gate data.s[3] = 0; 4080Sstevel@tonic-gate else 4090Sstevel@tonic-gate goto badret; 4100Sstevel@tonic-gate } 4110Sstevel@tonic-gate } else { 4120Sstevel@tonic-gate if (copyin(addr, &data.s[3], 4130Sstevel@tonic-gate (size_t)sz) == -1) { 4140Sstevel@tonic-gate if (nf) 4150Sstevel@tonic-gate data.s[3] = 0; 4160Sstevel@tonic-gate else 4170Sstevel@tonic-gate goto badret; 4180Sstevel@tonic-gate } 4190Sstevel@tonic-gate } 4200Sstevel@tonic-gate /* if signed and the sign bit is set extend it */ 4210Sstevel@tonic-gate if (((inst >> 22) & 1) && ((data.s[3] >> 15) & 1)) { 4220Sstevel@tonic-gate data.i[0] = (uint_t)-1; /* extend sign bit */ 4230Sstevel@tonic-gate data.s[2] = (ushort_t)-1; 4240Sstevel@tonic-gate } else { 4250Sstevel@tonic-gate data.i[0] = 0; /* clear upper 32+16 bits */ 4260Sstevel@tonic-gate data.s[2] = 0; 4270Sstevel@tonic-gate } 4280Sstevel@tonic-gate } else if (sz == 4) { 4290Sstevel@tonic-gate if (ltlend) { 4300Sstevel@tonic-gate if (xcopyin_little(addr, &data.i[1], 4310Sstevel@tonic-gate (size_t)sz) != 0) { 4320Sstevel@tonic-gate if (!nf) 4330Sstevel@tonic-gate goto badret; 4340Sstevel@tonic-gate data.i[1] = 0; 4350Sstevel@tonic-gate } 4360Sstevel@tonic-gate } else { 4370Sstevel@tonic-gate if (copyin(addr, &data.i[1], 4380Sstevel@tonic-gate (size_t)sz) == -1) { 4390Sstevel@tonic-gate if (!nf) 4400Sstevel@tonic-gate goto badret; 4410Sstevel@tonic-gate data.i[1] = 0; 4420Sstevel@tonic-gate } 4430Sstevel@tonic-gate } 4440Sstevel@tonic-gate /* if signed and the sign bit is set extend it */ 4450Sstevel@tonic-gate if (((inst >> 22) & 1) && ((data.i[1] >> 31) & 1)) { 4460Sstevel@tonic-gate data.i[0] = (uint_t)-1; /* extend sign bit */ 4470Sstevel@tonic-gate } else { 4480Sstevel@tonic-gate data.i[0] = 0; /* clear upper 32 bits */ 4490Sstevel@tonic-gate } 4500Sstevel@tonic-gate } else { 4510Sstevel@tonic-gate if (ltlend) { 4520Sstevel@tonic-gate if (xcopyin_little(addr, &data.l[0], 4530Sstevel@tonic-gate (size_t)sz) != 0) { 4540Sstevel@tonic-gate if (!nf) 4550Sstevel@tonic-gate goto badret; 4560Sstevel@tonic-gate data.l[0] = 0; 4570Sstevel@tonic-gate } 4580Sstevel@tonic-gate } else { 4590Sstevel@tonic-gate if (copyin(addr, &data.l[0], 4600Sstevel@tonic-gate (size_t)sz) == -1) { 4610Sstevel@tonic-gate if (!nf) 4620Sstevel@tonic-gate goto badret; 4630Sstevel@tonic-gate data.l[0] = 0; 4640Sstevel@tonic-gate } 4650Sstevel@tonic-gate } 4660Sstevel@tonic-gate } 4670Sstevel@tonic-gate 4680Sstevel@tonic-gate if (aligndebug) { 4690Sstevel@tonic-gate if (sz == 16) { 4700Sstevel@tonic-gate printf("data %x %x %x %x\n", 4710Sstevel@tonic-gate data.i[0], data.i[1], data.i[2], data.c[3]); 4720Sstevel@tonic-gate } else { 4730Sstevel@tonic-gate printf("data %x %x %x %x %x %x %x %x\n", 4740Sstevel@tonic-gate data.c[0], data.c[1], data.c[2], data.c[3], 4750Sstevel@tonic-gate data.c[4], data.c[5], data.c[6], data.c[7]); 4760Sstevel@tonic-gate } 4770Sstevel@tonic-gate } 4780Sstevel@tonic-gate 4790Sstevel@tonic-gate if (floatflg) { /* if fpu_exists write fpu reg */ 4800Sstevel@tonic-gate klwp_id_t lwp = ttolwp(curthread); 4810Sstevel@tonic-gate kfpu_t *fp = lwptofpu(lwp); 4820Sstevel@tonic-gate /* Ensure fp has been enabled */ 4830Sstevel@tonic-gate if (fpu_exists) { 4840Sstevel@tonic-gate if (!(_fp_read_fprs() & FPRS_FEF)) 4850Sstevel@tonic-gate fp_enable(); 4860Sstevel@tonic-gate } else { 4870Sstevel@tonic-gate if (!fp->fpu_en) 4880Sstevel@tonic-gate fp_enable(); 4890Sstevel@tonic-gate } 4900Sstevel@tonic-gate /* if fpu_exists read fpu reg */ 4910Sstevel@tonic-gate if (fpu_exists) { 4920Sstevel@tonic-gate if (fsrflg) { 4930Sstevel@tonic-gate _fp_write_pfsr(&data.l[0]); 4940Sstevel@tonic-gate } else { 4950Sstevel@tonic-gate if (sz == 4) 4960Sstevel@tonic-gate _fp_write_pfreg( 4970Sstevel@tonic-gate (unsigned *)&data.i[1], rd); 4980Sstevel@tonic-gate if (sz >= 8) 4990Sstevel@tonic-gate _fp_write_pdreg( 5000Sstevel@tonic-gate &data.l[0], rd); 5010Sstevel@tonic-gate if (sz == 16) 5020Sstevel@tonic-gate _fp_write_pdreg( 5030Sstevel@tonic-gate &data.l[1], rd+1); 5040Sstevel@tonic-gate } 5050Sstevel@tonic-gate } else { 5060Sstevel@tonic-gate if (fsrflg) { 5070Sstevel@tonic-gate fp->fpu_fsr = data.l[0]; 5080Sstevel@tonic-gate } else { 5090Sstevel@tonic-gate if (sz == 4) 5100Sstevel@tonic-gate fp->fpu_fr.fpu_regs[rd] = 5110Sstevel@tonic-gate (unsigned)data.i[1]; 5120Sstevel@tonic-gate if (sz >= 8) 5130Sstevel@tonic-gate fp->fpu_fr.fpu_dregs[rd] = 5140Sstevel@tonic-gate data.l[0]; 5150Sstevel@tonic-gate if (sz == 16) 5160Sstevel@tonic-gate fp->fpu_fr.fpu_dregs[rd+1] = 5170Sstevel@tonic-gate data.l[1]; 5180Sstevel@tonic-gate } 5190Sstevel@tonic-gate } 5200Sstevel@tonic-gate } else { 5210Sstevel@tonic-gate if (lddstdflg) { /* split the data */ 522*518Swsm if (ltlend) { 523*518Swsm /* 524*518Swsm * For LDD, each 32-bit word is byte- 525*518Swsm * swapped individually. We didn't 526*518Swsm * do that above, but this will give 527*518Swsm * us the desired result. 528*518Swsm */ 529*518Swsm data.i[3] = data.i[0]; 530*518Swsm } else { 531*518Swsm data.i[3] = data.i[1]; 532*518Swsm data.i[1] = data.i[0]; 533*518Swsm } 534*518Swsm data.i[0] = 0; 5350Sstevel@tonic-gate data.i[2] = 0; 5360Sstevel@tonic-gate if (putreg(&data.l[0], rp, rd, badaddr) == -1) 5370Sstevel@tonic-gate goto badret; 5380Sstevel@tonic-gate if (putreg(&data.l[1], rp, rd+1, badaddr) == -1) 5390Sstevel@tonic-gate goto badret; 5400Sstevel@tonic-gate } else { 5410Sstevel@tonic-gate if (putreg(&data.l[0], rp, rd, badaddr) == -1) 5420Sstevel@tonic-gate goto badret; 5430Sstevel@tonic-gate } 5440Sstevel@tonic-gate } 5450Sstevel@tonic-gate } 5460Sstevel@tonic-gate return (SIMU_SUCCESS); 5470Sstevel@tonic-gate badret: 5480Sstevel@tonic-gate return (SIMU_FAULT); 5490Sstevel@tonic-gate } 5500Sstevel@tonic-gate 551*518Swsm 552*518Swsm int 553*518Swsm simulate_lddstd(struct regs *rp, caddr_t *badaddr) 554*518Swsm { 555*518Swsm uint_t inst, op3, asi = 0; 556*518Swsm uint_t rd, rs1, rs2; 557*518Swsm int rv = 0; 558*518Swsm int nf = 0, ltlend = 0, usermode; 559*518Swsm int immflg; 560*518Swsm uint64_t reven; 561*518Swsm uint64_t rodd; 562*518Swsm caddr_t addr; 563*518Swsm uint64_t val; 564*518Swsm uint64_t data; 565*518Swsm 566*518Swsm usermode = USERMODE(rp->r_tstate); 567*518Swsm 568*518Swsm if (usermode) 569*518Swsm inst = fetch_user_instr((caddr_t)rp->r_pc); 570*518Swsm else 571*518Swsm inst = *(uint_t *)rp->r_pc; 572*518Swsm 573*518Swsm op3 = (inst >> 19) & 0x3f; 574*518Swsm rd = (inst >> 25) & 0x1f; 575*518Swsm rs1 = (inst >> 14) & 0x1f; 576*518Swsm rs2 = inst & 0x1f; 577*518Swsm immflg = (inst >> 13) & 1; 578*518Swsm 579*518Swsm if (USERMODE(rp->r_tstate)) 580*518Swsm (void) flush_user_windows_to_stack(NULL); 581*518Swsm else 582*518Swsm flush_windows(); 583*518Swsm 584*518Swsm if ((op3 >> 4) & 1) { /* is this LDDA/STDA? */ 585*518Swsm if (immflg) { 586*518Swsm asi = (uint_t)(rp->r_tstate >> TSTATE_ASI_SHIFT) & 587*518Swsm TSTATE_ASI_MASK; 588*518Swsm } else { 589*518Swsm asi = (inst >> 5) & 0xff; 590*518Swsm } 591*518Swsm switch (asi) { 592*518Swsm case ASI_P: 593*518Swsm case ASI_S: 594*518Swsm break; 595*518Swsm case ASI_PNF: 596*518Swsm case ASI_SNF: 597*518Swsm nf = 1; 598*518Swsm break; 599*518Swsm case ASI_PL: 600*518Swsm case ASI_SL: 601*518Swsm ltlend = 1; 602*518Swsm break; 603*518Swsm case ASI_PNFL: 604*518Swsm case ASI_SNFL: 605*518Swsm ltlend = 1; 606*518Swsm nf = 1; 607*518Swsm break; 608*518Swsm case ASI_AIUP: 609*518Swsm case ASI_AIUS: 610*518Swsm usermode = 1; 611*518Swsm break; 612*518Swsm case ASI_AIUPL: 613*518Swsm case ASI_AIUSL: 614*518Swsm usermode = 1; 615*518Swsm ltlend = 1; 616*518Swsm break; 617*518Swsm default: 618*518Swsm return (SIMU_ILLEGAL); 619*518Swsm } 620*518Swsm } 621*518Swsm 622*518Swsm if (getreg(rp, rs1, &val, badaddr)) 623*518Swsm return (SIMU_FAULT); 624*518Swsm addr = (caddr_t)val; /* convert to 32/64 bit address */ 625*518Swsm 626*518Swsm /* check immediate bit and use immediate field or reg (rs2) */ 627*518Swsm if (immflg) { 628*518Swsm int imm; 629*518Swsm imm = inst & 0x1fff; /* mask out immediate field */ 630*518Swsm imm <<= 19; /* sign extend it */ 631*518Swsm imm >>= 19; 632*518Swsm addr += imm; /* compute address */ 633*518Swsm } else { 634*518Swsm if (getreg(rp, rs2, &val, badaddr)) 635*518Swsm return (SIMU_FAULT); 636*518Swsm addr += val; 637*518Swsm } 638*518Swsm 639*518Swsm /* 640*518Swsm * T_UNIMP_LDD and T_UNIMP_STD are higher priority than 641*518Swsm * T_ALIGNMENT. So we have to make sure that the address is 642*518Swsm * kosher before trying to use it, because the hardware hasn't 643*518Swsm * checked it for us yet. 644*518Swsm */ 645*518Swsm if (((uintptr_t)addr & 0x7) != 0) { 646*518Swsm if (curproc->p_fixalignment) 647*518Swsm return (do_unaligned(rp, badaddr)); 648*518Swsm else 649*518Swsm return (SIMU_UNALIGN); 650*518Swsm } 651*518Swsm 652*518Swsm /* 653*518Swsm * If this is a 32-bit program, chop the address accordingly. 654*518Swsm */ 655*518Swsm if (curproc->p_model == DATAMODEL_ILP32 && usermode) 656*518Swsm addr = (caddr_t)(caddr32_t)addr; 657*518Swsm 658*518Swsm if ((inst >> 21) & 1) { /* store */ 659*518Swsm if (getreg(rp, rd, &reven, badaddr)) 660*518Swsm return (SIMU_FAULT); 661*518Swsm if (getreg(rp, rd+1, &rodd, badaddr)) 662*518Swsm return (SIMU_FAULT); 663*518Swsm if (ltlend) { 664*518Swsm reven = BSWAP_32(reven); 665*518Swsm rodd = BSWAP_32(rodd); 666*518Swsm } 667*518Swsm data = (reven << 32) | rodd; 668*518Swsm if (usermode) { 669*518Swsm if (suword64_nowatch(addr, data) == -1) 670*518Swsm return (SIMU_FAULT); 671*518Swsm } else { 672*518Swsm *(uint64_t *)addr = data; 673*518Swsm } 674*518Swsm } else { /* load */ 675*518Swsm if (usermode) { 676*518Swsm if (fuword64_nowatch(addr, &data)) { 677*518Swsm if (nf) 678*518Swsm data = 0; 679*518Swsm else 680*518Swsm return (SIMU_FAULT); 681*518Swsm } 682*518Swsm } else 683*518Swsm data = *(uint64_t *)addr; 684*518Swsm 685*518Swsm reven = (data >> 32); 686*518Swsm rodd = (uint64_t)(uint32_t)data; 687*518Swsm if (ltlend) { 688*518Swsm reven = BSWAP_32(reven); 689*518Swsm rodd = BSWAP_32(rodd); 690*518Swsm } 691*518Swsm 692*518Swsm if (putreg(&reven, rp, rd, badaddr) == -1) 693*518Swsm return (SIMU_FAULT); 694*518Swsm if (putreg(&rodd, rp, rd+1, badaddr) == -1) 695*518Swsm return (SIMU_FAULT); 696*518Swsm } 697*518Swsm return (SIMU_SUCCESS); 698*518Swsm } 699*518Swsm 700*518Swsm 7010Sstevel@tonic-gate /* 7020Sstevel@tonic-gate * simulate popc 7030Sstevel@tonic-gate */ 7040Sstevel@tonic-gate static int 7050Sstevel@tonic-gate simulate_popc(struct regs *rp, caddr_t *badaddr, uint_t inst) 7060Sstevel@tonic-gate { 7070Sstevel@tonic-gate uint_t rd, rs2, rs1; 7080Sstevel@tonic-gate uint_t immflg; 7090Sstevel@tonic-gate uint64_t val, cnt = 0; 7100Sstevel@tonic-gate 7110Sstevel@tonic-gate rd = (inst >> 25) & 0x1f; 7120Sstevel@tonic-gate rs1 = (inst >> 14) & 0x1f; 7130Sstevel@tonic-gate rs2 = inst & 0x1f; 7140Sstevel@tonic-gate immflg = (inst >> 13) & 1; 7150Sstevel@tonic-gate 7160Sstevel@tonic-gate if (rs1 > 0) 7170Sstevel@tonic-gate return (SIMU_ILLEGAL); 7180Sstevel@tonic-gate 7190Sstevel@tonic-gate (void) flush_user_windows_to_stack(NULL); 7200Sstevel@tonic-gate 7210Sstevel@tonic-gate /* check immediate bit and use immediate field or reg (rs2) */ 7220Sstevel@tonic-gate if (immflg) { 7230Sstevel@tonic-gate int64_t imm; 7240Sstevel@tonic-gate imm = inst & 0x1fff; /* mask out immediate field */ 7250Sstevel@tonic-gate imm <<= 51; /* sign extend it */ 7260Sstevel@tonic-gate imm >>= 51; 7270Sstevel@tonic-gate if (imm != 0) { 7280Sstevel@tonic-gate for (cnt = 0; imm != 0; imm &= imm-1) 7290Sstevel@tonic-gate cnt++; 7300Sstevel@tonic-gate } 7310Sstevel@tonic-gate } else { 7320Sstevel@tonic-gate if (getreg(rp, rs2, &val, badaddr)) 7330Sstevel@tonic-gate return (SIMU_FAULT); 7340Sstevel@tonic-gate if (val != 0) { 7350Sstevel@tonic-gate for (cnt = 0; val != 0; val &= val-1) 7360Sstevel@tonic-gate cnt++; 7370Sstevel@tonic-gate } 7380Sstevel@tonic-gate } 7390Sstevel@tonic-gate 7400Sstevel@tonic-gate if (putreg(&cnt, rp, rd, badaddr) == -1) 7410Sstevel@tonic-gate return (SIMU_FAULT); 7420Sstevel@tonic-gate 7430Sstevel@tonic-gate return (SIMU_SUCCESS); 7440Sstevel@tonic-gate } 7450Sstevel@tonic-gate 7460Sstevel@tonic-gate /* 7470Sstevel@tonic-gate * simulate unimplemented instructions (popc, ldqf{a}, stqf{a}) 7480Sstevel@tonic-gate */ 7490Sstevel@tonic-gate int 7500Sstevel@tonic-gate simulate_unimp(struct regs *rp, caddr_t *badaddr) 7510Sstevel@tonic-gate { 7520Sstevel@tonic-gate uint_t inst, optype, op3, asi; 7530Sstevel@tonic-gate uint_t rs1, rd; 7540Sstevel@tonic-gate uint_t ignor, i; 7550Sstevel@tonic-gate machpcb_t *mpcb = lwptompcb(ttolwp(curthread)); 7560Sstevel@tonic-gate int nomatch = 0; 7570Sstevel@tonic-gate caddr_t addr = (caddr_t)rp->r_pc; 7580Sstevel@tonic-gate struct as *as; 7590Sstevel@tonic-gate caddr_t ka; 7600Sstevel@tonic-gate pfn_t pfnum; 7610Sstevel@tonic-gate page_t *pp; 7620Sstevel@tonic-gate proc_t *p = ttoproc(curthread); 7630Sstevel@tonic-gate struct seg *mapseg; 7640Sstevel@tonic-gate struct segvn_data *svd; 7650Sstevel@tonic-gate 7660Sstevel@tonic-gate ASSERT(USERMODE(rp->r_tstate)); 7670Sstevel@tonic-gate inst = fetch_user_instr(addr); 7680Sstevel@tonic-gate if (inst == (uint_t)-1) { 7690Sstevel@tonic-gate mpcb->mpcb_illexcaddr = addr; 7700Sstevel@tonic-gate mpcb->mpcb_illexcinsn = (uint32_t)-1; 7710Sstevel@tonic-gate return (SIMU_ILLEGAL); 7720Sstevel@tonic-gate } 7730Sstevel@tonic-gate 7740Sstevel@tonic-gate /* 7750Sstevel@tonic-gate * When fixing dirty v8 instructions there's a race if two processors 7760Sstevel@tonic-gate * are executing the dirty executable at the same time. If one 7770Sstevel@tonic-gate * cleans the instruction as the other is executing it the second 7780Sstevel@tonic-gate * processor will see a clean instruction when it comes through this 7790Sstevel@tonic-gate * code and will return SIMU_ILLEGAL. To work around the race 7800Sstevel@tonic-gate * this code will keep track of the last illegal instruction seen 7810Sstevel@tonic-gate * by each lwp and will only take action if the illegal instruction 7820Sstevel@tonic-gate * is repeatable. 7830Sstevel@tonic-gate */ 7840Sstevel@tonic-gate if (addr != mpcb->mpcb_illexcaddr || 7850Sstevel@tonic-gate inst != mpcb->mpcb_illexcinsn) 7860Sstevel@tonic-gate nomatch = 1; 7870Sstevel@tonic-gate mpcb->mpcb_illexcaddr = addr; 7880Sstevel@tonic-gate mpcb->mpcb_illexcinsn = inst; 7890Sstevel@tonic-gate 7900Sstevel@tonic-gate /* instruction fields */ 7910Sstevel@tonic-gate i = (inst >> 13) & 0x1; 7920Sstevel@tonic-gate rd = (inst >> 25) & 0x1f; 7930Sstevel@tonic-gate optype = (inst >> 30) & 0x3; 7940Sstevel@tonic-gate op3 = (inst >> 19) & 0x3f; 7950Sstevel@tonic-gate ignor = (inst >> 5) & 0xff; 7960Sstevel@tonic-gate if (IS_IBIT_SET(inst)) { 7970Sstevel@tonic-gate asi = (uint32_t)((rp->r_tstate >> TSTATE_ASI_SHIFT) & 7980Sstevel@tonic-gate TSTATE_ASI_MASK); 7990Sstevel@tonic-gate } else { 8000Sstevel@tonic-gate asi = ignor; 8010Sstevel@tonic-gate } 8020Sstevel@tonic-gate 8030Sstevel@tonic-gate if (IS_VIS1(optype, op3) || 8040Sstevel@tonic-gate IS_PARTIAL_OR_SHORT_FLOAT_LD_ST(optype, op3, asi)) { 8050Sstevel@tonic-gate klwp_t *lwp = ttolwp(curthread); 8060Sstevel@tonic-gate kfpu_t *fp = lwptofpu(lwp); 8070Sstevel@tonic-gate if (fpu_exists) { 8080Sstevel@tonic-gate if (!(_fp_read_fprs() & FPRS_FEF)) 8090Sstevel@tonic-gate fp_enable(); 8100Sstevel@tonic-gate _fp_read_pfsr(&fp->fpu_fsr); 8110Sstevel@tonic-gate } else { 8120Sstevel@tonic-gate if (!fp->fpu_en) 8130Sstevel@tonic-gate fp_enable(); 8140Sstevel@tonic-gate } 8150Sstevel@tonic-gate fp_precise(rp); 8160Sstevel@tonic-gate return (SIMU_RETRY); 8170Sstevel@tonic-gate } 8180Sstevel@tonic-gate 8190Sstevel@tonic-gate if (optype == 2 && op3 == IOP_V8_POPC) { 8200Sstevel@tonic-gate return (simulate_popc(rp, badaddr, inst)); 8210Sstevel@tonic-gate } else if (optype == 3 && op3 == IOP_V8_POPC) { 8220Sstevel@tonic-gate return (SIMU_ILLEGAL); 8230Sstevel@tonic-gate } 8240Sstevel@tonic-gate 8250Sstevel@tonic-gate if (optype == OP_V8_LDSTR) { 8260Sstevel@tonic-gate if (op3 == IOP_V8_LDQF || op3 == IOP_V8_LDQFA || 8270Sstevel@tonic-gate op3 == IOP_V8_STQF || op3 == IOP_V8_STQFA) 8280Sstevel@tonic-gate return (do_unaligned(rp, badaddr)); 8290Sstevel@tonic-gate } 8300Sstevel@tonic-gate 8310Sstevel@tonic-gate if (nomatch) 8320Sstevel@tonic-gate return (SIMU_RETRY); 8330Sstevel@tonic-gate 8340Sstevel@tonic-gate /* 8350Sstevel@tonic-gate * The rest of the code handles v8 binaries with instructions 8360Sstevel@tonic-gate * that have dirty (non-zero) bits in reserved or 'ignored' 8370Sstevel@tonic-gate * fields; these will cause core dumps on v9 machines. 8380Sstevel@tonic-gate * 8390Sstevel@tonic-gate * We only clean dirty instructions in 32-bit programs (ie, v8) 8400Sstevel@tonic-gate * running on SPARCv9 processors. True v9 programs are forced 8410Sstevel@tonic-gate * to use the instruction set as intended. 8420Sstevel@tonic-gate */ 8430Sstevel@tonic-gate if (lwp_getdatamodel(curthread->t_lwp) != DATAMODEL_ILP32) 8440Sstevel@tonic-gate return (SIMU_ILLEGAL); 8450Sstevel@tonic-gate switch (optype) { 8460Sstevel@tonic-gate case OP_V8_BRANCH: 8470Sstevel@tonic-gate case OP_V8_CALL: 8480Sstevel@tonic-gate return (SIMU_ILLEGAL); /* these don't have ignored fields */ 8490Sstevel@tonic-gate /*NOTREACHED*/ 8500Sstevel@tonic-gate case OP_V8_ARITH: 8510Sstevel@tonic-gate switch (op3) { 8520Sstevel@tonic-gate case IOP_V8_RETT: 8530Sstevel@tonic-gate if (rd == 0 && !(i == 0 && ignor)) 8540Sstevel@tonic-gate return (SIMU_ILLEGAL); 8550Sstevel@tonic-gate if (rd) 8560Sstevel@tonic-gate inst &= ~(0x1f << 25); 8570Sstevel@tonic-gate if (i == 0 && ignor) 8580Sstevel@tonic-gate inst &= ~(0xff << 5); 8590Sstevel@tonic-gate break; 8600Sstevel@tonic-gate case IOP_V8_TCC: 8610Sstevel@tonic-gate if (i == 0 && ignor != 0) { 8620Sstevel@tonic-gate inst &= ~(0xff << 5); 8630Sstevel@tonic-gate } else if (i == 1 && (((inst >> 7) & 0x3f) != 0)) { 8640Sstevel@tonic-gate inst &= ~(0x3f << 7); 8650Sstevel@tonic-gate } else { 8660Sstevel@tonic-gate return (SIMU_ILLEGAL); 8670Sstevel@tonic-gate } 8680Sstevel@tonic-gate break; 8690Sstevel@tonic-gate case IOP_V8_JMPL: 8700Sstevel@tonic-gate case IOP_V8_RESTORE: 8710Sstevel@tonic-gate case IOP_V8_SAVE: 8720Sstevel@tonic-gate if ((op3 == IOP_V8_RETT && rd) || 8730Sstevel@tonic-gate (i == 0 && ignor)) { 8740Sstevel@tonic-gate inst &= ~(0xff << 5); 8750Sstevel@tonic-gate } else { 8760Sstevel@tonic-gate return (SIMU_ILLEGAL); 8770Sstevel@tonic-gate } 8780Sstevel@tonic-gate break; 8790Sstevel@tonic-gate case IOP_V8_FCMP: 8800Sstevel@tonic-gate if (rd == 0) 8810Sstevel@tonic-gate return (SIMU_ILLEGAL); 8820Sstevel@tonic-gate inst &= ~(0x1f << 25); 8830Sstevel@tonic-gate break; 8840Sstevel@tonic-gate case IOP_V8_RDASR: 8850Sstevel@tonic-gate rs1 = ((inst >> 14) & 0x1f); 8860Sstevel@tonic-gate if (rs1 == 1 || (rs1 >= 7 && rs1 <= 14)) { 8870Sstevel@tonic-gate /* 8880Sstevel@tonic-gate * The instruction specifies an invalid 8890Sstevel@tonic-gate * state register - better bail out than 8900Sstevel@tonic-gate * "fix" it when we're not sure what was 8910Sstevel@tonic-gate * intended. 8920Sstevel@tonic-gate */ 8930Sstevel@tonic-gate return (SIMU_ILLEGAL); 8940Sstevel@tonic-gate } 8950Sstevel@tonic-gate /* 8960Sstevel@tonic-gate * Note: this case includes the 'stbar' 8970Sstevel@tonic-gate * instruction (rs1 == 15 && i == 0). 8980Sstevel@tonic-gate */ 8990Sstevel@tonic-gate if ((ignor = (inst & 0x3fff)) != 0) 9000Sstevel@tonic-gate inst &= ~(0x3fff); 9010Sstevel@tonic-gate break; 9020Sstevel@tonic-gate case IOP_V8_SRA: 9030Sstevel@tonic-gate case IOP_V8_SRL: 9040Sstevel@tonic-gate case IOP_V8_SLL: 9050Sstevel@tonic-gate if (ignor == 0) 9060Sstevel@tonic-gate return (SIMU_ILLEGAL); 9070Sstevel@tonic-gate inst &= ~(0xff << 5); 9080Sstevel@tonic-gate break; 9090Sstevel@tonic-gate case IOP_V8_ADD: 9100Sstevel@tonic-gate case IOP_V8_AND: 9110Sstevel@tonic-gate case IOP_V8_OR: 9120Sstevel@tonic-gate case IOP_V8_XOR: 9130Sstevel@tonic-gate case IOP_V8_SUB: 9140Sstevel@tonic-gate case IOP_V8_ANDN: 9150Sstevel@tonic-gate case IOP_V8_ORN: 9160Sstevel@tonic-gate case IOP_V8_XNOR: 9170Sstevel@tonic-gate case IOP_V8_ADDC: 9180Sstevel@tonic-gate case IOP_V8_UMUL: 9190Sstevel@tonic-gate case IOP_V8_SMUL: 9200Sstevel@tonic-gate case IOP_V8_SUBC: 9210Sstevel@tonic-gate case IOP_V8_UDIV: 9220Sstevel@tonic-gate case IOP_V8_SDIV: 9230Sstevel@tonic-gate case IOP_V8_ADDcc: 9240Sstevel@tonic-gate case IOP_V8_ANDcc: 9250Sstevel@tonic-gate case IOP_V8_ORcc: 9260Sstevel@tonic-gate case IOP_V8_XORcc: 9270Sstevel@tonic-gate case IOP_V8_SUBcc: 9280Sstevel@tonic-gate case IOP_V8_ANDNcc: 9290Sstevel@tonic-gate case IOP_V8_ORNcc: 9300Sstevel@tonic-gate case IOP_V8_XNORcc: 9310Sstevel@tonic-gate case IOP_V8_ADDCcc: 9320Sstevel@tonic-gate case IOP_V8_UMULcc: 9330Sstevel@tonic-gate case IOP_V8_SMULcc: 9340Sstevel@tonic-gate case IOP_V8_SUBCcc: 9350Sstevel@tonic-gate case IOP_V8_UDIVcc: 9360Sstevel@tonic-gate case IOP_V8_SDIVcc: 9370Sstevel@tonic-gate case IOP_V8_TADDcc: 9380Sstevel@tonic-gate case IOP_V8_TSUBcc: 9390Sstevel@tonic-gate case IOP_V8_TADDccTV: 9400Sstevel@tonic-gate case IOP_V8_TSUBccTV: 9410Sstevel@tonic-gate case IOP_V8_MULScc: 9420Sstevel@tonic-gate case IOP_V8_WRASR: 9430Sstevel@tonic-gate case IOP_V8_FLUSH: 9440Sstevel@tonic-gate if (i != 0 || ignor == 0) 9450Sstevel@tonic-gate return (SIMU_ILLEGAL); 9460Sstevel@tonic-gate inst &= ~(0xff << 5); 9470Sstevel@tonic-gate break; 9480Sstevel@tonic-gate default: 9490Sstevel@tonic-gate return (SIMU_ILLEGAL); 9500Sstevel@tonic-gate } 9510Sstevel@tonic-gate break; 9520Sstevel@tonic-gate case OP_V8_LDSTR: 9530Sstevel@tonic-gate switch (op3) { 9540Sstevel@tonic-gate case IOP_V8_STFSR: 9550Sstevel@tonic-gate case IOP_V8_LDFSR: 9560Sstevel@tonic-gate if (rd == 0 && !(i == 0 && ignor)) 9570Sstevel@tonic-gate return (SIMU_ILLEGAL); 9580Sstevel@tonic-gate if (rd) 9590Sstevel@tonic-gate inst &= ~(0x1f << 25); 9600Sstevel@tonic-gate if (i == 0 && ignor) 9610Sstevel@tonic-gate inst &= ~(0xff << 5); 9620Sstevel@tonic-gate break; 9630Sstevel@tonic-gate default: 9640Sstevel@tonic-gate if (optype == OP_V8_LDSTR && !IS_LDST_ALT(op3) && 9650Sstevel@tonic-gate i == 0 && ignor) 9660Sstevel@tonic-gate inst &= ~(0xff << 5); 9670Sstevel@tonic-gate else 9680Sstevel@tonic-gate return (SIMU_ILLEGAL); 9690Sstevel@tonic-gate break; 9700Sstevel@tonic-gate } 9710Sstevel@tonic-gate break; 9720Sstevel@tonic-gate default: 9730Sstevel@tonic-gate return (SIMU_ILLEGAL); 9740Sstevel@tonic-gate } 9750Sstevel@tonic-gate 9760Sstevel@tonic-gate as = p->p_as; 9770Sstevel@tonic-gate 9780Sstevel@tonic-gate AS_LOCK_ENTER(as, &as->a_lock, RW_READER); 9790Sstevel@tonic-gate mapseg = as_findseg(as, (caddr_t)rp->r_pc, 0); 9800Sstevel@tonic-gate ASSERT(mapseg != NULL); 9810Sstevel@tonic-gate svd = (struct segvn_data *)mapseg->s_data; 9820Sstevel@tonic-gate 9830Sstevel@tonic-gate /* 9840Sstevel@tonic-gate * We only create COW page for MAP_PRIVATE mappings. 9850Sstevel@tonic-gate */ 9860Sstevel@tonic-gate SEGVN_LOCK_ENTER(as, &svd->lock, RW_READER); 9870Sstevel@tonic-gate if ((svd->type & MAP_TYPE) & MAP_SHARED) { 9880Sstevel@tonic-gate SEGVN_LOCK_EXIT(as, &svd->lock); 9890Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 9900Sstevel@tonic-gate return (SIMU_ILLEGAL); 9910Sstevel@tonic-gate } 9920Sstevel@tonic-gate SEGVN_LOCK_EXIT(as, &svd->lock); 9930Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 9940Sstevel@tonic-gate 9950Sstevel@tonic-gate /* 9960Sstevel@tonic-gate * A "flush" instruction using the user PC's vaddr will not work 9970Sstevel@tonic-gate * here, at least on Spitfire. Instead we create a temporary kernel 9980Sstevel@tonic-gate * mapping to the user's text page, then modify and flush that. 9990Sstevel@tonic-gate * Break COW by locking user page. 10000Sstevel@tonic-gate */ 10010Sstevel@tonic-gate if (as_fault(as->a_hat, as, (caddr_t)(rp->r_pc & PAGEMASK), PAGESIZE, 10020Sstevel@tonic-gate F_SOFTLOCK, S_READ)) 10030Sstevel@tonic-gate return (SIMU_FAULT); 10040Sstevel@tonic-gate 10050Sstevel@tonic-gate AS_LOCK_ENTER(as, &as->a_lock, RW_READER); 10060Sstevel@tonic-gate pfnum = hat_getpfnum(as->a_hat, (caddr_t)rp->r_pc); 10070Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 10080Sstevel@tonic-gate if (pf_is_memory(pfnum)) { 10090Sstevel@tonic-gate pp = page_numtopp_nolock(pfnum); 10100Sstevel@tonic-gate ASSERT(pp == NULL || PAGE_LOCKED(pp)); 10110Sstevel@tonic-gate } else { 10120Sstevel@tonic-gate (void) as_fault(as->a_hat, as, (caddr_t)(rp->r_pc & PAGEMASK), 10130Sstevel@tonic-gate PAGESIZE, F_SOFTUNLOCK, S_READ); 10140Sstevel@tonic-gate return (SIMU_FAULT); 10150Sstevel@tonic-gate } 10160Sstevel@tonic-gate 10170Sstevel@tonic-gate AS_LOCK_ENTER(as, &as->a_lock, RW_READER); 10180Sstevel@tonic-gate ka = ppmapin(pp, PROT_READ|PROT_WRITE, (caddr_t)rp->r_pc); 10190Sstevel@tonic-gate *(uint_t *)(ka + (uintptr_t)(rp->r_pc % PAGESIZE)) = inst; 10200Sstevel@tonic-gate doflush(ka + (uintptr_t)(rp->r_pc % PAGESIZE)); 10210Sstevel@tonic-gate ppmapout(ka); 10220Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 10230Sstevel@tonic-gate 10240Sstevel@tonic-gate (void) as_fault(as->a_hat, as, (caddr_t)(rp->r_pc & PAGEMASK), 10250Sstevel@tonic-gate PAGESIZE, F_SOFTUNLOCK, S_READ); 10260Sstevel@tonic-gate return (SIMU_RETRY); 10270Sstevel@tonic-gate } 10280Sstevel@tonic-gate 10290Sstevel@tonic-gate /* 10300Sstevel@tonic-gate * Get the value of a register for instruction simulation 10310Sstevel@tonic-gate * by using the regs or window structure pointers. 10320Sstevel@tonic-gate * Return 0 for success, and -1 for failure. If there is a failure, 10330Sstevel@tonic-gate * save the faulting address using badaddr pointer. 10340Sstevel@tonic-gate * We have 64 bit globals and outs, and 32 or 64 bit ins and locals. 10350Sstevel@tonic-gate * Don't truncate globals/outs for 32 bit programs, for v8+ support. 10360Sstevel@tonic-gate */ 10370Sstevel@tonic-gate int 10380Sstevel@tonic-gate getreg(struct regs *rp, uint_t reg, uint64_t *val, caddr_t *badaddr) 10390Sstevel@tonic-gate { 10400Sstevel@tonic-gate uint64_t *rgs, *sp; 10410Sstevel@tonic-gate int rv = 0; 10420Sstevel@tonic-gate 10430Sstevel@tonic-gate rgs = (uint64_t *)&rp->r_ps; /* globals and outs */ 10440Sstevel@tonic-gate sp = (uint64_t *)rp->r_sp; /* ins and locals */ 10450Sstevel@tonic-gate if (reg == 0) { 10460Sstevel@tonic-gate *val = 0; 10470Sstevel@tonic-gate } else if (reg < 16) { 10480Sstevel@tonic-gate *val = rgs[reg]; 10490Sstevel@tonic-gate } else if (IS_V9STACK(sp)) { 10500Sstevel@tonic-gate uint64_t *rw = (uint64_t *)((uintptr_t)sp + V9BIAS64); 10510Sstevel@tonic-gate uint64_t *addr = (uint64_t *)&rw[reg - 16]; 10520Sstevel@tonic-gate uint64_t res; 10530Sstevel@tonic-gate 10540Sstevel@tonic-gate if (USERMODE(rp->r_tstate)) { 10550Sstevel@tonic-gate if (fuword64_nowatch(addr, &res) == -1) { 10560Sstevel@tonic-gate *badaddr = (caddr_t)addr; 10570Sstevel@tonic-gate rv = -1; 10580Sstevel@tonic-gate } 10590Sstevel@tonic-gate } else { 10600Sstevel@tonic-gate res = *addr; 10610Sstevel@tonic-gate } 10620Sstevel@tonic-gate *val = res; 10630Sstevel@tonic-gate } else { 10640Sstevel@tonic-gate uint32_t *rw = (uint32_t *)(caddr32_t)sp; 10650Sstevel@tonic-gate uint32_t *addr = (uint32_t *)&rw[reg - 16]; 10660Sstevel@tonic-gate uint32_t res; 10670Sstevel@tonic-gate 10680Sstevel@tonic-gate if (USERMODE(rp->r_tstate)) { 10690Sstevel@tonic-gate if (fuword32_nowatch(addr, &res) == -1) { 10700Sstevel@tonic-gate *badaddr = (caddr_t)addr; 10710Sstevel@tonic-gate rv = -1; 10720Sstevel@tonic-gate } 10730Sstevel@tonic-gate } else { 10740Sstevel@tonic-gate res = *addr; 10750Sstevel@tonic-gate } 10760Sstevel@tonic-gate *val = (uint64_t)res; 10770Sstevel@tonic-gate } 10780Sstevel@tonic-gate return (rv); 10790Sstevel@tonic-gate } 10800Sstevel@tonic-gate 10810Sstevel@tonic-gate /* 10820Sstevel@tonic-gate * Set the value of a register after instruction simulation 10830Sstevel@tonic-gate * by using the regs or window structure pointers. 10840Sstevel@tonic-gate * Return 0 for succes -1 failure. 10850Sstevel@tonic-gate * save the faulting address using badaddr pointer. 10860Sstevel@tonic-gate * We have 64 bit globals and outs, and 32 or 64 bit ins and locals. 10870Sstevel@tonic-gate * Don't truncate globals/outs for 32 bit programs, for v8+ support. 10880Sstevel@tonic-gate */ 10890Sstevel@tonic-gate int 10900Sstevel@tonic-gate putreg(uint64_t *data, struct regs *rp, uint_t reg, caddr_t *badaddr) 10910Sstevel@tonic-gate { 10920Sstevel@tonic-gate uint64_t *rgs, *sp; 10930Sstevel@tonic-gate int rv = 0; 10940Sstevel@tonic-gate 10950Sstevel@tonic-gate rgs = (uint64_t *)&rp->r_ps; /* globals and outs */ 10960Sstevel@tonic-gate sp = (uint64_t *)rp->r_sp; /* ins and locals */ 10970Sstevel@tonic-gate if (reg == 0) { 10980Sstevel@tonic-gate return (0); 10990Sstevel@tonic-gate } else if (reg < 16) { 11000Sstevel@tonic-gate rgs[reg] = *data; 11010Sstevel@tonic-gate } else if (IS_V9STACK(sp)) { 11020Sstevel@tonic-gate uint64_t *rw = (uint64_t *)((uintptr_t)sp + V9BIAS64); 11030Sstevel@tonic-gate uint64_t *addr = (uint64_t *)&rw[reg - 16]; 11040Sstevel@tonic-gate uint64_t res; 11050Sstevel@tonic-gate 11060Sstevel@tonic-gate if (USERMODE(rp->r_tstate)) { 11070Sstevel@tonic-gate struct machpcb *mpcb = lwptompcb(curthread->t_lwp); 11080Sstevel@tonic-gate 11090Sstevel@tonic-gate res = *data; 11100Sstevel@tonic-gate if (suword64_nowatch(addr, res) != 0) { 11110Sstevel@tonic-gate *badaddr = (caddr_t)addr; 11120Sstevel@tonic-gate rv = -1; 11130Sstevel@tonic-gate } 11140Sstevel@tonic-gate /* 11150Sstevel@tonic-gate * We have changed a local or in register; 11160Sstevel@tonic-gate * nuke the watchpoint return windows. 11170Sstevel@tonic-gate */ 11180Sstevel@tonic-gate mpcb->mpcb_rsp[0] = NULL; 11190Sstevel@tonic-gate mpcb->mpcb_rsp[1] = NULL; 11200Sstevel@tonic-gate } else { 11210Sstevel@tonic-gate res = *data; 11220Sstevel@tonic-gate *addr = res; 11230Sstevel@tonic-gate } 11240Sstevel@tonic-gate } else { 11250Sstevel@tonic-gate uint32_t *rw = (uint32_t *)(caddr32_t)sp; 11260Sstevel@tonic-gate uint32_t *addr = (uint32_t *)&rw[reg - 16]; 11270Sstevel@tonic-gate uint32_t res; 11280Sstevel@tonic-gate 11290Sstevel@tonic-gate if (USERMODE(rp->r_tstate)) { 11300Sstevel@tonic-gate struct machpcb *mpcb = lwptompcb(curthread->t_lwp); 11310Sstevel@tonic-gate 11320Sstevel@tonic-gate res = (uint_t)*data; 11330Sstevel@tonic-gate if (suword32_nowatch(addr, res) != 0) { 11340Sstevel@tonic-gate *badaddr = (caddr_t)addr; 11350Sstevel@tonic-gate rv = -1; 11360Sstevel@tonic-gate } 11370Sstevel@tonic-gate /* 11380Sstevel@tonic-gate * We have changed a local or in register; 11390Sstevel@tonic-gate * nuke the watchpoint return windows. 11400Sstevel@tonic-gate */ 11410Sstevel@tonic-gate mpcb->mpcb_rsp[0] = NULL; 11420Sstevel@tonic-gate mpcb->mpcb_rsp[1] = NULL; 11430Sstevel@tonic-gate 11440Sstevel@tonic-gate } else { 11450Sstevel@tonic-gate res = (uint_t)*data; 11460Sstevel@tonic-gate *addr = res; 11470Sstevel@tonic-gate } 11480Sstevel@tonic-gate } 11490Sstevel@tonic-gate return (rv); 11500Sstevel@tonic-gate } 11510Sstevel@tonic-gate 11520Sstevel@tonic-gate /* 11530Sstevel@tonic-gate * Calculate a memory reference address from instruction 11540Sstevel@tonic-gate * operands, used to return the address of a fault, instead 11550Sstevel@tonic-gate * of the instruction when an error occurs. This is code that is 11560Sstevel@tonic-gate * common with most of the routines that simulate instructions. 11570Sstevel@tonic-gate */ 11580Sstevel@tonic-gate int 11590Sstevel@tonic-gate calc_memaddr(struct regs *rp, caddr_t *badaddr) 11600Sstevel@tonic-gate { 11610Sstevel@tonic-gate uint_t inst; 11620Sstevel@tonic-gate uint_t rd, rs1, rs2; 11630Sstevel@tonic-gate int sz; 11640Sstevel@tonic-gate int immflg; 11650Sstevel@tonic-gate int floatflg; 11660Sstevel@tonic-gate caddr_t addr; 11670Sstevel@tonic-gate uint64_t val; 11680Sstevel@tonic-gate 11690Sstevel@tonic-gate if (USERMODE(rp->r_tstate)) 11700Sstevel@tonic-gate inst = fetch_user_instr((caddr_t)rp->r_pc); 11710Sstevel@tonic-gate else 11720Sstevel@tonic-gate inst = *(uint_t *)rp->r_pc; 11730Sstevel@tonic-gate 11740Sstevel@tonic-gate rd = (inst >> 25) & 0x1f; 11750Sstevel@tonic-gate rs1 = (inst >> 14) & 0x1f; 11760Sstevel@tonic-gate rs2 = inst & 0x1f; 11770Sstevel@tonic-gate floatflg = (inst >> 24) & 1; 11780Sstevel@tonic-gate immflg = (inst >> 13) & 1; 11790Sstevel@tonic-gate 11800Sstevel@tonic-gate if (floatflg) { 11810Sstevel@tonic-gate switch ((inst >> 19) & 3) { /* map size bits to a number */ 11820Sstevel@tonic-gate case 0: sz = 4; break; /* ldf/stf */ 11830Sstevel@tonic-gate case 1: return (0); /* ld[x]fsr/st[x]fsr */ 11840Sstevel@tonic-gate case 2: sz = 16; break; /* ldqf/stqf */ 11850Sstevel@tonic-gate case 3: sz = 8; break; /* lddf/stdf */ 11860Sstevel@tonic-gate } 11870Sstevel@tonic-gate /* 11880Sstevel@tonic-gate * Fix to access extra double register encoding plus 11890Sstevel@tonic-gate * compensate to access the correct fpu_dreg. 11900Sstevel@tonic-gate */ 11910Sstevel@tonic-gate if (sz > 4) { 11920Sstevel@tonic-gate if ((rd & 1) == 1) 11930Sstevel@tonic-gate rd = (rd & 0x1e) | 0x20; 11940Sstevel@tonic-gate rd = rd >> 1; 11950Sstevel@tonic-gate } 11960Sstevel@tonic-gate } else { 11970Sstevel@tonic-gate switch ((inst >> 19) & 0xf) { /* map size bits to a number */ 11980Sstevel@tonic-gate case 0: /* lduw */ 11990Sstevel@tonic-gate case 4: /* stw */ 12000Sstevel@tonic-gate case 8: /* ldsw */ 12010Sstevel@tonic-gate case 0xf: /* swap */ 12020Sstevel@tonic-gate sz = 4; break; 12030Sstevel@tonic-gate case 1: /* ldub */ 12040Sstevel@tonic-gate case 5: /* stb */ 12050Sstevel@tonic-gate case 9: /* ldsb */ 12060Sstevel@tonic-gate case 0xd: /* ldstub */ 12070Sstevel@tonic-gate sz = 1; break; 12080Sstevel@tonic-gate case 2: /* lduh */ 12090Sstevel@tonic-gate case 6: /* sth */ 12100Sstevel@tonic-gate case 0xa: /* ldsh */ 12110Sstevel@tonic-gate sz = 2; break; 12120Sstevel@tonic-gate case 3: /* ldd */ 12130Sstevel@tonic-gate case 7: /* std */ 12140Sstevel@tonic-gate case 0xb: /* ldx */ 12150Sstevel@tonic-gate case 0xe: /* stx */ 12160Sstevel@tonic-gate sz = 8; break; 12170Sstevel@tonic-gate } 12180Sstevel@tonic-gate } 12190Sstevel@tonic-gate 12200Sstevel@tonic-gate if (USERMODE(rp->r_tstate)) 12210Sstevel@tonic-gate (void) flush_user_windows_to_stack(NULL); 12220Sstevel@tonic-gate else 12230Sstevel@tonic-gate flush_windows(); 12240Sstevel@tonic-gate 12250Sstevel@tonic-gate if (getreg(rp, rs1, &val, badaddr)) 12260Sstevel@tonic-gate return (SIMU_FAULT); 12270Sstevel@tonic-gate addr = (caddr_t)val; 12280Sstevel@tonic-gate 12290Sstevel@tonic-gate /* check immediate bit and use immediate field or reg (rs2) */ 12300Sstevel@tonic-gate if (immflg) { 12310Sstevel@tonic-gate int imm; 12320Sstevel@tonic-gate imm = inst & 0x1fff; /* mask out immediate field */ 12330Sstevel@tonic-gate imm <<= 19; /* sign extend it */ 12340Sstevel@tonic-gate imm >>= 19; 12350Sstevel@tonic-gate addr += imm; /* compute address */ 12360Sstevel@tonic-gate } else { 12370Sstevel@tonic-gate if (getreg(rp, rs2, &val, badaddr)) 12380Sstevel@tonic-gate return (SIMU_FAULT); 12390Sstevel@tonic-gate addr += val; 12400Sstevel@tonic-gate } 12410Sstevel@tonic-gate 12420Sstevel@tonic-gate /* 12430Sstevel@tonic-gate * If this is a 32-bit program, chop the address accordingly. 12440Sstevel@tonic-gate */ 12450Sstevel@tonic-gate if (curproc->p_model == DATAMODEL_ILP32 && 12460Sstevel@tonic-gate USERMODE(rp->r_tstate)) 12470Sstevel@tonic-gate addr = (caddr_t)(caddr32_t)addr; 12480Sstevel@tonic-gate 12490Sstevel@tonic-gate *badaddr = addr; 12500Sstevel@tonic-gate return ((uintptr_t)addr & (sz - 1) ? SIMU_UNALIGN : SIMU_SUCCESS); 12510Sstevel@tonic-gate } 12520Sstevel@tonic-gate 12530Sstevel@tonic-gate /* 12540Sstevel@tonic-gate * Return the size of a load or store instruction (1, 2, 4, 8, 16, 64). 12550Sstevel@tonic-gate * Also compute the precise address by instruction disassembly. 12560Sstevel@tonic-gate * (v9 page faults only provide the page address via the hardware.) 12570Sstevel@tonic-gate * Return 0 on failure (not a load or store instruction). 12580Sstevel@tonic-gate */ 12590Sstevel@tonic-gate int 12600Sstevel@tonic-gate instr_size(struct regs *rp, caddr_t *addrp, enum seg_rw rdwr) 12610Sstevel@tonic-gate { 12620Sstevel@tonic-gate uint_t inst, op3, asi; 12630Sstevel@tonic-gate uint_t rd, rs1, rs2; 12640Sstevel@tonic-gate int sz = 0; 12650Sstevel@tonic-gate int immflg; 12660Sstevel@tonic-gate int floatflg; 12670Sstevel@tonic-gate caddr_t addr; 12680Sstevel@tonic-gate caddr_t badaddr; 12690Sstevel@tonic-gate uint64_t val; 12700Sstevel@tonic-gate 12710Sstevel@tonic-gate if (rdwr == S_EXEC) { 12720Sstevel@tonic-gate *addrp = (caddr_t)rp->r_pc; 12730Sstevel@tonic-gate return (4); 12740Sstevel@tonic-gate } 12750Sstevel@tonic-gate 12760Sstevel@tonic-gate /* 12770Sstevel@tonic-gate * Fetch the instruction from user-level. 12780Sstevel@tonic-gate * We would like to assert this: 12790Sstevel@tonic-gate * ASSERT(USERMODE(rp->r_tstate)); 12800Sstevel@tonic-gate * but we can't because we can reach this point from a 12810Sstevel@tonic-gate * register window underflow/overflow and the v9 wbuf 12820Sstevel@tonic-gate * traps call trap() with T_USER even though r_tstate 12830Sstevel@tonic-gate * indicates a system trap, not a user trap. 12840Sstevel@tonic-gate */ 12850Sstevel@tonic-gate inst = fetch_user_instr((caddr_t)rp->r_pc); 12860Sstevel@tonic-gate 12870Sstevel@tonic-gate op3 = (inst >> 19) & 0x3f; 12880Sstevel@tonic-gate rd = (inst >> 25) & 0x1f; 12890Sstevel@tonic-gate rs1 = (inst >> 14) & 0x1f; 12900Sstevel@tonic-gate rs2 = inst & 0x1f; 12910Sstevel@tonic-gate floatflg = (inst >> 24) & 1; 12920Sstevel@tonic-gate immflg = (inst >> 13) & 1; 12930Sstevel@tonic-gate 12940Sstevel@tonic-gate /* if not load or store do nothing. can't happen? */ 12950Sstevel@tonic-gate if ((inst >> 30) != 3) 12960Sstevel@tonic-gate return (0); 12970Sstevel@tonic-gate 12980Sstevel@tonic-gate if (immflg) 12990Sstevel@tonic-gate asi = (uint_t)((rp->r_tstate >> TSTATE_ASI_SHIFT) & 13000Sstevel@tonic-gate TSTATE_ASI_MASK); 13010Sstevel@tonic-gate else 13020Sstevel@tonic-gate asi = (inst >> 5) & 0xff; 13030Sstevel@tonic-gate 13040Sstevel@tonic-gate if (floatflg) { 13050Sstevel@tonic-gate /* check for ld/st alternate and highest defined V9 asi */ 13060Sstevel@tonic-gate if ((op3 & 0x30) == 0x30 && asi > ASI_SNFL) { 13070Sstevel@tonic-gate sz = extended_asi_size(asi); 13080Sstevel@tonic-gate } else { 13090Sstevel@tonic-gate switch (op3 & 3) { 13100Sstevel@tonic-gate case 0: 13110Sstevel@tonic-gate sz = 4; /* ldf/stf/cas */ 13120Sstevel@tonic-gate break; 13130Sstevel@tonic-gate case 1: 13140Sstevel@tonic-gate if (rd == 0) 13150Sstevel@tonic-gate sz = 4; /* ldfsr/stfsr */ 13160Sstevel@tonic-gate else 13170Sstevel@tonic-gate sz = 8; /* ldxfsr/stxfsr */ 13180Sstevel@tonic-gate break; 13190Sstevel@tonic-gate case 2: 13200Sstevel@tonic-gate if (op3 == 0x3e) 13210Sstevel@tonic-gate sz = 8; /* casx */ 13220Sstevel@tonic-gate else 13230Sstevel@tonic-gate sz = 16; /* ldqf/stqf */ 13240Sstevel@tonic-gate break; 13250Sstevel@tonic-gate case 3: 13260Sstevel@tonic-gate sz = 8; /* lddf/stdf */ 13270Sstevel@tonic-gate break; 13280Sstevel@tonic-gate } 13290Sstevel@tonic-gate } 13300Sstevel@tonic-gate } else { 13310Sstevel@tonic-gate switch (op3 & 0xf) { /* map size bits to a number */ 13320Sstevel@tonic-gate case 0: /* lduw */ 13330Sstevel@tonic-gate case 4: /* stw */ 13340Sstevel@tonic-gate case 8: /* ldsw */ 13350Sstevel@tonic-gate case 0xf: /* swap */ 13360Sstevel@tonic-gate sz = 4; break; 13370Sstevel@tonic-gate case 1: /* ldub */ 13380Sstevel@tonic-gate case 5: /* stb */ 13390Sstevel@tonic-gate case 9: /* ldsb */ 13400Sstevel@tonic-gate case 0xd: /* ldstub */ 13410Sstevel@tonic-gate sz = 1; break; 13420Sstevel@tonic-gate case 2: /* lduh */ 13430Sstevel@tonic-gate case 6: /* sth */ 13440Sstevel@tonic-gate case 0xa: /* ldsh */ 13450Sstevel@tonic-gate sz = 2; break; 13460Sstevel@tonic-gate case 3: /* ldd */ 13470Sstevel@tonic-gate case 7: /* std */ 13480Sstevel@tonic-gate case 0xb: /* ldx */ 13490Sstevel@tonic-gate case 0xe: /* stx */ 13500Sstevel@tonic-gate sz = 8; break; 13510Sstevel@tonic-gate } 13520Sstevel@tonic-gate } 13530Sstevel@tonic-gate 13540Sstevel@tonic-gate if (sz == 0) /* can't happen? */ 13550Sstevel@tonic-gate return (0); 13560Sstevel@tonic-gate (void) flush_user_windows_to_stack(NULL); 13570Sstevel@tonic-gate 13580Sstevel@tonic-gate if (getreg(rp, rs1, &val, &badaddr)) 13590Sstevel@tonic-gate return (0); 13600Sstevel@tonic-gate addr = (caddr_t)val; 13610Sstevel@tonic-gate 13620Sstevel@tonic-gate /* cas/casx don't use rs2 / simm13 to compute the address */ 13630Sstevel@tonic-gate if ((op3 & 0x3d) != 0x3c) { 13640Sstevel@tonic-gate /* check immediate bit and use immediate field or reg (rs2) */ 13650Sstevel@tonic-gate if (immflg) { 13660Sstevel@tonic-gate int imm; 13670Sstevel@tonic-gate imm = inst & 0x1fff; /* mask out immediate field */ 13680Sstevel@tonic-gate imm <<= 19; /* sign extend it */ 13690Sstevel@tonic-gate imm >>= 19; 13700Sstevel@tonic-gate addr += imm; /* compute address */ 13710Sstevel@tonic-gate } else { 13720Sstevel@tonic-gate /* 13730Sstevel@tonic-gate * asi's in the 0xCx range are partial store 13740Sstevel@tonic-gate * instructions. For these, rs2 is a mask, not part of 13750Sstevel@tonic-gate * the address. 13760Sstevel@tonic-gate */ 13770Sstevel@tonic-gate if (!(floatflg && (asi & 0xf0) == 0xc0)) { 13780Sstevel@tonic-gate if (getreg(rp, rs2, &val, &badaddr)) 13790Sstevel@tonic-gate return (0); 13800Sstevel@tonic-gate addr += val; 13810Sstevel@tonic-gate } 13820Sstevel@tonic-gate } 13830Sstevel@tonic-gate } 13840Sstevel@tonic-gate 13850Sstevel@tonic-gate /* 13860Sstevel@tonic-gate * If this is a 32-bit program, chop the address accordingly. 13870Sstevel@tonic-gate */ 13880Sstevel@tonic-gate if (curproc->p_model == DATAMODEL_ILP32) 13890Sstevel@tonic-gate addr = (caddr_t)(caddr32_t)addr; 13900Sstevel@tonic-gate 13910Sstevel@tonic-gate *addrp = addr; 13920Sstevel@tonic-gate ASSERT(sz != 0); 13930Sstevel@tonic-gate return (sz); 13940Sstevel@tonic-gate } 13950Sstevel@tonic-gate 13960Sstevel@tonic-gate /* 13970Sstevel@tonic-gate * Fetch an instruction from user-level. 13980Sstevel@tonic-gate * Deal with watchpoints, if they are in effect. 13990Sstevel@tonic-gate */ 14000Sstevel@tonic-gate int32_t 14010Sstevel@tonic-gate fetch_user_instr(caddr_t vaddr) 14020Sstevel@tonic-gate { 14030Sstevel@tonic-gate proc_t *p = curproc; 14040Sstevel@tonic-gate int32_t instr; 14050Sstevel@tonic-gate 14060Sstevel@tonic-gate /* 14070Sstevel@tonic-gate * If this is a 32-bit program, chop the address accordingly. 14080Sstevel@tonic-gate */ 14090Sstevel@tonic-gate if (p->p_model == DATAMODEL_ILP32) 14100Sstevel@tonic-gate vaddr = (caddr_t)(caddr32_t)vaddr; 14110Sstevel@tonic-gate 14120Sstevel@tonic-gate if (fuword32_nowatch(vaddr, (uint32_t *)&instr) == -1) 14130Sstevel@tonic-gate instr = -1; 14140Sstevel@tonic-gate 14150Sstevel@tonic-gate return (instr); 14160Sstevel@tonic-gate } 1417