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.
34 #include "base/intmath.hh"
35 #include "base/loader/object_file.hh"
36 #include "base/loader/symtab.hh"
37 #include "base/statistics.hh"
38 #include "config/full_system.hh"
39 #include "cpu/exec_context.hh"
40 #include "mem/page_table.hh"
41 #include "mem/physical.hh"
42 #include "mem/translating_port.hh"
43 #include "sim/builder.hh"
44 #include "sim/process.hh"
45 #include "sim/stats.hh"
46 #include "sim/syscall_emul.hh"
47 #include "sim/system.hh"
50 using namespace TheISA
;
53 // The purpose of this code is to fake the loader & syscall mechanism
54 // when there's no OS: thus there's no resone to use it in FULL_SYSTEM
55 // mode when we do have an OS
58 #error "process.cc not compatible with FULL_SYSTEM"
61 // current number of allocated processes
62 int num_processes
= 0;
64 Process::Process(const string
&nm
,
66 int stdin_fd
, // initial I/O descriptors
69 : SimObject(nm
), system(_system
)
71 // initialize first 3 fds (stdin, stdout, stderr)
72 fd_map
[STDIN_FILENO
] = stdin_fd
;
73 fd_map
[STDOUT_FILENO
] = stdout_fd
;
74 fd_map
[STDERR_FILENO
] = stderr_fd
;
76 // mark remaining fds as free
77 for (int i
= 3; i
<= MAX_FD
; ++i
) {
81 mmap_start
= mmap_end
= 0;
82 nxm_start
= nxm_end
= 0;
83 pTable
= new PageTable(system
);
84 // other parameters will be initialized when the program is loaded
91 using namespace Stats
;
94 .name(name() + ".PROG:num_syscalls")
95 .desc("Number of system calls")
100 // static helper functions
103 Process::openInputFile(const string
&filename
)
105 int fd
= open(filename
.c_str(), O_RDONLY
);
109 cerr
<< "unable to open \"" << filename
<< "\" for reading\n";
110 fatal("can't open input file");
118 Process::openOutputFile(const string
&filename
)
120 int fd
= open(filename
.c_str(), O_WRONLY
| O_CREAT
| O_TRUNC
, 0774);
124 cerr
<< "unable to open \"" << filename
<< "\" for writing\n";
125 fatal("can't open output file");
133 Process::registerExecContext(ExecContext
*xc
)
136 int myIndex
= execContexts
.size();
137 execContexts
.push_back(xc
);
139 // return CPU number to caller
146 if (execContexts
.empty())
147 fatal("Process %s is not associated with any CPUs!\n", name());
149 // first exec context for this process... initialize & enable
150 ExecContext
*xc
= execContexts
[0];
152 // mark this context as active so it will start ticking.
156 mem_port
= system
->physmem
->getPort("functional");
157 initVirtMem
= new TranslatingPort("process init port", pTable
, true);
158 mem_port
->setPeer(initVirtMem
);
159 initVirtMem
->setPeer(mem_port
);
163 Process::replaceExecContext(ExecContext
*xc
, int xcIndex
)
165 if (xcIndex
>= execContexts
.size()) {
166 panic("replaceExecContext: bad xcIndex, %d >= %d\n",
167 xcIndex
, execContexts
.size());
170 execContexts
[xcIndex
] = xc
;
173 // map simulator fd sim_fd to target fd tgt_fd
175 Process::dup_fd(int sim_fd
, int tgt_fd
)
177 if (tgt_fd
< 0 || tgt_fd
> MAX_FD
)
178 panic("Process::dup_fd tried to dup past MAX_FD (%d)", tgt_fd
);
180 fd_map
[tgt_fd
] = sim_fd
;
184 // generate new target fd for sim_fd
186 Process::alloc_fd(int sim_fd
)
188 // in case open() returns an error, don't allocate a new fd
192 // find first free target fd
193 for (int free_fd
= 0; free_fd
< MAX_FD
; ++free_fd
) {
194 if (fd_map
[free_fd
] == -1) {
195 fd_map
[free_fd
] = sim_fd
;
200 panic("Process::alloc_fd: out of file descriptors!");
204 // free target fd (e.g., after close)
206 Process::free_fd(int tgt_fd
)
208 if (fd_map
[tgt_fd
] == -1)
209 warn("Process::free_fd: request to free unused fd %d", tgt_fd
);
215 // look up simulator fd for given target fd
217 Process::sim_fd(int tgt_fd
)
222 return fd_map
[tgt_fd
];
228 // need to declare these here since there is no concrete Process type
229 // that can be constructed (i.e., no REGISTER_SIM_OBJECT() macro call,
230 // which is where these get declared for concrete types).
232 DEFINE_SIM_OBJECT_CLASS_NAME("Process", Process
)
235 ////////////////////////////////////////////////////////////////////////
237 // LiveProcess member definitions
239 ////////////////////////////////////////////////////////////////////////
243 copyStringArray(vector
<string
> &strings
, Addr array_ptr
, Addr data_ptr
,
244 TranslatingPort
* memPort
)
247 for (int i
= 0; i
< strings
.size(); ++i
) {
248 data_ptr_swap
= htog(data_ptr
);
249 memPort
->writeBlob(array_ptr
, (uint8_t*)&data_ptr_swap
, sizeof(Addr
));
250 memPort
->writeString(data_ptr
, strings
[i
].c_str());
251 array_ptr
+= sizeof(Addr
);
252 data_ptr
+= strings
[i
].size() + 1;
254 // add NULL terminator
257 memPort
->writeBlob(array_ptr
, (uint8_t*)&data_ptr
, sizeof(Addr
));
260 LiveProcess::LiveProcess(const string
&nm
, ObjectFile
*_objFile
,
262 int stdin_fd
, int stdout_fd
, int stderr_fd
,
263 vector
<string
> &_argv
, vector
<string
> &_envp
)
264 : Process(nm
, _system
, stdin_fd
, stdout_fd
, stderr_fd
),
265 objFile(_objFile
), argv(_argv
), envp(_envp
)
267 prog_fname
= argv
[0];
269 // load up symbols, if any... these may be used for debugging or
271 if (!debugSymbolTable
) {
272 debugSymbolTable
= new SymbolTable();
273 if (!objFile
->loadGlobalSymbols(debugSymbolTable
) ||
274 !objFile
->loadLocalSymbols(debugSymbolTable
)) {
275 // didn't load any symbols
276 delete debugSymbolTable
;
277 debugSymbolTable
= NULL
;
283 LiveProcess::argsInit(int intSize
, int pageSize
)
287 // load object file into target memory
288 objFile
->loadSections(initVirtMem
);
290 // Calculate how much space we need for arg & env arrays.
291 int argv_array_size
= intSize
* (argv
.size() + 1);
292 int envp_array_size
= intSize
* (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
;
311 stack_min
&= ~(intSize
-1);
312 stack_size
= stack_base
- stack_min
;
314 pTable
->allocate(roundDown(stack_min
, pageSize
),
315 roundUp(stack_size
, pageSize
));
317 // map out initial stack contents
318 Addr argv_array_base
= stack_min
+ intSize
; // room for argc
319 Addr envp_array_base
= argv_array_base
+ argv_array_size
;
320 Addr arg_data_base
= envp_array_base
+ envp_array_size
;
321 Addr env_data_base
= arg_data_base
+ arg_data_size
;
323 // write contents to stack
324 uint64_t argc
= argv
.size();
326 argc
= htog((uint64_t)argc
);
327 else if (intSize
== 4)
328 argc
= htog((uint32_t)argc
);
330 panic("Unknown int size");
332 initVirtMem
->writeBlob(stack_min
, (uint8_t*)&argc
, intSize
);
334 copyStringArray(argv
, argv_array_base
, arg_data_base
, initVirtMem
);
335 copyStringArray(envp
, envp_array_base
, env_data_base
, initVirtMem
);
337 execContexts
[0]->setIntReg(ArgumentReg0
, argc
);
338 execContexts
[0]->setIntReg(ArgumentReg1
, argv_array_base
);
339 execContexts
[0]->setIntReg(StackPointerReg
, stack_min
);
341 Addr prog_entry
= objFile
->entryPoint();
342 execContexts
[0]->setPC(prog_entry
);
343 execContexts
[0]->setNextPC(prog_entry
+ sizeof(MachInst
));
344 execContexts
[0]->setNextNPC(prog_entry
+ (2 * sizeof(MachInst
)));
350 LiveProcess::syscall(int64_t callnum
, ExecContext
*xc
)
354 SyscallDesc
*desc
= getDesc(callnum
);
356 fatal("Syscall %d out of range", callnum
);
358 desc
->doSyscall(callnum
, this, xc
);
361 DEFINE_SIM_OBJECT_CLASS_NAME("LiveProcess", LiveProcess
);