gem5  v19.0.0.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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  * Authors: Gabe Black
39  */
40 
41 #include "arch/x86/utility.hh"
42 
43 #include "arch/x86/interrupts.hh"
44 #include "arch/x86/registers.hh"
45 #include "arch/x86/x86_traits.hh"
46 #include "cpu/base.hh"
47 #include "fputils/fp80.h"
48 #include "sim/full_system.hh"
49 
50 namespace X86ISA {
51 
52 uint64_t
53 getArgument(ThreadContext *tc, int &number, uint16_t size, bool fp)
54 {
55  if (fp) {
56  panic("getArgument(): Floating point arguments not implemented\n");
57  } else if (size != 8) {
58  panic("getArgument(): Can only handle 64-bit arguments.\n");
59  }
60 
61  // The first 6 integer arguments are passed in registers, the rest
62  // are passed on the stack.
63  const int int_reg_map[] = {
64  INTREG_RDI, INTREG_RSI, INTREG_RDX,
65  INTREG_RCX, INTREG_R8, INTREG_R9
66  };
67  if (number < sizeof(int_reg_map) / sizeof(*int_reg_map)) {
68  return tc->readIntReg(int_reg_map[number]);
69  } else {
70  panic("getArgument(): Don't know how to handle stack arguments.\n");
71  }
72 }
73 
74 void
76 {
77  // This function assumes no side effects other than TLB invalidation
78  // need to be considered while copying state. That will likely not be
79  // true in the future.
80  for (int i = 0; i < NUM_MISCREGS; ++i) {
81  if (!isValidMiscReg(i))
82  continue;
83 
85  }
86 
87  // The TSC has to be updated with side-effects if the CPUs in a
88  // CPU switch have different frequencies.
90 
91  dest->getITBPtr()->flushAll();
92  dest->getDTBPtr()->flushAll();
93 }
94 
95 void
97 {
98  //copy int regs
99  for (int i = 0; i < NumIntRegs; ++i)
100  dest->setIntRegFlat(i, src->readIntRegFlat(i));
101  //copy float regs
102  for (int i = 0; i < NumFloatRegs; ++i)
103  dest->setFloatRegFlat(i, src->readFloatRegFlat(i));
104  //copy condition-code regs
105  for (int i = 0; i < NumCCRegs; ++i)
106  dest->setCCRegFlat(i, src->readCCRegFlat(i));
107  copyMiscRegs(src, dest);
108  dest->pcState(src->pcState());
109 }
110 
111 void
113 {
114  panic("Not implemented for x86\n");
115 }
116 
117 uint64_t
119 {
120  const uint64_t ncc_flags(tc->readMiscRegNoEffect(MISCREG_RFLAGS));
121  const uint64_t cc_flags(tc->readCCReg(X86ISA::CCREG_ZAPS));
122  const uint64_t cfof_bits(tc->readCCReg(X86ISA::CCREG_CFOF));
123  const uint64_t df_bit(tc->readCCReg(X86ISA::CCREG_DF));
124  // ecf (PSEUDO(3)) & ezf (PSEUDO(4)) are only visible to
125  // microcode, so we can safely ignore them.
126 
127  // Reconstruct the real rflags state, mask out internal flags, and
128  // make sure reserved bits have the expected values.
129  return ((ncc_flags | cc_flags | cfof_bits | df_bit) & 0x3F7FD5)
130  | 0x2;
131 }
132 
133 void
135 {
138  tc->setCCReg(X86ISA::CCREG_DF, val & DFBit);
139 
140  // Internal microcode registers (ECF & EZF)
141  tc->setCCReg(X86ISA::CCREG_ECF, 0);
142  tc->setCCReg(X86ISA::CCREG_EZF, 0);
143 
144  // Update the RFLAGS misc reg with whatever didn't go into the
145  // magic registers.
146  tc->setMiscReg(MISCREG_RFLAGS, val & ~(ccFlagMask | cfofMask | DFBit));
147 }
148 
149 uint8_t
150 convX87TagsToXTags(uint16_t ftw)
151 {
152  uint8_t ftwx(0);
153  for (int i = 0; i < 8; ++i) {
154  // Extract the tag for the current element on the FP stack
155  const unsigned tag((ftw >> (2 * i)) & 0x3);
156 
157  /*
158  * Check the type of the current FP element. Valid values are:
159  * 0 == Valid
160  * 1 == Zero
161  * 2 == Special (Nan, unsupported, infinity, denormal)
162  * 3 == Empty
163  */
164  // The xsave version of the tag word only keeps track of
165  // whether the element is empty or not. Set the corresponding
166  // bit in the ftwx if it's not empty,
167  if (tag != 0x3)
168  ftwx |= 1 << i;
169  }
170 
171  return ftwx;
172 }
173 
174 uint16_t
175 convX87XTagsToTags(uint8_t ftwx)
176 {
177  uint16_t ftw(0);
178  for (int i = 0; i < 8; ++i) {
179  const unsigned xtag(((ftwx >> i) & 0x1));
180 
181  // The xtag for an x87 stack position is 0 for empty stack positions.
182  if (!xtag) {
183  // Set the tag word to 3 (empty) for the current element.
184  ftw |= 0x3 << (2 * i);
185  } else {
186  // TODO: We currently assume that non-empty elements are
187  // valid (0x0), but we should ideally reconstruct the full
188  // state (valid/zero/special).
189  }
190  }
191 
192  return ftw;
193 }
194 
195 uint16_t
196 genX87Tags(uint16_t ftw, uint8_t top, int8_t spm)
197 {
198  const uint8_t new_top((top + spm + 8) % 8);
199 
200  if (spm > 0) {
201  // Removing elements from the stack. Flag the elements as empty.
202  for (int i = top; i != new_top; i = (i + 1 + 8) % 8)
203  ftw |= 0x3 << (2 * i);
204  } else if (spm < 0) {
205  // Adding elements to the stack. Flag the new elements as
206  // valid. We should ideally decode them and "do the right
207  // thing".
208  for (int i = new_top; i != top; i = (i + 1 + 8) % 8)
209  ftw &= ~(0x3 << (2 * i));
210  }
211 
212  return ftw;
213 }
214 
215 double
216 loadFloat80(const void *_mem)
217 {
218  fp80_t fp80;
219  memcpy(fp80.bits, _mem, 10);
220 
221  return fp80_cvtd(fp80);
222 }
223 
224 void
225 storeFloat80(void *_mem, double value)
226 {
227  fp80_t fp80 = fp80_cvfd(value);
228  memcpy(_mem, fp80.bits, 10);
229 }
230 
231 } // namespace X86_ISA
#define panic(...)
This implements a cprintf based panic() function.
Definition: logging.hh:167
void skipFunction(ThreadContext *tc)
Definition: utility.cc:112
virtual void flushAll()=0
Remove all entries from the TLB.
uint64_t getRFlags(ThreadContext *tc)
Reconstruct the rflags register from the internal gem5 register state.
Definition: utility.cc:118
virtual void setMiscReg(RegIndex misc_reg, RegVal val)=0
void copyMiscRegs(ThreadContext *src, ThreadContext *dest)
Definition: utility.cc:75
Definition: test.h:61
Bitfield< 7 > i
virtual BaseTLB * getDTBPtr()=0
virtual TheISA::PCState pcState() const =0
virtual RegVal readIntReg(RegIndex reg_idx) const =0
virtual void setMiscRegNoEffect(RegIndex misc_reg, RegVal val)=0
void storeFloat80(void *_mem, double value)
Convert and store a double as an 80-bit float.
Definition: utility.cc:225
virtual RegVal readCCReg(RegIndex reg_idx) const =0
static bool isValidMiscReg(int index)
Definition: misc.hh:404
const int NumFloatRegs
Definition: registers.hh:65
ThreadContext is the external interface to all thread state for anything outside of the CPU...
Bitfield< 63 > val
Definition: misc.hh:771
virtual RegVal readCCRegFlat(RegIndex idx) const =0
void copyRegs(ThreadContext *src, ThreadContext *dest)
Definition: utility.cc:96
uint64_t getArgument(ThreadContext *tc, int &number, uint16_t size, bool fp)
Definition: utility.cc:53
virtual BaseTLB * getITBPtr()=0
virtual void setFloatRegFlat(RegIndex idx, RegVal val)=0
uint16_t convX87XTagsToTags(uint8_t ftwx)
Convert an x87 xtag word to normal tags format.
Definition: utility.cc:175
void setRFlags(ThreadContext *tc, uint64_t val)
Set update the rflags register and internal gem5 state.
Definition: utility.cc:134
virtual void setCCReg(RegIndex reg_idx, RegVal val)=0
virtual void setCCRegFlat(RegIndex idx, RegVal val)=0
const int NumCCRegs
Definition: registers.hh:61
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:196
virtual RegVal readMiscRegNoEffect(RegIndex misc_reg) const =0
This is exposed globally, independent of the ISA.
Definition: acpi.hh:57
uint8_t convX87TagsToXTags(uint16_t ftw)
Convert an x87 tag word to abridged tag format.
Definition: utility.cc:150
const int NumIntRegs
Definition: registers.hh:60
virtual RegVal readIntRegFlat(RegIndex idx) const =0
Flat register interfaces.
virtual RegVal readFloatRegFlat(RegIndex idx) const =0
const uint32_t ccFlagMask
Definition: misc.hh:69
double loadFloat80(const void *_mem)
Load an 80-bit float from memory and convert it to double.
Definition: utility.cc:216
virtual void setIntRegFlat(RegIndex idx, RegVal val)=0
Bitfield< 19, 16 > fp
const uint32_t cfofMask
Definition: misc.hh:68
virtual RegVal readMiscReg(RegIndex misc_reg)=0

Generated on Fri Feb 28 2020 16:26:56 for gem5 by doxygen 1.8.13