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__
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"
51 struct LiveProcessParams;
57 template<class IntType>
66 AuxVector(IntType type, IntType val);
69 class Process : public SimObject
73 /// Pointer to object representing the system this process is
77 // thread contexts associated with this process
78 std::vector<int> contextIds;
80 // number of CPUs (esxec contexts, really) assigned to this process.
81 unsigned int numCpus() { return contextIds.size(); }
83 // record of blocked context
87 ThreadContext *waitingContext;
89 WaitRec(Addr chan, ThreadContext *ctx)
90 : waitChan(chan), waitingContext(ctx)
94 // list of all blocked contexts
95 std::list<WaitRec> waitList;
97 Addr brk_point; // top of the data segment
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
103 // The maximum size allowed for the stack.
106 // addr to use for next stack region (for multithreaded apps)
107 Addr next_thread_stack_base;
109 // Base of region for mmaps (when user doesn't specify an address).
113 // Base of region for nxm data
117 Stats::Scalar num_syscalls; // number of syscalls executed
121 Process(ProcessParams *params);
123 virtual void initState();
125 DrainState drain() M5_ATTR_OVERRIDE;
129 //This id is assigned by m5 and is used to keep process' tlb entries
133 // flag for using architecture specific page table
135 // running KvmCPU in SE mode requires special initialization
138 PageTableBase* pTable;
141 /// Memory proxy for initialization (image loading)
142 SETranslatingPortProxy initVirtMem;
145 static const int NUM_FDS = 1024;
147 // File descriptor remapping support.
148 std::shared_ptr<std::array<FDEntry, NUM_FDS>> fd_array;
150 // Standard file descriptor options for initialization and checkpoints.
151 std::map<std::string, int> imap;
152 std::map<std::string, int> oemap;
155 // inherit file descriptor map from another process (necessary for clone)
156 void inheritFdArray(Process *p);
158 // override of virtual SimObject method: register statistics
159 virtual void regStats();
161 // After getting registered with system object, tell process which
162 // system-wide context id it is assigned.
163 void assignThreadContext(int context_id)
165 contextIds.push_back(context_id);
168 // Find a free context to use
169 ThreadContext *findFreeContext();
171 // provide program name for debug messages
172 virtual const char *progName() const { return "<unknown>"; }
174 // generate new target fd for sim_fd
175 int alloc_fd(int sim_fd, const std::string& filename, int flags, int mode,
178 // disassociate target fd with simulator fd and cleanup subsidiary fields
179 void reset_fd_entry(int tgt_fd);
181 // look up simulator fd for given target fd
182 int sim_fd(int tgt_fd);
184 // look up fd entry for a given target fd
185 FDEntry *get_fd_entry(int tgt_fd);
187 // look up target fd for given host fd
188 // Assumes a 1:1 mapping between target file descriptor and host file
189 // descriptor. Given the current API, this must be true given that it's
190 // not possible to map multiple target file descriptors to the same host
192 int tgt_fd(int sim_fd);
194 // fix all offsets for currently open files and save them
195 void fix_file_offsets();
197 // find all offsets for currently open files and save them
198 void find_file_offsets();
200 // set the source of this read pipe for a checkpoint resume
201 void setReadPipeSource(int read_pipe_fd, int source_fd);
203 virtual void syscall(int64_t callnum, ThreadContext *tc) = 0;
205 void allocateMem(Addr vaddr, int64_t size, bool clobber = false);
207 /// Attempt to fix up a fault at vaddr by allocating a page on the stack.
208 /// @return Whether the fault has been fixed.
209 bool fixupStackFault(Addr vaddr);
212 * Maps a contiguous range of virtual addresses in this process's
213 * address space to a contiguous range of physical addresses.
214 * This function exists primarily to expose the map operation to
215 * python, so that configuration scripts can set up mappings in SE mode.
217 * @param vaddr The starting virtual address of the range.
218 * @param paddr The starting physical address of the range.
219 * @param size The length of the range in bytes.
220 * @param cacheable Specifies whether accesses are cacheable.
221 * @return True if the map operation was successful. (At this
222 * point in time, the map operation always succeeds.)
224 bool map(Addr vaddr, Addr paddr, int size, bool cacheable = true);
226 void serialize(CheckpointOut &cp) const M5_ATTR_OVERRIDE;
227 void unserialize(CheckpointIn &cp) M5_ATTR_OVERRIDE;
231 // "Live" process with system calls redirected to host system
234 class LiveProcess : public Process
238 std::vector<std::string> argv;
239 std::vector<std::string> envp;
242 LiveProcess(LiveProcessParams *params, ObjectFile *objFile);
244 // Id of the owner of the process
250 // pid of the process and it's parent
254 // Emulated drivers available to this process
255 std::vector<EmulatedDriver *> drivers;
259 enum AuxiliaryVectorType {
275 // The following may be specific to Linux
281 M5_BASE_PLATFORM = 24,
286 M5_AT_VECTOR_SIZE = 44
289 inline uint64_t uid() {return __uid;}
290 inline uint64_t euid() {return __euid;}
291 inline uint64_t gid() {return __gid;}
292 inline uint64_t egid() {return __egid;}
293 inline uint64_t pid() {return __pid;}
294 inline uint64_t ppid() {return __ppid;}
296 // provide program name for debug messages
297 virtual const char *progName() const { return argv[0].c_str(); }
300 fullPath(const std::string &filename)
302 if (filename[0] == '/' || cwd.empty())
305 std::string full = cwd;
307 if (cwd[cwd.size() - 1] != '/')
310 return full + filename;
313 std::string getcwd() const { return cwd; }
315 virtual void syscall(int64_t callnum, ThreadContext *tc);
317 virtual TheISA::IntReg getSyscallArg(ThreadContext *tc, int &i) = 0;
318 virtual TheISA::IntReg getSyscallArg(ThreadContext *tc, int &i, int width);
319 virtual void setSyscallArg(ThreadContext *tc,
320 int i, TheISA::IntReg val) = 0;
321 virtual void setSyscallReturn(ThreadContext *tc,
322 SyscallReturn return_value) = 0;
324 virtual SyscallDesc *getDesc(int callnum) = 0;
327 * Find an emulated device driver.
329 * @param filename Name of the device (under /dev)
330 * @return Pointer to driver object if found, else NULL
332 EmulatedDriver *findDriver(std::string filename);
334 // this function is used to create the LiveProcess object, since
335 // we can't tell which subclass of LiveProcess to use until we
336 // open and look at the object file.
337 static LiveProcess *create(LiveProcessParams *params);
341 #endif // __PROCESS_HH__