process.cc revision 2976
1/*
2 * Copyright (c) 2003-2004 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#include "arch/sparc/isa_traits.hh"
33#include "arch/sparc/process.hh"
34#include "base/loader/object_file.hh"
35#include "base/loader/elf_object.hh"
36#include "base/misc.hh"
37#include "cpu/thread_context.hh"
38#include "mem/page_table.hh"
39#include "mem/translating_port.hh"
40#include "sim/system.hh"
41
42using namespace std;
43using namespace SparcISA;
44
45
46SparcLiveProcess::SparcLiveProcess(const std::string &nm, ObjectFile *objFile,
47        System *_system, int stdin_fd, int stdout_fd, int stderr_fd,
48        std::vector<std::string> &argv, std::vector<std::string> &envp)
49    : LiveProcess(nm, objFile, _system, stdin_fd, stdout_fd, stderr_fd,
50        argv, envp)
51{
52
53    // XXX all the below need to be updated for SPARC - Ali
54    brk_point = objFile->dataBase() + objFile->dataSize() + objFile->bssSize();
55    brk_point = roundUp(brk_point, VMPageSize);
56
57    // Set up stack. On SPARC Linux, stack goes from the top of memory
58    // downward, less the hole for the kernel address space.
59    stack_base = ((Addr)0x80000000000ULL);
60
61    // Set up region for mmaps.  Tru64 seems to start just above 0 and
62    // grow up from there.
63    mmap_start = mmap_end = 0x800000;
64
65    // Set pointer for next thread stack.  Reserve 8M for main stack.
66    next_thread_stack_base = stack_base - (8 * 1024 * 1024);
67}
68
69void
70SparcLiveProcess::startup()
71{
72    argsInit(MachineBytes, VMPageSize);
73
74    //From the SPARC ABI
75
76    //The process runs in user mode
77    threadContexts[0]->setMiscRegWithEffect(MISCREG_PSTATE, 0x02);
78
79    //Setup default FP state
80    threadContexts[0]->setMiscReg(MISCREG_FSR, 0);
81
82    threadContexts[0]->setMiscReg(MISCREG_TICK, 0);
83    //
84    /*
85     * Register window management registers
86     */
87
88    //No windows contain info from other programs
89    threadContexts[0]->setMiscRegWithEffect(MISCREG_OTHERWIN, 0);
90    //There are no windows to pop
91    threadContexts[0]->setMiscRegWithEffect(MISCREG_CANRESTORE, 0);
92    //All windows are available to save into
93    threadContexts[0]->setMiscRegWithEffect(MISCREG_CANSAVE, NWindows - 2);
94    //All windows are "clean"
95    threadContexts[0]->setMiscRegWithEffect(MISCREG_CLEANWIN, NWindows);
96    //Start with register window 0
97    threadContexts[0]->setMiscRegWithEffect(MISCREG_CWP, 0);
98}
99
100m5_auxv_t buildAuxVect(int64_t type, int64_t val)
101{
102    m5_auxv_t result;
103    result.a_type = TheISA::htog(type);
104    result.a_val = TheISA::htog(val);
105    return result;
106}
107
108void
109SparcLiveProcess::argsInit(int intSize, int pageSize)
110{
111    Process::startup();
112
113    Addr alignmentMask = ~(intSize - 1);
114
115    // load object file into target memory
116    objFile->loadSections(initVirtMem);
117
118    //These are the auxilliary vector types
119    enum auxTypes
120    {
121        SPARC_AT_HWCAP = 16,
122        SPARC_AT_PAGESZ = 6,
123        SPARC_AT_CLKTCK = 17,
124        SPARC_AT_PHDR = 3,
125        SPARC_AT_PHENT = 4,
126        SPARC_AT_PHNUM = 5,
127        SPARC_AT_BASE = 7,
128        SPARC_AT_FLAGS = 8,
129        SPARC_AT_ENTRY = 9,
130        SPARC_AT_UID = 11,
131        SPARC_AT_EUID = 12,
132        SPARC_AT_GID = 13,
133        SPARC_AT_EGID = 14,
134        SPARC_AT_SECURE = 23
135    };
136
137    enum hardwareCaps
138    {
139        M5_HWCAP_SPARC_FLUSH = 1,
140        M5_HWCAP_SPARC_STBAR = 2,
141        M5_HWCAP_SPARC_SWAP = 4,
142        M5_HWCAP_SPARC_MULDIV = 8,
143        M5_HWCAP_SPARC_V9 = 16,
144        //This one should technically only be set
145        //if there is a cheetah or cheetah_plus tlb,
146        //but we'll use it all the time
147        M5_HWCAP_SPARC_ULTRA3 = 32
148    };
149
150    const int64_t hwcap =
151        M5_HWCAP_SPARC_FLUSH |
152        M5_HWCAP_SPARC_STBAR |
153        M5_HWCAP_SPARC_SWAP |
154        M5_HWCAP_SPARC_MULDIV |
155        M5_HWCAP_SPARC_V9 |
156        M5_HWCAP_SPARC_ULTRA3;
157
158
159    //Setup the auxilliary vectors. These will already have endian conversion.
160    //Auxilliary vectors are loaded only for elf formatted executables.
161    ElfObject * elfObject = dynamic_cast<ElfObject *>(objFile);
162    if(elfObject)
163    {
164        //Bits which describe the system hardware capabilities
165        auxv.push_back(buildAuxVect(SPARC_AT_HWCAP, hwcap));
166        //The system page size
167        auxv.push_back(buildAuxVect(SPARC_AT_PAGESZ, SparcISA::VMPageSize));
168        //Defined to be 100 in the kernel source.
169        //Frequency at which times() increments
170        auxv.push_back(buildAuxVect(SPARC_AT_CLKTCK, 100));
171        // For statically linked executables, this is the virtual address of the
172        // program header tables if they appear in the executable image
173        auxv.push_back(buildAuxVect(SPARC_AT_PHDR, elfObject->programHeaderTable()));
174        // This is the size of a program header entry from the elf file.
175        auxv.push_back(buildAuxVect(SPARC_AT_PHENT, elfObject->programHeaderSize()));
176        // This is the number of program headers from the original elf file.
177        auxv.push_back(buildAuxVect(SPARC_AT_PHNUM, elfObject->programHeaderCount()));
178        //This is the address of the elf "interpreter", It should be set
179        //to 0 for regular executables. It should be something else
180        //(not sure what) for dynamic libraries.
181        auxv.push_back(buildAuxVect(SPARC_AT_BASE, 0));
182        //This is hardwired to 0 in the elf loading code in the kernel
183        auxv.push_back(buildAuxVect(SPARC_AT_FLAGS, 0));
184        //The entry point to the program
185        auxv.push_back(buildAuxVect(SPARC_AT_ENTRY, objFile->entryPoint()));
186        //Different user and group IDs
187        auxv.push_back(buildAuxVect(SPARC_AT_UID, 100));
188        auxv.push_back(buildAuxVect(SPARC_AT_EUID, 100));
189        auxv.push_back(buildAuxVect(SPARC_AT_GID, 100));
190        auxv.push_back(buildAuxVect(SPARC_AT_EGID, 100));
191        //Whether to enable "secure mode" in the executable
192        auxv.push_back(buildAuxVect(SPARC_AT_SECURE, 0));
193    }
194
195    //Figure out how big the initial stack needs to be
196
197    //Each auxilliary vector is two 8 byte words
198    int aux_data_size = 2 * intSize * auxv.size();
199    int env_data_size = 0;
200    for (int i = 0; i < envp.size(); ++i) {
201        env_data_size += envp[i].size() + 1;
202    }
203    int arg_data_size = 0;
204    for (int i = 0; i < argv.size(); ++i) {
205        arg_data_size += argv[i].size() + 1;
206    }
207
208    int aux_array_size = intSize * 2 * (auxv.size() + 1);
209
210    int argv_array_size = intSize * (argv.size() + 1);
211    int envp_array_size = intSize * (envp.size() + 1);
212
213    int argc_size = intSize;
214    int window_save_size = intSize * 16;
215
216    int info_block_size =
217        (aux_data_size +
218        env_data_size +
219        arg_data_size +
220        ~alignmentMask) & alignmentMask;
221
222    int info_block_padding =
223        info_block_size -
224        aux_data_size -
225        env_data_size -
226        arg_data_size;
227
228    int space_needed =
229        info_block_size +
230        aux_array_size +
231        envp_array_size +
232        argv_array_size +
233        argc_size +
234        window_save_size;
235
236    stack_min = stack_base - space_needed;
237    stack_min &= alignmentMask;
238    stack_size = stack_base - stack_min;
239
240    // map memory
241    pTable->allocate(roundDown(stack_min, pageSize),
242                     roundUp(stack_size, pageSize));
243
244    // map out initial stack contents
245    Addr aux_data_base = stack_base - aux_data_size - info_block_padding;
246    Addr env_data_base = aux_data_base - env_data_size;
247    Addr arg_data_base = env_data_base - arg_data_size;
248    Addr auxv_array_base = arg_data_base - aux_array_size;
249    Addr envp_array_base = auxv_array_base - envp_array_size;
250    Addr argv_array_base = envp_array_base - argv_array_size;
251    Addr argc_base = argv_array_base - argc_size;
252    Addr window_save_base = argc_base - window_save_size;
253
254    DPRINTF(Sparc, "The addresses of items on the initial stack:\n");
255    DPRINTF(Sparc, "0x%x - aux data\n", aux_data_base);
256    DPRINTF(Sparc, "0x%x - env data\n", env_data_base);
257    DPRINTF(Sparc, "0x%x - arg data\n", arg_data_base);
258    DPRINTF(Sparc, "0x%x - auxv array\n", auxv_array_base);
259    DPRINTF(Sparc, "0x%x - envp array\n", envp_array_base);
260    DPRINTF(Sparc, "0x%x - argv array\n", argv_array_base);
261    DPRINTF(Sparc, "0x%x - argc \n", argc_base);
262    DPRINTF(Sparc, "0x%x - window save\n", window_save_base);
263    DPRINTF(Sparc, "0x%x - stack min\n", stack_min);
264
265    // write contents to stack
266    uint64_t argc = argv.size();
267    uint64_t guestArgc = TheISA::htog(argc);
268
269    //Copy the aux stuff
270    for(int x = 0; x < auxv.size(); x++)
271    {
272        initVirtMem->writeBlob(auxv_array_base + x * 2 * intSize,
273                (uint8_t*)&(auxv[x].a_type), intSize);
274        initVirtMem->writeBlob(auxv_array_base + (x * 2 + 1) * intSize,
275                (uint8_t*)&(auxv[x].a_val), intSize);
276    }
277    //Write out the terminating zeroed auxilliary vector
278    const uint64_t zero = 0;
279    initVirtMem->writeBlob(auxv_array_base + 2 * intSize * auxv.size(),
280            (uint8_t*)&zero, 2 * intSize);
281
282    copyStringArray(envp, envp_array_base, env_data_base, initVirtMem);
283    copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem);
284
285    initVirtMem->writeBlob(argc_base, (uint8_t*)&guestArgc, intSize);
286
287    threadContexts[0]->setIntReg(ArgumentReg0, argc);
288    threadContexts[0]->setIntReg(ArgumentReg1, argv_array_base);
289    threadContexts[0]->setIntReg(StackPointerReg, stack_min - StackBias);
290
291    Addr prog_entry = objFile->entryPoint();
292    threadContexts[0]->setPC(prog_entry);
293    threadContexts[0]->setNextPC(prog_entry + sizeof(MachInst));
294    threadContexts[0]->setNextNPC(prog_entry + (2 * sizeof(MachInst)));
295
296//    num_processes++;
297}
298