process.cc revision 11886
1/*
2 * Copyright (c) 2004-2005 The Regents of The University of Michigan
3 * Copyright (c) 2016 The University of Virginia
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: Gabe Black
30 *          Ali Saidi
31 *          Korey Sewell
32 *          Alec Roelke
33 */
34#include "arch/riscv/process.hh"
35
36#include <vector>
37
38#include "arch/riscv/isa_traits.hh"
39#include "base/loader/elf_object.hh"
40#include "base/loader/object_file.hh"
41#include "base/misc.hh"
42#include "cpu/thread_context.hh"
43#include "debug/Loader.hh"
44#include "mem/page_table.hh"
45#include "sim/aux_vector.hh"
46#include "sim/process.hh"
47#include "sim/process_impl.hh"
48#include "sim/syscall_return.hh"
49#include "sim/system.hh"
50
51using namespace std;
52using namespace RiscvISA;
53
54RiscvProcess::RiscvProcess(ProcessParams * params,
55    ObjectFile *objFile) : Process(params, objFile)
56{
57    // Set up stack. On RISC-V, stack starts at the top of kuseg
58    // user address space. RISC-V stack grows down from here
59    memState->stackBase = (Addr)0x7FFFFFFF;
60
61    // Set pointer for next thread stack.  Reserve 8M for main stack.
62    memState->nextThreadStackBase = memState->stackBase - (8 * 1024 * 1024);
63
64    // Set up break point (Top of Heap)
65    memState->brkPoint = objFile->bssBase() + objFile->bssSize();
66
67    // Set up region for mmaps.  Start it 1GB above the top of the heap.
68    memState->mmapEnd = memState->brkPoint + 0x40000000L;
69}
70
71void
72RiscvProcess::initState()
73{
74    Process::initState();
75
76    argsInit<uint64_t>(PageBytes);
77}
78
79template<class IntType> void
80RiscvProcess::argsInit(int pageSize)
81{
82    updateBias();
83
84    // load object file into target memory
85    objFile->loadSections(initVirtMem);
86
87    typedef AuxVector<IntType> auxv_t;
88    vector<auxv_t> auxv;
89    ElfObject * elfObject = dynamic_cast<ElfObject *>(objFile);
90    if (elfObject) {
91        // Set the system page size
92        auxv.push_back(auxv_t(M5_AT_PAGESZ, RiscvISA::PageBytes));
93        // Set the frequency at which time() increments
94        auxv.push_back(auxv_t(M5_AT_CLKTCK, 100));
95        // For statically linked executables, this is the virtual
96        // address of the program header tables if they appear in the
97        // executable image.
98        auxv.push_back(auxv_t(M5_AT_PHDR, elfObject->programHeaderTable()));
99        DPRINTF(Loader, "auxv at PHDR %08p\n",
100            elfObject->programHeaderTable());
101        // This is the size of a program header entry from the elf file.
102        auxv.push_back(auxv_t(M5_AT_PHENT, elfObject->programHeaderSize()));
103        // This is the number of program headers from the original elf file.
104        auxv.push_back(auxv_t(M5_AT_PHNUM, elfObject->programHeaderCount()));
105        auxv.push_back(auxv_t(M5_AT_BASE, getBias()));
106        //The entry point to the program
107        auxv.push_back(auxv_t(M5_AT_ENTRY, objFile->entryPoint()));
108        //Different user and group IDs
109        auxv.push_back(auxv_t(M5_AT_UID, uid()));
110        auxv.push_back(auxv_t(M5_AT_EUID, euid()));
111        auxv.push_back(auxv_t(M5_AT_GID, gid()));
112        auxv.push_back(auxv_t(M5_AT_EGID, egid()));
113    }
114
115    const IntType zero = 0;
116    IntType argc = htog((IntType)argv.size());
117    int argv_array_size = sizeof(Addr) * argv.size();
118    int arg_data_size = 0;
119    for (string arg: argv)
120        arg_data_size += arg.size() + 1;
121    int envp_array_size = sizeof(Addr) * envp.size();
122    int env_data_size = 0;
123    for (string env: envp)
124        env_data_size += env.size() + 1;
125    int auxv_array_size = 2 * sizeof(IntType)*auxv.size();
126
127    memState->stackSize = sizeof(IntType) + argv_array_size + 2 *
128        sizeof(Addr) + arg_data_size + 2 * sizeof(Addr);
129    if (!envp.empty()) {
130        memState->stackSize += 2 * sizeof(Addr) + envp_array_size + 2 *
131            sizeof(Addr) + env_data_size;
132    }
133    if (!auxv.empty())
134        memState->stackSize += 2 * sizeof(Addr) + auxv_array_size;
135    memState->stackMin = roundDown(memState->stackBase - memState->stackSize,
136                                   pageSize);
137    allocateMem(memState->stackMin, roundUp(memState->stackSize, pageSize));
138
139    Addr argv_array_base = memState->stackMin + sizeof(IntType);
140    Addr arg_data_base = argv_array_base + argv_array_size + 2 * sizeof(Addr);
141    Addr envp_array_base = arg_data_base + arg_data_size;
142    if (!envp.empty())
143        envp_array_base += 2 * sizeof(Addr);
144    Addr env_data_base = envp_array_base + envp_array_size;
145    if (!envp.empty())
146        env_data_base += 2 * sizeof(Addr);
147
148    vector<Addr> arg_pointers;
149    if (!argv.empty()) {
150        arg_pointers.push_back(arg_data_base);
151        for (int i = 0; i < argv.size() - 1; i++) {
152            arg_pointers.push_back(arg_pointers[i] + argv[i].size() + 1);
153        }
154    }
155
156    vector<Addr> env_pointers;
157    if (!envp.empty()) {
158        env_pointers.push_back(env_data_base);
159        for (int i = 0; i < envp.size() - 1; i++) {
160            env_pointers.push_back(env_pointers[i] + envp[i].size() + 1);
161        }
162    }
163
164    Addr sp = memState->stackMin;
165    initVirtMem.writeBlob(sp, (uint8_t *)&argc, sizeof(IntType));
166    sp += sizeof(IntType);
167    for (Addr arg_pointer: arg_pointers) {
168        initVirtMem.writeBlob(sp, (uint8_t *)&arg_pointer, sizeof(Addr));
169        sp += sizeof(Addr);
170    }
171    for (int i = 0; i < 2; i++) {
172        initVirtMem.writeBlob(sp, (uint8_t *)&zero, sizeof(Addr));
173        sp += sizeof(Addr);
174    }
175    for (int i = 0; i < argv.size(); i++) {
176        initVirtMem.writeString(sp, argv[i].c_str());
177        sp += argv[i].size() + 1;
178    }
179    if (!envp.empty()) {
180        for (int i = 0; i < 2; i++) {
181            initVirtMem.writeBlob(sp, (uint8_t *)&zero, sizeof(Addr));
182            sp += sizeof(Addr);
183        }
184    }
185    for (Addr env_pointer: env_pointers)
186        initVirtMem.writeBlob(sp, (uint8_t *)&env_pointer, sizeof(Addr));
187    if (!envp.empty()) {
188        for (int i = 0; i < 2; i++) {
189            initVirtMem.writeBlob(sp, (uint8_t *)&zero, sizeof(Addr));
190            sp += sizeof(Addr);
191        }
192    }
193    for (int i = 0; i < envp.size(); i++) {
194        initVirtMem.writeString(sp, envp[i].c_str());
195        sp += envp[i].size() + 1;
196    }
197    if (!auxv.empty()) {
198        for (int i = 0; i < 2; i++) {
199            initVirtMem.writeBlob(sp, (uint8_t *)&zero, sizeof(Addr));
200            sp += sizeof(Addr);
201        }
202    }
203    for (auxv_t aux: auxv) {
204        initVirtMem.writeBlob(sp, (uint8_t *)&aux.a_type, sizeof(IntType));
205        initVirtMem.writeBlob(sp + sizeof(IntType), (uint8_t *)&aux.a_val,
206            sizeof(IntType));
207        sp += 2 * sizeof(IntType);
208    }
209    for (int i = 0; i < 2; i++) {
210        initVirtMem.writeBlob(sp, (uint8_t *)&zero, sizeof(Addr));
211        sp += sizeof(Addr);
212    }
213
214    ThreadContext *tc = system->getThreadContext(contextIds[0]);
215    tc->setIntReg(StackPointerReg, memState->stackMin);
216    tc->pcState(getStartPC());
217}
218
219RiscvISA::IntReg
220RiscvProcess::getSyscallArg(ThreadContext *tc, int &i)
221{
222    // RISC-V only has four system call argument registers by convention, so
223    // if a larger index is requested return 0
224    RiscvISA::IntReg retval = 0;
225    if (i < 4)
226        retval = tc->readIntReg(SyscallArgumentRegs[i]);
227    i++;
228    return retval;
229}
230
231void
232RiscvProcess::setSyscallArg(ThreadContext *tc, int i, RiscvISA::IntReg val)
233{
234    tc->setIntReg(SyscallArgumentRegs[i], val);
235}
236
237void
238RiscvProcess::setSyscallReturn(ThreadContext *tc, SyscallReturn sysret)
239{
240    if (sysret.successful()) {
241        // no error
242        tc->setIntReg(SyscallPseudoReturnReg, sysret.returnValue());
243    } else {
244        // got an error, return details
245        tc->setIntReg(SyscallPseudoReturnReg, sysret.errnoValue());
246    }
247}
248