2 * Copyright (c) 2014 Advanced Micro Devices, Inc.
3 * Copyright (c) 2012 ARM Limited
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.
15 * Copyright (c) 2001-2005 The Regents of The University of Michigan
16 * All rights reserved.
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.
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.
41 * Authors: Nathan Binkert
52 #include "base/loader/object_file.hh"
53 #include "base/loader/symtab.hh"
54 #include "base/intmath.hh"
55 #include "base/statistics.hh"
56 #include "config/the_isa.hh"
57 #include "cpu/thread_context.hh"
58 #include "mem/page_table.hh"
59 #include "mem/multi_level_page_table.hh"
60 #include "mem/se_translating_port_proxy.hh"
61 #include "params/LiveProcess.hh"
62 #include "params/Process.hh"
63 #include "sim/debug.hh"
64 #include "sim/process.hh"
65 #include "sim/process_impl.hh"
66 #include "sim/stats.hh"
67 #include "sim/syscall_emul.hh"
68 #include "sim/system.hh"
70 #if THE_ISA == ALPHA_ISA
71 #include "arch/alpha/linux/process.hh"
72 #include "arch/alpha/tru64/process.hh"
73 #elif THE_ISA == SPARC_ISA
74 #include "arch/sparc/linux/process.hh"
75 #include "arch/sparc/solaris/process.hh"
76 #elif THE_ISA == MIPS_ISA
77 #include "arch/mips/linux/process.hh"
78 #elif THE_ISA == ARM_ISA
79 #include "arch/arm/linux/process.hh"
80 #elif THE_ISA == X86_ISA
81 #include "arch/x86/linux/process.hh"
82 #elif THE_ISA == POWER_ISA
83 #include "arch/power/linux/process.hh"
85 #error "THE_ISA not set"
90 using namespace TheISA
;
92 // current number of allocated processes
93 int num_processes
= 0;
95 template<class IntType
>
96 AuxVector
<IntType
>::AuxVector(IntType type
, IntType val
)
98 a_type
= TheISA::htog(type
);
99 a_val
= TheISA::htog(val
);
102 template struct AuxVector
<uint32_t>;
103 template struct AuxVector
<uint64_t>;
105 Process::Process(ProcessParams
* params
)
106 : SimObject(params
), system(params
->system
),
107 max_stack_size(params
->max_stack_size
),
108 M5_pid(system
->allocatePID()),
109 useArchPT(params
->useArchPT
),
111 static_cast<PageTableBase
*>(new ArchPageTable(name(), M5_pid
, system
)) :
112 static_cast<PageTableBase
*>(new FuncPageTable(name(), M5_pid
)) ),
113 initVirtMem(system
->getSystemPort(), this,
114 SETranslatingPortProxy::Always
)
116 string in
= params
->input
;
117 string out
= params
->output
;
118 string err
= params
->errout
;
120 // initialize file descriptors to default: same as simulator
121 int stdin_fd
, stdout_fd
, stderr_fd
;
123 if (in
== "stdin" || in
== "cin")
124 stdin_fd
= STDIN_FILENO
;
125 else if (in
== "None")
128 stdin_fd
= Process::openInputFile(in
);
130 if (out
== "stdout" || out
== "cout")
131 stdout_fd
= STDOUT_FILENO
;
132 else if (out
== "stderr" || out
== "cerr")
133 stdout_fd
= STDERR_FILENO
;
134 else if (out
== "None")
137 stdout_fd
= Process::openOutputFile(out
);
139 if (err
== "stdout" || err
== "cout")
140 stderr_fd
= STDOUT_FILENO
;
141 else if (err
== "stderr" || err
== "cerr")
142 stderr_fd
= STDERR_FILENO
;
143 else if (err
== "None")
146 stderr_fd
= stdout_fd
;
148 stderr_fd
= Process::openOutputFile(err
);
150 // initialize first 3 fds (stdin, stdout, stderr)
151 Process::FdMap
*fdo
= &fd_map
[STDIN_FILENO
];
154 fdo
->flags
= O_RDONLY
;
158 fdo
= &fd_map
[STDOUT_FILENO
];
161 fdo
->flags
= O_WRONLY
| O_CREAT
| O_TRUNC
;
165 fdo
= &fd_map
[STDERR_FILENO
];
168 fdo
->flags
= O_WRONLY
;
173 // mark remaining fds as free
174 for (int i
= 3; i
<= MAX_FD
; ++i
) {
179 mmap_start
= mmap_end
= 0;
180 nxm_start
= nxm_end
= 0;
181 // other parameters will be initialized when the program is loaded
188 using namespace Stats
;
191 .name(name() + ".num_syscalls")
192 .desc("Number of system calls")
197 // static helper functions
200 Process::openInputFile(const string
&filename
)
202 int fd
= open(filename
.c_str(), O_RDONLY
);
206 cerr
<< "unable to open \"" << filename
<< "\" for reading\n";
207 fatal("can't open input file");
215 Process::openOutputFile(const string
&filename
)
217 int fd
= open(filename
.c_str(), O_WRONLY
| O_CREAT
| O_TRUNC
, 0664);
221 cerr
<< "unable to open \"" << filename
<< "\" for writing\n";
222 fatal("can't open output file");
229 Process::findFreeContext()
231 int size
= contextIds
.size();
233 for (int i
= 0; i
< size
; ++i
) {
234 tc
= system
->getThreadContext(contextIds
[i
]);
235 if (tc
->status() == ThreadContext::Halted
) {
236 // inactive context, free to use
246 if (contextIds
.empty())
247 fatal("Process %s is not associated with any HW contexts!\n", name());
249 // first thread context for this process... initialize & enable
250 ThreadContext
*tc
= system
->getThreadContext(contextIds
[0]);
252 // mark this context as active so it will start ticking.
255 pTable
->initState(tc
);
258 // map simulator fd sim_fd to target fd tgt_fd
260 Process::dup_fd(int sim_fd
, int tgt_fd
)
262 if (tgt_fd
< 0 || tgt_fd
> MAX_FD
)
263 panic("Process::dup_fd tried to dup past MAX_FD (%d)", tgt_fd
);
265 Process::FdMap
*fdo
= &fd_map
[tgt_fd
];
270 // generate new target fd for sim_fd
272 Process::alloc_fd(int sim_fd
, string filename
, int flags
, int mode
, bool pipe
)
274 // in case open() returns an error, don't allocate a new fd
278 // find first free target fd
279 for (int free_fd
= 0; free_fd
<= MAX_FD
; ++free_fd
) {
280 Process::FdMap
*fdo
= &fd_map
[free_fd
];
281 if (fdo
->fd
== -1 && fdo
->driver
== NULL
) {
283 fdo
->filename
= filename
;
288 fdo
->readPipeSource
= 0;
293 panic("Process::alloc_fd: out of file descriptors!");
297 // free target fd (e.g., after close)
299 Process::free_fd(int tgt_fd
)
301 Process::FdMap
*fdo
= &fd_map
[tgt_fd
];
303 warn("Process::free_fd: request to free unused fd %d", tgt_fd
);
306 fdo
->filename
= "NULL";
311 fdo
->readPipeSource
= 0;
316 // look up simulator fd for given target fd
318 Process::sim_fd(int tgt_fd
)
320 if (tgt_fd
< 0 || tgt_fd
> MAX_FD
)
323 return fd_map
[tgt_fd
].fd
;
327 Process::sim_fd_obj(int tgt_fd
)
329 if (tgt_fd
< 0 || tgt_fd
> MAX_FD
)
332 return &fd_map
[tgt_fd
];
336 Process::allocateMem(Addr vaddr
, int64_t size
, bool clobber
)
338 int npages
= divCeil(size
, (int64_t)PageBytes
);
339 Addr paddr
= system
->allocPhysPages(npages
);
340 pTable
->map(vaddr
, paddr
, size
, clobber
);
344 Process::fixupStackFault(Addr vaddr
)
346 // Check if this is already on the stack and there's just no page there
348 if (vaddr
>= stack_min
&& vaddr
< stack_base
) {
349 allocateMem(roundDown(vaddr
, PageBytes
), PageBytes
);
353 // We've accessed the next page of the stack, so extend it to include
355 if (vaddr
< stack_min
&& vaddr
>= stack_base
- max_stack_size
) {
356 while (vaddr
< stack_min
) {
357 stack_min
-= TheISA::PageBytes
;
358 if (stack_base
- stack_min
> max_stack_size
)
359 fatal("Maximum stack size exceeded\n");
360 allocateMem(stack_min
, TheISA::PageBytes
);
361 inform("Increasing stack size by one page.");
368 // find all offsets for currently open files and save them
370 Process::fix_file_offsets()
372 Process::FdMap
*fdo_stdin
= &fd_map
[STDIN_FILENO
];
373 Process::FdMap
*fdo_stdout
= &fd_map
[STDOUT_FILENO
];
374 Process::FdMap
*fdo_stderr
= &fd_map
[STDERR_FILENO
];
375 string in
= fdo_stdin
->filename
;
376 string out
= fdo_stdout
->filename
;
377 string err
= fdo_stderr
->filename
;
379 // initialize file descriptors to default: same as simulator
380 int stdin_fd
, stdout_fd
, stderr_fd
;
382 if (in
== "stdin" || in
== "cin")
383 stdin_fd
= STDIN_FILENO
;
384 else if (in
== "None")
387 // open standard in and seek to the right location
388 stdin_fd
= Process::openInputFile(in
);
389 if (lseek(stdin_fd
, fdo_stdin
->fileOffset
, SEEK_SET
) < 0)
390 panic("Unable to seek to correct location in file: %s", in
);
393 if (out
== "stdout" || out
== "cout")
394 stdout_fd
= STDOUT_FILENO
;
395 else if (out
== "stderr" || out
== "cerr")
396 stdout_fd
= STDERR_FILENO
;
397 else if (out
== "None")
400 stdout_fd
= Process::openOutputFile(out
);
401 if (lseek(stdout_fd
, fdo_stdout
->fileOffset
, SEEK_SET
) < 0)
402 panic("Unable to seek to correct location in file: %s", out
);
405 if (err
== "stdout" || err
== "cout")
406 stderr_fd
= STDOUT_FILENO
;
407 else if (err
== "stderr" || err
== "cerr")
408 stderr_fd
= STDERR_FILENO
;
409 else if (err
== "None")
412 stderr_fd
= stdout_fd
;
414 stderr_fd
= Process::openOutputFile(err
);
415 if (lseek(stderr_fd
, fdo_stderr
->fileOffset
, SEEK_SET
) < 0)
416 panic("Unable to seek to correct location in file: %s", err
);
419 fdo_stdin
->fd
= stdin_fd
;
420 fdo_stdout
->fd
= stdout_fd
;
421 fdo_stderr
->fd
= stderr_fd
;
424 for (int free_fd
= 3; free_fd
<= MAX_FD
; ++free_fd
) {
425 Process::FdMap
*fdo
= &fd_map
[free_fd
];
428 if (fdo
->filename
== "PIPE-WRITE")
431 assert (fdo
->filename
== "PIPE-READ");
434 int pipe_retval
= pipe(fds
);
436 if (pipe_retval
< 0) {
438 panic("Unable to create new pipe.");
440 fdo
->fd
= fds
[0]; //set read pipe
441 Process::FdMap
*fdo_write
= &fd_map
[fdo
->readPipeSource
];
442 if (fdo_write
->filename
!= "PIPE-WRITE")
443 panic ("Couldn't find write end of the pipe");
445 fdo_write
->fd
= fds
[1];//set write pipe
449 int fd
= open(fdo
->filename
.c_str(), fdo
->flags
, fdo
->mode
);
452 panic("Unable to open file: %s", fdo
->filename
);
455 //Seek to correct location before checkpoint
456 if (lseek(fd
,fdo
->fileOffset
, SEEK_SET
) < 0)
457 panic("Unable to seek to correct location in file: %s",
465 Process::find_file_offsets()
467 for (int free_fd
= 0; free_fd
<= MAX_FD
; ++free_fd
) {
468 Process::FdMap
*fdo
= &fd_map
[free_fd
];
470 fdo
->fileOffset
= lseek(fdo
->fd
, 0, SEEK_CUR
);
472 fdo
->filename
= "NULL";
479 Process::setReadPipeSource(int read_pipe_fd
, int source_fd
)
481 Process::FdMap
*fdo
= &fd_map
[read_pipe_fd
];
482 fdo
->readPipeSource
= source_fd
;
486 Process::FdMap::serialize(std::ostream
&os
)
488 SERIALIZE_SCALAR(fd
);
489 SERIALIZE_SCALAR(isPipe
);
490 SERIALIZE_SCALAR(filename
);
491 SERIALIZE_SCALAR(flags
);
492 SERIALIZE_SCALAR(readPipeSource
);
493 SERIALIZE_SCALAR(fileOffset
);
497 Process::FdMap::unserialize(Checkpoint
*cp
, const std::string
§ion
)
499 UNSERIALIZE_SCALAR(fd
);
500 UNSERIALIZE_SCALAR(isPipe
);
501 UNSERIALIZE_SCALAR(filename
);
502 UNSERIALIZE_SCALAR(flags
);
503 UNSERIALIZE_SCALAR(readPipeSource
);
504 UNSERIALIZE_SCALAR(fileOffset
);
508 Process::serialize(std::ostream
&os
)
510 SERIALIZE_SCALAR(brk_point
);
511 SERIALIZE_SCALAR(stack_base
);
512 SERIALIZE_SCALAR(stack_size
);
513 SERIALIZE_SCALAR(stack_min
);
514 SERIALIZE_SCALAR(next_thread_stack_base
);
515 SERIALIZE_SCALAR(mmap_start
);
516 SERIALIZE_SCALAR(mmap_end
);
517 SERIALIZE_SCALAR(nxm_start
);
518 SERIALIZE_SCALAR(nxm_end
);
520 pTable
->serialize(os
);
521 for (int x
= 0; x
<= MAX_FD
; x
++) {
522 nameOut(os
, csprintf("%s.FdMap%d", name(), x
));
523 fd_map
[x
].serialize(os
);
525 SERIALIZE_SCALAR(M5_pid
);
530 Process::unserialize(Checkpoint
*cp
, const std::string
§ion
)
532 UNSERIALIZE_SCALAR(brk_point
);
533 UNSERIALIZE_SCALAR(stack_base
);
534 UNSERIALIZE_SCALAR(stack_size
);
535 UNSERIALIZE_SCALAR(stack_min
);
536 UNSERIALIZE_SCALAR(next_thread_stack_base
);
537 UNSERIALIZE_SCALAR(mmap_start
);
538 UNSERIALIZE_SCALAR(mmap_end
);
539 UNSERIALIZE_SCALAR(nxm_start
);
540 UNSERIALIZE_SCALAR(nxm_end
);
541 pTable
->unserialize(cp
, section
);
542 for (int x
= 0; x
<= MAX_FD
; x
++) {
543 fd_map
[x
].unserialize(cp
, csprintf("%s.FdMap%d", section
, x
));
546 UNSERIALIZE_OPT_SCALAR(M5_pid
);
547 // The above returns a bool so that you could do something if you don't
548 // find the param in the checkpoint if you wanted to, like set a default
549 // but in this case we'll just stick with the instantianted value if not
555 Process::map(Addr vaddr
, Addr paddr
, int size
)
557 pTable
->map(vaddr
, paddr
, size
);
562 ////////////////////////////////////////////////////////////////////////
564 // LiveProcess member definitions
566 ////////////////////////////////////////////////////////////////////////
569 LiveProcess::LiveProcess(LiveProcessParams
* params
, ObjectFile
*_objFile
)
570 : Process(params
), objFile(_objFile
),
571 argv(params
->cmd
), envp(params
->env
), cwd(params
->cwd
),
572 drivers(params
->drivers
)
575 __euid
= params
->euid
;
577 __egid
= params
->egid
;
579 __ppid
= params
->ppid
;
581 // load up symbols, if any... these may be used for debugging or
583 if (!debugSymbolTable
) {
584 debugSymbolTable
= new SymbolTable();
585 if (!objFile
->loadGlobalSymbols(debugSymbolTable
) ||
586 !objFile
->loadLocalSymbols(debugSymbolTable
) ||
587 !objFile
->loadWeakSymbols(debugSymbolTable
)) {
588 // didn't load any symbols
589 delete debugSymbolTable
;
590 debugSymbolTable
= NULL
;
596 LiveProcess::syscall(int64_t callnum
, ThreadContext
*tc
)
600 SyscallDesc
*desc
= getDesc(callnum
);
602 fatal("Syscall %d out of range", callnum
);
604 desc
->doSyscall(callnum
, this, tc
);
608 LiveProcess::getSyscallArg(ThreadContext
*tc
, int &i
, int width
)
610 return getSyscallArg(tc
, i
);
615 LiveProcess::findDriver(std::string filename
)
617 for (EmulatedDriver
*d
: drivers
) {
618 if (d
->match(filename
))
627 LiveProcess::create(LiveProcessParams
* params
)
629 LiveProcess
*process
= NULL
;
632 params
->executable
== "" ? params
->cmd
[0] : params
->executable
;
633 ObjectFile
*objFile
= createObjectFile(executable
);
634 if (objFile
== NULL
) {
635 fatal("Can't load object file %s", executable
);
638 if (objFile
->isDynamic())
639 fatal("Object file is a dynamic executable however only static "
640 "executables are supported!\n Please recompile your "
641 "executable as a static binary and try again.\n");
643 #if THE_ISA == ALPHA_ISA
644 if (objFile
->getArch() != ObjectFile::Alpha
)
645 fatal("Object file architecture does not match compiled ISA (Alpha).");
647 switch (objFile
->getOpSys()) {
648 case ObjectFile::Tru64
:
649 process
= new AlphaTru64Process(params
, objFile
);
652 case ObjectFile::UnknownOpSys
:
653 warn("Unknown operating system; assuming Linux.");
655 case ObjectFile::Linux
:
656 process
= new AlphaLinuxProcess(params
, objFile
);
660 fatal("Unknown/unsupported operating system.");
662 #elif THE_ISA == SPARC_ISA
663 if (objFile
->getArch() != ObjectFile::SPARC64
&&
664 objFile
->getArch() != ObjectFile::SPARC32
)
665 fatal("Object file architecture does not match compiled ISA (SPARC).");
666 switch (objFile
->getOpSys()) {
667 case ObjectFile::UnknownOpSys
:
668 warn("Unknown operating system; assuming Linux.");
670 case ObjectFile::Linux
:
671 if (objFile
->getArch() == ObjectFile::SPARC64
) {
672 process
= new Sparc64LinuxProcess(params
, objFile
);
674 process
= new Sparc32LinuxProcess(params
, objFile
);
679 case ObjectFile::Solaris
:
680 process
= new SparcSolarisProcess(params
, objFile
);
684 fatal("Unknown/unsupported operating system.");
686 #elif THE_ISA == X86_ISA
687 if (objFile
->getArch() != ObjectFile::X86_64
&&
688 objFile
->getArch() != ObjectFile::I386
)
689 fatal("Object file architecture does not match compiled ISA (x86).");
690 switch (objFile
->getOpSys()) {
691 case ObjectFile::UnknownOpSys
:
692 warn("Unknown operating system; assuming Linux.");
694 case ObjectFile::Linux
:
695 if (objFile
->getArch() == ObjectFile::X86_64
) {
696 process
= new X86_64LinuxProcess(params
, objFile
);
698 process
= new I386LinuxProcess(params
, objFile
);
703 fatal("Unknown/unsupported operating system.");
705 #elif THE_ISA == MIPS_ISA
706 if (objFile
->getArch() != ObjectFile::Mips
)
707 fatal("Object file architecture does not match compiled ISA (MIPS).");
708 switch (objFile
->getOpSys()) {
709 case ObjectFile::UnknownOpSys
:
710 warn("Unknown operating system; assuming Linux.");
712 case ObjectFile::Linux
:
713 process
= new MipsLinuxProcess(params
, objFile
);
717 fatal("Unknown/unsupported operating system.");
719 #elif THE_ISA == ARM_ISA
720 ObjectFile::Arch arch
= objFile
->getArch();
721 if (arch
!= ObjectFile::Arm
&& arch
!= ObjectFile::Thumb
&&
722 arch
!= ObjectFile::Arm64
)
723 fatal("Object file architecture does not match compiled ISA (ARM).");
724 switch (objFile
->getOpSys()) {
725 case ObjectFile::UnknownOpSys
:
726 warn("Unknown operating system; assuming Linux.");
728 case ObjectFile::Linux
:
729 if (arch
== ObjectFile::Arm64
) {
730 process
= new ArmLinuxProcess64(params
, objFile
,
733 process
= new ArmLinuxProcess32(params
, objFile
,
737 case ObjectFile::LinuxArmOABI
:
738 fatal("M5 does not support ARM OABI binaries. Please recompile with an"
741 fatal("Unknown/unsupported operating system.");
743 #elif THE_ISA == POWER_ISA
744 if (objFile
->getArch() != ObjectFile::Power
)
745 fatal("Object file architecture does not match compiled ISA (Power).");
746 switch (objFile
->getOpSys()) {
747 case ObjectFile::UnknownOpSys
:
748 warn("Unknown operating system; assuming Linux.");
750 case ObjectFile::Linux
:
751 process
= new PowerLinuxProcess(params
, objFile
);
755 fatal("Unknown/unsupported operating system.");
758 #error "THE_ISA not set"
762 fatal("Unknown error creating process object.");
767 LiveProcessParams::create()
769 return LiveProcess::create(this);