process.hh (11886:43b882cada33) process.hh (11894:3afbe563324c)
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/sim_object.hh"
51
52struct ProcessParams;
53
54class EmulatedDriver;
55class ObjectFile;
56class PageTableBase;
57class SyscallDesc;
58class SyscallReturn;
59class System;
60class ThreadContext;
61
62class Process : public SimObject
63{
64 public:
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/sim_object.hh"
51
52struct ProcessParams;
53
54class EmulatedDriver;
55class ObjectFile;
56class PageTableBase;
57class SyscallDesc;
58class SyscallReturn;
59class System;
60class ThreadContext;
61
62class Process : public SimObject
63{
64 public:
65 struct WaitRec
66 {
67 Addr waitChan;
68 ThreadContext *waitingContext;
69
65
70 WaitRec(Addr chan, ThreadContext *ctx)
71 : waitChan(chan), waitingContext(ctx)
72 { }
73 };
74
75 struct MemState
76 {
77 Addr brkPoint;
78 Addr stackBase;
79 unsigned stackSize;
80 Addr stackMin;
81 Addr nextThreadStackBase;
82 Addr mmapEnd;
83
84 MemState()
85 : brkPoint(0), stackBase(0), stackSize(0), stackMin(0),
86 nextThreadStackBase(0), mmapEnd(0)
87 { }
88
89 MemState&
90 operator=(const MemState &in)
91 {
92 if (this == &in)
93 return *this;
94
95 brkPoint = in.brkPoint;
96 stackBase = in.stackBase;
97 stackSize = in.stackSize;
98 stackMin = in.stackMin;
99 nextThreadStackBase = in.nextThreadStackBase;
100 mmapEnd = in.mmapEnd;
101 return *this;
102 }
103
104 void serialize(CheckpointOut &cp) const;
105 void unserialize(CheckpointIn &cp);
106 };
107
108 Process(ProcessParams *params, ObjectFile *obj_file);
109
110 void serialize(CheckpointOut &cp) const override;
111 void unserialize(CheckpointIn &cp) override;
112
113 void initState() override;
114 DrainState drain() override;
115
116 void syscall(int64_t callnum, ThreadContext *tc, Fault *fault);
117 virtual TheISA::IntReg getSyscallArg(ThreadContext *tc, int &i) = 0;
118 virtual TheISA::IntReg getSyscallArg(ThreadContext *tc, int &i, int width);
119 virtual void setSyscallArg(ThreadContext *tc, int i,
120 TheISA::IntReg val) = 0;
121 virtual void setSyscallReturn(ThreadContext *tc,
122 SyscallReturn return_value) = 0;
123 virtual SyscallDesc *getDesc(int callnum) = 0;
124
125 inline uint64_t uid() { return _uid; }
126 inline uint64_t euid() { return _euid; }
127 inline uint64_t gid() { return _gid; }
128 inline uint64_t egid() { return _egid; }
129 inline uint64_t pid() { return _pid; }
130 inline uint64_t ppid() { return _ppid; }
131 inline uint64_t pgid() { return _pgid; }
132 inline uint64_t tgid() { return _tgid; }
133 inline void setpgid(uint64_t pgid) { _pgid = pgid; }
134
135 const char *progName() const { return executable.c_str(); }
136 std::string fullPath(const std::string &filename);
137 std::string getcwd() const { return cwd; }
138
139 /**
140 * Find an emulated device driver.
141 *
142 * @param filename Name of the device (under /dev)
143 * @return Pointer to driver object if found, else NULL
144 */
145 EmulatedDriver *findDriver(std::string filename);
146
147 // This function acts as a callback to update the bias value in
148 // the object file because the parameters needed to calculate the
149 // bias are not available when the object file is created.
150 void updateBias();
151 Addr getBias();
152 Addr getStartPC();
153 ObjectFile *getInterpreter();
154
155 // override of virtual SimObject method: register statistics
156 void regStats() override;
157
158 void allocateMem(Addr vaddr, int64_t size, bool clobber = false);
159
160 /// Attempt to fix up a fault at vaddr by allocating a page on the stack.
161 /// @return Whether the fault has been fixed.
162 bool fixupStackFault(Addr vaddr);
163
164 // After getting registered with system object, tell process which
165 // system-wide context id it is assigned.
166 void
167 assignThreadContext(ContextID context_id)
168 {
169 contextIds.push_back(context_id);
170 }
171
172 // Find a free context to use
173 ThreadContext *findFreeContext();
174
175 /**
176 * After delegating a thread context to a child process
177 * no longer should relate to the ThreadContext
178 */
179 void revokeThreadContext(int context_id);
180
181 /**
182 * Does mmap region grow upward or downward from mmapEnd? Most
183 * platforms grow downward, but a few (such as Alpha) grow upward
184 * instead, so they can override this method to return false.
185 */
186 virtual bool mmapGrowsDown() const { return true; }
187
188 /**
189 * Maps a contiguous range of virtual addresses in this process's
190 * address space to a contiguous range of physical addresses.
191 * This function exists primarily to expose the map operation to
192 * python, so that configuration scripts can set up mappings in SE mode.
193 *
194 * @param vaddr The starting virtual address of the range.
195 * @param paddr The starting physical address of the range.
196 * @param size The length of the range in bytes.
197 * @param cacheable Specifies whether accesses are cacheable.
198 * @return True if the map operation was successful. (At this
199 * point in time, the map operation always succeeds.)
200 */
201 bool map(Addr vaddr, Addr paddr, int size, bool cacheable = true);
202
203 void replicatePage(Addr vaddr, Addr new_paddr, ThreadContext *old_tc,
204 ThreadContext *new_tc, bool alloc_page);
205
206 void clone(ThreadContext *old_tc, ThreadContext *new_tc, Process *new_p,
207 TheISA::IntReg flags);
208
66 struct MemState
67 {
68 Addr brkPoint;
69 Addr stackBase;
70 unsigned stackSize;
71 Addr stackMin;
72 Addr nextThreadStackBase;
73 Addr mmapEnd;
74
75 MemState()
76 : brkPoint(0), stackBase(0), stackSize(0), stackMin(0),
77 nextThreadStackBase(0), mmapEnd(0)
78 { }
79
80 MemState&
81 operator=(const MemState &in)
82 {
83 if (this == &in)
84 return *this;
85
86 brkPoint = in.brkPoint;
87 stackBase = in.stackBase;
88 stackSize = in.stackSize;
89 stackMin = in.stackMin;
90 nextThreadStackBase = in.nextThreadStackBase;
91 mmapEnd = in.mmapEnd;
92 return *this;
93 }
94
95 void serialize(CheckpointOut &cp) const;
96 void unserialize(CheckpointIn &cp);
97 };
98
99 Process(ProcessParams *params, ObjectFile *obj_file);
100
101 void serialize(CheckpointOut &cp) const override;
102 void unserialize(CheckpointIn &cp) override;
103
104 void initState() override;
105 DrainState drain() override;
106
107 void syscall(int64_t callnum, ThreadContext *tc, Fault *fault);
108 virtual TheISA::IntReg getSyscallArg(ThreadContext *tc, int &i) = 0;
109 virtual TheISA::IntReg getSyscallArg(ThreadContext *tc, int &i, int width);
110 virtual void setSyscallArg(ThreadContext *tc, int i,
111 TheISA::IntReg val) = 0;
112 virtual void setSyscallReturn(ThreadContext *tc,
113 SyscallReturn return_value) = 0;
114 virtual SyscallDesc *getDesc(int callnum) = 0;
115
116 inline uint64_t uid() { return _uid; }
117 inline uint64_t euid() { return _euid; }
118 inline uint64_t gid() { return _gid; }
119 inline uint64_t egid() { return _egid; }
120 inline uint64_t pid() { return _pid; }
121 inline uint64_t ppid() { return _ppid; }
122 inline uint64_t pgid() { return _pgid; }
123 inline uint64_t tgid() { return _tgid; }
124 inline void setpgid(uint64_t pgid) { _pgid = pgid; }
125
126 const char *progName() const { return executable.c_str(); }
127 std::string fullPath(const std::string &filename);
128 std::string getcwd() const { return cwd; }
129
130 /**
131 * Find an emulated device driver.
132 *
133 * @param filename Name of the device (under /dev)
134 * @return Pointer to driver object if found, else NULL
135 */
136 EmulatedDriver *findDriver(std::string filename);
137
138 // This function acts as a callback to update the bias value in
139 // the object file because the parameters needed to calculate the
140 // bias are not available when the object file is created.
141 void updateBias();
142 Addr getBias();
143 Addr getStartPC();
144 ObjectFile *getInterpreter();
145
146 // override of virtual SimObject method: register statistics
147 void regStats() override;
148
149 void allocateMem(Addr vaddr, int64_t size, bool clobber = false);
150
151 /// Attempt to fix up a fault at vaddr by allocating a page on the stack.
152 /// @return Whether the fault has been fixed.
153 bool fixupStackFault(Addr vaddr);
154
155 // After getting registered with system object, tell process which
156 // system-wide context id it is assigned.
157 void
158 assignThreadContext(ContextID context_id)
159 {
160 contextIds.push_back(context_id);
161 }
162
163 // Find a free context to use
164 ThreadContext *findFreeContext();
165
166 /**
167 * After delegating a thread context to a child process
168 * no longer should relate to the ThreadContext
169 */
170 void revokeThreadContext(int context_id);
171
172 /**
173 * Does mmap region grow upward or downward from mmapEnd? Most
174 * platforms grow downward, but a few (such as Alpha) grow upward
175 * instead, so they can override this method to return false.
176 */
177 virtual bool mmapGrowsDown() const { return true; }
178
179 /**
180 * Maps a contiguous range of virtual addresses in this process's
181 * address space to a contiguous range of physical addresses.
182 * This function exists primarily to expose the map operation to
183 * python, so that configuration scripts can set up mappings in SE mode.
184 *
185 * @param vaddr The starting virtual address of the range.
186 * @param paddr The starting physical address of the range.
187 * @param size The length of the range in bytes.
188 * @param cacheable Specifies whether accesses are cacheable.
189 * @return True if the map operation was successful. (At this
190 * point in time, the map operation always succeeds.)
191 */
192 bool map(Addr vaddr, Addr paddr, int size, bool cacheable = true);
193
194 void replicatePage(Addr vaddr, Addr new_paddr, ThreadContext *old_tc,
195 ThreadContext *new_tc, bool alloc_page);
196
197 void clone(ThreadContext *old_tc, ThreadContext *new_tc, Process *new_p,
198 TheISA::IntReg flags);
199
209 // list of all blocked contexts
210 std::list<WaitRec> waitList;
211
212 // thread contexts associated with this process
213 std::vector<ContextID> contextIds;
214
215 // system object which owns this process
216 System *system;
217
218 Stats::Scalar numSyscalls; // track how many system calls are executed
219
220 bool useArchPT; // flag for using architecture specific page table
221 bool kvmInSE; // running KVM requires special initialization
222
223 PageTableBase* pTable;
224
225 SETranslatingPortProxy initVirtMem; // memory proxy for initial image load
226
227 ObjectFile *objFile;
228 std::vector<std::string> argv;
229 std::vector<std::string> envp;
230 std::string cwd;
231 std::string executable;
232
233 // Id of the owner of the process
234 uint64_t _uid;
235 uint64_t _euid;
236 uint64_t _gid;
237 uint64_t _egid;
238
239 // pid of the process and it's parent
240 uint64_t _pid;
241 uint64_t _ppid;
242 uint64_t _pgid;
243 uint64_t _tgid;
244
245 // Emulated drivers available to this process
246 std::vector<EmulatedDriver *> drivers;
247
248 std::shared_ptr<FDArray> fds;
249
250 bool *exitGroup;
251
252 Addr maxStackSize;
253 MemState *memState;
254
255 /**
256 * Calls a futex wakeup at the address specified by this pointer when
257 * this process exits.
258 */
259 uint64_t childClearTID;
260
261 // Process was forked with SIGCHLD set.
262 bool *sigchld;
263};
264
265#endif // __PROCESS_HH__
200 // thread contexts associated with this process
201 std::vector<ContextID> contextIds;
202
203 // system object which owns this process
204 System *system;
205
206 Stats::Scalar numSyscalls; // track how many system calls are executed
207
208 bool useArchPT; // flag for using architecture specific page table
209 bool kvmInSE; // running KVM requires special initialization
210
211 PageTableBase* pTable;
212
213 SETranslatingPortProxy initVirtMem; // memory proxy for initial image load
214
215 ObjectFile *objFile;
216 std::vector<std::string> argv;
217 std::vector<std::string> envp;
218 std::string cwd;
219 std::string executable;
220
221 // Id of the owner of the process
222 uint64_t _uid;
223 uint64_t _euid;
224 uint64_t _gid;
225 uint64_t _egid;
226
227 // pid of the process and it's parent
228 uint64_t _pid;
229 uint64_t _ppid;
230 uint64_t _pgid;
231 uint64_t _tgid;
232
233 // Emulated drivers available to this process
234 std::vector<EmulatedDriver *> drivers;
235
236 std::shared_ptr<FDArray> fds;
237
238 bool *exitGroup;
239
240 Addr maxStackSize;
241 MemState *memState;
242
243 /**
244 * Calls a futex wakeup at the address specified by this pointer when
245 * this process exits.
246 */
247 uint64_t childClearTID;
248
249 // Process was forked with SIGCHLD set.
250 bool *sigchld;
251};
252
253#endif // __PROCESS_HH__