2 * Copyright (c) 2011-2014,2017-2019 ARM Limited
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.
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.
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.
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.
42 #include "sim/system.hh"
46 #include "arch/remote_gdb.hh"
47 #include "arch/utility.hh"
48 #include "base/loader/object_file.hh"
49 #include "base/loader/symtab.hh"
50 #include "base/str.hh"
51 #include "base/trace.hh"
52 #include "config/use_kvm.hh"
54 #include "cpu/kvm/base.hh"
55 #include "cpu/kvm/vm.hh"
57 #include "cpu/base.hh"
58 #include "cpu/thread_context.hh"
59 #include "debug/Loader.hh"
60 #include "debug/Quiesce.hh"
61 #include "debug/WorkItems.hh"
62 #include "mem/abstract_mem.hh"
63 #include "mem/physical.hh"
64 #include "params/System.hh"
65 #include "sim/byteswap.hh"
66 #include "sim/debug.hh"
67 #include "sim/full_system.hh"
68 #include "sim/redirect_path.hh"
71 using namespace TheISA
;
73 vector
<System
*> System::systemList
;
76 System::Threads::insert(ThreadContext
*tc
, ContextID id
)
78 if (id
== InvalidContextID
) {
79 for (id
= 0; id
< size(); id
++) {
80 if (!threads
[id
].context
)
86 threads
.resize(id
+ 1);
88 fatal_if(threads
[id
].context
,
89 "Cannot have two thread contexts with the same id (%d).", id
);
93 # if THE_ISA != NULL_ISA
94 int port
= getRemoteGDBPort();
96 t
.gdb
= new RemoteGDB(tc
->getSystemPtr(), tc
, port
+ id
);
105 System::Threads::replace(ThreadContext
*tc
, ContextID id
)
107 auto &t
= thread(id
);
110 t
.gdb
->replaceThreadContext(tc
);
114 System::Threads::findFree()
116 for (auto &thread
: threads
) {
117 if (thread
.context
->status() == ThreadContext::Halted
)
118 return thread
.context
;
124 System::Threads::numRunning() const
127 for (auto &thread
: threads
) {
128 auto status
= thread
.context
->status();
129 if (status
!= ThreadContext::Halted
&&
130 status
!= ThreadContext::Halting
) {
137 int System::numSystemsRunning
= 0;
139 System::System(Params
*p
)
140 : SimObject(p
), _systemPort("system_port", this),
141 multiThread(p
->multi_thread
),
143 init_param(p
->init_param
),
144 physProxy(_systemPort
, p
->cache_line_size
),
145 workload(p
->workload
),
151 physmem(name() + ".physmem", p
->memories
, p
->mmap_using_noreserve
),
152 memoryMode(p
->mem_mode
),
153 _cacheLineSize(p
->cache_line_size
),
156 numWorkIds(p
->num_work_ids
),
157 thermalModel(p
->thermal_model
),
159 _m5opRange(p
->m5ops_base
?
160 RangeSize(p
->m5ops_base
, 0x10000) :
161 AddrRange(1, 0)), // Create an empty range if disabled
163 redirectPaths(p
->redirect_paths
)
166 workload
->system
= this;
168 // add self to global system list
169 systemList
.push_back(this);
173 kvmVM
->setSystem(this);
177 // check if the cache line size is a value known to work
178 if (!(_cacheLineSize
== 16 || _cacheLineSize
== 32 ||
179 _cacheLineSize
== 64 || _cacheLineSize
== 128))
180 warn_once("Cache line size is neither 16, 32, 64 nor 128 bytes.\n");
182 // Get the generic system master IDs
183 MasterID tmp_id M5_VAR_USED
;
184 tmp_id
= getMasterId(this, "writebacks");
185 assert(tmp_id
== Request::wbMasterId
);
186 tmp_id
= getMasterId(this, "functional");
187 assert(tmp_id
== Request::funcMasterId
);
188 tmp_id
= getMasterId(this, "interrupt");
189 assert(tmp_id
== Request::intMasterId
);
191 // increment the number of running systems
194 // Set back pointers to the system in all memories
195 for (int x
= 0; x
< params()->memories
.size(); x
++)
196 params()->memories
[x
]->system(this);
201 for (uint32_t j
= 0; j
< numWorkIds
; j
++)
202 delete workItemStats
[j
];
208 // check that the system port is connected
209 if (!_systemPort
.isConnected())
210 panic("System port on %s is not connected.\n", name());
216 SimObject::startup();
218 // Now that we're about to start simulation, wait for GDB connections if
220 #if THE_ISA != NULL_ISA
221 for (int i
= 0; i
< threads
.size(); i
++) {
222 auto *gdb
= threads
.thread(i
).gdb
;
223 auto *cpu
= threads
[i
]->getCpuPtr();
224 if (gdb
&& cpu
->waitForRemoteGDB()) {
225 inform("%s: Waiting for a remote GDB connection on port %d.",
226 cpu
->name(), gdb
->port());
234 System::getPort(const std::string
&if_name
, PortID idx
)
236 // no need to distinguish at the moment (besides checking)
241 System::setMemoryMode(Enums::MemoryMode mode
)
243 assert(drainState() == DrainState::Drained
);
247 bool System::breakpoint()
251 auto *gdb
= threads
.thread(0).gdb
;
254 return gdb
->breakpoint();
258 System::registerThreadContext(ThreadContext
*tc
, ContextID assigned
)
260 ContextID id
= threads
.insert(tc
, assigned
);
262 for (auto *e
: liveEvents
)
269 System::schedule(PCEvent
*event
)
272 liveEvents
.push_back(event
);
273 for (auto *tc
: threads
)
274 all
= tc
->schedule(event
) && all
;
279 System::remove(PCEvent
*event
)
282 liveEvents
.remove(event
);
283 for (auto *tc
: threads
)
284 all
= tc
->remove(event
) && all
;
289 System::replaceThreadContext(ThreadContext
*tc
, ContextID context_id
)
291 auto *otc
= threads
[context_id
];
292 threads
.replace(tc
, context_id
);
294 for (auto *e
: liveEvents
) {
301 System::validKvmEnvironment() const
307 for (auto *tc
: threads
) {
308 if (!dynamic_cast<BaseKvmCPU
*>(tc
->getCpuPtr()))
319 System::allocPhysPages(int npages
)
321 Addr return_addr
= pagePtr
<< PageShift
;
324 Addr next_return_addr
= pagePtr
<< PageShift
;
326 if (_m5opRange
.contains(next_return_addr
)) {
327 warn("Reached m5ops MMIO region\n");
328 return_addr
= 0xffffffff;
329 pagePtr
= 0xffffffff >> PageShift
;
332 if ((pagePtr
<< PageShift
) > physmem
.totalSize())
333 fatal("Out of memory, please increase size of physical memory.");
338 System::memSize() const
340 return physmem
.totalSize();
344 System::freeMemSize() const
346 return physmem
.totalSize() - (pagePtr
<< PageShift
);
350 System::isMemAddr(Addr addr
) const
352 return physmem
.isMemAddr(addr
);
356 System::drainResume()
362 System::serialize(CheckpointOut
&cp
) const
364 SERIALIZE_SCALAR(pagePtr
);
366 // also serialize the memories in the system
367 physmem
.serializeSection(cp
, "physmem");
372 System::unserialize(CheckpointIn
&cp
)
374 UNSERIALIZE_SCALAR(pagePtr
);
376 // also unserialize the memories in the system
377 physmem
.unserializeSection(cp
, "physmem");
383 SimObject::regStats();
385 for (uint32_t j
= 0; j
< numWorkIds
; j
++) {
386 workItemStats
[j
] = new Stats::Histogram();
387 stringstream namestr
;
388 ccprintf(namestr
, "work_item_type%d", j
);
389 workItemStats
[j
]->init(20)
390 .name(name() + "." + namestr
.str())
391 .desc("Run time stat for" + namestr
.str())
392 .prereq(*workItemStats
[j
]);
397 System::workItemEnd(uint32_t tid
, uint32_t workid
)
399 std::pair
<uint32_t,uint32_t> p(tid
, workid
);
400 if (!lastWorkItemStarted
.count(p
))
403 Tick samp
= curTick() - lastWorkItemStarted
[p
];
404 DPRINTF(WorkItems
, "Work item end: %d\t%d\t%lld\n", tid
, workid
, samp
);
406 if (workid
>= numWorkIds
)
407 fatal("Got workid greater than specified in system configuration\n");
409 workItemStats
[workid
]->sample(samp
);
410 lastWorkItemStarted
.erase(p
);
414 System::printSystems()
416 ios::fmtflags
flags(cerr
.flags());
418 vector
<System
*>::iterator i
= systemList
.begin();
419 vector
<System
*>::iterator end
= systemList
.end();
420 for (; i
!= end
; ++i
) {
422 cerr
<< "System " << sys
->name() << ": " << hex
<< sys
<< endl
;
431 System::printSystems();
435 System::stripSystemName(const std::string
& master_name
) const
437 if (startswith(master_name
, name())) {
438 return master_name
.substr(name().size());
445 System::lookupMasterId(const SimObject
* obj
) const
447 MasterID id
= Request::invldMasterId
;
449 // number of occurrences of the SimObject pointer
450 // in the master list.
453 for (int i
= 0; i
< masters
.size(); i
++) {
454 if (masters
[i
].obj
== obj
) {
460 fatal_if(obj_number
> 1,
461 "Cannot lookup MasterID by SimObject pointer: "
462 "More than one master is sharing the same SimObject\n");
468 System::lookupMasterId(const std::string
& master_name
) const
470 std::string name
= stripSystemName(master_name
);
472 for (int i
= 0; i
< masters
.size(); i
++) {
473 if (masters
[i
].masterName
== name
) {
478 return Request::invldMasterId
;
482 System::getGlobalMasterId(const std::string
& master_name
)
484 return _getMasterId(nullptr, master_name
);
488 System::getMasterId(const SimObject
* master
, std::string submaster
)
490 auto master_name
= leafMasterName(master
, submaster
);
491 return _getMasterId(master
, master_name
);
495 System::_getMasterId(const SimObject
* master
, const std::string
& master_name
)
497 std::string name
= stripSystemName(master_name
);
499 // CPUs in switch_cpus ask for ids again after switching
500 for (int i
= 0; i
< masters
.size(); i
++) {
501 if (masters
[i
].masterName
== name
) {
506 // Verify that the statistics haven't been enabled yet
507 // Otherwise objects will have sized their stat buckets and
508 // they will be too small
510 if (Stats::enabled()) {
511 fatal("Can't request a masterId after regStats(). "
512 "You must do so in init().\n");
515 // Generate a new MasterID incrementally
516 MasterID master_id
= masters
.size();
518 // Append the new Master metadata to the group of system Masters.
519 masters
.emplace_back(master
, name
, master_id
);
521 return masters
.back().masterId
;
525 System::leafMasterName(const SimObject
* master
, const std::string
& submaster
)
527 if (submaster
.empty()) {
528 return master
->name();
530 // Get the full master name by appending the submaster name to
531 // the root SimObject master name
532 return master
->name() + "." + submaster
;
537 System::getMasterName(MasterID master_id
)
539 if (master_id
>= masters
.size())
540 fatal("Invalid master_id passed to getMasterName()\n");
542 const auto& master_info
= masters
[master_id
];
543 return master_info
.masterName
;
547 SystemParams::create()
549 return new System(this);