gem5  v20.1.0.0
utility.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2007 The Hewlett-Packard Development Company
3  * Copyright (c) 2011 Advanced Micro Devices, Inc.
4  * All rights reserved.
5  *
6  * The license below extends only to copyright in the software and shall
7  * not be construed as granting a license to any other intellectual
8  * property including but not limited to intellectual property relating
9  * to a hardware implementation of the functionality of the software
10  * licensed hereunder. You may use the software subject to the license
11  * terms below provided that you ensure that this notice is replicated
12  * unmodified and in its entirety in all distributions of the software,
13  * modified or unmodified, in source code or in binary form.
14  *
15  * Redistribution and use in source and binary forms, with or without
16  * modification, are permitted provided that the following conditions are
17  * met: redistributions of source code must retain the above copyright
18  * notice, this list of conditions and the following disclaimer;
19  * redistributions in binary form must reproduce the above copyright
20  * notice, this list of conditions and the following disclaimer in the
21  * documentation and/or other materials provided with the distribution;
22  * neither the name of the copyright holders nor the names of its
23  * contributors may be used to endorse or promote products derived from
24  * this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #include "arch/x86/utility.hh"
40 
41 #include "arch/x86/interrupts.hh"
42 #include "arch/x86/registers.hh"
43 #include "arch/x86/x86_traits.hh"
44 #include "cpu/base.hh"
45 #include "fputils/fp80.h"
46 #include "sim/full_system.hh"
47 
48 namespace X86ISA {
49 
50 uint64_t
51 getArgument(ThreadContext *tc, int &number, uint16_t size, bool fp)
52 {
53  if (fp) {
54  panic("getArgument(): Floating point arguments not implemented\n");
55  } else if (size != 8) {
56  panic("getArgument(): Can only handle 64-bit arguments.\n");
57  }
58 
59  // The first 6 integer arguments are passed in registers, the rest
60  // are passed on the stack.
61  const int int_reg_map[] = {
62  INTREG_RDI, INTREG_RSI, INTREG_RDX,
63  INTREG_RCX, INTREG_R8, INTREG_R9
64  };
65  if (number < sizeof(int_reg_map) / sizeof(*int_reg_map)) {
66  return tc->readIntReg(int_reg_map[number]);
67  } else {
68  panic("getArgument(): Don't know how to handle stack arguments.\n");
69  }
70 }
71 
72 void
74 {
75  // This function assumes no side effects other than TLB invalidation
76  // need to be considered while copying state. That will likely not be
77  // true in the future.
78  for (int i = 0; i < NUM_MISCREGS; ++i) {
79  if (!isValidMiscReg(i))
80  continue;
81 
83  }
84 
85  // The TSC has to be updated with side-effects if the CPUs in a
86  // CPU switch have different frequencies.
88 
89  dest->getITBPtr()->flushAll();
90  dest->getDTBPtr()->flushAll();
91 }
92 
93 void
95 {
96  //copy int regs
97  for (int i = 0; i < NumIntRegs; ++i)
98  dest->setIntRegFlat(i, src->readIntRegFlat(i));
99  //copy float regs
100  for (int i = 0; i < NumFloatRegs; ++i)
101  dest->setFloatRegFlat(i, src->readFloatRegFlat(i));
102  //copy condition-code regs
103  for (int i = 0; i < NumCCRegs; ++i)
104  dest->setCCRegFlat(i, src->readCCRegFlat(i));
105  copyMiscRegs(src, dest);
106  dest->pcState(src->pcState());
107 }
108 
109 uint64_t
111 {
112  const uint64_t ncc_flags(tc->readMiscRegNoEffect(MISCREG_RFLAGS));
113  const uint64_t cc_flags(tc->readCCReg(X86ISA::CCREG_ZAPS));
114  const uint64_t cfof_bits(tc->readCCReg(X86ISA::CCREG_CFOF));
115  const uint64_t df_bit(tc->readCCReg(X86ISA::CCREG_DF));
116  // ecf (PSEUDO(3)) & ezf (PSEUDO(4)) are only visible to
117  // microcode, so we can safely ignore them.
118 
119  // Reconstruct the real rflags state, mask out internal flags, and
120  // make sure reserved bits have the expected values.
121  return ((ncc_flags | cc_flags | cfof_bits | df_bit) & 0x3F7FD5)
122  | 0x2;
123 }
124 
125 void
127 {
131 
132  // Internal microcode registers (ECF & EZF)
133  tc->setCCReg(X86ISA::CCREG_ECF, 0);
134  tc->setCCReg(X86ISA::CCREG_EZF, 0);
135 
136  // Update the RFLAGS misc reg with whatever didn't go into the
137  // magic registers.
139 }
140 
141 uint8_t
142 convX87TagsToXTags(uint16_t ftw)
143 {
144  uint8_t ftwx(0);
145  for (int i = 0; i < 8; ++i) {
146  // Extract the tag for the current element on the FP stack
147  const unsigned tag((ftw >> (2 * i)) & 0x3);
148 
149  /*
150  * Check the type of the current FP element. Valid values are:
151  * 0 == Valid
152  * 1 == Zero
153  * 2 == Special (Nan, unsupported, infinity, denormal)
154  * 3 == Empty
155  */
156  // The xsave version of the tag word only keeps track of
157  // whether the element is empty or not. Set the corresponding
158  // bit in the ftwx if it's not empty,
159  if (tag != 0x3)
160  ftwx |= 1 << i;
161  }
162 
163  return ftwx;
164 }
165 
166 uint16_t
167 convX87XTagsToTags(uint8_t ftwx)
168 {
169  uint16_t ftw(0);
170  for (int i = 0; i < 8; ++i) {
171  const unsigned xtag(((ftwx >> i) & 0x1));
172 
173  // The xtag for an x87 stack position is 0 for empty stack positions.
174  if (!xtag) {
175  // Set the tag word to 3 (empty) for the current element.
176  ftw |= 0x3 << (2 * i);
177  } else {
178  // TODO: We currently assume that non-empty elements are
179  // valid (0x0), but we should ideally reconstruct the full
180  // state (valid/zero/special).
181  }
182  }
183 
184  return ftw;
185 }
186 
187 uint16_t
188 genX87Tags(uint16_t ftw, uint8_t top, int8_t spm)
189 {
190  const uint8_t new_top((top + spm + 8) % 8);
191 
192  if (spm > 0) {
193  // Removing elements from the stack. Flag the elements as empty.
194  for (int i = top; i != new_top; i = (i + 1 + 8) % 8)
195  ftw |= 0x3 << (2 * i);
196  } else if (spm < 0) {
197  // Adding elements to the stack. Flag the new elements as
198  // valid. We should ideally decode them and "do the right
199  // thing".
200  for (int i = new_top; i != top; i = (i + 1 + 8) % 8)
201  ftw &= ~(0x3 << (2 * i));
202  }
203 
204  return ftw;
205 }
206 
207 double
208 loadFloat80(const void *_mem)
209 {
210  fp80_t fp80;
211  memcpy(fp80.bits, _mem, 10);
212 
213  return fp80_cvtd(fp80);
214 }
215 
216 void
217 storeFloat80(void *_mem, double value)
218 {
219  fp80_t fp80 = fp80_cvfd(value);
220  memcpy(_mem, fp80.bits, 10);
221 }
222 
223 } // namespace X86_ISA
X86ISA::CCREG_ZAPS
@ CCREG_ZAPS
Definition: ccr.hh:47
ThreadContext::readMiscRegNoEffect
virtual RegVal readMiscRegNoEffect(RegIndex misc_reg) const =0
X86ISA::convX87TagsToXTags
uint8_t convX87TagsToXTags(uint16_t ftw)
Convert an x87 tag word to abridged tag format.
Definition: utility.cc:142
ThreadContext::readIntRegFlat
virtual RegVal readIntRegFlat(RegIndex idx) const =0
Flat register interfaces.
X86ISA::genX87Tags
uint16_t genX87Tags(uint16_t ftw, uint8_t top, int8_t spm)
Generate and updated x87 tag register after a push/pop operation.
Definition: utility.cc:188
ArmISA::fp
Bitfield< 19, 16 > fp
Definition: miscregs_types.hh:173
x86_traits.hh
X86ISA::NumIntRegs
const int NumIntRegs
Definition: registers.hh:58
registers.hh
ArmISA::INTREG_R8
@ INTREG_R8
Definition: intregs.hh:62
ArmISA::i
Bitfield< 7 > i
Definition: miscregs_types.hh:63
ArmISA::INTREG_R9
@ INTREG_R9
Definition: intregs.hh:63
interrupts.hh
X86ISA::loadFloat80
double loadFloat80(const void *_mem)
Load an 80-bit float from memory and convert it to double.
Definition: utility.cc:208
top
Definition: test.h:61
X86ISA::copyRegs
void copyRegs(ThreadContext *src, ThreadContext *dest)
Definition: utility.cc:94
X86ISA::convX87XTagsToTags
uint16_t convX87XTagsToTags(uint8_t ftwx)
Convert an x87 xtag word to normal tags format.
Definition: utility.cc:167
X86ISA::CCREG_DF
@ CCREG_DF
Definition: ccr.hh:49
ThreadContext::setFloatRegFlat
virtual void setFloatRegFlat(RegIndex idx, RegVal val)=0
X86ISA::CCREG_CFOF
@ CCREG_CFOF
Definition: ccr.hh:48
X86ISA::storeFloat80
void storeFloat80(void *_mem, double value)
Convert and store a double as an 80-bit float.
Definition: utility.cc:217
ThreadContext::readCCRegFlat
virtual RegVal readCCRegFlat(RegIndex idx) const =0
X86ISA::NUM_MISCREGS
@ NUM_MISCREGS
Definition: misc.hh:398
ThreadContext
ThreadContext is the external interface to all thread state for anything outside of the CPU.
Definition: thread_context.hh:88
X86ISA::MISCREG_RFLAGS
@ MISCREG_RFLAGS
Definition: misc.hh:134
ThreadContext::setIntRegFlat
virtual void setIntRegFlat(RegIndex idx, RegVal val)=0
X86ISA::ccFlagMask
const uint32_t ccFlagMask
Definition: misc.hh:67
X86ISA::isValidMiscReg
static bool isValidMiscReg(int index)
Definition: misc.hh:402
X86ISA::NumFloatRegs
const int NumFloatRegs
Definition: registers.hh:63
utility.hh
X86ISA::setRFlags
void setRFlags(ThreadContext *tc, uint64_t val)
Set update the rflags register and internal gem5 state.
Definition: utility.cc:126
ThreadContext::readFloatRegFlat
virtual RegVal readFloatRegFlat(RegIndex idx) const =0
X86ISA::val
Bitfield< 63 > val
Definition: misc.hh:769
X86ISA
This is exposed globally, independent of the ISA.
Definition: acpi.hh:55
X86ISA::DFBit
@ DFBit
Definition: misc.hh:62
ThreadContext::getITBPtr
virtual BaseTLB * getITBPtr()=0
full_system.hh
ThreadContext::pcState
virtual TheISA::PCState pcState() const =0
X86ISA::getRFlags
uint64_t getRFlags(ThreadContext *tc)
Reconstruct the rflags register from the internal gem5 register state.
Definition: utility.cc:110
ThreadContext::readCCReg
virtual RegVal readCCReg(RegIndex reg_idx) const =0
ThreadContext::setCCReg
virtual void setCCReg(RegIndex reg_idx, RegVal val)=0
ThreadContext::setCCRegFlat
virtual void setCCRegFlat(RegIndex idx, RegVal val)=0
base.hh
ThreadContext::readMiscReg
virtual RegVal readMiscReg(RegIndex misc_reg)=0
X86ISA::getArgument
uint64_t getArgument(ThreadContext *tc, int &number, uint16_t size, bool fp)
Definition: utility.cc:51
ThreadContext::setMiscReg
virtual void setMiscReg(RegIndex misc_reg, RegVal val)=0
X86ISA::CCREG_EZF
@ CCREG_EZF
Definition: ccr.hh:51
ThreadContext::setMiscRegNoEffect
virtual void setMiscRegNoEffect(RegIndex misc_reg, RegVal val)=0
ThreadContext::readIntReg
virtual RegVal readIntReg(RegIndex reg_idx) const =0
X86ISA::cfofMask
const uint32_t cfofMask
Definition: misc.hh:66
X86ISA::copyMiscRegs
void copyMiscRegs(ThreadContext *src, ThreadContext *dest)
Definition: utility.cc:73
X86ISA::MISCREG_TSC
@ MISCREG_TSC
Definition: misc.hh:143
BaseTLB::flushAll
virtual void flushAll()=0
Remove all entries from the TLB.
ThreadContext::getDTBPtr
virtual BaseTLB * getDTBPtr()=0
X86ISA::NumCCRegs
const int NumCCRegs
Definition: registers.hh:59
panic
#define panic(...)
This implements a cprintf based panic() function.
Definition: logging.hh:171
X86ISA::CCREG_ECF
@ CCREG_ECF
Definition: ccr.hh:50

Generated on Wed Sep 30 2020 14:02:01 for gem5 by doxygen 1.8.17