c725d3b1c3cc0b44dd7c489001353fcce9912c08
2 * Copyright (c) 2001-2004 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.
35 #include "base/intmath.hh"
36 #include "base/loader/object_file.hh"
37 #include "base/loader/symtab.hh"
38 #include "base/statistics.hh"
39 #include "cpu/exec_context.hh"
40 #include "cpu/full_cpu/smt.hh"
41 #include "cpu/full_cpu/thread.hh"
43 #include "mem/functional_mem/main_memory.hh"
44 #include "sim/builder.hh"
45 #include "sim/fake_syscall.hh"
46 #include "sim/process.hh"
47 #include "sim/stats.hh"
50 #include "arch/alpha/alpha_tru64_process.hh"
51 #include "arch/alpha/alpha_linux_process.hh"
57 // The purpose of this code is to fake the loader & syscall mechanism
58 // when there's no OS: thus there's no resone to use it in FULL_SYSTEM
59 // mode when we do have an OS
62 #error "process.cc not compatible with FULL_SYSTEM"
65 // current number of allocated processes
66 int num_processes
= 0;
68 Process::Process(const string
&name
,
69 int stdin_fd
, // initial I/O descriptors
74 // allocate memory space
75 memory
= new MainMemory(name
+ ".MainMem");
77 // allocate initial register file
78 init_regs
= new RegFile
;
79 memset(init_regs
, 0, sizeof(RegFile
));
81 // initialize first 3 fds (stdin, stdout, stderr)
82 fd_map
[STDIN_FILENO
] = stdin_fd
;
83 fd_map
[STDOUT_FILENO
] = stdout_fd
;
84 fd_map
[STDERR_FILENO
] = stderr_fd
;
86 // mark remaining fds as free
87 for (int i
= 3; i
<= MAX_FD
; ++i
) {
93 // other parameters will be initialized when the program is loaded
99 using namespace Stats
;
102 .name(name() + ".PROG:num_syscalls")
103 .desc("Number of system calls")
108 // static helper functions
111 Process::openInputFile(const string
&filename
)
113 int fd
= open(filename
.c_str(), O_RDONLY
);
117 cerr
<< "unable to open \"" << filename
<< "\" for reading\n";
118 fatal("can't open input file");
126 Process::openOutputFile(const string
&filename
)
128 int fd
= open(filename
.c_str(), O_WRONLY
| O_CREAT
| O_TRUNC
, 0774);
132 cerr
<< "unable to open \"" << filename
<< "\" for writing\n";
133 fatal("can't open output file");
141 Process::registerExecContext(ExecContext
*xc
)
144 int myIndex
= execContexts
.size();
145 execContexts
.push_back(xc
);
148 // first exec context for this process... initialize & enable
150 // copy process's initial regs struct
151 xc
->regs
= *init_regs
;
153 // mark this context as active.
154 // activate with zero delay so that we start ticking right
159 // return CPU number to caller and increment available CPU count
165 Process::replaceExecContext(ExecContext
*xc
, int xcIndex
)
167 if (xcIndex
>= execContexts
.size()) {
168 panic("replaceExecContext: bad xcIndex, %d >= %d\n",
169 xcIndex
, execContexts
.size());
172 execContexts
[xcIndex
] = xc
;
175 // map simulator fd sim_fd to target fd tgt_fd
177 Process::dup_fd(int sim_fd
, int tgt_fd
)
179 if (tgt_fd
< 0 || tgt_fd
> MAX_FD
)
180 panic("Process::dup_fd tried to dup past MAX_FD (%d)", tgt_fd
);
182 fd_map
[tgt_fd
] = sim_fd
;
186 // generate new target fd for sim_fd
188 Process::open_fd(int sim_fd
)
192 // in case open() returns an error, don't allocate a new fd
196 // find first free target fd
197 for (free_fd
= 0; fd_map
[free_fd
] >= 0; ++free_fd
) {
198 if (free_fd
== MAX_FD
)
199 panic("Process::open_fd: out of file descriptors!");
202 fd_map
[free_fd
] = sim_fd
;
208 // look up simulator fd for given target fd
210 Process::sim_fd(int tgt_fd
)
215 return fd_map
[tgt_fd
];
221 // need to declare these here since there is no concrete Process type
222 // that can be constructed (i.e., no REGISTER_SIM_OBJECT() macro call,
223 // which is where these get declared for concrete types).
225 DEFINE_SIM_OBJECT_CLASS_NAME("Process", Process
)
228 ////////////////////////////////////////////////////////////////////////
230 // LiveProcess member definitions
232 ////////////////////////////////////////////////////////////////////////
236 copyStringArray(vector
<string
> &strings
, Addr array_ptr
, Addr data_ptr
,
237 FunctionalMemory
*memory
)
239 for (int i
= 0; i
< strings
.size(); ++i
) {
240 memory
->access(Write
, array_ptr
, &data_ptr
, sizeof(Addr
));
241 memory
->writeString(data_ptr
, strings
[i
].c_str());
242 array_ptr
+= sizeof(Addr
);
243 data_ptr
+= strings
[i
].size() + 1;
245 // add NULL terminator
247 memory
->access(Write
, array_ptr
, &data_ptr
, sizeof(Addr
));
250 LiveProcess::LiveProcess(const string
&name
, ObjectFile
*objFile
,
251 int stdin_fd
, int stdout_fd
, int stderr_fd
,
252 vector
<string
> &argv
, vector
<string
> &envp
)
253 : Process(name
, stdin_fd
, stdout_fd
, stderr_fd
)
255 prog_fname
= argv
[0];
257 prog_entry
= objFile
->entryPoint();
258 text_base
= objFile
->textBase();
259 text_size
= objFile
->textSize();
260 data_base
= objFile
->dataBase();
261 data_size
= objFile
->dataSize() + objFile
->bssSize();
262 brk_point
= RoundUp
<uint64_t>(data_base
+ data_size
, VMPageSize
);
264 // load object file into target memory
265 objFile
->loadSections(memory
);
267 // load up symbols, if any... these may be used for debugging or
269 if (!debugSymbolTable
) {
270 debugSymbolTable
= new SymbolTable();
271 if (!objFile
->loadGlobalSymbols(debugSymbolTable
) ||
272 !objFile
->loadLocalSymbols(debugSymbolTable
)) {
273 // didn't load any symbols
274 delete debugSymbolTable
;
275 debugSymbolTable
= NULL
;
279 // Set up stack. On Alpha, stack goes below text section. This
280 // code should get moved to some architecture-specific spot.
281 stack_base
= text_base
- (409600+4096);
283 // Set up region for mmaps. Tru64 seems to start just above 0 and
284 // grow up from there.
287 // Set pointer for next thread stack. Reserve 8M for main stack.
288 next_thread_stack_base
= stack_base
- (8 * 1024 * 1024);
290 // Calculate how much space we need for arg & env arrays.
291 int argv_array_size
= sizeof(Addr
) * (argv
.size() + 1);
292 int envp_array_size
= sizeof(Addr
) * (envp
.size() + 1);
293 int arg_data_size
= 0;
294 for (int i
= 0; i
< argv
.size(); ++i
) {
295 arg_data_size
+= argv
[i
].size() + 1;
297 int env_data_size
= 0;
298 for (int i
= 0; i
< envp
.size(); ++i
) {
299 env_data_size
+= envp
[i
].size() + 1;
303 argv_array_size
+ envp_array_size
+ arg_data_size
+ env_data_size
;
304 // for SimpleScalar compatibility
305 if (space_needed
< 16384)
306 space_needed
= 16384;
308 // set bottom of stack
309 stack_min
= stack_base
- space_needed
;
312 stack_size
= stack_base
- stack_min
;
314 // map out initial stack contents
315 Addr argv_array_base
= stack_min
+ sizeof(uint64_t); // room for argc
316 Addr envp_array_base
= argv_array_base
+ argv_array_size
;
317 Addr arg_data_base
= envp_array_base
+ envp_array_size
;
318 Addr env_data_base
= arg_data_base
+ arg_data_size
;
320 // write contents to stack
321 uint64_t argc
= argv
.size();
322 memory
->access(Write
, stack_min
, &argc
, sizeof(uint64_t));
324 copyStringArray(argv
, argv_array_base
, arg_data_base
, memory
);
325 copyStringArray(envp
, envp_array_base
, env_data_base
, memory
);
327 init_regs
->intRegFile
[ArgumentReg0
] = argc
;
328 init_regs
->intRegFile
[ArgumentReg1
] = argv_array_base
;
329 init_regs
->intRegFile
[StackPointerReg
] = stack_min
;
330 init_regs
->intRegFile
[GlobalPointerReg
] = objFile
->globalPointer();
331 init_regs
->pc
= prog_entry
;
332 init_regs
->npc
= prog_entry
+ sizeof(MachInst
);
337 LiveProcess::create(const string
&name
,
338 int stdin_fd
, int stdout_fd
, int stderr_fd
,
339 vector
<string
> &argv
, vector
<string
> &envp
)
341 LiveProcess
*process
= NULL
;
342 ObjectFile
*objFile
= createObjectFile(argv
[0]);
343 if (objFile
== NULL
) {
344 fatal("Can't load object file %s", argv
[0]);
347 // check object type & set up syscall emulation pointer
348 if (objFile
->getArch() == ObjectFile::Alpha
) {
349 switch (objFile
->getOpSys()) {
350 case ObjectFile::Tru64
:
351 process
= new AlphaTru64Process(name
, objFile
,
352 stdin_fd
, stdout_fd
, stderr_fd
,
356 case ObjectFile::Linux
:
357 process
= new AlphaLinuxProcess(name
, objFile
,
358 stdin_fd
, stdout_fd
, stderr_fd
,
363 fatal("Unknown/unsupported operating system.");
366 fatal("Unknown object file architecture.");
372 fatal("Unknown error creating process object.");
378 BEGIN_DECLARE_SIM_OBJECT_PARAMS(LiveProcess
)
380 VectorParam
<string
> cmd
;
382 Param
<string
> output
;
383 VectorParam
<string
> env
;
385 END_DECLARE_SIM_OBJECT_PARAMS(LiveProcess
)
388 BEGIN_INIT_SIM_OBJECT_PARAMS(LiveProcess
)
390 INIT_PARAM(cmd
, "command line (executable plus arguments)"),
391 INIT_PARAM(input
, "filename for stdin (dflt: use sim stdin)"),
392 INIT_PARAM(output
, "filename for stdout/stderr (dflt: use sim stdout)"),
393 INIT_PARAM(env
, "environment settings")
395 END_INIT_SIM_OBJECT_PARAMS(LiveProcess
)
398 CREATE_SIM_OBJECT(LiveProcess
)
403 // initialize file descriptors to default: same as simulator
404 int stdin_fd
, stdout_fd
, stderr_fd
;
406 if (in
== "stdin" || in
== "cin")
407 stdin_fd
= STDIN_FILENO
;
409 stdin_fd
= Process::openInputFile(input
);
411 if (out
== "stdout" || out
== "cout")
412 stdout_fd
= STDOUT_FILENO
;
413 else if (out
== "stderr" || out
== "cerr")
414 stdout_fd
= STDERR_FILENO
;
416 stdout_fd
= Process::openOutputFile(out
);
418 stderr_fd
= (stdout_fd
!= STDOUT_FILENO
) ? stdout_fd
: STDERR_FILENO
;
420 return LiveProcess::create(getInstanceName(),
421 stdin_fd
, stdout_fd
, stderr_fd
,
425 REGISTER_SIM_OBJECT("LiveProcess", LiveProcess
)