process.cc (5184:8782de2949e5) process.cc (5282:2dba627b6646)
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;

--- 18 unchanged lines hidden (view full) ---

27 *
28 * Authors: Nathan Binkert
29 * Steve Reinhardt
30 * Ali Saidi
31 */
32
33#include <unistd.h>
34#include <fcntl.h>
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;

--- 18 unchanged lines hidden (view full) ---

27 *
28 * Authors: Nathan Binkert
29 * Steve Reinhardt
30 * Ali Saidi
31 */
32
33#include <unistd.h>
34#include <fcntl.h>
35
36#include <string>
37
38#include "arch/remote_gdb.hh"
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"

--- 65 unchanged lines hidden (view full) ---

109 stdout_fd = -1;
110 else
111 stdout_fd = Process::openOutputFile(out);
112
113 stderr_fd = (stdout_fd != STDOUT_FILENO) ? stdout_fd : STDERR_FILENO;
114
115 M5_pid = system->allocatePID();
116 // initialize first 3 fds (stdin, stdout, stderr)
35#include <string>
36
37#include "arch/remote_gdb.hh"
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"

--- 65 unchanged lines hidden (view full) ---

108 stdout_fd = -1;
109 else
110 stdout_fd = Process::openOutputFile(out);
111
112 stderr_fd = (stdout_fd != STDOUT_FILENO) ? stdout_fd : STDERR_FILENO;
113
114 M5_pid = system->allocatePID();
115 // initialize first 3 fds (stdin, stdout, stderr)
117 fd_map[STDIN_FILENO] = stdin_fd;
118 fd_map[STDOUT_FILENO] = stdout_fd;
119 fd_map[STDERR_FILENO] = stderr_fd;
116 Process::FdMap *fdo = &fd_map[STDIN_FILENO];
117 fdo->fd = stdin_fd;
118 fdo->filename = in;
119 fdo->flags = O_RDONLY;
120 fdo->mode = -1;
121 fdo->fileOffset = 0;
120
122
123 fdo = &fd_map[STDOUT_FILENO];
124 fdo->fd = stdout_fd;
125 fdo->filename = out;
126 fdo->flags = O_WRONLY | O_CREAT | O_TRUNC;
127 fdo->mode = 0774;
128 fdo->fileOffset = 0;
129
130 fdo = &fd_map[STDERR_FILENO];
131 fdo->fd = stderr_fd;
132 fdo->filename = "STDERR";
133 fdo->flags = O_WRONLY;
134 fdo->mode = -1;
135 fdo->fileOffset = 0;
136
137
121 // mark remaining fds as free
122 for (int i = 3; i <= MAX_FD; ++i) {
138 // mark remaining fds as free
139 for (int i = 3; i <= MAX_FD; ++i) {
123 fd_map[i] = -1;
140 Process::FdMap *fdo = &fd_map[i];
141 fdo->fd = -1;
124 }
125
126 mmap_start = mmap_end = 0;
127 nxm_start = nxm_end = 0;
128 pTable = new PageTable(this);
129 // other parameters will be initialized when the program is loaded
130}
131

--- 93 unchanged lines hidden (view full) ---

225
226// map simulator fd sim_fd to target fd tgt_fd
227void
228Process::dup_fd(int sim_fd, int tgt_fd)
229{
230 if (tgt_fd < 0 || tgt_fd > MAX_FD)
231 panic("Process::dup_fd tried to dup past MAX_FD (%d)", tgt_fd);
232
142 }
143
144 mmap_start = mmap_end = 0;
145 nxm_start = nxm_end = 0;
146 pTable = new PageTable(this);
147 // other parameters will be initialized when the program is loaded
148}
149

--- 93 unchanged lines hidden (view full) ---

243
244// map simulator fd sim_fd to target fd tgt_fd
245void
246Process::dup_fd(int sim_fd, int tgt_fd)
247{
248 if (tgt_fd < 0 || tgt_fd > MAX_FD)
249 panic("Process::dup_fd tried to dup past MAX_FD (%d)", tgt_fd);
250
233 fd_map[tgt_fd] = sim_fd;
251 Process::FdMap *fdo = &fd_map[tgt_fd];
252 fdo->fd = sim_fd;
234}
235
236
237// generate new target fd for sim_fd
238int
253}
254
255
256// generate new target fd for sim_fd
257int
239Process::alloc_fd(int sim_fd)
258Process::alloc_fd(int sim_fd, string filename, int flags, int mode, bool pipe)
240{
241 // in case open() returns an error, don't allocate a new fd
242 if (sim_fd == -1)
243 return -1;
244
245 // find first free target fd
259{
260 // in case open() returns an error, don't allocate a new fd
261 if (sim_fd == -1)
262 return -1;
263
264 // find first free target fd
246 for (int free_fd = 0; free_fd < MAX_FD; ++free_fd) {
247 if (fd_map[free_fd] == -1) {
248 fd_map[free_fd] = sim_fd;
265 for (int free_fd = 0; free_fd <= MAX_FD; ++free_fd) {
266 Process::FdMap *fdo = &fd_map[free_fd];
267 if (fdo->fd == -1) {
268 fdo->fd = sim_fd;
269 fdo->filename = filename;
270 fdo->mode = mode;
271 fdo->fileOffset = 0;
272 fdo->flags = flags;
273 fdo->isPipe = pipe;
274 fdo->readPipeSource = 0;
249 return free_fd;
250 }
251 }
252
253 panic("Process::alloc_fd: out of file descriptors!");
254}
255
256
257// free target fd (e.g., after close)
258void
259Process::free_fd(int tgt_fd)
260{
275 return free_fd;
276 }
277 }
278
279 panic("Process::alloc_fd: out of file descriptors!");
280}
281
282
283// free target fd (e.g., after close)
284void
285Process::free_fd(int tgt_fd)
286{
261 if (fd_map[tgt_fd] == -1)
287 Process::FdMap *fdo = &fd_map[tgt_fd];
288 if (fdo->fd == -1)
262 warn("Process::free_fd: request to free unused fd %d", tgt_fd);
263
289 warn("Process::free_fd: request to free unused fd %d", tgt_fd);
290
264 fd_map[tgt_fd] = -1;
291 fdo->fd = -1;
292 fdo->filename = "NULL";
293 fdo->mode = 0;
294 fdo->fileOffset = 0;
295 fdo->flags = 0;
296 fdo->isPipe = false;
297 fdo->readPipeSource = 0;
265}
266
267
268// look up simulator fd for given target fd
269int
270Process::sim_fd(int tgt_fd)
271{
272 if (tgt_fd > MAX_FD)
273 return -1;
274
298}
299
300
301// look up simulator fd for given target fd
302int
303Process::sim_fd(int tgt_fd)
304{
305 if (tgt_fd > MAX_FD)
306 return -1;
307
275 return fd_map[tgt_fd];
308 return fd_map[tgt_fd].fd;
276}
277
309}
310
311Process::FdMap *
312Process::sim_fd_obj(int tgt_fd)
313{
314 if (tgt_fd > MAX_FD)
315 panic("sim_fd_obj called in fd out of range.");
316
317 return &fd_map[tgt_fd];
318}
278bool
279Process::checkAndAllocNextPage(Addr vaddr)
280{
281 // if this is an initial write we might not have
282 if (vaddr >= stack_min && vaddr < stack_base) {
283 pTable->allocate(roundDown(vaddr, VMPageSize), VMPageSize);
284 return true;
285 }

--- 10 unchanged lines hidden (view full) ---

296 pTable->allocate(stack_min, TheISA::PageBytes);
297 warn("Increasing stack size by one page.");
298 };
299 return true;
300 }
301 return false;
302}
303
319bool
320Process::checkAndAllocNextPage(Addr vaddr)
321{
322 // if this is an initial write we might not have
323 if (vaddr >= stack_min && vaddr < stack_base) {
324 pTable->allocate(roundDown(vaddr, VMPageSize), VMPageSize);
325 return true;
326 }

--- 10 unchanged lines hidden (view full) ---

337 pTable->allocate(stack_min, TheISA::PageBytes);
338 warn("Increasing stack size by one page.");
339 };
340 return true;
341 }
342 return false;
343}
344
345 // find all offsets for currently open files and save them
304void
346void
347Process::fix_file_offsets() {
348 Process::FdMap *fdo_stdin = &fd_map[STDIN_FILENO];
349 Process::FdMap *fdo_stdout = &fd_map[STDOUT_FILENO];
350 Process::FdMap *fdo_stderr = &fd_map[STDERR_FILENO];
351 string in = fdo_stdin->filename;
352 string out = fdo_stdout->filename;
353
354 // initialize file descriptors to default: same as simulator
355 int stdin_fd, stdout_fd, stderr_fd;
356
357 if (in == "stdin" || in == "cin")
358 stdin_fd = STDIN_FILENO;
359 else if (in == "None")
360 stdin_fd = -1;
361 else{
362 //OPEN standard in and seek to the right location
363 stdin_fd = Process::openInputFile(in);
364 if (lseek(stdin_fd, fdo_stdin->fileOffset, SEEK_SET) < 0)
365 panic("Unable to seek to correct location in file: %s", in);
366 }
367
368 if (out == "stdout" || out == "cout")
369 stdout_fd = STDOUT_FILENO;
370 else if (out == "stderr" || out == "cerr")
371 stdout_fd = STDERR_FILENO;
372 else if (out == "None")
373 stdout_fd = -1;
374 else{
375 stdout_fd = Process::openOutputFile(out);
376 if (lseek(stdin_fd, fdo_stdout->fileOffset, SEEK_SET) < 0)
377 panic("Unable to seek to correct in file: %s", out);
378 }
379
380 stderr_fd = (stdout_fd != STDOUT_FILENO) ? stdout_fd : STDERR_FILENO;
381
382 fdo_stdin->fd = stdin_fd;
383 fdo_stdout->fd = stdout_fd;
384 fdo_stderr->fd = stderr_fd;
385
386
387 for (int free_fd = 3; free_fd <= MAX_FD; ++free_fd) {
388 Process::FdMap *fdo = &fd_map[free_fd];
389 if (fdo->fd != -1) {
390 if (fdo->isPipe){
391 if (fdo->filename == "PIPE-WRITE")
392 continue;
393 else {
394 assert (fdo->filename == "PIPE-READ");
395 //create a new pipe
396 int fds[2];
397 int pipe_retval = pipe(fds);
398
399 if (pipe_retval < 0) {
400 // error
401 panic("Unable to create new pipe.");
402 }
403 fdo->fd = fds[0]; //set read pipe
404 Process::FdMap *fdo_write = &fd_map[fdo->readPipeSource];
405 if (fdo_write->filename != "PIPE-WRITE")
406 panic ("Couldn't find write end of the pipe");
407
408 fdo_write->fd = fds[1];//set write pipe
409 }
410 } else {
411 //Open file
412 int fd = open(fdo->filename.c_str(), fdo->flags, fdo->mode);
413
414 if (fd == -1)
415 panic("Unable to open file: %s", fdo->filename);
416 fdo->fd = fd;
417
418 //Seek to correct location before checkpoint
419 if (lseek(fd,fdo->fileOffset, SEEK_SET) < 0)
420 panic("Unable to seek to correct location in file: %s", fdo->filename);
421 }
422 }
423 }
424}
425void
426Process::find_file_offsets(){
427 for (int free_fd = 0; free_fd <= MAX_FD; ++free_fd) {
428 Process::FdMap *fdo = &fd_map[free_fd];
429 if (fdo->fd != -1) {
430 fdo->fileOffset = lseek(fdo->fd, 0, SEEK_CUR);
431 } else {
432 fdo->filename = "NULL";
433 fdo->fileOffset = 0;
434 }
435 }
436}
437
438void
439Process::setReadPipeSource(int read_pipe_fd, int source_fd){
440 Process::FdMap *fdo = &fd_map[read_pipe_fd];
441 fdo->readPipeSource = source_fd;
442}
443
444void
445Process::FdMap::serialize(std::ostream &os)
446{
447 SERIALIZE_SCALAR(fd);
448 SERIALIZE_SCALAR(isPipe);
449 SERIALIZE_SCALAR(filename);
450 SERIALIZE_SCALAR(flags);
451 SERIALIZE_SCALAR(readPipeSource);
452 SERIALIZE_SCALAR(fileOffset);
453}
454
455void
456Process::FdMap::unserialize(Checkpoint *cp, const std::string &section)
457{
458 UNSERIALIZE_SCALAR(fd);
459 UNSERIALIZE_SCALAR(isPipe);
460 UNSERIALIZE_SCALAR(filename);
461 UNSERIALIZE_SCALAR(flags);
462 UNSERIALIZE_SCALAR(readPipeSource);
463 UNSERIALIZE_SCALAR(fileOffset);
464}
465
466void
305Process::serialize(std::ostream &os)
306{
307 SERIALIZE_SCALAR(initialContextLoaded);
308 SERIALIZE_SCALAR(brk_point);
309 SERIALIZE_SCALAR(stack_base);
310 SERIALIZE_SCALAR(stack_size);
311 SERIALIZE_SCALAR(stack_min);
312 SERIALIZE_SCALAR(next_thread_stack_base);
313 SERIALIZE_SCALAR(mmap_start);
314 SERIALIZE_SCALAR(mmap_end);
315 SERIALIZE_SCALAR(nxm_start);
316 SERIALIZE_SCALAR(nxm_end);
467Process::serialize(std::ostream &os)
468{
469 SERIALIZE_SCALAR(initialContextLoaded);
470 SERIALIZE_SCALAR(brk_point);
471 SERIALIZE_SCALAR(stack_base);
472 SERIALIZE_SCALAR(stack_size);
473 SERIALIZE_SCALAR(stack_min);
474 SERIALIZE_SCALAR(next_thread_stack_base);
475 SERIALIZE_SCALAR(mmap_start);
476 SERIALIZE_SCALAR(mmap_end);
477 SERIALIZE_SCALAR(nxm_start);
478 SERIALIZE_SCALAR(nxm_end);
317 SERIALIZE_ARRAY(fd_map, MAX_FD);
318
479 find_file_offsets();
319 pTable->serialize(os);
480 pTable->serialize(os);
481 for (int x = 0; x <= MAX_FD; x++) {
482 nameOut(os, csprintf("%s.FdMap%d", name(), x));
483 fd_map[x].serialize(os);
484 }
485
320}
321
322void
323Process::unserialize(Checkpoint *cp, const std::string &section)
324{
325 UNSERIALIZE_SCALAR(initialContextLoaded);
326 UNSERIALIZE_SCALAR(brk_point);
327 UNSERIALIZE_SCALAR(stack_base);
328 UNSERIALIZE_SCALAR(stack_size);
329 UNSERIALIZE_SCALAR(stack_min);
330 UNSERIALIZE_SCALAR(next_thread_stack_base);
331 UNSERIALIZE_SCALAR(mmap_start);
332 UNSERIALIZE_SCALAR(mmap_end);
333 UNSERIALIZE_SCALAR(nxm_start);
334 UNSERIALIZE_SCALAR(nxm_end);
486}
487
488void
489Process::unserialize(Checkpoint *cp, const std::string &section)
490{
491 UNSERIALIZE_SCALAR(initialContextLoaded);
492 UNSERIALIZE_SCALAR(brk_point);
493 UNSERIALIZE_SCALAR(stack_base);
494 UNSERIALIZE_SCALAR(stack_size);
495 UNSERIALIZE_SCALAR(stack_min);
496 UNSERIALIZE_SCALAR(next_thread_stack_base);
497 UNSERIALIZE_SCALAR(mmap_start);
498 UNSERIALIZE_SCALAR(mmap_end);
499 UNSERIALIZE_SCALAR(nxm_start);
500 UNSERIALIZE_SCALAR(nxm_end);
335 UNSERIALIZE_ARRAY(fd_map, MAX_FD);
336
337 pTable->unserialize(cp, section);
501 pTable->unserialize(cp, section);
502 for (int x = 0; x <= MAX_FD; x++) {
503 fd_map[x].unserialize(cp, csprintf("%s.FdMap%d", section, x));
504 }
505 fix_file_offsets();
338
506
339
340 checkpointRestored = true;
341
342}
343
344
345////////////////////////////////////////////////////////////////////////
346//
347// LiveProcess member definitions

--- 203 unchanged lines hidden ---
507 checkpointRestored = true;
508
509}
510
511
512////////////////////////////////////////////////////////////////////////
513//
514// LiveProcess member definitions

--- 203 unchanged lines hidden ---