process.hh revision 7447:3fc243687abb
1/*
2 * Copyright (c) 2001-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Nathan Binkert
29 *          Steve Reinhardt
30 */
31
32#ifndef __PROCESS_HH__
33#define __PROCESS_HH__
34
35//
36// The purpose of this code is to fake the loader & syscall mechanism
37// when there's no OS: thus there's no reason to use it in FULL_SYSTEM
38// mode when we do have an OS.
39//
40#include "config/full_system.hh"
41
42#if !FULL_SYSTEM
43
44#include <string>
45#include <vector>
46
47#include "arch/registers.hh"
48#include "base/statistics.hh"
49#include "base/types.hh"
50#include "config/the_isa.hh"
51#include "sim/sim_object.hh"
52#include "sim/syscallreturn.hh"
53
54class PageTable;
55class ProcessParams;
56class LiveProcessParams;
57class SyscallDesc;
58class System;
59class ThreadContext;
60class TranslatingPort;
61
62template<class IntType>
63struct AuxVector
64{
65    IntType a_type;
66    IntType a_val;
67
68    AuxVector()
69    {}
70
71    AuxVector(IntType type, IntType val);
72};
73
74class Process : public SimObject
75{
76  public:
77
78    /// Pointer to object representing the system this process is
79    /// running on.
80    System *system;
81
82    // have we initialized a thread context from this process?  If
83    // yes, subsequent contexts are assumed to be for dynamically
84    // created threads and are not initialized.
85    bool initialContextLoaded;
86
87    bool checkpointRestored;
88
89    // thread contexts associated with this process
90    std::vector<int> contextIds;
91
92    // number of CPUs (esxec contexts, really) assigned to this process.
93    unsigned int numCpus() { return contextIds.size(); }
94
95    // record of blocked context
96    struct WaitRec
97    {
98        Addr waitChan;
99        ThreadContext *waitingContext;
100
101        WaitRec(Addr chan, ThreadContext *ctx)
102            : waitChan(chan), waitingContext(ctx)
103        {       }
104    };
105
106    // list of all blocked contexts
107    std::list<WaitRec> waitList;
108
109    Addr brk_point;             // top of the data segment
110
111    Addr stack_base;            // stack segment base (highest address)
112    unsigned stack_size;        // initial stack size
113    Addr stack_min;             // lowest address accessed on the stack
114
115    // The maximum size allowed for the stack.
116    Addr max_stack_size;
117
118    // addr to use for next stack region (for multithreaded apps)
119    Addr next_thread_stack_base;
120
121    // Base of region for mmaps (when user doesn't specify an address).
122    Addr mmap_start;
123    Addr mmap_end;
124
125    // Base of region for nxm data
126    Addr nxm_start;
127    Addr nxm_end;
128
129    std::string prog_fname;     // file name
130
131    Stats::Scalar num_syscalls;       // number of syscalls executed
132
133
134  protected:
135    // constructor
136    Process(ProcessParams * params);
137
138    // post initialization startup
139    virtual void startup();
140
141  protected:
142    /// Memory object for initialization (image loading)
143    TranslatingPort *initVirtMem;
144
145  public:
146    PageTable *pTable;
147
148    //This id is assigned by m5 and is used to keep process' tlb entries
149    //separated.
150    uint64_t M5_pid;
151
152    class FdMap
153    {
154      public:
155            int fd;
156            std::string filename;
157            int mode;
158            int flags;
159            bool isPipe;
160            int readPipeSource;
161            uint64_t fileOffset;
162
163
164            FdMap()
165            {
166                    fd = -1;
167                    filename = "NULL";
168                    mode = 0;
169                    flags = 0;
170                    isPipe = false;
171                    readPipeSource = 0;
172                    fileOffset = 0;
173
174            }
175
176        void serialize(std::ostream &os);
177        void unserialize(Checkpoint *cp, const std::string &section);
178
179    };
180
181  private:
182    // file descriptor remapping support
183    static const int MAX_FD = 256;    // max legal fd value
184    FdMap fd_map[MAX_FD+1];
185
186
187  public:
188    // static helper functions to generate file descriptors for constructor
189    static int openInputFile(const std::string &filename);
190    static int openOutputFile(const std::string &filename);
191
192    // override of virtual SimObject method: register statistics
193    virtual void regStats();
194
195    // After getting registered with system object, tell process which
196    // system-wide context id it is assigned.
197    void assignThreadContext(int context_id)
198    {
199        contextIds.push_back(context_id);
200    }
201
202    // Find a free context to use
203    ThreadContext * findFreeContext();
204
205    // map simulator fd sim_fd to target fd tgt_fd
206    void dup_fd(int sim_fd, int tgt_fd);
207
208    // generate new target fd for sim_fd
209    int alloc_fd(int sim_fd, std::string filename, int flags, int mode, bool pipe);
210
211    // free target fd (e.g., after close)
212    void free_fd(int tgt_fd);
213
214    // look up simulator fd for given target fd
215    int sim_fd(int tgt_fd);
216
217    // look up simulator fd_map object for a given target fd
218    FdMap * sim_fd_obj(int tgt_fd);
219
220    // fix all offsets for currently open files and save them
221    void fix_file_offsets();
222
223    // find all offsets for currently open files and save them
224    void find_file_offsets();
225
226    // set the source of this read pipe for a checkpoint resume
227    void setReadPipeSource(int read_pipe_fd, int source_fd);
228
229    virtual void syscall(int64_t callnum, ThreadContext *tc) = 0;
230
231    // check if the this addr is on the next available page and allocate it
232    // if it's not we'll panic
233    bool checkAndAllocNextPage(Addr vaddr);
234
235    void serialize(std::ostream &os);
236    void unserialize(Checkpoint *cp, const std::string &section);
237};
238
239//
240// "Live" process with system calls redirected to host system
241//
242class ObjectFile;
243class LiveProcess : public Process
244{
245  protected:
246    ObjectFile *objFile;
247    std::vector<std::string> argv;
248    std::vector<std::string> envp;
249    std::string cwd;
250
251    LiveProcess(LiveProcessParams * params, ObjectFile *objFile);
252
253    virtual void argsInit(int intSize, int pageSize);
254
255    // Id of the owner of the process
256    uint64_t __uid;
257    uint64_t __euid;
258    uint64_t __gid;
259    uint64_t __egid;
260
261    // pid of the process and it's parent
262    uint64_t __pid;
263    uint64_t __ppid;
264
265  public:
266
267    enum AuxiliaryVectorType {
268        M5_AT_NULL = 0,
269        M5_AT_IGNORE = 1,
270        M5_AT_EXECFD = 2,
271        M5_AT_PHDR = 3,
272        M5_AT_PHENT = 4,
273        M5_AT_PHNUM = 5,
274        M5_AT_PAGESZ = 6,
275        M5_AT_BASE = 7,
276        M5_AT_FLAGS = 8,
277        M5_AT_ENTRY = 9,
278        M5_AT_NOTELF = 10,
279        M5_AT_UID = 11,
280        M5_AT_EUID = 12,
281        M5_AT_GID = 13,
282        M5_AT_EGID = 14,
283        // The following may be specific to Linux
284        M5_AT_PLATFORM = 15,
285        M5_AT_HWCAP = 16,
286        M5_AT_CLKTCK = 17,
287
288        M5_AT_SECURE = 23,
289        M5_BASE_PLATFORM = 24,
290        M5_AT_RANDOM = 25,
291
292        M5_AT_EXECFN = 31,
293
294        M5_AT_VECTOR_SIZE = 44
295    };
296
297    inline uint64_t uid() {return __uid;}
298    inline uint64_t euid() {return __euid;}
299    inline uint64_t gid() {return __gid;}
300    inline uint64_t egid() {return __egid;}
301    inline uint64_t pid() {return __pid;}
302    inline uint64_t ppid() {return __ppid;}
303
304    std::string
305    fullPath(const std::string &filename)
306    {
307        if (filename[0] == '/' || cwd.empty())
308            return filename;
309
310        std::string full = cwd;
311
312        if (cwd[cwd.size() - 1] != '/')
313            full += '/';
314
315        return full + filename;
316    }
317
318    std::string getcwd() const { return cwd; }
319
320    virtual void syscall(int64_t callnum, ThreadContext *tc);
321
322    virtual TheISA::IntReg getSyscallArg(ThreadContext *tc, int &i) = 0;
323    virtual TheISA::IntReg getSyscallArg(ThreadContext *tc, int &i, int width);
324    virtual void setSyscallArg(ThreadContext *tc,
325            int i, TheISA::IntReg val) = 0;
326    virtual void setSyscallReturn(ThreadContext *tc,
327            SyscallReturn return_value) = 0;
328
329    virtual SyscallDesc* getDesc(int callnum) = 0;
330
331    // this function is used to create the LiveProcess object, since
332    // we can't tell which subclass of LiveProcess to use until we
333    // open and look at the object file.
334    static LiveProcess *create(LiveProcessParams * params);
335};
336
337
338#endif // !FULL_SYSTEM
339
340#endif // __PROCESS_HH__
341