2003-12-02 04:34:38 +01:00
|
|
|
/*
|
2005-03-10 20:20:12 +01:00
|
|
|
* Copyright (c) 2003-2005 The Regents of The University of Michigan
|
2003-12-02 04:34:38 +01:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are
|
|
|
|
* met: redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer;
|
|
|
|
* redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution;
|
|
|
|
* neither the name of the copyright holders nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived from
|
|
|
|
* this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include "sim/syscall_emul.hh"
|
|
|
|
#include "base/trace.hh"
|
|
|
|
#include "cpu/exec_context.hh"
|
2005-06-05 02:50:10 +02:00
|
|
|
#include "cpu/base.hh"
|
2003-12-02 04:34:38 +01:00
|
|
|
#include "sim/process.hh"
|
|
|
|
|
|
|
|
#include "sim/sim_events.hh"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
void
|
|
|
|
SyscallDesc::doSyscall(int callnum, Process *process, ExecContext *xc)
|
|
|
|
{
|
|
|
|
DPRINTFR(SyscallVerbose, "%s: syscall %s called\n",
|
|
|
|
xc->cpu->name(), name);
|
|
|
|
|
2005-03-09 21:52:10 +01:00
|
|
|
SyscallReturn retval = (*funcPtr)(this, callnum, process, xc);
|
2003-12-02 04:34:38 +01:00
|
|
|
|
|
|
|
DPRINTFR(SyscallVerbose, "%s: syscall %s returns %d\n",
|
2005-03-09 21:52:10 +01:00
|
|
|
xc->cpu->name(), name, retval.value());
|
2003-12-02 04:34:38 +01:00
|
|
|
|
2005-03-09 21:52:10 +01:00
|
|
|
if (!(flags & SyscallDesc::SuppressReturnValue))
|
2003-12-02 04:34:38 +01:00
|
|
|
xc->setSyscallReturn(retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-03-09 21:52:10 +01:00
|
|
|
SyscallReturn
|
2003-12-02 04:34:38 +01:00
|
|
|
unimplementedFunc(SyscallDesc *desc, int callnum, Process *process,
|
|
|
|
ExecContext *xc)
|
|
|
|
{
|
2005-11-11 03:05:31 +01:00
|
|
|
fatal("syscall %s (#%d) unimplemented.", desc->name, callnum);
|
2003-12-02 04:34:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-03-09 21:52:10 +01:00
|
|
|
SyscallReturn
|
2003-12-02 04:34:38 +01:00
|
|
|
ignoreFunc(SyscallDesc *desc, int callnum, Process *process,
|
|
|
|
ExecContext *xc)
|
|
|
|
{
|
2005-11-11 03:05:31 +01:00
|
|
|
warn("ignoring syscall %s(%d, %d, ...)", desc->name,
|
|
|
|
xc->getSyscallArg(0), xc->getSyscallArg(1));
|
2003-12-02 04:34:38 +01:00
|
|
|
|
2005-03-10 20:20:12 +01:00
|
|
|
return 0;
|
2003-12-02 04:34:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-03-09 21:52:10 +01:00
|
|
|
SyscallReturn
|
2003-12-02 04:34:38 +01:00
|
|
|
exitFunc(SyscallDesc *desc, int callnum, Process *process,
|
|
|
|
ExecContext *xc)
|
|
|
|
{
|
|
|
|
new SimExitEvent("syscall caused exit", xc->getSyscallArg(0) & 0xff);
|
|
|
|
|
2005-03-10 20:20:12 +01:00
|
|
|
return 1;
|
2003-12-02 04:34:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-03-09 21:52:10 +01:00
|
|
|
SyscallReturn
|
2003-12-02 04:34:38 +01:00
|
|
|
getpagesizeFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
|
|
|
|
{
|
2005-03-10 20:20:12 +01:00
|
|
|
return VMPageSize;
|
2003-12-02 04:34:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-03-09 21:52:10 +01:00
|
|
|
SyscallReturn
|
2003-12-02 04:34:38 +01:00
|
|
|
obreakFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
|
|
|
|
{
|
|
|
|
// change brk addr to first arg
|
2004-02-11 06:43:57 +01:00
|
|
|
Addr new_brk = xc->getSyscallArg(0);
|
|
|
|
if (new_brk != 0)
|
2005-03-09 21:52:10 +01:00
|
|
|
{
|
2004-02-11 06:43:57 +01:00
|
|
|
p->brk_point = xc->getSyscallArg(0);
|
2005-03-09 21:52:10 +01:00
|
|
|
}
|
2005-03-10 20:20:12 +01:00
|
|
|
DPRINTF(SyscallVerbose, "Break Point changed to: %#X\n", p->brk_point);
|
|
|
|
return p->brk_point;
|
2003-12-02 04:34:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-03-09 21:52:10 +01:00
|
|
|
SyscallReturn
|
2003-12-02 04:34:38 +01:00
|
|
|
closeFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
|
|
|
|
{
|
2005-11-11 03:08:33 +01:00
|
|
|
int target_fd = xc->getSyscallArg(0);
|
|
|
|
int status = close(p->sim_fd(target_fd));
|
|
|
|
if (status >= 0)
|
|
|
|
p->free_fd(target_fd);
|
|
|
|
return status;
|
2003-12-02 04:34:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-03-09 21:52:10 +01:00
|
|
|
SyscallReturn
|
2003-12-02 04:34:38 +01:00
|
|
|
readFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
|
|
|
|
{
|
|
|
|
int fd = p->sim_fd(xc->getSyscallArg(0));
|
|
|
|
int nbytes = xc->getSyscallArg(2);
|
|
|
|
BufferArg bufArg(xc->getSyscallArg(1), nbytes);
|
|
|
|
|
|
|
|
int bytes_read = read(fd, bufArg.bufferPtr(), nbytes);
|
|
|
|
|
|
|
|
if (bytes_read != -1)
|
|
|
|
bufArg.copyOut(xc->mem);
|
|
|
|
|
2005-03-10 20:20:12 +01:00
|
|
|
return bytes_read;
|
2003-12-02 04:34:38 +01:00
|
|
|
}
|
|
|
|
|
2005-03-09 21:52:10 +01:00
|
|
|
SyscallReturn
|
2003-12-02 04:34:38 +01:00
|
|
|
writeFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
|
|
|
|
{
|
|
|
|
int fd = p->sim_fd(xc->getSyscallArg(0));
|
|
|
|
int nbytes = xc->getSyscallArg(2);
|
|
|
|
BufferArg bufArg(xc->getSyscallArg(1), nbytes);
|
|
|
|
|
|
|
|
bufArg.copyIn(xc->mem);
|
|
|
|
|
|
|
|
int bytes_written = write(fd, bufArg.bufferPtr(), nbytes);
|
|
|
|
|
|
|
|
fsync(fd);
|
|
|
|
|
2005-03-10 20:20:12 +01:00
|
|
|
return bytes_written;
|
2003-12-02 04:34:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-03-09 21:52:10 +01:00
|
|
|
SyscallReturn
|
2003-12-02 04:34:38 +01:00
|
|
|
lseekFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
|
|
|
|
{
|
|
|
|
int fd = p->sim_fd(xc->getSyscallArg(0));
|
|
|
|
uint64_t offs = xc->getSyscallArg(1);
|
|
|
|
int whence = xc->getSyscallArg(2);
|
|
|
|
|
|
|
|
off_t result = lseek(fd, offs, whence);
|
|
|
|
|
2005-03-10 20:20:12 +01:00
|
|
|
return (result == (off_t)-1) ? -errno : result;
|
2003-12-02 04:34:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-03-09 21:52:10 +01:00
|
|
|
SyscallReturn
|
2003-12-02 04:34:38 +01:00
|
|
|
munmapFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
|
|
|
|
{
|
|
|
|
// given that we don't really implement mmap, munmap is really easy
|
2005-03-10 20:20:12 +01:00
|
|
|
return 0;
|
2003-12-02 04:34:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const char *hostname = "m5.eecs.umich.edu";
|
|
|
|
|
2005-03-09 21:52:10 +01:00
|
|
|
SyscallReturn
|
2003-12-02 04:34:38 +01:00
|
|
|
gethostnameFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
|
|
|
|
{
|
|
|
|
int name_len = xc->getSyscallArg(1);
|
|
|
|
BufferArg name(xc->getSyscallArg(0), name_len);
|
|
|
|
|
|
|
|
strncpy((char *)name.bufferPtr(), hostname, name_len);
|
|
|
|
|
|
|
|
name.copyOut(xc->mem);
|
|
|
|
|
2005-03-10 20:20:12 +01:00
|
|
|
return 0;
|
2003-12-02 04:34:38 +01:00
|
|
|
}
|
|
|
|
|
2005-03-09 21:52:10 +01:00
|
|
|
SyscallReturn
|
2004-02-05 18:16:17 +01:00
|
|
|
unlinkFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
|
|
|
|
{
|
2005-06-03 22:19:34 +02:00
|
|
|
string path;
|
2004-02-05 18:16:17 +01:00
|
|
|
|
|
|
|
if (xc->mem->readString(path, xc->getSyscallArg(0)) != No_Fault)
|
2005-03-09 21:52:10 +01:00
|
|
|
return (TheISA::IntReg)-EFAULT;
|
2004-02-05 18:16:17 +01:00
|
|
|
|
|
|
|
int result = unlink(path.c_str());
|
2005-03-10 20:20:12 +01:00
|
|
|
return (result == -1) ? -errno : result;
|
2004-02-05 18:16:17 +01:00
|
|
|
}
|
|
|
|
|
2005-03-09 21:52:10 +01:00
|
|
|
SyscallReturn
|
2004-02-05 18:16:17 +01:00
|
|
|
renameFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
|
|
|
|
{
|
2005-06-03 22:19:34 +02:00
|
|
|
string old_name;
|
2004-02-05 18:16:17 +01:00
|
|
|
|
|
|
|
if (xc->mem->readString(old_name, xc->getSyscallArg(0)) != No_Fault)
|
2005-03-10 20:20:12 +01:00
|
|
|
return -EFAULT;
|
2004-02-05 18:16:17 +01:00
|
|
|
|
2005-06-03 22:19:34 +02:00
|
|
|
string new_name;
|
2004-02-05 18:16:17 +01:00
|
|
|
|
|
|
|
if (xc->mem->readString(new_name, xc->getSyscallArg(1)) != No_Fault)
|
2005-03-10 20:20:12 +01:00
|
|
|
return -EFAULT;
|
2004-02-05 18:16:17 +01:00
|
|
|
|
2005-06-03 22:19:34 +02:00
|
|
|
int64_t result = rename(old_name.c_str(), new_name.c_str());
|
2005-03-10 20:20:12 +01:00
|
|
|
return (result == -1) ? -errno : result;
|
2004-02-05 18:16:17 +01:00
|
|
|
}
|
2003-12-02 04:34:38 +01:00
|
|
|
|
2005-06-03 22:19:34 +02:00
|
|
|
SyscallReturn
|
|
|
|
truncateFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
|
|
|
|
{
|
|
|
|
string path;
|
|
|
|
|
|
|
|
if (xc->mem->readString(path, xc->getSyscallArg(0)) != No_Fault)
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
off_t length = xc->getSyscallArg(1);
|
|
|
|
|
|
|
|
int result = truncate(path.c_str(), length);
|
|
|
|
return (result == -1) ? -errno : result;
|
|
|
|
}
|
|
|
|
|
|
|
|
SyscallReturn
|
|
|
|
ftruncateFunc(SyscallDesc *desc, int num, Process *process, ExecContext *xc)
|
|
|
|
{
|
|
|
|
int fd = process->sim_fd(xc->getSyscallArg(0));
|
|
|
|
|
|
|
|
if (fd < 0)
|
|
|
|
return -EBADF;
|
|
|
|
|
|
|
|
off_t length = xc->getSyscallArg(1);
|
|
|
|
|
|
|
|
int result = ftruncate(fd, length);
|
|
|
|
return (result == -1) ? -errno : result;
|
|
|
|
}
|
2005-11-22 18:08:08 +01:00
|
|
|
|
|
|
|
SyscallReturn
|
|
|
|
chownFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
|
|
|
|
{
|
|
|
|
string path;
|
|
|
|
|
|
|
|
if (xc->mem->readString(path, xc->getSyscallArg(0)) != No_Fault)
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
/* XXX endianess */
|
|
|
|
uint32_t owner = xc->getSyscallArg(1);
|
|
|
|
uid_t hostOwner = owner;
|
|
|
|
uint32_t group = xc->getSyscallArg(2);
|
|
|
|
gid_t hostGroup = group;
|
|
|
|
|
|
|
|
int result = chown(path.c_str(), hostOwner, hostGroup);
|
|
|
|
return (result == -1) ? -errno : result;
|
|
|
|
}
|
|
|
|
|
|
|
|
SyscallReturn
|
|
|
|
fchownFunc(SyscallDesc *desc, int num, Process *process, ExecContext *xc)
|
|
|
|
{
|
|
|
|
int fd = process->sim_fd(xc->getSyscallArg(0));
|
|
|
|
|
|
|
|
if (fd < 0)
|
|
|
|
return -EBADF;
|
|
|
|
|
|
|
|
/* XXX endianess */
|
|
|
|
uint32_t owner = xc->getSyscallArg(1);
|
|
|
|
uid_t hostOwner = owner;
|
|
|
|
uint32_t group = xc->getSyscallArg(2);
|
|
|
|
gid_t hostGroup = group;
|
|
|
|
|
|
|
|
int result = fchown(fd, hostOwner, hostGroup);
|
|
|
|
return (result == -1) ? -errno : result;
|
|
|
|
}
|