syscall_emul.cc revision 2680
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 <fcntl.h> 33#include <unistd.h> 34 35#include <string> 36#include <iostream> 37 38#include "sim/syscall_emul.hh" 39#include "base/chunk_generator.hh" 40#include "base/trace.hh" 41#include "cpu/thread_context.hh" 42#include "cpu/base.hh" 43#include "mem/page_table.hh" 44#include "sim/process.hh" 45 46#include "sim/sim_events.hh" 47 48using namespace std; 49using namespace TheISA; 50 51void 52SyscallDesc::doSyscall(int callnum, Process *process, ThreadContext *tc) 53{ 54 DPRINTFR(SyscallVerbose, "%d: %s: syscall %s called w/arguments %d,%d,%d,%d\n", 55 curTick,tc->getCpuPtr()->name(), name, 56 tc->getSyscallArg(0),tc->getSyscallArg(1), 57 tc->getSyscallArg(2),tc->getSyscallArg(3)); 58 59 SyscallReturn retval = (*funcPtr)(this, callnum, process, tc); 60 61 DPRINTFR(SyscallVerbose, "%d: %s: syscall %s returns %d\n", 62 curTick,tc->getCpuPtr()->name(), name, retval.value()); 63 64 if (!(flags & SyscallDesc::SuppressReturnValue)) 65 tc->setSyscallReturn(retval); 66} 67 68 69SyscallReturn 70unimplementedFunc(SyscallDesc *desc, int callnum, Process *process, 71 ThreadContext *tc) 72{ 73 fatal("syscall %s (#%d) unimplemented.", desc->name, callnum); 74 75 return 1; 76} 77 78 79SyscallReturn 80ignoreFunc(SyscallDesc *desc, int callnum, Process *process, 81 ThreadContext *tc) 82{ 83 warn("ignoring syscall %s(%d, %d, ...)", desc->name, 84 tc->getSyscallArg(0), tc->getSyscallArg(1)); 85 86 return 0; 87} 88 89 90SyscallReturn 91exitFunc(SyscallDesc *desc, int callnum, Process *process, 92 ThreadContext *tc) 93{ 94 new SimExitEvent("target called exit()", tc->getSyscallArg(0) & 0xff); 95 96 return 1; 97} 98 99 100SyscallReturn 101getpagesizeFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 102{ 103 return (int)VMPageSize; 104} 105 106 107SyscallReturn 108obreakFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 109{ 110 Addr junk; 111 112 // change brk addr to first arg 113 Addr new_brk = tc->getSyscallArg(0); 114 if (new_brk != 0) { 115 for (ChunkGenerator gen(p->brk_point, new_brk - p->brk_point, 116 VMPageSize); !gen.done(); gen.next()) { 117 if (!p->pTable->translate(gen.addr(), junk)) 118 p->pTable->allocate(roundDown(gen.addr(), VMPageSize), 119 VMPageSize); 120 } 121 p->brk_point = new_brk; 122 } 123 DPRINTF(SyscallVerbose, "Break Point changed to: %#X\n", p->brk_point); 124 return p->brk_point; 125} 126 127 128SyscallReturn 129closeFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 130{ 131 int target_fd = tc->getSyscallArg(0); 132 int status = close(p->sim_fd(target_fd)); 133 if (status >= 0) 134 p->free_fd(target_fd); 135 return status; 136} 137 138 139SyscallReturn 140readFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 141{ 142 int fd = p->sim_fd(tc->getSyscallArg(0)); 143 int nbytes = tc->getSyscallArg(2); 144 BufferArg bufArg(tc->getSyscallArg(1), nbytes); 145 146 int bytes_read = read(fd, bufArg.bufferPtr(), nbytes); 147 148 if (bytes_read != -1) 149 bufArg.copyOut(tc->getMemPort()); 150 151 return bytes_read; 152} 153 154SyscallReturn 155writeFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 156{ 157 int fd = p->sim_fd(tc->getSyscallArg(0)); 158 int nbytes = tc->getSyscallArg(2); 159 BufferArg bufArg(tc->getSyscallArg(1), nbytes); 160 161 bufArg.copyIn(tc->getMemPort()); 162 163 int bytes_written = write(fd, bufArg.bufferPtr(), nbytes); 164 165 fsync(fd); 166 167 return bytes_written; 168} 169 170 171SyscallReturn 172lseekFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 173{ 174 int fd = p->sim_fd(tc->getSyscallArg(0)); 175 uint64_t offs = tc->getSyscallArg(1); 176 int whence = tc->getSyscallArg(2); 177 178 off_t result = lseek(fd, offs, whence); 179 180 return (result == (off_t)-1) ? -errno : result; 181} 182 183 184SyscallReturn 185munmapFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 186{ 187 // given that we don't really implement mmap, munmap is really easy 188 return 0; 189} 190 191 192const char *hostname = "m5.eecs.umich.edu"; 193 194SyscallReturn 195gethostnameFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 196{ 197 int name_len = tc->getSyscallArg(1); 198 BufferArg name(tc->getSyscallArg(0), name_len); 199 200 strncpy((char *)name.bufferPtr(), hostname, name_len); 201 202 name.copyOut(tc->getMemPort()); 203 204 return 0; 205} 206 207SyscallReturn 208unlinkFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 209{ 210 string path; 211 212 if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0))) 213 return (TheISA::IntReg)-EFAULT; 214 215 int result = unlink(path.c_str()); 216 return (result == -1) ? -errno : result; 217} 218 219SyscallReturn 220renameFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 221{ 222 string old_name; 223 224 if (!tc->getMemPort()->tryReadString(old_name, tc->getSyscallArg(0))) 225 return -EFAULT; 226 227 string new_name; 228 229 if (!tc->getMemPort()->tryReadString(new_name, tc->getSyscallArg(1))) 230 return -EFAULT; 231 232 int64_t result = rename(old_name.c_str(), new_name.c_str()); 233 return (result == -1) ? -errno : result; 234} 235 236SyscallReturn 237truncateFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 238{ 239 string path; 240 241 if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0))) 242 return -EFAULT; 243 244 off_t length = tc->getSyscallArg(1); 245 246 int result = truncate(path.c_str(), length); 247 return (result == -1) ? -errno : result; 248} 249 250SyscallReturn 251ftruncateFunc(SyscallDesc *desc, int num, Process *process, ThreadContext *tc) 252{ 253 int fd = process->sim_fd(tc->getSyscallArg(0)); 254 255 if (fd < 0) 256 return -EBADF; 257 258 off_t length = tc->getSyscallArg(1); 259 260 int result = ftruncate(fd, length); 261 return (result == -1) ? -errno : result; 262} 263 264SyscallReturn 265chownFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 266{ 267 string path; 268 269 if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0))) 270 return -EFAULT; 271 272 /* XXX endianess */ 273 uint32_t owner = tc->getSyscallArg(1); 274 uid_t hostOwner = owner; 275 uint32_t group = tc->getSyscallArg(2); 276 gid_t hostGroup = group; 277 278 int result = chown(path.c_str(), hostOwner, hostGroup); 279 return (result == -1) ? -errno : result; 280} 281 282SyscallReturn 283fchownFunc(SyscallDesc *desc, int num, Process *process, ThreadContext *tc) 284{ 285 int fd = process->sim_fd(tc->getSyscallArg(0)); 286 287 if (fd < 0) 288 return -EBADF; 289 290 /* XXX endianess */ 291 uint32_t owner = tc->getSyscallArg(1); 292 uid_t hostOwner = owner; 293 uint32_t group = tc->getSyscallArg(2); 294 gid_t hostGroup = group; 295 296 int result = fchown(fd, hostOwner, hostGroup); 297 return (result == -1) ? -errno : result; 298} 299 300 301SyscallReturn 302fcntlFunc(SyscallDesc *desc, int num, Process *process, 303 ThreadContext *tc) 304{ 305 int fd = tc->getSyscallArg(0); 306 307 if (fd < 0 || process->sim_fd(fd) < 0) 308 return -EBADF; 309 310 int cmd = tc->getSyscallArg(1); 311 switch (cmd) { 312 case 0: // F_DUPFD 313 // if we really wanted to support this, we'd need to do it 314 // in the target fd space. 315 warn("fcntl(%d, F_DUPFD) not supported, error returned\n", fd); 316 return -EMFILE; 317 318 case 1: // F_GETFD (get close-on-exec flag) 319 case 2: // F_SETFD (set close-on-exec flag) 320 return 0; 321 322 case 3: // F_GETFL (get file flags) 323 case 4: // F_SETFL (set file flags) 324 // not sure if this is totally valid, but we'll pass it through 325 // to the underlying OS 326 warn("fcntl(%d, %d) passed through to host\n", fd, cmd); 327 return fcntl(process->sim_fd(fd), cmd); 328 // return 0; 329 330 case 7: // F_GETLK (get lock) 331 case 8: // F_SETLK (set lock) 332 case 9: // F_SETLKW (set lock and wait) 333 // don't mess with file locking... just act like it's OK 334 warn("File lock call (fcntl(%d, %d)) ignored.\n", fd, cmd); 335 return 0; 336 337 default: 338 warn("Unknown fcntl command %d\n", cmd); 339 return 0; 340 } 341} 342 343SyscallReturn 344pipePseudoFunc(SyscallDesc *desc, int callnum, Process *process, 345 ThreadContext *tc) 346{ 347 int fds[2], sim_fds[2]; 348 int pipe_retval = pipe(fds); 349 350 if (pipe_retval < 0) { 351 // error 352 return pipe_retval; 353 } 354 355 sim_fds[0] = process->alloc_fd(fds[0]); 356 sim_fds[1] = process->alloc_fd(fds[1]); 357 358 // Alpha Linux convention for pipe() is that fd[0] is returned as 359 // the return value of the function, and fd[1] is returned in r20. 360 tc->setIntReg(SyscallPseudoReturnReg, sim_fds[1]); 361 return sim_fds[0]; 362} 363 364 365SyscallReturn 366getpidPseudoFunc(SyscallDesc *desc, int callnum, Process *process, 367 ThreadContext *tc) 368{ 369 // Make up a PID. There's no interprocess communication in 370 // fake_syscall mode, so there's no way for a process to know it's 371 // not getting a unique value. 372 373 tc->setIntReg(SyscallPseudoReturnReg, 99); 374 return 100; 375} 376 377 378SyscallReturn 379getuidPseudoFunc(SyscallDesc *desc, int callnum, Process *process, 380 ThreadContext *tc) 381{ 382 // Make up a UID and EUID... it shouldn't matter, and we want the 383 // simulation to be deterministic. 384 385 // EUID goes in r20. 386 tc->setIntReg(SyscallPseudoReturnReg, 100); //EUID 387 return 100; // UID 388} 389 390 391SyscallReturn 392getgidPseudoFunc(SyscallDesc *desc, int callnum, Process *process, 393 ThreadContext *tc) 394{ 395 // Get current group ID. EGID goes in r20. 396 tc->setIntReg(SyscallPseudoReturnReg, 100); //EGID 397 return 100; 398} 399 400 401SyscallReturn 402setuidFunc(SyscallDesc *desc, int callnum, Process *process, 403 ThreadContext *tc) 404{ 405 // can't fathom why a benchmark would call this. 406 warn("Ignoring call to setuid(%d)\n", tc->getSyscallArg(0)); 407 return 0; 408} 409 410SyscallReturn 411getpidFunc(SyscallDesc *desc, int callnum, Process *process, 412 ThreadContext *tc) 413{ 414 // Make up a PID. There's no interprocess communication in 415 // fake_syscall mode, so there's no way for a process to know it's 416 // not getting a unique value. 417 418 tc->setIntReg(SyscallPseudoReturnReg, 99); //PID 419 return 100; 420} 421 422SyscallReturn 423getppidFunc(SyscallDesc *desc, int callnum, Process *process, 424 ThreadContext *tc) 425{ 426 return 99; 427} 428 429SyscallReturn 430getuidFunc(SyscallDesc *desc, int callnum, Process *process, 431 ThreadContext *tc) 432{ 433 return 100; // UID 434} 435 436SyscallReturn 437geteuidFunc(SyscallDesc *desc, int callnum, Process *process, 438 ThreadContext *tc) 439{ 440 return 100; // UID 441} 442 443SyscallReturn 444getgidFunc(SyscallDesc *desc, int callnum, Process *process, 445 ThreadContext *tc) 446{ 447 return 100; 448} 449 450SyscallReturn 451getegidFunc(SyscallDesc *desc, int callnum, Process *process, 452 ThreadContext *tc) 453{ 454 return 100; 455} 456 457 458