process.hh revision 4117
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"
484117Sgblack@eecs.umich.edu#include "mem/translating_port.hh"
494111Sgblack@eecs.umich.edu#include "sim/host.hh"
5056SN/A#include "sim/sim_object.hh"
512SN/A
522680Sktlim@umich.educlass ThreadContext;
532093SN/Aclass SyscallDesc;
542462SN/Aclass PageTable;
552401SN/Aclass TranslatingPort;
562378SN/Aclass System;
573971Sgblack@eecs.umich.educlass GDBListener;
583971Sgblack@eecs.umich.edunamespace TheISA
593971Sgblack@eecs.umich.edu{
603971Sgblack@eecs.umich.edu    class RemoteGDB;
613971Sgblack@eecs.umich.edu}
622378SN/A
634117Sgblack@eecs.umich.edu//This needs to be templated for cases where 32 bit pointers are needed.
644117Sgblack@eecs.umich.edutemplate<class AddrType>
652561SN/Avoid
664117Sgblack@eecs.umich.educopyStringArray(std::vector<std::string> &strings,
674117Sgblack@eecs.umich.edu        AddrType array_ptr, AddrType data_ptr,
684117Sgblack@eecs.umich.edu        TranslatingPort* memPort)
694117Sgblack@eecs.umich.edu{
704117Sgblack@eecs.umich.edu    AddrType data_ptr_swap;
714117Sgblack@eecs.umich.edu    for (int i = 0; i < strings.size(); ++i) {
724117Sgblack@eecs.umich.edu        data_ptr_swap = htog(data_ptr);
734117Sgblack@eecs.umich.edu        memPort->writeBlob(array_ptr, (uint8_t*)&data_ptr_swap,
744117Sgblack@eecs.umich.edu                sizeof(AddrType));
754117Sgblack@eecs.umich.edu        memPort->writeString(data_ptr, strings[i].c_str());
764117Sgblack@eecs.umich.edu        array_ptr += sizeof(AddrType);
774117Sgblack@eecs.umich.edu        data_ptr += strings[i].size() + 1;
784117Sgblack@eecs.umich.edu    }
794117Sgblack@eecs.umich.edu    // add NULL terminator
804117Sgblack@eecs.umich.edu    data_ptr = 0;
814117Sgblack@eecs.umich.edu
824117Sgblack@eecs.umich.edu    memPort->writeBlob(array_ptr, (uint8_t*)&data_ptr, sizeof(AddrType));
834117Sgblack@eecs.umich.edu}
842561SN/A
852SN/Aclass Process : public SimObject
862SN/A{
872SN/A  public:
882SN/A
892378SN/A    /// Pointer to object representing the system this process is
902378SN/A    /// running on.
912378SN/A    System *system;
922378SN/A
932680Sktlim@umich.edu    // have we initialized a thread context from this process?  If
942SN/A    // yes, subsequent contexts are assumed to be for dynamically
952SN/A    // created threads and are not initialized.
962SN/A    bool initialContextLoaded;
972SN/A
982680Sktlim@umich.edu    // thread contexts associated with this process
992680Sktlim@umich.edu    std::vector<ThreadContext *> threadContexts;
100180SN/A
1013971Sgblack@eecs.umich.edu    // remote gdb objects
1023971Sgblack@eecs.umich.edu    std::vector<TheISA::RemoteGDB *> remoteGDB;
1033971Sgblack@eecs.umich.edu    std::vector<GDBListener *> gdbListen;
1043971Sgblack@eecs.umich.edu    bool breakpoint();
1053971Sgblack@eecs.umich.edu
106180SN/A    // number of CPUs (esxec contexts, really) assigned to this process.
1072680Sktlim@umich.edu    unsigned int numCpus() { return threadContexts.size(); }
1082SN/A
1092SN/A    // record of blocked context
1102SN/A    struct WaitRec
1112SN/A    {
1122SN/A        Addr waitChan;
1132680Sktlim@umich.edu        ThreadContext *waitingContext;
1142SN/A
1152680Sktlim@umich.edu        WaitRec(Addr chan, ThreadContext *ctx)
1162SN/A            : waitChan(chan), waitingContext(ctx)
1172378SN/A        {	}
1182SN/A    };
1192SN/A
1202SN/A    // list of all blocked contexts
1212SN/A    std::list<WaitRec> waitList;
1222SN/A
1232SN/A    Addr brk_point;		// top of the data segment
1242SN/A
1252SN/A    Addr stack_base;		// stack segment base (highest address)
1262SN/A    unsigned stack_size;	// initial stack size
1272SN/A    Addr stack_min;		// lowest address accessed on the stack
1282SN/A
1292SN/A    // addr to use for next stack region (for multithreaded apps)
1302SN/A    Addr next_thread_stack_base;
1312SN/A
132360SN/A    // Base of region for mmaps (when user doesn't specify an address).
1331408SN/A    Addr mmap_start;
1341408SN/A    Addr mmap_end;
135360SN/A
1361514SN/A    // Base of region for nxm data
1371514SN/A    Addr nxm_start;
1381514SN/A    Addr nxm_end;
1391514SN/A
1402SN/A    std::string prog_fname;	// file name
1412SN/A
142729SN/A    Stats::Scalar<> num_syscalls;	// number of syscalls executed
1432SN/A
1442SN/A
1452SN/A  protected:
1462SN/A    // constructor
1471450SN/A    Process(const std::string &nm,
1482378SN/A            System *_system,
1492SN/A            int stdin_fd, 	// initial I/O descriptors
1502SN/A            int stdout_fd,
1512SN/A            int stderr_fd);
1522SN/A
1531395SN/A    // post initialization startup
1541395SN/A    virtual void startup();
1552SN/A
1562SN/A  protected:
1572378SN/A    /// Memory object for initialization (image loading)
1582401SN/A    TranslatingPort *initVirtMem;
1592378SN/A
1602378SN/A  public:
1612378SN/A    PageTable *pTable;
1622SN/A
1632SN/A  private:
1642SN/A    // file descriptor remapping support
1651970SN/A    static const int MAX_FD = 256;	// max legal fd value
1662SN/A    int fd_map[MAX_FD+1];
1672SN/A
1682SN/A  public:
1692SN/A    // static helper functions to generate file descriptors for constructor
1702SN/A    static int openInputFile(const std::string &filename);
1712SN/A    static int openOutputFile(const std::string &filename);
1722SN/A
1732SN/A    // override of virtual SimObject method: register statistics
1742SN/A    virtual void regStats();
1752SN/A
1762680Sktlim@umich.edu    // register a thread context for this process.
1772680Sktlim@umich.edu    // returns tc's cpu number (index into threadContexts[])
1782680Sktlim@umich.edu    int registerThreadContext(ThreadContext *tc);
179180SN/A
180180SN/A
1812680Sktlim@umich.edu    void replaceThreadContext(ThreadContext *tc, int tcIndex);
1822SN/A
1832SN/A    // map simulator fd sim_fd to target fd tgt_fd
1842SN/A    void dup_fd(int sim_fd, int tgt_fd);
1852SN/A
1862SN/A    // generate new target fd for sim_fd
1871970SN/A    int alloc_fd(int sim_fd);
1881970SN/A
1891970SN/A    // free target fd (e.g., after close)
1901970SN/A    void free_fd(int tgt_fd);
1912SN/A
1922SN/A    // look up simulator fd for given target fd
1932SN/A    int sim_fd(int tgt_fd);
1942SN/A
1952680Sktlim@umich.edu    virtual void syscall(int64_t callnum, ThreadContext *tc) = 0;
1963311Ssaidi@eecs.umich.edu
1973311Ssaidi@eecs.umich.edu    void serialize(std::ostream &os);
1983311Ssaidi@eecs.umich.edu    void unserialize(Checkpoint *cp, const std::string &section);
1992SN/A};
2002SN/A
2012SN/A//
2022SN/A// "Live" process with system calls redirected to host system
2032SN/A//
204360SN/Aclass ObjectFile;
2052SN/Aclass LiveProcess : public Process
2062SN/A{
207360SN/A  protected:
2082378SN/A    ObjectFile *objFile;
2092378SN/A    std::vector<std::string> argv;
2102378SN/A    std::vector<std::string> envp;
2113669Sbinkertn@umich.edu    std::string cwd;
2122378SN/A
2131450SN/A    LiveProcess(const std::string &nm, ObjectFile *objFile,
2142378SN/A                System *_system, int stdin_fd, int stdout_fd, int stderr_fd,
2152SN/A                std::vector<std::string> &argv,
2163114Sgblack@eecs.umich.edu                std::vector<std::string> &envp,
2173669Sbinkertn@umich.edu                const std::string &cwd,
2183114Sgblack@eecs.umich.edu                uint64_t _uid, uint64_t _euid,
2193114Sgblack@eecs.umich.edu                uint64_t _gid, uint64_t _egid,
2203114Sgblack@eecs.umich.edu                uint64_t _pid, uint64_t _ppid);
2212SN/A
2222561SN/A    virtual void argsInit(int intSize, int pageSize);
2232378SN/A
2243114Sgblack@eecs.umich.edu    // Id of the owner of the process
2253114Sgblack@eecs.umich.edu    uint64_t __uid;
2263114Sgblack@eecs.umich.edu    uint64_t __euid;
2273114Sgblack@eecs.umich.edu    uint64_t __gid;
2283114Sgblack@eecs.umich.edu    uint64_t __egid;
2293114Sgblack@eecs.umich.edu
2303114Sgblack@eecs.umich.edu    // pid of the process and it's parent
2313114Sgblack@eecs.umich.edu    uint64_t __pid;
2323114Sgblack@eecs.umich.edu    uint64_t __ppid;
2333114Sgblack@eecs.umich.edu
234360SN/A  public:
2353114Sgblack@eecs.umich.edu
2363114Sgblack@eecs.umich.edu    inline uint64_t uid() {return __uid;}
2373114Sgblack@eecs.umich.edu    inline uint64_t euid() {return __euid;}
2383114Sgblack@eecs.umich.edu    inline uint64_t gid() {return __gid;}
2393114Sgblack@eecs.umich.edu    inline uint64_t egid() {return __egid;}
2403114Sgblack@eecs.umich.edu    inline uint64_t pid() {return __pid;}
2413114Sgblack@eecs.umich.edu    inline uint64_t ppid() {return __ppid;}
2423114Sgblack@eecs.umich.edu
2433669Sbinkertn@umich.edu    std::string
2443669Sbinkertn@umich.edu    fullPath(const std::string &filename)
2453669Sbinkertn@umich.edu    {
2463669Sbinkertn@umich.edu        if (filename[0] == '/' || cwd.empty())
2473669Sbinkertn@umich.edu            return filename;
2483669Sbinkertn@umich.edu
2493669Sbinkertn@umich.edu        std::string full = cwd;
2503669Sbinkertn@umich.edu
2513669Sbinkertn@umich.edu        if (cwd[cwd.size() - 1] != '/')
2523669Sbinkertn@umich.edu            full += '/';
2533669Sbinkertn@umich.edu
2543669Sbinkertn@umich.edu        return full + filename;
2553669Sbinkertn@umich.edu    }
2563669Sbinkertn@umich.edu
2572680Sktlim@umich.edu    virtual void syscall(int64_t callnum, ThreadContext *tc);
2582093SN/A
2592462SN/A    virtual SyscallDesc* getDesc(int callnum) = 0;
2602715Sstever@eecs.umich.edu
2612715Sstever@eecs.umich.edu    // this function is used to create the LiveProcess object, since
2622715Sstever@eecs.umich.edu    // we can't tell which subclass of LiveProcess to use until we
2632715Sstever@eecs.umich.edu    // open and look at the object file.
2642715Sstever@eecs.umich.edu    static LiveProcess *create(const std::string &nm,
2652715Sstever@eecs.umich.edu                               System *_system,
2662715Sstever@eecs.umich.edu                               int stdin_fd, int stdout_fd, int stderr_fd,
2672715Sstever@eecs.umich.edu                               std::string executable,
2682715Sstever@eecs.umich.edu                               std::vector<std::string> &argv,
2693114Sgblack@eecs.umich.edu                               std::vector<std::string> &envp,
2703669Sbinkertn@umich.edu                               const std::string &cwd,
2713114Sgblack@eecs.umich.edu                               uint64_t _uid, uint64_t _euid,
2723114Sgblack@eecs.umich.edu                               uint64_t _gid, uint64_t _egid,
2733114Sgblack@eecs.umich.edu                               uint64_t _pid, uint64_t _ppid);
2742SN/A};
2752SN/A
276360SN/A
2772SN/A#endif // !FULL_SYSTEM
2782SN/A
279360SN/A#endif // __PROCESS_HH__
280