Deleted Added
sdiff udiff text old ( 12100:5f19ea125548 ) new ( 12122:20512f6810d7 )
full compact
1/*
2 * Copyright (c) 2011-2014 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 "sim/system.hh"
49
50#include "arch/remote_gdb.hh"
51#include "arch/utility.hh"
52#include "base/loader/object_file.hh"
53#include "base/loader/symtab.hh"
54#include "base/str.hh"
55#include "base/trace.hh"
56#include "config/use_kvm.hh"
57#if USE_KVM
58#include "cpu/kvm/base.hh"
59#include "cpu/kvm/vm.hh"
60#endif
61#include "cpu/thread_context.hh"
62#include "debug/Loader.hh"
63#include "debug/WorkItems.hh"
64#include "mem/abstract_mem.hh"
65#include "mem/physical.hh"
66#include "params/System.hh"
67#include "sim/byteswap.hh"
68#include "sim/debug.hh"
69#include "sim/full_system.hh"
70
71/**
72 * To avoid linking errors with LTO, only include the header if we
73 * actually have a definition.
74 */
75#if THE_ISA != NULL_ISA
76#include "kern/kernel_stats.hh"
77
78#endif
79
80using namespace std;
81using namespace TheISA;
82
83vector<System *> System::systemList;
84
85int System::numSystemsRunning = 0;
86
87System::System(Params *p)
88 : MemObject(p), _systemPort("system_port", this),
89 _numContexts(0),
90 multiThread(p->multi_thread),
91 pagePtr(0),
92 init_param(p->init_param),
93 physProxy(_systemPort, p->cache_line_size),
94 kernelSymtab(nullptr),
95 kernel(nullptr),
96 loadAddrMask(p->load_addr_mask),
97 loadAddrOffset(p->load_offset),
98#if USE_KVM
99 kvmVM(p->kvm_vm),
100#else
101 kvmVM(nullptr),
102#endif
103 physmem(name() + ".physmem", p->memories, p->mmap_using_noreserve),
104 memoryMode(p->mem_mode),
105 _cacheLineSize(p->cache_line_size),
106 workItemsBegin(0),
107 workItemsEnd(0),
108 numWorkIds(p->num_work_ids),
109 thermalModel(p->thermal_model),
110 _params(p),
111 totalNumInsts(0),
112 instEventQueue("system instruction-based event queue")
113{
114 // add self to global system list
115 systemList.push_back(this);
116
117#if USE_KVM
118 if (kvmVM) {
119 kvmVM->setSystem(this);
120 }
121#endif
122
123 if (FullSystem) {
124 kernelSymtab = new SymbolTable;
125 if (!debugSymbolTable)
126 debugSymbolTable = new SymbolTable;
127 }
128
129 // check if the cache line size is a value known to work
130 if (!(_cacheLineSize == 16 || _cacheLineSize == 32 ||
131 _cacheLineSize == 64 || _cacheLineSize == 128))
132 warn_once("Cache line size is neither 16, 32, 64 nor 128 bytes.\n");
133
134 // Get the generic system master IDs
135 MasterID tmp_id M5_VAR_USED;
136 tmp_id = getMasterId("writebacks");
137 assert(tmp_id == Request::wbMasterId);
138 tmp_id = getMasterId("functional");
139 assert(tmp_id == Request::funcMasterId);
140 tmp_id = getMasterId("interrupt");
141 assert(tmp_id == Request::intMasterId);
142
143 if (FullSystem) {
144 if (params()->kernel == "") {
145 inform("No kernel set for full system simulation. "
146 "Assuming you know what you're doing\n");
147 } else {
148 // Get the kernel code
149 kernel = createObjectFile(params()->kernel);
150 inform("kernel located at: %s", params()->kernel);
151
152 if (kernel == NULL)
153 fatal("Could not load kernel file %s", params()->kernel);
154
155 // setup entry points
156 kernelStart = kernel->textBase();
157 kernelEnd = kernel->bssBase() + kernel->bssSize();
158 kernelEntry = kernel->entryPoint();
159
160 // load symbols
161 if (!kernel->loadGlobalSymbols(kernelSymtab))
162 fatal("could not load kernel symbols\n");
163
164 if (!kernel->loadLocalSymbols(kernelSymtab))
165 fatal("could not load kernel local symbols\n");
166
167 if (!kernel->loadGlobalSymbols(debugSymbolTable))
168 fatal("could not load kernel symbols\n");
169
170 if (!kernel->loadLocalSymbols(debugSymbolTable))
171 fatal("could not load kernel local symbols\n");
172
173 // Loading only needs to happen once and after memory system is
174 // connected so it will happen in initState()
175 }
176 }
177
178 // increment the number of running systems
179 numSystemsRunning++;
180
181 // Set back pointers to the system in all memories
182 for (int x = 0; x < params()->memories.size(); x++)
183 params()->memories[x]->system(this);
184}
185
186System::~System()
187{
188 delete kernelSymtab;
189 delete kernel;
190
191 for (uint32_t j = 0; j < numWorkIds; j++)
192 delete workItemStats[j];
193}
194
195void
196System::init()
197{
198 // check that the system port is connected
199 if (!_systemPort.isConnected())
200 panic("System port on %s is not connected.\n", name());
201}
202
203BaseMasterPort&
204System::getMasterPort(const std::string &if_name, PortID idx)
205{
206 // no need to distinguish at the moment (besides checking)
207 return _systemPort;
208}
209
210void
211System::setMemoryMode(Enums::MemoryMode mode)
212{
213 assert(drainState() == DrainState::Drained);
214 memoryMode = mode;
215}
216
217bool System::breakpoint()
218{
219 if (remoteGDB.size())
220 return remoteGDB[0]->breakpoint();
221 return false;
222}
223
224/**
225 * Setting rgdb_wait to a positive integer waits for a remote debugger to
226 * connect to that context ID before continuing. This should really
227 be a parameter on the CPU object or something...
228 */
229int rgdb_wait = -1;
230
231ContextID
232System::registerThreadContext(ThreadContext *tc, ContextID assigned)
233{
234 int id;
235 if (assigned == InvalidContextID) {
236 for (id = 0; id < threadContexts.size(); id++) {
237 if (!threadContexts[id])
238 break;
239 }
240
241 if (threadContexts.size() <= id)
242 threadContexts.resize(id + 1);
243 } else {
244 if (threadContexts.size() <= assigned)
245 threadContexts.resize(assigned + 1);
246 id = assigned;
247 }
248
249 if (threadContexts[id])
250 fatal("Cannot have two CPUs with the same id (%d)\n", id);
251
252 threadContexts[id] = tc;
253 _numContexts++;
254
255#if THE_ISA != NULL_ISA
256 int port = getRemoteGDBPort();
257 if (port) {
258 RemoteGDB *rgdb = new RemoteGDB(this, tc);
259 GDBListener *gdbl = new GDBListener(rgdb, port + id);
260 gdbl->listen();
261
262 if (rgdb_wait != -1 && rgdb_wait == id)
263 gdbl->accept();
264
265 if (remoteGDB.size() <= id) {
266 remoteGDB.resize(id + 1);
267 }
268
269 remoteGDB[id] = rgdb;
270 }
271#endif
272
273 activeCpus.push_back(false);
274
275 return id;
276}
277
278int
279System::numRunningContexts()
280{
281 int running = 0;
282 for (int i = 0; i < _numContexts; ++i) {
283 if (threadContexts[i]->status() != ThreadContext::Halted)
284 ++running;
285 }
286 return running;
287}
288
289void
290System::initState()
291{
292 if (FullSystem) {
293 for (int i = 0; i < threadContexts.size(); i++)
294 TheISA::startupCPU(threadContexts[i], i);
295 // Moved from the constructor to here since it relies on the
296 // address map being resolved in the interconnect
297 /**
298 * Load the kernel code into memory
299 */
300 if (params()->kernel != "") {
301 if (params()->kernel_addr_check) {
302 // Validate kernel mapping before loading binary
303 if (!(isMemAddr((kernelStart & loadAddrMask) +
304 loadAddrOffset) &&
305 isMemAddr((kernelEnd & loadAddrMask) +
306 loadAddrOffset))) {
307 fatal("Kernel is mapped to invalid location (not memory). "
308 "kernelStart 0x(%x) - kernelEnd 0x(%x) %#x:%#x\n",
309 kernelStart,
310 kernelEnd, (kernelStart & loadAddrMask) +
311 loadAddrOffset,
312 (kernelEnd & loadAddrMask) + loadAddrOffset);
313 }
314 }
315 // Load program sections into memory
316 kernel->loadSections(physProxy, loadAddrMask, loadAddrOffset);
317
318 DPRINTF(Loader, "Kernel start = %#x\n", kernelStart);
319 DPRINTF(Loader, "Kernel end = %#x\n", kernelEnd);
320 DPRINTF(Loader, "Kernel entry = %#x\n", kernelEntry);
321 DPRINTF(Loader, "Kernel loaded...\n");
322 }
323 }
324}
325
326void
327System::replaceThreadContext(ThreadContext *tc, ContextID context_id)
328{
329 if (context_id >= threadContexts.size()) {
330 panic("replaceThreadContext: bad id, %d >= %d\n",
331 context_id, threadContexts.size());
332 }
333
334 threadContexts[context_id] = tc;
335 if (context_id < remoteGDB.size())
336 remoteGDB[context_id]->replaceThreadContext(tc);
337}
338
339bool
340System::validKvmEnvironment() const
341{
342#if USE_KVM
343 if (threadContexts.empty())
344 return false;
345
346 for (auto tc : threadContexts) {
347 if (dynamic_cast<BaseKvmCPU*>(tc->getCpuPtr()) == nullptr) {
348 return false;
349 }
350 }
351 return true;
352#else
353 return false;
354#endif
355}
356
357Addr
358System::allocPhysPages(int npages)
359{
360 Addr return_addr = pagePtr << PageShift;
361 pagePtr += npages;
362
363 Addr next_return_addr = pagePtr << PageShift;
364
365 AddrRange m5opRange(0xffff0000, 0xffffffff);
366 if (m5opRange.contains(next_return_addr)) {
367 warn("Reached m5ops MMIO region\n");
368 return_addr = 0xffffffff;
369 pagePtr = 0xffffffff >> PageShift;
370 }
371
372 if ((pagePtr << PageShift) > physmem.totalSize())
373 fatal("Out of memory, please increase size of physical memory.");
374 return return_addr;
375}
376
377Addr
378System::memSize() const
379{
380 return physmem.totalSize();
381}
382
383Addr
384System::freeMemSize() const
385{
386 return physmem.totalSize() - (pagePtr << PageShift);
387}
388
389bool
390System::isMemAddr(Addr addr) const
391{
392 return physmem.isMemAddr(addr);
393}
394
395void
396System::drainResume()
397{
398 totalNumInsts = 0;
399}
400
401void
402System::serialize(CheckpointOut &cp) const
403{
404 if (FullSystem)
405 kernelSymtab->serialize("kernel_symtab", cp);
406 SERIALIZE_SCALAR(pagePtr);
407 serializeSymtab(cp);
408
409 // also serialize the memories in the system
410 physmem.serializeSection(cp, "physmem");
411}
412
413
414void
415System::unserialize(CheckpointIn &cp)
416{
417 if (FullSystem)
418 kernelSymtab->unserialize("kernel_symtab", cp);
419 UNSERIALIZE_SCALAR(pagePtr);
420 unserializeSymtab(cp);
421
422 // also unserialize the memories in the system
423 physmem.unserializeSection(cp, "physmem");
424}
425
426void
427System::regStats()
428{
429 MemObject::regStats();
430
431 for (uint32_t j = 0; j < numWorkIds ; j++) {
432 workItemStats[j] = new Stats::Histogram();
433 stringstream namestr;
434 ccprintf(namestr, "work_item_type%d", j);
435 workItemStats[j]->init(20)
436 .name(name() + "." + namestr.str())
437 .desc("Run time stat for" + namestr.str())
438 .prereq(*workItemStats[j]);
439 }
440}
441
442void
443System::workItemEnd(uint32_t tid, uint32_t workid)
444{
445 std::pair<uint32_t,uint32_t> p(tid, workid);
446 if (!lastWorkItemStarted.count(p))
447 return;
448
449 Tick samp = curTick() - lastWorkItemStarted[p];
450 DPRINTF(WorkItems, "Work item end: %d\t%d\t%lld\n", tid, workid, samp);
451
452 if (workid >= numWorkIds)
453 fatal("Got workid greater than specified in system configuration\n");
454
455 workItemStats[workid]->sample(samp);
456 lastWorkItemStarted.erase(p);
457}
458
459void
460System::printSystems()
461{
462 ios::fmtflags flags(cerr.flags());
463
464 vector<System *>::iterator i = systemList.begin();
465 vector<System *>::iterator end = systemList.end();
466 for (; i != end; ++i) {
467 System *sys = *i;
468 cerr << "System " << sys->name() << ": " << hex << sys << endl;
469 }
470
471 cerr.flags(flags);
472}
473
474void
475printSystems()
476{
477 System::printSystems();
478}
479
480MasterID
481System::getMasterId(std::string master_name)
482{
483 // strip off system name if the string starts with it
484 if (startswith(master_name, name()))
485 master_name = master_name.erase(0, name().size() + 1);
486
487 // CPUs in switch_cpus ask for ids again after switching
488 for (int i = 0; i < masterIds.size(); i++) {
489 if (masterIds[i] == master_name) {
490 return i;
491 }
492 }
493
494 // Verify that the statistics haven't been enabled yet
495 // Otherwise objects will have sized their stat buckets and
496 // they will be too small
497
498 if (Stats::enabled()) {
499 fatal("Can't request a masterId after regStats(). "
500 "You must do so in init().\n");
501 }
502
503 masterIds.push_back(master_name);
504
505 return masterIds.size() - 1;
506}
507
508std::string
509System::getMasterName(MasterID master_id)
510{
511 if (master_id >= masterIds.size())
512 fatal("Invalid master_id passed to getMasterName()\n");
513
514 return masterIds[master_id];
515}
516
517System *
518SystemParams::create()
519{
520 return new System(this);
521}