2 * Copyright (c) 2001-2005 The Regents of The University of Michigan
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * Authors: Nathan Binkert
37 #include "arch/remote_gdb.hh"
38 #include "base/intmath.hh"
39 #include "base/loader/object_file.hh"
40 #include "base/loader/symtab.hh"
41 #include "base/statistics.hh"
42 #include "config/full_system.hh"
43 #include "config/the_isa.hh"
44 #include "cpu/thread_context.hh"
45 #include "mem/page_table.hh"
46 #include "mem/physical.hh"
47 #include "mem/translating_port.hh"
48 #include "params/Process.hh"
49 #include "params/LiveProcess.hh"
50 #include "sim/debug.hh"
51 #include "sim/process.hh"
52 #include "sim/process_impl.hh"
53 #include "sim/stats.hh"
54 #include "sim/syscall_emul.hh"
55 #include "sim/system.hh"
57 #if THE_ISA == ALPHA_ISA
58 #include "arch/alpha/linux/process.hh"
59 #include "arch/alpha/tru64/process.hh"
60 #elif THE_ISA == SPARC_ISA
61 #include "arch/sparc/linux/process.hh"
62 #include "arch/sparc/solaris/process.hh"
63 #elif THE_ISA == MIPS_ISA
64 #include "arch/mips/linux/process.hh"
65 #elif THE_ISA == ARM_ISA
66 #include "arch/arm/linux/process.hh"
67 #elif THE_ISA == X86_ISA
68 #include "arch/x86/linux/process.hh"
69 #elif THE_ISA == POWER_ISA
70 #include "arch/power/linux/process.hh"
72 #error "THE_ISA not set"
77 using namespace TheISA
;
80 // The purpose of this code is to fake the loader & syscall mechanism
81 // when there's no OS: thus there's no resone to use it in FULL_SYSTEM
82 // mode when we do have an OS
85 #error "process.cc not compatible with FULL_SYSTEM"
88 // current number of allocated processes
89 int num_processes
= 0;
91 template<class IntType
>
92 AuxVector
<IntType
>::AuxVector(IntType type
, IntType val
)
94 a_type
= TheISA::htog(type
);
95 a_val
= TheISA::htog(val
);
98 template class AuxVector
<uint32_t>;
99 template class AuxVector
<uint64_t>;
101 Process::Process(ProcessParams
* params
)
102 : SimObject(params
), system(params
->system
), checkpointRestored(false),
103 max_stack_size(params
->max_stack_size
)
105 string in
= params
->input
;
106 string out
= params
->output
;
107 string err
= params
->errout
;
109 // initialize file descriptors to default: same as simulator
110 int stdin_fd
, stdout_fd
, stderr_fd
;
112 if (in
== "stdin" || in
== "cin")
113 stdin_fd
= STDIN_FILENO
;
114 else if (in
== "None")
117 stdin_fd
= Process::openInputFile(in
);
119 if (out
== "stdout" || out
== "cout")
120 stdout_fd
= STDOUT_FILENO
;
121 else if (out
== "stderr" || out
== "cerr")
122 stdout_fd
= STDERR_FILENO
;
123 else if (out
== "None")
126 stdout_fd
= Process::openOutputFile(out
);
128 if (err
== "stdout" || err
== "cout")
129 stderr_fd
= STDOUT_FILENO
;
130 else if (err
== "stderr" || err
== "cerr")
131 stderr_fd
= STDERR_FILENO
;
132 else if (err
== "None")
135 stderr_fd
= stdout_fd
;
137 stderr_fd
= Process::openOutputFile(err
);
139 M5_pid
= system
->allocatePID();
140 // initialize first 3 fds (stdin, stdout, stderr)
141 Process::FdMap
*fdo
= &fd_map
[STDIN_FILENO
];
144 fdo
->flags
= O_RDONLY
;
148 fdo
= &fd_map
[STDOUT_FILENO
];
151 fdo
->flags
= O_WRONLY
| O_CREAT
| O_TRUNC
;
155 fdo
= &fd_map
[STDERR_FILENO
];
158 fdo
->flags
= O_WRONLY
;
163 // mark remaining fds as free
164 for (int i
= 3; i
<= MAX_FD
; ++i
) {
165 Process::FdMap
*fdo
= &fd_map
[i
];
169 mmap_start
= mmap_end
= 0;
170 nxm_start
= nxm_end
= 0;
171 pTable
= new PageTable(this);
172 // other parameters will be initialized when the program is loaded
179 using namespace Stats
;
182 .name(name() + ".PROG:num_syscalls")
183 .desc("Number of system calls")
188 // static helper functions
191 Process::openInputFile(const string
&filename
)
193 int fd
= open(filename
.c_str(), O_RDONLY
);
197 cerr
<< "unable to open \"" << filename
<< "\" for reading\n";
198 fatal("can't open input file");
206 Process::openOutputFile(const string
&filename
)
208 int fd
= open(filename
.c_str(), O_WRONLY
| O_CREAT
| O_TRUNC
, 0664);
212 cerr
<< "unable to open \"" << filename
<< "\" for writing\n";
213 fatal("can't open output file");
220 Process::findFreeContext()
222 int size
= contextIds
.size();
224 for (int i
= 0; i
< size
; ++i
) {
225 tc
= system
->getThreadContext(contextIds
[i
]);
226 if (tc
->status() == ThreadContext::Halted
) {
227 // inactive context, free to use
237 if (contextIds
.empty())
238 fatal("Process %s is not associated with any HW contexts!\n", name());
240 // first thread context for this process... initialize & enable
241 ThreadContext
*tc
= system
->getThreadContext(contextIds
[0]);
243 // mark this context as active so it will start ticking.
247 mem_port
= system
->physmem
->getPort("functional");
248 initVirtMem
= new TranslatingPort("process init port", this,
249 TranslatingPort::Always
);
250 mem_port
->setPeer(initVirtMem
);
251 initVirtMem
->setPeer(mem_port
);
254 // map simulator fd sim_fd to target fd tgt_fd
256 Process::dup_fd(int sim_fd
, int tgt_fd
)
258 if (tgt_fd
< 0 || tgt_fd
> MAX_FD
)
259 panic("Process::dup_fd tried to dup past MAX_FD (%d)", tgt_fd
);
261 Process::FdMap
*fdo
= &fd_map
[tgt_fd
];
266 // generate new target fd for sim_fd
268 Process::alloc_fd(int sim_fd
, string filename
, int flags
, int mode
, bool pipe
)
270 // in case open() returns an error, don't allocate a new fd
274 // find first free target fd
275 for (int free_fd
= 0; free_fd
<= MAX_FD
; ++free_fd
) {
276 Process::FdMap
*fdo
= &fd_map
[free_fd
];
279 fdo
->filename
= filename
;
284 fdo
->readPipeSource
= 0;
289 panic("Process::alloc_fd: out of file descriptors!");
293 // free target fd (e.g., after close)
295 Process::free_fd(int tgt_fd
)
297 Process::FdMap
*fdo
= &fd_map
[tgt_fd
];
299 warn("Process::free_fd: request to free unused fd %d", tgt_fd
);
302 fdo
->filename
= "NULL";
307 fdo
->readPipeSource
= 0;
311 // look up simulator fd for given target fd
313 Process::sim_fd(int tgt_fd
)
318 return fd_map
[tgt_fd
].fd
;
322 Process::sim_fd_obj(int tgt_fd
)
325 panic("sim_fd_obj called in fd out of range.");
327 return &fd_map
[tgt_fd
];
330 Process::checkAndAllocNextPage(Addr vaddr
)
332 // if this is an initial write we might not have
333 if (vaddr
>= stack_min
&& vaddr
< stack_base
) {
334 pTable
->allocate(roundDown(vaddr
, VMPageSize
), VMPageSize
);
338 // We've accessed the next page of the stack, so extend the stack
340 if (vaddr
< stack_min
&& vaddr
>= stack_base
- max_stack_size
) {
341 while (vaddr
< stack_min
) {
342 stack_min
-= TheISA::PageBytes
;
343 if(stack_base
- stack_min
> max_stack_size
)
344 fatal("Maximum stack size exceeded\n");
345 if(stack_base
- stack_min
> 8*1024*1024)
346 fatal("Over max stack size for one thread\n");
347 pTable
->allocate(stack_min
, TheISA::PageBytes
);
348 inform("Increasing stack size by one page.");
355 // find all offsets for currently open files and save them
357 Process::fix_file_offsets() {
358 Process::FdMap
*fdo_stdin
= &fd_map
[STDIN_FILENO
];
359 Process::FdMap
*fdo_stdout
= &fd_map
[STDOUT_FILENO
];
360 Process::FdMap
*fdo_stderr
= &fd_map
[STDERR_FILENO
];
361 string in
= fdo_stdin
->filename
;
362 string out
= fdo_stdout
->filename
;
363 string err
= fdo_stderr
->filename
;
365 // initialize file descriptors to default: same as simulator
366 int stdin_fd
, stdout_fd
, stderr_fd
;
368 if (in
== "stdin" || in
== "cin")
369 stdin_fd
= STDIN_FILENO
;
370 else if (in
== "None")
373 //OPEN standard in and seek to the right location
374 stdin_fd
= Process::openInputFile(in
);
375 if (lseek(stdin_fd
, fdo_stdin
->fileOffset
, SEEK_SET
) < 0)
376 panic("Unable to seek to correct location in file: %s", in
);
379 if (out
== "stdout" || out
== "cout")
380 stdout_fd
= STDOUT_FILENO
;
381 else if (out
== "stderr" || out
== "cerr")
382 stdout_fd
= STDERR_FILENO
;
383 else if (out
== "None")
386 stdout_fd
= Process::openOutputFile(out
);
387 if (lseek(stdout_fd
, fdo_stdout
->fileOffset
, SEEK_SET
) < 0)
388 panic("Unable to seek to correct location in file: %s", out
);
391 if (err
== "stdout" || err
== "cout")
392 stderr_fd
= STDOUT_FILENO
;
393 else if (err
== "stderr" || err
== "cerr")
394 stderr_fd
= STDERR_FILENO
;
395 else if (err
== "None")
398 stderr_fd
= stdout_fd
;
400 stderr_fd
= Process::openOutputFile(err
);
401 if (lseek(stderr_fd
, fdo_stderr
->fileOffset
, SEEK_SET
) < 0)
402 panic("Unable to seek to correct location in file: %s", err
);
405 fdo_stdin
->fd
= stdin_fd
;
406 fdo_stdout
->fd
= stdout_fd
;
407 fdo_stderr
->fd
= stderr_fd
;
410 for (int free_fd
= 3; free_fd
<= MAX_FD
; ++free_fd
) {
411 Process::FdMap
*fdo
= &fd_map
[free_fd
];
414 if (fdo
->filename
== "PIPE-WRITE")
417 assert (fdo
->filename
== "PIPE-READ");
420 int pipe_retval
= pipe(fds
);
422 if (pipe_retval
< 0) {
424 panic("Unable to create new pipe.");
426 fdo
->fd
= fds
[0]; //set read pipe
427 Process::FdMap
*fdo_write
= &fd_map
[fdo
->readPipeSource
];
428 if (fdo_write
->filename
!= "PIPE-WRITE")
429 panic ("Couldn't find write end of the pipe");
431 fdo_write
->fd
= fds
[1];//set write pipe
435 int fd
= open(fdo
->filename
.c_str(), fdo
->flags
, fdo
->mode
);
438 panic("Unable to open file: %s", fdo
->filename
);
441 //Seek to correct location before checkpoint
442 if (lseek(fd
,fdo
->fileOffset
, SEEK_SET
) < 0)
443 panic("Unable to seek to correct location in file: %s", fdo
->filename
);
449 Process::find_file_offsets(){
450 for (int free_fd
= 0; free_fd
<= MAX_FD
; ++free_fd
) {
451 Process::FdMap
*fdo
= &fd_map
[free_fd
];
453 fdo
->fileOffset
= lseek(fdo
->fd
, 0, SEEK_CUR
);
455 fdo
->filename
= "NULL";
462 Process::setReadPipeSource(int read_pipe_fd
, int source_fd
){
463 Process::FdMap
*fdo
= &fd_map
[read_pipe_fd
];
464 fdo
->readPipeSource
= source_fd
;
468 Process::FdMap::serialize(std::ostream
&os
)
470 SERIALIZE_SCALAR(fd
);
471 SERIALIZE_SCALAR(isPipe
);
472 SERIALIZE_SCALAR(filename
);
473 SERIALIZE_SCALAR(flags
);
474 SERIALIZE_SCALAR(readPipeSource
);
475 SERIALIZE_SCALAR(fileOffset
);
479 Process::FdMap::unserialize(Checkpoint
*cp
, const std::string
§ion
)
481 UNSERIALIZE_SCALAR(fd
);
482 UNSERIALIZE_SCALAR(isPipe
);
483 UNSERIALIZE_SCALAR(filename
);
484 UNSERIALIZE_SCALAR(flags
);
485 UNSERIALIZE_SCALAR(readPipeSource
);
486 UNSERIALIZE_SCALAR(fileOffset
);
490 Process::serialize(std::ostream
&os
)
492 SERIALIZE_SCALAR(initialContextLoaded
);
493 SERIALIZE_SCALAR(brk_point
);
494 SERIALIZE_SCALAR(stack_base
);
495 SERIALIZE_SCALAR(stack_size
);
496 SERIALIZE_SCALAR(stack_min
);
497 SERIALIZE_SCALAR(next_thread_stack_base
);
498 SERIALIZE_SCALAR(mmap_start
);
499 SERIALIZE_SCALAR(mmap_end
);
500 SERIALIZE_SCALAR(nxm_start
);
501 SERIALIZE_SCALAR(nxm_end
);
503 pTable
->serialize(os
);
504 for (int x
= 0; x
<= MAX_FD
; x
++) {
505 nameOut(os
, csprintf("%s.FdMap%d", name(), x
));
506 fd_map
[x
].serialize(os
);
512 Process::unserialize(Checkpoint
*cp
, const std::string
§ion
)
514 UNSERIALIZE_SCALAR(initialContextLoaded
);
515 UNSERIALIZE_SCALAR(brk_point
);
516 UNSERIALIZE_SCALAR(stack_base
);
517 UNSERIALIZE_SCALAR(stack_size
);
518 UNSERIALIZE_SCALAR(stack_min
);
519 UNSERIALIZE_SCALAR(next_thread_stack_base
);
520 UNSERIALIZE_SCALAR(mmap_start
);
521 UNSERIALIZE_SCALAR(mmap_end
);
522 UNSERIALIZE_SCALAR(nxm_start
);
523 UNSERIALIZE_SCALAR(nxm_end
);
524 pTable
->unserialize(cp
, section
);
525 for (int x
= 0; x
<= MAX_FD
; x
++) {
526 fd_map
[x
].unserialize(cp
, csprintf("%s.FdMap%d", section
, x
));
530 checkpointRestored
= true;
535 ////////////////////////////////////////////////////////////////////////
537 // LiveProcess member definitions
539 ////////////////////////////////////////////////////////////////////////
542 LiveProcess::LiveProcess(LiveProcessParams
* params
, ObjectFile
*_objFile
)
543 : Process(params
), objFile(_objFile
),
544 argv(params
->cmd
), envp(params
->env
), cwd(params
->cwd
)
547 __euid
= params
->euid
;
549 __egid
= params
->egid
;
551 __ppid
= params
->ppid
;
553 prog_fname
= params
->cmd
[0];
555 // load up symbols, if any... these may be used for debugging or
557 if (!debugSymbolTable
) {
558 debugSymbolTable
= new SymbolTable();
559 if (!objFile
->loadGlobalSymbols(debugSymbolTable
) ||
560 !objFile
->loadLocalSymbols(debugSymbolTable
)) {
561 // didn't load any symbols
562 delete debugSymbolTable
;
563 debugSymbolTable
= NULL
;
569 LiveProcess::argsInit(int intSize
, int pageSize
)
573 // load object file into target memory
574 objFile
->loadSections(initVirtMem
);
576 // Calculate how much space we need for arg & env arrays.
577 int argv_array_size
= intSize
* (argv
.size() + 1);
578 int envp_array_size
= intSize
* (envp
.size() + 1);
579 int arg_data_size
= 0;
580 for (vector
<string
>::size_type i
= 0; i
< argv
.size(); ++i
) {
581 arg_data_size
+= argv
[i
].size() + 1;
583 int env_data_size
= 0;
584 for (vector
<string
>::size_type i
= 0; i
< envp
.size(); ++i
) {
585 env_data_size
+= envp
[i
].size() + 1;
589 argv_array_size
+ envp_array_size
+ arg_data_size
+ env_data_size
;
590 if (space_needed
< 32*1024)
591 space_needed
= 32*1024;
593 // set bottom of stack
594 stack_min
= stack_base
- space_needed
;
596 stack_min
= roundDown(stack_min
, pageSize
);
597 stack_size
= stack_base
- stack_min
;
599 pTable
->allocate(stack_min
, roundUp(stack_size
, pageSize
));
601 // map out initial stack contents
602 Addr argv_array_base
= stack_min
+ intSize
; // room for argc
603 Addr envp_array_base
= argv_array_base
+ argv_array_size
;
604 Addr arg_data_base
= envp_array_base
+ envp_array_size
;
605 Addr env_data_base
= arg_data_base
+ arg_data_size
;
607 // write contents to stack
608 uint64_t argc
= argv
.size();
610 argc
= htog((uint64_t)argc
);
611 else if (intSize
== 4)
612 argc
= htog((uint32_t)argc
);
614 panic("Unknown int size");
616 initVirtMem
->writeBlob(stack_min
, (uint8_t*)&argc
, intSize
);
618 copyStringArray(argv
, argv_array_base
, arg_data_base
, initVirtMem
);
619 copyStringArray(envp
, envp_array_base
, env_data_base
, initVirtMem
);
621 ThreadContext
*tc
= system
->getThreadContext(contextIds
[0]);
623 setSyscallArg(tc
, 0, argc
);
624 setSyscallArg(tc
, 1, argv_array_base
);
625 tc
->setIntReg(StackPointerReg
, stack_min
);
627 Addr prog_entry
= objFile
->entryPoint();
628 tc
->setPC(prog_entry
);
629 tc
->setNextPC(prog_entry
+ sizeof(MachInst
));
631 #if THE_ISA != ALPHA_ISA && THE_ISA != POWER_ISA //e.g. MIPS or Sparc
632 tc
->setNextNPC(prog_entry
+ (2 * sizeof(MachInst
)));
639 LiveProcess::syscall(int64_t callnum
, ThreadContext
*tc
)
643 SyscallDesc
*desc
= getDesc(callnum
);
645 fatal("Syscall %d out of range", callnum
);
647 desc
->doSyscall(callnum
, this, tc
);
651 LiveProcess::create(LiveProcessParams
* params
)
653 LiveProcess
*process
= NULL
;
656 params
->executable
== "" ? params
->cmd
[0] : params
->executable
;
657 ObjectFile
*objFile
= createObjectFile(executable
);
658 if (objFile
== NULL
) {
659 fatal("Can't load object file %s", executable
);
662 if (objFile
->isDynamic())
663 fatal("Object file is a dynamic executable however only static "
664 "executables are supported!\n Please recompile your "
665 "executable as a static binary and try again.\n");
667 #if THE_ISA == ALPHA_ISA
668 if (objFile
->getArch() != ObjectFile::Alpha
)
669 fatal("Object file architecture does not match compiled ISA (Alpha).");
671 switch (objFile
->getOpSys()) {
672 case ObjectFile::Tru64
:
673 process
= new AlphaTru64Process(params
, objFile
);
676 case ObjectFile::UnknownOpSys
:
677 warn("Unknown operating system; assuming Linux.");
679 case ObjectFile::Linux
:
680 process
= new AlphaLinuxProcess(params
, objFile
);
684 fatal("Unknown/unsupported operating system.");
686 #elif THE_ISA == SPARC_ISA
687 if (objFile
->getArch() != ObjectFile::SPARC64
&&
688 objFile
->getArch() != ObjectFile::SPARC32
)
689 fatal("Object file architecture does not match compiled ISA (SPARC).");
690 switch (objFile
->getOpSys()) {
691 case ObjectFile::UnknownOpSys
:
692 warn("Unknown operating system; assuming Linux.");
694 case ObjectFile::Linux
:
695 if (objFile
->getArch() == ObjectFile::SPARC64
) {
696 process
= new Sparc64LinuxProcess(params
, objFile
);
698 process
= new Sparc32LinuxProcess(params
, objFile
);
703 case ObjectFile::Solaris
:
704 process
= new SparcSolarisProcess(params
, objFile
);
708 fatal("Unknown/unsupported operating system.");
710 #elif THE_ISA == X86_ISA
711 if (objFile
->getArch() != ObjectFile::X86_64
&&
712 objFile
->getArch() != ObjectFile::I386
)
713 fatal("Object file architecture does not match compiled ISA (x86).");
714 switch (objFile
->getOpSys()) {
715 case ObjectFile::UnknownOpSys
:
716 warn("Unknown operating system; assuming Linux.");
718 case ObjectFile::Linux
:
719 if (objFile
->getArch() == ObjectFile::X86_64
) {
720 process
= new X86_64LinuxProcess(params
, objFile
);
722 process
= new I386LinuxProcess(params
, objFile
);
727 fatal("Unknown/unsupported operating system.");
729 #elif THE_ISA == MIPS_ISA
730 if (objFile
->getArch() != ObjectFile::Mips
)
731 fatal("Object file architecture does not match compiled ISA (MIPS).");
732 switch (objFile
->getOpSys()) {
733 case ObjectFile::UnknownOpSys
:
734 warn("Unknown operating system; assuming Linux.");
736 case ObjectFile::Linux
:
737 process
= new MipsLinuxProcess(params
, objFile
);
741 fatal("Unknown/unsupported operating system.");
743 #elif THE_ISA == ARM_ISA
744 if (objFile
->getArch() != ObjectFile::Arm
)
745 fatal("Object file architecture does not match compiled ISA (ARM).");
746 switch (objFile
->getOpSys()) {
747 case ObjectFile::UnknownOpSys
:
748 warn("Unknown operating system; assuming Linux.");
750 case ObjectFile::Linux
:
751 process
= new ArmLinuxProcess(params
, objFile
);
753 case ObjectFile::LinuxArmOABI
:
754 fatal("M5 does not support ARM OABI binaries. Please recompile with an"
757 fatal("Unknown/unsupported operating system.");
759 #elif THE_ISA == POWER_ISA
760 if (objFile
->getArch() != ObjectFile::Power
)
761 fatal("Object file architecture does not match compiled ISA (Power).");
762 switch (objFile
->getOpSys()) {
763 case ObjectFile::UnknownOpSys
:
764 warn("Unknown operating system; assuming Linux.");
766 case ObjectFile::Linux
:
767 process
= new PowerLinuxProcess(params
, objFile
);
771 fatal("Unknown/unsupported operating system.");
774 #error "THE_ISA not set"
779 fatal("Unknown error creating process object.");
784 LiveProcessParams::create()
786 return LiveProcess::create(this);