From ef08046af413e2dc19cf7e8e1a3a329cc3c05bec Mon Sep 17 00:00:00 2001 From: Brandon Potter Date: Fri, 24 Jul 2015 12:25:22 -0700 Subject: syscall_emul: file descriptor interface changes This patch gets rid of unused Process::dup_fd method and does minor refactoring in the process class files. The file descriptor max has been changed to be the number of file descriptors since this clarifies the loop boundary condition and cleans up the code a bit. The fd_map field has been altered to be dynamically allocated as opposed to being an array; the intention here is to build on this is subsequent patches to allow processes to share their file descriptors with the clone system call. --- src/sim/process.cc | 68 ++++++++++++++++++++++-------------------------------- 1 file changed, 27 insertions(+), 41 deletions(-) (limited to 'src/sim/process.cc') diff --git a/src/sim/process.cc b/src/sim/process.cc index 198f05adf..8aca849a6 100644 --- a/src/sim/process.cc +++ b/src/sim/process.cc @@ -115,7 +115,8 @@ Process::Process(ProcessParams * params) static_cast(new ArchPageTable(name(), M5_pid, system)) : static_cast(new FuncPageTable(name(), M5_pid)) ), initVirtMem(system->getSystemPort(), this, - SETranslatingPortProxy::Always) + SETranslatingPortProxy::Always), + fd_map(new FdMap[NUM_FDS]) { string in = params->input; string out = params->output; @@ -175,7 +176,7 @@ Process::Process(ProcessParams * params) // mark remaining fds as free - for (int i = 3; i <= MAX_FD; ++i) { + for (int i = 3; i < NUM_FDS; ++i) { fdo = &fd_map[i]; fdo->fd = -1; } @@ -200,6 +201,7 @@ Process::regStats() // // static helper functions // + int Process::openInputFile(const string &filename) { @@ -232,14 +234,10 @@ Process::openOutputFile(const string &filename) ThreadContext * Process::findFreeContext() { - int size = contextIds.size(); - ThreadContext *tc; - for (int i = 0; i < size; ++i) { - tc = system->getThreadContext(contextIds[i]); - if (tc->status() == ThreadContext::Halted) { - // inactive context, free to use + for (int id : contextIds) { + ThreadContext *tc = system->getThreadContext(id); + if (tc->status() == ThreadContext::Halted) return tc; - } } return NULL; } @@ -266,19 +264,6 @@ Process::drain() return DrainState::Drained; } -// map simulator fd sim_fd to target fd tgt_fd -void -Process::dup_fd(int sim_fd, int tgt_fd) -{ - if (tgt_fd < 0 || tgt_fd > MAX_FD) - panic("Process::dup_fd tried to dup past MAX_FD (%d)", tgt_fd); - - Process::FdMap *fdo = &fd_map[tgt_fd]; - fdo->fd = sim_fd; -} - - -// generate new target fd for sim_fd int Process::alloc_fd(int sim_fd, const string& filename, int flags, int mode, bool pipe) @@ -288,7 +273,7 @@ Process::alloc_fd(int sim_fd, const string& filename, int flags, int mode, return -1; // find first free target fd - for (int free_fd = 0; free_fd <= MAX_FD; ++free_fd) { + for (int free_fd = 0; free_fd < NUM_FDS; ++free_fd) { Process::FdMap *fdo = &fd_map[free_fd]; if (fdo->fd == -1 && fdo->driver == NULL) { fdo->fd = sim_fd; @@ -305,14 +290,11 @@ Process::alloc_fd(int sim_fd, const string& filename, int flags, int mode, panic("Process::alloc_fd: out of file descriptors!"); } - -// free target fd (e.g., after close) void -Process::free_fd(int tgt_fd) +Process::free_fdmap_entry(int tgt_fd) { Process::FdMap *fdo = &fd_map[tgt_fd]; - if (fdo->fd == -1) - warn("Process::free_fd: request to free unused fd %d", tgt_fd); + assert(fd_map[tgt_fd].fd > -1); fdo->fd = -1; fdo->filename = "NULL"; @@ -324,26 +306,30 @@ Process::free_fd(int tgt_fd) fdo->driver = NULL; } - -// look up simulator fd for given target fd int Process::sim_fd(int tgt_fd) { - if (tgt_fd < 0 || tgt_fd > MAX_FD) - return -1; - - return fd_map[tgt_fd].fd; + FdMap *obj = sim_fd_obj(tgt_fd); + return obj ? obj->fd : -1; } Process::FdMap * Process::sim_fd_obj(int tgt_fd) { - if (tgt_fd < 0 || tgt_fd > MAX_FD) + if (tgt_fd < 0 || tgt_fd >= NUM_FDS) return NULL; - return &fd_map[tgt_fd]; } +int +Process::tgt_fd(int sim_fd) +{ + for (int index = 0; index < NUM_FDS; ++index) + if (fd_map[index].fd == sim_fd) + return index; + return -1; +} + void Process::allocateMem(Addr vaddr, int64_t size, bool clobber) { @@ -433,7 +419,7 @@ Process::fix_file_offsets() fdo_stderr->fd = stderr_fd; - for (int free_fd = 3; free_fd <= MAX_FD; ++free_fd) { + for (int free_fd = 3; free_fd < NUM_FDS; ++free_fd) { Process::FdMap *fdo = &fd_map[free_fd]; if (fdo->fd != -1) { if (fdo->isPipe){ @@ -476,7 +462,7 @@ Process::fix_file_offsets() void Process::find_file_offsets() { - for (int free_fd = 0; free_fd <= MAX_FD; ++free_fd) { + for (int free_fd = 0; free_fd < NUM_FDS; ++free_fd) { Process::FdMap *fdo = &fd_map[free_fd]; if (fdo->fd != -1) { fdo->fileOffset = lseek(fdo->fd, 0, SEEK_CUR); @@ -529,7 +515,7 @@ Process::serialize(CheckpointOut &cp) const SERIALIZE_SCALAR(nxm_start); SERIALIZE_SCALAR(nxm_end); pTable->serialize(cp); - for (int x = 0; x <= MAX_FD; x++) { + for (int x = 0; x < NUM_FDS; x++) { fd_map[x].serializeSection(cp, csprintf("FdMap%d", x)); } SERIALIZE_SCALAR(M5_pid); @@ -549,7 +535,7 @@ Process::unserialize(CheckpointIn &cp) UNSERIALIZE_SCALAR(nxm_start); UNSERIALIZE_SCALAR(nxm_end); pTable->unserialize(cp); - for (int x = 0; x <= MAX_FD; x++) { + for (int x = 0; x < NUM_FDS; x++) { fd_map[x].unserializeSection(cp, csprintf("FdMap%d", x)); } fix_file_offsets(); @@ -557,7 +543,7 @@ Process::unserialize(CheckpointIn &cp) // The above returns a bool so that you could do something if you don't // find the param in the checkpoint if you wanted to, like set a default // but in this case we'll just stick with the instantianted value if not - // found. + // found. } -- cgit v1.2.3