New directory structure:
[gem5.git] / src / sim / pseudo_inst.cc
1 /*
2 * Copyright (c) 2003-2006 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <unistd.h>
32
33 #include <string>
34
35 #include "sim/pseudo_inst.hh"
36 #include "arch/vtophys.hh"
37 #include "cpu/base.hh"
38 #include "cpu/sampler/sampler.hh"
39 #include "cpu/exec_context.hh"
40 #include "kern/kernel_stats.hh"
41 #include "sim/param.hh"
42 #include "sim/serialize.hh"
43 #include "sim/sim_exit.hh"
44 #include "sim/stat_control.hh"
45 #include "sim/stats.hh"
46 #include "sim/system.hh"
47 #include "sim/debug.hh"
48 #include "sim/vptr.hh"
49
50 using namespace std;
51
52 extern Sampler *SampCPU;
53
54 using namespace Stats;
55 using namespace TheISA;
56
57 namespace AlphaPseudo
58 {
59 bool doStatisticsInsts;
60 bool doCheckpointInsts;
61 bool doQuiesce;
62
63 void
64 arm(ExecContext *xc)
65 {
66 xc->getCpuPtr()->kernelStats->arm();
67 }
68
69 void
70 quiesce(ExecContext *xc)
71 {
72 if (!doQuiesce)
73 return;
74
75 xc->suspend();
76 xc->getCpuPtr()->kernelStats->quiesce();
77 }
78
79 void
80 quiesceNs(ExecContext *xc, uint64_t ns)
81 {
82 if (!doQuiesce || ns == 0)
83 return;
84
85 Event *quiesceEvent = xc->getQuiesceEvent();
86
87 if (quiesceEvent->scheduled())
88 quiesceEvent->reschedule(curTick + Clock::Int::ns * ns);
89 else
90 quiesceEvent->schedule(curTick + Clock::Int::ns * ns);
91
92 xc->suspend();
93 xc->getCpuPtr()->kernelStats->quiesce();
94 }
95
96 void
97 quiesceCycles(ExecContext *xc, uint64_t cycles)
98 {
99 if (!doQuiesce || cycles == 0)
100 return;
101
102 Event *quiesceEvent = xc->getQuiesceEvent();
103
104 if (quiesceEvent->scheduled())
105 quiesceEvent->reschedule(curTick +
106 xc->getCpuPtr()->cycles(cycles));
107 else
108 quiesceEvent->schedule(curTick +
109 xc->getCpuPtr()->cycles(cycles));
110
111 xc->suspend();
112 xc->getCpuPtr()->kernelStats->quiesce();
113 }
114
115 uint64_t
116 quiesceTime(ExecContext *xc)
117 {
118 return (xc->readLastActivate() - xc->readLastSuspend()) / Clock::Int::ns;
119 }
120
121 void
122 ivlb(ExecContext *xc)
123 {
124 xc->getCpuPtr()->kernelStats->ivlb();
125 }
126
127 void
128 ivle(ExecContext *xc)
129 {
130 }
131
132 void
133 m5exit_old(ExecContext *xc)
134 {
135 SimExit(curTick, "m5_exit_old instruction encountered");
136 }
137
138 void
139 m5exit(ExecContext *xc, Tick delay)
140 {
141 Tick when = curTick + delay * Clock::Int::ns;
142 SimExit(when, "m5_exit instruction encountered");
143 }
144
145 void
146 resetstats(ExecContext *xc, Tick delay, Tick period)
147 {
148 if (!doStatisticsInsts)
149 return;
150
151
152 Tick when = curTick + delay * Clock::Int::ns;
153 Tick repeat = period * Clock::Int::ns;
154
155 using namespace Stats;
156 SetupEvent(Reset, when, repeat);
157 }
158
159 void
160 dumpstats(ExecContext *xc, Tick delay, Tick period)
161 {
162 if (!doStatisticsInsts)
163 return;
164
165
166 Tick when = curTick + delay * Clock::Int::ns;
167 Tick repeat = period * Clock::Int::ns;
168
169 using namespace Stats;
170 SetupEvent(Dump, when, repeat);
171 }
172
173 void
174 addsymbol(ExecContext *xc, Addr addr, Addr symbolAddr)
175 {
176 char symb[100];
177 CopyStringOut(xc, symb, symbolAddr, 100);
178 std::string symbol(symb);
179
180 DPRINTF(Loader, "Loaded symbol: %s @ %#llx\n", symbol, addr);
181
182 xc->getSystemPtr()->kernelSymtab->insert(addr,symbol);
183 }
184
185 void
186 dumpresetstats(ExecContext *xc, Tick delay, Tick period)
187 {
188 if (!doStatisticsInsts)
189 return;
190
191
192 Tick when = curTick + delay * Clock::Int::ns;
193 Tick repeat = period * Clock::Int::ns;
194
195 using namespace Stats;
196 SetupEvent(Dump|Reset, when, repeat);
197 }
198
199 void
200 m5checkpoint(ExecContext *xc, Tick delay, Tick period)
201 {
202 if (!doCheckpointInsts)
203 return;
204
205
206 Tick when = curTick + delay * Clock::Int::ns;
207 Tick repeat = period * Clock::Int::ns;
208
209 Checkpoint::setup(when, repeat);
210 }
211
212 uint64_t
213 readfile(ExecContext *xc, Addr vaddr, uint64_t len, uint64_t offset)
214 {
215 const string &file = xc->getCpuPtr()->system->params()->readfile;
216 if (file.empty()) {
217 return ULL(0);
218 }
219
220 uint64_t result = 0;
221
222 int fd = ::open(file.c_str(), O_RDONLY, 0);
223 if (fd < 0)
224 panic("could not open file %s\n", file);
225
226 if (::lseek(fd, offset, SEEK_SET) < 0)
227 panic("could not seek: %s", strerror(errno));
228
229 char *buf = new char[len];
230 char *p = buf;
231 while (len > 0) {
232 int bytes = ::read(fd, p, len);
233 if (bytes <= 0)
234 break;
235
236 p += bytes;
237 result += bytes;
238 len -= bytes;
239 }
240
241 close(fd);
242 CopyIn(xc, vaddr, buf, result);
243 delete [] buf;
244 return result;
245 }
246
247 class Context : public ParamContext
248 {
249 public:
250 Context(const string &section) : ParamContext(section) {}
251 void checkParams();
252 };
253
254 Context context("pseudo_inst");
255
256 Param<bool> __quiesce(&context, "quiesce",
257 "enable quiesce instructions",
258 true);
259 Param<bool> __statistics(&context, "statistics",
260 "enable statistics pseudo instructions",
261 true);
262 Param<bool> __checkpoint(&context, "checkpoint",
263 "enable checkpoint pseudo instructions",
264 true);
265
266 void
267 Context::checkParams()
268 {
269 doQuiesce = __quiesce;
270 doStatisticsInsts = __statistics;
271 doCheckpointInsts = __checkpoint;
272 }
273
274 void debugbreak(ExecContext *xc)
275 {
276 debug_break();
277 }
278
279 void switchcpu(ExecContext *xc)
280 {
281 if (SampCPU)
282 SampCPU->switchCPUs();
283 }
284 }