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 "cpu/thread_context.hh"
44 #include "mem/page_table.hh"
45 #include "mem/physical.hh"
46 #include "mem/translating_port.hh"
47 #include "params/Process.hh"
48 #include "params/LiveProcess.hh"
49 #include "sim/debug.hh"
50 #include "sim/process.hh"
51 #include "sim/process_impl.hh"
52 #include "sim/stats.hh"
53 #include "sim/syscall_emul.hh"
54 #include "sim/system.hh"
56 #include "arch/isa_specific.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"
70 #error "THE_ISA not set"
75 using namespace TheISA
;
78 // The purpose of this code is to fake the loader & syscall mechanism
79 // when there's no OS: thus there's no resone to use it in FULL_SYSTEM
80 // mode when we do have an OS
83 #error "process.cc not compatible with FULL_SYSTEM"
86 // current number of allocated processes
87 int num_processes
= 0;
89 template<class IntType
>
90 AuxVector
<IntType
>::AuxVector(IntType type
, IntType val
)
92 a_type
= TheISA::htog(type
);
93 a_val
= TheISA::htog(val
);
96 template class AuxVector
<uint32_t>;
97 template class AuxVector
<uint64_t>;
99 Process::Process(ProcessParams
* params
)
100 : SimObject(params
), system(params
->system
), checkpointRestored(false),
101 max_stack_size(params
->max_stack_size
)
103 string in
= params
->input
;
104 string out
= params
->output
;
105 string err
= params
->errout
;
107 // initialize file descriptors to default: same as simulator
108 int stdin_fd
, stdout_fd
, stderr_fd
;
110 if (in
== "stdin" || in
== "cin")
111 stdin_fd
= STDIN_FILENO
;
112 else if (in
== "None")
115 stdin_fd
= Process::openInputFile(in
);
117 if (out
== "stdout" || out
== "cout")
118 stdout_fd
= STDOUT_FILENO
;
119 else if (out
== "stderr" || out
== "cerr")
120 stdout_fd
= STDERR_FILENO
;
121 else if (out
== "None")
124 stdout_fd
= Process::openOutputFile(out
);
126 if (err
== "stdout" || err
== "cout")
127 stderr_fd
= STDOUT_FILENO
;
128 else if (err
== "stderr" || err
== "cerr")
129 stderr_fd
= STDERR_FILENO
;
130 else if (err
== "None")
133 stderr_fd
= stdout_fd
;
135 stderr_fd
= Process::openOutputFile(err
);
137 M5_pid
= system
->allocatePID();
138 // initialize first 3 fds (stdin, stdout, stderr)
139 Process::FdMap
*fdo
= &fd_map
[STDIN_FILENO
];
142 fdo
->flags
= O_RDONLY
;
146 fdo
= &fd_map
[STDOUT_FILENO
];
149 fdo
->flags
= O_WRONLY
| O_CREAT
| O_TRUNC
;
153 fdo
= &fd_map
[STDERR_FILENO
];
156 fdo
->flags
= O_WRONLY
;
161 // mark remaining fds as free
162 for (int i
= 3; i
<= MAX_FD
; ++i
) {
163 Process::FdMap
*fdo
= &fd_map
[i
];
167 mmap_start
= mmap_end
= 0;
168 nxm_start
= nxm_end
= 0;
169 pTable
= new PageTable(this);
170 // other parameters will be initialized when the program is loaded
177 using namespace Stats
;
180 .name(name() + ".PROG:num_syscalls")
181 .desc("Number of system calls")
186 // static helper functions
189 Process::openInputFile(const string
&filename
)
191 int fd
= open(filename
.c_str(), O_RDONLY
);
195 cerr
<< "unable to open \"" << filename
<< "\" for reading\n";
196 fatal("can't open input file");
204 Process::openOutputFile(const string
&filename
)
206 int fd
= open(filename
.c_str(), O_WRONLY
| O_CREAT
| O_TRUNC
, 0664);
210 cerr
<< "unable to open \"" << filename
<< "\" for writing\n";
211 fatal("can't open output file");
218 Process::findFreeContext()
220 int size
= contextIds
.size();
222 for (int i
= 0; i
< size
; ++i
) {
223 tc
= system
->getThreadContext(contextIds
[i
]);
224 if (tc
->status() == ThreadContext::Unallocated
) {
225 // inactive context, free to use
235 if (contextIds
.empty())
236 fatal("Process %s is not associated with any HW contexts!\n", name());
238 // first thread context for this process... initialize & enable
239 ThreadContext
*tc
= system
->getThreadContext(contextIds
[0]);
241 // mark this context as active so it will start ticking.
245 mem_port
= system
->physmem
->getPort("functional");
246 initVirtMem
= new TranslatingPort("process init port", this,
247 TranslatingPort::Always
);
248 mem_port
->setPeer(initVirtMem
);
249 initVirtMem
->setPeer(mem_port
);
252 // map simulator fd sim_fd to target fd tgt_fd
254 Process::dup_fd(int sim_fd
, int tgt_fd
)
256 if (tgt_fd
< 0 || tgt_fd
> MAX_FD
)
257 panic("Process::dup_fd tried to dup past MAX_FD (%d)", tgt_fd
);
259 Process::FdMap
*fdo
= &fd_map
[tgt_fd
];
264 // generate new target fd for sim_fd
266 Process::alloc_fd(int sim_fd
, string filename
, int flags
, int mode
, bool pipe
)
268 // in case open() returns an error, don't allocate a new fd
272 // find first free target fd
273 for (int free_fd
= 0; free_fd
<= MAX_FD
; ++free_fd
) {
274 Process::FdMap
*fdo
= &fd_map
[free_fd
];
277 fdo
->filename
= filename
;
282 fdo
->readPipeSource
= 0;
287 panic("Process::alloc_fd: out of file descriptors!");
291 // free target fd (e.g., after close)
293 Process::free_fd(int tgt_fd
)
295 Process::FdMap
*fdo
= &fd_map
[tgt_fd
];
297 warn("Process::free_fd: request to free unused fd %d", tgt_fd
);
300 fdo
->filename
= "NULL";
305 fdo
->readPipeSource
= 0;
309 // look up simulator fd for given target fd
311 Process::sim_fd(int tgt_fd
)
316 return fd_map
[tgt_fd
].fd
;
320 Process::sim_fd_obj(int tgt_fd
)
323 panic("sim_fd_obj called in fd out of range.");
325 return &fd_map
[tgt_fd
];
328 Process::checkAndAllocNextPage(Addr vaddr
)
330 // if this is an initial write we might not have
331 if (vaddr
>= stack_min
&& vaddr
< stack_base
) {
332 pTable
->allocate(roundDown(vaddr
, VMPageSize
), VMPageSize
);
336 // We've accessed the next page of the stack, so extend the stack
338 if (vaddr
< stack_min
&& vaddr
>= stack_base
- max_stack_size
) {
339 while (vaddr
< stack_min
) {
340 stack_min
-= TheISA::PageBytes
;
341 if(stack_base
- stack_min
> max_stack_size
)
342 fatal("Maximum stack size exceeded\n");
343 if(stack_base
- stack_min
> 8*1024*1024)
344 fatal("Over max stack size for one thread\n");
345 pTable
->allocate(stack_min
, TheISA::PageBytes
);
346 inform("Increasing stack size by one page.");
353 // find all offsets for currently open files and save them
355 Process::fix_file_offsets() {
356 Process::FdMap
*fdo_stdin
= &fd_map
[STDIN_FILENO
];
357 Process::FdMap
*fdo_stdout
= &fd_map
[STDOUT_FILENO
];
358 Process::FdMap
*fdo_stderr
= &fd_map
[STDERR_FILENO
];
359 string in
= fdo_stdin
->filename
;
360 string out
= fdo_stdout
->filename
;
361 string err
= fdo_stderr
->filename
;
363 // initialize file descriptors to default: same as simulator
364 int stdin_fd
, stdout_fd
, stderr_fd
;
366 if (in
== "stdin" || in
== "cin")
367 stdin_fd
= STDIN_FILENO
;
368 else if (in
== "None")
371 //OPEN standard in and seek to the right location
372 stdin_fd
= Process::openInputFile(in
);
373 if (lseek(stdin_fd
, fdo_stdin
->fileOffset
, SEEK_SET
) < 0)
374 panic("Unable to seek to correct location in file: %s", in
);
377 if (out
== "stdout" || out
== "cout")
378 stdout_fd
= STDOUT_FILENO
;
379 else if (out
== "stderr" || out
== "cerr")
380 stdout_fd
= STDERR_FILENO
;
381 else if (out
== "None")
384 stdout_fd
= Process::openOutputFile(out
);
385 if (lseek(stdout_fd
, fdo_stdout
->fileOffset
, SEEK_SET
) < 0)
386 panic("Unable to seek to correct location in file: %s", out
);
389 if (err
== "stdout" || err
== "cout")
390 stderr_fd
= STDOUT_FILENO
;
391 else if (err
== "stderr" || err
== "cerr")
392 stderr_fd
= STDERR_FILENO
;
393 else if (err
== "None")
396 stderr_fd
= stdout_fd
;
398 stderr_fd
= Process::openOutputFile(err
);
399 if (lseek(stderr_fd
, fdo_stderr
->fileOffset
, SEEK_SET
) < 0)
400 panic("Unable to seek to correct location in file: %s", err
);
403 fdo_stdin
->fd
= stdin_fd
;
404 fdo_stdout
->fd
= stdout_fd
;
405 fdo_stderr
->fd
= stderr_fd
;
408 for (int free_fd
= 3; free_fd
<= MAX_FD
; ++free_fd
) {
409 Process::FdMap
*fdo
= &fd_map
[free_fd
];
412 if (fdo
->filename
== "PIPE-WRITE")
415 assert (fdo
->filename
== "PIPE-READ");
418 int pipe_retval
= pipe(fds
);
420 if (pipe_retval
< 0) {
422 panic("Unable to create new pipe.");
424 fdo
->fd
= fds
[0]; //set read pipe
425 Process::FdMap
*fdo_write
= &fd_map
[fdo
->readPipeSource
];
426 if (fdo_write
->filename
!= "PIPE-WRITE")
427 panic ("Couldn't find write end of the pipe");
429 fdo_write
->fd
= fds
[1];//set write pipe
433 int fd
= open(fdo
->filename
.c_str(), fdo
->flags
, fdo
->mode
);
436 panic("Unable to open file: %s", fdo
->filename
);
439 //Seek to correct location before checkpoint
440 if (lseek(fd
,fdo
->fileOffset
, SEEK_SET
) < 0)
441 panic("Unable to seek to correct location in file: %s", fdo
->filename
);
447 Process::find_file_offsets(){
448 for (int free_fd
= 0; free_fd
<= MAX_FD
; ++free_fd
) {
449 Process::FdMap
*fdo
= &fd_map
[free_fd
];
451 fdo
->fileOffset
= lseek(fdo
->fd
, 0, SEEK_CUR
);
453 fdo
->filename
= "NULL";
460 Process::setReadPipeSource(int read_pipe_fd
, int source_fd
){
461 Process::FdMap
*fdo
= &fd_map
[read_pipe_fd
];
462 fdo
->readPipeSource
= source_fd
;
466 Process::FdMap::serialize(std::ostream
&os
)
468 SERIALIZE_SCALAR(fd
);
469 SERIALIZE_SCALAR(isPipe
);
470 SERIALIZE_SCALAR(filename
);
471 SERIALIZE_SCALAR(flags
);
472 SERIALIZE_SCALAR(readPipeSource
);
473 SERIALIZE_SCALAR(fileOffset
);
477 Process::FdMap::unserialize(Checkpoint
*cp
, const std::string
§ion
)
479 UNSERIALIZE_SCALAR(fd
);
480 UNSERIALIZE_SCALAR(isPipe
);
481 UNSERIALIZE_SCALAR(filename
);
482 UNSERIALIZE_SCALAR(flags
);
483 UNSERIALIZE_SCALAR(readPipeSource
);
484 UNSERIALIZE_SCALAR(fileOffset
);
488 Process::serialize(std::ostream
&os
)
490 SERIALIZE_SCALAR(initialContextLoaded
);
491 SERIALIZE_SCALAR(brk_point
);
492 SERIALIZE_SCALAR(stack_base
);
493 SERIALIZE_SCALAR(stack_size
);
494 SERIALIZE_SCALAR(stack_min
);
495 SERIALIZE_SCALAR(next_thread_stack_base
);
496 SERIALIZE_SCALAR(mmap_start
);
497 SERIALIZE_SCALAR(mmap_end
);
498 SERIALIZE_SCALAR(nxm_start
);
499 SERIALIZE_SCALAR(nxm_end
);
501 pTable
->serialize(os
);
502 for (int x
= 0; x
<= MAX_FD
; x
++) {
503 nameOut(os
, csprintf("%s.FdMap%d", name(), x
));
504 fd_map
[x
].serialize(os
);
510 Process::unserialize(Checkpoint
*cp
, const std::string
§ion
)
512 UNSERIALIZE_SCALAR(initialContextLoaded
);
513 UNSERIALIZE_SCALAR(brk_point
);
514 UNSERIALIZE_SCALAR(stack_base
);
515 UNSERIALIZE_SCALAR(stack_size
);
516 UNSERIALIZE_SCALAR(stack_min
);
517 UNSERIALIZE_SCALAR(next_thread_stack_base
);
518 UNSERIALIZE_SCALAR(mmap_start
);
519 UNSERIALIZE_SCALAR(mmap_end
);
520 UNSERIALIZE_SCALAR(nxm_start
);
521 UNSERIALIZE_SCALAR(nxm_end
);
522 pTable
->unserialize(cp
, section
);
523 for (int x
= 0; x
<= MAX_FD
; x
++) {
524 fd_map
[x
].unserialize(cp
, csprintf("%s.FdMap%d", section
, x
));
528 checkpointRestored
= true;
533 ////////////////////////////////////////////////////////////////////////
535 // LiveProcess member definitions
537 ////////////////////////////////////////////////////////////////////////
540 LiveProcess::LiveProcess(LiveProcessParams
* params
, ObjectFile
*_objFile
)
541 : Process(params
), objFile(_objFile
),
542 argv(params
->cmd
), envp(params
->env
), cwd(params
->cwd
)
545 __euid
= params
->euid
;
547 __egid
= params
->egid
;
549 __ppid
= params
->ppid
;
551 prog_fname
= params
->cmd
[0];
553 // load up symbols, if any... these may be used for debugging or
555 if (!debugSymbolTable
) {
556 debugSymbolTable
= new SymbolTable();
557 if (!objFile
->loadGlobalSymbols(debugSymbolTable
) ||
558 !objFile
->loadLocalSymbols(debugSymbolTable
)) {
559 // didn't load any symbols
560 delete debugSymbolTable
;
561 debugSymbolTable
= NULL
;
567 LiveProcess::argsInit(int intSize
, int pageSize
)
571 // load object file into target memory
572 objFile
->loadSections(initVirtMem
);
574 // Calculate how much space we need for arg & env arrays.
575 int argv_array_size
= intSize
* (argv
.size() + 1);
576 int envp_array_size
= intSize
* (envp
.size() + 1);
577 int arg_data_size
= 0;
578 for (int i
= 0; i
< argv
.size(); ++i
) {
579 arg_data_size
+= argv
[i
].size() + 1;
581 int env_data_size
= 0;
582 for (int i
= 0; i
< envp
.size(); ++i
) {
583 env_data_size
+= envp
[i
].size() + 1;
587 argv_array_size
+ envp_array_size
+ arg_data_size
+ env_data_size
;
588 if (space_needed
< 32*1024)
589 space_needed
= 32*1024;
591 // set bottom of stack
592 stack_min
= stack_base
- space_needed
;
594 stack_min
= roundDown(stack_min
, pageSize
);
595 stack_size
= stack_base
- stack_min
;
597 pTable
->allocate(stack_min
, roundUp(stack_size
, pageSize
));
599 // map out initial stack contents
600 Addr argv_array_base
= stack_min
+ intSize
; // room for argc
601 Addr envp_array_base
= argv_array_base
+ argv_array_size
;
602 Addr arg_data_base
= envp_array_base
+ envp_array_size
;
603 Addr env_data_base
= arg_data_base
+ arg_data_size
;
605 // write contents to stack
606 uint64_t argc
= argv
.size();
608 argc
= htog((uint64_t)argc
);
609 else if (intSize
== 4)
610 argc
= htog((uint32_t)argc
);
612 panic("Unknown int size");
614 initVirtMem
->writeBlob(stack_min
, (uint8_t*)&argc
, intSize
);
616 copyStringArray(argv
, argv_array_base
, arg_data_base
, initVirtMem
);
617 copyStringArray(envp
, envp_array_base
, env_data_base
, initVirtMem
);
619 assert(NumArgumentRegs
>= 2);
621 ThreadContext
*tc
= system
->getThreadContext(contextIds
[0]);
623 tc
->setIntReg(ArgumentReg
[0], argc
);
624 tc
->setIntReg(ArgumentReg
[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 //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
)
712 fatal("Object file architecture does not match compiled ISA (x86).");
713 switch (objFile
->getOpSys()) {
714 case ObjectFile::UnknownOpSys
:
715 warn("Unknown operating system; assuming Linux.");
717 case ObjectFile::Linux
:
718 process
= new X86LinuxProcess(params
, objFile
);
722 fatal("Unknown/unsupported operating system.");
724 #elif THE_ISA == MIPS_ISA
725 if (objFile
->getArch() != ObjectFile::Mips
)
726 fatal("Object file architecture does not match compiled ISA (MIPS).");
727 switch (objFile
->getOpSys()) {
728 case ObjectFile::UnknownOpSys
:
729 warn("Unknown operating system; assuming Linux.");
731 case ObjectFile::Linux
:
732 process
= new MipsLinuxProcess(params
, objFile
);
736 fatal("Unknown/unsupported operating system.");
738 #elif THE_ISA == ARM_ISA
739 if (objFile
->getArch() != ObjectFile::Arm
)
740 fatal("Object file architecture does not match compiled ISA (ARM).");
741 switch (objFile
->getOpSys()) {
742 case ObjectFile::UnknownOpSys
:
743 warn("Unknown operating system; assuming Linux.");
745 case ObjectFile::Linux
:
746 process
= new ArmLinuxProcess(params
, objFile
);
750 fatal("Unknown/unsupported operating system.");
753 #error "THE_ISA not set"
758 fatal("Unknown error creating process object.");
763 LiveProcessParams::create()
765 return LiveProcess::create(this);