syscall_emul.cc revision 12796:16dffc0e6c7f
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 522SyscallReturn 523linkFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 524{ 525 string path; 526 string new_path; 527 528 int index = 0; 529 auto &virt_mem = tc->getMemProxy(); 530 if (!virt_mem.tryReadString(path, p->getSyscallArg(tc, index))) 531 return -EFAULT; 532 if (!virt_mem.tryReadString(new_path, p->getSyscallArg(tc, index))) 533 return -EFAULT; 534 535 path = p->fullPath(path); 536 new_path = p->fullPath(new_path); 537 538 int result = link(path.c_str(), new_path.c_str()); 539 return (result == -1) ? -errno : result; 540} 541 542SyscallReturn 543symlinkFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 544{ 545 string path; 546 string new_path; 547 548 int index = 0; 549 auto &virt_mem = tc->getMemProxy(); 550 if (!virt_mem.tryReadString(path, p->getSyscallArg(tc, index))) 551 return -EFAULT; 552 if (!virt_mem.tryReadString(new_path, p->getSyscallArg(tc, index))) 553 return -EFAULT; 554 555 path = p->fullPath(path); 556 new_path = p->fullPath(new_path); 557 558 int result = symlink(path.c_str(), new_path.c_str()); 559 return (result == -1) ? -errno : result; 560} 561 562SyscallReturn 563mkdirFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 564{ 565 string path; 566 567 int index = 0; 568 if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index))) 569 return -EFAULT; 570 571 // Adjust path for current working directory 572 path = p->fullPath(path); 573 574 mode_t mode = p->getSyscallArg(tc, index); 575 576 int result = mkdir(path.c_str(), mode); 577 return (result == -1) ? -errno : result; 578} 579 580SyscallReturn 581renameFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 582{ 583 string old_name; 584 585 int index = 0; 586 if (!tc->getMemProxy().tryReadString(old_name, p->getSyscallArg(tc, index))) 587 return -EFAULT; 588 589 string new_name; 590 591 if (!tc->getMemProxy().tryReadString(new_name, p->getSyscallArg(tc, index))) 592 return -EFAULT; 593 594 // Adjust path for current working directory 595 old_name = p->fullPath(old_name); 596 new_name = p->fullPath(new_name); 597 598 int64_t result = rename(old_name.c_str(), new_name.c_str()); 599 return (result == -1) ? -errno : result; 600} 601 602SyscallReturn 603truncateFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 604{ 605 string path; 606 607 int index = 0; 608 if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index))) 609 return -EFAULT; 610 611 off_t length = p->getSyscallArg(tc, index); 612 613 // Adjust path for current working directory 614 path = p->fullPath(path); 615 616 int result = truncate(path.c_str(), length); 617 return (result == -1) ? -errno : result; 618} 619 620SyscallReturn 621ftruncateFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 622{ 623 int index = 0; 624 int tgt_fd = p->getSyscallArg(tc, index); 625 off_t length = p->getSyscallArg(tc, index); 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 int result = ftruncate(sim_fd, length); 633 return (result == -1) ? -errno : result; 634} 635 636SyscallReturn 637truncate64Func(SyscallDesc *desc, int num, 638 Process *process, ThreadContext *tc) 639{ 640 int index = 0; 641 string path; 642 643 if (!tc->getMemProxy().tryReadString(path, process->getSyscallArg(tc, index))) 644 return -EFAULT; 645 646 int64_t length = process->getSyscallArg(tc, index, 64); 647 648 // Adjust path for current working directory 649 path = process->fullPath(path); 650 651#if NO_STAT64 652 int result = truncate(path.c_str(), length); 653#else 654 int result = truncate64(path.c_str(), length); 655#endif 656 return (result == -1) ? -errno : result; 657} 658 659SyscallReturn 660ftruncate64Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 661{ 662 int index = 0; 663 int tgt_fd = p->getSyscallArg(tc, index); 664 int64_t length = p->getSyscallArg(tc, index, 64); 665 666 auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 667 if (!ffdp) 668 return -EBADF; 669 int sim_fd = ffdp->getSimFD(); 670 671#if NO_STAT64 672 int result = ftruncate(sim_fd, length); 673#else 674 int result = ftruncate64(sim_fd, length); 675#endif 676 return (result == -1) ? -errno : result; 677} 678 679SyscallReturn 680umaskFunc(SyscallDesc *desc, int num, Process *process, ThreadContext *tc) 681{ 682 // Letting the simulated program change the simulator's umask seems like 683 // a bad idea. Compromise by just returning the current umask but not 684 // changing anything. 685 mode_t oldMask = umask(0); 686 umask(oldMask); 687 return (int)oldMask; 688} 689 690SyscallReturn 691chownFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 692{ 693 string path; 694 695 int index = 0; 696 if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index))) 697 return -EFAULT; 698 699 /* XXX endianess */ 700 uint32_t owner = p->getSyscallArg(tc, index); 701 uid_t hostOwner = owner; 702 uint32_t group = p->getSyscallArg(tc, index); 703 gid_t hostGroup = group; 704 705 // Adjust path for current working directory 706 path = p->fullPath(path); 707 708 int result = chown(path.c_str(), hostOwner, hostGroup); 709 return (result == -1) ? -errno : result; 710} 711 712SyscallReturn 713fchownFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 714{ 715 int index = 0; 716 int tgt_fd = p->getSyscallArg(tc, index); 717 718 auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 719 if (!ffdp) 720 return -EBADF; 721 int sim_fd = ffdp->getSimFD(); 722 723 /* XXX endianess */ 724 uint32_t owner = p->getSyscallArg(tc, index); 725 uid_t hostOwner = owner; 726 uint32_t group = p->getSyscallArg(tc, index); 727 gid_t hostGroup = group; 728 729 int result = fchown(sim_fd, hostOwner, hostGroup); 730 return (result == -1) ? -errno : result; 731} 732 733/** 734 * FIXME: The file description is not shared among file descriptors created 735 * with dup. Really, it's difficult to maintain fields like file offset or 736 * flags since an update to such a field won't be reflected in the metadata 737 * for the fd entries that we maintain for checkpoint restoration. 738 */ 739SyscallReturn 740dupFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 741{ 742 int index = 0; 743 int tgt_fd = p->getSyscallArg(tc, index); 744 745 auto old_hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]); 746 if (!old_hbfdp) 747 return -EBADF; 748 int sim_fd = old_hbfdp->getSimFD(); 749 750 int result = dup(sim_fd); 751 if (result == -1) 752 return -errno; 753 754 auto new_hbfdp = std::dynamic_pointer_cast<HBFDEntry>(old_hbfdp->clone()); 755 new_hbfdp->setSimFD(result); 756 new_hbfdp->setCOE(false); 757 return p->fds->allocFD(new_hbfdp); 758} 759 760SyscallReturn 761dup2Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 762{ 763 int index = 0; 764 765 int old_tgt_fd = p->getSyscallArg(tc, index); 766 auto old_hbp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[old_tgt_fd]); 767 if (!old_hbp) 768 return -EBADF; 769 int old_sim_fd = old_hbp->getSimFD(); 770 771 /** 772 * We need a valid host file descriptor number to be able to pass into 773 * the second parameter for dup2 (newfd), but we don't know what the 774 * viable numbers are; we execute the open call to retrieve one. 775 */ 776 int res_fd = dup2(old_sim_fd, open("/dev/null", O_RDONLY)); 777 if (res_fd == -1) 778 return -errno; 779 780 int new_tgt_fd = p->getSyscallArg(tc, index); 781 auto new_hbp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[new_tgt_fd]); 782 if (new_hbp) 783 p->fds->closeFDEntry(new_tgt_fd); 784 new_hbp = std::dynamic_pointer_cast<HBFDEntry>(old_hbp->clone()); 785 new_hbp->setSimFD(res_fd); 786 new_hbp->setCOE(false); 787 788 return p->fds->allocFD(new_hbp); 789} 790 791SyscallReturn 792fcntlFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 793{ 794 int arg; 795 int index = 0; 796 int tgt_fd = p->getSyscallArg(tc, index); 797 int cmd = p->getSyscallArg(tc, index); 798 799 auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]); 800 if (!hbfdp) 801 return -EBADF; 802 int sim_fd = hbfdp->getSimFD(); 803 804 int coe = hbfdp->getCOE(); 805 806 switch (cmd) { 807 case F_GETFD: 808 return coe & FD_CLOEXEC; 809 810 case F_SETFD: { 811 arg = p->getSyscallArg(tc, index); 812 arg ? hbfdp->setCOE(true) : hbfdp->setCOE(false); 813 return 0; 814 } 815 816 // Rely on the host to maintain the file status flags for this file 817 // description rather than maintain it ourselves. Admittedly, this 818 // is suboptimal (and possibly error prone), but it is difficult to 819 // maintain the flags by tracking them across the different descriptors 820 // (that refer to this file description) caused by clone, dup, and 821 // subsequent fcntls. 822 case F_GETFL: 823 case F_SETFL: { 824 arg = p->getSyscallArg(tc, index); 825 int rv = fcntl(sim_fd, cmd, arg); 826 return (rv == -1) ? -errno : rv; 827 } 828 829 default: 830 warn("fcntl: unsupported command %d\n", cmd); 831 return 0; 832 } 833} 834 835SyscallReturn 836fcntl64Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 837{ 838 int index = 0; 839 int tgt_fd = p->getSyscallArg(tc, index); 840 841 auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]); 842 if (!hbfdp) 843 return -EBADF; 844 int sim_fd = hbfdp->getSimFD(); 845 846 int cmd = p->getSyscallArg(tc, index); 847 switch (cmd) { 848 case 33: //F_GETLK64 849 warn("fcntl64(%d, F_GETLK64) not supported, error returned\n", tgt_fd); 850 return -EMFILE; 851 852 case 34: // F_SETLK64 853 case 35: // F_SETLKW64 854 warn("fcntl64(%d, F_SETLK(W)64) not supported, error returned\n", 855 tgt_fd); 856 return -EMFILE; 857 858 default: 859 // not sure if this is totally valid, but we'll pass it through 860 // to the underlying OS 861 warn("fcntl64(%d, %d) passed through to host\n", tgt_fd, cmd); 862 return fcntl(sim_fd, cmd); 863 } 864} 865 866SyscallReturn 867pipeImpl(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc, 868 bool pseudoPipe) 869{ 870 Addr tgt_addr = 0; 871 if (!pseudoPipe) { 872 int index = 0; 873 tgt_addr = p->getSyscallArg(tc, index); 874 } 875 876 int sim_fds[2], tgt_fds[2]; 877 878 int pipe_retval = pipe(sim_fds); 879 if (pipe_retval == -1) 880 return -errno; 881 882 auto rend = PipeFDEntry::EndType::read; 883 auto rpfd = std::make_shared<PipeFDEntry>(sim_fds[0], O_WRONLY, rend); 884 tgt_fds[0] = p->fds->allocFD(rpfd); 885 886 auto wend = PipeFDEntry::EndType::write; 887 auto wpfd = std::make_shared<PipeFDEntry>(sim_fds[1], O_RDONLY, wend); 888 tgt_fds[1] = p->fds->allocFD(wpfd); 889 890 /** 891 * Now patch the read object to record the target file descriptor chosen 892 * as the write end of the pipe. 893 */ 894 rpfd->setPipeReadSource(tgt_fds[1]); 895 896 /** 897 * Alpha Linux convention for pipe() is that fd[0] is returned as 898 * the return value of the function, and fd[1] is returned in r20. 899 */ 900 if (pseudoPipe) { 901 tc->setIntReg(SyscallPseudoReturnReg, tgt_fds[1]); 902 return tgt_fds[0]; 903 } 904 905 /** 906 * Copy the target file descriptors into buffer space and then copy 907 * the buffer space back into the target address space. 908 */ 909 BufferArg tgt_handle(tgt_addr, sizeof(int[2])); 910 int *buf_ptr = (int*)tgt_handle.bufferPtr(); 911 buf_ptr[0] = tgt_fds[0]; 912 buf_ptr[1] = tgt_fds[1]; 913 tgt_handle.copyOut(tc->getMemProxy()); 914 return 0; 915} 916 917SyscallReturn 918pipePseudoFunc(SyscallDesc *desc, int callnum, Process *process, 919 ThreadContext *tc) 920{ 921 return pipeImpl(desc, callnum, process, tc, true); 922} 923 924SyscallReturn 925pipeFunc(SyscallDesc *desc, int callnum, Process *process, ThreadContext *tc) 926{ 927 return pipeImpl(desc, callnum, process, tc, false); 928} 929 930SyscallReturn 931setpgidFunc(SyscallDesc *desc, int callnum, Process *process, 932 ThreadContext *tc) 933{ 934 int index = 0; 935 int pid = process->getSyscallArg(tc, index); 936 int pgid = process->getSyscallArg(tc, index); 937 938 if (pgid < 0) 939 return -EINVAL; 940 941 if (pid == 0) { 942 process->setpgid(process->pid()); 943 return 0; 944 } 945 946 Process *matched_ph = nullptr; 947 System *sysh = tc->getSystemPtr(); 948 949 // Retrieves process pointer from active/suspended thread contexts. 950 for (int i = 0; i < sysh->numContexts(); i++) { 951 if (sysh->threadContexts[i]->status() != ThreadContext::Halted) { 952 Process *temp_h = sysh->threadContexts[i]->getProcessPtr(); 953 Process *walk_ph = (Process*)temp_h; 954 955 if (walk_ph && walk_ph->pid() == process->pid()) 956 matched_ph = walk_ph; 957 } 958 } 959 960 assert(matched_ph); 961 matched_ph->setpgid((pgid == 0) ? matched_ph->pid() : pgid); 962 963 return 0; 964} 965 966SyscallReturn 967getpidPseudoFunc(SyscallDesc *desc, int callnum, Process *process, 968 ThreadContext *tc) 969{ 970 // Make up a PID. There's no interprocess communication in 971 // fake_syscall mode, so there's no way for a process to know it's 972 // not getting a unique value. 973 974 tc->setIntReg(SyscallPseudoReturnReg, process->ppid()); 975 return process->pid(); 976} 977 978 979SyscallReturn 980getuidPseudoFunc(SyscallDesc *desc, int callnum, Process *process, 981 ThreadContext *tc) 982{ 983 // Make up a UID and EUID... it shouldn't matter, and we want the 984 // simulation to be deterministic. 985 986 // EUID goes in r20. 987 tc->setIntReg(SyscallPseudoReturnReg, process->euid()); // EUID 988 return process->uid(); // UID 989} 990 991 992SyscallReturn 993getgidPseudoFunc(SyscallDesc *desc, int callnum, Process *process, 994 ThreadContext *tc) 995{ 996 // Get current group ID. EGID goes in r20. 997 tc->setIntReg(SyscallPseudoReturnReg, process->egid()); // EGID 998 return process->gid(); 999} 1000 1001 1002SyscallReturn 1003setuidFunc(SyscallDesc *desc, int callnum, Process *process, 1004 ThreadContext *tc) 1005{ 1006 // can't fathom why a benchmark would call this. 1007 int index = 0; 1008 warn("Ignoring call to setuid(%d)\n", process->getSyscallArg(tc, index)); 1009 return 0; 1010} 1011 1012SyscallReturn 1013getpidFunc(SyscallDesc *desc, int callnum, Process *process, 1014 ThreadContext *tc) 1015{ 1016 return process->tgid(); 1017} 1018 1019SyscallReturn 1020gettidFunc(SyscallDesc *desc, int callnum, Process *process, 1021 ThreadContext *tc) 1022{ 1023 return process->pid(); 1024} 1025 1026SyscallReturn 1027getppidFunc(SyscallDesc *desc, int callnum, Process *process, 1028 ThreadContext *tc) 1029{ 1030 return process->ppid(); 1031} 1032 1033SyscallReturn 1034getuidFunc(SyscallDesc *desc, int callnum, Process *process, 1035 ThreadContext *tc) 1036{ 1037 return process->uid(); // UID 1038} 1039 1040SyscallReturn 1041geteuidFunc(SyscallDesc *desc, int callnum, Process *process, 1042 ThreadContext *tc) 1043{ 1044 return process->euid(); // UID 1045} 1046 1047SyscallReturn 1048getgidFunc(SyscallDesc *desc, int callnum, Process *process, 1049 ThreadContext *tc) 1050{ 1051 return process->gid(); 1052} 1053 1054SyscallReturn 1055getegidFunc(SyscallDesc *desc, int callnum, Process *process, 1056 ThreadContext *tc) 1057{ 1058 return process->egid(); 1059} 1060 1061SyscallReturn 1062fallocateFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 1063{ 1064#if NO_FALLOCATE 1065 warn("Host OS cannot support calls to fallocate. Ignoring syscall"); 1066#else 1067 int index = 0; 1068 int tgt_fd = p->getSyscallArg(tc, index); 1069 int mode = p->getSyscallArg(tc, index); 1070 off_t offset = p->getSyscallArg(tc, index); 1071 off_t len = p->getSyscallArg(tc, index); 1072 1073 auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 1074 if (!ffdp) 1075 return -EBADF; 1076 int sim_fd = ffdp->getSimFD(); 1077 1078 int result = fallocate(sim_fd, mode, offset, len); 1079 if (result < 0) 1080 return -errno; 1081#endif 1082 return 0; 1083} 1084 1085SyscallReturn 1086accessFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc, 1087 int index) 1088{ 1089 string path; 1090 if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index))) 1091 return -EFAULT; 1092 1093 // Adjust path for current working directory 1094 path = p->fullPath(path); 1095 1096 mode_t mode = p->getSyscallArg(tc, index); 1097 1098 int result = access(path.c_str(), mode); 1099 return (result == -1) ? -errno : result; 1100} 1101 1102SyscallReturn 1103accessFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 1104{ 1105 return accessFunc(desc, callnum, p, tc, 0); 1106} 1107 1108