2 * Copyright (c) 2003 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 "main_memory.hh"
40 #include "fake_syscall.hh"
41 #include "object_file.hh"
42 #include "exec_context.hh"
45 #include "statistics.hh"
46 #include "sim_stats.hh"
51 // The purpose of this code is to fake the loader & syscall mechanism
52 // when there's no OS: thus there's no resone to use it in FULL_SYSTEM
53 // mode when we do have an OS
56 #error "prog.cc not compatible with FULL_SYSTEM"
59 // max allowable number of processes: should be no real cost to
60 // cranking this up if necessary
61 const int MAX_PROCESSES
= 8;
63 // current number of allocated processes
64 int num_processes
= 0;
66 Process::Process(const string
&name
,
67 int stdin_fd
, // initial I/O descriptors
72 // allocate memory space
73 memory
= new MainMemory(name
+ ".MainMem");
75 // allocate initial register file
76 init_regs
= new RegFile
;
78 // initialize first 3 fds (stdin, stdout, stderr)
79 fd_map
[STDIN_FILENO
] = stdin_fd
;
80 fd_map
[STDOUT_FILENO
] = stdout_fd
;
81 fd_map
[STDERR_FILENO
] = stderr_fd
;
83 // mark remaining fds as free
84 for (int i
= 3; i
<= MAX_FD
; ++i
) {
92 // other parameters will be initialized when the program is loaded
98 using namespace Statistics
;
101 .name(name() + ".PROG:num_syscalls")
102 .desc("Number of system calls")
107 // static helper functions
110 Process::openInputFile(const string
&filename
)
112 int fd
= open(filename
.c_str(), O_RDONLY
);
116 cerr
<< "unable to open \"" << filename
<< "\" for reading\n";
117 fatal("can't open input file");
125 Process::openOutputFile(const string
&filename
)
127 int fd
= open(filename
.c_str(), O_WRONLY
| O_CREAT
| O_TRUNC
, 0774);
131 cerr
<< "unable to open \"" << filename
<< "\" for writing\n";
132 fatal("can't open output file");
140 Process::registerExecContext(ExecContext
*ec
)
142 if (execContexts
.empty()) {
143 // first exec context for this process... initialize & enable
145 // copy process's initial regs struct
146 ec
->regs
= *init_regs
;
148 // mark this context as active
149 ec
->setStatus(ExecContext::Active
);
152 ec
->setStatus(ExecContext::Unallocated
);
156 execContexts
.push_back(ec
);
158 // increment available CPU count
163 // map simulator fd sim_fd to target fd tgt_fd
165 Process::dup_fd(int sim_fd
, int tgt_fd
)
167 if (tgt_fd
< 0 || tgt_fd
> MAX_FD
)
168 panic("Process::dup_fd tried to dup past MAX_FD (%d)", tgt_fd
);
170 fd_map
[tgt_fd
] = sim_fd
;
174 // generate new target fd for sim_fd
176 Process::open_fd(int sim_fd
)
180 // in case open() returns an error, don't allocate a new fd
184 // find first free target fd
185 for (free_fd
= 0; fd_map
[free_fd
] >= 0; ++free_fd
) {
186 if (free_fd
== MAX_FD
)
187 panic("Process::open_fd: out of file descriptors!");
190 fd_map
[free_fd
] = sim_fd
;
196 // look up simulator fd for given target fd
198 Process::sim_fd(int tgt_fd
)
203 return fd_map
[tgt_fd
];
209 // need to declare these here since there is no concrete Process type
210 // that can be constructed (i.e., no REGISTER_SIM_OBJECT() macro call,
211 // which is where these get declared for concrete types).
213 DEFINE_SIM_OBJECT_CLASS_NAME("Process object", Process
)
216 ////////////////////////////////////////////////////////////////////////
218 // LiveProcess member definitions
220 ////////////////////////////////////////////////////////////////////////
224 copyStringArray(vector
<string
> &strings
, Addr array_ptr
, Addr data_ptr
,
225 FunctionalMemory
*memory
)
227 for (int i
= 0; i
< strings
.size(); ++i
) {
228 memory
->access(Write
, array_ptr
, &data_ptr
, sizeof(Addr
));
229 memory
->writeString(data_ptr
, strings
[i
].c_str());
230 array_ptr
+= sizeof(Addr
);
231 data_ptr
+= strings
[i
].size() + 1;
233 // add NULL terminator
235 memory
->access(Write
, array_ptr
, &data_ptr
, sizeof(Addr
));
238 LiveProcess::LiveProcess(const string
&name
,
239 int stdin_fd
, int stdout_fd
, int stderr_fd
,
240 vector
<string
> &argv
, vector
<string
> &envp
)
241 : Process(name
, stdin_fd
, stdout_fd
, stderr_fd
)
243 prog_fname
= argv
[0];
244 ObjectFile
*objFile
= createObjectFile(prog_fname
);
245 if (objFile
== NULL
) {
246 fatal("Can't load object file %s", prog_fname
);
249 prog_entry
= objFile
->entryPoint();
250 text_base
= objFile
->textBase();
251 text_size
= objFile
->textSize();
252 data_base
= objFile
->dataBase();
253 data_size
= objFile
->dataSize() + objFile
->bssSize();
254 brk_point
= ROUND_UP(data_base
+ data_size
, VMPageSize
);
256 // load object file into target memory
257 objFile
->loadSections(memory
);
259 // Set up stack. On Alpha, stack goes below text section. This
260 // code should get moved to some architecture-specific spot.
261 stack_base
= text_base
- (409600+4096);
263 // Set pointer for next thread stack. Reserve 8M for main stack.
264 next_thread_stack_base
= stack_base
- (8 * 1024 * 1024);
266 // Calculate how much space we need for arg & env arrays.
267 int argv_array_size
= sizeof(Addr
) * (argv
.size() + 1);
268 int envp_array_size
= sizeof(Addr
) * (envp
.size() + 1);
269 int arg_data_size
= 0;
270 for (int i
= 0; i
< argv
.size(); ++i
) {
271 arg_data_size
+= argv
[i
].size() + 1;
273 int env_data_size
= 0;
274 for (int i
= 0; i
< envp
.size(); ++i
) {
275 env_data_size
+= envp
[i
].size() + 1;
279 argv_array_size
+ envp_array_size
+ arg_data_size
+ env_data_size
;
280 // for SimpleScalar compatibility
281 if (space_needed
< 16384)
282 space_needed
= 16384;
284 // set bottom of stack
285 stack_min
= stack_base
- space_needed
;
288 stack_size
= stack_base
- stack_min
;
290 // map out initial stack contents
291 Addr argv_array_base
= stack_min
+ sizeof(uint64_t); // room for argc
292 Addr envp_array_base
= argv_array_base
+ argv_array_size
;
293 Addr arg_data_base
= envp_array_base
+ envp_array_size
;
294 Addr env_data_base
= arg_data_base
+ arg_data_size
;
296 // write contents to stack
297 uint64_t argc
= argv
.size();
298 memory
->access(Write
, stack_min
, &argc
, sizeof(uint64_t));
300 copyStringArray(argv
, argv_array_base
, arg_data_base
, memory
);
301 copyStringArray(envp
, envp_array_base
, env_data_base
, memory
);
303 init_regs
->intRegFile
[ArgumentReg0
] = argc
;
304 init_regs
->intRegFile
[ArgumentReg1
] = argv_array_base
;
305 init_regs
->intRegFile
[StackPointerReg
] = stack_min
;
306 init_regs
->intRegFile
[GlobalPointerReg
] = objFile
->globalPointer();
307 init_regs
->pc
= prog_entry
;
308 init_regs
->npc
= prog_entry
+ sizeof(MachInst
);
313 LiveProcess::syscall(ExecContext
*xc
)
317 fake_syscall(this, xc
);
321 BEGIN_DECLARE_SIM_OBJECT_PARAMS(LiveProcess
)
323 VectorParam
<string
> cmd
;
325 Param
<string
> output
;
326 VectorParam
<string
> env
;
328 END_DECLARE_SIM_OBJECT_PARAMS(LiveProcess
)
331 BEGIN_INIT_SIM_OBJECT_PARAMS(LiveProcess
)
333 INIT_PARAM(cmd
, "command line (executable plus arguments)"),
334 INIT_PARAM(input
, "filename for stdin (dflt: use sim stdin)"),
335 INIT_PARAM(output
, "filename for stdout/stderr (dflt: use sim stdout)"),
336 INIT_PARAM(env
, "environment settings")
338 END_INIT_SIM_OBJECT_PARAMS(LiveProcess
)
341 CREATE_SIM_OBJECT(LiveProcess
)
343 // initialize file descriptors to default: same as simulator
344 int stdin_fd
= input
.isValid() ? Process::openInputFile(input
) : 0;
345 int stdout_fd
= output
.isValid() ? Process::openOutputFile(output
) : 1;
346 int stderr_fd
= output
.isValid() ? stdout_fd
: 2;
348 // dummy for default env
349 vector
<string
> null_vec
;
351 // We do this with "temp" because of the bogus compiler warning
352 // you get with g++ 2.95 -O if you just "return new LiveProcess(..."
353 LiveProcess
*temp
= new LiveProcess(getInstanceName(),
354 stdin_fd
, stdout_fd
, stderr_fd
,
356 env
.isValid() ? env
: null_vec
);
362 REGISTER_SIM_OBJECT("LiveProcess", LiveProcess
)