process.hh revision 8737
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
352SN/A//
362SN/A// The purpose of this code is to fake the loader & syscall mechanism
372SN/A// when there's no OS: thus there's no reason to use it in FULL_SYSTEM
382SN/A// mode when we do have an OS.
392SN/A//
401858SN/A#include "config/full_system.hh"
411858SN/A
421858SN/A#if !FULL_SYSTEM
432SN/A
444117Sgblack@eecs.umich.edu#include <string>
45180SN/A#include <vector>
462SN/A
476329Sgblack@eecs.umich.edu#include "arch/registers.hh"
482378SN/A#include "base/statistics.hh"
496214Snate@binkert.org#include "base/types.hh"
506658Snate@binkert.org#include "config/the_isa.hh"
5156SN/A#include "sim/sim_object.hh"
525958Sgblack@eecs.umich.edu#include "sim/syscallreturn.hh"
532SN/A
545154Sgblack@eecs.umich.educlass PageTable;
558737Skoansin.tan@gmail.comstruct ProcessParams;
568737Skoansin.tan@gmail.comstruct LiveProcessParams;
575154Sgblack@eecs.umich.educlass SyscallDesc;
585154Sgblack@eecs.umich.educlass System;
592680Sktlim@umich.educlass ThreadContext;
608706Sandreas.hansson@arm.comclass SETranslatingPortProxy;
612378SN/A
625758Shsul@eecs.umich.edutemplate<class IntType>
635771Shsul@eecs.umich.edustruct AuxVector
645758Shsul@eecs.umich.edu{
655758Shsul@eecs.umich.edu    IntType a_type;
665771Shsul@eecs.umich.edu    IntType a_val;
675758Shsul@eecs.umich.edu
685771Shsul@eecs.umich.edu    AuxVector()
695758Shsul@eecs.umich.edu    {}
705758Shsul@eecs.umich.edu
715771Shsul@eecs.umich.edu    AuxVector(IntType type, IntType val);
725758Shsul@eecs.umich.edu};
735758Shsul@eecs.umich.edu
742SN/Aclass Process : public SimObject
752SN/A{
762SN/A  public:
772SN/A
782378SN/A    /// Pointer to object representing the system this process is
792378SN/A    /// running on.
802378SN/A    System *system;
812378SN/A
822680Sktlim@umich.edu    // thread contexts associated with this process
835713Shsul@eecs.umich.edu    std::vector<int> contextIds;
84180SN/A
85180SN/A    // number of CPUs (esxec contexts, really) assigned to this process.
865713Shsul@eecs.umich.edu    unsigned int numCpus() { return contextIds.size(); }
872SN/A
882SN/A    // record of blocked context
892SN/A    struct WaitRec
902SN/A    {
912SN/A        Addr waitChan;
922680Sktlim@umich.edu        ThreadContext *waitingContext;
932SN/A
942680Sktlim@umich.edu        WaitRec(Addr chan, ThreadContext *ctx)
952SN/A            : waitChan(chan), waitingContext(ctx)
965543Ssaidi@eecs.umich.edu        {       }
972SN/A    };
982SN/A
992SN/A    // list of all blocked contexts
1002SN/A    std::list<WaitRec> waitList;
1012SN/A
1025543Ssaidi@eecs.umich.edu    Addr brk_point;             // top of the data segment
1032SN/A
1045543Ssaidi@eecs.umich.edu    Addr stack_base;            // stack segment base (highest address)
1055543Ssaidi@eecs.umich.edu    unsigned stack_size;        // initial stack size
1065543Ssaidi@eecs.umich.edu    Addr stack_min;             // lowest address accessed on the stack
1072SN/A
1085154Sgblack@eecs.umich.edu    // The maximum size allowed for the stack.
1095154Sgblack@eecs.umich.edu    Addr max_stack_size;
1105154Sgblack@eecs.umich.edu
1112SN/A    // addr to use for next stack region (for multithreaded apps)
1122SN/A    Addr next_thread_stack_base;
1132SN/A
114360SN/A    // Base of region for mmaps (when user doesn't specify an address).
1151408SN/A    Addr mmap_start;
1161408SN/A    Addr mmap_end;
117360SN/A
1181514SN/A    // Base of region for nxm data
1191514SN/A    Addr nxm_start;
1201514SN/A    Addr nxm_end;
1211514SN/A
1225543Ssaidi@eecs.umich.edu    std::string prog_fname;     // file name
1232SN/A
1245999Snate@binkert.org    Stats::Scalar num_syscalls;       // number of syscalls executed
1252SN/A
1262SN/A
1272SN/A  protected:
1282SN/A    // constructor
1298325Ssteve.reinhardt@amd.com    Process(ProcessParams *params);
1302SN/A
1317532Ssteve.reinhardt@amd.com    virtual void initState();
1322SN/A
1332SN/A  protected:
1342378SN/A    /// Memory object for initialization (image loading)
1358706Sandreas.hansson@arm.com    SETranslatingPortProxy *initVirtMem;
1362378SN/A
1372378SN/A  public:
1382378SN/A    PageTable *pTable;
1392SN/A
1404997Sgblack@eecs.umich.edu    //This id is assigned by m5 and is used to keep process' tlb entries
1414997Sgblack@eecs.umich.edu    //separated.
1424997Sgblack@eecs.umich.edu    uint64_t M5_pid;
1434997Sgblack@eecs.umich.edu
1445282Srstrong@cs.ucsd.edu    class FdMap
1455282Srstrong@cs.ucsd.edu    {
1465282Srstrong@cs.ucsd.edu      public:
1478325Ssteve.reinhardt@amd.com        int fd;
1488325Ssteve.reinhardt@amd.com        std::string filename;
1498325Ssteve.reinhardt@amd.com        int mode;
1508325Ssteve.reinhardt@amd.com        int flags;
1518325Ssteve.reinhardt@amd.com        bool isPipe;
1528325Ssteve.reinhardt@amd.com        int readPipeSource;
1538325Ssteve.reinhardt@amd.com        uint64_t fileOffset;
1545282Srstrong@cs.ucsd.edu
1558325Ssteve.reinhardt@amd.com        FdMap()
1568325Ssteve.reinhardt@amd.com            : fd(-1), filename("NULL"), mode(0), flags(0),
1578325Ssteve.reinhardt@amd.com              isPipe(false), readPipeSource(0), fileOffset(0)
1588325Ssteve.reinhardt@amd.com        { }
1595282Srstrong@cs.ucsd.edu
1605282Srstrong@cs.ucsd.edu        void serialize(std::ostream &os);
1615282Srstrong@cs.ucsd.edu        void unserialize(Checkpoint *cp, const std::string &section);
1625282Srstrong@cs.ucsd.edu    };
1635282Srstrong@cs.ucsd.edu
1642SN/A  private:
1652SN/A    // file descriptor remapping support
1665282Srstrong@cs.ucsd.edu    static const int MAX_FD = 256;    // max legal fd value
1675282Srstrong@cs.ucsd.edu    FdMap fd_map[MAX_FD+1];
1685282Srstrong@cs.ucsd.edu
1692SN/A
1702SN/A  public:
1712SN/A    // static helper functions to generate file descriptors for constructor
1722SN/A    static int openInputFile(const std::string &filename);
1732SN/A    static int openOutputFile(const std::string &filename);
1742SN/A
1752SN/A    // override of virtual SimObject method: register statistics
1762SN/A    virtual void regStats();
1772SN/A
1785713Shsul@eecs.umich.edu    // After getting registered with system object, tell process which
1795713Shsul@eecs.umich.edu    // system-wide context id it is assigned.
1805713Shsul@eecs.umich.edu    void assignThreadContext(int context_id)
1815713Shsul@eecs.umich.edu    {
1825713Shsul@eecs.umich.edu        contextIds.push_back(context_id);
1835713Shsul@eecs.umich.edu    }
184180SN/A
1855713Shsul@eecs.umich.edu    // Find a free context to use
1868325Ssteve.reinhardt@amd.com    ThreadContext *findFreeContext();
1872SN/A
1882SN/A    // map simulator fd sim_fd to target fd tgt_fd
1892SN/A    void dup_fd(int sim_fd, int tgt_fd);
1902SN/A
1912SN/A    // generate new target fd for sim_fd
1928325Ssteve.reinhardt@amd.com    int alloc_fd(int sim_fd, std::string filename, int flags, int mode,
1938325Ssteve.reinhardt@amd.com                 bool pipe);
1941970SN/A
1951970SN/A    // free target fd (e.g., after close)
1961970SN/A    void free_fd(int tgt_fd);
1972SN/A
1982SN/A    // look up simulator fd for given target fd
1992SN/A    int sim_fd(int tgt_fd);
2002SN/A
2015282Srstrong@cs.ucsd.edu    // look up simulator fd_map object for a given target fd
2028325Ssteve.reinhardt@amd.com    FdMap *sim_fd_obj(int tgt_fd);
2035282Srstrong@cs.ucsd.edu
2045282Srstrong@cs.ucsd.edu    // fix all offsets for currently open files and save them
2055282Srstrong@cs.ucsd.edu    void fix_file_offsets();
2065282Srstrong@cs.ucsd.edu
2075282Srstrong@cs.ucsd.edu    // find all offsets for currently open files and save them
2085282Srstrong@cs.ucsd.edu    void find_file_offsets();
2095282Srstrong@cs.ucsd.edu
2105282Srstrong@cs.ucsd.edu    // set the source of this read pipe for a checkpoint resume
2115282Srstrong@cs.ucsd.edu    void setReadPipeSource(int read_pipe_fd, int source_fd);
2125282Srstrong@cs.ucsd.edu
2132680Sktlim@umich.edu    virtual void syscall(int64_t callnum, ThreadContext *tc) = 0;
2143311Ssaidi@eecs.umich.edu
2158601Ssteve.reinhardt@amd.com    void allocateMem(Addr vaddr, int64_t size, bool clobber = false);
2168601Ssteve.reinhardt@amd.com
2178539Sgblack@eecs.umich.edu    /// Attempt to fix up a fault at vaddr by allocating a page on the stack.
2188539Sgblack@eecs.umich.edu    /// @return Whether the fault has been fixed.
2198539Sgblack@eecs.umich.edu    bool fixupStackFault(Addr vaddr);
2204434Ssaidi@eecs.umich.edu
2213311Ssaidi@eecs.umich.edu    void serialize(std::ostream &os);
2223311Ssaidi@eecs.umich.edu    void unserialize(Checkpoint *cp, const std::string &section);
2232SN/A};
2242SN/A
2252SN/A//
2262SN/A// "Live" process with system calls redirected to host system
2272SN/A//
228360SN/Aclass ObjectFile;
2292SN/Aclass LiveProcess : public Process
2302SN/A{
231360SN/A  protected:
2322378SN/A    ObjectFile *objFile;
2332378SN/A    std::vector<std::string> argv;
2342378SN/A    std::vector<std::string> envp;
2353669Sbinkertn@umich.edu    std::string cwd;
2362378SN/A
2378325Ssteve.reinhardt@amd.com    LiveProcess(LiveProcessParams *params, ObjectFile *objFile);
2382SN/A
2393114Sgblack@eecs.umich.edu    // Id of the owner of the process
2403114Sgblack@eecs.umich.edu    uint64_t __uid;
2413114Sgblack@eecs.umich.edu    uint64_t __euid;
2423114Sgblack@eecs.umich.edu    uint64_t __gid;
2433114Sgblack@eecs.umich.edu    uint64_t __egid;
2443114Sgblack@eecs.umich.edu
2453114Sgblack@eecs.umich.edu    // pid of the process and it's parent
2463114Sgblack@eecs.umich.edu    uint64_t __pid;
2473114Sgblack@eecs.umich.edu    uint64_t __ppid;
2483114Sgblack@eecs.umich.edu
249360SN/A  public:
2503114Sgblack@eecs.umich.edu
2514793Sgblack@eecs.umich.edu    enum AuxiliaryVectorType {
2524793Sgblack@eecs.umich.edu        M5_AT_NULL = 0,
2534793Sgblack@eecs.umich.edu        M5_AT_IGNORE = 1,
2544793Sgblack@eecs.umich.edu        M5_AT_EXECFD = 2,
2554793Sgblack@eecs.umich.edu        M5_AT_PHDR = 3,
2564793Sgblack@eecs.umich.edu        M5_AT_PHENT = 4,
2574793Sgblack@eecs.umich.edu        M5_AT_PHNUM = 5,
2584793Sgblack@eecs.umich.edu        M5_AT_PAGESZ = 6,
2594793Sgblack@eecs.umich.edu        M5_AT_BASE = 7,
2604793Sgblack@eecs.umich.edu        M5_AT_FLAGS = 8,
2614793Sgblack@eecs.umich.edu        M5_AT_ENTRY = 9,
2624793Sgblack@eecs.umich.edu        M5_AT_NOTELF = 10,
2634793Sgblack@eecs.umich.edu        M5_AT_UID = 11,
2644793Sgblack@eecs.umich.edu        M5_AT_EUID = 12,
2654793Sgblack@eecs.umich.edu        M5_AT_GID = 13,
2664793Sgblack@eecs.umich.edu        M5_AT_EGID = 14,
2674793Sgblack@eecs.umich.edu        // The following may be specific to Linux
2684793Sgblack@eecs.umich.edu        M5_AT_PLATFORM = 15,
2694793Sgblack@eecs.umich.edu        M5_AT_HWCAP = 16,
2704793Sgblack@eecs.umich.edu        M5_AT_CLKTCK = 17,
2714793Sgblack@eecs.umich.edu
2724793Sgblack@eecs.umich.edu        M5_AT_SECURE = 23,
2736399Sgblack@eecs.umich.edu        M5_BASE_PLATFORM = 24,
2746399Sgblack@eecs.umich.edu        M5_AT_RANDOM = 25,
2756399Sgblack@eecs.umich.edu
2766399Sgblack@eecs.umich.edu        M5_AT_EXECFN = 31,
2774793Sgblack@eecs.umich.edu
2784793Sgblack@eecs.umich.edu        M5_AT_VECTOR_SIZE = 44
2794793Sgblack@eecs.umich.edu    };
2804793Sgblack@eecs.umich.edu
2813114Sgblack@eecs.umich.edu    inline uint64_t uid() {return __uid;}
2823114Sgblack@eecs.umich.edu    inline uint64_t euid() {return __euid;}
2833114Sgblack@eecs.umich.edu    inline uint64_t gid() {return __gid;}
2843114Sgblack@eecs.umich.edu    inline uint64_t egid() {return __egid;}
2853114Sgblack@eecs.umich.edu    inline uint64_t pid() {return __pid;}
2863114Sgblack@eecs.umich.edu    inline uint64_t ppid() {return __ppid;}
2873114Sgblack@eecs.umich.edu
2883669Sbinkertn@umich.edu    std::string
2893669Sbinkertn@umich.edu    fullPath(const std::string &filename)
2903669Sbinkertn@umich.edu    {
2913669Sbinkertn@umich.edu        if (filename[0] == '/' || cwd.empty())
2923669Sbinkertn@umich.edu            return filename;
2933669Sbinkertn@umich.edu
2943669Sbinkertn@umich.edu        std::string full = cwd;
2953669Sbinkertn@umich.edu
2963669Sbinkertn@umich.edu        if (cwd[cwd.size() - 1] != '/')
2973669Sbinkertn@umich.edu            full += '/';
2983669Sbinkertn@umich.edu
2993669Sbinkertn@umich.edu        return full + filename;
3003669Sbinkertn@umich.edu    }
3013669Sbinkertn@umich.edu
3025513SMichael.Adler@intel.com    std::string getcwd() const { return cwd; }
3035513SMichael.Adler@intel.com
3042680Sktlim@umich.edu    virtual void syscall(int64_t callnum, ThreadContext *tc);
3056701Sgblack@eecs.umich.edu
3066701Sgblack@eecs.umich.edu    virtual TheISA::IntReg getSyscallArg(ThreadContext *tc, int &i) = 0;
3076701Sgblack@eecs.umich.edu    virtual TheISA::IntReg getSyscallArg(ThreadContext *tc, int &i, int width);
3085958Sgblack@eecs.umich.edu    virtual void setSyscallArg(ThreadContext *tc,
3095958Sgblack@eecs.umich.edu            int i, TheISA::IntReg val) = 0;
3105958Sgblack@eecs.umich.edu    virtual void setSyscallReturn(ThreadContext *tc,
3115958Sgblack@eecs.umich.edu            SyscallReturn return_value) = 0;
3122093SN/A
3138325Ssteve.reinhardt@amd.com    virtual SyscallDesc *getDesc(int callnum) = 0;
3142715Sstever@eecs.umich.edu
3152715Sstever@eecs.umich.edu    // this function is used to create the LiveProcess object, since
3162715Sstever@eecs.umich.edu    // we can't tell which subclass of LiveProcess to use until we
3172715Sstever@eecs.umich.edu    // open and look at the object file.
3188325Ssteve.reinhardt@amd.com    static LiveProcess *create(LiveProcessParams *params);
3192SN/A};
3202SN/A
321360SN/A
3222SN/A#endif // !FULL_SYSTEM
3232SN/A
324360SN/A#endif // __PROCESS_HH__
325