syscall_emul: move mmapGrowsDown() to LiveProcess
[gem5.git] / src / sim / process.hh
1 /*
2 * Copyright (c) 2014 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 * Authors: Nathan Binkert
30 * Steve Reinhardt
31 */
32
33 #ifndef __PROCESS_HH__
34 #define __PROCESS_HH__
35
36 #include <array>
37 #include <string>
38 #include <vector>
39
40 #include "arch/registers.hh"
41 #include "base/statistics.hh"
42 #include "base/types.hh"
43 #include "config/the_isa.hh"
44 #include "mem/se_translating_port_proxy.hh"
45 #include "sim/fd_entry.hh"
46 #include "sim/sim_object.hh"
47 #include "sim/syscallreturn.hh"
48
49 class PageTable;
50 struct ProcessParams;
51 struct LiveProcessParams;
52 class SyscallDesc;
53 class System;
54 class ThreadContext;
55 class EmulatedDriver;
56
57 template<class IntType>
58 struct AuxVector
59 {
60 IntType a_type;
61 IntType a_val;
62
63 AuxVector()
64 {}
65
66 AuxVector(IntType type, IntType val);
67 };
68
69 class Process : public SimObject
70 {
71 public:
72
73 /// Pointer to object representing the system this process is
74 /// running on.
75 System *system;
76
77 // thread contexts associated with this process
78 std::vector<ContextID> contextIds;
79
80 // number of CPUs (esxec contexts, really) assigned to this process.
81 unsigned int numCpus() { return contextIds.size(); }
82
83 // record of blocked context
84 struct WaitRec
85 {
86 Addr waitChan;
87 ThreadContext *waitingContext;
88
89 WaitRec(Addr chan, ThreadContext *ctx)
90 : waitChan(chan), waitingContext(ctx)
91 { }
92 };
93
94 // list of all blocked contexts
95 std::list<WaitRec> waitList;
96
97 Addr brk_point; // top of the data segment
98
99 Addr stack_base; // stack segment base (highest address)
100 unsigned stack_size; // initial stack size
101 Addr stack_min; // lowest address accessed on the stack
102
103 // The maximum size allowed for the stack.
104 Addr max_stack_size;
105
106 // addr to use for next stack region (for multithreaded apps)
107 Addr next_thread_stack_base;
108
109 // Base of region for mmaps (when user doesn't specify an address).
110 Addr mmap_end;
111
112 // Does mmap region grow upward or downward from mmap_end? Most
113 // platforms grow downward, but a few (such as Alpha) grow upward
114 // instead, so they can override thie method to return false.
115 virtual bool mmapGrowsDown() const { return true; }
116
117 // Base of region for nxm data
118 Addr nxm_start;
119 Addr nxm_end;
120
121 Stats::Scalar num_syscalls; // number of syscalls executed
122
123 protected:
124 // constructor
125 Process(ProcessParams *params);
126
127 void initState() override;
128
129 DrainState drain() override;
130
131 public:
132
133 //This id is assigned by m5 and is used to keep process' tlb entries
134 //separated.
135 uint64_t M5_pid;
136
137 // flag for using architecture specific page table
138 bool useArchPT;
139 // running KvmCPU in SE mode requires special initialization
140 bool kvmInSE;
141
142 PageTableBase* pTable;
143
144 protected:
145 /// Memory proxy for initialization (image loading)
146 SETranslatingPortProxy initVirtMem;
147
148 private:
149 static const int NUM_FDS = 1024;
150
151 // File descriptor remapping support.
152 std::shared_ptr<std::array<FDEntry, NUM_FDS>> fd_array;
153
154 // Standard file descriptor options for initialization and checkpoints.
155 std::map<std::string, int> imap;
156 std::map<std::string, int> oemap;
157
158 public:
159 // inherit file descriptor map from another process (necessary for clone)
160 void inheritFDArray(Process *p);
161
162 // override of virtual SimObject method: register statistics
163 void regStats() override;
164
165 // After getting registered with system object, tell process which
166 // system-wide context id it is assigned.
167 void assignThreadContext(ContextID context_id)
168 {
169 contextIds.push_back(context_id);
170 }
171
172 // Find a free context to use
173 ThreadContext *findFreeContext();
174
175 // provide program name for debug messages
176 virtual const char *progName() const { return "<unknown>"; }
177
178 // generate new target fd for sim_fd
179 int allocFD(int sim_fd, const std::string& filename, int flags, int mode,
180 bool pipe);
181
182 // disassociate target fd with simulator fd and cleanup subsidiary fields
183 void resetFDEntry(int tgt_fd);
184
185 // look up simulator fd for given target fd
186 int getSimFD(int tgt_fd);
187
188 // look up fd entry for a given target fd
189 FDEntry *getFDEntry(int tgt_fd);
190
191 // look up target fd for given host fd
192 // Assumes a 1:1 mapping between target file descriptor and host file
193 // descriptor. Given the current API, this must be true given that it's
194 // not possible to map multiple target file descriptors to the same host
195 // file descriptor
196 int getTgtFD(int sim_fd);
197
198 // fix all offsets for currently open files and save them
199 void fixFileOffsets();
200
201 // find all offsets for currently open files and save them
202 void findFileOffsets();
203
204 // set the source of this read pipe for a checkpoint resume
205 void setReadPipeSource(int read_pipe_fd, int source_fd);
206
207 virtual void syscall(int64_t callnum, ThreadContext *tc) = 0;
208
209 void allocateMem(Addr vaddr, int64_t size, bool clobber = false);
210
211 /// Attempt to fix up a fault at vaddr by allocating a page on the stack.
212 /// @return Whether the fault has been fixed.
213 bool fixupStackFault(Addr vaddr);
214
215 /**
216 * Maps a contiguous range of virtual addresses in this process's
217 * address space to a contiguous range of physical addresses.
218 * This function exists primarily to expose the map operation to
219 * python, so that configuration scripts can set up mappings in SE mode.
220 *
221 * @param vaddr The starting virtual address of the range.
222 * @param paddr The starting physical address of the range.
223 * @param size The length of the range in bytes.
224 * @param cacheable Specifies whether accesses are cacheable.
225 * @return True if the map operation was successful. (At this
226 * point in time, the map operation always succeeds.)
227 */
228 bool map(Addr vaddr, Addr paddr, int size, bool cacheable = true);
229
230 void serialize(CheckpointOut &cp) const override;
231 void unserialize(CheckpointIn &cp) override;
232 };
233
234 //
235 // "Live" process with system calls redirected to host system
236 //
237 class ObjectFile;
238 class LiveProcess : public Process
239 {
240 protected:
241 ObjectFile *objFile;
242 std::vector<std::string> argv;
243 std::vector<std::string> envp;
244 std::string cwd;
245 std::string executable;
246
247 LiveProcess(LiveProcessParams *params, ObjectFile *objFile);
248
249 // Id of the owner of the process
250 uint64_t __uid;
251 uint64_t __euid;
252 uint64_t __gid;
253 uint64_t __egid;
254
255 // pid of the process and it's parent
256 uint64_t __pid;
257 uint64_t __ppid;
258
259 // Emulated drivers available to this process
260 std::vector<EmulatedDriver *> drivers;
261
262 public:
263
264 enum AuxiliaryVectorType {
265 M5_AT_NULL = 0,
266 M5_AT_IGNORE = 1,
267 M5_AT_EXECFD = 2,
268 M5_AT_PHDR = 3,
269 M5_AT_PHENT = 4,
270 M5_AT_PHNUM = 5,
271 M5_AT_PAGESZ = 6,
272 M5_AT_BASE = 7,
273 M5_AT_FLAGS = 8,
274 M5_AT_ENTRY = 9,
275 M5_AT_NOTELF = 10,
276 M5_AT_UID = 11,
277 M5_AT_EUID = 12,
278 M5_AT_GID = 13,
279 M5_AT_EGID = 14,
280 // The following may be specific to Linux
281 M5_AT_PLATFORM = 15,
282 M5_AT_HWCAP = 16,
283 M5_AT_CLKTCK = 17,
284
285 M5_AT_SECURE = 23,
286 M5_BASE_PLATFORM = 24,
287 M5_AT_RANDOM = 25,
288
289 M5_AT_EXECFN = 31,
290
291 M5_AT_VECTOR_SIZE = 44
292 };
293
294 inline uint64_t uid() {return __uid;}
295 inline uint64_t euid() {return __euid;}
296 inline uint64_t gid() {return __gid;}
297 inline uint64_t egid() {return __egid;}
298 inline uint64_t pid() {return __pid;}
299 inline uint64_t ppid() {return __ppid;}
300
301 // provide program name for debug messages
302 virtual const char *progName() const { return executable.c_str(); }
303
304 std::string
305 fullPath(const std::string &filename)
306 {
307 if (filename[0] == '/' || cwd.empty())
308 return filename;
309
310 std::string full = cwd;
311
312 if (cwd[cwd.size() - 1] != '/')
313 full += '/';
314
315 return full + filename;
316 }
317
318 std::string getcwd() const { return cwd; }
319
320 virtual void syscall(int64_t callnum, ThreadContext *tc);
321
322 virtual TheISA::IntReg getSyscallArg(ThreadContext *tc, int &i) = 0;
323 virtual TheISA::IntReg getSyscallArg(ThreadContext *tc, int &i, int width);
324 virtual void setSyscallArg(ThreadContext *tc,
325 int i, TheISA::IntReg val) = 0;
326 virtual void setSyscallReturn(ThreadContext *tc,
327 SyscallReturn return_value) = 0;
328
329 virtual SyscallDesc *getDesc(int callnum) = 0;
330
331 /**
332 * Find an emulated device driver.
333 *
334 * @param filename Name of the device (under /dev)
335 * @return Pointer to driver object if found, else NULL
336 */
337 EmulatedDriver *findDriver(std::string filename);
338
339 // this function is used to create the LiveProcess object, since
340 // we can't tell which subclass of LiveProcess to use until we
341 // open and look at the object file.
342 static LiveProcess *create(LiveProcessParams *params);
343 };
344
345
346 #endif // __PROCESS_HH__