process.cc revision 14014:ce216ee5d886
1/* 2 * Copyright (c) 2014-2016 Advanced Micro Devices, Inc. 3 * Copyright (c) 2012 ARM Limited 4 * All rights reserved 5 * 6 * The license below extends only to copyright in the software and shall 7 * not be construed as granting a license to any other intellectual 8 * property including but not limited to intellectual property relating 9 * to a hardware implementation of the functionality of the software 10 * licensed hereunder. You may use the software subject to the license 11 * terms below provided that you ensure that this notice is replicated 12 * unmodified and in its entirety in all distributions of the software, 13 * modified or unmodified, in source code or in binary form. 14 * 15 * Copyright (c) 2001-2005 The Regents of The University of Michigan 16 * All rights reserved. 17 * 18 * Redistribution and use in source and binary forms, with or without 19 * modification, are permitted provided that the following conditions are 20 * met: redistributions of source code must retain the above copyright 21 * notice, this list of conditions and the following disclaimer; 22 * redistributions in binary form must reproduce the above copyright 23 * notice, this list of conditions and the following disclaimer in the 24 * documentation and/or other materials provided with the distribution; 25 * neither the name of the copyright holders nor the names of its 26 * contributors may be used to endorse or promote products derived from 27 * this software without specific prior written permission. 28 * 29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 33 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 34 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 39 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 40 * 41 * Authors: Nathan Binkert 42 * Steve Reinhardt 43 * Ali Saidi 44 * Brandon Potter 45 */ 46 47#include "sim/process.hh" 48 49#include <fcntl.h> 50#include <unistd.h> 51 52#include <array> 53#include <climits> 54#include <csignal> 55#include <map> 56#include <string> 57#include <vector> 58 59#include "base/intmath.hh" 60#include "base/loader/object_file.hh" 61#include "base/loader/symtab.hh" 62#include "base/statistics.hh" 63#include "config/the_isa.hh" 64#include "cpu/thread_context.hh" 65#include "mem/page_table.hh" 66#include "mem/se_translating_port_proxy.hh" 67#include "params/Process.hh" 68#include "sim/emul_driver.hh" 69#include "sim/fd_array.hh" 70#include "sim/fd_entry.hh" 71#include "sim/redirect_path.hh" 72#include "sim/syscall_desc.hh" 73#include "sim/system.hh" 74 75using namespace std; 76using namespace TheISA; 77 78static std::string 79normalize(std::string& directory) 80{ 81 if (directory.back() != '/') 82 directory += '/'; 83 return directory; 84} 85 86Process::Process(ProcessParams *params, EmulationPageTable *pTable, 87 ObjectFile *obj_file) 88 : SimObject(params), system(params->system), 89 useArchPT(params->useArchPT), 90 kvmInSE(params->kvmInSE), 91 useForClone(false), 92 pTable(pTable), 93 initVirtMem(system->getSystemPort(), this, 94 SETranslatingPortProxy::Always), 95 objFile(obj_file), 96 argv(params->cmd), envp(params->env), 97 executable(params->executable), 98 tgtCwd(normalize(params->cwd)), 99 hostCwd(checkPathRedirect(tgtCwd)), 100 release(params->release), 101 _uid(params->uid), _euid(params->euid), 102 _gid(params->gid), _egid(params->egid), 103 _pid(params->pid), _ppid(params->ppid), 104 _pgid(params->pgid), drivers(params->drivers), 105 fds(make_shared<FDArray>(params->input, params->output, params->errout)), 106 childClearTID(0) 107{ 108 if (_pid >= System::maxPID) 109 fatal("_pid is too large: %d", _pid); 110 111 auto ret_pair = system->PIDs.emplace(_pid); 112 if (!ret_pair.second) 113 fatal("_pid %d is already used", _pid); 114 115 /** 116 * Linux bundles together processes into this concept called a thread 117 * group. The thread group is responsible for recording which processes 118 * behave as threads within a process context. The thread group leader 119 * is the process who's tgid is equal to its pid. Other processes which 120 * belong to the thread group, but do not lead the thread group, are 121 * treated as child threads. These threads are created by the clone system 122 * call with options specified to create threads (differing from the 123 * options used to implement a fork). By default, set up the tgid/pid 124 * with a new, equivalent value. If CLONE_THREAD is specified, patch 125 * the tgid value with the old process' value. 126 */ 127 _tgid = params->pid; 128 129 exitGroup = new bool(); 130 sigchld = new bool(); 131 132 if (!debugSymbolTable) { 133 debugSymbolTable = new SymbolTable(); 134 if (!objFile->loadGlobalSymbols(debugSymbolTable) || 135 !objFile->loadLocalSymbols(debugSymbolTable) || 136 !objFile->loadWeakSymbols(debugSymbolTable)) { 137 delete debugSymbolTable; 138 debugSymbolTable = nullptr; 139 } 140 } 141} 142 143void 144Process::clone(ThreadContext *otc, ThreadContext *ntc, 145 Process *np, RegVal flags) 146{ 147#ifndef CLONE_VM 148#define CLONE_VM 0 149#endif 150#ifndef CLONE_FILES 151#define CLONE_FILES 0 152#endif 153#ifndef CLONE_THREAD 154#define CLONE_THREAD 0 155#endif 156 if (CLONE_VM & flags) { 157 /** 158 * Share the process memory address space between the new process 159 * and the old process. Changes in one will be visible in the other 160 * due to the pointer use. 161 */ 162 delete np->pTable; 163 np->pTable = pTable; 164 ntc->getMemProxy().setPageTable(np->pTable); 165 166 np->memState = memState; 167 } else { 168 /** 169 * Duplicate the process memory address space. The state needs to be 170 * copied over (rather than using pointers to share everything). 171 */ 172 typedef std::vector<pair<Addr,Addr>> MapVec; 173 MapVec mappings; 174 pTable->getMappings(&mappings); 175 176 for (auto map : mappings) { 177 Addr paddr, vaddr = map.first; 178 bool alloc_page = !(np->pTable->translate(vaddr, paddr)); 179 np->replicatePage(vaddr, paddr, otc, ntc, alloc_page); 180 } 181 182 *np->memState = *memState; 183 } 184 185 if (CLONE_FILES & flags) { 186 /** 187 * The parent and child file descriptors are shared because the 188 * two FDArray pointers are pointing to the same FDArray. Opening 189 * and closing file descriptors will be visible to both processes. 190 */ 191 np->fds = fds; 192 } else { 193 /** 194 * Copy the file descriptors from the old process into the new 195 * child process. The file descriptors entry can be opened and 196 * closed independently of the other process being considered. The 197 * host file descriptors are also dup'd so that the flags for the 198 * host file descriptor is independent of the other process. 199 */ 200 for (int tgt_fd = 0; tgt_fd < fds->getSize(); tgt_fd++) { 201 std::shared_ptr<FDArray> nfds = np->fds; 202 std::shared_ptr<FDEntry> this_fde = (*fds)[tgt_fd]; 203 if (!this_fde) { 204 nfds->setFDEntry(tgt_fd, nullptr); 205 continue; 206 } 207 nfds->setFDEntry(tgt_fd, this_fde->clone()); 208 209 auto this_hbfd = std::dynamic_pointer_cast<HBFDEntry>(this_fde); 210 if (!this_hbfd) 211 continue; 212 213 int this_sim_fd = this_hbfd->getSimFD(); 214 if (this_sim_fd <= 2) 215 continue; 216 217 int np_sim_fd = dup(this_sim_fd); 218 assert(np_sim_fd != -1); 219 220 auto nhbfd = std::dynamic_pointer_cast<HBFDEntry>((*nfds)[tgt_fd]); 221 nhbfd->setSimFD(np_sim_fd); 222 } 223 } 224 225 if (CLONE_THREAD & flags) { 226 np->_tgid = _tgid; 227 delete np->exitGroup; 228 np->exitGroup = exitGroup; 229 } 230 231 np->argv.insert(np->argv.end(), argv.begin(), argv.end()); 232 np->envp.insert(np->envp.end(), envp.begin(), envp.end()); 233} 234 235void 236Process::regStats() 237{ 238 SimObject::regStats(); 239 240 using namespace Stats; 241 242 numSyscalls 243 .name(name() + ".numSyscalls") 244 .desc("Number of system calls") 245 ; 246} 247 248ThreadContext * 249Process::findFreeContext() 250{ 251 for (auto &it : system->threadContexts) { 252 if (ThreadContext::Halted == it->status()) 253 return it; 254 } 255 return nullptr; 256} 257 258void 259Process::revokeThreadContext(int context_id) 260{ 261 std::vector<ContextID>::iterator it; 262 for (it = contextIds.begin(); it != contextIds.end(); it++) { 263 if (*it == context_id) { 264 contextIds.erase(it); 265 return; 266 } 267 } 268 warn("Unable to find thread context to revoke"); 269} 270 271void 272Process::initState() 273{ 274 if (contextIds.empty()) 275 fatal("Process %s is not associated with any HW contexts!\n", name()); 276 277 // first thread context for this process... initialize & enable 278 ThreadContext *tc = system->getThreadContext(contextIds[0]); 279 280 // mark this context as active so it will start ticking. 281 tc->activate(); 282 283 pTable->initState(tc); 284} 285 286DrainState 287Process::drain() 288{ 289 fds->updateFileOffsets(); 290 return DrainState::Drained; 291} 292 293void 294Process::allocateMem(Addr vaddr, int64_t size, bool clobber) 295{ 296 int npages = divCeil(size, (int64_t)PageBytes); 297 Addr paddr = system->allocPhysPages(npages); 298 pTable->map(vaddr, paddr, size, 299 clobber ? EmulationPageTable::Clobber : 300 EmulationPageTable::MappingFlags(0)); 301} 302 303void 304Process::replicatePage(Addr vaddr, Addr new_paddr, ThreadContext *old_tc, 305 ThreadContext *new_tc, bool allocate_page) 306{ 307 if (allocate_page) 308 new_paddr = system->allocPhysPages(1); 309 310 // Read from old physical page. 311 uint8_t *buf_p = new uint8_t[PageBytes]; 312 old_tc->getMemProxy().readBlob(vaddr, buf_p, PageBytes); 313 314 // Create new mapping in process address space by clobbering existing 315 // mapping (if any existed) and then write to the new physical page. 316 bool clobber = true; 317 pTable->map(vaddr, new_paddr, PageBytes, clobber); 318 new_tc->getMemProxy().writeBlob(vaddr, buf_p, PageBytes); 319 delete[] buf_p; 320} 321 322bool 323Process::fixupStackFault(Addr vaddr) 324{ 325 Addr stack_min = memState->getStackMin(); 326 Addr stack_base = memState->getStackBase(); 327 Addr max_stack_size = memState->getMaxStackSize(); 328 329 // Check if this is already on the stack and there's just no page there 330 // yet. 331 if (vaddr >= stack_min && vaddr < stack_base) { 332 allocateMem(roundDown(vaddr, PageBytes), PageBytes); 333 return true; 334 } 335 336 // We've accessed the next page of the stack, so extend it to include 337 // this address. 338 if (vaddr < stack_min && vaddr >= stack_base - max_stack_size) { 339 while (vaddr < stack_min) { 340 stack_min -= TheISA::PageBytes; 341 if (stack_base - stack_min > max_stack_size) 342 fatal("Maximum stack size exceeded\n"); 343 allocateMem(stack_min, TheISA::PageBytes); 344 inform("Increasing stack size by one page."); 345 } 346 memState->setStackMin(stack_min); 347 return true; 348 } 349 return false; 350} 351 352void 353Process::serialize(CheckpointOut &cp) const 354{ 355 memState->serialize(cp); 356 pTable->serialize(cp); 357 /** 358 * Checkpoints for file descriptors currently do not work. Need to 359 * come back and fix them at a later date. 360 */ 361 362 warn("Checkpoints for file descriptors currently do not work."); 363} 364 365void 366Process::unserialize(CheckpointIn &cp) 367{ 368 memState->unserialize(cp); 369 pTable->unserialize(cp); 370 /** 371 * Checkpoints for file descriptors currently do not work. Need to 372 * come back and fix them at a later date. 373 */ 374 warn("Checkpoints for file descriptors currently do not work."); 375 // The above returns a bool so that you could do something if you don't 376 // find the param in the checkpoint if you wanted to, like set a default 377 // but in this case we'll just stick with the instantiated value if not 378 // found. 379} 380 381bool 382Process::map(Addr vaddr, Addr paddr, int size, bool cacheable) 383{ 384 pTable->map(vaddr, paddr, size, 385 cacheable ? EmulationPageTable::MappingFlags(0) : 386 EmulationPageTable::Uncacheable); 387 return true; 388} 389 390void 391Process::syscall(int64_t callnum, ThreadContext *tc, Fault *fault) 392{ 393 numSyscalls++; 394 395 SyscallDesc *desc = getDesc(callnum); 396 if (desc == nullptr) 397 fatal("Syscall %d out of range", callnum); 398 399 desc->doSyscall(callnum, tc, fault); 400} 401 402RegVal 403Process::getSyscallArg(ThreadContext *tc, int &i, int width) 404{ 405 return getSyscallArg(tc, i); 406} 407 408EmulatedDriver * 409Process::findDriver(std::string filename) 410{ 411 for (EmulatedDriver *d : drivers) { 412 if (d->match(filename)) 413 return d; 414 } 415 416 return nullptr; 417} 418 419std::string 420Process::checkPathRedirect(const std::string &filename) 421{ 422 // If the input parameter contains a relative path, convert it. 423 // The target version of the current working directory is fine since 424 // we immediately convert it using redirect paths into a host version. 425 auto abs_path = absolutePath(filename, false); 426 427 for (auto path : system->redirectPaths) { 428 // Search through the redirect paths to see if a starting substring of 429 // our path falls into any buckets which need to redirected. 430 if (startswith(abs_path, path->appPath())) { 431 std::string tail = abs_path.substr(path->appPath().size()); 432 433 // If this path needs to be redirected, search through a list 434 // of targets to see if we can match a valid file (or directory). 435 for (auto host_path : path->hostPaths()) { 436 if (access((host_path + tail).c_str(), R_OK) == 0) { 437 // Return the valid match. 438 return host_path + tail; 439 } 440 } 441 // The path needs to be redirected, but the file or directory 442 // does not exist on the host filesystem. Return the first 443 // host path as a default. 444 return path->hostPaths()[0] + tail; 445 } 446 } 447 448 // The path does not need to be redirected. 449 return abs_path; 450} 451 452void 453Process::updateBias() 454{ 455 ObjectFile *interp = objFile->getInterpreter(); 456 457 if (!interp || !interp->relocatable()) 458 return; 459 460 // Determine how large the interpreters footprint will be in the process 461 // address space. 462 Addr interp_mapsize = roundUp(interp->mapSize(), TheISA::PageBytes); 463 464 // We are allocating the memory area; set the bias to the lowest address 465 // in the allocated memory region. 466 Addr mmap_end = memState->getMmapEnd(); 467 Addr ld_bias = mmapGrowsDown() ? mmap_end - interp_mapsize : mmap_end; 468 469 // Adjust the process mmap area to give the interpreter room; the real 470 // execve system call would just invoke the kernel's internal mmap 471 // functions to make these adjustments. 472 mmap_end = mmapGrowsDown() ? ld_bias : mmap_end + interp_mapsize; 473 memState->setMmapEnd(mmap_end); 474 475 interp->updateBias(ld_bias); 476} 477 478ObjectFile * 479Process::getInterpreter() 480{ 481 return objFile->getInterpreter(); 482} 483 484Addr 485Process::getBias() 486{ 487 ObjectFile *interp = getInterpreter(); 488 489 return interp ? interp->bias() : objFile->bias(); 490} 491 492Addr 493Process::getStartPC() 494{ 495 ObjectFile *interp = getInterpreter(); 496 497 return interp ? interp->entryPoint() : objFile->entryPoint(); 498} 499 500std::string 501Process::absolutePath(const std::string &filename, bool host_filesystem) 502{ 503 if (filename.empty() || startswith(filename, "/")) 504 return filename; 505 506 // Construct the absolute path given the current working directory for 507 // either the host filesystem or target filesystem. The distinction only 508 // matters if filesystem redirection is utilized in the simulation. 509 auto path_base = std::string(); 510 if (host_filesystem) { 511 path_base = hostCwd; 512 assert(!hostCwd.empty()); 513 } else { 514 path_base = tgtCwd; 515 assert(!tgtCwd.empty()); 516 } 517 518 // Add a trailing '/' if the current working directory did not have one. 519 normalize(path_base); 520 521 // Append the filename onto the current working path. 522 auto absolute_path = path_base + filename; 523 524 return absolute_path; 525} 526 527Process * 528ProcessParams::create() 529{ 530 Process *process = nullptr; 531 532 // If not specified, set the executable parameter equal to the 533 // simulated system's zeroth command line parameter 534 if (executable == "") { 535 executable = cmd[0]; 536 } 537 538 ObjectFile *obj_file = createObjectFile(executable); 539 fatal_if(!obj_file, "Can't load object file %s", executable); 540 541 process = ObjectFile::tryLoaders(this, obj_file); 542 fatal_if(!process, "Unknown error creating process object."); 543 544 return process; 545} 546