process.cc (10932:cafae9abd4e4) process.cc (11140:cf07f8bf58db)
1/*
2 * Copyright (c) 2014 Advanced Micro Devices, Inc.
3 * Copyright (c) 2012 ARM Limited
4 * All rights reserved
5 *
6 * The license below extends only to copyright in the software and shall
7 * not be construed as granting a license to any other intellectual
8 * property including but not limited to intellectual property relating
9 * to a hardware implementation of the functionality of the software
10 * licensed hereunder. You may use the software subject to the license
11 * terms below provided that you ensure that this notice is replicated
12 * unmodified and in its entirety in all distributions of the software,
13 * modified or unmodified, in source code or in binary form.
14 *
15 * Copyright (c) 2001-2005 The Regents of The University of Michigan
16 * All rights reserved.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions are
20 * met: redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer;
22 * redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution;
25 * neither the name of the copyright holders nor the names of its
26 * contributors may be used to endorse or promote products derived from
27 * this software without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Authors: Nathan Binkert
42 * Steve Reinhardt
43 * Ali Saidi
44 */
45
46#include <fcntl.h>
47#include <unistd.h>
48
49#include <cstdio>
50#include <map>
51#include <string>
52
53#include "base/loader/object_file.hh"
54#include "base/loader/symtab.hh"
55#include "base/intmath.hh"
56#include "base/statistics.hh"
57#include "config/the_isa.hh"
58#include "cpu/thread_context.hh"
59#include "mem/page_table.hh"
60#include "mem/multi_level_page_table.hh"
61#include "mem/se_translating_port_proxy.hh"
62#include "params/LiveProcess.hh"
63#include "params/Process.hh"
64#include "sim/debug.hh"
65#include "sim/process.hh"
66#include "sim/process_impl.hh"
67#include "sim/stats.hh"
68#include "sim/syscall_emul.hh"
69#include "sim/system.hh"
70
71#if THE_ISA == ALPHA_ISA
72#include "arch/alpha/linux/process.hh"
73#include "arch/alpha/tru64/process.hh"
74#elif THE_ISA == SPARC_ISA
75#include "arch/sparc/linux/process.hh"
76#include "arch/sparc/solaris/process.hh"
77#elif THE_ISA == MIPS_ISA
78#include "arch/mips/linux/process.hh"
79#elif THE_ISA == ARM_ISA
80#include "arch/arm/linux/process.hh"
81#include "arch/arm/freebsd/process.hh"
82#elif THE_ISA == X86_ISA
83#include "arch/x86/linux/process.hh"
84#elif THE_ISA == POWER_ISA
85#include "arch/power/linux/process.hh"
86#else
87#error "THE_ISA not set"
88#endif
89
90
91using namespace std;
92using namespace TheISA;
93
94// current number of allocated processes
95int num_processes = 0;
96
97template<class IntType>
98
99AuxVector<IntType>::AuxVector(IntType type, IntType val)
100{
101 a_type = TheISA::htog(type);
102 a_val = TheISA::htog(val);
103}
104
105template struct AuxVector<uint32_t>;
106template struct AuxVector<uint64_t>;
107
108static int
109openFile(const string& filename, int flags, mode_t mode)
110{
111 int sim_fd = open(filename.c_str(), flags, mode);
112 if (sim_fd != -1)
113 return sim_fd;
114 fatal("Unable to open %s with mode %O", filename, mode);
115}
116
117static int
118openInputFile(const string &filename)
119{
120 return openFile(filename, O_RDONLY, 0);
121}
122
123static int
124openOutputFile(const string &filename)
125{
126 return openFile(filename, O_WRONLY | O_CREAT | O_TRUNC, 0664);
127}
128
129Process::Process(ProcessParams * params)
130 : SimObject(params), system(params->system),
131 brk_point(0), stack_base(0), stack_size(0), stack_min(0),
132 max_stack_size(params->max_stack_size),
133 next_thread_stack_base(0),
134 M5_pid(system->allocatePID()),
135 useArchPT(params->useArchPT),
136 kvmInSE(params->kvmInSE),
137 pTable(useArchPT ?
138 static_cast<PageTableBase *>(new ArchPageTable(name(), M5_pid, system)) :
139 static_cast<PageTableBase *>(new FuncPageTable(name(), M5_pid)) ),
140 initVirtMem(system->getSystemPort(), this,
141 SETranslatingPortProxy::Always),
142 fd_array(make_shared<array<FDEntry, NUM_FDS>>()),
143 imap {{"", -1},
144 {"cin", STDIN_FILENO},
145 {"stdin", STDIN_FILENO}},
146 oemap{{"", -1},
147 {"cout", STDOUT_FILENO},
148 {"stdout", STDOUT_FILENO},
149 {"cerr", STDERR_FILENO},
150 {"stderr", STDERR_FILENO}}
151{
152 int sim_fd;
153 std::map<string,int>::iterator it;
154
155 // Search through the input options and set fd if match is found;
156 // otherwise, open an input file and seek to location.
157 FDEntry *fde_stdin = getFDEntry(STDIN_FILENO);
158 if ((it = imap.find(params->input)) != imap.end())
159 sim_fd = it->second;
160 else
161 sim_fd = openInputFile(params->input);
162 fde_stdin->set(sim_fd, params->input, O_RDONLY, -1, false);
163
164 // Search through the output/error options and set fd if match is found;
165 // otherwise, open an output file and seek to location.
166 FDEntry *fde_stdout = getFDEntry(STDOUT_FILENO);
167 if ((it = oemap.find(params->output)) != oemap.end())
168 sim_fd = it->second;
169 else
170 sim_fd = openOutputFile(params->output);
171 fde_stdout->set(sim_fd, params->output, O_WRONLY | O_CREAT | O_TRUNC,
172 0664, false);
173
174 FDEntry *fde_stderr = getFDEntry(STDERR_FILENO);
175 if (params->output == params->errout)
176 // Reuse the same file descriptor if these match.
177 sim_fd = fde_stdout->fd;
178 else if ((it = oemap.find(params->errout)) != oemap.end())
179 sim_fd = it->second;
180 else
181 sim_fd = openOutputFile(params->errout);
182 fde_stderr->set(sim_fd, params->errout, O_WRONLY | O_CREAT | O_TRUNC,
183 0664, false);
184
185 mmap_start = mmap_end = 0;
186 nxm_start = nxm_end = 0;
187 // other parameters will be initialized when the program is loaded
188}
189
190
191void
192Process::regStats()
193{
194 using namespace Stats;
195
196 num_syscalls
197 .name(name() + ".num_syscalls")
198 .desc("Number of system calls")
199 ;
200}
201
202void
203Process::inheritFDArray(Process *p)
204{
205 fd_array = p->fd_array;
206}
207
208ThreadContext *
209Process::findFreeContext()
210{
211 for (int id : contextIds) {
212 ThreadContext *tc = system->getThreadContext(id);
213 if (tc->status() == ThreadContext::Halted)
214 return tc;
215 }
216 return NULL;
217}
218
219void
220Process::initState()
221{
222 if (contextIds.empty())
223 fatal("Process %s is not associated with any HW contexts!\n", name());
224
225 // first thread context for this process... initialize & enable
226 ThreadContext *tc = system->getThreadContext(contextIds[0]);
227
228 // mark this context as active so it will start ticking.
229 tc->activate();
230
231 pTable->initState(tc);
232}
233
234DrainState
235Process::drain()
236{
237 findFileOffsets();
238 return DrainState::Drained;
239}
240
241int
242Process::allocFD(int sim_fd, const string& filename, int flags, int mode,
243 bool pipe)
244{
245 if (sim_fd == -1)
246 return -1;
247
248 for (int free_fd = 0; free_fd < fd_array->size(); free_fd++) {
249 FDEntry *fde = getFDEntry(free_fd);
250 if (fde->isFree()) {
251 fde->set(sim_fd, filename, flags, mode, pipe);
252 return free_fd;
253 }
254 }
255
256 fatal("Out of target file descriptors");
257}
258
259void
260Process::resetFDEntry(int tgt_fd)
261{
262 FDEntry *fde = getFDEntry(tgt_fd);
263 assert(fde->fd > -1);
264
265 fde->reset();
266}
267
268int
269Process::getSimFD(int tgt_fd)
270{
271 FDEntry *entry = getFDEntry(tgt_fd);
272 return entry ? entry->fd : -1;
273}
274
275FDEntry *
276Process::getFDEntry(int tgt_fd)
277{
278 assert(0 <= tgt_fd && tgt_fd < fd_array->size());
279 return &(*fd_array)[tgt_fd];
280}
281
282int
283Process::getTgtFD(int sim_fd)
284{
285 for (int index = 0; index < fd_array->size(); index++)
286 if ((*fd_array)[index].fd == sim_fd)
287 return index;
288 return -1;
289}
290
291void
292Process::allocateMem(Addr vaddr, int64_t size, bool clobber)
293{
294 int npages = divCeil(size, (int64_t)PageBytes);
295 Addr paddr = system->allocPhysPages(npages);
296 pTable->map(vaddr, paddr, size, clobber ? PageTableBase::Clobber : 0);
297}
298
299bool
300Process::fixupStackFault(Addr vaddr)
301{
302 // Check if this is already on the stack and there's just no page there
303 // yet.
304 if (vaddr >= stack_min && vaddr < stack_base) {
305 allocateMem(roundDown(vaddr, PageBytes), PageBytes);
306 return true;
307 }
308
309 // We've accessed the next page of the stack, so extend it to include
310 // this address.
311 if (vaddr < stack_min && vaddr >= stack_base - max_stack_size) {
312 while (vaddr < stack_min) {
313 stack_min -= TheISA::PageBytes;
314 if (stack_base - stack_min > max_stack_size)
315 fatal("Maximum stack size exceeded\n");
316 allocateMem(stack_min, TheISA::PageBytes);
317 inform("Increasing stack size by one page.");
318 };
319 return true;
320 }
321 return false;
322}
323
324void
325Process::fixFileOffsets()
326{
327 auto seek = [] (FDEntry *fde)
328 {
329 if (lseek(fde->fd, fde->fileOffset, SEEK_SET) < 0)
330 fatal("Unable to see to location in %s", fde->filename);
331 };
332
333 std::map<string,int>::iterator it;
334
335 // Search through the input options and set fd if match is found;
336 // otherwise, open an input file and seek to location.
337 FDEntry *fde_stdin = getFDEntry(STDIN_FILENO);
338 if ((it = imap.find(fde_stdin->filename)) != imap.end()) {
339 fde_stdin->fd = it->second;
340 } else {
341 fde_stdin->fd = openInputFile(fde_stdin->filename);
342 seek(fde_stdin);
343 }
344
345 // Search through the output/error options and set fd if match is found;
346 // otherwise, open an output file and seek to location.
347 FDEntry *fde_stdout = getFDEntry(STDOUT_FILENO);
348 if ((it = oemap.find(fde_stdout->filename)) != oemap.end()) {
349 fde_stdout->fd = it->second;
350 } else {
351 fde_stdout->fd = openOutputFile(fde_stdout->filename);
352 seek(fde_stdout);
353 }
354
355 FDEntry *fde_stderr = getFDEntry(STDERR_FILENO);
356 if (fde_stdout->filename == fde_stderr->filename) {
357 // Reuse the same file descriptor if these match.
358 fde_stderr->fd = fde_stdout->fd;
359 } else if ((it = oemap.find(fde_stderr->filename)) != oemap.end()) {
360 fde_stderr->fd = it->second;
361 } else {
362 fde_stderr->fd = openOutputFile(fde_stderr->filename);
363 seek(fde_stderr);
364 }
365
366 for (int tgt_fd = 3; tgt_fd < fd_array->size(); tgt_fd++) {
367 FDEntry *fde = getFDEntry(tgt_fd);
368 if (fde->fd == -1)
369 continue;
370
371 if (fde->isPipe) {
372 if (fde->filename == "PIPE-WRITE")
373 continue;
374 assert(fde->filename == "PIPE-READ");
375
376 int fds[2];
377 if (pipe(fds) < 0)
378 fatal("Unable to create new pipe");
379
380 fde->fd = fds[0];
381
382 FDEntry *fde_write = getFDEntry(fde->readPipeSource);
383 assert(
384 fde_write->filename == "PIPE-WRITE");
385 fde_write->fd = fds[1];
386 } else {
387 fde->fd = openFile(fde->filename.c_str(), fde->flags, fde->mode);
388 seek(fde);
389 }
390 }
391}
392
393void
394Process::findFileOffsets()
395{
396 for (auto& fde : *fd_array) {
397 if (fde.fd != -1)
398 fde.fileOffset = lseek(fde.fd, 0, SEEK_CUR);
399 }
400}
401
402void
403Process::setReadPipeSource(int read_pipe_fd, int source_fd)
404{
405 FDEntry *fde = getFDEntry(read_pipe_fd);
406 assert(source_fd >= -1);
407 fde->readPipeSource = source_fd;
408}
409
410void
411Process::serialize(CheckpointOut &cp) const
412{
413 SERIALIZE_SCALAR(brk_point);
414 SERIALIZE_SCALAR(stack_base);
415 SERIALIZE_SCALAR(stack_size);
416 SERIALIZE_SCALAR(stack_min);
417 SERIALIZE_SCALAR(next_thread_stack_base);
418 SERIALIZE_SCALAR(mmap_start);
419 SERIALIZE_SCALAR(mmap_end);
420 SERIALIZE_SCALAR(nxm_start);
421 SERIALIZE_SCALAR(nxm_end);
422 pTable->serialize(cp);
423 for (int x = 0; x < fd_array->size(); x++) {
424 (*fd_array)[x].serializeSection(cp, csprintf("FDEntry%d", x));
425 }
426 SERIALIZE_SCALAR(M5_pid);
427
428}
429
430void
431Process::unserialize(CheckpointIn &cp)
432{
433 UNSERIALIZE_SCALAR(brk_point);
434 UNSERIALIZE_SCALAR(stack_base);
435 UNSERIALIZE_SCALAR(stack_size);
436 UNSERIALIZE_SCALAR(stack_min);
437 UNSERIALIZE_SCALAR(next_thread_stack_base);
438 UNSERIALIZE_SCALAR(mmap_start);
439 UNSERIALIZE_SCALAR(mmap_end);
440 UNSERIALIZE_SCALAR(nxm_start);
441 UNSERIALIZE_SCALAR(nxm_end);
442 pTable->unserialize(cp);
443 for (int x = 0; x < fd_array->size(); x++) {
444 FDEntry *fde = getFDEntry(x);
445 fde->unserializeSection(cp, csprintf("FDEntry%d", x));
446 }
447 fixFileOffsets();
448 UNSERIALIZE_OPT_SCALAR(M5_pid);
449 // The above returns a bool so that you could do something if you don't
450 // find the param in the checkpoint if you wanted to, like set a default
451 // but in this case we'll just stick with the instantiated value if not
452 // found.
453}
454
455
456bool
457Process::map(Addr vaddr, Addr paddr, int size, bool cacheable)
458{
459 pTable->map(vaddr, paddr, size,
460 cacheable ? 0 : PageTableBase::Uncacheable);
461 return true;
462}
463
464
465////////////////////////////////////////////////////////////////////////
466//
467// LiveProcess member definitions
468//
469////////////////////////////////////////////////////////////////////////
470
471
472LiveProcess::LiveProcess(LiveProcessParams *params, ObjectFile *_objFile)
473 : Process(params), objFile(_objFile),
474 argv(params->cmd), envp(params->env), cwd(params->cwd),
1/*
2 * Copyright (c) 2014 Advanced Micro Devices, Inc.
3 * Copyright (c) 2012 ARM Limited
4 * All rights reserved
5 *
6 * The license below extends only to copyright in the software and shall
7 * not be construed as granting a license to any other intellectual
8 * property including but not limited to intellectual property relating
9 * to a hardware implementation of the functionality of the software
10 * licensed hereunder. You may use the software subject to the license
11 * terms below provided that you ensure that this notice is replicated
12 * unmodified and in its entirety in all distributions of the software,
13 * modified or unmodified, in source code or in binary form.
14 *
15 * Copyright (c) 2001-2005 The Regents of The University of Michigan
16 * All rights reserved.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions are
20 * met: redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer;
22 * redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution;
25 * neither the name of the copyright holders nor the names of its
26 * contributors may be used to endorse or promote products derived from
27 * this software without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Authors: Nathan Binkert
42 * Steve Reinhardt
43 * Ali Saidi
44 */
45
46#include <fcntl.h>
47#include <unistd.h>
48
49#include <cstdio>
50#include <map>
51#include <string>
52
53#include "base/loader/object_file.hh"
54#include "base/loader/symtab.hh"
55#include "base/intmath.hh"
56#include "base/statistics.hh"
57#include "config/the_isa.hh"
58#include "cpu/thread_context.hh"
59#include "mem/page_table.hh"
60#include "mem/multi_level_page_table.hh"
61#include "mem/se_translating_port_proxy.hh"
62#include "params/LiveProcess.hh"
63#include "params/Process.hh"
64#include "sim/debug.hh"
65#include "sim/process.hh"
66#include "sim/process_impl.hh"
67#include "sim/stats.hh"
68#include "sim/syscall_emul.hh"
69#include "sim/system.hh"
70
71#if THE_ISA == ALPHA_ISA
72#include "arch/alpha/linux/process.hh"
73#include "arch/alpha/tru64/process.hh"
74#elif THE_ISA == SPARC_ISA
75#include "arch/sparc/linux/process.hh"
76#include "arch/sparc/solaris/process.hh"
77#elif THE_ISA == MIPS_ISA
78#include "arch/mips/linux/process.hh"
79#elif THE_ISA == ARM_ISA
80#include "arch/arm/linux/process.hh"
81#include "arch/arm/freebsd/process.hh"
82#elif THE_ISA == X86_ISA
83#include "arch/x86/linux/process.hh"
84#elif THE_ISA == POWER_ISA
85#include "arch/power/linux/process.hh"
86#else
87#error "THE_ISA not set"
88#endif
89
90
91using namespace std;
92using namespace TheISA;
93
94// current number of allocated processes
95int num_processes = 0;
96
97template<class IntType>
98
99AuxVector<IntType>::AuxVector(IntType type, IntType val)
100{
101 a_type = TheISA::htog(type);
102 a_val = TheISA::htog(val);
103}
104
105template struct AuxVector<uint32_t>;
106template struct AuxVector<uint64_t>;
107
108static int
109openFile(const string& filename, int flags, mode_t mode)
110{
111 int sim_fd = open(filename.c_str(), flags, mode);
112 if (sim_fd != -1)
113 return sim_fd;
114 fatal("Unable to open %s with mode %O", filename, mode);
115}
116
117static int
118openInputFile(const string &filename)
119{
120 return openFile(filename, O_RDONLY, 0);
121}
122
123static int
124openOutputFile(const string &filename)
125{
126 return openFile(filename, O_WRONLY | O_CREAT | O_TRUNC, 0664);
127}
128
129Process::Process(ProcessParams * params)
130 : SimObject(params), system(params->system),
131 brk_point(0), stack_base(0), stack_size(0), stack_min(0),
132 max_stack_size(params->max_stack_size),
133 next_thread_stack_base(0),
134 M5_pid(system->allocatePID()),
135 useArchPT(params->useArchPT),
136 kvmInSE(params->kvmInSE),
137 pTable(useArchPT ?
138 static_cast<PageTableBase *>(new ArchPageTable(name(), M5_pid, system)) :
139 static_cast<PageTableBase *>(new FuncPageTable(name(), M5_pid)) ),
140 initVirtMem(system->getSystemPort(), this,
141 SETranslatingPortProxy::Always),
142 fd_array(make_shared<array<FDEntry, NUM_FDS>>()),
143 imap {{"", -1},
144 {"cin", STDIN_FILENO},
145 {"stdin", STDIN_FILENO}},
146 oemap{{"", -1},
147 {"cout", STDOUT_FILENO},
148 {"stdout", STDOUT_FILENO},
149 {"cerr", STDERR_FILENO},
150 {"stderr", STDERR_FILENO}}
151{
152 int sim_fd;
153 std::map<string,int>::iterator it;
154
155 // Search through the input options and set fd if match is found;
156 // otherwise, open an input file and seek to location.
157 FDEntry *fde_stdin = getFDEntry(STDIN_FILENO);
158 if ((it = imap.find(params->input)) != imap.end())
159 sim_fd = it->second;
160 else
161 sim_fd = openInputFile(params->input);
162 fde_stdin->set(sim_fd, params->input, O_RDONLY, -1, false);
163
164 // Search through the output/error options and set fd if match is found;
165 // otherwise, open an output file and seek to location.
166 FDEntry *fde_stdout = getFDEntry(STDOUT_FILENO);
167 if ((it = oemap.find(params->output)) != oemap.end())
168 sim_fd = it->second;
169 else
170 sim_fd = openOutputFile(params->output);
171 fde_stdout->set(sim_fd, params->output, O_WRONLY | O_CREAT | O_TRUNC,
172 0664, false);
173
174 FDEntry *fde_stderr = getFDEntry(STDERR_FILENO);
175 if (params->output == params->errout)
176 // Reuse the same file descriptor if these match.
177 sim_fd = fde_stdout->fd;
178 else if ((it = oemap.find(params->errout)) != oemap.end())
179 sim_fd = it->second;
180 else
181 sim_fd = openOutputFile(params->errout);
182 fde_stderr->set(sim_fd, params->errout, O_WRONLY | O_CREAT | O_TRUNC,
183 0664, false);
184
185 mmap_start = mmap_end = 0;
186 nxm_start = nxm_end = 0;
187 // other parameters will be initialized when the program is loaded
188}
189
190
191void
192Process::regStats()
193{
194 using namespace Stats;
195
196 num_syscalls
197 .name(name() + ".num_syscalls")
198 .desc("Number of system calls")
199 ;
200}
201
202void
203Process::inheritFDArray(Process *p)
204{
205 fd_array = p->fd_array;
206}
207
208ThreadContext *
209Process::findFreeContext()
210{
211 for (int id : contextIds) {
212 ThreadContext *tc = system->getThreadContext(id);
213 if (tc->status() == ThreadContext::Halted)
214 return tc;
215 }
216 return NULL;
217}
218
219void
220Process::initState()
221{
222 if (contextIds.empty())
223 fatal("Process %s is not associated with any HW contexts!\n", name());
224
225 // first thread context for this process... initialize & enable
226 ThreadContext *tc = system->getThreadContext(contextIds[0]);
227
228 // mark this context as active so it will start ticking.
229 tc->activate();
230
231 pTable->initState(tc);
232}
233
234DrainState
235Process::drain()
236{
237 findFileOffsets();
238 return DrainState::Drained;
239}
240
241int
242Process::allocFD(int sim_fd, const string& filename, int flags, int mode,
243 bool pipe)
244{
245 if (sim_fd == -1)
246 return -1;
247
248 for (int free_fd = 0; free_fd < fd_array->size(); free_fd++) {
249 FDEntry *fde = getFDEntry(free_fd);
250 if (fde->isFree()) {
251 fde->set(sim_fd, filename, flags, mode, pipe);
252 return free_fd;
253 }
254 }
255
256 fatal("Out of target file descriptors");
257}
258
259void
260Process::resetFDEntry(int tgt_fd)
261{
262 FDEntry *fde = getFDEntry(tgt_fd);
263 assert(fde->fd > -1);
264
265 fde->reset();
266}
267
268int
269Process::getSimFD(int tgt_fd)
270{
271 FDEntry *entry = getFDEntry(tgt_fd);
272 return entry ? entry->fd : -1;
273}
274
275FDEntry *
276Process::getFDEntry(int tgt_fd)
277{
278 assert(0 <= tgt_fd && tgt_fd < fd_array->size());
279 return &(*fd_array)[tgt_fd];
280}
281
282int
283Process::getTgtFD(int sim_fd)
284{
285 for (int index = 0; index < fd_array->size(); index++)
286 if ((*fd_array)[index].fd == sim_fd)
287 return index;
288 return -1;
289}
290
291void
292Process::allocateMem(Addr vaddr, int64_t size, bool clobber)
293{
294 int npages = divCeil(size, (int64_t)PageBytes);
295 Addr paddr = system->allocPhysPages(npages);
296 pTable->map(vaddr, paddr, size, clobber ? PageTableBase::Clobber : 0);
297}
298
299bool
300Process::fixupStackFault(Addr vaddr)
301{
302 // Check if this is already on the stack and there's just no page there
303 // yet.
304 if (vaddr >= stack_min && vaddr < stack_base) {
305 allocateMem(roundDown(vaddr, PageBytes), PageBytes);
306 return true;
307 }
308
309 // We've accessed the next page of the stack, so extend it to include
310 // this address.
311 if (vaddr < stack_min && vaddr >= stack_base - max_stack_size) {
312 while (vaddr < stack_min) {
313 stack_min -= TheISA::PageBytes;
314 if (stack_base - stack_min > max_stack_size)
315 fatal("Maximum stack size exceeded\n");
316 allocateMem(stack_min, TheISA::PageBytes);
317 inform("Increasing stack size by one page.");
318 };
319 return true;
320 }
321 return false;
322}
323
324void
325Process::fixFileOffsets()
326{
327 auto seek = [] (FDEntry *fde)
328 {
329 if (lseek(fde->fd, fde->fileOffset, SEEK_SET) < 0)
330 fatal("Unable to see to location in %s", fde->filename);
331 };
332
333 std::map<string,int>::iterator it;
334
335 // Search through the input options and set fd if match is found;
336 // otherwise, open an input file and seek to location.
337 FDEntry *fde_stdin = getFDEntry(STDIN_FILENO);
338 if ((it = imap.find(fde_stdin->filename)) != imap.end()) {
339 fde_stdin->fd = it->second;
340 } else {
341 fde_stdin->fd = openInputFile(fde_stdin->filename);
342 seek(fde_stdin);
343 }
344
345 // Search through the output/error options and set fd if match is found;
346 // otherwise, open an output file and seek to location.
347 FDEntry *fde_stdout = getFDEntry(STDOUT_FILENO);
348 if ((it = oemap.find(fde_stdout->filename)) != oemap.end()) {
349 fde_stdout->fd = it->second;
350 } else {
351 fde_stdout->fd = openOutputFile(fde_stdout->filename);
352 seek(fde_stdout);
353 }
354
355 FDEntry *fde_stderr = getFDEntry(STDERR_FILENO);
356 if (fde_stdout->filename == fde_stderr->filename) {
357 // Reuse the same file descriptor if these match.
358 fde_stderr->fd = fde_stdout->fd;
359 } else if ((it = oemap.find(fde_stderr->filename)) != oemap.end()) {
360 fde_stderr->fd = it->second;
361 } else {
362 fde_stderr->fd = openOutputFile(fde_stderr->filename);
363 seek(fde_stderr);
364 }
365
366 for (int tgt_fd = 3; tgt_fd < fd_array->size(); tgt_fd++) {
367 FDEntry *fde = getFDEntry(tgt_fd);
368 if (fde->fd == -1)
369 continue;
370
371 if (fde->isPipe) {
372 if (fde->filename == "PIPE-WRITE")
373 continue;
374 assert(fde->filename == "PIPE-READ");
375
376 int fds[2];
377 if (pipe(fds) < 0)
378 fatal("Unable to create new pipe");
379
380 fde->fd = fds[0];
381
382 FDEntry *fde_write = getFDEntry(fde->readPipeSource);
383 assert(
384 fde_write->filename == "PIPE-WRITE");
385 fde_write->fd = fds[1];
386 } else {
387 fde->fd = openFile(fde->filename.c_str(), fde->flags, fde->mode);
388 seek(fde);
389 }
390 }
391}
392
393void
394Process::findFileOffsets()
395{
396 for (auto& fde : *fd_array) {
397 if (fde.fd != -1)
398 fde.fileOffset = lseek(fde.fd, 0, SEEK_CUR);
399 }
400}
401
402void
403Process::setReadPipeSource(int read_pipe_fd, int source_fd)
404{
405 FDEntry *fde = getFDEntry(read_pipe_fd);
406 assert(source_fd >= -1);
407 fde->readPipeSource = source_fd;
408}
409
410void
411Process::serialize(CheckpointOut &cp) const
412{
413 SERIALIZE_SCALAR(brk_point);
414 SERIALIZE_SCALAR(stack_base);
415 SERIALIZE_SCALAR(stack_size);
416 SERIALIZE_SCALAR(stack_min);
417 SERIALIZE_SCALAR(next_thread_stack_base);
418 SERIALIZE_SCALAR(mmap_start);
419 SERIALIZE_SCALAR(mmap_end);
420 SERIALIZE_SCALAR(nxm_start);
421 SERIALIZE_SCALAR(nxm_end);
422 pTable->serialize(cp);
423 for (int x = 0; x < fd_array->size(); x++) {
424 (*fd_array)[x].serializeSection(cp, csprintf("FDEntry%d", x));
425 }
426 SERIALIZE_SCALAR(M5_pid);
427
428}
429
430void
431Process::unserialize(CheckpointIn &cp)
432{
433 UNSERIALIZE_SCALAR(brk_point);
434 UNSERIALIZE_SCALAR(stack_base);
435 UNSERIALIZE_SCALAR(stack_size);
436 UNSERIALIZE_SCALAR(stack_min);
437 UNSERIALIZE_SCALAR(next_thread_stack_base);
438 UNSERIALIZE_SCALAR(mmap_start);
439 UNSERIALIZE_SCALAR(mmap_end);
440 UNSERIALIZE_SCALAR(nxm_start);
441 UNSERIALIZE_SCALAR(nxm_end);
442 pTable->unserialize(cp);
443 for (int x = 0; x < fd_array->size(); x++) {
444 FDEntry *fde = getFDEntry(x);
445 fde->unserializeSection(cp, csprintf("FDEntry%d", x));
446 }
447 fixFileOffsets();
448 UNSERIALIZE_OPT_SCALAR(M5_pid);
449 // The above returns a bool so that you could do something if you don't
450 // find the param in the checkpoint if you wanted to, like set a default
451 // but in this case we'll just stick with the instantiated value if not
452 // found.
453}
454
455
456bool
457Process::map(Addr vaddr, Addr paddr, int size, bool cacheable)
458{
459 pTable->map(vaddr, paddr, size,
460 cacheable ? 0 : PageTableBase::Uncacheable);
461 return true;
462}
463
464
465////////////////////////////////////////////////////////////////////////
466//
467// LiveProcess member definitions
468//
469////////////////////////////////////////////////////////////////////////
470
471
472LiveProcess::LiveProcess(LiveProcessParams *params, ObjectFile *_objFile)
473 : Process(params), objFile(_objFile),
474 argv(params->cmd), envp(params->env), cwd(params->cwd),
475 executable(params->executable),
475 __uid(params->uid), __euid(params->euid),
476 __gid(params->gid), __egid(params->egid),
477 __pid(params->pid), __ppid(params->ppid),
478 drivers(params->drivers)
479{
480
481 // load up symbols, if any... these may be used for debugging or
482 // profiling.
483 if (!debugSymbolTable) {
484 debugSymbolTable = new SymbolTable();
485 if (!objFile->loadGlobalSymbols(debugSymbolTable) ||
486 !objFile->loadLocalSymbols(debugSymbolTable) ||
487 !objFile->loadWeakSymbols(debugSymbolTable)) {
488 // didn't load any symbols
489 delete debugSymbolTable;
490 debugSymbolTable = NULL;
491 }
492 }
493}
494
495void
496LiveProcess::syscall(int64_t callnum, ThreadContext *tc)
497{
498 num_syscalls++;
499
500 SyscallDesc *desc = getDesc(callnum);
501 if (desc == NULL)
502 fatal("Syscall %d out of range", callnum);
503
504 desc->doSyscall(callnum, this, tc);
505}
506
507IntReg
508LiveProcess::getSyscallArg(ThreadContext *tc, int &i, int width)
509{
510 return getSyscallArg(tc, i);
511}
512
513
514EmulatedDriver *
515LiveProcess::findDriver(std::string filename)
516{
517 for (EmulatedDriver *d : drivers) {
518 if (d->match(filename))
519 return d;
520 }
521
522 return NULL;
523}
524
525
526LiveProcess *
527LiveProcess::create(LiveProcessParams * params)
528{
529 LiveProcess *process = NULL;
530
476 __uid(params->uid), __euid(params->euid),
477 __gid(params->gid), __egid(params->egid),
478 __pid(params->pid), __ppid(params->ppid),
479 drivers(params->drivers)
480{
481
482 // load up symbols, if any... these may be used for debugging or
483 // profiling.
484 if (!debugSymbolTable) {
485 debugSymbolTable = new SymbolTable();
486 if (!objFile->loadGlobalSymbols(debugSymbolTable) ||
487 !objFile->loadLocalSymbols(debugSymbolTable) ||
488 !objFile->loadWeakSymbols(debugSymbolTable)) {
489 // didn't load any symbols
490 delete debugSymbolTable;
491 debugSymbolTable = NULL;
492 }
493 }
494}
495
496void
497LiveProcess::syscall(int64_t callnum, ThreadContext *tc)
498{
499 num_syscalls++;
500
501 SyscallDesc *desc = getDesc(callnum);
502 if (desc == NULL)
503 fatal("Syscall %d out of range", callnum);
504
505 desc->doSyscall(callnum, this, tc);
506}
507
508IntReg
509LiveProcess::getSyscallArg(ThreadContext *tc, int &i, int width)
510{
511 return getSyscallArg(tc, i);
512}
513
514
515EmulatedDriver *
516LiveProcess::findDriver(std::string filename)
517{
518 for (EmulatedDriver *d : drivers) {
519 if (d->match(filename))
520 return d;
521 }
522
523 return NULL;
524}
525
526
527LiveProcess *
528LiveProcess::create(LiveProcessParams * params)
529{
530 LiveProcess *process = NULL;
531
531 string executable =
532 params->executable == "" ? params->cmd[0] : params->executable;
533 ObjectFile *objFile = createObjectFile(executable);
532 // If not specified, set the executable parameter equal to the
533 // simulated system's zeroth command line parameter
534 if (params->executable == "") {
535 params->executable = params->cmd[0];
536 }
537
538 ObjectFile *objFile = createObjectFile(params->executable);
534 if (objFile == NULL) {
539 if (objFile == NULL) {
535 fatal("Can't load object file %s", executable);
540 fatal("Can't load object file %s", params->executable);
536 }
537
538 if (objFile->isDynamic())
539 fatal("Object file is a dynamic executable however only static "
540 "executables are supported!\n Please recompile your "
541 "executable as a static binary and try again.\n");
542
543#if THE_ISA == ALPHA_ISA
544 if (objFile->getArch() != ObjectFile::Alpha)
545 fatal("Object file architecture does not match compiled ISA (Alpha).");
546
547 switch (objFile->getOpSys()) {
548 case ObjectFile::Tru64:
549 process = new AlphaTru64Process(params, objFile);
550 break;
551
552 case ObjectFile::UnknownOpSys:
553 warn("Unknown operating system; assuming Linux.");
554 // fall through
555 case ObjectFile::Linux:
556 process = new AlphaLinuxProcess(params, objFile);
557 break;
558
559 default:
560 fatal("Unknown/unsupported operating system.");
561 }
562#elif THE_ISA == SPARC_ISA
563 if (objFile->getArch() != ObjectFile::SPARC64 &&
564 objFile->getArch() != ObjectFile::SPARC32)
565 fatal("Object file architecture does not match compiled ISA (SPARC).");
566 switch (objFile->getOpSys()) {
567 case ObjectFile::UnknownOpSys:
568 warn("Unknown operating system; assuming Linux.");
569 // fall through
570 case ObjectFile::Linux:
571 if (objFile->getArch() == ObjectFile::SPARC64) {
572 process = new Sparc64LinuxProcess(params, objFile);
573 } else {
574 process = new Sparc32LinuxProcess(params, objFile);
575 }
576 break;
577
578
579 case ObjectFile::Solaris:
580 process = new SparcSolarisProcess(params, objFile);
581 break;
582
583 default:
584 fatal("Unknown/unsupported operating system.");
585 }
586#elif THE_ISA == X86_ISA
587 if (objFile->getArch() != ObjectFile::X86_64 &&
588 objFile->getArch() != ObjectFile::I386)
589 fatal("Object file architecture does not match compiled ISA (x86).");
590 switch (objFile->getOpSys()) {
591 case ObjectFile::UnknownOpSys:
592 warn("Unknown operating system; assuming Linux.");
593 // fall through
594 case ObjectFile::Linux:
595 if (objFile->getArch() == ObjectFile::X86_64) {
596 process = new X86_64LinuxProcess(params, objFile);
597 } else {
598 process = new I386LinuxProcess(params, objFile);
599 }
600 break;
601
602 default:
603 fatal("Unknown/unsupported operating system.");
604 }
605#elif THE_ISA == MIPS_ISA
606 if (objFile->getArch() != ObjectFile::Mips)
607 fatal("Object file architecture does not match compiled ISA (MIPS).");
608 switch (objFile->getOpSys()) {
609 case ObjectFile::UnknownOpSys:
610 warn("Unknown operating system; assuming Linux.");
611 // fall through
612 case ObjectFile::Linux:
613 process = new MipsLinuxProcess(params, objFile);
614 break;
615
616 default:
617 fatal("Unknown/unsupported operating system.");
618 }
619#elif THE_ISA == ARM_ISA
620 ObjectFile::Arch arch = objFile->getArch();
621 if (arch != ObjectFile::Arm && arch != ObjectFile::Thumb &&
622 arch != ObjectFile::Arm64)
623 fatal("Object file architecture does not match compiled ISA (ARM).");
624 switch (objFile->getOpSys()) {
625 case ObjectFile::UnknownOpSys:
626 warn("Unknown operating system; assuming Linux.");
627 // fall through
628 case ObjectFile::Linux:
629 if (arch == ObjectFile::Arm64) {
630 process = new ArmLinuxProcess64(params, objFile,
631 objFile->getArch());
632 } else {
633 process = new ArmLinuxProcess32(params, objFile,
634 objFile->getArch());
635 }
636 break;
637 case ObjectFile::FreeBSD:
638 if (arch == ObjectFile::Arm64) {
639 process = new ArmFreebsdProcess64(params, objFile,
640 objFile->getArch());
641 } else {
642 process = new ArmFreebsdProcess32(params, objFile,
643 objFile->getArch());
644 }
645 break;
646 case ObjectFile::LinuxArmOABI:
647 fatal("M5 does not support ARM OABI binaries. Please recompile with an"
648 " EABI compiler.");
649 default:
650 fatal("Unknown/unsupported operating system.");
651 }
652#elif THE_ISA == POWER_ISA
653 if (objFile->getArch() != ObjectFile::Power)
654 fatal("Object file architecture does not match compiled ISA (Power).");
655 switch (objFile->getOpSys()) {
656 case ObjectFile::UnknownOpSys:
657 warn("Unknown operating system; assuming Linux.");
658 // fall through
659 case ObjectFile::Linux:
660 process = new PowerLinuxProcess(params, objFile);
661 break;
662
663 default:
664 fatal("Unknown/unsupported operating system.");
665 }
666#else
667#error "THE_ISA not set"
668#endif
669
670 if (process == NULL)
671 fatal("Unknown error creating process object.");
672 return process;
673}
674
675LiveProcess *
676LiveProcessParams::create()
677{
678 return LiveProcess::create(this);
679}
541 }
542
543 if (objFile->isDynamic())
544 fatal("Object file is a dynamic executable however only static "
545 "executables are supported!\n Please recompile your "
546 "executable as a static binary and try again.\n");
547
548#if THE_ISA == ALPHA_ISA
549 if (objFile->getArch() != ObjectFile::Alpha)
550 fatal("Object file architecture does not match compiled ISA (Alpha).");
551
552 switch (objFile->getOpSys()) {
553 case ObjectFile::Tru64:
554 process = new AlphaTru64Process(params, objFile);
555 break;
556
557 case ObjectFile::UnknownOpSys:
558 warn("Unknown operating system; assuming Linux.");
559 // fall through
560 case ObjectFile::Linux:
561 process = new AlphaLinuxProcess(params, objFile);
562 break;
563
564 default:
565 fatal("Unknown/unsupported operating system.");
566 }
567#elif THE_ISA == SPARC_ISA
568 if (objFile->getArch() != ObjectFile::SPARC64 &&
569 objFile->getArch() != ObjectFile::SPARC32)
570 fatal("Object file architecture does not match compiled ISA (SPARC).");
571 switch (objFile->getOpSys()) {
572 case ObjectFile::UnknownOpSys:
573 warn("Unknown operating system; assuming Linux.");
574 // fall through
575 case ObjectFile::Linux:
576 if (objFile->getArch() == ObjectFile::SPARC64) {
577 process = new Sparc64LinuxProcess(params, objFile);
578 } else {
579 process = new Sparc32LinuxProcess(params, objFile);
580 }
581 break;
582
583
584 case ObjectFile::Solaris:
585 process = new SparcSolarisProcess(params, objFile);
586 break;
587
588 default:
589 fatal("Unknown/unsupported operating system.");
590 }
591#elif THE_ISA == X86_ISA
592 if (objFile->getArch() != ObjectFile::X86_64 &&
593 objFile->getArch() != ObjectFile::I386)
594 fatal("Object file architecture does not match compiled ISA (x86).");
595 switch (objFile->getOpSys()) {
596 case ObjectFile::UnknownOpSys:
597 warn("Unknown operating system; assuming Linux.");
598 // fall through
599 case ObjectFile::Linux:
600 if (objFile->getArch() == ObjectFile::X86_64) {
601 process = new X86_64LinuxProcess(params, objFile);
602 } else {
603 process = new I386LinuxProcess(params, objFile);
604 }
605 break;
606
607 default:
608 fatal("Unknown/unsupported operating system.");
609 }
610#elif THE_ISA == MIPS_ISA
611 if (objFile->getArch() != ObjectFile::Mips)
612 fatal("Object file architecture does not match compiled ISA (MIPS).");
613 switch (objFile->getOpSys()) {
614 case ObjectFile::UnknownOpSys:
615 warn("Unknown operating system; assuming Linux.");
616 // fall through
617 case ObjectFile::Linux:
618 process = new MipsLinuxProcess(params, objFile);
619 break;
620
621 default:
622 fatal("Unknown/unsupported operating system.");
623 }
624#elif THE_ISA == ARM_ISA
625 ObjectFile::Arch arch = objFile->getArch();
626 if (arch != ObjectFile::Arm && arch != ObjectFile::Thumb &&
627 arch != ObjectFile::Arm64)
628 fatal("Object file architecture does not match compiled ISA (ARM).");
629 switch (objFile->getOpSys()) {
630 case ObjectFile::UnknownOpSys:
631 warn("Unknown operating system; assuming Linux.");
632 // fall through
633 case ObjectFile::Linux:
634 if (arch == ObjectFile::Arm64) {
635 process = new ArmLinuxProcess64(params, objFile,
636 objFile->getArch());
637 } else {
638 process = new ArmLinuxProcess32(params, objFile,
639 objFile->getArch());
640 }
641 break;
642 case ObjectFile::FreeBSD:
643 if (arch == ObjectFile::Arm64) {
644 process = new ArmFreebsdProcess64(params, objFile,
645 objFile->getArch());
646 } else {
647 process = new ArmFreebsdProcess32(params, objFile,
648 objFile->getArch());
649 }
650 break;
651 case ObjectFile::LinuxArmOABI:
652 fatal("M5 does not support ARM OABI binaries. Please recompile with an"
653 " EABI compiler.");
654 default:
655 fatal("Unknown/unsupported operating system.");
656 }
657#elif THE_ISA == POWER_ISA
658 if (objFile->getArch() != ObjectFile::Power)
659 fatal("Object file architecture does not match compiled ISA (Power).");
660 switch (objFile->getOpSys()) {
661 case ObjectFile::UnknownOpSys:
662 warn("Unknown operating system; assuming Linux.");
663 // fall through
664 case ObjectFile::Linux:
665 process = new PowerLinuxProcess(params, objFile);
666 break;
667
668 default:
669 fatal("Unknown/unsupported operating system.");
670 }
671#else
672#error "THE_ISA not set"
673#endif
674
675 if (process == NULL)
676 fatal("Unknown error creating process object.");
677 return process;
678}
679
680LiveProcess *
681LiveProcessParams::create()
682{
683 return LiveProcess::create(this);
684}