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