process.hh revision 2190
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
29#ifndef __PROCESS_HH__
30#define __PROCESS_HH__
31
32//
33// The purpose of this code is to fake the loader & syscall mechanism
34// when there's no OS: thus there's no reason to use it in FULL_SYSTEM
35// mode when we do have an OS.
36//
37#include "config/full_system.hh"
38
39#if !FULL_SYSTEM
40
41#include <vector>
42
43#include "arch/isa_traits.hh"
44#include "sim/sim_object.hh"
45#include "sim/stats.hh"
46#include "base/statistics.hh"
47#include "base/trace.hh"
48
49class CPUExecContext;
50class ExecContext;
51class FunctionalMemory;
52class SyscallDesc;
53class Process : public SimObject
54{
55  protected:
56    typedef TheISA::RegFile RegFile;
57    typedef TheISA::MachInst MachInst;
58  public:
59
60    // have we initialized an execution context from this process?  If
61    // yes, subsequent contexts are assumed to be for dynamically
62    // created threads and are not initialized.
63    bool initialContextLoaded;
64
65    // execution contexts associated with this process
66    std::vector<ExecContext *> execContexts;
67
68    // number of CPUs (esxec contexts, really) assigned to this process.
69    unsigned int numCpus() { return execContexts.size(); }
70
71    // record of blocked context
72    struct WaitRec
73    {
74        Addr waitChan;
75        ExecContext *waitingContext;
76
77        WaitRec(Addr chan, ExecContext *ctx)
78            : waitChan(chan), waitingContext(ctx)
79        {
80        }
81    };
82
83    // list of all blocked contexts
84    std::list<WaitRec> waitList;
85
86    RegFile *init_regs;		// initial register contents
87    CPUExecContext *cpuXC;      // XC to hold the init_regs
88
89    Addr text_base;		// text (code) segment base
90    unsigned text_size;		// text (code) size in bytes
91
92    Addr data_base;		// initialized data segment base
93    unsigned data_size;		// initialized data + bss size in bytes
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    // addr to use for next stack region (for multithreaded apps)
102    Addr next_thread_stack_base;
103
104    // Base of region for mmaps (when user doesn't specify an address).
105    Addr mmap_start;
106    Addr mmap_end;
107
108    // Base of region for nxm data
109    Addr nxm_start;
110    Addr nxm_end;
111
112    std::string prog_fname;	// file name
113    Addr prog_entry;		// entry point (initial PC)
114
115    Stats::Scalar<> num_syscalls;	// number of syscalls executed
116
117
118  protected:
119    // constructor
120    Process(const std::string &nm,
121            int stdin_fd, 	// initial I/O descriptors
122            int stdout_fd,
123            int stderr_fd);
124
125    // post initialization startup
126    virtual void startup();
127
128  protected:
129    FunctionalMemory *memory;
130
131  private:
132    // file descriptor remapping support
133    static const int MAX_FD = 256;	// max legal fd value
134    int fd_map[MAX_FD+1];
135
136  public:
137    // static helper functions to generate file descriptors for constructor
138    static int openInputFile(const std::string &filename);
139    static int openOutputFile(const std::string &filename);
140
141    // override of virtual SimObject method: register statistics
142    virtual void regStats();
143
144    // register an execution context for this process.
145    // returns xc's cpu number (index into execContexts[])
146    int registerExecContext(ExecContext *xc);
147
148
149    void replaceExecContext(ExecContext *xc, int xcIndex);
150
151    // map simulator fd sim_fd to target fd tgt_fd
152    void dup_fd(int sim_fd, int tgt_fd);
153
154    // generate new target fd for sim_fd
155    int alloc_fd(int sim_fd);
156
157    // free target fd (e.g., after close)
158    void free_fd(int tgt_fd);
159
160    // look up simulator fd for given target fd
161    int sim_fd(int tgt_fd);
162
163    // is this a valid instruction fetch address?
164    bool validInstAddr(Addr addr)
165    {
166        return (text_base <= addr &&
167                addr < text_base + text_size &&
168                !(addr & (sizeof(MachInst)-1)));
169    }
170
171    // is this a valid address? (used to filter data fetches)
172    // note that we just assume stack size <= 16MB
173    // this may be alpha-specific
174    bool validDataAddr(Addr addr)
175    {
176        return ((data_base <= addr && addr < brk_point) ||
177                (next_thread_stack_base <= addr && addr < stack_base) ||
178                (text_base <= addr && addr < (text_base + text_size)) ||
179                (mmap_start <= addr && addr < mmap_end) ||
180                (nxm_start <= addr && addr < nxm_end));
181    }
182
183    virtual void syscall(ExecContext *xc) = 0;
184
185    virtual FunctionalMemory *getMemory() { return memory; }
186};
187
188//
189// "Live" process with system calls redirected to host system
190//
191class ObjectFile;
192class LiveProcess : public Process
193{
194  protected:
195    LiveProcess(const std::string &nm, ObjectFile *objFile,
196                int stdin_fd, int stdout_fd, int stderr_fd,
197                std::vector<std::string> &argv,
198                std::vector<std::string> &envp);
199
200  public:
201    // this function is used to create the LiveProcess object, since
202    // we can't tell which subclass of LiveProcess to use until we
203    // open and look at the object file.
204    static LiveProcess *create(const std::string &nm,
205                               int stdin_fd, int stdout_fd, int stderr_fd,
206                               std::string executable,
207                               std::vector<std::string> &argv,
208                               std::vector<std::string> &envp);
209
210    virtual void syscall(ExecContext *xc);
211
212    virtual SyscallDesc* getDesc(int callnum) { panic("Must be implemented."); }
213
214};
215
216
217#endif // !FULL_SYSTEM
218
219#endif // __PROCESS_HH__
220