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 "base/intmath.hh"
39 #include "base/loader/object_file.hh"
40 #include "base/loader/symtab.hh"
41 #include "base/statistics.hh"
42 #include "config/full_system.hh"
43 #include "cpu/thread_context.hh"
44 #include "mem/page_table.hh"
45 #include "mem/physical.hh"
46 #include "mem/translating_port.hh"
47 #include "sim/builder.hh"
48 #include "sim/process.hh"
49 #include "sim/stats.hh"
50 #include "sim/syscall_emul.hh"
51 #include "sim/system.hh"
54 using namespace TheISA
;
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
&nm
,
70 int stdin_fd
, // initial I/O descriptors
73 : SimObject(nm
), system(_system
)
75 // initialize first 3 fds (stdin, stdout, stderr)
76 fd_map
[STDIN_FILENO
] = stdin_fd
;
77 fd_map
[STDOUT_FILENO
] = stdout_fd
;
78 fd_map
[STDERR_FILENO
] = stderr_fd
;
80 // mark remaining fds as free
81 for (int i
= 3; i
<= MAX_FD
; ++i
) {
85 mmap_start
= mmap_end
= 0;
86 nxm_start
= nxm_end
= 0;
87 pTable
= new PageTable(system
);
88 // other parameters will be initialized when the program is loaded
95 using namespace Stats
;
98 .name(name() + ".PROG:num_syscalls")
99 .desc("Number of system calls")
104 // static helper functions
107 Process::openInputFile(const string
&filename
)
109 int fd
= open(filename
.c_str(), O_RDONLY
);
113 cerr
<< "unable to open \"" << filename
<< "\" for reading\n";
114 fatal("can't open input file");
122 Process::openOutputFile(const string
&filename
)
124 int fd
= open(filename
.c_str(), O_WRONLY
| O_CREAT
| O_TRUNC
, 0774);
128 cerr
<< "unable to open \"" << filename
<< "\" for writing\n";
129 fatal("can't open output file");
137 Process::registerThreadContext(ThreadContext
*tc
)
140 int myIndex
= threadContexts
.size();
141 threadContexts
.push_back(tc
);
143 // return CPU number to caller
150 if (threadContexts
.empty())
151 fatal("Process %s is not associated with any CPUs!\n", name());
153 // first thread context for this process... initialize & enable
154 ThreadContext
*tc
= threadContexts
[0];
156 // mark this context as active so it will start ticking.
160 mem_port
= system
->physmem
->getPort("functional");
161 initVirtMem
= new TranslatingPort("process init port", pTable
, true);
162 mem_port
->setPeer(initVirtMem
);
163 initVirtMem
->setPeer(mem_port
);
167 Process::replaceThreadContext(ThreadContext
*tc
, int tcIndex
)
169 if (tcIndex
>= threadContexts
.size()) {
170 panic("replaceThreadContext: bad tcIndex, %d >= %d\n",
171 tcIndex
, threadContexts
.size());
174 threadContexts
[tcIndex
] = tc
;
177 // map simulator fd sim_fd to target fd tgt_fd
179 Process::dup_fd(int sim_fd
, int tgt_fd
)
181 if (tgt_fd
< 0 || tgt_fd
> MAX_FD
)
182 panic("Process::dup_fd tried to dup past MAX_FD (%d)", tgt_fd
);
184 fd_map
[tgt_fd
] = sim_fd
;
188 // generate new target fd for sim_fd
190 Process::alloc_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 (int free_fd
= 0; free_fd
< MAX_FD
; ++free_fd
) {
198 if (fd_map
[free_fd
] == -1) {
199 fd_map
[free_fd
] = sim_fd
;
204 panic("Process::alloc_fd: out of file descriptors!");
208 // free target fd (e.g., after close)
210 Process::free_fd(int tgt_fd
)
212 if (fd_map
[tgt_fd
] == -1)
213 warn("Process::free_fd: request to free unused fd %d", tgt_fd
);
219 // look up simulator fd for given target fd
221 Process::sim_fd(int tgt_fd
)
226 return fd_map
[tgt_fd
];
232 // need to declare these here since there is no concrete Process type
233 // that can be constructed (i.e., no REGISTER_SIM_OBJECT() macro call,
234 // which is where these get declared for concrete types).
236 DEFINE_SIM_OBJECT_CLASS_NAME("Process", Process
)
239 ////////////////////////////////////////////////////////////////////////
241 // LiveProcess member definitions
243 ////////////////////////////////////////////////////////////////////////
247 copyStringArray(vector
<string
> &strings
, Addr array_ptr
, Addr data_ptr
,
248 TranslatingPort
* memPort
)
251 for (int i
= 0; i
< strings
.size(); ++i
) {
252 data_ptr_swap
= htog(data_ptr
);
253 memPort
->writeBlob(array_ptr
, (uint8_t*)&data_ptr_swap
, sizeof(Addr
));
254 memPort
->writeString(data_ptr
, strings
[i
].c_str());
255 array_ptr
+= sizeof(Addr
);
256 data_ptr
+= strings
[i
].size() + 1;
258 // add NULL terminator
261 memPort
->writeBlob(array_ptr
, (uint8_t*)&data_ptr
, sizeof(Addr
));
264 LiveProcess::LiveProcess(const string
&nm
, ObjectFile
*_objFile
,
266 int stdin_fd
, int stdout_fd
, int stderr_fd
,
267 vector
<string
> &_argv
, vector
<string
> &_envp
)
268 : Process(nm
, _system
, stdin_fd
, stdout_fd
, stderr_fd
),
269 objFile(_objFile
), argv(_argv
), envp(_envp
)
271 prog_fname
= argv
[0];
273 // load up symbols, if any... these may be used for debugging or
275 if (!debugSymbolTable
) {
276 debugSymbolTable
= new SymbolTable();
277 if (!objFile
->loadGlobalSymbols(debugSymbolTable
) ||
278 !objFile
->loadLocalSymbols(debugSymbolTable
)) {
279 // didn't load any symbols
280 delete debugSymbolTable
;
281 debugSymbolTable
= NULL
;
287 LiveProcess::argsInit(int intSize
, int pageSize
)
291 // load object file into target memory
292 objFile
->loadSections(initVirtMem
);
294 // Calculate how much space we need for arg & env arrays.
295 int argv_array_size
= intSize
* (argv
.size() + 1);
296 int envp_array_size
= intSize
* (envp
.size() + 1);
297 int arg_data_size
= 0;
298 for (int i
= 0; i
< argv
.size(); ++i
) {
299 arg_data_size
+= argv
[i
].size() + 1;
301 int env_data_size
= 0;
302 for (int i
= 0; i
< envp
.size(); ++i
) {
303 env_data_size
+= envp
[i
].size() + 1;
307 argv_array_size
+ envp_array_size
+ arg_data_size
+ env_data_size
;
308 // for SimpleScalar compatibility
309 if (space_needed
< 16384)
310 space_needed
= 16384;
312 // set bottom of stack
313 stack_min
= stack_base
- space_needed
;
315 stack_min
&= ~(intSize
-1);
316 stack_size
= stack_base
- stack_min
;
318 pTable
->allocate(roundDown(stack_min
, pageSize
),
319 roundUp(stack_size
, pageSize
));
321 // map out initial stack contents
322 Addr argv_array_base
= stack_min
+ intSize
; // room for argc
323 Addr envp_array_base
= argv_array_base
+ argv_array_size
;
324 Addr arg_data_base
= envp_array_base
+ envp_array_size
;
325 Addr env_data_base
= arg_data_base
+ arg_data_size
;
327 // write contents to stack
328 uint64_t argc
= argv
.size();
330 argc
= htog((uint64_t)argc
);
331 else if (intSize
== 4)
332 argc
= htog((uint32_t)argc
);
334 panic("Unknown int size");
336 initVirtMem
->writeBlob(stack_min
, (uint8_t*)&argc
, intSize
);
338 copyStringArray(argv
, argv_array_base
, arg_data_base
, initVirtMem
);
339 copyStringArray(envp
, envp_array_base
, env_data_base
, initVirtMem
);
341 threadContexts
[0]->setIntReg(ArgumentReg0
, argc
);
342 threadContexts
[0]->setIntReg(ArgumentReg1
, argv_array_base
);
343 threadContexts
[0]->setIntReg(StackPointerReg
, stack_min
);
345 Addr prog_entry
= objFile
->entryPoint();
346 threadContexts
[0]->setPC(prog_entry
);
347 threadContexts
[0]->setNextPC(prog_entry
+ sizeof(MachInst
));
348 threadContexts
[0]->setNextNPC(prog_entry
+ (2 * sizeof(MachInst
)));
354 LiveProcess::syscall(int64_t callnum
, ThreadContext
*tc
)
358 SyscallDesc
*desc
= getDesc(callnum
);
360 fatal("Syscall %d out of range", callnum
);
362 desc
->doSyscall(callnum
, this, tc
);
365 DEFINE_SIM_OBJECT_CLASS_NAME("LiveProcess", LiveProcess
);