process.cc revision 2
1/*
2 * Copyright (c) 2003 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#include <stdio.h>
32
33#include <string>
34
35#include "main_memory.hh"
36#include "prog.hh"
37
38#include "eio.hh"
39#include "thread.hh"
40#include "fake_syscall.hh"
41#include "loader.hh"
42#include "exec_context.hh"
43#include "smt.hh"
44
45#include "statistics.hh"
46#include "sim_stats.hh"
47
48using namespace std;
49
50//
51// The purpose of this code is to fake the loader & syscall mechanism
52// when there's no OS: thus there's no resone to use it in FULL_SYSTEM
53// mode when we do have an OS
54//
55#ifdef FULL_SYSTEM
56#error "prog.cc not compatible with FULL_SYSTEM"
57#endif
58
59// max allowable number of processes: should be no real cost to
60// cranking this up if necessary
61const int MAX_PROCESSES = 8;
62
63// current number of allocated processes
64int num_processes = 0;
65
66Process::Process(const string &name,
67                 int stdin_fd, 	// initial I/O descriptors
68                 int stdout_fd,
69                 int stderr_fd)
70    : SimObject(name)
71{
72    // allocate memory space
73    memory = new MainMemory(name + ".MainMem");
74
75    // allocate initial register file
76    init_regs = new RegFile;
77
78    // initialize first 3 fds (stdin, stdout, stderr)
79    fd_map[STDIN_FILENO] = stdin_fd;
80    fd_map[STDOUT_FILENO] = stdout_fd;
81    fd_map[STDERR_FILENO] = stderr_fd;
82
83    // mark remaining fds as free
84    for (int i = 3; i <= MAX_FD; ++i) {
85        fd_map[i] = -1;
86    }
87
88    numCpus = 0;
89
90    num_syscalls = 0;
91
92    // other parameters will be initialized when the program is loaded
93}
94
95void
96Process::regStats()
97{
98    using namespace Statistics;
99
100    num_syscalls
101        .name(name() + ".PROG:num_syscalls")
102        .desc("Number of system calls")
103        ;
104}
105
106//
107// static helper functions
108//
109int
110Process::openInputFile(const string &filename)
111{
112    int fd = open(filename.c_str(), O_RDONLY);
113
114    if (fd == -1) {
115        perror(NULL);
116        cerr << "unable to open \"" << filename << "\" for reading\n";
117        fatal("can't open input file");
118    }
119
120    return fd;
121}
122
123
124int
125Process::openOutputFile(const string &filename)
126{
127    int fd = open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0774);
128
129    if (fd == -1) {
130        perror(NULL);
131        cerr << "unable to open \"" << filename << "\" for writing\n";
132        fatal("can't open output file");
133    }
134
135    return fd;
136}
137
138
139void
140Process::registerExecContext(ExecContext *ec)
141{
142    if (execContexts.empty()) {
143        // first exec context for this process... initialize & enable
144
145        // copy process's initial regs struct
146        ec->regs = *init_regs;
147
148        // mark this context as active
149        ec->setStatus(ExecContext::Active);
150    }
151    else {
152        ec->setStatus(ExecContext::Unallocated);
153    }
154
155    // add to list
156    execContexts.push_back(ec);
157
158    // increment available CPU count
159    ++numCpus;
160}
161
162
163// map simulator fd sim_fd to target fd tgt_fd
164void
165Process::dup_fd(int sim_fd, int tgt_fd)
166{
167    if (tgt_fd < 0 || tgt_fd > MAX_FD)
168        panic("Process::dup_fd tried to dup past MAX_FD (%d)", tgt_fd);
169
170    fd_map[tgt_fd] = sim_fd;
171}
172
173
174// generate new target fd for sim_fd
175int
176Process::open_fd(int sim_fd)
177{
178    int free_fd;
179
180    // in case open() returns an error, don't allocate a new fd
181    if (sim_fd == -1)
182        return -1;
183
184    // find first free target fd
185    for (free_fd = 0; fd_map[free_fd] >= 0; ++free_fd) {
186        if (free_fd == MAX_FD)
187            panic("Process::open_fd: out of file descriptors!");
188    }
189
190    fd_map[free_fd] = sim_fd;
191
192    return free_fd;
193}
194
195
196// look up simulator fd for given target fd
197int
198Process::sim_fd(int tgt_fd)
199{
200    if (tgt_fd > MAX_FD)
201        return -1;
202
203    return fd_map[tgt_fd];
204}
205
206
207
208//
209// need to declare these here since there is no concrete Process type
210// that can be constructed (i.e., no REGISTER_SIM_OBJECT() macro call,
211// which is where these get declared for concrete types).
212//
213DEFINE_SIM_OBJECT_CLASS_NAME("Process object", Process)
214
215
216////////////////////////////////////////////////////////////////////////
217//
218// LiveProcess member definitions
219//
220////////////////////////////////////////////////////////////////////////
221
222LiveProcess::LiveProcess(const string &name,
223                         int stdin_fd, int stdout_fd, int stderr_fd,
224                         vector<string> &argv, vector<string> &envp)
225    : Process(name, stdin_fd, stdout_fd, stderr_fd)
226{
227    smt_load_prog(argv, envp, init_regs, this);
228}
229
230
231void
232LiveProcess::syscall(ExecContext *xc)
233{
234    num_syscalls++;
235
236    fake_syscall(this, xc);
237}
238
239
240BEGIN_DECLARE_SIM_OBJECT_PARAMS(LiveProcess)
241
242    VectorParam<string> cmd;
243    Param<string> input;
244    Param<string> output;
245    VectorParam<string> env;
246
247END_DECLARE_SIM_OBJECT_PARAMS(LiveProcess)
248
249
250BEGIN_INIT_SIM_OBJECT_PARAMS(LiveProcess)
251
252    INIT_PARAM(cmd, "command line (executable plus arguments)"),
253    INIT_PARAM(input, "filename for stdin (dflt: use sim stdin)"),
254    INIT_PARAM(output, "filename for stdout/stderr (dflt: use sim stdout)"),
255    INIT_PARAM(env, "environment settings")
256
257END_INIT_SIM_OBJECT_PARAMS(LiveProcess)
258
259
260CREATE_SIM_OBJECT(LiveProcess)
261{
262    // initialize file descriptors to default: same as simulator
263    int stdin_fd = input.isValid() ? Process::openInputFile(input) : 0;
264    int stdout_fd = output.isValid() ? Process::openOutputFile(output) : 1;
265    int stderr_fd = output.isValid() ? stdout_fd : 2;
266
267    // dummy for default env
268    vector<string> null_vec;
269
270    //  We do this with "temp" because of the bogus compiler warning
271    //  you get with g++ 2.95 -O if you just "return new LiveProcess(..."
272    LiveProcess *temp = new LiveProcess(getInstanceName(),
273                                        stdin_fd, stdout_fd, stderr_fd,
274                                        cmd,
275                                        env.isValid() ? env : null_vec);
276
277    return temp;
278}
279
280
281REGISTER_SIM_OBJECT("LiveProcess", LiveProcess)
282