process.hh revision 6214
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
475958Sgblack@eecs.umich.edu#include "arch/types.hh"
482378SN/A#include "base/statistics.hh"
496214Snate@binkert.org#include "base/types.hh"
5056SN/A#include "sim/sim_object.hh"
515958Sgblack@eecs.umich.edu#include "sim/syscallreturn.hh"
522SN/A
535154Sgblack@eecs.umich.educlass GDBListener;
545154Sgblack@eecs.umich.educlass PageTable;
555154Sgblack@eecs.umich.educlass ProcessParams;
565154Sgblack@eecs.umich.educlass LiveProcessParams;
575154Sgblack@eecs.umich.educlass SyscallDesc;
585154Sgblack@eecs.umich.educlass System;
592680Sktlim@umich.educlass ThreadContext;
602401SN/Aclass TranslatingPort;
613971Sgblack@eecs.umich.edunamespace TheISA
623971Sgblack@eecs.umich.edu{
633971Sgblack@eecs.umich.edu    class RemoteGDB;
643971Sgblack@eecs.umich.edu}
652378SN/A
665758Shsul@eecs.umich.edutemplate<class IntType>
675771Shsul@eecs.umich.edustruct AuxVector
685758Shsul@eecs.umich.edu{
695758Shsul@eecs.umich.edu    IntType a_type;
705771Shsul@eecs.umich.edu    IntType a_val;
715758Shsul@eecs.umich.edu
725771Shsul@eecs.umich.edu    AuxVector()
735758Shsul@eecs.umich.edu    {}
745758Shsul@eecs.umich.edu
755771Shsul@eecs.umich.edu    AuxVector(IntType type, IntType val);
765758Shsul@eecs.umich.edu};
775758Shsul@eecs.umich.edu
782SN/Aclass Process : public SimObject
792SN/A{
802SN/A  public:
812SN/A
822378SN/A    /// Pointer to object representing the system this process is
832378SN/A    /// running on.
842378SN/A    System *system;
852378SN/A
862680Sktlim@umich.edu    // have we initialized a thread context from this process?  If
872SN/A    // yes, subsequent contexts are assumed to be for dynamically
882SN/A    // created threads and are not initialized.
892SN/A    bool initialContextLoaded;
902SN/A
915183Ssaidi@eecs.umich.edu    bool checkpointRestored;
925183Ssaidi@eecs.umich.edu
932680Sktlim@umich.edu    // thread contexts associated with this process
945713Shsul@eecs.umich.edu    std::vector<int> contextIds;
95180SN/A
963971Sgblack@eecs.umich.edu    // remote gdb objects
973971Sgblack@eecs.umich.edu    std::vector<TheISA::RemoteGDB *> remoteGDB;
983971Sgblack@eecs.umich.edu    std::vector<GDBListener *> gdbListen;
993971Sgblack@eecs.umich.edu    bool breakpoint();
1003971Sgblack@eecs.umich.edu
101180SN/A    // number of CPUs (esxec contexts, really) assigned to this process.
1025713Shsul@eecs.umich.edu    unsigned int numCpus() { return contextIds.size(); }
1032SN/A
1042SN/A    // record of blocked context
1052SN/A    struct WaitRec
1062SN/A    {
1072SN/A        Addr waitChan;
1082680Sktlim@umich.edu        ThreadContext *waitingContext;
1092SN/A
1102680Sktlim@umich.edu        WaitRec(Addr chan, ThreadContext *ctx)
1112SN/A            : waitChan(chan), waitingContext(ctx)
1125543Ssaidi@eecs.umich.edu        {       }
1132SN/A    };
1142SN/A
1152SN/A    // list of all blocked contexts
1162SN/A    std::list<WaitRec> waitList;
1172SN/A
1185543Ssaidi@eecs.umich.edu    Addr brk_point;             // top of the data segment
1192SN/A
1205543Ssaidi@eecs.umich.edu    Addr stack_base;            // stack segment base (highest address)
1215543Ssaidi@eecs.umich.edu    unsigned stack_size;        // initial stack size
1225543Ssaidi@eecs.umich.edu    Addr stack_min;             // lowest address accessed on the stack
1232SN/A
1245154Sgblack@eecs.umich.edu    // The maximum size allowed for the stack.
1255154Sgblack@eecs.umich.edu    Addr max_stack_size;
1265154Sgblack@eecs.umich.edu
1272SN/A    // addr to use for next stack region (for multithreaded apps)
1282SN/A    Addr next_thread_stack_base;
1292SN/A
130360SN/A    // Base of region for mmaps (when user doesn't specify an address).
1311408SN/A    Addr mmap_start;
1321408SN/A    Addr mmap_end;
133360SN/A
1341514SN/A    // Base of region for nxm data
1351514SN/A    Addr nxm_start;
1361514SN/A    Addr nxm_end;
1371514SN/A
1385543Ssaidi@eecs.umich.edu    std::string prog_fname;     // file name
1392SN/A
1405999Snate@binkert.org    Stats::Scalar num_syscalls;       // number of syscalls executed
1412SN/A
1422SN/A
1432SN/A  protected:
1442SN/A    // constructor
1455154Sgblack@eecs.umich.edu    Process(ProcessParams * params);
1462SN/A
1471395SN/A    // post initialization startup
1481395SN/A    virtual void startup();
1492SN/A
1502SN/A  protected:
1512378SN/A    /// Memory object for initialization (image loading)
1522401SN/A    TranslatingPort *initVirtMem;
1532378SN/A
1542378SN/A  public:
1552378SN/A    PageTable *pTable;
1562SN/A
1574997Sgblack@eecs.umich.edu    //This id is assigned by m5 and is used to keep process' tlb entries
1584997Sgblack@eecs.umich.edu    //separated.
1594997Sgblack@eecs.umich.edu    uint64_t M5_pid;
1604997Sgblack@eecs.umich.edu
1615282Srstrong@cs.ucsd.edu    class FdMap
1625282Srstrong@cs.ucsd.edu    {
1635282Srstrong@cs.ucsd.edu      public:
1645282Srstrong@cs.ucsd.edu            int fd;
1655282Srstrong@cs.ucsd.edu            std::string filename;
1665282Srstrong@cs.ucsd.edu            int mode;
1675282Srstrong@cs.ucsd.edu            int flags;
1685282Srstrong@cs.ucsd.edu            bool isPipe;
1695282Srstrong@cs.ucsd.edu            int readPipeSource;
1705282Srstrong@cs.ucsd.edu            uint64_t fileOffset;
1715282Srstrong@cs.ucsd.edu
1725282Srstrong@cs.ucsd.edu
1735282Srstrong@cs.ucsd.edu            FdMap()
1745282Srstrong@cs.ucsd.edu            {
1755282Srstrong@cs.ucsd.edu                    fd = -1;
1765282Srstrong@cs.ucsd.edu                    filename = "NULL";
1775282Srstrong@cs.ucsd.edu                    mode = 0;
1785282Srstrong@cs.ucsd.edu                    flags = 0;
1795282Srstrong@cs.ucsd.edu                    isPipe = false;
1805282Srstrong@cs.ucsd.edu                    readPipeSource = 0;
1815282Srstrong@cs.ucsd.edu                    fileOffset = 0;
1825282Srstrong@cs.ucsd.edu
1835282Srstrong@cs.ucsd.edu            }
1845282Srstrong@cs.ucsd.edu
1855282Srstrong@cs.ucsd.edu        void serialize(std::ostream &os);
1865282Srstrong@cs.ucsd.edu        void unserialize(Checkpoint *cp, const std::string &section);
1875282Srstrong@cs.ucsd.edu
1885282Srstrong@cs.ucsd.edu    };
1895282Srstrong@cs.ucsd.edu
1902SN/A  private:
1912SN/A    // file descriptor remapping support
1925282Srstrong@cs.ucsd.edu    static const int MAX_FD = 256;    // max legal fd value
1935282Srstrong@cs.ucsd.edu    FdMap fd_map[MAX_FD+1];
1945282Srstrong@cs.ucsd.edu
1952SN/A
1962SN/A  public:
1972SN/A    // static helper functions to generate file descriptors for constructor
1982SN/A    static int openInputFile(const std::string &filename);
1992SN/A    static int openOutputFile(const std::string &filename);
2002SN/A
2012SN/A    // override of virtual SimObject method: register statistics
2022SN/A    virtual void regStats();
2032SN/A
2045713Shsul@eecs.umich.edu    // After getting registered with system object, tell process which
2055713Shsul@eecs.umich.edu    // system-wide context id it is assigned.
2065713Shsul@eecs.umich.edu    void assignThreadContext(int context_id)
2075713Shsul@eecs.umich.edu    {
2085713Shsul@eecs.umich.edu        contextIds.push_back(context_id);
2095713Shsul@eecs.umich.edu    }
210180SN/A
2115713Shsul@eecs.umich.edu    // Find a free context to use
2125713Shsul@eecs.umich.edu    ThreadContext * findFreeContext();
2132SN/A
2142SN/A    // map simulator fd sim_fd to target fd tgt_fd
2152SN/A    void dup_fd(int sim_fd, int tgt_fd);
2162SN/A
2172SN/A    // generate new target fd for sim_fd
2185282Srstrong@cs.ucsd.edu    int alloc_fd(int sim_fd, std::string filename, int flags, int mode, bool pipe);
2191970SN/A
2201970SN/A    // free target fd (e.g., after close)
2211970SN/A    void free_fd(int tgt_fd);
2222SN/A
2232SN/A    // look up simulator fd for given target fd
2242SN/A    int sim_fd(int tgt_fd);
2252SN/A
2265282Srstrong@cs.ucsd.edu    // look up simulator fd_map object for a given target fd
2275282Srstrong@cs.ucsd.edu    FdMap * sim_fd_obj(int tgt_fd);
2285282Srstrong@cs.ucsd.edu
2295282Srstrong@cs.ucsd.edu    // fix all offsets for currently open files and save them
2305282Srstrong@cs.ucsd.edu    void fix_file_offsets();
2315282Srstrong@cs.ucsd.edu
2325282Srstrong@cs.ucsd.edu    // find all offsets for currently open files and save them
2335282Srstrong@cs.ucsd.edu    void find_file_offsets();
2345282Srstrong@cs.ucsd.edu
2355282Srstrong@cs.ucsd.edu    // set the source of this read pipe for a checkpoint resume
2365282Srstrong@cs.ucsd.edu    void setReadPipeSource(int read_pipe_fd, int source_fd);
2375282Srstrong@cs.ucsd.edu
2382680Sktlim@umich.edu    virtual void syscall(int64_t callnum, ThreadContext *tc) = 0;
2393311Ssaidi@eecs.umich.edu
2404434Ssaidi@eecs.umich.edu    // check if the this addr is on the next available page and allocate it
2414434Ssaidi@eecs.umich.edu    // if it's not we'll panic
2424434Ssaidi@eecs.umich.edu    bool checkAndAllocNextPage(Addr vaddr);
2434434Ssaidi@eecs.umich.edu
2443311Ssaidi@eecs.umich.edu    void serialize(std::ostream &os);
2453311Ssaidi@eecs.umich.edu    void unserialize(Checkpoint *cp, const std::string &section);
2462SN/A};
2472SN/A
2482SN/A//
2492SN/A// "Live" process with system calls redirected to host system
2502SN/A//
251360SN/Aclass ObjectFile;
2522SN/Aclass LiveProcess : public Process
2532SN/A{
254360SN/A  protected:
2552378SN/A    ObjectFile *objFile;
2562378SN/A    std::vector<std::string> argv;
2572378SN/A    std::vector<std::string> envp;
2583669Sbinkertn@umich.edu    std::string cwd;
2592378SN/A
2605154Sgblack@eecs.umich.edu    LiveProcess(LiveProcessParams * params, ObjectFile *objFile);
2612SN/A
2622561SN/A    virtual void argsInit(int intSize, int pageSize);
2632378SN/A
2643114Sgblack@eecs.umich.edu    // Id of the owner of the process
2653114Sgblack@eecs.umich.edu    uint64_t __uid;
2663114Sgblack@eecs.umich.edu    uint64_t __euid;
2673114Sgblack@eecs.umich.edu    uint64_t __gid;
2683114Sgblack@eecs.umich.edu    uint64_t __egid;
2693114Sgblack@eecs.umich.edu
2703114Sgblack@eecs.umich.edu    // pid of the process and it's parent
2713114Sgblack@eecs.umich.edu    uint64_t __pid;
2723114Sgblack@eecs.umich.edu    uint64_t __ppid;
2733114Sgblack@eecs.umich.edu
274360SN/A  public:
2753114Sgblack@eecs.umich.edu
2764793Sgblack@eecs.umich.edu    enum AuxiliaryVectorType {
2774793Sgblack@eecs.umich.edu        M5_AT_NULL = 0,
2784793Sgblack@eecs.umich.edu        M5_AT_IGNORE = 1,
2794793Sgblack@eecs.umich.edu        M5_AT_EXECFD = 2,
2804793Sgblack@eecs.umich.edu        M5_AT_PHDR = 3,
2814793Sgblack@eecs.umich.edu        M5_AT_PHENT = 4,
2824793Sgblack@eecs.umich.edu        M5_AT_PHNUM = 5,
2834793Sgblack@eecs.umich.edu        M5_AT_PAGESZ = 6,
2844793Sgblack@eecs.umich.edu        M5_AT_BASE = 7,
2854793Sgblack@eecs.umich.edu        M5_AT_FLAGS = 8,
2864793Sgblack@eecs.umich.edu        M5_AT_ENTRY = 9,
2874793Sgblack@eecs.umich.edu        M5_AT_NOTELF = 10,
2884793Sgblack@eecs.umich.edu        M5_AT_UID = 11,
2894793Sgblack@eecs.umich.edu        M5_AT_EUID = 12,
2904793Sgblack@eecs.umich.edu        M5_AT_GID = 13,
2914793Sgblack@eecs.umich.edu        M5_AT_EGID = 14,
2924793Sgblack@eecs.umich.edu        // The following may be specific to Linux
2934793Sgblack@eecs.umich.edu        M5_AT_PLATFORM = 15,
2944793Sgblack@eecs.umich.edu        M5_AT_HWCAP = 16,
2954793Sgblack@eecs.umich.edu        M5_AT_CLKTCK = 17,
2964793Sgblack@eecs.umich.edu
2974793Sgblack@eecs.umich.edu        M5_AT_SECURE = 23,
2984793Sgblack@eecs.umich.edu
2994793Sgblack@eecs.umich.edu        M5_AT_VECTOR_SIZE = 44
3004793Sgblack@eecs.umich.edu    };
3014793Sgblack@eecs.umich.edu
3023114Sgblack@eecs.umich.edu    inline uint64_t uid() {return __uid;}
3033114Sgblack@eecs.umich.edu    inline uint64_t euid() {return __euid;}
3043114Sgblack@eecs.umich.edu    inline uint64_t gid() {return __gid;}
3053114Sgblack@eecs.umich.edu    inline uint64_t egid() {return __egid;}
3063114Sgblack@eecs.umich.edu    inline uint64_t pid() {return __pid;}
3073114Sgblack@eecs.umich.edu    inline uint64_t ppid() {return __ppid;}
3083114Sgblack@eecs.umich.edu
3093669Sbinkertn@umich.edu    std::string
3103669Sbinkertn@umich.edu    fullPath(const std::string &filename)
3113669Sbinkertn@umich.edu    {
3123669Sbinkertn@umich.edu        if (filename[0] == '/' || cwd.empty())
3133669Sbinkertn@umich.edu            return filename;
3143669Sbinkertn@umich.edu
3153669Sbinkertn@umich.edu        std::string full = cwd;
3163669Sbinkertn@umich.edu
3173669Sbinkertn@umich.edu        if (cwd[cwd.size() - 1] != '/')
3183669Sbinkertn@umich.edu            full += '/';
3193669Sbinkertn@umich.edu
3203669Sbinkertn@umich.edu        return full + filename;
3213669Sbinkertn@umich.edu    }
3223669Sbinkertn@umich.edu
3235513SMichael.Adler@intel.com    std::string getcwd() const { return cwd; }
3245513SMichael.Adler@intel.com
3252680Sktlim@umich.edu    virtual void syscall(int64_t callnum, ThreadContext *tc);
3265958Sgblack@eecs.umich.edu    virtual TheISA::IntReg getSyscallArg(ThreadContext *tc, int i) = 0;
3275958Sgblack@eecs.umich.edu    virtual void setSyscallArg(ThreadContext *tc,
3285958Sgblack@eecs.umich.edu            int i, TheISA::IntReg val) = 0;
3295958Sgblack@eecs.umich.edu    virtual void setSyscallReturn(ThreadContext *tc,
3305958Sgblack@eecs.umich.edu            SyscallReturn return_value) = 0;
3312093SN/A
3322462SN/A    virtual SyscallDesc* getDesc(int callnum) = 0;
3332715Sstever@eecs.umich.edu
3342715Sstever@eecs.umich.edu    // this function is used to create the LiveProcess object, since
3352715Sstever@eecs.umich.edu    // we can't tell which subclass of LiveProcess to use until we
3362715Sstever@eecs.umich.edu    // open and look at the object file.
3375154Sgblack@eecs.umich.edu    static LiveProcess *create(LiveProcessParams * params);
3382SN/A};
3392SN/A
340360SN/A
3412SN/A#endif // !FULL_SYSTEM
3422SN/A
343360SN/A#endif // __PROCESS_HH__
344