process.hh revision 14149
12SN/A/*
21762SN/A * Copyright (c) 2014-2016 Advanced Micro Devices, Inc.
32SN/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
272665Ssaidi@eecs.umich.edu * 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
312665Ssaidi@eecs.umich.edu *          Brandon Potter
322SN/A */
332SN/A
342SN/A#ifndef __PROCESS_HH__
352SN/A#define __PROCESS_HH__
367349SAli.Saidi@ARM.com
377680Sgblack@eecs.umich.edu#include <inttypes.h>
3856SN/A
398229Snate@binkert.org#include <map>
401717SN/A#include <string>
412518SN/A#include <vector>
4256SN/A
434776Sgblack@eecs.umich.edu#include "arch/registers.hh"
448232Snate@binkert.org#include "base/statistics.hh"
454762Snate@binkert.org#include "base/types.hh"
463065Sgblack@eecs.umich.edu#include "config/the_isa.hh"
472SN/A#include "mem/se_translating_port_proxy.hh"
482973Sgblack@eecs.umich.edu#include "sim/fd_array.hh"
492SN/A#include "sim/fd_entry.hh"
503506Ssaidi@eecs.umich.edu#include "sim/mem_state.hh"
514054Sbinkertn@umich.edu#include "sim/sim_object.hh"
524054Sbinkertn@umich.edu
535866Sksewell@umich.edustruct ProcessParams;
545866Sksewell@umich.edu
555866Sksewell@umich.educlass EmulatedDriver;
565866Sksewell@umich.educlass ObjectFile;
575866Sksewell@umich.educlass EmulationPageTable;
585866Sksewell@umich.educlass SyscallDesc;
595784Sgblack@eecs.umich.educlass SyscallReturn;
604054Sbinkertn@umich.educlass System;
614776Sgblack@eecs.umich.educlass ThreadContext;
624054Sbinkertn@umich.edu
638300Schander.sudanthi@arm.comclass Process : public SimObject
648300Schander.sudanthi@arm.com{
658300Schander.sudanthi@arm.com  public:
668300Schander.sudanthi@arm.com    Process(ProcessParams *params, EmulationPageTable *pTable,
678300Schander.sudanthi@arm.com            ObjectFile *obj_file);
688300Schander.sudanthi@arm.com
698232Snate@binkert.org    void serialize(CheckpointOut &cp) const override;
705866Sksewell@umich.edu    void unserialize(CheckpointIn &cp) override;
714054Sbinkertn@umich.edu
724776Sgblack@eecs.umich.edu    void initState() override;
734054Sbinkertn@umich.edu    DrainState drain() override;
748232Snate@binkert.org
754776Sgblack@eecs.umich.edu    virtual void syscall(int64_t callnum, ThreadContext *tc, Fault *fault);
764054Sbinkertn@umich.edu    virtual RegVal getSyscallArg(ThreadContext *tc, int &i) = 0;
778300Schander.sudanthi@arm.com    virtual RegVal getSyscallArg(ThreadContext *tc, int &i, int width);
788300Schander.sudanthi@arm.com    virtual void setSyscallArg(ThreadContext *tc, int i, RegVal val) = 0;
798300Schander.sudanthi@arm.com    virtual void setSyscallReturn(ThreadContext *tc,
808232Snate@binkert.org                                  SyscallReturn return_value) = 0;
815715Shsul@eecs.umich.edu    virtual SyscallDesc *getDesc(int callnum) = 0;
824776Sgblack@eecs.umich.edu
834776Sgblack@eecs.umich.edu    inline uint64_t uid() { return _uid; }
844776Sgblack@eecs.umich.edu    inline uint64_t euid() { return _euid; }
857720Sgblack@eecs.umich.edu    inline uint64_t gid() { return _gid; }
869809Sumesh.b2006@gmail.com    inline uint64_t egid() { return _egid; }
879809Sumesh.b2006@gmail.com    inline uint64_t pid() { return _pid; }
889809Sumesh.b2006@gmail.com    inline uint64_t ppid() { return _ppid; }
897349SAli.Saidi@ARM.com    inline uint64_t pgid() { return _pgid; }
907349SAli.Saidi@ARM.com    inline void pgid(uint64_t pgid) { _pgid = pgid; }
915784Sgblack@eecs.umich.edu    inline uint64_t tgid() { return _tgid; }
927720Sgblack@eecs.umich.edu
937349SAli.Saidi@ARM.com    const char *progName() const { return executable.c_str(); }
944776Sgblack@eecs.umich.edu
954776Sgblack@eecs.umich.edu    /**
965784Sgblack@eecs.umich.edu     * Find an emulated device driver.
977720Sgblack@eecs.umich.edu     *
985784Sgblack@eecs.umich.edu     * @param filename Name of the device (under /dev)
995784Sgblack@eecs.umich.edu     * @return Pointer to driver object if found, else nullptr
1005784Sgblack@eecs.umich.edu     */
1015784Sgblack@eecs.umich.edu    EmulatedDriver *findDriver(std::string filename);
1025784Sgblack@eecs.umich.edu
1035784Sgblack@eecs.umich.edu    // This function acts as a callback to update the bias value in
1044776Sgblack@eecs.umich.edu    // the object file because the parameters needed to calculate the
1054776Sgblack@eecs.umich.edu    // bias are not available when the object file is created.
1064776Sgblack@eecs.umich.edu    void updateBias();
1074776Sgblack@eecs.umich.edu    Addr getBias();
1084776Sgblack@eecs.umich.edu    Addr getStartPC();
1097349SAli.Saidi@ARM.com    ObjectFile *getInterpreter();
1104776Sgblack@eecs.umich.edu
1115784Sgblack@eecs.umich.edu    // override of virtual SimObject method: register statistics
1125784Sgblack@eecs.umich.edu    void regStats() override;
1135784Sgblack@eecs.umich.edu
1148232Snate@binkert.org    void allocateMem(Addr vaddr, int64_t size, bool clobber = false);
1155784Sgblack@eecs.umich.edu
1165784Sgblack@eecs.umich.edu    /// Attempt to fix up a fault at vaddr by allocating a page on the stack.
1175784Sgblack@eecs.umich.edu    /// @return Whether the fault has been fixed.
1188232Snate@binkert.org    bool fixupStackFault(Addr vaddr);
1197600Sminkyu.jeong@arm.com
1207600Sminkyu.jeong@arm.com    // After getting registered with system object, tell process which
1217600Sminkyu.jeong@arm.com    // system-wide context id it is assigned.
1228232Snate@binkert.org    void
1235784Sgblack@eecs.umich.edu    assignThreadContext(ContextID context_id)
1245784Sgblack@eecs.umich.edu    {
1255784Sgblack@eecs.umich.edu        contextIds.push_back(context_id);
1268232Snate@binkert.org    }
1275784Sgblack@eecs.umich.edu
1285784Sgblack@eecs.umich.edu    // Find a free context to use
1298232Snate@binkert.org    ThreadContext *findFreeContext();
1305784Sgblack@eecs.umich.edu
1315784Sgblack@eecs.umich.edu    /**
1328232Snate@binkert.org     * After delegating a thread context to a child process
1335784Sgblack@eecs.umich.edu     * no longer should relate to the ThreadContext
1344776Sgblack@eecs.umich.edu     */
1354776Sgblack@eecs.umich.edu    void revokeThreadContext(int context_id);
1364776Sgblack@eecs.umich.edu
1374776Sgblack@eecs.umich.edu    /**
1384776Sgblack@eecs.umich.edu     * Does mmap region grow upward or downward from mmapEnd?  Most
1394776Sgblack@eecs.umich.edu     * platforms grow downward, but a few (such as Alpha) grow upward
1403506Ssaidi@eecs.umich.edu     * instead, so they can override this method to return false.
1413506Ssaidi@eecs.umich.edu     */
1425784Sgblack@eecs.umich.edu    virtual bool mmapGrowsDown() const { return true; }
1435784Sgblack@eecs.umich.edu
1445784Sgblack@eecs.umich.edu    /**
1455784Sgblack@eecs.umich.edu     * Maps a contiguous range of virtual addresses in this process's
1465784Sgblack@eecs.umich.edu     * address space to a contiguous range of physical addresses.
1475784Sgblack@eecs.umich.edu     * This function exists primarily to expose the map operation to
1485784Sgblack@eecs.umich.edu     * python, so that configuration scripts can set up mappings in SE mode.
1495784Sgblack@eecs.umich.edu     *
1505784Sgblack@eecs.umich.edu     * @param vaddr The starting virtual address of the range.
1515784Sgblack@eecs.umich.edu     * @param paddr The starting physical address of the range.
1525784Sgblack@eecs.umich.edu     * @param size The length of the range in bytes.
1538232Snate@binkert.org     * @param cacheable Specifies whether accesses are cacheable.
1548232Snate@binkert.org     * @return True if the map operation was successful.  (At this
1558232Snate@binkert.org     *           point in time, the map operation always succeeds.)
1568232Snate@binkert.org     */
1575791Srstrong@cs.ucsd.edu    bool map(Addr vaddr, Addr paddr, int size, bool cacheable = true);
1585784Sgblack@eecs.umich.edu
1595784Sgblack@eecs.umich.edu    void replicatePage(Addr vaddr, Addr new_paddr, ThreadContext *old_tc,
1608232Snate@binkert.org                       ThreadContext *new_tc, bool alloc_page);
1615784Sgblack@eecs.umich.edu
1625784Sgblack@eecs.umich.edu    virtual void clone(ThreadContext *old_tc, ThreadContext *new_tc,
1635784Sgblack@eecs.umich.edu                       Process *new_p, RegVal flags);
1645784Sgblack@eecs.umich.edu
1657811Ssteve.reinhardt@amd.com    // thread contexts associated with this process
1664776Sgblack@eecs.umich.edu    std::vector<ContextID> contextIds;
1672SN/A
1682SN/A    // system object which owns this process
1694776Sgblack@eecs.umich.edu    System *system;
1702SN/A
1714776Sgblack@eecs.umich.edu    Stats::Scalar numSyscalls;  // track how many system calls are executed
1724776Sgblack@eecs.umich.edu
1733748Sgblack@eecs.umich.edu    // flag for using architecture specific page table
1745034Smilesck@eecs.umich.edu    bool useArchPT;
1758902Sandreas.hansson@arm.com    // running KVM requires special initialization
176    bool kvmInSE;
177    // flag for using the process as a thread which shares page tables
178    bool useForClone;
179
180    EmulationPageTable *pTable;
181
182    SETranslatingPortProxy initVirtMem; // memory proxy for initial image load
183
184    ObjectFile *objFile;
185    std::vector<std::string> argv;
186    std::vector<std::string> envp;
187    std::string executable;
188
189    /**
190     * Return an absolute path given a relative path paired with the current
191     * working directory of the process running under simulation.
192     *
193     * @param path The relative path (generally a filename) that needs the
194     * current working directory prepended.
195     * @param host_fs A flag which determines whether to return a
196     * path for the host filesystem or the filesystem of the process running
197     * under simulation. Only matters if filesysem redirection is used to
198     * replace files (or directories) that would normally appear via the
199     * host filesystem.
200     * @return String containing an absolute path.
201     */
202    std::string absolutePath(const std::string &path, bool host_fs);
203
204    /**
205     * Redirect file path if it matches any keys initialized by system object.
206     * @param filename An input parameter containing either a relative path
207     * or an absolute path. If given a relative path, the path will be
208     * prepended to the current working directory of the simulation with
209     * respect to the host filesystem.
210     * @return String containing an absolute path.
211     */
212    std::string checkPathRedirect(const std::string &filename);
213
214    /**
215     * The cwd members are used to track changes to the current working
216     * directory for the purpose of executing system calls which depend on
217     * relative paths (i.e. open, chdir).
218     *
219     * The tgt member and host member may differ if the path for the current
220     * working directory is redirected to point to a different location
221     * (i.e. `cd /proc` should point to '$(gem5_repo)/m5out/fs/proc'
222     * instead of '/proc').
223     */
224    std::string tgtCwd;
225    std::string hostCwd;
226
227    // Syscall emulation uname release.
228    std::string release;
229
230    // Id of the owner of the process
231    uint64_t _uid;
232    uint64_t _euid;
233    uint64_t _gid;
234    uint64_t _egid;
235
236    // pid of the process and it's parent
237    uint64_t _pid;
238    uint64_t _ppid;
239    uint64_t _pgid;
240    uint64_t _tgid;
241
242    // Emulated drivers available to this process
243    std::vector<EmulatedDriver *> drivers;
244
245    std::shared_ptr<FDArray> fds;
246
247    bool *exitGroup;
248    std::shared_ptr<MemState> memState;
249
250    /**
251     * Calls a futex wakeup at the address specified by this pointer when
252     * this process exits.
253     */
254    uint64_t childClearTID;
255
256    // Process was forked with SIGCHLD set.
257    bool *sigchld;
258};
259
260#endif // __PROCESS_HH__
261