process.hh revision 2378
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 "base/statistics.hh"
44#include "base/trace.hh"
45#include "mem/base_mem.hh"
46#include "mem/mem_interface.hh"
47#include "mem/page_table.hh"
48#include "sim/sim_object.hh"
49#include "sim/stats.hh"
50#include "targetarch/isa_traits.hh"
51
52class ExecContext;
53class FunctionalMemory;
54class System;
55
56class Process : public SimObject
57{
58  public:
59
60    /// Pointer to object representing the system this process is
61    /// running on.
62    System *system;
63
64    // have we initialized an execution context from this process?  If
65    // yes, subsequent contexts are assumed to be for dynamically
66    // created threads and are not initialized.
67    bool initialContextLoaded;
68
69    // execution contexts associated with this process
70    std::vector<ExecContext *> execContexts;
71
72    // number of CPUs (esxec contexts, really) assigned to this process.
73    unsigned int numCpus() { return execContexts.size(); }
74
75    // record of blocked context
76    struct WaitRec
77    {
78        Addr waitChan;
79        ExecContext *waitingContext;
80
81        WaitRec(Addr chan, ExecContext *ctx)
82            : waitChan(chan), waitingContext(ctx)
83        {	}
84    };
85
86    // list of all blocked contexts
87    std::list<WaitRec> waitList;
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            System *_system,
122            int stdin_fd, 	// initial I/O descriptors
123            int stdout_fd,
124            int stderr_fd);
125
126    // post initialization startup
127    virtual void startup();
128
129  protected:
130    /// Memory object for initialization (image loading)
131    FunctionalMemory *initVirtMem;
132
133  public:
134    PageTable *pTable;
135
136  private:
137    // file descriptor remapping support
138    static const int MAX_FD = 256;	// max legal fd value
139    int fd_map[MAX_FD+1];
140
141  public:
142    // static helper functions to generate file descriptors for constructor
143    static int openInputFile(const std::string &filename);
144    static int openOutputFile(const std::string &filename);
145
146    // override of virtual SimObject method: register statistics
147    virtual void regStats();
148
149    // register an execution context for this process.
150    // returns xc's cpu number (index into execContexts[])
151    int registerExecContext(ExecContext *xc);
152
153
154    void replaceExecContext(ExecContext *xc, int xcIndex);
155
156    // map simulator fd sim_fd to target fd tgt_fd
157    void dup_fd(int sim_fd, int tgt_fd);
158
159    // generate new target fd for sim_fd
160    int alloc_fd(int sim_fd);
161
162    // free target fd (e.g., after close)
163    void free_fd(int tgt_fd);
164
165    // look up simulator fd for given target fd
166    int sim_fd(int tgt_fd);
167
168    // is this a valid instruction fetch address?
169    bool validInstAddr(Addr addr)
170    {
171        return (text_base <= addr &&
172                addr < text_base + text_size &&
173                !(addr & (sizeof(MachInst)-1)));
174    }
175
176    // is this a valid address? (used to filter data fetches)
177    // note that we just assume stack size <= 16MB
178    // this may be alpha-specific
179    bool validDataAddr(Addr addr)
180    {
181        return ((data_base <= addr && addr < brk_point) ||
182                (next_thread_stack_base <= addr && addr < stack_base) ||
183                (text_base <= addr && addr < (text_base + text_size)) ||
184                (mmap_start <= addr && addr < mmap_end) ||
185                (nxm_start <= addr && addr < nxm_end));
186    }
187
188    virtual void syscall(ExecContext *xc) = 0;
189};
190
191//
192// "Live" process with system calls redirected to host system
193//
194class ObjectFile;
195class LiveProcess : public Process
196{
197  protected:
198    ObjectFile *objFile;
199    std::vector<std::string> argv;
200    std::vector<std::string> envp;
201
202    LiveProcess(const std::string &nm, ObjectFile *objFile,
203                System *_system, int stdin_fd, int stdout_fd, int stderr_fd,
204                std::vector<std::string> &argv,
205                std::vector<std::string> &envp);
206
207    void startup();
208
209  public:
210    // this function is used to create the LiveProcess object, since
211    // we can't tell which subclass of LiveProcess to use until we
212    // open and look at the object file.
213    static LiveProcess *create(const std::string &nm,
214                               System *_system,
215                               int stdin_fd, int stdout_fd, int stderr_fd,
216                               std::string executable,
217                               std::vector<std::string> &argv,
218                               std::vector<std::string> &envp);
219};
220
221
222#endif // !FULL_SYSTEM
223
224#endif // __PROCESS_HH__
225