emulenv.cc revision 11793
14486Sbinkertn@umich.edu/*
24486Sbinkertn@umich.edu * Copyright (c) 2007 The Hewlett-Packard Development Company
34486Sbinkertn@umich.edu * All rights reserved.
44486Sbinkertn@umich.edu *
54486Sbinkertn@umich.edu * The license below extends only to copyright in the software and shall
64486Sbinkertn@umich.edu * not be construed as granting a license to any other intellectual
74486Sbinkertn@umich.edu * property including but not limited to intellectual property relating
84486Sbinkertn@umich.edu * to a hardware implementation of the functionality of the software
94486Sbinkertn@umich.edu * licensed hereunder.  You may use the software subject to the license
104486Sbinkertn@umich.edu * terms below provided that you ensure that this notice is replicated
114486Sbinkertn@umich.edu * unmodified and in its entirety in all distributions of the software,
124486Sbinkertn@umich.edu * modified or unmodified, in source code or in binary form.
134486Sbinkertn@umich.edu *
144486Sbinkertn@umich.edu * Redistribution and use in source and binary forms, with or without
154486Sbinkertn@umich.edu * modification, are permitted provided that the following conditions are
164486Sbinkertn@umich.edu * met: redistributions of source code must retain the above copyright
174486Sbinkertn@umich.edu * notice, this list of conditions and the following disclaimer;
184486Sbinkertn@umich.edu * redistributions in binary form must reproduce the above copyright
194486Sbinkertn@umich.edu * notice, this list of conditions and the following disclaimer in the
204486Sbinkertn@umich.edu * documentation and/or other materials provided with the distribution;
214486Sbinkertn@umich.edu * neither the name of the copyright holders nor the names of its
224486Sbinkertn@umich.edu * contributors may be used to endorse or promote products derived from
234486Sbinkertn@umich.edu * this software without specific prior written permission.
244486Sbinkertn@umich.edu *
254486Sbinkertn@umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
264486Sbinkertn@umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
274486Sbinkertn@umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
284486Sbinkertn@umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
296654Snate@binkert.org * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
303102SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
313102SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
321681SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
333223SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
348887Sgeoffrey.blake@arm.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
359480Snilay@cs.wisc.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
364486Sbinkertn@umich.edu *
372817SN/A * Authors: Gabe Black
382817SN/A */
399341SAndreas.Sandberg@arm.com
409341SAndreas.Sandberg@arm.com#include "arch/x86/emulenv.hh"
419518SAndreas.Sandberg@ARM.com
429518SAndreas.Sandberg@ARM.com#include <cassert>
439518SAndreas.Sandberg@ARM.com
449518SAndreas.Sandberg@ARM.com#include "base/misc.hh"
459518SAndreas.Sandberg@ARM.com
469518SAndreas.Sandberg@ARM.comusing namespace X86ISA;
479518SAndreas.Sandberg@ARM.com
489518SAndreas.Sandberg@ARM.comvoid EmulEnv::doModRM(const ExtMachInst & machInst)
499518SAndreas.Sandberg@ARM.com{
509518SAndreas.Sandberg@ARM.com    assert(machInst.modRM.mod != 3);
519518SAndreas.Sandberg@ARM.com    //Use the SIB byte for addressing if the modrm byte calls for it.
529518SAndreas.Sandberg@ARM.com    if (machInst.modRM.rm == 4 && machInst.addrSize != 2) {
532932SN/A        scale = 1 << machInst.sib.scale;
541681SN/A        index = machInst.sib.index | (machInst.rex.x << 3);
554597Sbinkertn@umich.edu        base = machInst.sib.base | (machInst.rex.b << 3);
561681SN/A        //In this special case, we don't use a base. The displacement also
579184Sandreas.hansson@arm.com        //changes, but that's managed by the decoder.
589184Sandreas.hansson@arm.com        if (machInst.sib.base == INTREG_RBP && machInst.modRM.mod == 0)
599184Sandreas.hansson@arm.com            base = NUM_INTREGS;
609184Sandreas.hansson@arm.com        //In -this- special case, we don't use an index.
619184Sandreas.hansson@arm.com        if (index == INTREG_RSP)
622932SN/A            index = NUM_INTREGS;
639982Satgutier@umich.edu    } else {
6410331Smitch.hayenga@arm.com        if (machInst.addrSize == 2) {
6510331Smitch.hayenga@arm.com            unsigned rm = machInst.modRM.rm;
662932SN/A            if (rm <= 3) {
679184Sandreas.hansson@arm.com                scale = 1;
689184Sandreas.hansson@arm.com                if (rm < 2) {
699184Sandreas.hansson@arm.com                    base = INTREG_RBX;
709184Sandreas.hansson@arm.com                } else {
719184Sandreas.hansson@arm.com                    base = INTREG_RBP;
722932SN/A                }
731681SN/A                index = (rm % 2) ? INTREG_RDI : INTREG_RSI;
749184Sandreas.hansson@arm.com            } else {
759184Sandreas.hansson@arm.com                scale = 0;
769184Sandreas.hansson@arm.com                switch (rm) {
779184Sandreas.hansson@arm.com                  case 4:
782932SN/A                    base = INTREG_RSI;
791681SN/A                    break;
809184Sandreas.hansson@arm.com                  case 5:
812932SN/A                    base = INTREG_RDI;
829184Sandreas.hansson@arm.com                    break;
832932SN/A                  case 6:
849184Sandreas.hansson@arm.com                    base = INTREG_RBP;
852932SN/A                    break;
862932SN/A                  case 7:
872932SN/A                    base = INTREG_RBX;
882932SN/A                    break;
893223SN/A                }
902932SN/A            }
919184Sandreas.hansson@arm.com        } else {
921681SN/A            scale = 0;
939184Sandreas.hansson@arm.com            base = machInst.modRM.rm | (machInst.rex.b << 3);
942932SN/A            if (machInst.modRM.mod == 0 && machInst.modRM.rm == 5) {
952932SN/A                //Since we need to use a different encoding of this
969184Sandreas.hansson@arm.com                //instruction anyway, just ignore the base in those cases
979184Sandreas.hansson@arm.com                base = NUM_INTREGS;
981681SN/A            }
992932SN/A        }
1002932SN/A    }
1011681SN/A    //Figure out what segment to use. This won't be entirely accurate since
1022932SN/A    //the presence of a displacement is supposed to make the instruction
1032932SN/A    //default to the data segment.
1048199SAli.Saidi@ARM.com    if ((base != INTREG_RBP && base != INTREG_RSP) || machInst.dispSize) {
1058199SAli.Saidi@ARM.com        seg = SEGMENT_REG_DS;
1068199SAli.Saidi@ARM.com        //Handle any segment override that might have been in the instruction
1078519SAli.Saidi@ARM.com        int segFromInst = machInst.legacy.seg;
1088519SAli.Saidi@ARM.com        if (segFromInst)
1092932SN/A            seg = (SegmentRegIndex)(segFromInst - 1);
1102932SN/A    } else {
1111681SN/A        seg = SEGMENT_REG_SS;
1122932SN/A    }
1131681SN/A}
1142932SN/A
1152932SN/Avoid EmulEnv::setSeg(const ExtMachInst & machInst)
1162932SN/A{
1179921Syasuko.eckert@amd.com    seg = SEGMENT_REG_DS;
1189921Syasuko.eckert@amd.com    //Handle any segment override that might have been in the instruction
1199921Syasuko.eckert@amd.com    int segFromInst = machInst.legacy.seg;
1209921Syasuko.eckert@amd.com    if (segFromInst)
1219921Syasuko.eckert@amd.com        seg = (SegmentRegIndex)(segFromInst - 1);
1229921Syasuko.eckert@amd.com}
1239921Syasuko.eckert@amd.com