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