arch,base,cpu,sim: Statically allocate debugSymbolTable.
[gem5.git] / src / arch / arm / linux / fs_workload.cc
1 /*
2 * Copyright (c) 2010-2013, 2016 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
41 #include "arch/arm/linux/fs_workload.hh"
42
43 #include "arch/arm/isa_traits.hh"
44 #include "arch/arm/linux/atag.hh"
45 #include "arch/arm/system.hh"
46 #include "arch/arm/utility.hh"
47 #include "arch/generic/linux/threadinfo.hh"
48 #include "base/loader/dtb_file.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 "kern/linux/helpers.hh"
57 #include "kern/system_events.hh"
58 #include "mem/physical.hh"
59 #include "sim/stat_control.hh"
60
61 using namespace Linux;
62
63 namespace ArmISA
64 {
65
66 FsLinux::FsLinux(Params *p) : ArmISA::FsWorkload(p),
67 enableContextSwitchStatsDump(p->enable_context_switch_stats_dump)
68 {}
69
70 void
71 FsLinux::initState()
72 {
73 ArmISA::FsWorkload::initState();
74
75 // Load symbols at physical address, we might not want
76 // to do this permanently, for but early bootup work
77 // it is helpful.
78 if (params()->early_kernel_symbols) {
79 kernelObj->loadGlobalSymbols(kernelSymtab, 0, 0, _loadAddrMask);
80 kernelObj->loadGlobalSymbols(
81 &Loader::debugSymbolTable, 0, 0, _loadAddrMask);
82 }
83
84 // Setup boot data structure
85 // Check if the kernel image has a symbol that tells us it supports
86 // device trees.
87 bool kernel_has_fdt_support =
88 kernelSymtab->find("unflatten_device_tree") != kernelSymtab->end();
89 bool dtb_file_specified = params()->dtb_filename != "";
90
91 if (kernel_has_fdt_support && dtb_file_specified) {
92 // Kernel supports flattened device tree and dtb file specified.
93 // Using Device Tree Blob to describe system configuration.
94 inform("Loading DTB file: %s at address %#x\n", params()->dtb_filename,
95 params()->atags_addr + _loadAddrOffset);
96
97 auto *dtb_file = new ::Loader::DtbFile(params()->dtb_filename);
98
99 if (!dtb_file->addBootCmdLine(
100 commandLine.c_str(), commandLine.size())) {
101 warn("couldn't append bootargs to DTB file: %s\n",
102 params()->dtb_filename);
103 }
104
105 dtb_file->buildImage().
106 offset(params()->atags_addr + _loadAddrOffset).
107 write(system->physProxy);
108 delete dtb_file;
109 } else {
110 // Using ATAGS
111 // Warn if the kernel supports FDT and we haven't specified one
112 if (kernel_has_fdt_support) {
113 assert(!dtb_file_specified);
114 warn("Kernel supports device tree, but no DTB file specified\n");
115 }
116 // Warn if the kernel doesn't support FDT and we have specified one
117 if (dtb_file_specified) {
118 assert(!kernel_has_fdt_support);
119 warn("DTB file specified, but no device tree support in kernel\n");
120 }
121
122 AtagCore ac;
123 ac.flags(1); // read-only
124 ac.pagesize(8192);
125 ac.rootdev(0);
126
127 AddrRangeList atagRanges = system->getPhysMem().getConfAddrRanges();
128 fatal_if(atagRanges.size() != 1,
129 "Expected a single ATAG memory entry but got %d",
130 atagRanges.size());
131 AtagMem am;
132 am.memSize(atagRanges.begin()->size());
133 am.memStart(atagRanges.begin()->start());
134
135 AtagCmdline ad;
136 ad.cmdline(commandLine);
137
138 DPRINTF(Loader, "boot command line %d bytes: %s\n",
139 ad.size() << 2, commandLine);
140
141 AtagNone an;
142
143 uint32_t size = ac.size() + am.size() + ad.size() + an.size();
144 uint32_t offset = 0;
145 uint8_t *boot_data = new uint8_t[size << 2];
146
147 offset += ac.copyOut(boot_data + offset);
148 offset += am.copyOut(boot_data + offset);
149 offset += ad.copyOut(boot_data + offset);
150 offset += an.copyOut(boot_data + offset);
151
152 DPRINTF(Loader, "Boot atags was %d bytes in total\n", size << 2);
153 DDUMP(Loader, boot_data, size << 2);
154
155 system->physProxy.writeBlob(params()->atags_addr + _loadAddrOffset,
156 boot_data, size << 2);
157
158 delete[] boot_data;
159 }
160
161 // Kernel boot requirements to set up r0, r1 and r2 in ARMv7
162 for (auto tc: system->threadContexts) {
163 tc->setIntReg(0, 0);
164 tc->setIntReg(1, params()->machine_type);
165 tc->setIntReg(2, params()->atags_addr + _loadAddrOffset);
166 }
167 }
168
169 FsLinux::~FsLinux()
170 {
171 delete debugPrintk;
172 delete skipUDelay;
173 delete skipConstUDelay;
174 delete kernelOops;
175 delete kernelPanic;
176
177 delete dumpStats;
178 delete debugPrintk;
179 }
180
181 void
182 FsLinux::startup()
183 {
184 FsWorkload::startup();
185
186 if (enableContextSwitchStatsDump) {
187 if (getArch() == Loader::Arm64)
188 dumpStats = addKernelFuncEvent<DumpStats64>("__switch_to");
189 else
190 dumpStats = addKernelFuncEvent<DumpStats>("__switch_to");
191
192 panic_if(!dumpStats, "dumpStats not created!");
193
194 std::string task_filename = "tasks.txt";
195 taskFile = simout.create(name() + "." + task_filename);
196
197 for (const auto tc : system->threadContexts) {
198 uint32_t pid = tc->getCpuPtr()->getPid();
199 if (pid != BaseCPU::invldPid) {
200 mapPid(tc, pid);
201 tc->getCpuPtr()->taskId(taskMap[pid]);
202 }
203 }
204 }
205
206 const std::string dmesg_output = name() + ".dmesg";
207 if (params()->panic_on_panic) {
208 kernelPanic = addKernelFuncEventOrPanic<Linux::KernelPanic>(
209 "panic", "Kernel panic in simulated kernel", dmesg_output);
210 } else {
211 kernelPanic = addKernelFuncEventOrPanic<Linux::DmesgDump>(
212 "panic", "Kernel panic in simulated kernel", dmesg_output);
213 }
214
215 if (params()->panic_on_oops) {
216 kernelOops = addKernelFuncEventOrPanic<Linux::KernelPanic>(
217 "oops_exit", "Kernel oops in guest", dmesg_output);
218 } else {
219 kernelOops = addKernelFuncEventOrPanic<Linux::DmesgDump>(
220 "oops_exit", "Kernel oops in guest", dmesg_output);
221 }
222
223 // With ARM udelay() is #defined to __udelay
224 // newer kernels use __loop_udelay and __loop_const_udelay symbols
225 skipUDelay = addKernelFuncEvent<SkipUDelay<SkipFunc>>(
226 "__loop_udelay", "__udelay", 1000, 0);
227 if (!skipUDelay)
228 skipUDelay = addKernelFuncEventOrPanic<SkipUDelay<SkipFunc>>(
229 "__udelay", "__udelay", 1000, 0);
230
231 // constant arguments to udelay() have some precomputation done ahead of
232 // time. Constant comes from code.
233 skipConstUDelay = addKernelFuncEvent<SkipUDelay<SkipFunc>>(
234 "__loop_const_udelay", "__const_udelay", 1000, 107374);
235 if (!skipConstUDelay) {
236 skipConstUDelay = addKernelFuncEventOrPanic<SkipUDelay<SkipFunc>>(
237 "__const_udelay", "__const_udelay", 1000, 107374);
238 }
239
240 if (getArch() == Loader::Arm64) {
241 debugPrintk = addKernelFuncEvent<
242 DebugPrintk<SkipFuncLinux64>>("dprintk");
243 } else {
244 debugPrintk = addKernelFuncEvent<
245 DebugPrintk<SkipFuncLinux32>>("dprintk");
246 }
247 }
248
249 void
250 FsLinux::mapPid(ThreadContext *tc, uint32_t pid)
251 {
252 // Create a new unique identifier for this pid
253 std::map<uint32_t, uint32_t>::iterator itr = taskMap.find(pid);
254 if (itr == taskMap.end()) {
255 uint32_t map_size = taskMap.size();
256 if (map_size > ContextSwitchTaskId::MaxNormalTaskId + 1) {
257 warn_once("Error out of identifiers for cache occupancy stats");
258 taskMap[pid] = ContextSwitchTaskId::Unknown;
259 } else {
260 taskMap[pid] = map_size;
261 }
262 }
263 }
264
265 void
266 FsLinux::dumpDmesg()
267 {
268 Linux::dumpDmesg(system->getThreadContext(0), std::cout);
269 }
270
271 /**
272 * Extracts the information used by the DumpStatsPCEvent by reading the
273 * thread_info pointer passed to __switch_to() in 32 bit ARM Linux
274 *
275 * r0 = task_struct of the previously running process
276 * r1 = thread_info of the previously running process
277 * r2 = thread_info of the next process to run
278 */
279 void
280 DumpStats::getTaskDetails(ThreadContext *tc, uint32_t &pid,
281 uint32_t &tgid, std::string &next_task_str, int32_t &mm) {
282
283 Linux::ThreadInfo ti(tc);
284 Addr task_descriptor = tc->readIntReg(2);
285 pid = ti.curTaskPID(task_descriptor);
286 tgid = ti.curTaskTGID(task_descriptor);
287 next_task_str = ti.curTaskName(task_descriptor);
288
289 // Streamline treats pid == -1 as the kernel process.
290 // Also pid == 0 implies idle process (except during Linux boot)
291 mm = ti.curTaskMm(task_descriptor);
292 }
293
294 /**
295 * Extracts the information used by the DumpStatsPCEvent64 by reading the
296 * task_struct pointer passed to __switch_to() in 64 bit ARM Linux
297 *
298 * r0 = task_struct of the previously running process
299 * r1 = task_struct of next process to run
300 */
301 void
302 DumpStats64::getTaskDetails(ThreadContext *tc, uint32_t &pid,
303 uint32_t &tgid, std::string &next_task_str, int32_t &mm) {
304
305 Linux::ThreadInfo ti(tc);
306 Addr task_struct = tc->readIntReg(1);
307 pid = ti.curTaskPIDFromTaskStruct(task_struct);
308 tgid = ti.curTaskTGIDFromTaskStruct(task_struct);
309 next_task_str = ti.curTaskNameFromTaskStruct(task_struct);
310
311 // Streamline treats pid == -1 as the kernel process.
312 // Also pid == 0 implies idle process (except during Linux boot)
313 mm = ti.curTaskMmFromTaskStruct(task_struct);
314 }
315
316 /** This function is called whenever the the kernel function
317 * "__switch_to" is called to change running tasks.
318 */
319 void
320 DumpStats::process(ThreadContext *tc)
321 {
322 uint32_t pid = 0;
323 uint32_t tgid = 0;
324 std::string next_task_str;
325 int32_t mm = 0;
326
327 getTaskDetails(tc, pid, tgid, next_task_str, mm);
328
329 bool is_kernel = (mm == 0);
330 if (is_kernel && (pid != 0)) {
331 pid = -1;
332 tgid = -1;
333 next_task_str = "kernel";
334 }
335
336 FsLinux* wl = dynamic_cast<FsLinux *>(tc->getSystemPtr()->workload);
337 panic_if(!wl, "System workload is not ARM Linux!");
338 std::map<uint32_t, uint32_t>& taskMap = wl->taskMap;
339
340 // Create a new unique identifier for this pid
341 wl->mapPid(tc, pid);
342
343 // Set cpu task id, output process info, and dump stats
344 tc->getCpuPtr()->taskId(taskMap[pid]);
345 tc->getCpuPtr()->setPid(pid);
346
347 OutputStream* taskFile = wl->taskFile;
348
349 // Task file is read by cache occupancy plotting script or
350 // Streamline conversion script.
351 ccprintf(*(taskFile->stream()),
352 "tick=%lld %d cpu_id=%d next_pid=%d next_tgid=%d next_task=%s\n",
353 curTick(), taskMap[pid], tc->cpuId(), (int)pid, (int)tgid,
354 next_task_str);
355 taskFile->stream()->flush();
356
357 // Dump and reset statistics
358 Stats::schedStatEvent(true, true, curTick(), 0);
359 }
360
361 } // namespace ArmISA
362
363 FsLinux *
364 ArmFsLinuxParams::create()
365 {
366 return new FsLinux(this);
367 }