process.cc revision 4166:ecebe3ac19b4
1/*
2 * Copyright (c) 2003-2006 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/*
33 * Copyright (c) 2007 The Hewlett-Packard Development Company
34 * All rights reserved.
35 *
36 * Redistribution and use of this software in source and binary forms,
37 * with or without modification, are permitted provided that the
38 * following conditions are met:
39 *
40 * The software must be used only for Non-Commercial Use which means any
41 * use which is NOT directed to receiving any direct monetary
42 * compensation for, or commercial advantage from such use.  Illustrative
43 * examples of non-commercial use are academic research, personal study,
44 * teaching, education and corporate research & development.
45 * Illustrative examples of commercial use are distributing products for
46 * commercial advantage and providing services using the software for
47 * commercial advantage.
48 *
49 * If you wish to use this software or functionality therein that may be
50 * covered by patents for commercial use, please contact:
51 *     Director of Intellectual Property Licensing
52 *     Office of Strategy and Technology
53 *     Hewlett-Packard Company
54 *     1501 Page Mill Road
55 *     Palo Alto, California  94304
56 *
57 * Redistributions of source code must retain the above copyright notice,
58 * this list of conditions and the following disclaimer.  Redistributions
59 * in binary form must reproduce the above copyright notice, this list of
60 * conditions and the following disclaimer in the documentation and/or
61 * other materials provided with the distribution.  Neither the name of
62 * the COPYRIGHT HOLDER(s), HEWLETT-PACKARD COMPANY, nor the names of its
63 * contributors may be used to endorse or promote products derived from
64 * this software without specific prior written permission.  No right of
65 * sublicense is granted herewith.  Derivatives of the software and
66 * output created using the software may be prepared, but only for
67 * Non-Commercial Uses.  Derivatives of the software may be shared with
68 * others provided: (i) the others agree to abide by the list of
69 * conditions herein which includes the Non-Commercial Use restrictions;
70 * and (ii) such Derivatives of the software include the above copyright
71 * notice to acknowledge the contribution from this software where
72 * applicable, this list of conditions and the disclaimer below.
73 *
74 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
75 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
76 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
77 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
78 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
79 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
80 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
81 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
82 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
83 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
84 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
85 *
86 * Authors: Gabe Black
87 */
88
89#include "arch/x86/isa_traits.hh"
90#include "arch/x86/process.hh"
91#include "arch/x86/types.hh"
92#include "base/loader/object_file.hh"
93#include "base/loader/elf_object.hh"
94#include "base/misc.hh"
95#include "cpu/thread_context.hh"
96#include "mem/page_table.hh"
97#include "mem/translating_port.hh"
98#include "sim/system.hh"
99
100using namespace std;
101using namespace X86ISA;
102
103M5_64_auxv_t::M5_64_auxv_t(int64_t type, int64_t val)
104{
105    a_type = TheISA::htog(type);
106    a_val = TheISA::htog(val);
107}
108
109X86LiveProcess::X86LiveProcess(const std::string &nm, ObjectFile *objFile,
110        System *_system, int stdin_fd, int stdout_fd, int stderr_fd,
111        std::vector<std::string> &argv, std::vector<std::string> &envp,
112        const std::string &cwd,
113        uint64_t _uid, uint64_t _euid, uint64_t _gid, uint64_t _egid,
114        uint64_t _pid, uint64_t _ppid)
115    : LiveProcess(nm, objFile, _system, stdin_fd, stdout_fd, stderr_fd,
116        argv, envp, cwd, _uid, _euid, _gid, _egid, _pid, _ppid)
117{
118    brk_point = objFile->dataBase() + objFile->dataSize() + objFile->bssSize();
119    brk_point = roundUp(brk_point, VMPageSize);
120
121    // Set pointer for next thread stack.  Reserve 8M for main stack.
122    next_thread_stack_base = stack_base - (8 * 1024 * 1024);
123
124    // Set up stack. On SPARC Linux, stack goes from the top of memory
125    // downward, less the hole for the kernel address space.
126    stack_base = (Addr)0x80000000000ULL;
127
128    // Set up region for mmaps.  Tru64 seems to start just above 0 and
129    // grow up from there.
130    mmap_start = mmap_end = 0xfffff80000000000ULL;
131}
132
133void X86LiveProcess::handleTrap(int trapNum, ThreadContext *tc)
134{
135    switch(trapNum)
136    {
137      default:
138        panic("Unimplemented trap to operating system: trap number %#x.\n", trapNum);
139    }
140}
141
142void
143X86LiveProcess::startup()
144{
145    argsInit(sizeof(IntReg), VMPageSize);
146
147    //The AMD64 abi says that only rsp and rdx are defined at process
148    //startup. rsp will be set by argsInit, and I don't understand what
149    //rdx should be set to. The other floating point and integer registers
150    //will be zeroed by the register file constructors, but control registers
151    //should be initialized here. Since none of those are implemented, there
152    //isn't anything here.
153}
154
155void
156X86LiveProcess::argsInit(int intSize, int pageSize)
157{
158    typedef M5_64_auxv_t auxv_t;
159    Process::startup();
160
161    string filename;
162    if(argv.size() < 1)
163        filename = "";
164    else
165        filename = argv[0];
166
167    Addr alignmentMask = ~(intSize - 1);
168
169    // load object file into target memory
170    objFile->loadSections(initVirtMem);
171
172    //These are the auxilliary vector types
173    enum auxTypes
174    {
175        X86_AT_NULL = 0,
176        X86_AT_IGNORE = 1,
177        X86_AT_EXECFD = 2,
178        X86_AT_PHDR = 3,
179        X86_AT_PHENT = 4,
180        X86_AT_PHNUM = 5,
181        X86_AT_PAGESZ = 6,
182        X86_AT_BASE = 7,
183        X86_AT_FLAGS = 8,
184        X86_AT_ENTRY = 9,
185        X86_AT_NOTELF = 10,
186        X86_AT_UID = 11,
187        X86_AT_EUID = 12,
188        X86_AT_GID = 13,
189        X86_AT_EGID = 14
190    };
191
192    //Setup the auxilliary vectors. These will already have endian conversion.
193    //Auxilliary vectors are loaded only for elf formatted executables.
194    ElfObject * elfObject = dynamic_cast<ElfObject *>(objFile);
195    if(elfObject)
196    {
197        /*
198        //Bits which describe the system hardware capabilities
199        auxv.push_back(auxv_t(SPARC_AT_HWCAP, hwcap));
200        //The system page size
201        auxv.push_back(auxv_t(SPARC_AT_PAGESZ, SparcISA::VMPageSize));
202        //Defined to be 100 in the kernel source.
203        //Frequency at which times() increments
204        auxv.push_back(auxv_t(SPARC_AT_CLKTCK, 100));
205        // For statically linked executables, this is the virtual address of the
206        // program header tables if they appear in the executable image
207        auxv.push_back(auxv_t(SPARC_AT_PHDR, elfObject->programHeaderTable()));
208        // This is the size of a program header entry from the elf file.
209        auxv.push_back(auxv_t(SPARC_AT_PHENT, elfObject->programHeaderSize()));
210        // This is the number of program headers from the original elf file.
211        auxv.push_back(auxv_t(SPARC_AT_PHNUM, elfObject->programHeaderCount()));
212        //This is the address of the elf "interpreter", It should be set
213        //to 0 for regular executables. It should be something else
214        //(not sure what) for dynamic libraries.
215        auxv.push_back(auxv_t(SPARC_AT_BASE, 0));
216        //This is hardwired to 0 in the elf loading code in the kernel
217        auxv.push_back(auxv_t(SPARC_AT_FLAGS, 0));
218        //The entry point to the program
219        auxv.push_back(auxv_t(SPARC_AT_ENTRY, objFile->entryPoint()));
220        //Different user and group IDs
221        auxv.push_back(auxv_t(SPARC_AT_UID, uid()));
222        auxv.push_back(auxv_t(SPARC_AT_EUID, euid()));
223        auxv.push_back(auxv_t(SPARC_AT_GID, gid()));
224        auxv.push_back(auxv_t(SPARC_AT_EGID, egid()));
225        //Whether to enable "secure mode" in the executable
226        auxv.push_back(auxv_t(SPARC_AT_SECURE, 0));*/
227    }
228
229    //Figure out how big the initial stack needs to be
230
231    // The unaccounted for 0 at the top of the stack
232    int mysterious_size = intSize;
233
234    //This is the name of the file which is present on the initial stack
235    //It's purpose is to let the user space linker examine the original file.
236    int file_name_size = filename.size() + 1;
237
238    int env_data_size = 0;
239    for (int i = 0; i < envp.size(); ++i) {
240        env_data_size += envp[i].size() + 1;
241    }
242    int arg_data_size = 0;
243    for (int i = 0; i < argv.size(); ++i) {
244        arg_data_size += argv[i].size() + 1;
245    }
246
247    //The info_block needs to be padded so it's size is a multiple of the
248    //alignment mask. Also, it appears that there needs to be at least some
249    //padding, so if the size is already a multiple, we need to increase it
250    //anyway.
251    int info_block_size =
252        (file_name_size +
253        env_data_size +
254        arg_data_size +
255        intSize) & alignmentMask;
256
257    int info_block_padding =
258        info_block_size -
259        file_name_size -
260        env_data_size -
261        arg_data_size;
262
263    //Each auxilliary vector is two 8 byte words
264    int aux_array_size = intSize * 2 * (auxv.size() + 1);
265
266    int envp_array_size = intSize * (envp.size() + 1);
267    int argv_array_size = intSize * (argv.size() + 1);
268
269    int argc_size = intSize;
270    int window_save_size = intSize * 16;
271
272    int space_needed =
273        mysterious_size +
274        info_block_size +
275        aux_array_size +
276        envp_array_size +
277        argv_array_size +
278        argc_size +
279        window_save_size;
280
281    stack_min = stack_base - space_needed;
282    stack_min &= alignmentMask;
283    stack_size = stack_base - stack_min;
284
285    // map memory
286    pTable->allocate(roundDown(stack_min, pageSize),
287                     roundUp(stack_size, pageSize));
288
289    // map out initial stack contents
290    Addr mysterious_base = stack_base - mysterious_size;
291    Addr file_name_base = mysterious_base - file_name_size;
292    Addr env_data_base = file_name_base - env_data_size;
293    Addr arg_data_base = env_data_base - arg_data_size;
294    Addr auxv_array_base = arg_data_base - aux_array_size - info_block_padding;
295    Addr envp_array_base = auxv_array_base - envp_array_size;
296    Addr argv_array_base = envp_array_base - argv_array_size;
297    Addr argc_base = argv_array_base - argc_size;
298#ifndef NDEBUG
299    // only used in DPRINTF
300    Addr window_save_base = argc_base - window_save_size;
301#endif
302
303    DPRINTF(X86, "The addresses of items on the initial stack:\n");
304    DPRINTF(X86, "0x%x - file name\n", file_name_base);
305    DPRINTF(X86, "0x%x - env data\n", env_data_base);
306    DPRINTF(X86, "0x%x - arg data\n", arg_data_base);
307    DPRINTF(X86, "0x%x - auxv array\n", auxv_array_base);
308    DPRINTF(X86, "0x%x - envp array\n", envp_array_base);
309    DPRINTF(X86, "0x%x - argv array\n", argv_array_base);
310    DPRINTF(X86, "0x%x - argc \n", argc_base);
311    DPRINTF(X86, "0x%x - window save\n", window_save_base);
312    DPRINTF(X86, "0x%x - stack min\n", stack_min);
313
314    // write contents to stack
315
316    // figure out argc
317    uint64_t argc = argv.size();
318    uint64_t guestArgc = TheISA::htog(argc);
319
320    //Write out the mysterious 0
321    uint64_t mysterious_zero = 0;
322    initVirtMem->writeBlob(mysterious_base,
323            (uint8_t*)&mysterious_zero, mysterious_size);
324
325    //Write the file name
326    initVirtMem->writeString(file_name_base, filename.c_str());
327
328    //Copy the aux stuff
329    for(int x = 0; x < auxv.size(); x++)
330    {
331        initVirtMem->writeBlob(auxv_array_base + x * 2 * intSize,
332                (uint8_t*)&(auxv[x].a_type), intSize);
333        initVirtMem->writeBlob(auxv_array_base + (x * 2 + 1) * intSize,
334                (uint8_t*)&(auxv[x].a_val), intSize);
335    }
336    //Write out the terminating zeroed auxilliary vector
337    const uint64_t zero = 0;
338    initVirtMem->writeBlob(auxv_array_base + 2 * intSize * auxv.size(),
339            (uint8_t*)&zero, 2 * intSize);
340
341    copyStringArray(envp, envp_array_base, env_data_base, initVirtMem);
342    copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem);
343
344    initVirtMem->writeBlob(argc_base, (uint8_t*)&guestArgc, intSize);
345
346    //Set up the thread context to start running the process
347    threadContexts[0]->setIntReg(ArgumentReg0, argc);
348    threadContexts[0]->setIntReg(ArgumentReg1, argv_array_base);
349    threadContexts[0]->setIntReg(StackPointerReg, stack_min);
350
351    Addr prog_entry = objFile->entryPoint();
352    threadContexts[0]->setPC(prog_entry);
353    threadContexts[0]->setNextPC(prog_entry + sizeof(MachInst));
354
355    //Align the "stack_min" to a page boundary.
356    stack_min = roundDown(stack_min, pageSize);
357
358//    num_processes++;
359}
360