fd_entry.hh (11800:54436a1784dc) fd_entry.hh (11856:103e2f92c965)
1/*
1/*
2 * Copyright (c) 2015 Advanced Micro Devices, Inc.
3 * Copyright (c) 2001-2005 The Regents of The University of Michigan
2 * Copyright (c) 2016 Advanced Micro Devices, Inc.
3 * All rights reserved.
4 *
4 *
5 * For use for simulation and test purposes only
6 *
5 * Redistribution and use in source and binary forms, with or without
7 * 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.
8 * modification, are permitted provided that the following conditions are met:
15 *
9 *
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.
10 * 1. Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer.
27 *
12 *
28 * Authors: Nathan Binkert
29 * Steve Reinhardt
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 * Author: Brandon Potter
30 */
31
32#ifndef __FD_ENTRY_HH__
33#define __FD_ENTRY_HH__
34
34 */
35
36#ifndef __FD_ENTRY_HH__
37#define __FD_ENTRY_HH__
38
39#include <memory>
35#include <ostream>
36#include <string>
37
38#include "sim/serialize.hh"
39
40class EmulatedDriver;
41
42/**
40#include <ostream>
41#include <string>
42
43#include "sim/serialize.hh"
44
45class EmulatedDriver;
46
47/**
43 * FDEntry is used to manage a single file descriptor mapping and metadata
44 * for processes.
45 * Note that the fields are all declared publicly since system calls have a
46 * habit of only needing to access a single field at a time and accessor
47 * methods seem like overkill.
48 * Holds a single file descriptor mapping and that mapping's data for
49 * processes running in syscall emulation mode.
48 */
49class FDEntry : public Serializable
50{
51 public:
50 */
51class FDEntry : public Serializable
52{
53 public:
52 /**
53 * Constructor contains default values
54 * The file descriptor is free.
55 */
56 FDEntry()
57 : fd(-1), mode(0), flags(0), isPipe(false), readPipeSource(0),
58 fileOffset(0), filename(""), driver(NULL)
54 FDEntry(bool close_on_exec = false)
55 : _closeOnExec(close_on_exec)
59 { }
60
56 { }
57
58 virtual std::shared_ptr<FDEntry> clone() const = 0;
59
60 inline bool getCOE() const { return _closeOnExec; }
61
62 inline void setCOE(bool close_on_exec) { _closeOnExec = close_on_exec; }
63
64 virtual void serialize(CheckpointOut &cp) const;
65 virtual void unserialize(CheckpointIn &cp);
66
67 protected:
68 bool _closeOnExec;
69};
70
71/**
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.
75 */
76class HBFDEntry: public FDEntry
77{
78 public:
79 HBFDEntry(int flags, int sim_fd, bool close_on_exec = false)
80 : FDEntry(close_on_exec), _flags(flags), _simFD(sim_fd)
81 { }
82
83 inline int getFlags() const { return _flags; }
84 inline int getSimFD() const { return _simFD; }
85
86 inline void setFlags(int flags) { _flags = flags; }
87 inline void setSimFD(int sim_fd) { _simFD = sim_fd; }
88
89 protected:
90 int _flags;
91 int _simFD;
92};
93
94/**
95 * Holds file descriptors for host-backed files; host-backed files are
96 * files which were opened on the physical machine where the simulation
97 * is running (probably the thing on/under your desk). All regular files
98 * are redirected to make it appear that the file descriptor assignment
99 * starts at file descriptor '3' (not including stdin, stdout, stderr) and
100 * then grows upward.
101 */
102class FileFDEntry: public HBFDEntry
103{
104 public:
105 FileFDEntry(int sim_fd, int flags, std::string const& file_name,
106 uint64_t file_offset, bool close_on_exec = false)
107 : HBFDEntry(flags, sim_fd, close_on_exec),
108 _fileName(file_name), _fileOffset(file_offset)
109 { }
110
111 FileFDEntry(FileFDEntry const& reg, bool close_on_exec = false)
112 : HBFDEntry(reg._flags, reg._simFD, close_on_exec),
113 _fileName(reg._fileName), _fileOffset(reg._fileOffset)
114 { }
115
116 inline std::shared_ptr<FDEntry>
117 clone() const override
118 {
119 return std::make_shared<FileFDEntry>(*this);
120 }
121
122 inline std::string getFileName() const { return _fileName; }
123 inline uint64_t getFileOffset() const { return _fileOffset; }
124
125 inline void setFileName(std::string file_name) { _fileName = file_name; }
126 inline void setFileOffset (uint64_t f_off) { _fileOffset = f_off; }
127
61 void serialize(CheckpointOut &cp) const override;
62 void unserialize(CheckpointIn &cp) override;
63
128 void serialize(CheckpointOut &cp) const override;
129 void unserialize(CheckpointIn &cp) override;
130
64 /**
65 * Check if the target file descriptor is in use.
66 * @return value denoting if target file descriptor already used
67 */
68 bool isFree();
131 private:
132 std::string _fileName;
133 uint64_t _fileOffset;
134};
69
135
70 /**
71 * Fill in members for this file descriptor entry.
72 * @param sim_fd host file descriptor
73 * @param name filename string
74 * @param flags current flags of the file descriptor
75 * @param mode current mode of the file descriptor
76 * @param pipe denotes whether the file descriptor belongs to a pipe
77 */
78 void set(int sim_fd, const std::string name, int flags, int mode,
79 bool pipe);
136/**
137 * Holds the metadata needed to maintain the mappings for file descriptors
138 * allocated with the pipe() system calls and its variants.
139 */
140class PipeFDEntry: public HBFDEntry
141{
142 public:
143 enum EndType {
144 read = 0,
145 write = 1
146 };
80
147
81 /** Reset members to their default values. */
82 void reset();
148 PipeFDEntry(int sim_fd, int flags, EndType pipe_end_type,
149 bool close_on_exec = false)
150 : HBFDEntry(flags, sim_fd, close_on_exec), _pipeReadSource(-1),
151 _pipeEndType(pipe_end_type)
152 { }
83
153
84 int fd;
85 int mode;
86 int flags;
87 bool isPipe;
88 int readPipeSource;
89 uint64_t fileOffset;
90 std::string filename;
91 EmulatedDriver *driver;
154 PipeFDEntry(PipeFDEntry const& pipe, bool close_on_exec = false)
155 : HBFDEntry(pipe._flags, pipe._simFD, close_on_exec),
156 _pipeReadSource(pipe._pipeReadSource),
157 _pipeEndType(pipe._pipeEndType)
158 { }
159
160 inline std::shared_ptr<FDEntry>
161 clone() const override
162 {
163 return std::make_shared<PipeFDEntry>(*this);
164 }
165
166 inline EndType getEndType() const { return _pipeEndType; }
167 inline int getPipeReadSource() const { return _pipeReadSource; }
168
169 inline void setPipeReadSource(int tgt_fd) { _pipeReadSource = tgt_fd; }
170 inline void setEndType(EndType type) { _pipeEndType = type; }
171
172 void serialize(CheckpointOut &cp) const override;
173 void unserialize(CheckpointIn &cp) override;
174
175 private:
176 int _pipeReadSource;
177 EndType _pipeEndType;
92};
93
178};
179
180/**
181 * Holds file descriptors needed to simulate devices opened with pseudo
182 * files (commonly with calls to ioctls).
183 */
184class DeviceFDEntry : public FDEntry
185{
186 public:
187 DeviceFDEntry(EmulatedDriver *driver, std::string const& file_name,
188 bool close_on_exec = false)
189 : FDEntry(close_on_exec), _driver(driver), _fileName(file_name)
190 { }
191
192 DeviceFDEntry(DeviceFDEntry const& dev, bool close_on_exec = false)
193 : FDEntry(close_on_exec), _driver(dev._driver),
194 _fileName(dev._fileName)
195 { }
196
197 std::shared_ptr<FDEntry>
198 clone() const override
199 {
200 return std::make_shared<DeviceFDEntry>(*this);
201 }
202
203 inline EmulatedDriver *getDriver() const { return _driver; }
204 inline std::string getFileName() const { return _fileName; }
205
206 void serialize(CheckpointOut &cp) const override;
207 void unserialize(CheckpointIn &cp) override;
208
209 private:
210 EmulatedDriver *_driver;
211 std::string _fileName;
212};
213
94#endif // __FD_ENTRY_HH__
214#endif // __FD_ENTRY_HH__