f27b70853e1618f886fe576471ff931127c21d0d
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
>
98 AuxVector
<IntType
>::AuxVector(IntType type
, IntType val
)
100 a_type
= TheISA::htog(type
);
101 a_val
= TheISA::htog(val
);
104 template struct AuxVector
<uint32_t>;
105 template struct AuxVector
<uint64_t>;
108 openFile(const string
& filename
, int flags
, mode_t mode
)
110 int sim_fd
= open(filename
.c_str(), flags
, mode
);
113 fatal("Unable to open %s with mode %O", filename
, mode
);
117 openInputFile(const string
&filename
)
119 return openFile(filename
, O_RDONLY
, 0);
123 openOutputFile(const string
&filename
)
125 return openFile(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0664);
128 Process::Process(ProcessParams
* params
)
129 : SimObject(params
), system(params
->system
),
130 brk_point(0), stack_base(0), stack_size(0), stack_min(0),
131 max_stack_size(params
->max_stack_size
),
132 next_thread_stack_base(0),
133 M5_pid(system
->allocatePID()),
134 useArchPT(params
->useArchPT
),
135 kvmInSE(params
->kvmInSE
),
137 static_cast<PageTableBase
*>(new ArchPageTable(name(), M5_pid
, system
)) :
138 static_cast<PageTableBase
*>(new FuncPageTable(name(), M5_pid
)) ),
139 initVirtMem(system
->getSystemPort(), this,
140 SETranslatingPortProxy::Always
),
141 fd_array(make_shared
<array
<FDEntry
, NUM_FDS
>>()),
143 {"cin", STDIN_FILENO
},
144 {"stdin", STDIN_FILENO
}},
146 {"cout", STDOUT_FILENO
},
147 {"stdout", STDOUT_FILENO
},
148 {"cerr", STDERR_FILENO
},
149 {"stderr", STDERR_FILENO
}}
152 std::map
<string
,int>::iterator it
;
154 // Search through the input options and set fd if match is found;
155 // otherwise, open an input file and seek to location.
156 FDEntry
*fde_stdin
= get_fd_entry(STDIN_FILENO
);
157 if ((it
= imap
.find(params
->input
)) != imap
.end())
160 sim_fd
= openInputFile(params
->input
);
161 fde_stdin
->set(sim_fd
, params
->input
, O_RDONLY
, -1, false);
163 // Search through the output/error options and set fd if match is found;
164 // otherwise, open an output file and seek to location.
165 FDEntry
*fde_stdout
= get_fd_entry(STDOUT_FILENO
);
166 if ((it
= oemap
.find(params
->output
)) != oemap
.end())
169 sim_fd
= openOutputFile(params
->output
);
170 fde_stdout
->set(sim_fd
, params
->output
, O_WRONLY
| O_CREAT
| O_TRUNC
,
173 FDEntry
*fde_stderr
= get_fd_entry(STDERR_FILENO
);
174 if (params
->output
== params
->errout
)
175 // Reuse the same file descriptor if these match.
176 sim_fd
= fde_stdout
->fd
;
177 else if ((it
= oemap
.find(params
->errout
)) != oemap
.end())
180 sim_fd
= openOutputFile(params
->errout
);
181 fde_stderr
->set(sim_fd
, params
->errout
, O_WRONLY
| O_CREAT
| O_TRUNC
,
184 mmap_start
= mmap_end
= 0;
185 nxm_start
= nxm_end
= 0;
186 // other parameters will be initialized when the program is loaded
193 using namespace Stats
;
196 .name(name() + ".num_syscalls")
197 .desc("Number of system calls")
202 Process::inheritFdArray(Process
*p
)
204 fd_array
= p
->fd_array
;
208 Process::findFreeContext()
210 for (int id
: contextIds
) {
211 ThreadContext
*tc
= system
->getThreadContext(id
);
212 if (tc
->status() == ThreadContext::Halted
)
221 if (contextIds
.empty())
222 fatal("Process %s is not associated with any HW contexts!\n", name());
224 // first thread context for this process... initialize & enable
225 ThreadContext
*tc
= system
->getThreadContext(contextIds
[0]);
227 // mark this context as active so it will start ticking.
230 pTable
->initState(tc
);
237 return DrainState::Drained
;
241 Process::alloc_fd(int sim_fd
, const string
& filename
, int flags
, int mode
,
247 for (int free_fd
= 0; free_fd
< fd_array
->size(); free_fd
++) {
248 FDEntry
*fde
= get_fd_entry(free_fd
);
250 fde
->set(sim_fd
, filename
, flags
, mode
, pipe
);
255 fatal("Out of target file descriptors");
259 Process::reset_fd_entry(int tgt_fd
)
261 FDEntry
*fde
= get_fd_entry(tgt_fd
);
262 assert(fde
->fd
> -1);
268 Process::sim_fd(int tgt_fd
)
270 FDEntry
*entry
= get_fd_entry(tgt_fd
);
271 return entry
? entry
->fd
: -1;
275 Process::get_fd_entry(int tgt_fd
)
277 assert(0 <= tgt_fd
&& tgt_fd
< fd_array
->size());
278 return &(*fd_array
)[tgt_fd
];
282 Process::tgt_fd(int sim_fd
)
284 for (int index
= 0; index
< fd_array
->size(); index
++)
285 if ((*fd_array
)[index
].fd
== sim_fd
)
291 Process::allocateMem(Addr vaddr
, int64_t size
, bool clobber
)
293 int npages
= divCeil(size
, (int64_t)PageBytes
);
294 Addr paddr
= system
->allocPhysPages(npages
);
295 pTable
->map(vaddr
, paddr
, size
, clobber
? PageTableBase::Clobber
: 0);
299 Process::fixupStackFault(Addr vaddr
)
301 // Check if this is already on the stack and there's just no page there
303 if (vaddr
>= stack_min
&& vaddr
< stack_base
) {
304 allocateMem(roundDown(vaddr
, PageBytes
), PageBytes
);
308 // We've accessed the next page of the stack, so extend it to include
310 if (vaddr
< stack_min
&& vaddr
>= stack_base
- max_stack_size
) {
311 while (vaddr
< stack_min
) {
312 stack_min
-= TheISA::PageBytes
;
313 if (stack_base
- stack_min
> max_stack_size
)
314 fatal("Maximum stack size exceeded\n");
315 allocateMem(stack_min
, TheISA::PageBytes
);
316 inform("Increasing stack size by one page.");
324 Process::fix_file_offsets()
326 auto seek
= [] (FDEntry
*fde
)
328 if (lseek(fde
->fd
, fde
->fileOffset
, SEEK_SET
) < 0)
329 fatal("Unable to see to location in %s", fde
->filename
);
332 std::map
<string
,int>::iterator it
;
334 // Search through the input options and set fd if match is found;
335 // otherwise, open an input file and seek to location.
336 FDEntry
*fde_stdin
= get_fd_entry(STDIN_FILENO
);
337 if ((it
= imap
.find(fde_stdin
->filename
)) != imap
.end()) {
338 fde_stdin
->fd
= it
->second
;
340 fde_stdin
->fd
= openInputFile(fde_stdin
->filename
);
344 // Search through the output/error options and set fd if match is found;
345 // otherwise, open an output file and seek to location.
346 FDEntry
*fde_stdout
= get_fd_entry(STDOUT_FILENO
);
347 if ((it
= oemap
.find(fde_stdout
->filename
)) != oemap
.end()) {
348 fde_stdout
->fd
= it
->second
;
350 fde_stdout
->fd
= openOutputFile(fde_stdout
->filename
);
354 FDEntry
*fde_stderr
= get_fd_entry(STDERR_FILENO
);
355 if (fde_stdout
->filename
== fde_stderr
->filename
) {
356 // Reuse the same file descriptor if these match.
357 fde_stderr
->fd
= fde_stdout
->fd
;
358 } else if ((it
= oemap
.find(fde_stderr
->filename
)) != oemap
.end()) {
359 fde_stderr
->fd
= it
->second
;
361 fde_stderr
->fd
= openOutputFile(fde_stderr
->filename
);
365 for (int tgt_fd
= 3; tgt_fd
< fd_array
->size(); tgt_fd
++) {
366 FDEntry
*fde
= get_fd_entry(tgt_fd
);
371 if (fde
->filename
== "PIPE-WRITE")
373 assert(fde
->filename
== "PIPE-READ");
377 fatal("Unable to create new pipe");
381 FDEntry
*fde_write
= get_fd_entry(fde
->readPipeSource
);
382 assert(fde_write
->filename
== "PIPE-WRITE");
383 fde_write
->fd
= fds
[1];
385 fde
->fd
= openFile(fde
->filename
.c_str(), fde
->flags
, fde
->mode
);
392 Process::find_file_offsets()
394 for (auto& fde
: *fd_array
) {
396 fde
.fileOffset
= lseek(fde
.fd
, 0, SEEK_CUR
);
401 Process::setReadPipeSource(int read_pipe_fd
, int source_fd
)
403 FDEntry
*fde
= get_fd_entry(read_pipe_fd
);
404 assert(source_fd
>= -1);
405 fde
->readPipeSource
= source_fd
;
409 Process::serialize(CheckpointOut
&cp
) const
411 SERIALIZE_SCALAR(brk_point
);
412 SERIALIZE_SCALAR(stack_base
);
413 SERIALIZE_SCALAR(stack_size
);
414 SERIALIZE_SCALAR(stack_min
);
415 SERIALIZE_SCALAR(next_thread_stack_base
);
416 SERIALIZE_SCALAR(mmap_start
);
417 SERIALIZE_SCALAR(mmap_end
);
418 SERIALIZE_SCALAR(nxm_start
);
419 SERIALIZE_SCALAR(nxm_end
);
420 pTable
->serialize(cp
);
421 for (int x
= 0; x
< fd_array
->size(); x
++) {
422 (*fd_array
)[x
].serializeSection(cp
, csprintf("FDEntry%d", x
));
424 SERIALIZE_SCALAR(M5_pid
);
429 Process::unserialize(CheckpointIn
&cp
)
431 UNSERIALIZE_SCALAR(brk_point
);
432 UNSERIALIZE_SCALAR(stack_base
);
433 UNSERIALIZE_SCALAR(stack_size
);
434 UNSERIALIZE_SCALAR(stack_min
);
435 UNSERIALIZE_SCALAR(next_thread_stack_base
);
436 UNSERIALIZE_SCALAR(mmap_start
);
437 UNSERIALIZE_SCALAR(mmap_end
);
438 UNSERIALIZE_SCALAR(nxm_start
);
439 UNSERIALIZE_SCALAR(nxm_end
);
440 pTable
->unserialize(cp
);
441 for (int x
= 0; x
< fd_array
->size(); x
++) {
442 FDEntry
*fde
= get_fd_entry(x
);
443 fde
->unserializeSection(cp
, csprintf("FDEntry%d", x
));
446 UNSERIALIZE_OPT_SCALAR(M5_pid
);
447 // The above returns a bool so that you could do something if you don't
448 // find the param in the checkpoint if you wanted to, like set a default
449 // but in this case we'll just stick with the instantiated value if not
455 Process::map(Addr vaddr
, Addr paddr
, int size
, bool cacheable
)
457 pTable
->map(vaddr
, paddr
, size
,
458 cacheable
? 0 : PageTableBase::Uncacheable
);
463 ////////////////////////////////////////////////////////////////////////
465 // LiveProcess member definitions
467 ////////////////////////////////////////////////////////////////////////
470 LiveProcess::LiveProcess(LiveProcessParams
*params
, ObjectFile
*_objFile
)
471 : Process(params
), objFile(_objFile
),
472 argv(params
->cmd
), envp(params
->env
), cwd(params
->cwd
),
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
;
530 params
->executable
== "" ? params
->cmd
[0] : params
->executable
;
531 ObjectFile
*objFile
= createObjectFile(executable
);
532 if (objFile
== NULL
) {
533 fatal("Can't load object file %s", executable
);
536 if (objFile
->isDynamic())
537 fatal("Object file is a dynamic executable however only static "
538 "executables are supported!\n Please recompile your "
539 "executable as a static binary and try again.\n");
541 #if THE_ISA == ALPHA_ISA
542 if (objFile
->getArch() != ObjectFile::Alpha
)
543 fatal("Object file architecture does not match compiled ISA (Alpha).");
545 switch (objFile
->getOpSys()) {
546 case ObjectFile::Tru64
:
547 process
= new AlphaTru64Process(params
, objFile
);
550 case ObjectFile::UnknownOpSys
:
551 warn("Unknown operating system; assuming Linux.");
553 case ObjectFile::Linux
:
554 process
= new AlphaLinuxProcess(params
, objFile
);
558 fatal("Unknown/unsupported operating system.");
560 #elif THE_ISA == SPARC_ISA
561 if (objFile
->getArch() != ObjectFile::SPARC64
&&
562 objFile
->getArch() != ObjectFile::SPARC32
)
563 fatal("Object file architecture does not match compiled ISA (SPARC).");
564 switch (objFile
->getOpSys()) {
565 case ObjectFile::UnknownOpSys
:
566 warn("Unknown operating system; assuming Linux.");
568 case ObjectFile::Linux
:
569 if (objFile
->getArch() == ObjectFile::SPARC64
) {
570 process
= new Sparc64LinuxProcess(params
, objFile
);
572 process
= new Sparc32LinuxProcess(params
, objFile
);
577 case ObjectFile::Solaris
:
578 process
= new SparcSolarisProcess(params
, objFile
);
582 fatal("Unknown/unsupported operating system.");
584 #elif THE_ISA == X86_ISA
585 if (objFile
->getArch() != ObjectFile::X86_64
&&
586 objFile
->getArch() != ObjectFile::I386
)
587 fatal("Object file architecture does not match compiled ISA (x86).");
588 switch (objFile
->getOpSys()) {
589 case ObjectFile::UnknownOpSys
:
590 warn("Unknown operating system; assuming Linux.");
592 case ObjectFile::Linux
:
593 if (objFile
->getArch() == ObjectFile::X86_64
) {
594 process
= new X86_64LinuxProcess(params
, objFile
);
596 process
= new I386LinuxProcess(params
, objFile
);
601 fatal("Unknown/unsupported operating system.");
603 #elif THE_ISA == MIPS_ISA
604 if (objFile
->getArch() != ObjectFile::Mips
)
605 fatal("Object file architecture does not match compiled ISA (MIPS).");
606 switch (objFile
->getOpSys()) {
607 case ObjectFile::UnknownOpSys
:
608 warn("Unknown operating system; assuming Linux.");
610 case ObjectFile::Linux
:
611 process
= new MipsLinuxProcess(params
, objFile
);
615 fatal("Unknown/unsupported operating system.");
617 #elif THE_ISA == ARM_ISA
618 ObjectFile::Arch arch
= objFile
->getArch();
619 if (arch
!= ObjectFile::Arm
&& arch
!= ObjectFile::Thumb
&&
620 arch
!= ObjectFile::Arm64
)
621 fatal("Object file architecture does not match compiled ISA (ARM).");
622 switch (objFile
->getOpSys()) {
623 case ObjectFile::UnknownOpSys
:
624 warn("Unknown operating system; assuming Linux.");
626 case ObjectFile::Linux
:
627 if (arch
== ObjectFile::Arm64
) {
628 process
= new ArmLinuxProcess64(params
, objFile
,
631 process
= new ArmLinuxProcess32(params
, objFile
,
635 case ObjectFile::FreeBSD
:
636 if (arch
== ObjectFile::Arm64
) {
637 process
= new ArmFreebsdProcess64(params
, objFile
,
640 process
= new ArmFreebsdProcess32(params
, objFile
,
644 case ObjectFile::LinuxArmOABI
:
645 fatal("M5 does not support ARM OABI binaries. Please recompile with an"
648 fatal("Unknown/unsupported operating system.");
650 #elif THE_ISA == POWER_ISA
651 if (objFile
->getArch() != ObjectFile::Power
)
652 fatal("Object file architecture does not match compiled ISA (Power).");
653 switch (objFile
->getOpSys()) {
654 case ObjectFile::UnknownOpSys
:
655 warn("Unknown operating system; assuming Linux.");
657 case ObjectFile::Linux
:
658 process
= new PowerLinuxProcess(params
, objFile
);
662 fatal("Unknown/unsupported operating system.");
665 #error "THE_ISA not set"
669 fatal("Unknown error creating process object.");
674 LiveProcessParams::create()
676 return LiveProcess::create(this);