Merge with head, hopefully the last time for this batch.
[gem5.git] / src / sim / system.cc
1 /*
2 * Copyright (c) 2011-2012 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Copyright (c) 2003-2006 The Regents of The University of Michigan
15 * Copyright (c) 2011 Regents of the University of California
16 * All rights reserved.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions are
20 * met: redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer;
22 * redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution;
25 * neither the name of the copyright holders nor the names of its
26 * contributors may be used to endorse or promote products derived from
27 * this software without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Authors: Steve Reinhardt
42 * Lisa Hsu
43 * Nathan Binkert
44 * Ali Saidi
45 * Rick Strong
46 */
47
48 #include "arch/isa_traits.hh"
49 #include "arch/remote_gdb.hh"
50 #include "arch/utility.hh"
51 #include "arch/vtophys.hh"
52 #include "base/loader/object_file.hh"
53 #include "base/loader/symtab.hh"
54 #include "base/trace.hh"
55 #include "config/the_isa.hh"
56 #include "cpu/thread_context.hh"
57 #include "debug/Loader.hh"
58 #include "debug/WorkItems.hh"
59 #include "kern/kernel_stats.hh"
60 #include "mem/fs_translating_port_proxy.hh"
61 #include "mem/mem_object.hh"
62 #include "mem/physical.hh"
63 #include "params/System.hh"
64 #include "sim/byteswap.hh"
65 #include "sim/debug.hh"
66 #include "sim/full_system.hh"
67 #include "sim/system.hh"
68
69 using namespace std;
70 using namespace TheISA;
71
72 vector<System *> System::systemList;
73
74 int System::numSystemsRunning = 0;
75
76 System::System(Params *p)
77 : MemObject(p), _systemPort("system_port", this),
78 physmem(p->physmem),
79 _numContexts(0),
80 init_param(p->init_param),
81 loadAddrMask(p->load_addr_mask),
82 nextPID(0),
83 memoryMode(p->mem_mode),
84 workItemsBegin(0),
85 workItemsEnd(0),
86 numWorkIds(p->num_work_ids),
87 _params(p),
88 totalNumInsts(0),
89 instEventQueue("system instruction-based event queue")
90 {
91 // add self to global system list
92 systemList.push_back(this);
93
94 /** Keep track of all memories we can execute code out of
95 * in our system
96 */
97 for (int x = 0; x < p->memories.size(); x++) {
98 if (!p->memories[x])
99 continue;
100 memRanges.push_back(RangeSize(p->memories[x]->start(),
101 p->memories[x]->size()));
102 }
103
104 if (FullSystem) {
105 kernelSymtab = new SymbolTable;
106 if (!debugSymbolTable)
107 debugSymbolTable = new SymbolTable;
108
109 /**
110 * Get a port proxy to memory
111 */
112 physProxy = new PortProxy(*getSystemPort());
113 virtProxy = new FSTranslatingPortProxy(*getSystemPort());
114 }
115 }
116
117 System::~System()
118 {
119 delete kernelSymtab;
120 delete kernel;
121
122 for (uint32_t j = 0; j < numWorkIds; j++)
123 delete workItemStats[j];
124 }
125
126 void
127 System::init()
128 {
129 // check that the system port is connected
130 if (!_systemPort.isConnected())
131 panic("System port on %s is not connected.\n", name());
132 }
133
134 Port*
135 System::getPort(const std::string &if_name, int idx)
136 {
137 // no need to distinguish at the moment (besides checking)
138 return &_systemPort;
139 }
140
141 void
142 System::setMemoryMode(Enums::MemoryMode mode)
143 {
144 assert(getState() == Drained);
145 memoryMode = mode;
146 }
147
148 bool System::breakpoint()
149 {
150 if (remoteGDB.size())
151 return remoteGDB[0]->breakpoint();
152 return false;
153 }
154
155 /**
156 * Setting rgdb_wait to a positive integer waits for a remote debugger to
157 * connect to that context ID before continuing. This should really
158 be a parameter on the CPU object or something...
159 */
160 int rgdb_wait = -1;
161
162 int
163 System::registerThreadContext(ThreadContext *tc, int assigned)
164 {
165 int id;
166 if (assigned == -1) {
167 for (id = 0; id < threadContexts.size(); id++) {
168 if (!threadContexts[id])
169 break;
170 }
171
172 if (threadContexts.size() <= id)
173 threadContexts.resize(id + 1);
174 } else {
175 if (threadContexts.size() <= assigned)
176 threadContexts.resize(assigned + 1);
177 id = assigned;
178 }
179
180 if (threadContexts[id])
181 fatal("Cannot have two CPUs with the same id (%d)\n", id);
182
183 threadContexts[id] = tc;
184 _numContexts++;
185
186 int port = getRemoteGDBPort();
187 if (port) {
188 RemoteGDB *rgdb = new RemoteGDB(this, tc);
189 GDBListener *gdbl = new GDBListener(rgdb, port + id);
190 gdbl->listen();
191
192 if (rgdb_wait != -1 && rgdb_wait == id)
193 gdbl->accept();
194
195 if (remoteGDB.size() <= id) {
196 remoteGDB.resize(id + 1);
197 }
198
199 remoteGDB[id] = rgdb;
200 }
201
202 activeCpus.push_back(false);
203
204 return id;
205 }
206
207 int
208 System::numRunningContexts()
209 {
210 int running = 0;
211 for (int i = 0; i < _numContexts; ++i) {
212 if (threadContexts[i]->status() != ThreadContext::Halted)
213 ++running;
214 }
215 return running;
216 }
217
218 void
219 System::initState()
220 {
221 int i;
222 if (FullSystem) {
223 for (i = 0; i < threadContexts.size(); i++)
224 TheISA::startupCPU(threadContexts[i], i);
225 // Moved from the constructor to here since it relies on the
226 // address map being resolved in the interconnect
227 /**
228 * Load the kernel code into memory
229 */
230 if (params()->kernel == "") {
231 inform("No kernel set for full system simulation. "
232 "Assuming you know what you're doing...\n");
233 } else {
234 // Load kernel code
235 kernel = createObjectFile(params()->kernel);
236 inform("kernel located at: %s", params()->kernel);
237
238 if (kernel == NULL)
239 fatal("Could not load kernel file %s", params()->kernel);
240
241 // Load program sections into memory
242 kernel->loadSections(physProxy, loadAddrMask);
243
244 // setup entry points
245 kernelStart = kernel->textBase();
246 kernelEnd = kernel->bssBase() + kernel->bssSize();
247 kernelEntry = kernel->entryPoint();
248
249 // load symbols
250 if (!kernel->loadGlobalSymbols(kernelSymtab))
251 fatal("could not load kernel symbols\n");
252
253 if (!kernel->loadLocalSymbols(kernelSymtab))
254 fatal("could not load kernel local symbols\n");
255
256 if (!kernel->loadGlobalSymbols(debugSymbolTable))
257 fatal("could not load kernel symbols\n");
258
259 if (!kernel->loadLocalSymbols(debugSymbolTable))
260 fatal("could not load kernel local symbols\n");
261
262 DPRINTF(Loader, "Kernel start = %#x\n", kernelStart);
263 DPRINTF(Loader, "Kernel end = %#x\n", kernelEnd);
264 DPRINTF(Loader, "Kernel entry = %#x\n", kernelEntry);
265 DPRINTF(Loader, "Kernel loaded...\n");
266 }
267 }
268
269 // increment the number of running systms
270 numSystemsRunning++;
271
272 activeCpus.clear();
273
274 if (!FullSystem)
275 return;
276
277 for (i = 0; i < threadContexts.size(); i++)
278 TheISA::startupCPU(threadContexts[i], i);
279 }
280
281 void
282 System::replaceThreadContext(ThreadContext *tc, int context_id)
283 {
284 if (context_id >= threadContexts.size()) {
285 panic("replaceThreadContext: bad id, %d >= %d\n",
286 context_id, threadContexts.size());
287 }
288
289 threadContexts[context_id] = tc;
290 if (context_id < remoteGDB.size())
291 remoteGDB[context_id]->replaceThreadContext(tc);
292 }
293
294 Addr
295 System::allocPhysPages(int npages)
296 {
297 Addr return_addr = pagePtr << LogVMPageSize;
298 pagePtr += npages;
299 if (return_addr >= physmem->size())
300 fatal("Out of memory, please increase size of physical memory.");
301 return return_addr;
302 }
303
304 Addr
305 System::memSize()
306 {
307 return physmem->size();
308 }
309
310 Addr
311 System::freeMemSize()
312 {
313 return physmem->size() - (pagePtr << LogVMPageSize);
314 }
315
316 bool
317 System::isMemory(const Addr addr) const
318 {
319 std::list<Range<Addr> >::const_iterator i;
320 for (i = memRanges.begin(); i != memRanges.end(); i++) {
321 if (*i == addr)
322 return true;
323 }
324 return false;
325 }
326
327 void
328 System::resume()
329 {
330 SimObject::resume();
331 totalNumInsts = 0;
332 }
333
334 void
335 System::serialize(ostream &os)
336 {
337 if (FullSystem)
338 kernelSymtab->serialize("kernel_symtab", os);
339 SERIALIZE_SCALAR(pagePtr);
340 SERIALIZE_SCALAR(nextPID);
341 }
342
343
344 void
345 System::unserialize(Checkpoint *cp, const string &section)
346 {
347 if (FullSystem)
348 kernelSymtab->unserialize("kernel_symtab", cp, section);
349 UNSERIALIZE_SCALAR(pagePtr);
350 UNSERIALIZE_SCALAR(nextPID);
351 }
352
353 void
354 System::regStats()
355 {
356 for (uint32_t j = 0; j < numWorkIds ; j++) {
357 workItemStats[j] = new Stats::Histogram();
358 stringstream namestr;
359 ccprintf(namestr, "work_item_type%d", j);
360 workItemStats[j]->init(20)
361 .name(name() + "." + namestr.str())
362 .desc("Run time stat for" + namestr.str())
363 .prereq(*workItemStats[j]);
364 }
365 }
366
367 void
368 System::workItemEnd(uint32_t tid, uint32_t workid)
369 {
370 std::pair<uint32_t,uint32_t> p(tid, workid);
371 if (!lastWorkItemStarted.count(p))
372 return;
373
374 Tick samp = curTick() - lastWorkItemStarted[p];
375 DPRINTF(WorkItems, "Work item end: %d\t%d\t%lld\n", tid, workid, samp);
376
377 if (workid >= numWorkIds)
378 fatal("Got workid greater than specified in system configuration\n");
379
380 workItemStats[workid]->sample(samp);
381 lastWorkItemStarted.erase(p);
382 }
383
384 void
385 System::printSystems()
386 {
387 vector<System *>::iterator i = systemList.begin();
388 vector<System *>::iterator end = systemList.end();
389 for (; i != end; ++i) {
390 System *sys = *i;
391 cerr << "System " << sys->name() << ": " << hex << sys << endl;
392 }
393 }
394
395 void
396 printSystems()
397 {
398 System::printSystems();
399 }
400
401 const char *System::MemoryModeStrings[3] = {"invalid", "atomic",
402 "timing"};
403
404 System *
405 SystemParams::create()
406 {
407 return new System(this);
408 }