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