process.hh revision 10298:77af86f37337
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;
53
54template<class IntType>
55struct AuxVector
56{
57    IntType a_type;
58    IntType a_val;
59
60    AuxVector()
61    {}
62
63    AuxVector(IntType type, IntType val);
64};
65
66class Process : public SimObject
67{
68  public:
69
70    /// Pointer to object representing the system this process is
71    /// running on.
72    System *system;
73
74    // thread contexts associated with this process
75    std::vector<int> contextIds;
76
77    // number of CPUs (esxec contexts, really) assigned to this process.
78    unsigned int numCpus() { return contextIds.size(); }
79
80    // record of blocked context
81    struct WaitRec
82    {
83        Addr waitChan;
84        ThreadContext *waitingContext;
85
86        WaitRec(Addr chan, ThreadContext *ctx)
87            : waitChan(chan), waitingContext(ctx)
88        {       }
89    };
90
91    // list of all blocked contexts
92    std::list<WaitRec> waitList;
93
94    Addr brk_point;             // top of the data segment
95
96    Addr stack_base;            // stack segment base (highest address)
97    unsigned stack_size;        // initial stack size
98    Addr stack_min;             // lowest address accessed on the stack
99
100    // The maximum size allowed for the stack.
101    Addr max_stack_size;
102
103    // addr to use for next stack region (for multithreaded apps)
104    Addr next_thread_stack_base;
105
106    // Base of region for mmaps (when user doesn't specify an address).
107    Addr mmap_start;
108    Addr mmap_end;
109
110    // Base of region for nxm data
111    Addr nxm_start;
112    Addr nxm_end;
113
114    Stats::Scalar num_syscalls;       // number of syscalls executed
115
116  protected:
117    // constructor
118    Process(ProcessParams *params);
119
120    virtual void initState();
121
122  public:
123
124    //This id is assigned by m5 and is used to keep process' tlb entries
125    //separated.
126    uint64_t M5_pid;
127
128    PageTableBase* pTable;
129
130    class FdMap
131    {
132      public:
133        int fd;
134        std::string filename;
135        int mode;
136        int flags;
137        bool isPipe;
138        int readPipeSource;
139        uint64_t fileOffset;
140
141        FdMap()
142            : fd(-1), filename("NULL"), mode(0), flags(0),
143              isPipe(false), readPipeSource(0), fileOffset(0)
144        { }
145
146        void serialize(std::ostream &os);
147        void unserialize(Checkpoint *cp, const std::string &section);
148    };
149
150  protected:
151    /// Memory proxy for initialization (image loading)
152    SETranslatingPortProxy initVirtMem;
153
154  private:
155    // file descriptor remapping support
156    static const int MAX_FD = 256;    // max legal fd value
157    FdMap fd_map[MAX_FD+1];
158
159
160  public:
161    // static helper functions to generate file descriptors for constructor
162    static int openInputFile(const std::string &filename);
163    static int openOutputFile(const std::string &filename);
164
165    // override of virtual SimObject method: register statistics
166    virtual void regStats();
167
168    // After getting registered with system object, tell process which
169    // system-wide context id it is assigned.
170    void assignThreadContext(int context_id)
171    {
172        contextIds.push_back(context_id);
173    }
174
175    // Find a free context to use
176    ThreadContext *findFreeContext();
177
178    // provide program name for debug messages
179    virtual const char *progName() const { return "<unknown>"; }
180
181    // map simulator fd sim_fd to target fd tgt_fd
182    void dup_fd(int sim_fd, int tgt_fd);
183
184    // generate new target fd for sim_fd
185    int alloc_fd(int sim_fd, std::string filename, int flags, int mode,
186                 bool pipe);
187
188    // free target fd (e.g., after close)
189    void free_fd(int tgt_fd);
190
191    // look up simulator fd for given target fd
192    int sim_fd(int tgt_fd);
193
194    // look up simulator fd_map object for a given target fd
195    FdMap *sim_fd_obj(int tgt_fd);
196
197    // fix all offsets for currently open files and save them
198    void fix_file_offsets();
199
200    // find all offsets for currently open files and save them
201    void find_file_offsets();
202
203    // set the source of this read pipe for a checkpoint resume
204    void setReadPipeSource(int read_pipe_fd, int source_fd);
205
206    virtual void syscall(int64_t callnum, ThreadContext *tc) = 0;
207
208    void allocateMem(Addr vaddr, int64_t size, bool clobber = false);
209
210    /// Attempt to fix up a fault at vaddr by allocating a page on the stack.
211    /// @return Whether the fault has been fixed.
212    bool fixupStackFault(Addr vaddr);
213
214    /**
215     * Map a contiguous range of virtual addresses in this process's
216     * address space to a contiguous range of physical addresses.
217     * This function exists primarily to enable exposing the map
218     * operation to python, so that configuration scripts can set up
219     * mappings in SE mode.
220     *
221     * @param vaddr The starting virtual address of the range.
222     * @param paddr The starting physical address of the range.
223     * @param size The length of the range in bytes.
224     * @return True if the map operation was successful.  (At this
225     *           point in time, the map operation always succeeds.)
226     */
227    bool map(Addr vaddr, Addr paddr, int size);
228
229    void serialize(std::ostream &os);
230    void unserialize(Checkpoint *cp, const std::string &section);
231};
232
233//
234// "Live" process with system calls redirected to host system
235//
236class ObjectFile;
237class LiveProcess : public Process
238{
239  protected:
240    ObjectFile *objFile;
241    std::vector<std::string> argv;
242    std::vector<std::string> envp;
243    std::string cwd;
244
245    LiveProcess(LiveProcessParams *params, ObjectFile *objFile);
246
247    // Id of the owner of the process
248    uint64_t __uid;
249    uint64_t __euid;
250    uint64_t __gid;
251    uint64_t __egid;
252
253    // pid of the process and it's parent
254    uint64_t __pid;
255    uint64_t __ppid;
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    // this function is used to create the LiveProcess object, since
327    // we can't tell which subclass of LiveProcess to use until we
328    // open and look at the object file.
329    static LiveProcess *create(LiveProcessParams *params);
330};
331
332
333#endif // __PROCESS_HH__
334