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