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
38 #include "arch/remote_gdb.hh"
39 #include "base/intmath.hh"
40 #include "base/loader/object_file.hh"
41 #include "base/loader/symtab.hh"
42 #include "base/statistics.hh"
43 #include "config/full_system.hh"
44 #include "cpu/thread_context.hh"
45 #include "mem/page_table.hh"
46 #include "mem/physical.hh"
47 #include "mem/translating_port.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(const string
&nm
,
88 int stdin_fd
, // initial I/O descriptors
91 : SimObject(makeParams(nm
)), system(_system
)
93 M5_pid
= system
->allocatePID();
94 // initialize first 3 fds (stdin, stdout, stderr)
95 fd_map
[STDIN_FILENO
] = stdin_fd
;
96 fd_map
[STDOUT_FILENO
] = stdout_fd
;
97 fd_map
[STDERR_FILENO
] = stderr_fd
;
99 // mark remaining fds as free
100 for (int i
= 3; i
<= MAX_FD
; ++i
) {
104 mmap_start
= mmap_end
= 0;
105 nxm_start
= nxm_end
= 0;
106 pTable
= new PageTable(system
);
107 // other parameters will be initialized when the program is loaded
114 using namespace Stats
;
117 .name(name() + ".PROG:num_syscalls")
118 .desc("Number of system calls")
123 // static helper functions
126 Process::openInputFile(const string
&filename
)
128 int fd
= open(filename
.c_str(), O_RDONLY
);
132 cerr
<< "unable to open \"" << filename
<< "\" for reading\n";
133 fatal("can't open input file");
141 Process::openOutputFile(const string
&filename
)
143 int fd
= open(filename
.c_str(), O_WRONLY
| O_CREAT
| O_TRUNC
, 0774);
147 cerr
<< "unable to open \"" << filename
<< "\" for writing\n";
148 fatal("can't open output file");
156 Process::registerThreadContext(ThreadContext
*tc
)
159 int myIndex
= threadContexts
.size();
160 threadContexts
.push_back(tc
);
162 // RemoteGDB *rgdb = new RemoteGDB(system, tc);
163 // GDBListener *gdbl = new GDBListener(rgdb, 7000 + myIndex);
167 // remoteGDB.push_back(rgdb);
169 // return CPU number to caller
176 if (threadContexts
.empty())
177 fatal("Process %s is not associated with any CPUs!\n", name());
179 // first thread context for this process... initialize & enable
180 ThreadContext
*tc
= threadContexts
[0];
182 // mark this context as active so it will start ticking.
186 mem_port
= system
->physmem
->getPort("functional");
187 initVirtMem
= new TranslatingPort("process init port", this,
188 TranslatingPort::Always
);
189 mem_port
->setPeer(initVirtMem
);
190 initVirtMem
->setPeer(mem_port
);
194 Process::replaceThreadContext(ThreadContext
*tc
, int tcIndex
)
196 if (tcIndex
>= threadContexts
.size()) {
197 panic("replaceThreadContext: bad tcIndex, %d >= %d\n",
198 tcIndex
, threadContexts
.size());
201 threadContexts
[tcIndex
] = tc
;
204 // map simulator fd sim_fd to target fd tgt_fd
206 Process::dup_fd(int sim_fd
, int tgt_fd
)
208 if (tgt_fd
< 0 || tgt_fd
> MAX_FD
)
209 panic("Process::dup_fd tried to dup past MAX_FD (%d)", tgt_fd
);
211 fd_map
[tgt_fd
] = sim_fd
;
215 // generate new target fd for sim_fd
217 Process::alloc_fd(int sim_fd
)
219 // in case open() returns an error, don't allocate a new fd
223 // find first free target fd
224 for (int free_fd
= 0; free_fd
< MAX_FD
; ++free_fd
) {
225 if (fd_map
[free_fd
] == -1) {
226 fd_map
[free_fd
] = sim_fd
;
231 panic("Process::alloc_fd: out of file descriptors!");
235 // free target fd (e.g., after close)
237 Process::free_fd(int tgt_fd
)
239 if (fd_map
[tgt_fd
] == -1)
240 warn("Process::free_fd: request to free unused fd %d", tgt_fd
);
246 // look up simulator fd for given target fd
248 Process::sim_fd(int tgt_fd
)
253 return fd_map
[tgt_fd
];
257 Process::checkAndAllocNextPage(Addr vaddr
)
259 // if this is an initial write we might not have
260 if (vaddr
>= stack_min
&& vaddr
< stack_base
) {
261 pTable
->allocate(roundDown(vaddr
, VMPageSize
), VMPageSize
);
265 // We've accessed the next page of the stack, so extend the stack
267 if(vaddr
< stack_min
&& vaddr
>= stack_min
- TheISA::PageBytes
)
269 stack_min
-= TheISA::PageBytes
;
270 if(stack_base
- stack_min
> 8*1024*1024)
271 fatal("Over max stack size for one thread\n");
272 pTable
->allocate(stack_min
, TheISA::PageBytes
);
273 warn("Increasing stack size by one page.");
280 Process::serialize(std::ostream
&os
)
282 SERIALIZE_SCALAR(initialContextLoaded
);
283 SERIALIZE_SCALAR(brk_point
);
284 SERIALIZE_SCALAR(stack_base
);
285 SERIALIZE_SCALAR(stack_size
);
286 SERIALIZE_SCALAR(stack_min
);
287 SERIALIZE_SCALAR(next_thread_stack_base
);
288 SERIALIZE_SCALAR(mmap_start
);
289 SERIALIZE_SCALAR(mmap_end
);
290 SERIALIZE_SCALAR(nxm_start
);
291 SERIALIZE_SCALAR(nxm_end
);
292 SERIALIZE_ARRAY(fd_map
, MAX_FD
);
294 pTable
->serialize(os
);
298 Process::unserialize(Checkpoint
*cp
, const std::string
§ion
)
300 UNSERIALIZE_SCALAR(initialContextLoaded
);
301 UNSERIALIZE_SCALAR(brk_point
);
302 UNSERIALIZE_SCALAR(stack_base
);
303 UNSERIALIZE_SCALAR(stack_size
);
304 UNSERIALIZE_SCALAR(stack_min
);
305 UNSERIALIZE_SCALAR(next_thread_stack_base
);
306 UNSERIALIZE_SCALAR(mmap_start
);
307 UNSERIALIZE_SCALAR(mmap_end
);
308 UNSERIALIZE_SCALAR(nxm_start
);
309 UNSERIALIZE_SCALAR(nxm_end
);
310 UNSERIALIZE_ARRAY(fd_map
, MAX_FD
);
312 pTable
->unserialize(cp
, section
);
316 ////////////////////////////////////////////////////////////////////////
318 // LiveProcess member definitions
320 ////////////////////////////////////////////////////////////////////////
323 LiveProcess::LiveProcess(const string
&nm
, ObjectFile
*_objFile
,
325 int stdin_fd
, int stdout_fd
, int stderr_fd
,
326 vector
<string
> &_argv
, vector
<string
> &_envp
,
328 uint64_t _uid
, uint64_t _euid
,
329 uint64_t _gid
, uint64_t _egid
,
330 uint64_t _pid
, uint64_t _ppid
)
331 : Process(nm
, _system
, stdin_fd
, stdout_fd
, stderr_fd
),
332 objFile(_objFile
), argv(_argv
), envp(_envp
), cwd(_cwd
)
341 prog_fname
= argv
[0];
343 // load up symbols, if any... these may be used for debugging or
345 if (!debugSymbolTable
) {
346 debugSymbolTable
= new SymbolTable();
347 if (!objFile
->loadGlobalSymbols(debugSymbolTable
) ||
348 !objFile
->loadLocalSymbols(debugSymbolTable
)) {
349 // didn't load any symbols
350 delete debugSymbolTable
;
351 debugSymbolTable
= NULL
;
357 LiveProcess::argsInit(int intSize
, int pageSize
)
361 // load object file into target memory
362 objFile
->loadSections(initVirtMem
);
364 // Calculate how much space we need for arg & env arrays.
365 int argv_array_size
= intSize
* (argv
.size() + 1);
366 int envp_array_size
= intSize
* (envp
.size() + 1);
367 int arg_data_size
= 0;
368 for (int i
= 0; i
< argv
.size(); ++i
) {
369 arg_data_size
+= argv
[i
].size() + 1;
371 int env_data_size
= 0;
372 for (int i
= 0; i
< envp
.size(); ++i
) {
373 env_data_size
+= envp
[i
].size() + 1;
377 argv_array_size
+ envp_array_size
+ arg_data_size
+ env_data_size
;
378 if (space_needed
< 32*1024)
379 space_needed
= 32*1024;
381 // set bottom of stack
382 stack_min
= stack_base
- space_needed
;
384 stack_min
= roundDown(stack_min
, pageSize
);
385 stack_size
= stack_base
- stack_min
;
387 pTable
->allocate(stack_min
, roundUp(stack_size
, pageSize
));
389 // map out initial stack contents
390 Addr argv_array_base
= stack_min
+ intSize
; // room for argc
391 Addr envp_array_base
= argv_array_base
+ argv_array_size
;
392 Addr arg_data_base
= envp_array_base
+ envp_array_size
;
393 Addr env_data_base
= arg_data_base
+ arg_data_size
;
395 // write contents to stack
396 uint64_t argc
= argv
.size();
398 argc
= htog((uint64_t)argc
);
399 else if (intSize
== 4)
400 argc
= htog((uint32_t)argc
);
402 panic("Unknown int size");
404 initVirtMem
->writeBlob(stack_min
, (uint8_t*)&argc
, intSize
);
406 copyStringArray(argv
, argv_array_base
, arg_data_base
, initVirtMem
);
407 copyStringArray(envp
, envp_array_base
, env_data_base
, initVirtMem
);
409 assert(NumArgumentRegs
>= 2);
410 threadContexts
[0]->setIntReg(ArgumentReg
[0], argc
);
411 threadContexts
[0]->setIntReg(ArgumentReg
[1], argv_array_base
);
412 threadContexts
[0]->setIntReg(StackPointerReg
, stack_min
);
414 Addr prog_entry
= objFile
->entryPoint();
415 threadContexts
[0]->setPC(prog_entry
);
416 threadContexts
[0]->setNextPC(prog_entry
+ sizeof(MachInst
));
418 #if THE_ISA != ALPHA_ISA //e.g. MIPS or Sparc
419 threadContexts
[0]->setNextNPC(prog_entry
+ (2 * sizeof(MachInst
)));
426 LiveProcess::syscall(int64_t callnum
, ThreadContext
*tc
)
430 SyscallDesc
*desc
= getDesc(callnum
);
432 fatal("Syscall %d out of range", callnum
);
434 desc
->doSyscall(callnum
, this, tc
);
438 LiveProcess::create(const std::string
&nm
, System
*system
, int stdin_fd
,
439 int stdout_fd
, int stderr_fd
, std::string executable
,
440 std::vector
<std::string
> &argv
,
441 std::vector
<std::string
> &envp
,
442 const std::string
&cwd
,
443 uint64_t _uid
, uint64_t _euid
,
444 uint64_t _gid
, uint64_t _egid
,
445 uint64_t _pid
, uint64_t _ppid
)
447 LiveProcess
*process
= NULL
;
449 ObjectFile
*objFile
= createObjectFile(executable
);
450 if (objFile
== NULL
) {
451 fatal("Can't load object file %s", executable
);
454 if (objFile
->isDynamic())
455 fatal("Object file is a dynamic executable however only static "
456 "executables are supported!\n Please recompile your "
457 "executable as a static binary and try again.\n");
459 if (objFile
->hasTLS())
460 fatal("Object file has a TLS section and loading of TLS sections "
461 "are not currently supported!\n Please recompile your "
462 "executable with a non-TLS toolchain or add TLS support to "
463 "M5 and try again\n");
465 #if THE_ISA == ALPHA_ISA
466 if (objFile
->getArch() != ObjectFile::Alpha
)
467 fatal("Object file architecture does not match compiled ISA (Alpha).");
468 switch (objFile
->getOpSys()) {
469 case ObjectFile::Tru64
:
470 process
= new AlphaTru64Process(nm
, objFile
, system
,
471 stdin_fd
, stdout_fd
, stderr_fd
,
473 _uid
, _euid
, _gid
, _egid
, _pid
, _ppid
);
476 case ObjectFile::Linux
:
477 process
= new AlphaLinuxProcess(nm
, objFile
, system
,
478 stdin_fd
, stdout_fd
, stderr_fd
,
480 _uid
, _euid
, _gid
, _egid
, _pid
, _ppid
);
484 fatal("Unknown/unsupported operating system.");
486 #elif THE_ISA == SPARC_ISA
487 if (objFile
->getArch() != ObjectFile::SPARC64
&& objFile
->getArch() != ObjectFile::SPARC32
)
488 fatal("Object file architecture does not match compiled ISA (SPARC).");
489 switch (objFile
->getOpSys()) {
490 case ObjectFile::Linux
:
491 if (objFile
->getArch() == ObjectFile::SPARC64
) {
492 process
= new Sparc64LinuxProcess(nm
, objFile
, system
,
493 stdin_fd
, stdout_fd
, stderr_fd
,
498 process
= new Sparc32LinuxProcess(nm
, objFile
, system
,
499 stdin_fd
, stdout_fd
, stderr_fd
,
507 case ObjectFile::Solaris
:
508 process
= new SparcSolarisProcess(nm
, objFile
, system
,
509 stdin_fd
, stdout_fd
, stderr_fd
,
511 _uid
, _euid
, _gid
, _egid
, _pid
, _ppid
);
514 fatal("Unknown/unsupported operating system.");
516 #elif THE_ISA == X86_ISA
517 if (objFile
->getArch() != ObjectFile::X86
)
518 fatal("Object file architecture does not match compiled ISA (x86).");
519 switch (objFile
->getOpSys()) {
520 case ObjectFile::Linux
:
521 process
= new X86LinuxProcess(nm
, objFile
, system
,
522 stdin_fd
, stdout_fd
, stderr_fd
,
528 fatal("Unknown/unsupported operating system.");
530 #elif THE_ISA == MIPS_ISA
531 if (objFile
->getArch() != ObjectFile::Mips
)
532 fatal("Object file architecture does not match compiled ISA (MIPS).");
533 switch (objFile
->getOpSys()) {
534 case ObjectFile::Linux
:
535 process
= new MipsLinuxProcess(nm
, objFile
, system
,
536 stdin_fd
, stdout_fd
, stderr_fd
,
538 _uid
, _euid
, _gid
, _egid
, _pid
, _ppid
);
542 fatal("Unknown/unsupported operating system.");
545 #error "THE_ISA not set"
550 fatal("Unknown error creating process object.");
555 LiveProcessParams::create()
560 // initialize file descriptors to default: same as simulator
561 int stdin_fd
, stdout_fd
, stderr_fd
;
563 if (in
== "stdin" || in
== "cin")
564 stdin_fd
= STDIN_FILENO
;
566 stdin_fd
= Process::openInputFile(input
);
568 if (out
== "stdout" || out
== "cout")
569 stdout_fd
= STDOUT_FILENO
;
570 else if (out
== "stderr" || out
== "cerr")
571 stdout_fd
= STDERR_FILENO
;
573 stdout_fd
= Process::openOutputFile(out
);
575 stderr_fd
= (stdout_fd
!= STDOUT_FILENO
) ? stdout_fd
: STDERR_FILENO
;
577 return LiveProcess::create(name
, system
,
578 stdin_fd
, stdout_fd
, stderr_fd
,
579 (string
)executable
== "" ? cmd
[0] : executable
,
581 uid
, euid
, gid
, egid
, pid
, ppid
);