2 * Copyright (c) 2012 ARM Limited
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
14 * Copyright (c) 2001-2005 The Regents of The University of Michigan
15 * All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 * Authors: Nathan Binkert
51 #include "base/loader/object_file.hh"
52 #include "base/loader/symtab.hh"
53 #include "base/intmath.hh"
54 #include "base/statistics.hh"
55 #include "config/the_isa.hh"
56 #include "cpu/thread_context.hh"
57 #include "mem/page_table.hh"
58 #include "mem/se_translating_port_proxy.hh"
59 #include "params/LiveProcess.hh"
60 #include "params/Process.hh"
61 #include "sim/debug.hh"
62 #include "sim/process.hh"
63 #include "sim/process_impl.hh"
64 #include "sim/stats.hh"
65 #include "sim/syscall_emul.hh"
66 #include "sim/system.hh"
68 #if THE_ISA == ALPHA_ISA
69 #include "arch/alpha/linux/process.hh"
70 #include "arch/alpha/tru64/process.hh"
71 #elif THE_ISA == SPARC_ISA
72 #include "arch/sparc/linux/process.hh"
73 #include "arch/sparc/solaris/process.hh"
74 #elif THE_ISA == MIPS_ISA
75 #include "arch/mips/linux/process.hh"
76 #elif THE_ISA == ARM_ISA
77 #include "arch/arm/linux/process.hh"
78 #elif THE_ISA == X86_ISA
79 #include "arch/x86/linux/process.hh"
80 #elif THE_ISA == POWER_ISA
81 #include "arch/power/linux/process.hh"
83 #error "THE_ISA not set"
88 using namespace TheISA
;
90 // current number of allocated processes
91 int num_processes
= 0;
93 template<class IntType
>
94 AuxVector
<IntType
>::AuxVector(IntType type
, IntType val
)
96 a_type
= TheISA::htog(type
);
97 a_val
= TheISA::htog(val
);
100 template struct AuxVector
<uint32_t>;
101 template struct AuxVector
<uint64_t>;
103 Process::Process(ProcessParams
* params
)
104 : SimObject(params
), system(params
->system
),
105 max_stack_size(params
->max_stack_size
),
106 M5_pid(system
->allocatePID()),
107 pTable(new PageTable(name(), M5_pid
)),
108 initVirtMem(system
->getSystemPort(), this,
109 SETranslatingPortProxy::Always
)
111 string in
= params
->input
;
112 string out
= params
->output
;
113 string err
= params
->errout
;
115 // initialize file descriptors to default: same as simulator
116 int stdin_fd
, stdout_fd
, stderr_fd
;
118 if (in
== "stdin" || in
== "cin")
119 stdin_fd
= STDIN_FILENO
;
120 else if (in
== "None")
123 stdin_fd
= Process::openInputFile(in
);
125 if (out
== "stdout" || out
== "cout")
126 stdout_fd
= STDOUT_FILENO
;
127 else if (out
== "stderr" || out
== "cerr")
128 stdout_fd
= STDERR_FILENO
;
129 else if (out
== "None")
132 stdout_fd
= Process::openOutputFile(out
);
134 if (err
== "stdout" || err
== "cout")
135 stderr_fd
= STDOUT_FILENO
;
136 else if (err
== "stderr" || err
== "cerr")
137 stderr_fd
= STDERR_FILENO
;
138 else if (err
== "None")
141 stderr_fd
= stdout_fd
;
143 stderr_fd
= Process::openOutputFile(err
);
145 // initialize first 3 fds (stdin, stdout, stderr)
146 Process::FdMap
*fdo
= &fd_map
[STDIN_FILENO
];
149 fdo
->flags
= O_RDONLY
;
153 fdo
= &fd_map
[STDOUT_FILENO
];
156 fdo
->flags
= O_WRONLY
| O_CREAT
| O_TRUNC
;
160 fdo
= &fd_map
[STDERR_FILENO
];
163 fdo
->flags
= O_WRONLY
;
168 // mark remaining fds as free
169 for (int i
= 3; i
<= MAX_FD
; ++i
) {
174 mmap_start
= mmap_end
= 0;
175 nxm_start
= nxm_end
= 0;
176 // other parameters will be initialized when the program is loaded
183 using namespace Stats
;
186 .name(name() + ".num_syscalls")
187 .desc("Number of system calls")
192 // static helper functions
195 Process::openInputFile(const string
&filename
)
197 int fd
= open(filename
.c_str(), O_RDONLY
);
201 cerr
<< "unable to open \"" << filename
<< "\" for reading\n";
202 fatal("can't open input file");
210 Process::openOutputFile(const string
&filename
)
212 int fd
= open(filename
.c_str(), O_WRONLY
| O_CREAT
| O_TRUNC
, 0664);
216 cerr
<< "unable to open \"" << filename
<< "\" for writing\n";
217 fatal("can't open output file");
224 Process::findFreeContext()
226 int size
= contextIds
.size();
228 for (int i
= 0; i
< size
; ++i
) {
229 tc
= system
->getThreadContext(contextIds
[i
]);
230 if (tc
->status() == ThreadContext::Halted
) {
231 // inactive context, free to use
241 if (contextIds
.empty())
242 fatal("Process %s is not associated with any HW contexts!\n", name());
244 // first thread context for this process... initialize & enable
245 ThreadContext
*tc
= system
->getThreadContext(contextIds
[0]);
247 // mark this context as active so it will start ticking.
248 tc
->activate(Cycles(0));
251 // map simulator fd sim_fd to target fd tgt_fd
253 Process::dup_fd(int sim_fd
, int tgt_fd
)
255 if (tgt_fd
< 0 || tgt_fd
> MAX_FD
)
256 panic("Process::dup_fd tried to dup past MAX_FD (%d)", tgt_fd
);
258 Process::FdMap
*fdo
= &fd_map
[tgt_fd
];
263 // generate new target fd for sim_fd
265 Process::alloc_fd(int sim_fd
, string filename
, int flags
, int mode
, bool pipe
)
267 // in case open() returns an error, don't allocate a new fd
271 // find first free target fd
272 for (int free_fd
= 0; free_fd
<= MAX_FD
; ++free_fd
) {
273 Process::FdMap
*fdo
= &fd_map
[free_fd
];
276 fdo
->filename
= filename
;
281 fdo
->readPipeSource
= 0;
286 panic("Process::alloc_fd: out of file descriptors!");
290 // free target fd (e.g., after close)
292 Process::free_fd(int tgt_fd
)
294 Process::FdMap
*fdo
= &fd_map
[tgt_fd
];
296 warn("Process::free_fd: request to free unused fd %d", tgt_fd
);
299 fdo
->filename
= "NULL";
304 fdo
->readPipeSource
= 0;
308 // look up simulator fd for given target fd
310 Process::sim_fd(int tgt_fd
)
312 if (tgt_fd
< 0 || tgt_fd
> MAX_FD
)
315 return fd_map
[tgt_fd
].fd
;
319 Process::sim_fd_obj(int tgt_fd
)
321 if (tgt_fd
< 0 || tgt_fd
> MAX_FD
)
324 return &fd_map
[tgt_fd
];
328 Process::allocateMem(Addr vaddr
, int64_t size
, bool clobber
)
330 int npages
= divCeil(size
, (int64_t)VMPageSize
);
331 Addr paddr
= system
->allocPhysPages(npages
);
332 pTable
->map(vaddr
, paddr
, size
, clobber
);
336 Process::fixupStackFault(Addr vaddr
)
338 // Check if this is already on the stack and there's just no page there
340 if (vaddr
>= stack_min
&& vaddr
< stack_base
) {
341 allocateMem(roundDown(vaddr
, VMPageSize
), VMPageSize
);
345 // We've accessed the next page of the stack, so extend it to include
347 if (vaddr
< stack_min
&& vaddr
>= stack_base
- max_stack_size
) {
348 while (vaddr
< stack_min
) {
349 stack_min
-= TheISA::PageBytes
;
350 if (stack_base
- stack_min
> max_stack_size
)
351 fatal("Maximum stack size exceeded\n");
352 allocateMem(stack_min
, TheISA::PageBytes
);
353 inform("Increasing stack size by one page.");
360 // find all offsets for currently open files and save them
362 Process::fix_file_offsets()
364 Process::FdMap
*fdo_stdin
= &fd_map
[STDIN_FILENO
];
365 Process::FdMap
*fdo_stdout
= &fd_map
[STDOUT_FILENO
];
366 Process::FdMap
*fdo_stderr
= &fd_map
[STDERR_FILENO
];
367 string in
= fdo_stdin
->filename
;
368 string out
= fdo_stdout
->filename
;
369 string err
= fdo_stderr
->filename
;
371 // initialize file descriptors to default: same as simulator
372 int stdin_fd
, stdout_fd
, stderr_fd
;
374 if (in
== "stdin" || in
== "cin")
375 stdin_fd
= STDIN_FILENO
;
376 else if (in
== "None")
379 // open standard in and seek to the right location
380 stdin_fd
= Process::openInputFile(in
);
381 if (lseek(stdin_fd
, fdo_stdin
->fileOffset
, SEEK_SET
) < 0)
382 panic("Unable to seek to correct location in file: %s", in
);
385 if (out
== "stdout" || out
== "cout")
386 stdout_fd
= STDOUT_FILENO
;
387 else if (out
== "stderr" || out
== "cerr")
388 stdout_fd
= STDERR_FILENO
;
389 else if (out
== "None")
392 stdout_fd
= Process::openOutputFile(out
);
393 if (lseek(stdout_fd
, fdo_stdout
->fileOffset
, SEEK_SET
) < 0)
394 panic("Unable to seek to correct location in file: %s", out
);
397 if (err
== "stdout" || err
== "cout")
398 stderr_fd
= STDOUT_FILENO
;
399 else if (err
== "stderr" || err
== "cerr")
400 stderr_fd
= STDERR_FILENO
;
401 else if (err
== "None")
404 stderr_fd
= stdout_fd
;
406 stderr_fd
= Process::openOutputFile(err
);
407 if (lseek(stderr_fd
, fdo_stderr
->fileOffset
, SEEK_SET
) < 0)
408 panic("Unable to seek to correct location in file: %s", err
);
411 fdo_stdin
->fd
= stdin_fd
;
412 fdo_stdout
->fd
= stdout_fd
;
413 fdo_stderr
->fd
= stderr_fd
;
416 for (int free_fd
= 3; free_fd
<= MAX_FD
; ++free_fd
) {
417 Process::FdMap
*fdo
= &fd_map
[free_fd
];
420 if (fdo
->filename
== "PIPE-WRITE")
423 assert (fdo
->filename
== "PIPE-READ");
426 int pipe_retval
= pipe(fds
);
428 if (pipe_retval
< 0) {
430 panic("Unable to create new pipe.");
432 fdo
->fd
= fds
[0]; //set read pipe
433 Process::FdMap
*fdo_write
= &fd_map
[fdo
->readPipeSource
];
434 if (fdo_write
->filename
!= "PIPE-WRITE")
435 panic ("Couldn't find write end of the pipe");
437 fdo_write
->fd
= fds
[1];//set write pipe
441 int fd
= open(fdo
->filename
.c_str(), fdo
->flags
, fdo
->mode
);
444 panic("Unable to open file: %s", fdo
->filename
);
447 //Seek to correct location before checkpoint
448 if (lseek(fd
,fdo
->fileOffset
, SEEK_SET
) < 0)
449 panic("Unable to seek to correct location in file: %s",
457 Process::find_file_offsets()
459 for (int free_fd
= 0; free_fd
<= MAX_FD
; ++free_fd
) {
460 Process::FdMap
*fdo
= &fd_map
[free_fd
];
462 fdo
->fileOffset
= lseek(fdo
->fd
, 0, SEEK_CUR
);
464 fdo
->filename
= "NULL";
471 Process::setReadPipeSource(int read_pipe_fd
, int source_fd
)
473 Process::FdMap
*fdo
= &fd_map
[read_pipe_fd
];
474 fdo
->readPipeSource
= source_fd
;
478 Process::FdMap::serialize(std::ostream
&os
)
480 SERIALIZE_SCALAR(fd
);
481 SERIALIZE_SCALAR(isPipe
);
482 SERIALIZE_SCALAR(filename
);
483 SERIALIZE_SCALAR(flags
);
484 SERIALIZE_SCALAR(readPipeSource
);
485 SERIALIZE_SCALAR(fileOffset
);
489 Process::FdMap::unserialize(Checkpoint
*cp
, const std::string
§ion
)
491 UNSERIALIZE_SCALAR(fd
);
492 UNSERIALIZE_SCALAR(isPipe
);
493 UNSERIALIZE_SCALAR(filename
);
494 UNSERIALIZE_SCALAR(flags
);
495 UNSERIALIZE_SCALAR(readPipeSource
);
496 UNSERIALIZE_SCALAR(fileOffset
);
500 Process::serialize(std::ostream
&os
)
502 SERIALIZE_SCALAR(brk_point
);
503 SERIALIZE_SCALAR(stack_base
);
504 SERIALIZE_SCALAR(stack_size
);
505 SERIALIZE_SCALAR(stack_min
);
506 SERIALIZE_SCALAR(next_thread_stack_base
);
507 SERIALIZE_SCALAR(mmap_start
);
508 SERIALIZE_SCALAR(mmap_end
);
509 SERIALIZE_SCALAR(nxm_start
);
510 SERIALIZE_SCALAR(nxm_end
);
512 pTable
->serialize(os
);
513 for (int x
= 0; x
<= MAX_FD
; x
++) {
514 nameOut(os
, csprintf("%s.FdMap%d", name(), x
));
515 fd_map
[x
].serialize(os
);
517 SERIALIZE_SCALAR(M5_pid
);
522 Process::unserialize(Checkpoint
*cp
, const std::string
§ion
)
524 UNSERIALIZE_SCALAR(brk_point
);
525 UNSERIALIZE_SCALAR(stack_base
);
526 UNSERIALIZE_SCALAR(stack_size
);
527 UNSERIALIZE_SCALAR(stack_min
);
528 UNSERIALIZE_SCALAR(next_thread_stack_base
);
529 UNSERIALIZE_SCALAR(mmap_start
);
530 UNSERIALIZE_SCALAR(mmap_end
);
531 UNSERIALIZE_SCALAR(nxm_start
);
532 UNSERIALIZE_SCALAR(nxm_end
);
533 pTable
->unserialize(cp
, section
);
534 for (int x
= 0; x
<= MAX_FD
; x
++) {
535 fd_map
[x
].unserialize(cp
, csprintf("%s.FdMap%d", section
, x
));
538 UNSERIALIZE_OPT_SCALAR(M5_pid
);
539 // The above returns a bool so that you could do something if you don't
540 // find the param in the checkpoint if you wanted to, like set a default
541 // but in this case we'll just stick with the instantianted value if not
547 Process::map(Addr vaddr
, Addr paddr
, int size
)
549 pTable
->map(vaddr
, paddr
, size
);
554 ////////////////////////////////////////////////////////////////////////
556 // LiveProcess member definitions
558 ////////////////////////////////////////////////////////////////////////
561 LiveProcess::LiveProcess(LiveProcessParams
* params
, ObjectFile
*_objFile
)
562 : Process(params
), objFile(_objFile
),
563 argv(params
->cmd
), envp(params
->env
), cwd(params
->cwd
)
566 __euid
= params
->euid
;
568 __egid
= params
->egid
;
570 __ppid
= params
->ppid
;
572 // load up symbols, if any... these may be used for debugging or
574 if (!debugSymbolTable
) {
575 debugSymbolTable
= new SymbolTable();
576 if (!objFile
->loadGlobalSymbols(debugSymbolTable
) ||
577 !objFile
->loadLocalSymbols(debugSymbolTable
)) {
578 // didn't load any symbols
579 delete debugSymbolTable
;
580 debugSymbolTable
= NULL
;
586 LiveProcess::syscall(int64_t callnum
, ThreadContext
*tc
)
590 SyscallDesc
*desc
= getDesc(callnum
);
592 fatal("Syscall %d out of range", callnum
);
594 desc
->doSyscall(callnum
, this, tc
);
598 LiveProcess::getSyscallArg(ThreadContext
*tc
, int &i
, int width
)
600 return getSyscallArg(tc
, i
);
604 LiveProcess::create(LiveProcessParams
* params
)
606 LiveProcess
*process
= NULL
;
609 params
->executable
== "" ? params
->cmd
[0] : params
->executable
;
610 ObjectFile
*objFile
= createObjectFile(executable
);
611 if (objFile
== NULL
) {
612 fatal("Can't load object file %s", executable
);
615 if (objFile
->isDynamic())
616 fatal("Object file is a dynamic executable however only static "
617 "executables are supported!\n Please recompile your "
618 "executable as a static binary and try again.\n");
620 #if THE_ISA == ALPHA_ISA
621 if (objFile
->getArch() != ObjectFile::Alpha
)
622 fatal("Object file architecture does not match compiled ISA (Alpha).");
624 switch (objFile
->getOpSys()) {
625 case ObjectFile::Tru64
:
626 process
= new AlphaTru64Process(params
, objFile
);
629 case ObjectFile::UnknownOpSys
:
630 warn("Unknown operating system; assuming Linux.");
632 case ObjectFile::Linux
:
633 process
= new AlphaLinuxProcess(params
, objFile
);
637 fatal("Unknown/unsupported operating system.");
639 #elif THE_ISA == SPARC_ISA
640 if (objFile
->getArch() != ObjectFile::SPARC64
&&
641 objFile
->getArch() != ObjectFile::SPARC32
)
642 fatal("Object file architecture does not match compiled ISA (SPARC).");
643 switch (objFile
->getOpSys()) {
644 case ObjectFile::UnknownOpSys
:
645 warn("Unknown operating system; assuming Linux.");
647 case ObjectFile::Linux
:
648 if (objFile
->getArch() == ObjectFile::SPARC64
) {
649 process
= new Sparc64LinuxProcess(params
, objFile
);
651 process
= new Sparc32LinuxProcess(params
, objFile
);
656 case ObjectFile::Solaris
:
657 process
= new SparcSolarisProcess(params
, objFile
);
661 fatal("Unknown/unsupported operating system.");
663 #elif THE_ISA == X86_ISA
664 if (objFile
->getArch() != ObjectFile::X86_64
&&
665 objFile
->getArch() != ObjectFile::I386
)
666 fatal("Object file architecture does not match compiled ISA (x86).");
667 switch (objFile
->getOpSys()) {
668 case ObjectFile::UnknownOpSys
:
669 warn("Unknown operating system; assuming Linux.");
671 case ObjectFile::Linux
:
672 if (objFile
->getArch() == ObjectFile::X86_64
) {
673 process
= new X86_64LinuxProcess(params
, objFile
);
675 process
= new I386LinuxProcess(params
, objFile
);
680 fatal("Unknown/unsupported operating system.");
682 #elif THE_ISA == MIPS_ISA
683 if (objFile
->getArch() != ObjectFile::Mips
)
684 fatal("Object file architecture does not match compiled ISA (MIPS).");
685 switch (objFile
->getOpSys()) {
686 case ObjectFile::UnknownOpSys
:
687 warn("Unknown operating system; assuming Linux.");
689 case ObjectFile::Linux
:
690 process
= new MipsLinuxProcess(params
, objFile
);
694 fatal("Unknown/unsupported operating system.");
696 #elif THE_ISA == ARM_ISA
697 if (objFile
->getArch() != ObjectFile::Arm
&&
698 objFile
->getArch() != ObjectFile::Thumb
)
699 fatal("Object file architecture does not match compiled ISA (ARM).");
700 switch (objFile
->getOpSys()) {
701 case ObjectFile::UnknownOpSys
:
702 warn("Unknown operating system; assuming Linux.");
704 case ObjectFile::Linux
:
705 process
= new ArmLinuxProcess(params
, objFile
, objFile
->getArch());
707 case ObjectFile::LinuxArmOABI
:
708 fatal("M5 does not support ARM OABI binaries. Please recompile with an"
711 fatal("Unknown/unsupported operating system.");
713 #elif THE_ISA == POWER_ISA
714 if (objFile
->getArch() != ObjectFile::Power
)
715 fatal("Object file architecture does not match compiled ISA (Power).");
716 switch (objFile
->getOpSys()) {
717 case ObjectFile::UnknownOpSys
:
718 warn("Unknown operating system; assuming Linux.");
720 case ObjectFile::Linux
:
721 process
= new PowerLinuxProcess(params
, objFile
);
725 fatal("Unknown/unsupported operating system.");
728 #error "THE_ISA not set"
732 fatal("Unknown error creating process object.");
737 LiveProcessParams::create()
739 return LiveProcess::create(this);