process.cc revision 180
14158Sgblack@eecs.umich.edu/*
24158Sgblack@eecs.umich.edu * Copyright (c) 2003 The Regents of The University of Michigan
34158Sgblack@eecs.umich.edu * All rights reserved.
44158Sgblack@eecs.umich.edu *
54158Sgblack@eecs.umich.edu * Redistribution and use in source and binary forms, with or without
64158Sgblack@eecs.umich.edu * modification, are permitted provided that the following conditions are
74158Sgblack@eecs.umich.edu * met: redistributions of source code must retain the above copyright
84158Sgblack@eecs.umich.edu * notice, this list of conditions and the following disclaimer;
94158Sgblack@eecs.umich.edu * redistributions in binary form must reproduce the above copyright
104158Sgblack@eecs.umich.edu * notice, this list of conditions and the following disclaimer in the
114158Sgblack@eecs.umich.edu * documentation and/or other materials provided with the distribution;
124158Sgblack@eecs.umich.edu * neither the name of the copyright holders nor the names of its
134158Sgblack@eecs.umich.edu * contributors may be used to endorse or promote products derived from
144158Sgblack@eecs.umich.edu * this software without specific prior written permission.
154158Sgblack@eecs.umich.edu *
164158Sgblack@eecs.umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
174158Sgblack@eecs.umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
184158Sgblack@eecs.umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
194158Sgblack@eecs.umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
204158Sgblack@eecs.umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
214158Sgblack@eecs.umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
224158Sgblack@eecs.umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
234158Sgblack@eecs.umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
244158Sgblack@eecs.umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
254158Sgblack@eecs.umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
264158Sgblack@eecs.umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
274158Sgblack@eecs.umich.edu */
284158Sgblack@eecs.umich.edu
295409Sgblack@eecs.umich.edu#include <unistd.h>
304158Sgblack@eecs.umich.edu#include <fcntl.h>
314158Sgblack@eecs.umich.edu
324158Sgblack@eecs.umich.edu#include <cstdio>
334158Sgblack@eecs.umich.edu#include <string>
344158Sgblack@eecs.umich.edu
354158Sgblack@eecs.umich.edu#include "base/intmath.hh"
364158Sgblack@eecs.umich.edu#include "base/loader/object_file.hh"
374158Sgblack@eecs.umich.edu#include "base/statistics.hh"
384158Sgblack@eecs.umich.edu#include "cpu/exec_context.hh"
394158Sgblack@eecs.umich.edu#include "cpu/full_cpu/smt.hh"
404158Sgblack@eecs.umich.edu#include "cpu/full_cpu/thread.hh"
414158Sgblack@eecs.umich.edu#include "eio/eio.hh"
424158Sgblack@eecs.umich.edu#include "mem/functional_mem/main_memory.hh"
434158Sgblack@eecs.umich.edu#include "sim/builder.hh"
444158Sgblack@eecs.umich.edu#include "sim/fake_syscall.hh"
454158Sgblack@eecs.umich.edu#include "sim/prog.hh"
464158Sgblack@eecs.umich.edu#include "sim/sim_stats.hh"
474158Sgblack@eecs.umich.edu
484158Sgblack@eecs.umich.eduusing namespace std;
494158Sgblack@eecs.umich.edu
504158Sgblack@eecs.umich.edu//
514158Sgblack@eecs.umich.edu// The purpose of this code is to fake the loader & syscall mechanism
524158Sgblack@eecs.umich.edu// when there's no OS: thus there's no resone to use it in FULL_SYSTEM
534158Sgblack@eecs.umich.edu// mode when we do have an OS
544158Sgblack@eecs.umich.edu//
554158Sgblack@eecs.umich.edu#ifdef FULL_SYSTEM
564158Sgblack@eecs.umich.edu#error "prog.cc not compatible with FULL_SYSTEM"
574158Sgblack@eecs.umich.edu#endif
584158Sgblack@eecs.umich.edu
594158Sgblack@eecs.umich.edu// max allowable number of processes: should be no real cost to
604158Sgblack@eecs.umich.edu// cranking this up if necessary
614158Sgblack@eecs.umich.educonst int MAX_PROCESSES = 8;
624158Sgblack@eecs.umich.edu
634158Sgblack@eecs.umich.edu// current number of allocated processes
644158Sgblack@eecs.umich.eduint num_processes = 0;
654158Sgblack@eecs.umich.edu
664158Sgblack@eecs.umich.eduProcess::Process(const string &name,
674158Sgblack@eecs.umich.edu                 int stdin_fd, 	// initial I/O descriptors
684158Sgblack@eecs.umich.edu                 int stdout_fd,
694158Sgblack@eecs.umich.edu                 int stderr_fd)
704158Sgblack@eecs.umich.edu    : SimObject(name)
714158Sgblack@eecs.umich.edu{
724158Sgblack@eecs.umich.edu    // allocate memory space
734158Sgblack@eecs.umich.edu    memory = new MainMemory(name + ".MainMem");
744158Sgblack@eecs.umich.edu
754158Sgblack@eecs.umich.edu    // allocate initial register file
764158Sgblack@eecs.umich.edu    init_regs = new RegFile;
774158Sgblack@eecs.umich.edu
784158Sgblack@eecs.umich.edu    // initialize first 3 fds (stdin, stdout, stderr)
794158Sgblack@eecs.umich.edu    fd_map[STDIN_FILENO] = stdin_fd;
804158Sgblack@eecs.umich.edu    fd_map[STDOUT_FILENO] = stdout_fd;
814158Sgblack@eecs.umich.edu    fd_map[STDERR_FILENO] = stderr_fd;
824158Sgblack@eecs.umich.edu
834158Sgblack@eecs.umich.edu    // mark remaining fds as free
844158Sgblack@eecs.umich.edu    for (int i = 3; i <= MAX_FD; ++i) {
854158Sgblack@eecs.umich.edu        fd_map[i] = -1;
864158Sgblack@eecs.umich.edu    }
874158Sgblack@eecs.umich.edu
884158Sgblack@eecs.umich.edu    num_syscalls = 0;
894158Sgblack@eecs.umich.edu
904158Sgblack@eecs.umich.edu    // other parameters will be initialized when the program is loaded
914158Sgblack@eecs.umich.edu}
924158Sgblack@eecs.umich.edu
934158Sgblack@eecs.umich.eduvoid
944158Sgblack@eecs.umich.eduProcess::regStats()
954158Sgblack@eecs.umich.edu{
964158Sgblack@eecs.umich.edu    using namespace Statistics;
976360Sgblack@eecs.umich.edu
986360Sgblack@eecs.umich.edu    num_syscalls
996360Sgblack@eecs.umich.edu        .name(name() + ".PROG:num_syscalls")
1006360Sgblack@eecs.umich.edu        .desc("Number of system calls")
1016360Sgblack@eecs.umich.edu        ;
1026360Sgblack@eecs.umich.edu}
1036360Sgblack@eecs.umich.edu
1046360Sgblack@eecs.umich.edu//
1056360Sgblack@eecs.umich.edu// static helper functions
1066360Sgblack@eecs.umich.edu//
1076360Sgblack@eecs.umich.eduint
1086360Sgblack@eecs.umich.eduProcess::openInputFile(const string &filename)
1096360Sgblack@eecs.umich.edu{
1106360Sgblack@eecs.umich.edu    int fd = open(filename.c_str(), O_RDONLY);
1116360Sgblack@eecs.umich.edu
1126360Sgblack@eecs.umich.edu    if (fd == -1) {
1136360Sgblack@eecs.umich.edu        perror(NULL);
1146360Sgblack@eecs.umich.edu        cerr << "unable to open \"" << filename << "\" for reading\n";
1154158Sgblack@eecs.umich.edu        fatal("can't open input file");
1166360Sgblack@eecs.umich.edu    }
1176360Sgblack@eecs.umich.edu
1186360Sgblack@eecs.umich.edu    return fd;
1196360Sgblack@eecs.umich.edu}
1206360Sgblack@eecs.umich.edu
1216360Sgblack@eecs.umich.edu
1226360Sgblack@eecs.umich.eduint
1236360Sgblack@eecs.umich.eduProcess::openOutputFile(const string &filename)
1246360Sgblack@eecs.umich.edu{
1256360Sgblack@eecs.umich.edu    int fd = open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0774);
1266360Sgblack@eecs.umich.edu
1276360Sgblack@eecs.umich.edu    if (fd == -1) {
1286360Sgblack@eecs.umich.edu        perror(NULL);
1296360Sgblack@eecs.umich.edu        cerr << "unable to open \"" << filename << "\" for writing\n";
1306479Sgblack@eecs.umich.edu        fatal("can't open output file");
1316360Sgblack@eecs.umich.edu    }
1326360Sgblack@eecs.umich.edu
1336360Sgblack@eecs.umich.edu    return fd;
1346360Sgblack@eecs.umich.edu}
1356360Sgblack@eecs.umich.edu
1366360Sgblack@eecs.umich.edu
1376360Sgblack@eecs.umich.eduint
1386360Sgblack@eecs.umich.eduProcess::registerExecContext(ExecContext *xc)
1396360Sgblack@eecs.umich.edu{
1406360Sgblack@eecs.umich.edu    // add to list
1416360Sgblack@eecs.umich.edu    int myIndex = execContexts.size();
1426360Sgblack@eecs.umich.edu    execContexts.push_back(xc);
1435026Sgblack@eecs.umich.edu
1445026Sgblack@eecs.umich.edu    if (myIndex == 0) {
1455026Sgblack@eecs.umich.edu        // first exec context for this process... initialize & enable
1465426Sgblack@eecs.umich.edu
1475426Sgblack@eecs.umich.edu        // copy process's initial regs struct
1486360Sgblack@eecs.umich.edu        xc->regs = *init_regs;
1495426Sgblack@eecs.umich.edu
1505082Sgblack@eecs.umich.edu        // mark this context as active
1516360Sgblack@eecs.umich.edu        xc->initStatus(ExecContext::Active);
1526360Sgblack@eecs.umich.edu    }
1535294Sgblack@eecs.umich.edu    else {
1546360Sgblack@eecs.umich.edu        xc->initStatus(ExecContext::Unallocated);
1555290Sgblack@eecs.umich.edu    }
1565294Sgblack@eecs.umich.edu
1575294Sgblack@eecs.umich.edu    // return CPU number to caller and increment available CPU count
1586360Sgblack@eecs.umich.edu    return myIndex;
1596360Sgblack@eecs.umich.edu}
1606360Sgblack@eecs.umich.edu
1616360Sgblack@eecs.umich.edu
1626360Sgblack@eecs.umich.eduvoid
1636360Sgblack@eecs.umich.eduProcess::replaceExecContext(int xcIndex, ExecContext *xc)
1646360Sgblack@eecs.umich.edu{
1656360Sgblack@eecs.umich.edu    if (xcIndex >= execContexts.size()) {
1666360Sgblack@eecs.umich.edu        panic("replaceExecContext: bad xcIndex, %d >= %d\n",
1676360Sgblack@eecs.umich.edu              xcIndex, execContexts.size());
1686360Sgblack@eecs.umich.edu    }
1696360Sgblack@eecs.umich.edu
1705294Sgblack@eecs.umich.edu    execContexts[xcIndex] = xc;
1715294Sgblack@eecs.umich.edu}
1726360Sgblack@eecs.umich.edu
1736360Sgblack@eecs.umich.edu// map simulator fd sim_fd to target fd tgt_fd
1746360Sgblack@eecs.umich.eduvoid
1756360Sgblack@eecs.umich.eduProcess::dup_fd(int sim_fd, int tgt_fd)
1766360Sgblack@eecs.umich.edu{
1776360Sgblack@eecs.umich.edu    if (tgt_fd < 0 || tgt_fd > MAX_FD)
1786360Sgblack@eecs.umich.edu        panic("Process::dup_fd tried to dup past MAX_FD (%d)", tgt_fd);
1796360Sgblack@eecs.umich.edu
1806360Sgblack@eecs.umich.edu    fd_map[tgt_fd] = sim_fd;
1816360Sgblack@eecs.umich.edu}
1826360Sgblack@eecs.umich.edu
1836360Sgblack@eecs.umich.edu
1846360Sgblack@eecs.umich.edu// generate new target fd for sim_fd
1856360Sgblack@eecs.umich.eduint
1866360Sgblack@eecs.umich.eduProcess::open_fd(int sim_fd)
1876360Sgblack@eecs.umich.edu{
1884158Sgblack@eecs.umich.edu    int free_fd;
189
190    // in case open() returns an error, don't allocate a new fd
191    if (sim_fd == -1)
192        return -1;
193
194    // find first free target fd
195    for (free_fd = 0; fd_map[free_fd] >= 0; ++free_fd) {
196        if (free_fd == MAX_FD)
197            panic("Process::open_fd: out of file descriptors!");
198    }
199
200    fd_map[free_fd] = sim_fd;
201
202    return free_fd;
203}
204
205
206// look up simulator fd for given target fd
207int
208Process::sim_fd(int tgt_fd)
209{
210    if (tgt_fd > MAX_FD)
211        return -1;
212
213    return fd_map[tgt_fd];
214}
215
216
217
218//
219// need to declare these here since there is no concrete Process type
220// that can be constructed (i.e., no REGISTER_SIM_OBJECT() macro call,
221// which is where these get declared for concrete types).
222//
223DEFINE_SIM_OBJECT_CLASS_NAME("Process object", Process)
224
225
226////////////////////////////////////////////////////////////////////////
227//
228// LiveProcess member definitions
229//
230////////////////////////////////////////////////////////////////////////
231
232
233static void
234copyStringArray(vector<string> &strings, Addr array_ptr, Addr data_ptr,
235                FunctionalMemory *memory)
236{
237    for (int i = 0; i < strings.size(); ++i) {
238        memory->access(Write, array_ptr, &data_ptr, sizeof(Addr));
239        memory->writeString(data_ptr, strings[i].c_str());
240        array_ptr += sizeof(Addr);
241        data_ptr += strings[i].size() + 1;
242    }
243    // add NULL terminator
244    data_ptr = 0;
245    memory->access(Write, array_ptr, &data_ptr, sizeof(Addr));
246}
247
248LiveProcess::LiveProcess(const string &name,
249                         int stdin_fd, int stdout_fd, int stderr_fd,
250                         vector<string> &argv, vector<string> &envp)
251    : Process(name, stdin_fd, stdout_fd, stderr_fd)
252{
253    prog_fname = argv[0];
254    ObjectFile *objFile = createObjectFile(prog_fname);
255    if (objFile == NULL) {
256        fatal("Can't load object file %s", prog_fname);
257    }
258
259    prog_entry = objFile->entryPoint();
260    text_base = objFile->textBase();
261    text_size = objFile->textSize();
262    data_base = objFile->dataBase();
263    data_size = objFile->dataSize() + objFile->bssSize();
264    brk_point = RoundUp<uint64_t>(data_base + data_size, VMPageSize);
265
266    // load object file into target memory
267    objFile->loadSections(memory);
268
269    // Set up stack.  On Alpha, stack goes below text section.  This
270    // code should get moved to some architecture-specific spot.
271    stack_base = text_base - (409600+4096);
272
273    // Set pointer for next thread stack.  Reserve 8M for main stack.
274    next_thread_stack_base = stack_base - (8 * 1024 * 1024);
275
276    // Calculate how much space we need for arg & env arrays.
277    int argv_array_size = sizeof(Addr) * (argv.size() + 1);
278    int envp_array_size = sizeof(Addr) * (envp.size() + 1);
279    int arg_data_size = 0;
280    for (int i = 0; i < argv.size(); ++i) {
281        arg_data_size += argv[i].size() + 1;
282    }
283    int env_data_size = 0;
284    for (int i = 0; i < envp.size(); ++i) {
285        env_data_size += envp[i].size() + 1;
286    }
287
288    int space_needed =
289        argv_array_size + envp_array_size + arg_data_size + env_data_size;
290    // for SimpleScalar compatibility
291    if (space_needed < 16384)
292        space_needed = 16384;
293
294    // set bottom of stack
295    stack_min = stack_base - space_needed;
296    // align it
297    stack_min &= ~7;
298    stack_size = stack_base - stack_min;
299
300    // map out initial stack contents
301    Addr argv_array_base = stack_min + sizeof(uint64_t); // room for argc
302    Addr envp_array_base = argv_array_base + argv_array_size;
303    Addr arg_data_base = envp_array_base + envp_array_size;
304    Addr env_data_base = arg_data_base + arg_data_size;
305
306    // write contents to stack
307    uint64_t argc = argv.size();
308    memory->access(Write, stack_min, &argc, sizeof(uint64_t));
309
310    copyStringArray(argv, argv_array_base, arg_data_base, memory);
311    copyStringArray(envp, envp_array_base, env_data_base, memory);
312
313    init_regs->intRegFile[ArgumentReg0] = argc;
314    init_regs->intRegFile[ArgumentReg1] = argv_array_base;
315    init_regs->intRegFile[StackPointerReg] = stack_min;
316    init_regs->intRegFile[GlobalPointerReg] = objFile->globalPointer();
317    init_regs->pc = prog_entry;
318    init_regs->npc = prog_entry + sizeof(MachInst);
319}
320
321
322void
323LiveProcess::syscall(ExecContext *xc)
324{
325    num_syscalls++;
326
327    fake_syscall(this, xc);
328}
329
330
331BEGIN_DECLARE_SIM_OBJECT_PARAMS(LiveProcess)
332
333    VectorParam<string> cmd;
334    Param<string> input;
335    Param<string> output;
336    VectorParam<string> env;
337
338END_DECLARE_SIM_OBJECT_PARAMS(LiveProcess)
339
340
341BEGIN_INIT_SIM_OBJECT_PARAMS(LiveProcess)
342
343    INIT_PARAM(cmd, "command line (executable plus arguments)"),
344    INIT_PARAM(input, "filename for stdin (dflt: use sim stdin)"),
345    INIT_PARAM(output, "filename for stdout/stderr (dflt: use sim stdout)"),
346    INIT_PARAM(env, "environment settings")
347
348END_INIT_SIM_OBJECT_PARAMS(LiveProcess)
349
350
351CREATE_SIM_OBJECT(LiveProcess)
352{
353    // initialize file descriptors to default: same as simulator
354    int stdin_fd = input.isValid() ? Process::openInputFile(input) : 0;
355    int stdout_fd = output.isValid() ? Process::openOutputFile(output) : 1;
356    int stderr_fd = output.isValid() ? stdout_fd : 2;
357
358    // dummy for default env
359    vector<string> null_vec;
360
361    //  We do this with "temp" because of the bogus compiler warning
362    //  you get with g++ 2.95 -O if you just "return new LiveProcess(..."
363    LiveProcess *temp = new LiveProcess(getInstanceName(),
364                                        stdin_fd, stdout_fd, stderr_fd,
365                                        cmd,
366                                        env.isValid() ? env : null_vec);
367
368    return temp;
369}
370
371
372REGISTER_SIM_OBJECT("LiveProcess", LiveProcess)
373