2 * Copyright (c) 2016 Advanced Micro Devices, Inc.
5 * For use for simulation and test purposes only
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
10 * 1. Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer.
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.
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.
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.
33 * Authors: Brandon Potter
36 #ifndef __FD_ENTRY_HH__
37 #define __FD_ENTRY_HH__
43 #include "sim/serialize.hh"
48 * Holds a single file descriptor mapping and that mapping's data for
49 * processes running in syscall emulation mode.
51 class FDEntry : public Serializable
54 FDEntry(bool close_on_exec = false)
55 : _closeOnExec(close_on_exec)
58 virtual std::shared_ptr<FDEntry> clone() const = 0;
60 bool getCOE() const { return _closeOnExec; }
62 void setCOE(bool close_on_exec) { _closeOnExec = close_on_exec; }
64 virtual void serialize(CheckpointOut &cp) const;
65 virtual void unserialize(CheckpointIn &cp);
72 * Extends the base class to include a host-backed file descriptor field
73 * that records the integer used to represent the file descriptor on the host
74 * and the file's flags.
76 class HBFDEntry: public FDEntry
79 HBFDEntry(int flags, int sim_fd, bool close_on_exec = false)
80 : FDEntry(close_on_exec), _flags(flags), _simFD(sim_fd)
83 HBFDEntry(HBFDEntry const& reg, bool close_on_exec = false)
84 : FDEntry(close_on_exec), _flags(reg._flags), _simFD(reg._simFD)
87 std::shared_ptr<FDEntry>
88 clone() const override
90 return std::make_shared<HBFDEntry>(*this);
93 int getFlags() const { return _flags; }
94 int getSimFD() const { return _simFD; }
96 void setFlags(int flags) { _flags = flags; }
97 void setSimFD(int sim_fd) { _simFD = sim_fd; }
105 * Holds file descriptors for host-backed files; host-backed files are
106 * files which were opened on the physical machine where the simulation
107 * is running (probably the thing on/under your desk). All regular files
108 * are redirected to make it appear that the file descriptor assignment
109 * starts at file descriptor '3' (not including stdin, stdout, stderr) and
112 class FileFDEntry: public HBFDEntry
115 FileFDEntry(int sim_fd, int flags, std::string const& file_name,
116 uint64_t file_offset, bool close_on_exec = false)
117 : HBFDEntry(flags, sim_fd, close_on_exec),
118 _fileName(file_name), _fileOffset(file_offset)
121 FileFDEntry(FileFDEntry const& reg, bool close_on_exec = false)
122 : HBFDEntry(reg._flags, reg._simFD, close_on_exec),
123 _fileName(reg._fileName), _fileOffset(reg._fileOffset)
126 std::shared_ptr<FDEntry>
127 clone() const override
129 return std::make_shared<FileFDEntry>(*this);
132 std::string const& getFileName() const { return _fileName; }
133 uint64_t getFileOffset() const { return _fileOffset; }
135 void setFileName(std::string const& file_name) { _fileName = file_name; }
136 void setFileOffset(uint64_t f_off) { _fileOffset = f_off; }
138 void serialize(CheckpointOut &cp) const override;
139 void unserialize(CheckpointIn &cp) override;
142 std::string _fileName;
143 uint64_t _fileOffset;
147 * Holds the metadata needed to maintain the mappings for file descriptors
148 * allocated with the pipe() system calls and its variants.
150 class PipeFDEntry: public HBFDEntry
158 PipeFDEntry(int sim_fd, int flags, EndType pipe_end_type,
159 bool close_on_exec = false)
160 : HBFDEntry(flags, sim_fd, close_on_exec), _pipeReadSource(-1),
161 _pipeEndType(pipe_end_type)
164 PipeFDEntry(PipeFDEntry const& pipe, bool close_on_exec = false)
165 : HBFDEntry(pipe._flags, pipe._simFD, close_on_exec),
166 _pipeReadSource(pipe._pipeReadSource),
167 _pipeEndType(pipe._pipeEndType)
170 std::shared_ptr<FDEntry>
171 clone() const override
173 return std::make_shared<PipeFDEntry>(*this);
176 EndType getEndType() const { return _pipeEndType; }
177 int getPipeReadSource() const { return _pipeReadSource; }
179 void setPipeReadSource(int tgt_fd) { _pipeReadSource = tgt_fd; }
180 void setEndType(EndType type) { _pipeEndType = type; }
182 void serialize(CheckpointOut &cp) const override;
183 void unserialize(CheckpointIn &cp) override;
187 EndType _pipeEndType;
191 * Holds file descriptors needed to simulate devices opened with pseudo
192 * files (commonly with calls to ioctls).
194 class DeviceFDEntry : public FDEntry
197 DeviceFDEntry(EmulatedDriver *driver, std::string const& file_name,
198 bool close_on_exec = false)
199 : FDEntry(close_on_exec), _driver(driver), _fileName(file_name)
202 DeviceFDEntry(DeviceFDEntry const& dev, bool close_on_exec = false)
203 : FDEntry(close_on_exec), _driver(dev._driver),
204 _fileName(dev._fileName)
207 std::shared_ptr<FDEntry>
208 clone() const override
210 return std::make_shared<DeviceFDEntry>(*this);
213 EmulatedDriver *getDriver() const { return _driver; }
214 std::string const& getFileName() const { return _fileName; }
216 void serialize(CheckpointOut &cp) const override;
217 void unserialize(CheckpointIn &cp) override;
220 EmulatedDriver *_driver;
221 std::string _fileName;
224 class SocketFDEntry: public HBFDEntry
227 SocketFDEntry(int sim_fd, int domain, int type, int protocol,
228 bool close_on_exec = false)
229 : HBFDEntry(0, sim_fd, close_on_exec),
230 _domain(domain), _type(type), _protocol(protocol)
233 SocketFDEntry(SocketFDEntry const& reg, bool close_on_exec = false)
234 : HBFDEntry(reg._flags, reg._simFD, close_on_exec),
235 _domain(reg._domain), _type(reg._type), _protocol(reg._protocol)
238 std::shared_ptr<FDEntry>
239 clone() const override
241 return std::make_shared<SocketFDEntry>(*this);
249 #endif // __FD_ENTRY_HH__