process.hh revision 10929:b2bbfec74eca
12817Sksewell@umich.edu/*
28733Sgeoffrey.blake@arm.com * Copyright (c) 2014 Advanced Micro Devices, Inc.
38733Sgeoffrey.blake@arm.com * Copyright (c) 2001-2005 The Regents of The University of Michigan
48733Sgeoffrey.blake@arm.com * All rights reserved.
58733Sgeoffrey.blake@arm.com *
68733Sgeoffrey.blake@arm.com * Redistribution and use in source and binary forms, with or without
78733Sgeoffrey.blake@arm.com * modification, are permitted provided that the following conditions are
88733Sgeoffrey.blake@arm.com * met: redistributions of source code must retain the above copyright
98733Sgeoffrey.blake@arm.com * notice, this list of conditions and the following disclaimer;
108733Sgeoffrey.blake@arm.com * redistributions in binary form must reproduce the above copyright
118733Sgeoffrey.blake@arm.com * notice, this list of conditions and the following disclaimer in the
128733Sgeoffrey.blake@arm.com * documentation and/or other materials provided with the distribution;
138733Sgeoffrey.blake@arm.com * neither the name of the copyright holders nor the names of its
142817Sksewell@umich.edu * contributors may be used to endorse or promote products derived from
152817Sksewell@umich.edu * this software without specific prior written permission.
162817Sksewell@umich.edu *
172817Sksewell@umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
182817Sksewell@umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
192817Sksewell@umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
202817Sksewell@umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
212817Sksewell@umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
222817Sksewell@umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
232817Sksewell@umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
242817Sksewell@umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
252817Sksewell@umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
262817Sksewell@umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
272817Sksewell@umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
282817Sksewell@umich.edu *
292817Sksewell@umich.edu * Authors: Nathan Binkert
302817Sksewell@umich.edu *          Steve Reinhardt
312817Sksewell@umich.edu */
322817Sksewell@umich.edu
332817Sksewell@umich.edu#ifndef __PROCESS_HH__
342817Sksewell@umich.edu#define __PROCESS_HH__
352817Sksewell@umich.edu
362817Sksewell@umich.edu#include <string>
372817Sksewell@umich.edu#include <vector>
382817Sksewell@umich.edu
392817Sksewell@umich.edu#include "arch/registers.hh"
402817Sksewell@umich.edu#include "base/statistics.hh"
412817Sksewell@umich.edu#include "base/types.hh"
422817Sksewell@umich.edu#include "config/the_isa.hh"
432817Sksewell@umich.edu#include "mem/se_translating_port_proxy.hh"
442817Sksewell@umich.edu#include "sim/sim_object.hh"
452817Sksewell@umich.edu#include "sim/syscallreturn.hh"
466658Snate@binkert.org
478229Snate@binkert.orgclass PageTable;
482935Sksewell@umich.edustruct ProcessParams;
492817Sksewell@umich.edustruct LiveProcessParams;
502834Sksewell@umich.educlass SyscallDesc;
512834Sksewell@umich.educlass System;
522834Sksewell@umich.educlass ThreadContext;
538902Sandreas.hansson@arm.comclass EmulatedDriver;
542834Sksewell@umich.edu
552817Sksewell@umich.edutemplate<class IntType>
562817Sksewell@umich.edustruct AuxVector
572817Sksewell@umich.edu{
582817Sksewell@umich.edu    IntType a_type;
592817Sksewell@umich.edu    IntType a_val;
602817Sksewell@umich.edu
612817Sksewell@umich.edu    AuxVector()
622817Sksewell@umich.edu    {}
632817Sksewell@umich.edu
642817Sksewell@umich.edu    AuxVector(IntType type, IntType val);
652817Sksewell@umich.edu};
662817Sksewell@umich.edu
672817Sksewell@umich.educlass Process : public SimObject
682817Sksewell@umich.edu{
692817Sksewell@umich.edu  public:
702817Sksewell@umich.edu
712817Sksewell@umich.edu    /// Pointer to object representing the system this process is
722817Sksewell@umich.edu    /// running on.
732817Sksewell@umich.edu    System *system;
742817Sksewell@umich.edu
752817Sksewell@umich.edu    // thread contexts associated with this process
762817Sksewell@umich.edu    std::vector<int> contextIds;
772817Sksewell@umich.edu
782817Sksewell@umich.edu    // number of CPUs (esxec contexts, really) assigned to this process.
792817Sksewell@umich.edu    unsigned int numCpus() { return contextIds.size(); }
803784Sgblack@eecs.umich.edu
816022Sgblack@eecs.umich.edu    // record of blocked context
823784Sgblack@eecs.umich.edu    struct WaitRec
833784Sgblack@eecs.umich.edu    {
846022Sgblack@eecs.umich.edu        Addr waitChan;
853784Sgblack@eecs.umich.edu        ThreadContext *waitingContext;
868887Sgeoffrey.blake@arm.com
878733Sgeoffrey.blake@arm.com        WaitRec(Addr chan, ThreadContext *ctx)
889020Sgblack@eecs.umich.edu            : waitChan(chan), waitingContext(ctx)
898541Sgblack@eecs.umich.edu        {       }
902817Sksewell@umich.edu    };
912817Sksewell@umich.edu
922817Sksewell@umich.edu    // list of all blocked contexts
932817Sksewell@umich.edu    std::list<WaitRec> waitList;
945712Shsul@eecs.umich.edu
952817Sksewell@umich.edu    Addr brk_point;             // top of the data segment
965714Shsul@eecs.umich.edu
975714Shsul@eecs.umich.edu    Addr stack_base;            // stack segment base (highest address)
985714Shsul@eecs.umich.edu    unsigned stack_size;        // initial stack size
995714Shsul@eecs.umich.edu    Addr stack_min;             // lowest address accessed on the stack
1005715Shsul@eecs.umich.edu
1015715Shsul@eecs.umich.edu    // The maximum size allowed for the stack.
1025715Shsul@eecs.umich.edu    Addr max_stack_size;
1035715Shsul@eecs.umich.edu
1042817Sksewell@umich.edu    // addr to use for next stack region (for multithreaded apps)
1052817Sksewell@umich.edu    Addr next_thread_stack_base;
1062817Sksewell@umich.edu
1072817Sksewell@umich.edu    // Base of region for mmaps (when user doesn't specify an address).
1083548Sgblack@eecs.umich.edu    Addr mmap_start;
1092817Sksewell@umich.edu    Addr mmap_end;
1102817Sksewell@umich.edu
1118541Sgblack@eecs.umich.edu    // Base of region for nxm data
1128541Sgblack@eecs.umich.edu    Addr nxm_start;
1138754Sgblack@eecs.umich.edu    Addr nxm_end;
1148852Sandreas.hansson@arm.com
1152817Sksewell@umich.edu    Stats::Scalar num_syscalls;       // number of syscalls executed
1168852Sandreas.hansson@arm.com
1173675Sktlim@umich.edu  protected:
1188706Sandreas.hansson@arm.com    // constructor
1198706Sandreas.hansson@arm.com    Process(ProcessParams *params);
1208799Sgblack@eecs.umich.edu
1218852Sandreas.hansson@arm.com    virtual void initState();
1228706Sandreas.hansson@arm.com
1232817Sksewell@umich.edu    DrainState drain() M5_ATTR_OVERRIDE;
1242817Sksewell@umich.edu
1252817Sksewell@umich.edu  public:
1262817Sksewell@umich.edu
1272817Sksewell@umich.edu    //This id is assigned by m5 and is used to keep process' tlb entries
1282817Sksewell@umich.edu    //separated.
1292817Sksewell@umich.edu    uint64_t M5_pid;
1302817Sksewell@umich.edu
1312817Sksewell@umich.edu    // flag for using architecture specific page table
1322817Sksewell@umich.edu    bool useArchPT;
1332817Sksewell@umich.edu    // running KvmCPU in SE mode requires special initialization
1342817Sksewell@umich.edu    bool kvmInSE;
1352817Sksewell@umich.edu
1365250Sksewell@umich.edu    PageTableBase* pTable;
1372817Sksewell@umich.edu
1382817Sksewell@umich.edu    class FdMap : public Serializable
1395250Sksewell@umich.edu    {
1402817Sksewell@umich.edu      public:
1412817Sksewell@umich.edu        int fd;
1422817Sksewell@umich.edu        std::string filename;
1432817Sksewell@umich.edu        int mode;
1442817Sksewell@umich.edu        int flags;
1458777Sgblack@eecs.umich.edu        bool isPipe;
1462817Sksewell@umich.edu        int readPipeSource;
1472817Sksewell@umich.edu        uint64_t fileOffset;
1482817Sksewell@umich.edu        EmulatedDriver *driver;
1492817Sksewell@umich.edu
1502817Sksewell@umich.edu        FdMap()
1512817Sksewell@umich.edu            : fd(-1), filename("NULL"), mode(0), flags(0),
1522817Sksewell@umich.edu              isPipe(false), readPipeSource(0), fileOffset(0), driver(NULL)
1532817Sksewell@umich.edu        { }
1542817Sksewell@umich.edu
1552817Sksewell@umich.edu        void serialize(CheckpointOut &cp) const M5_ATTR_OVERRIDE;
1562817Sksewell@umich.edu        void unserialize(CheckpointIn &cp) M5_ATTR_OVERRIDE;
1572817Sksewell@umich.edu    };
1582817Sksewell@umich.edu
1592817Sksewell@umich.edu  protected:
1602817Sksewell@umich.edu    /// Memory proxy for initialization (image loading)
1612817Sksewell@umich.edu    SETranslatingPortProxy initVirtMem;
1622817Sksewell@umich.edu
1632817Sksewell@umich.edu  private:
1642817Sksewell@umich.edu    static const int NUM_FDS = 1024;
1652817Sksewell@umich.edu
1662817Sksewell@umich.edu    // File descriptor remapping support.
1672817Sksewell@umich.edu    FdMap *fd_map;
1682817Sksewell@umich.edu
1692817Sksewell@umich.edu  public:
1702817Sksewell@umich.edu    // static helper functions to generate file descriptors for constructor
1712817Sksewell@umich.edu    static int openInputFile(const std::string &filename);
1722817Sksewell@umich.edu    static int openOutputFile(const std::string &filename);
1732817Sksewell@umich.edu
1742817Sksewell@umich.edu    // override of virtual SimObject method: register statistics
1752817Sksewell@umich.edu    virtual void regStats();
1762817Sksewell@umich.edu
1772817Sksewell@umich.edu    // After getting registered with system object, tell process which
1782817Sksewell@umich.edu    // system-wide context id it is assigned.
1792817Sksewell@umich.edu    void assignThreadContext(int context_id)
1802817Sksewell@umich.edu    {
1812817Sksewell@umich.edu        contextIds.push_back(context_id);
1822817Sksewell@umich.edu    }
1832817Sksewell@umich.edu
1842817Sksewell@umich.edu    // Find a free context to use
1852817Sksewell@umich.edu    ThreadContext *findFreeContext();
1862817Sksewell@umich.edu
1877720Sgblack@eecs.umich.edu    // provide program name for debug messages
1887720Sgblack@eecs.umich.edu    virtual const char *progName() const { return "<unknown>"; }
1897720Sgblack@eecs.umich.edu
1907720Sgblack@eecs.umich.edu    // generate new target fd for sim_fd
1917720Sgblack@eecs.umich.edu    int alloc_fd(int sim_fd, const std::string& filename, int flags, int mode,
1927720Sgblack@eecs.umich.edu                 bool pipe);
1937720Sgblack@eecs.umich.edu
1948733Sgeoffrey.blake@arm.com    // disassociate target fd with simulator fd and cleanup subsidiary fields
1958733Sgeoffrey.blake@arm.com    void free_fdmap_entry(int tgt_fd);
1962817Sksewell@umich.edu
1977720Sgblack@eecs.umich.edu    // look up simulator fd for given target fd
1987720Sgblack@eecs.umich.edu    int sim_fd(int tgt_fd);
1992817Sksewell@umich.edu
2002817Sksewell@umich.edu    // look up simulator fd_map object for a given target fd
2017720Sgblack@eecs.umich.edu    FdMap *sim_fd_obj(int tgt_fd);
2027720Sgblack@eecs.umich.edu
2032817Sksewell@umich.edu    // look up target fd for given host fd
2047720Sgblack@eecs.umich.edu    // Assumes a 1:1 mapping between target file descriptor and host file
2057720Sgblack@eecs.umich.edu    // descriptor. Given the current API, this must be true given that it's
2067720Sgblack@eecs.umich.edu    // not possible to map multiple target file descriptors to the same host
2075259Sksewell@umich.edu    // file descriptor
2082817Sksewell@umich.edu    int tgt_fd(int sim_fd);
2094172Ssaidi@eecs.umich.edu
2105715Shsul@eecs.umich.edu    // fix all offsets for currently open files and save them
2114172Ssaidi@eecs.umich.edu    void fix_file_offsets();
2124172Ssaidi@eecs.umich.edu
2134172Ssaidi@eecs.umich.edu    // find all offsets for currently open files and save them
2142817Sksewell@umich.edu    void find_file_offsets();
2155715Shsul@eecs.umich.edu
2162817Sksewell@umich.edu    // set the source of this read pipe for a checkpoint resume
2172817Sksewell@umich.edu    void setReadPipeSource(int read_pipe_fd, int source_fd);
2184172Ssaidi@eecs.umich.edu
2192817Sksewell@umich.edu    virtual void syscall(int64_t callnum, ThreadContext *tc) = 0;
2202817Sksewell@umich.edu
2212817Sksewell@umich.edu    void allocateMem(Addr vaddr, int64_t size, bool clobber = false);
2224172Ssaidi@eecs.umich.edu
2232817Sksewell@umich.edu    /// Attempt to fix up a fault at vaddr by allocating a page on the stack.
2246313Sgblack@eecs.umich.edu    /// @return Whether the fault has been fixed.
2256313Sgblack@eecs.umich.edu    bool fixupStackFault(Addr vaddr);
2266313Sgblack@eecs.umich.edu
2272817Sksewell@umich.edu    /**
2282817Sksewell@umich.edu     * Maps a contiguous range of virtual addresses in this process's
2292817Sksewell@umich.edu     * address space to a contiguous range of physical addresses.
2302817Sksewell@umich.edu     * This function exists primarily to expose the map operation to
2312817Sksewell@umich.edu     * python, so that configuration scripts can set up mappings in SE mode.
2322817Sksewell@umich.edu     *
2332817Sksewell@umich.edu     * @param vaddr The starting virtual address of the range.
2342817Sksewell@umich.edu     * @param paddr The starting physical address of the range.
2352817Sksewell@umich.edu     * @param size The length of the range in bytes.
2362817Sksewell@umich.edu     * @param cacheable Specifies whether accesses are cacheable.
2372817Sksewell@umich.edu     * @return True if the map operation was successful.  (At this
2382817Sksewell@umich.edu     *           point in time, the map operation always succeeds.)
2392817Sksewell@umich.edu     */
2402817Sksewell@umich.edu    bool map(Addr vaddr, Addr paddr, int size, bool cacheable = true);
2412817Sksewell@umich.edu
2422817Sksewell@umich.edu    void serialize(CheckpointOut &cp) const M5_ATTR_OVERRIDE;
2432817Sksewell@umich.edu    void unserialize(CheckpointIn &cp) M5_ATTR_OVERRIDE;
2442817Sksewell@umich.edu};
2452817Sksewell@umich.edu
2465715Shsul@eecs.umich.edu//
2472817Sksewell@umich.edu// "Live" process with system calls redirected to host system
2482817Sksewell@umich.edu//
2492817Sksewell@umich.educlass ObjectFile;
2508777Sgblack@eecs.umich.educlass LiveProcess : public Process
2515595Sgblack@eecs.umich.edu{
2525595Sgblack@eecs.umich.edu  protected:
2535595Sgblack@eecs.umich.edu    ObjectFile *objFile;
2545595Sgblack@eecs.umich.edu    std::vector<std::string> argv;
2555595Sgblack@eecs.umich.edu    std::vector<std::string> envp;
2565595Sgblack@eecs.umich.edu    std::string cwd;
2572817Sksewell@umich.edu
2582817Sksewell@umich.edu    LiveProcess(LiveProcessParams *params, ObjectFile *objFile);
2592817Sksewell@umich.edu
260    // Id of the owner of the process
261    uint64_t __uid;
262    uint64_t __euid;
263    uint64_t __gid;
264    uint64_t __egid;
265
266    // pid of the process and it's parent
267    uint64_t __pid;
268    uint64_t __ppid;
269
270    // Emulated drivers available to this process
271    std::vector<EmulatedDriver *> drivers;
272
273  public:
274
275    enum AuxiliaryVectorType {
276        M5_AT_NULL = 0,
277        M5_AT_IGNORE = 1,
278        M5_AT_EXECFD = 2,
279        M5_AT_PHDR = 3,
280        M5_AT_PHENT = 4,
281        M5_AT_PHNUM = 5,
282        M5_AT_PAGESZ = 6,
283        M5_AT_BASE = 7,
284        M5_AT_FLAGS = 8,
285        M5_AT_ENTRY = 9,
286        M5_AT_NOTELF = 10,
287        M5_AT_UID = 11,
288        M5_AT_EUID = 12,
289        M5_AT_GID = 13,
290        M5_AT_EGID = 14,
291        // The following may be specific to Linux
292        M5_AT_PLATFORM = 15,
293        M5_AT_HWCAP = 16,
294        M5_AT_CLKTCK = 17,
295
296        M5_AT_SECURE = 23,
297        M5_BASE_PLATFORM = 24,
298        M5_AT_RANDOM = 25,
299
300        M5_AT_EXECFN = 31,
301
302        M5_AT_VECTOR_SIZE = 44
303    };
304
305    inline uint64_t uid() {return __uid;}
306    inline uint64_t euid() {return __euid;}
307    inline uint64_t gid() {return __gid;}
308    inline uint64_t egid() {return __egid;}
309    inline uint64_t pid() {return __pid;}
310    inline uint64_t ppid() {return __ppid;}
311
312    // provide program name for debug messages
313    virtual const char *progName() const { return argv[0].c_str(); }
314
315    std::string
316    fullPath(const std::string &filename)
317    {
318        if (filename[0] == '/' || cwd.empty())
319            return filename;
320
321        std::string full = cwd;
322
323        if (cwd[cwd.size() - 1] != '/')
324            full += '/';
325
326        return full + filename;
327    }
328
329    std::string getcwd() const { return cwd; }
330
331    virtual void syscall(int64_t callnum, ThreadContext *tc);
332
333    virtual TheISA::IntReg getSyscallArg(ThreadContext *tc, int &i) = 0;
334    virtual TheISA::IntReg getSyscallArg(ThreadContext *tc, int &i, int width);
335    virtual void setSyscallArg(ThreadContext *tc,
336            int i, TheISA::IntReg val) = 0;
337    virtual void setSyscallReturn(ThreadContext *tc,
338            SyscallReturn return_value) = 0;
339
340    virtual SyscallDesc *getDesc(int callnum) = 0;
341
342    /**
343     * Find an emulated device driver.
344     *
345     * @param filename Name of the device (under /dev)
346     * @return Pointer to driver object if found, else NULL
347     */
348    EmulatedDriver *findDriver(std::string filename);
349
350    // this function is used to create the LiveProcess object, since
351    // we can't tell which subclass of LiveProcess to use until we
352    // open and look at the object file.
353    static LiveProcess *create(LiveProcessParams *params);
354};
355
356
357#endif // __PROCESS_HH__
358