process.cc revision 6701:4842482e1bd1
1/*
2 * Copyright (c) 2007-2008 The Florida State University
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: Stephen Hines
29 */
30
31#include "arch/arm/isa_traits.hh"
32#include "arch/arm/process.hh"
33#include "arch/arm/types.hh"
34#include "base/loader/elf_object.hh"
35#include "base/loader/object_file.hh"
36#include "base/misc.hh"
37#include "cpu/thread_context.hh"
38#include "mem/page_table.hh"
39#include "mem/translating_port.hh"
40#include "sim/process_impl.hh"
41#include "sim/system.hh"
42
43using namespace std;
44using namespace ArmISA;
45
46ArmLiveProcess::ArmLiveProcess(LiveProcessParams *params, ObjectFile *objFile)
47    : LiveProcess(params, objFile)
48{
49    stack_base = 0xbf000000L;
50
51    // Set pointer for next thread stack.  Reserve 8M for main stack.
52    next_thread_stack_base = stack_base - (8 * 1024 * 1024);
53
54    // Set up break point (Top of Heap)
55    brk_point = objFile->dataBase() + objFile->dataSize() + objFile->bssSize();
56    brk_point = roundUp(brk_point, VMPageSize);
57
58    // Set up region for mmaps. For now, start at bottom of kuseg space.
59    mmap_start = mmap_end = 0x70000000L;
60}
61
62void
63ArmLiveProcess::startup()
64{
65    argsInit(MachineBytes, VMPageSize);
66}
67
68void
69ArmLiveProcess::copyStringArray32(std::vector<std::string> &strings,
70        Addr array_ptr, Addr data_ptr,
71        TranslatingPort* memPort)
72{
73    Addr data_ptr_swap;
74    for (int i = 0; i < strings.size(); ++i) {
75        data_ptr_swap = htog(data_ptr);
76        memPort->writeBlob(array_ptr, (uint8_t*)&data_ptr_swap,
77                sizeof(uint32_t));
78        memPort->writeString(data_ptr, strings[i].c_str());
79        array_ptr += sizeof(uint32_t);
80        data_ptr += strings[i].size() + 1;
81    }
82    // add NULL terminator
83    data_ptr = 0;
84
85    memPort->writeBlob(array_ptr, (uint8_t*)&data_ptr, sizeof(uint32_t));
86}
87
88void
89ArmLiveProcess::argsInit(int intSize, int pageSize)
90{
91    typedef AuxVector<uint32_t> auxv_t;
92    std::vector<auxv_t> auxv;
93
94    string filename;
95    if (argv.size() < 1)
96        filename = "";
97    else
98        filename = argv[0];
99
100    //We want 16 byte alignment
101    uint64_t align = 16;
102
103    // Overloaded argsInit so that we can fine-tune for ARM architecture
104    Process::startup();
105
106    // load object file into target memory
107    objFile->loadSections(initVirtMem);
108
109    enum ArmCpuFeature {
110        Arm_Swp = 1 << 0,
111        Arm_Half = 1 << 1,
112        Arm_Thumb = 1 << 2,
113        Arm_26Bit = 1 << 3,
114        Arm_FastMult = 1 << 4,
115        Arm_Fpa = 1 << 5,
116        Arm_Vfp = 1 << 6,
117        Arm_Edsp = 1 << 7,
118        Arm_Java = 1 << 8,
119        Arm_Iwmmxt = 1 << 9,
120        Arm_Crunch = 1 << 10
121    };
122
123    //Setup the auxilliary vectors. These will already have endian conversion.
124    //Auxilliary vectors are loaded only for elf formatted executables.
125    ElfObject * elfObject = dynamic_cast<ElfObject *>(objFile);
126    if (elfObject) {
127        uint32_t features =
128            Arm_Swp |
129            Arm_Half |
130            Arm_Thumb |
131//            Arm_26Bit |
132            Arm_FastMult |
133//            Arm_Fpa |
134            Arm_Vfp |
135            Arm_Edsp |
136            Arm_Java |
137//            Arm_Iwmmxt |
138//            Arm_Crunch |
139            0;
140
141        //Bits which describe the system hardware capabilities
142        //XXX Figure out what these should be
143        auxv.push_back(auxv_t(M5_AT_HWCAP, features));
144        //The system page size
145        auxv.push_back(auxv_t(M5_AT_PAGESZ, ArmISA::VMPageSize));
146        //Frequency at which times() increments
147        auxv.push_back(auxv_t(M5_AT_CLKTCK, 0x64));
148        // For statically linked executables, this is the virtual address of the
149        // program header tables if they appear in the executable image
150        auxv.push_back(auxv_t(M5_AT_PHDR, elfObject->programHeaderTable()));
151        // This is the size of a program header entry from the elf file.
152        auxv.push_back(auxv_t(M5_AT_PHENT, elfObject->programHeaderSize()));
153        // This is the number of program headers from the original elf file.
154        auxv.push_back(auxv_t(M5_AT_PHNUM, elfObject->programHeaderCount()));
155        //This is the address of the elf "interpreter", It should be set
156        //to 0 for regular executables. It should be something else
157        //(not sure what) for dynamic libraries.
158        auxv.push_back(auxv_t(M5_AT_BASE, 0));
159
160        //XXX Figure out what this should be.
161        auxv.push_back(auxv_t(M5_AT_FLAGS, 0));
162        //The entry point to the program
163        auxv.push_back(auxv_t(M5_AT_ENTRY, objFile->entryPoint()));
164        //Different user and group IDs
165        auxv.push_back(auxv_t(M5_AT_UID, uid()));
166        auxv.push_back(auxv_t(M5_AT_EUID, euid()));
167        auxv.push_back(auxv_t(M5_AT_GID, gid()));
168        auxv.push_back(auxv_t(M5_AT_EGID, egid()));
169        //Whether to enable "secure mode" in the executable
170        auxv.push_back(auxv_t(M5_AT_SECURE, 0));
171        //The filename of the program
172        auxv.push_back(auxv_t(M5_AT_EXECFN, 0));
173        //The string "v51" with unknown meaning
174        auxv.push_back(auxv_t(M5_AT_PLATFORM, 0));
175    }
176
177    //Figure out how big the initial stack nedes to be
178
179    // A sentry NULL void pointer at the top of the stack.
180    int sentry_size = intSize;
181
182    string platform = "v51";
183    int platform_size = platform.size() + 1;
184
185    // The aux vectors are put on the stack in two groups. The first group are
186    // the vectors that are generated as the elf is loaded. The second group
187    // are the ones that were computed ahead of time and include the platform
188    // string.
189    int aux_data_size = filename.size() + 1;
190
191    int env_data_size = 0;
192    for (int i = 0; i < envp.size(); ++i) {
193        env_data_size += envp[i].size() + 1;
194    }
195    int arg_data_size = 0;
196    for (int i = 0; i < argv.size(); ++i) {
197        arg_data_size += argv[i].size() + 1;
198    }
199
200    int info_block_size =
201        sentry_size + env_data_size + arg_data_size +
202        aux_data_size + platform_size;
203
204    //Each auxilliary vector is two 4 byte words
205    int aux_array_size = intSize * 2 * (auxv.size() + 1);
206
207    int envp_array_size = intSize * (envp.size() + 1);
208    int argv_array_size = intSize * (argv.size() + 1);
209
210    int argc_size = intSize;
211
212    //Figure out the size of the contents of the actual initial frame
213    int frame_size =
214        info_block_size +
215        aux_array_size +
216        envp_array_size +
217        argv_array_size +
218        argc_size;
219
220    //There needs to be padding after the auxiliary vector data so that the
221    //very bottom of the stack is aligned properly.
222    int partial_size = frame_size;
223    int aligned_partial_size = roundUp(partial_size, align);
224    int aux_padding = aligned_partial_size - partial_size;
225
226    int space_needed = frame_size + aux_padding;
227
228    stack_min = stack_base - space_needed;
229    stack_min = roundDown(stack_min, align);
230    stack_size = stack_base - stack_min;
231
232    // map memory
233    pTable->allocate(roundDown(stack_min, pageSize),
234                     roundUp(stack_size, pageSize));
235
236    // map out initial stack contents
237    uint32_t sentry_base = stack_base - sentry_size;
238    uint32_t aux_data_base = sentry_base - aux_data_size;
239    uint32_t env_data_base = aux_data_base - env_data_size;
240    uint32_t arg_data_base = env_data_base - arg_data_size;
241    uint32_t platform_base = arg_data_base - platform_size;
242    uint32_t auxv_array_base = platform_base - aux_array_size - aux_padding;
243    uint32_t envp_array_base = auxv_array_base - envp_array_size;
244    uint32_t argv_array_base = envp_array_base - argv_array_size;
245    uint32_t argc_base = argv_array_base - argc_size;
246
247    DPRINTF(Stack, "The addresses of items on the initial stack:\n");
248    DPRINTF(Stack, "0x%x - aux data\n", aux_data_base);
249    DPRINTF(Stack, "0x%x - env data\n", env_data_base);
250    DPRINTF(Stack, "0x%x - arg data\n", arg_data_base);
251    DPRINTF(Stack, "0x%x - platform base\n", platform_base);
252    DPRINTF(Stack, "0x%x - auxv array\n", auxv_array_base);
253    DPRINTF(Stack, "0x%x - envp array\n", envp_array_base);
254    DPRINTF(Stack, "0x%x - argv array\n", argv_array_base);
255    DPRINTF(Stack, "0x%x - argc \n", argc_base);
256    DPRINTF(Stack, "0x%x - stack min\n", stack_min);
257
258    // write contents to stack
259
260    // figure out argc
261    uint32_t argc = argv.size();
262    uint32_t guestArgc = ArmISA::htog(argc);
263
264    //Write out the sentry void *
265    uint32_t sentry_NULL = 0;
266    initVirtMem->writeBlob(sentry_base,
267            (uint8_t*)&sentry_NULL, sentry_size);
268
269    //Fix up the aux vectors which point to other data
270    for (int i = auxv.size() - 1; i >= 0; i--) {
271        if (auxv[i].a_type == M5_AT_PLATFORM) {
272            auxv[i].a_val = platform_base;
273            initVirtMem->writeString(platform_base, platform.c_str());
274        } else if (auxv[i].a_type == M5_AT_EXECFN) {
275            auxv[i].a_val = aux_data_base;
276            initVirtMem->writeString(aux_data_base, filename.c_str());
277        }
278    }
279
280    //Copy the aux stuff
281    for(int x = 0; x < auxv.size(); x++)
282    {
283        initVirtMem->writeBlob(auxv_array_base + x * 2 * intSize,
284                (uint8_t*)&(auxv[x].a_type), intSize);
285        initVirtMem->writeBlob(auxv_array_base + (x * 2 + 1) * intSize,
286                (uint8_t*)&(auxv[x].a_val), intSize);
287    }
288    //Write out the terminating zeroed auxilliary vector
289    const uint64_t zero = 0;
290    initVirtMem->writeBlob(auxv_array_base + 2 * intSize * auxv.size(),
291            (uint8_t*)&zero, 2 * intSize);
292
293    copyStringArray(envp, envp_array_base, env_data_base, initVirtMem);
294    copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem);
295
296    initVirtMem->writeBlob(argc_base, (uint8_t*)&guestArgc, intSize);
297
298    ThreadContext *tc = system->getThreadContext(contextIds[0]);
299    //Set the stack pointer register
300    tc->setIntReg(StackPointerReg, stack_min);
301    //A pointer to a function to run when the program exits. We'll set this
302    //to zero explicitly to make sure this isn't used.
303    tc->setIntReg(ArgumentReg0, 0);
304    //Set argument regs 1 and 2 to argv[0] and envp[0] respectively
305    if (argv.size() > 0) {
306        tc->setIntReg(ArgumentReg1, arg_data_base + arg_data_size -
307                                    argv[argv.size() - 1].size() - 1);
308    } else {
309        tc->setIntReg(ArgumentReg1, 0);
310    }
311    if (envp.size() > 0) {
312        tc->setIntReg(ArgumentReg2, env_data_base + env_data_size -
313                                    envp[envp.size() - 1].size() - 1);
314    } else {
315        tc->setIntReg(ArgumentReg2, 0);
316    }
317
318    Addr prog_entry = objFile->entryPoint();
319    tc->setPC(prog_entry);
320    tc->setNextPC(prog_entry + sizeof(MachInst));
321
322    //Align the "stack_min" to a page boundary.
323    stack_min = roundDown(stack_min, pageSize);
324}
325
326ArmISA::IntReg
327ArmLiveProcess::getSyscallArg(ThreadContext *tc, int &i)
328{
329    assert(i < 4);
330    return tc->readIntReg(ArgumentReg0 + i++);
331}
332
333void
334ArmLiveProcess::setSyscallArg(ThreadContext *tc,
335        int i, ArmISA::IntReg val)
336{
337    assert(i < 4);
338    tc->setIntReg(ArgumentReg0 + i, val);
339}
340
341void
342ArmLiveProcess::setSyscallReturn(ThreadContext *tc,
343        SyscallReturn return_value)
344{
345    tc->setIntReg(ReturnValueReg, return_value.value());
346}
347