process.hh revision 5154
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
472378SN/A#include "base/statistics.hh"
484111Sgblack@eecs.umich.edu#include "sim/host.hh"
4956SN/A#include "sim/sim_object.hh"
502SN/A
515154Sgblack@eecs.umich.educlass GDBListener;
525154Sgblack@eecs.umich.educlass PageTable;
535154Sgblack@eecs.umich.educlass ProcessParams;
545154Sgblack@eecs.umich.educlass LiveProcessParams;
555154Sgblack@eecs.umich.educlass SyscallDesc;
565154Sgblack@eecs.umich.educlass System;
572680Sktlim@umich.educlass ThreadContext;
582401SN/Aclass TranslatingPort;
593971Sgblack@eecs.umich.edunamespace TheISA
603971Sgblack@eecs.umich.edu{
613971Sgblack@eecs.umich.edu    class RemoteGDB;
623971Sgblack@eecs.umich.edu}
632378SN/A
642SN/Aclass Process : public SimObject
652SN/A{
662SN/A  public:
672SN/A
682378SN/A    /// Pointer to object representing the system this process is
692378SN/A    /// running on.
702378SN/A    System *system;
712378SN/A
722680Sktlim@umich.edu    // have we initialized a thread context from this process?  If
732SN/A    // yes, subsequent contexts are assumed to be for dynamically
742SN/A    // created threads and are not initialized.
752SN/A    bool initialContextLoaded;
762SN/A
772680Sktlim@umich.edu    // thread contexts associated with this process
782680Sktlim@umich.edu    std::vector<ThreadContext *> threadContexts;
79180SN/A
803971Sgblack@eecs.umich.edu    // remote gdb objects
813971Sgblack@eecs.umich.edu    std::vector<TheISA::RemoteGDB *> remoteGDB;
823971Sgblack@eecs.umich.edu    std::vector<GDBListener *> gdbListen;
833971Sgblack@eecs.umich.edu    bool breakpoint();
843971Sgblack@eecs.umich.edu
85180SN/A    // number of CPUs (esxec contexts, really) assigned to this process.
862680Sktlim@umich.edu    unsigned int numCpus() { return threadContexts.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)
962378SN/A        {	}
972SN/A    };
982SN/A
992SN/A    // list of all blocked contexts
1002SN/A    std::list<WaitRec> waitList;
1012SN/A
1022SN/A    Addr brk_point;		// top of the data segment
1032SN/A
1042SN/A    Addr stack_base;		// stack segment base (highest address)
1052SN/A    unsigned stack_size;	// initial stack size
1062SN/A    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
1222SN/A    std::string prog_fname;	// file name
1232SN/A
124729SN/A    Stats::Scalar<> num_syscalls;	// number of syscalls executed
1252SN/A
1262SN/A
1272SN/A  protected:
1282SN/A    // constructor
1295154Sgblack@eecs.umich.edu    Process(ProcessParams * params);
1302SN/A
1311395SN/A    // post initialization startup
1321395SN/A    virtual void startup();
1332SN/A
1342SN/A  protected:
1352378SN/A    /// Memory object for initialization (image loading)
1362401SN/A    TranslatingPort *initVirtMem;
1372378SN/A
1382378SN/A  public:
1392378SN/A    PageTable *pTable;
1402SN/A
1414997Sgblack@eecs.umich.edu    //This id is assigned by m5 and is used to keep process' tlb entries
1424997Sgblack@eecs.umich.edu    //separated.
1434997Sgblack@eecs.umich.edu    uint64_t M5_pid;
1444997Sgblack@eecs.umich.edu
1452SN/A  private:
1462SN/A    // file descriptor remapping support
1471970SN/A    static const int MAX_FD = 256;	// max legal fd value
1482SN/A    int fd_map[MAX_FD+1];
1492SN/A
1502SN/A  public:
1512SN/A    // static helper functions to generate file descriptors for constructor
1522SN/A    static int openInputFile(const std::string &filename);
1532SN/A    static int openOutputFile(const std::string &filename);
1542SN/A
1552SN/A    // override of virtual SimObject method: register statistics
1562SN/A    virtual void regStats();
1572SN/A
1582680Sktlim@umich.edu    // register a thread context for this process.
1592680Sktlim@umich.edu    // returns tc's cpu number (index into threadContexts[])
1602680Sktlim@umich.edu    int registerThreadContext(ThreadContext *tc);
161180SN/A
162180SN/A
1632680Sktlim@umich.edu    void replaceThreadContext(ThreadContext *tc, int tcIndex);
1642SN/A
1652SN/A    // map simulator fd sim_fd to target fd tgt_fd
1662SN/A    void dup_fd(int sim_fd, int tgt_fd);
1672SN/A
1682SN/A    // generate new target fd for sim_fd
1691970SN/A    int alloc_fd(int sim_fd);
1701970SN/A
1711970SN/A    // free target fd (e.g., after close)
1721970SN/A    void free_fd(int tgt_fd);
1732SN/A
1742SN/A    // look up simulator fd for given target fd
1752SN/A    int sim_fd(int tgt_fd);
1762SN/A
1772680Sktlim@umich.edu    virtual void syscall(int64_t callnum, ThreadContext *tc) = 0;
1783311Ssaidi@eecs.umich.edu
1794434Ssaidi@eecs.umich.edu    // check if the this addr is on the next available page and allocate it
1804434Ssaidi@eecs.umich.edu    // if it's not we'll panic
1814434Ssaidi@eecs.umich.edu    bool checkAndAllocNextPage(Addr vaddr);
1824434Ssaidi@eecs.umich.edu
1833311Ssaidi@eecs.umich.edu    void serialize(std::ostream &os);
1843311Ssaidi@eecs.umich.edu    void unserialize(Checkpoint *cp, const std::string &section);
1852SN/A};
1862SN/A
1872SN/A//
1882SN/A// "Live" process with system calls redirected to host system
1892SN/A//
190360SN/Aclass ObjectFile;
1912SN/Aclass LiveProcess : public Process
1922SN/A{
193360SN/A  protected:
1942378SN/A    ObjectFile *objFile;
1952378SN/A    std::vector<std::string> argv;
1962378SN/A    std::vector<std::string> envp;
1973669Sbinkertn@umich.edu    std::string cwd;
1982378SN/A
1995154Sgblack@eecs.umich.edu    LiveProcess(LiveProcessParams * params, ObjectFile *objFile);
2002SN/A
2012561SN/A    virtual void argsInit(int intSize, int pageSize);
2022378SN/A
2033114Sgblack@eecs.umich.edu    // Id of the owner of the process
2043114Sgblack@eecs.umich.edu    uint64_t __uid;
2053114Sgblack@eecs.umich.edu    uint64_t __euid;
2063114Sgblack@eecs.umich.edu    uint64_t __gid;
2073114Sgblack@eecs.umich.edu    uint64_t __egid;
2083114Sgblack@eecs.umich.edu
2093114Sgblack@eecs.umich.edu    // pid of the process and it's parent
2103114Sgblack@eecs.umich.edu    uint64_t __pid;
2113114Sgblack@eecs.umich.edu    uint64_t __ppid;
2123114Sgblack@eecs.umich.edu
213360SN/A  public:
2143114Sgblack@eecs.umich.edu
2154793Sgblack@eecs.umich.edu    enum AuxiliaryVectorType {
2164793Sgblack@eecs.umich.edu        M5_AT_NULL = 0,
2174793Sgblack@eecs.umich.edu        M5_AT_IGNORE = 1,
2184793Sgblack@eecs.umich.edu        M5_AT_EXECFD = 2,
2194793Sgblack@eecs.umich.edu        M5_AT_PHDR = 3,
2204793Sgblack@eecs.umich.edu        M5_AT_PHENT = 4,
2214793Sgblack@eecs.umich.edu        M5_AT_PHNUM = 5,
2224793Sgblack@eecs.umich.edu        M5_AT_PAGESZ = 6,
2234793Sgblack@eecs.umich.edu        M5_AT_BASE = 7,
2244793Sgblack@eecs.umich.edu        M5_AT_FLAGS = 8,
2254793Sgblack@eecs.umich.edu        M5_AT_ENTRY = 9,
2264793Sgblack@eecs.umich.edu        M5_AT_NOTELF = 10,
2274793Sgblack@eecs.umich.edu        M5_AT_UID = 11,
2284793Sgblack@eecs.umich.edu        M5_AT_EUID = 12,
2294793Sgblack@eecs.umich.edu        M5_AT_GID = 13,
2304793Sgblack@eecs.umich.edu        M5_AT_EGID = 14,
2314793Sgblack@eecs.umich.edu        // The following may be specific to Linux
2324793Sgblack@eecs.umich.edu        M5_AT_PLATFORM = 15,
2334793Sgblack@eecs.umich.edu        M5_AT_HWCAP = 16,
2344793Sgblack@eecs.umich.edu        M5_AT_CLKTCK = 17,
2354793Sgblack@eecs.umich.edu
2364793Sgblack@eecs.umich.edu        M5_AT_SECURE = 23,
2374793Sgblack@eecs.umich.edu
2384793Sgblack@eecs.umich.edu        M5_AT_VECTOR_SIZE = 44
2394793Sgblack@eecs.umich.edu    };
2404793Sgblack@eecs.umich.edu
2413114Sgblack@eecs.umich.edu    inline uint64_t uid() {return __uid;}
2423114Sgblack@eecs.umich.edu    inline uint64_t euid() {return __euid;}
2433114Sgblack@eecs.umich.edu    inline uint64_t gid() {return __gid;}
2443114Sgblack@eecs.umich.edu    inline uint64_t egid() {return __egid;}
2453114Sgblack@eecs.umich.edu    inline uint64_t pid() {return __pid;}
2463114Sgblack@eecs.umich.edu    inline uint64_t ppid() {return __ppid;}
2473114Sgblack@eecs.umich.edu
2483669Sbinkertn@umich.edu    std::string
2493669Sbinkertn@umich.edu    fullPath(const std::string &filename)
2503669Sbinkertn@umich.edu    {
2513669Sbinkertn@umich.edu        if (filename[0] == '/' || cwd.empty())
2523669Sbinkertn@umich.edu            return filename;
2533669Sbinkertn@umich.edu
2543669Sbinkertn@umich.edu        std::string full = cwd;
2553669Sbinkertn@umich.edu
2563669Sbinkertn@umich.edu        if (cwd[cwd.size() - 1] != '/')
2573669Sbinkertn@umich.edu            full += '/';
2583669Sbinkertn@umich.edu
2593669Sbinkertn@umich.edu        return full + filename;
2603669Sbinkertn@umich.edu    }
2613669Sbinkertn@umich.edu
2622680Sktlim@umich.edu    virtual void syscall(int64_t callnum, ThreadContext *tc);
2632093SN/A
2642462SN/A    virtual SyscallDesc* getDesc(int callnum) = 0;
2652715Sstever@eecs.umich.edu
2662715Sstever@eecs.umich.edu    // this function is used to create the LiveProcess object, since
2672715Sstever@eecs.umich.edu    // we can't tell which subclass of LiveProcess to use until we
2682715Sstever@eecs.umich.edu    // open and look at the object file.
2695154Sgblack@eecs.umich.edu    static LiveProcess *create(LiveProcessParams * params);
2702SN/A};
2712SN/A
272360SN/A
2732SN/A#endif // !FULL_SYSTEM
2742SN/A
275360SN/A#endif // __PROCESS_HH__
276