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 "sim/builder.hh"
49 #include "sim/process.hh"
50 #include "sim/stats.hh"
51 #include "sim/syscall_emul.hh"
52 #include "sim/system.hh"
54 #include "arch/isa_specific.hh"
55 #if THE_ISA == ALPHA_ISA
56 #include "arch/alpha/linux/process.hh"
57 #include "arch/alpha/tru64/process.hh"
58 #elif THE_ISA == SPARC_ISA
59 #include "arch/sparc/linux/process.hh"
60 #include "arch/sparc/solaris/process.hh"
61 #elif THE_ISA == MIPS_ISA
62 #include "arch/mips/linux/process.hh"
64 #error "THE_ISA not set"
69 using namespace TheISA
;
72 // The purpose of this code is to fake the loader & syscall mechanism
73 // when there's no OS: thus there's no resone to use it in FULL_SYSTEM
74 // mode when we do have an OS
77 #error "process.cc not compatible with FULL_SYSTEM"
80 // current number of allocated processes
81 int num_processes
= 0;
83 Process::Process(const string
&nm
,
85 int stdin_fd
, // initial I/O descriptors
88 : SimObject(nm
), system(_system
)
90 // initialize first 3 fds (stdin, stdout, stderr)
91 fd_map
[STDIN_FILENO
] = stdin_fd
;
92 fd_map
[STDOUT_FILENO
] = stdout_fd
;
93 fd_map
[STDERR_FILENO
] = stderr_fd
;
95 // mark remaining fds as free
96 for (int i
= 3; i
<= MAX_FD
; ++i
) {
100 mmap_start
= mmap_end
= 0;
101 nxm_start
= nxm_end
= 0;
102 pTable
= new PageTable(system
);
103 // other parameters will be initialized when the program is loaded
110 using namespace Stats
;
113 .name(name() + ".PROG:num_syscalls")
114 .desc("Number of system calls")
119 // static helper functions
122 Process::openInputFile(const string
&filename
)
124 int fd
= open(filename
.c_str(), O_RDONLY
);
128 cerr
<< "unable to open \"" << filename
<< "\" for reading\n";
129 fatal("can't open input file");
137 Process::openOutputFile(const string
&filename
)
139 int fd
= open(filename
.c_str(), O_WRONLY
| O_CREAT
| O_TRUNC
, 0774);
143 cerr
<< "unable to open \"" << filename
<< "\" for writing\n";
144 fatal("can't open output file");
152 Process::registerThreadContext(ThreadContext
*tc
)
155 int myIndex
= threadContexts
.size();
156 threadContexts
.push_back(tc
);
158 RemoteGDB
*rgdb
= new RemoteGDB(system
, tc
);
159 GDBListener
*gdbl
= new GDBListener(rgdb
, 7000 + myIndex
);
163 remoteGDB
.push_back(rgdb
);
165 // return CPU number to caller
172 if (threadContexts
.empty())
173 fatal("Process %s is not associated with any CPUs!\n", name());
175 // first thread context for this process... initialize & enable
176 ThreadContext
*tc
= threadContexts
[0];
178 // mark this context as active so it will start ticking.
182 mem_port
= system
->physmem
->getPort("functional");
183 initVirtMem
= new TranslatingPort("process init port", pTable
, true);
184 mem_port
->setPeer(initVirtMem
);
185 initVirtMem
->setPeer(mem_port
);
189 Process::replaceThreadContext(ThreadContext
*tc
, int tcIndex
)
191 if (tcIndex
>= threadContexts
.size()) {
192 panic("replaceThreadContext: bad tcIndex, %d >= %d\n",
193 tcIndex
, threadContexts
.size());
196 threadContexts
[tcIndex
] = tc
;
199 // map simulator fd sim_fd to target fd tgt_fd
201 Process::dup_fd(int sim_fd
, int tgt_fd
)
203 if (tgt_fd
< 0 || tgt_fd
> MAX_FD
)
204 panic("Process::dup_fd tried to dup past MAX_FD (%d)", tgt_fd
);
206 fd_map
[tgt_fd
] = sim_fd
;
210 // generate new target fd for sim_fd
212 Process::alloc_fd(int sim_fd
)
214 // in case open() returns an error, don't allocate a new fd
218 // find first free target fd
219 for (int free_fd
= 0; free_fd
< MAX_FD
; ++free_fd
) {
220 if (fd_map
[free_fd
] == -1) {
221 fd_map
[free_fd
] = sim_fd
;
226 panic("Process::alloc_fd: out of file descriptors!");
230 // free target fd (e.g., after close)
232 Process::free_fd(int tgt_fd
)
234 if (fd_map
[tgt_fd
] == -1)
235 warn("Process::free_fd: request to free unused fd %d", tgt_fd
);
241 // look up simulator fd for given target fd
243 Process::sim_fd(int tgt_fd
)
248 return fd_map
[tgt_fd
];
252 Process::serialize(std::ostream
&os
)
254 SERIALIZE_SCALAR(initialContextLoaded
);
255 SERIALIZE_SCALAR(brk_point
);
256 SERIALIZE_SCALAR(stack_base
);
257 SERIALIZE_SCALAR(stack_size
);
258 SERIALIZE_SCALAR(stack_min
);
259 SERIALIZE_SCALAR(next_thread_stack_base
);
260 SERIALIZE_SCALAR(mmap_start
);
261 SERIALIZE_SCALAR(mmap_end
);
262 SERIALIZE_SCALAR(nxm_start
);
263 SERIALIZE_SCALAR(nxm_end
);
264 SERIALIZE_ARRAY(fd_map
, MAX_FD
);
266 pTable
->serialize(os
);
270 Process::unserialize(Checkpoint
*cp
, const std::string
§ion
)
272 UNSERIALIZE_SCALAR(initialContextLoaded
);
273 UNSERIALIZE_SCALAR(brk_point
);
274 UNSERIALIZE_SCALAR(stack_base
);
275 UNSERIALIZE_SCALAR(stack_size
);
276 UNSERIALIZE_SCALAR(stack_min
);
277 UNSERIALIZE_SCALAR(next_thread_stack_base
);
278 UNSERIALIZE_SCALAR(mmap_start
);
279 UNSERIALIZE_SCALAR(mmap_end
);
280 UNSERIALIZE_SCALAR(nxm_start
);
281 UNSERIALIZE_SCALAR(nxm_end
);
282 UNSERIALIZE_ARRAY(fd_map
, MAX_FD
);
284 pTable
->unserialize(cp
, section
);
289 // need to declare these here since there is no concrete Process type
290 // that can be constructed (i.e., no REGISTER_SIM_OBJECT() macro call,
291 // which is where these get declared for concrete types).
293 DEFINE_SIM_OBJECT_CLASS_NAME("Process", Process
)
296 ////////////////////////////////////////////////////////////////////////
298 // LiveProcess member definitions
300 ////////////////////////////////////////////////////////////////////////
304 copyStringArray(vector
<string
> &strings
, Addr array_ptr
, Addr data_ptr
,
305 TranslatingPort
* memPort
, int ptr_size
)
308 for (int i
= 0; i
< strings
.size(); ++i
) {
309 data_ptr_swap
= htog(data_ptr
);
310 memPort
->writeBlob(array_ptr
, (uint8_t*)&data_ptr_swap
, ptr_size
);
311 memPort
->writeString(data_ptr
, strings
[i
].c_str());
312 array_ptr
+= ptr_size
;
313 data_ptr
+= strings
[i
].size() + 1;
315 // add NULL terminator
318 memPort
->writeBlob(array_ptr
, (uint8_t*)&data_ptr
, ptr_size
);
321 LiveProcess::LiveProcess(const string
&nm
, ObjectFile
*_objFile
,
323 int stdin_fd
, int stdout_fd
, int stderr_fd
,
324 vector
<string
> &_argv
, vector
<string
> &_envp
,
326 uint64_t _uid
, uint64_t _euid
,
327 uint64_t _gid
, uint64_t _egid
,
328 uint64_t _pid
, uint64_t _ppid
)
329 : Process(nm
, _system
, stdin_fd
, stdout_fd
, stderr_fd
),
330 objFile(_objFile
), argv(_argv
), envp(_envp
), cwd(_cwd
)
339 prog_fname
= argv
[0];
341 // load up symbols, if any... these may be used for debugging or
343 if (!debugSymbolTable
) {
344 debugSymbolTable
= new SymbolTable();
345 if (!objFile
->loadGlobalSymbols(debugSymbolTable
) ||
346 !objFile
->loadLocalSymbols(debugSymbolTable
)) {
347 // didn't load any symbols
348 delete debugSymbolTable
;
349 debugSymbolTable
= NULL
;
355 LiveProcess::argsInit(int intSize
, int pageSize
)
359 // load object file into target memory
360 objFile
->loadSections(initVirtMem
);
362 // Calculate how much space we need for arg & env arrays.
363 int argv_array_size
= intSize
* (argv
.size() + 1);
364 int envp_array_size
= intSize
* (envp
.size() + 1);
365 int arg_data_size
= 0;
366 for (int i
= 0; i
< argv
.size(); ++i
) {
367 arg_data_size
+= argv
[i
].size() + 1;
369 int env_data_size
= 0;
370 for (int i
= 0; i
< envp
.size(); ++i
) {
371 env_data_size
+= envp
[i
].size() + 1;
375 argv_array_size
+ envp_array_size
+ arg_data_size
+ env_data_size
;
376 if (space_needed
< 32*1024)
377 space_needed
= 32*1024;
379 // set bottom of stack
380 stack_min
= stack_base
- space_needed
;
382 stack_min
= roundDown(stack_min
, pageSize
);
383 stack_size
= stack_base
- stack_min
;
385 pTable
->allocate(stack_min
, roundUp(stack_size
, pageSize
));
387 // map out initial stack contents
388 Addr argv_array_base
= stack_min
+ intSize
; // room for argc
389 Addr envp_array_base
= argv_array_base
+ argv_array_size
;
390 Addr arg_data_base
= envp_array_base
+ envp_array_size
;
391 Addr env_data_base
= arg_data_base
+ arg_data_size
;
393 // write contents to stack
394 uint64_t argc
= argv
.size();
396 argc
= htog((uint64_t)argc
);
397 else if (intSize
== 4)
398 argc
= htog((uint32_t)argc
);
400 panic("Unknown int size");
402 initVirtMem
->writeBlob(stack_min
, (uint8_t*)&argc
, intSize
);
404 copyStringArray(argv
, argv_array_base
, arg_data_base
, initVirtMem
);
405 copyStringArray(envp
, envp_array_base
, env_data_base
, initVirtMem
);
407 threadContexts
[0]->setIntReg(ArgumentReg0
, argc
);
408 threadContexts
[0]->setIntReg(ArgumentReg1
, argv_array_base
);
409 threadContexts
[0]->setIntReg(StackPointerReg
, stack_min
);
411 Addr prog_entry
= objFile
->entryPoint();
412 threadContexts
[0]->setPC(prog_entry
);
413 threadContexts
[0]->setNextPC(prog_entry
+ sizeof(MachInst
));
415 #if THE_ISA != ALPHA_ISA //e.g. MIPS or Sparc
416 threadContexts
[0]->setNextNPC(prog_entry
+ (2 * sizeof(MachInst
)));
423 LiveProcess::syscall(int64_t callnum
, ThreadContext
*tc
)
427 SyscallDesc
*desc
= getDesc(callnum
);
429 fatal("Syscall %d out of range", callnum
);
431 desc
->doSyscall(callnum
, this, tc
);
435 LiveProcess::create(const std::string
&nm
, System
*system
, int stdin_fd
,
436 int stdout_fd
, int stderr_fd
, std::string executable
,
437 std::vector
<std::string
> &argv
,
438 std::vector
<std::string
> &envp
,
439 const std::string
&cwd
,
440 uint64_t _uid
, uint64_t _euid
,
441 uint64_t _gid
, uint64_t _egid
,
442 uint64_t _pid
, uint64_t _ppid
)
444 LiveProcess
*process
= NULL
;
446 ObjectFile
*objFile
= createObjectFile(executable
);
447 if (objFile
== NULL
) {
448 fatal("Can't load object file %s", executable
);
451 if (objFile
->isDynamic())
452 fatal("Object file is a dynamic executable however only static "
453 "executables are supported!\n Please recompile your "
454 "executable as a static binary and try again.\n");
456 #if THE_ISA == ALPHA_ISA
457 if (objFile
->getArch() != ObjectFile::Alpha
)
458 fatal("Object file architecture does not match compiled ISA (Alpha).");
459 switch (objFile
->getOpSys()) {
460 case ObjectFile::Tru64
:
461 process
= new AlphaTru64Process(nm
, objFile
, system
,
462 stdin_fd
, stdout_fd
, stderr_fd
,
464 _uid
, _euid
, _gid
, _egid
, _pid
, _ppid
);
467 case ObjectFile::Linux
:
468 process
= new AlphaLinuxProcess(nm
, objFile
, system
,
469 stdin_fd
, stdout_fd
, stderr_fd
,
471 _uid
, _euid
, _gid
, _egid
, _pid
, _ppid
);
475 fatal("Unknown/unsupported operating system.");
477 #elif THE_ISA == SPARC_ISA
478 if (objFile
->getArch() != ObjectFile::SPARC64
&& objFile
->getArch() != ObjectFile::SPARC32
)
479 fatal("Object file architecture does not match compiled ISA (SPARC).");
480 switch (objFile
->getOpSys()) {
481 case ObjectFile::Linux
:
482 if (objFile
->getArch() == ObjectFile::SPARC64
) {
483 process
= new Sparc64LinuxProcess(nm
, objFile
, system
,
484 stdin_fd
, stdout_fd
, stderr_fd
,
489 process
= new Sparc32LinuxProcess(nm
, objFile
, system
,
490 stdin_fd
, stdout_fd
, stderr_fd
,
498 case ObjectFile::Solaris
:
499 process
= new SparcSolarisProcess(nm
, objFile
, system
,
500 stdin_fd
, stdout_fd
, stderr_fd
,
502 _uid
, _euid
, _gid
, _egid
, _pid
, _ppid
);
505 fatal("Unknown/unsupported operating system.");
507 #elif THE_ISA == MIPS_ISA
508 if (objFile
->getArch() != ObjectFile::Mips
)
509 fatal("Object file architecture does not match compiled ISA (MIPS).");
510 switch (objFile
->getOpSys()) {
511 case ObjectFile::Linux
:
512 process
= new MipsLinuxProcess(nm
, objFile
, system
,
513 stdin_fd
, stdout_fd
, stderr_fd
,
515 _uid
, _euid
, _gid
, _egid
, _pid
, _ppid
);
519 fatal("Unknown/unsupported operating system.");
522 #error "THE_ISA not set"
527 fatal("Unknown error creating process object.");
532 BEGIN_DECLARE_SIM_OBJECT_PARAMS(LiveProcess
)
534 VectorParam
<string
> cmd
;
535 Param
<string
> executable
;
537 Param
<string
> output
;
538 VectorParam
<string
> env
;
540 SimObjectParam
<System
*> system
;
542 Param
<uint64_t> euid
;
544 Param
<uint64_t> egid
;
546 Param
<uint64_t> ppid
;
548 END_DECLARE_SIM_OBJECT_PARAMS(LiveProcess
)
551 BEGIN_INIT_SIM_OBJECT_PARAMS(LiveProcess
)
553 INIT_PARAM(cmd
, "command line (executable plus arguments)"),
554 INIT_PARAM(executable
, "executable (overrides cmd[0] if set)"),
555 INIT_PARAM(input
, "filename for stdin (dflt: use sim stdin)"),
556 INIT_PARAM(output
, "filename for stdout/stderr (dflt: use sim stdout)"),
557 INIT_PARAM(env
, "environment settings"),
558 INIT_PARAM(cwd
, "current working directory"),
559 INIT_PARAM(system
, "system"),
560 INIT_PARAM(uid
, "user id"),
561 INIT_PARAM(euid
, "effective user id"),
562 INIT_PARAM(gid
, "group id"),
563 INIT_PARAM(egid
, "effective group id"),
564 INIT_PARAM(pid
, "process id"),
565 INIT_PARAM(ppid
, "parent process id")
567 END_INIT_SIM_OBJECT_PARAMS(LiveProcess
)
570 CREATE_SIM_OBJECT(LiveProcess
)
575 // initialize file descriptors to default: same as simulator
576 int stdin_fd
, stdout_fd
, stderr_fd
;
578 if (in
== "stdin" || in
== "cin")
579 stdin_fd
= STDIN_FILENO
;
581 stdin_fd
= Process::openInputFile(input
);
583 if (out
== "stdout" || out
== "cout")
584 stdout_fd
= STDOUT_FILENO
;
585 else if (out
== "stderr" || out
== "cerr")
586 stdout_fd
= STDERR_FILENO
;
588 stdout_fd
= Process::openOutputFile(out
);
590 stderr_fd
= (stdout_fd
!= STDOUT_FILENO
) ? stdout_fd
: STDERR_FILENO
;
592 return LiveProcess::create(getInstanceName(), system
,
593 stdin_fd
, stdout_fd
, stderr_fd
,
594 (string
)executable
== "" ? cmd
[0] : executable
,
596 uid
, euid
, gid
, egid
, pid
, ppid
);
600 REGISTER_SIM_OBJECT("LiveProcess", LiveProcess
)