Actually free Process fd_map entries when a file is closed...
[gem5.git] / sim / system.cc
1 /*
2 * Copyright (c) 2002-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include "base/loader/object_file.hh"
30 #include "base/loader/symtab.hh"
31 #include "base/remote_gdb.hh"
32 #include "cpu/exec_context.hh"
33 #include "kern/kernel_stats.hh"
34 #include "mem/functional/memory_control.hh"
35 #include "mem/functional/physical.hh"
36 #include "targetarch/vtophys.hh"
37 #include "sim/builder.hh"
38 #include "sim/system.hh"
39 #include "base/trace.hh"
40
41 using namespace std;
42
43 vector<System *> System::systemList;
44
45 int System::numSystemsRunning = 0;
46
47 System::System(Params *p)
48 : SimObject(p->name), memctrl(p->memctrl), physmem(p->physmem),
49 init_param(p->init_param), numcpus(0), params(p)
50 {
51 // add self to global system list
52 systemList.push_back(this);
53
54 kernelSymtab = new SymbolTable;
55 consoleSymtab = new SymbolTable;
56 palSymtab = new SymbolTable;
57 allSymtab = new SymbolTable;
58 debugSymbolTable = new SymbolTable;
59
60 /**
61 * Load the kernel, pal, and console code into memory
62 */
63 // Load kernel code
64 kernel = createObjectFile(params->kernel_path);
65 if (kernel == NULL)
66 fatal("Could not load kernel file %s", params->kernel_path);
67
68 // Load Console Code
69 console = createObjectFile(params->console_path);
70 if (console == NULL)
71 fatal("Could not load console file %s", params->console_path);
72
73 // Load pal file
74 pal = createObjectFile(params->palcode);
75 if (pal == NULL)
76 fatal("Could not load PALcode file %s", params->palcode);
77
78
79 // Load program sections into memory
80 pal->loadSections(physmem, true);
81 console->loadSections(physmem, true);
82 kernel->loadSections(physmem, true);
83
84 // setup entry points
85 kernelStart = kernel->textBase();
86 kernelEnd = kernel->bssBase() + kernel->bssSize();
87 kernelEntry = kernel->entryPoint();
88
89 // load symbols
90 if (!kernel->loadGlobalSymbols(kernelSymtab))
91 panic("could not load kernel symbols\n");
92
93 if (!kernel->loadLocalSymbols(kernelSymtab))
94 panic("could not load kernel local symbols\n");
95
96 if (!console->loadGlobalSymbols(consoleSymtab))
97 panic("could not load console symbols\n");
98
99 if (!pal->loadGlobalSymbols(palSymtab))
100 panic("could not load pal symbols\n");
101
102 if (!pal->loadLocalSymbols(palSymtab))
103 panic("could not load pal symbols\n");
104
105 if (!kernel->loadGlobalSymbols(allSymtab))
106 panic("could not load kernel symbols\n");
107
108 if (!kernel->loadLocalSymbols(allSymtab))
109 panic("could not load kernel local symbols\n");
110
111 if (!console->loadGlobalSymbols(allSymtab))
112 panic("could not load console symbols\n");
113
114 if (!pal->loadGlobalSymbols(allSymtab))
115 panic("could not load pal symbols\n");
116
117 if (!pal->loadLocalSymbols(allSymtab))
118 panic("could not load pal symbols\n");
119
120 if (!kernel->loadGlobalSymbols(debugSymbolTable))
121 panic("could not load kernel symbols\n");
122
123 if (!kernel->loadLocalSymbols(debugSymbolTable))
124 panic("could not load kernel local symbols\n");
125
126 if (!console->loadGlobalSymbols(debugSymbolTable))
127 panic("could not load console symbols\n");
128
129 if (!pal->loadGlobalSymbols(debugSymbolTable))
130 panic("could not load pal symbols\n");
131
132 if (!pal->loadLocalSymbols(debugSymbolTable))
133 panic("could not load pal symbols\n");
134
135
136 DPRINTF(Loader, "Kernel start = %#x\n", kernelStart);
137 DPRINTF(Loader, "Kernel end = %#x\n", kernelEnd);
138 DPRINTF(Loader, "Kernel entry = %#x\n", kernelEntry);
139 DPRINTF(Loader, "Kernel loaded...\n");
140
141 Addr addr = 0;
142 #ifdef DEBUG
143 consolePanicEvent = addConsoleFuncEvent<BreakPCEvent>("panic");
144 #endif
145
146 /**
147 * Copy the osflags (kernel arguments) into the consoles
148 * memory. (Presently Linux does not use the console service
149 * routine to get these command line arguments, but Tru64 and
150 * others do.)
151 */
152 if (consoleSymtab->findAddress("env_booted_osflags", addr)) {
153 Addr paddr = vtophys(physmem, addr);
154 char *osflags = (char *)physmem->dma_addr(paddr, sizeof(uint32_t));
155
156 if (osflags)
157 strcpy(osflags, params->boot_osflags.c_str());
158 }
159
160 /**
161 * Set the hardware reset parameter block system type and revision
162 * information to Tsunami.
163 */
164 if (consoleSymtab->findAddress("m5_rpb", addr)) {
165 Addr paddr = vtophys(physmem, addr);
166 char *hwrpb = (char *)physmem->dma_addr(paddr, sizeof(uint64_t));
167
168 if (!hwrpb)
169 panic("could not translate hwrpb addr\n");
170
171 *(uint64_t*)(hwrpb+0x50) = htog(params->system_type);
172 *(uint64_t*)(hwrpb+0x58) = htog(params->system_rev);
173 } else
174 panic("could not find hwrpb\n");
175
176 // increment the number of running systms
177 numSystemsRunning++;
178
179 kernelBinning = new Kernel::Binning(this);
180 }
181
182 System::~System()
183 {
184 delete kernelSymtab;
185 delete consoleSymtab;
186 delete kernel;
187 delete console;
188 delete pal;
189
190 delete kernelBinning;
191
192 #ifdef DEBUG
193 delete consolePanicEvent;
194 #endif
195 }
196
197
198 /**
199 * This function fixes up addresses that are used to match PCs for
200 * hooking simulator events on to target function executions.
201 *
202 * Alpha binaries may have multiple global offset table (GOT)
203 * sections. A function that uses the GOT starts with a
204 * two-instruction prolog which sets the global pointer (gp == r29) to
205 * the appropriate GOT section. The proper gp value is calculated
206 * based on the function address, which must be passed by the caller
207 * in the procedure value register (pv aka t12 == r27). This sequence
208 * looks like the following:
209 *
210 * opcode Ra Rb offset
211 * ldah gp,X(pv) 09 29 27 X
212 * lda gp,Y(gp) 08 29 29 Y
213 *
214 * for some constant offsets X and Y. The catch is that the linker
215 * (or maybe even the compiler, I'm not sure) may recognize that the
216 * caller and callee are using the same GOT section, making this
217 * prolog redundant, and modify the call target to skip these
218 * instructions. If we check for execution of the first instruction
219 * of a function (the one the symbol points to) to detect when to skip
220 * it, we'll miss all these modified calls. It might work to
221 * unconditionally check for the third instruction, but not all
222 * functions have this prolog, and there's some chance that those
223 * first two instructions could have undesired consequences. So we do
224 * the Right Thing and pattern-match the first two instructions of the
225 * function to decide where to patch.
226 *
227 * Eventually this code should be moved into an ISA-specific file.
228 */
229 Addr
230 System::fixFuncEventAddr(Addr addr)
231 {
232 // mask for just the opcode, Ra, and Rb fields (not the offset)
233 const uint32_t inst_mask = 0xffff0000;
234 // ldah gp,X(pv): opcode 9, Ra = 29, Rb = 27
235 const uint32_t gp_ldah_pattern = (9 << 26) | (29 << 21) | (27 << 16);
236 // lda gp,Y(gp): opcode 8, Ra = 29, rb = 29
237 const uint32_t gp_lda_pattern = (8 << 26) | (29 << 21) | (29 << 16);
238 // instruction size
239 const int sz = sizeof(uint32_t);
240
241 Addr paddr = vtophys(physmem, addr);
242 uint32_t i1 = *(uint32_t *)physmem->dma_addr(paddr, sz);
243 uint32_t i2 = *(uint32_t *)physmem->dma_addr(paddr+sz, sz);
244
245 if ((i1 & inst_mask) == gp_ldah_pattern &&
246 (i2 & inst_mask) == gp_lda_pattern) {
247 Addr new_addr = addr + 2*sz;
248 DPRINTF(Loader, "fixFuncEventAddr: %p -> %p", addr, new_addr);
249 return new_addr;
250 } else {
251 return addr;
252 }
253 }
254
255
256 void
257 System::setAlphaAccess(Addr access)
258 {
259 Addr addr = 0;
260 if (consoleSymtab->findAddress("m5AlphaAccess", addr)) {
261 Addr paddr = vtophys(physmem, addr);
262 uint64_t *m5AlphaAccess =
263 (uint64_t *)physmem->dma_addr(paddr, sizeof(uint64_t));
264
265 if (!m5AlphaAccess)
266 panic("could not translate m5AlphaAccess addr\n");
267
268 *m5AlphaAccess = htog(EV5::Phys2K0Seg(access));
269 } else
270 panic("could not find m5AlphaAccess\n");
271 }
272
273
274 bool
275 System::breakpoint()
276 {
277 return remoteGDB[0]->trap(ALPHA_KENTRY_INT);
278 }
279
280 int rgdb_wait = -1;
281
282 int
283 System::registerExecContext(ExecContext *xc, int id)
284 {
285 if (id == -1) {
286 for (id = 0; id < execContexts.size(); id++) {
287 if (!execContexts[id])
288 break;
289 }
290 }
291
292 if (execContexts.size() <= id)
293 execContexts.resize(id + 1);
294
295 if (execContexts[id])
296 panic("Cannot have two CPUs with the same id (%d)\n", id);
297
298 execContexts[id] = xc;
299 numcpus++;
300
301 RemoteGDB *rgdb = new RemoteGDB(this, xc);
302 GDBListener *gdbl = new GDBListener(rgdb, 7000 + id);
303 gdbl->listen();
304 /**
305 * Uncommenting this line waits for a remote debugger to connect
306 * to the simulator before continuing.
307 */
308 if (rgdb_wait != -1 && rgdb_wait == id)
309 gdbl->accept();
310
311 if (remoteGDB.size() <= id) {
312 remoteGDB.resize(id + 1);
313 }
314
315 remoteGDB[id] = rgdb;
316
317 return id;
318 }
319
320 void
321 System::startup()
322 {
323 if (!execContexts.empty()) {
324 // activate with zero delay so that we start ticking right
325 // away on cycle 0
326 execContexts[0]->activate(0);
327 }
328 }
329
330 void
331 System::replaceExecContext(ExecContext *xc, int id)
332 {
333 if (id >= execContexts.size()) {
334 panic("replaceExecContext: bad id, %d >= %d\n",
335 id, execContexts.size());
336 }
337
338 execContexts[id] = xc;
339 remoteGDB[id]->replaceExecContext(xc);
340 }
341
342 void
343 System::regStats()
344 {
345 kernelBinning->regStats(name() + ".kern");
346 }
347
348 void
349 System::serialize(ostream &os)
350 {
351 kernelBinning->serialize(os);
352 }
353
354
355 void
356 System::unserialize(Checkpoint *cp, const string &section)
357 {
358 kernelBinning->unserialize(cp, section);
359 }
360
361 void
362 System::printSystems()
363 {
364 vector<System *>::iterator i = systemList.begin();
365 vector<System *>::iterator end = systemList.end();
366 for (; i != end; ++i) {
367 System *sys = *i;
368 cerr << "System " << sys->name() << ": " << hex << sys << endl;
369 }
370 }
371
372 extern "C"
373 void
374 printSystems()
375 {
376 System::printSystems();
377 }
378
379 BEGIN_DECLARE_SIM_OBJECT_PARAMS(System)
380
381 Param<Tick> boot_cpu_frequency;
382 SimObjectParam<MemoryController *> memctrl;
383 SimObjectParam<PhysicalMemory *> physmem;
384
385 Param<string> kernel;
386 Param<string> console;
387 Param<string> pal;
388
389 Param<string> boot_osflags;
390 Param<string> readfile;
391 Param<unsigned int> init_param;
392
393 Param<uint64_t> system_type;
394 Param<uint64_t> system_rev;
395
396 Param<bool> bin;
397 VectorParam<string> binned_fns;
398 Param<bool> bin_int;
399
400 END_DECLARE_SIM_OBJECT_PARAMS(System)
401
402 BEGIN_INIT_SIM_OBJECT_PARAMS(System)
403
404 INIT_PARAM(boot_cpu_frequency, "Frequency of the boot CPU"),
405 INIT_PARAM(memctrl, "memory controller"),
406 INIT_PARAM(physmem, "phsyical memory"),
407 INIT_PARAM(kernel, "file that contains the kernel code"),
408 INIT_PARAM(console, "file that contains the console code"),
409 INIT_PARAM(pal, "file that contains palcode"),
410 INIT_PARAM_DFLT(boot_osflags, "flags to pass to the kernel during boot",
411 "a"),
412 INIT_PARAM_DFLT(readfile, "file to read startup script from", ""),
413 INIT_PARAM_DFLT(init_param, "numerical value to pass into simulator", 0),
414 INIT_PARAM_DFLT(system_type, "Type of system we are emulating", 34),
415 INIT_PARAM_DFLT(system_rev, "Revision of system we are emulating", 1<<10),
416 INIT_PARAM_DFLT(bin, "is this system to be binned", false),
417 INIT_PARAM(binned_fns, "functions to be broken down and binned"),
418 INIT_PARAM_DFLT(bin_int, "is interrupt code binned seperately?", true)
419
420 END_INIT_SIM_OBJECT_PARAMS(System)
421
422 CREATE_SIM_OBJECT(System)
423 {
424 System::Params *p = new System::Params;
425 p->name = getInstanceName();
426 p->boot_cpu_frequency = boot_cpu_frequency;
427 p->memctrl = memctrl;
428 p->physmem = physmem;
429 p->kernel_path = kernel;
430 p->console_path = console;
431 p->palcode = pal;
432 p->boot_osflags = boot_osflags;
433 p->init_param = init_param;
434 p->readfile = readfile;
435 p->system_type = system_type;
436 p->system_rev = system_rev;
437 p->bin = bin;
438 p->binned_fns = binned_fns;
439 p->bin_int = bin_int;
440 return new System(p);
441 }
442
443 REGISTER_SIM_OBJECT("System", System)
444