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/compiler.hh"
49 #include "base/loader/object_file.hh"
50 #include "base/loader/symtab.hh"
51 #include "base/str.hh"
52 #include "base/trace.hh"
53 #include "config/the_isa.hh"
54 #include "config/use_kvm.hh"
56 #include "cpu/kvm/base.hh"
57 #include "cpu/kvm/vm.hh"
59 #if THE_ISA != NULL_ISA
60 #include "cpu/base.hh"
62 #include "cpu/thread_context.hh"
63 #include "debug/Loader.hh"
64 #include "debug/Quiesce.hh"
65 #include "debug/WorkItems.hh"
66 #include "mem/abstract_mem.hh"
67 #include "mem/physical.hh"
68 #include "params/System.hh"
69 #include "sim/byteswap.hh"
70 #include "sim/debug.hh"
71 #include "sim/full_system.hh"
72 #include "sim/redirect_path.hh"
74 std::vector
<System
*> System::systemList
;
77 System::Threads::Thread::resume()
79 # if THE_ISA != NULL_ISA
80 DPRINTFS(Quiesce
, context
->getCpuPtr(), "activating\n");
86 System::Threads::Thread::name() const
89 return csprintf("%s.threads[%d]", context
->getSystemPtr()->name(),
90 context
->contextId());
94 System::Threads::Thread::quiesce() const
97 auto *workload
= context
->getSystemPtr()->workload
;
99 workload
->recordQuiesce();
103 System::Threads::insert(ThreadContext
*tc
, ContextID id
)
105 if (id
== InvalidContextID
) {
106 for (id
= 0; id
< size(); id
++) {
107 if (!threads
[id
].context
)
113 threads
.resize(id
+ 1);
115 fatal_if(threads
[id
].context
,
116 "Cannot have two thread contexts with the same id (%d).", id
);
118 auto *sys
= tc
->getSystemPtr();
120 auto &t
= thread(id
);
122 // Look up this thread again on resume, in case the threads vector has
124 t
.resumeEvent
= new EventFunctionWrapper(
125 [this, id
](){ thread(id
).resume(); }, sys
->name());
126 # if THE_ISA != NULL_ISA
127 int port
= getRemoteGDBPort();
129 t
.gdb
= new TheISA::RemoteGDB(sys
, tc
, port
+ id
);
138 System::Threads::replace(ThreadContext
*tc
, ContextID id
)
140 auto &t
= thread(id
);
141 panic_if(!t
.context
, "Can't replace a context which doesn't exist.");
143 t
.gdb
->replaceThreadContext(tc
);
144 # if THE_ISA != NULL_ISA
145 if (t
.resumeEvent
->scheduled()) {
146 Tick when
= t
.resumeEvent
->when();
147 t
.context
->getCpuPtr()->deschedule(t
.resumeEvent
);
148 tc
->getCpuPtr()->schedule(t
.resumeEvent
, when
);
155 System::Threads::findFree()
157 for (auto &thread
: threads
) {
158 if (thread
.context
->status() == ThreadContext::Halted
)
159 return thread
.context
;
165 System::Threads::numRunning() const
168 for (auto &thread
: threads
) {
169 auto status
= thread
.context
->status();
170 if (status
!= ThreadContext::Halted
&&
171 status
!= ThreadContext::Halting
) {
179 System::Threads::quiesce(ContextID id
)
181 auto &t
= thread(id
);
182 # if THE_ISA != NULL_ISA
183 M5_VAR_USED BaseCPU
*cpu
= t
.context
->getCpuPtr();
184 DPRINTFS(Quiesce
, cpu
, "quiesce()\n");
190 System::Threads::quiesceTick(ContextID id
, Tick when
)
192 # if THE_ISA != NULL_ISA
193 auto &t
= thread(id
);
194 BaseCPU
*cpu
= t
.context
->getCpuPtr();
196 DPRINTFS(Quiesce
, cpu
, "quiesceTick until %u\n", when
);
199 cpu
->reschedule(t
.resumeEvent
, when
, true);
203 int System::numSystemsRunning
= 0;
205 System::System(const Params
&p
)
206 : SimObject(p
), _systemPort("system_port", this),
207 multiThread(p
.multi_thread
),
209 init_param(p
.init_param
),
210 physProxy(_systemPort
, p
.cache_line_size
),
211 workload(p
.workload
),
217 physmem(name() + ".physmem", p
.memories
, p
.mmap_using_noreserve
,
219 memoryMode(p
.mem_mode
),
220 _cacheLineSize(p
.cache_line_size
),
223 numWorkIds(p
.num_work_ids
),
224 thermalModel(p
.thermal_model
),
226 _m5opRange(p
.m5ops_base
?
227 RangeSize(p
.m5ops_base
, 0x10000) :
228 AddrRange(1, 0)), // Create an empty range if disabled
229 redirectPaths(p
.redirect_paths
)
232 workload
->system
= this;
234 // add self to global system list
235 systemList
.push_back(this);
239 kvmVM
->setSystem(this);
243 // check if the cache line size is a value known to work
244 if (!(_cacheLineSize
== 16 || _cacheLineSize
== 32 ||
245 _cacheLineSize
== 64 || _cacheLineSize
== 128))
246 warn_once("Cache line size is neither 16, 32, 64 nor 128 bytes.\n");
248 // Get the generic system requestor IDs
249 M5_VAR_USED RequestorID tmp_id
;
250 tmp_id
= getRequestorId(this, "writebacks");
251 assert(tmp_id
== Request::wbRequestorId
);
252 tmp_id
= getRequestorId(this, "functional");
253 assert(tmp_id
== Request::funcRequestorId
);
254 tmp_id
= getRequestorId(this, "interrupt");
255 assert(tmp_id
== Request::intRequestorId
);
257 // increment the number of running systems
260 // Set back pointers to the system in all memories
261 for (int x
= 0; x
< params().memories
.size(); x
++)
262 params().memories
[x
]->system(this);
267 for (uint32_t j
= 0; j
< numWorkIds
; j
++)
268 delete workItemStats
[j
];
274 SimObject::startup();
276 // Now that we're about to start simulation, wait for GDB connections if
278 #if THE_ISA != NULL_ISA
279 for (int i
= 0; i
< threads
.size(); i
++) {
280 auto *gdb
= threads
.thread(i
).gdb
;
281 auto *cpu
= threads
[i
]->getCpuPtr();
282 if (gdb
&& cpu
->waitForRemoteGDB()) {
283 inform("%s: Waiting for a remote GDB connection on port %d.",
284 cpu
->name(), gdb
->port());
292 System::getPort(const std::string
&if_name
, PortID idx
)
294 // no need to distinguish at the moment (besides checking)
299 System::setMemoryMode(Enums::MemoryMode mode
)
301 assert(drainState() == DrainState::Drained
);
305 bool System::breakpoint()
309 auto *gdb
= threads
.thread(0).gdb
;
312 return gdb
->breakpoint();
316 System::registerThreadContext(ThreadContext
*tc
, ContextID assigned
)
318 ContextID id
= threads
.insert(tc
, assigned
);
320 for (auto *e
: liveEvents
)
327 System::schedule(PCEvent
*event
)
330 liveEvents
.push_back(event
);
331 for (auto *tc
: threads
)
332 all
= tc
->schedule(event
) && all
;
337 System::remove(PCEvent
*event
)
340 liveEvents
.remove(event
);
341 for (auto *tc
: threads
)
342 all
= tc
->remove(event
) && all
;
347 System::replaceThreadContext(ThreadContext
*tc
, ContextID context_id
)
349 auto *otc
= threads
[context_id
];
350 threads
.replace(tc
, context_id
);
352 for (auto *e
: liveEvents
) {
359 System::validKvmEnvironment() const
365 for (auto *tc
: threads
) {
366 if (!dynamic_cast<BaseKvmCPU
*>(tc
->getCpuPtr()))
377 System::allocPhysPages(int npages
)
379 Addr return_addr
= pagePtr
<< TheISA::PageShift
;
382 Addr next_return_addr
= pagePtr
<< TheISA::PageShift
;
384 if (_m5opRange
.contains(next_return_addr
)) {
385 warn("Reached m5ops MMIO region\n");
386 return_addr
= 0xffffffff;
387 pagePtr
= 0xffffffff >> TheISA::PageShift
;
390 if ((pagePtr
<< TheISA::PageShift
) > physmem
.totalSize())
391 fatal("Out of memory, please increase size of physical memory.");
396 System::memSize() const
398 return physmem
.totalSize();
402 System::freeMemSize() const
404 return physmem
.totalSize() - (pagePtr
<< TheISA::PageShift
);
408 System::isMemAddr(Addr addr
) const
410 return physmem
.isMemAddr(addr
);
414 System::addDeviceMemory(RequestorID requestor_id
, AbstractMemory
*deviceMemory
)
416 if (!deviceMemMap
.count(requestor_id
)) {
417 deviceMemMap
.insert(std::make_pair(requestor_id
, deviceMemory
));
422 System::isDeviceMemAddr(PacketPtr pkt
) const
424 const RequestorID
& id
= pkt
->requestorId();
426 return (deviceMemMap
.count(id
) &&
427 deviceMemMap
.at(id
)->getAddrRange().contains(pkt
->getAddr()));
431 System::getDeviceMemory(RequestorID id
) const
433 panic_if(!deviceMemMap
.count(id
),
434 "No device memory found for RequestorID %d\n", id
);
435 return deviceMemMap
.at(id
);
439 System::serialize(CheckpointOut
&cp
) const
441 SERIALIZE_SCALAR(pagePtr
);
443 for (auto &t
: threads
.threads
) {
445 if (t
.resumeEvent
&& t
.resumeEvent
->scheduled())
446 when
= t
.resumeEvent
->when();
447 ContextID id
= t
.context
->contextId();
448 paramOut(cp
, csprintf("quiesceEndTick_%d", id
), when
);
451 // also serialize the memories in the system
452 physmem
.serializeSection(cp
, "physmem");
457 System::unserialize(CheckpointIn
&cp
)
459 UNSERIALIZE_SCALAR(pagePtr
);
461 for (auto &t
: threads
.threads
) {
463 ContextID id
= t
.context
->contextId();
464 if (!optParamIn(cp
, csprintf("quiesceEndTick_%d", id
), when
) ||
465 !when
|| !t
.resumeEvent
) {
468 # if THE_ISA != NULL_ISA
469 t
.context
->getCpuPtr()->schedule(t
.resumeEvent
, when
);
473 // also unserialize the memories in the system
474 physmem
.unserializeSection(cp
, "physmem");
480 SimObject::regStats();
482 for (uint32_t j
= 0; j
< numWorkIds
; j
++) {
483 workItemStats
[j
] = new Stats::Histogram(this);
484 std::stringstream namestr
;
485 ccprintf(namestr
, "work_item_type%d", j
);
486 workItemStats
[j
]->init(20)
487 .name(name() + "." + namestr
.str())
488 .desc("Run time stat for" + namestr
.str())
489 .prereq(*workItemStats
[j
]);
494 System::workItemEnd(uint32_t tid
, uint32_t workid
)
496 std::pair
<uint32_t,uint32_t> p(tid
, workid
);
497 if (!lastWorkItemStarted
.count(p
))
500 Tick samp
= curTick() - lastWorkItemStarted
[p
];
501 DPRINTF(WorkItems
, "Work item end: %d\t%d\t%lld\n", tid
, workid
, samp
);
503 if (workid
>= numWorkIds
)
504 fatal("Got workid greater than specified in system configuration\n");
506 workItemStats
[workid
]->sample(samp
);
507 lastWorkItemStarted
.erase(p
);
511 System::printSystems()
513 std::ios::fmtflags
flags(std::cerr
.flags());
515 std::vector
<System
*>::iterator i
= systemList
.begin();
516 std::vector
<System
*>::iterator end
= systemList
.end();
517 for (; i
!= end
; ++i
) {
519 std::cerr
<< "System " << sys
->name() << ": " << std::hex
<< sys
523 std::cerr
.flags(flags
);
529 System::printSystems();
533 System::stripSystemName(const std::string
& requestor_name
) const
535 if (startswith(requestor_name
, name())) {
536 return requestor_name
.substr(name().size());
538 return requestor_name
;
543 System::lookupRequestorId(const SimObject
* obj
) const
545 RequestorID id
= Request::invldRequestorId
;
547 // number of occurrences of the SimObject pointer
548 // in the requestor list.
551 for (int i
= 0; i
< requestors
.size(); i
++) {
552 if (requestors
[i
].obj
== obj
) {
558 fatal_if(obj_number
> 1,
559 "Cannot lookup RequestorID by SimObject pointer: "
560 "More than one requestor is sharing the same SimObject\n");
566 System::lookupRequestorId(const std::string
& requestor_name
) const
568 std::string name
= stripSystemName(requestor_name
);
570 for (int i
= 0; i
< requestors
.size(); i
++) {
571 if (requestors
[i
].req_name
== name
) {
576 return Request::invldRequestorId
;
580 System::getGlobalRequestorId(const std::string
& requestor_name
)
582 return _getRequestorId(nullptr, requestor_name
);
586 System::getRequestorId(const SimObject
* requestor
, std::string subrequestor
)
588 auto requestor_name
= leafRequestorName(requestor
, subrequestor
);
589 return _getRequestorId(requestor
, requestor_name
);
593 System::_getRequestorId(const SimObject
* requestor
,
594 const std::string
& requestor_name
)
596 std::string name
= stripSystemName(requestor_name
);
598 // CPUs in switch_cpus ask for ids again after switching
599 for (int i
= 0; i
< requestors
.size(); i
++) {
600 if (requestors
[i
].req_name
== name
) {
605 // Verify that the statistics haven't been enabled yet
606 // Otherwise objects will have sized their stat buckets and
607 // they will be too small
609 if (Stats::enabled()) {
610 fatal("Can't request a requestorId after regStats(). "
611 "You must do so in init().\n");
614 // Generate a new RequestorID incrementally
615 RequestorID requestor_id
= requestors
.size();
617 // Append the new Requestor metadata to the group of system Requestors.
618 requestors
.emplace_back(requestor
, name
, requestor_id
);
620 return requestors
.back().id
;
624 System::leafRequestorName(const SimObject
* requestor
,
625 const std::string
& subrequestor
)
627 if (subrequestor
.empty()) {
628 return requestor
->name();
630 // Get the full requestor name by appending the subrequestor name to
631 // the root SimObject requestor name
632 return requestor
->name() + "." + subrequestor
;
637 System::getRequestorName(RequestorID requestor_id
)
639 if (requestor_id
>= requestors
.size())
640 fatal("Invalid requestor_id passed to getRequestorName()\n");
642 const auto& requestor_info
= requestors
[requestor_id
];
643 return requestor_info
.req_name
;