syscall_emul.cc revision 12716:430023c6881b
1/* 2 * Copyright (c) 2003-2005 The Regents of The University of Michigan 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer; 9 * redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution; 12 * neither the name of the copyright holders nor the names of its 13 * contributors may be used to endorse or promote products derived from 14 * this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * 28 * Authors: Steve Reinhardt 29 * Ali Saidi 30 */ 31 32#include "sim/syscall_emul.hh" 33 34#include <fcntl.h> 35#include <unistd.h> 36 37#include <csignal> 38#include <iostream> 39#include <string> 40 41#include "arch/utility.hh" 42#include "base/chunk_generator.hh" 43#include "base/trace.hh" 44#include "config/the_isa.hh" 45#include "cpu/thread_context.hh" 46#include "dev/net/dist_iface.hh" 47#include "mem/page_table.hh" 48#include "sim/process.hh" 49#include "sim/sim_exit.hh" 50#include "sim/syscall_debug_macros.hh" 51#include "sim/syscall_desc.hh" 52#include "sim/system.hh" 53 54using namespace std; 55using namespace TheISA; 56 57SyscallReturn 58unimplementedFunc(SyscallDesc *desc, int callnum, Process *process, 59 ThreadContext *tc) 60{ 61 fatal("syscall %s (#%d) unimplemented.", desc->name(), callnum); 62 63 return 1; 64} 65 66 67SyscallReturn 68ignoreFunc(SyscallDesc *desc, int callnum, Process *process, 69 ThreadContext *tc) 70{ 71 if (desc->needWarning()) { 72 warn("ignoring syscall %s(...)%s", desc->name(), desc->warnOnce() ? 73 "\n (further warnings will be suppressed)" : ""); 74 } 75 76 return 0; 77} 78 79static void 80exitFutexWake(ThreadContext *tc, Addr addr, uint64_t tgid) 81{ 82 // Clear value at address pointed to by thread's childClearTID field. 83 BufferArg ctidBuf(addr, sizeof(long)); 84 long *ctid = (long *)ctidBuf.bufferPtr(); 85 *ctid = 0; 86 ctidBuf.copyOut(tc->getMemProxy()); 87 88 FutexMap &futex_map = tc->getSystemPtr()->futexMap; 89 // Wake one of the waiting threads. 90 futex_map.wakeup(addr, tgid, 1); 91} 92 93static SyscallReturn 94exitImpl(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc, 95 bool group) 96{ 97 int index = 0; 98 int status = p->getSyscallArg(tc, index); 99 100 System *sys = tc->getSystemPtr(); 101 102 int activeContexts = 0; 103 for (auto &system: sys->systemList) 104 activeContexts += system->numRunningContexts(); 105 if (activeContexts == 1) { 106 /** 107 * Even though we are terminating the final thread context, dist-gem5 108 * requires the simulation to remain active and provide 109 * synchronization messages to the switch process. So we just halt 110 * the last thread context and return. The simulation will be 111 * terminated by dist-gem5 in a coordinated manner once all nodes 112 * have signaled their readiness to exit. For non dist-gem5 113 * simulations, readyToExit() always returns true. 114 */ 115 if (!DistIface::readyToExit(0)) { 116 tc->halt(); 117 return status; 118 } 119 120 exitSimLoop("exiting with last active thread context", status & 0xff); 121 return status; 122 } 123 124 if (group) 125 *p->exitGroup = true; 126 127 if (p->childClearTID) 128 exitFutexWake(tc, p->childClearTID, p->tgid()); 129 130 bool last_thread = true; 131 Process *parent = nullptr, *tg_lead = nullptr; 132 for (int i = 0; last_thread && i < sys->numContexts(); i++) { 133 Process *walk; 134 if (!(walk = sys->threadContexts[i]->getProcessPtr())) 135 continue; 136 137 /** 138 * Threads in a thread group require special handing. For instance, 139 * we send the SIGCHLD signal so that it appears that it came from 140 * the head of the group. We also only delete file descriptors if 141 * we are the last thread in the thread group. 142 */ 143 if (walk->pid() == p->tgid()) 144 tg_lead = walk; 145 146 if ((sys->threadContexts[i]->status() != ThreadContext::Halted) 147 && (walk != p)) { 148 /** 149 * Check if we share thread group with the pointer; this denotes 150 * that we are not the last thread active in the thread group. 151 * Note that setting this to false also prevents further 152 * iterations of the loop. 153 */ 154 if (walk->tgid() == p->tgid()) 155 last_thread = false; 156 157 /** 158 * A corner case exists which involves execve(). After execve(), 159 * the execve will enable SIGCHLD in the process. The problem 160 * occurs when the exiting process is the root process in the 161 * system; there is no parent to receive the signal. We obviate 162 * this problem by setting the root process' ppid to zero in the 163 * Python configuration files. We really should handle the 164 * root/execve specific case more gracefully. 165 */ 166 if (*p->sigchld && (p->ppid() != 0) && (walk->pid() == p->ppid())) 167 parent = walk; 168 } 169 } 170 171 if (last_thread) { 172 if (parent) { 173 assert(tg_lead); 174 sys->signalList.push_back(BasicSignal(tg_lead, parent, SIGCHLD)); 175 } 176 177 /** 178 * Run though FD array of the exiting process and close all file 179 * descriptors except for the standard file descriptors. 180 * (The standard file descriptors are shared with gem5.) 181 */ 182 for (int i = 0; i < p->fds->getSize(); i++) { 183 if ((*p->fds)[i]) 184 p->fds->closeFDEntry(i); 185 } 186 } 187 188 tc->halt(); 189 return status; 190} 191 192SyscallReturn 193exitFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 194{ 195 return exitImpl(desc, callnum, p, tc, false); 196} 197 198SyscallReturn 199exitGroupFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 200{ 201 return exitImpl(desc, callnum, p, tc, true); 202} 203 204SyscallReturn 205getpagesizeFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 206{ 207 return (int)PageBytes; 208} 209 210 211SyscallReturn 212brkFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 213{ 214 // change brk addr to first arg 215 int index = 0; 216 Addr new_brk = p->getSyscallArg(tc, index); 217 218 std::shared_ptr<MemState> mem_state = p->memState; 219 Addr brk_point = mem_state->getBrkPoint(); 220 221 // in Linux at least, brk(0) returns the current break value 222 // (note that the syscall and the glibc function have different behavior) 223 if (new_brk == 0) 224 return brk_point; 225 226 if (new_brk > brk_point) { 227 // might need to allocate some new pages 228 for (ChunkGenerator gen(brk_point, 229 new_brk - brk_point, 230 PageBytes); !gen.done(); gen.next()) { 231 if (!p->pTable->translate(gen.addr())) 232 p->allocateMem(roundDown(gen.addr(), PageBytes), PageBytes); 233 234 // if the address is already there, zero it out 235 else { 236 uint8_t zero = 0; 237 SETranslatingPortProxy &tp = tc->getMemProxy(); 238 239 // split non-page aligned accesses 240 Addr next_page = roundUp(gen.addr(), PageBytes); 241 uint32_t size_needed = next_page - gen.addr(); 242 tp.memsetBlob(gen.addr(), zero, size_needed); 243 if (gen.addr() + PageBytes > next_page && 244 next_page < new_brk && 245 p->pTable->translate(next_page)) { 246 size_needed = PageBytes - size_needed; 247 tp.memsetBlob(next_page, zero, size_needed); 248 } 249 } 250 } 251 } 252 253 mem_state->setBrkPoint(new_brk); 254 DPRINTF_SYSCALL(Verbose, "brk: break point changed to: %#X\n", 255 mem_state->getBrkPoint()); 256 return mem_state->getBrkPoint(); 257} 258 259SyscallReturn 260setTidAddressFunc(SyscallDesc *desc, int callnum, Process *process, 261 ThreadContext *tc) 262{ 263 int index = 0; 264 uint64_t tidPtr = process->getSyscallArg(tc, index); 265 266 process->childClearTID = tidPtr; 267 return process->pid(); 268} 269 270SyscallReturn 271closeFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 272{ 273 int index = 0; 274 int tgt_fd = p->getSyscallArg(tc, index); 275 276 return p->fds->closeFDEntry(tgt_fd); 277} 278 279 280SyscallReturn 281readFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 282{ 283 int index = 0; 284 int tgt_fd = p->getSyscallArg(tc, index); 285 Addr buf_ptr = p->getSyscallArg(tc, index); 286 int nbytes = p->getSyscallArg(tc, index); 287 288 auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]); 289 if (!hbfdp) 290 return -EBADF; 291 int sim_fd = hbfdp->getSimFD(); 292 293 BufferArg bufArg(buf_ptr, nbytes); 294 int bytes_read = read(sim_fd, bufArg.bufferPtr(), nbytes); 295 296 if (bytes_read > 0) 297 bufArg.copyOut(tc->getMemProxy()); 298 299 return bytes_read; 300} 301 302SyscallReturn 303writeFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 304{ 305 int index = 0; 306 int tgt_fd = p->getSyscallArg(tc, index); 307 Addr buf_ptr = p->getSyscallArg(tc, index); 308 int nbytes = p->getSyscallArg(tc, index); 309 310 auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]); 311 if (!hbfdp) 312 return -EBADF; 313 int sim_fd = hbfdp->getSimFD(); 314 315 BufferArg bufArg(buf_ptr, nbytes); 316 bufArg.copyIn(tc->getMemProxy()); 317 318 int bytes_written = write(sim_fd, bufArg.bufferPtr(), nbytes); 319 320 fsync(sim_fd); 321 322 return bytes_written; 323} 324 325 326SyscallReturn 327lseekFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 328{ 329 int index = 0; 330 int tgt_fd = p->getSyscallArg(tc, index); 331 uint64_t offs = p->getSyscallArg(tc, index); 332 int whence = p->getSyscallArg(tc, index); 333 334 auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 335 if (!ffdp) 336 return -EBADF; 337 int sim_fd = ffdp->getSimFD(); 338 339 off_t result = lseek(sim_fd, offs, whence); 340 341 return (result == (off_t)-1) ? -errno : result; 342} 343 344 345SyscallReturn 346_llseekFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 347{ 348 int index = 0; 349 int tgt_fd = p->getSyscallArg(tc, index); 350 uint64_t offset_high = p->getSyscallArg(tc, index); 351 uint32_t offset_low = p->getSyscallArg(tc, index); 352 Addr result_ptr = p->getSyscallArg(tc, index); 353 int whence = p->getSyscallArg(tc, index); 354 355 auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 356 if (!ffdp) 357 return -EBADF; 358 int sim_fd = ffdp->getSimFD(); 359 360 uint64_t offset = (offset_high << 32) | offset_low; 361 362 uint64_t result = lseek(sim_fd, offset, whence); 363 result = TheISA::htog(result); 364 365 if (result == (off_t)-1) 366 return -errno; 367 // Assuming that the size of loff_t is 64 bits on the target platform 368 BufferArg result_buf(result_ptr, sizeof(result)); 369 memcpy(result_buf.bufferPtr(), &result, sizeof(result)); 370 result_buf.copyOut(tc->getMemProxy()); 371 return 0; 372} 373 374 375SyscallReturn 376munmapFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 377{ 378 // With mmap more fully implemented, it might be worthwhile to bite 379 // the bullet and implement munmap. Should allow us to reuse simulated 380 // memory. 381 return 0; 382} 383 384 385const char *hostname = "m5.eecs.umich.edu"; 386 387SyscallReturn 388gethostnameFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 389{ 390 int index = 0; 391 Addr buf_ptr = p->getSyscallArg(tc, index); 392 int name_len = p->getSyscallArg(tc, index); 393 BufferArg name(buf_ptr, name_len); 394 395 strncpy((char *)name.bufferPtr(), hostname, name_len); 396 397 name.copyOut(tc->getMemProxy()); 398 399 return 0; 400} 401 402SyscallReturn 403getcwdFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 404{ 405 int result = 0; 406 int index = 0; 407 Addr buf_ptr = p->getSyscallArg(tc, index); 408 unsigned long size = p->getSyscallArg(tc, index); 409 BufferArg buf(buf_ptr, size); 410 411 // Is current working directory defined? 412 string cwd = p->getcwd(); 413 if (!cwd.empty()) { 414 if (cwd.length() >= size) { 415 // Buffer too small 416 return -ERANGE; 417 } 418 strncpy((char *)buf.bufferPtr(), cwd.c_str(), size); 419 result = cwd.length(); 420 } else { 421 if (getcwd((char *)buf.bufferPtr(), size)) { 422 result = strlen((char *)buf.bufferPtr()); 423 } else { 424 result = -1; 425 } 426 } 427 428 buf.copyOut(tc->getMemProxy()); 429 430 return (result == -1) ? -errno : result; 431} 432 433SyscallReturn 434readlinkFunc(SyscallDesc *desc, int callnum, Process *process, 435 ThreadContext *tc) 436{ 437 return readlinkFunc(desc, callnum, process, tc, 0); 438} 439 440SyscallReturn 441readlinkFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc, 442 int index) 443{ 444 string path; 445 446 if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index))) 447 return -EFAULT; 448 449 // Adjust path for current working directory 450 path = p->fullPath(path); 451 452 Addr buf_ptr = p->getSyscallArg(tc, index); 453 size_t bufsiz = p->getSyscallArg(tc, index); 454 455 BufferArg buf(buf_ptr, bufsiz); 456 457 int result = -1; 458 if (path != "/proc/self/exe") { 459 result = readlink(path.c_str(), (char *)buf.bufferPtr(), bufsiz); 460 } else { 461 // Emulate readlink() called on '/proc/self/exe' should return the 462 // absolute path of the binary running in the simulated system (the 463 // Process' executable). It is possible that using this path in 464 // the simulated system will result in unexpected behavior if: 465 // 1) One binary runs another (e.g., -c time -o "my_binary"), and 466 // called binary calls readlink(). 467 // 2) The host's full path to the running benchmark changes from one 468 // simulation to another. This can result in different simulated 469 // performance since the simulated system will process the binary 470 // path differently, even if the binary itself does not change. 471 472 // Get the absolute canonical path to the running application 473 char real_path[PATH_MAX]; 474 char *check_real_path = realpath(p->progName(), real_path); 475 if (!check_real_path) { 476 fatal("readlink('/proc/self/exe') unable to resolve path to " 477 "executable: %s", p->progName()); 478 } 479 strncpy((char*)buf.bufferPtr(), real_path, bufsiz); 480 size_t real_path_len = strlen(real_path); 481 if (real_path_len > bufsiz) { 482 // readlink will truncate the contents of the 483 // path to ensure it is no more than bufsiz 484 result = bufsiz; 485 } else { 486 result = real_path_len; 487 } 488 489 // Issue a warning about potential unexpected results 490 warn_once("readlink() called on '/proc/self/exe' may yield unexpected " 491 "results in various settings.\n Returning '%s'\n", 492 (char*)buf.bufferPtr()); 493 } 494 495 buf.copyOut(tc->getMemProxy()); 496 497 return (result == -1) ? -errno : result; 498} 499 500SyscallReturn 501unlinkFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 502{ 503 return unlinkHelper(desc, num, p, tc, 0); 504} 505 506SyscallReturn 507unlinkHelper(SyscallDesc *desc, int num, Process *p, ThreadContext *tc, 508 int index) 509{ 510 string path; 511 512 if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index))) 513 return -EFAULT; 514 515 // Adjust path for current working directory 516 path = p->fullPath(path); 517 518 int result = unlink(path.c_str()); 519 return (result == -1) ? -errno : result; 520} 521 522 523SyscallReturn 524mkdirFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 525{ 526 string path; 527 528 int index = 0; 529 if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index))) 530 return -EFAULT; 531 532 // Adjust path for current working directory 533 path = p->fullPath(path); 534 535 mode_t mode = p->getSyscallArg(tc, index); 536 537 int result = mkdir(path.c_str(), mode); 538 return (result == -1) ? -errno : result; 539} 540 541SyscallReturn 542renameFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 543{ 544 string old_name; 545 546 int index = 0; 547 if (!tc->getMemProxy().tryReadString(old_name, p->getSyscallArg(tc, index))) 548 return -EFAULT; 549 550 string new_name; 551 552 if (!tc->getMemProxy().tryReadString(new_name, p->getSyscallArg(tc, index))) 553 return -EFAULT; 554 555 // Adjust path for current working directory 556 old_name = p->fullPath(old_name); 557 new_name = p->fullPath(new_name); 558 559 int64_t result = rename(old_name.c_str(), new_name.c_str()); 560 return (result == -1) ? -errno : result; 561} 562 563SyscallReturn 564truncateFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 565{ 566 string path; 567 568 int index = 0; 569 if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index))) 570 return -EFAULT; 571 572 off_t length = p->getSyscallArg(tc, index); 573 574 // Adjust path for current working directory 575 path = p->fullPath(path); 576 577 int result = truncate(path.c_str(), length); 578 return (result == -1) ? -errno : result; 579} 580 581SyscallReturn 582ftruncateFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 583{ 584 int index = 0; 585 int tgt_fd = p->getSyscallArg(tc, index); 586 off_t length = p->getSyscallArg(tc, index); 587 588 auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 589 if (!ffdp) 590 return -EBADF; 591 int sim_fd = ffdp->getSimFD(); 592 593 int result = ftruncate(sim_fd, length); 594 return (result == -1) ? -errno : result; 595} 596 597SyscallReturn 598truncate64Func(SyscallDesc *desc, int num, 599 Process *process, ThreadContext *tc) 600{ 601 int index = 0; 602 string path; 603 604 if (!tc->getMemProxy().tryReadString(path, process->getSyscallArg(tc, index))) 605 return -EFAULT; 606 607 int64_t length = process->getSyscallArg(tc, index, 64); 608 609 // Adjust path for current working directory 610 path = process->fullPath(path); 611 612#if NO_STAT64 613 int result = truncate(path.c_str(), length); 614#else 615 int result = truncate64(path.c_str(), length); 616#endif 617 return (result == -1) ? -errno : result; 618} 619 620SyscallReturn 621ftruncate64Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 622{ 623 int index = 0; 624 int tgt_fd = p->getSyscallArg(tc, index); 625 int64_t length = p->getSyscallArg(tc, index, 64); 626 627 auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 628 if (!ffdp) 629 return -EBADF; 630 int sim_fd = ffdp->getSimFD(); 631 632#if NO_STAT64 633 int result = ftruncate(sim_fd, length); 634#else 635 int result = ftruncate64(sim_fd, length); 636#endif 637 return (result == -1) ? -errno : result; 638} 639 640SyscallReturn 641umaskFunc(SyscallDesc *desc, int num, Process *process, ThreadContext *tc) 642{ 643 // Letting the simulated program change the simulator's umask seems like 644 // a bad idea. Compromise by just returning the current umask but not 645 // changing anything. 646 mode_t oldMask = umask(0); 647 umask(oldMask); 648 return (int)oldMask; 649} 650 651SyscallReturn 652chownFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 653{ 654 string path; 655 656 int index = 0; 657 if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index))) 658 return -EFAULT; 659 660 /* XXX endianess */ 661 uint32_t owner = p->getSyscallArg(tc, index); 662 uid_t hostOwner = owner; 663 uint32_t group = p->getSyscallArg(tc, index); 664 gid_t hostGroup = group; 665 666 // Adjust path for current working directory 667 path = p->fullPath(path); 668 669 int result = chown(path.c_str(), hostOwner, hostGroup); 670 return (result == -1) ? -errno : result; 671} 672 673SyscallReturn 674fchownFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 675{ 676 int index = 0; 677 int tgt_fd = p->getSyscallArg(tc, index); 678 679 auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 680 if (!ffdp) 681 return -EBADF; 682 int sim_fd = ffdp->getSimFD(); 683 684 /* XXX endianess */ 685 uint32_t owner = p->getSyscallArg(tc, index); 686 uid_t hostOwner = owner; 687 uint32_t group = p->getSyscallArg(tc, index); 688 gid_t hostGroup = group; 689 690 int result = fchown(sim_fd, hostOwner, hostGroup); 691 return (result == -1) ? -errno : result; 692} 693 694/** 695 * FIXME: The file description is not shared among file descriptors created 696 * with dup. Really, it's difficult to maintain fields like file offset or 697 * flags since an update to such a field won't be reflected in the metadata 698 * for the fd entries that we maintain for checkpoint restoration. 699 */ 700SyscallReturn 701dupFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 702{ 703 int index = 0; 704 int tgt_fd = p->getSyscallArg(tc, index); 705 706 auto old_hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]); 707 if (!old_hbfdp) 708 return -EBADF; 709 int sim_fd = old_hbfdp->getSimFD(); 710 711 int result = dup(sim_fd); 712 if (result == -1) 713 return -errno; 714 715 auto new_hbfdp = std::dynamic_pointer_cast<HBFDEntry>(old_hbfdp->clone()); 716 new_hbfdp->setSimFD(result); 717 new_hbfdp->setCOE(false); 718 return p->fds->allocFD(new_hbfdp); 719} 720 721SyscallReturn 722dup2Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 723{ 724 int index = 0; 725 726 int old_tgt_fd = p->getSyscallArg(tc, index); 727 auto old_hbp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[old_tgt_fd]); 728 if (!old_hbp) 729 return -EBADF; 730 int old_sim_fd = old_hbp->getSimFD(); 731 732 /** 733 * We need a valid host file descriptor number to be able to pass into 734 * the second parameter for dup2 (newfd), but we don't know what the 735 * viable numbers are; we execute the open call to retrieve one. 736 */ 737 int res_fd = dup2(old_sim_fd, open("/dev/null", O_RDONLY)); 738 if (res_fd == -1) 739 return -errno; 740 741 int new_tgt_fd = p->getSyscallArg(tc, index); 742 auto new_hbp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[new_tgt_fd]); 743 if (new_hbp) 744 p->fds->closeFDEntry(new_tgt_fd); 745 new_hbp = std::dynamic_pointer_cast<HBFDEntry>(old_hbp->clone()); 746 new_hbp->setSimFD(res_fd); 747 new_hbp->setCOE(false); 748 749 return p->fds->allocFD(new_hbp); 750} 751 752SyscallReturn 753fcntlFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 754{ 755 int arg; 756 int index = 0; 757 int tgt_fd = p->getSyscallArg(tc, index); 758 int cmd = p->getSyscallArg(tc, index); 759 760 auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]); 761 if (!hbfdp) 762 return -EBADF; 763 int sim_fd = hbfdp->getSimFD(); 764 765 int coe = hbfdp->getCOE(); 766 767 switch (cmd) { 768 case F_GETFD: 769 return coe & FD_CLOEXEC; 770 771 case F_SETFD: { 772 arg = p->getSyscallArg(tc, index); 773 arg ? hbfdp->setCOE(true) : hbfdp->setCOE(false); 774 return 0; 775 } 776 777 // Rely on the host to maintain the file status flags for this file 778 // description rather than maintain it ourselves. Admittedly, this 779 // is suboptimal (and possibly error prone), but it is difficult to 780 // maintain the flags by tracking them across the different descriptors 781 // (that refer to this file description) caused by clone, dup, and 782 // subsequent fcntls. 783 case F_GETFL: 784 case F_SETFL: { 785 arg = p->getSyscallArg(tc, index); 786 int rv = fcntl(sim_fd, cmd, arg); 787 return (rv == -1) ? -errno : rv; 788 } 789 790 default: 791 warn("fcntl: unsupported command %d\n", cmd); 792 return 0; 793 } 794} 795 796SyscallReturn 797fcntl64Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 798{ 799 int index = 0; 800 int tgt_fd = p->getSyscallArg(tc, index); 801 802 auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]); 803 if (!hbfdp) 804 return -EBADF; 805 int sim_fd = hbfdp->getSimFD(); 806 807 int cmd = p->getSyscallArg(tc, index); 808 switch (cmd) { 809 case 33: //F_GETLK64 810 warn("fcntl64(%d, F_GETLK64) not supported, error returned\n", tgt_fd); 811 return -EMFILE; 812 813 case 34: // F_SETLK64 814 case 35: // F_SETLKW64 815 warn("fcntl64(%d, F_SETLK(W)64) not supported, error returned\n", 816 tgt_fd); 817 return -EMFILE; 818 819 default: 820 // not sure if this is totally valid, but we'll pass it through 821 // to the underlying OS 822 warn("fcntl64(%d, %d) passed through to host\n", tgt_fd, cmd); 823 return fcntl(sim_fd, cmd); 824 } 825} 826 827SyscallReturn 828pipeImpl(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc, 829 bool pseudoPipe) 830{ 831 Addr tgt_addr = 0; 832 if (!pseudoPipe) { 833 int index = 0; 834 tgt_addr = p->getSyscallArg(tc, index); 835 } 836 837 int sim_fds[2], tgt_fds[2]; 838 839 int pipe_retval = pipe(sim_fds); 840 if (pipe_retval == -1) 841 return -errno; 842 843 auto rend = PipeFDEntry::EndType::read; 844 auto rpfd = std::make_shared<PipeFDEntry>(sim_fds[0], O_WRONLY, rend); 845 tgt_fds[0] = p->fds->allocFD(rpfd); 846 847 auto wend = PipeFDEntry::EndType::write; 848 auto wpfd = std::make_shared<PipeFDEntry>(sim_fds[1], O_RDONLY, wend); 849 tgt_fds[1] = p->fds->allocFD(wpfd); 850 851 /** 852 * Now patch the read object to record the target file descriptor chosen 853 * as the write end of the pipe. 854 */ 855 rpfd->setPipeReadSource(tgt_fds[1]); 856 857 /** 858 * Alpha Linux convention for pipe() is that fd[0] is returned as 859 * the return value of the function, and fd[1] is returned in r20. 860 */ 861 if (pseudoPipe) { 862 tc->setIntReg(SyscallPseudoReturnReg, tgt_fds[1]); 863 return tgt_fds[0]; 864 } 865 866 /** 867 * Copy the target file descriptors into buffer space and then copy 868 * the buffer space back into the target address space. 869 */ 870 BufferArg tgt_handle(tgt_addr, sizeof(int[2])); 871 int *buf_ptr = (int*)tgt_handle.bufferPtr(); 872 buf_ptr[0] = tgt_fds[0]; 873 buf_ptr[1] = tgt_fds[1]; 874 tgt_handle.copyOut(tc->getMemProxy()); 875 return 0; 876} 877 878SyscallReturn 879pipePseudoFunc(SyscallDesc *desc, int callnum, Process *process, 880 ThreadContext *tc) 881{ 882 return pipeImpl(desc, callnum, process, tc, true); 883} 884 885SyscallReturn 886pipeFunc(SyscallDesc *desc, int callnum, Process *process, ThreadContext *tc) 887{ 888 return pipeImpl(desc, callnum, process, tc, false); 889} 890 891SyscallReturn 892setpgidFunc(SyscallDesc *desc, int callnum, Process *process, 893 ThreadContext *tc) 894{ 895 int index = 0; 896 int pid = process->getSyscallArg(tc, index); 897 int pgid = process->getSyscallArg(tc, index); 898 899 if (pgid < 0) 900 return -EINVAL; 901 902 if (pid == 0) { 903 process->setpgid(process->pid()); 904 return 0; 905 } 906 907 Process *matched_ph = nullptr; 908 System *sysh = tc->getSystemPtr(); 909 910 // Retrieves process pointer from active/suspended thread contexts. 911 for (int i = 0; i < sysh->numContexts(); i++) { 912 if (sysh->threadContexts[i]->status() != ThreadContext::Halted) { 913 Process *temp_h = sysh->threadContexts[i]->getProcessPtr(); 914 Process *walk_ph = (Process*)temp_h; 915 916 if (walk_ph && walk_ph->pid() == process->pid()) 917 matched_ph = walk_ph; 918 } 919 } 920 921 assert(matched_ph); 922 matched_ph->setpgid((pgid == 0) ? matched_ph->pid() : pgid); 923 924 return 0; 925} 926 927SyscallReturn 928getpidPseudoFunc(SyscallDesc *desc, int callnum, Process *process, 929 ThreadContext *tc) 930{ 931 // Make up a PID. There's no interprocess communication in 932 // fake_syscall mode, so there's no way for a process to know it's 933 // not getting a unique value. 934 935 tc->setIntReg(SyscallPseudoReturnReg, process->ppid()); 936 return process->pid(); 937} 938 939 940SyscallReturn 941getuidPseudoFunc(SyscallDesc *desc, int callnum, Process *process, 942 ThreadContext *tc) 943{ 944 // Make up a UID and EUID... it shouldn't matter, and we want the 945 // simulation to be deterministic. 946 947 // EUID goes in r20. 948 tc->setIntReg(SyscallPseudoReturnReg, process->euid()); // EUID 949 return process->uid(); // UID 950} 951 952 953SyscallReturn 954getgidPseudoFunc(SyscallDesc *desc, int callnum, Process *process, 955 ThreadContext *tc) 956{ 957 // Get current group ID. EGID goes in r20. 958 tc->setIntReg(SyscallPseudoReturnReg, process->egid()); // EGID 959 return process->gid(); 960} 961 962 963SyscallReturn 964setuidFunc(SyscallDesc *desc, int callnum, Process *process, 965 ThreadContext *tc) 966{ 967 // can't fathom why a benchmark would call this. 968 int index = 0; 969 warn("Ignoring call to setuid(%d)\n", process->getSyscallArg(tc, index)); 970 return 0; 971} 972 973SyscallReturn 974getpidFunc(SyscallDesc *desc, int callnum, Process *process, 975 ThreadContext *tc) 976{ 977 return process->tgid(); 978} 979 980SyscallReturn 981gettidFunc(SyscallDesc *desc, int callnum, Process *process, 982 ThreadContext *tc) 983{ 984 return process->pid(); 985} 986 987SyscallReturn 988getppidFunc(SyscallDesc *desc, int callnum, Process *process, 989 ThreadContext *tc) 990{ 991 return process->ppid(); 992} 993 994SyscallReturn 995getuidFunc(SyscallDesc *desc, int callnum, Process *process, 996 ThreadContext *tc) 997{ 998 return process->uid(); // UID 999} 1000 1001SyscallReturn 1002geteuidFunc(SyscallDesc *desc, int callnum, Process *process, 1003 ThreadContext *tc) 1004{ 1005 return process->euid(); // UID 1006} 1007 1008SyscallReturn 1009getgidFunc(SyscallDesc *desc, int callnum, Process *process, 1010 ThreadContext *tc) 1011{ 1012 return process->gid(); 1013} 1014 1015SyscallReturn 1016getegidFunc(SyscallDesc *desc, int callnum, Process *process, 1017 ThreadContext *tc) 1018{ 1019 return process->egid(); 1020} 1021 1022SyscallReturn 1023fallocateFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 1024{ 1025#if NO_FALLOCATE 1026 warn("Host OS cannot support calls to fallocate. Ignoring syscall"); 1027#else 1028 int index = 0; 1029 int tgt_fd = p->getSyscallArg(tc, index); 1030 int mode = p->getSyscallArg(tc, index); 1031 off_t offset = p->getSyscallArg(tc, index); 1032 off_t len = p->getSyscallArg(tc, index); 1033 1034 auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 1035 if (!ffdp) 1036 return -EBADF; 1037 int sim_fd = ffdp->getSimFD(); 1038 1039 int result = fallocate(sim_fd, mode, offset, len); 1040 if (result < 0) 1041 return -errno; 1042#endif 1043 return 0; 1044} 1045 1046SyscallReturn 1047accessFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc, 1048 int index) 1049{ 1050 string path; 1051 if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index))) 1052 return -EFAULT; 1053 1054 // Adjust path for current working directory 1055 path = p->fullPath(path); 1056 1057 mode_t mode = p->getSyscallArg(tc, index); 1058 1059 int result = access(path.c_str(), mode); 1060 return (result == -1) ? -errno : result; 1061} 1062 1063SyscallReturn 1064accessFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 1065{ 1066 return accessFunc(desc, callnum, p, tc, 0); 1067} 1068 1069