process.hh revision 11854:0e94e16e26ea
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 <inttypes.h>
37
38#include <array>
39#include <map>
40#include <string>
41#include <vector>
42
43#include "arch/registers.hh"
44#include "base/statistics.hh"
45#include "base/types.hh"
46#include "config/the_isa.hh"
47#include "mem/se_translating_port_proxy.hh"
48#include "sim/fd_entry.hh"
49#include "sim/sim_object.hh"
50
51struct ProcessParams;
52
53class EmulatedDriver;
54class ObjectFile;
55class PageTableBase;
56class SyscallDesc;
57class SyscallReturn;
58class System;
59class ThreadContext;
60
61class Process : public SimObject
62{
63  public:
64    struct WaitRec
65    {
66        Addr waitChan;
67        ThreadContext *waitingContext;
68
69        WaitRec(Addr chan, ThreadContext *ctx)
70            : waitChan(chan), waitingContext(ctx)
71        { }
72    };
73
74    Process(ProcessParams *params, ObjectFile *obj_file);
75
76    void serialize(CheckpointOut &cp) const override;
77    void unserialize(CheckpointIn &cp) override;
78
79    void initState() override;
80    DrainState drain() override;
81
82    void syscall(int64_t callnum, ThreadContext *tc);
83    virtual TheISA::IntReg getSyscallArg(ThreadContext *tc, int &i) = 0;
84    virtual TheISA::IntReg getSyscallArg(ThreadContext *tc, int &i, int width);
85    virtual void setSyscallArg(ThreadContext *tc, int i,
86                               TheISA::IntReg val) = 0;
87    virtual void setSyscallReturn(ThreadContext *tc,
88                                  SyscallReturn return_value) = 0;
89    virtual SyscallDesc *getDesc(int callnum) = 0;
90
91    inline uint64_t uid() { return _uid; }
92    inline uint64_t euid() { return _euid; }
93    inline uint64_t gid() { return _gid; }
94    inline uint64_t egid() { return _egid; }
95    inline uint64_t pid() { return _pid; }
96    inline uint64_t ppid() { return _ppid; }
97
98    const char *progName() const { return executable.c_str(); }
99    std::string fullPath(const std::string &filename);
100    std::string getcwd() const { return cwd; }
101
102    /**
103     * Find an emulated device driver.
104     *
105     * @param filename Name of the device (under /dev)
106     * @return Pointer to driver object if found, else NULL
107     */
108    EmulatedDriver *findDriver(std::string filename);
109
110    // This function acts as a callback to update the bias value in
111    // the object file because the parameters needed to calculate the
112    // bias are not available when the object file is created.
113    void updateBias();
114    Addr getBias();
115    Addr getStartPC();
116    ObjectFile *getInterpreter();
117
118    // inherit file descriptor map from another process (necessary for clone)
119    void inheritFDArray(Process *p);
120
121    // override of virtual SimObject method: register statistics
122    void regStats() override;
123
124    // generate new target fd for sim_fd
125    int allocFD(int sim_fd, const std::string& filename, int flags, int mode,
126                bool pipe);
127
128    // disassociate target fd with simulator fd and cleanup subsidiary fields
129    void resetFDEntry(int tgt_fd);
130
131    // look up simulator fd for given target fd
132    int getSimFD(int tgt_fd);
133
134    // look up fd entry for a given target fd
135    FDEntry *getFDEntry(int tgt_fd);
136
137    // look up target fd for given host fd
138    // Assumes a 1:1 mapping between target file descriptor and host file
139    // descriptor. Given the current API, this must be true given that it's
140    // not possible to map multiple target file descriptors to the same host
141    // file descriptor
142    int getTgtFD(int sim_fd);
143
144    // fix all offsets for currently open files and save them
145    void fixFileOffsets();
146
147    // find all offsets for currently open files and save them
148    void findFileOffsets();
149
150    // set the source of this read pipe for a checkpoint resume
151    void setReadPipeSource(int read_pipe_fd, int source_fd);
152
153
154    void allocateMem(Addr vaddr, int64_t size, bool clobber = false);
155
156    /// Attempt to fix up a fault at vaddr by allocating a page on the stack.
157    /// @return Whether the fault has been fixed.
158    bool fixupStackFault(Addr vaddr);
159
160    // After getting registered with system object, tell process which
161    // system-wide context id it is assigned.
162    void
163    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    /**
172     * Does mmap region grow upward or downward from mmap_end?  Most
173     * platforms grow downward, but a few (such as Alpha) grow upward
174     * instead, so they can override this method to return false.
175     */
176    virtual bool mmapGrowsDown() const { return true; }
177
178    /**
179     * Maps a contiguous range of virtual addresses in this process's
180     * address space to a contiguous range of physical addresses.
181     * This function exists primarily to expose the map operation to
182     * python, so that configuration scripts can set up mappings in SE mode.
183     *
184     * @param vaddr The starting virtual address of the range.
185     * @param paddr The starting physical address of the range.
186     * @param size The length of the range in bytes.
187     * @param cacheable Specifies whether accesses are cacheable.
188     * @return True if the map operation was successful.  (At this
189     *           point in time, the map operation always succeeds.)
190     */
191    bool map(Addr vaddr, Addr paddr, int size, bool cacheable = true);
192
193    // list of all blocked contexts
194    std::list<WaitRec> waitList;
195
196    // thread contexts associated with this process
197    std::vector<ContextID> contextIds;
198
199    // system object which owns this process
200    System *system;
201
202    Addr brk_point;              // top of the data segment
203    Addr stack_base;             // stack segment base
204    unsigned stack_size;         // initial stack size
205    Addr stack_min;              // furthest address accessed from stack base
206    Addr max_stack_size;         // the maximum size allowed for the stack
207    Addr next_thread_stack_base; // addr for next region w/ multithreaded apps
208    Addr mmap_end;               // base of automatic mmap region allocs
209
210    Stats::Scalar num_syscalls;  // track how many system calls are executed
211
212    bool useArchPT; // flag for using architecture specific page table
213    bool kvmInSE;   // running KVM requires special initialization
214
215    PageTableBase* pTable;
216
217    SETranslatingPortProxy initVirtMem; // memory proxy for initial image load
218
219    static const int NUM_FDS = 1024;
220
221    // File descriptor remapping support.
222    std::shared_ptr<std::array<FDEntry, NUM_FDS>> fd_array;
223
224    // Standard file descriptor options for initialization and checkpoints.
225    std::map<std::string, int> imap;
226    std::map<std::string, int> oemap;
227
228    ObjectFile *objFile;
229    std::vector<std::string> argv;
230    std::vector<std::string> envp;
231    std::string cwd;
232    std::string executable;
233
234    // Id of the owner of the process
235    uint64_t _uid;
236    uint64_t _euid;
237    uint64_t _gid;
238    uint64_t _egid;
239
240    // pid of the process and it's parent
241    uint64_t _pid;
242    uint64_t _ppid;
243
244    // Emulated drivers available to this process
245    std::vector<EmulatedDriver *> drivers;
246};
247
248#endif // __PROCESS_HH__
249