process.hh revision 180
112841Sgabeblack@google.com/*
212841Sgabeblack@google.com * Copyright (c) 2003 The Regents of The University of Michigan
312841Sgabeblack@google.com * All rights reserved.
412841Sgabeblack@google.com *
512841Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
612841Sgabeblack@google.com * modification, are permitted provided that the following conditions are
712841Sgabeblack@google.com * met: redistributions of source code must retain the above copyright
812841Sgabeblack@google.com * notice, this list of conditions and the following disclaimer;
912841Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright
1012841Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the
1112841Sgabeblack@google.com * documentation and/or other materials provided with the distribution;
1212841Sgabeblack@google.com * neither the name of the copyright holders nor the names of its
1312841Sgabeblack@google.com * contributors may be used to endorse or promote products derived from
1412841Sgabeblack@google.com * this software without specific prior written permission.
1512841Sgabeblack@google.com *
1612841Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1712841Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1812841Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1912841Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2012841Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2112841Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2212841Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2312841Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2412841Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2512841Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2612841Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2712841Sgabeblack@google.com */
2812841Sgabeblack@google.com
2912841Sgabeblack@google.com#ifndef __PROG_HH__
3012841Sgabeblack@google.com#define __PROG_HH__
3112841Sgabeblack@google.com
3212841Sgabeblack@google.com//
3312841Sgabeblack@google.com// The purpose of this code is to fake the loader & syscall mechanism
3412841Sgabeblack@google.com// when there's no OS: thus there's no reason to use it in FULL_SYSTEM
3512841Sgabeblack@google.com// mode when we do have an OS.
3612841Sgabeblack@google.com//
3712841Sgabeblack@google.com#ifndef FULL_SYSTEM
3812841Sgabeblack@google.com
3912841Sgabeblack@google.com#include <vector>
4012841Sgabeblack@google.com
4112841Sgabeblack@google.com#include "targetarch/isa_traits.hh"
4212841Sgabeblack@google.com#include "sim/sim_object.hh"
4312841Sgabeblack@google.com#include "sim/sim_stats.hh"
4412841Sgabeblack@google.com#include "base/statistics.hh"
4512841Sgabeblack@google.com
4612841Sgabeblack@google.comclass ExecContext;
4712841Sgabeblack@google.comclass FunctionalMemory;
4812841Sgabeblack@google.comclass Process : public SimObject
4912841Sgabeblack@google.com{
5012841Sgabeblack@google.com  public:
5112841Sgabeblack@google.com
5212841Sgabeblack@google.com    // have we initialized an execution context from this process?  If
5312841Sgabeblack@google.com    // yes, subsequent contexts are assumed to be for dynamically
5412841Sgabeblack@google.com    // created threads and are not initialized.
5512841Sgabeblack@google.com    bool initialContextLoaded;
5612841Sgabeblack@google.com
5712841Sgabeblack@google.com    // execution contexts associated with this process
5812841Sgabeblack@google.com    std::vector<ExecContext *> execContexts;
5912841Sgabeblack@google.com
6012841Sgabeblack@google.com    // number of CPUs (esxec contexts, really) assigned to this process.
6112841Sgabeblack@google.com    unsigned int numCpus() { return execContexts.size(); }
6212841Sgabeblack@google.com
6312841Sgabeblack@google.com    // record of blocked context
6412841Sgabeblack@google.com    struct WaitRec
6512841Sgabeblack@google.com    {
6612841Sgabeblack@google.com        Addr waitChan;
6712841Sgabeblack@google.com        ExecContext *waitingContext;
6812841Sgabeblack@google.com
6912841Sgabeblack@google.com        WaitRec(Addr chan, ExecContext *ctx)
7012841Sgabeblack@google.com            : waitChan(chan), waitingContext(ctx)
7112841Sgabeblack@google.com        {
7212841Sgabeblack@google.com        }
7312841Sgabeblack@google.com    };
7412841Sgabeblack@google.com
7512841Sgabeblack@google.com    // list of all blocked contexts
7612841Sgabeblack@google.com    std::list<WaitRec> waitList;
7712841Sgabeblack@google.com
7812841Sgabeblack@google.com    RegFile *init_regs;		// initial register contents
7912841Sgabeblack@google.com
8012841Sgabeblack@google.com    Addr text_base;		// text (code) segment base
8112841Sgabeblack@google.com    unsigned text_size;		// text (code) size in bytes
8212841Sgabeblack@google.com
8312841Sgabeblack@google.com    Addr data_base;		// initialized data segment base
8412841Sgabeblack@google.com    unsigned data_size;		// initialized data + bss size in bytes
8512841Sgabeblack@google.com
8612841Sgabeblack@google.com    Addr brk_point;		// top of the data segment
8712841Sgabeblack@google.com
8812841Sgabeblack@google.com    Addr stack_base;		// stack segment base (highest address)
8912841Sgabeblack@google.com    unsigned stack_size;	// initial stack size
9012841Sgabeblack@google.com    Addr stack_min;		// lowest address accessed on the stack
9112841Sgabeblack@google.com
9212841Sgabeblack@google.com
9312841Sgabeblack@google.com    // addr to use for next stack region (for multithreaded apps)
9412841Sgabeblack@google.com    Addr next_thread_stack_base;
9512841Sgabeblack@google.com
9612841Sgabeblack@google.com    std::string prog_fname;	// file name
9712841Sgabeblack@google.com    Addr prog_entry;		// entry point (initial PC)
9812841Sgabeblack@google.com
99    Statistics::Scalar<> num_syscalls;	// number of syscalls executed
100
101
102  protected:
103    // constructor
104    Process(const std::string &name,
105            int stdin_fd, 	// initial I/O descriptors
106            int stdout_fd,
107            int stderr_fd);
108
109
110  protected:
111    FunctionalMemory *memory;
112
113  private:
114    // file descriptor remapping support
115    static const int MAX_FD = 100;	// max legal fd value
116    int fd_map[MAX_FD+1];
117
118  public:
119    // static helper functions to generate file descriptors for constructor
120    static int openInputFile(const std::string &filename);
121    static int openOutputFile(const std::string &filename);
122
123    // override of virtual SimObject method: register statistics
124    virtual void regStats();
125
126    // register an execution context for this process.
127    // returns xc's cpu number (index into execContexts[])
128    int registerExecContext(ExecContext *xc);
129
130
131    void replaceExecContext(int xcIndex, ExecContext *xc);
132
133    // map simulator fd sim_fd to target fd tgt_fd
134    void dup_fd(int sim_fd, int tgt_fd);
135
136    // generate new target fd for sim_fd
137    int open_fd(int sim_fd);
138
139    // look up simulator fd for given target fd
140    int sim_fd(int tgt_fd);
141
142    // is this a valid instruction fetch address?
143    bool validInstAddr(Addr addr)
144    {
145        return (text_base <= addr &&
146                addr < text_base + text_size &&
147                !(addr & (sizeof(MachInst)-1)));
148    }
149
150    // is this a valid address? (used to filter data fetches)
151    // note that we just assume stack size <= 16MB
152    // this may be alpha-specific
153    bool validDataAddr(Addr addr)
154    {
155        return ((data_base <= addr && addr < brk_point) ||
156                ((stack_base - 16*1024*1024) <= addr && addr < stack_base) ||
157                (text_base <= addr && addr < (text_base + text_size)));
158    }
159
160    virtual void syscall(ExecContext *xc) = 0;
161
162    virtual FunctionalMemory *getMemory() { return memory; }
163};
164
165//
166// "Live" process with system calls redirected to host system
167//
168class MainMemory;
169class LiveProcess : public Process
170{
171  public:
172    LiveProcess(const std::string &name,
173                int stdin_fd, int stdout_fd, int stderr_fd,
174                std::vector<std::string> &argv,
175                std::vector<std::string> &envp);
176
177    virtual void syscall(ExecContext *xc);
178};
179
180#endif // !FULL_SYSTEM
181
182#endif // __PROG_HH__
183