process.cc (2640:266b80dd5eca) process.cc (2665:a124942bacb8)
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.
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 * Authors: Nathan Binkert
29 * Steve Reinhardt
30 * Ali Saidi
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);
31 */
32
33#include <unistd.h>
34#include <fcntl.h>
35
36#include <string>
37
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/exec_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"
52
53using namespace std;
54using namespace TheISA;
55
56//
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
60//
61#if FULL_SYSTEM
62#error "process.cc not compatible with FULL_SYSTEM"
63#endif
64
65// current number of allocated processes
66int num_processes = 0;
67
68Process::Process(const string &nm,
69 System *_system,
70 int stdin_fd, // initial I/O descriptors
71 int stdout_fd,
72 int stderr_fd)
73 : SimObject(nm), system(_system)
74{
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;
79
80 // mark remaining fds as free
81 for (int i = 3; i <= MAX_FD; ++i) {
82 fd_map[i] = -1;
83 }
84
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
89}
90
91
92void
93Process::regStats()
94{
95 using namespace Stats;
96
97 num_syscalls
98 .name(name() + ".PROG:num_syscalls")
99 .desc("Number of system calls")
100 ;
101}
102
103//
104// static helper functions
105//
106int
107Process::openInputFile(const string &filename)
108{
109 int fd = open(filename.c_str(), O_RDONLY);
110
111 if (fd == -1) {
112 perror(NULL);
113 cerr << "unable to open \"" << filename << "\" for reading\n";
114 fatal("can't open input file");
115 }
116
117 return fd;
118}
119
120
121int
122Process::openOutputFile(const string &filename)
123{
124 int fd = open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0774);
125
126 if (fd == -1) {
127 perror(NULL);
128 cerr << "unable to open \"" << filename << "\" for writing\n";
129 fatal("can't open output file");
130 }
131
132 return fd;
133}
134
135
136int
137Process::registerExecContext(ExecContext *xc)
138{
139 // add to list
140 int myIndex = execContexts.size();
141 execContexts.push_back(xc);
142
143 // return CPU number to caller
144 return myIndex;
145}
146
147void
148Process::startup()
149{
150 if (execContexts.empty())
151 fatal("Process %s is not associated with any CPUs!\n", name());
152
153 // first exec context for this process... initialize & enable
154 ExecContext *xc = execContexts[0];
155
156 // mark this context as active so it will start ticking.
157 xc->activate(0);
158
159 Port *mem_port;
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);
164}
165
166void
167Process::replaceExecContext(ExecContext *xc, int xcIndex)
168{
169 if (xcIndex >= execContexts.size()) {
170 panic("replaceExecContext: bad xcIndex, %d >= %d\n",
171 xcIndex, execContexts.size());
172 }
173
174 execContexts[xcIndex] = xc;
175}
176
177// map simulator fd sim_fd to target fd tgt_fd
178void
179Process::dup_fd(int sim_fd, int tgt_fd)
180{
181 if (tgt_fd < 0 || tgt_fd > MAX_FD)
182 panic("Process::dup_fd tried to dup past MAX_FD (%d)", tgt_fd);
183
184 fd_map[tgt_fd] = sim_fd;
185}
186
187
188// generate new target fd for sim_fd
189int
190Process::alloc_fd(int sim_fd)
191{
192 // in case open() returns an error, don't allocate a new fd
193 if (sim_fd == -1)
194 return -1;
195
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;
200 return free_fd;
201 }
202 }
203
204 panic("Process::alloc_fd: out of file descriptors!");
205}
206
207
208// free target fd (e.g., after close)
209void
210Process::free_fd(int tgt_fd)
211{
212 if (fd_map[tgt_fd] == -1)
213 warn("Process::free_fd: request to free unused fd %d", tgt_fd);
214
215 fd_map[tgt_fd] = -1;
216}
217
218
219// look up simulator fd for given target fd
220int
221Process::sim_fd(int tgt_fd)
222{
223 if (tgt_fd > MAX_FD)
224 return -1;
225
226 return fd_map[tgt_fd];
227}
228
229
230
231//
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).
235//
236DEFINE_SIM_OBJECT_CLASS_NAME("Process", Process)
237
238
239////////////////////////////////////////////////////////////////////////
240//
241// LiveProcess member definitions
242//
243////////////////////////////////////////////////////////////////////////
244
245
246void
247copyStringArray(vector<string> &strings, Addr array_ptr, Addr data_ptr,
248 TranslatingPort* memPort)
249{
250 Addr data_ptr_swap;
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;
257 }
258 // add NULL terminator
259 data_ptr = 0;
260
261 memPort->writeBlob(array_ptr, (uint8_t*)&data_ptr, sizeof(Addr));
262}
263
264LiveProcess::LiveProcess(const string &nm, ObjectFile *_objFile,
265 System *_system,
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)
270{
271 prog_fname = argv[0];
272
273 // load up symbols, if any... these may be used for debugging or
274 // profiling.
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;
282 }
283 }
284}
285
286void
287LiveProcess::argsInit(int intSize, int pageSize)
288{
289 Process::startup();
290
291 // load object file into target memory
292 objFile->loadSections(initVirtMem);
293
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;
300 }
301 int env_data_size = 0;
302 for (int i = 0; i < envp.size(); ++i) {
303 env_data_size += envp[i].size() + 1;
304 }
305
306 int space_needed =
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;
311
312 // set bottom of stack
313 stack_min = stack_base - space_needed;
314 // align it
315 stack_min &= ~(intSize-1);
316 stack_size = stack_base - stack_min;
317 // map memory
318 pTable->allocate(roundDown(stack_min, pageSize),
319 roundUp(stack_size, pageSize));
320
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;
326
327 // write contents to stack
328 uint64_t argc = argv.size();
329 if (intSize == 8)
330 argc = htog((uint64_t)argc);
331 else if (intSize == 4)
332 argc = htog((uint32_t)argc);
333 else
334 panic("Unknown int size");
335
336 initVirtMem->writeBlob(stack_min, (uint8_t*)&argc, intSize);
337
338 copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem);
339 copyStringArray(envp, envp_array_base, env_data_base, initVirtMem);
340
341 execContexts[0]->setIntReg(ArgumentReg0, argc);
342 execContexts[0]->setIntReg(ArgumentReg1, argv_array_base);
343 execContexts[0]->setIntReg(StackPointerReg, stack_min);
344
345 Addr prog_entry = objFile->entryPoint();
346 execContexts[0]->setPC(prog_entry);
347 execContexts[0]->setNextPC(prog_entry + sizeof(MachInst));
348 execContexts[0]->setNextNPC(prog_entry + (2 * sizeof(MachInst)));
349
350 num_processes++;
351}
352
353void
354LiveProcess::syscall(int64_t callnum, ExecContext *xc)
355{
356 num_syscalls++;
357
358 SyscallDesc *desc = getDesc(callnum);
359 if (desc == NULL)
360 fatal("Syscall %d out of range", callnum);
361
362 desc->doSyscall(callnum, this, xc);
363}
364
365DEFINE_SIM_OBJECT_CLASS_NAME("LiveProcess", LiveProcess);