1/*
2 * Copyright (c) 2014-2016 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 *          Brandon Potter
32 */
33
34#ifndef __PROCESS_HH__
35#define __PROCESS_HH__
36
37#include <inttypes.h>
38
39#include <map>
40#include <string>
41#include <vector>
42
43#include "arch/registers.hh"
44#include "base/statistics.hh"
45#include "base/types.hh"
46#include "config/the_isa.hh"
47#include "mem/se_translating_port_proxy.hh"
48#include "sim/fd_array.hh"
49#include "sim/fd_entry.hh"
50#include "sim/mem_state.hh"
51#include "sim/sim_object.hh"
52
53struct ProcessParams;
54
55class EmulatedDriver;
56class ObjectFile;
57class EmulationPageTable;
58class SyscallDesc;
59class SyscallReturn;
60class System;
61class ThreadContext;
62
63class Process : public SimObject
64{
65  public:
66    Process(ProcessParams *params, EmulationPageTable *pTable,
67            ObjectFile *obj_file);
68
69    void serialize(CheckpointOut &cp) const override;
70    void unserialize(CheckpointIn &cp) override;
71
72    void initState() override;
73    DrainState drain() override;
74
75    virtual void syscall(int64_t callnum, ThreadContext *tc, Fault *fault);
76    virtual RegVal getSyscallArg(ThreadContext *tc, int &i) = 0;
77    virtual RegVal getSyscallArg(ThreadContext *tc, int &i, int width);
78    virtual void setSyscallArg(ThreadContext *tc, int i, RegVal val) = 0;
79    virtual void setSyscallReturn(ThreadContext *tc,
80                                  SyscallReturn return_value) = 0;
81    virtual SyscallDesc *getDesc(int callnum) = 0;
82
83    inline uint64_t uid() { return _uid; }
84    inline uint64_t euid() { return _euid; }
85    inline uint64_t gid() { return _gid; }
86    inline uint64_t egid() { return _egid; }
87    inline uint64_t pid() { return _pid; }
88    inline uint64_t ppid() { return _ppid; }
89    inline uint64_t pgid() { return _pgid; }
90    inline void pgid(uint64_t pgid) { _pgid = pgid; }
91    inline uint64_t tgid() { return _tgid; }
92
93    const char *progName() const { return executable.c_str(); }
94
95    /**
96     * Find an emulated device driver.
97     *
98     * @param filename Name of the device (under /dev)
99     * @return Pointer to driver object if found, else nullptr
100     */
101    EmulatedDriver *findDriver(std::string filename);
102
103    // This function acts as a callback to update the bias value in
104    // the object file because the parameters needed to calculate the
105    // bias are not available when the object file is created.
106    void updateBias();
107    Addr getBias();
108    Addr getStartPC();
109    ObjectFile *getInterpreter();
110
111    // override of virtual SimObject method: register statistics
112    void regStats() override;
113
114    void allocateMem(Addr vaddr, int64_t size, bool clobber = false);
115
116    /// Attempt to fix up a fault at vaddr by allocating a page on the stack.
117    /// @return Whether the fault has been fixed.
118    bool fixupStackFault(Addr vaddr);
119
120    // After getting registered with system object, tell process which
121    // system-wide context id it is assigned.
122    void
123    assignThreadContext(ContextID context_id)
124    {
125        contextIds.push_back(context_id);
126    }
127
128    // Find a free context to use
129    ThreadContext *findFreeContext();
130
131    /**
132     * After delegating a thread context to a child process
133     * no longer should relate to the ThreadContext
134     */
135    void revokeThreadContext(int context_id);
136
137    /**
138     * Does mmap region grow upward or downward from mmapEnd?  Most
139     * platforms grow downward, but a few (such as Alpha) grow upward
140     * instead, so they can override this method to return false.
141     */
142    virtual bool mmapGrowsDown() const { return true; }
143
144    /**
145     * Maps a contiguous range of virtual addresses in this process's
146     * address space to a contiguous range of physical addresses.
147     * This function exists primarily to expose the map operation to
148     * python, so that configuration scripts can set up mappings in SE mode.
149     *
150     * @param vaddr The starting virtual address of the range.
151     * @param paddr The starting physical address of the range.
152     * @param size The length of the range in bytes.
153     * @param cacheable Specifies whether accesses are cacheable.
154     * @return True if the map operation was successful.  (At this
155     *           point in time, the map operation always succeeds.)
156     */
157    bool map(Addr vaddr, Addr paddr, int size, bool cacheable = true);
158
159    void replicatePage(Addr vaddr, Addr new_paddr, ThreadContext *old_tc,
160                       ThreadContext *new_tc, bool alloc_page);
161
162    virtual void clone(ThreadContext *old_tc, ThreadContext *new_tc,
163                       Process *new_p, RegVal flags);
164
165    // thread contexts associated with this process
166    std::vector<ContextID> contextIds;
167
168    // system object which owns this process
169    System *system;
170
171    Stats::Scalar numSyscalls;  // track how many system calls are executed
172
173    // flag for using architecture specific page table
174    bool useArchPT;
175    // running KVM requires special initialization
176    bool kvmInSE;
177    // flag for using the process as a thread which shares page tables
178    bool useForClone;
179
180    EmulationPageTable *pTable;
181
182    SETranslatingPortProxy initVirtMem; // memory proxy for initial image load
183
184    ObjectFile *objFile;
185    std::vector<std::string> argv;
186    std::vector<std::string> envp;
187    std::string executable;
188
189    /**
190     * Return an absolute path given a relative path paired with the current
191     * working directory of the process running under simulation.
192     *
193     * @param path The relative path (generally a filename) that needs the
194     * current working directory prepended.
195     * @param host_fs A flag which determines whether to return a
196     * path for the host filesystem or the filesystem of the process running
197     * under simulation. Only matters if filesysem redirection is used to
198     * replace files (or directories) that would normally appear via the
199     * host filesystem.
200     * @return String containing an absolute path.
201     */
202    std::string absolutePath(const std::string &path, bool host_fs);
203
204    /**
205     * Redirect file path if it matches any keys initialized by system object.
206     * @param filename An input parameter containing either a relative path
207     * or an absolute path. If given a relative path, the path will be
208     * prepended to the current working directory of the simulation with
209     * respect to the host filesystem.
210     * @return String containing an absolute path.
211     */
212    std::string checkPathRedirect(const std::string &filename);
213
214    /**
215     * The cwd members are used to track changes to the current working
216     * directory for the purpose of executing system calls which depend on
217     * relative paths (i.e. open, chdir).
218     *
219     * The tgt member and host member may differ if the path for the current
220     * working directory is redirected to point to a different location
221     * (i.e. `cd /proc` should point to '$(gem5_repo)/m5out/fs/proc'
222     * instead of '/proc').
223     */
224    std::string tgtCwd;
225    std::string hostCwd;
226
227    // Syscall emulation uname release.
228    std::string release;
229
230    // Id of the owner of the process
231    uint64_t _uid;
232    uint64_t _euid;
233    uint64_t _gid;
234    uint64_t _egid;
235
236    // pid of the process and it's parent
237    uint64_t _pid;
238    uint64_t _ppid;
239    uint64_t _pgid;
240    uint64_t _tgid;
241
242    // Emulated drivers available to this process
243    std::vector<EmulatedDriver *> drivers;
244
245    std::shared_ptr<FDArray> fds;
246
247    bool *exitGroup;
248    std::shared_ptr<MemState> memState;
249
250    /**
251     * Calls a futex wakeup at the address specified by this pointer when
252     * this process exits.
253     */
254    uint64_t childClearTID;
255
256    // Process was forked with SIGCHLD set.
257    bool *sigchld;
258};
259
260#endif // __PROCESS_HH__
261