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