2 * Copyright (c) 2014 Advanced Micro Devices, Inc.
3 * Copyright (c) 2001-2005 The Regents of The University of Michigan
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.
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.
29 * Authors: Nathan Binkert
33 #ifndef __PROCESS_HH__
34 #define __PROCESS_HH__
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"
49 struct LiveProcessParams;
54 template<class IntType>
63 AuxVector(IntType type, IntType val);
66 class Process : public SimObject
70 /// Pointer to object representing the system this process is
74 // thread contexts associated with this process
75 std::vector<int> contextIds;
77 // number of CPUs (esxec contexts, really) assigned to this process.
78 unsigned int numCpus() { return contextIds.size(); }
80 // record of blocked context
84 ThreadContext *waitingContext;
86 WaitRec(Addr chan, ThreadContext *ctx)
87 : waitChan(chan), waitingContext(ctx)
91 // list of all blocked contexts
92 std::list<WaitRec> waitList;
94 Addr brk_point; // top of the data segment
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
100 // The maximum size allowed for the stack.
103 // addr to use for next stack region (for multithreaded apps)
104 Addr next_thread_stack_base;
106 // Base of region for mmaps (when user doesn't specify an address).
110 // Base of region for nxm data
114 Stats::Scalar num_syscalls; // number of syscalls executed
118 Process(ProcessParams *params);
120 virtual void initState();
124 //This id is assigned by m5 and is used to keep process' tlb entries
128 // flag for using architecture specific page table
130 PageTableBase* pTable;
136 std::string filename;
144 : fd(-1), filename("NULL"), mode(0), flags(0),
145 isPipe(false), readPipeSource(0), fileOffset(0)
148 void serialize(std::ostream &os);
149 void unserialize(Checkpoint *cp, const std::string §ion);
153 /// Memory proxy for initialization (image loading)
154 SETranslatingPortProxy initVirtMem;
157 // file descriptor remapping support
158 static const int MAX_FD = 256; // max legal fd value
159 FdMap fd_map[MAX_FD+1];
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);
167 // override of virtual SimObject method: register statistics
168 virtual void regStats();
170 // After getting registered with system object, tell process which
171 // system-wide context id it is assigned.
172 void assignThreadContext(int context_id)
174 contextIds.push_back(context_id);
177 // Find a free context to use
178 ThreadContext *findFreeContext();
180 // provide program name for debug messages
181 virtual const char *progName() const { return "<unknown>"; }
183 // map simulator fd sim_fd to target fd tgt_fd
184 void dup_fd(int sim_fd, int tgt_fd);
186 // generate new target fd for sim_fd
187 int alloc_fd(int sim_fd, std::string filename, int flags, int mode,
190 // free target fd (e.g., after close)
191 void free_fd(int tgt_fd);
193 // look up simulator fd for given target fd
194 int sim_fd(int tgt_fd);
196 // look up simulator fd_map object for a given target fd
197 FdMap *sim_fd_obj(int tgt_fd);
199 // fix all offsets for currently open files and save them
200 void fix_file_offsets();
202 // find all offsets for currently open files and save them
203 void find_file_offsets();
205 // set the source of this read pipe for a checkpoint resume
206 void setReadPipeSource(int read_pipe_fd, int source_fd);
208 virtual void syscall(int64_t callnum, ThreadContext *tc) = 0;
210 void allocateMem(Addr vaddr, int64_t size, bool clobber = false);
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);
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.
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.)
229 bool map(Addr vaddr, Addr paddr, int size);
231 void serialize(std::ostream &os);
232 void unserialize(Checkpoint *cp, const std::string §ion);
236 // "Live" process with system calls redirected to host system
239 class LiveProcess : public Process
243 std::vector<std::string> argv;
244 std::vector<std::string> envp;
247 LiveProcess(LiveProcessParams *params, ObjectFile *objFile);
249 // Id of the owner of the process
255 // pid of the process and it's parent
261 enum AuxiliaryVectorType {
277 // The following may be specific to Linux
283 M5_BASE_PLATFORM = 24,
288 M5_AT_VECTOR_SIZE = 44
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;}
298 // provide program name for debug messages
299 virtual const char *progName() const { return argv[0].c_str(); }
302 fullPath(const std::string &filename)
304 if (filename[0] == '/' || cwd.empty())
307 std::string full = cwd;
309 if (cwd[cwd.size() - 1] != '/')
312 return full + filename;
315 std::string getcwd() const { return cwd; }
317 virtual void syscall(int64_t callnum, ThreadContext *tc);
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;
326 virtual SyscallDesc *getDesc(int callnum) = 0;
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);
335 #endif // __PROCESS_HH__