process.cc revision 2665:a124942bacb8
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 * Authors: Gabe Black
29 *          Ali Saidi
30 */
31
32#include "arch/sparc/isa_traits.hh"
33#include "arch/sparc/process.hh"
34#include "arch/sparc/linux/process.hh"
35#include "arch/sparc/solaris/process.hh"
36#include "base/loader/object_file.hh"
37#include "base/misc.hh"
38#include "cpu/exec_context.hh"
39#include "mem/page_table.hh"
40#include "mem/translating_port.hh"
41#include "sim/builder.hh"
42#include "sim/system.hh"
43
44using namespace std;
45using namespace SparcISA;
46
47SparcLiveProcess *
48SparcLiveProcess::create(const std::string &nm, System *system, int stdin_fd,
49        int stdout_fd, int stderr_fd, std::string executable,
50        std::vector<std::string> &argv, std::vector<std::string> &envp)
51{
52    SparcLiveProcess *process = NULL;
53
54    ObjectFile *objFile = createObjectFile(executable);
55    if (objFile == NULL) {
56        fatal("Can't load object file %s", executable);
57    }
58
59
60    if (objFile->getArch() != ObjectFile::SPARC)
61        fatal("Object file with arch %x does not match architecture %x.",
62                objFile->getArch(), ObjectFile::SPARC);
63    switch (objFile->getOpSys()) {
64      case ObjectFile::Linux:
65        process = new SparcLinuxProcess(nm, objFile, system,
66                                        stdin_fd, stdout_fd, stderr_fd,
67                                        argv, envp);
68        break;
69
70
71      case ObjectFile::Solaris:
72        process = new SparcSolarisProcess(nm, objFile, system,
73                                        stdin_fd, stdout_fd, stderr_fd,
74                                        argv, envp);
75        break;
76      default:
77        fatal("Unknown/unsupported operating system.");
78    }
79
80    if (process == NULL)
81        fatal("Unknown error creating process object.");
82    return process;
83}
84
85SparcLiveProcess::SparcLiveProcess(const std::string &nm, ObjectFile *objFile,
86        System *_system, int stdin_fd, int stdout_fd, int stderr_fd,
87        std::vector<std::string> &argv, std::vector<std::string> &envp)
88    : LiveProcess(nm, objFile, _system, stdin_fd, stdout_fd, stderr_fd,
89        argv, envp)
90{
91
92    // XXX all the below need to be updated for SPARC - Ali
93    brk_point = objFile->dataBase() + objFile->dataSize() + objFile->bssSize();
94    brk_point = roundUp(brk_point, VMPageSize);
95
96    // Set up stack. On SPARC Linux, stack goes from the top of memory
97    // downward, less the hole for the kernel address space.
98    stack_base = ((Addr)0x80000000000ULL);
99
100    // Set up region for mmaps.  Tru64 seems to start just above 0 and
101    // grow up from there.
102    mmap_start = mmap_end = 0x800000;
103
104    // Set pointer for next thread stack.  Reserve 8M for main stack.
105    next_thread_stack_base = stack_base - (8 * 1024 * 1024);
106}
107
108void
109SparcLiveProcess::startup()
110{
111    argsInit(MachineBytes, VMPageSize);
112
113    //From the SPARC ABI
114
115    //The process runs in user mode
116    execContexts[0]->setMiscRegWithEffect(MISCREG_PSTATE, 0x02);
117
118    //Setup default FP state
119    execContexts[0]->setMiscReg(MISCREG_FSR, 0);
120
121    execContexts[0]->setMiscReg(MISCREG_TICK, 0);
122    //
123    /*
124     * Register window management registers
125     */
126
127    //No windows contain info from other programs
128    execContexts[0]->setMiscRegWithEffect(MISCREG_OTHERWIN, 0);
129    //There are no windows to pop
130    execContexts[0]->setMiscRegWithEffect(MISCREG_CANRESTORE, 0);
131    //All windows are available to save into
132    execContexts[0]->setMiscRegWithEffect(MISCREG_CANSAVE, NWindows - 2);
133    //All windows are "clean"
134    execContexts[0]->setMiscRegWithEffect(MISCREG_CLEANWIN, NWindows);
135    //Start with register window 0
136    execContexts[0]->setMiscRegWithEffect(MISCREG_CWP, 0);
137}
138
139m5_auxv_t buildAuxVect(int64_t type, int64_t val)
140{
141    m5_auxv_t result;
142    result.a_type = TheISA::htog(type);
143    result.a_val = TheISA::htog(val);
144    return result;
145}
146
147void
148SparcLiveProcess::argsInit(int intSize, int pageSize)
149{
150    Process::startup();
151
152    Addr alignmentMask = ~(intSize - 1);
153
154    // load object file into target memory
155    objFile->loadSections(initVirtMem);
156
157    //These are the auxilliary vector types
158    enum auxTypes
159    {
160        SPARC_AT_HWCAP = 16,
161        SPARC_AT_PAGESZ = 6,
162        SPARC_AT_CLKTCK = 17,
163        SPARC_AT_PHDR = 3,
164        SPARC_AT_PHENT = 4,
165        SPARC_AT_PHNUM = 5,
166        SPARC_AT_BASE = 7,
167        SPARC_AT_FLAGS = 8,
168        SPARC_AT_ENTRY = 9,
169        SPARC_AT_UID = 11,
170        SPARC_AT_EUID = 12,
171        SPARC_AT_GID = 13,
172        SPARC_AT_EGID = 14
173    };
174
175    enum hardwareCaps
176    {
177        M5_HWCAP_SPARC_FLUSH = 1,
178        M5_HWCAP_SPARC_STBAR = 2,
179        M5_HWCAP_SPARC_SWAP = 4,
180        M5_HWCAP_SPARC_MULDIV = 8,
181        M5_HWCAP_SPARC_V9 = 16,
182        //This one should technically only be set
183        //if there is a cheetah or cheetah_plus tlb,
184        //but we'll use it all the time
185        M5_HWCAP_SPARC_ULTRA3 = 32
186    };
187
188    const int64_t hwcap =
189        M5_HWCAP_SPARC_FLUSH |
190        M5_HWCAP_SPARC_STBAR |
191        M5_HWCAP_SPARC_SWAP |
192        M5_HWCAP_SPARC_MULDIV |
193        M5_HWCAP_SPARC_V9 |
194        M5_HWCAP_SPARC_ULTRA3;
195
196    //Setup the auxilliary vectors. These will already have
197    //endian conversion.
198    auxv.push_back(buildAuxVect(SPARC_AT_EGID, 100));
199    auxv.push_back(buildAuxVect(SPARC_AT_GID, 100));
200    auxv.push_back(buildAuxVect(SPARC_AT_EUID, 100));
201    auxv.push_back(buildAuxVect(SPARC_AT_UID, 100));
202    //This would work, but the entry point is a protected member
203    //auxv.push_back(buildAuxVect(SPARC_AT_ENTRY, objFile->entry));
204    auxv.push_back(buildAuxVect(SPARC_AT_FLAGS, 0));
205    //This is the address of the elf "interpreter", which I don't
206    //think we currently set up. It should be set to 0 (I think)
207    //auxv.push_back(buildAuxVect(SPARC_AT_BASE, 0));
208    //This is the number of headers which were in the original elf
209    //file. This information isn't avaibale by this point.
210    //auxv.push_back(buildAuxVect(SPARC_AT_PHNUM, 3));
211    //This is the size of a program header entry. This isn't easy
212    //to compute here.
213    //auxv.push_back(buildAuxVect(SPARC_AT_PHENT, blah));
214    //This is should be set to load_addr (whatever that is) +
215    //e_phoff. I think it's a pointer to the program headers.
216    //auxv.push_back(buildAuxVect(SPARC_AT_PHDR, blah));
217    //This should be easy to get right, but I won't set it for now
218    //auxv.push_back(buildAuxVect(SPARC_AT_CLKTCK, blah));
219    auxv.push_back(buildAuxVect(SPARC_AT_PAGESZ, SparcISA::VMPageSize));
220    auxv.push_back(buildAuxVect(SPARC_AT_HWCAP, hwcap));
221
222    //Figure out how big the initial stack needs to be
223
224    //Each auxilliary vector is two 8 byte words
225    int aux_data_size = 2 * intSize * auxv.size();
226    int env_data_size = 0;
227    for (int i = 0; i < envp.size(); ++i) {
228        env_data_size += envp[i].size() + 1;
229    }
230    int arg_data_size = 0;
231    for (int i = 0; i < argv.size(); ++i) {
232        arg_data_size += argv[i].size() + 1;
233    }
234
235    int aux_array_size = intSize * 2 * (auxv.size() + 1);
236
237    int argv_array_size = intSize * (argv.size() + 1);
238    int envp_array_size = intSize * (envp.size() + 1);
239
240    int argc_size = intSize;
241    int window_save_size = intSize * 16;
242
243    int info_block_size =
244        (aux_data_size +
245        env_data_size +
246        arg_data_size +
247        ~alignmentMask) & alignmentMask;
248
249    int info_block_padding =
250        info_block_size -
251        aux_data_size -
252        env_data_size -
253        arg_data_size;
254
255    int space_needed =
256        info_block_size +
257        aux_array_size +
258        envp_array_size +
259        argv_array_size +
260        argc_size +
261        window_save_size;
262
263    stack_min = stack_base - space_needed;
264    stack_min &= alignmentMask;
265    stack_size = stack_base - stack_min;
266
267    // map memory
268    pTable->allocate(roundDown(stack_min, pageSize),
269                     roundUp(stack_size, pageSize));
270
271    // map out initial stack contents
272    Addr aux_data_base = stack_base - aux_data_size - info_block_padding;
273    Addr env_data_base = aux_data_base - env_data_size;
274    Addr arg_data_base = env_data_base - arg_data_size;
275    Addr auxv_array_base = arg_data_base - aux_array_size;
276    Addr envp_array_base = auxv_array_base - envp_array_size;
277    Addr argv_array_base = envp_array_base - argv_array_size;
278    Addr argc_base = argv_array_base - argc_size;
279    Addr window_save_base = argc_base - window_save_size;
280
281    DPRINTF(Sparc, "The addresses of items on the initial stack:\n");
282    DPRINTF(Sparc, "0x%x - aux data\n", aux_data_base);
283    DPRINTF(Sparc, "0x%x - env data\n", env_data_base);
284    DPRINTF(Sparc, "0x%x - arg data\n", arg_data_base);
285    DPRINTF(Sparc, "0x%x - auxv array\n", auxv_array_base);
286    DPRINTF(Sparc, "0x%x - envp array\n", envp_array_base);
287    DPRINTF(Sparc, "0x%x - argv array\n", argv_array_base);
288    DPRINTF(Sparc, "0x%x - argc \n", argc_base);
289    DPRINTF(Sparc, "0x%x - window save\n", window_save_base);
290    DPRINTF(Sparc, "0x%x - stack min\n", stack_min);
291
292    // write contents to stack
293    uint64_t argc = argv.size();
294    uint64_t guestArgc = TheISA::htog(argc);
295
296    //Copy the aux stuff
297    for(int x = 0; x < auxv.size(); x++)
298    {
299        initVirtMem->writeBlob(auxv_array_base + x * 2 * intSize,
300                (uint8_t*)&(auxv[x].a_type), intSize);
301        initVirtMem->writeBlob(auxv_array_base + (x * 2 + 1) * intSize,
302                (uint8_t*)&(auxv[x].a_val), intSize);
303    }
304    //Write out the terminating zeroed auxilliary vector
305    const uint64_t zero = 0;
306    initVirtMem->writeBlob(auxv_array_base + 2 * intSize * auxv.size(),
307            (uint8_t*)&zero, 2 * intSize);
308
309    copyStringArray(envp, envp_array_base, env_data_base, initVirtMem);
310    copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem);
311
312    initVirtMem->writeBlob(argc_base, (uint8_t*)&guestArgc, intSize);
313
314    execContexts[0]->setIntReg(ArgumentReg0, argc);
315    execContexts[0]->setIntReg(ArgumentReg1, argv_array_base);
316    execContexts[0]->setIntReg(StackPointerReg, stack_min - StackBias);
317
318    Addr prog_entry = objFile->entryPoint();
319    execContexts[0]->setPC(prog_entry);
320    execContexts[0]->setNextPC(prog_entry + sizeof(MachInst));
321    execContexts[0]->setNextNPC(prog_entry + (2 * sizeof(MachInst)));
322
323//    num_processes++;
324}
325
326
327BEGIN_DECLARE_SIM_OBJECT_PARAMS(SparcLiveProcess)
328
329    VectorParam<string> cmd;
330    Param<string> executable;
331    Param<string> input;
332    Param<string> output;
333    VectorParam<string> env;
334    SimObjectParam<System *> system;
335
336END_DECLARE_SIM_OBJECT_PARAMS(SparcLiveProcess)
337
338
339BEGIN_INIT_SIM_OBJECT_PARAMS(SparcLiveProcess)
340
341    INIT_PARAM(cmd, "command line (executable plus arguments)"),
342    INIT_PARAM(executable, "executable (overrides cmd[0] if set)"),
343    INIT_PARAM(input, "filename for stdin (dflt: use sim stdin)"),
344    INIT_PARAM(output, "filename for stdout/stderr (dflt: use sim stdout)"),
345    INIT_PARAM(env, "environment settings"),
346    INIT_PARAM(system, "system")
347
348END_INIT_SIM_OBJECT_PARAMS(SparcLiveProcess)
349
350
351CREATE_SIM_OBJECT(SparcLiveProcess)
352{
353    string in = input;
354    string out = output;
355
356    // initialize file descriptors to default: same as simulator
357    int stdin_fd, stdout_fd, stderr_fd;
358
359    if (in == "stdin" || in == "cin")
360        stdin_fd = STDIN_FILENO;
361    else
362        stdin_fd = Process::openInputFile(input);
363
364    if (out == "stdout" || out == "cout")
365        stdout_fd = STDOUT_FILENO;
366    else if (out == "stderr" || out == "cerr")
367        stdout_fd = STDERR_FILENO;
368    else
369        stdout_fd = Process::openOutputFile(out);
370
371    stderr_fd = (stdout_fd != STDOUT_FILENO) ? stdout_fd : STDERR_FILENO;
372
373    return SparcLiveProcess::create(getInstanceName(), system,
374                               stdin_fd, stdout_fd, stderr_fd,
375                               (string)executable == "" ? cmd[0] : executable,
376                               cmd, env);
377}
378
379
380REGISTER_SIM_OBJECT("SparcLiveProcess", SparcLiveProcess)
381
382
383