syscall_emul: [patch 10/22] refactor fdentry and add fdarray class
[gem5.git] / src / sim / fd_array.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 * Author: Brandon Potter
34 */
35
36 #ifndef __FD_ARRAY_HH__
37 #define __FD_ARRAY_HH__
38
39 #include <array>
40 #include <memory>
41 #include <string>
42
43 #include "sim/fd_entry.hh"
44
45 class FDArray
46 {
47 private:
48 static const int NUM_FDS = 1024;
49
50 public:
51 /**
52 * Initialize the file descriptor array and set the standard file
53 * descriptors to defaults or values passed in with the process
54 * params.
55 * @param input Used to initialize the stdin file descriptor
56 * @param output Used to initialize the stdout file descriptor
57 * @param errout Used to initialize the stderr file descriptor
58 */
59 FDArray(std::string const& input, std::string const& output,
60 std::string const& errout);
61
62 std::string _input;
63 std::string _output;
64 std::string _errout;
65
66 /**
67 * Figure out the file offsets for all currently open files and save them
68 * the offsets during the calls to drain by the owning process.
69 */
70 void updateFileOffsets();
71
72 /**
73 * Restore all offsets for currently open files during the unserialize
74 * phase for the owning process class.
75 */
76 void restoreFileOffsets();
77
78 /**
79 * Treat this object like a normal array in using the subscript operator
80 * to pull entries out of it.
81 * @param tgt_fd Use target file descriptors to index the array.
82 */
83 inline std::shared_ptr<FDEntry>
84 operator[](int tgt_fd)
85 {
86 return getFDEntry(tgt_fd);
87 }
88
89 /**
90 * Step through the file descriptor array and find the first available
91 * entry which is denoted as being free by being a 'nullptr'. That file
92 * descriptor entry is the new target file descriptor entry that we
93 * return as the return parameter.
94 * @param fdp Allocated beforehand and passed into this method;
95 * the fdp is meant to be a generic pointer capable of pointing to
96 * different types of file descriptors. Must cast the pointer to the
97 * correct type before dereferencing to access the needed fields.
98 */
99 int allocFD(std::shared_ptr<FDEntry> fdp);
100
101 /**
102 * Return the size of the _fdArray field
103 */
104 int getSize() const { return _fdArray.size(); }
105
106 /**
107 * Try to close the host file descriptor. If successful, set the
108 * specified file descriptor entry object pointer to nullptr.
109 * Used to "close" the target file descriptor.
110 * @param tgt_fd Use target file descriptors to index the array.
111 */
112 int closeFDEntry(int tgt_fd);
113
114 private:
115 /**
116 * Help clarify our intention when opening files in the init and
117 * restoration code. These are helper functions which are not meant to
118 * be exposed to other objects or files.
119 */
120 int openFile(std::string const& file_name, int flags, mode_t mode) const;
121 int openInputFile(std::string const& file_name) const;
122 int openOutputFile(std::string const& file_name) const;
123
124 /**
125 * Return the file descriptor entry object associated with the index
126 * provided. (The index is protected with bounds checking on the array
127 * size without the use of the array's at operator.)
128 * @param tgt_fd Use target file descriptors to index the array.
129 */
130 std::shared_ptr<FDEntry> getFDEntry(int tgt_fd);
131
132 /**
133 * Hold pointers to the file descriptor entries. The array size is
134 * statically defined by the operating system.
135 */
136 std::array<std::shared_ptr<FDEntry>, NUM_FDS> _fdArray;
137
138 /**
139 * Hold strings which represent the default values which are checked
140 * against to initialize the standard file descriptors. If the string
141 * provided doesn't hit against these maps, then a file is opened on the
142 * host instead of using the host's standard file descriptors.
143 */
144 std::map<std::string, int> imap;
145 std::map<std::string, int> oemap;
146 };
147
148 #endif // __FD_ARRAY_HH__