system.cc revision 9649:c717bd5e0a1d
1/*
2 * Copyright (c) 2010-2012 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder.  You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Copyright (c) 2002-2006 The Regents of The University of Michigan
15 * All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *
40 * Authors: Ali Saidi
41 */
42
43#include "arch/arm/linux/atag.hh"
44#include "arch/arm/linux/system.hh"
45#include "arch/arm/isa_traits.hh"
46#include "arch/arm/utility.hh"
47#include "arch/generic/linux/threadinfo.hh"
48#include "base/loader/dtb_object.hh"
49#include "base/loader/object_file.hh"
50#include "base/loader/symtab.hh"
51#include "cpu/base.hh"
52#include "cpu/pc_event.hh"
53#include "cpu/thread_context.hh"
54#include "debug/Loader.hh"
55#include "kern/linux/events.hh"
56#include "mem/fs_translating_port_proxy.hh"
57#include "mem/physical.hh"
58#include "sim/stat_control.hh"
59
60using namespace ArmISA;
61using namespace Linux;
62
63LinuxArmSystem::LinuxArmSystem(Params *p)
64    : ArmSystem(p),
65      enableContextSwitchStatsDump(p->enable_context_switch_stats_dump),
66      kernelPanicEvent(NULL), kernelOopsEvent(NULL)
67{
68    if (p->panic_on_panic) {
69        kernelPanicEvent = addKernelFuncEventOrPanic<PanicPCEvent>(
70            "panic", "Kernel panic in simulated kernel");
71    } else {
72#ifndef NDEBUG
73        kernelPanicEvent = addKernelFuncEventOrPanic<BreakPCEvent>("panic");
74#endif
75    }
76
77    if (p->panic_on_oops) {
78        kernelOopsEvent = addKernelFuncEventOrPanic<PanicPCEvent>(
79            "oops_exit", "Kernel oops in guest");
80    }
81
82    // With ARM udelay() is #defined to __udelay
83    uDelaySkipEvent = addKernelFuncEventOrPanic<UDelayEvent>(
84        "__udelay", "__udelay", 1000, 0);
85
86    // constant arguments to udelay() have some precomputation done ahead of
87    // time. Constant comes from code.
88    constUDelaySkipEvent = addKernelFuncEventOrPanic<UDelayEvent>(
89        "__const_udelay", "__const_udelay", 1000, 107374);
90
91    secDataPtrAddr = 0;
92    secDataAddr = 0;
93    penReleaseAddr = 0;
94    kernelSymtab->findAddress("__secondary_data", secDataPtrAddr);
95    kernelSymtab->findAddress("secondary_data", secDataAddr);
96    kernelSymtab->findAddress("pen_release", penReleaseAddr);
97
98    secDataPtrAddr &= ~ULL(0x7F);
99    secDataAddr &= ~ULL(0x7F);
100    penReleaseAddr &= ~ULL(0x7F);
101}
102
103bool
104LinuxArmSystem::adderBootUncacheable(Addr a)
105{
106    Addr block = a & ~ULL(0x7F);
107    if (block == secDataPtrAddr || block == secDataAddr ||
108            block == penReleaseAddr)
109        return true;
110    return false;
111}
112
113void
114LinuxArmSystem::initState()
115{
116    // Moved from the constructor to here since it relies on the
117    // address map being resolved in the interconnect
118
119    // Call the initialisation of the super class
120    ArmSystem::initState();
121
122    // Load symbols at physical address, we might not want
123    // to do this permanently, for but early bootup work
124    // it is helpful.
125    if (params()->early_kernel_symbols) {
126        kernel->loadGlobalSymbols(kernelSymtab, loadAddrMask);
127        kernel->loadGlobalSymbols(debugSymbolTable, loadAddrMask);
128    }
129
130    // Setup boot data structure
131    Addr addr = 0;
132    // Check if the kernel image has a symbol that tells us it supports
133    // device trees.
134    bool kernel_has_fdt_support =
135        kernelSymtab->findAddress("unflatten_device_tree", addr);
136    bool dtb_file_specified = params()->dtb_filename != "";
137
138    if (kernel_has_fdt_support && dtb_file_specified) {
139        // Kernel supports flattened device tree and dtb file specified.
140        // Using Device Tree Blob to describe system configuration.
141        inform("Loading DTB file: %s\n", params()->dtb_filename);
142
143        ObjectFile *dtb_file = createObjectFile(params()->dtb_filename, true);
144        if (!dtb_file) {
145            fatal("couldn't load DTB file: %s\n", params()->dtb_filename);
146        }
147
148        DtbObject *_dtb_file = dynamic_cast<DtbObject*>(dtb_file);
149
150        if (_dtb_file) {
151            if (!_dtb_file->addBootCmdLine(params()->boot_osflags.c_str(),
152                                           params()->boot_osflags.size())) {
153                warn("couldn't append bootargs to DTB file: %s\n",
154                     params()->dtb_filename);
155            }
156        } else {
157            warn("dtb_file cast failed; couldn't append bootargs "
158                 "to DTB file: %s\n", params()->dtb_filename);
159        }
160
161        dtb_file->setTextBase(params()->atags_addr);
162        dtb_file->loadSections(physProxy);
163        delete dtb_file;
164    } else {
165        // Using ATAGS
166        // Warn if the kernel supports FDT and we haven't specified one
167        if (kernel_has_fdt_support) {
168            assert(!dtb_file_specified);
169            warn("Kernel supports device tree, but no DTB file specified\n");
170        }
171        // Warn if the kernel doesn't support FDT and we have specified one
172        if (dtb_file_specified) {
173            assert(!kernel_has_fdt_support);
174            warn("DTB file specified, but no device tree support in kernel\n");
175        }
176
177        AtagCore ac;
178        ac.flags(1); // read-only
179        ac.pagesize(8192);
180        ac.rootdev(0);
181
182        AddrRangeList atagRanges = physmem.getConfAddrRanges();
183        if (atagRanges.size() != 1) {
184            fatal("Expected a single ATAG memory entry but got %d\n",
185                  atagRanges.size());
186        }
187        AtagMem am;
188        am.memSize(atagRanges.begin()->size());
189        am.memStart(atagRanges.begin()->start());
190
191        AtagCmdline ad;
192        ad.cmdline(params()->boot_osflags);
193
194        DPRINTF(Loader, "boot command line %d bytes: %s\n",
195                ad.size() <<2, params()->boot_osflags.c_str());
196
197        AtagNone an;
198
199        uint32_t size = ac.size() + am.size() + ad.size() + an.size();
200        uint32_t offset = 0;
201        uint8_t *boot_data = new uint8_t[size << 2];
202
203        offset += ac.copyOut(boot_data + offset);
204        offset += am.copyOut(boot_data + offset);
205        offset += ad.copyOut(boot_data + offset);
206        offset += an.copyOut(boot_data + offset);
207
208        DPRINTF(Loader, "Boot atags was %d bytes in total\n", size << 2);
209        DDUMP(Loader, boot_data, size << 2);
210
211        physProxy.writeBlob(params()->atags_addr, boot_data, size << 2);
212
213        delete[] boot_data;
214    }
215
216    for (int i = 0; i < threadContexts.size(); i++) {
217        threadContexts[i]->setIntReg(0, 0);
218        threadContexts[i]->setIntReg(1, params()->machine_type);
219        threadContexts[i]->setIntReg(2, params()->atags_addr);
220    }
221}
222
223LinuxArmSystem::~LinuxArmSystem()
224{
225    if (uDelaySkipEvent)
226        delete uDelaySkipEvent;
227    if (constUDelaySkipEvent)
228        delete constUDelaySkipEvent;
229
230    if (dumpStatsPCEvent)
231        delete dumpStatsPCEvent;
232}
233
234LinuxArmSystem *
235LinuxArmSystemParams::create()
236{
237    return new LinuxArmSystem(this);
238}
239
240void
241LinuxArmSystem::startup()
242{
243    if (enableContextSwitchStatsDump) {
244        dumpStatsPCEvent = addKernelFuncEvent<DumpStatsPCEvent>("__switch_to");
245        if (!dumpStatsPCEvent)
246           panic("dumpStatsPCEvent not created!");
247
248        std::string task_filename = "tasks.txt";
249        taskFile = simout.create(name() + "." + task_filename);
250
251        for (int i = 0; i < _numContexts; i++) {
252            ThreadContext *tc = threadContexts[i];
253            uint32_t pid = tc->getCpuPtr()->getPid();
254            if (pid != Request::invldPid) {
255                mapPid(tc, pid);
256                tc->getCpuPtr()->taskId(taskMap[pid]);
257            }
258        }
259    }
260}
261
262void
263LinuxArmSystem::mapPid(ThreadContext *tc, uint32_t pid)
264{
265    // Create a new unique identifier for this pid
266    std::map<uint32_t, uint32_t>::iterator itr = taskMap.find(pid);
267    if (itr == taskMap.end()) {
268        uint32_t map_size = taskMap.size();
269        if (map_size > ContextSwitchTaskId::MaxNormalTaskId + 1) {
270            warn_once("Error out of identifiers for cache occupancy stats");
271            taskMap[pid] = ContextSwitchTaskId::Unknown;
272        } else {
273            taskMap[pid] = map_size;
274        }
275    }
276}
277
278/** This function is called whenever the the kernel function
279 *  "__switch_to" is called to change running tasks.
280 *
281 *  r0 = task_struct of the previously running process
282 *  r1 = task_info of the previously running process
283 *  r2 = task_info of the next process to run
284 */
285void
286DumpStatsPCEvent::process(ThreadContext *tc)
287{
288    Linux::ThreadInfo ti(tc);
289    Addr task_descriptor = tc->readIntReg(2);
290    uint32_t pid = ti.curTaskPID(task_descriptor);
291    uint32_t tgid = ti.curTaskTGID(task_descriptor);
292    std::string next_task_str = ti.curTaskName(task_descriptor);
293
294    // Streamline treats pid == -1 as the kernel process.
295    // Also pid == 0 implies idle process (except during Linux boot)
296    int32_t mm = ti.curTaskMm(task_descriptor);
297    bool is_kernel = (mm == 0);
298    if (is_kernel && (pid != 0)) {
299        pid = -1;
300        tgid = -1;
301        next_task_str = "kernel";
302    }
303
304    LinuxArmSystem* sys = dynamic_cast<LinuxArmSystem *>(tc->getSystemPtr());
305    if (!sys) {
306        panic("System is not LinuxArmSystem while getting Linux process info!");
307    }
308    std::map<uint32_t, uint32_t>& taskMap = sys->taskMap;
309
310    // Create a new unique identifier for this pid
311    sys->mapPid(tc, pid);
312
313    // Set cpu task id, output process info, and dump stats
314    tc->getCpuPtr()->taskId(taskMap[pid]);
315    tc->getCpuPtr()->setPid(pid);
316
317    std::ostream* taskFile = sys->taskFile;
318
319    // Task file is read by cache occupancy plotting script or
320    // Streamline conversion script.
321    ccprintf(*taskFile,
322             "tick=%lld %d cpu_id=%d next_pid=%d next_tgid=%d next_task=%s\n",
323             curTick(), taskMap[pid], tc->cpuId(), (int) pid, (int) tgid,
324             next_task_str);
325    taskFile->flush();
326
327    // Dump and reset statistics
328    Stats::schedStatEvent(true, true, curTick(), 0);
329}
330
331