process.cc revision 11800
1/*
2 * Copyright (c) 2007-2008 The Florida State University
3 * Copyright (c) 2009 The University of Edinburgh
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met: redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer;
10 * redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution;
13 * neither the name of the copyright holders nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 * Authors: Stephen Hines
30 *          Timothy M. Jones
31 */
32
33#include "arch/power/process.hh"
34
35#include "arch/power/isa_traits.hh"
36#include "arch/power/types.hh"
37#include "base/loader/elf_object.hh"
38#include "base/loader/object_file.hh"
39#include "base/misc.hh"
40#include "cpu/thread_context.hh"
41#include "debug/Stack.hh"
42#include "mem/page_table.hh"
43#include "sim/process_impl.hh"
44#include "sim/syscall_return.hh"
45#include "sim/system.hh"
46
47using namespace std;
48using namespace PowerISA;
49
50PowerLiveProcess::PowerLiveProcess(LiveProcessParams *params,
51        ObjectFile *objFile)
52    : LiveProcess(params, objFile)
53{
54    stack_base = 0xbf000000L;
55
56    // Set pointer for next thread stack.  Reserve 8M for main stack.
57    next_thread_stack_base = stack_base - (8 * 1024 * 1024);
58
59    // Set up break point (Top of Heap)
60    brk_point = objFile->dataBase() + objFile->dataSize() + objFile->bssSize();
61    brk_point = roundUp(brk_point, PageBytes);
62
63    // Set up region for mmaps. For now, start at bottom of kuseg space.
64    mmap_end = 0x70000000L;
65}
66
67void
68PowerLiveProcess::initState()
69{
70    Process::initState();
71
72    argsInit(MachineBytes, PageBytes);
73}
74
75void
76PowerLiveProcess::argsInit(int intSize, int pageSize)
77{
78    typedef AuxVector<uint32_t> auxv_t;
79    std::vector<auxv_t> auxv;
80
81    string filename;
82    if (argv.size() < 1)
83        filename = "";
84    else
85        filename = argv[0];
86
87    //We want 16 byte alignment
88    uint64_t align = 16;
89
90    // Patch the ld_bias for dynamic executables.
91    updateBias();
92
93    // load object file into target memory
94    objFile->loadSections(initVirtMem);
95
96    //Setup the auxilliary vectors. These will already have endian conversion.
97    //Auxilliary vectors are loaded only for elf formatted executables.
98    ElfObject * elfObject = dynamic_cast<ElfObject *>(objFile);
99    if (elfObject) {
100        uint32_t features = 0;
101
102        //Bits which describe the system hardware capabilities
103        //XXX Figure out what these should be
104        auxv.push_back(auxv_t(M5_AT_HWCAP, features));
105        //The system page size
106        auxv.push_back(auxv_t(M5_AT_PAGESZ, PowerISA::PageBytes));
107        //Frequency at which times() increments
108        auxv.push_back(auxv_t(M5_AT_CLKTCK, 0x64));
109        // For statically linked executables, this is the virtual address of the
110        // program header tables if they appear in the executable image
111        auxv.push_back(auxv_t(M5_AT_PHDR, elfObject->programHeaderTable()));
112        // This is the size of a program header entry from the elf file.
113        auxv.push_back(auxv_t(M5_AT_PHENT, elfObject->programHeaderSize()));
114        // This is the number of program headers from the original elf file.
115        auxv.push_back(auxv_t(M5_AT_PHNUM, elfObject->programHeaderCount()));
116        // This is the base address of the ELF interpreter; it should be
117        // zero for static executables or contain the base address for
118        // dynamic executables.
119        auxv.push_back(auxv_t(M5_AT_BASE, getBias()));
120        //XXX Figure out what this should be.
121        auxv.push_back(auxv_t(M5_AT_FLAGS, 0));
122        //The entry point to the program
123        auxv.push_back(auxv_t(M5_AT_ENTRY, objFile->entryPoint()));
124        //Different user and group IDs
125        auxv.push_back(auxv_t(M5_AT_UID, uid()));
126        auxv.push_back(auxv_t(M5_AT_EUID, euid()));
127        auxv.push_back(auxv_t(M5_AT_GID, gid()));
128        auxv.push_back(auxv_t(M5_AT_EGID, egid()));
129        //Whether to enable "secure mode" in the executable
130        auxv.push_back(auxv_t(M5_AT_SECURE, 0));
131        //The filename of the program
132        auxv.push_back(auxv_t(M5_AT_EXECFN, 0));
133        //The string "v51" with unknown meaning
134        auxv.push_back(auxv_t(M5_AT_PLATFORM, 0));
135    }
136
137    //Figure out how big the initial stack nedes to be
138
139    // A sentry NULL void pointer at the top of the stack.
140    int sentry_size = intSize;
141
142    string platform = "v51";
143    int platform_size = platform.size() + 1;
144
145    // The aux vectors are put on the stack in two groups. The first group are
146    // the vectors that are generated as the elf is loaded. The second group
147    // are the ones that were computed ahead of time and include the platform
148    // string.
149    int aux_data_size = filename.size() + 1;
150
151    int env_data_size = 0;
152    for (int i = 0; i < envp.size(); ++i) {
153        env_data_size += envp[i].size() + 1;
154    }
155    int arg_data_size = 0;
156    for (int i = 0; i < argv.size(); ++i) {
157        arg_data_size += argv[i].size() + 1;
158    }
159
160    int info_block_size =
161        sentry_size + env_data_size + arg_data_size +
162        aux_data_size + platform_size;
163
164    //Each auxilliary vector is two 4 byte words
165    int aux_array_size = intSize * 2 * (auxv.size() + 1);
166
167    int envp_array_size = intSize * (envp.size() + 1);
168    int argv_array_size = intSize * (argv.size() + 1);
169
170    int argc_size = intSize;
171
172    //Figure out the size of the contents of the actual initial frame
173    int frame_size =
174        info_block_size +
175        aux_array_size +
176        envp_array_size +
177        argv_array_size +
178        argc_size;
179
180    //There needs to be padding after the auxiliary vector data so that the
181    //very bottom of the stack is aligned properly.
182    int partial_size = frame_size;
183    int aligned_partial_size = roundUp(partial_size, align);
184    int aux_padding = aligned_partial_size - partial_size;
185
186    int space_needed = frame_size + aux_padding;
187
188    stack_min = stack_base - space_needed;
189    stack_min = roundDown(stack_min, align);
190    stack_size = stack_base - stack_min;
191
192    // map memory
193    allocateMem(roundDown(stack_min, pageSize), roundUp(stack_size, pageSize));
194
195    // map out initial stack contents
196    uint32_t sentry_base = stack_base - sentry_size;
197    uint32_t aux_data_base = sentry_base - aux_data_size;
198    uint32_t env_data_base = aux_data_base - env_data_size;
199    uint32_t arg_data_base = env_data_base - arg_data_size;
200    uint32_t platform_base = arg_data_base - platform_size;
201    uint32_t auxv_array_base = platform_base - aux_array_size - aux_padding;
202    uint32_t envp_array_base = auxv_array_base - envp_array_size;
203    uint32_t argv_array_base = envp_array_base - argv_array_size;
204    uint32_t argc_base = argv_array_base - argc_size;
205
206    DPRINTF(Stack, "The addresses of items on the initial stack:\n");
207    DPRINTF(Stack, "0x%x - aux data\n", aux_data_base);
208    DPRINTF(Stack, "0x%x - env data\n", env_data_base);
209    DPRINTF(Stack, "0x%x - arg data\n", arg_data_base);
210    DPRINTF(Stack, "0x%x - platform base\n", platform_base);
211    DPRINTF(Stack, "0x%x - auxv array\n", auxv_array_base);
212    DPRINTF(Stack, "0x%x - envp array\n", envp_array_base);
213    DPRINTF(Stack, "0x%x - argv array\n", argv_array_base);
214    DPRINTF(Stack, "0x%x - argc \n", argc_base);
215    DPRINTF(Stack, "0x%x - stack min\n", stack_min);
216
217    // write contents to stack
218
219    // figure out argc
220    uint32_t argc = argv.size();
221    uint32_t guestArgc = PowerISA::htog(argc);
222
223    //Write out the sentry void *
224    uint32_t sentry_NULL = 0;
225    initVirtMem.writeBlob(sentry_base,
226            (uint8_t*)&sentry_NULL, sentry_size);
227
228    //Fix up the aux vectors which point to other data
229    for (int i = auxv.size() - 1; i >= 0; i--) {
230        if (auxv[i].a_type == M5_AT_PLATFORM) {
231            auxv[i].a_val = platform_base;
232            initVirtMem.writeString(platform_base, platform.c_str());
233        } else if (auxv[i].a_type == M5_AT_EXECFN) {
234            auxv[i].a_val = aux_data_base;
235            initVirtMem.writeString(aux_data_base, filename.c_str());
236        }
237    }
238
239    //Copy the aux stuff
240    for (int x = 0; x < auxv.size(); x++)
241    {
242        initVirtMem.writeBlob(auxv_array_base + x * 2 * intSize,
243                (uint8_t*)&(auxv[x].a_type), intSize);
244        initVirtMem.writeBlob(auxv_array_base + (x * 2 + 1) * intSize,
245                (uint8_t*)&(auxv[x].a_val), intSize);
246    }
247    //Write out the terminating zeroed auxilliary vector
248    const uint64_t zero = 0;
249    initVirtMem.writeBlob(auxv_array_base + 2 * intSize * auxv.size(),
250            (uint8_t*)&zero, 2 * intSize);
251
252    copyStringArray(envp, envp_array_base, env_data_base, initVirtMem);
253    copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem);
254
255    initVirtMem.writeBlob(argc_base, (uint8_t*)&guestArgc, intSize);
256
257    ThreadContext *tc = system->getThreadContext(contextIds[0]);
258
259    //Set the stack pointer register
260    tc->setIntReg(StackPointerReg, stack_min);
261
262    tc->pcState(getStartPC());
263
264    //Align the "stack_min" to a page boundary.
265    stack_min = roundDown(stack_min, pageSize);
266}
267
268PowerISA::IntReg
269PowerLiveProcess::getSyscallArg(ThreadContext *tc, int &i)
270{
271    assert(i < 5);
272    return tc->readIntReg(ArgumentReg0 + i++);
273}
274
275void
276PowerLiveProcess::setSyscallArg(ThreadContext *tc,
277        int i, PowerISA::IntReg val)
278{
279    assert(i < 5);
280    tc->setIntReg(ArgumentReg0 + i, val);
281}
282
283void
284PowerLiveProcess::setSyscallReturn(ThreadContext *tc, SyscallReturn sysret)
285{
286    Cr cr = tc->readIntReg(INTREG_CR);
287    if (sysret.successful()) {
288        cr.cr0.so = 0;
289    } else {
290        cr.cr0.so = 1;
291    }
292    tc->setIntReg(INTREG_CR, cr);
293    tc->setIntReg(ReturnValueReg, sysret.encodedValue());
294}
295