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