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