sim: Add a returnInto function to the SyscallDesc class.
[gem5.git] / src / sim / fd_entry.hh
1 /*
2 * Copyright (c) 2016 Advanced Micro Devices, Inc.
3 * All rights reserved.
4 *
5 * For use for simulation and test purposes only
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright notice,
14 * this list of conditions and the following disclaimer in the documentation
15 * and/or other materials provided with the distribution.
16 *
17 * 3. Neither the name of the copyright holder nor the names of its
18 * contributors may be used to endorse or promote products derived from this
19 * software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #ifndef __FD_ENTRY_HH__
35 #define __FD_ENTRY_HH__
36
37 #include <memory>
38 #include <ostream>
39 #include <string>
40
41 #include "sim/serialize.hh"
42
43 class EmulatedDriver;
44
45 /**
46 * Holds a single file descriptor mapping and that mapping's data for
47 * processes running in syscall emulation mode.
48 */
49 class FDEntry : public Serializable
50 {
51 public:
52 FDEntry(bool close_on_exec = false)
53 : _closeOnExec(close_on_exec)
54 { }
55
56 virtual std::shared_ptr<FDEntry> clone() const = 0;
57
58 bool getCOE() const { return _closeOnExec; }
59
60 void setCOE(bool close_on_exec) { _closeOnExec = close_on_exec; }
61
62 virtual void serialize(CheckpointOut &cp) const;
63 virtual void unserialize(CheckpointIn &cp);
64
65 protected:
66 bool _closeOnExec;
67 };
68
69 /**
70 * Extends the base class to include a host-backed file descriptor field
71 * that records the integer used to represent the file descriptor on the host
72 * and the file's flags.
73 */
74 class HBFDEntry: public FDEntry
75 {
76 public:
77 HBFDEntry(int flags, int sim_fd, bool close_on_exec = false)
78 : FDEntry(close_on_exec), _flags(flags), _simFD(sim_fd)
79 { }
80
81 HBFDEntry(HBFDEntry const& reg, bool close_on_exec = false)
82 : FDEntry(close_on_exec), _flags(reg._flags), _simFD(reg._simFD)
83 { }
84
85 std::shared_ptr<FDEntry>
86 clone() const override
87 {
88 return std::make_shared<HBFDEntry>(*this);
89 }
90
91 int getFlags() const { return _flags; }
92 int getSimFD() const { return _simFD; }
93
94 void setFlags(int flags) { _flags = flags; }
95 void setSimFD(int sim_fd) { _simFD = sim_fd; }
96
97 protected:
98 int _flags;
99 int _simFD;
100 };
101
102 /**
103 * Holds file descriptors for host-backed files; host-backed files are
104 * files which were opened on the physical machine where the simulation
105 * is running (probably the thing on/under your desk). All regular files
106 * are redirected to make it appear that the file descriptor assignment
107 * starts at file descriptor '3' (not including stdin, stdout, stderr) and
108 * then grows upward.
109 */
110 class FileFDEntry: public HBFDEntry
111 {
112 public:
113 FileFDEntry(int sim_fd, int flags, std::string const& file_name,
114 uint64_t file_offset, bool close_on_exec = false)
115 : HBFDEntry(flags, sim_fd, close_on_exec),
116 _fileName(file_name), _fileOffset(file_offset)
117 { }
118
119 FileFDEntry(FileFDEntry const& reg, bool close_on_exec = false)
120 : HBFDEntry(reg._flags, reg._simFD, close_on_exec),
121 _fileName(reg._fileName), _fileOffset(reg._fileOffset)
122 { }
123
124 std::shared_ptr<FDEntry>
125 clone() const override
126 {
127 return std::make_shared<FileFDEntry>(*this);
128 }
129
130 std::string const& getFileName() const { return _fileName; }
131 uint64_t getFileOffset() const { return _fileOffset; }
132
133 void setFileName(std::string const& file_name) { _fileName = file_name; }
134 void setFileOffset(uint64_t f_off) { _fileOffset = f_off; }
135
136 void serialize(CheckpointOut &cp) const override;
137 void unserialize(CheckpointIn &cp) override;
138
139 private:
140 std::string _fileName;
141 uint64_t _fileOffset;
142 };
143
144 /**
145 * Holds the metadata needed to maintain the mappings for file descriptors
146 * allocated with the pipe() system calls and its variants.
147 */
148 class PipeFDEntry: public HBFDEntry
149 {
150 public:
151 enum EndType {
152 read = 0,
153 write = 1
154 };
155
156 PipeFDEntry(int sim_fd, int flags, EndType pipe_end_type,
157 bool close_on_exec = false)
158 : HBFDEntry(flags, sim_fd, close_on_exec), _pipeReadSource(-1),
159 _pipeEndType(pipe_end_type)
160 { }
161
162 PipeFDEntry(PipeFDEntry const& pipe, bool close_on_exec = false)
163 : HBFDEntry(pipe._flags, pipe._simFD, close_on_exec),
164 _pipeReadSource(pipe._pipeReadSource),
165 _pipeEndType(pipe._pipeEndType)
166 { }
167
168 std::shared_ptr<FDEntry>
169 clone() const override
170 {
171 return std::make_shared<PipeFDEntry>(*this);
172 }
173
174 EndType getEndType() const { return _pipeEndType; }
175 int getPipeReadSource() const { return _pipeReadSource; }
176
177 void setPipeReadSource(int tgt_fd) { _pipeReadSource = tgt_fd; }
178 void setEndType(EndType type) { _pipeEndType = type; }
179
180 void serialize(CheckpointOut &cp) const override;
181 void unserialize(CheckpointIn &cp) override;
182
183 private:
184 int _pipeReadSource;
185 EndType _pipeEndType;
186 };
187
188 /**
189 * Holds file descriptors needed to simulate devices opened with pseudo
190 * files (commonly with calls to ioctls).
191 */
192 class DeviceFDEntry : public FDEntry
193 {
194 public:
195 DeviceFDEntry(EmulatedDriver *driver, std::string const& file_name,
196 bool close_on_exec = false)
197 : FDEntry(close_on_exec), _driver(driver), _fileName(file_name)
198 { }
199
200 DeviceFDEntry(DeviceFDEntry const& dev, bool close_on_exec = false)
201 : FDEntry(close_on_exec), _driver(dev._driver),
202 _fileName(dev._fileName)
203 { }
204
205 std::shared_ptr<FDEntry>
206 clone() const override
207 {
208 return std::make_shared<DeviceFDEntry>(*this);
209 }
210
211 EmulatedDriver *getDriver() const { return _driver; }
212 std::string const& getFileName() const { return _fileName; }
213
214 void serialize(CheckpointOut &cp) const override;
215 void unserialize(CheckpointIn &cp) override;
216
217 private:
218 EmulatedDriver *_driver;
219 std::string _fileName;
220 };
221
222 class SocketFDEntry: public HBFDEntry
223 {
224 public:
225 SocketFDEntry(int sim_fd, int domain, int type, int protocol,
226 bool close_on_exec = false)
227 : HBFDEntry(0, sim_fd, close_on_exec),
228 _domain(domain), _type(type), _protocol(protocol)
229 { }
230
231 SocketFDEntry(SocketFDEntry const& reg, bool close_on_exec = false)
232 : HBFDEntry(reg._flags, reg._simFD, close_on_exec),
233 _domain(reg._domain), _type(reg._type), _protocol(reg._protocol)
234 { }
235
236 std::shared_ptr<FDEntry>
237 clone() const override
238 {
239 return std::make_shared<SocketFDEntry>(*this);
240 }
241
242 int _domain;
243 int _type;
244 int _protocol;
245 };
246
247 #endif // __FD_ENTRY_HH__