2 * Copyright (c) 2002-2005 The Regents of The University of Michigan
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.
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.
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"
43 vector
<System
*> System::systemList
;
45 int System::numSystemsRunning
= 0;
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
)
51 // add self to global system list
52 systemList
.push_back(this);
54 kernelSymtab
= new SymbolTable
;
55 consoleSymtab
= new SymbolTable
;
56 palSymtab
= new SymbolTable
;
57 allSymtab
= new SymbolTable
;
58 debugSymbolTable
= new SymbolTable
;
61 * Load the kernel, pal, and console code into memory
64 kernel
= createObjectFile(params
->kernel_path
);
66 fatal("Could not load kernel file %s", params
->kernel_path
);
69 console
= createObjectFile(params
->console_path
);
71 fatal("Could not load console file %s", params
->console_path
);
74 pal
= createObjectFile(params
->palcode
);
76 fatal("Could not load PALcode file %s", params
->palcode
);
79 // Load program sections into memory
80 pal
->loadSections(physmem
, true);
81 console
->loadSections(physmem
, true);
82 kernel
->loadSections(physmem
, true);
85 kernelStart
= kernel
->textBase();
86 kernelEnd
= kernel
->bssBase() + kernel
->bssSize();
87 kernelEntry
= kernel
->entryPoint();
90 if (!kernel
->loadGlobalSymbols(kernelSymtab
))
91 panic("could not load kernel symbols\n");
93 if (!kernel
->loadLocalSymbols(kernelSymtab
))
94 panic("could not load kernel local symbols\n");
96 if (!console
->loadGlobalSymbols(consoleSymtab
))
97 panic("could not load console symbols\n");
99 if (!pal
->loadGlobalSymbols(palSymtab
))
100 panic("could not load pal symbols\n");
102 if (!pal
->loadLocalSymbols(palSymtab
))
103 panic("could not load pal symbols\n");
105 if (!kernel
->loadGlobalSymbols(allSymtab
))
106 panic("could not load kernel symbols\n");
108 if (!kernel
->loadLocalSymbols(allSymtab
))
109 panic("could not load kernel local symbols\n");
111 if (!console
->loadGlobalSymbols(allSymtab
))
112 panic("could not load console symbols\n");
114 if (!pal
->loadGlobalSymbols(allSymtab
))
115 panic("could not load pal symbols\n");
117 if (!pal
->loadLocalSymbols(allSymtab
))
118 panic("could not load pal symbols\n");
120 if (!kernel
->loadGlobalSymbols(debugSymbolTable
))
121 panic("could not load kernel symbols\n");
123 if (!kernel
->loadLocalSymbols(debugSymbolTable
))
124 panic("could not load kernel local symbols\n");
126 if (!console
->loadGlobalSymbols(debugSymbolTable
))
127 panic("could not load console symbols\n");
129 if (!pal
->loadGlobalSymbols(debugSymbolTable
))
130 panic("could not load pal symbols\n");
132 if (!pal
->loadLocalSymbols(debugSymbolTable
))
133 panic("could not load pal symbols\n");
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");
143 consolePanicEvent
= addConsoleFuncEvent
<BreakPCEvent
>("panic");
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
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));
157 strcpy(osflags
, params
->boot_osflags
.c_str());
161 * Set the hardware reset parameter block system type and revision
162 * information to Tsunami.
164 if (consoleSymtab
->findAddress("m5_rpb", addr
)) {
165 Addr paddr
= vtophys(physmem
, addr
);
166 char *hwrpb
= (char *)physmem
->dma_addr(paddr
, sizeof(uint64_t));
169 panic("could not translate hwrpb addr\n");
171 *(uint64_t*)(hwrpb
+0x50) = htog(params
->system_type
);
172 *(uint64_t*)(hwrpb
+0x58) = htog(params
->system_rev
);
174 panic("could not find hwrpb\n");
176 // increment the number of running systms
179 kernelBinning
= new Kernel::Binning(this);
185 delete consoleSymtab
;
190 delete kernelBinning
;
193 delete consolePanicEvent
;
199 * This function fixes up addresses that are used to match PCs for
200 * hooking simulator events on to target function executions.
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:
210 * opcode Ra Rb offset
211 * ldah gp,X(pv) 09 29 27 X
212 * lda gp,Y(gp) 08 29 29 Y
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.
227 * Eventually this code should be moved into an ISA-specific file.
230 System::fixFuncEventAddr(Addr addr
)
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);
239 const int sz
= sizeof(uint32_t);
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
);
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
);
257 System::setAlphaAccess(Addr access
)
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));
266 panic("could not translate m5AlphaAccess addr\n");
268 *m5AlphaAccess
= htog(EV5::Phys2K0Seg(access
));
270 panic("could not find m5AlphaAccess\n");
277 return remoteGDB
[0]->trap(ALPHA_KENTRY_INT
);
283 System::registerExecContext(ExecContext
*xc
, int id
)
286 for (id
= 0; id
< execContexts
.size(); id
++) {
287 if (!execContexts
[id
])
292 if (execContexts
.size() <= id
)
293 execContexts
.resize(id
+ 1);
295 if (execContexts
[id
])
296 panic("Cannot have two CPUs with the same id (%d)\n", id
);
298 execContexts
[id
] = xc
;
301 RemoteGDB
*rgdb
= new RemoteGDB(this, xc
);
302 GDBListener
*gdbl
= new GDBListener(rgdb
, 7000 + id
);
305 * Uncommenting this line waits for a remote debugger to connect
306 * to the simulator before continuing.
308 if (rgdb_wait
!= -1 && rgdb_wait
== id
)
311 if (remoteGDB
.size() <= id
) {
312 remoteGDB
.resize(id
+ 1);
315 remoteGDB
[id
] = rgdb
;
323 if (!execContexts
.empty()) {
324 // activate with zero delay so that we start ticking right
326 execContexts
[0]->activate(0);
331 System::replaceExecContext(ExecContext
*xc
, int id
)
333 if (id
>= execContexts
.size()) {
334 panic("replaceExecContext: bad id, %d >= %d\n",
335 id
, execContexts
.size());
338 execContexts
[id
] = xc
;
339 remoteGDB
[id
]->replaceExecContext(xc
);
345 kernelBinning
->regStats(name() + ".kern");
349 System::serialize(ostream
&os
)
351 kernelBinning
->serialize(os
);
356 System::unserialize(Checkpoint
*cp
, const string
§ion
)
358 kernelBinning
->unserialize(cp
, section
);
362 System::printSystems()
364 vector
<System
*>::iterator i
= systemList
.begin();
365 vector
<System
*>::iterator end
= systemList
.end();
366 for (; i
!= end
; ++i
) {
368 cerr
<< "System " << sys
->name() << ": " << hex
<< sys
<< endl
;
376 System::printSystems();
379 BEGIN_DECLARE_SIM_OBJECT_PARAMS(System
)
381 Param
<Tick
> boot_cpu_frequency
;
382 SimObjectParam
<MemoryController
*> memctrl
;
383 SimObjectParam
<PhysicalMemory
*> physmem
;
385 Param
<string
> kernel
;
386 Param
<string
> console
;
389 Param
<string
> boot_osflags
;
390 Param
<string
> readfile
;
391 Param
<unsigned int> init_param
;
393 Param
<uint64_t> system_type
;
394 Param
<uint64_t> system_rev
;
397 VectorParam
<string
> binned_fns
;
400 END_DECLARE_SIM_OBJECT_PARAMS(System
)
402 BEGIN_INIT_SIM_OBJECT_PARAMS(System
)
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",
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)
420 END_INIT_SIM_OBJECT_PARAMS(System
)
422 CREATE_SIM_OBJECT(System
)
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
;
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
;
438 p
->binned_fns
= binned_fns
;
439 p
->bin_int
= bin_int
;
440 return new System(p
);
443 REGISTER_SIM_OBJECT("System", System
)