process.hh revision 11852:df43a146a38a
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    Stats::Scalar num_syscalls;       // number of syscalls executed
120
121  protected:
122    // constructor
123    Process(ProcessParams *params);
124
125    void initState() override;
126
127    DrainState drain() override;
128
129  public:
130
131    // flag for using architecture specific page table
132    bool useArchPT;
133    // running KvmCPU in SE mode requires special initialization
134    bool kvmInSE;
135
136    PageTableBase* pTable;
137
138  protected:
139    /// Memory proxy for initialization (image loading)
140    SETranslatingPortProxy initVirtMem;
141
142  private:
143    static const int NUM_FDS = 1024;
144
145    // File descriptor remapping support.
146    std::shared_ptr<std::array<FDEntry, NUM_FDS>> fd_array;
147
148    // Standard file descriptor options for initialization and checkpoints.
149    std::map<std::string, int> imap;
150    std::map<std::string, int> oemap;
151
152  public:
153    // inherit file descriptor map from another process (necessary for clone)
154    void inheritFDArray(Process *p);
155
156    // override of virtual SimObject method: register statistics
157    void regStats() override;
158
159    // After getting registered with system object, tell process which
160    // system-wide context id it is assigned.
161    void assignThreadContext(ContextID context_id)
162    {
163        contextIds.push_back(context_id);
164    }
165
166    // Find a free context to use
167    ThreadContext *findFreeContext();
168
169    // generate new target fd for sim_fd
170    int allocFD(int sim_fd, const std::string& filename, int flags, int mode,
171                bool pipe);
172
173    // disassociate target fd with simulator fd and cleanup subsidiary fields
174    void resetFDEntry(int tgt_fd);
175
176    // look up simulator fd for given target fd
177    int getSimFD(int tgt_fd);
178
179    // look up fd entry for a given target fd
180    FDEntry *getFDEntry(int tgt_fd);
181
182    // look up target fd for given host fd
183    // Assumes a 1:1 mapping between target file descriptor and host file
184    // descriptor. Given the current API, this must be true given that it's
185    // not possible to map multiple target file descriptors to the same host
186    // file descriptor
187    int getTgtFD(int sim_fd);
188
189    // fix all offsets for currently open files and save them
190    void fixFileOffsets();
191
192    // find all offsets for currently open files and save them
193    void findFileOffsets();
194
195    // set the source of this read pipe for a checkpoint resume
196    void setReadPipeSource(int read_pipe_fd, int source_fd);
197
198
199    void allocateMem(Addr vaddr, int64_t size, bool clobber = false);
200
201    /// Attempt to fix up a fault at vaddr by allocating a page on the stack.
202    /// @return Whether the fault has been fixed.
203    bool fixupStackFault(Addr vaddr);
204
205    /**
206     * Maps a contiguous range of virtual addresses in this process's
207     * address space to a contiguous range of physical addresses.
208     * This function exists primarily to expose the map operation to
209     * python, so that configuration scripts can set up mappings in SE mode.
210     *
211     * @param vaddr The starting virtual address of the range.
212     * @param paddr The starting physical address of the range.
213     * @param size The length of the range in bytes.
214     * @param cacheable Specifies whether accesses are cacheable.
215     * @return True if the map operation was successful.  (At this
216     *           point in time, the map operation always succeeds.)
217     */
218    bool map(Addr vaddr, Addr paddr, int size, bool cacheable = true);
219
220    void serialize(CheckpointOut &cp) const override;
221    void unserialize(CheckpointIn &cp) override;
222
223  protected:
224    ObjectFile *objFile;
225    std::vector<std::string> argv;
226    std::vector<std::string> envp;
227    std::string cwd;
228    std::string executable;
229
230    Process(ProcessParams *params, ObjectFile *obj_file);
231
232  public:
233    // Id of the owner of the process
234    uint64_t _uid;
235    uint64_t _euid;
236    uint64_t _gid;
237    uint64_t _egid;
238
239    // pid of the process and it's parent
240    uint64_t _pid;
241    uint64_t _ppid;
242
243    // Emulated drivers available to this process
244    std::vector<EmulatedDriver *> drivers;
245
246    enum AuxiliaryVectorType {
247        M5_AT_NULL = 0,
248        M5_AT_IGNORE = 1,
249        M5_AT_EXECFD = 2,
250        M5_AT_PHDR = 3,
251        M5_AT_PHENT = 4,
252        M5_AT_PHNUM = 5,
253        M5_AT_PAGESZ = 6,
254        M5_AT_BASE = 7,
255        M5_AT_FLAGS = 8,
256        M5_AT_ENTRY = 9,
257        M5_AT_NOTELF = 10,
258        M5_AT_UID = 11,
259        M5_AT_EUID = 12,
260        M5_AT_GID = 13,
261        M5_AT_EGID = 14,
262        // The following may be specific to Linux
263        M5_AT_PLATFORM = 15,
264        M5_AT_HWCAP = 16,
265        M5_AT_CLKTCK = 17,
266
267        M5_AT_SECURE = 23,
268        M5_BASE_PLATFORM = 24,
269        M5_AT_RANDOM = 25,
270
271        M5_AT_EXECFN = 31,
272
273        M5_AT_VECTOR_SIZE = 44
274    };
275
276    inline uint64_t uid() { return _uid; }
277    inline uint64_t euid() { return _euid; }
278    inline uint64_t gid() { return _gid; }
279    inline uint64_t egid() { return _egid; }
280    inline uint64_t pid() { return _pid; }
281    inline uint64_t ppid() { return _ppid; }
282
283    // provide program name for debug messages
284    const char *progName() const { return executable.c_str(); }
285
286    std::string
287    fullPath(const std::string &filename)
288    {
289        if (filename[0] == '/' || cwd.empty())
290            return filename;
291
292        std::string full = cwd;
293
294        if (cwd[cwd.size() - 1] != '/')
295            full += '/';
296
297        return full + filename;
298    }
299
300    std::string getcwd() const { return cwd; }
301
302    void syscall(int64_t callnum, ThreadContext *tc);
303
304    virtual TheISA::IntReg getSyscallArg(ThreadContext *tc, int &i) = 0;
305    virtual TheISA::IntReg getSyscallArg(ThreadContext *tc, int &i, int width);
306    virtual void setSyscallArg(ThreadContext *tc,
307            int i, TheISA::IntReg val) = 0;
308    virtual void setSyscallReturn(ThreadContext *tc,
309            SyscallReturn return_value) = 0;
310
311    virtual SyscallDesc *getDesc(int callnum) = 0;
312
313    /**
314     * Find an emulated device driver.
315     *
316     * @param filename Name of the device (under /dev)
317     * @return Pointer to driver object if found, else NULL
318     */
319    EmulatedDriver *findDriver(std::string filename);
320
321    // This function acts as a callback to update the bias value in
322    // the object file because the parameters needed to calculate the
323    // bias are not available when the object file is created.
324    void updateBias();
325
326    ObjectFile *getInterpreter();
327
328    Addr getBias();
329    Addr getStartPC();
330};
331
332#endif // __PROCESS_HH__
333