emulenv.cc revision 4863:b6dacc9a39ff
112855Sgabeblack@google.com/* 212855Sgabeblack@google.com * Copyright (c) 2007 The Hewlett-Packard Development Company 312855Sgabeblack@google.com * All rights reserved. 412855Sgabeblack@google.com * 512855Sgabeblack@google.com * Redistribution and use of this software in source and binary forms, 612855Sgabeblack@google.com * with or without modification, are permitted provided that the 712855Sgabeblack@google.com * following conditions are met: 812855Sgabeblack@google.com * 912855Sgabeblack@google.com * The software must be used only for Non-Commercial Use which means any 1012855Sgabeblack@google.com * use which is NOT directed to receiving any direct monetary 1112855Sgabeblack@google.com * compensation for, or commercial advantage from such use. Illustrative 1212855Sgabeblack@google.com * examples of non-commercial use are academic research, personal study, 1312855Sgabeblack@google.com * teaching, education and corporate research & development. 1412855Sgabeblack@google.com * Illustrative examples of commercial use are distributing products for 1512855Sgabeblack@google.com * commercial advantage and providing services using the software for 1612855Sgabeblack@google.com * commercial advantage. 1712855Sgabeblack@google.com * 1812855Sgabeblack@google.com * If you wish to use this software or functionality therein that may be 1912855Sgabeblack@google.com * covered by patents for commercial use, please contact: 2012855Sgabeblack@google.com * Director of Intellectual Property Licensing 2112855Sgabeblack@google.com * Office of Strategy and Technology 2212855Sgabeblack@google.com * Hewlett-Packard Company 2312855Sgabeblack@google.com * 1501 Page Mill Road 2412855Sgabeblack@google.com * Palo Alto, California 94304 2512855Sgabeblack@google.com * 2612855Sgabeblack@google.com * Redistributions of source code must retain the above copyright notice, 2712855Sgabeblack@google.com * this list of conditions and the following disclaimer. Redistributions 2812855Sgabeblack@google.com * in binary form must reproduce the above copyright notice, this list of 2912855Sgabeblack@google.com * conditions and the following disclaimer in the documentation and/or 3012855Sgabeblack@google.com * other materials provided with the distribution. Neither the name of 3112855Sgabeblack@google.com * the COPYRIGHT HOLDER(s), HEWLETT-PACKARD COMPANY, nor the names of its 3212855Sgabeblack@google.com * contributors may be used to endorse or promote products derived from 3312855Sgabeblack@google.com * this software without specific prior written permission. No right of 3412855Sgabeblack@google.com * sublicense is granted herewith. Derivatives of the software and 3512855Sgabeblack@google.com * output created using the software may be prepared, but only for 3612855Sgabeblack@google.com * Non-Commercial Uses. Derivatives of the software may be shared with 3712855Sgabeblack@google.com * others provided: (i) the others agree to abide by the list of 3812855Sgabeblack@google.com * conditions herein which includes the Non-Commercial Use restrictions; 3912855Sgabeblack@google.com * and (ii) such Derivatives of the software include the above copyright 4012855Sgabeblack@google.com * notice to acknowledge the contribution from this software where 4112855Sgabeblack@google.com * applicable, this list of conditions and the disclaimer below. 4212855Sgabeblack@google.com * 4312855Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 4412855Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 4512855Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 4612855Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 4712855Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 4812855Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 4912855Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 5012855Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 5112855Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 5212855Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 5312855Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 5412855Sgabeblack@google.com * 5512855Sgabeblack@google.com * Authors: Gabe Black 5612855Sgabeblack@google.com */ 5712855Sgabeblack@google.com 5812855Sgabeblack@google.com#include "arch/x86/emulenv.hh" 5912855Sgabeblack@google.com#include "base/misc.hh" 6012855Sgabeblack@google.com 6112855Sgabeblack@google.comusing namespace X86ISA; 6212855Sgabeblack@google.com 6312855Sgabeblack@google.comvoid EmulEnv::doModRM(const ExtMachInst & machInst) 6412855Sgabeblack@google.com{ 6512855Sgabeblack@google.com assert(machInst.modRM.mod != 3); 6612855Sgabeblack@google.com //Use the SIB byte for addressing if the modrm byte calls for it. 6712855Sgabeblack@google.com if (machInst.modRM.rm == 4 && machInst.addrSize != 2) { 6812855Sgabeblack@google.com scale = 1 << machInst.sib.scale; 6912855Sgabeblack@google.com index = machInst.sib.index | (machInst.rex.x << 3); 7012855Sgabeblack@google.com base = machInst.sib.base | (machInst.rex.b << 3); 7112855Sgabeblack@google.com //In this special case, we don't use a base. The displacement also 7212855Sgabeblack@google.com //changes, but that's managed by the predecoder. 7312855Sgabeblack@google.com if (machInst.sib.base == INTREG_RBP && machInst.modRM.mod == 0) 7412855Sgabeblack@google.com base = NUM_INTREGS; 7512855Sgabeblack@google.com //In -this- special case, we don't use an index. 7612855Sgabeblack@google.com if (index == INTREG_RSP) 7712855Sgabeblack@google.com index = NUM_INTREGS; 7812855Sgabeblack@google.com } else { 7912855Sgabeblack@google.com if (machInst.addrSize == 2) { 8012855Sgabeblack@google.com warn("I'm not really using 16 bit MODRM like I'm supposed to!\n"); 8112855Sgabeblack@google.com } else { 8212855Sgabeblack@google.com scale = 0; 8312855Sgabeblack@google.com base = machInst.modRM.rm | (machInst.rex.b << 3); 8412855Sgabeblack@google.com if (machInst.modRM.mod == 0 && machInst.modRM.rm == 5) { 8512855Sgabeblack@google.com //Since we need to use a different encoding of this 8612855Sgabeblack@google.com //instruction anyway, just ignore the base in those cases 8712855Sgabeblack@google.com base = NUM_INTREGS; 8812855Sgabeblack@google.com } 8912855Sgabeblack@google.com } 9012855Sgabeblack@google.com } 9112855Sgabeblack@google.com //Figure out what segment to use. This won't be entirely accurate since 9212855Sgabeblack@google.com //the presence of a displacement is supposed to make the instruction 9312855Sgabeblack@google.com //default to the data segment. 9412855Sgabeblack@google.com if (base != INTREG_RBP && base != INTREG_RSP || 9512855Sgabeblack@google.com 0/*Has an immediate offset*/) { 9612855Sgabeblack@google.com seg = SEGMENT_REG_DS; 9712855Sgabeblack@google.com //Handle any segment override that might have been in the instruction 9812855Sgabeblack@google.com int segFromInst = machInst.legacy.seg; 9912855Sgabeblack@google.com if (segFromInst) 10012855Sgabeblack@google.com seg = (SegmentRegIndex)(segFromInst - 1); 10112855Sgabeblack@google.com } else { 10212855Sgabeblack@google.com seg = SEGMENT_REG_SS; 10312855Sgabeblack@google.com } 10412855Sgabeblack@google.com} 10512855Sgabeblack@google.com 10612855Sgabeblack@google.com