process.hh revision 6658
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
546658Snate@binkert.orgclass BaseRemoteGDB;
555154Sgblack@eecs.umich.educlass GDBListener;
565154Sgblack@eecs.umich.educlass PageTable;
575154Sgblack@eecs.umich.educlass ProcessParams;
585154Sgblack@eecs.umich.educlass LiveProcessParams;
595154Sgblack@eecs.umich.educlass SyscallDesc;
605154Sgblack@eecs.umich.educlass System;
612680Sktlim@umich.educlass ThreadContext;
622401SN/Aclass TranslatingPort;
632378SN/A
645758Shsul@eecs.umich.edutemplate<class IntType>
655771Shsul@eecs.umich.edustruct AuxVector
665758Shsul@eecs.umich.edu{
675758Shsul@eecs.umich.edu    IntType a_type;
685771Shsul@eecs.umich.edu    IntType a_val;
695758Shsul@eecs.umich.edu
705771Shsul@eecs.umich.edu    AuxVector()
715758Shsul@eecs.umich.edu    {}
725758Shsul@eecs.umich.edu
735771Shsul@eecs.umich.edu    AuxVector(IntType type, IntType val);
745758Shsul@eecs.umich.edu};
755758Shsul@eecs.umich.edu
762SN/Aclass Process : public SimObject
772SN/A{
782SN/A  public:
792SN/A
802378SN/A    /// Pointer to object representing the system this process is
812378SN/A    /// running on.
822378SN/A    System *system;
832378SN/A
842680Sktlim@umich.edu    // have we initialized a thread context from this process?  If
852SN/A    // yes, subsequent contexts are assumed to be for dynamically
862SN/A    // created threads and are not initialized.
872SN/A    bool initialContextLoaded;
882SN/A
895183Ssaidi@eecs.umich.edu    bool checkpointRestored;
905183Ssaidi@eecs.umich.edu
912680Sktlim@umich.edu    // thread contexts associated with this process
925713Shsul@eecs.umich.edu    std::vector<int> contextIds;
93180SN/A
943971Sgblack@eecs.umich.edu    // remote gdb objects
956658Snate@binkert.org    std::vector<BaseRemoteGDB *> remoteGDB;
963971Sgblack@eecs.umich.edu    std::vector<GDBListener *> gdbListen;
973971Sgblack@eecs.umich.edu    bool breakpoint();
983971Sgblack@eecs.umich.edu
99180SN/A    // number of CPUs (esxec contexts, really) assigned to this process.
1005713Shsul@eecs.umich.edu    unsigned int numCpus() { return contextIds.size(); }
1012SN/A
1022SN/A    // record of blocked context
1032SN/A    struct WaitRec
1042SN/A    {
1052SN/A        Addr waitChan;
1062680Sktlim@umich.edu        ThreadContext *waitingContext;
1072SN/A
1082680Sktlim@umich.edu        WaitRec(Addr chan, ThreadContext *ctx)
1092SN/A            : waitChan(chan), waitingContext(ctx)
1105543Ssaidi@eecs.umich.edu        {       }
1112SN/A    };
1122SN/A
1132SN/A    // list of all blocked contexts
1142SN/A    std::list<WaitRec> waitList;
1152SN/A
1165543Ssaidi@eecs.umich.edu    Addr brk_point;             // top of the data segment
1172SN/A
1185543Ssaidi@eecs.umich.edu    Addr stack_base;            // stack segment base (highest address)
1195543Ssaidi@eecs.umich.edu    unsigned stack_size;        // initial stack size
1205543Ssaidi@eecs.umich.edu    Addr stack_min;             // lowest address accessed on the stack
1212SN/A
1225154Sgblack@eecs.umich.edu    // The maximum size allowed for the stack.
1235154Sgblack@eecs.umich.edu    Addr max_stack_size;
1245154Sgblack@eecs.umich.edu
1252SN/A    // addr to use for next stack region (for multithreaded apps)
1262SN/A    Addr next_thread_stack_base;
1272SN/A
128360SN/A    // Base of region for mmaps (when user doesn't specify an address).
1291408SN/A    Addr mmap_start;
1301408SN/A    Addr mmap_end;
131360SN/A
1321514SN/A    // Base of region for nxm data
1331514SN/A    Addr nxm_start;
1341514SN/A    Addr nxm_end;
1351514SN/A
1365543Ssaidi@eecs.umich.edu    std::string prog_fname;     // file name
1372SN/A
1385999Snate@binkert.org    Stats::Scalar num_syscalls;       // number of syscalls executed
1392SN/A
1402SN/A
1412SN/A  protected:
1422SN/A    // constructor
1435154Sgblack@eecs.umich.edu    Process(ProcessParams * params);
1442SN/A
1451395SN/A    // post initialization startup
1461395SN/A    virtual void startup();
1472SN/A
1482SN/A  protected:
1492378SN/A    /// Memory object for initialization (image loading)
1502401SN/A    TranslatingPort *initVirtMem;
1512378SN/A
1522378SN/A  public:
1532378SN/A    PageTable *pTable;
1542SN/A
1554997Sgblack@eecs.umich.edu    //This id is assigned by m5 and is used to keep process' tlb entries
1564997Sgblack@eecs.umich.edu    //separated.
1574997Sgblack@eecs.umich.edu    uint64_t M5_pid;
1584997Sgblack@eecs.umich.edu
1595282Srstrong@cs.ucsd.edu    class FdMap
1605282Srstrong@cs.ucsd.edu    {
1615282Srstrong@cs.ucsd.edu      public:
1625282Srstrong@cs.ucsd.edu            int fd;
1635282Srstrong@cs.ucsd.edu            std::string filename;
1645282Srstrong@cs.ucsd.edu            int mode;
1655282Srstrong@cs.ucsd.edu            int flags;
1665282Srstrong@cs.ucsd.edu            bool isPipe;
1675282Srstrong@cs.ucsd.edu            int readPipeSource;
1685282Srstrong@cs.ucsd.edu            uint64_t fileOffset;
1695282Srstrong@cs.ucsd.edu
1705282Srstrong@cs.ucsd.edu
1715282Srstrong@cs.ucsd.edu            FdMap()
1725282Srstrong@cs.ucsd.edu            {
1735282Srstrong@cs.ucsd.edu                    fd = -1;
1745282Srstrong@cs.ucsd.edu                    filename = "NULL";
1755282Srstrong@cs.ucsd.edu                    mode = 0;
1765282Srstrong@cs.ucsd.edu                    flags = 0;
1775282Srstrong@cs.ucsd.edu                    isPipe = false;
1785282Srstrong@cs.ucsd.edu                    readPipeSource = 0;
1795282Srstrong@cs.ucsd.edu                    fileOffset = 0;
1805282Srstrong@cs.ucsd.edu
1815282Srstrong@cs.ucsd.edu            }
1825282Srstrong@cs.ucsd.edu
1835282Srstrong@cs.ucsd.edu        void serialize(std::ostream &os);
1845282Srstrong@cs.ucsd.edu        void unserialize(Checkpoint *cp, const std::string &section);
1855282Srstrong@cs.ucsd.edu
1865282Srstrong@cs.ucsd.edu    };
1875282Srstrong@cs.ucsd.edu
1882SN/A  private:
1892SN/A    // file descriptor remapping support
1905282Srstrong@cs.ucsd.edu    static const int MAX_FD = 256;    // max legal fd value
1915282Srstrong@cs.ucsd.edu    FdMap fd_map[MAX_FD+1];
1925282Srstrong@cs.ucsd.edu
1932SN/A
1942SN/A  public:
1952SN/A    // static helper functions to generate file descriptors for constructor
1962SN/A    static int openInputFile(const std::string &filename);
1972SN/A    static int openOutputFile(const std::string &filename);
1982SN/A
1992SN/A    // override of virtual SimObject method: register statistics
2002SN/A    virtual void regStats();
2012SN/A
2025713Shsul@eecs.umich.edu    // After getting registered with system object, tell process which
2035713Shsul@eecs.umich.edu    // system-wide context id it is assigned.
2045713Shsul@eecs.umich.edu    void assignThreadContext(int context_id)
2055713Shsul@eecs.umich.edu    {
2065713Shsul@eecs.umich.edu        contextIds.push_back(context_id);
2075713Shsul@eecs.umich.edu    }
208180SN/A
2095713Shsul@eecs.umich.edu    // Find a free context to use
2105713Shsul@eecs.umich.edu    ThreadContext * findFreeContext();
2112SN/A
2122SN/A    // map simulator fd sim_fd to target fd tgt_fd
2132SN/A    void dup_fd(int sim_fd, int tgt_fd);
2142SN/A
2152SN/A    // generate new target fd for sim_fd
2165282Srstrong@cs.ucsd.edu    int alloc_fd(int sim_fd, std::string filename, int flags, int mode, bool pipe);
2171970SN/A
2181970SN/A    // free target fd (e.g., after close)
2191970SN/A    void free_fd(int tgt_fd);
2202SN/A
2212SN/A    // look up simulator fd for given target fd
2222SN/A    int sim_fd(int tgt_fd);
2232SN/A
2245282Srstrong@cs.ucsd.edu    // look up simulator fd_map object for a given target fd
2255282Srstrong@cs.ucsd.edu    FdMap * sim_fd_obj(int tgt_fd);
2265282Srstrong@cs.ucsd.edu
2275282Srstrong@cs.ucsd.edu    // fix all offsets for currently open files and save them
2285282Srstrong@cs.ucsd.edu    void fix_file_offsets();
2295282Srstrong@cs.ucsd.edu
2305282Srstrong@cs.ucsd.edu    // find all offsets for currently open files and save them
2315282Srstrong@cs.ucsd.edu    void find_file_offsets();
2325282Srstrong@cs.ucsd.edu
2335282Srstrong@cs.ucsd.edu    // set the source of this read pipe for a checkpoint resume
2345282Srstrong@cs.ucsd.edu    void setReadPipeSource(int read_pipe_fd, int source_fd);
2355282Srstrong@cs.ucsd.edu
2362680Sktlim@umich.edu    virtual void syscall(int64_t callnum, ThreadContext *tc) = 0;
2373311Ssaidi@eecs.umich.edu
2384434Ssaidi@eecs.umich.edu    // check if the this addr is on the next available page and allocate it
2394434Ssaidi@eecs.umich.edu    // if it's not we'll panic
2404434Ssaidi@eecs.umich.edu    bool checkAndAllocNextPage(Addr vaddr);
2414434Ssaidi@eecs.umich.edu
2423311Ssaidi@eecs.umich.edu    void serialize(std::ostream &os);
2433311Ssaidi@eecs.umich.edu    void unserialize(Checkpoint *cp, const std::string &section);
2442SN/A};
2452SN/A
2462SN/A//
2472SN/A// "Live" process with system calls redirected to host system
2482SN/A//
249360SN/Aclass ObjectFile;
2502SN/Aclass LiveProcess : public Process
2512SN/A{
252360SN/A  protected:
2532378SN/A    ObjectFile *objFile;
2542378SN/A    std::vector<std::string> argv;
2552378SN/A    std::vector<std::string> envp;
2563669Sbinkertn@umich.edu    std::string cwd;
2572378SN/A
2585154Sgblack@eecs.umich.edu    LiveProcess(LiveProcessParams * params, ObjectFile *objFile);
2592SN/A
2602561SN/A    virtual void argsInit(int intSize, int pageSize);
2612378SN/A
2623114Sgblack@eecs.umich.edu    // Id of the owner of the process
2633114Sgblack@eecs.umich.edu    uint64_t __uid;
2643114Sgblack@eecs.umich.edu    uint64_t __euid;
2653114Sgblack@eecs.umich.edu    uint64_t __gid;
2663114Sgblack@eecs.umich.edu    uint64_t __egid;
2673114Sgblack@eecs.umich.edu
2683114Sgblack@eecs.umich.edu    // pid of the process and it's parent
2693114Sgblack@eecs.umich.edu    uint64_t __pid;
2703114Sgblack@eecs.umich.edu    uint64_t __ppid;
2713114Sgblack@eecs.umich.edu
272360SN/A  public:
2733114Sgblack@eecs.umich.edu
2744793Sgblack@eecs.umich.edu    enum AuxiliaryVectorType {
2754793Sgblack@eecs.umich.edu        M5_AT_NULL = 0,
2764793Sgblack@eecs.umich.edu        M5_AT_IGNORE = 1,
2774793Sgblack@eecs.umich.edu        M5_AT_EXECFD = 2,
2784793Sgblack@eecs.umich.edu        M5_AT_PHDR = 3,
2794793Sgblack@eecs.umich.edu        M5_AT_PHENT = 4,
2804793Sgblack@eecs.umich.edu        M5_AT_PHNUM = 5,
2814793Sgblack@eecs.umich.edu        M5_AT_PAGESZ = 6,
2824793Sgblack@eecs.umich.edu        M5_AT_BASE = 7,
2834793Sgblack@eecs.umich.edu        M5_AT_FLAGS = 8,
2844793Sgblack@eecs.umich.edu        M5_AT_ENTRY = 9,
2854793Sgblack@eecs.umich.edu        M5_AT_NOTELF = 10,
2864793Sgblack@eecs.umich.edu        M5_AT_UID = 11,
2874793Sgblack@eecs.umich.edu        M5_AT_EUID = 12,
2884793Sgblack@eecs.umich.edu        M5_AT_GID = 13,
2894793Sgblack@eecs.umich.edu        M5_AT_EGID = 14,
2904793Sgblack@eecs.umich.edu        // The following may be specific to Linux
2914793Sgblack@eecs.umich.edu        M5_AT_PLATFORM = 15,
2924793Sgblack@eecs.umich.edu        M5_AT_HWCAP = 16,
2934793Sgblack@eecs.umich.edu        M5_AT_CLKTCK = 17,
2944793Sgblack@eecs.umich.edu
2954793Sgblack@eecs.umich.edu        M5_AT_SECURE = 23,
2966399Sgblack@eecs.umich.edu        M5_BASE_PLATFORM = 24,
2976399Sgblack@eecs.umich.edu        M5_AT_RANDOM = 25,
2986399Sgblack@eecs.umich.edu
2996399Sgblack@eecs.umich.edu        M5_AT_EXECFN = 31,
3004793Sgblack@eecs.umich.edu
3014793Sgblack@eecs.umich.edu        M5_AT_VECTOR_SIZE = 44
3024793Sgblack@eecs.umich.edu    };
3034793Sgblack@eecs.umich.edu
3043114Sgblack@eecs.umich.edu    inline uint64_t uid() {return __uid;}
3053114Sgblack@eecs.umich.edu    inline uint64_t euid() {return __euid;}
3063114Sgblack@eecs.umich.edu    inline uint64_t gid() {return __gid;}
3073114Sgblack@eecs.umich.edu    inline uint64_t egid() {return __egid;}
3083114Sgblack@eecs.umich.edu    inline uint64_t pid() {return __pid;}
3093114Sgblack@eecs.umich.edu    inline uint64_t ppid() {return __ppid;}
3103114Sgblack@eecs.umich.edu
3113669Sbinkertn@umich.edu    std::string
3123669Sbinkertn@umich.edu    fullPath(const std::string &filename)
3133669Sbinkertn@umich.edu    {
3143669Sbinkertn@umich.edu        if (filename[0] == '/' || cwd.empty())
3153669Sbinkertn@umich.edu            return filename;
3163669Sbinkertn@umich.edu
3173669Sbinkertn@umich.edu        std::string full = cwd;
3183669Sbinkertn@umich.edu
3193669Sbinkertn@umich.edu        if (cwd[cwd.size() - 1] != '/')
3203669Sbinkertn@umich.edu            full += '/';
3213669Sbinkertn@umich.edu
3223669Sbinkertn@umich.edu        return full + filename;
3233669Sbinkertn@umich.edu    }
3243669Sbinkertn@umich.edu
3255513SMichael.Adler@intel.com    std::string getcwd() const { return cwd; }
3265513SMichael.Adler@intel.com
3272680Sktlim@umich.edu    virtual void syscall(int64_t callnum, ThreadContext *tc);
3285958Sgblack@eecs.umich.edu    virtual TheISA::IntReg getSyscallArg(ThreadContext *tc, int i) = 0;
3295958Sgblack@eecs.umich.edu    virtual void setSyscallArg(ThreadContext *tc,
3305958Sgblack@eecs.umich.edu            int i, TheISA::IntReg val) = 0;
3315958Sgblack@eecs.umich.edu    virtual void setSyscallReturn(ThreadContext *tc,
3325958Sgblack@eecs.umich.edu            SyscallReturn return_value) = 0;
3332093SN/A
3342462SN/A    virtual SyscallDesc* getDesc(int callnum) = 0;
3352715Sstever@eecs.umich.edu
3362715Sstever@eecs.umich.edu    // this function is used to create the LiveProcess object, since
3372715Sstever@eecs.umich.edu    // we can't tell which subclass of LiveProcess to use until we
3382715Sstever@eecs.umich.edu    // open and look at the object file.
3395154Sgblack@eecs.umich.edu    static LiveProcess *create(LiveProcessParams * params);
3402SN/A};
3412SN/A
342360SN/A
3432SN/A#endif // !FULL_SYSTEM
3442SN/A
345360SN/A#endif // __PROCESS_HH__
346