gem5  v19.0.0.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
isa.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 RISC-V Foundation
3  * Copyright (c) 2016 The University of Virginia
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met: redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer;
10  * redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution;
13  * neither the name of the copyright holders nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  * Authors: Alec Roelke
30  */
31 #include "arch/riscv/isa.hh"
32 
33 #include <ctime>
34 #include <set>
35 #include <sstream>
36 
37 #include "arch/riscv/interrupts.hh"
38 #include "arch/riscv/registers.hh"
39 #include "base/bitfield.hh"
40 #include "cpu/base.hh"
41 #include "debug/RiscvMisc.hh"
42 #include "params/RiscvISA.hh"
43 #include "sim/core.hh"
44 #include "sim/pseudo_inst.hh"
45 
46 namespace RiscvISA
47 {
48 
50 {
51  miscRegFile.resize(NumMiscRegs);
52  clear();
53 }
54 
55 const RiscvISAParams *
56 ISA::params() const
57 {
58  return dynamic_cast<const Params *>(_params);
59 }
60 
61 void ISA::clear()
62 {
63  std::fill(miscRegFile.begin(), miscRegFile.end(), 0);
64 
66  miscRegFile[MISCREG_ISA] = (2ULL << MXL_OFFSET) | 0x14112D;
67  miscRegFile[MISCREG_VENDORID] = 0;
68  miscRegFile[MISCREG_ARCHID] = 0;
69  miscRegFile[MISCREG_IMPID] = 0;
70  miscRegFile[MISCREG_STATUS] = (2ULL << UXL_OFFSET) | (2ULL << SXL_OFFSET) |
71  (1ULL << FS_OFFSET);
72  miscRegFile[MISCREG_MCOUNTEREN] = 0x7;
73  miscRegFile[MISCREG_SCOUNTEREN] = 0x7;
74 }
75 
76 bool
77 ISA::hpmCounterEnabled(int misc_reg) const
78 {
79  int hpmcounter = misc_reg - MISCREG_CYCLE;
80  if (hpmcounter < 0 || hpmcounter > 31)
81  panic("Illegal HPM counter %d\n", hpmcounter);
82  int counteren;
84  case PRV_M:
85  return true;
86  case PRV_S:
87  counteren = MISCREG_MCOUNTEREN;
88  break;
89  case PRV_U:
90  counteren = MISCREG_SCOUNTEREN;
91  break;
92  default:
93  panic("Unknown privilege level %d\n", miscRegFile[MISCREG_PRV]);
94  return false;
95  }
96  return (miscRegFile[counteren] & (1ULL << (hpmcounter))) > 0;
97 }
98 
99 RegVal
100 ISA::readMiscRegNoEffect(int misc_reg) const
101 {
102  if (misc_reg > NumMiscRegs || misc_reg < 0) {
103  // Illegal CSR
104  panic("Illegal CSR index %#x\n", misc_reg);
105  return -1;
106  }
107  DPRINTF(RiscvMisc, "Reading MiscReg %d: %#llx.\n", misc_reg,
108  miscRegFile[misc_reg]);
109  return miscRegFile[misc_reg];
110 }
111 
112 RegVal
113 ISA::readMiscReg(int misc_reg, ThreadContext *tc)
114 {
115  switch (misc_reg) {
116  case MISCREG_HARTID:
117  return tc->contextId();
118  case MISCREG_CYCLE:
120  DPRINTF(RiscvMisc, "Cycle counter at: %llu.\n",
121  tc->getCpuPtr()->curCycle());
122  return tc->getCpuPtr()->curCycle();
123  } else {
124  warn("Cycle counter disabled.\n");
125  return 0;
126  }
127  case MISCREG_TIME:
129  DPRINTF(RiscvMisc, "Wall-clock counter at: %llu.\n",
130  std::time(nullptr));
131  return std::time(nullptr);
132  } else {
133  warn("Wall clock disabled.\n");
134  return 0;
135  }
136  case MISCREG_INSTRET:
138  DPRINTF(RiscvMisc, "Instruction counter at: %llu.\n",
139  tc->getCpuPtr()->totalInsts());
140  return tc->getCpuPtr()->totalInsts();
141  } else {
142  warn("Instruction counter disabled.\n");
143  return 0;
144  }
145  case MISCREG_IP:
146  {
147  auto ic = dynamic_cast<RiscvISA::Interrupts *>(
149  return ic->readIP();
150  }
151  case MISCREG_IE:
152  {
153  auto ic = dynamic_cast<RiscvISA::Interrupts *>(
155  return ic->readIE();
156  }
157  default:
158  // Try reading HPM counters
159  // As a placeholder, all HPM counters are just cycle counters
160  if (misc_reg >= MISCREG_HPMCOUNTER03 &&
161  misc_reg <= MISCREG_HPMCOUNTER31) {
162  if (hpmCounterEnabled(misc_reg)) {
163  DPRINTF(RiscvMisc, "HPM counter %d: %llu.\n",
164  misc_reg - MISCREG_CYCLE, tc->getCpuPtr()->curCycle());
165  return tc->getCpuPtr()->curCycle();
166  } else {
167  warn("HPM counter %d disabled.\n", misc_reg - MISCREG_CYCLE);
168  return 0;
169  }
170  }
171  return readMiscRegNoEffect(misc_reg);
172  }
173 }
174 
175 void
177 {
178  if (misc_reg > NumMiscRegs || misc_reg < 0) {
179  // Illegal CSR
180  panic("Illegal CSR index %#x\n", misc_reg);
181  }
182  DPRINTF(RiscvMisc, "Setting MiscReg %d to %#x.\n", misc_reg, val);
183  miscRegFile[misc_reg] = val;
184 }
185 
186 void
188 {
189  if (misc_reg >= MISCREG_CYCLE && misc_reg <= MISCREG_HPMCOUNTER31) {
190  // Ignore writes to HPM counters for now
191  warn("Ignoring write to %s.\n", CSRData.at(misc_reg).name);
192  } else {
193  switch (misc_reg) {
194  case MISCREG_IP:
195  {
196  auto ic = dynamic_cast<RiscvISA::Interrupts *>(
198  ic->setIP(val);
199  }
200  break;
201  case MISCREG_IE:
202  {
203  auto ic = dynamic_cast<RiscvISA::Interrupts *>(
205  ic->setIE(val);
206  }
207  break;
208  default:
209  setMiscRegNoEffect(misc_reg, val);
210  }
211  }
212 }
213 
214 }
215 
217 RiscvISAParams::create()
218 {
219  return new RiscvISA::ISA(this);
220 }
#define panic(...)
This implements a cprintf based panic() function.
Definition: logging.hh:167
#define DPRINTF(x,...)
Definition: trace.hh:229
bool hpmCounterEnabled(int counter) const
Definition: isa.cc:77
const off_t FS_OFFSET
Definition: registers.hh:652
RegVal readMiscReg(int misc_reg, ThreadContext *tc)
Definition: isa.cc:113
ISA(Params *p)
Definition: isa.cc:49
void setMiscReg(int misc_reg, RegVal val, ThreadContext *tc)
Definition: isa.cc:187
uint64_t RegVal
Definition: types.hh:168
virtual BaseCPU * getCpuPtr()=0
ThreadContext is the external interface to all thread state for anything outside of the CPU...
const Params * params() const
Definition: isa.cc:56
Bitfield< 0 > p
Bitfield< 63 > val
Definition: misc.hh:771
const int NumMiscRegs
Definition: registers.hh:264
virtual Counter totalInsts() const =0
const std::map< int, CSRMetadata > CSRData
Definition: registers.hh:438
Cycles curCycle() const
Determine the current cycle, corresponding to a tick aligned to a clock edge.
const off_t UXL_OFFSET
Definition: registers.hh:651
#define ULL(N)
uint64_t constant
Definition: types.hh:50
RegVal readMiscRegNoEffect(int misc_reg) const
Definition: isa.cc:100
const off_t SXL_OFFSET
Definition: registers.hh:650
virtual int threadId() const =0
const SimObjectParams * _params
Cached copy of the object parameters.
Definition: sim_object.hh:110
virtual ContextID contextId() const =0
BaseInterrupts * getInterruptController(ThreadID tid)
Definition: base.hh:226
RiscvISAParams Params
Definition: isa.hh:74
Definition: isa.hh:35
#define warn(...)
Definition: logging.hh:212
void clear()
Definition: isa.cc:61
std::vector< RegVal > miscRegFile
Definition: isa.hh:69
void setMiscRegNoEffect(int misc_reg, RegVal val)
Definition: isa.cc:176
bool ic(TxDesc *d)

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