process.cc (7447:3fc243687abb) process.cc (7487:2a5e4070155e)
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
31 */
32
33#include <unistd.h>
34#include <fcntl.h>
35
36#include <cstdio>
37#include <string>
38
39#include "base/intmath.hh"
40#include "base/loader/object_file.hh"
41#include "base/loader/symtab.hh"
42#include "base/statistics.hh"
43#include "config/full_system.hh"
44#include "config/the_isa.hh"
45#include "cpu/thread_context.hh"
46#include "mem/page_table.hh"
47#include "mem/physical.hh"
48#include "mem/translating_port.hh"
49#include "params/Process.hh"
50#include "params/LiveProcess.hh"
51#include "sim/debug.hh"
52#include "sim/process.hh"
53#include "sim/process_impl.hh"
54#include "sim/stats.hh"
55#include "sim/syscall_emul.hh"
56#include "sim/system.hh"
57
58#if THE_ISA == ALPHA_ISA
59#include "arch/alpha/linux/process.hh"
60#include "arch/alpha/tru64/process.hh"
61#elif THE_ISA == SPARC_ISA
62#include "arch/sparc/linux/process.hh"
63#include "arch/sparc/solaris/process.hh"
64#elif THE_ISA == MIPS_ISA
65#include "arch/mips/linux/process.hh"
66#elif THE_ISA == ARM_ISA
67#include "arch/arm/linux/process.hh"
68#elif THE_ISA == X86_ISA
69#include "arch/x86/linux/process.hh"
70#elif THE_ISA == POWER_ISA
71#include "arch/power/linux/process.hh"
72#else
73#error "THE_ISA not set"
74#endif
75
76
77using namespace std;
78using namespace TheISA;
79
80//
81// The purpose of this code is to fake the loader & syscall mechanism
82// when there's no OS: thus there's no resone to use it in FULL_SYSTEM
83// mode when we do have an OS
84//
85#if FULL_SYSTEM
86#error "process.cc not compatible with FULL_SYSTEM"
87#endif
88
89// current number of allocated processes
90int num_processes = 0;
91
92template<class IntType>
93AuxVector<IntType>::AuxVector(IntType type, IntType val)
94{
95 a_type = TheISA::htog(type);
96 a_val = TheISA::htog(val);
97}
98
99template class AuxVector<uint32_t>;
100template class AuxVector<uint64_t>;
101
102Process::Process(ProcessParams * params)
103 : SimObject(params), system(params->system), checkpointRestored(false),
104 max_stack_size(params->max_stack_size)
105{
106 string in = params->input;
107 string out = params->output;
108 string err = params->errout;
109
110 // initialize file descriptors to default: same as simulator
111 int stdin_fd, stdout_fd, stderr_fd;
112
113 if (in == "stdin" || in == "cin")
114 stdin_fd = STDIN_FILENO;
115 else if (in == "None")
116 stdin_fd = -1;
117 else
118 stdin_fd = Process::openInputFile(in);
119
120 if (out == "stdout" || out == "cout")
121 stdout_fd = STDOUT_FILENO;
122 else if (out == "stderr" || out == "cerr")
123 stdout_fd = STDERR_FILENO;
124 else if (out == "None")
125 stdout_fd = -1;
126 else
127 stdout_fd = Process::openOutputFile(out);
128
129 if (err == "stdout" || err == "cout")
130 stderr_fd = STDOUT_FILENO;
131 else if (err == "stderr" || err == "cerr")
132 stderr_fd = STDERR_FILENO;
133 else if (err == "None")
134 stderr_fd = -1;
135 else if (err == out)
136 stderr_fd = stdout_fd;
137 else
138 stderr_fd = Process::openOutputFile(err);
139
140 M5_pid = system->allocatePID();
141 // initialize first 3 fds (stdin, stdout, stderr)
142 Process::FdMap *fdo = &fd_map[STDIN_FILENO];
143 fdo->fd = stdin_fd;
144 fdo->filename = in;
145 fdo->flags = O_RDONLY;
146 fdo->mode = -1;
147 fdo->fileOffset = 0;
148
149 fdo = &fd_map[STDOUT_FILENO];
150 fdo->fd = stdout_fd;
151 fdo->filename = out;
152 fdo->flags = O_WRONLY | O_CREAT | O_TRUNC;
153 fdo->mode = 0774;
154 fdo->fileOffset = 0;
155
156 fdo = &fd_map[STDERR_FILENO];
157 fdo->fd = stderr_fd;
158 fdo->filename = err;
159 fdo->flags = O_WRONLY;
160 fdo->mode = -1;
161 fdo->fileOffset = 0;
162
163
164 // mark remaining fds as free
165 for (int i = 3; i <= MAX_FD; ++i) {
166 Process::FdMap *fdo = &fd_map[i];
167 fdo->fd = -1;
168 }
169
170 mmap_start = mmap_end = 0;
171 nxm_start = nxm_end = 0;
172 pTable = new PageTable(this);
173 // other parameters will be initialized when the program is loaded
174}
175
176
177void
178Process::regStats()
179{
180 using namespace Stats;
181
182 num_syscalls
183 .name(name() + ".PROG:num_syscalls")
184 .desc("Number of system calls")
185 ;
186}
187
188//
189// static helper functions
190//
191int
192Process::openInputFile(const string &filename)
193{
194 int fd = open(filename.c_str(), O_RDONLY);
195
196 if (fd == -1) {
197 perror(NULL);
198 cerr << "unable to open \"" << filename << "\" for reading\n";
199 fatal("can't open input file");
200 }
201
202 return fd;
203}
204
205
206int
207Process::openOutputFile(const string &filename)
208{
209 int fd = open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0664);
210
211 if (fd == -1) {
212 perror(NULL);
213 cerr << "unable to open \"" << filename << "\" for writing\n";
214 fatal("can't open output file");
215 }
216
217 return fd;
218}
219
220ThreadContext *
221Process::findFreeContext()
222{
223 int size = contextIds.size();
224 ThreadContext *tc;
225 for (int i = 0; i < size; ++i) {
226 tc = system->getThreadContext(contextIds[i]);
227 if (tc->status() == ThreadContext::Halted) {
228 // inactive context, free to use
229 return tc;
230 }
231 }
232 return NULL;
233}
234
235void
236Process::startup()
237{
238 if (contextIds.empty())
239 fatal("Process %s is not associated with any HW contexts!\n", name());
240
241 // first thread context for this process... initialize & enable
242 ThreadContext *tc = system->getThreadContext(contextIds[0]);
243
244 // mark this context as active so it will start ticking.
245 tc->activate(0);
246
247 Port *mem_port;
248 mem_port = system->physmem->getPort("functional");
249 initVirtMem = new TranslatingPort("process init port", this,
250 TranslatingPort::Always);
251 mem_port->setPeer(initVirtMem);
252 initVirtMem->setPeer(mem_port);
253}
254
255// map simulator fd sim_fd to target fd tgt_fd
256void
257Process::dup_fd(int sim_fd, int tgt_fd)
258{
259 if (tgt_fd < 0 || tgt_fd > MAX_FD)
260 panic("Process::dup_fd tried to dup past MAX_FD (%d)", tgt_fd);
261
262 Process::FdMap *fdo = &fd_map[tgt_fd];
263 fdo->fd = sim_fd;
264}
265
266
267// generate new target fd for sim_fd
268int
269Process::alloc_fd(int sim_fd, string filename, int flags, int mode, bool pipe)
270{
271 // in case open() returns an error, don't allocate a new fd
272 if (sim_fd == -1)
273 return -1;
274
275 // find first free target fd
276 for (int free_fd = 0; free_fd <= MAX_FD; ++free_fd) {
277 Process::FdMap *fdo = &fd_map[free_fd];
278 if (fdo->fd == -1) {
279 fdo->fd = sim_fd;
280 fdo->filename = filename;
281 fdo->mode = mode;
282 fdo->fileOffset = 0;
283 fdo->flags = flags;
284 fdo->isPipe = pipe;
285 fdo->readPipeSource = 0;
286 return free_fd;
287 }
288 }
289
290 panic("Process::alloc_fd: out of file descriptors!");
291}
292
293
294// free target fd (e.g., after close)
295void
296Process::free_fd(int tgt_fd)
297{
298 Process::FdMap *fdo = &fd_map[tgt_fd];
299 if (fdo->fd == -1)
300 warn("Process::free_fd: request to free unused fd %d", tgt_fd);
301
302 fdo->fd = -1;
303 fdo->filename = "NULL";
304 fdo->mode = 0;
305 fdo->fileOffset = 0;
306 fdo->flags = 0;
307 fdo->isPipe = false;
308 fdo->readPipeSource = 0;
309}
310
311
312// look up simulator fd for given target fd
313int
314Process::sim_fd(int tgt_fd)
315{
316 if (tgt_fd > MAX_FD)
317 return -1;
318
319 return fd_map[tgt_fd].fd;
320}
321
322Process::FdMap *
323Process::sim_fd_obj(int tgt_fd)
324{
325 if (tgt_fd > MAX_FD)
326 panic("sim_fd_obj called in fd out of range.");
327
328 return &fd_map[tgt_fd];
329}
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
31 */
32
33#include <unistd.h>
34#include <fcntl.h>
35
36#include <cstdio>
37#include <string>
38
39#include "base/intmath.hh"
40#include "base/loader/object_file.hh"
41#include "base/loader/symtab.hh"
42#include "base/statistics.hh"
43#include "config/full_system.hh"
44#include "config/the_isa.hh"
45#include "cpu/thread_context.hh"
46#include "mem/page_table.hh"
47#include "mem/physical.hh"
48#include "mem/translating_port.hh"
49#include "params/Process.hh"
50#include "params/LiveProcess.hh"
51#include "sim/debug.hh"
52#include "sim/process.hh"
53#include "sim/process_impl.hh"
54#include "sim/stats.hh"
55#include "sim/syscall_emul.hh"
56#include "sim/system.hh"
57
58#if THE_ISA == ALPHA_ISA
59#include "arch/alpha/linux/process.hh"
60#include "arch/alpha/tru64/process.hh"
61#elif THE_ISA == SPARC_ISA
62#include "arch/sparc/linux/process.hh"
63#include "arch/sparc/solaris/process.hh"
64#elif THE_ISA == MIPS_ISA
65#include "arch/mips/linux/process.hh"
66#elif THE_ISA == ARM_ISA
67#include "arch/arm/linux/process.hh"
68#elif THE_ISA == X86_ISA
69#include "arch/x86/linux/process.hh"
70#elif THE_ISA == POWER_ISA
71#include "arch/power/linux/process.hh"
72#else
73#error "THE_ISA not set"
74#endif
75
76
77using namespace std;
78using namespace TheISA;
79
80//
81// The purpose of this code is to fake the loader & syscall mechanism
82// when there's no OS: thus there's no resone to use it in FULL_SYSTEM
83// mode when we do have an OS
84//
85#if FULL_SYSTEM
86#error "process.cc not compatible with FULL_SYSTEM"
87#endif
88
89// current number of allocated processes
90int num_processes = 0;
91
92template<class IntType>
93AuxVector<IntType>::AuxVector(IntType type, IntType val)
94{
95 a_type = TheISA::htog(type);
96 a_val = TheISA::htog(val);
97}
98
99template class AuxVector<uint32_t>;
100template class AuxVector<uint64_t>;
101
102Process::Process(ProcessParams * params)
103 : SimObject(params), system(params->system), checkpointRestored(false),
104 max_stack_size(params->max_stack_size)
105{
106 string in = params->input;
107 string out = params->output;
108 string err = params->errout;
109
110 // initialize file descriptors to default: same as simulator
111 int stdin_fd, stdout_fd, stderr_fd;
112
113 if (in == "stdin" || in == "cin")
114 stdin_fd = STDIN_FILENO;
115 else if (in == "None")
116 stdin_fd = -1;
117 else
118 stdin_fd = Process::openInputFile(in);
119
120 if (out == "stdout" || out == "cout")
121 stdout_fd = STDOUT_FILENO;
122 else if (out == "stderr" || out == "cerr")
123 stdout_fd = STDERR_FILENO;
124 else if (out == "None")
125 stdout_fd = -1;
126 else
127 stdout_fd = Process::openOutputFile(out);
128
129 if (err == "stdout" || err == "cout")
130 stderr_fd = STDOUT_FILENO;
131 else if (err == "stderr" || err == "cerr")
132 stderr_fd = STDERR_FILENO;
133 else if (err == "None")
134 stderr_fd = -1;
135 else if (err == out)
136 stderr_fd = stdout_fd;
137 else
138 stderr_fd = Process::openOutputFile(err);
139
140 M5_pid = system->allocatePID();
141 // initialize first 3 fds (stdin, stdout, stderr)
142 Process::FdMap *fdo = &fd_map[STDIN_FILENO];
143 fdo->fd = stdin_fd;
144 fdo->filename = in;
145 fdo->flags = O_RDONLY;
146 fdo->mode = -1;
147 fdo->fileOffset = 0;
148
149 fdo = &fd_map[STDOUT_FILENO];
150 fdo->fd = stdout_fd;
151 fdo->filename = out;
152 fdo->flags = O_WRONLY | O_CREAT | O_TRUNC;
153 fdo->mode = 0774;
154 fdo->fileOffset = 0;
155
156 fdo = &fd_map[STDERR_FILENO];
157 fdo->fd = stderr_fd;
158 fdo->filename = err;
159 fdo->flags = O_WRONLY;
160 fdo->mode = -1;
161 fdo->fileOffset = 0;
162
163
164 // mark remaining fds as free
165 for (int i = 3; i <= MAX_FD; ++i) {
166 Process::FdMap *fdo = &fd_map[i];
167 fdo->fd = -1;
168 }
169
170 mmap_start = mmap_end = 0;
171 nxm_start = nxm_end = 0;
172 pTable = new PageTable(this);
173 // other parameters will be initialized when the program is loaded
174}
175
176
177void
178Process::regStats()
179{
180 using namespace Stats;
181
182 num_syscalls
183 .name(name() + ".PROG:num_syscalls")
184 .desc("Number of system calls")
185 ;
186}
187
188//
189// static helper functions
190//
191int
192Process::openInputFile(const string &filename)
193{
194 int fd = open(filename.c_str(), O_RDONLY);
195
196 if (fd == -1) {
197 perror(NULL);
198 cerr << "unable to open \"" << filename << "\" for reading\n";
199 fatal("can't open input file");
200 }
201
202 return fd;
203}
204
205
206int
207Process::openOutputFile(const string &filename)
208{
209 int fd = open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0664);
210
211 if (fd == -1) {
212 perror(NULL);
213 cerr << "unable to open \"" << filename << "\" for writing\n";
214 fatal("can't open output file");
215 }
216
217 return fd;
218}
219
220ThreadContext *
221Process::findFreeContext()
222{
223 int size = contextIds.size();
224 ThreadContext *tc;
225 for (int i = 0; i < size; ++i) {
226 tc = system->getThreadContext(contextIds[i]);
227 if (tc->status() == ThreadContext::Halted) {
228 // inactive context, free to use
229 return tc;
230 }
231 }
232 return NULL;
233}
234
235void
236Process::startup()
237{
238 if (contextIds.empty())
239 fatal("Process %s is not associated with any HW contexts!\n", name());
240
241 // first thread context for this process... initialize & enable
242 ThreadContext *tc = system->getThreadContext(contextIds[0]);
243
244 // mark this context as active so it will start ticking.
245 tc->activate(0);
246
247 Port *mem_port;
248 mem_port = system->physmem->getPort("functional");
249 initVirtMem = new TranslatingPort("process init port", this,
250 TranslatingPort::Always);
251 mem_port->setPeer(initVirtMem);
252 initVirtMem->setPeer(mem_port);
253}
254
255// map simulator fd sim_fd to target fd tgt_fd
256void
257Process::dup_fd(int sim_fd, int tgt_fd)
258{
259 if (tgt_fd < 0 || tgt_fd > MAX_FD)
260 panic("Process::dup_fd tried to dup past MAX_FD (%d)", tgt_fd);
261
262 Process::FdMap *fdo = &fd_map[tgt_fd];
263 fdo->fd = sim_fd;
264}
265
266
267// generate new target fd for sim_fd
268int
269Process::alloc_fd(int sim_fd, string filename, int flags, int mode, bool pipe)
270{
271 // in case open() returns an error, don't allocate a new fd
272 if (sim_fd == -1)
273 return -1;
274
275 // find first free target fd
276 for (int free_fd = 0; free_fd <= MAX_FD; ++free_fd) {
277 Process::FdMap *fdo = &fd_map[free_fd];
278 if (fdo->fd == -1) {
279 fdo->fd = sim_fd;
280 fdo->filename = filename;
281 fdo->mode = mode;
282 fdo->fileOffset = 0;
283 fdo->flags = flags;
284 fdo->isPipe = pipe;
285 fdo->readPipeSource = 0;
286 return free_fd;
287 }
288 }
289
290 panic("Process::alloc_fd: out of file descriptors!");
291}
292
293
294// free target fd (e.g., after close)
295void
296Process::free_fd(int tgt_fd)
297{
298 Process::FdMap *fdo = &fd_map[tgt_fd];
299 if (fdo->fd == -1)
300 warn("Process::free_fd: request to free unused fd %d", tgt_fd);
301
302 fdo->fd = -1;
303 fdo->filename = "NULL";
304 fdo->mode = 0;
305 fdo->fileOffset = 0;
306 fdo->flags = 0;
307 fdo->isPipe = false;
308 fdo->readPipeSource = 0;
309}
310
311
312// look up simulator fd for given target fd
313int
314Process::sim_fd(int tgt_fd)
315{
316 if (tgt_fd > MAX_FD)
317 return -1;
318
319 return fd_map[tgt_fd].fd;
320}
321
322Process::FdMap *
323Process::sim_fd_obj(int tgt_fd)
324{
325 if (tgt_fd > MAX_FD)
326 panic("sim_fd_obj called in fd out of range.");
327
328 return &fd_map[tgt_fd];
329}
330
330bool
331Process::checkAndAllocNextPage(Addr vaddr)
332{
333 // if this is an initial write we might not have
334 if (vaddr >= stack_min && vaddr < stack_base) {
335 pTable->allocate(roundDown(vaddr, VMPageSize), VMPageSize);
336 return true;
337 }
338
339 // We've accessed the next page of the stack, so extend the stack
340 // to cover it.
341 if (vaddr < stack_min && vaddr >= stack_base - max_stack_size) {
342 while (vaddr < stack_min) {
343 stack_min -= TheISA::PageBytes;
344 if(stack_base - stack_min > max_stack_size)
345 fatal("Maximum stack size exceeded\n");
346 if(stack_base - stack_min > 8*1024*1024)
347 fatal("Over max stack size for one thread\n");
348 pTable->allocate(stack_min, TheISA::PageBytes);
349 inform("Increasing stack size by one page.");
350 };
351 return true;
352 }
353 return false;
354}
355
331bool
332Process::checkAndAllocNextPage(Addr vaddr)
333{
334 // if this is an initial write we might not have
335 if (vaddr >= stack_min && vaddr < stack_base) {
336 pTable->allocate(roundDown(vaddr, VMPageSize), VMPageSize);
337 return true;
338 }
339
340 // We've accessed the next page of the stack, so extend the stack
341 // to cover it.
342 if (vaddr < stack_min && vaddr >= stack_base - max_stack_size) {
343 while (vaddr < stack_min) {
344 stack_min -= TheISA::PageBytes;
345 if(stack_base - stack_min > max_stack_size)
346 fatal("Maximum stack size exceeded\n");
347 if(stack_base - stack_min > 8*1024*1024)
348 fatal("Over max stack size for one thread\n");
349 pTable->allocate(stack_min, TheISA::PageBytes);
350 inform("Increasing stack size by one page.");
351 };
352 return true;
353 }
354 return false;
355}
356
356 // find all offsets for currently open files and save them
357// find all offsets for currently open files and save them
357void
358void
358Process::fix_file_offsets() {
359Process::fix_file_offsets()
360{
359 Process::FdMap *fdo_stdin = &fd_map[STDIN_FILENO];
360 Process::FdMap *fdo_stdout = &fd_map[STDOUT_FILENO];
361 Process::FdMap *fdo_stderr = &fd_map[STDERR_FILENO];
362 string in = fdo_stdin->filename;
363 string out = fdo_stdout->filename;
364 string err = fdo_stderr->filename;
365
366 // initialize file descriptors to default: same as simulator
367 int stdin_fd, stdout_fd, stderr_fd;
368
369 if (in == "stdin" || in == "cin")
370 stdin_fd = STDIN_FILENO;
371 else if (in == "None")
372 stdin_fd = -1;
361 Process::FdMap *fdo_stdin = &fd_map[STDIN_FILENO];
362 Process::FdMap *fdo_stdout = &fd_map[STDOUT_FILENO];
363 Process::FdMap *fdo_stderr = &fd_map[STDERR_FILENO];
364 string in = fdo_stdin->filename;
365 string out = fdo_stdout->filename;
366 string err = fdo_stderr->filename;
367
368 // initialize file descriptors to default: same as simulator
369 int stdin_fd, stdout_fd, stderr_fd;
370
371 if (in == "stdin" || in == "cin")
372 stdin_fd = STDIN_FILENO;
373 else if (in == "None")
374 stdin_fd = -1;
373 else{
374 //OPEN standard in and seek to the right location
375 else {
376 // open standard in and seek to the right location
375 stdin_fd = Process::openInputFile(in);
376 if (lseek(stdin_fd, fdo_stdin->fileOffset, SEEK_SET) < 0)
377 panic("Unable to seek to correct location in file: %s", in);
378 }
379
380 if (out == "stdout" || out == "cout")
381 stdout_fd = STDOUT_FILENO;
382 else if (out == "stderr" || out == "cerr")
383 stdout_fd = STDERR_FILENO;
384 else if (out == "None")
385 stdout_fd = -1;
377 stdin_fd = Process::openInputFile(in);
378 if (lseek(stdin_fd, fdo_stdin->fileOffset, SEEK_SET) < 0)
379 panic("Unable to seek to correct location in file: %s", in);
380 }
381
382 if (out == "stdout" || out == "cout")
383 stdout_fd = STDOUT_FILENO;
384 else if (out == "stderr" || out == "cerr")
385 stdout_fd = STDERR_FILENO;
386 else if (out == "None")
387 stdout_fd = -1;
386 else{
388 else {
387 stdout_fd = Process::openOutputFile(out);
388 if (lseek(stdout_fd, fdo_stdout->fileOffset, SEEK_SET) < 0)
389 panic("Unable to seek to correct location in file: %s", out);
390 }
391
392 if (err == "stdout" || err == "cout")
393 stderr_fd = STDOUT_FILENO;
394 else if (err == "stderr" || err == "cerr")
395 stderr_fd = STDERR_FILENO;
396 else if (err == "None")
397 stderr_fd = -1;
398 else if (err == out)
399 stderr_fd = stdout_fd;
400 else {
401 stderr_fd = Process::openOutputFile(err);
402 if (lseek(stderr_fd, fdo_stderr->fileOffset, SEEK_SET) < 0)
403 panic("Unable to seek to correct location in file: %s", err);
404 }
405
406 fdo_stdin->fd = stdin_fd;
407 fdo_stdout->fd = stdout_fd;
408 fdo_stderr->fd = stderr_fd;
409
410
411 for (int free_fd = 3; free_fd <= MAX_FD; ++free_fd) {
412 Process::FdMap *fdo = &fd_map[free_fd];
413 if (fdo->fd != -1) {
414 if (fdo->isPipe){
415 if (fdo->filename == "PIPE-WRITE")
416 continue;
417 else {
418 assert (fdo->filename == "PIPE-READ");
419 //create a new pipe
420 int fds[2];
421 int pipe_retval = pipe(fds);
422
423 if (pipe_retval < 0) {
424 // error
425 panic("Unable to create new pipe.");
426 }
427 fdo->fd = fds[0]; //set read pipe
428 Process::FdMap *fdo_write = &fd_map[fdo->readPipeSource];
429 if (fdo_write->filename != "PIPE-WRITE")
430 panic ("Couldn't find write end of the pipe");
431
432 fdo_write->fd = fds[1];//set write pipe
433 }
434 } else {
435 //Open file
436 int fd = open(fdo->filename.c_str(), fdo->flags, fdo->mode);
437
438 if (fd == -1)
439 panic("Unable to open file: %s", fdo->filename);
440 fdo->fd = fd;
441
442 //Seek to correct location before checkpoint
443 if (lseek(fd,fdo->fileOffset, SEEK_SET) < 0)
389 stdout_fd = Process::openOutputFile(out);
390 if (lseek(stdout_fd, fdo_stdout->fileOffset, SEEK_SET) < 0)
391 panic("Unable to seek to correct location in file: %s", out);
392 }
393
394 if (err == "stdout" || err == "cout")
395 stderr_fd = STDOUT_FILENO;
396 else if (err == "stderr" || err == "cerr")
397 stderr_fd = STDERR_FILENO;
398 else if (err == "None")
399 stderr_fd = -1;
400 else if (err == out)
401 stderr_fd = stdout_fd;
402 else {
403 stderr_fd = Process::openOutputFile(err);
404 if (lseek(stderr_fd, fdo_stderr->fileOffset, SEEK_SET) < 0)
405 panic("Unable to seek to correct location in file: %s", err);
406 }
407
408 fdo_stdin->fd = stdin_fd;
409 fdo_stdout->fd = stdout_fd;
410 fdo_stderr->fd = stderr_fd;
411
412
413 for (int free_fd = 3; free_fd <= MAX_FD; ++free_fd) {
414 Process::FdMap *fdo = &fd_map[free_fd];
415 if (fdo->fd != -1) {
416 if (fdo->isPipe){
417 if (fdo->filename == "PIPE-WRITE")
418 continue;
419 else {
420 assert (fdo->filename == "PIPE-READ");
421 //create a new pipe
422 int fds[2];
423 int pipe_retval = pipe(fds);
424
425 if (pipe_retval < 0) {
426 // error
427 panic("Unable to create new pipe.");
428 }
429 fdo->fd = fds[0]; //set read pipe
430 Process::FdMap *fdo_write = &fd_map[fdo->readPipeSource];
431 if (fdo_write->filename != "PIPE-WRITE")
432 panic ("Couldn't find write end of the pipe");
433
434 fdo_write->fd = fds[1];//set write pipe
435 }
436 } else {
437 //Open file
438 int fd = open(fdo->filename.c_str(), fdo->flags, fdo->mode);
439
440 if (fd == -1)
441 panic("Unable to open file: %s", fdo->filename);
442 fdo->fd = fd;
443
444 //Seek to correct location before checkpoint
445 if (lseek(fd,fdo->fileOffset, SEEK_SET) < 0)
444 panic("Unable to seek to correct location in file: %s", fdo->filename);
446 panic("Unable to seek to correct location in file: %s",
447 fdo->filename);
445 }
446 }
447 }
448}
448 }
449 }
450 }
451}
452
449void
453void
450Process::find_file_offsets(){
454Process::find_file_offsets()
455{
451 for (int free_fd = 0; free_fd <= MAX_FD; ++free_fd) {
452 Process::FdMap *fdo = &fd_map[free_fd];
453 if (fdo->fd != -1) {
454 fdo->fileOffset = lseek(fdo->fd, 0, SEEK_CUR);
456 for (int free_fd = 0; free_fd <= MAX_FD; ++free_fd) {
457 Process::FdMap *fdo = &fd_map[free_fd];
458 if (fdo->fd != -1) {
459 fdo->fileOffset = lseek(fdo->fd, 0, SEEK_CUR);
455 } else {
460 } else {
456 fdo->filename = "NULL";
457 fdo->fileOffset = 0;
458 }
459 }
460}
461
462void
461 fdo->filename = "NULL";
462 fdo->fileOffset = 0;
463 }
464 }
465}
466
467void
463Process::setReadPipeSource(int read_pipe_fd, int source_fd){
468Process::setReadPipeSource(int read_pipe_fd, int source_fd)
469{
464 Process::FdMap *fdo = &fd_map[read_pipe_fd];
465 fdo->readPipeSource = source_fd;
466}
467
468void
469Process::FdMap::serialize(std::ostream &os)
470{
471 SERIALIZE_SCALAR(fd);
472 SERIALIZE_SCALAR(isPipe);
473 SERIALIZE_SCALAR(filename);
474 SERIALIZE_SCALAR(flags);
475 SERIALIZE_SCALAR(readPipeSource);
476 SERIALIZE_SCALAR(fileOffset);
477}
478
479void
480Process::FdMap::unserialize(Checkpoint *cp, const std::string &section)
481{
482 UNSERIALIZE_SCALAR(fd);
483 UNSERIALIZE_SCALAR(isPipe);
484 UNSERIALIZE_SCALAR(filename);
485 UNSERIALIZE_SCALAR(flags);
486 UNSERIALIZE_SCALAR(readPipeSource);
487 UNSERIALIZE_SCALAR(fileOffset);
488}
489
490void
491Process::serialize(std::ostream &os)
492{
493 SERIALIZE_SCALAR(initialContextLoaded);
494 SERIALIZE_SCALAR(brk_point);
495 SERIALIZE_SCALAR(stack_base);
496 SERIALIZE_SCALAR(stack_size);
497 SERIALIZE_SCALAR(stack_min);
498 SERIALIZE_SCALAR(next_thread_stack_base);
499 SERIALIZE_SCALAR(mmap_start);
500 SERIALIZE_SCALAR(mmap_end);
501 SERIALIZE_SCALAR(nxm_start);
502 SERIALIZE_SCALAR(nxm_end);
503 find_file_offsets();
504 pTable->serialize(os);
505 for (int x = 0; x <= MAX_FD; x++) {
506 nameOut(os, csprintf("%s.FdMap%d", name(), x));
507 fd_map[x].serialize(os);
508 }
509 SERIALIZE_SCALAR(M5_pid);
510
511}
512
513void
514Process::unserialize(Checkpoint *cp, const std::string &section)
515{
516 UNSERIALIZE_SCALAR(initialContextLoaded);
517 UNSERIALIZE_SCALAR(brk_point);
518 UNSERIALIZE_SCALAR(stack_base);
519 UNSERIALIZE_SCALAR(stack_size);
520 UNSERIALIZE_SCALAR(stack_min);
521 UNSERIALIZE_SCALAR(next_thread_stack_base);
522 UNSERIALIZE_SCALAR(mmap_start);
523 UNSERIALIZE_SCALAR(mmap_end);
524 UNSERIALIZE_SCALAR(nxm_start);
525 UNSERIALIZE_SCALAR(nxm_end);
526 pTable->unserialize(cp, section);
527 for (int x = 0; x <= MAX_FD; x++) {
528 fd_map[x].unserialize(cp, csprintf("%s.FdMap%d", section, x));
470 Process::FdMap *fdo = &fd_map[read_pipe_fd];
471 fdo->readPipeSource = source_fd;
472}
473
474void
475Process::FdMap::serialize(std::ostream &os)
476{
477 SERIALIZE_SCALAR(fd);
478 SERIALIZE_SCALAR(isPipe);
479 SERIALIZE_SCALAR(filename);
480 SERIALIZE_SCALAR(flags);
481 SERIALIZE_SCALAR(readPipeSource);
482 SERIALIZE_SCALAR(fileOffset);
483}
484
485void
486Process::FdMap::unserialize(Checkpoint *cp, const std::string &section)
487{
488 UNSERIALIZE_SCALAR(fd);
489 UNSERIALIZE_SCALAR(isPipe);
490 UNSERIALIZE_SCALAR(filename);
491 UNSERIALIZE_SCALAR(flags);
492 UNSERIALIZE_SCALAR(readPipeSource);
493 UNSERIALIZE_SCALAR(fileOffset);
494}
495
496void
497Process::serialize(std::ostream &os)
498{
499 SERIALIZE_SCALAR(initialContextLoaded);
500 SERIALIZE_SCALAR(brk_point);
501 SERIALIZE_SCALAR(stack_base);
502 SERIALIZE_SCALAR(stack_size);
503 SERIALIZE_SCALAR(stack_min);
504 SERIALIZE_SCALAR(next_thread_stack_base);
505 SERIALIZE_SCALAR(mmap_start);
506 SERIALIZE_SCALAR(mmap_end);
507 SERIALIZE_SCALAR(nxm_start);
508 SERIALIZE_SCALAR(nxm_end);
509 find_file_offsets();
510 pTable->serialize(os);
511 for (int x = 0; x <= MAX_FD; x++) {
512 nameOut(os, csprintf("%s.FdMap%d", name(), x));
513 fd_map[x].serialize(os);
514 }
515 SERIALIZE_SCALAR(M5_pid);
516
517}
518
519void
520Process::unserialize(Checkpoint *cp, const std::string &section)
521{
522 UNSERIALIZE_SCALAR(initialContextLoaded);
523 UNSERIALIZE_SCALAR(brk_point);
524 UNSERIALIZE_SCALAR(stack_base);
525 UNSERIALIZE_SCALAR(stack_size);
526 UNSERIALIZE_SCALAR(stack_min);
527 UNSERIALIZE_SCALAR(next_thread_stack_base);
528 UNSERIALIZE_SCALAR(mmap_start);
529 UNSERIALIZE_SCALAR(mmap_end);
530 UNSERIALIZE_SCALAR(nxm_start);
531 UNSERIALIZE_SCALAR(nxm_end);
532 pTable->unserialize(cp, section);
533 for (int x = 0; x <= MAX_FD; x++) {
534 fd_map[x].unserialize(cp, csprintf("%s.FdMap%d", section, x));
529 }
535 }
530 fix_file_offsets();
531 UNSERIALIZE_OPT_SCALAR(M5_pid);
532 // The above returns a bool so that you could do something if you don't
533 // find the param in the checkpoint if you wanted to, like set a default
534 // but in this case we'll just stick with the instantianted value if not
535 // found.
536
537 checkpointRestored = true;
538
539}
540
541
542////////////////////////////////////////////////////////////////////////
543//
544// LiveProcess member definitions
545//
546////////////////////////////////////////////////////////////////////////
547
548
549LiveProcess::LiveProcess(LiveProcessParams * params, ObjectFile *_objFile)
550 : Process(params), objFile(_objFile),
551 argv(params->cmd), envp(params->env), cwd(params->cwd)
552{
553 __uid = params->uid;
554 __euid = params->euid;
555 __gid = params->gid;
556 __egid = params->egid;
557 __pid = params->pid;
558 __ppid = params->ppid;
559
560 prog_fname = params->cmd[0];
561
562 // load up symbols, if any... these may be used for debugging or
563 // profiling.
564 if (!debugSymbolTable) {
565 debugSymbolTable = new SymbolTable();
566 if (!objFile->loadGlobalSymbols(debugSymbolTable) ||
567 !objFile->loadLocalSymbols(debugSymbolTable)) {
568 // didn't load any symbols
569 delete debugSymbolTable;
570 debugSymbolTable = NULL;
571 }
572 }
573}
574
575void
576LiveProcess::argsInit(int intSize, int pageSize)
577{
578 Process::startup();
579
580 // load object file into target memory
581 objFile->loadSections(initVirtMem);
582
583 // Calculate how much space we need for arg & env arrays.
584 int argv_array_size = intSize * (argv.size() + 1);
585 int envp_array_size = intSize * (envp.size() + 1);
586 int arg_data_size = 0;
587 for (vector<string>::size_type i = 0; i < argv.size(); ++i) {
588 arg_data_size += argv[i].size() + 1;
589 }
590 int env_data_size = 0;
591 for (vector<string>::size_type i = 0; i < envp.size(); ++i) {
592 env_data_size += envp[i].size() + 1;
593 }
594
595 int space_needed =
596 argv_array_size + envp_array_size + arg_data_size + env_data_size;
597 if (space_needed < 32*1024)
598 space_needed = 32*1024;
599
600 // set bottom of stack
601 stack_min = stack_base - space_needed;
602 // align it
603 stack_min = roundDown(stack_min, pageSize);
604 stack_size = stack_base - stack_min;
605 // map memory
606 pTable->allocate(stack_min, roundUp(stack_size, pageSize));
607
608 // map out initial stack contents
609 Addr argv_array_base = stack_min + intSize; // room for argc
610 Addr envp_array_base = argv_array_base + argv_array_size;
611 Addr arg_data_base = envp_array_base + envp_array_size;
612 Addr env_data_base = arg_data_base + arg_data_size;
613
614 // write contents to stack
615 uint64_t argc = argv.size();
616 if (intSize == 8)
617 argc = htog((uint64_t)argc);
618 else if (intSize == 4)
619 argc = htog((uint32_t)argc);
620 else
621 panic("Unknown int size");
622
623 initVirtMem->writeBlob(stack_min, (uint8_t*)&argc, intSize);
624
625 copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem);
626 copyStringArray(envp, envp_array_base, env_data_base, initVirtMem);
627
628 ThreadContext *tc = system->getThreadContext(contextIds[0]);
629
630 setSyscallArg(tc, 0, argc);
631 setSyscallArg(tc, 1, argv_array_base);
632 tc->setIntReg(StackPointerReg, stack_min);
633
634 Addr prog_entry = objFile->entryPoint();
635 tc->setPC(prog_entry);
636 tc->setNextPC(prog_entry + sizeof(MachInst));
637
638#if THE_ISA != ALPHA_ISA && THE_ISA != POWER_ISA //e.g. MIPS or Sparc
639 tc->setNextNPC(prog_entry + (2 * sizeof(MachInst)));
640#endif
641
642 num_processes++;
643}
644
645void
646LiveProcess::syscall(int64_t callnum, ThreadContext *tc)
647{
648 num_syscalls++;
649
650 SyscallDesc *desc = getDesc(callnum);
651 if (desc == NULL)
652 fatal("Syscall %d out of range", callnum);
653
654 desc->doSyscall(callnum, this, tc);
655}
656
657IntReg
658LiveProcess::getSyscallArg(ThreadContext *tc, int &i, int width)
659{
660 return getSyscallArg(tc, i);
661}
662
663LiveProcess *
664LiveProcess::create(LiveProcessParams * params)
665{
666 LiveProcess *process = NULL;
667
668 string executable =
669 params->executable == "" ? params->cmd[0] : params->executable;
670 ObjectFile *objFile = createObjectFile(executable);
671 if (objFile == NULL) {
672 fatal("Can't load object file %s", executable);
673 }
674
675 if (objFile->isDynamic())
676 fatal("Object file is a dynamic executable however only static "
677 "executables are supported!\n Please recompile your "
678 "executable as a static binary and try again.\n");
679
680#if THE_ISA == ALPHA_ISA
681 if (objFile->getArch() != ObjectFile::Alpha)
682 fatal("Object file architecture does not match compiled ISA (Alpha).");
683
684 switch (objFile->getOpSys()) {
685 case ObjectFile::Tru64:
686 process = new AlphaTru64Process(params, objFile);
687 break;
688
689 case ObjectFile::UnknownOpSys:
690 warn("Unknown operating system; assuming Linux.");
691 // fall through
692 case ObjectFile::Linux:
693 process = new AlphaLinuxProcess(params, objFile);
694 break;
695
696 default:
697 fatal("Unknown/unsupported operating system.");
698 }
699#elif THE_ISA == SPARC_ISA
700 if (objFile->getArch() != ObjectFile::SPARC64 &&
701 objFile->getArch() != ObjectFile::SPARC32)
702 fatal("Object file architecture does not match compiled ISA (SPARC).");
703 switch (objFile->getOpSys()) {
704 case ObjectFile::UnknownOpSys:
705 warn("Unknown operating system; assuming Linux.");
706 // fall through
707 case ObjectFile::Linux:
708 if (objFile->getArch() == ObjectFile::SPARC64) {
709 process = new Sparc64LinuxProcess(params, objFile);
710 } else {
711 process = new Sparc32LinuxProcess(params, objFile);
712 }
713 break;
714
715
716 case ObjectFile::Solaris:
717 process = new SparcSolarisProcess(params, objFile);
718 break;
719
720 default:
721 fatal("Unknown/unsupported operating system.");
722 }
723#elif THE_ISA == X86_ISA
724 if (objFile->getArch() != ObjectFile::X86_64 &&
725 objFile->getArch() != ObjectFile::I386)
726 fatal("Object file architecture does not match compiled ISA (x86).");
727 switch (objFile->getOpSys()) {
728 case ObjectFile::UnknownOpSys:
729 warn("Unknown operating system; assuming Linux.");
730 // fall through
731 case ObjectFile::Linux:
732 if (objFile->getArch() == ObjectFile::X86_64) {
733 process = new X86_64LinuxProcess(params, objFile);
734 } else {
735 process = new I386LinuxProcess(params, objFile);
736 }
737 break;
738
739 default:
740 fatal("Unknown/unsupported operating system.");
741 }
742#elif THE_ISA == MIPS_ISA
743 if (objFile->getArch() != ObjectFile::Mips)
744 fatal("Object file architecture does not match compiled ISA (MIPS).");
745 switch (objFile->getOpSys()) {
746 case ObjectFile::UnknownOpSys:
747 warn("Unknown operating system; assuming Linux.");
748 // fall through
749 case ObjectFile::Linux:
750 process = new MipsLinuxProcess(params, objFile);
751 break;
752
753 default:
754 fatal("Unknown/unsupported operating system.");
755 }
756#elif THE_ISA == ARM_ISA
757 if (objFile->getArch() != ObjectFile::Arm &&
758 objFile->getArch() != ObjectFile::Thumb)
759 fatal("Object file architecture does not match compiled ISA (ARM).");
760 switch (objFile->getOpSys()) {
761 case ObjectFile::UnknownOpSys:
762 warn("Unknown operating system; assuming Linux.");
763 // fall through
764 case ObjectFile::Linux:
765 process = new ArmLinuxProcess(params, objFile, objFile->getArch());
766 break;
767 case ObjectFile::LinuxArmOABI:
768 fatal("M5 does not support ARM OABI binaries. Please recompile with an"
769 " EABI compiler.");
770 default:
771 fatal("Unknown/unsupported operating system.");
772 }
773#elif THE_ISA == POWER_ISA
774 if (objFile->getArch() != ObjectFile::Power)
775 fatal("Object file architecture does not match compiled ISA (Power).");
776 switch (objFile->getOpSys()) {
777 case ObjectFile::UnknownOpSys:
778 warn("Unknown operating system; assuming Linux.");
779 // fall through
780 case ObjectFile::Linux:
781 process = new PowerLinuxProcess(params, objFile);
782 break;
783
784 default:
785 fatal("Unknown/unsupported operating system.");
786 }
787#else
788#error "THE_ISA not set"
789#endif
790
791
792 if (process == NULL)
793 fatal("Unknown error creating process object.");
794 return process;
795}
796
797LiveProcess *
798LiveProcessParams::create()
799{
800 return LiveProcess::create(this);
801}
536 fix_file_offsets();
537 UNSERIALIZE_OPT_SCALAR(M5_pid);
538 // The above returns a bool so that you could do something if you don't
539 // find the param in the checkpoint if you wanted to, like set a default
540 // but in this case we'll just stick with the instantianted value if not
541 // found.
542
543 checkpointRestored = true;
544
545}
546
547
548////////////////////////////////////////////////////////////////////////
549//
550// LiveProcess member definitions
551//
552////////////////////////////////////////////////////////////////////////
553
554
555LiveProcess::LiveProcess(LiveProcessParams * params, ObjectFile *_objFile)
556 : Process(params), objFile(_objFile),
557 argv(params->cmd), envp(params->env), cwd(params->cwd)
558{
559 __uid = params->uid;
560 __euid = params->euid;
561 __gid = params->gid;
562 __egid = params->egid;
563 __pid = params->pid;
564 __ppid = params->ppid;
565
566 prog_fname = params->cmd[0];
567
568 // load up symbols, if any... these may be used for debugging or
569 // profiling.
570 if (!debugSymbolTable) {
571 debugSymbolTable = new SymbolTable();
572 if (!objFile->loadGlobalSymbols(debugSymbolTable) ||
573 !objFile->loadLocalSymbols(debugSymbolTable)) {
574 // didn't load any symbols
575 delete debugSymbolTable;
576 debugSymbolTable = NULL;
577 }
578 }
579}
580
581void
582LiveProcess::argsInit(int intSize, int pageSize)
583{
584 Process::startup();
585
586 // load object file into target memory
587 objFile->loadSections(initVirtMem);
588
589 // Calculate how much space we need for arg & env arrays.
590 int argv_array_size = intSize * (argv.size() + 1);
591 int envp_array_size = intSize * (envp.size() + 1);
592 int arg_data_size = 0;
593 for (vector<string>::size_type i = 0; i < argv.size(); ++i) {
594 arg_data_size += argv[i].size() + 1;
595 }
596 int env_data_size = 0;
597 for (vector<string>::size_type i = 0; i < envp.size(); ++i) {
598 env_data_size += envp[i].size() + 1;
599 }
600
601 int space_needed =
602 argv_array_size + envp_array_size + arg_data_size + env_data_size;
603 if (space_needed < 32*1024)
604 space_needed = 32*1024;
605
606 // set bottom of stack
607 stack_min = stack_base - space_needed;
608 // align it
609 stack_min = roundDown(stack_min, pageSize);
610 stack_size = stack_base - stack_min;
611 // map memory
612 pTable->allocate(stack_min, roundUp(stack_size, pageSize));
613
614 // map out initial stack contents
615 Addr argv_array_base = stack_min + intSize; // room for argc
616 Addr envp_array_base = argv_array_base + argv_array_size;
617 Addr arg_data_base = envp_array_base + envp_array_size;
618 Addr env_data_base = arg_data_base + arg_data_size;
619
620 // write contents to stack
621 uint64_t argc = argv.size();
622 if (intSize == 8)
623 argc = htog((uint64_t)argc);
624 else if (intSize == 4)
625 argc = htog((uint32_t)argc);
626 else
627 panic("Unknown int size");
628
629 initVirtMem->writeBlob(stack_min, (uint8_t*)&argc, intSize);
630
631 copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem);
632 copyStringArray(envp, envp_array_base, env_data_base, initVirtMem);
633
634 ThreadContext *tc = system->getThreadContext(contextIds[0]);
635
636 setSyscallArg(tc, 0, argc);
637 setSyscallArg(tc, 1, argv_array_base);
638 tc->setIntReg(StackPointerReg, stack_min);
639
640 Addr prog_entry = objFile->entryPoint();
641 tc->setPC(prog_entry);
642 tc->setNextPC(prog_entry + sizeof(MachInst));
643
644#if THE_ISA != ALPHA_ISA && THE_ISA != POWER_ISA //e.g. MIPS or Sparc
645 tc->setNextNPC(prog_entry + (2 * sizeof(MachInst)));
646#endif
647
648 num_processes++;
649}
650
651void
652LiveProcess::syscall(int64_t callnum, ThreadContext *tc)
653{
654 num_syscalls++;
655
656 SyscallDesc *desc = getDesc(callnum);
657 if (desc == NULL)
658 fatal("Syscall %d out of range", callnum);
659
660 desc->doSyscall(callnum, this, tc);
661}
662
663IntReg
664LiveProcess::getSyscallArg(ThreadContext *tc, int &i, int width)
665{
666 return getSyscallArg(tc, i);
667}
668
669LiveProcess *
670LiveProcess::create(LiveProcessParams * params)
671{
672 LiveProcess *process = NULL;
673
674 string executable =
675 params->executable == "" ? params->cmd[0] : params->executable;
676 ObjectFile *objFile = createObjectFile(executable);
677 if (objFile == NULL) {
678 fatal("Can't load object file %s", executable);
679 }
680
681 if (objFile->isDynamic())
682 fatal("Object file is a dynamic executable however only static "
683 "executables are supported!\n Please recompile your "
684 "executable as a static binary and try again.\n");
685
686#if THE_ISA == ALPHA_ISA
687 if (objFile->getArch() != ObjectFile::Alpha)
688 fatal("Object file architecture does not match compiled ISA (Alpha).");
689
690 switch (objFile->getOpSys()) {
691 case ObjectFile::Tru64:
692 process = new AlphaTru64Process(params, objFile);
693 break;
694
695 case ObjectFile::UnknownOpSys:
696 warn("Unknown operating system; assuming Linux.");
697 // fall through
698 case ObjectFile::Linux:
699 process = new AlphaLinuxProcess(params, objFile);
700 break;
701
702 default:
703 fatal("Unknown/unsupported operating system.");
704 }
705#elif THE_ISA == SPARC_ISA
706 if (objFile->getArch() != ObjectFile::SPARC64 &&
707 objFile->getArch() != ObjectFile::SPARC32)
708 fatal("Object file architecture does not match compiled ISA (SPARC).");
709 switch (objFile->getOpSys()) {
710 case ObjectFile::UnknownOpSys:
711 warn("Unknown operating system; assuming Linux.");
712 // fall through
713 case ObjectFile::Linux:
714 if (objFile->getArch() == ObjectFile::SPARC64) {
715 process = new Sparc64LinuxProcess(params, objFile);
716 } else {
717 process = new Sparc32LinuxProcess(params, objFile);
718 }
719 break;
720
721
722 case ObjectFile::Solaris:
723 process = new SparcSolarisProcess(params, objFile);
724 break;
725
726 default:
727 fatal("Unknown/unsupported operating system.");
728 }
729#elif THE_ISA == X86_ISA
730 if (objFile->getArch() != ObjectFile::X86_64 &&
731 objFile->getArch() != ObjectFile::I386)
732 fatal("Object file architecture does not match compiled ISA (x86).");
733 switch (objFile->getOpSys()) {
734 case ObjectFile::UnknownOpSys:
735 warn("Unknown operating system; assuming Linux.");
736 // fall through
737 case ObjectFile::Linux:
738 if (objFile->getArch() == ObjectFile::X86_64) {
739 process = new X86_64LinuxProcess(params, objFile);
740 } else {
741 process = new I386LinuxProcess(params, objFile);
742 }
743 break;
744
745 default:
746 fatal("Unknown/unsupported operating system.");
747 }
748#elif THE_ISA == MIPS_ISA
749 if (objFile->getArch() != ObjectFile::Mips)
750 fatal("Object file architecture does not match compiled ISA (MIPS).");
751 switch (objFile->getOpSys()) {
752 case ObjectFile::UnknownOpSys:
753 warn("Unknown operating system; assuming Linux.");
754 // fall through
755 case ObjectFile::Linux:
756 process = new MipsLinuxProcess(params, objFile);
757 break;
758
759 default:
760 fatal("Unknown/unsupported operating system.");
761 }
762#elif THE_ISA == ARM_ISA
763 if (objFile->getArch() != ObjectFile::Arm &&
764 objFile->getArch() != ObjectFile::Thumb)
765 fatal("Object file architecture does not match compiled ISA (ARM).");
766 switch (objFile->getOpSys()) {
767 case ObjectFile::UnknownOpSys:
768 warn("Unknown operating system; assuming Linux.");
769 // fall through
770 case ObjectFile::Linux:
771 process = new ArmLinuxProcess(params, objFile, objFile->getArch());
772 break;
773 case ObjectFile::LinuxArmOABI:
774 fatal("M5 does not support ARM OABI binaries. Please recompile with an"
775 " EABI compiler.");
776 default:
777 fatal("Unknown/unsupported operating system.");
778 }
779#elif THE_ISA == POWER_ISA
780 if (objFile->getArch() != ObjectFile::Power)
781 fatal("Object file architecture does not match compiled ISA (Power).");
782 switch (objFile->getOpSys()) {
783 case ObjectFile::UnknownOpSys:
784 warn("Unknown operating system; assuming Linux.");
785 // fall through
786 case ObjectFile::Linux:
787 process = new PowerLinuxProcess(params, objFile);
788 break;
789
790 default:
791 fatal("Unknown/unsupported operating system.");
792 }
793#else
794#error "THE_ISA not set"
795#endif
796
797
798 if (process == NULL)
799 fatal("Unknown error creating process object.");
800 return process;
801}
802
803LiveProcess *
804LiveProcessParams::create()
805{
806 return LiveProcess::create(this);
807}