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