process.hh revision 11005:e7f403b6b76f
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    virtual void initState();
124
125    DrainState drain() M5_ATTR_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    virtual void regStats();
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 M5_ATTR_OVERRIDE;
227    void unserialize(CheckpointIn &cp) M5_ATTR_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
242    LiveProcess(LiveProcessParams *params, ObjectFile *objFile);
243
244    // Id of the owner of the process
245    uint64_t __uid;
246    uint64_t __euid;
247    uint64_t __gid;
248    uint64_t __egid;
249
250    // pid of the process and it's parent
251    uint64_t __pid;
252    uint64_t __ppid;
253
254    // Emulated drivers available to this process
255    std::vector<EmulatedDriver *> drivers;
256
257  public:
258
259    enum AuxiliaryVectorType {
260        M5_AT_NULL = 0,
261        M5_AT_IGNORE = 1,
262        M5_AT_EXECFD = 2,
263        M5_AT_PHDR = 3,
264        M5_AT_PHENT = 4,
265        M5_AT_PHNUM = 5,
266        M5_AT_PAGESZ = 6,
267        M5_AT_BASE = 7,
268        M5_AT_FLAGS = 8,
269        M5_AT_ENTRY = 9,
270        M5_AT_NOTELF = 10,
271        M5_AT_UID = 11,
272        M5_AT_EUID = 12,
273        M5_AT_GID = 13,
274        M5_AT_EGID = 14,
275        // The following may be specific to Linux
276        M5_AT_PLATFORM = 15,
277        M5_AT_HWCAP = 16,
278        M5_AT_CLKTCK = 17,
279
280        M5_AT_SECURE = 23,
281        M5_BASE_PLATFORM = 24,
282        M5_AT_RANDOM = 25,
283
284        M5_AT_EXECFN = 31,
285
286        M5_AT_VECTOR_SIZE = 44
287    };
288
289    inline uint64_t uid() {return __uid;}
290    inline uint64_t euid() {return __euid;}
291    inline uint64_t gid() {return __gid;}
292    inline uint64_t egid() {return __egid;}
293    inline uint64_t pid() {return __pid;}
294    inline uint64_t ppid() {return __ppid;}
295
296    // provide program name for debug messages
297    virtual const char *progName() const { return argv[0].c_str(); }
298
299    std::string
300    fullPath(const std::string &filename)
301    {
302        if (filename[0] == '/' || cwd.empty())
303            return filename;
304
305        std::string full = cwd;
306
307        if (cwd[cwd.size() - 1] != '/')
308            full += '/';
309
310        return full + filename;
311    }
312
313    std::string getcwd() const { return cwd; }
314
315    virtual void syscall(int64_t callnum, ThreadContext *tc);
316
317    virtual TheISA::IntReg getSyscallArg(ThreadContext *tc, int &i) = 0;
318    virtual TheISA::IntReg getSyscallArg(ThreadContext *tc, int &i, int width);
319    virtual void setSyscallArg(ThreadContext *tc,
320            int i, TheISA::IntReg val) = 0;
321    virtual void setSyscallReturn(ThreadContext *tc,
322            SyscallReturn return_value) = 0;
323
324    virtual SyscallDesc *getDesc(int callnum) = 0;
325
326    /**
327     * Find an emulated device driver.
328     *
329     * @param filename Name of the device (under /dev)
330     * @return Pointer to driver object if found, else NULL
331     */
332    EmulatedDriver *findDriver(std::string filename);
333
334    // this function is used to create the LiveProcess object, since
335    // we can't tell which subclass of LiveProcess to use until we
336    // open and look at the object file.
337    static LiveProcess *create(LiveProcessParams *params);
338};
339
340
341#endif // __PROCESS_HH__
342