process.hh revision 11851
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>
3711800Sbrandon.potter@amd.com#include <map>
384117Sgblack@eecs.umich.edu#include <string>
39180SN/A#include <vector>
402SN/A
416329Sgblack@eecs.umich.edu#include "arch/registers.hh"
422378SN/A#include "base/statistics.hh"
436214Snate@binkert.org#include "base/types.hh"
446658Snate@binkert.org#include "config/the_isa.hh"
458852Sandreas.hansson@arm.com#include "mem/se_translating_port_proxy.hh"
4610930Sbrandon.potter@amd.com#include "sim/fd_entry.hh"
4756SN/A#include "sim/sim_object.hh"
482SN/A
498737Skoansin.tan@gmail.comstruct ProcessParams;
5011800Sbrandon.potter@amd.com
5111800Sbrandon.potter@amd.comclass EmulatedDriver;
5211851Sbrandon.potter@amd.comclass ObjectFile;
5311800Sbrandon.potter@amd.comclass PageTableBase;
545154Sgblack@eecs.umich.educlass SyscallDesc;
5511800Sbrandon.potter@amd.comclass SyscallReturn;
565154Sgblack@eecs.umich.educlass System;
572680Sktlim@umich.educlass ThreadContext;
582378SN/A
595758Shsul@eecs.umich.edutemplate<class IntType>
605771Shsul@eecs.umich.edustruct AuxVector
615758Shsul@eecs.umich.edu{
625758Shsul@eecs.umich.edu    IntType a_type;
635771Shsul@eecs.umich.edu    IntType a_val;
645758Shsul@eecs.umich.edu
655771Shsul@eecs.umich.edu    AuxVector()
665758Shsul@eecs.umich.edu    {}
675758Shsul@eecs.umich.edu
685771Shsul@eecs.umich.edu    AuxVector(IntType type, IntType val);
695758Shsul@eecs.umich.edu};
705758Shsul@eecs.umich.edu
712SN/Aclass Process : public SimObject
722SN/A{
732SN/A  public:
742SN/A
752378SN/A    /// Pointer to object representing the system this process is
762378SN/A    /// running on.
772378SN/A    System *system;
782378SN/A
792680Sktlim@umich.edu    // thread contexts associated with this process
8011005Sandreas.sandberg@arm.com    std::vector<ContextID> contextIds;
81180SN/A
82180SN/A    // number of CPUs (esxec contexts, really) assigned to this process.
835713Shsul@eecs.umich.edu    unsigned int numCpus() { return contextIds.size(); }
842SN/A
852SN/A    // record of blocked context
862SN/A    struct WaitRec
872SN/A    {
882SN/A        Addr waitChan;
892680Sktlim@umich.edu        ThreadContext *waitingContext;
902SN/A
912680Sktlim@umich.edu        WaitRec(Addr chan, ThreadContext *ctx)
922SN/A            : waitChan(chan), waitingContext(ctx)
935543Ssaidi@eecs.umich.edu        {       }
942SN/A    };
952SN/A
962SN/A    // list of all blocked contexts
972SN/A    std::list<WaitRec> waitList;
982SN/A
995543Ssaidi@eecs.umich.edu    Addr brk_point;             // top of the data segment
1002SN/A
1015543Ssaidi@eecs.umich.edu    Addr stack_base;            // stack segment base (highest address)
1025543Ssaidi@eecs.umich.edu    unsigned stack_size;        // initial stack size
1035543Ssaidi@eecs.umich.edu    Addr stack_min;             // lowest address accessed on the stack
1042SN/A
1055154Sgblack@eecs.umich.edu    // The maximum size allowed for the stack.
1065154Sgblack@eecs.umich.edu    Addr max_stack_size;
1075154Sgblack@eecs.umich.edu
1082SN/A    // addr to use for next stack region (for multithreaded apps)
1092SN/A    Addr next_thread_stack_base;
1102SN/A
111360SN/A    // Base of region for mmaps (when user doesn't specify an address).
1121408SN/A    Addr mmap_end;
113360SN/A
11411386Ssteve.reinhardt@amd.com    // Does mmap region grow upward or downward from mmap_end?  Most
11511386Ssteve.reinhardt@amd.com    // platforms grow downward, but a few (such as Alpha) grow upward
11611386Ssteve.reinhardt@amd.com    // instead, so they can override thie method to return false.
11711386Ssteve.reinhardt@amd.com    virtual bool mmapGrowsDown() const { return true; }
11811386Ssteve.reinhardt@amd.com
1191514SN/A    // Base of region for nxm data
1201514SN/A    Addr nxm_start;
1211514SN/A    Addr nxm_end;
1221514SN/A
1235999Snate@binkert.org    Stats::Scalar num_syscalls;       // number of syscalls executed
1242SN/A
1252SN/A  protected:
1262SN/A    // constructor
1278325Ssteve.reinhardt@amd.com    Process(ProcessParams *params);
1282SN/A
12911169Sandreas.hansson@arm.com    void initState() override;
1302SN/A
13111168Sandreas.hansson@arm.com    DrainState drain() override;
13210905Sandreas.sandberg@arm.com
1332378SN/A  public:
1342SN/A
13510299Salexandru.dutu@amd.com    // flag for using architecture specific page table
13610299Salexandru.dutu@amd.com    bool useArchPT;
13710554Salexandru.dutu@amd.com    // running KvmCPU in SE mode requires special initialization
13810554Salexandru.dutu@amd.com    bool kvmInSE;
13910554Salexandru.dutu@amd.com
14010298Salexandru.dutu@amd.com    PageTableBase* pTable;
1418852Sandreas.hansson@arm.com
1428852Sandreas.hansson@arm.com  protected:
1438852Sandreas.hansson@arm.com    /// Memory proxy for initialization (image loading)
1448852Sandreas.hansson@arm.com    SETranslatingPortProxy initVirtMem;
1458852Sandreas.hansson@arm.com
1462SN/A  private:
14710929Sbrandon.potter@amd.com    static const int NUM_FDS = 1024;
1485282Srstrong@cs.ucsd.edu
14910929Sbrandon.potter@amd.com    // File descriptor remapping support.
15010930Sbrandon.potter@amd.com    std::shared_ptr<std::array<FDEntry, NUM_FDS>> fd_array;
15110930Sbrandon.potter@amd.com
15210930Sbrandon.potter@amd.com    // Standard file descriptor options for initialization and checkpoints.
15310930Sbrandon.potter@amd.com    std::map<std::string, int> imap;
15410930Sbrandon.potter@amd.com    std::map<std::string, int> oemap;
1552SN/A
1562SN/A  public:
15710930Sbrandon.potter@amd.com    // inherit file descriptor map from another process (necessary for clone)
15810932Sbrandon.potter@amd.com    void inheritFDArray(Process *p);
1592SN/A
1602SN/A    // override of virtual SimObject method: register statistics
16111169Sandreas.hansson@arm.com    void regStats() override;
1622SN/A
1635713Shsul@eecs.umich.edu    // After getting registered with system object, tell process which
1645713Shsul@eecs.umich.edu    // system-wide context id it is assigned.
16511005Sandreas.sandberg@arm.com    void assignThreadContext(ContextID context_id)
1665713Shsul@eecs.umich.edu    {
1675713Shsul@eecs.umich.edu        contextIds.push_back(context_id);
1685713Shsul@eecs.umich.edu    }
169180SN/A
1705713Shsul@eecs.umich.edu    // Find a free context to use
1718325Ssteve.reinhardt@amd.com    ThreadContext *findFreeContext();
1722SN/A
1732SN/A    // generate new target fd for sim_fd
17410932Sbrandon.potter@amd.com    int allocFD(int sim_fd, const std::string& filename, int flags, int mode,
17510932Sbrandon.potter@amd.com                bool pipe);
1761970SN/A
17710929Sbrandon.potter@amd.com    // disassociate target fd with simulator fd and cleanup subsidiary fields
17810932Sbrandon.potter@amd.com    void resetFDEntry(int tgt_fd);
1792SN/A
1802SN/A    // look up simulator fd for given target fd
18110932Sbrandon.potter@amd.com    int getSimFD(int tgt_fd);
1822SN/A
18310930Sbrandon.potter@amd.com    // look up fd entry for a given target fd
18410932Sbrandon.potter@amd.com    FDEntry *getFDEntry(int tgt_fd);
1855282Srstrong@cs.ucsd.edu
18610929Sbrandon.potter@amd.com    // look up target fd for given host fd
18710929Sbrandon.potter@amd.com    // Assumes a 1:1 mapping between target file descriptor and host file
18810929Sbrandon.potter@amd.com    // descriptor. Given the current API, this must be true given that it's
18910929Sbrandon.potter@amd.com    // not possible to map multiple target file descriptors to the same host
19010929Sbrandon.potter@amd.com    // file descriptor
19110932Sbrandon.potter@amd.com    int getTgtFD(int sim_fd);
19210929Sbrandon.potter@amd.com
1935282Srstrong@cs.ucsd.edu    // fix all offsets for currently open files and save them
19410932Sbrandon.potter@amd.com    void fixFileOffsets();
1955282Srstrong@cs.ucsd.edu
1965282Srstrong@cs.ucsd.edu    // find all offsets for currently open files and save them
19710932Sbrandon.potter@amd.com    void findFileOffsets();
1985282Srstrong@cs.ucsd.edu
1995282Srstrong@cs.ucsd.edu    // set the source of this read pipe for a checkpoint resume
2005282Srstrong@cs.ucsd.edu    void setReadPipeSource(int read_pipe_fd, int source_fd);
2015282Srstrong@cs.ucsd.edu
2023311Ssaidi@eecs.umich.edu
2038601Ssteve.reinhardt@amd.com    void allocateMem(Addr vaddr, int64_t size, bool clobber = false);
2048601Ssteve.reinhardt@amd.com
2058539Sgblack@eecs.umich.edu    /// Attempt to fix up a fault at vaddr by allocating a page on the stack.
2068539Sgblack@eecs.umich.edu    /// @return Whether the fault has been fixed.
2078539Sgblack@eecs.umich.edu    bool fixupStackFault(Addr vaddr);
2084434Ssaidi@eecs.umich.edu
2099110Ssteve.reinhardt@amd.com    /**
21010558Salexandru.dutu@amd.com     * Maps a contiguous range of virtual addresses in this process's
2119110Ssteve.reinhardt@amd.com     * address space to a contiguous range of physical addresses.
21210558Salexandru.dutu@amd.com     * This function exists primarily to expose the map operation to
21310558Salexandru.dutu@amd.com     * python, so that configuration scripts can set up mappings in SE mode.
2149110Ssteve.reinhardt@amd.com     *
2159110Ssteve.reinhardt@amd.com     * @param vaddr The starting virtual address of the range.
2169110Ssteve.reinhardt@amd.com     * @param paddr The starting physical address of the range.
2179110Ssteve.reinhardt@amd.com     * @param size The length of the range in bytes.
21810558Salexandru.dutu@amd.com     * @param cacheable Specifies whether accesses are cacheable.
2199110Ssteve.reinhardt@amd.com     * @return True if the map operation was successful.  (At this
2209110Ssteve.reinhardt@amd.com     *           point in time, the map operation always succeeds.)
2219110Ssteve.reinhardt@amd.com     */
22210558Salexandru.dutu@amd.com    bool map(Addr vaddr, Addr paddr, int size, bool cacheable = true);
2239110Ssteve.reinhardt@amd.com
22411168Sandreas.hansson@arm.com    void serialize(CheckpointOut &cp) const override;
22511168Sandreas.hansson@arm.com    void unserialize(CheckpointIn &cp) override;
22611801Sbrandon.potter@amd.com
22711851Sbrandon.potter@amd.com  protected:
22811851Sbrandon.potter@amd.com    ObjectFile *objFile;
22911851Sbrandon.potter@amd.com    std::vector<std::string> argv;
23011851Sbrandon.potter@amd.com    std::vector<std::string> envp;
23111851Sbrandon.potter@amd.com    std::string cwd;
23211851Sbrandon.potter@amd.com    std::string executable;
23311851Sbrandon.potter@amd.com
23411851Sbrandon.potter@amd.com    Process(ProcessParams *params, ObjectFile *obj_file);
23511851Sbrandon.potter@amd.com
23611801Sbrandon.potter@amd.com  public:
23711801Sbrandon.potter@amd.com    // Id of the owner of the process
23811801Sbrandon.potter@amd.com    uint64_t _uid;
23911801Sbrandon.potter@amd.com    uint64_t _euid;
24011801Sbrandon.potter@amd.com    uint64_t _gid;
24111801Sbrandon.potter@amd.com    uint64_t _egid;
24211801Sbrandon.potter@amd.com
24311801Sbrandon.potter@amd.com    // pid of the process and it's parent
24411801Sbrandon.potter@amd.com    uint64_t _pid;
24511801Sbrandon.potter@amd.com    uint64_t _ppid;
24611801Sbrandon.potter@amd.com
24710496Ssteve.reinhardt@amd.com    // Emulated drivers available to this process
24810496Ssteve.reinhardt@amd.com    std::vector<EmulatedDriver *> drivers;
24910496Ssteve.reinhardt@amd.com
2504793Sgblack@eecs.umich.edu    enum AuxiliaryVectorType {
2514793Sgblack@eecs.umich.edu        M5_AT_NULL = 0,
2524793Sgblack@eecs.umich.edu        M5_AT_IGNORE = 1,
2534793Sgblack@eecs.umich.edu        M5_AT_EXECFD = 2,
2544793Sgblack@eecs.umich.edu        M5_AT_PHDR = 3,
2554793Sgblack@eecs.umich.edu        M5_AT_PHENT = 4,
2564793Sgblack@eecs.umich.edu        M5_AT_PHNUM = 5,
2574793Sgblack@eecs.umich.edu        M5_AT_PAGESZ = 6,
2584793Sgblack@eecs.umich.edu        M5_AT_BASE = 7,
2594793Sgblack@eecs.umich.edu        M5_AT_FLAGS = 8,
2604793Sgblack@eecs.umich.edu        M5_AT_ENTRY = 9,
2614793Sgblack@eecs.umich.edu        M5_AT_NOTELF = 10,
2624793Sgblack@eecs.umich.edu        M5_AT_UID = 11,
2634793Sgblack@eecs.umich.edu        M5_AT_EUID = 12,
2644793Sgblack@eecs.umich.edu        M5_AT_GID = 13,
2654793Sgblack@eecs.umich.edu        M5_AT_EGID = 14,
2664793Sgblack@eecs.umich.edu        // The following may be specific to Linux
2674793Sgblack@eecs.umich.edu        M5_AT_PLATFORM = 15,
2684793Sgblack@eecs.umich.edu        M5_AT_HWCAP = 16,
2694793Sgblack@eecs.umich.edu        M5_AT_CLKTCK = 17,
2704793Sgblack@eecs.umich.edu
2714793Sgblack@eecs.umich.edu        M5_AT_SECURE = 23,
2726399Sgblack@eecs.umich.edu        M5_BASE_PLATFORM = 24,
2736399Sgblack@eecs.umich.edu        M5_AT_RANDOM = 25,
2746399Sgblack@eecs.umich.edu
2756399Sgblack@eecs.umich.edu        M5_AT_EXECFN = 31,
2764793Sgblack@eecs.umich.edu
2774793Sgblack@eecs.umich.edu        M5_AT_VECTOR_SIZE = 44
2784793Sgblack@eecs.umich.edu    };
2794793Sgblack@eecs.umich.edu
28011801Sbrandon.potter@amd.com    inline uint64_t uid() { return _uid; }
28111801Sbrandon.potter@amd.com    inline uint64_t euid() { return _euid; }
28211801Sbrandon.potter@amd.com    inline uint64_t gid() { return _gid; }
28311801Sbrandon.potter@amd.com    inline uint64_t egid() { return _egid; }
28411801Sbrandon.potter@amd.com    inline uint64_t pid() { return _pid; }
28511801Sbrandon.potter@amd.com    inline uint64_t ppid() { return _ppid; }
2863114Sgblack@eecs.umich.edu
2879144Ssteve.reinhardt@amd.com    // provide program name for debug messages
28811851Sbrandon.potter@amd.com    const char *progName() const { return executable.c_str(); }
2899144Ssteve.reinhardt@amd.com
2903669Sbinkertn@umich.edu    std::string
2913669Sbinkertn@umich.edu    fullPath(const std::string &filename)
2923669Sbinkertn@umich.edu    {
2933669Sbinkertn@umich.edu        if (filename[0] == '/' || cwd.empty())
2943669Sbinkertn@umich.edu            return filename;
2953669Sbinkertn@umich.edu
2963669Sbinkertn@umich.edu        std::string full = cwd;
2973669Sbinkertn@umich.edu
2983669Sbinkertn@umich.edu        if (cwd[cwd.size() - 1] != '/')
2993669Sbinkertn@umich.edu            full += '/';
3003669Sbinkertn@umich.edu
3013669Sbinkertn@umich.edu        return full + filename;
3023669Sbinkertn@umich.edu    }
3033669Sbinkertn@umich.edu
3045513SMichael.Adler@intel.com    std::string getcwd() const { return cwd; }
3055513SMichael.Adler@intel.com
30611851Sbrandon.potter@amd.com    void syscall(int64_t callnum, ThreadContext *tc);
3076701Sgblack@eecs.umich.edu
3086701Sgblack@eecs.umich.edu    virtual TheISA::IntReg getSyscallArg(ThreadContext *tc, int &i) = 0;
3096701Sgblack@eecs.umich.edu    virtual TheISA::IntReg getSyscallArg(ThreadContext *tc, int &i, int width);
3105958Sgblack@eecs.umich.edu    virtual void setSyscallArg(ThreadContext *tc,
3115958Sgblack@eecs.umich.edu            int i, TheISA::IntReg val) = 0;
3125958Sgblack@eecs.umich.edu    virtual void setSyscallReturn(ThreadContext *tc,
3135958Sgblack@eecs.umich.edu            SyscallReturn return_value) = 0;
3142093SN/A
3158325Ssteve.reinhardt@amd.com    virtual SyscallDesc *getDesc(int callnum) = 0;
3162715Sstever@eecs.umich.edu
31710496Ssteve.reinhardt@amd.com    /**
31810496Ssteve.reinhardt@amd.com     * Find an emulated device driver.
31910496Ssteve.reinhardt@amd.com     *
32010496Ssteve.reinhardt@amd.com     * @param filename Name of the device (under /dev)
32110496Ssteve.reinhardt@amd.com     * @return Pointer to driver object if found, else NULL
32210496Ssteve.reinhardt@amd.com     */
32310496Ssteve.reinhardt@amd.com    EmulatedDriver *findDriver(std::string filename);
32410496Ssteve.reinhardt@amd.com
32511389Sbrandon.potter@amd.com    // This function acts as a callback to update the bias value in
32611389Sbrandon.potter@amd.com    // the object file because the parameters needed to calculate the
32711389Sbrandon.potter@amd.com    // bias are not available when the object file is created.
32811389Sbrandon.potter@amd.com    void updateBias();
32911389Sbrandon.potter@amd.com
33011392Sbrandon.potter@amd.com    ObjectFile *getInterpreter();
33111392Sbrandon.potter@amd.com
33211389Sbrandon.potter@amd.com    Addr getBias();
33311389Sbrandon.potter@amd.com    Addr getStartPC();
3342SN/A};
3352SN/A
336360SN/A#endif // __PROCESS_HH__
337