Hand merge
[gem5.git] / sim / syscall_emul.cc
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
29 #include <unistd.h>
30
31 #include <string>
32 #include <iostream>
33
34 #include "sim/syscall_emul.hh"
35 #include "base/trace.hh"
36 #include "cpu/exec_context.hh"
37 #include "cpu/base_cpu.hh"
38 #include "sim/process.hh"
39
40 #include "sim/sim_events.hh"
41
42 using namespace std;
43
44 void
45 SyscallDesc::doSyscall(int callnum, Process *process, ExecContext *xc)
46 {
47 DPRINTFR(SyscallVerbose, "%s: syscall %s called\n",
48 xc->cpu->name(), name);
49
50 SyscallReturn retval = (*funcPtr)(this, callnum, process, xc);
51
52 DPRINTFR(SyscallVerbose, "%s: syscall %s returns %d\n",
53 xc->cpu->name(), name, retval.value());
54
55 if (!(flags & SyscallDesc::SuppressReturnValue))
56 xc->setSyscallReturn(retval);
57 }
58
59
60 SyscallReturn
61 unimplementedFunc(SyscallDesc *desc, int callnum, Process *process,
62 ExecContext *xc)
63 {
64 cerr << "Error: syscall " << desc->name
65 << " (#" << callnum << ") unimplemented.";
66 cerr << " Args: " << xc->getSyscallArg(0) << ", " << xc->getSyscallArg(1)
67 << ", ..." << endl;
68
69 abort();
70 }
71
72
73 SyscallReturn
74 ignoreFunc(SyscallDesc *desc, int callnum, Process *process,
75 ExecContext *xc)
76 {
77 DCOUT(SyscallWarnings) << "Warning: ignoring syscall " << desc->name
78 << "(" << xc->getSyscallArg(0)
79 << ", " << xc->getSyscallArg(1)
80 << ", ...)" << endl;
81
82 return 0;
83 }
84
85
86 SyscallReturn
87 exitFunc(SyscallDesc *desc, int callnum, Process *process,
88 ExecContext *xc)
89 {
90 new SimExitEvent("syscall caused exit", xc->getSyscallArg(0) & 0xff);
91
92 return 1;
93 }
94
95
96 SyscallReturn
97 getpagesizeFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
98 {
99 return VMPageSize;
100 }
101
102
103 SyscallReturn
104 obreakFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
105 {
106 // change brk addr to first arg
107 Addr new_brk = xc->getSyscallArg(0);
108 if (new_brk != 0)
109 {
110 p->brk_point = xc->getSyscallArg(0);
111 }
112 DPRINTF(SyscallVerbose, "Break Point changed to: %#X\n", p->brk_point);
113 return p->brk_point;
114 }
115
116
117 SyscallReturn
118 closeFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
119 {
120 int fd = p->sim_fd(xc->getSyscallArg(0));
121 return close(fd);
122 }
123
124
125 SyscallReturn
126 readFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
127 {
128 int fd = p->sim_fd(xc->getSyscallArg(0));
129 int nbytes = xc->getSyscallArg(2);
130 BufferArg bufArg(xc->getSyscallArg(1), nbytes);
131
132 int bytes_read = read(fd, bufArg.bufferPtr(), nbytes);
133
134 if (bytes_read != -1)
135 bufArg.copyOut(xc->mem);
136
137 return bytes_read;
138 }
139
140 SyscallReturn
141 writeFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
142 {
143 int fd = p->sim_fd(xc->getSyscallArg(0));
144 int nbytes = xc->getSyscallArg(2);
145 BufferArg bufArg(xc->getSyscallArg(1), nbytes);
146
147 bufArg.copyIn(xc->mem);
148
149 int bytes_written = write(fd, bufArg.bufferPtr(), nbytes);
150
151 fsync(fd);
152
153 return bytes_written;
154 }
155
156
157 SyscallReturn
158 lseekFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
159 {
160 int fd = p->sim_fd(xc->getSyscallArg(0));
161 uint64_t offs = xc->getSyscallArg(1);
162 int whence = xc->getSyscallArg(2);
163
164 off_t result = lseek(fd, offs, whence);
165
166 return (result == (off_t)-1) ? -errno : result;
167 }
168
169
170 SyscallReturn
171 munmapFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
172 {
173 // given that we don't really implement mmap, munmap is really easy
174 return 0;
175 }
176
177
178 const char *hostname = "m5.eecs.umich.edu";
179
180 SyscallReturn
181 gethostnameFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
182 {
183 int name_len = xc->getSyscallArg(1);
184 BufferArg name(xc->getSyscallArg(0), name_len);
185
186 strncpy((char *)name.bufferPtr(), hostname, name_len);
187
188 name.copyOut(xc->mem);
189
190 return 0;
191 }
192
193 SyscallReturn
194 unlinkFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
195 {
196 std::string path;
197
198 if (xc->mem->readString(path, xc->getSyscallArg(0)) != No_Fault)
199 return (TheISA::IntReg)-EFAULT;
200
201 int result = unlink(path.c_str());
202 return (result == -1) ? -errno : result;
203 }
204
205 SyscallReturn
206 renameFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
207 {
208 std::string old_name;
209
210 if (xc->mem->readString(old_name, xc->getSyscallArg(0)) != No_Fault)
211 return -EFAULT;
212
213 std::string new_name;
214
215 if (xc->mem->readString(new_name, xc->getSyscallArg(1)) != No_Fault)
216 return -EFAULT;
217
218 int64_t result = rename(old_name.c_str(),new_name.c_str());
219 return (result == -1) ? -errno : result;
220 }
221