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 "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 "sim/builder.hh"
48 #include "sim/process.hh"
49 #include "sim/stats.hh"
50 #include "sim/syscall_emul.hh"
51 #include "sim/system.hh"
53 #include "arch/isa_specific.hh"
54 #if THE_ISA == ALPHA_ISA
55 #include "arch/alpha/linux/process.hh"
56 #include "arch/alpha/tru64/process.hh"
57 #elif THE_ISA == SPARC_ISA
58 #include "arch/sparc/linux/process.hh"
59 #include "arch/sparc/solaris/process.hh"
60 #elif THE_ISA == MIPS_ISA
61 #include "arch/mips/linux/process.hh"
63 #error "THE_ISA not set"
68 using namespace TheISA
;
71 // The purpose of this code is to fake the loader & syscall mechanism
72 // when there's no OS: thus there's no resone to use it in FULL_SYSTEM
73 // mode when we do have an OS
76 #error "process.cc not compatible with FULL_SYSTEM"
79 // current number of allocated processes
80 int num_processes
= 0;
82 Process::Process(const string
&nm
,
84 int stdin_fd
, // initial I/O descriptors
87 : SimObject(nm
), system(_system
)
89 // initialize first 3 fds (stdin, stdout, stderr)
90 fd_map
[STDIN_FILENO
] = stdin_fd
;
91 fd_map
[STDOUT_FILENO
] = stdout_fd
;
92 fd_map
[STDERR_FILENO
] = stderr_fd
;
94 // mark remaining fds as free
95 for (int i
= 3; i
<= MAX_FD
; ++i
) {
99 mmap_start
= mmap_end
= 0;
100 nxm_start
= nxm_end
= 0;
101 pTable
= new PageTable(system
);
102 // other parameters will be initialized when the program is loaded
109 using namespace Stats
;
112 .name(name() + ".PROG:num_syscalls")
113 .desc("Number of system calls")
118 // static helper functions
121 Process::openInputFile(const string
&filename
)
123 int fd
= open(filename
.c_str(), O_RDONLY
);
127 cerr
<< "unable to open \"" << filename
<< "\" for reading\n";
128 fatal("can't open input file");
136 Process::openOutputFile(const string
&filename
)
138 int fd
= open(filename
.c_str(), O_WRONLY
| O_CREAT
| O_TRUNC
, 0774);
142 cerr
<< "unable to open \"" << filename
<< "\" for writing\n";
143 fatal("can't open output file");
151 Process::registerThreadContext(ThreadContext
*tc
)
154 int myIndex
= threadContexts
.size();
155 threadContexts
.push_back(tc
);
157 // return CPU number to caller
164 if (threadContexts
.empty())
165 fatal("Process %s is not associated with any CPUs!\n", name());
167 // first thread context for this process... initialize & enable
168 ThreadContext
*tc
= threadContexts
[0];
170 // mark this context as active so it will start ticking.
174 mem_port
= system
->physmem
->getPort("functional");
175 initVirtMem
= new TranslatingPort("process init port", pTable
, true);
176 mem_port
->setPeer(initVirtMem
);
177 initVirtMem
->setPeer(mem_port
);
181 Process::replaceThreadContext(ThreadContext
*tc
, int tcIndex
)
183 if (tcIndex
>= threadContexts
.size()) {
184 panic("replaceThreadContext: bad tcIndex, %d >= %d\n",
185 tcIndex
, threadContexts
.size());
188 threadContexts
[tcIndex
] = tc
;
191 // map simulator fd sim_fd to target fd tgt_fd
193 Process::dup_fd(int sim_fd
, int tgt_fd
)
195 if (tgt_fd
< 0 || tgt_fd
> MAX_FD
)
196 panic("Process::dup_fd tried to dup past MAX_FD (%d)", tgt_fd
);
198 fd_map
[tgt_fd
] = sim_fd
;
202 // generate new target fd for sim_fd
204 Process::alloc_fd(int sim_fd
)
206 // in case open() returns an error, don't allocate a new fd
210 // find first free target fd
211 for (int free_fd
= 0; free_fd
< MAX_FD
; ++free_fd
) {
212 if (fd_map
[free_fd
] == -1) {
213 fd_map
[free_fd
] = sim_fd
;
218 panic("Process::alloc_fd: out of file descriptors!");
222 // free target fd (e.g., after close)
224 Process::free_fd(int tgt_fd
)
226 if (fd_map
[tgt_fd
] == -1)
227 warn("Process::free_fd: request to free unused fd %d", tgt_fd
);
233 // look up simulator fd for given target fd
235 Process::sim_fd(int tgt_fd
)
240 return fd_map
[tgt_fd
];
246 // need to declare these here since there is no concrete Process type
247 // that can be constructed (i.e., no REGISTER_SIM_OBJECT() macro call,
248 // which is where these get declared for concrete types).
250 DEFINE_SIM_OBJECT_CLASS_NAME("Process", Process
)
253 ////////////////////////////////////////////////////////////////////////
255 // LiveProcess member definitions
257 ////////////////////////////////////////////////////////////////////////
261 copyStringArray(vector
<string
> &strings
, Addr array_ptr
, Addr data_ptr
,
262 TranslatingPort
* memPort
)
265 for (int i
= 0; i
< strings
.size(); ++i
) {
266 data_ptr_swap
= htog(data_ptr
);
267 memPort
->writeBlob(array_ptr
, (uint8_t*)&data_ptr_swap
, sizeof(Addr
));
268 memPort
->writeString(data_ptr
, strings
[i
].c_str());
269 array_ptr
+= sizeof(Addr
);
270 data_ptr
+= strings
[i
].size() + 1;
272 // add NULL terminator
275 memPort
->writeBlob(array_ptr
, (uint8_t*)&data_ptr
, sizeof(Addr
));
278 LiveProcess::LiveProcess(const string
&nm
, ObjectFile
*_objFile
,
280 int stdin_fd
, int stdout_fd
, int stderr_fd
,
281 vector
<string
> &_argv
, vector
<string
> &_envp
,
282 uint64_t _uid
, uint64_t _euid
,
283 uint64_t _gid
, uint64_t _egid
,
284 uint64_t _pid
, uint64_t _ppid
)
285 : Process(nm
, _system
, stdin_fd
, stdout_fd
, stderr_fd
),
286 objFile(_objFile
), argv(_argv
), envp(_envp
)
295 prog_fname
= argv
[0];
297 // load up symbols, if any... these may be used for debugging or
299 if (!debugSymbolTable
) {
300 debugSymbolTable
= new SymbolTable();
301 if (!objFile
->loadGlobalSymbols(debugSymbolTable
) ||
302 !objFile
->loadLocalSymbols(debugSymbolTable
)) {
303 // didn't load any symbols
304 delete debugSymbolTable
;
305 debugSymbolTable
= NULL
;
311 LiveProcess::argsInit(int intSize
, int pageSize
)
315 // load object file into target memory
316 objFile
->loadSections(initVirtMem
);
318 // Calculate how much space we need for arg & env arrays.
319 int argv_array_size
= intSize
* (argv
.size() + 1);
320 int envp_array_size
= intSize
* (envp
.size() + 1);
321 int arg_data_size
= 0;
322 for (int i
= 0; i
< argv
.size(); ++i
) {
323 arg_data_size
+= argv
[i
].size() + 1;
325 int env_data_size
= 0;
326 for (int i
= 0; i
< envp
.size(); ++i
) {
327 env_data_size
+= envp
[i
].size() + 1;
331 argv_array_size
+ envp_array_size
+ arg_data_size
+ env_data_size
;
332 if (space_needed
< 32*1024)
333 space_needed
= 32*1024;
335 // set bottom of stack
336 stack_min
= stack_base
- space_needed
;
338 stack_min
= roundDown(stack_min
, pageSize
);
339 stack_size
= stack_base
- stack_min
;
341 pTable
->allocate(stack_min
, roundUp(stack_size
, pageSize
));
343 // map out initial stack contents
344 Addr argv_array_base
= stack_min
+ intSize
; // room for argc
345 Addr envp_array_base
= argv_array_base
+ argv_array_size
;
346 Addr arg_data_base
= envp_array_base
+ envp_array_size
;
347 Addr env_data_base
= arg_data_base
+ arg_data_size
;
349 // write contents to stack
350 uint64_t argc
= argv
.size();
352 argc
= htog((uint64_t)argc
);
353 else if (intSize
== 4)
354 argc
= htog((uint32_t)argc
);
356 panic("Unknown int size");
358 initVirtMem
->writeBlob(stack_min
, (uint8_t*)&argc
, intSize
);
360 copyStringArray(argv
, argv_array_base
, arg_data_base
, initVirtMem
);
361 copyStringArray(envp
, envp_array_base
, env_data_base
, initVirtMem
);
363 threadContexts
[0]->setIntReg(ArgumentReg0
, argc
);
364 threadContexts
[0]->setIntReg(ArgumentReg1
, argv_array_base
);
365 threadContexts
[0]->setIntReg(StackPointerReg
, stack_min
);
367 Addr prog_entry
= objFile
->entryPoint();
368 threadContexts
[0]->setPC(prog_entry
);
369 threadContexts
[0]->setNextPC(prog_entry
+ sizeof(MachInst
));
371 #if THE_ISA != ALPHA_ISA //e.g. MIPS or Sparc
372 threadContexts
[0]->setNextNPC(prog_entry
+ (2 * sizeof(MachInst
)));
379 LiveProcess::syscall(int64_t callnum
, ThreadContext
*tc
)
383 SyscallDesc
*desc
= getDesc(callnum
);
385 fatal("Syscall %d out of range", callnum
);
387 desc
->doSyscall(callnum
, this, tc
);
391 LiveProcess::create(const std::string
&nm
, System
*system
, int stdin_fd
,
392 int stdout_fd
, int stderr_fd
, std::string executable
,
393 std::vector
<std::string
> &argv
,
394 std::vector
<std::string
> &envp
,
395 uint64_t _uid
, uint64_t _euid
,
396 uint64_t _gid
, uint64_t _egid
,
397 uint64_t _pid
, uint64_t _ppid
)
399 LiveProcess
*process
= NULL
;
401 ObjectFile
*objFile
= createObjectFile(executable
);
402 if (objFile
== NULL
) {
403 fatal("Can't load object file %s", executable
);
406 #if THE_ISA == ALPHA_ISA
407 if (objFile
->getArch() != ObjectFile::Alpha
)
408 fatal("Object file architecture does not match compiled ISA (Alpha).");
409 switch (objFile
->getOpSys()) {
410 case ObjectFile::Tru64
:
411 process
= new AlphaTru64Process(nm
, objFile
, system
,
412 stdin_fd
, stdout_fd
, stderr_fd
,
414 _uid
, _euid
, _gid
, _egid
, _pid
, _ppid
);
417 case ObjectFile::Linux
:
418 process
= new AlphaLinuxProcess(nm
, objFile
, system
,
419 stdin_fd
, stdout_fd
, stderr_fd
,
421 _uid
, _euid
, _gid
, _egid
, _pid
, _ppid
);
425 fatal("Unknown/unsupported operating system.");
427 #elif THE_ISA == SPARC_ISA
428 if (objFile
->getArch() != ObjectFile::SPARC
)
429 fatal("Object file architecture does not match compiled ISA (SPARC).");
430 switch (objFile
->getOpSys()) {
431 case ObjectFile::Linux
:
432 process
= new SparcLinuxProcess(nm
, objFile
, system
,
433 stdin_fd
, stdout_fd
, stderr_fd
,
435 _uid
, _euid
, _gid
, _egid
, _pid
, _ppid
);
439 case ObjectFile::Solaris
:
440 process
= new SparcSolarisProcess(nm
, objFile
, system
,
441 stdin_fd
, stdout_fd
, stderr_fd
,
443 _uid
, _euid
, _gid
, _egid
, _pid
, _ppid
);
446 fatal("Unknown/unsupported operating system.");
448 #elif THE_ISA == MIPS_ISA
449 if (objFile
->getArch() != ObjectFile::Mips
)
450 fatal("Object file architecture does not match compiled ISA (MIPS).");
451 switch (objFile
->getOpSys()) {
452 case ObjectFile::Linux
:
453 process
= new MipsLinuxProcess(nm
, objFile
, system
,
454 stdin_fd
, stdout_fd
, stderr_fd
,
456 _uid
, _euid
, _gid
, _egid
, _pid
, _ppid
);
460 fatal("Unknown/unsupported operating system.");
463 #error "THE_ISA not set"
468 fatal("Unknown error creating process object.");
473 BEGIN_DECLARE_SIM_OBJECT_PARAMS(LiveProcess
)
475 VectorParam
<string
> cmd
;
476 Param
<string
> executable
;
478 Param
<string
> output
;
479 VectorParam
<string
> env
;
480 SimObjectParam
<System
*> system
;
482 Param
<uint64_t> euid
;
484 Param
<uint64_t> egid
;
486 Param
<uint64_t> ppid
;
488 END_DECLARE_SIM_OBJECT_PARAMS(LiveProcess
)
491 BEGIN_INIT_SIM_OBJECT_PARAMS(LiveProcess
)
493 INIT_PARAM(cmd
, "command line (executable plus arguments)"),
494 INIT_PARAM(executable
, "executable (overrides cmd[0] if set)"),
495 INIT_PARAM(input
, "filename for stdin (dflt: use sim stdin)"),
496 INIT_PARAM(output
, "filename for stdout/stderr (dflt: use sim stdout)"),
497 INIT_PARAM(env
, "environment settings"),
498 INIT_PARAM(system
, "system"),
499 INIT_PARAM(uid
, "user id"),
500 INIT_PARAM(euid
, "effective user id"),
501 INIT_PARAM(gid
, "group id"),
502 INIT_PARAM(egid
, "effective group id"),
503 INIT_PARAM(pid
, "process id"),
504 INIT_PARAM(ppid
, "parent process id")
506 END_INIT_SIM_OBJECT_PARAMS(LiveProcess
)
509 CREATE_SIM_OBJECT(LiveProcess
)
514 // initialize file descriptors to default: same as simulator
515 int stdin_fd
, stdout_fd
, stderr_fd
;
517 if (in
== "stdin" || in
== "cin")
518 stdin_fd
= STDIN_FILENO
;
520 stdin_fd
= Process::openInputFile(input
);
522 if (out
== "stdout" || out
== "cout")
523 stdout_fd
= STDOUT_FILENO
;
524 else if (out
== "stderr" || out
== "cerr")
525 stdout_fd
= STDERR_FILENO
;
527 stdout_fd
= Process::openOutputFile(out
);
529 stderr_fd
= (stdout_fd
!= STDOUT_FILENO
) ? stdout_fd
: STDERR_FILENO
;
531 return LiveProcess::create(getInstanceName(), system
,
532 stdin_fd
, stdout_fd
, stderr_fd
,
533 (string
)executable
== "" ? cmd
[0] : executable
,
535 uid
, euid
, gid
, egid
, pid
, ppid
);
539 REGISTER_SIM_OBJECT("LiveProcess", LiveProcess
)