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 "arch/isa_traits.hh"
39 #include "sim/byteswap.hh"
40 #include "sim/system.hh"
41 #include "base/trace.hh"
45 vector
<System
*> System::systemList
;
47 int System::numSystemsRunning
= 0;
49 System::System(Params
*p
)
50 : SimObject(p
->name
), memctrl(p
->memctrl
), physmem(p
->physmem
),
51 init_param(p
->init_param
), numcpus(0), params(p
)
53 // add self to global system list
54 systemList
.push_back(this);
56 kernelSymtab
= new SymbolTable
;
57 consoleSymtab
= new SymbolTable
;
58 palSymtab
= new SymbolTable
;
59 debugSymbolTable
= new SymbolTable
;
62 * Load the kernel, pal, and console code into memory
65 kernel
= createObjectFile(params
->kernel_path
);
67 fatal("Could not load kernel file %s", params
->kernel_path
);
70 console
= createObjectFile(params
->console_path
);
72 fatal("Could not load console file %s", params
->console_path
);
75 pal
= createObjectFile(params
->palcode
);
77 fatal("Could not load PALcode file %s", params
->palcode
);
80 // Load program sections into memory
81 pal
->loadSections(physmem
, true);
82 console
->loadSections(physmem
, true);
83 kernel
->loadSections(physmem
, true);
86 kernelStart
= kernel
->textBase();
87 kernelEnd
= kernel
->bssBase() + kernel
->bssSize();
88 kernelEntry
= kernel
->entryPoint();
91 if (!kernel
->loadGlobalSymbols(kernelSymtab
))
92 panic("could not load kernel symbols\n");
94 if (!kernel
->loadLocalSymbols(kernelSymtab
))
95 panic("could not load kernel local symbols\n");
97 if (!console
->loadGlobalSymbols(consoleSymtab
))
98 panic("could not load console symbols\n");
100 if (!pal
->loadGlobalSymbols(palSymtab
))
101 panic("could not load pal symbols\n");
103 if (!pal
->loadLocalSymbols(palSymtab
))
104 panic("could not load pal symbols\n");
106 if (!kernel
->loadGlobalSymbols(debugSymbolTable
))
107 panic("could not load kernel symbols\n");
109 if (!kernel
->loadLocalSymbols(debugSymbolTable
))
110 panic("could not load kernel local symbols\n");
112 if (!console
->loadGlobalSymbols(debugSymbolTable
))
113 panic("could not load console symbols\n");
115 if (!pal
->loadGlobalSymbols(debugSymbolTable
))
116 panic("could not load pal symbols\n");
118 if (!pal
->loadLocalSymbols(debugSymbolTable
))
119 panic("could not load pal symbols\n");
122 DPRINTF(Loader
, "Kernel start = %#x\n", kernelStart
);
123 DPRINTF(Loader
, "Kernel end = %#x\n", kernelEnd
);
124 DPRINTF(Loader
, "Kernel entry = %#x\n", kernelEntry
);
125 DPRINTF(Loader
, "Kernel loaded...\n");
129 consolePanicEvent
= addConsoleFuncEvent
<BreakPCEvent
>("panic");
133 * Copy the osflags (kernel arguments) into the consoles
134 * memory. (Presently Linux does not use the console service
135 * routine to get these command line arguments, but Tru64 and
138 if (consoleSymtab
->findAddress("env_booted_osflags", addr
)) {
139 Addr paddr
= vtophys(physmem
, addr
);
140 char *osflags
= (char *)physmem
->dma_addr(paddr
, sizeof(uint32_t));
143 strcpy(osflags
, params
->boot_osflags
.c_str());
147 * Set the hardware reset parameter block system type and revision
148 * information to Tsunami.
150 if (consoleSymtab
->findAddress("m5_rpb", addr
)) {
151 Addr paddr
= vtophys(physmem
, addr
);
152 char *hwrpb
= (char *)physmem
->dma_addr(paddr
, sizeof(uint64_t));
155 panic("could not translate hwrpb addr\n");
157 *(uint64_t*)(hwrpb
+0x50) = htog(params
->system_type
);
158 *(uint64_t*)(hwrpb
+0x58) = htog(params
->system_rev
);
160 panic("could not find hwrpb\n");
162 // increment the number of running systms
165 kernelBinning
= new Kernel::Binning(this);
171 delete consoleSymtab
;
176 delete kernelBinning
;
179 delete consolePanicEvent
;
185 * This function fixes up addresses that are used to match PCs for
186 * hooking simulator events on to target function executions.
188 * Alpha binaries may have multiple global offset table (GOT)
189 * sections. A function that uses the GOT starts with a
190 * two-instruction prolog which sets the global pointer (gp == r29) to
191 * the appropriate GOT section. The proper gp value is calculated
192 * based on the function address, which must be passed by the caller
193 * in the procedure value register (pv aka t12 == r27). This sequence
194 * looks like the following:
196 * opcode Ra Rb offset
197 * ldah gp,X(pv) 09 29 27 X
198 * lda gp,Y(gp) 08 29 29 Y
200 * for some constant offsets X and Y. The catch is that the linker
201 * (or maybe even the compiler, I'm not sure) may recognize that the
202 * caller and callee are using the same GOT section, making this
203 * prolog redundant, and modify the call target to skip these
204 * instructions. If we check for execution of the first instruction
205 * of a function (the one the symbol points to) to detect when to skip
206 * it, we'll miss all these modified calls. It might work to
207 * unconditionally check for the third instruction, but not all
208 * functions have this prolog, and there's some chance that those
209 * first two instructions could have undesired consequences. So we do
210 * the Right Thing and pattern-match the first two instructions of the
211 * function to decide where to patch.
213 * Eventually this code should be moved into an ISA-specific file.
216 System::fixFuncEventAddr(Addr addr
)
218 // mask for just the opcode, Ra, and Rb fields (not the offset)
219 const uint32_t inst_mask
= 0xffff0000;
220 // ldah gp,X(pv): opcode 9, Ra = 29, Rb = 27
221 const uint32_t gp_ldah_pattern
= (9 << 26) | (29 << 21) | (27 << 16);
222 // lda gp,Y(gp): opcode 8, Ra = 29, rb = 29
223 const uint32_t gp_lda_pattern
= (8 << 26) | (29 << 21) | (29 << 16);
225 const int sz
= sizeof(uint32_t);
227 Addr paddr
= vtophys(physmem
, addr
);
228 uint32_t i1
= *(uint32_t *)physmem
->dma_addr(paddr
, sz
);
229 uint32_t i2
= *(uint32_t *)physmem
->dma_addr(paddr
+sz
, sz
);
231 if ((i1
& inst_mask
) == gp_ldah_pattern
&&
232 (i2
& inst_mask
) == gp_lda_pattern
) {
233 Addr new_addr
= addr
+ 2*sz
;
234 DPRINTF(Loader
, "fixFuncEventAddr: %p -> %p", addr
, new_addr
);
243 System::setAlphaAccess(Addr access
)
246 if (consoleSymtab
->findAddress("m5AlphaAccess", addr
)) {
247 Addr paddr
= vtophys(physmem
, addr
);
248 uint64_t *m5AlphaAccess
=
249 (uint64_t *)physmem
->dma_addr(paddr
, sizeof(uint64_t));
252 panic("could not translate m5AlphaAccess addr\n");
254 *m5AlphaAccess
= htog(EV5::Phys2K0Seg(access
));
256 panic("could not find m5AlphaAccess\n");
263 return remoteGDB
[0]->trap(ALPHA_KENTRY_INT
);
269 System::registerExecContext(ExecContext
*xc
, int id
)
272 for (id
= 0; id
< execContexts
.size(); id
++) {
273 if (!execContexts
[id
])
278 if (execContexts
.size() <= id
)
279 execContexts
.resize(id
+ 1);
281 if (execContexts
[id
])
282 panic("Cannot have two CPUs with the same id (%d)\n", id
);
284 execContexts
[id
] = xc
;
287 RemoteGDB
*rgdb
= new RemoteGDB(this, xc
);
288 GDBListener
*gdbl
= new GDBListener(rgdb
, 7000 + id
);
291 * Uncommenting this line waits for a remote debugger to connect
292 * to the simulator before continuing.
294 if (rgdb_wait
!= -1 && rgdb_wait
== id
)
297 if (remoteGDB
.size() <= id
) {
298 remoteGDB
.resize(id
+ 1);
301 remoteGDB
[id
] = rgdb
;
309 if (!execContexts
.empty()) {
310 // activate with zero delay so that we start ticking right
312 execContexts
[0]->activate(0);
317 System::replaceExecContext(ExecContext
*xc
, int id
)
319 if (id
>= execContexts
.size()) {
320 panic("replaceExecContext: bad id, %d >= %d\n",
321 id
, execContexts
.size());
324 execContexts
[id
] = xc
;
325 remoteGDB
[id
]->replaceExecContext(xc
);
331 kernelBinning
->regStats(name() + ".kern");
335 System::serialize(ostream
&os
)
337 kernelBinning
->serialize(os
);
339 kernelSymtab
->serialize("kernel_symtab", os
);
340 consoleSymtab
->serialize("console_symtab", os
);
341 palSymtab
->serialize("pal_symtab", os
);
346 System::unserialize(Checkpoint
*cp
, const string
§ion
)
348 kernelBinning
->unserialize(cp
, section
);
350 kernelSymtab
->unserialize("kernel_symtab", cp
, section
);
351 consoleSymtab
->unserialize("console_symtab", cp
, section
);
352 palSymtab
->unserialize("pal_symtab", cp
, section
);
356 System::printSystems()
358 vector
<System
*>::iterator i
= systemList
.begin();
359 vector
<System
*>::iterator end
= systemList
.end();
360 for (; i
!= end
; ++i
) {
362 cerr
<< "System " << sys
->name() << ": " << hex
<< sys
<< endl
;
370 System::printSystems();
373 BEGIN_DECLARE_SIM_OBJECT_PARAMS(System
)
375 Param
<Tick
> boot_cpu_frequency
;
376 SimObjectParam
<MemoryController
*> memctrl
;
377 SimObjectParam
<PhysicalMemory
*> physmem
;
379 Param
<string
> kernel
;
380 Param
<string
> console
;
383 Param
<string
> boot_osflags
;
384 Param
<string
> readfile
;
385 Param
<unsigned int> init_param
;
387 Param
<uint64_t> system_type
;
388 Param
<uint64_t> system_rev
;
391 VectorParam
<string
> binned_fns
;
394 END_DECLARE_SIM_OBJECT_PARAMS(System
)
396 BEGIN_INIT_SIM_OBJECT_PARAMS(System
)
398 INIT_PARAM(boot_cpu_frequency
, "Frequency of the boot CPU"),
399 INIT_PARAM(memctrl
, "memory controller"),
400 INIT_PARAM(physmem
, "phsyical memory"),
401 INIT_PARAM(kernel
, "file that contains the kernel code"),
402 INIT_PARAM(console
, "file that contains the console code"),
403 INIT_PARAM(pal
, "file that contains palcode"),
404 INIT_PARAM_DFLT(boot_osflags
, "flags to pass to the kernel during boot",
406 INIT_PARAM_DFLT(readfile
, "file to read startup script from", ""),
407 INIT_PARAM_DFLT(init_param
, "numerical value to pass into simulator", 0),
408 INIT_PARAM_DFLT(system_type
, "Type of system we are emulating", 34),
409 INIT_PARAM_DFLT(system_rev
, "Revision of system we are emulating", 1<<10),
410 INIT_PARAM_DFLT(bin
, "is this system to be binned", false),
411 INIT_PARAM(binned_fns
, "functions to be broken down and binned"),
412 INIT_PARAM_DFLT(bin_int
, "is interrupt code binned seperately?", true)
414 END_INIT_SIM_OBJECT_PARAMS(System
)
416 CREATE_SIM_OBJECT(System
)
418 System::Params
*p
= new System::Params
;
419 p
->name
= getInstanceName();
420 p
->boot_cpu_frequency
= boot_cpu_frequency
;
421 p
->memctrl
= memctrl
;
422 p
->physmem
= physmem
;
423 p
->kernel_path
= kernel
;
424 p
->console_path
= console
;
426 p
->boot_osflags
= boot_osflags
;
427 p
->init_param
= init_param
;
428 p
->readfile
= readfile
;
429 p
->system_type
= system_type
;
430 p
->system_rev
= system_rev
;
432 p
->binned_fns
= binned_fns
;
433 p
->bin_int
= bin_int
;
434 return new System(p
);
437 REGISTER_SIM_OBJECT("System", System
)