process.cc revision 12432
112855Sgabeblack@google.com/*
212855Sgabeblack@google.com * Copyright (c) 2004-2005 The Regents of The University of Michigan
312855Sgabeblack@google.com * All rights reserved.
412855Sgabeblack@google.com *
512855Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
612855Sgabeblack@google.com * modification, are permitted provided that the following conditions are
712855Sgabeblack@google.com * met: redistributions of source code must retain the above copyright
812855Sgabeblack@google.com * notice, this list of conditions and the following disclaimer;
912855Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright
1012855Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the
1112855Sgabeblack@google.com * documentation and/or other materials provided with the distribution;
1212855Sgabeblack@google.com * neither the name of the copyright holders nor the names of its
1312855Sgabeblack@google.com * contributors may be used to endorse or promote products derived from
1412855Sgabeblack@google.com * this software without specific prior written permission.
1512855Sgabeblack@google.com *
1612855Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1712855Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1812855Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1912855Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2012855Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2112855Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2212855Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2312855Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2412855Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2512855Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2612855Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2712855Sgabeblack@google.com *
2812855Sgabeblack@google.com * Authors: Gabe Black
2912855Sgabeblack@google.com *          Ali Saidi
3012855Sgabeblack@google.com *          Korey Sewell
3112855Sgabeblack@google.com */
3212855Sgabeblack@google.com
3312855Sgabeblack@google.com#include "arch/mips/process.hh"
3412855Sgabeblack@google.com
3512855Sgabeblack@google.com#include "arch/mips/isa_traits.hh"
3612855Sgabeblack@google.com#include "base/loader/elf_object.hh"
3712855Sgabeblack@google.com#include "base/loader/object_file.hh"
3812855Sgabeblack@google.com#include "base/logging.hh"
3912855Sgabeblack@google.com#include "cpu/thread_context.hh"
4012855Sgabeblack@google.com#include "debug/Loader.hh"
4112855Sgabeblack@google.com#include "mem/page_table.hh"
4212855Sgabeblack@google.com#include "params/Process.hh"
4312855Sgabeblack@google.com#include "sim/aux_vector.hh"
4412855Sgabeblack@google.com#include "sim/process.hh"
4512855Sgabeblack@google.com#include "sim/process_impl.hh"
4612855Sgabeblack@google.com#include "sim/syscall_return.hh"
4712855Sgabeblack@google.com#include "sim/system.hh"
4812855Sgabeblack@google.com
4912855Sgabeblack@google.comusing namespace std;
5012855Sgabeblack@google.comusing namespace MipsISA;
5112855Sgabeblack@google.com
5212855Sgabeblack@google.comMipsProcess::MipsProcess(ProcessParams *params, ObjectFile *objFile)
5312855Sgabeblack@google.com    : Process(params, new FuncPageTable(params->name, params->pid, PageBytes),
5412855Sgabeblack@google.com              objFile)
5512855Sgabeblack@google.com{
5612855Sgabeblack@google.com    fatal_if(!params->useArchPT, "Arch page tables not implemented.");
5712855Sgabeblack@google.com    // Set up stack. On MIPS, stack starts at the top of kuseg
5812855Sgabeblack@google.com    // user address space. MIPS stack grows down from here
5912855Sgabeblack@google.com    Addr stack_base = 0x7FFFFFFF;
6012855Sgabeblack@google.com
6112855Sgabeblack@google.com    Addr max_stack_size = 8 * 1024 * 1024;
6212855Sgabeblack@google.com
6312855Sgabeblack@google.com    // Set pointer for next thread stack.  Reserve 8M for main stack.
6412855Sgabeblack@google.com    Addr next_thread_stack_base = stack_base - max_stack_size;
6512855Sgabeblack@google.com
6612855Sgabeblack@google.com    // Set up break point (Top of Heap)
6712855Sgabeblack@google.com    Addr brk_point = objFile->dataBase() + objFile->dataSize() +
6812855Sgabeblack@google.com                     objFile->bssSize();
6912855Sgabeblack@google.com    brk_point = roundUp(brk_point, PageBytes);
7012855Sgabeblack@google.com
7112855Sgabeblack@google.com    // Set up region for mmaps.  Start it 1GB above the top of the heap.
7212855Sgabeblack@google.com    Addr mmap_end = brk_point + 0x40000000L;
7312855Sgabeblack@google.com
7412855Sgabeblack@google.com    memState = make_shared<MemState>(brk_point, stack_base, max_stack_size,
7512855Sgabeblack@google.com                                     next_thread_stack_base, mmap_end);
7612855Sgabeblack@google.com}
7712855Sgabeblack@google.com
78void
79MipsProcess::initState()
80{
81    Process::initState();
82
83    argsInit<uint32_t>(PageBytes);
84}
85
86template<class IntType>
87void
88MipsProcess::argsInit(int pageSize)
89{
90    int intSize = sizeof(IntType);
91
92    // Patch the ld_bias for dynamic executables.
93    updateBias();
94
95    // load object file into target memory
96    objFile->loadSections(initVirtMem);
97
98    typedef AuxVector<IntType> auxv_t;
99    std::vector<auxv_t> auxv;
100
101    ElfObject * elfObject = dynamic_cast<ElfObject *>(objFile);
102    if (elfObject)
103    {
104        // Set the system page size
105        auxv.push_back(auxv_t(M5_AT_PAGESZ, MipsISA::PageBytes));
106        // Set the frequency at which time() increments
107        auxv.push_back(auxv_t(M5_AT_CLKTCK, 100));
108        // For statically linked executables, this is the virtual
109        // address of the program header tables if they appear in the
110        // executable image.
111        auxv.push_back(auxv_t(M5_AT_PHDR, elfObject->programHeaderTable()));
112        DPRINTF(Loader, "auxv at PHDR %08p\n", elfObject->programHeaderTable());
113        // This is the size of a program header entry from the elf file.
114        auxv.push_back(auxv_t(M5_AT_PHENT, elfObject->programHeaderSize()));
115        // This is the number of program headers from the original elf file.
116        auxv.push_back(auxv_t(M5_AT_PHNUM, elfObject->programHeaderCount()));
117        // This is the base address of the ELF interpreter; it should be
118        // zero for static executables or contain the base address for
119        // dynamic executables.
120        auxv.push_back(auxv_t(M5_AT_BASE, getBias()));
121        //The entry point to the program
122        auxv.push_back(auxv_t(M5_AT_ENTRY, objFile->entryPoint()));
123        //Different user and group IDs
124        auxv.push_back(auxv_t(M5_AT_UID, uid()));
125        auxv.push_back(auxv_t(M5_AT_EUID, euid()));
126        auxv.push_back(auxv_t(M5_AT_GID, gid()));
127        auxv.push_back(auxv_t(M5_AT_EGID, egid()));
128    }
129
130    // Calculate how much space we need for arg & env & auxv arrays.
131    int argv_array_size = intSize * (argv.size() + 1);
132    int envp_array_size = intSize * (envp.size() + 1);
133    int auxv_array_size = intSize * 2 * (auxv.size() + 1);
134
135    int arg_data_size = 0;
136    for (vector<string>::size_type i = 0; i < argv.size(); ++i) {
137        arg_data_size += argv[i].size() + 1;
138    }
139    int env_data_size = 0;
140    for (vector<string>::size_type i = 0; i < envp.size(); ++i) {
141        env_data_size += envp[i].size() + 1;
142    }
143
144    int space_needed =
145        argv_array_size +
146        envp_array_size +
147        auxv_array_size +
148        arg_data_size +
149        env_data_size;
150
151    // set bottom of stack
152    memState->setStackMin(memState->getStackBase() - space_needed);
153    // align it
154    memState->setStackMin(roundDown(memState->getStackMin(), pageSize));
155    memState->setStackSize(memState->getStackBase() - memState->getStackMin());
156    // map memory
157    allocateMem(memState->getStackMin(), roundUp(memState->getStackSize(),
158                pageSize));
159
160    // map out initial stack contents; leave room for argc
161    IntType argv_array_base = memState->getStackMin() + intSize;
162    IntType envp_array_base = argv_array_base + argv_array_size;
163    IntType auxv_array_base = envp_array_base + envp_array_size;
164    IntType arg_data_base = auxv_array_base + auxv_array_size;
165    IntType env_data_base = arg_data_base + arg_data_size;
166
167    // write contents to stack
168    IntType argc = argv.size();
169
170    argc = htog((IntType)argc);
171
172    initVirtMem.writeBlob(memState->getStackMin(), (uint8_t*)&argc, intSize);
173
174    copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem);
175
176    copyStringArray(envp, envp_array_base, env_data_base, initVirtMem);
177
178    // Copy the aux vector
179    for (typename vector<auxv_t>::size_type x = 0; x < auxv.size(); x++) {
180        initVirtMem.writeBlob(auxv_array_base + x * 2 * intSize,
181                (uint8_t*)&(auxv[x].a_type), intSize);
182        initVirtMem.writeBlob(auxv_array_base + (x * 2 + 1) * intSize,
183                (uint8_t*)&(auxv[x].a_val), intSize);
184    }
185
186    // Write out the terminating zeroed auxilliary vector
187    for (unsigned i = 0; i < 2; i++) {
188        const IntType zero = 0;
189        const Addr addr = auxv_array_base + 2 * intSize * (auxv.size() + i);
190        initVirtMem.writeBlob(addr, (uint8_t*)&zero, intSize);
191    }
192
193    ThreadContext *tc = system->getThreadContext(contextIds[0]);
194
195    setSyscallArg(tc, 0, argc);
196    setSyscallArg(tc, 1, argv_array_base);
197    tc->setIntReg(StackPointerReg, memState->getStackMin());
198
199    tc->pcState(getStartPC());
200}
201
202
203MipsISA::IntReg
204MipsProcess::getSyscallArg(ThreadContext *tc, int &i)
205{
206    assert(i < 6);
207    return tc->readIntReg(FirstArgumentReg + i++);
208}
209
210void
211MipsProcess::setSyscallArg(ThreadContext *tc, int i, MipsISA::IntReg val)
212{
213    assert(i < 6);
214    tc->setIntReg(FirstArgumentReg + i, val);
215}
216
217void
218MipsProcess::setSyscallReturn(ThreadContext *tc, SyscallReturn sysret)
219{
220    if (sysret.successful()) {
221        // no error
222        tc->setIntReg(SyscallSuccessReg, 0);
223        tc->setIntReg(ReturnValueReg, sysret.returnValue());
224    } else {
225        // got an error, return details
226        tc->setIntReg(SyscallSuccessReg, (IntReg) -1);
227        tc->setIntReg(ReturnValueReg, sysret.errnoValue());
228    }
229}
230