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
;
96 // initialize file descriptors to default: same as simulator
97 int stdin_fd
, stdout_fd
, stderr_fd
;
99 if (in
== "stdin" || in
== "cin")
100 stdin_fd
= STDIN_FILENO
;
101 else if (in
== "None")
104 stdin_fd
= Process::openInputFile(in
);
106 if (out
== "stdout" || out
== "cout")
107 stdout_fd
= STDOUT_FILENO
;
108 else if (out
== "stderr" || out
== "cerr")
109 stdout_fd
= STDERR_FILENO
;
110 else if (out
== "None")
113 stdout_fd
= Process::openOutputFile(out
);
115 stderr_fd
= (stdout_fd
!= STDOUT_FILENO
) ? stdout_fd
: STDERR_FILENO
;
117 M5_pid
= system
->allocatePID();
118 // initialize first 3 fds (stdin, stdout, stderr)
119 Process::FdMap
*fdo
= &fd_map
[STDIN_FILENO
];
122 fdo
->flags
= O_RDONLY
;
126 fdo
= &fd_map
[STDOUT_FILENO
];
129 fdo
->flags
= O_WRONLY
| O_CREAT
| O_TRUNC
;
133 fdo
= &fd_map
[STDERR_FILENO
];
135 fdo
->filename
= "STDERR";
136 fdo
->flags
= O_WRONLY
;
141 // mark remaining fds as free
142 for (int i
= 3; i
<= MAX_FD
; ++i
) {
143 Process::FdMap
*fdo
= &fd_map
[i
];
147 mmap_start
= mmap_end
= 0;
148 nxm_start
= nxm_end
= 0;
149 pTable
= new PageTable(this);
150 // other parameters will be initialized when the program is loaded
157 using namespace Stats
;
160 .name(name() + ".PROG:num_syscalls")
161 .desc("Number of system calls")
166 // static helper functions
169 Process::openInputFile(const string
&filename
)
171 int fd
= open(filename
.c_str(), O_RDONLY
);
175 cerr
<< "unable to open \"" << filename
<< "\" for reading\n";
176 fatal("can't open input file");
184 Process::openOutputFile(const string
&filename
)
186 int fd
= open(filename
.c_str(), O_WRONLY
| O_CREAT
| O_TRUNC
, 0774);
190 cerr
<< "unable to open \"" << filename
<< "\" for writing\n";
191 fatal("can't open output file");
199 Process::registerThreadContext(ThreadContext
*tc
)
202 int myIndex
= threadContexts
.size();
203 threadContexts
.push_back(tc
);
205 int port
= getRemoteGDBPort();
207 RemoteGDB
*rgdb
= new RemoteGDB(system
, tc
);
208 GDBListener
*gdbl
= new GDBListener(rgdb
, port
+ myIndex
);
211 remoteGDB
.push_back(rgdb
);
214 // return CPU number to caller
221 if (threadContexts
.empty())
222 fatal("Process %s is not associated with any CPUs!\n", name());
224 // first thread context for this process... initialize & enable
225 ThreadContext
*tc
= threadContexts
[0];
227 // mark this context as active so it will start ticking.
231 mem_port
= system
->physmem
->getPort("functional");
232 initVirtMem
= new TranslatingPort("process init port", this,
233 TranslatingPort::Always
);
234 mem_port
->setPeer(initVirtMem
);
235 initVirtMem
->setPeer(mem_port
);
239 Process::replaceThreadContext(ThreadContext
*tc
, int tcIndex
)
241 if (tcIndex
>= threadContexts
.size()) {
242 panic("replaceThreadContext: bad tcIndex, %d >= %d\n",
243 tcIndex
, threadContexts
.size());
246 threadContexts
[tcIndex
] = tc
;
249 // map simulator fd sim_fd to target fd tgt_fd
251 Process::dup_fd(int sim_fd
, int tgt_fd
)
253 if (tgt_fd
< 0 || tgt_fd
> MAX_FD
)
254 panic("Process::dup_fd tried to dup past MAX_FD (%d)", tgt_fd
);
256 Process::FdMap
*fdo
= &fd_map
[tgt_fd
];
261 // generate new target fd for sim_fd
263 Process::alloc_fd(int sim_fd
, string filename
, int flags
, int mode
, bool pipe
)
265 // in case open() returns an error, don't allocate a new fd
269 // find first free target fd
270 for (int free_fd
= 0; free_fd
<= MAX_FD
; ++free_fd
) {
271 Process::FdMap
*fdo
= &fd_map
[free_fd
];
274 fdo
->filename
= filename
;
279 fdo
->readPipeSource
= 0;
284 panic("Process::alloc_fd: out of file descriptors!");
288 // free target fd (e.g., after close)
290 Process::free_fd(int tgt_fd
)
292 Process::FdMap
*fdo
= &fd_map
[tgt_fd
];
294 warn("Process::free_fd: request to free unused fd %d", tgt_fd
);
297 fdo
->filename
= "NULL";
302 fdo
->readPipeSource
= 0;
306 // look up simulator fd for given target fd
308 Process::sim_fd(int tgt_fd
)
313 return fd_map
[tgt_fd
].fd
;
317 Process::sim_fd_obj(int tgt_fd
)
320 panic("sim_fd_obj called in fd out of range.");
322 return &fd_map
[tgt_fd
];
325 Process::checkAndAllocNextPage(Addr vaddr
)
327 // if this is an initial write we might not have
328 if (vaddr
>= stack_min
&& vaddr
< stack_base
) {
329 pTable
->allocate(roundDown(vaddr
, VMPageSize
), VMPageSize
);
333 // We've accessed the next page of the stack, so extend the stack
335 if (vaddr
< stack_min
&& vaddr
>= stack_base
- max_stack_size
) {
336 while (vaddr
< stack_min
) {
337 stack_min
-= TheISA::PageBytes
;
338 if(stack_base
- stack_min
> max_stack_size
)
339 fatal("Maximum stack size exceeded\n");
340 if(stack_base
- stack_min
> 8*1024*1024)
341 fatal("Over max stack size for one thread\n");
342 pTable
->allocate(stack_min
, TheISA::PageBytes
);
343 warn("Increasing stack size by one page.");
350 // find all offsets for currently open files and save them
352 Process::fix_file_offsets() {
353 Process::FdMap
*fdo_stdin
= &fd_map
[STDIN_FILENO
];
354 Process::FdMap
*fdo_stdout
= &fd_map
[STDOUT_FILENO
];
355 Process::FdMap
*fdo_stderr
= &fd_map
[STDERR_FILENO
];
356 string in
= fdo_stdin
->filename
;
357 string out
= fdo_stdout
->filename
;
359 // initialize file descriptors to default: same as simulator
360 int stdin_fd
, stdout_fd
, stderr_fd
;
362 if (in
== "stdin" || in
== "cin")
363 stdin_fd
= STDIN_FILENO
;
364 else if (in
== "None")
367 //OPEN standard in and seek to the right location
368 stdin_fd
= Process::openInputFile(in
);
369 if (lseek(stdin_fd
, fdo_stdin
->fileOffset
, SEEK_SET
) < 0)
370 panic("Unable to seek to correct location in file: %s", in
);
373 if (out
== "stdout" || out
== "cout")
374 stdout_fd
= STDOUT_FILENO
;
375 else if (out
== "stderr" || out
== "cerr")
376 stdout_fd
= STDERR_FILENO
;
377 else if (out
== "None")
380 stdout_fd
= Process::openOutputFile(out
);
381 if (lseek(stdin_fd
, fdo_stdout
->fileOffset
, SEEK_SET
) < 0)
382 panic("Unable to seek to correct in file: %s", out
);
385 stderr_fd
= (stdout_fd
!= STDOUT_FILENO
) ? stdout_fd
: STDERR_FILENO
;
387 fdo_stdin
->fd
= stdin_fd
;
388 fdo_stdout
->fd
= stdout_fd
;
389 fdo_stderr
->fd
= stderr_fd
;
392 for (int free_fd
= 3; free_fd
<= MAX_FD
; ++free_fd
) {
393 Process::FdMap
*fdo
= &fd_map
[free_fd
];
396 if (fdo
->filename
== "PIPE-WRITE")
399 assert (fdo
->filename
== "PIPE-READ");
402 int pipe_retval
= pipe(fds
);
404 if (pipe_retval
< 0) {
406 panic("Unable to create new pipe.");
408 fdo
->fd
= fds
[0]; //set read pipe
409 Process::FdMap
*fdo_write
= &fd_map
[fdo
->readPipeSource
];
410 if (fdo_write
->filename
!= "PIPE-WRITE")
411 panic ("Couldn't find write end of the pipe");
413 fdo_write
->fd
= fds
[1];//set write pipe
417 int fd
= open(fdo
->filename
.c_str(), fdo
->flags
, fdo
->mode
);
420 panic("Unable to open file: %s", fdo
->filename
);
423 //Seek to correct location before checkpoint
424 if (lseek(fd
,fdo
->fileOffset
, SEEK_SET
) < 0)
425 panic("Unable to seek to correct location in file: %s", fdo
->filename
);
431 Process::find_file_offsets(){
432 for (int free_fd
= 0; free_fd
<= MAX_FD
; ++free_fd
) {
433 Process::FdMap
*fdo
= &fd_map
[free_fd
];
435 fdo
->fileOffset
= lseek(fdo
->fd
, 0, SEEK_CUR
);
437 fdo
->filename
= "NULL";
444 Process::setReadPipeSource(int read_pipe_fd
, int source_fd
){
445 Process::FdMap
*fdo
= &fd_map
[read_pipe_fd
];
446 fdo
->readPipeSource
= source_fd
;
450 Process::FdMap::serialize(std::ostream
&os
)
452 SERIALIZE_SCALAR(fd
);
453 SERIALIZE_SCALAR(isPipe
);
454 SERIALIZE_SCALAR(filename
);
455 SERIALIZE_SCALAR(flags
);
456 SERIALIZE_SCALAR(readPipeSource
);
457 SERIALIZE_SCALAR(fileOffset
);
461 Process::FdMap::unserialize(Checkpoint
*cp
, const std::string
§ion
)
463 UNSERIALIZE_SCALAR(fd
);
464 UNSERIALIZE_SCALAR(isPipe
);
465 UNSERIALIZE_SCALAR(filename
);
466 UNSERIALIZE_SCALAR(flags
);
467 UNSERIALIZE_SCALAR(readPipeSource
);
468 UNSERIALIZE_SCALAR(fileOffset
);
472 Process::serialize(std::ostream
&os
)
474 SERIALIZE_SCALAR(initialContextLoaded
);
475 SERIALIZE_SCALAR(brk_point
);
476 SERIALIZE_SCALAR(stack_base
);
477 SERIALIZE_SCALAR(stack_size
);
478 SERIALIZE_SCALAR(stack_min
);
479 SERIALIZE_SCALAR(next_thread_stack_base
);
480 SERIALIZE_SCALAR(mmap_start
);
481 SERIALIZE_SCALAR(mmap_end
);
482 SERIALIZE_SCALAR(nxm_start
);
483 SERIALIZE_SCALAR(nxm_end
);
485 pTable
->serialize(os
);
486 for (int x
= 0; x
<= MAX_FD
; x
++) {
487 nameOut(os
, csprintf("%s.FdMap%d", name(), x
));
488 fd_map
[x
].serialize(os
);
494 Process::unserialize(Checkpoint
*cp
, const std::string
§ion
)
496 UNSERIALIZE_SCALAR(initialContextLoaded
);
497 UNSERIALIZE_SCALAR(brk_point
);
498 UNSERIALIZE_SCALAR(stack_base
);
499 UNSERIALIZE_SCALAR(stack_size
);
500 UNSERIALIZE_SCALAR(stack_min
);
501 UNSERIALIZE_SCALAR(next_thread_stack_base
);
502 UNSERIALIZE_SCALAR(mmap_start
);
503 UNSERIALIZE_SCALAR(mmap_end
);
504 UNSERIALIZE_SCALAR(nxm_start
);
505 UNSERIALIZE_SCALAR(nxm_end
);
506 pTable
->unserialize(cp
, section
);
507 for (int x
= 0; x
<= MAX_FD
; x
++) {
508 fd_map
[x
].unserialize(cp
, csprintf("%s.FdMap%d", section
, x
));
512 checkpointRestored
= true;
517 ////////////////////////////////////////////////////////////////////////
519 // LiveProcess member definitions
521 ////////////////////////////////////////////////////////////////////////
524 LiveProcess::LiveProcess(LiveProcessParams
* params
, ObjectFile
*_objFile
)
525 : Process(params
), objFile(_objFile
),
526 argv(params
->cmd
), envp(params
->env
), cwd(params
->cwd
)
529 __euid
= params
->euid
;
531 __egid
= params
->egid
;
533 __ppid
= params
->ppid
;
535 prog_fname
= params
->cmd
[0];
537 // load up symbols, if any... these may be used for debugging or
539 if (!debugSymbolTable
) {
540 debugSymbolTable
= new SymbolTable();
541 if (!objFile
->loadGlobalSymbols(debugSymbolTable
) ||
542 !objFile
->loadLocalSymbols(debugSymbolTable
)) {
543 // didn't load any symbols
544 delete debugSymbolTable
;
545 debugSymbolTable
= NULL
;
551 LiveProcess::argsInit(int intSize
, int pageSize
)
555 // load object file into target memory
556 objFile
->loadSections(initVirtMem
);
558 // Calculate how much space we need for arg & env arrays.
559 int argv_array_size
= intSize
* (argv
.size() + 1);
560 int envp_array_size
= intSize
* (envp
.size() + 1);
561 int arg_data_size
= 0;
562 for (int i
= 0; i
< argv
.size(); ++i
) {
563 arg_data_size
+= argv
[i
].size() + 1;
565 int env_data_size
= 0;
566 for (int i
= 0; i
< envp
.size(); ++i
) {
567 env_data_size
+= envp
[i
].size() + 1;
571 argv_array_size
+ envp_array_size
+ arg_data_size
+ env_data_size
;
572 if (space_needed
< 32*1024)
573 space_needed
= 32*1024;
575 // set bottom of stack
576 stack_min
= stack_base
- space_needed
;
578 stack_min
= roundDown(stack_min
, pageSize
);
579 stack_size
= stack_base
- stack_min
;
581 pTable
->allocate(stack_min
, roundUp(stack_size
, pageSize
));
583 // map out initial stack contents
584 Addr argv_array_base
= stack_min
+ intSize
; // room for argc
585 Addr envp_array_base
= argv_array_base
+ argv_array_size
;
586 Addr arg_data_base
= envp_array_base
+ envp_array_size
;
587 Addr env_data_base
= arg_data_base
+ arg_data_size
;
589 // write contents to stack
590 uint64_t argc
= argv
.size();
592 argc
= htog((uint64_t)argc
);
593 else if (intSize
== 4)
594 argc
= htog((uint32_t)argc
);
596 panic("Unknown int size");
598 initVirtMem
->writeBlob(stack_min
, (uint8_t*)&argc
, intSize
);
600 copyStringArray(argv
, argv_array_base
, arg_data_base
, initVirtMem
);
601 copyStringArray(envp
, envp_array_base
, env_data_base
, initVirtMem
);
603 assert(NumArgumentRegs
>= 2);
604 threadContexts
[0]->setIntReg(ArgumentReg
[0], argc
);
605 threadContexts
[0]->setIntReg(ArgumentReg
[1], argv_array_base
);
606 threadContexts
[0]->setIntReg(StackPointerReg
, stack_min
);
608 Addr prog_entry
= objFile
->entryPoint();
609 threadContexts
[0]->setPC(prog_entry
);
610 threadContexts
[0]->setNextPC(prog_entry
+ sizeof(MachInst
));
612 #if THE_ISA != ALPHA_ISA //e.g. MIPS or Sparc
613 threadContexts
[0]->setNextNPC(prog_entry
+ (2 * sizeof(MachInst
)));
620 LiveProcess::syscall(int64_t callnum
, ThreadContext
*tc
)
624 SyscallDesc
*desc
= getDesc(callnum
);
626 fatal("Syscall %d out of range", callnum
);
628 desc
->doSyscall(callnum
, this, tc
);
632 LiveProcess::create(LiveProcessParams
* params
)
634 LiveProcess
*process
= NULL
;
637 params
->executable
== "" ? params
->cmd
[0] : params
->executable
;
638 ObjectFile
*objFile
= createObjectFile(executable
);
639 if (objFile
== NULL
) {
640 fatal("Can't load object file %s", executable
);
643 if (objFile
->isDynamic())
644 fatal("Object file is a dynamic executable however only static "
645 "executables are supported!\n Please recompile your "
646 "executable as a static binary and try again.\n");
648 #if THE_ISA == ALPHA_ISA
649 if (objFile
->hasTLS())
650 fatal("Object file has a TLS section and single threaded TLS is not\n"
651 " currently supported for Alpha! Please recompile your "
652 "executable with \n a non-TLS toolchain.\n");
654 if (objFile
->getArch() != ObjectFile::Alpha
)
655 fatal("Object file architecture does not match compiled ISA (Alpha).");
656 switch (objFile
->getOpSys()) {
657 case ObjectFile::Tru64
:
658 process
= new AlphaTru64Process(params
, objFile
);
661 case ObjectFile::Linux
:
662 process
= new AlphaLinuxProcess(params
, objFile
);
666 fatal("Unknown/unsupported operating system.");
668 #elif THE_ISA == SPARC_ISA
669 if (objFile
->getArch() != ObjectFile::SPARC64
&& objFile
->getArch() != ObjectFile::SPARC32
)
670 fatal("Object file architecture does not match compiled ISA (SPARC).");
671 switch (objFile
->getOpSys()) {
672 case ObjectFile::Linux
:
673 if (objFile
->getArch() == ObjectFile::SPARC64
) {
674 process
= new Sparc64LinuxProcess(params
, objFile
);
676 process
= new Sparc32LinuxProcess(params
, objFile
);
681 case ObjectFile::Solaris
:
682 process
= new SparcSolarisProcess(params
, objFile
);
685 fatal("Unknown/unsupported operating system.");
687 #elif THE_ISA == X86_ISA
688 if (objFile
->getArch() != ObjectFile::X86
)
689 fatal("Object file architecture does not match compiled ISA (x86).");
690 switch (objFile
->getOpSys()) {
691 case ObjectFile::Linux
:
692 process
= new X86LinuxProcess(params
, objFile
);
695 fatal("Unknown/unsupported operating system.");
697 #elif THE_ISA == MIPS_ISA
698 if (objFile
->getArch() != ObjectFile::Mips
)
699 fatal("Object file architecture does not match compiled ISA (MIPS).");
700 switch (objFile
->getOpSys()) {
701 case ObjectFile::Linux
:
702 process
= new MipsLinuxProcess(params
, objFile
);
706 fatal("Unknown/unsupported operating system.");
708 #elif THE_ISA == ARM_ISA
709 if (objFile
->getArch() != ObjectFile::Arm
)
710 fatal("Object file architecture does not match compiled ISA (ARM).");
711 switch (objFile
->getOpSys()) {
712 case ObjectFile::Linux
:
713 process
= new ArmLinuxProcess(params
, objFile
);
717 fatal("Unknown/unsupported operating system.");
720 #error "THE_ISA not set"
725 fatal("Unknown error creating process object.");
730 LiveProcessParams::create()
732 return LiveProcess::create(this);