process.hh revision 10299:bec0c5ffc323
11689SN/A/*
213601Sgiacomo.travaglini@arm.com * Copyright (c) 2014 Advanced Micro Devices, Inc.
39919Ssteve.reinhardt@amd.com * Copyright (c) 2001-2005 The Regents of The University of Michigan
48707Sandreas.hansson@arm.com * All rights reserved.
58707Sandreas.hansson@arm.com *
68707Sandreas.hansson@arm.com * Redistribution and use in source and binary forms, with or without
78707Sandreas.hansson@arm.com * modification, are permitted provided that the following conditions are
88707Sandreas.hansson@arm.com * met: redistributions of source code must retain the above copyright
98707Sandreas.hansson@arm.com * notice, this list of conditions and the following disclaimer;
108707Sandreas.hansson@arm.com * redistributions in binary form must reproduce the above copyright
118707Sandreas.hansson@arm.com * notice, this list of conditions and the following disclaimer in the
128707Sandreas.hansson@arm.com * documentation and/or other materials provided with the distribution;
138707Sandreas.hansson@arm.com * neither the name of the copyright holders nor the names of its
148707Sandreas.hansson@arm.com * contributors may be used to endorse or promote products derived from
151689SN/A * this software without specific prior written permission.
167897Shestness@cs.utexas.edu *
171689SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
181689SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
191689SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
201689SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
211689SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
221689SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
231689SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
241689SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
251689SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
261689SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
271689SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
281689SN/A *
291689SN/A * Authors: Nathan Binkert
301689SN/A *          Steve Reinhardt
311689SN/A */
321689SN/A
331689SN/A#ifndef __PROCESS_HH__
341689SN/A#define __PROCESS_HH__
351689SN/A
361689SN/A#include <string>
371689SN/A#include <vector>
381689SN/A
391689SN/A#include "arch/registers.hh"
401689SN/A#include "base/statistics.hh"
412665Ssaidi@eecs.umich.edu#include "base/types.hh"
422665Ssaidi@eecs.umich.edu#include "config/the_isa.hh"
432756Sksewell@umich.edu#include "mem/se_translating_port_proxy.hh"
447897Shestness@cs.utexas.edu#include "sim/sim_object.hh"
451689SN/A#include "sim/syscallreturn.hh"
461689SN/A
472325SN/Aclass PageTable;
482325SN/Astruct ProcessParams;
491060SN/Astruct LiveProcessParams;
501060SN/Aclass SyscallDesc;
511060SN/Aclass System;
522292SN/Aclass ThreadContext;
532292SN/A
541681SN/Atemplate<class IntType>
551060SN/Astruct AuxVector
5612109SRekai.GonzalezAlberquilla@arm.com{
572980Sgblack@eecs.umich.edu    IntType a_type;
581060SN/A    IntType a_val;
596658Snate@binkert.org
601717SN/A    AuxVector()
611717SN/A    {}
622292SN/A
632292SN/A    AuxVector(IntType type, IntType val);
648229Snate@binkert.org};
658229Snate@binkert.org
668229Snate@binkert.orgclass Process : public SimObject
678229Snate@binkert.org{
682817Sksewell@umich.edu  public:
698229Snate@binkert.org
701060SN/A    /// Pointer to object representing the system this process is
711060SN/A    /// running on.
722316SN/A    System *system;
732316SN/A
742680Sktlim@umich.edu    // thread contexts associated with this process
752817Sksewell@umich.edu    std::vector<int> contextIds;
762817Sksewell@umich.edu
772843Sktlim@umich.edu    // number of CPUs (esxec contexts, really) assigned to this process.
782843Sktlim@umich.edu    unsigned int numCpus() { return contextIds.size(); }
792669Sktlim@umich.edu
801060SN/A    // record of blocked context
811060SN/A    struct WaitRec
828737Skoansin.tan@gmail.com    {
835529Snate@binkert.org        Addr waitChan;
842733Sktlim@umich.edu        ThreadContext *waitingContext;
851060SN/A
861060SN/A        WaitRec(Addr chan, ThreadContext *ctx)
871060SN/A            : waitChan(chan), waitingContext(ctx)
885529Snate@binkert.org        {       }
892292SN/A    };
902292SN/A
911060SN/A    // list of all blocked contexts
921060SN/A    std::list<WaitRec> waitList;
932348SN/A
942348SN/A    Addr brk_point;             // top of the data segment
952348SN/A
962348SN/A    Addr stack_base;            // stack segment base (highest address)
972348SN/A    unsigned stack_size;        // initial stack size
981060SN/A    Addr stack_min;             // lowest address accessed on the stack
992733Sktlim@umich.edu
1001060SN/A    // The maximum size allowed for the stack.
1011060SN/A    Addr max_stack_size;
1022325SN/A
1031060SN/A    // addr to use for next stack region (for multithreaded apps)
1041061SN/A    Addr next_thread_stack_base;
1054329Sktlim@umich.edu
1061060SN/A    // Base of region for mmaps (when user doesn't specify an address).
10712109SRekai.GonzalezAlberquilla@arm.com    Addr mmap_start;
10812109SRekai.GonzalezAlberquilla@arm.com    Addr mmap_end;
10912109SRekai.GonzalezAlberquilla@arm.com
11013610Sgiacomo.gabrielli@arm.com    // Base of region for nxm data
11113610Sgiacomo.gabrielli@arm.com    Addr nxm_start;
1125595Sgblack@eecs.umich.edu    Addr nxm_end;
1132292SN/A
1142292SN/A    Stats::Scalar num_syscalls;       // number of syscalls executed
1152292SN/A
1162292SN/A  protected:
1172817Sksewell@umich.edu    // constructor
1182829Sksewell@umich.edu    Process(ProcessParams *params);
1191060SN/A
1201060SN/A    virtual void initState();
1211060SN/A
1221060SN/A  public:
1231060SN/A
1242307SN/A    //This id is assigned by m5 and is used to keep process' tlb entries
1252307SN/A    //separated.
1261060SN/A    uint64_t M5_pid;
1271060SN/A
12812406Sgabeblack@google.com    // flag for using architecture specific page table
12912406Sgabeblack@google.com    bool useArchPT;
13013590Srekai.gonzalezalberquilla@arm.com    PageTableBase* pTable;
1313781Sgblack@eecs.umich.edu
1322292SN/A    class FdMap
1331060SN/A    {
1341060SN/A      public:
1351060SN/A        int fd;
1368707Sandreas.hansson@arm.com        std::string filename;
1378707Sandreas.hansson@arm.com        int mode;
1388707Sandreas.hansson@arm.com        int flags;
1398707Sandreas.hansson@arm.com        bool isPipe;
1409608Sandreas.hansson@arm.com        int readPipeSource;
1418707Sandreas.hansson@arm.com        uint64_t fileOffset;
1428707Sandreas.hansson@arm.com
1438707Sandreas.hansson@arm.com        FdMap()
1448707Sandreas.hansson@arm.com            : fd(-1), filename("NULL"), mode(0), flags(0),
1458707Sandreas.hansson@arm.com              isPipe(false), readPipeSource(0), fileOffset(0)
1468707Sandreas.hansson@arm.com        { }
1478707Sandreas.hansson@arm.com
1488707Sandreas.hansson@arm.com        void serialize(std::ostream &os);
1499608Sandreas.hansson@arm.com        void unserialize(Checkpoint *cp, const std::string &section);
1508707Sandreas.hansson@arm.com    };
1518707Sandreas.hansson@arm.com
1528707Sandreas.hansson@arm.com  protected:
1538707Sandreas.hansson@arm.com    /// Memory proxy for initialization (image loading)
1548707Sandreas.hansson@arm.com    SETranslatingPortProxy initVirtMem;
1558707Sandreas.hansson@arm.com
1568975Sandreas.hansson@arm.com  private:
1578707Sandreas.hansson@arm.com    // file descriptor remapping support
1588707Sandreas.hansson@arm.com    static const int MAX_FD = 256;    // max legal fd value
15910713Sandreas.hansson@arm.com    FdMap fd_map[MAX_FD+1];
1608707Sandreas.hansson@arm.com
1618707Sandreas.hansson@arm.com
1628707Sandreas.hansson@arm.com  public:
1638707Sandreas.hansson@arm.com    // static helper functions to generate file descriptors for constructor
1648707Sandreas.hansson@arm.com    static int openInputFile(const std::string &filename);
1659608Sandreas.hansson@arm.com    static int openOutputFile(const std::string &filename);
1668707Sandreas.hansson@arm.com
1678707Sandreas.hansson@arm.com    // override of virtual SimObject method: register statistics
1688707Sandreas.hansson@arm.com    virtual void regStats();
1698707Sandreas.hansson@arm.com
1708707Sandreas.hansson@arm.com    // After getting registered with system object, tell process which
17110529Smorr@cs.wisc.edu    // system-wide context id it is assigned.
1728707Sandreas.hansson@arm.com    void assignThreadContext(int context_id)
1738707Sandreas.hansson@arm.com    {
1748707Sandreas.hansson@arm.com        contextIds.push_back(context_id);
1758707Sandreas.hansson@arm.com    }
17610529Smorr@cs.wisc.edu
17710529Smorr@cs.wisc.edu    // Find a free context to use
1788707Sandreas.hansson@arm.com    ThreadContext *findFreeContext();
1798707Sandreas.hansson@arm.com
1808707Sandreas.hansson@arm.com    // provide program name for debug messages
1818707Sandreas.hansson@arm.com    virtual const char *progName() const { return "<unknown>"; }
1828707Sandreas.hansson@arm.com
1838707Sandreas.hansson@arm.com    // map simulator fd sim_fd to target fd tgt_fd
1848707Sandreas.hansson@arm.com    void dup_fd(int sim_fd, int tgt_fd);
1858975Sandreas.hansson@arm.com
1868975Sandreas.hansson@arm.com    // generate new target fd for sim_fd
1878707Sandreas.hansson@arm.com    int alloc_fd(int sim_fd, std::string filename, int flags, int mode,
1889608Sandreas.hansson@arm.com                 bool pipe);
1899608Sandreas.hansson@arm.com
1909608Sandreas.hansson@arm.com    // free target fd (e.g., after close)
1919608Sandreas.hansson@arm.com    void free_fd(int tgt_fd);
1929608Sandreas.hansson@arm.com
1938707Sandreas.hansson@arm.com    // look up simulator fd for given target fd
19410713Sandreas.hansson@arm.com    int sim_fd(int tgt_fd);
1958707Sandreas.hansson@arm.com
1968707Sandreas.hansson@arm.com    // look up simulator fd_map object for a given target fd
1978707Sandreas.hansson@arm.com    FdMap *sim_fd_obj(int tgt_fd);
1988707Sandreas.hansson@arm.com
1998707Sandreas.hansson@arm.com    // fix all offsets for currently open files and save them
2008711Sandreas.hansson@arm.com    void fix_file_offsets();
2018707Sandreas.hansson@arm.com
2028922Swilliam.wang@arm.com    // find all offsets for currently open files and save them
2038707Sandreas.hansson@arm.com    void find_file_offsets();
2048707Sandreas.hansson@arm.com
2052292SN/A    // set the source of this read pipe for a checkpoint resume
20612127Sspwilson2@wisc.edu    void setReadPipeSource(int read_pipe_fd, int source_fd);
2071060SN/A
20813641Sqtt2@cornell.edu    virtual void syscall(int64_t callnum, ThreadContext *tc) = 0;
20913641Sqtt2@cornell.edu
21013641Sqtt2@cornell.edu    void allocateMem(Addr vaddr, int64_t size, bool clobber = false);
2112292SN/A
2129180Sandreas.hansson@arm.com    /// Attempt to fix up a fault at vaddr by allocating a page on the stack.
2131060SN/A    /// @return Whether the fault has been fixed.
2141060SN/A    bool fixupStackFault(Addr vaddr);
2159179Sandreas.hansson@arm.com
2161060SN/A    /**
2179179Sandreas.hansson@arm.com     * Map a contiguous range of virtual addresses in this process's
2181060SN/A     * address space to a contiguous range of physical addresses.
2191060SN/A     * This function exists primarily to enable exposing the map
2202292SN/A     * operation to python, so that configuration scripts can set up
2211060SN/A     * mappings in SE mode.
2221060SN/A     *
2231060SN/A     * @param vaddr The starting virtual address of the range.
2241060SN/A     * @param paddr The starting physical address of the range.
2251060SN/A     * @param size The length of the range in bytes.
2261060SN/A     * @return True if the map operation was successful.  (At this
2279444SAndreas.Sandberg@ARM.com     *           point in time, the map operation always succeeds.)
22810913Sandreas.sandberg@arm.com     */
2299444SAndreas.Sandberg@ARM.com    bool map(Addr vaddr, Addr paddr, int size);
2309444SAndreas.Sandberg@ARM.com
2319444SAndreas.Sandberg@ARM.com    void serialize(std::ostream &os);
2329444SAndreas.Sandberg@ARM.com    void unserialize(Checkpoint *cp, const std::string &section);
2339444SAndreas.Sandberg@ARM.com};
2349444SAndreas.Sandberg@ARM.com
2359444SAndreas.Sandberg@ARM.com//
2369444SAndreas.Sandberg@ARM.com// "Live" process with system calls redirected to host system
2379444SAndreas.Sandberg@ARM.com//
2389444SAndreas.Sandberg@ARM.comclass ObjectFile;
2399444SAndreas.Sandberg@ARM.comclass LiveProcess : public Process
2409444SAndreas.Sandberg@ARM.com{
2419444SAndreas.Sandberg@ARM.com  protected:
2429444SAndreas.Sandberg@ARM.com    ObjectFile *objFile;
2439444SAndreas.Sandberg@ARM.com    std::vector<std::string> argv;
2449444SAndreas.Sandberg@ARM.com    std::vector<std::string> envp;
2459444SAndreas.Sandberg@ARM.com    std::string cwd;
2469444SAndreas.Sandberg@ARM.com
2479444SAndreas.Sandberg@ARM.com    LiveProcess(LiveProcessParams *params, ObjectFile *objFile);
2489444SAndreas.Sandberg@ARM.com
2499444SAndreas.Sandberg@ARM.com    // Id of the owner of the process
2509444SAndreas.Sandberg@ARM.com    uint64_t __uid;
2519444SAndreas.Sandberg@ARM.com    uint64_t __euid;
2529444SAndreas.Sandberg@ARM.com    uint64_t __gid;
2539444SAndreas.Sandberg@ARM.com    uint64_t __egid;
2541060SN/A
2552292SN/A    // pid of the process and it's parent
2565595Sgblack@eecs.umich.edu    uint64_t __pid;
2572292SN/A    uint64_t __ppid;
2581755SN/A
2591060SN/A  public:
2602292SN/A
26111169Sandreas.hansson@arm.com    enum AuxiliaryVectorType {
2621684SN/A        M5_AT_NULL = 0,
26310023Smatt.horsnell@ARM.com        M5_AT_IGNORE = 1,
26410023Smatt.horsnell@ARM.com        M5_AT_EXECFD = 2,
26510023Smatt.horsnell@ARM.com        M5_AT_PHDR = 3,
26610023Smatt.horsnell@ARM.com        M5_AT_PHENT = 4,
26711169Sandreas.hansson@arm.com        M5_AT_PHNUM = 5,
26810023Smatt.horsnell@ARM.com        M5_AT_PAGESZ = 6,
2695358Sgblack@eecs.umich.edu        M5_AT_BASE = 7,
2705358Sgblack@eecs.umich.edu        M5_AT_FLAGS = 8,
2715358Sgblack@eecs.umich.edu        M5_AT_ENTRY = 9,
2725358Sgblack@eecs.umich.edu        M5_AT_NOTELF = 10,
2735358Sgblack@eecs.umich.edu        M5_AT_UID = 11,
2745358Sgblack@eecs.umich.edu        M5_AT_EUID = 12,
2755358Sgblack@eecs.umich.edu        M5_AT_GID = 13,
2765358Sgblack@eecs.umich.edu        M5_AT_EGID = 14,
2775358Sgblack@eecs.umich.edu        // The following may be specific to Linux
2785358Sgblack@eecs.umich.edu        M5_AT_PLATFORM = 15,
2795358Sgblack@eecs.umich.edu        M5_AT_HWCAP = 16,
2805358Sgblack@eecs.umich.edu        M5_AT_CLKTCK = 17,
2815358Sgblack@eecs.umich.edu
2825358Sgblack@eecs.umich.edu        M5_AT_SECURE = 23,
2835358Sgblack@eecs.umich.edu        M5_BASE_PLATFORM = 24,
2845358Sgblack@eecs.umich.edu        M5_AT_RANDOM = 25,
2852292SN/A
2862292SN/A        M5_AT_EXECFN = 31,
2872292SN/A
2881684SN/A        M5_AT_VECTOR_SIZE = 44
2891684SN/A    };
2902292SN/A
29111169Sandreas.hansson@arm.com    inline uint64_t uid() {return __uid;}
2921060SN/A    inline uint64_t euid() {return __euid;}
29311169Sandreas.hansson@arm.com    inline uint64_t gid() {return __gid;}
2949427SAndreas.Sandberg@ARM.com    inline uint64_t egid() {return __egid;}
2952834Sksewell@umich.edu    inline uint64_t pid() {return __pid;}
2962834Sksewell@umich.edu    inline uint64_t ppid() {return __ppid;}
2972834Sksewell@umich.edu
2982834Sksewell@umich.edu    // provide program name for debug messages
2992829Sksewell@umich.edu    virtual const char *progName() const { return argv[0].c_str(); }
3006221Snate@binkert.org
3012875Sksewell@umich.edu    std::string
3022875Sksewell@umich.edu    fullPath(const std::string &filename)
3036221Snate@binkert.org    {
3042829Sksewell@umich.edu        if (filename[0] == '/' || cwd.empty())
3052292SN/A            return filename;
3066221Snate@binkert.org
3071060SN/A        std::string full = cwd;
3082292SN/A
3096221Snate@binkert.org        if (cwd[cwd.size() - 1] != '/')
3102292SN/A            full += '/';
3112292SN/A
31211169Sandreas.hansson@arm.com        return full + filename;
3138834Satgutier@umich.edu    }
3148834Satgutier@umich.edu
31511169Sandreas.hansson@arm.com    std::string getcwd() const { return cwd; }
3162292SN/A
3172292SN/A    virtual void syscall(int64_t callnum, ThreadContext *tc);
31811169Sandreas.hansson@arm.com
3192292SN/A    virtual TheISA::IntReg getSyscallArg(ThreadContext *tc, int &i) = 0;
3202292SN/A    virtual TheISA::IntReg getSyscallArg(ThreadContext *tc, int &i, int width);
32111169Sandreas.hansson@arm.com    virtual void setSyscallArg(ThreadContext *tc,
3222292SN/A            int i, TheISA::IntReg val) = 0;
3232292SN/A    virtual void setSyscallReturn(ThreadContext *tc,
3242292SN/A            SyscallReturn return_value) = 0;
3252292SN/A
32611169Sandreas.hansson@arm.com    virtual SyscallDesc *getDesc(int callnum) = 0;
3272292SN/A
3282292SN/A    // this function is used to create the LiveProcess object, since
3292292SN/A    // we can't tell which subclass of LiveProcess to use until we
3302292SN/A    // open and look at the object file.
3319444SAndreas.Sandberg@ARM.com    static LiveProcess *create(LiveProcessParams *params);
33210913Sandreas.sandberg@arm.com};
3339444SAndreas.Sandberg@ARM.com
33411168Sandreas.hansson@arm.com
33511168Sandreas.hansson@arm.com#endif // __PROCESS_HH__
3362864Sktlim@umich.edu