process.hh revision 11851:824055fe6b30
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 <map>
38#include <string>
39#include <vector>
40
41#include "arch/registers.hh"
42#include "base/statistics.hh"
43#include "base/types.hh"
44#include "config/the_isa.hh"
45#include "mem/se_translating_port_proxy.hh"
46#include "sim/fd_entry.hh"
47#include "sim/sim_object.hh"
48
49struct ProcessParams;
50
51class EmulatedDriver;
52class ObjectFile;
53class PageTableBase;
54class SyscallDesc;
55class SyscallReturn;
56class System;
57class ThreadContext;
58
59template<class IntType>
60struct AuxVector
61{
62    IntType a_type;
63    IntType a_val;
64
65    AuxVector()
66    {}
67
68    AuxVector(IntType type, IntType val);
69};
70
71class Process : public SimObject
72{
73  public:
74
75    /// Pointer to object representing the system this process is
76    /// running on.
77    System *system;
78
79    // thread contexts associated with this process
80    std::vector<ContextID> contextIds;
81
82    // number of CPUs (esxec contexts, really) assigned to this process.
83    unsigned int numCpus() { return contextIds.size(); }
84
85    // record of blocked context
86    struct WaitRec
87    {
88        Addr waitChan;
89        ThreadContext *waitingContext;
90
91        WaitRec(Addr chan, ThreadContext *ctx)
92            : waitChan(chan), waitingContext(ctx)
93        {       }
94    };
95
96    // list of all blocked contexts
97    std::list<WaitRec> waitList;
98
99    Addr brk_point;             // top of the data segment
100
101    Addr stack_base;            // stack segment base (highest address)
102    unsigned stack_size;        // initial stack size
103    Addr stack_min;             // lowest address accessed on the stack
104
105    // The maximum size allowed for the stack.
106    Addr max_stack_size;
107
108    // addr to use for next stack region (for multithreaded apps)
109    Addr next_thread_stack_base;
110
111    // Base of region for mmaps (when user doesn't specify an address).
112    Addr mmap_end;
113
114    // Does mmap region grow upward or downward from mmap_end?  Most
115    // platforms grow downward, but a few (such as Alpha) grow upward
116    // instead, so they can override thie method to return false.
117    virtual bool mmapGrowsDown() const { return true; }
118
119    // Base of region for nxm data
120    Addr nxm_start;
121    Addr nxm_end;
122
123    Stats::Scalar num_syscalls;       // number of syscalls executed
124
125  protected:
126    // constructor
127    Process(ProcessParams *params);
128
129    void initState() override;
130
131    DrainState drain() override;
132
133  public:
134
135    // flag for using architecture specific page table
136    bool useArchPT;
137    // running KvmCPU in SE mode requires special initialization
138    bool kvmInSE;
139
140    PageTableBase* pTable;
141
142  protected:
143    /// Memory proxy for initialization (image loading)
144    SETranslatingPortProxy initVirtMem;
145
146  private:
147    static const int NUM_FDS = 1024;
148
149    // File descriptor remapping support.
150    std::shared_ptr<std::array<FDEntry, NUM_FDS>> fd_array;
151
152    // Standard file descriptor options for initialization and checkpoints.
153    std::map<std::string, int> imap;
154    std::map<std::string, int> oemap;
155
156  public:
157    // inherit file descriptor map from another process (necessary for clone)
158    void inheritFDArray(Process *p);
159
160    // override of virtual SimObject method: register statistics
161    void regStats() override;
162
163    // After getting registered with system object, tell process which
164    // system-wide context id it is assigned.
165    void assignThreadContext(ContextID context_id)
166    {
167        contextIds.push_back(context_id);
168    }
169
170    // Find a free context to use
171    ThreadContext *findFreeContext();
172
173    // generate new target fd for sim_fd
174    int allocFD(int sim_fd, const std::string& filename, int flags, int mode,
175                bool pipe);
176
177    // disassociate target fd with simulator fd and cleanup subsidiary fields
178    void resetFDEntry(int tgt_fd);
179
180    // look up simulator fd for given target fd
181    int getSimFD(int tgt_fd);
182
183    // look up fd entry for a given target fd
184    FDEntry *getFDEntry(int tgt_fd);
185
186    // look up target fd for given host fd
187    // Assumes a 1:1 mapping between target file descriptor and host file
188    // descriptor. Given the current API, this must be true given that it's
189    // not possible to map multiple target file descriptors to the same host
190    // file descriptor
191    int getTgtFD(int sim_fd);
192
193    // fix all offsets for currently open files and save them
194    void fixFileOffsets();
195
196    // find all offsets for currently open files and save them
197    void findFileOffsets();
198
199    // set the source of this read pipe for a checkpoint resume
200    void setReadPipeSource(int read_pipe_fd, int source_fd);
201
202
203    void allocateMem(Addr vaddr, int64_t size, bool clobber = false);
204
205    /// Attempt to fix up a fault at vaddr by allocating a page on the stack.
206    /// @return Whether the fault has been fixed.
207    bool fixupStackFault(Addr vaddr);
208
209    /**
210     * Maps a contiguous range of virtual addresses in this process's
211     * address space to a contiguous range of physical addresses.
212     * This function exists primarily to expose the map operation to
213     * python, so that configuration scripts can set up mappings in SE mode.
214     *
215     * @param vaddr The starting virtual address of the range.
216     * @param paddr The starting physical address of the range.
217     * @param size The length of the range in bytes.
218     * @param cacheable Specifies whether accesses are cacheable.
219     * @return True if the map operation was successful.  (At this
220     *           point in time, the map operation always succeeds.)
221     */
222    bool map(Addr vaddr, Addr paddr, int size, bool cacheable = true);
223
224    void serialize(CheckpointOut &cp) const override;
225    void unserialize(CheckpointIn &cp) override;
226
227  protected:
228    ObjectFile *objFile;
229    std::vector<std::string> argv;
230    std::vector<std::string> envp;
231    std::string cwd;
232    std::string executable;
233
234    Process(ProcessParams *params, ObjectFile *obj_file);
235
236  public:
237    // Id of the owner of the process
238    uint64_t _uid;
239    uint64_t _euid;
240    uint64_t _gid;
241    uint64_t _egid;
242
243    // pid of the process and it's parent
244    uint64_t _pid;
245    uint64_t _ppid;
246
247    // Emulated drivers available to this process
248    std::vector<EmulatedDriver *> drivers;
249
250    enum AuxiliaryVectorType {
251        M5_AT_NULL = 0,
252        M5_AT_IGNORE = 1,
253        M5_AT_EXECFD = 2,
254        M5_AT_PHDR = 3,
255        M5_AT_PHENT = 4,
256        M5_AT_PHNUM = 5,
257        M5_AT_PAGESZ = 6,
258        M5_AT_BASE = 7,
259        M5_AT_FLAGS = 8,
260        M5_AT_ENTRY = 9,
261        M5_AT_NOTELF = 10,
262        M5_AT_UID = 11,
263        M5_AT_EUID = 12,
264        M5_AT_GID = 13,
265        M5_AT_EGID = 14,
266        // The following may be specific to Linux
267        M5_AT_PLATFORM = 15,
268        M5_AT_HWCAP = 16,
269        M5_AT_CLKTCK = 17,
270
271        M5_AT_SECURE = 23,
272        M5_BASE_PLATFORM = 24,
273        M5_AT_RANDOM = 25,
274
275        M5_AT_EXECFN = 31,
276
277        M5_AT_VECTOR_SIZE = 44
278    };
279
280    inline uint64_t uid() { return _uid; }
281    inline uint64_t euid() { return _euid; }
282    inline uint64_t gid() { return _gid; }
283    inline uint64_t egid() { return _egid; }
284    inline uint64_t pid() { return _pid; }
285    inline uint64_t ppid() { return _ppid; }
286
287    // provide program name for debug messages
288    const char *progName() const { return executable.c_str(); }
289
290    std::string
291    fullPath(const std::string &filename)
292    {
293        if (filename[0] == '/' || cwd.empty())
294            return filename;
295
296        std::string full = cwd;
297
298        if (cwd[cwd.size() - 1] != '/')
299            full += '/';
300
301        return full + filename;
302    }
303
304    std::string getcwd() const { return cwd; }
305
306    void syscall(int64_t callnum, ThreadContext *tc);
307
308    virtual TheISA::IntReg getSyscallArg(ThreadContext *tc, int &i) = 0;
309    virtual TheISA::IntReg getSyscallArg(ThreadContext *tc, int &i, int width);
310    virtual void setSyscallArg(ThreadContext *tc,
311            int i, TheISA::IntReg val) = 0;
312    virtual void setSyscallReturn(ThreadContext *tc,
313            SyscallReturn return_value) = 0;
314
315    virtual SyscallDesc *getDesc(int callnum) = 0;
316
317    /**
318     * Find an emulated device driver.
319     *
320     * @param filename Name of the device (under /dev)
321     * @return Pointer to driver object if found, else NULL
322     */
323    EmulatedDriver *findDriver(std::string filename);
324
325    // This function acts as a callback to update the bias value in
326    // the object file because the parameters needed to calculate the
327    // bias are not available when the object file is created.
328    void updateBias();
329
330    ObjectFile *getInterpreter();
331
332    Addr getBias();
333    Addr getStartPC();
334};
335
336#endif // __PROCESS_HH__
337