Serialization: Fix serialization of file descriptors. Make sure open
[gem5.git] / src / sim / process.hh
1 /*
2 * Copyright (c) 2001-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Nathan Binkert
29 * Steve Reinhardt
30 */
31
32 #ifndef __PROCESS_HH__
33 #define __PROCESS_HH__
34
35 //
36 // The purpose of this code is to fake the loader & syscall mechanism
37 // when there's no OS: thus there's no reason to use it in FULL_SYSTEM
38 // mode when we do have an OS.
39 //
40 #include "config/full_system.hh"
41
42 #if !FULL_SYSTEM
43
44 #include <string>
45 #include <vector>
46
47 #include "base/statistics.hh"
48 #include "sim/host.hh"
49 #include "sim/sim_object.hh"
50
51 class GDBListener;
52 class PageTable;
53 class ProcessParams;
54 class LiveProcessParams;
55 class SyscallDesc;
56 class System;
57 class ThreadContext;
58 class TranslatingPort;
59 namespace TheISA
60 {
61 class RemoteGDB;
62 }
63
64 class Process : public SimObject
65 {
66 public:
67
68 /// Pointer to object representing the system this process is
69 /// running on.
70 System *system;
71
72 // have we initialized a thread context from this process? If
73 // yes, subsequent contexts are assumed to be for dynamically
74 // created threads and are not initialized.
75 bool initialContextLoaded;
76
77 bool checkpointRestored;
78
79 // thread contexts associated with this process
80 std::vector<ThreadContext *> threadContexts;
81
82 // remote gdb objects
83 std::vector<TheISA::RemoteGDB *> remoteGDB;
84 std::vector<GDBListener *> gdbListen;
85 bool breakpoint();
86
87 // number of CPUs (esxec contexts, really) assigned to this process.
88 unsigned int numCpus() { return threadContexts.size(); }
89
90 // record of blocked context
91 struct WaitRec
92 {
93 Addr waitChan;
94 ThreadContext *waitingContext;
95
96 WaitRec(Addr chan, ThreadContext *ctx)
97 : waitChan(chan), waitingContext(ctx)
98 { }
99 };
100
101 // list of all blocked contexts
102 std::list<WaitRec> waitList;
103
104 Addr brk_point; // top of the data segment
105
106 Addr stack_base; // stack segment base (highest address)
107 unsigned stack_size; // initial stack size
108 Addr stack_min; // lowest address accessed on the stack
109
110 // The maximum size allowed for the stack.
111 Addr max_stack_size;
112
113 // addr to use for next stack region (for multithreaded apps)
114 Addr next_thread_stack_base;
115
116 // Base of region for mmaps (when user doesn't specify an address).
117 Addr mmap_start;
118 Addr mmap_end;
119
120 // Base of region for nxm data
121 Addr nxm_start;
122 Addr nxm_end;
123
124 std::string prog_fname; // file name
125
126 Stats::Scalar<> num_syscalls; // number of syscalls executed
127
128
129 protected:
130 // constructor
131 Process(ProcessParams * params);
132
133 // post initialization startup
134 virtual void startup();
135
136 protected:
137 /// Memory object for initialization (image loading)
138 TranslatingPort *initVirtMem;
139
140 public:
141 PageTable *pTable;
142
143 //This id is assigned by m5 and is used to keep process' tlb entries
144 //separated.
145 uint64_t M5_pid;
146
147 class FdMap
148 {
149 public:
150 int fd;
151 std::string filename;
152 int mode;
153 int flags;
154 bool isPipe;
155 int readPipeSource;
156 uint64_t fileOffset;
157
158
159 FdMap()
160 {
161 fd = -1;
162 filename = "NULL";
163 mode = 0;
164 flags = 0;
165 isPipe = false;
166 readPipeSource = 0;
167 fileOffset = 0;
168
169 }
170
171 void serialize(std::ostream &os);
172 void unserialize(Checkpoint *cp, const std::string &section);
173
174 };
175
176 private:
177 // file descriptor remapping support
178 static const int MAX_FD = 256; // max legal fd value
179 FdMap fd_map[MAX_FD+1];
180
181
182 public:
183 // static helper functions to generate file descriptors for constructor
184 static int openInputFile(const std::string &filename);
185 static int openOutputFile(const std::string &filename);
186
187 // override of virtual SimObject method: register statistics
188 virtual void regStats();
189
190 // register a thread context for this process.
191 // returns tc's cpu number (index into threadContexts[])
192 int registerThreadContext(ThreadContext *tc);
193
194
195 void replaceThreadContext(ThreadContext *tc, int tcIndex);
196
197 // map simulator fd sim_fd to target fd tgt_fd
198 void dup_fd(int sim_fd, int tgt_fd);
199
200 // generate new target fd for sim_fd
201 int alloc_fd(int sim_fd, std::string filename, int flags, int mode, bool pipe);
202
203 // free target fd (e.g., after close)
204 void free_fd(int tgt_fd);
205
206 // look up simulator fd for given target fd
207 int sim_fd(int tgt_fd);
208
209 // look up simulator fd_map object for a given target fd
210 FdMap * sim_fd_obj(int tgt_fd);
211
212 // fix all offsets for currently open files and save them
213 void fix_file_offsets();
214
215 // find all offsets for currently open files and save them
216 void find_file_offsets();
217
218 // set the source of this read pipe for a checkpoint resume
219 void setReadPipeSource(int read_pipe_fd, int source_fd);
220
221 virtual void syscall(int64_t callnum, ThreadContext *tc) = 0;
222
223 // check if the this addr is on the next available page and allocate it
224 // if it's not we'll panic
225 bool checkAndAllocNextPage(Addr vaddr);
226
227 void serialize(std::ostream &os);
228 void unserialize(Checkpoint *cp, const std::string &section);
229 };
230
231 //
232 // "Live" process with system calls redirected to host system
233 //
234 class ObjectFile;
235 class LiveProcess : public Process
236 {
237 protected:
238 ObjectFile *objFile;
239 std::vector<std::string> argv;
240 std::vector<std::string> envp;
241 std::string cwd;
242
243 LiveProcess(LiveProcessParams * params, ObjectFile *objFile);
244
245 virtual void argsInit(int intSize, int pageSize);
246
247 // Id of the owner of the process
248 uint64_t __uid;
249 uint64_t __euid;
250 uint64_t __gid;
251 uint64_t __egid;
252
253 // pid of the process and it's parent
254 uint64_t __pid;
255 uint64_t __ppid;
256
257 public:
258
259 enum AuxiliaryVectorType {
260 M5_AT_NULL = 0,
261 M5_AT_IGNORE = 1,
262 M5_AT_EXECFD = 2,
263 M5_AT_PHDR = 3,
264 M5_AT_PHENT = 4,
265 M5_AT_PHNUM = 5,
266 M5_AT_PAGESZ = 6,
267 M5_AT_BASE = 7,
268 M5_AT_FLAGS = 8,
269 M5_AT_ENTRY = 9,
270 M5_AT_NOTELF = 10,
271 M5_AT_UID = 11,
272 M5_AT_EUID = 12,
273 M5_AT_GID = 13,
274 M5_AT_EGID = 14,
275 // The following may be specific to Linux
276 M5_AT_PLATFORM = 15,
277 M5_AT_HWCAP = 16,
278 M5_AT_CLKTCK = 17,
279
280 M5_AT_SECURE = 23,
281
282 M5_AT_VECTOR_SIZE = 44
283 };
284
285 inline uint64_t uid() {return __uid;}
286 inline uint64_t euid() {return __euid;}
287 inline uint64_t gid() {return __gid;}
288 inline uint64_t egid() {return __egid;}
289 inline uint64_t pid() {return __pid;}
290 inline uint64_t ppid() {return __ppid;}
291
292 std::string
293 fullPath(const std::string &filename)
294 {
295 if (filename[0] == '/' || cwd.empty())
296 return filename;
297
298 std::string full = cwd;
299
300 if (cwd[cwd.size() - 1] != '/')
301 full += '/';
302
303 return full + filename;
304 }
305
306 virtual void syscall(int64_t callnum, ThreadContext *tc);
307
308 virtual SyscallDesc* getDesc(int callnum) = 0;
309
310 // this function is used to create the LiveProcess object, since
311 // we can't tell which subclass of LiveProcess to use until we
312 // open and look at the object file.
313 static LiveProcess *create(LiveProcessParams * params);
314 };
315
316
317 #endif // !FULL_SYSTEM
318
319 #endif // __PROCESS_HH__