process.cc revision 2686:f0d591379ac3
14484Sbinkertn@umich.edu/*
24484Sbinkertn@umich.edu * Copyright (c) 2003-2004 The Regents of The University of Michigan
34484Sbinkertn@umich.edu * All rights reserved.
44484Sbinkertn@umich.edu *
54484Sbinkertn@umich.edu * Redistribution and use in source and binary forms, with or without
64484Sbinkertn@umich.edu * modification, are permitted provided that the following conditions are
74484Sbinkertn@umich.edu * met: redistributions of source code must retain the above copyright
84484Sbinkertn@umich.edu * notice, this list of conditions and the following disclaimer;
94484Sbinkertn@umich.edu * redistributions in binary form must reproduce the above copyright
104484Sbinkertn@umich.edu * notice, this list of conditions and the following disclaimer in the
114484Sbinkertn@umich.edu * documentation and/or other materials provided with the distribution;
124484Sbinkertn@umich.edu * neither the name of the copyright holders nor the names of its
134484Sbinkertn@umich.edu * contributors may be used to endorse or promote products derived from
144484Sbinkertn@umich.edu * this software without specific prior written permission.
154484Sbinkertn@umich.edu *
164484Sbinkertn@umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
174484Sbinkertn@umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
184484Sbinkertn@umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
194484Sbinkertn@umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
204484Sbinkertn@umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
214484Sbinkertn@umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
224484Sbinkertn@umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
234484Sbinkertn@umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
244484Sbinkertn@umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
254484Sbinkertn@umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
264484Sbinkertn@umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
274484Sbinkertn@umich.edu *
284484Sbinkertn@umich.edu * Authors: Gabe Black
294484Sbinkertn@umich.edu *          Ali Saidi
304484Sbinkertn@umich.edu *          Korey Sewell
314484Sbinkertn@umich.edu */
324484Sbinkertn@umich.edu
334484Sbinkertn@umich.edu#include "arch/mips/isa_traits.hh"
344484Sbinkertn@umich.edu#include "arch/mips/process.hh"
354484Sbinkertn@umich.edu#include "arch/mips/linux/process.hh"
364484Sbinkertn@umich.edu#include "base/loader/object_file.hh"
374484Sbinkertn@umich.edu#include "base/misc.hh"
384484Sbinkertn@umich.edu#include "cpu/thread_context.hh"
394484Sbinkertn@umich.edu#include "sim/builder.hh"
404484Sbinkertn@umich.edu#include "sim/system.hh"
414484Sbinkertn@umich.edu
424484Sbinkertn@umich.eduusing namespace std;
434484Sbinkertn@umich.eduusing namespace MipsISA;
444484Sbinkertn@umich.edu
454484Sbinkertn@umich.edu
464484Sbinkertn@umich.eduMipsLiveProcess *
474484Sbinkertn@umich.eduMipsLiveProcess::create(const std::string &nm, System *system, int stdin_fd,
484484Sbinkertn@umich.edu        int stdout_fd, int stderr_fd, std::string executable,
494484Sbinkertn@umich.edu        std::vector<std::string> &argv, std::vector<std::string> &envp)
504484Sbinkertn@umich.edu{
514484Sbinkertn@umich.edu    MipsLiveProcess *process = NULL;
524484Sbinkertn@umich.edu
534484Sbinkertn@umich.edu    ObjectFile *objFile = createObjectFile(executable);
544484Sbinkertn@umich.edu    if (objFile == NULL) {
554484Sbinkertn@umich.edu        fatal("Can't load object file %s", executable);
564484Sbinkertn@umich.edu    }
574484Sbinkertn@umich.edu
584484Sbinkertn@umich.edu
594484Sbinkertn@umich.edu    if (objFile->getArch() != ObjectFile::Mips)
604484Sbinkertn@umich.edu        fatal("Object file does not match MIPS architecture.");
614484Sbinkertn@umich.edu
624484Sbinkertn@umich.edu    switch (objFile->getOpSys()) {
634484Sbinkertn@umich.edu      case ObjectFile::Linux:
644484Sbinkertn@umich.edu        process = new MipsLinuxProcess(nm, objFile, system,
654484Sbinkertn@umich.edu                                        stdin_fd, stdout_fd, stderr_fd,
664484Sbinkertn@umich.edu                                        argv, envp);
674484Sbinkertn@umich.edu        break;
684484Sbinkertn@umich.edu
694484Sbinkertn@umich.edu      default:
704484Sbinkertn@umich.edu        fatal("Unknown/unsupported operating system.");
714484Sbinkertn@umich.edu    }
724484Sbinkertn@umich.edu
734484Sbinkertn@umich.edu    if (process == NULL)
744484Sbinkertn@umich.edu        fatal("Unknown error creating process object.");
754484Sbinkertn@umich.edu    return process;
764484Sbinkertn@umich.edu}
774484Sbinkertn@umich.edu
784484Sbinkertn@umich.eduMipsLiveProcess::MipsLiveProcess(const std::string &nm, ObjectFile *objFile,
794484Sbinkertn@umich.edu        System *_system, int stdin_fd, int stdout_fd, int stderr_fd,
804484Sbinkertn@umich.edu        std::vector<std::string> &argv, std::vector<std::string> &envp)
814484Sbinkertn@umich.edu    : LiveProcess(nm, objFile, _system, stdin_fd, stdout_fd, stderr_fd,
824484Sbinkertn@umich.edu        argv, envp)
834484Sbinkertn@umich.edu{
844484Sbinkertn@umich.edu    // Set up stack. On MIPS, stack starts at the top of kuseg
854484Sbinkertn@umich.edu    // user address space. MIPS stack grows down from here
864484Sbinkertn@umich.edu    stack_base = 0x7FFFFFFF;
874484Sbinkertn@umich.edu
884484Sbinkertn@umich.edu    // Set pointer for next thread stack.  Reserve 8M for main stack.
894484Sbinkertn@umich.edu    next_thread_stack_base = stack_base - (8 * 1024 * 1024);
90
91    // Set up break point (Top of Heap)
92    brk_point = objFile->dataBase() + objFile->dataSize() + objFile->bssSize();
93    brk_point = roundUp(brk_point, VMPageSize);
94
95    // Set up region for mmaps. For now, start at bottom of kuseg space.
96    mmap_start = mmap_end = 0x10000;
97}
98
99void
100MipsLiveProcess::startup()
101{
102    argsInit(MachineBytes, VMPageSize);
103}
104
105
106
107
108BEGIN_DECLARE_SIM_OBJECT_PARAMS(MipsLiveProcess)
109
110    VectorParam<string> cmd;
111    Param<string> executable;
112    Param<string> input;
113    Param<string> output;
114    VectorParam<string> env;
115    SimObjectParam<System *> system;
116
117END_DECLARE_SIM_OBJECT_PARAMS(MipsLiveProcess)
118
119
120BEGIN_INIT_SIM_OBJECT_PARAMS(MipsLiveProcess)
121
122    INIT_PARAM(cmd, "command line (executable plus arguments)"),
123    INIT_PARAM(executable, "executable (overrides cmd[0] if set)"),
124    INIT_PARAM(input, "filename for stdin (dflt: use sim stdin)"),
125    INIT_PARAM(output, "filename for stdout/stderr (dflt: use sim stdout)"),
126    INIT_PARAM(env, "environment settings"),
127    INIT_PARAM(system, "system")
128
129END_INIT_SIM_OBJECT_PARAMS(MipsLiveProcess)
130
131
132CREATE_SIM_OBJECT(MipsLiveProcess)
133{
134    string in = input;
135    string out = output;
136
137    // initialize file descriptors to default: same as simulator
138    int stdin_fd, stdout_fd, stderr_fd;
139
140    if (in == "stdin" || in == "cin")
141        stdin_fd = STDIN_FILENO;
142    else
143        stdin_fd = Process::openInputFile(input);
144
145    if (out == "stdout" || out == "cout")
146        stdout_fd = STDOUT_FILENO;
147    else if (out == "stderr" || out == "cerr")
148        stdout_fd = STDERR_FILENO;
149    else
150        stdout_fd = Process::openOutputFile(out);
151
152    stderr_fd = (stdout_fd != STDOUT_FILENO) ? stdout_fd : STDERR_FILENO;
153
154    return MipsLiveProcess::create(getInstanceName(), system,
155                               stdin_fd, stdout_fd, stderr_fd,
156                               (string)executable == "" ? cmd[0] : executable,
157                               cmd, env);
158}
159
160
161REGISTER_SIM_OBJECT("MipsLiveProcess", MipsLiveProcess)
162
163
164