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::Halted
) {
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 ThreadContext
*tc
= system
->getThreadContext(contextIds
[0]);
621 setSyscallArg(tc
, 0, argc
);
622 setSyscallArg(tc
, 1, argv_array_base
);
623 tc
->setIntReg(StackPointerReg
, stack_min
);
625 Addr prog_entry
= objFile
->entryPoint();
626 tc
->setPC(prog_entry
);
627 tc
->setNextPC(prog_entry
+ sizeof(MachInst
));
629 #if THE_ISA != ALPHA_ISA //e.g. MIPS or Sparc
630 tc
->setNextNPC(prog_entry
+ (2 * sizeof(MachInst
)));
637 LiveProcess::syscall(int64_t callnum
, ThreadContext
*tc
)
641 SyscallDesc
*desc
= getDesc(callnum
);
643 fatal("Syscall %d out of range", callnum
);
645 desc
->doSyscall(callnum
, this, tc
);
649 LiveProcess::create(LiveProcessParams
* params
)
651 LiveProcess
*process
= NULL
;
654 params
->executable
== "" ? params
->cmd
[0] : params
->executable
;
655 ObjectFile
*objFile
= createObjectFile(executable
);
656 if (objFile
== NULL
) {
657 fatal("Can't load object file %s", executable
);
660 if (objFile
->isDynamic())
661 fatal("Object file is a dynamic executable however only static "
662 "executables are supported!\n Please recompile your "
663 "executable as a static binary and try again.\n");
665 #if THE_ISA == ALPHA_ISA
666 if (objFile
->getArch() != ObjectFile::Alpha
)
667 fatal("Object file architecture does not match compiled ISA (Alpha).");
669 switch (objFile
->getOpSys()) {
670 case ObjectFile::Tru64
:
671 process
= new AlphaTru64Process(params
, objFile
);
674 case ObjectFile::UnknownOpSys
:
675 warn("Unknown operating system; assuming Linux.");
677 case ObjectFile::Linux
:
678 process
= new AlphaLinuxProcess(params
, objFile
);
682 fatal("Unknown/unsupported operating system.");
684 #elif THE_ISA == SPARC_ISA
685 if (objFile
->getArch() != ObjectFile::SPARC64
&&
686 objFile
->getArch() != ObjectFile::SPARC32
)
687 fatal("Object file architecture does not match compiled ISA (SPARC).");
688 switch (objFile
->getOpSys()) {
689 case ObjectFile::UnknownOpSys
:
690 warn("Unknown operating system; assuming Linux.");
692 case ObjectFile::Linux
:
693 if (objFile
->getArch() == ObjectFile::SPARC64
) {
694 process
= new Sparc64LinuxProcess(params
, objFile
);
696 process
= new Sparc32LinuxProcess(params
, objFile
);
701 case ObjectFile::Solaris
:
702 process
= new SparcSolarisProcess(params
, objFile
);
706 fatal("Unknown/unsupported operating system.");
708 #elif THE_ISA == X86_ISA
709 if (objFile
->getArch() != ObjectFile::X86_64
&&
710 objFile
->getArch() != ObjectFile::I386
)
711 fatal("Object file architecture does not match compiled ISA (x86).");
712 switch (objFile
->getOpSys()) {
713 case ObjectFile::UnknownOpSys
:
714 warn("Unknown operating system; assuming Linux.");
716 case ObjectFile::Linux
:
717 if (objFile
->getArch() == ObjectFile::X86_64
) {
718 process
= new X86_64LinuxProcess(params
, objFile
);
720 process
= new I386LinuxProcess(params
, objFile
);
725 fatal("Unknown/unsupported operating system.");
727 #elif THE_ISA == MIPS_ISA
728 if (objFile
->getArch() != ObjectFile::Mips
)
729 fatal("Object file architecture does not match compiled ISA (MIPS).");
730 switch (objFile
->getOpSys()) {
731 case ObjectFile::UnknownOpSys
:
732 warn("Unknown operating system; assuming Linux.");
734 case ObjectFile::Linux
:
735 process
= new MipsLinuxProcess(params
, objFile
);
739 fatal("Unknown/unsupported operating system.");
741 #elif THE_ISA == ARM_ISA
742 if (objFile
->getArch() != ObjectFile::Arm
)
743 fatal("Object file architecture does not match compiled ISA (ARM).");
744 switch (objFile
->getOpSys()) {
745 case ObjectFile::UnknownOpSys
:
746 warn("Unknown operating system; assuming Linux.");
748 case ObjectFile::Linux
:
749 process
= new ArmLinuxProcess(params
, objFile
);
753 fatal("Unknown/unsupported operating system.");
756 #error "THE_ISA not set"
761 fatal("Unknown error creating process object.");
766 LiveProcessParams::create()
768 return LiveProcess::create(this);