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
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"
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"
87 #error "THE_ISA not set"
92 using namespace TheISA
;
94 // current number of allocated processes
95 int num_processes
= 0;
97 template<class IntType
>
99 AuxVector
<IntType
>::AuxVector(IntType type
, IntType val
)
101 a_type
= TheISA::htog(type
);
102 a_val
= TheISA::htog(val
);
105 template struct AuxVector
<uint32_t>;
106 template struct AuxVector
<uint64_t>;
109 openFile(const string
& filename
, int flags
, mode_t mode
)
111 int sim_fd
= open(filename
.c_str(), flags
, mode
);
114 fatal("Unable to open %s with mode %O", filename
, mode
);
118 openInputFile(const string
&filename
)
120 return openFile(filename
, O_RDONLY
, 0);
124 openOutputFile(const string
&filename
)
126 return openFile(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0664);
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
),
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
>>()),
144 {"cin", STDIN_FILENO
},
145 {"stdin", STDIN_FILENO
}},
147 {"cout", STDOUT_FILENO
},
148 {"stdout", STDOUT_FILENO
},
149 {"cerr", STDERR_FILENO
},
150 {"stderr", STDERR_FILENO
}}
153 std::map
<string
,int>::iterator it
;
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())
161 sim_fd
= openInputFile(params
->input
);
162 fde_stdin
->set(sim_fd
, params
->input
, O_RDONLY
, -1, false);
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())
170 sim_fd
= openOutputFile(params
->output
);
171 fde_stdout
->set(sim_fd
, params
->output
, O_WRONLY
| O_CREAT
| O_TRUNC
,
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())
181 sim_fd
= openOutputFile(params
->errout
);
182 fde_stderr
->set(sim_fd
, params
->errout
, O_WRONLY
| O_CREAT
| O_TRUNC
,
185 mmap_start
= mmap_end
= 0;
186 nxm_start
= nxm_end
= 0;
187 // other parameters will be initialized when the program is loaded
194 using namespace Stats
;
197 .name(name() + ".num_syscalls")
198 .desc("Number of system calls")
203 Process::inheritFDArray(Process
*p
)
205 fd_array
= p
->fd_array
;
209 Process::findFreeContext()
211 for (int id
: contextIds
) {
212 ThreadContext
*tc
= system
->getThreadContext(id
);
213 if (tc
->status() == ThreadContext::Halted
)
222 if (contextIds
.empty())
223 fatal("Process %s is not associated with any HW contexts!\n", name());
225 // first thread context for this process... initialize & enable
226 ThreadContext
*tc
= system
->getThreadContext(contextIds
[0]);
228 // mark this context as active so it will start ticking.
231 pTable
->initState(tc
);
238 return DrainState::Drained
;
242 Process::allocFD(int sim_fd
, const string
& filename
, int flags
, int mode
,
245 for (int free_fd
= 0; free_fd
< fd_array
->size(); free_fd
++) {
246 FDEntry
*fde
= getFDEntry(free_fd
);
248 fde
->set(sim_fd
, filename
, flags
, mode
, pipe
);
253 fatal("Out of target file descriptors");
257 Process::resetFDEntry(int tgt_fd
)
259 FDEntry
*fde
= getFDEntry(tgt_fd
);
260 assert(fde
->fd
> -1);
266 Process::getSimFD(int tgt_fd
)
268 FDEntry
*entry
= getFDEntry(tgt_fd
);
269 return entry
? entry
->fd
: -1;
273 Process::getFDEntry(int tgt_fd
)
275 assert(0 <= tgt_fd
&& tgt_fd
< fd_array
->size());
276 return &(*fd_array
)[tgt_fd
];
280 Process::getTgtFD(int sim_fd
)
282 for (int index
= 0; index
< fd_array
->size(); index
++)
283 if ((*fd_array
)[index
].fd
== sim_fd
)
289 Process::allocateMem(Addr vaddr
, int64_t size
, bool clobber
)
291 int npages
= divCeil(size
, (int64_t)PageBytes
);
292 Addr paddr
= system
->allocPhysPages(npages
);
293 pTable
->map(vaddr
, paddr
, size
, clobber
? PageTableBase::Clobber
: 0);
297 Process::fixupStackFault(Addr vaddr
)
299 // Check if this is already on the stack and there's just no page there
301 if (vaddr
>= stack_min
&& vaddr
< stack_base
) {
302 allocateMem(roundDown(vaddr
, PageBytes
), PageBytes
);
306 // We've accessed the next page of the stack, so extend it to include
308 if (vaddr
< stack_min
&& vaddr
>= stack_base
- max_stack_size
) {
309 while (vaddr
< stack_min
) {
310 stack_min
-= TheISA::PageBytes
;
311 if (stack_base
- stack_min
> max_stack_size
)
312 fatal("Maximum stack size exceeded\n");
313 allocateMem(stack_min
, TheISA::PageBytes
);
314 inform("Increasing stack size by one page.");
322 Process::fixFileOffsets()
324 auto seek
= [] (FDEntry
*fde
)
326 if (lseek(fde
->fd
, fde
->fileOffset
, SEEK_SET
) < 0)
327 fatal("Unable to see to location in %s", fde
->filename
);
330 std::map
<string
,int>::iterator it
;
332 // Search through the input options and set fd if match is found;
333 // otherwise, open an input file and seek to location.
334 FDEntry
*fde_stdin
= getFDEntry(STDIN_FILENO
);
335 if ((it
= imap
.find(fde_stdin
->filename
)) != imap
.end()) {
336 fde_stdin
->fd
= it
->second
;
338 fde_stdin
->fd
= openInputFile(fde_stdin
->filename
);
342 // Search through the output/error options and set fd if match is found;
343 // otherwise, open an output file and seek to location.
344 FDEntry
*fde_stdout
= getFDEntry(STDOUT_FILENO
);
345 if ((it
= oemap
.find(fde_stdout
->filename
)) != oemap
.end()) {
346 fde_stdout
->fd
= it
->second
;
348 fde_stdout
->fd
= openOutputFile(fde_stdout
->filename
);
352 FDEntry
*fde_stderr
= getFDEntry(STDERR_FILENO
);
353 if (fde_stdout
->filename
== fde_stderr
->filename
) {
354 // Reuse the same file descriptor if these match.
355 fde_stderr
->fd
= fde_stdout
->fd
;
356 } else if ((it
= oemap
.find(fde_stderr
->filename
)) != oemap
.end()) {
357 fde_stderr
->fd
= it
->second
;
359 fde_stderr
->fd
= openOutputFile(fde_stderr
->filename
);
363 for (int tgt_fd
= 3; tgt_fd
< fd_array
->size(); tgt_fd
++) {
364 FDEntry
*fde
= getFDEntry(tgt_fd
);
369 if (fde
->filename
== "PIPE-WRITE")
371 assert(fde
->filename
== "PIPE-READ");
375 fatal("Unable to create new pipe");
379 FDEntry
*fde_write
= getFDEntry(fde
->readPipeSource
);
381 fde_write
->filename
== "PIPE-WRITE");
382 fde_write
->fd
= fds
[1];
384 fde
->fd
= openFile(fde
->filename
.c_str(), fde
->flags
, fde
->mode
);
391 Process::findFileOffsets()
393 for (auto& fde
: *fd_array
) {
395 fde
.fileOffset
= lseek(fde
.fd
, 0, SEEK_CUR
);
400 Process::setReadPipeSource(int read_pipe_fd
, int source_fd
)
402 FDEntry
*fde
= getFDEntry(read_pipe_fd
);
403 assert(source_fd
>= -1);
404 fde
->readPipeSource
= source_fd
;
408 Process::serialize(CheckpointOut
&cp
) const
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
));
423 SERIALIZE_SCALAR(M5_pid
);
428 Process::unserialize(CheckpointIn
&cp
)
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
));
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
454 Process::map(Addr vaddr
, Addr paddr
, int size
, bool cacheable
)
456 pTable
->map(vaddr
, paddr
, size
,
457 cacheable
? 0 : PageTableBase::Uncacheable
);
462 ////////////////////////////////////////////////////////////////////////
464 // LiveProcess member definitions
466 ////////////////////////////////////////////////////////////////////////
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
)
479 // load up symbols, if any... these may be used for debugging or
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
;
494 LiveProcess::syscall(int64_t callnum
, ThreadContext
*tc
)
498 SyscallDesc
*desc
= getDesc(callnum
);
500 fatal("Syscall %d out of range", callnum
);
502 desc
->doSyscall(callnum
, this, tc
);
506 LiveProcess::getSyscallArg(ThreadContext
*tc
, int &i
, int width
)
508 return getSyscallArg(tc
, i
);
513 LiveProcess::findDriver(std::string filename
)
515 for (EmulatedDriver
*d
: drivers
) {
516 if (d
->match(filename
))
525 LiveProcess::create(LiveProcessParams
* params
)
527 LiveProcess
*process
= NULL
;
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];
535 ObjectFile
*objFile
= createObjectFile(params
->executable
);
536 if (objFile
== NULL
) {
537 fatal("Can't load object file %s", params
->executable
);
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");
545 #if THE_ISA == ALPHA_ISA
546 if (objFile
->getArch() != ObjectFile::Alpha
)
547 fatal("Object file architecture does not match compiled ISA (Alpha).");
549 switch (objFile
->getOpSys()) {
550 case ObjectFile::Tru64
:
551 process
= new AlphaTru64Process(params
, objFile
);
554 case ObjectFile::UnknownOpSys
:
555 warn("Unknown operating system; assuming Linux.");
557 case ObjectFile::Linux
:
558 process
= new AlphaLinuxProcess(params
, objFile
);
562 fatal("Unknown/unsupported operating system.");
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.");
572 case ObjectFile::Linux
:
573 if (objFile
->getArch() == ObjectFile::SPARC64
) {
574 process
= new Sparc64LinuxProcess(params
, objFile
);
576 process
= new Sparc32LinuxProcess(params
, objFile
);
581 case ObjectFile::Solaris
:
582 process
= new SparcSolarisProcess(params
, objFile
);
586 fatal("Unknown/unsupported operating system.");
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.");
596 case ObjectFile::Linux
:
597 if (objFile
->getArch() == ObjectFile::X86_64
) {
598 process
= new X86_64LinuxProcess(params
, objFile
);
600 process
= new I386LinuxProcess(params
, objFile
);
605 fatal("Unknown/unsupported operating system.");
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.");
614 case ObjectFile::Linux
:
615 process
= new MipsLinuxProcess(params
, objFile
);
619 fatal("Unknown/unsupported operating system.");
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.");
630 case ObjectFile::Linux
:
631 if (arch
== ObjectFile::Arm64
) {
632 process
= new ArmLinuxProcess64(params
, objFile
,
635 process
= new ArmLinuxProcess32(params
, objFile
,
639 case ObjectFile::FreeBSD
:
640 if (arch
== ObjectFile::Arm64
) {
641 process
= new ArmFreebsdProcess64(params
, objFile
,
644 process
= new ArmFreebsdProcess32(params
, objFile
,
648 case ObjectFile::LinuxArmOABI
:
649 fatal("M5 does not support ARM OABI binaries. Please recompile with an"
652 fatal("Unknown/unsupported operating system.");
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.");
661 case ObjectFile::Linux
:
662 process
= new PowerLinuxProcess(params
, objFile
);
666 fatal("Unknown/unsupported operating system.");
669 #error "THE_ISA not set"
673 fatal("Unknown error creating process object.");
678 LiveProcessParams::create()
680 return LiveProcess::create(this);