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