process.hh revision 3311
12SN/A/*
21762SN/A * Copyright (c) 2001-2005 The Regents of The University of Michigan
32SN/A * All rights reserved.
42SN/A *
52SN/A * Redistribution and use in source and binary forms, with or without
62SN/A * modification, are permitted provided that the following conditions are
72SN/A * met: redistributions of source code must retain the above copyright
82SN/A * notice, this list of conditions and the following disclaimer;
92SN/A * redistributions in binary form must reproduce the above copyright
102SN/A * notice, this list of conditions and the following disclaimer in the
112SN/A * documentation and/or other materials provided with the distribution;
122SN/A * neither the name of the copyright holders nor the names of its
132SN/A * contributors may be used to endorse or promote products derived from
142SN/A * this software without specific prior written permission.
152SN/A *
162SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665Ssaidi@eecs.umich.edu *
282665Ssaidi@eecs.umich.edu * Authors: Nathan Binkert
292665Ssaidi@eecs.umich.edu *          Steve Reinhardt
302SN/A */
312SN/A
32360SN/A#ifndef __PROCESS_HH__
33360SN/A#define __PROCESS_HH__
342SN/A
352SN/A//
362SN/A// The purpose of this code is to fake the loader & syscall mechanism
372SN/A// when there's no OS: thus there's no reason to use it in FULL_SYSTEM
382SN/A// mode when we do have an OS.
392SN/A//
401858SN/A#include "config/full_system.hh"
411858SN/A
421858SN/A#if !FULL_SYSTEM
432SN/A
44180SN/A#include <vector>
452SN/A
462378SN/A#include "base/statistics.hh"
4756SN/A#include "sim/sim_object.hh"
482SN/A
492680Sktlim@umich.educlass ThreadContext;
502093SN/Aclass SyscallDesc;
512462SN/Aclass PageTable;
522401SN/Aclass TranslatingPort;
532378SN/Aclass System;
542378SN/A
552561SN/Avoid
562561SN/AcopyStringArray(std::vector<std::string> &strings, Addr array_ptr,
572561SN/A        Addr data_ptr, TranslatingPort* memPort);
582561SN/A
592SN/Aclass Process : public SimObject
602SN/A{
612SN/A  public:
622SN/A
632378SN/A    /// Pointer to object representing the system this process is
642378SN/A    /// running on.
652378SN/A    System *system;
662378SN/A
672680Sktlim@umich.edu    // have we initialized a thread context from this process?  If
682SN/A    // yes, subsequent contexts are assumed to be for dynamically
692SN/A    // created threads and are not initialized.
702SN/A    bool initialContextLoaded;
712SN/A
722680Sktlim@umich.edu    // thread contexts associated with this process
732680Sktlim@umich.edu    std::vector<ThreadContext *> threadContexts;
74180SN/A
75180SN/A    // number of CPUs (esxec contexts, really) assigned to this process.
762680Sktlim@umich.edu    unsigned int numCpus() { return threadContexts.size(); }
772SN/A
782SN/A    // record of blocked context
792SN/A    struct WaitRec
802SN/A    {
812SN/A        Addr waitChan;
822680Sktlim@umich.edu        ThreadContext *waitingContext;
832SN/A
842680Sktlim@umich.edu        WaitRec(Addr chan, ThreadContext *ctx)
852SN/A            : waitChan(chan), waitingContext(ctx)
862378SN/A        {	}
872SN/A    };
882SN/A
892SN/A    // list of all blocked contexts
902SN/A    std::list<WaitRec> waitList;
912SN/A
922SN/A    Addr brk_point;		// top of the data segment
932SN/A
942SN/A    Addr stack_base;		// stack segment base (highest address)
952SN/A    unsigned stack_size;	// initial stack size
962SN/A    Addr stack_min;		// lowest address accessed on the stack
972SN/A
982SN/A    // addr to use for next stack region (for multithreaded apps)
992SN/A    Addr next_thread_stack_base;
1002SN/A
101360SN/A    // Base of region for mmaps (when user doesn't specify an address).
1021408SN/A    Addr mmap_start;
1031408SN/A    Addr mmap_end;
104360SN/A
1051514SN/A    // Base of region for nxm data
1061514SN/A    Addr nxm_start;
1071514SN/A    Addr nxm_end;
1081514SN/A
1092SN/A    std::string prog_fname;	// file name
1102SN/A
111729SN/A    Stats::Scalar<> num_syscalls;	// number of syscalls executed
1122SN/A
1132SN/A
1142SN/A  protected:
1152SN/A    // constructor
1161450SN/A    Process(const std::string &nm,
1172378SN/A            System *_system,
1182SN/A            int stdin_fd, 	// initial I/O descriptors
1192SN/A            int stdout_fd,
1202SN/A            int stderr_fd);
1212SN/A
1221395SN/A    // post initialization startup
1231395SN/A    virtual void startup();
1242SN/A
1252SN/A  protected:
1262378SN/A    /// Memory object for initialization (image loading)
1272401SN/A    TranslatingPort *initVirtMem;
1282378SN/A
1292378SN/A  public:
1302378SN/A    PageTable *pTable;
1312SN/A
1322SN/A  private:
1332SN/A    // file descriptor remapping support
1341970SN/A    static const int MAX_FD = 256;	// max legal fd value
1352SN/A    int fd_map[MAX_FD+1];
1362SN/A
1372SN/A  public:
1382SN/A    // static helper functions to generate file descriptors for constructor
1392SN/A    static int openInputFile(const std::string &filename);
1402SN/A    static int openOutputFile(const std::string &filename);
1412SN/A
1422SN/A    // override of virtual SimObject method: register statistics
1432SN/A    virtual void regStats();
1442SN/A
1452680Sktlim@umich.edu    // register a thread context for this process.
1462680Sktlim@umich.edu    // returns tc's cpu number (index into threadContexts[])
1472680Sktlim@umich.edu    int registerThreadContext(ThreadContext *tc);
148180SN/A
149180SN/A
1502680Sktlim@umich.edu    void replaceThreadContext(ThreadContext *tc, int tcIndex);
1512SN/A
1522SN/A    // map simulator fd sim_fd to target fd tgt_fd
1532SN/A    void dup_fd(int sim_fd, int tgt_fd);
1542SN/A
1552SN/A    // generate new target fd for sim_fd
1561970SN/A    int alloc_fd(int sim_fd);
1571970SN/A
1581970SN/A    // free target fd (e.g., after close)
1591970SN/A    void free_fd(int tgt_fd);
1602SN/A
1612SN/A    // look up simulator fd for given target fd
1622SN/A    int sim_fd(int tgt_fd);
1632SN/A
1642680Sktlim@umich.edu    virtual void syscall(int64_t callnum, ThreadContext *tc) = 0;
1653311Ssaidi@eecs.umich.edu
1663311Ssaidi@eecs.umich.edu    void serialize(std::ostream &os);
1673311Ssaidi@eecs.umich.edu    void unserialize(Checkpoint *cp, const std::string &section);
1682SN/A};
1692SN/A
1702SN/A//
1712SN/A// "Live" process with system calls redirected to host system
1722SN/A//
173360SN/Aclass ObjectFile;
1742SN/Aclass LiveProcess : public Process
1752SN/A{
176360SN/A  protected:
1772378SN/A    ObjectFile *objFile;
1782378SN/A    std::vector<std::string> argv;
1792378SN/A    std::vector<std::string> envp;
1802378SN/A
1811450SN/A    LiveProcess(const std::string &nm, ObjectFile *objFile,
1822378SN/A                System *_system, int stdin_fd, int stdout_fd, int stderr_fd,
1832SN/A                std::vector<std::string> &argv,
1843114Sgblack@eecs.umich.edu                std::vector<std::string> &envp,
1853114Sgblack@eecs.umich.edu                uint64_t _uid, uint64_t _euid,
1863114Sgblack@eecs.umich.edu                uint64_t _gid, uint64_t _egid,
1873114Sgblack@eecs.umich.edu                uint64_t _pid, uint64_t _ppid);
1882SN/A
1892561SN/A    virtual void argsInit(int intSize, int pageSize);
1902378SN/A
1913114Sgblack@eecs.umich.edu    // Id of the owner of the process
1923114Sgblack@eecs.umich.edu    uint64_t __uid;
1933114Sgblack@eecs.umich.edu    uint64_t __euid;
1943114Sgblack@eecs.umich.edu    uint64_t __gid;
1953114Sgblack@eecs.umich.edu    uint64_t __egid;
1963114Sgblack@eecs.umich.edu
1973114Sgblack@eecs.umich.edu    // pid of the process and it's parent
1983114Sgblack@eecs.umich.edu    uint64_t __pid;
1993114Sgblack@eecs.umich.edu    uint64_t __ppid;
2003114Sgblack@eecs.umich.edu
201360SN/A  public:
2023114Sgblack@eecs.umich.edu
2033114Sgblack@eecs.umich.edu    inline uint64_t uid() {return __uid;}
2043114Sgblack@eecs.umich.edu    inline uint64_t euid() {return __euid;}
2053114Sgblack@eecs.umich.edu    inline uint64_t gid() {return __gid;}
2063114Sgblack@eecs.umich.edu    inline uint64_t egid() {return __egid;}
2073114Sgblack@eecs.umich.edu    inline uint64_t pid() {return __pid;}
2083114Sgblack@eecs.umich.edu    inline uint64_t ppid() {return __ppid;}
2093114Sgblack@eecs.umich.edu
2102680Sktlim@umich.edu    virtual void syscall(int64_t callnum, ThreadContext *tc);
2112093SN/A
2122462SN/A    virtual SyscallDesc* getDesc(int callnum) = 0;
2132715Sstever@eecs.umich.edu
2142715Sstever@eecs.umich.edu    // this function is used to create the LiveProcess object, since
2152715Sstever@eecs.umich.edu    // we can't tell which subclass of LiveProcess to use until we
2162715Sstever@eecs.umich.edu    // open and look at the object file.
2172715Sstever@eecs.umich.edu    static LiveProcess *create(const std::string &nm,
2182715Sstever@eecs.umich.edu                               System *_system,
2192715Sstever@eecs.umich.edu                               int stdin_fd, int stdout_fd, int stderr_fd,
2202715Sstever@eecs.umich.edu                               std::string executable,
2212715Sstever@eecs.umich.edu                               std::vector<std::string> &argv,
2223114Sgblack@eecs.umich.edu                               std::vector<std::string> &envp,
2233114Sgblack@eecs.umich.edu                               uint64_t _uid, uint64_t _euid,
2243114Sgblack@eecs.umich.edu                               uint64_t _gid, uint64_t _egid,
2253114Sgblack@eecs.umich.edu                               uint64_t _pid, uint64_t _ppid);
2262SN/A};
2272SN/A
228360SN/A
2292SN/A#endif // !FULL_SYSTEM
2302SN/A
231360SN/A#endif // __PROCESS_HH__
232