syscall_emul, style: fix newline issue inside assert
[gem5.git] / src / sim / process.cc
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
91 using namespace std;
92 using namespace TheISA;
93
94 // current number of allocated processes
95 int num_processes = 0;
96
97 template<class IntType>
98
99 AuxVector<IntType>::AuxVector(IntType type, IntType val)
100 {
101 a_type = TheISA::htog(type);
102 a_val = TheISA::htog(val);
103 }
104
105 template struct AuxVector<uint32_t>;
106 template struct AuxVector<uint64_t>;
107
108 static int
109 openFile(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
117 static int
118 openInputFile(const string &filename)
119 {
120 return openFile(filename, O_RDONLY, 0);
121 }
122
123 static int
124 openOutputFile(const string &filename)
125 {
126 return openFile(filename, O_WRONLY | O_CREAT | O_TRUNC, 0664);
127 }
128
129 Process::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
191 void
192 Process::regStats()
193 {
194 using namespace Stats;
195
196 num_syscalls
197 .name(name() + ".num_syscalls")
198 .desc("Number of system calls")
199 ;
200 }
201
202 void
203 Process::inheritFDArray(Process *p)
204 {
205 fd_array = p->fd_array;
206 }
207
208 ThreadContext *
209 Process::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
219 void
220 Process::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
234 DrainState
235 Process::drain()
236 {
237 findFileOffsets();
238 return DrainState::Drained;
239 }
240
241 int
242 Process::allocFD(int sim_fd, const string& filename, int flags, int mode,
243 bool pipe)
244 {
245 for (int free_fd = 0; free_fd < fd_array->size(); free_fd++) {
246 FDEntry *fde = getFDEntry(free_fd);
247 if (fde->isFree()) {
248 fde->set(sim_fd, filename, flags, mode, pipe);
249 return free_fd;
250 }
251 }
252
253 fatal("Out of target file descriptors");
254 }
255
256 void
257 Process::resetFDEntry(int tgt_fd)
258 {
259 FDEntry *fde = getFDEntry(tgt_fd);
260 assert(fde->fd > -1);
261
262 fde->reset();
263 }
264
265 int
266 Process::getSimFD(int tgt_fd)
267 {
268 FDEntry *entry = getFDEntry(tgt_fd);
269 return entry ? entry->fd : -1;
270 }
271
272 FDEntry *
273 Process::getFDEntry(int tgt_fd)
274 {
275 assert(0 <= tgt_fd && tgt_fd < fd_array->size());
276 return &(*fd_array)[tgt_fd];
277 }
278
279 int
280 Process::getTgtFD(int sim_fd)
281 {
282 for (int index = 0; index < fd_array->size(); index++)
283 if ((*fd_array)[index].fd == sim_fd)
284 return index;
285 return -1;
286 }
287
288 void
289 Process::allocateMem(Addr vaddr, int64_t size, bool clobber)
290 {
291 int npages = divCeil(size, (int64_t)PageBytes);
292 Addr paddr = system->allocPhysPages(npages);
293 pTable->map(vaddr, paddr, size,
294 clobber ? PageTableBase::Clobber : PageTableBase::Zero);
295 }
296
297 bool
298 Process::fixupStackFault(Addr vaddr)
299 {
300 // Check if this is already on the stack and there's just no page there
301 // yet.
302 if (vaddr >= stack_min && vaddr < stack_base) {
303 allocateMem(roundDown(vaddr, PageBytes), PageBytes);
304 return true;
305 }
306
307 // We've accessed the next page of the stack, so extend it to include
308 // this address.
309 if (vaddr < stack_min && vaddr >= stack_base - max_stack_size) {
310 while (vaddr < stack_min) {
311 stack_min -= TheISA::PageBytes;
312 if (stack_base - stack_min > max_stack_size)
313 fatal("Maximum stack size exceeded\n");
314 allocateMem(stack_min, TheISA::PageBytes);
315 inform("Increasing stack size by one page.");
316 };
317 return true;
318 }
319 return false;
320 }
321
322 void
323 Process::fixFileOffsets()
324 {
325 auto seek = [] (FDEntry *fde)
326 {
327 if (lseek(fde->fd, fde->fileOffset, SEEK_SET) < 0)
328 fatal("Unable to see to location in %s", fde->filename);
329 };
330
331 std::map<string,int>::iterator it;
332
333 // Search through the input options and set fd if match is found;
334 // otherwise, open an input file and seek to location.
335 FDEntry *fde_stdin = getFDEntry(STDIN_FILENO);
336 if ((it = imap.find(fde_stdin->filename)) != imap.end()) {
337 fde_stdin->fd = it->second;
338 } else {
339 fde_stdin->fd = openInputFile(fde_stdin->filename);
340 seek(fde_stdin);
341 }
342
343 // Search through the output/error options and set fd if match is found;
344 // otherwise, open an output file and seek to location.
345 FDEntry *fde_stdout = getFDEntry(STDOUT_FILENO);
346 if ((it = oemap.find(fde_stdout->filename)) != oemap.end()) {
347 fde_stdout->fd = it->second;
348 } else {
349 fde_stdout->fd = openOutputFile(fde_stdout->filename);
350 seek(fde_stdout);
351 }
352
353 FDEntry *fde_stderr = getFDEntry(STDERR_FILENO);
354 if (fde_stdout->filename == fde_stderr->filename) {
355 // Reuse the same file descriptor if these match.
356 fde_stderr->fd = fde_stdout->fd;
357 } else if ((it = oemap.find(fde_stderr->filename)) != oemap.end()) {
358 fde_stderr->fd = it->second;
359 } else {
360 fde_stderr->fd = openOutputFile(fde_stderr->filename);
361 seek(fde_stderr);
362 }
363
364 for (int tgt_fd = 3; tgt_fd < fd_array->size(); tgt_fd++) {
365 FDEntry *fde = getFDEntry(tgt_fd);
366 if (fde->fd == -1)
367 continue;
368
369 if (fde->isPipe) {
370 if (fde->filename == "PIPE-WRITE")
371 continue;
372 assert(fde->filename == "PIPE-READ");
373
374 int fds[2];
375 if (pipe(fds) < 0)
376 fatal("Unable to create new pipe");
377
378 fde->fd = fds[0];
379
380 FDEntry *fde_write = getFDEntry(fde->readPipeSource);
381 assert(fde_write->filename == "PIPE-WRITE");
382 fde_write->fd = fds[1];
383 } else {
384 fde->fd = openFile(fde->filename.c_str(), fde->flags, fde->mode);
385 seek(fde);
386 }
387 }
388 }
389
390 void
391 Process::findFileOffsets()
392 {
393 for (auto& fde : *fd_array) {
394 if (fde.fd != -1)
395 fde.fileOffset = lseek(fde.fd, 0, SEEK_CUR);
396 }
397 }
398
399 void
400 Process::setReadPipeSource(int read_pipe_fd, int source_fd)
401 {
402 FDEntry *fde = getFDEntry(read_pipe_fd);
403 assert(source_fd >= -1);
404 fde->readPipeSource = source_fd;
405 }
406
407 void
408 Process::serialize(CheckpointOut &cp) const
409 {
410 SERIALIZE_SCALAR(brk_point);
411 SERIALIZE_SCALAR(stack_base);
412 SERIALIZE_SCALAR(stack_size);
413 SERIALIZE_SCALAR(stack_min);
414 SERIALIZE_SCALAR(next_thread_stack_base);
415 SERIALIZE_SCALAR(mmap_start);
416 SERIALIZE_SCALAR(mmap_end);
417 SERIALIZE_SCALAR(nxm_start);
418 SERIALIZE_SCALAR(nxm_end);
419 pTable->serialize(cp);
420 for (int x = 0; x < fd_array->size(); x++) {
421 (*fd_array)[x].serializeSection(cp, csprintf("FDEntry%d", x));
422 }
423 SERIALIZE_SCALAR(M5_pid);
424
425 }
426
427 void
428 Process::unserialize(CheckpointIn &cp)
429 {
430 UNSERIALIZE_SCALAR(brk_point);
431 UNSERIALIZE_SCALAR(stack_base);
432 UNSERIALIZE_SCALAR(stack_size);
433 UNSERIALIZE_SCALAR(stack_min);
434 UNSERIALIZE_SCALAR(next_thread_stack_base);
435 UNSERIALIZE_SCALAR(mmap_start);
436 UNSERIALIZE_SCALAR(mmap_end);
437 UNSERIALIZE_SCALAR(nxm_start);
438 UNSERIALIZE_SCALAR(nxm_end);
439 pTable->unserialize(cp);
440 for (int x = 0; x < fd_array->size(); x++) {
441 FDEntry *fde = getFDEntry(x);
442 fde->unserializeSection(cp, csprintf("FDEntry%d", x));
443 }
444 fixFileOffsets();
445 UNSERIALIZE_OPT_SCALAR(M5_pid);
446 // The above returns a bool so that you could do something if you don't
447 // find the param in the checkpoint if you wanted to, like set a default
448 // but in this case we'll just stick with the instantiated value if not
449 // found.
450 }
451
452
453 bool
454 Process::map(Addr vaddr, Addr paddr, int size, bool cacheable)
455 {
456 pTable->map(vaddr, paddr, size,
457 cacheable ? PageTableBase::Zero : PageTableBase::Uncacheable);
458 return true;
459 }
460
461
462 ////////////////////////////////////////////////////////////////////////
463 //
464 // LiveProcess member definitions
465 //
466 ////////////////////////////////////////////////////////////////////////
467
468
469 LiveProcess::LiveProcess(LiveProcessParams *params, ObjectFile *_objFile)
470 : Process(params), objFile(_objFile),
471 argv(params->cmd), envp(params->env), cwd(params->cwd),
472 executable(params->executable),
473 __uid(params->uid), __euid(params->euid),
474 __gid(params->gid), __egid(params->egid),
475 __pid(params->pid), __ppid(params->ppid),
476 drivers(params->drivers)
477 {
478
479 // load up symbols, if any... these may be used for debugging or
480 // profiling.
481 if (!debugSymbolTable) {
482 debugSymbolTable = new SymbolTable();
483 if (!objFile->loadGlobalSymbols(debugSymbolTable) ||
484 !objFile->loadLocalSymbols(debugSymbolTable) ||
485 !objFile->loadWeakSymbols(debugSymbolTable)) {
486 // didn't load any symbols
487 delete debugSymbolTable;
488 debugSymbolTable = NULL;
489 }
490 }
491 }
492
493 void
494 LiveProcess::syscall(int64_t callnum, ThreadContext *tc)
495 {
496 num_syscalls++;
497
498 SyscallDesc *desc = getDesc(callnum);
499 if (desc == NULL)
500 fatal("Syscall %d out of range", callnum);
501
502 desc->doSyscall(callnum, this, tc);
503 }
504
505 IntReg
506 LiveProcess::getSyscallArg(ThreadContext *tc, int &i, int width)
507 {
508 return getSyscallArg(tc, i);
509 }
510
511
512 EmulatedDriver *
513 LiveProcess::findDriver(std::string filename)
514 {
515 for (EmulatedDriver *d : drivers) {
516 if (d->match(filename))
517 return d;
518 }
519
520 return NULL;
521 }
522
523
524 LiveProcess *
525 LiveProcess::create(LiveProcessParams * params)
526 {
527 LiveProcess *process = NULL;
528
529 // If not specified, set the executable parameter equal to the
530 // simulated system's zeroth command line parameter
531 if (params->executable == "") {
532 params->executable = params->cmd[0];
533 }
534
535 ObjectFile *objFile = createObjectFile(params->executable);
536 if (objFile == NULL) {
537 fatal("Can't load object file %s", params->executable);
538 }
539
540 if (objFile->isDynamic())
541 fatal("Object file is a dynamic executable however only static "
542 "executables are supported!\n Please recompile your "
543 "executable as a static binary and try again.\n");
544
545 #if THE_ISA == ALPHA_ISA
546 if (objFile->getArch() != ObjectFile::Alpha)
547 fatal("Object file architecture does not match compiled ISA (Alpha).");
548
549 switch (objFile->getOpSys()) {
550 case ObjectFile::Tru64:
551 process = new AlphaTru64Process(params, objFile);
552 break;
553
554 case ObjectFile::UnknownOpSys:
555 warn("Unknown operating system; assuming Linux.");
556 // fall through
557 case ObjectFile::Linux:
558 process = new AlphaLinuxProcess(params, objFile);
559 break;
560
561 default:
562 fatal("Unknown/unsupported operating system.");
563 }
564 #elif THE_ISA == SPARC_ISA
565 if (objFile->getArch() != ObjectFile::SPARC64 &&
566 objFile->getArch() != ObjectFile::SPARC32)
567 fatal("Object file architecture does not match compiled ISA (SPARC).");
568 switch (objFile->getOpSys()) {
569 case ObjectFile::UnknownOpSys:
570 warn("Unknown operating system; assuming Linux.");
571 // fall through
572 case ObjectFile::Linux:
573 if (objFile->getArch() == ObjectFile::SPARC64) {
574 process = new Sparc64LinuxProcess(params, objFile);
575 } else {
576 process = new Sparc32LinuxProcess(params, objFile);
577 }
578 break;
579
580
581 case ObjectFile::Solaris:
582 process = new SparcSolarisProcess(params, objFile);
583 break;
584
585 default:
586 fatal("Unknown/unsupported operating system.");
587 }
588 #elif THE_ISA == X86_ISA
589 if (objFile->getArch() != ObjectFile::X86_64 &&
590 objFile->getArch() != ObjectFile::I386)
591 fatal("Object file architecture does not match compiled ISA (x86).");
592 switch (objFile->getOpSys()) {
593 case ObjectFile::UnknownOpSys:
594 warn("Unknown operating system; assuming Linux.");
595 // fall through
596 case ObjectFile::Linux:
597 if (objFile->getArch() == ObjectFile::X86_64) {
598 process = new X86_64LinuxProcess(params, objFile);
599 } else {
600 process = new I386LinuxProcess(params, objFile);
601 }
602 break;
603
604 default:
605 fatal("Unknown/unsupported operating system.");
606 }
607 #elif THE_ISA == MIPS_ISA
608 if (objFile->getArch() != ObjectFile::Mips)
609 fatal("Object file architecture does not match compiled ISA (MIPS).");
610 switch (objFile->getOpSys()) {
611 case ObjectFile::UnknownOpSys:
612 warn("Unknown operating system; assuming Linux.");
613 // fall through
614 case ObjectFile::Linux:
615 process = new MipsLinuxProcess(params, objFile);
616 break;
617
618 default:
619 fatal("Unknown/unsupported operating system.");
620 }
621 #elif THE_ISA == ARM_ISA
622 ObjectFile::Arch arch = objFile->getArch();
623 if (arch != ObjectFile::Arm && arch != ObjectFile::Thumb &&
624 arch != ObjectFile::Arm64)
625 fatal("Object file architecture does not match compiled ISA (ARM).");
626 switch (objFile->getOpSys()) {
627 case ObjectFile::UnknownOpSys:
628 warn("Unknown operating system; assuming Linux.");
629 // fall through
630 case ObjectFile::Linux:
631 if (arch == ObjectFile::Arm64) {
632 process = new ArmLinuxProcess64(params, objFile,
633 objFile->getArch());
634 } else {
635 process = new ArmLinuxProcess32(params, objFile,
636 objFile->getArch());
637 }
638 break;
639 case ObjectFile::FreeBSD:
640 if (arch == ObjectFile::Arm64) {
641 process = new ArmFreebsdProcess64(params, objFile,
642 objFile->getArch());
643 } else {
644 process = new ArmFreebsdProcess32(params, objFile,
645 objFile->getArch());
646 }
647 break;
648 case ObjectFile::LinuxArmOABI:
649 fatal("M5 does not support ARM OABI binaries. Please recompile with an"
650 " EABI compiler.");
651 default:
652 fatal("Unknown/unsupported operating system.");
653 }
654 #elif THE_ISA == POWER_ISA
655 if (objFile->getArch() != ObjectFile::Power)
656 fatal("Object file architecture does not match compiled ISA (Power).");
657 switch (objFile->getOpSys()) {
658 case ObjectFile::UnknownOpSys:
659 warn("Unknown operating system; assuming Linux.");
660 // fall through
661 case ObjectFile::Linux:
662 process = new PowerLinuxProcess(params, objFile);
663 break;
664
665 default:
666 fatal("Unknown/unsupported operating system.");
667 }
668 #else
669 #error "THE_ISA not set"
670 #endif
671
672 if (process == NULL)
673 fatal("Unknown error creating process object.");
674 return process;
675 }
676
677 LiveProcess *
678 LiveProcessParams::create()
679 {
680 return LiveProcess::create(this);
681 }