process.hh revision 11723
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
3610930Sbrandon.potter@amd.com#include <array>
374117Sgblack@eecs.umich.edu#include <string>
38180SN/A#include <vector>
392SN/A
406329Sgblack@eecs.umich.edu#include "arch/registers.hh"
412378SN/A#include "base/statistics.hh"
426214Snate@binkert.org#include "base/types.hh"
436658Snate@binkert.org#include "config/the_isa.hh"
448852Sandreas.hansson@arm.com#include "mem/se_translating_port_proxy.hh"
4510930Sbrandon.potter@amd.com#include "sim/fd_entry.hh"
4656SN/A#include "sim/sim_object.hh"
475958Sgblack@eecs.umich.edu#include "sim/syscallreturn.hh"
482SN/A
495154Sgblack@eecs.umich.educlass PageTable;
508737Skoansin.tan@gmail.comstruct ProcessParams;
518737Skoansin.tan@gmail.comstruct LiveProcessParams;
525154Sgblack@eecs.umich.educlass SyscallDesc;
535154Sgblack@eecs.umich.educlass System;
542680Sktlim@umich.educlass ThreadContext;
5510496Ssteve.reinhardt@amd.comclass EmulatedDriver;
562378SN/A
575758Shsul@eecs.umich.edutemplate<class IntType>
585771Shsul@eecs.umich.edustruct AuxVector
595758Shsul@eecs.umich.edu{
605758Shsul@eecs.umich.edu    IntType a_type;
615771Shsul@eecs.umich.edu    IntType a_val;
625758Shsul@eecs.umich.edu
635771Shsul@eecs.umich.edu    AuxVector()
645758Shsul@eecs.umich.edu    {}
655758Shsul@eecs.umich.edu
665771Shsul@eecs.umich.edu    AuxVector(IntType type, IntType val);
675758Shsul@eecs.umich.edu};
685758Shsul@eecs.umich.edu
692SN/Aclass Process : public SimObject
702SN/A{
712SN/A  public:
722SN/A
732378SN/A    /// Pointer to object representing the system this process is
742378SN/A    /// running on.
752378SN/A    System *system;
762378SN/A
772680Sktlim@umich.edu    // thread contexts associated with this process
7811005Sandreas.sandberg@arm.com    std::vector<ContextID> contextIds;
79180SN/A
80180SN/A    // number of CPUs (esxec contexts, really) assigned to this process.
815713Shsul@eecs.umich.edu    unsigned int numCpus() { return contextIds.size(); }
822SN/A
832SN/A    // record of blocked context
842SN/A    struct WaitRec
852SN/A    {
862SN/A        Addr waitChan;
872680Sktlim@umich.edu        ThreadContext *waitingContext;
882SN/A
892680Sktlim@umich.edu        WaitRec(Addr chan, ThreadContext *ctx)
902SN/A            : waitChan(chan), waitingContext(ctx)
915543Ssaidi@eecs.umich.edu        {       }
922SN/A    };
932SN/A
942SN/A    // list of all blocked contexts
952SN/A    std::list<WaitRec> waitList;
962SN/A
975543Ssaidi@eecs.umich.edu    Addr brk_point;             // top of the data segment
982SN/A
995543Ssaidi@eecs.umich.edu    Addr stack_base;            // stack segment base (highest address)
1005543Ssaidi@eecs.umich.edu    unsigned stack_size;        // initial stack size
1015543Ssaidi@eecs.umich.edu    Addr stack_min;             // lowest address accessed on the stack
1022SN/A
1035154Sgblack@eecs.umich.edu    // The maximum size allowed for the stack.
1045154Sgblack@eecs.umich.edu    Addr max_stack_size;
1055154Sgblack@eecs.umich.edu
1062SN/A    // addr to use for next stack region (for multithreaded apps)
1072SN/A    Addr next_thread_stack_base;
1082SN/A
109360SN/A    // Base of region for mmaps (when user doesn't specify an address).
1101408SN/A    Addr mmap_end;
111360SN/A
11211386Ssteve.reinhardt@amd.com    // Does mmap region grow upward or downward from mmap_end?  Most
11311386Ssteve.reinhardt@amd.com    // platforms grow downward, but a few (such as Alpha) grow upward
11411386Ssteve.reinhardt@amd.com    // instead, so they can override thie method to return false.
11511386Ssteve.reinhardt@amd.com    virtual bool mmapGrowsDown() const { return true; }
11611386Ssteve.reinhardt@amd.com
1171514SN/A    // Base of region for nxm data
1181514SN/A    Addr nxm_start;
1191514SN/A    Addr nxm_end;
1201514SN/A
1215999Snate@binkert.org    Stats::Scalar num_syscalls;       // number of syscalls executed
1222SN/A
1232SN/A  protected:
1242SN/A    // constructor
1258325Ssteve.reinhardt@amd.com    Process(ProcessParams *params);
1262SN/A
12711169Sandreas.hansson@arm.com    void initState() override;
1282SN/A
12911168Sandreas.hansson@arm.com    DrainState drain() override;
13010905Sandreas.sandberg@arm.com
1312378SN/A  public:
1322SN/A
1334997Sgblack@eecs.umich.edu    //This id is assigned by m5 and is used to keep process' tlb entries
1344997Sgblack@eecs.umich.edu    //separated.
1354997Sgblack@eecs.umich.edu    uint64_t M5_pid;
1364997Sgblack@eecs.umich.edu
13710299Salexandru.dutu@amd.com    // flag for using architecture specific page table
13810299Salexandru.dutu@amd.com    bool useArchPT;
13910554Salexandru.dutu@amd.com    // running KvmCPU in SE mode requires special initialization
14010554Salexandru.dutu@amd.com    bool kvmInSE;
14110554Salexandru.dutu@amd.com
14210298Salexandru.dutu@amd.com    PageTableBase* pTable;
1438852Sandreas.hansson@arm.com
1448852Sandreas.hansson@arm.com  protected:
1458852Sandreas.hansson@arm.com    /// Memory proxy for initialization (image loading)
1468852Sandreas.hansson@arm.com    SETranslatingPortProxy initVirtMem;
1478852Sandreas.hansson@arm.com
1482SN/A  private:
14910929Sbrandon.potter@amd.com    static const int NUM_FDS = 1024;
1505282Srstrong@cs.ucsd.edu
15110929Sbrandon.potter@amd.com    // File descriptor remapping support.
15210930Sbrandon.potter@amd.com    std::shared_ptr<std::array<FDEntry, NUM_FDS>> fd_array;
15310930Sbrandon.potter@amd.com
15410930Sbrandon.potter@amd.com    // Standard file descriptor options for initialization and checkpoints.
15510930Sbrandon.potter@amd.com    std::map<std::string, int> imap;
15610930Sbrandon.potter@amd.com    std::map<std::string, int> oemap;
1572SN/A
1582SN/A  public:
15910930Sbrandon.potter@amd.com    // inherit file descriptor map from another process (necessary for clone)
16010932Sbrandon.potter@amd.com    void inheritFDArray(Process *p);
1612SN/A
1622SN/A    // override of virtual SimObject method: register statistics
16311169Sandreas.hansson@arm.com    void regStats() override;
1642SN/A
1655713Shsul@eecs.umich.edu    // After getting registered with system object, tell process which
1665713Shsul@eecs.umich.edu    // system-wide context id it is assigned.
16711005Sandreas.sandberg@arm.com    void assignThreadContext(ContextID context_id)
1685713Shsul@eecs.umich.edu    {
1695713Shsul@eecs.umich.edu        contextIds.push_back(context_id);
1705713Shsul@eecs.umich.edu    }
171180SN/A
1725713Shsul@eecs.umich.edu    // Find a free context to use
1738325Ssteve.reinhardt@amd.com    ThreadContext *findFreeContext();
1742SN/A
1759144Ssteve.reinhardt@amd.com    // provide program name for debug messages
1769144Ssteve.reinhardt@amd.com    virtual const char *progName() const { return "<unknown>"; }
1779144Ssteve.reinhardt@amd.com
1782SN/A    // generate new target fd for sim_fd
17910932Sbrandon.potter@amd.com    int allocFD(int sim_fd, const std::string& filename, int flags, int mode,
18010932Sbrandon.potter@amd.com                bool pipe);
1811970SN/A
18210929Sbrandon.potter@amd.com    // disassociate target fd with simulator fd and cleanup subsidiary fields
18310932Sbrandon.potter@amd.com    void resetFDEntry(int tgt_fd);
1842SN/A
1852SN/A    // look up simulator fd for given target fd
18610932Sbrandon.potter@amd.com    int getSimFD(int tgt_fd);
1872SN/A
18810930Sbrandon.potter@amd.com    // look up fd entry for a given target fd
18910932Sbrandon.potter@amd.com    FDEntry *getFDEntry(int tgt_fd);
1905282Srstrong@cs.ucsd.edu
19110929Sbrandon.potter@amd.com    // look up target fd for given host fd
19210929Sbrandon.potter@amd.com    // Assumes a 1:1 mapping between target file descriptor and host file
19310929Sbrandon.potter@amd.com    // descriptor. Given the current API, this must be true given that it's
19410929Sbrandon.potter@amd.com    // not possible to map multiple target file descriptors to the same host
19510929Sbrandon.potter@amd.com    // file descriptor
19610932Sbrandon.potter@amd.com    int getTgtFD(int sim_fd);
19710929Sbrandon.potter@amd.com
1985282Srstrong@cs.ucsd.edu    // fix all offsets for currently open files and save them
19910932Sbrandon.potter@amd.com    void fixFileOffsets();
2005282Srstrong@cs.ucsd.edu
2015282Srstrong@cs.ucsd.edu    // find all offsets for currently open files and save them
20210932Sbrandon.potter@amd.com    void findFileOffsets();
2035282Srstrong@cs.ucsd.edu
2045282Srstrong@cs.ucsd.edu    // set the source of this read pipe for a checkpoint resume
2055282Srstrong@cs.ucsd.edu    void setReadPipeSource(int read_pipe_fd, int source_fd);
2065282Srstrong@cs.ucsd.edu
2072680Sktlim@umich.edu    virtual void syscall(int64_t callnum, ThreadContext *tc) = 0;
2083311Ssaidi@eecs.umich.edu
2098601Ssteve.reinhardt@amd.com    void allocateMem(Addr vaddr, int64_t size, bool clobber = false);
2108601Ssteve.reinhardt@amd.com
2118539Sgblack@eecs.umich.edu    /// Attempt to fix up a fault at vaddr by allocating a page on the stack.
2128539Sgblack@eecs.umich.edu    /// @return Whether the fault has been fixed.
2138539Sgblack@eecs.umich.edu    bool fixupStackFault(Addr vaddr);
2144434Ssaidi@eecs.umich.edu
2159110Ssteve.reinhardt@amd.com    /**
21610558Salexandru.dutu@amd.com     * Maps a contiguous range of virtual addresses in this process's
2179110Ssteve.reinhardt@amd.com     * address space to a contiguous range of physical addresses.
21810558Salexandru.dutu@amd.com     * This function exists primarily to expose the map operation to
21910558Salexandru.dutu@amd.com     * python, so that configuration scripts can set up mappings in SE mode.
2209110Ssteve.reinhardt@amd.com     *
2219110Ssteve.reinhardt@amd.com     * @param vaddr The starting virtual address of the range.
2229110Ssteve.reinhardt@amd.com     * @param paddr The starting physical address of the range.
2239110Ssteve.reinhardt@amd.com     * @param size The length of the range in bytes.
22410558Salexandru.dutu@amd.com     * @param cacheable Specifies whether accesses are cacheable.
2259110Ssteve.reinhardt@amd.com     * @return True if the map operation was successful.  (At this
2269110Ssteve.reinhardt@amd.com     *           point in time, the map operation always succeeds.)
2279110Ssteve.reinhardt@amd.com     */
22810558Salexandru.dutu@amd.com    bool map(Addr vaddr, Addr paddr, int size, bool cacheable = true);
2299110Ssteve.reinhardt@amd.com
23011168Sandreas.hansson@arm.com    void serialize(CheckpointOut &cp) const override;
23111168Sandreas.hansson@arm.com    void unserialize(CheckpointIn &cp) override;
2322SN/A};
2332SN/A
2342SN/A//
2352SN/A// "Live" process with system calls redirected to host system
2362SN/A//
237360SN/Aclass ObjectFile;
2382SN/Aclass LiveProcess : public Process
2392SN/A{
240360SN/A  protected:
2412378SN/A    ObjectFile *objFile;
2422378SN/A    std::vector<std::string> argv;
2432378SN/A    std::vector<std::string> envp;
2443669Sbinkertn@umich.edu    std::string cwd;
24511140Sjthestness@gmail.com    std::string executable;
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
25910496Ssteve.reinhardt@amd.com    // Emulated drivers available to this process
26010496Ssteve.reinhardt@amd.com    std::vector<EmulatedDriver *> drivers;
26110496Ssteve.reinhardt@amd.com
262360SN/A  public:
2633114Sgblack@eecs.umich.edu
2644793Sgblack@eecs.umich.edu    enum AuxiliaryVectorType {
2654793Sgblack@eecs.umich.edu        M5_AT_NULL = 0,
2664793Sgblack@eecs.umich.edu        M5_AT_IGNORE = 1,
2674793Sgblack@eecs.umich.edu        M5_AT_EXECFD = 2,
2684793Sgblack@eecs.umich.edu        M5_AT_PHDR = 3,
2694793Sgblack@eecs.umich.edu        M5_AT_PHENT = 4,
2704793Sgblack@eecs.umich.edu        M5_AT_PHNUM = 5,
2714793Sgblack@eecs.umich.edu        M5_AT_PAGESZ = 6,
2724793Sgblack@eecs.umich.edu        M5_AT_BASE = 7,
2734793Sgblack@eecs.umich.edu        M5_AT_FLAGS = 8,
2744793Sgblack@eecs.umich.edu        M5_AT_ENTRY = 9,
2754793Sgblack@eecs.umich.edu        M5_AT_NOTELF = 10,
2764793Sgblack@eecs.umich.edu        M5_AT_UID = 11,
2774793Sgblack@eecs.umich.edu        M5_AT_EUID = 12,
2784793Sgblack@eecs.umich.edu        M5_AT_GID = 13,
2794793Sgblack@eecs.umich.edu        M5_AT_EGID = 14,
2804793Sgblack@eecs.umich.edu        // The following may be specific to Linux
2814793Sgblack@eecs.umich.edu        M5_AT_PLATFORM = 15,
2824793Sgblack@eecs.umich.edu        M5_AT_HWCAP = 16,
2834793Sgblack@eecs.umich.edu        M5_AT_CLKTCK = 17,
2844793Sgblack@eecs.umich.edu
2854793Sgblack@eecs.umich.edu        M5_AT_SECURE = 23,
2866399Sgblack@eecs.umich.edu        M5_BASE_PLATFORM = 24,
2876399Sgblack@eecs.umich.edu        M5_AT_RANDOM = 25,
2886399Sgblack@eecs.umich.edu
2896399Sgblack@eecs.umich.edu        M5_AT_EXECFN = 31,
2904793Sgblack@eecs.umich.edu
2914793Sgblack@eecs.umich.edu        M5_AT_VECTOR_SIZE = 44
2924793Sgblack@eecs.umich.edu    };
2934793Sgblack@eecs.umich.edu
2943114Sgblack@eecs.umich.edu    inline uint64_t uid() {return __uid;}
2953114Sgblack@eecs.umich.edu    inline uint64_t euid() {return __euid;}
2963114Sgblack@eecs.umich.edu    inline uint64_t gid() {return __gid;}
2973114Sgblack@eecs.umich.edu    inline uint64_t egid() {return __egid;}
2983114Sgblack@eecs.umich.edu    inline uint64_t pid() {return __pid;}
2993114Sgblack@eecs.umich.edu    inline uint64_t ppid() {return __ppid;}
3003114Sgblack@eecs.umich.edu
3019144Ssteve.reinhardt@amd.com    // provide program name for debug messages
30211140Sjthestness@gmail.com    virtual const char *progName() const { return executable.c_str(); }
3039144Ssteve.reinhardt@amd.com
3043669Sbinkertn@umich.edu    std::string
3053669Sbinkertn@umich.edu    fullPath(const std::string &filename)
3063669Sbinkertn@umich.edu    {
3073669Sbinkertn@umich.edu        if (filename[0] == '/' || cwd.empty())
3083669Sbinkertn@umich.edu            return filename;
3093669Sbinkertn@umich.edu
3103669Sbinkertn@umich.edu        std::string full = cwd;
3113669Sbinkertn@umich.edu
3123669Sbinkertn@umich.edu        if (cwd[cwd.size() - 1] != '/')
3133669Sbinkertn@umich.edu            full += '/';
3143669Sbinkertn@umich.edu
3153669Sbinkertn@umich.edu        return full + filename;
3163669Sbinkertn@umich.edu    }
3173669Sbinkertn@umich.edu
3185513SMichael.Adler@intel.com    std::string getcwd() const { return cwd; }
3195513SMichael.Adler@intel.com
3202680Sktlim@umich.edu    virtual void syscall(int64_t callnum, ThreadContext *tc);
3216701Sgblack@eecs.umich.edu
3226701Sgblack@eecs.umich.edu    virtual TheISA::IntReg getSyscallArg(ThreadContext *tc, int &i) = 0;
3236701Sgblack@eecs.umich.edu    virtual TheISA::IntReg getSyscallArg(ThreadContext *tc, int &i, int width);
3245958Sgblack@eecs.umich.edu    virtual void setSyscallArg(ThreadContext *tc,
3255958Sgblack@eecs.umich.edu            int i, TheISA::IntReg val) = 0;
3265958Sgblack@eecs.umich.edu    virtual void setSyscallReturn(ThreadContext *tc,
3275958Sgblack@eecs.umich.edu            SyscallReturn return_value) = 0;
3282093SN/A
3298325Ssteve.reinhardt@amd.com    virtual SyscallDesc *getDesc(int callnum) = 0;
3302715Sstever@eecs.umich.edu
33110496Ssteve.reinhardt@amd.com    /**
33210496Ssteve.reinhardt@amd.com     * Find an emulated device driver.
33310496Ssteve.reinhardt@amd.com     *
33410496Ssteve.reinhardt@amd.com     * @param filename Name of the device (under /dev)
33510496Ssteve.reinhardt@amd.com     * @return Pointer to driver object if found, else NULL
33610496Ssteve.reinhardt@amd.com     */
33710496Ssteve.reinhardt@amd.com    EmulatedDriver *findDriver(std::string filename);
33810496Ssteve.reinhardt@amd.com
33911389Sbrandon.potter@amd.com    // This function acts as a callback to update the bias value in
34011389Sbrandon.potter@amd.com    // the object file because the parameters needed to calculate the
34111389Sbrandon.potter@amd.com    // bias are not available when the object file is created.
34211389Sbrandon.potter@amd.com    void updateBias();
34311389Sbrandon.potter@amd.com
34411392Sbrandon.potter@amd.com    ObjectFile *getInterpreter();
34511392Sbrandon.potter@amd.com
34611389Sbrandon.potter@amd.com    Addr getBias();
34711389Sbrandon.potter@amd.com    Addr getStartPC();
34811389Sbrandon.potter@amd.com
3492715Sstever@eecs.umich.edu    // this function is used to create the LiveProcess object, since
3502715Sstever@eecs.umich.edu    // we can't tell which subclass of LiveProcess to use until we
3512715Sstever@eecs.umich.edu    // open and look at the object file.
3528325Ssteve.reinhardt@amd.com    static LiveProcess *create(LiveProcessParams *params);
3532SN/A};
3542SN/A
355360SN/A
356360SN/A#endif // __PROCESS_HH__
357