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;
55 template<class IntType>
64 AuxVector(IntType type, IntType val);
67 class Process : public SimObject
71 /// Pointer to object representing the system this process is
75 // thread contexts associated with this process
76 std::vector<int> contextIds;
78 // number of CPUs (esxec contexts, really) assigned to this process.
79 unsigned int numCpus() { return contextIds.size(); }
81 // record of blocked context
85 ThreadContext *waitingContext;
87 WaitRec(Addr chan, ThreadContext *ctx)
88 : waitChan(chan), waitingContext(ctx)
92 // list of all blocked contexts
93 std::list<WaitRec> waitList;
95 Addr brk_point; // top of the data segment
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
101 // The maximum size allowed for the stack.
104 // addr to use for next stack region (for multithreaded apps)
105 Addr next_thread_stack_base;
107 // Base of region for mmaps (when user doesn't specify an address).
111 // Base of region for nxm data
115 Stats::Scalar num_syscalls; // number of syscalls executed
119 Process(ProcessParams *params);
121 virtual void initState();
125 //This id is assigned by m5 and is used to keep process' tlb entries
129 // flag for using architecture specific page table
131 // running KvmCPU in SE mode requires special initialization
134 PageTableBase* pTable;
140 std::string filename;
146 EmulatedDriver *driver;
149 : fd(-1), filename("NULL"), mode(0), flags(0),
150 isPipe(false), readPipeSource(0), fileOffset(0), driver(NULL)
153 void serialize(std::ostream &os);
154 void unserialize(Checkpoint *cp, const std::string §ion);
158 /// Memory proxy for initialization (image loading)
159 SETranslatingPortProxy initVirtMem;
162 // file descriptor remapping support
163 static const int MAX_FD = 256; // max legal fd value
164 FdMap fd_map[MAX_FD+1];
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);
172 // override of virtual SimObject method: register statistics
173 virtual void regStats();
175 // After getting registered with system object, tell process which
176 // system-wide context id it is assigned.
177 void assignThreadContext(int context_id)
179 contextIds.push_back(context_id);
182 // Find a free context to use
183 ThreadContext *findFreeContext();
185 // provide program name for debug messages
186 virtual const char *progName() const { return "<unknown>"; }
188 // map simulator fd sim_fd to target fd tgt_fd
189 void dup_fd(int sim_fd, int tgt_fd);
191 // generate new target fd for sim_fd
192 int alloc_fd(int sim_fd, std::string filename, int flags, int mode,
195 // free target fd (e.g., after close)
196 void free_fd(int tgt_fd);
198 // look up simulator fd for given target fd
199 int sim_fd(int tgt_fd);
201 // look up simulator fd_map object for a given target fd
202 FdMap *sim_fd_obj(int tgt_fd);
204 // fix all offsets for currently open files and save them
205 void fix_file_offsets();
207 // find all offsets for currently open files and save them
208 void find_file_offsets();
210 // set the source of this read pipe for a checkpoint resume
211 void setReadPipeSource(int read_pipe_fd, int source_fd);
213 virtual void syscall(int64_t callnum, ThreadContext *tc) = 0;
215 void allocateMem(Addr vaddr, int64_t size, bool clobber = false);
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);
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.
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.)
234 bool map(Addr vaddr, Addr paddr, int size, bool cacheable = true);
236 void serialize(std::ostream &os);
237 void unserialize(Checkpoint *cp, const std::string §ion);
241 // "Live" process with system calls redirected to host system
244 class LiveProcess : public Process
248 std::vector<std::string> argv;
249 std::vector<std::string> envp;
252 LiveProcess(LiveProcessParams *params, ObjectFile *objFile);
254 // Id of the owner of the process
260 // pid of the process and it's parent
264 // Emulated drivers available to this process
265 std::vector<EmulatedDriver *> drivers;
269 enum AuxiliaryVectorType {
285 // The following may be specific to Linux
291 M5_BASE_PLATFORM = 24,
296 M5_AT_VECTOR_SIZE = 44
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;}
306 // provide program name for debug messages
307 virtual const char *progName() const { return argv[0].c_str(); }
310 fullPath(const std::string &filename)
312 if (filename[0] == '/' || cwd.empty())
315 std::string full = cwd;
317 if (cwd[cwd.size() - 1] != '/')
320 return full + filename;
323 std::string getcwd() const { return cwd; }
325 virtual void syscall(int64_t callnum, ThreadContext *tc);
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;
334 virtual SyscallDesc *getDesc(int callnum) = 0;
337 * Find an emulated device driver.
339 * @param filename Name of the device (under /dev)
340 * @return Pointer to driver object if found, else NULL
342 EmulatedDriver *findDriver(std::string filename);
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);
351 #endif // __PROCESS_HH__