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/process.hh"
50 #include "sim/process_impl.hh"
51 #include "sim/stats.hh"
52 #include "sim/syscall_emul.hh"
53 #include "sim/system.hh"
55 #include "arch/isa_specific.hh"
56 #if THE_ISA == ALPHA_ISA
57 #include "arch/alpha/linux/process.hh"
58 #include "arch/alpha/tru64/process.hh"
59 #elif THE_ISA == SPARC_ISA
60 #include "arch/sparc/linux/process.hh"
61 #include "arch/sparc/solaris/process.hh"
62 #elif THE_ISA == MIPS_ISA
63 #include "arch/mips/linux/process.hh"
64 #elif THE_ISA == X86_ISA
65 #include "arch/x86/linux/process.hh"
67 #error "THE_ISA not set"
72 using namespace TheISA
;
75 // The purpose of this code is to fake the loader & syscall mechanism
76 // when there's no OS: thus there's no resone to use it in FULL_SYSTEM
77 // mode when we do have an OS
80 #error "process.cc not compatible with FULL_SYSTEM"
83 // current number of allocated processes
84 int num_processes
= 0;
86 Process::Process(ProcessParams
* params
)
87 : SimObject(params
), system(params
->system
), checkpointRestored(false),
88 max_stack_size(params
->max_stack_size
)
90 string in
= params
->input
;
91 string out
= params
->output
;
93 // initialize file descriptors to default: same as simulator
94 int stdin_fd
, stdout_fd
, stderr_fd
;
96 if (in
== "stdin" || in
== "cin")
97 stdin_fd
= STDIN_FILENO
;
98 else if (in
== "None")
101 stdin_fd
= Process::openInputFile(in
);
103 if (out
== "stdout" || out
== "cout")
104 stdout_fd
= STDOUT_FILENO
;
105 else if (out
== "stderr" || out
== "cerr")
106 stdout_fd
= STDERR_FILENO
;
107 else if (out
== "None")
110 stdout_fd
= Process::openOutputFile(out
);
112 stderr_fd
= (stdout_fd
!= STDOUT_FILENO
) ? stdout_fd
: STDERR_FILENO
;
114 M5_pid
= system
->allocatePID();
115 // initialize first 3 fds (stdin, stdout, stderr)
116 Process::FdMap
*fdo
= &fd_map
[STDIN_FILENO
];
119 fdo
->flags
= O_RDONLY
;
123 fdo
= &fd_map
[STDOUT_FILENO
];
126 fdo
->flags
= O_WRONLY
| O_CREAT
| O_TRUNC
;
130 fdo
= &fd_map
[STDERR_FILENO
];
132 fdo
->filename
= "STDERR";
133 fdo
->flags
= O_WRONLY
;
138 // mark remaining fds as free
139 for (int i
= 3; i
<= MAX_FD
; ++i
) {
140 Process::FdMap
*fdo
= &fd_map
[i
];
144 mmap_start
= mmap_end
= 0;
145 nxm_start
= nxm_end
= 0;
146 pTable
= new PageTable(this);
147 // other parameters will be initialized when the program is loaded
154 using namespace Stats
;
157 .name(name() + ".PROG:num_syscalls")
158 .desc("Number of system calls")
163 // static helper functions
166 Process::openInputFile(const string
&filename
)
168 int fd
= open(filename
.c_str(), O_RDONLY
);
172 cerr
<< "unable to open \"" << filename
<< "\" for reading\n";
173 fatal("can't open input file");
181 Process::openOutputFile(const string
&filename
)
183 int fd
= open(filename
.c_str(), O_WRONLY
| O_CREAT
| O_TRUNC
, 0774);
187 cerr
<< "unable to open \"" << filename
<< "\" for writing\n";
188 fatal("can't open output file");
196 Process::registerThreadContext(ThreadContext
*tc
)
199 int myIndex
= threadContexts
.size();
200 threadContexts
.push_back(tc
);
202 RemoteGDB
*rgdb
= new RemoteGDB(system
, tc
);
203 GDBListener
*gdbl
= new GDBListener(rgdb
, 7000 + myIndex
);
207 remoteGDB
.push_back(rgdb
);
209 // return CPU number to caller
216 if (threadContexts
.empty())
217 fatal("Process %s is not associated with any CPUs!\n", name());
219 // first thread context for this process... initialize & enable
220 ThreadContext
*tc
= threadContexts
[0];
222 // mark this context as active so it will start ticking.
226 mem_port
= system
->physmem
->getPort("functional");
227 initVirtMem
= new TranslatingPort("process init port", this,
228 TranslatingPort::Always
);
229 mem_port
->setPeer(initVirtMem
);
230 initVirtMem
->setPeer(mem_port
);
234 Process::replaceThreadContext(ThreadContext
*tc
, int tcIndex
)
236 if (tcIndex
>= threadContexts
.size()) {
237 panic("replaceThreadContext: bad tcIndex, %d >= %d\n",
238 tcIndex
, threadContexts
.size());
241 threadContexts
[tcIndex
] = tc
;
244 // map simulator fd sim_fd to target fd tgt_fd
246 Process::dup_fd(int sim_fd
, int tgt_fd
)
248 if (tgt_fd
< 0 || tgt_fd
> MAX_FD
)
249 panic("Process::dup_fd tried to dup past MAX_FD (%d)", tgt_fd
);
251 Process::FdMap
*fdo
= &fd_map
[tgt_fd
];
256 // generate new target fd for sim_fd
258 Process::alloc_fd(int sim_fd
, string filename
, int flags
, int mode
, bool pipe
)
260 // in case open() returns an error, don't allocate a new fd
264 // find first free target fd
265 for (int free_fd
= 0; free_fd
<= MAX_FD
; ++free_fd
) {
266 Process::FdMap
*fdo
= &fd_map
[free_fd
];
269 fdo
->filename
= filename
;
274 fdo
->readPipeSource
= 0;
279 panic("Process::alloc_fd: out of file descriptors!");
283 // free target fd (e.g., after close)
285 Process::free_fd(int tgt_fd
)
287 Process::FdMap
*fdo
= &fd_map
[tgt_fd
];
289 warn("Process::free_fd: request to free unused fd %d", tgt_fd
);
292 fdo
->filename
= "NULL";
297 fdo
->readPipeSource
= 0;
301 // look up simulator fd for given target fd
303 Process::sim_fd(int tgt_fd
)
308 return fd_map
[tgt_fd
].fd
;
312 Process::sim_fd_obj(int tgt_fd
)
315 panic("sim_fd_obj called in fd out of range.");
317 return &fd_map
[tgt_fd
];
320 Process::checkAndAllocNextPage(Addr vaddr
)
322 // if this is an initial write we might not have
323 if (vaddr
>= stack_min
&& vaddr
< stack_base
) {
324 pTable
->allocate(roundDown(vaddr
, VMPageSize
), VMPageSize
);
328 // We've accessed the next page of the stack, so extend the stack
330 if (vaddr
< stack_min
&& vaddr
>= stack_base
- max_stack_size
) {
331 while (vaddr
< stack_min
) {
332 stack_min
-= TheISA::PageBytes
;
333 if(stack_base
- stack_min
> max_stack_size
)
334 fatal("Maximum stack size exceeded\n");
335 if(stack_base
- stack_min
> 8*1024*1024)
336 fatal("Over max stack size for one thread\n");
337 pTable
->allocate(stack_min
, TheISA::PageBytes
);
338 warn("Increasing stack size by one page.");
345 // find all offsets for currently open files and save them
347 Process::fix_file_offsets() {
348 Process::FdMap
*fdo_stdin
= &fd_map
[STDIN_FILENO
];
349 Process::FdMap
*fdo_stdout
= &fd_map
[STDOUT_FILENO
];
350 Process::FdMap
*fdo_stderr
= &fd_map
[STDERR_FILENO
];
351 string in
= fdo_stdin
->filename
;
352 string out
= fdo_stdout
->filename
;
354 // initialize file descriptors to default: same as simulator
355 int stdin_fd
, stdout_fd
, stderr_fd
;
357 if (in
== "stdin" || in
== "cin")
358 stdin_fd
= STDIN_FILENO
;
359 else if (in
== "None")
362 //OPEN standard in and seek to the right location
363 stdin_fd
= Process::openInputFile(in
);
364 if (lseek(stdin_fd
, fdo_stdin
->fileOffset
, SEEK_SET
) < 0)
365 panic("Unable to seek to correct location in file: %s", in
);
368 if (out
== "stdout" || out
== "cout")
369 stdout_fd
= STDOUT_FILENO
;
370 else if (out
== "stderr" || out
== "cerr")
371 stdout_fd
= STDERR_FILENO
;
372 else if (out
== "None")
375 stdout_fd
= Process::openOutputFile(out
);
376 if (lseek(stdin_fd
, fdo_stdout
->fileOffset
, SEEK_SET
) < 0)
377 panic("Unable to seek to correct in file: %s", out
);
380 stderr_fd
= (stdout_fd
!= STDOUT_FILENO
) ? stdout_fd
: STDERR_FILENO
;
382 fdo_stdin
->fd
= stdin_fd
;
383 fdo_stdout
->fd
= stdout_fd
;
384 fdo_stderr
->fd
= stderr_fd
;
387 for (int free_fd
= 3; free_fd
<= MAX_FD
; ++free_fd
) {
388 Process::FdMap
*fdo
= &fd_map
[free_fd
];
391 if (fdo
->filename
== "PIPE-WRITE")
394 assert (fdo
->filename
== "PIPE-READ");
397 int pipe_retval
= pipe(fds
);
399 if (pipe_retval
< 0) {
401 panic("Unable to create new pipe.");
403 fdo
->fd
= fds
[0]; //set read pipe
404 Process::FdMap
*fdo_write
= &fd_map
[fdo
->readPipeSource
];
405 if (fdo_write
->filename
!= "PIPE-WRITE")
406 panic ("Couldn't find write end of the pipe");
408 fdo_write
->fd
= fds
[1];//set write pipe
412 int fd
= open(fdo
->filename
.c_str(), fdo
->flags
, fdo
->mode
);
415 panic("Unable to open file: %s", fdo
->filename
);
418 //Seek to correct location before checkpoint
419 if (lseek(fd
,fdo
->fileOffset
, SEEK_SET
) < 0)
420 panic("Unable to seek to correct location in file: %s", fdo
->filename
);
426 Process::find_file_offsets(){
427 for (int free_fd
= 0; free_fd
<= MAX_FD
; ++free_fd
) {
428 Process::FdMap
*fdo
= &fd_map
[free_fd
];
430 fdo
->fileOffset
= lseek(fdo
->fd
, 0, SEEK_CUR
);
432 fdo
->filename
= "NULL";
439 Process::setReadPipeSource(int read_pipe_fd
, int source_fd
){
440 Process::FdMap
*fdo
= &fd_map
[read_pipe_fd
];
441 fdo
->readPipeSource
= source_fd
;
445 Process::FdMap::serialize(std::ostream
&os
)
447 SERIALIZE_SCALAR(fd
);
448 SERIALIZE_SCALAR(isPipe
);
449 SERIALIZE_SCALAR(filename
);
450 SERIALIZE_SCALAR(flags
);
451 SERIALIZE_SCALAR(readPipeSource
);
452 SERIALIZE_SCALAR(fileOffset
);
456 Process::FdMap::unserialize(Checkpoint
*cp
, const std::string
§ion
)
458 UNSERIALIZE_SCALAR(fd
);
459 UNSERIALIZE_SCALAR(isPipe
);
460 UNSERIALIZE_SCALAR(filename
);
461 UNSERIALIZE_SCALAR(flags
);
462 UNSERIALIZE_SCALAR(readPipeSource
);
463 UNSERIALIZE_SCALAR(fileOffset
);
467 Process::serialize(std::ostream
&os
)
469 SERIALIZE_SCALAR(initialContextLoaded
);
470 SERIALIZE_SCALAR(brk_point
);
471 SERIALIZE_SCALAR(stack_base
);
472 SERIALIZE_SCALAR(stack_size
);
473 SERIALIZE_SCALAR(stack_min
);
474 SERIALIZE_SCALAR(next_thread_stack_base
);
475 SERIALIZE_SCALAR(mmap_start
);
476 SERIALIZE_SCALAR(mmap_end
);
477 SERIALIZE_SCALAR(nxm_start
);
478 SERIALIZE_SCALAR(nxm_end
);
480 pTable
->serialize(os
);
481 for (int x
= 0; x
<= MAX_FD
; x
++) {
482 nameOut(os
, csprintf("%s.FdMap%d", name(), x
));
483 fd_map
[x
].serialize(os
);
489 Process::unserialize(Checkpoint
*cp
, const std::string
§ion
)
491 UNSERIALIZE_SCALAR(initialContextLoaded
);
492 UNSERIALIZE_SCALAR(brk_point
);
493 UNSERIALIZE_SCALAR(stack_base
);
494 UNSERIALIZE_SCALAR(stack_size
);
495 UNSERIALIZE_SCALAR(stack_min
);
496 UNSERIALIZE_SCALAR(next_thread_stack_base
);
497 UNSERIALIZE_SCALAR(mmap_start
);
498 UNSERIALIZE_SCALAR(mmap_end
);
499 UNSERIALIZE_SCALAR(nxm_start
);
500 UNSERIALIZE_SCALAR(nxm_end
);
501 pTable
->unserialize(cp
, section
);
502 for (int x
= 0; x
<= MAX_FD
; x
++) {
503 fd_map
[x
].unserialize(cp
, csprintf("%s.FdMap%d", section
, x
));
507 checkpointRestored
= true;
512 ////////////////////////////////////////////////////////////////////////
514 // LiveProcess member definitions
516 ////////////////////////////////////////////////////////////////////////
519 LiveProcess::LiveProcess(LiveProcessParams
* params
, ObjectFile
*_objFile
)
520 : Process(params
), objFile(_objFile
),
521 argv(params
->cmd
), envp(params
->env
), cwd(params
->cwd
)
524 __euid
= params
->euid
;
526 __egid
= params
->egid
;
528 __ppid
= params
->ppid
;
530 prog_fname
= params
->cmd
[0];
532 // load up symbols, if any... these may be used for debugging or
534 if (!debugSymbolTable
) {
535 debugSymbolTable
= new SymbolTable();
536 if (!objFile
->loadGlobalSymbols(debugSymbolTable
) ||
537 !objFile
->loadLocalSymbols(debugSymbolTable
)) {
538 // didn't load any symbols
539 delete debugSymbolTable
;
540 debugSymbolTable
= NULL
;
546 LiveProcess::argsInit(int intSize
, int pageSize
)
550 // load object file into target memory
551 objFile
->loadSections(initVirtMem
);
553 // Calculate how much space we need for arg & env arrays.
554 int argv_array_size
= intSize
* (argv
.size() + 1);
555 int envp_array_size
= intSize
* (envp
.size() + 1);
556 int arg_data_size
= 0;
557 for (int i
= 0; i
< argv
.size(); ++i
) {
558 arg_data_size
+= argv
[i
].size() + 1;
560 int env_data_size
= 0;
561 for (int i
= 0; i
< envp
.size(); ++i
) {
562 env_data_size
+= envp
[i
].size() + 1;
566 argv_array_size
+ envp_array_size
+ arg_data_size
+ env_data_size
;
567 if (space_needed
< 32*1024)
568 space_needed
= 32*1024;
570 // set bottom of stack
571 stack_min
= stack_base
- space_needed
;
573 stack_min
= roundDown(stack_min
, pageSize
);
574 stack_size
= stack_base
- stack_min
;
576 pTable
->allocate(stack_min
, roundUp(stack_size
, pageSize
));
578 // map out initial stack contents
579 Addr argv_array_base
= stack_min
+ intSize
; // room for argc
580 Addr envp_array_base
= argv_array_base
+ argv_array_size
;
581 Addr arg_data_base
= envp_array_base
+ envp_array_size
;
582 Addr env_data_base
= arg_data_base
+ arg_data_size
;
584 // write contents to stack
585 uint64_t argc
= argv
.size();
587 argc
= htog((uint64_t)argc
);
588 else if (intSize
== 4)
589 argc
= htog((uint32_t)argc
);
591 panic("Unknown int size");
593 initVirtMem
->writeBlob(stack_min
, (uint8_t*)&argc
, intSize
);
595 copyStringArray(argv
, argv_array_base
, arg_data_base
, initVirtMem
);
596 copyStringArray(envp
, envp_array_base
, env_data_base
, initVirtMem
);
598 assert(NumArgumentRegs
>= 2);
599 threadContexts
[0]->setIntReg(ArgumentReg
[0], argc
);
600 threadContexts
[0]->setIntReg(ArgumentReg
[1], argv_array_base
);
601 threadContexts
[0]->setIntReg(StackPointerReg
, stack_min
);
603 Addr prog_entry
= objFile
->entryPoint();
604 threadContexts
[0]->setPC(prog_entry
);
605 threadContexts
[0]->setNextPC(prog_entry
+ sizeof(MachInst
));
607 #if THE_ISA != ALPHA_ISA //e.g. MIPS or Sparc
608 threadContexts
[0]->setNextNPC(prog_entry
+ (2 * sizeof(MachInst
)));
615 LiveProcess::syscall(int64_t callnum
, ThreadContext
*tc
)
619 SyscallDesc
*desc
= getDesc(callnum
);
621 fatal("Syscall %d out of range", callnum
);
623 desc
->doSyscall(callnum
, this, tc
);
627 LiveProcess::create(LiveProcessParams
* params
)
629 LiveProcess
*process
= NULL
;
632 params
->executable
== "" ? params
->cmd
[0] : params
->executable
;
633 ObjectFile
*objFile
= createObjectFile(executable
);
634 if (objFile
== NULL
) {
635 fatal("Can't load object file %s", executable
);
638 if (objFile
->isDynamic())
639 fatal("Object file is a dynamic executable however only static "
640 "executables are supported!\n Please recompile your "
641 "executable as a static binary and try again.\n");
643 #if THE_ISA == ALPHA_ISA
644 if (objFile
->hasTLS())
645 fatal("Object file has a TLS section and single threaded TLS is not\n"
646 " currently supported for Alpha! Please recompile your "
647 "executable with \n a non-TLS toolchain.\n");
649 if (objFile
->getArch() != ObjectFile::Alpha
)
650 fatal("Object file architecture does not match compiled ISA (Alpha).");
651 switch (objFile
->getOpSys()) {
652 case ObjectFile::Tru64
:
653 process
= new AlphaTru64Process(params
, objFile
);
656 case ObjectFile::Linux
:
657 process
= new AlphaLinuxProcess(params
, objFile
);
661 fatal("Unknown/unsupported operating system.");
663 #elif THE_ISA == SPARC_ISA
664 if (objFile
->getArch() != ObjectFile::SPARC64
&& objFile
->getArch() != ObjectFile::SPARC32
)
665 fatal("Object file architecture does not match compiled ISA (SPARC).");
666 switch (objFile
->getOpSys()) {
667 case ObjectFile::Linux
:
668 if (objFile
->getArch() == ObjectFile::SPARC64
) {
669 process
= new Sparc64LinuxProcess(params
, objFile
);
671 process
= new Sparc32LinuxProcess(params
, objFile
);
676 case ObjectFile::Solaris
:
677 process
= new SparcSolarisProcess(params
, objFile
);
680 fatal("Unknown/unsupported operating system.");
682 #elif THE_ISA == X86_ISA
683 if (objFile
->getArch() != ObjectFile::X86
)
684 fatal("Object file architecture does not match compiled ISA (x86).");
685 switch (objFile
->getOpSys()) {
686 case ObjectFile::Linux
:
687 process
= new X86LinuxProcess(params
, objFile
);
690 fatal("Unknown/unsupported operating system.");
692 #elif THE_ISA == MIPS_ISA
693 if (objFile
->getArch() != ObjectFile::Mips
)
694 fatal("Object file architecture does not match compiled ISA (MIPS).");
695 switch (objFile
->getOpSys()) {
696 case ObjectFile::Linux
:
697 process
= new MipsLinuxProcess(params
, objFile
);
701 fatal("Unknown/unsupported operating system.");
704 #error "THE_ISA not set"
709 fatal("Unknown error creating process object.");
714 LiveProcessParams::create()
716 return LiveProcess::create(this);