a1ca84bf555d8e47956786b1ef05ea24d4db4fd8
[gem5.git] / src / sim / process.hh
1 /*
2 * Copyright (c) 2014-2016 Advanced Micro Devices, Inc.
3 * Copyright (c) 2001-2005 The Regents of The University of Michigan
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met: redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer;
10 * redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution;
13 * neither the name of the copyright holders nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 * Authors: Nathan Binkert
30 * Steve Reinhardt
31 * Brandon Potter
32 */
33
34 #ifndef __PROCESS_HH__
35 #define __PROCESS_HH__
36
37 #include <inttypes.h>
38
39 #include <map>
40 #include <string>
41 #include <vector>
42
43 #include "arch/registers.hh"
44 #include "base/statistics.hh"
45 #include "base/types.hh"
46 #include "config/the_isa.hh"
47 #include "mem/se_translating_port_proxy.hh"
48 #include "sim/fd_array.hh"
49 #include "sim/fd_entry.hh"
50 #include "sim/mem_state.hh"
51 #include "sim/sim_object.hh"
52
53 struct ProcessParams;
54
55 class EmulatedDriver;
56 class ObjectFile;
57 class EmulationPageTable;
58 class SyscallDesc;
59 class SyscallReturn;
60 class System;
61 class ThreadContext;
62
63 class Process : public SimObject
64 {
65 public:
66 Process(ProcessParams *params, EmulationPageTable *pTable,
67 ObjectFile *obj_file);
68
69 void serialize(CheckpointOut &cp) const override;
70 void unserialize(CheckpointIn &cp) override;
71
72 void initState() override;
73 DrainState drain() override;
74
75 virtual void syscall(int64_t callnum, ThreadContext *tc, Fault *fault);
76 virtual RegVal getSyscallArg(ThreadContext *tc, int &i) = 0;
77 virtual RegVal getSyscallArg(ThreadContext *tc, int &i, int width);
78 virtual void setSyscallArg(ThreadContext *tc, int i, RegVal val) = 0;
79 virtual void setSyscallReturn(ThreadContext *tc,
80 SyscallReturn return_value) = 0;
81 virtual SyscallDesc *getDesc(int callnum) = 0;
82
83 inline uint64_t uid() { return _uid; }
84 inline uint64_t euid() { return _euid; }
85 inline uint64_t gid() { return _gid; }
86 inline uint64_t egid() { return _egid; }
87 inline uint64_t pid() { return _pid; }
88 inline uint64_t ppid() { return _ppid; }
89 inline uint64_t pgid() { return _pgid; }
90 inline uint64_t tgid() { return _tgid; }
91 inline void setpgid(uint64_t pgid) { _pgid = pgid; }
92
93 const char *progName() const { return executable.c_str(); }
94 std::string fullPath(const std::string &filename);
95 std::string getcwd() const { return cwd; }
96
97 /**
98 * Find an emulated device driver.
99 *
100 * @param filename Name of the device (under /dev)
101 * @return Pointer to driver object if found, else nullptr
102 */
103 EmulatedDriver *findDriver(std::string filename);
104
105 // This function acts as a callback to update the bias value in
106 // the object file because the parameters needed to calculate the
107 // bias are not available when the object file is created.
108 void updateBias();
109 Addr getBias();
110 Addr getStartPC();
111 ObjectFile *getInterpreter();
112
113 // override of virtual SimObject method: register statistics
114 void regStats() override;
115
116 void allocateMem(Addr vaddr, int64_t size, bool clobber = false);
117
118 /// Attempt to fix up a fault at vaddr by allocating a page on the stack.
119 /// @return Whether the fault has been fixed.
120 bool fixupStackFault(Addr vaddr);
121
122 // After getting registered with system object, tell process which
123 // system-wide context id it is assigned.
124 void
125 assignThreadContext(ContextID context_id)
126 {
127 contextIds.push_back(context_id);
128 }
129
130 // Find a free context to use
131 ThreadContext *findFreeContext();
132
133 /**
134 * After delegating a thread context to a child process
135 * no longer should relate to the ThreadContext
136 */
137 void revokeThreadContext(int context_id);
138
139 /**
140 * Does mmap region grow upward or downward from mmapEnd? Most
141 * platforms grow downward, but a few (such as Alpha) grow upward
142 * instead, so they can override this method to return false.
143 */
144 virtual bool mmapGrowsDown() const { return true; }
145
146 /**
147 * Maps a contiguous range of virtual addresses in this process's
148 * address space to a contiguous range of physical addresses.
149 * This function exists primarily to expose the map operation to
150 * python, so that configuration scripts can set up mappings in SE mode.
151 *
152 * @param vaddr The starting virtual address of the range.
153 * @param paddr The starting physical address of the range.
154 * @param size The length of the range in bytes.
155 * @param cacheable Specifies whether accesses are cacheable.
156 * @return True if the map operation was successful. (At this
157 * point in time, the map operation always succeeds.)
158 */
159 bool map(Addr vaddr, Addr paddr, int size, bool cacheable = true);
160
161 void replicatePage(Addr vaddr, Addr new_paddr, ThreadContext *old_tc,
162 ThreadContext *new_tc, bool alloc_page);
163
164 virtual void clone(ThreadContext *old_tc, ThreadContext *new_tc,
165 Process *new_p, RegVal flags);
166
167 // thread contexts associated with this process
168 std::vector<ContextID> contextIds;
169
170 // system object which owns this process
171 System *system;
172
173 Stats::Scalar numSyscalls; // track how many system calls are executed
174
175 // flag for using architecture specific page table
176 bool useArchPT;
177 // running KVM requires special initialization
178 bool kvmInSE;
179 // flag for using the process as a thread which shares page tables
180 bool useForClone;
181
182 EmulationPageTable *pTable;
183
184 SETranslatingPortProxy initVirtMem; // memory proxy for initial image load
185
186 ObjectFile *objFile;
187 std::vector<std::string> argv;
188 std::vector<std::string> envp;
189 std::string cwd;
190 std::string executable;
191
192 // Id of the owner of the process
193 uint64_t _uid;
194 uint64_t _euid;
195 uint64_t _gid;
196 uint64_t _egid;
197
198 // pid of the process and it's parent
199 uint64_t _pid;
200 uint64_t _ppid;
201 uint64_t _pgid;
202 uint64_t _tgid;
203
204 // Emulated drivers available to this process
205 std::vector<EmulatedDriver *> drivers;
206
207 std::shared_ptr<FDArray> fds;
208
209 bool *exitGroup;
210 std::shared_ptr<MemState> memState;
211
212 /**
213 * Calls a futex wakeup at the address specified by this pointer when
214 * this process exits.
215 */
216 uint64_t childClearTID;
217
218 // Process was forked with SIGCHLD set.
219 bool *sigchld;
220 };
221
222 #endif // __PROCESS_HH__