process.cc revision 4849
111308Santhony.gutierrez@amd.com/*
211308Santhony.gutierrez@amd.com * Copyright (c) 2003-2006 The Regents of The University of Michigan
311308Santhony.gutierrez@amd.com * All rights reserved.
411308Santhony.gutierrez@amd.com *
511308Santhony.gutierrez@amd.com * Redistribution and use in source and binary forms, with or without
611308Santhony.gutierrez@amd.com * modification, are permitted provided that the following conditions are
711308Santhony.gutierrez@amd.com * met: redistributions of source code must retain the above copyright
811308Santhony.gutierrez@amd.com * notice, this list of conditions and the following disclaimer;
911308Santhony.gutierrez@amd.com * redistributions in binary form must reproduce the above copyright
1011308Santhony.gutierrez@amd.com * notice, this list of conditions and the following disclaimer in the
1111308Santhony.gutierrez@amd.com * documentation and/or other materials provided with the distribution;
1211308Santhony.gutierrez@amd.com * neither the name of the copyright holders nor the names of its
1311308Santhony.gutierrez@amd.com * contributors may be used to endorse or promote products derived from
1411308Santhony.gutierrez@amd.com * this software without specific prior written permission.
1511308Santhony.gutierrez@amd.com *
1611308Santhony.gutierrez@amd.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1711308Santhony.gutierrez@amd.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1811308Santhony.gutierrez@amd.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1911308Santhony.gutierrez@amd.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2011308Santhony.gutierrez@amd.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2111308Santhony.gutierrez@amd.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2211308Santhony.gutierrez@amd.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2311308Santhony.gutierrez@amd.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2411308Santhony.gutierrez@amd.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2511308Santhony.gutierrez@amd.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2611308Santhony.gutierrez@amd.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2711308Santhony.gutierrez@amd.com *
2811308Santhony.gutierrez@amd.com * Authors: Gabe Black
2911308Santhony.gutierrez@amd.com *          Ali Saidi
3011308Santhony.gutierrez@amd.com */
3111308Santhony.gutierrez@amd.com
3211308Santhony.gutierrez@amd.com/*
3311308Santhony.gutierrez@amd.com * Copyright (c) 2007 The Hewlett-Packard Development Company
3411308Santhony.gutierrez@amd.com * All rights reserved.
3511308Santhony.gutierrez@amd.com *
3611308Santhony.gutierrez@amd.com * Redistribution and use of this software in source and binary forms,
3711308Santhony.gutierrez@amd.com * with or without modification, are permitted provided that the
3811308Santhony.gutierrez@amd.com * following conditions are met:
3911308Santhony.gutierrez@amd.com *
4011308Santhony.gutierrez@amd.com * The software must be used only for Non-Commercial Use which means any
4111308Santhony.gutierrez@amd.com * use which is NOT directed to receiving any direct monetary
4211308Santhony.gutierrez@amd.com * compensation for, or commercial advantage from such use.  Illustrative
4311308Santhony.gutierrez@amd.com * examples of non-commercial use are academic research, personal study,
4411308Santhony.gutierrez@amd.com * teaching, education and corporate research & development.
4511308Santhony.gutierrez@amd.com * Illustrative examples of commercial use are distributing products for
4611308Santhony.gutierrez@amd.com * commercial advantage and providing services using the software for
4711308Santhony.gutierrez@amd.com * commercial advantage.
4811308Santhony.gutierrez@amd.com *
4911308Santhony.gutierrez@amd.com * If you wish to use this software or functionality therein that may be
5011308Santhony.gutierrez@amd.com * covered by patents for commercial use, please contact:
5111308Santhony.gutierrez@amd.com *     Director of Intellectual Property Licensing
5211308Santhony.gutierrez@amd.com *     Office of Strategy and Technology
5311308Santhony.gutierrez@amd.com *     Hewlett-Packard Company
5411308Santhony.gutierrez@amd.com *     1501 Page Mill Road
5511308Santhony.gutierrez@amd.com *     Palo Alto, California  94304
5611308Santhony.gutierrez@amd.com *
5711308Santhony.gutierrez@amd.com * Redistributions of source code must retain the above copyright notice,
5811308Santhony.gutierrez@amd.com * this list of conditions and the following disclaimer.  Redistributions
5911308Santhony.gutierrez@amd.com * in binary form must reproduce the above copyright notice, this list of
6011308Santhony.gutierrez@amd.com * conditions and the following disclaimer in the documentation and/or
6111308Santhony.gutierrez@amd.com * other materials provided with the distribution.  Neither the name of
6211308Santhony.gutierrez@amd.com * the COPYRIGHT HOLDER(s), HEWLETT-PACKARD COMPANY, nor the names of its
6311308Santhony.gutierrez@amd.com * contributors may be used to endorse or promote products derived from
6411308Santhony.gutierrez@amd.com * this software without specific prior written permission.  No right of
6511308Santhony.gutierrez@amd.com * sublicense is granted herewith.  Derivatives of the software and
6611308Santhony.gutierrez@amd.com * output created using the software may be prepared, but only for
6711308Santhony.gutierrez@amd.com * Non-Commercial Uses.  Derivatives of the software may be shared with
6811308Santhony.gutierrez@amd.com * others provided: (i) the others agree to abide by the list of
6911308Santhony.gutierrez@amd.com * conditions herein which includes the Non-Commercial Use restrictions;
7011308Santhony.gutierrez@amd.com * and (ii) such Derivatives of the software include the above copyright
7111308Santhony.gutierrez@amd.com * notice to acknowledge the contribution from this software where
7211308Santhony.gutierrez@amd.com * applicable, this list of conditions and the disclaimer below.
7311308Santhony.gutierrez@amd.com *
7411308Santhony.gutierrez@amd.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
7511308Santhony.gutierrez@amd.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7611308Santhony.gutierrez@amd.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7711308Santhony.gutierrez@amd.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7811308Santhony.gutierrez@amd.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
7911308Santhony.gutierrez@amd.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
8011308Santhony.gutierrez@amd.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
8111308Santhony.gutierrez@amd.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
8211308Santhony.gutierrez@amd.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
8311308Santhony.gutierrez@amd.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
8411308Santhony.gutierrez@amd.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
8511308Santhony.gutierrez@amd.com *
8611308Santhony.gutierrez@amd.com * 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/process_impl.hh"
99#include "sim/system.hh"
100
101using namespace std;
102using namespace X86ISA;
103
104M5_64_auxv_t::M5_64_auxv_t(int64_t type, int64_t val)
105{
106    a_type = TheISA::htog(type);
107    a_val = TheISA::htog(val);
108}
109
110X86LiveProcess::X86LiveProcess(const std::string &nm, ObjectFile *objFile,
111        System *_system, int stdin_fd, int stdout_fd, int stderr_fd,
112        std::vector<std::string> &argv, std::vector<std::string> &envp,
113        const std::string &cwd,
114        uint64_t _uid, uint64_t _euid, uint64_t _gid, uint64_t _egid,
115        uint64_t _pid, uint64_t _ppid)
116    : LiveProcess(nm, objFile, _system, stdin_fd, stdout_fd, stderr_fd,
117        argv, envp, cwd, _uid, _euid, _gid, _egid, _pid, _ppid)
118{
119    brk_point = objFile->dataBase() + objFile->dataSize() + objFile->bssSize();
120    brk_point = roundUp(brk_point, VMPageSize);
121
122    // Set pointer for next thread stack.  Reserve 8M for main stack.
123    next_thread_stack_base = stack_base - (8 * 1024 * 1024);
124
125    // Set up stack. On X86_64 Linux, stack goes from the top of memory
126    // downward, less the hole for the kernel address space plus one page
127    // for undertermined purposes.
128    stack_base = (Addr)0x7FFFFFFFF000ULL;
129
130    // Set up region for mmaps. This was determined empirically and may not
131    // always be correct.
132    mmap_start = mmap_end = 0x2aaaaaaab000;
133}
134
135void X86LiveProcess::handleTrap(int trapNum, ThreadContext *tc)
136{
137    switch(trapNum)
138    {
139      default:
140        panic("Unimplemented trap to operating system: trap number %#x.\n", trapNum);
141    }
142}
143
144void
145X86LiveProcess::startup()
146{
147    argsInit(sizeof(IntReg), VMPageSize);
148
149    //The AMD64 abi says that only rsp and rdx are defined at process
150    //startup. rsp will be set by argsInit, and I don't understand what
151    //rdx should be set to. The other floating point and integer registers
152    //will be zeroed by the register file constructors, but control registers
153    //should be initialized here. Since none of those are implemented, there
154    //isn't anything here.
155}
156
157void
158X86LiveProcess::argsInit(int intSize, int pageSize)
159{
160    typedef M5_64_auxv_t auxv_t;
161    Process::startup();
162
163    string filename;
164    if(argv.size() < 1)
165        filename = "";
166    else
167        filename = argv[0];
168
169    //We want 16 byte alignment
170    uint64_t align = 16;
171
172    // load object file into target memory
173    objFile->loadSections(initVirtMem);
174
175    enum X86CpuFeature {
176        X86_OnboardFPU = 1 << 0,
177        X86_VirtualModeExtensions = 1 << 1,
178        X86_DebuggingExtensions = 1 << 2,
179        X86_PageSizeExtensions = 1 << 3,
180
181        X86_TimeStampCounter = 1 << 4,
182        X86_ModelSpecificRegisters = 1 << 5,
183        X86_PhysicalAddressExtensions = 1 << 6,
184        X86_MachineCheckExtensions = 1 << 7,
185
186        X86_CMPXCHG8Instruction = 1 << 8,
187        X86_OnboardAPIC = 1 << 9,
188        X86_SYSENTER_SYSEXIT = 1 << 11,
189
190        X86_MemoryTypeRangeRegisters = 1 << 12,
191        X86_PageGlobalEnable = 1 << 13,
192        X86_MachineCheckArchitecture = 1 << 14,
193        X86_CMOVInstruction = 1 << 15,
194
195        X86_PageAttributeTable = 1 << 16,
196        X86_36BitPSEs = 1 << 17,
197        X86_ProcessorSerialNumber = 1 << 18,
198        X86_CLFLUSHInstruction = 1 << 19,
199
200        X86_DebugTraceStore = 1 << 21,
201        X86_ACPIViaMSR = 1 << 22,
202        X86_MultimediaExtensions = 1 << 23,
203
204        X86_FXSAVE_FXRSTOR = 1 << 24,
205        X86_StreamingSIMDExtensions = 1 << 25,
206        X86_StreamingSIMDExtensions2 = 1 << 26,
207        X86_CPUSelfSnoop = 1 << 27,
208
209        X86_HyperThreading = 1 << 28,
210        X86_AutomaticClockControl = 1 << 29,
211        X86_IA64Processor = 1 << 30
212    };
213
214    //Setup the auxilliary vectors. These will already have endian conversion.
215    //Auxilliary vectors are loaded only for elf formatted executables.
216    ElfObject * elfObject = dynamic_cast<ElfObject *>(objFile);
217    if(elfObject)
218    {
219        uint64_t features =
220            X86_OnboardFPU |
221            X86_VirtualModeExtensions |
222            X86_DebuggingExtensions |
223            X86_PageSizeExtensions |
224            X86_TimeStampCounter |
225            X86_ModelSpecificRegisters |
226            X86_PhysicalAddressExtensions |
227            X86_MachineCheckExtensions |
228            X86_CMPXCHG8Instruction |
229            X86_OnboardAPIC |
230            X86_SYSENTER_SYSEXIT |
231            X86_MemoryTypeRangeRegisters |
232            X86_PageGlobalEnable |
233            X86_MachineCheckArchitecture |
234            X86_CMOVInstruction |
235            X86_PageAttributeTable |
236            X86_36BitPSEs |
237//            X86_ProcessorSerialNumber |
238            X86_CLFLUSHInstruction |
239//            X86_DebugTraceStore |
240//            X86_ACPIViaMSR |
241            X86_MultimediaExtensions |
242            X86_FXSAVE_FXRSTOR |
243            X86_StreamingSIMDExtensions |
244            X86_StreamingSIMDExtensions2 |
245//            X86_CPUSelfSnoop |
246//            X86_HyperThreading |
247//            X86_AutomaticClockControl |
248//            X86_IA64Processor |
249            0;
250
251        //Bits which describe the system hardware capabilities
252        //XXX Figure out what these should be
253        auxv.push_back(auxv_t(M5_AT_HWCAP, features));
254        //The system page size
255        auxv.push_back(auxv_t(M5_AT_PAGESZ, X86ISA::VMPageSize));
256        //Frequency at which times() increments
257        auxv.push_back(auxv_t(M5_AT_CLKTCK, 100));
258        // For statically linked executables, this is the virtual address of the
259        // program header tables if they appear in the executable image
260        auxv.push_back(auxv_t(M5_AT_PHDR, elfObject->programHeaderTable()));
261        // This is the size of a program header entry from the elf file.
262        auxv.push_back(auxv_t(M5_AT_PHENT, elfObject->programHeaderSize()));
263        // This is the number of program headers from the original elf file.
264        auxv.push_back(auxv_t(M5_AT_PHNUM, elfObject->programHeaderCount()));
265        //Defined to be 100 in the kernel source.
266        //This is the address of the elf "interpreter", It should be set
267        //to 0 for regular executables. It should be something else
268        //(not sure what) for dynamic libraries.
269        auxv.push_back(auxv_t(M5_AT_BASE, 0));
270
271        //XXX Figure out what this should be.
272        auxv.push_back(auxv_t(M5_AT_FLAGS, 0));
273        //The entry point to the program
274        auxv.push_back(auxv_t(M5_AT_ENTRY, objFile->entryPoint()));
275        //Different user and group IDs
276        auxv.push_back(auxv_t(M5_AT_UID, uid()));
277        auxv.push_back(auxv_t(M5_AT_EUID, euid()));
278        auxv.push_back(auxv_t(M5_AT_GID, gid()));
279        auxv.push_back(auxv_t(M5_AT_EGID, egid()));
280        //Whether to enable "secure mode" in the executable
281        auxv.push_back(auxv_t(M5_AT_SECURE, 0));
282        //The string "x86_64" with unknown meaning
283        auxv.push_back(auxv_t(M5_AT_PLATFORM, 0));
284    }
285
286    //Figure out how big the initial stack needs to be
287
288    // A sentry NULL void pointer at the top of the stack.
289    int sentry_size = intSize;
290
291    //This is the name of the file which is present on the initial stack
292    //It's purpose is to let the user space linker examine the original file.
293    int file_name_size = filename.size() + 1;
294
295    string platform = "x86_64";
296    int aux_data_size = platform.size() + 1;
297
298    int env_data_size = 0;
299    for (int i = 0; i < envp.size(); ++i) {
300        env_data_size += envp[i].size() + 1;
301    }
302    int arg_data_size = 0;
303    for (int i = 0; i < argv.size(); ++i) {
304        arg_data_size += argv[i].size() + 1;
305    }
306
307    //The info_block needs to be padded so it's size is a multiple of the
308    //alignment mask. Also, it appears that there needs to be at least some
309    //padding, so if the size is already a multiple, we need to increase it
310    //anyway.
311    int base_info_block_size =
312        sentry_size + file_name_size + env_data_size + arg_data_size;
313
314    int info_block_size = roundUp(base_info_block_size, align);
315
316    int info_block_padding = info_block_size - base_info_block_size;
317
318    //Each auxilliary vector is two 8 byte words
319    int aux_array_size = intSize * 2 * (auxv.size() + 1);
320
321    int envp_array_size = intSize * (envp.size() + 1);
322    int argv_array_size = intSize * (argv.size() + 1);
323
324    int argc_size = intSize;
325
326    //Figure out the size of the contents of the actual initial frame
327    int frame_size =
328        aux_array_size +
329        envp_array_size +
330        argv_array_size +
331        argc_size;
332
333    //There needs to be padding after the auxiliary vector data so that the
334    //very bottom of the stack is aligned properly.
335    int partial_size = frame_size + aux_data_size;
336    warn("The partial size is %d.\n", partial_size);
337    int aligned_partial_size = roundUp(partial_size, align);
338    warn("The aligned partial size is %d.\n", aligned_partial_size);
339    int aux_padding = aligned_partial_size - partial_size;
340    warn("The padding is %d.\n", aux_padding);
341
342    int space_needed =
343        info_block_size +
344        aux_data_size +
345        aux_padding +
346        frame_size;
347
348    stack_min = stack_base - space_needed;
349    stack_min = roundDown(stack_min, align);
350    stack_size = stack_base - stack_min;
351
352    // map memory
353    pTable->allocate(roundDown(stack_min, pageSize),
354                     roundUp(stack_size, pageSize));
355
356    // map out initial stack contents
357    Addr sentry_base = stack_base - sentry_size;
358    Addr file_name_base = sentry_base - file_name_size;
359    Addr env_data_base = file_name_base - env_data_size;
360    Addr arg_data_base = env_data_base - arg_data_size;
361    Addr aux_data_base = arg_data_base - info_block_padding - aux_data_size;
362    Addr auxv_array_base = aux_data_base - aux_array_size - aux_padding;
363    Addr envp_array_base = auxv_array_base - envp_array_size;
364    Addr argv_array_base = envp_array_base - argv_array_size;
365    Addr argc_base = argv_array_base - argc_size;
366
367    DPRINTF(X86, "The addresses of items on the initial stack:\n");
368    DPRINTF(X86, "0x%x - file name\n", file_name_base);
369    DPRINTF(X86, "0x%x - env data\n", env_data_base);
370    DPRINTF(X86, "0x%x - arg data\n", arg_data_base);
371    DPRINTF(X86, "0x%x - aux data\n", aux_data_base);
372    DPRINTF(X86, "0x%x - auxv array\n", auxv_array_base);
373    DPRINTF(X86, "0x%x - envp array\n", envp_array_base);
374    DPRINTF(X86, "0x%x - argv array\n", argv_array_base);
375    DPRINTF(X86, "0x%x - argc \n", argc_base);
376    DPRINTF(X86, "0x%x - stack min\n", stack_min);
377
378    // write contents to stack
379
380    // figure out argc
381    uint64_t argc = argv.size();
382    uint64_t guestArgc = TheISA::htog(argc);
383
384    //Write out the sentry void *
385    uint64_t sentry_NULL = 0;
386    initVirtMem->writeBlob(sentry_base,
387            (uint8_t*)&sentry_NULL, sentry_size);
388
389    //Write the file name
390    initVirtMem->writeString(file_name_base, filename.c_str());
391
392    //Fix up the aux vector which points to the "platform" string
393    assert(auxv[auxv.size() - 1].a_type = M5_AT_PLATFORM);
394    auxv[auxv.size() - 1].a_val = aux_data_base;
395
396    //Copy the aux stuff
397    for(int x = 0; x < auxv.size(); x++)
398    {
399        initVirtMem->writeBlob(auxv_array_base + x * 2 * intSize,
400                (uint8_t*)&(auxv[x].a_type), intSize);
401        initVirtMem->writeBlob(auxv_array_base + (x * 2 + 1) * intSize,
402                (uint8_t*)&(auxv[x].a_val), intSize);
403    }
404    //Write out the terminating zeroed auxilliary vector
405    const uint64_t zero = 0;
406    initVirtMem->writeBlob(auxv_array_base + 2 * intSize * auxv.size(),
407            (uint8_t*)&zero, 2 * intSize);
408
409    initVirtMem->writeString(aux_data_base, platform.c_str());
410
411    copyStringArray(envp, envp_array_base, env_data_base, initVirtMem);
412    copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem);
413
414    initVirtMem->writeBlob(argc_base, (uint8_t*)&guestArgc, intSize);
415
416    //Set the stack pointer register
417    threadContexts[0]->setIntReg(StackPointerReg, stack_min);
418
419    Addr prog_entry = objFile->entryPoint();
420    threadContexts[0]->setPC(prog_entry);
421    threadContexts[0]->setNextPC(prog_entry + sizeof(MachInst));
422
423    //Align the "stack_min" to a page boundary.
424    stack_min = roundDown(stack_min, pageSize);
425
426//    num_processes++;
427}
428