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