process.hh revision 11919:54c2e390fc22
18112Sgblack@eecs.umich.edu/*
28112Sgblack@eecs.umich.edu * Copyright (c) 2014-2016 Advanced Micro Devices, Inc.
38112Sgblack@eecs.umich.edu * Copyright (c) 2001-2005 The Regents of The University of Michigan
48112Sgblack@eecs.umich.edu * All rights reserved.
58112Sgblack@eecs.umich.edu *
68112Sgblack@eecs.umich.edu * Redistribution and use in source and binary forms, with or without
78112Sgblack@eecs.umich.edu * modification, are permitted provided that the following conditions are
88112Sgblack@eecs.umich.edu * met: redistributions of source code must retain the above copyright
98112Sgblack@eecs.umich.edu * notice, this list of conditions and the following disclaimer;
108112Sgblack@eecs.umich.edu * redistributions in binary form must reproduce the above copyright
118112Sgblack@eecs.umich.edu * notice, this list of conditions and the following disclaimer in the
128112Sgblack@eecs.umich.edu * documentation and/or other materials provided with the distribution;
138112Sgblack@eecs.umich.edu * neither the name of the copyright holders nor the names of its
148112Sgblack@eecs.umich.edu * contributors may be used to endorse or promote products derived from
158112Sgblack@eecs.umich.edu * this software without specific prior written permission.
168112Sgblack@eecs.umich.edu *
178112Sgblack@eecs.umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
188112Sgblack@eecs.umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
198112Sgblack@eecs.umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
208112Sgblack@eecs.umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
218112Sgblack@eecs.umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
228112Sgblack@eecs.umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
238112Sgblack@eecs.umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
248112Sgblack@eecs.umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
258112Sgblack@eecs.umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
268112Sgblack@eecs.umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
278112Sgblack@eecs.umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
288112Sgblack@eecs.umich.edu *
298113Sgblack@eecs.umich.edu * Authors: Nathan Binkert
308113Sgblack@eecs.umich.edu *          Steve Reinhardt
318113Sgblack@eecs.umich.edu *          Brandon Potter
328113Sgblack@eecs.umich.edu */
338113Sgblack@eecs.umich.edu
348113Sgblack@eecs.umich.edu#ifndef __PROCESS_HH__
358113Sgblack@eecs.umich.edu#define __PROCESS_HH__
368113Sgblack@eecs.umich.edu
378113Sgblack@eecs.umich.edu#include <inttypes.h>
388113Sgblack@eecs.umich.edu
398113Sgblack@eecs.umich.edu#include <map>
408113Sgblack@eecs.umich.edu#include <string>
418113Sgblack@eecs.umich.edu#include <vector>
428113Sgblack@eecs.umich.edu
438113Sgblack@eecs.umich.edu#include "arch/registers.hh"
448113Sgblack@eecs.umich.edu#include "base/statistics.hh"
458113Sgblack@eecs.umich.edu#include "base/types.hh"
468113Sgblack@eecs.umich.edu#include "config/the_isa.hh"
478113Sgblack@eecs.umich.edu#include "mem/se_translating_port_proxy.hh"
488113Sgblack@eecs.umich.edu#include "sim/fd_array.hh"
498113Sgblack@eecs.umich.edu#include "sim/fd_entry.hh"
508113Sgblack@eecs.umich.edu#include "sim/mem_state.hh"
518113Sgblack@eecs.umich.edu#include "sim/sim_object.hh"
528113Sgblack@eecs.umich.edu
538113Sgblack@eecs.umich.edustruct ProcessParams;
548113Sgblack@eecs.umich.edu
558113Sgblack@eecs.umich.educlass EmulatedDriver;
568113Sgblack@eecs.umich.educlass ObjectFile;
578113Sgblack@eecs.umich.educlass PageTableBase;
588113Sgblack@eecs.umich.educlass SyscallDesc;
598113Sgblack@eecs.umich.educlass SyscallReturn;
608113Sgblack@eecs.umich.educlass System;
618113Sgblack@eecs.umich.educlass ThreadContext;
628113Sgblack@eecs.umich.edu
638113Sgblack@eecs.umich.educlass Process : public SimObject
648113Sgblack@eecs.umich.edu{
658113Sgblack@eecs.umich.edu  public:
668113Sgblack@eecs.umich.edu    Process(ProcessParams *params, ObjectFile *obj_file);
678113Sgblack@eecs.umich.edu
68    void serialize(CheckpointOut &cp) const override;
69    void unserialize(CheckpointIn &cp) override;
70
71    void initState() override;
72    DrainState drain() override;
73
74    void syscall(int64_t callnum, ThreadContext *tc, Fault *fault);
75    virtual TheISA::IntReg getSyscallArg(ThreadContext *tc, int &i) = 0;
76    virtual TheISA::IntReg getSyscallArg(ThreadContext *tc, int &i, int width);
77    virtual void setSyscallArg(ThreadContext *tc, int i,
78                               TheISA::IntReg val) = 0;
79    virtual void setSyscallReturn(ThreadContext *tc,
80                                  SyscallReturn return_value) = 0;
81    virtual SyscallDesc *getDesc(int callnum) = 0;
82
83    inline uint64_t uid() { return _uid; }
84    inline uint64_t euid() { return _euid; }
85    inline uint64_t gid() { return _gid; }
86    inline uint64_t egid() { return _egid; }
87    inline uint64_t pid() { return _pid; }
88    inline uint64_t ppid() { return _ppid; }
89    inline uint64_t pgid() { return _pgid; }
90    inline uint64_t tgid() { return _tgid; }
91    inline void setpgid(uint64_t pgid) { _pgid = pgid; }
92
93    const char *progName() const { return executable.c_str(); }
94    std::string fullPath(const std::string &filename);
95    std::string getcwd() const { return cwd; }
96
97    /**
98     * Find an emulated device driver.
99     *
100     * @param filename Name of the device (under /dev)
101     * @return Pointer to driver object if found, else nullptr
102     */
103    EmulatedDriver *findDriver(std::string filename);
104
105    // This function acts as a callback to update the bias value in
106    // the object file because the parameters needed to calculate the
107    // bias are not available when the object file is created.
108    void updateBias();
109    Addr getBias();
110    Addr getStartPC();
111    ObjectFile *getInterpreter();
112
113    // override of virtual SimObject method: register statistics
114    void regStats() override;
115
116    void allocateMem(Addr vaddr, int64_t size, bool clobber = false);
117
118    /// Attempt to fix up a fault at vaddr by allocating a page on the stack.
119    /// @return Whether the fault has been fixed.
120    bool fixupStackFault(Addr vaddr);
121
122    // After getting registered with system object, tell process which
123    // system-wide context id it is assigned.
124    void
125    assignThreadContext(ContextID context_id)
126    {
127        contextIds.push_back(context_id);
128    }
129
130    // Find a free context to use
131    ThreadContext *findFreeContext();
132
133    /**
134     * After delegating a thread context to a child process
135     * no longer should relate to the ThreadContext
136     */
137    void revokeThreadContext(int context_id);
138
139    /**
140     * Does mmap region grow upward or downward from mmapEnd?  Most
141     * platforms grow downward, but a few (such as Alpha) grow upward
142     * instead, so they can override this method to return false.
143     */
144    virtual bool mmapGrowsDown() const { return true; }
145
146    /**
147     * Maps a contiguous range of virtual addresses in this process's
148     * address space to a contiguous range of physical addresses.
149     * This function exists primarily to expose the map operation to
150     * python, so that configuration scripts can set up mappings in SE mode.
151     *
152     * @param vaddr The starting virtual address of the range.
153     * @param paddr The starting physical address of the range.
154     * @param size The length of the range in bytes.
155     * @param cacheable Specifies whether accesses are cacheable.
156     * @return True if the map operation was successful.  (At this
157     *           point in time, the map operation always succeeds.)
158     */
159    bool map(Addr vaddr, Addr paddr, int size, bool cacheable = true);
160
161    void replicatePage(Addr vaddr, Addr new_paddr, ThreadContext *old_tc,
162                       ThreadContext *new_tc, bool alloc_page);
163
164    void clone(ThreadContext *old_tc, ThreadContext *new_tc, Process *new_p,
165               TheISA::IntReg flags);
166
167    // thread contexts associated with this process
168    std::vector<ContextID> contextIds;
169
170    // system object which owns this process
171    System *system;
172
173    Stats::Scalar numSyscalls;  // track how many system calls are executed
174
175    bool useArchPT; // flag for using architecture specific page table
176    bool kvmInSE;   // running KVM requires special initialization
177
178    PageTableBase* pTable;
179
180    SETranslatingPortProxy initVirtMem; // memory proxy for initial image load
181
182    ObjectFile *objFile;
183    std::vector<std::string> argv;
184    std::vector<std::string> envp;
185    std::string cwd;
186    std::string executable;
187
188    // Id of the owner of the process
189    uint64_t _uid;
190    uint64_t _euid;
191    uint64_t _gid;
192    uint64_t _egid;
193
194    // pid of the process and it's parent
195    uint64_t _pid;
196    uint64_t _ppid;
197    uint64_t _pgid;
198    uint64_t _tgid;
199
200    // Emulated drivers available to this process
201    std::vector<EmulatedDriver *> drivers;
202
203    std::shared_ptr<FDArray> fds;
204
205    bool *exitGroup;
206    std::shared_ptr<MemState> memState;
207
208    /**
209     * Calls a futex wakeup at the address specified by this pointer when
210     * this process exits.
211     */
212    uint64_t childClearTID;
213
214    // Process was forked with SIGCHLD set.
215    bool *sigchld;
216};
217
218#endif // __PROCESS_HH__
219