Deleted Added
sdiff udiff text old ( 2640:266b80dd5eca ) new ( 2665:a124942bacb8 )
full compact
1/*
2 * Copyright (c) 2001-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
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.
15 *
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.
27 */
28
29#include <unistd.h>
30#include <fcntl.h>
31
32#include <string>
33
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"
48
49using namespace std;
50using namespace TheISA;
51
52//
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
56//
57#if FULL_SYSTEM
58#error "process.cc not compatible with FULL_SYSTEM"
59#endif
60
61// current number of allocated processes
62int num_processes = 0;
63
64Process::Process(const string &nm,
65 System *_system,
66 int stdin_fd, // initial I/O descriptors
67 int stdout_fd,
68 int stderr_fd)
69 : SimObject(nm), system(_system)
70{
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;
75
76 // mark remaining fds as free
77 for (int i = 3; i <= MAX_FD; ++i) {
78 fd_map[i] = -1;
79 }
80
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
85}
86
87
88void
89Process::regStats()
90{
91 using namespace Stats;
92
93 num_syscalls
94 .name(name() + ".PROG:num_syscalls")
95 .desc("Number of system calls")
96 ;
97}
98
99//
100// static helper functions
101//
102int
103Process::openInputFile(const string &filename)
104{
105 int fd = open(filename.c_str(), O_RDONLY);
106
107 if (fd == -1) {
108 perror(NULL);
109 cerr << "unable to open \"" << filename << "\" for reading\n";
110 fatal("can't open input file");
111 }
112
113 return fd;
114}
115
116
117int
118Process::openOutputFile(const string &filename)
119{
120 int fd = open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0774);
121
122 if (fd == -1) {
123 perror(NULL);
124 cerr << "unable to open \"" << filename << "\" for writing\n";
125 fatal("can't open output file");
126 }
127
128 return fd;
129}
130
131
132int
133Process::registerExecContext(ExecContext *xc)
134{
135 // add to list
136 int myIndex = execContexts.size();
137 execContexts.push_back(xc);
138
139 // return CPU number to caller
140 return myIndex;
141}
142
143void
144Process::startup()
145{
146 if (execContexts.empty())
147 fatal("Process %s is not associated with any CPUs!\n", name());
148
149 // first exec context for this process... initialize & enable
150 ExecContext *xc = execContexts[0];
151
152 // mark this context as active so it will start ticking.
153 xc->activate(0);
154
155 Port *mem_port;
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);
160}
161
162void
163Process::replaceExecContext(ExecContext *xc, int xcIndex)
164{
165 if (xcIndex >= execContexts.size()) {
166 panic("replaceExecContext: bad xcIndex, %d >= %d\n",
167 xcIndex, execContexts.size());
168 }
169
170 execContexts[xcIndex] = xc;
171}
172
173// map simulator fd sim_fd to target fd tgt_fd
174void
175Process::dup_fd(int sim_fd, int tgt_fd)
176{
177 if (tgt_fd < 0 || tgt_fd > MAX_FD)
178 panic("Process::dup_fd tried to dup past MAX_FD (%d)", tgt_fd);
179
180 fd_map[tgt_fd] = sim_fd;
181}
182
183
184// generate new target fd for sim_fd
185int
186Process::alloc_fd(int sim_fd)
187{
188 // in case open() returns an error, don't allocate a new fd
189 if (sim_fd == -1)
190 return -1;
191
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;
196 return free_fd;
197 }
198 }
199
200 panic("Process::alloc_fd: out of file descriptors!");
201}
202
203
204// free target fd (e.g., after close)
205void
206Process::free_fd(int tgt_fd)
207{
208 if (fd_map[tgt_fd] == -1)
209 warn("Process::free_fd: request to free unused fd %d", tgt_fd);
210
211 fd_map[tgt_fd] = -1;
212}
213
214
215// look up simulator fd for given target fd
216int
217Process::sim_fd(int tgt_fd)
218{
219 if (tgt_fd > MAX_FD)
220 return -1;
221
222 return fd_map[tgt_fd];
223}
224
225
226
227//
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).
231//
232DEFINE_SIM_OBJECT_CLASS_NAME("Process", Process)
233
234
235////////////////////////////////////////////////////////////////////////
236//
237// LiveProcess member definitions
238//
239////////////////////////////////////////////////////////////////////////
240
241
242void
243copyStringArray(vector<string> &strings, Addr array_ptr, Addr data_ptr,
244 TranslatingPort* memPort)
245{
246 Addr data_ptr_swap;
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;
253 }
254 // add NULL terminator
255 data_ptr = 0;
256
257 memPort->writeBlob(array_ptr, (uint8_t*)&data_ptr, sizeof(Addr));
258}
259
260LiveProcess::LiveProcess(const string &nm, ObjectFile *_objFile,
261 System *_system,
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)
266{
267 prog_fname = argv[0];
268
269 // load up symbols, if any... these may be used for debugging or
270 // profiling.
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;
278 }
279 }
280}
281
282void
283LiveProcess::argsInit(int intSize, int pageSize)
284{
285 Process::startup();
286
287 // load object file into target memory
288 objFile->loadSections(initVirtMem);
289
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;
296 }
297 int env_data_size = 0;
298 for (int i = 0; i < envp.size(); ++i) {
299 env_data_size += envp[i].size() + 1;
300 }
301
302 int space_needed =
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;
307
308 // set bottom of stack
309 stack_min = stack_base - space_needed;
310 // align it
311 stack_min &= ~(intSize-1);
312 stack_size = stack_base - stack_min;
313 // map memory
314 pTable->allocate(roundDown(stack_min, pageSize),
315 roundUp(stack_size, pageSize));
316
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;
322
323 // write contents to stack
324 uint64_t argc = argv.size();
325 if (intSize == 8)
326 argc = htog((uint64_t)argc);
327 else if (intSize == 4)
328 argc = htog((uint32_t)argc);
329 else
330 panic("Unknown int size");
331
332 initVirtMem->writeBlob(stack_min, (uint8_t*)&argc, intSize);
333
334 copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem);
335 copyStringArray(envp, envp_array_base, env_data_base, initVirtMem);
336
337 execContexts[0]->setIntReg(ArgumentReg0, argc);
338 execContexts[0]->setIntReg(ArgumentReg1, argv_array_base);
339 execContexts[0]->setIntReg(StackPointerReg, stack_min);
340
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)));
345
346 num_processes++;
347}
348
349void
350LiveProcess::syscall(int64_t callnum, ExecContext *xc)
351{
352 num_syscalls++;
353
354 SyscallDesc *desc = getDesc(callnum);
355 if (desc == NULL)
356 fatal("Syscall %d out of range", callnum);
357
358 desc->doSyscall(callnum, this, xc);
359}
360
361DEFINE_SIM_OBJECT_CLASS_NAME("LiveProcess", LiveProcess);