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