system.cc revision 2036
1/*
2 * Copyright (c) 2004-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/**
30 * @file
31 * This code loads the linux kernel, console, pal and patches certain
32 * functions.  The symbol tables are loaded so that traces can show
33 * the executing function and we can skip functions. Various delay
34 * loops are skipped and their final values manually computed to speed
35 * up boot time.
36 */
37
38#include "base/loader/symtab.hh"
39#include "cpu/exec_context.hh"
40#include "cpu/base.hh"
41#include "kern/linux/linux_system.hh"
42#include "kern/linux/linux_threadinfo.hh"
43#include "kern/linux/printk.hh"
44#include "mem/functional/memory_control.hh"
45#include "mem/functional/physical.hh"
46#include "sim/builder.hh"
47#include "sim/byteswap.hh"
48#include "dev/platform.hh"
49#include "targetarch/arguments.hh"
50#include "targetarch/vtophys.hh"
51
52using namespace std;
53
54LinuxSystem::LinuxSystem(Params *p)
55    : System(p)
56{
57    Addr addr = 0;
58    Addr paddr = 0;
59
60    /**
61     * The symbol swapper_pg_dir marks the beginning of the kernel and
62     * the location of bootloader passed arguments
63     */
64    if (!kernelSymtab->findAddress("swapper_pg_dir", KernelStart)) {
65        panic("Could not determine start location of kernel");
66    }
67
68    /**
69     * Since we aren't using a bootloader, we have to copy the
70     * kernel arguments directly into the kernel's memory.
71     */
72    paddr = vtophys(physmem, CommandLine());
73    char *commandline = (char *)physmem->dma_addr(paddr, sizeof(uint64_t));
74    if (commandline)
75        strncpy(commandline, params->boot_osflags.c_str(), CommandLineSize);
76
77    /**
78     * find the address of the est_cycle_freq variable and insert it
79     * so we don't through the lengthly process of trying to
80     * calculated it by using the PIT, RTC, etc.
81     */
82    if (kernelSymtab->findAddress("est_cycle_freq", addr)) {
83        paddr = vtophys(physmem, addr);
84        uint8_t *est_cycle_frequency =
85            physmem->dma_addr(paddr, sizeof(uint64_t));
86
87        if (est_cycle_frequency)
88            *(uint64_t *)est_cycle_frequency =
89                Clock::Frequency / p->boot_cpu_frequency;
90    }
91
92
93    /**
94     * EV5 only supports 127 ASNs so we are going to tell the kernel that the
95     * paritiuclar EV6 we have only supports 127 asns.
96     * @todo At some point we should change ev5.hh and the palcode to support
97     * 255 ASNs.
98     */
99    if (kernelSymtab->findAddress("dp264_mv", addr)) {
100        paddr = vtophys(physmem, addr);
101        char *dp264_mv = (char *)physmem->dma_addr(paddr, sizeof(uint64_t));
102
103        if (dp264_mv) {
104            *(uint32_t*)(dp264_mv+0x18) = LittleEndianGuest::htog((uint32_t)127);
105        } else
106            panic("could not translate dp264_mv addr\n");
107
108    } else
109        panic("could not find dp264_mv\n");
110
111#ifndef NDEBUG
112    kernelPanicEvent = addKernelFuncEvent<BreakPCEvent>("panic");
113    if (!kernelPanicEvent)
114        panic("could not find kernel symbol \'panic\'");
115
116#if 0
117    kernelDieEvent = addKernelFuncEvent<BreakPCEvent>("die_if_kernel");
118    if (!kernelDieEvent)
119        panic("could not find kernel symbol \'die_if_kernel\'");
120#endif
121
122#endif
123
124    /**
125     * Any time ide_delay_50ms, calibarte_delay or
126     * determine_cpu_caches is called just skip the
127     * function. Currently determine_cpu_caches only is used put
128     * information in proc, however if that changes in the future we
129     * will have to fill in the cache size variables appropriately.
130     */
131
132    skipIdeDelay50msEvent =
133        addKernelFuncEvent<SkipFuncEvent>("ide_delay_50ms");
134    skipDelayLoopEvent =
135        addKernelFuncEvent<SkipDelayLoopEvent>("calibrate_delay");
136    skipCacheProbeEvent =
137        addKernelFuncEvent<SkipFuncEvent>("determine_cpu_caches");
138    debugPrintkEvent = addKernelFuncEvent<DebugPrintkEvent>("dprintk");
139    idleStartEvent = addKernelFuncEvent<IdleStartEvent>("cpu_idle");
140
141    if (kernelSymtab->findAddress("alpha_switch_to", addr) && DTRACE(Thread)) {
142        printThreadEvent = new PrintThreadInfo(&pcEventQueue, "threadinfo",
143                                               addr + sizeof(MachInst) * 6);
144    } else {
145        printThreadEvent = NULL;
146    }
147
148    if (params->bin_int) {
149        intStartEvent = addPalFuncEvent<InterruptStartEvent>("sys_int_21");
150        if (!intStartEvent)
151            panic("could not find symbol: sys_int_21\n");
152
153        intEndEvent = addPalFuncEvent<InterruptEndEvent>("rti_to_kern");
154        if (!intEndEvent)
155            panic("could not find symbol: rti_to_kern\n");
156
157        intEndEvent2 = addPalFuncEvent<InterruptEndEvent>("rti_to_user");
158        if (!intEndEvent2)
159            panic("could not find symbol: rti_to_user\n");
160
161        intEndEvent3 = addKernelFuncEvent<InterruptEndEvent>("do_softirq");
162        if (!intEndEvent3)
163            panic("could not find symbol: do_softirq\n");
164    }
165}
166
167LinuxSystem::~LinuxSystem()
168{
169#ifndef NDEBUG
170    delete kernelPanicEvent;
171#endif
172    delete skipIdeDelay50msEvent;
173    delete skipDelayLoopEvent;
174    delete skipCacheProbeEvent;
175    delete debugPrintkEvent;
176    delete idleStartEvent;
177    delete printThreadEvent;
178    delete intStartEvent;
179    delete intEndEvent;
180    delete intEndEvent2;
181}
182
183
184void
185LinuxSystem::setDelayLoop(ExecContext *xc)
186{
187    Addr addr = 0;
188    if (kernelSymtab->findAddress("loops_per_jiffy", addr)) {
189        Addr paddr = vtophys(physmem, addr);
190
191        uint8_t *loops_per_jiffy =
192            physmem->dma_addr(paddr, sizeof(uint32_t));
193
194        Tick cpuFreq = xc->cpu->frequency();
195        Tick intrFreq = platform->intrFrequency();
196        *(uint32_t *)loops_per_jiffy =
197            (uint32_t)((cpuFreq / intrFreq) * 0.9988);
198    }
199}
200
201void
202LinuxSystem::SkipDelayLoopEvent::process(ExecContext *xc)
203{
204    SkipFuncEvent::process(xc);
205    // calculate and set loops_per_jiffy
206    ((LinuxSystem *)xc->system)->setDelayLoop(xc);
207}
208
209void
210LinuxSystem::DebugPrintkEvent::process(ExecContext *xc)
211{
212    if (DTRACE(DebugPrintf)) {
213        if (!raw) {
214            StringWrap name(xc->system->name() + ".dprintk");
215            DPRINTFN("");
216        }
217
218        AlphaArguments args(xc);
219        Printk(args);
220        SkipFuncEvent::process(xc);
221    }
222}
223
224void
225LinuxSystem::PrintThreadInfo::process(ExecContext *xc)
226{
227    Linux::ThreadInfo ti(xc);
228
229    DPRINTF(Thread, "Currently Executing Thread %s, pid %d, started at: %d\n",
230            ti.curTaskName(), ti.curTaskPID(), ti.curTaskStart());
231}
232
233
234BEGIN_DECLARE_SIM_OBJECT_PARAMS(LinuxSystem)
235
236    Param<Tick> boot_cpu_frequency;
237    SimObjectParam<MemoryController *> memctrl;
238    SimObjectParam<PhysicalMemory *> physmem;
239
240    Param<string> kernel;
241    Param<string> console;
242    Param<string> pal;
243
244    Param<string> boot_osflags;
245    Param<string> readfile;
246    Param<unsigned int> init_param;
247
248    Param<uint64_t> system_type;
249    Param<uint64_t> system_rev;
250
251    Param<bool> bin;
252    VectorParam<string> binned_fns;
253    Param<bool> bin_int;
254
255END_DECLARE_SIM_OBJECT_PARAMS(LinuxSystem)
256
257BEGIN_INIT_SIM_OBJECT_PARAMS(LinuxSystem)
258
259    INIT_PARAM(boot_cpu_frequency, "Frequency of the boot CPU"),
260    INIT_PARAM(memctrl, "memory controller"),
261    INIT_PARAM(physmem, "phsyical memory"),
262    INIT_PARAM(kernel, "file that contains the kernel code"),
263    INIT_PARAM(console, "file that contains the console code"),
264    INIT_PARAM(pal, "file that contains palcode"),
265    INIT_PARAM_DFLT(boot_osflags, "flags to pass to the kernel during boot",
266                    "a"),
267    INIT_PARAM_DFLT(readfile, "file to read startup script from", ""),
268    INIT_PARAM_DFLT(init_param, "numerical value to pass into simulator", 0),
269    INIT_PARAM_DFLT(system_type, "Type of system we are emulating", 34),
270    INIT_PARAM_DFLT(system_rev, "Revision of system we are emulating", 1<<10),
271    INIT_PARAM_DFLT(bin, "is this system to be binned", false),
272    INIT_PARAM(binned_fns, "functions to be broken down and binned"),
273    INIT_PARAM_DFLT(bin_int, "is interrupt code binned seperately?", true)
274
275END_INIT_SIM_OBJECT_PARAMS(LinuxSystem)
276
277CREATE_SIM_OBJECT(LinuxSystem)
278{
279    System::Params *p = new System::Params;
280    p->name = getInstanceName();
281    p->boot_cpu_frequency = boot_cpu_frequency;
282    p->memctrl = memctrl;
283    p->physmem = physmem;
284    p->kernel_path = kernel;
285    p->console_path = console;
286    p->palcode = pal;
287    p->boot_osflags = boot_osflags;
288    p->init_param = init_param;
289    p->readfile = readfile;
290    p->system_type = system_type;
291    p->system_rev = system_rev;
292    p->bin = bin;
293    p->binned_fns = binned_fns;
294    p->bin_int = bin_int;
295    return new LinuxSystem(p);
296}
297
298REGISTER_SIM_OBJECT("LinuxSystem", LinuxSystem)
299
300