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 Process::Process(ProcessParams
* params
)
90 : SimObject(params
), system(params
->system
), checkpointRestored(false),
91 max_stack_size(params
->max_stack_size
)
93 string in
= params
->input
;
94 string out
= params
->output
;
95 string err
= params
->errout
;
97 // initialize file descriptors to default: same as simulator
98 int stdin_fd
, stdout_fd
, stderr_fd
;
100 if (in
== "stdin" || in
== "cin")
101 stdin_fd
= STDIN_FILENO
;
102 else if (in
== "None")
105 stdin_fd
= Process::openInputFile(in
);
107 if (out
== "stdout" || out
== "cout")
108 stdout_fd
= STDOUT_FILENO
;
109 else if (out
== "stderr" || out
== "cerr")
110 stdout_fd
= STDERR_FILENO
;
111 else if (out
== "None")
114 stdout_fd
= Process::openOutputFile(out
);
116 if (err
== "stdout" || err
== "cout")
117 stderr_fd
= STDOUT_FILENO
;
118 else if (err
== "stderr" || err
== "cerr")
119 stderr_fd
= STDERR_FILENO
;
120 else if (err
== "None")
123 stderr_fd
= stdout_fd
;
125 stderr_fd
= Process::openOutputFile(err
);
127 M5_pid
= system
->allocatePID();
128 // initialize first 3 fds (stdin, stdout, stderr)
129 Process::FdMap
*fdo
= &fd_map
[STDIN_FILENO
];
132 fdo
->flags
= O_RDONLY
;
136 fdo
= &fd_map
[STDOUT_FILENO
];
139 fdo
->flags
= O_WRONLY
| O_CREAT
| O_TRUNC
;
143 fdo
= &fd_map
[STDERR_FILENO
];
146 fdo
->flags
= O_WRONLY
;
151 // mark remaining fds as free
152 for (int i
= 3; i
<= MAX_FD
; ++i
) {
153 Process::FdMap
*fdo
= &fd_map
[i
];
157 mmap_start
= mmap_end
= 0;
158 nxm_start
= nxm_end
= 0;
159 pTable
= new PageTable(this);
160 // other parameters will be initialized when the program is loaded
167 using namespace Stats
;
170 .name(name() + ".PROG:num_syscalls")
171 .desc("Number of system calls")
176 // static helper functions
179 Process::openInputFile(const string
&filename
)
181 int fd
= open(filename
.c_str(), O_RDONLY
);
185 cerr
<< "unable to open \"" << filename
<< "\" for reading\n";
186 fatal("can't open input file");
194 Process::openOutputFile(const string
&filename
)
196 int fd
= open(filename
.c_str(), O_WRONLY
| O_CREAT
| O_TRUNC
, 0664);
200 cerr
<< "unable to open \"" << filename
<< "\" for writing\n";
201 fatal("can't open output file");
208 Process::findFreeContext()
210 int size
= contextIds
.size();
212 for (int i
= 0; i
< size
; ++i
) {
213 tc
= system
->getThreadContext(contextIds
[i
]);
214 if (tc
->status() == ThreadContext::Unallocated
) {
215 // inactive context, free to use
225 if (contextIds
.empty())
226 fatal("Process %s is not associated with any HW contexts!\n", name());
228 // first thread context for this process... initialize & enable
229 ThreadContext
*tc
= system
->getThreadContext(contextIds
[0]);
231 // mark this context as active so it will start ticking.
235 mem_port
= system
->physmem
->getPort("functional");
236 initVirtMem
= new TranslatingPort("process init port", this,
237 TranslatingPort::Always
);
238 mem_port
->setPeer(initVirtMem
);
239 initVirtMem
->setPeer(mem_port
);
242 // map simulator fd sim_fd to target fd tgt_fd
244 Process::dup_fd(int sim_fd
, int tgt_fd
)
246 if (tgt_fd
< 0 || tgt_fd
> MAX_FD
)
247 panic("Process::dup_fd tried to dup past MAX_FD (%d)", tgt_fd
);
249 Process::FdMap
*fdo
= &fd_map
[tgt_fd
];
254 // generate new target fd for sim_fd
256 Process::alloc_fd(int sim_fd
, string filename
, int flags
, int mode
, bool pipe
)
258 // in case open() returns an error, don't allocate a new fd
262 // find first free target fd
263 for (int free_fd
= 0; free_fd
<= MAX_FD
; ++free_fd
) {
264 Process::FdMap
*fdo
= &fd_map
[free_fd
];
267 fdo
->filename
= filename
;
272 fdo
->readPipeSource
= 0;
277 panic("Process::alloc_fd: out of file descriptors!");
281 // free target fd (e.g., after close)
283 Process::free_fd(int tgt_fd
)
285 Process::FdMap
*fdo
= &fd_map
[tgt_fd
];
287 warn("Process::free_fd: request to free unused fd %d", tgt_fd
);
290 fdo
->filename
= "NULL";
295 fdo
->readPipeSource
= 0;
299 // look up simulator fd for given target fd
301 Process::sim_fd(int tgt_fd
)
306 return fd_map
[tgt_fd
].fd
;
310 Process::sim_fd_obj(int tgt_fd
)
313 panic("sim_fd_obj called in fd out of range.");
315 return &fd_map
[tgt_fd
];
318 Process::checkAndAllocNextPage(Addr vaddr
)
320 // if this is an initial write we might not have
321 if (vaddr
>= stack_min
&& vaddr
< stack_base
) {
322 pTable
->allocate(roundDown(vaddr
, VMPageSize
), VMPageSize
);
326 // We've accessed the next page of the stack, so extend the stack
328 if (vaddr
< stack_min
&& vaddr
>= stack_base
- max_stack_size
) {
329 while (vaddr
< stack_min
) {
330 stack_min
-= TheISA::PageBytes
;
331 if(stack_base
- stack_min
> max_stack_size
)
332 fatal("Maximum stack size exceeded\n");
333 if(stack_base
- stack_min
> 8*1024*1024)
334 fatal("Over max stack size for one thread\n");
335 pTable
->allocate(stack_min
, TheISA::PageBytes
);
336 warn("Increasing stack size by one page.");
343 // find all offsets for currently open files and save them
345 Process::fix_file_offsets() {
346 Process::FdMap
*fdo_stdin
= &fd_map
[STDIN_FILENO
];
347 Process::FdMap
*fdo_stdout
= &fd_map
[STDOUT_FILENO
];
348 Process::FdMap
*fdo_stderr
= &fd_map
[STDERR_FILENO
];
349 string in
= fdo_stdin
->filename
;
350 string out
= fdo_stdout
->filename
;
351 string err
= fdo_stderr
->filename
;
353 // initialize file descriptors to default: same as simulator
354 int stdin_fd
, stdout_fd
, stderr_fd
;
356 if (in
== "stdin" || in
== "cin")
357 stdin_fd
= STDIN_FILENO
;
358 else if (in
== "None")
361 //OPEN standard in and seek to the right location
362 stdin_fd
= Process::openInputFile(in
);
363 if (lseek(stdin_fd
, fdo_stdin
->fileOffset
, SEEK_SET
) < 0)
364 panic("Unable to seek to correct location in file: %s", in
);
367 if (out
== "stdout" || out
== "cout")
368 stdout_fd
= STDOUT_FILENO
;
369 else if (out
== "stderr" || out
== "cerr")
370 stdout_fd
= STDERR_FILENO
;
371 else if (out
== "None")
374 stdout_fd
= Process::openOutputFile(out
);
375 if (lseek(stdout_fd
, fdo_stdout
->fileOffset
, SEEK_SET
) < 0)
376 panic("Unable to seek to correct location in file: %s", out
);
379 if (err
== "stdout" || err
== "cout")
380 stderr_fd
= STDOUT_FILENO
;
381 else if (err
== "stderr" || err
== "cerr")
382 stderr_fd
= STDERR_FILENO
;
383 else if (err
== "None")
386 stderr_fd
= stdout_fd
;
388 stderr_fd
= Process::openOutputFile(err
);
389 if (lseek(stderr_fd
, fdo_stderr
->fileOffset
, SEEK_SET
) < 0)
390 panic("Unable to seek to correct location in file: %s", err
);
393 fdo_stdin
->fd
= stdin_fd
;
394 fdo_stdout
->fd
= stdout_fd
;
395 fdo_stderr
->fd
= stderr_fd
;
398 for (int free_fd
= 3; free_fd
<= MAX_FD
; ++free_fd
) {
399 Process::FdMap
*fdo
= &fd_map
[free_fd
];
402 if (fdo
->filename
== "PIPE-WRITE")
405 assert (fdo
->filename
== "PIPE-READ");
408 int pipe_retval
= pipe(fds
);
410 if (pipe_retval
< 0) {
412 panic("Unable to create new pipe.");
414 fdo
->fd
= fds
[0]; //set read pipe
415 Process::FdMap
*fdo_write
= &fd_map
[fdo
->readPipeSource
];
416 if (fdo_write
->filename
!= "PIPE-WRITE")
417 panic ("Couldn't find write end of the pipe");
419 fdo_write
->fd
= fds
[1];//set write pipe
423 int fd
= open(fdo
->filename
.c_str(), fdo
->flags
, fdo
->mode
);
426 panic("Unable to open file: %s", fdo
->filename
);
429 //Seek to correct location before checkpoint
430 if (lseek(fd
,fdo
->fileOffset
, SEEK_SET
) < 0)
431 panic("Unable to seek to correct location in file: %s", fdo
->filename
);
437 Process::find_file_offsets(){
438 for (int free_fd
= 0; free_fd
<= MAX_FD
; ++free_fd
) {
439 Process::FdMap
*fdo
= &fd_map
[free_fd
];
441 fdo
->fileOffset
= lseek(fdo
->fd
, 0, SEEK_CUR
);
443 fdo
->filename
= "NULL";
450 Process::setReadPipeSource(int read_pipe_fd
, int source_fd
){
451 Process::FdMap
*fdo
= &fd_map
[read_pipe_fd
];
452 fdo
->readPipeSource
= source_fd
;
456 Process::FdMap::serialize(std::ostream
&os
)
458 SERIALIZE_SCALAR(fd
);
459 SERIALIZE_SCALAR(isPipe
);
460 SERIALIZE_SCALAR(filename
);
461 SERIALIZE_SCALAR(flags
);
462 SERIALIZE_SCALAR(readPipeSource
);
463 SERIALIZE_SCALAR(fileOffset
);
467 Process::FdMap::unserialize(Checkpoint
*cp
, const std::string
§ion
)
469 UNSERIALIZE_SCALAR(fd
);
470 UNSERIALIZE_SCALAR(isPipe
);
471 UNSERIALIZE_SCALAR(filename
);
472 UNSERIALIZE_SCALAR(flags
);
473 UNSERIALIZE_SCALAR(readPipeSource
);
474 UNSERIALIZE_SCALAR(fileOffset
);
478 Process::serialize(std::ostream
&os
)
480 SERIALIZE_SCALAR(initialContextLoaded
);
481 SERIALIZE_SCALAR(brk_point
);
482 SERIALIZE_SCALAR(stack_base
);
483 SERIALIZE_SCALAR(stack_size
);
484 SERIALIZE_SCALAR(stack_min
);
485 SERIALIZE_SCALAR(next_thread_stack_base
);
486 SERIALIZE_SCALAR(mmap_start
);
487 SERIALIZE_SCALAR(mmap_end
);
488 SERIALIZE_SCALAR(nxm_start
);
489 SERIALIZE_SCALAR(nxm_end
);
491 pTable
->serialize(os
);
492 for (int x
= 0; x
<= MAX_FD
; x
++) {
493 nameOut(os
, csprintf("%s.FdMap%d", name(), x
));
494 fd_map
[x
].serialize(os
);
500 Process::unserialize(Checkpoint
*cp
, const std::string
§ion
)
502 UNSERIALIZE_SCALAR(initialContextLoaded
);
503 UNSERIALIZE_SCALAR(brk_point
);
504 UNSERIALIZE_SCALAR(stack_base
);
505 UNSERIALIZE_SCALAR(stack_size
);
506 UNSERIALIZE_SCALAR(stack_min
);
507 UNSERIALIZE_SCALAR(next_thread_stack_base
);
508 UNSERIALIZE_SCALAR(mmap_start
);
509 UNSERIALIZE_SCALAR(mmap_end
);
510 UNSERIALIZE_SCALAR(nxm_start
);
511 UNSERIALIZE_SCALAR(nxm_end
);
512 pTable
->unserialize(cp
, section
);
513 for (int x
= 0; x
<= MAX_FD
; x
++) {
514 fd_map
[x
].unserialize(cp
, csprintf("%s.FdMap%d", section
, x
));
518 checkpointRestored
= true;
523 ////////////////////////////////////////////////////////////////////////
525 // LiveProcess member definitions
527 ////////////////////////////////////////////////////////////////////////
530 LiveProcess::LiveProcess(LiveProcessParams
* params
, ObjectFile
*_objFile
)
531 : Process(params
), objFile(_objFile
),
532 argv(params
->cmd
), envp(params
->env
), cwd(params
->cwd
)
535 __euid
= params
->euid
;
537 __egid
= params
->egid
;
539 __ppid
= params
->ppid
;
541 prog_fname
= params
->cmd
[0];
543 // load up symbols, if any... these may be used for debugging or
545 if (!debugSymbolTable
) {
546 debugSymbolTable
= new SymbolTable();
547 if (!objFile
->loadGlobalSymbols(debugSymbolTable
) ||
548 !objFile
->loadLocalSymbols(debugSymbolTable
)) {
549 // didn't load any symbols
550 delete debugSymbolTable
;
551 debugSymbolTable
= NULL
;
557 LiveProcess::argsInit(int intSize
, int pageSize
)
561 // load object file into target memory
562 objFile
->loadSections(initVirtMem
);
564 // Calculate how much space we need for arg & env arrays.
565 int argv_array_size
= intSize
* (argv
.size() + 1);
566 int envp_array_size
= intSize
* (envp
.size() + 1);
567 int arg_data_size
= 0;
568 for (int i
= 0; i
< argv
.size(); ++i
) {
569 arg_data_size
+= argv
[i
].size() + 1;
571 int env_data_size
= 0;
572 for (int i
= 0; i
< envp
.size(); ++i
) {
573 env_data_size
+= envp
[i
].size() + 1;
577 argv_array_size
+ envp_array_size
+ arg_data_size
+ env_data_size
;
578 if (space_needed
< 32*1024)
579 space_needed
= 32*1024;
581 // set bottom of stack
582 stack_min
= stack_base
- space_needed
;
584 stack_min
= roundDown(stack_min
, pageSize
);
585 stack_size
= stack_base
- stack_min
;
587 pTable
->allocate(stack_min
, roundUp(stack_size
, pageSize
));
589 // map out initial stack contents
590 Addr argv_array_base
= stack_min
+ intSize
; // room for argc
591 Addr envp_array_base
= argv_array_base
+ argv_array_size
;
592 Addr arg_data_base
= envp_array_base
+ envp_array_size
;
593 Addr env_data_base
= arg_data_base
+ arg_data_size
;
595 // write contents to stack
596 uint64_t argc
= argv
.size();
598 argc
= htog((uint64_t)argc
);
599 else if (intSize
== 4)
600 argc
= htog((uint32_t)argc
);
602 panic("Unknown int size");
604 initVirtMem
->writeBlob(stack_min
, (uint8_t*)&argc
, intSize
);
606 copyStringArray(argv
, argv_array_base
, arg_data_base
, initVirtMem
);
607 copyStringArray(envp
, envp_array_base
, env_data_base
, initVirtMem
);
609 assert(NumArgumentRegs
>= 2);
611 ThreadContext
*tc
= system
->getThreadContext(contextIds
[0]);
613 tc
->setIntReg(ArgumentReg
[0], argc
);
614 tc
->setIntReg(ArgumentReg
[1], argv_array_base
);
615 tc
->setIntReg(StackPointerReg
, stack_min
);
617 Addr prog_entry
= objFile
->entryPoint();
618 tc
->setPC(prog_entry
);
619 tc
->setNextPC(prog_entry
+ sizeof(MachInst
));
621 #if THE_ISA != ALPHA_ISA //e.g. MIPS or Sparc
622 tc
->setNextNPC(prog_entry
+ (2 * sizeof(MachInst
)));
629 LiveProcess::syscall(int64_t callnum
, ThreadContext
*tc
)
633 SyscallDesc
*desc
= getDesc(callnum
);
635 fatal("Syscall %d out of range", callnum
);
637 desc
->doSyscall(callnum
, this, tc
);
641 LiveProcess::create(LiveProcessParams
* params
)
643 LiveProcess
*process
= NULL
;
646 params
->executable
== "" ? params
->cmd
[0] : params
->executable
;
647 ObjectFile
*objFile
= createObjectFile(executable
);
648 if (objFile
== NULL
) {
649 fatal("Can't load object file %s", executable
);
652 if (objFile
->isDynamic())
653 fatal("Object file is a dynamic executable however only static "
654 "executables are supported!\n Please recompile your "
655 "executable as a static binary and try again.\n");
657 #if THE_ISA == ALPHA_ISA
658 if (objFile
->getArch() != ObjectFile::Alpha
)
659 fatal("Object file architecture does not match compiled ISA (Alpha).");
661 if (objFile
->hasTLS())
662 fatal("Object file has a TLS section and single threaded TLS is not\n"
663 " currently supported for Alpha! Please recompile your "
664 "executable with \n a non-TLS toolchain.\n");
666 switch (objFile
->getOpSys()) {
667 case ObjectFile::Tru64
:
668 process
= new AlphaTru64Process(params
, objFile
);
671 case ObjectFile::UnknownOpSys
:
672 warn("Unknown operating system; assuming Linux.");
674 case ObjectFile::Linux
:
675 process
= new AlphaLinuxProcess(params
, objFile
);
679 fatal("Unknown/unsupported operating system.");
681 #elif THE_ISA == SPARC_ISA
682 if (objFile
->getArch() != ObjectFile::SPARC64
&&
683 objFile
->getArch() != ObjectFile::SPARC32
)
684 fatal("Object file architecture does not match compiled ISA (SPARC).");
685 switch (objFile
->getOpSys()) {
686 case ObjectFile::UnknownOpSys
:
687 warn("Unknown operating system; assuming Linux.");
689 case ObjectFile::Linux
:
690 if (objFile
->getArch() == ObjectFile::SPARC64
) {
691 process
= new Sparc64LinuxProcess(params
, objFile
);
693 process
= new Sparc32LinuxProcess(params
, objFile
);
698 case ObjectFile::Solaris
:
699 process
= new SparcSolarisProcess(params
, objFile
);
703 fatal("Unknown/unsupported operating system.");
705 #elif THE_ISA == X86_ISA
706 if (objFile
->getArch() != ObjectFile::X86
)
707 fatal("Object file architecture does not match compiled ISA (x86).");
708 switch (objFile
->getOpSys()) {
709 case ObjectFile::UnknownOpSys
:
710 warn("Unknown operating system; assuming Linux.");
712 case ObjectFile::Linux
:
713 process
= new X86LinuxProcess(params
, objFile
);
717 fatal("Unknown/unsupported operating system.");
719 #elif THE_ISA == MIPS_ISA
720 if (objFile
->getArch() != ObjectFile::Mips
)
721 fatal("Object file architecture does not match compiled ISA (MIPS).");
722 switch (objFile
->getOpSys()) {
723 case ObjectFile::UnknownOpSys
:
724 warn("Unknown operating system; assuming Linux.");
726 case ObjectFile::Linux
:
727 process
= new MipsLinuxProcess(params
, objFile
);
731 fatal("Unknown/unsupported operating system.");
733 #elif THE_ISA == ARM_ISA
734 if (objFile
->getArch() != ObjectFile::Arm
)
735 fatal("Object file architecture does not match compiled ISA (ARM).");
736 switch (objFile
->getOpSys()) {
737 case ObjectFile::UnknownOpSys
:
738 warn("Unknown operating system; assuming Linux.");
740 case ObjectFile::Linux
:
741 process
= new ArmLinuxProcess(params
, objFile
);
745 fatal("Unknown/unsupported operating system.");
748 #error "THE_ISA not set"
753 fatal("Unknown error creating process object.");
758 LiveProcessParams::create()
760 return LiveProcess::create(this);