tests: arch-power: Add 64-bit hello binaries
[gem5.git] / src / sim / process.hh
1 /*
2 * Copyright (c) 2014-2016 Advanced Micro Devices, Inc.
3 * Copyright (c) 2001-2005 The Regents of The University of Michigan
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met: redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer;
10 * redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution;
13 * neither the name of the copyright holders nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #ifndef __PROCESS_HH__
31 #define __PROCESS_HH__
32
33 #include <inttypes.h>
34
35 #include <map>
36 #include <memory>
37 #include <string>
38 #include <vector>
39
40 #include "arch/registers.hh"
41 #include "base/loader/memory_image.hh"
42 #include "base/statistics.hh"
43 #include "base/types.hh"
44 #include "config/the_isa.hh"
45 #include "mem/se_translating_port_proxy.hh"
46 #include "sim/fd_array.hh"
47 #include "sim/fd_entry.hh"
48 #include "sim/mem_state.hh"
49 #include "sim/sim_object.hh"
50
51 namespace Loader
52 {
53 class ObjectFile;
54 } // namespace Loader
55
56 struct ProcessParams;
57
58 class EmulatedDriver;
59 class EmulationPageTable;
60 class SyscallDesc;
61 class SyscallReturn;
62 class System;
63 class ThreadContext;
64
65 class Process : public SimObject
66 {
67 public:
68 Process(const ProcessParams &params, EmulationPageTable *pTable,
69 ::Loader::ObjectFile *obj_file);
70
71 void serialize(CheckpointOut &cp) const override;
72 void unserialize(CheckpointIn &cp) override;
73
74 void init() override;
75 void initState() override;
76 DrainState drain() override;
77
78 virtual void syscall(ThreadContext *tc) { numSyscalls++; }
79
80 inline uint64_t uid() { return _uid; }
81 inline uint64_t euid() { return _euid; }
82 inline uint64_t gid() { return _gid; }
83 inline uint64_t egid() { return _egid; }
84 inline uint64_t pid() { return _pid; }
85 inline uint64_t ppid() { return _ppid; }
86 inline uint64_t pgid() { return _pgid; }
87 inline void pgid(uint64_t pgid) { _pgid = pgid; }
88 inline uint64_t tgid() { return _tgid; }
89
90 const char *progName() const { return executable.c_str(); }
91
92 /**
93 * Find an emulated device driver.
94 *
95 * @param filename Name of the device (under /dev)
96 * @return Pointer to driver object if found, else nullptr
97 */
98 EmulatedDriver *findDriver(std::string filename);
99
100 // This function acts as a callback to update the bias value in
101 // the object file because the parameters needed to calculate the
102 // bias are not available when the object file is created.
103 void updateBias();
104 Addr getBias();
105 Addr getStartPC();
106 ::Loader::ObjectFile *getInterpreter();
107
108 void allocateMem(Addr vaddr, int64_t size, bool clobber = false);
109
110 /// Attempt to fix up a fault at vaddr by allocating a page on the stack.
111 /// @return Whether the fault has been fixed.
112 bool fixupFault(Addr vaddr);
113
114 // After getting registered with system object, tell process which
115 // system-wide context id it is assigned.
116 void
117 assignThreadContext(ContextID context_id)
118 {
119 contextIds.push_back(context_id);
120 }
121
122 /**
123 * After delegating a thread context to a child process
124 * no longer should relate to the ThreadContext
125 */
126 void revokeThreadContext(int context_id);
127
128 /**
129 * Does mmap region grow upward or downward from mmapEnd? Most
130 * platforms grow downward, but a few (such as Alpha) grow upward
131 * instead, so they can override this method to return false.
132 */
133 virtual bool mmapGrowsDown() const { return true; }
134
135 /**
136 * Maps a contiguous range of virtual addresses in this process's
137 * address space to a contiguous range of physical addresses.
138 * This function exists primarily to expose the map operation to
139 * python, so that configuration scripts can set up mappings in SE mode.
140 *
141 * @param vaddr The starting virtual address of the range.
142 * @param paddr The starting physical address of the range.
143 * @param size The length of the range in bytes.
144 * @param cacheable Specifies whether accesses are cacheable.
145 * @return True if the map operation was successful. (At this
146 * point in time, the map operation always succeeds.)
147 */
148 bool map(Addr vaddr, Addr paddr, int size, bool cacheable = true);
149
150 void replicatePage(Addr vaddr, Addr new_paddr, ThreadContext *old_tc,
151 ThreadContext *new_tc, bool alloc_page);
152
153 virtual void clone(ThreadContext *old_tc, ThreadContext *new_tc,
154 Process *new_p, RegVal flags);
155
156 // thread contexts associated with this process
157 std::vector<ContextID> contextIds;
158
159 // system object which owns this process
160 System *system;
161
162 // flag for using architecture specific page table
163 bool useArchPT;
164 // running KVM requires special initialization
165 bool kvmInSE;
166 // flag for using the process as a thread which shares page tables
167 bool useForClone;
168
169 EmulationPageTable *pTable;
170
171 // Memory proxy for initial image load.
172 std::unique_ptr<SETranslatingPortProxy> initVirtMem;
173
174 /**
175 * Each instance of a Loader subclass will have a chance to try to load
176 * an object file when tryLoaders is called. If they can't because they
177 * aren't compatible with it (wrong arch, wrong OS, etc), then they
178 * silently fail by returning nullptr so other loaders can try.
179 */
180 class Loader
181 {
182 public:
183 Loader();
184
185 /* Loader instances are singletons. */
186 Loader(const Loader &) = delete;
187 void operator=(const Loader &) = delete;
188
189 virtual ~Loader() {}
190
191 /**
192 * Each subclass needs to implement this method. If the loader is
193 * compatible with the passed in object file, it should return the
194 * created Process object corresponding to it. If not, it should fail
195 * silently and return nullptr. If there's a non-compatibliity related
196 * error like file IO errors, etc., those should fail non-silently
197 * with a panic or fail as normal.
198 */
199 virtual Process *load(const ProcessParams &params,
200 ::Loader::ObjectFile *obj_file) = 0;
201 };
202
203 // Try all the Loader instance's "load" methods one by one until one is
204 // successful. If none are, complain and fail.
205 static Process *tryLoaders(const ProcessParams &params,
206 ::Loader::ObjectFile *obj_file);
207
208 ::Loader::ObjectFile *objFile;
209 ::Loader::MemoryImage image;
210 ::Loader::MemoryImage interpImage;
211 std::vector<std::string> argv;
212 std::vector<std::string> envp;
213 std::string executable;
214
215 /**
216 * Return an absolute path given a relative path paired with the current
217 * working directory of the process running under simulation.
218 *
219 * @param path The relative path (generally a filename) that needs the
220 * current working directory prepended.
221 * @param host_fs A flag which determines whether to return a
222 * path for the host filesystem or the filesystem of the process running
223 * under simulation. Only matters if filesysem redirection is used to
224 * replace files (or directories) that would normally appear via the
225 * host filesystem.
226 * @return String containing an absolute path.
227 */
228 std::string absolutePath(const std::string &path, bool host_fs);
229
230 /**
231 * Redirect file path if it matches any keys initialized by system object.
232 * @param filename An input parameter containing either a relative path
233 * or an absolute path. If given a relative path, the path will be
234 * prepended to the current working directory of the simulation with
235 * respect to the host filesystem.
236 * @return String containing an absolute path.
237 */
238 std::string checkPathRedirect(const std::string &filename);
239
240 /**
241 * The cwd members are used to track changes to the current working
242 * directory for the purpose of executing system calls which depend on
243 * relative paths (i.e. open, chdir).
244 *
245 * The tgt member and host member may differ if the path for the current
246 * working directory is redirected to point to a different location
247 * (i.e. `cd /proc` should point to '$(gem5_repo)/m5out/fs/proc'
248 * instead of '/proc').
249 */
250 std::string tgtCwd;
251 std::string hostCwd;
252
253 // Syscall emulation uname release.
254 std::string release;
255
256 // Id of the owner of the process
257 uint64_t _uid;
258 uint64_t _euid;
259 uint64_t _gid;
260 uint64_t _egid;
261
262 // pid of the process and it's parent
263 uint64_t _pid;
264 uint64_t _ppid;
265 uint64_t _pgid;
266 uint64_t _tgid;
267
268 // Emulated drivers available to this process
269 std::vector<EmulatedDriver *> drivers;
270
271 std::shared_ptr<FDArray> fds;
272
273 bool *exitGroup;
274 std::shared_ptr<MemState> memState;
275
276 /**
277 * Calls a futex wakeup at the address specified by this pointer when
278 * this process exits.
279 */
280 uint64_t childClearTID;
281
282 // Process was forked with SIGCHLD set.
283 bool *sigchld;
284
285 Stats::Scalar numSyscalls; // track how many system calls are executed
286 };
287
288 #endif // __PROCESS_HH__