process.hh revision 10299
12SN/A/*
210298Salexandru.dutu@amd.com * Copyright (c) 2014 Advanced Micro Devices, Inc.
31762SN/A * Copyright (c) 2001-2005 The Regents of The University of Michigan
42SN/A * All rights reserved.
52SN/A *
62SN/A * Redistribution and use in source and binary forms, with or without
72SN/A * modification, are permitted provided that the following conditions are
82SN/A * met: redistributions of source code must retain the above copyright
92SN/A * notice, this list of conditions and the following disclaimer;
102SN/A * redistributions in binary form must reproduce the above copyright
112SN/A * notice, this list of conditions and the following disclaimer in the
122SN/A * documentation and/or other materials provided with the distribution;
132SN/A * neither the name of the copyright holders nor the names of its
142SN/A * contributors may be used to endorse or promote products derived from
152SN/A * this software without specific prior written permission.
162SN/A *
172SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
182SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
192SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
202SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
212SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
222SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
232SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
242SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
252SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
262SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
272SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
282665Ssaidi@eecs.umich.edu *
292665Ssaidi@eecs.umich.edu * Authors: Nathan Binkert
302665Ssaidi@eecs.umich.edu *          Steve Reinhardt
312SN/A */
322SN/A
33360SN/A#ifndef __PROCESS_HH__
34360SN/A#define __PROCESS_HH__
352SN/A
364117Sgblack@eecs.umich.edu#include <string>
37180SN/A#include <vector>
382SN/A
396329Sgblack@eecs.umich.edu#include "arch/registers.hh"
402378SN/A#include "base/statistics.hh"
416214Snate@binkert.org#include "base/types.hh"
426658Snate@binkert.org#include "config/the_isa.hh"
438852Sandreas.hansson@arm.com#include "mem/se_translating_port_proxy.hh"
4456SN/A#include "sim/sim_object.hh"
455958Sgblack@eecs.umich.edu#include "sim/syscallreturn.hh"
462SN/A
475154Sgblack@eecs.umich.educlass PageTable;
488737Skoansin.tan@gmail.comstruct ProcessParams;
498737Skoansin.tan@gmail.comstruct LiveProcessParams;
505154Sgblack@eecs.umich.educlass SyscallDesc;
515154Sgblack@eecs.umich.educlass System;
522680Sktlim@umich.educlass ThreadContext;
532378SN/A
545758Shsul@eecs.umich.edutemplate<class IntType>
555771Shsul@eecs.umich.edustruct AuxVector
565758Shsul@eecs.umich.edu{
575758Shsul@eecs.umich.edu    IntType a_type;
585771Shsul@eecs.umich.edu    IntType a_val;
595758Shsul@eecs.umich.edu
605771Shsul@eecs.umich.edu    AuxVector()
615758Shsul@eecs.umich.edu    {}
625758Shsul@eecs.umich.edu
635771Shsul@eecs.umich.edu    AuxVector(IntType type, IntType val);
645758Shsul@eecs.umich.edu};
655758Shsul@eecs.umich.edu
662SN/Aclass Process : public SimObject
672SN/A{
682SN/A  public:
692SN/A
702378SN/A    /// Pointer to object representing the system this process is
712378SN/A    /// running on.
722378SN/A    System *system;
732378SN/A
742680Sktlim@umich.edu    // thread contexts associated with this process
755713Shsul@eecs.umich.edu    std::vector<int> contextIds;
76180SN/A
77180SN/A    // number of CPUs (esxec contexts, really) assigned to this process.
785713Shsul@eecs.umich.edu    unsigned int numCpus() { return contextIds.size(); }
792SN/A
802SN/A    // record of blocked context
812SN/A    struct WaitRec
822SN/A    {
832SN/A        Addr waitChan;
842680Sktlim@umich.edu        ThreadContext *waitingContext;
852SN/A
862680Sktlim@umich.edu        WaitRec(Addr chan, ThreadContext *ctx)
872SN/A            : waitChan(chan), waitingContext(ctx)
885543Ssaidi@eecs.umich.edu        {       }
892SN/A    };
902SN/A
912SN/A    // list of all blocked contexts
922SN/A    std::list<WaitRec> waitList;
932SN/A
945543Ssaidi@eecs.umich.edu    Addr brk_point;             // top of the data segment
952SN/A
965543Ssaidi@eecs.umich.edu    Addr stack_base;            // stack segment base (highest address)
975543Ssaidi@eecs.umich.edu    unsigned stack_size;        // initial stack size
985543Ssaidi@eecs.umich.edu    Addr stack_min;             // lowest address accessed on the stack
992SN/A
1005154Sgblack@eecs.umich.edu    // The maximum size allowed for the stack.
1015154Sgblack@eecs.umich.edu    Addr max_stack_size;
1025154Sgblack@eecs.umich.edu
1032SN/A    // addr to use for next stack region (for multithreaded apps)
1042SN/A    Addr next_thread_stack_base;
1052SN/A
106360SN/A    // Base of region for mmaps (when user doesn't specify an address).
1071408SN/A    Addr mmap_start;
1081408SN/A    Addr mmap_end;
109360SN/A
1101514SN/A    // Base of region for nxm data
1111514SN/A    Addr nxm_start;
1121514SN/A    Addr nxm_end;
1131514SN/A
1145999Snate@binkert.org    Stats::Scalar num_syscalls;       // number of syscalls executed
1152SN/A
1162SN/A  protected:
1172SN/A    // constructor
1188325Ssteve.reinhardt@amd.com    Process(ProcessParams *params);
1192SN/A
1207532Ssteve.reinhardt@amd.com    virtual void initState();
1212SN/A
1222378SN/A  public:
1232SN/A
1244997Sgblack@eecs.umich.edu    //This id is assigned by m5 and is used to keep process' tlb entries
1254997Sgblack@eecs.umich.edu    //separated.
1264997Sgblack@eecs.umich.edu    uint64_t M5_pid;
1274997Sgblack@eecs.umich.edu
12810299Salexandru.dutu@amd.com    // flag for using architecture specific page table
12910299Salexandru.dutu@amd.com    bool useArchPT;
13010298Salexandru.dutu@amd.com    PageTableBase* 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
1809144Ssteve.reinhardt@amd.com    // provide program name for debug messages
1819144Ssteve.reinhardt@amd.com    virtual const char *progName() const { return "<unknown>"; }
1829144Ssteve.reinhardt@amd.com
1832SN/A    // map simulator fd sim_fd to target fd tgt_fd
1842SN/A    void dup_fd(int sim_fd, int tgt_fd);
1852SN/A
1862SN/A    // generate new target fd for sim_fd
1878325Ssteve.reinhardt@amd.com    int alloc_fd(int sim_fd, std::string filename, int flags, int mode,
1888325Ssteve.reinhardt@amd.com                 bool pipe);
1891970SN/A
1901970SN/A    // free target fd (e.g., after close)
1911970SN/A    void free_fd(int tgt_fd);
1922SN/A
1932SN/A    // look up simulator fd for given target fd
1942SN/A    int sim_fd(int tgt_fd);
1952SN/A
1965282Srstrong@cs.ucsd.edu    // look up simulator fd_map object for a given target fd
1978325Ssteve.reinhardt@amd.com    FdMap *sim_fd_obj(int tgt_fd);
1985282Srstrong@cs.ucsd.edu
1995282Srstrong@cs.ucsd.edu    // fix all offsets for currently open files and save them
2005282Srstrong@cs.ucsd.edu    void fix_file_offsets();
2015282Srstrong@cs.ucsd.edu
2025282Srstrong@cs.ucsd.edu    // find all offsets for currently open files and save them
2035282Srstrong@cs.ucsd.edu    void find_file_offsets();
2045282Srstrong@cs.ucsd.edu
2055282Srstrong@cs.ucsd.edu    // set the source of this read pipe for a checkpoint resume
2065282Srstrong@cs.ucsd.edu    void setReadPipeSource(int read_pipe_fd, int source_fd);
2075282Srstrong@cs.ucsd.edu
2082680Sktlim@umich.edu    virtual void syscall(int64_t callnum, ThreadContext *tc) = 0;
2093311Ssaidi@eecs.umich.edu
2108601Ssteve.reinhardt@amd.com    void allocateMem(Addr vaddr, int64_t size, bool clobber = false);
2118601Ssteve.reinhardt@amd.com
2128539Sgblack@eecs.umich.edu    /// Attempt to fix up a fault at vaddr by allocating a page on the stack.
2138539Sgblack@eecs.umich.edu    /// @return Whether the fault has been fixed.
2148539Sgblack@eecs.umich.edu    bool fixupStackFault(Addr vaddr);
2154434Ssaidi@eecs.umich.edu
2169110Ssteve.reinhardt@amd.com    /**
2179110Ssteve.reinhardt@amd.com     * Map a contiguous range of virtual addresses in this process's
2189110Ssteve.reinhardt@amd.com     * address space to a contiguous range of physical addresses.
2199110Ssteve.reinhardt@amd.com     * This function exists primarily to enable exposing the map
2209110Ssteve.reinhardt@amd.com     * operation to python, so that configuration scripts can set up
2219110Ssteve.reinhardt@amd.com     * mappings in SE mode.
2229110Ssteve.reinhardt@amd.com     *
2239110Ssteve.reinhardt@amd.com     * @param vaddr The starting virtual address of the range.
2249110Ssteve.reinhardt@amd.com     * @param paddr The starting physical address of the range.
2259110Ssteve.reinhardt@amd.com     * @param size The length of the range in bytes.
2269110Ssteve.reinhardt@amd.com     * @return True if the map operation was successful.  (At this
2279110Ssteve.reinhardt@amd.com     *           point in time, the map operation always succeeds.)
2289110Ssteve.reinhardt@amd.com     */
2299110Ssteve.reinhardt@amd.com    bool map(Addr vaddr, Addr paddr, int size);
2309110Ssteve.reinhardt@amd.com
2313311Ssaidi@eecs.umich.edu    void serialize(std::ostream &os);
2323311Ssaidi@eecs.umich.edu    void unserialize(Checkpoint *cp, const std::string &section);
2332SN/A};
2342SN/A
2352SN/A//
2362SN/A// "Live" process with system calls redirected to host system
2372SN/A//
238360SN/Aclass ObjectFile;
2392SN/Aclass LiveProcess : public Process
2402SN/A{
241360SN/A  protected:
2422378SN/A    ObjectFile *objFile;
2432378SN/A    std::vector<std::string> argv;
2442378SN/A    std::vector<std::string> envp;
2453669Sbinkertn@umich.edu    std::string cwd;
2462378SN/A
2478325Ssteve.reinhardt@amd.com    LiveProcess(LiveProcessParams *params, ObjectFile *objFile);
2482SN/A
2493114Sgblack@eecs.umich.edu    // Id of the owner of the process
2503114Sgblack@eecs.umich.edu    uint64_t __uid;
2513114Sgblack@eecs.umich.edu    uint64_t __euid;
2523114Sgblack@eecs.umich.edu    uint64_t __gid;
2533114Sgblack@eecs.umich.edu    uint64_t __egid;
2543114Sgblack@eecs.umich.edu
2553114Sgblack@eecs.umich.edu    // pid of the process and it's parent
2563114Sgblack@eecs.umich.edu    uint64_t __pid;
2573114Sgblack@eecs.umich.edu    uint64_t __ppid;
2583114Sgblack@eecs.umich.edu
259360SN/A  public:
2603114Sgblack@eecs.umich.edu
2614793Sgblack@eecs.umich.edu    enum AuxiliaryVectorType {
2624793Sgblack@eecs.umich.edu        M5_AT_NULL = 0,
2634793Sgblack@eecs.umich.edu        M5_AT_IGNORE = 1,
2644793Sgblack@eecs.umich.edu        M5_AT_EXECFD = 2,
2654793Sgblack@eecs.umich.edu        M5_AT_PHDR = 3,
2664793Sgblack@eecs.umich.edu        M5_AT_PHENT = 4,
2674793Sgblack@eecs.umich.edu        M5_AT_PHNUM = 5,
2684793Sgblack@eecs.umich.edu        M5_AT_PAGESZ = 6,
2694793Sgblack@eecs.umich.edu        M5_AT_BASE = 7,
2704793Sgblack@eecs.umich.edu        M5_AT_FLAGS = 8,
2714793Sgblack@eecs.umich.edu        M5_AT_ENTRY = 9,
2724793Sgblack@eecs.umich.edu        M5_AT_NOTELF = 10,
2734793Sgblack@eecs.umich.edu        M5_AT_UID = 11,
2744793Sgblack@eecs.umich.edu        M5_AT_EUID = 12,
2754793Sgblack@eecs.umich.edu        M5_AT_GID = 13,
2764793Sgblack@eecs.umich.edu        M5_AT_EGID = 14,
2774793Sgblack@eecs.umich.edu        // The following may be specific to Linux
2784793Sgblack@eecs.umich.edu        M5_AT_PLATFORM = 15,
2794793Sgblack@eecs.umich.edu        M5_AT_HWCAP = 16,
2804793Sgblack@eecs.umich.edu        M5_AT_CLKTCK = 17,
2814793Sgblack@eecs.umich.edu
2824793Sgblack@eecs.umich.edu        M5_AT_SECURE = 23,
2836399Sgblack@eecs.umich.edu        M5_BASE_PLATFORM = 24,
2846399Sgblack@eecs.umich.edu        M5_AT_RANDOM = 25,
2856399Sgblack@eecs.umich.edu
2866399Sgblack@eecs.umich.edu        M5_AT_EXECFN = 31,
2874793Sgblack@eecs.umich.edu
2884793Sgblack@eecs.umich.edu        M5_AT_VECTOR_SIZE = 44
2894793Sgblack@eecs.umich.edu    };
2904793Sgblack@eecs.umich.edu
2913114Sgblack@eecs.umich.edu    inline uint64_t uid() {return __uid;}
2923114Sgblack@eecs.umich.edu    inline uint64_t euid() {return __euid;}
2933114Sgblack@eecs.umich.edu    inline uint64_t gid() {return __gid;}
2943114Sgblack@eecs.umich.edu    inline uint64_t egid() {return __egid;}
2953114Sgblack@eecs.umich.edu    inline uint64_t pid() {return __pid;}
2963114Sgblack@eecs.umich.edu    inline uint64_t ppid() {return __ppid;}
2973114Sgblack@eecs.umich.edu
2989144Ssteve.reinhardt@amd.com    // provide program name for debug messages
2999144Ssteve.reinhardt@amd.com    virtual const char *progName() const { return argv[0].c_str(); }
3009144Ssteve.reinhardt@amd.com
3013669Sbinkertn@umich.edu    std::string
3023669Sbinkertn@umich.edu    fullPath(const std::string &filename)
3033669Sbinkertn@umich.edu    {
3043669Sbinkertn@umich.edu        if (filename[0] == '/' || cwd.empty())
3053669Sbinkertn@umich.edu            return filename;
3063669Sbinkertn@umich.edu
3073669Sbinkertn@umich.edu        std::string full = cwd;
3083669Sbinkertn@umich.edu
3093669Sbinkertn@umich.edu        if (cwd[cwd.size() - 1] != '/')
3103669Sbinkertn@umich.edu            full += '/';
3113669Sbinkertn@umich.edu
3123669Sbinkertn@umich.edu        return full + filename;
3133669Sbinkertn@umich.edu    }
3143669Sbinkertn@umich.edu
3155513SMichael.Adler@intel.com    std::string getcwd() const { return cwd; }
3165513SMichael.Adler@intel.com
3172680Sktlim@umich.edu    virtual void syscall(int64_t callnum, ThreadContext *tc);
3186701Sgblack@eecs.umich.edu
3196701Sgblack@eecs.umich.edu    virtual TheISA::IntReg getSyscallArg(ThreadContext *tc, int &i) = 0;
3206701Sgblack@eecs.umich.edu    virtual TheISA::IntReg getSyscallArg(ThreadContext *tc, int &i, int width);
3215958Sgblack@eecs.umich.edu    virtual void setSyscallArg(ThreadContext *tc,
3225958Sgblack@eecs.umich.edu            int i, TheISA::IntReg val) = 0;
3235958Sgblack@eecs.umich.edu    virtual void setSyscallReturn(ThreadContext *tc,
3245958Sgblack@eecs.umich.edu            SyscallReturn return_value) = 0;
3252093SN/A
3268325Ssteve.reinhardt@amd.com    virtual SyscallDesc *getDesc(int callnum) = 0;
3272715Sstever@eecs.umich.edu
3282715Sstever@eecs.umich.edu    // this function is used to create the LiveProcess object, since
3292715Sstever@eecs.umich.edu    // we can't tell which subclass of LiveProcess to use until we
3302715Sstever@eecs.umich.edu    // open and look at the object file.
3318325Ssteve.reinhardt@amd.com    static LiveProcess *create(LiveProcessParams *params);
3322SN/A};
3332SN/A
334360SN/A
335360SN/A#endif // __PROCESS_HH__
336