2 * Copyright (c) 2011-2012 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.
41 * Authors: Steve Reinhardt
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/physical.hh"
61 #include "params/System.hh"
62 #include "sim/byteswap.hh"
63 #include "sim/debug.hh"
64 #include "sim/full_system.hh"
65 #include "sim/system.hh"
68 using namespace TheISA
;
70 vector
<System
*> System::systemList
;
72 int System::numSystemsRunning
= 0;
74 System::System(Params
*p
)
75 : MemObject(p
), _systemPort("system_port", this),
78 init_param(p
->init_param
),
79 physProxy(_systemPort
),
80 virtProxy(_systemPort
),
81 loadAddrMask(p
->load_addr_mask
),
84 memoryMode(p
->mem_mode
),
87 numWorkIds(p
->num_work_ids
),
90 instEventQueue("system instruction-based event queue")
92 // add self to global system list
93 systemList
.push_back(this);
96 kernelSymtab
= new SymbolTable
;
97 if (!debugSymbolTable
)
98 debugSymbolTable
= new SymbolTable
;
101 // Get the generic system master IDs
102 MasterID tmp_id M5_VAR_USED
;
103 tmp_id
= getMasterId("writebacks");
104 assert(tmp_id
== Request::wbMasterId
);
105 tmp_id
= getMasterId("functional");
106 assert(tmp_id
== Request::funcMasterId
);
107 tmp_id
= getMasterId("interrupt");
108 assert(tmp_id
== Request::intMasterId
);
111 if (params()->kernel
== "") {
112 inform("No kernel set for full system simulation. "
113 "Assuming you know what you're doing if not SPARC ISA\n");
115 // Get the kernel code
116 kernel
= createObjectFile(params()->kernel
);
117 inform("kernel located at: %s", params()->kernel
);
120 fatal("Could not load kernel file %s", params()->kernel
);
122 // setup entry points
123 kernelStart
= kernel
->textBase();
124 kernelEnd
= kernel
->bssBase() + kernel
->bssSize();
125 kernelEntry
= kernel
->entryPoint();
128 if (!kernel
->loadGlobalSymbols(kernelSymtab
))
129 fatal("could not load kernel symbols\n");
131 if (!kernel
->loadLocalSymbols(kernelSymtab
))
132 fatal("could not load kernel local symbols\n");
134 if (!kernel
->loadGlobalSymbols(debugSymbolTable
))
135 fatal("could not load kernel symbols\n");
137 if (!kernel
->loadLocalSymbols(debugSymbolTable
))
138 fatal("could not load kernel local symbols\n");
140 // Loading only needs to happen once and after memory system is
141 // connected so it will happen in initState()
145 // increment the number of running systms
155 for (uint32_t j
= 0; j
< numWorkIds
; j
++)
156 delete workItemStats
[j
];
162 // check that the system port is connected
163 if (!_systemPort
.isConnected())
164 panic("System port on %s is not connected.\n", name());
168 System::getMasterPort(const std::string
&if_name
, int idx
)
170 // no need to distinguish at the moment (besides checking)
175 System::setMemoryMode(Enums::MemoryMode mode
)
177 assert(getState() == Drained
);
181 bool System::breakpoint()
183 if (remoteGDB
.size())
184 return remoteGDB
[0]->breakpoint();
189 * Setting rgdb_wait to a positive integer waits for a remote debugger to
190 * connect to that context ID before continuing. This should really
191 be a parameter on the CPU object or something...
196 System::registerThreadContext(ThreadContext
*tc
, int assigned
)
199 if (assigned
== -1) {
200 for (id
= 0; id
< threadContexts
.size(); id
++) {
201 if (!threadContexts
[id
])
205 if (threadContexts
.size() <= id
)
206 threadContexts
.resize(id
+ 1);
208 if (threadContexts
.size() <= assigned
)
209 threadContexts
.resize(assigned
+ 1);
213 if (threadContexts
[id
])
214 fatal("Cannot have two CPUs with the same id (%d)\n", id
);
216 threadContexts
[id
] = tc
;
219 int port
= getRemoteGDBPort();
221 RemoteGDB
*rgdb
= new RemoteGDB(this, tc
);
222 GDBListener
*gdbl
= new GDBListener(rgdb
, port
+ id
);
225 if (rgdb_wait
!= -1 && rgdb_wait
== id
)
228 if (remoteGDB
.size() <= id
) {
229 remoteGDB
.resize(id
+ 1);
232 remoteGDB
[id
] = rgdb
;
235 activeCpus
.push_back(false);
241 System::numRunningContexts()
244 for (int i
= 0; i
< _numContexts
; ++i
) {
245 if (threadContexts
[i
]->status() != ThreadContext::Halted
)
256 for (i
= 0; i
< threadContexts
.size(); i
++)
257 TheISA::startupCPU(threadContexts
[i
], i
);
258 // Moved from the constructor to here since it relies on the
259 // address map being resolved in the interconnect
261 * Load the kernel code into memory
263 if (params()->kernel
!= "") {
264 // Load program sections into memory
265 kernel
->loadSections(physProxy
, loadAddrMask
);
267 DPRINTF(Loader
, "Kernel start = %#x\n", kernelStart
);
268 DPRINTF(Loader
, "Kernel end = %#x\n", kernelEnd
);
269 DPRINTF(Loader
, "Kernel entry = %#x\n", kernelEntry
);
270 DPRINTF(Loader
, "Kernel loaded...\n");
279 for (i
= 0; i
< threadContexts
.size(); i
++)
280 TheISA::startupCPU(threadContexts
[i
], i
);
284 System::replaceThreadContext(ThreadContext
*tc
, int context_id
)
286 if (context_id
>= threadContexts
.size()) {
287 panic("replaceThreadContext: bad id, %d >= %d\n",
288 context_id
, threadContexts
.size());
291 threadContexts
[context_id
] = tc
;
292 if (context_id
< remoteGDB
.size())
293 remoteGDB
[context_id
]->replaceThreadContext(tc
);
297 System::allocPhysPages(int npages
)
299 Addr return_addr
= pagePtr
<< LogVMPageSize
;
301 if (pagePtr
> physmem
.totalSize())
302 fatal("Out of memory, please increase size of physical memory.");
307 System::memSize() const
309 return physmem
.totalSize();
313 System::freeMemSize() const
315 return physmem
.totalSize() - (pagePtr
<< LogVMPageSize
);
319 System::isMemAddr(Addr addr
) const
321 return physmem
.isMemAddr(addr
);
332 System::serialize(ostream
&os
)
335 kernelSymtab
->serialize("kernel_symtab", os
);
336 SERIALIZE_SCALAR(pagePtr
);
337 SERIALIZE_SCALAR(nextPID
);
342 System::unserialize(Checkpoint
*cp
, const string
§ion
)
345 kernelSymtab
->unserialize("kernel_symtab", cp
, section
);
346 UNSERIALIZE_SCALAR(pagePtr
);
347 UNSERIALIZE_SCALAR(nextPID
);
353 for (uint32_t j
= 0; j
< numWorkIds
; j
++) {
354 workItemStats
[j
] = new Stats::Histogram();
355 stringstream namestr
;
356 ccprintf(namestr
, "work_item_type%d", j
);
357 workItemStats
[j
]->init(20)
358 .name(name() + "." + namestr
.str())
359 .desc("Run time stat for" + namestr
.str())
360 .prereq(*workItemStats
[j
]);
365 System::workItemEnd(uint32_t tid
, uint32_t workid
)
367 std::pair
<uint32_t,uint32_t> p(tid
, workid
);
368 if (!lastWorkItemStarted
.count(p
))
371 Tick samp
= curTick() - lastWorkItemStarted
[p
];
372 DPRINTF(WorkItems
, "Work item end: %d\t%d\t%lld\n", tid
, workid
, samp
);
374 if (workid
>= numWorkIds
)
375 fatal("Got workid greater than specified in system configuration\n");
377 workItemStats
[workid
]->sample(samp
);
378 lastWorkItemStarted
.erase(p
);
382 System::printSystems()
384 vector
<System
*>::iterator i
= systemList
.begin();
385 vector
<System
*>::iterator end
= systemList
.end();
386 for (; i
!= end
; ++i
) {
388 cerr
<< "System " << sys
->name() << ": " << hex
<< sys
<< endl
;
395 System::printSystems();
399 System::getMasterId(std::string master_name
)
401 // strip off system name if the string starts with it
402 if (master_name
.size() > name().size() &&
403 master_name
.compare(0, name().size(), name()) == 0)
404 master_name
= master_name
.erase(0, name().size() + 1);
406 // CPUs in switch_cpus ask for ids again after switching
407 for (int i
= 0; i
< masterIds
.size(); i
++) {
408 if (masterIds
[i
] == master_name
) {
413 // Verify that the statistics haven't been enabled yet
414 // Otherwise objects will have sized their stat buckets and
415 // they will be too small
417 if (Stats::enabled())
418 fatal("Can't request a masterId after regStats(). \
419 You must do so in init().\n");
421 masterIds
.push_back(master_name
);
423 return masterIds
.size() - 1;
427 System::getMasterName(MasterID master_id
)
429 if (master_id
>= masterIds
.size())
430 fatal("Invalid master_id passed to getMasterName()\n");
432 return masterIds
[master_id
];
435 const char *System::MemoryModeStrings
[3] = {"invalid", "atomic",
439 SystemParams::create()
441 return new System(this);