process.hh revision 8852
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
354117Sgblack@eecs.umich.edu#include <string>
36180SN/A#include <vector>
372SN/A
386329Sgblack@eecs.umich.edu#include "arch/registers.hh"
392378SN/A#include "base/statistics.hh"
406214Snate@binkert.org#include "base/types.hh"
416658Snate@binkert.org#include "config/the_isa.hh"
428852Sandreas.hansson@arm.com#include "mem/se_translating_port_proxy.hh"
4356SN/A#include "sim/sim_object.hh"
445958Sgblack@eecs.umich.edu#include "sim/syscallreturn.hh"
452SN/A
465154Sgblack@eecs.umich.educlass PageTable;
478737Skoansin.tan@gmail.comstruct ProcessParams;
488737Skoansin.tan@gmail.comstruct LiveProcessParams;
495154Sgblack@eecs.umich.educlass SyscallDesc;
505154Sgblack@eecs.umich.educlass System;
512680Sktlim@umich.educlass ThreadContext;
522378SN/A
535758Shsul@eecs.umich.edutemplate<class IntType>
545771Shsul@eecs.umich.edustruct AuxVector
555758Shsul@eecs.umich.edu{
565758Shsul@eecs.umich.edu    IntType a_type;
575771Shsul@eecs.umich.edu    IntType a_val;
585758Shsul@eecs.umich.edu
595771Shsul@eecs.umich.edu    AuxVector()
605758Shsul@eecs.umich.edu    {}
615758Shsul@eecs.umich.edu
625771Shsul@eecs.umich.edu    AuxVector(IntType type, IntType val);
635758Shsul@eecs.umich.edu};
645758Shsul@eecs.umich.edu
652SN/Aclass Process : public SimObject
662SN/A{
672SN/A  public:
682SN/A
692378SN/A    /// Pointer to object representing the system this process is
702378SN/A    /// running on.
712378SN/A    System *system;
722378SN/A
732680Sktlim@umich.edu    // thread contexts associated with this process
745713Shsul@eecs.umich.edu    std::vector<int> contextIds;
75180SN/A
76180SN/A    // number of CPUs (esxec contexts, really) assigned to this process.
775713Shsul@eecs.umich.edu    unsigned int numCpus() { return contextIds.size(); }
782SN/A
792SN/A    // record of blocked context
802SN/A    struct WaitRec
812SN/A    {
822SN/A        Addr waitChan;
832680Sktlim@umich.edu        ThreadContext *waitingContext;
842SN/A
852680Sktlim@umich.edu        WaitRec(Addr chan, ThreadContext *ctx)
862SN/A            : waitChan(chan), waitingContext(ctx)
875543Ssaidi@eecs.umich.edu        {       }
882SN/A    };
892SN/A
902SN/A    // list of all blocked contexts
912SN/A    std::list<WaitRec> waitList;
922SN/A
935543Ssaidi@eecs.umich.edu    Addr brk_point;             // top of the data segment
942SN/A
955543Ssaidi@eecs.umich.edu    Addr stack_base;            // stack segment base (highest address)
965543Ssaidi@eecs.umich.edu    unsigned stack_size;        // initial stack size
975543Ssaidi@eecs.umich.edu    Addr stack_min;             // lowest address accessed on the stack
982SN/A
995154Sgblack@eecs.umich.edu    // The maximum size allowed for the stack.
1005154Sgblack@eecs.umich.edu    Addr max_stack_size;
1015154Sgblack@eecs.umich.edu
1022SN/A    // addr to use for next stack region (for multithreaded apps)
1032SN/A    Addr next_thread_stack_base;
1042SN/A
105360SN/A    // Base of region for mmaps (when user doesn't specify an address).
1061408SN/A    Addr mmap_start;
1071408SN/A    Addr mmap_end;
108360SN/A
1091514SN/A    // Base of region for nxm data
1101514SN/A    Addr nxm_start;
1111514SN/A    Addr nxm_end;
1121514SN/A
1135543Ssaidi@eecs.umich.edu    std::string prog_fname;     // file name
1142SN/A
1155999Snate@binkert.org    Stats::Scalar num_syscalls;       // number of syscalls executed
1162SN/A
1172SN/A
1182SN/A  protected:
1192SN/A    // constructor
1208325Ssteve.reinhardt@amd.com    Process(ProcessParams *params);
1212SN/A
1227532Ssteve.reinhardt@amd.com    virtual void initState();
1232SN/A
1242378SN/A  public:
1252SN/A
1264997Sgblack@eecs.umich.edu    //This id is assigned by m5 and is used to keep process' tlb entries
1274997Sgblack@eecs.umich.edu    //separated.
1284997Sgblack@eecs.umich.edu    uint64_t M5_pid;
1294997Sgblack@eecs.umich.edu
1308852Sandreas.hansson@arm.com    PageTable* pTable;
1318852Sandreas.hansson@arm.com
1325282Srstrong@cs.ucsd.edu    class FdMap
1335282Srstrong@cs.ucsd.edu    {
1345282Srstrong@cs.ucsd.edu      public:
1358325Ssteve.reinhardt@amd.com        int fd;
1368325Ssteve.reinhardt@amd.com        std::string filename;
1378325Ssteve.reinhardt@amd.com        int mode;
1388325Ssteve.reinhardt@amd.com        int flags;
1398325Ssteve.reinhardt@amd.com        bool isPipe;
1408325Ssteve.reinhardt@amd.com        int readPipeSource;
1418325Ssteve.reinhardt@amd.com        uint64_t fileOffset;
1425282Srstrong@cs.ucsd.edu
1438325Ssteve.reinhardt@amd.com        FdMap()
1448325Ssteve.reinhardt@amd.com            : fd(-1), filename("NULL"), mode(0), flags(0),
1458325Ssteve.reinhardt@amd.com              isPipe(false), readPipeSource(0), fileOffset(0)
1468325Ssteve.reinhardt@amd.com        { }
1475282Srstrong@cs.ucsd.edu
1485282Srstrong@cs.ucsd.edu        void serialize(std::ostream &os);
1495282Srstrong@cs.ucsd.edu        void unserialize(Checkpoint *cp, const std::string &section);
1505282Srstrong@cs.ucsd.edu    };
1515282Srstrong@cs.ucsd.edu
1528852Sandreas.hansson@arm.com  protected:
1538852Sandreas.hansson@arm.com    /// Memory proxy for initialization (image loading)
1548852Sandreas.hansson@arm.com    SETranslatingPortProxy initVirtMem;
1558852Sandreas.hansson@arm.com
1562SN/A  private:
1572SN/A    // file descriptor remapping support
1585282Srstrong@cs.ucsd.edu    static const int MAX_FD = 256;    // max legal fd value
1595282Srstrong@cs.ucsd.edu    FdMap fd_map[MAX_FD+1];
1605282Srstrong@cs.ucsd.edu
1612SN/A
1622SN/A  public:
1632SN/A    // static helper functions to generate file descriptors for constructor
1642SN/A    static int openInputFile(const std::string &filename);
1652SN/A    static int openOutputFile(const std::string &filename);
1662SN/A
1672SN/A    // override of virtual SimObject method: register statistics
1682SN/A    virtual void regStats();
1692SN/A
1705713Shsul@eecs.umich.edu    // After getting registered with system object, tell process which
1715713Shsul@eecs.umich.edu    // system-wide context id it is assigned.
1725713Shsul@eecs.umich.edu    void assignThreadContext(int context_id)
1735713Shsul@eecs.umich.edu    {
1745713Shsul@eecs.umich.edu        contextIds.push_back(context_id);
1755713Shsul@eecs.umich.edu    }
176180SN/A
1775713Shsul@eecs.umich.edu    // Find a free context to use
1788325Ssteve.reinhardt@amd.com    ThreadContext *findFreeContext();
1792SN/A
1802SN/A    // map simulator fd sim_fd to target fd tgt_fd
1812SN/A    void dup_fd(int sim_fd, int tgt_fd);
1822SN/A
1832SN/A    // generate new target fd for sim_fd
1848325Ssteve.reinhardt@amd.com    int alloc_fd(int sim_fd, std::string filename, int flags, int mode,
1858325Ssteve.reinhardt@amd.com                 bool pipe);
1861970SN/A
1871970SN/A    // free target fd (e.g., after close)
1881970SN/A    void free_fd(int tgt_fd);
1892SN/A
1902SN/A    // look up simulator fd for given target fd
1912SN/A    int sim_fd(int tgt_fd);
1922SN/A
1935282Srstrong@cs.ucsd.edu    // look up simulator fd_map object for a given target fd
1948325Ssteve.reinhardt@amd.com    FdMap *sim_fd_obj(int tgt_fd);
1955282Srstrong@cs.ucsd.edu
1965282Srstrong@cs.ucsd.edu    // fix all offsets for currently open files and save them
1975282Srstrong@cs.ucsd.edu    void fix_file_offsets();
1985282Srstrong@cs.ucsd.edu
1995282Srstrong@cs.ucsd.edu    // find all offsets for currently open files and save them
2005282Srstrong@cs.ucsd.edu    void find_file_offsets();
2015282Srstrong@cs.ucsd.edu
2025282Srstrong@cs.ucsd.edu    // set the source of this read pipe for a checkpoint resume
2035282Srstrong@cs.ucsd.edu    void setReadPipeSource(int read_pipe_fd, int source_fd);
2045282Srstrong@cs.ucsd.edu
2052680Sktlim@umich.edu    virtual void syscall(int64_t callnum, ThreadContext *tc) = 0;
2063311Ssaidi@eecs.umich.edu
2078601Ssteve.reinhardt@amd.com    void allocateMem(Addr vaddr, int64_t size, bool clobber = false);
2088601Ssteve.reinhardt@amd.com
2098539Sgblack@eecs.umich.edu    /// Attempt to fix up a fault at vaddr by allocating a page on the stack.
2108539Sgblack@eecs.umich.edu    /// @return Whether the fault has been fixed.
2118539Sgblack@eecs.umich.edu    bool fixupStackFault(Addr vaddr);
2124434Ssaidi@eecs.umich.edu
2133311Ssaidi@eecs.umich.edu    void serialize(std::ostream &os);
2143311Ssaidi@eecs.umich.edu    void unserialize(Checkpoint *cp, const std::string &section);
2152SN/A};
2162SN/A
2172SN/A//
2182SN/A// "Live" process with system calls redirected to host system
2192SN/A//
220360SN/Aclass ObjectFile;
2212SN/Aclass LiveProcess : public Process
2222SN/A{
223360SN/A  protected:
2242378SN/A    ObjectFile *objFile;
2252378SN/A    std::vector<std::string> argv;
2262378SN/A    std::vector<std::string> envp;
2273669Sbinkertn@umich.edu    std::string cwd;
2282378SN/A
2298325Ssteve.reinhardt@amd.com    LiveProcess(LiveProcessParams *params, ObjectFile *objFile);
2302SN/A
2313114Sgblack@eecs.umich.edu    // Id of the owner of the process
2323114Sgblack@eecs.umich.edu    uint64_t __uid;
2333114Sgblack@eecs.umich.edu    uint64_t __euid;
2343114Sgblack@eecs.umich.edu    uint64_t __gid;
2353114Sgblack@eecs.umich.edu    uint64_t __egid;
2363114Sgblack@eecs.umich.edu
2373114Sgblack@eecs.umich.edu    // pid of the process and it's parent
2383114Sgblack@eecs.umich.edu    uint64_t __pid;
2393114Sgblack@eecs.umich.edu    uint64_t __ppid;
2403114Sgblack@eecs.umich.edu
241360SN/A  public:
2423114Sgblack@eecs.umich.edu
2434793Sgblack@eecs.umich.edu    enum AuxiliaryVectorType {
2444793Sgblack@eecs.umich.edu        M5_AT_NULL = 0,
2454793Sgblack@eecs.umich.edu        M5_AT_IGNORE = 1,
2464793Sgblack@eecs.umich.edu        M5_AT_EXECFD = 2,
2474793Sgblack@eecs.umich.edu        M5_AT_PHDR = 3,
2484793Sgblack@eecs.umich.edu        M5_AT_PHENT = 4,
2494793Sgblack@eecs.umich.edu        M5_AT_PHNUM = 5,
2504793Sgblack@eecs.umich.edu        M5_AT_PAGESZ = 6,
2514793Sgblack@eecs.umich.edu        M5_AT_BASE = 7,
2524793Sgblack@eecs.umich.edu        M5_AT_FLAGS = 8,
2534793Sgblack@eecs.umich.edu        M5_AT_ENTRY = 9,
2544793Sgblack@eecs.umich.edu        M5_AT_NOTELF = 10,
2554793Sgblack@eecs.umich.edu        M5_AT_UID = 11,
2564793Sgblack@eecs.umich.edu        M5_AT_EUID = 12,
2574793Sgblack@eecs.umich.edu        M5_AT_GID = 13,
2584793Sgblack@eecs.umich.edu        M5_AT_EGID = 14,
2594793Sgblack@eecs.umich.edu        // The following may be specific to Linux
2604793Sgblack@eecs.umich.edu        M5_AT_PLATFORM = 15,
2614793Sgblack@eecs.umich.edu        M5_AT_HWCAP = 16,
2624793Sgblack@eecs.umich.edu        M5_AT_CLKTCK = 17,
2634793Sgblack@eecs.umich.edu
2644793Sgblack@eecs.umich.edu        M5_AT_SECURE = 23,
2656399Sgblack@eecs.umich.edu        M5_BASE_PLATFORM = 24,
2666399Sgblack@eecs.umich.edu        M5_AT_RANDOM = 25,
2676399Sgblack@eecs.umich.edu
2686399Sgblack@eecs.umich.edu        M5_AT_EXECFN = 31,
2694793Sgblack@eecs.umich.edu
2704793Sgblack@eecs.umich.edu        M5_AT_VECTOR_SIZE = 44
2714793Sgblack@eecs.umich.edu    };
2724793Sgblack@eecs.umich.edu
2733114Sgblack@eecs.umich.edu    inline uint64_t uid() {return __uid;}
2743114Sgblack@eecs.umich.edu    inline uint64_t euid() {return __euid;}
2753114Sgblack@eecs.umich.edu    inline uint64_t gid() {return __gid;}
2763114Sgblack@eecs.umich.edu    inline uint64_t egid() {return __egid;}
2773114Sgblack@eecs.umich.edu    inline uint64_t pid() {return __pid;}
2783114Sgblack@eecs.umich.edu    inline uint64_t ppid() {return __ppid;}
2793114Sgblack@eecs.umich.edu
2803669Sbinkertn@umich.edu    std::string
2813669Sbinkertn@umich.edu    fullPath(const std::string &filename)
2823669Sbinkertn@umich.edu    {
2833669Sbinkertn@umich.edu        if (filename[0] == '/' || cwd.empty())
2843669Sbinkertn@umich.edu            return filename;
2853669Sbinkertn@umich.edu
2863669Sbinkertn@umich.edu        std::string full = cwd;
2873669Sbinkertn@umich.edu
2883669Sbinkertn@umich.edu        if (cwd[cwd.size() - 1] != '/')
2893669Sbinkertn@umich.edu            full += '/';
2903669Sbinkertn@umich.edu
2913669Sbinkertn@umich.edu        return full + filename;
2923669Sbinkertn@umich.edu    }
2933669Sbinkertn@umich.edu
2945513SMichael.Adler@intel.com    std::string getcwd() const { return cwd; }
2955513SMichael.Adler@intel.com
2962680Sktlim@umich.edu    virtual void syscall(int64_t callnum, ThreadContext *tc);
2976701Sgblack@eecs.umich.edu
2986701Sgblack@eecs.umich.edu    virtual TheISA::IntReg getSyscallArg(ThreadContext *tc, int &i) = 0;
2996701Sgblack@eecs.umich.edu    virtual TheISA::IntReg getSyscallArg(ThreadContext *tc, int &i, int width);
3005958Sgblack@eecs.umich.edu    virtual void setSyscallArg(ThreadContext *tc,
3015958Sgblack@eecs.umich.edu            int i, TheISA::IntReg val) = 0;
3025958Sgblack@eecs.umich.edu    virtual void setSyscallReturn(ThreadContext *tc,
3035958Sgblack@eecs.umich.edu            SyscallReturn return_value) = 0;
3042093SN/A
3058325Ssteve.reinhardt@amd.com    virtual SyscallDesc *getDesc(int callnum) = 0;
3062715Sstever@eecs.umich.edu
3072715Sstever@eecs.umich.edu    // this function is used to create the LiveProcess object, since
3082715Sstever@eecs.umich.edu    // we can't tell which subclass of LiveProcess to use until we
3092715Sstever@eecs.umich.edu    // open and look at the object file.
3108325Ssteve.reinhardt@amd.com    static LiveProcess *create(LiveProcessParams *params);
3112SN/A};
3122SN/A
313360SN/A
314360SN/A#endif // __PROCESS_HH__
315