process.cc revision 3589:2fec1358ce80
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/asi.hh"
33#include "arch/sparc/isa_traits.hh"
34#include "arch/sparc/process.hh"
35#include "base/loader/object_file.hh"
36#include "base/loader/elf_object.hh"
37#include "base/misc.hh"
38#include "cpu/thread_context.hh"
39#include "mem/page_table.hh"
40#include "mem/translating_port.hh"
41#include "sim/system.hh"
42
43using namespace std;
44using namespace SparcISA;
45
46
47SparcLiveProcess::SparcLiveProcess(const std::string &nm, ObjectFile *objFile,
48        System *_system, int stdin_fd, int stdout_fd, int stderr_fd,
49        std::vector<std::string> &argv, std::vector<std::string> &envp,
50        uint64_t _uid, uint64_t _euid, uint64_t _gid, uint64_t _egid,
51        uint64_t _pid, uint64_t _ppid)
52    : LiveProcess(nm, objFile, _system, stdin_fd, stdout_fd, stderr_fd,
53        argv, envp, _uid, _euid, _gid, _egid, _pid, _ppid)
54{
55
56    // XXX all the below need to be updated for SPARC - Ali
57    brk_point = objFile->dataBase() + objFile->dataSize() + objFile->bssSize();
58    brk_point = roundUp(brk_point, VMPageSize);
59
60    // Set up stack. On SPARC Linux, stack goes from the top of memory
61    // downward, less the hole for the kernel address space.
62    stack_base = (Addr)0x80000000000ULL;
63
64    // Set up region for mmaps.  Tru64 seems to start just above 0 and
65    // grow up from there.
66    mmap_start = mmap_end = 0xfffff80000000000ULL;
67
68    // Set pointer for next thread stack.  Reserve 8M for main stack.
69    next_thread_stack_base = stack_base - (8 * 1024 * 1024);
70
71    //Initialize these to 0s
72    fillStart = 0;
73    spillStart = 0;
74}
75
76void
77SparcLiveProcess::startup()
78{
79    argsInit(MachineBytes, VMPageSize);
80
81    //From the SPARC ABI
82
83    //The process runs in user mode
84    threadContexts[0]->setMiscRegWithEffect(MISCREG_PSTATE, 0x02);
85
86    //Setup default FP state
87    threadContexts[0]->setMiscReg(MISCREG_FSR, 0);
88
89    threadContexts[0]->setMiscReg(MISCREG_TICK, 0);
90    //
91    /*
92     * Register window management registers
93     */
94
95    //No windows contain info from other programs
96    threadContexts[0]->setMiscReg(MISCREG_OTHERWIN, 0);
97    //There are no windows to pop
98    threadContexts[0]->setMiscReg(MISCREG_CANRESTORE, 0);
99    //All windows are available to save into
100    threadContexts[0]->setMiscReg(MISCREG_CANSAVE, NWindows - 2);
101    //All windows are "clean"
102    threadContexts[0]->setMiscReg(MISCREG_CLEANWIN, NWindows);
103    //Start with register window 0
104    threadContexts[0]->setMiscReg(MISCREG_CWP, 0);
105    //Always use spill and fill traps 0
106    threadContexts[0]->setMiscReg(MISCREG_WSTATE, 0);
107    //Set the trap level to 0
108    threadContexts[0]->setMiscReg(MISCREG_TL, 0);
109    //Set the ASI register to something fixed
110    threadContexts[0]->setMiscReg(MISCREG_ASI, ASI_PRIMARY);
111}
112
113m5_auxv_t buildAuxVect(int64_t type, int64_t val)
114{
115    m5_auxv_t result;
116    result.a_type = TheISA::htog(type);
117    result.a_val = TheISA::htog(val);
118    return result;
119}
120
121//We only use 19 instructions for the trap handlers, but there would be
122//space for 32 in a real SPARC trap table.
123const int numFillInsts = 32;
124const int numSpillInsts = 32;
125
126MachInst fillHandler[numFillInsts] =
127{
128    htog(0x87802018), //wr %g0, ASI_AIUP, %asi
129    htog(0xe0dba7ff), //ldxa [%sp + BIAS + (0*8)] %asi, %l0
130    htog(0xe2dba807), //ldxa [%sp + BIAS + (1*8)] %asi, %l1
131    htog(0xe4dba80f), //ldxa [%sp + BIAS + (2*8)] %asi, %l2
132    htog(0xe6dba817), //ldxa [%sp + BIAS + (3*8)] %asi, %l3
133    htog(0xe8dba81f), //ldxa [%sp + BIAS + (4*8)] %asi, %l4
134    htog(0xeadba827), //ldxa [%sp + BIAS + (5*8)] %asi, %l5
135    htog(0xecdba82f), //ldxa [%sp + BIAS + (6*8)] %asi, %l6
136    htog(0xeedba837), //ldxa [%sp + BIAS + (7*8)] %asi, %l7
137    htog(0xf0dba83f), //ldxa [%sp + BIAS + (8*8)] %asi, %i0
138    htog(0xf2dba847), //ldxa [%sp + BIAS + (9*8)] %asi, %i1
139    htog(0xf4dba84f), //ldxa [%sp + BIAS + (10*8)] %asi, %i2
140    htog(0xf6dba857), //ldxa [%sp + BIAS + (11*8)] %asi, %i3
141    htog(0xf8dba85f), //ldxa [%sp + BIAS + (12*8)] %asi, %i4
142    htog(0xfadba867), //ldxa [%sp + BIAS + (13*8)] %asi, %i5
143    htog(0xfcdba86f), //ldxa [%sp + BIAS + (14*8)] %asi, %i6
144    htog(0xfedba877), //ldxa [%sp + BIAS + (15*8)] %asi, %i7
145    htog(0x83880000), //restored
146    htog(0x83F00000), //retry
147    htog(0x00000000), //illtrap
148    htog(0x00000000), //illtrap
149    htog(0x00000000), //illtrap
150    htog(0x00000000), //illtrap
151    htog(0x00000000), //illtrap
152    htog(0x00000000), //illtrap
153    htog(0x00000000), //illtrap
154    htog(0x00000000), //illtrap
155    htog(0x00000000), //illtrap
156    htog(0x00000000), //illtrap
157    htog(0x00000000), //illtrap
158    htog(0x00000000), //illtrap
159    htog(0x00000000)  //illtrap
160};
161
162MachInst spillHandler[numSpillInsts] =
163{
164    htog(0x87802018), //wr %g0, ASI_AIUP, %asi
165    htog(0xe0f3a7ff), //stxa %l0, [%sp + BIAS + (0*8)] %asi
166    htog(0xe2f3a807), //stxa %l1, [%sp + BIAS + (1*8)] %asi
167    htog(0xe4f3a80f), //stxa %l2, [%sp + BIAS + (2*8)] %asi
168    htog(0xe6f3a817), //stxa %l3, [%sp + BIAS + (3*8)] %asi
169    htog(0xe8f3a81f), //stxa %l4, [%sp + BIAS + (4*8)] %asi
170    htog(0xeaf3a827), //stxa %l5, [%sp + BIAS + (5*8)] %asi
171    htog(0xecf3a82f), //stxa %l6, [%sp + BIAS + (6*8)] %asi
172    htog(0xeef3a837), //stxa %l7, [%sp + BIAS + (7*8)] %asi
173    htog(0xf0f3a83f), //stxa %i0, [%sp + BIAS + (8*8)] %asi
174    htog(0xf2f3a847), //stxa %i1, [%sp + BIAS + (9*8)] %asi
175    htog(0xf4f3a84f), //stxa %i2, [%sp + BIAS + (10*8)] %asi
176    htog(0xf6f3a857), //stxa %i3, [%sp + BIAS + (11*8)] %asi
177    htog(0xf8f3a85f), //stxa %i4, [%sp + BIAS + (12*8)] %asi
178    htog(0xfaf3a867), //stxa %i5, [%sp + BIAS + (13*8)] %asi
179    htog(0xfcf3a86f), //stxa %i6, [%sp + BIAS + (14*8)] %asi
180    htog(0xfef3a877), //stxa %i7, [%sp + BIAS + (15*8)] %asi
181    htog(0x81880000), //saved
182    htog(0x83F00000), //retry
183    htog(0x00000000), //illtrap
184    htog(0x00000000), //illtrap
185    htog(0x00000000), //illtrap
186    htog(0x00000000), //illtrap
187    htog(0x00000000), //illtrap
188    htog(0x00000000), //illtrap
189    htog(0x00000000), //illtrap
190    htog(0x00000000), //illtrap
191    htog(0x00000000), //illtrap
192    htog(0x00000000), //illtrap
193    htog(0x00000000), //illtrap
194    htog(0x00000000), //illtrap
195    htog(0x00000000)  //illtrap
196};
197
198void
199SparcLiveProcess::argsInit(int intSize, int pageSize)
200{
201    Process::startup();
202
203    string filename;
204    if(argv.size() < 1)
205        filename = "";
206    else
207        filename = argv[0];
208
209    Addr alignmentMask = ~(intSize - 1);
210
211    // load object file into target memory
212    objFile->loadSections(initVirtMem);
213
214    //These are the auxilliary vector types
215    enum auxTypes
216    {
217        SPARC_AT_HWCAP = 16,
218        SPARC_AT_PAGESZ = 6,
219        SPARC_AT_CLKTCK = 17,
220        SPARC_AT_PHDR = 3,
221        SPARC_AT_PHENT = 4,
222        SPARC_AT_PHNUM = 5,
223        SPARC_AT_BASE = 7,
224        SPARC_AT_FLAGS = 8,
225        SPARC_AT_ENTRY = 9,
226        SPARC_AT_UID = 11,
227        SPARC_AT_EUID = 12,
228        SPARC_AT_GID = 13,
229        SPARC_AT_EGID = 14,
230        SPARC_AT_SECURE = 23
231    };
232
233    enum hardwareCaps
234    {
235        M5_HWCAP_SPARC_FLUSH = 1,
236        M5_HWCAP_SPARC_STBAR = 2,
237        M5_HWCAP_SPARC_SWAP = 4,
238        M5_HWCAP_SPARC_MULDIV = 8,
239        M5_HWCAP_SPARC_V9 = 16,
240        //This one should technically only be set
241        //if there is a cheetah or cheetah_plus tlb,
242        //but we'll use it all the time
243        M5_HWCAP_SPARC_ULTRA3 = 32
244    };
245
246    const int64_t hwcap =
247        M5_HWCAP_SPARC_FLUSH |
248        M5_HWCAP_SPARC_STBAR |
249        M5_HWCAP_SPARC_SWAP |
250        M5_HWCAP_SPARC_MULDIV |
251        M5_HWCAP_SPARC_V9 |
252        M5_HWCAP_SPARC_ULTRA3;
253
254
255    //Setup the auxilliary vectors. These will already have endian conversion.
256    //Auxilliary vectors are loaded only for elf formatted executables.
257    ElfObject * elfObject = dynamic_cast<ElfObject *>(objFile);
258    if(elfObject)
259    {
260        //Bits which describe the system hardware capabilities
261        auxv.push_back(buildAuxVect(SPARC_AT_HWCAP, hwcap));
262        //The system page size
263        auxv.push_back(buildAuxVect(SPARC_AT_PAGESZ, SparcISA::VMPageSize));
264        //Defined to be 100 in the kernel source.
265        //Frequency at which times() increments
266        auxv.push_back(buildAuxVect(SPARC_AT_CLKTCK, 100));
267        // For statically linked executables, this is the virtual address of the
268        // program header tables if they appear in the executable image
269        auxv.push_back(buildAuxVect(SPARC_AT_PHDR, elfObject->programHeaderTable()));
270        // This is the size of a program header entry from the elf file.
271        auxv.push_back(buildAuxVect(SPARC_AT_PHENT, elfObject->programHeaderSize()));
272        // This is the number of program headers from the original elf file.
273        auxv.push_back(buildAuxVect(SPARC_AT_PHNUM, elfObject->programHeaderCount()));
274        //This is the address of the elf "interpreter", It should be set
275        //to 0 for regular executables. It should be something else
276        //(not sure what) for dynamic libraries.
277        auxv.push_back(buildAuxVect(SPARC_AT_BASE, 0));
278        //This is hardwired to 0 in the elf loading code in the kernel
279        auxv.push_back(buildAuxVect(SPARC_AT_FLAGS, 0));
280        //The entry point to the program
281        auxv.push_back(buildAuxVect(SPARC_AT_ENTRY, objFile->entryPoint()));
282        //Different user and group IDs
283        auxv.push_back(buildAuxVect(SPARC_AT_UID, uid()));
284        auxv.push_back(buildAuxVect(SPARC_AT_EUID, euid()));
285        auxv.push_back(buildAuxVect(SPARC_AT_GID, gid()));
286        auxv.push_back(buildAuxVect(SPARC_AT_EGID, egid()));
287        //Whether to enable "secure mode" in the executable
288        auxv.push_back(buildAuxVect(SPARC_AT_SECURE, 0));
289    }
290
291    //Figure out how big the initial stack needs to be
292
293    // The unaccounted for 0 at the top of the stack
294    int mysterious_size = intSize;
295
296    //This is the name of the file which is present on the initial stack
297    //It's purpose is to let the user space linker examine the original file.
298    int file_name_size = filename.size() + 1;
299
300    int env_data_size = 0;
301    for (int i = 0; i < envp.size(); ++i) {
302        env_data_size += envp[i].size() + 1;
303    }
304    int arg_data_size = 0;
305    for (int i = 0; i < argv.size(); ++i) {
306        arg_data_size += argv[i].size() + 1;
307    }
308
309    //The info_block needs to be padded so it's size is a multiple of the
310    //alignment mask. Also, it appears that there needs to be at least some
311    //padding, so if the size is already a multiple, we need to increase it
312    //anyway.
313    int info_block_size =
314        (file_name_size +
315        env_data_size +
316        arg_data_size +
317        intSize) & alignmentMask;
318
319    int info_block_padding =
320        info_block_size -
321        file_name_size -
322        env_data_size -
323        arg_data_size;
324
325    //Each auxilliary vector is two 8 byte words
326    int aux_array_size = intSize * 2 * (auxv.size() + 1);
327
328    int envp_array_size = intSize * (envp.size() + 1);
329    int argv_array_size = intSize * (argv.size() + 1);
330
331    int argc_size = intSize;
332    int window_save_size = intSize * 16;
333
334    int space_needed =
335        mysterious_size +
336        info_block_size +
337        aux_array_size +
338        envp_array_size +
339        argv_array_size +
340        argc_size +
341        window_save_size;
342
343    stack_min = stack_base - space_needed;
344    stack_min &= alignmentMask;
345    stack_size = stack_base - stack_min;
346
347    // map memory
348    pTable->allocate(roundDown(stack_min, pageSize),
349                     roundUp(stack_size, pageSize));
350
351    // map out initial stack contents
352    Addr mysterious_base = stack_base - mysterious_size;
353    Addr file_name_base = mysterious_base - file_name_size;
354    Addr env_data_base = file_name_base - env_data_size;
355    Addr arg_data_base = env_data_base - arg_data_size;
356    Addr auxv_array_base = arg_data_base - aux_array_size - info_block_padding;
357    Addr envp_array_base = auxv_array_base - envp_array_size;
358    Addr argv_array_base = envp_array_base - argv_array_size;
359    Addr argc_base = argv_array_base - argc_size;
360#ifndef NDEBUG
361    // only used in DPRINTF
362    Addr window_save_base = argc_base - window_save_size;
363#endif
364
365    DPRINTF(Sparc, "The addresses of items on the initial stack:\n");
366    DPRINTF(Sparc, "0x%x - file name\n", file_name_base);
367    DPRINTF(Sparc, "0x%x - env data\n", env_data_base);
368    DPRINTF(Sparc, "0x%x - arg data\n", arg_data_base);
369    DPRINTF(Sparc, "0x%x - auxv array\n", auxv_array_base);
370    DPRINTF(Sparc, "0x%x - envp array\n", envp_array_base);
371    DPRINTF(Sparc, "0x%x - argv array\n", argv_array_base);
372    DPRINTF(Sparc, "0x%x - argc \n", argc_base);
373    DPRINTF(Sparc, "0x%x - window save\n", window_save_base);
374    DPRINTF(Sparc, "0x%x - stack min\n", stack_min);
375
376    // write contents to stack
377
378    // figure out argc
379    uint64_t argc = argv.size();
380    uint64_t guestArgc = TheISA::htog(argc);
381
382    //Write out the mysterious 0
383    uint64_t mysterious_zero = 0;
384    initVirtMem->writeBlob(mysterious_base,
385            (uint8_t*)&mysterious_zero, mysterious_size);
386
387    //Write the file name
388    initVirtMem->writeString(file_name_base, filename.c_str());
389
390    //Copy the aux stuff
391    for(int x = 0; x < auxv.size(); x++)
392    {
393        initVirtMem->writeBlob(auxv_array_base + x * 2 * intSize,
394                (uint8_t*)&(auxv[x].a_type), intSize);
395        initVirtMem->writeBlob(auxv_array_base + (x * 2 + 1) * intSize,
396                (uint8_t*)&(auxv[x].a_val), intSize);
397    }
398    //Write out the terminating zeroed auxilliary vector
399    const uint64_t zero = 0;
400    initVirtMem->writeBlob(auxv_array_base + 2 * intSize * auxv.size(),
401            (uint8_t*)&zero, 2 * intSize);
402
403    copyStringArray(envp, envp_array_base, env_data_base, initVirtMem);
404    copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem);
405
406    initVirtMem->writeBlob(argc_base, (uint8_t*)&guestArgc, intSize);
407
408    //Stuff the trap handlers into the processes address space.
409    //Since the stack grows down and is the highest area in the processes
410    //address space, we can put stuff above it and stay out of the way.
411    int fillSize = sizeof(MachInst) * numFillInsts;
412    int spillSize = sizeof(MachInst) * numSpillInsts;
413    fillStart = stack_base;
414    spillStart = fillStart + fillSize;
415    initVirtMem->writeBlob(fillStart, (uint8_t*)fillHandler, fillSize);
416    initVirtMem->writeBlob(spillStart, (uint8_t*)spillHandler, spillSize);
417
418    //Set up the thread context to start running the process
419    threadContexts[0]->setIntReg(ArgumentReg0, argc);
420    threadContexts[0]->setIntReg(ArgumentReg1, argv_array_base);
421    threadContexts[0]->setIntReg(StackPointerReg, stack_min - StackBias);
422
423    Addr prog_entry = objFile->entryPoint();
424    threadContexts[0]->setPC(prog_entry);
425    threadContexts[0]->setNextPC(prog_entry + sizeof(MachInst));
426    threadContexts[0]->setNextNPC(prog_entry + (2 * sizeof(MachInst)));
427
428//    num_processes++;
429}
430