process.cc revision 2561
1/*
2 * Copyright (c) 2003-2004 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 "arch/sparc/isa_traits.hh"
30#include "arch/sparc/process.hh"
31#include "arch/sparc/linux/process.hh"
32#include "base/loader/object_file.hh"
33#include "base/misc.hh"
34#include "cpu/exec_context.hh"
35#include "mem/page_table.hh"
36#include "mem/translating_port.hh"
37#include "sim/builder.hh"
38#include "sim/system.hh"
39
40using namespace std;
41using namespace SparcISA;
42
43SparcLiveProcess *
44SparcLiveProcess::create(const std::string &nm, System *system, int stdin_fd,
45        int stdout_fd, int stderr_fd, std::string executable,
46        std::vector<std::string> &argv, std::vector<std::string> &envp)
47{
48    SparcLiveProcess *process = NULL;
49
50    ObjectFile *objFile = createObjectFile(executable);
51    if (objFile == NULL) {
52        fatal("Can't load object file %s", executable);
53    }
54
55
56    if (objFile->getArch() != ObjectFile::SPARC)
57        fatal("Object file does not match architecture.");
58    switch (objFile->getOpSys()) {
59      case ObjectFile::Linux:
60        process = new SparcLinuxProcess(nm, objFile, system,
61                                        stdin_fd, stdout_fd, stderr_fd,
62                                        argv, envp);
63        break;
64
65      case ObjectFile::Solaris:
66      default:
67        fatal("Unknown/unsupported operating system.");
68    }
69
70    if (process == NULL)
71        fatal("Unknown error creating process object.");
72    return process;
73}
74
75SparcLiveProcess::SparcLiveProcess(const std::string &nm, ObjectFile *objFile,
76        System *_system, int stdin_fd, int stdout_fd, int stderr_fd,
77        std::vector<std::string> &argv, std::vector<std::string> &envp)
78    : LiveProcess(nm, objFile, _system, stdin_fd, stdout_fd, stderr_fd,
79        argv, envp)
80{
81
82    // XXX all the below need to be updated for SPARC - Ali
83    brk_point = objFile->dataBase() + objFile->dataSize() + objFile->bssSize();
84    brk_point = roundUp(brk_point, VMPageSize);
85
86    // Set up stack. On SPARC Linux, stack goes from the top of memory
87    // downward, less the hole for the kernel address space.
88    stack_base = ((Addr)0x80000000000);
89
90    // Set up region for mmaps.  Tru64 seems to start just above 0 and
91    // grow up from there.
92    mmap_start = mmap_end = 0x10000;
93
94    // Set pointer for next thread stack.  Reserve 8M for main stack.
95    next_thread_stack_base = stack_base - (8 * 1024 * 1024);
96}
97
98void
99SparcLiveProcess::startup()
100{
101    argsInit(MachineBytes, VMPageSize);
102
103    //From the SPARC ABI
104
105    //The process runs in user mode
106    execContexts[0]->setMiscRegWithEffect(MISCREG_PSTATE_PRIV, 0);
107    //Interrupts are enabled
108    execContexts[0]->setMiscRegWithEffect(MISCREG_PSTATE_IE, 1);
109    //Round to nearest
110    execContexts[0]->setMiscRegWithEffect(MISCREG_FSR_RD, 0);
111    //Floating point traps are not enabled
112    execContexts[0]->setMiscRegWithEffect(MISCREG_FSR_TEM, 0);
113    //Turn non standard mode off
114    execContexts[0]->setMiscRegWithEffect(MISCREG_FSR_NS, 0);
115    //The floating point queue is empty
116    execContexts[0]->setMiscRegWithEffect(MISCREG_FSR_QNE, 0);
117    //There are no accrued eexecContext[0]eptions
118    execContexts[0]->setMiscRegWithEffect(MISCREG_FSR_AEXC, 0);
119    //There are no current eexecContext[0]eptions
120    execContexts[0]->setMiscRegWithEffect(MISCREG_FSR_CEXC, 0);
121
122    /*
123     * Register window management registers
124     */
125
126    //No windows contain info from other programs
127    execContexts[0]->setMiscRegWithEffect(MISCREG_OTHERWIN, 0);
128    //There are no windows to pop
129    execContexts[0]->setMiscRegWithEffect(MISCREG_CANRESTORE, 0);
130    //All windows are available to save into
131    execContexts[0]->setMiscRegWithEffect(MISCREG_CANSAVE, NWindows - 2);
132    //All windows are "clean"
133    execContexts[0]->setMiscRegWithEffect(MISCREG_CLEANWIN, NWindows);
134    //Start with register window 0
135    execContexts[0]->setMiscRegWithEffect(MISCREG_CWP, 0);
136}
137
138void
139SparcLiveProcess::argsInit(int intSize, int pageSize)
140{
141    Process::startup();
142
143    Addr alignmentMask = ~(intSize - 1);
144
145    // load object file into target memory
146    objFile->loadSections(initVirtMem);
147
148    //Figure out how big the initial stack needs to be
149
150    int aux_data_size = 0;
151    //Figure out the aux_data_size?
152    int env_data_size = 0;
153    for (int i = 0; i < envp.size(); ++i) {
154        env_data_size += envp[i].size() + 1;
155    }
156    int arg_data_size = 0;
157    for (int i = 0; i < argv.size(); ++i) {
158        arg_data_size += argv[i].size() + 1;
159    }
160
161    int aux_array_size = intSize * 2 * (auxv.size() + 1);
162
163    int argv_array_size = intSize * (argv.size() + 1);
164    int envp_array_size = intSize * (envp.size() + 1);
165
166    int argc_size = intSize;
167    int window_save_size = intSize * 16;
168
169    int info_block_size =
170        (aux_data_size +
171        env_data_size +
172        arg_data_size +
173        ~alignmentMask) & alignmentMask;
174
175    int info_block_padding =
176        info_block_size -
177        aux_data_size -
178        env_data_size -
179        arg_data_size;
180
181    int space_needed =
182        info_block_size +
183        aux_array_size +
184        envp_array_size +
185        argv_array_size +
186        argc_size +
187        window_save_size;
188
189    stack_min = stack_base - space_needed;
190    stack_min &= alignmentMask;
191    stack_size = stack_base - stack_min;
192
193    // map memory
194    pTable->allocate(roundDown(stack_min, pageSize),
195                     roundUp(stack_size, pageSize));
196
197    // map out initial stack contents
198    Addr aux_data_base = stack_base - aux_data_size - info_block_padding;
199    Addr env_data_base = aux_data_base - env_data_size;
200    Addr arg_data_base = env_data_base - arg_data_size;
201    Addr aux_array_base = arg_data_base - aux_array_size;
202    Addr envp_array_base = aux_array_base - envp_array_size;
203    Addr argv_array_base = envp_array_base - argv_array_size;
204    Addr argc_base = argv_array_base - argc_size;
205    Addr window_save_base = argc_base - window_save_size;
206
207    DPRINTF(Sparc, "The addresses of items on the initial stack:\n");
208    DPRINTF(Sparc, "0x%x - aux data\n", aux_data_base);
209    DPRINTF(Sparc, "0x%x - env data\n", env_data_base);
210    DPRINTF(Sparc, "0x%x - arg data\n", arg_data_base);
211    DPRINTF(Sparc, "0x%x - aux array\n", aux_array_base);
212    DPRINTF(Sparc, "0x%x - env array\n", envp_array_base);
213    DPRINTF(Sparc, "0x%x - arg array\n", argv_array_base);
214    DPRINTF(Sparc, "0x%x - argc \n", argc_base);
215    DPRINTF(Sparc, "0x%x - window save\n", window_save_base);
216    DPRINTF(Sparc, "0x%x - stack min\n", stack_min);
217
218    // write contents to stack
219    uint64_t argc = argv.size();
220
221    //Copy the aux stuff? For now just put in the null vect
222    const uint64_t zero = 0;
223    initVirtMem->writeBlob(aux_array_base, (uint8_t*)&zero, 2 * intSize);
224
225    copyStringArray(envp, envp_array_base, env_data_base, initVirtMem);
226    copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem);
227
228    initVirtMem->writeBlob(argc_base, (uint8_t*)&argc, intSize);
229
230    execContexts[0]->setIntReg(ArgumentReg0, argc);
231    execContexts[0]->setIntReg(ArgumentReg1, argv_array_base);
232    execContexts[0]->setIntReg(StackPointerReg, stack_min - StackBias);
233
234    Addr prog_entry = objFile->entryPoint();
235    execContexts[0]->setPC(prog_entry);
236    execContexts[0]->setNextPC(prog_entry + sizeof(MachInst));
237    execContexts[0]->setNextNPC(prog_entry + (2 * sizeof(MachInst)));
238
239//    num_processes++;
240}
241
242
243BEGIN_DECLARE_SIM_OBJECT_PARAMS(SparcLiveProcess)
244
245    VectorParam<string> cmd;
246    Param<string> executable;
247    Param<string> input;
248    Param<string> output;
249    VectorParam<string> env;
250    SimObjectParam<System *> system;
251
252END_DECLARE_SIM_OBJECT_PARAMS(SparcLiveProcess)
253
254
255BEGIN_INIT_SIM_OBJECT_PARAMS(SparcLiveProcess)
256
257    INIT_PARAM(cmd, "command line (executable plus arguments)"),
258    INIT_PARAM(executable, "executable (overrides cmd[0] if set)"),
259    INIT_PARAM(input, "filename for stdin (dflt: use sim stdin)"),
260    INIT_PARAM(output, "filename for stdout/stderr (dflt: use sim stdout)"),
261    INIT_PARAM(env, "environment settings"),
262    INIT_PARAM(system, "system")
263
264END_INIT_SIM_OBJECT_PARAMS(SparcLiveProcess)
265
266
267CREATE_SIM_OBJECT(SparcLiveProcess)
268{
269    string in = input;
270    string out = output;
271
272    // initialize file descriptors to default: same as simulator
273    int stdin_fd, stdout_fd, stderr_fd;
274
275    if (in == "stdin" || in == "cin")
276        stdin_fd = STDIN_FILENO;
277    else
278        stdin_fd = Process::openInputFile(input);
279
280    if (out == "stdout" || out == "cout")
281        stdout_fd = STDOUT_FILENO;
282    else if (out == "stderr" || out == "cerr")
283        stdout_fd = STDERR_FILENO;
284    else
285        stdout_fd = Process::openOutputFile(out);
286
287    stderr_fd = (stdout_fd != STDOUT_FILENO) ? stdout_fd : STDERR_FILENO;
288
289    return SparcLiveProcess::create(getInstanceName(), system,
290                               stdin_fd, stdout_fd, stderr_fd,
291                               (string)executable == "" ? cmd[0] : executable,
292                               cmd, env);
293}
294
295
296REGISTER_SIM_OBJECT("SparcLiveProcess", SparcLiveProcess)
297
298
299