process.hh revision 11854
12SN/A/*
210298Salexandru.dutu@amd.com * Copyright (c) 2014 Advanced Micro Devices, Inc.
31762SN/A * Copyright (c) 2001-2005 The Regents of The University of Michigan
42SN/A * All rights reserved.
52SN/A *
62SN/A * Redistribution and use in source and binary forms, with or without
72SN/A * modification, are permitted provided that the following conditions are
82SN/A * met: redistributions of source code must retain the above copyright
92SN/A * notice, this list of conditions and the following disclaimer;
102SN/A * redistributions in binary form must reproduce the above copyright
112SN/A * notice, this list of conditions and the following disclaimer in the
122SN/A * documentation and/or other materials provided with the distribution;
132SN/A * neither the name of the copyright holders nor the names of its
142SN/A * contributors may be used to endorse or promote products derived from
152SN/A * this software without specific prior written permission.
162SN/A *
172SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
182SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
192SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
202SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
212SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
222SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
232SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
242SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
252SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
262SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
272SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
282665Ssaidi@eecs.umich.edu *
292665Ssaidi@eecs.umich.edu * Authors: Nathan Binkert
302665Ssaidi@eecs.umich.edu *          Steve Reinhardt
312SN/A */
322SN/A
33360SN/A#ifndef __PROCESS_HH__
34360SN/A#define __PROCESS_HH__
352SN/A
3611854Sbrandon.potter@amd.com#include <inttypes.h>
3711854Sbrandon.potter@amd.com
3810930Sbrandon.potter@amd.com#include <array>
3911800Sbrandon.potter@amd.com#include <map>
404117Sgblack@eecs.umich.edu#include <string>
41180SN/A#include <vector>
422SN/A
436329Sgblack@eecs.umich.edu#include "arch/registers.hh"
442378SN/A#include "base/statistics.hh"
456214Snate@binkert.org#include "base/types.hh"
466658Snate@binkert.org#include "config/the_isa.hh"
478852Sandreas.hansson@arm.com#include "mem/se_translating_port_proxy.hh"
4810930Sbrandon.potter@amd.com#include "sim/fd_entry.hh"
4956SN/A#include "sim/sim_object.hh"
502SN/A
518737Skoansin.tan@gmail.comstruct ProcessParams;
5211800Sbrandon.potter@amd.com
5311800Sbrandon.potter@amd.comclass EmulatedDriver;
5411851Sbrandon.potter@amd.comclass ObjectFile;
5511800Sbrandon.potter@amd.comclass PageTableBase;
565154Sgblack@eecs.umich.educlass SyscallDesc;
5711800Sbrandon.potter@amd.comclass SyscallReturn;
585154Sgblack@eecs.umich.educlass System;
592680Sktlim@umich.educlass ThreadContext;
602378SN/A
612SN/Aclass Process : public SimObject
622SN/A{
632SN/A  public:
642SN/A    struct WaitRec
652SN/A    {
662SN/A        Addr waitChan;
672680Sktlim@umich.edu        ThreadContext *waitingContext;
682SN/A
692680Sktlim@umich.edu        WaitRec(Addr chan, ThreadContext *ctx)
702SN/A            : waitChan(chan), waitingContext(ctx)
7111854Sbrandon.potter@amd.com        { }
722SN/A    };
732SN/A
7411854Sbrandon.potter@amd.com    Process(ProcessParams *params, ObjectFile *obj_file);
752SN/A
7611854Sbrandon.potter@amd.com    void serialize(CheckpointOut &cp) const override;
7711854Sbrandon.potter@amd.com    void unserialize(CheckpointIn &cp) override;
782SN/A
7911169Sandreas.hansson@arm.com    void initState() override;
8011168Sandreas.hansson@arm.com    DrainState drain() override;
8110905Sandreas.sandberg@arm.com
8211854Sbrandon.potter@amd.com    void syscall(int64_t callnum, ThreadContext *tc);
8311854Sbrandon.potter@amd.com    virtual TheISA::IntReg getSyscallArg(ThreadContext *tc, int &i) = 0;
8411854Sbrandon.potter@amd.com    virtual TheISA::IntReg getSyscallArg(ThreadContext *tc, int &i, int width);
8511854Sbrandon.potter@amd.com    virtual void setSyscallArg(ThreadContext *tc, int i,
8611854Sbrandon.potter@amd.com                               TheISA::IntReg val) = 0;
8711854Sbrandon.potter@amd.com    virtual void setSyscallReturn(ThreadContext *tc,
8811854Sbrandon.potter@amd.com                                  SyscallReturn return_value) = 0;
8911854Sbrandon.potter@amd.com    virtual SyscallDesc *getDesc(int callnum) = 0;
902SN/A
9111854Sbrandon.potter@amd.com    inline uint64_t uid() { return _uid; }
9211854Sbrandon.potter@amd.com    inline uint64_t euid() { return _euid; }
9311854Sbrandon.potter@amd.com    inline uint64_t gid() { return _gid; }
9411854Sbrandon.potter@amd.com    inline uint64_t egid() { return _egid; }
9511854Sbrandon.potter@amd.com    inline uint64_t pid() { return _pid; }
9611854Sbrandon.potter@amd.com    inline uint64_t ppid() { return _ppid; }
9710554Salexandru.dutu@amd.com
9811854Sbrandon.potter@amd.com    const char *progName() const { return executable.c_str(); }
9911854Sbrandon.potter@amd.com    std::string fullPath(const std::string &filename);
10011854Sbrandon.potter@amd.com    std::string getcwd() const { return cwd; }
1018852Sandreas.hansson@arm.com
10211854Sbrandon.potter@amd.com    /**
10311854Sbrandon.potter@amd.com     * Find an emulated device driver.
10411854Sbrandon.potter@amd.com     *
10511854Sbrandon.potter@amd.com     * @param filename Name of the device (under /dev)
10611854Sbrandon.potter@amd.com     * @return Pointer to driver object if found, else NULL
10711854Sbrandon.potter@amd.com     */
10811854Sbrandon.potter@amd.com    EmulatedDriver *findDriver(std::string filename);
1098852Sandreas.hansson@arm.com
11011854Sbrandon.potter@amd.com    // This function acts as a callback to update the bias value in
11111854Sbrandon.potter@amd.com    // the object file because the parameters needed to calculate the
11211854Sbrandon.potter@amd.com    // bias are not available when the object file is created.
11311854Sbrandon.potter@amd.com    void updateBias();
11411854Sbrandon.potter@amd.com    Addr getBias();
11511854Sbrandon.potter@amd.com    Addr getStartPC();
11611854Sbrandon.potter@amd.com    ObjectFile *getInterpreter();
1175282Srstrong@cs.ucsd.edu
11810930Sbrandon.potter@amd.com    // inherit file descriptor map from another process (necessary for clone)
11910932Sbrandon.potter@amd.com    void inheritFDArray(Process *p);
1202SN/A
1212SN/A    // override of virtual SimObject method: register statistics
12211169Sandreas.hansson@arm.com    void regStats() override;
1232SN/A
1242SN/A    // generate new target fd for sim_fd
12510932Sbrandon.potter@amd.com    int allocFD(int sim_fd, const std::string& filename, int flags, int mode,
12610932Sbrandon.potter@amd.com                bool pipe);
1271970SN/A
12810929Sbrandon.potter@amd.com    // disassociate target fd with simulator fd and cleanup subsidiary fields
12910932Sbrandon.potter@amd.com    void resetFDEntry(int tgt_fd);
1302SN/A
1312SN/A    // look up simulator fd for given target fd
13210932Sbrandon.potter@amd.com    int getSimFD(int tgt_fd);
1332SN/A
13410930Sbrandon.potter@amd.com    // look up fd entry for a given target fd
13510932Sbrandon.potter@amd.com    FDEntry *getFDEntry(int tgt_fd);
1365282Srstrong@cs.ucsd.edu
13710929Sbrandon.potter@amd.com    // look up target fd for given host fd
13810929Sbrandon.potter@amd.com    // Assumes a 1:1 mapping between target file descriptor and host file
13910929Sbrandon.potter@amd.com    // descriptor. Given the current API, this must be true given that it's
14010929Sbrandon.potter@amd.com    // not possible to map multiple target file descriptors to the same host
14110929Sbrandon.potter@amd.com    // file descriptor
14210932Sbrandon.potter@amd.com    int getTgtFD(int sim_fd);
14310929Sbrandon.potter@amd.com
1445282Srstrong@cs.ucsd.edu    // fix all offsets for currently open files and save them
14510932Sbrandon.potter@amd.com    void fixFileOffsets();
1465282Srstrong@cs.ucsd.edu
1475282Srstrong@cs.ucsd.edu    // find all offsets for currently open files and save them
14810932Sbrandon.potter@amd.com    void findFileOffsets();
1495282Srstrong@cs.ucsd.edu
1505282Srstrong@cs.ucsd.edu    // set the source of this read pipe for a checkpoint resume
1515282Srstrong@cs.ucsd.edu    void setReadPipeSource(int read_pipe_fd, int source_fd);
1525282Srstrong@cs.ucsd.edu
1533311Ssaidi@eecs.umich.edu
1548601Ssteve.reinhardt@amd.com    void allocateMem(Addr vaddr, int64_t size, bool clobber = false);
1558601Ssteve.reinhardt@amd.com
1568539Sgblack@eecs.umich.edu    /// Attempt to fix up a fault at vaddr by allocating a page on the stack.
1578539Sgblack@eecs.umich.edu    /// @return Whether the fault has been fixed.
1588539Sgblack@eecs.umich.edu    bool fixupStackFault(Addr vaddr);
1594434Ssaidi@eecs.umich.edu
16011854Sbrandon.potter@amd.com    // After getting registered with system object, tell process which
16111854Sbrandon.potter@amd.com    // system-wide context id it is assigned.
16211854Sbrandon.potter@amd.com    void
16311854Sbrandon.potter@amd.com    assignThreadContext(ContextID context_id)
16411854Sbrandon.potter@amd.com    {
16511854Sbrandon.potter@amd.com        contextIds.push_back(context_id);
16611854Sbrandon.potter@amd.com    }
16711854Sbrandon.potter@amd.com
16811854Sbrandon.potter@amd.com    // Find a free context to use
16911854Sbrandon.potter@amd.com    ThreadContext *findFreeContext();
17011854Sbrandon.potter@amd.com
17111854Sbrandon.potter@amd.com    /**
17211854Sbrandon.potter@amd.com     * Does mmap region grow upward or downward from mmap_end?  Most
17311854Sbrandon.potter@amd.com     * platforms grow downward, but a few (such as Alpha) grow upward
17411854Sbrandon.potter@amd.com     * instead, so they can override this method to return false.
17511854Sbrandon.potter@amd.com     */
17611854Sbrandon.potter@amd.com    virtual bool mmapGrowsDown() const { return true; }
17711854Sbrandon.potter@amd.com
1789110Ssteve.reinhardt@amd.com    /**
17910558Salexandru.dutu@amd.com     * Maps a contiguous range of virtual addresses in this process's
1809110Ssteve.reinhardt@amd.com     * address space to a contiguous range of physical addresses.
18110558Salexandru.dutu@amd.com     * This function exists primarily to expose the map operation to
18210558Salexandru.dutu@amd.com     * python, so that configuration scripts can set up mappings in SE mode.
1839110Ssteve.reinhardt@amd.com     *
1849110Ssteve.reinhardt@amd.com     * @param vaddr The starting virtual address of the range.
1859110Ssteve.reinhardt@amd.com     * @param paddr The starting physical address of the range.
1869110Ssteve.reinhardt@amd.com     * @param size The length of the range in bytes.
18710558Salexandru.dutu@amd.com     * @param cacheable Specifies whether accesses are cacheable.
1889110Ssteve.reinhardt@amd.com     * @return True if the map operation was successful.  (At this
1899110Ssteve.reinhardt@amd.com     *           point in time, the map operation always succeeds.)
1909110Ssteve.reinhardt@amd.com     */
19110558Salexandru.dutu@amd.com    bool map(Addr vaddr, Addr paddr, int size, bool cacheable = true);
1929110Ssteve.reinhardt@amd.com
19311854Sbrandon.potter@amd.com    // list of all blocked contexts
19411854Sbrandon.potter@amd.com    std::list<WaitRec> waitList;
19511801Sbrandon.potter@amd.com
19611854Sbrandon.potter@amd.com    // thread contexts associated with this process
19711854Sbrandon.potter@amd.com    std::vector<ContextID> contextIds;
19811854Sbrandon.potter@amd.com
19911854Sbrandon.potter@amd.com    // system object which owns this process
20011854Sbrandon.potter@amd.com    System *system;
20111854Sbrandon.potter@amd.com
20211854Sbrandon.potter@amd.com    Addr brk_point;              // top of the data segment
20311854Sbrandon.potter@amd.com    Addr stack_base;             // stack segment base
20411854Sbrandon.potter@amd.com    unsigned stack_size;         // initial stack size
20511854Sbrandon.potter@amd.com    Addr stack_min;              // furthest address accessed from stack base
20611854Sbrandon.potter@amd.com    Addr max_stack_size;         // the maximum size allowed for the stack
20711854Sbrandon.potter@amd.com    Addr next_thread_stack_base; // addr for next region w/ multithreaded apps
20811854Sbrandon.potter@amd.com    Addr mmap_end;               // base of automatic mmap region allocs
20911854Sbrandon.potter@amd.com
21011854Sbrandon.potter@amd.com    Stats::Scalar num_syscalls;  // track how many system calls are executed
21111854Sbrandon.potter@amd.com
21211854Sbrandon.potter@amd.com    bool useArchPT; // flag for using architecture specific page table
21311854Sbrandon.potter@amd.com    bool kvmInSE;   // running KVM requires special initialization
21411854Sbrandon.potter@amd.com
21511854Sbrandon.potter@amd.com    PageTableBase* pTable;
21611854Sbrandon.potter@amd.com
21711854Sbrandon.potter@amd.com    SETranslatingPortProxy initVirtMem; // memory proxy for initial image load
21811854Sbrandon.potter@amd.com
21911854Sbrandon.potter@amd.com    static const int NUM_FDS = 1024;
22011854Sbrandon.potter@amd.com
22111854Sbrandon.potter@amd.com    // File descriptor remapping support.
22211854Sbrandon.potter@amd.com    std::shared_ptr<std::array<FDEntry, NUM_FDS>> fd_array;
22311854Sbrandon.potter@amd.com
22411854Sbrandon.potter@amd.com    // Standard file descriptor options for initialization and checkpoints.
22511854Sbrandon.potter@amd.com    std::map<std::string, int> imap;
22611854Sbrandon.potter@amd.com    std::map<std::string, int> oemap;
22711854Sbrandon.potter@amd.com
22811851Sbrandon.potter@amd.com    ObjectFile *objFile;
22911851Sbrandon.potter@amd.com    std::vector<std::string> argv;
23011851Sbrandon.potter@amd.com    std::vector<std::string> envp;
23111851Sbrandon.potter@amd.com    std::string cwd;
23211851Sbrandon.potter@amd.com    std::string executable;
23311851Sbrandon.potter@amd.com
23411801Sbrandon.potter@amd.com    // Id of the owner of the process
23511801Sbrandon.potter@amd.com    uint64_t _uid;
23611801Sbrandon.potter@amd.com    uint64_t _euid;
23711801Sbrandon.potter@amd.com    uint64_t _gid;
23811801Sbrandon.potter@amd.com    uint64_t _egid;
23911801Sbrandon.potter@amd.com
24011801Sbrandon.potter@amd.com    // pid of the process and it's parent
24111801Sbrandon.potter@amd.com    uint64_t _pid;
24211801Sbrandon.potter@amd.com    uint64_t _ppid;
24311801Sbrandon.potter@amd.com
24410496Ssteve.reinhardt@amd.com    // Emulated drivers available to this process
24510496Ssteve.reinhardt@amd.com    std::vector<EmulatedDriver *> drivers;
2462SN/A};
2472SN/A
248360SN/A#endif // __PROCESS_HH__
249