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/Process.hh"
49 #include "params/LiveProcess.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 == X86_ISA
66 #include "arch/x86/linux/process.hh"
68 #error "THE_ISA not set"
73 using namespace TheISA
;
76 // The purpose of this code is to fake the loader & syscall mechanism
77 // when there's no OS: thus there's no resone to use it in FULL_SYSTEM
78 // mode when we do have an OS
81 #error "process.cc not compatible with FULL_SYSTEM"
84 // current number of allocated processes
85 int num_processes
= 0;
87 Process::Process(ProcessParams
* params
)
88 : SimObject(params
), system(params
->system
), checkpointRestored(false),
89 max_stack_size(params
->max_stack_size
)
91 string in
= params
->input
;
92 string out
= params
->output
;
94 // initialize file descriptors to default: same as simulator
95 int stdin_fd
, stdout_fd
, stderr_fd
;
97 if (in
== "stdin" || in
== "cin")
98 stdin_fd
= STDIN_FILENO
;
99 else if (in
== "None")
102 stdin_fd
= Process::openInputFile(in
);
104 if (out
== "stdout" || out
== "cout")
105 stdout_fd
= STDOUT_FILENO
;
106 else if (out
== "stderr" || out
== "cerr")
107 stdout_fd
= STDERR_FILENO
;
108 else if (out
== "None")
111 stdout_fd
= Process::openOutputFile(out
);
113 stderr_fd
= (stdout_fd
!= STDOUT_FILENO
) ? stdout_fd
: STDERR_FILENO
;
115 M5_pid
= system
->allocatePID();
116 // initialize first 3 fds (stdin, stdout, stderr)
117 fd_map
[STDIN_FILENO
] = stdin_fd
;
118 fd_map
[STDOUT_FILENO
] = stdout_fd
;
119 fd_map
[STDERR_FILENO
] = stderr_fd
;
121 // mark remaining fds as free
122 for (int i
= 3; i
<= MAX_FD
; ++i
) {
126 mmap_start
= mmap_end
= 0;
127 nxm_start
= nxm_end
= 0;
128 pTable
= new PageTable(this);
129 // other parameters will be initialized when the program is loaded
136 using namespace Stats
;
139 .name(name() + ".PROG:num_syscalls")
140 .desc("Number of system calls")
145 // static helper functions
148 Process::openInputFile(const string
&filename
)
150 int fd
= open(filename
.c_str(), O_RDONLY
);
154 cerr
<< "unable to open \"" << filename
<< "\" for reading\n";
155 fatal("can't open input file");
163 Process::openOutputFile(const string
&filename
)
165 int fd
= open(filename
.c_str(), O_WRONLY
| O_CREAT
| O_TRUNC
, 0774);
169 cerr
<< "unable to open \"" << filename
<< "\" for writing\n";
170 fatal("can't open output file");
178 Process::registerThreadContext(ThreadContext
*tc
)
181 int myIndex
= threadContexts
.size();
182 threadContexts
.push_back(tc
);
184 RemoteGDB
*rgdb
= new RemoteGDB(system
, tc
);
185 GDBListener
*gdbl
= new GDBListener(rgdb
, 7000 + myIndex
);
189 remoteGDB
.push_back(rgdb
);
191 // return CPU number to caller
198 if (threadContexts
.empty())
199 fatal("Process %s is not associated with any CPUs!\n", name());
201 // first thread context for this process... initialize & enable
202 ThreadContext
*tc
= threadContexts
[0];
204 // mark this context as active so it will start ticking.
208 mem_port
= system
->physmem
->getPort("functional");
209 initVirtMem
= new TranslatingPort("process init port", this,
210 TranslatingPort::Always
);
211 mem_port
->setPeer(initVirtMem
);
212 initVirtMem
->setPeer(mem_port
);
216 Process::replaceThreadContext(ThreadContext
*tc
, int tcIndex
)
218 if (tcIndex
>= threadContexts
.size()) {
219 panic("replaceThreadContext: bad tcIndex, %d >= %d\n",
220 tcIndex
, threadContexts
.size());
223 threadContexts
[tcIndex
] = tc
;
226 // map simulator fd sim_fd to target fd tgt_fd
228 Process::dup_fd(int sim_fd
, int tgt_fd
)
230 if (tgt_fd
< 0 || tgt_fd
> MAX_FD
)
231 panic("Process::dup_fd tried to dup past MAX_FD (%d)", tgt_fd
);
233 fd_map
[tgt_fd
] = sim_fd
;
237 // generate new target fd for sim_fd
239 Process::alloc_fd(int sim_fd
)
241 // in case open() returns an error, don't allocate a new fd
245 // find first free target fd
246 for (int free_fd
= 0; free_fd
< MAX_FD
; ++free_fd
) {
247 if (fd_map
[free_fd
] == -1) {
248 fd_map
[free_fd
] = sim_fd
;
253 panic("Process::alloc_fd: out of file descriptors!");
257 // free target fd (e.g., after close)
259 Process::free_fd(int tgt_fd
)
261 if (fd_map
[tgt_fd
] == -1)
262 warn("Process::free_fd: request to free unused fd %d", tgt_fd
);
268 // look up simulator fd for given target fd
270 Process::sim_fd(int tgt_fd
)
275 return fd_map
[tgt_fd
];
279 Process::checkAndAllocNextPage(Addr vaddr
)
281 // if this is an initial write we might not have
282 if (vaddr
>= stack_min
&& vaddr
< stack_base
) {
283 pTable
->allocate(roundDown(vaddr
, VMPageSize
), VMPageSize
);
287 // We've accessed the next page of the stack, so extend the stack
289 if (vaddr
< stack_min
&& vaddr
>= stack_base
- max_stack_size
) {
290 while (vaddr
< stack_min
) {
291 stack_min
-= TheISA::PageBytes
;
292 if(stack_base
- stack_min
> max_stack_size
)
293 fatal("Maximum stack size exceeded\n");
294 if(stack_base
- stack_min
> 8*1024*1024)
295 fatal("Over max stack size for one thread\n");
296 pTable
->allocate(stack_min
, TheISA::PageBytes
);
297 warn("Increasing stack size by one page.");
305 Process::serialize(std::ostream
&os
)
307 SERIALIZE_SCALAR(initialContextLoaded
);
308 SERIALIZE_SCALAR(brk_point
);
309 SERIALIZE_SCALAR(stack_base
);
310 SERIALIZE_SCALAR(stack_size
);
311 SERIALIZE_SCALAR(stack_min
);
312 SERIALIZE_SCALAR(next_thread_stack_base
);
313 SERIALIZE_SCALAR(mmap_start
);
314 SERIALIZE_SCALAR(mmap_end
);
315 SERIALIZE_SCALAR(nxm_start
);
316 SERIALIZE_SCALAR(nxm_end
);
317 SERIALIZE_ARRAY(fd_map
, MAX_FD
);
319 pTable
->serialize(os
);
323 Process::unserialize(Checkpoint
*cp
, const std::string
§ion
)
325 UNSERIALIZE_SCALAR(initialContextLoaded
);
326 UNSERIALIZE_SCALAR(brk_point
);
327 UNSERIALIZE_SCALAR(stack_base
);
328 UNSERIALIZE_SCALAR(stack_size
);
329 UNSERIALIZE_SCALAR(stack_min
);
330 UNSERIALIZE_SCALAR(next_thread_stack_base
);
331 UNSERIALIZE_SCALAR(mmap_start
);
332 UNSERIALIZE_SCALAR(mmap_end
);
333 UNSERIALIZE_SCALAR(nxm_start
);
334 UNSERIALIZE_SCALAR(nxm_end
);
335 UNSERIALIZE_ARRAY(fd_map
, MAX_FD
);
337 pTable
->unserialize(cp
, section
);
340 checkpointRestored
= true;
345 ////////////////////////////////////////////////////////////////////////
347 // LiveProcess member definitions
349 ////////////////////////////////////////////////////////////////////////
352 LiveProcess::LiveProcess(LiveProcessParams
* params
, ObjectFile
*_objFile
)
353 : Process(params
), objFile(_objFile
),
354 argv(params
->cmd
), envp(params
->env
), cwd(params
->cwd
)
357 __euid
= params
->euid
;
359 __egid
= params
->egid
;
361 __ppid
= params
->ppid
;
363 prog_fname
= params
->cmd
[0];
365 // load up symbols, if any... these may be used for debugging or
367 if (!debugSymbolTable
) {
368 debugSymbolTable
= new SymbolTable();
369 if (!objFile
->loadGlobalSymbols(debugSymbolTable
) ||
370 !objFile
->loadLocalSymbols(debugSymbolTable
)) {
371 // didn't load any symbols
372 delete debugSymbolTable
;
373 debugSymbolTable
= NULL
;
379 LiveProcess::argsInit(int intSize
, int pageSize
)
383 // load object file into target memory
384 objFile
->loadSections(initVirtMem
);
386 // Calculate how much space we need for arg & env arrays.
387 int argv_array_size
= intSize
* (argv
.size() + 1);
388 int envp_array_size
= intSize
* (envp
.size() + 1);
389 int arg_data_size
= 0;
390 for (int i
= 0; i
< argv
.size(); ++i
) {
391 arg_data_size
+= argv
[i
].size() + 1;
393 int env_data_size
= 0;
394 for (int i
= 0; i
< envp
.size(); ++i
) {
395 env_data_size
+= envp
[i
].size() + 1;
399 argv_array_size
+ envp_array_size
+ arg_data_size
+ env_data_size
;
400 if (space_needed
< 32*1024)
401 space_needed
= 32*1024;
403 // set bottom of stack
404 stack_min
= stack_base
- space_needed
;
406 stack_min
= roundDown(stack_min
, pageSize
);
407 stack_size
= stack_base
- stack_min
;
409 pTable
->allocate(stack_min
, roundUp(stack_size
, pageSize
));
411 // map out initial stack contents
412 Addr argv_array_base
= stack_min
+ intSize
; // room for argc
413 Addr envp_array_base
= argv_array_base
+ argv_array_size
;
414 Addr arg_data_base
= envp_array_base
+ envp_array_size
;
415 Addr env_data_base
= arg_data_base
+ arg_data_size
;
417 // write contents to stack
418 uint64_t argc
= argv
.size();
420 argc
= htog((uint64_t)argc
);
421 else if (intSize
== 4)
422 argc
= htog((uint32_t)argc
);
424 panic("Unknown int size");
426 initVirtMem
->writeBlob(stack_min
, (uint8_t*)&argc
, intSize
);
428 copyStringArray(argv
, argv_array_base
, arg_data_base
, initVirtMem
);
429 copyStringArray(envp
, envp_array_base
, env_data_base
, initVirtMem
);
431 assert(NumArgumentRegs
>= 2);
432 threadContexts
[0]->setIntReg(ArgumentReg
[0], argc
);
433 threadContexts
[0]->setIntReg(ArgumentReg
[1], argv_array_base
);
434 threadContexts
[0]->setIntReg(StackPointerReg
, stack_min
);
436 Addr prog_entry
= objFile
->entryPoint();
437 threadContexts
[0]->setPC(prog_entry
);
438 threadContexts
[0]->setNextPC(prog_entry
+ sizeof(MachInst
));
440 #if THE_ISA != ALPHA_ISA //e.g. MIPS or Sparc
441 threadContexts
[0]->setNextNPC(prog_entry
+ (2 * sizeof(MachInst
)));
448 LiveProcess::syscall(int64_t callnum
, ThreadContext
*tc
)
452 SyscallDesc
*desc
= getDesc(callnum
);
454 fatal("Syscall %d out of range", callnum
);
456 desc
->doSyscall(callnum
, this, tc
);
460 LiveProcess::create(LiveProcessParams
* params
)
462 LiveProcess
*process
= NULL
;
465 params
->executable
== "" ? params
->cmd
[0] : params
->executable
;
466 ObjectFile
*objFile
= createObjectFile(executable
);
467 if (objFile
== NULL
) {
468 fatal("Can't load object file %s", executable
);
471 if (objFile
->isDynamic())
472 fatal("Object file is a dynamic executable however only static "
473 "executables are supported!\n Please recompile your "
474 "executable as a static binary and try again.\n");
476 #if THE_ISA == ALPHA_ISA
477 if (objFile
->hasTLS())
478 fatal("Object file has a TLS section and single threaded TLS is not\n"
479 " currently supported for Alpha! Please recompile your "
480 "executable with \n a non-TLS toolchain.\n");
482 if (objFile
->getArch() != ObjectFile::Alpha
)
483 fatal("Object file architecture does not match compiled ISA (Alpha).");
484 switch (objFile
->getOpSys()) {
485 case ObjectFile::Tru64
:
486 process
= new AlphaTru64Process(params
, objFile
);
489 case ObjectFile::Linux
:
490 process
= new AlphaLinuxProcess(params
, objFile
);
494 fatal("Unknown/unsupported operating system.");
496 #elif THE_ISA == SPARC_ISA
497 if (objFile
->getArch() != ObjectFile::SPARC64
&& objFile
->getArch() != ObjectFile::SPARC32
)
498 fatal("Object file architecture does not match compiled ISA (SPARC).");
499 switch (objFile
->getOpSys()) {
500 case ObjectFile::Linux
:
501 if (objFile
->getArch() == ObjectFile::SPARC64
) {
502 process
= new Sparc64LinuxProcess(params
, objFile
);
504 process
= new Sparc32LinuxProcess(params
, objFile
);
509 case ObjectFile::Solaris
:
510 process
= new SparcSolarisProcess(params
, objFile
);
513 fatal("Unknown/unsupported operating system.");
515 #elif THE_ISA == X86_ISA
516 if (objFile
->getArch() != ObjectFile::X86
)
517 fatal("Object file architecture does not match compiled ISA (x86).");
518 switch (objFile
->getOpSys()) {
519 case ObjectFile::Linux
:
520 process
= new X86LinuxProcess(params
, objFile
);
523 fatal("Unknown/unsupported operating system.");
525 #elif THE_ISA == MIPS_ISA
526 if (objFile
->getArch() != ObjectFile::Mips
)
527 fatal("Object file architecture does not match compiled ISA (MIPS).");
528 switch (objFile
->getOpSys()) {
529 case ObjectFile::Linux
:
530 process
= new MipsLinuxProcess(params
, objFile
);
534 fatal("Unknown/unsupported operating system.");
537 #error "THE_ISA not set"
542 fatal("Unknown error creating process object.");
547 LiveProcessParams::create()
549 return LiveProcess::create(this);