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 == ARM_ISA
65 #include "arch/arm/linux/process.hh"
66 #elif THE_ISA == X86_ISA
67 #include "arch/x86/linux/process.hh"
69 #error "THE_ISA not set"
74 using namespace TheISA
;
77 // The purpose of this code is to fake the loader & syscall mechanism
78 // when there's no OS: thus there's no resone to use it in FULL_SYSTEM
79 // mode when we do have an OS
82 #error "process.cc not compatible with FULL_SYSTEM"
85 // current number of allocated processes
86 int num_processes
= 0;
88 Process::Process(ProcessParams
* params
)
89 : SimObject(params
), system(params
->system
), checkpointRestored(false),
90 max_stack_size(params
->max_stack_size
)
92 string in
= params
->input
;
93 string out
= params
->output
;
95 // initialize file descriptors to default: same as simulator
96 int stdin_fd
, stdout_fd
, stderr_fd
;
98 if (in
== "stdin" || in
== "cin")
99 stdin_fd
= STDIN_FILENO
;
100 else if (in
== "None")
103 stdin_fd
= Process::openInputFile(in
);
105 if (out
== "stdout" || out
== "cout")
106 stdout_fd
= STDOUT_FILENO
;
107 else if (out
== "stderr" || out
== "cerr")
108 stdout_fd
= STDERR_FILENO
;
109 else if (out
== "None")
112 stdout_fd
= Process::openOutputFile(out
);
114 stderr_fd
= (stdout_fd
!= STDOUT_FILENO
) ? stdout_fd
: STDERR_FILENO
;
116 M5_pid
= system
->allocatePID();
117 // initialize first 3 fds (stdin, stdout, stderr)
118 Process::FdMap
*fdo
= &fd_map
[STDIN_FILENO
];
121 fdo
->flags
= O_RDONLY
;
125 fdo
= &fd_map
[STDOUT_FILENO
];
128 fdo
->flags
= O_WRONLY
| O_CREAT
| O_TRUNC
;
132 fdo
= &fd_map
[STDERR_FILENO
];
134 fdo
->filename
= "STDERR";
135 fdo
->flags
= O_WRONLY
;
140 // mark remaining fds as free
141 for (int i
= 3; i
<= MAX_FD
; ++i
) {
142 Process::FdMap
*fdo
= &fd_map
[i
];
146 mmap_start
= mmap_end
= 0;
147 nxm_start
= nxm_end
= 0;
148 pTable
= new PageTable(this);
149 // other parameters will be initialized when the program is loaded
156 using namespace Stats
;
159 .name(name() + ".PROG:num_syscalls")
160 .desc("Number of system calls")
165 // static helper functions
168 Process::openInputFile(const string
&filename
)
170 int fd
= open(filename
.c_str(), O_RDONLY
);
174 cerr
<< "unable to open \"" << filename
<< "\" for reading\n";
175 fatal("can't open input file");
183 Process::openOutputFile(const string
&filename
)
185 int fd
= open(filename
.c_str(), O_WRONLY
| O_CREAT
| O_TRUNC
, 0774);
189 cerr
<< "unable to open \"" << filename
<< "\" for writing\n";
190 fatal("can't open output file");
198 Process::registerThreadContext(ThreadContext
*tc
)
201 int myIndex
= threadContexts
.size();
202 threadContexts
.push_back(tc
);
204 RemoteGDB
*rgdb
= new RemoteGDB(system
, tc
);
205 GDBListener
*gdbl
= new GDBListener(rgdb
, 7000 + myIndex
);
209 remoteGDB
.push_back(rgdb
);
211 // return CPU number to caller
218 if (threadContexts
.empty())
219 fatal("Process %s is not associated with any CPUs!\n", name());
221 // first thread context for this process... initialize & enable
222 ThreadContext
*tc
= threadContexts
[0];
224 // mark this context as active so it will start ticking.
228 mem_port
= system
->physmem
->getPort("functional");
229 initVirtMem
= new TranslatingPort("process init port", this,
230 TranslatingPort::Always
);
231 mem_port
->setPeer(initVirtMem
);
232 initVirtMem
->setPeer(mem_port
);
236 Process::replaceThreadContext(ThreadContext
*tc
, int tcIndex
)
238 if (tcIndex
>= threadContexts
.size()) {
239 panic("replaceThreadContext: bad tcIndex, %d >= %d\n",
240 tcIndex
, threadContexts
.size());
243 threadContexts
[tcIndex
] = tc
;
246 // map simulator fd sim_fd to target fd tgt_fd
248 Process::dup_fd(int sim_fd
, int tgt_fd
)
250 if (tgt_fd
< 0 || tgt_fd
> MAX_FD
)
251 panic("Process::dup_fd tried to dup past MAX_FD (%d)", tgt_fd
);
253 Process::FdMap
*fdo
= &fd_map
[tgt_fd
];
258 // generate new target fd for sim_fd
260 Process::alloc_fd(int sim_fd
, string filename
, int flags
, int mode
, bool pipe
)
262 // in case open() returns an error, don't allocate a new fd
266 // find first free target fd
267 for (int free_fd
= 0; free_fd
<= MAX_FD
; ++free_fd
) {
268 Process::FdMap
*fdo
= &fd_map
[free_fd
];
271 fdo
->filename
= filename
;
276 fdo
->readPipeSource
= 0;
281 panic("Process::alloc_fd: out of file descriptors!");
285 // free target fd (e.g., after close)
287 Process::free_fd(int tgt_fd
)
289 Process::FdMap
*fdo
= &fd_map
[tgt_fd
];
291 warn("Process::free_fd: request to free unused fd %d", tgt_fd
);
294 fdo
->filename
= "NULL";
299 fdo
->readPipeSource
= 0;
303 // look up simulator fd for given target fd
305 Process::sim_fd(int tgt_fd
)
310 return fd_map
[tgt_fd
].fd
;
314 Process::sim_fd_obj(int tgt_fd
)
317 panic("sim_fd_obj called in fd out of range.");
319 return &fd_map
[tgt_fd
];
322 Process::checkAndAllocNextPage(Addr vaddr
)
324 // if this is an initial write we might not have
325 if (vaddr
>= stack_min
&& vaddr
< stack_base
) {
326 pTable
->allocate(roundDown(vaddr
, VMPageSize
), VMPageSize
);
330 // We've accessed the next page of the stack, so extend the stack
332 if (vaddr
< stack_min
&& vaddr
>= stack_base
- max_stack_size
) {
333 while (vaddr
< stack_min
) {
334 stack_min
-= TheISA::PageBytes
;
335 if(stack_base
- stack_min
> max_stack_size
)
336 fatal("Maximum stack size exceeded\n");
337 if(stack_base
- stack_min
> 8*1024*1024)
338 fatal("Over max stack size for one thread\n");
339 pTable
->allocate(stack_min
, TheISA::PageBytes
);
340 warn("Increasing stack size by one page.");
347 // find all offsets for currently open files and save them
349 Process::fix_file_offsets() {
350 Process::FdMap
*fdo_stdin
= &fd_map
[STDIN_FILENO
];
351 Process::FdMap
*fdo_stdout
= &fd_map
[STDOUT_FILENO
];
352 Process::FdMap
*fdo_stderr
= &fd_map
[STDERR_FILENO
];
353 string in
= fdo_stdin
->filename
;
354 string out
= fdo_stdout
->filename
;
356 // initialize file descriptors to default: same as simulator
357 int stdin_fd
, stdout_fd
, stderr_fd
;
359 if (in
== "stdin" || in
== "cin")
360 stdin_fd
= STDIN_FILENO
;
361 else if (in
== "None")
364 //OPEN standard in and seek to the right location
365 stdin_fd
= Process::openInputFile(in
);
366 if (lseek(stdin_fd
, fdo_stdin
->fileOffset
, SEEK_SET
) < 0)
367 panic("Unable to seek to correct location in file: %s", in
);
370 if (out
== "stdout" || out
== "cout")
371 stdout_fd
= STDOUT_FILENO
;
372 else if (out
== "stderr" || out
== "cerr")
373 stdout_fd
= STDERR_FILENO
;
374 else if (out
== "None")
377 stdout_fd
= Process::openOutputFile(out
);
378 if (lseek(stdin_fd
, fdo_stdout
->fileOffset
, SEEK_SET
) < 0)
379 panic("Unable to seek to correct in file: %s", out
);
382 stderr_fd
= (stdout_fd
!= STDOUT_FILENO
) ? stdout_fd
: STDERR_FILENO
;
384 fdo_stdin
->fd
= stdin_fd
;
385 fdo_stdout
->fd
= stdout_fd
;
386 fdo_stderr
->fd
= stderr_fd
;
389 for (int free_fd
= 3; free_fd
<= MAX_FD
; ++free_fd
) {
390 Process::FdMap
*fdo
= &fd_map
[free_fd
];
393 if (fdo
->filename
== "PIPE-WRITE")
396 assert (fdo
->filename
== "PIPE-READ");
399 int pipe_retval
= pipe(fds
);
401 if (pipe_retval
< 0) {
403 panic("Unable to create new pipe.");
405 fdo
->fd
= fds
[0]; //set read pipe
406 Process::FdMap
*fdo_write
= &fd_map
[fdo
->readPipeSource
];
407 if (fdo_write
->filename
!= "PIPE-WRITE")
408 panic ("Couldn't find write end of the pipe");
410 fdo_write
->fd
= fds
[1];//set write pipe
414 int fd
= open(fdo
->filename
.c_str(), fdo
->flags
, fdo
->mode
);
417 panic("Unable to open file: %s", fdo
->filename
);
420 //Seek to correct location before checkpoint
421 if (lseek(fd
,fdo
->fileOffset
, SEEK_SET
) < 0)
422 panic("Unable to seek to correct location in file: %s", fdo
->filename
);
428 Process::find_file_offsets(){
429 for (int free_fd
= 0; free_fd
<= MAX_FD
; ++free_fd
) {
430 Process::FdMap
*fdo
= &fd_map
[free_fd
];
432 fdo
->fileOffset
= lseek(fdo
->fd
, 0, SEEK_CUR
);
434 fdo
->filename
= "NULL";
441 Process::setReadPipeSource(int read_pipe_fd
, int source_fd
){
442 Process::FdMap
*fdo
= &fd_map
[read_pipe_fd
];
443 fdo
->readPipeSource
= source_fd
;
447 Process::FdMap::serialize(std::ostream
&os
)
449 SERIALIZE_SCALAR(fd
);
450 SERIALIZE_SCALAR(isPipe
);
451 SERIALIZE_SCALAR(filename
);
452 SERIALIZE_SCALAR(flags
);
453 SERIALIZE_SCALAR(readPipeSource
);
454 SERIALIZE_SCALAR(fileOffset
);
458 Process::FdMap::unserialize(Checkpoint
*cp
, const std::string
§ion
)
460 UNSERIALIZE_SCALAR(fd
);
461 UNSERIALIZE_SCALAR(isPipe
);
462 UNSERIALIZE_SCALAR(filename
);
463 UNSERIALIZE_SCALAR(flags
);
464 UNSERIALIZE_SCALAR(readPipeSource
);
465 UNSERIALIZE_SCALAR(fileOffset
);
469 Process::serialize(std::ostream
&os
)
471 SERIALIZE_SCALAR(initialContextLoaded
);
472 SERIALIZE_SCALAR(brk_point
);
473 SERIALIZE_SCALAR(stack_base
);
474 SERIALIZE_SCALAR(stack_size
);
475 SERIALIZE_SCALAR(stack_min
);
476 SERIALIZE_SCALAR(next_thread_stack_base
);
477 SERIALIZE_SCALAR(mmap_start
);
478 SERIALIZE_SCALAR(mmap_end
);
479 SERIALIZE_SCALAR(nxm_start
);
480 SERIALIZE_SCALAR(nxm_end
);
482 pTable
->serialize(os
);
483 for (int x
= 0; x
<= MAX_FD
; x
++) {
484 nameOut(os
, csprintf("%s.FdMap%d", name(), x
));
485 fd_map
[x
].serialize(os
);
491 Process::unserialize(Checkpoint
*cp
, const std::string
§ion
)
493 UNSERIALIZE_SCALAR(initialContextLoaded
);
494 UNSERIALIZE_SCALAR(brk_point
);
495 UNSERIALIZE_SCALAR(stack_base
);
496 UNSERIALIZE_SCALAR(stack_size
);
497 UNSERIALIZE_SCALAR(stack_min
);
498 UNSERIALIZE_SCALAR(next_thread_stack_base
);
499 UNSERIALIZE_SCALAR(mmap_start
);
500 UNSERIALIZE_SCALAR(mmap_end
);
501 UNSERIALIZE_SCALAR(nxm_start
);
502 UNSERIALIZE_SCALAR(nxm_end
);
503 pTable
->unserialize(cp
, section
);
504 for (int x
= 0; x
<= MAX_FD
; x
++) {
505 fd_map
[x
].unserialize(cp
, csprintf("%s.FdMap%d", section
, x
));
509 checkpointRestored
= true;
514 ////////////////////////////////////////////////////////////////////////
516 // LiveProcess member definitions
518 ////////////////////////////////////////////////////////////////////////
521 LiveProcess::LiveProcess(LiveProcessParams
* params
, ObjectFile
*_objFile
)
522 : Process(params
), objFile(_objFile
),
523 argv(params
->cmd
), envp(params
->env
), cwd(params
->cwd
)
526 __euid
= params
->euid
;
528 __egid
= params
->egid
;
530 __ppid
= params
->ppid
;
532 prog_fname
= params
->cmd
[0];
534 // load up symbols, if any... these may be used for debugging or
536 if (!debugSymbolTable
) {
537 debugSymbolTable
= new SymbolTable();
538 if (!objFile
->loadGlobalSymbols(debugSymbolTable
) ||
539 !objFile
->loadLocalSymbols(debugSymbolTable
)) {
540 // didn't load any symbols
541 delete debugSymbolTable
;
542 debugSymbolTable
= NULL
;
548 LiveProcess::argsInit(int intSize
, int pageSize
)
552 // load object file into target memory
553 objFile
->loadSections(initVirtMem
);
555 // Calculate how much space we need for arg & env arrays.
556 int argv_array_size
= intSize
* (argv
.size() + 1);
557 int envp_array_size
= intSize
* (envp
.size() + 1);
558 int arg_data_size
= 0;
559 for (int i
= 0; i
< argv
.size(); ++i
) {
560 arg_data_size
+= argv
[i
].size() + 1;
562 int env_data_size
= 0;
563 for (int i
= 0; i
< envp
.size(); ++i
) {
564 env_data_size
+= envp
[i
].size() + 1;
568 argv_array_size
+ envp_array_size
+ arg_data_size
+ env_data_size
;
569 if (space_needed
< 32*1024)
570 space_needed
= 32*1024;
572 // set bottom of stack
573 stack_min
= stack_base
- space_needed
;
575 stack_min
= roundDown(stack_min
, pageSize
);
576 stack_size
= stack_base
- stack_min
;
578 pTable
->allocate(stack_min
, roundUp(stack_size
, pageSize
));
580 // map out initial stack contents
581 Addr argv_array_base
= stack_min
+ intSize
; // room for argc
582 Addr envp_array_base
= argv_array_base
+ argv_array_size
;
583 Addr arg_data_base
= envp_array_base
+ envp_array_size
;
584 Addr env_data_base
= arg_data_base
+ arg_data_size
;
586 // write contents to stack
587 uint64_t argc
= argv
.size();
589 argc
= htog((uint64_t)argc
);
590 else if (intSize
== 4)
591 argc
= htog((uint32_t)argc
);
593 panic("Unknown int size");
595 initVirtMem
->writeBlob(stack_min
, (uint8_t*)&argc
, intSize
);
597 copyStringArray(argv
, argv_array_base
, arg_data_base
, initVirtMem
);
598 copyStringArray(envp
, envp_array_base
, env_data_base
, initVirtMem
);
600 assert(NumArgumentRegs
>= 2);
601 threadContexts
[0]->setIntReg(ArgumentReg
[0], argc
);
602 threadContexts
[0]->setIntReg(ArgumentReg
[1], argv_array_base
);
603 threadContexts
[0]->setIntReg(StackPointerReg
, stack_min
);
605 Addr prog_entry
= objFile
->entryPoint();
606 threadContexts
[0]->setPC(prog_entry
);
607 threadContexts
[0]->setNextPC(prog_entry
+ sizeof(MachInst
));
609 #if THE_ISA != ALPHA_ISA //e.g. MIPS or Sparc
610 threadContexts
[0]->setNextNPC(prog_entry
+ (2 * sizeof(MachInst
)));
617 LiveProcess::syscall(int64_t callnum
, ThreadContext
*tc
)
621 SyscallDesc
*desc
= getDesc(callnum
);
623 fatal("Syscall %d out of range", callnum
);
625 desc
->doSyscall(callnum
, this, tc
);
629 LiveProcess::create(LiveProcessParams
* params
)
631 LiveProcess
*process
= NULL
;
634 params
->executable
== "" ? params
->cmd
[0] : params
->executable
;
635 ObjectFile
*objFile
= createObjectFile(executable
);
636 if (objFile
== NULL
) {
637 fatal("Can't load object file %s", executable
);
640 if (objFile
->isDynamic())
641 fatal("Object file is a dynamic executable however only static "
642 "executables are supported!\n Please recompile your "
643 "executable as a static binary and try again.\n");
645 #if THE_ISA == ALPHA_ISA
646 if (objFile
->hasTLS())
647 fatal("Object file has a TLS section and single threaded TLS is not\n"
648 " currently supported for Alpha! Please recompile your "
649 "executable with \n a non-TLS toolchain.\n");
651 if (objFile
->getArch() != ObjectFile::Alpha
)
652 fatal("Object file architecture does not match compiled ISA (Alpha).");
653 switch (objFile
->getOpSys()) {
654 case ObjectFile::Tru64
:
655 process
= new AlphaTru64Process(params
, objFile
);
658 case ObjectFile::Linux
:
659 process
= new AlphaLinuxProcess(params
, objFile
);
663 fatal("Unknown/unsupported operating system.");
665 #elif THE_ISA == SPARC_ISA
666 if (objFile
->getArch() != ObjectFile::SPARC64
&& objFile
->getArch() != ObjectFile::SPARC32
)
667 fatal("Object file architecture does not match compiled ISA (SPARC).");
668 switch (objFile
->getOpSys()) {
669 case ObjectFile::Linux
:
670 if (objFile
->getArch() == ObjectFile::SPARC64
) {
671 process
= new Sparc64LinuxProcess(params
, objFile
);
673 process
= new Sparc32LinuxProcess(params
, objFile
);
678 case ObjectFile::Solaris
:
679 process
= new SparcSolarisProcess(params
, objFile
);
682 fatal("Unknown/unsupported operating system.");
684 #elif THE_ISA == X86_ISA
685 if (objFile
->getArch() != ObjectFile::X86
)
686 fatal("Object file architecture does not match compiled ISA (x86).");
687 switch (objFile
->getOpSys()) {
688 case ObjectFile::Linux
:
689 process
= new X86LinuxProcess(params
, objFile
);
692 fatal("Unknown/unsupported operating system.");
694 #elif THE_ISA == MIPS_ISA
695 if (objFile
->getArch() != ObjectFile::Mips
)
696 fatal("Object file architecture does not match compiled ISA (MIPS).");
697 switch (objFile
->getOpSys()) {
698 case ObjectFile::Linux
:
699 process
= new MipsLinuxProcess(params
, objFile
);
703 fatal("Unknown/unsupported operating system.");
705 #elif THE_ISA == ARM_ISA
706 if (objFile
->getArch() != ObjectFile::Arm
)
707 fatal("Object file architecture does not match compiled ISA (ARM).");
708 switch (objFile
->getOpSys()) {
709 case ObjectFile::Linux
:
710 process
= new ArmLinuxProcess(params
, objFile
);
714 fatal("Unknown/unsupported operating system.");
717 #error "THE_ISA not set"
722 fatal("Unknown error creating process object.");
727 LiveProcessParams::create()
729 return LiveProcess::create(this);