process.cc (11389:1e55f16160cb) process.cc (11793:ef606668d247)
1/*
2 * Copyright (c) 2004-2005 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 * Korey Sewell
31 */
32
1/*
2 * Copyright (c) 2004-2005 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 * Korey Sewell
31 */
32
33#include "arch/mips/isa_traits.hh"
34#include "arch/mips/process.hh"
33#include "arch/mips/process.hh"
34
35#include "arch/mips/isa_traits.hh"
35#include "base/loader/elf_object.hh"
36#include "base/loader/object_file.hh"
37#include "base/misc.hh"
38#include "cpu/thread_context.hh"
39#include "debug/Loader.hh"
40#include "mem/page_table.hh"
41#include "sim/process.hh"
42#include "sim/process_impl.hh"
43#include "sim/system.hh"
44
45using namespace std;
46using namespace MipsISA;
47
48MipsLiveProcess::MipsLiveProcess(LiveProcessParams * params,
49 ObjectFile *objFile)
50 : LiveProcess(params, objFile)
51{
52 // Set up stack. On MIPS, stack starts at the top of kuseg
53 // user address space. MIPS stack grows down from here
54 stack_base = 0x7FFFFFFF;
55
56 // Set pointer for next thread stack. Reserve 8M for main stack.
57 next_thread_stack_base = stack_base - (8 * 1024 * 1024);
58
59 // Set up break point (Top of Heap)
60 brk_point = objFile->dataBase() + objFile->dataSize() + objFile->bssSize();
61 brk_point = roundUp(brk_point, PageBytes);
62
63 // Set up region for mmaps. Start it 1GB above the top of the heap.
64 mmap_end = brk_point + 0x40000000L;
65}
66
67void
68MipsLiveProcess::initState()
69{
70 LiveProcess::initState();
71
72 argsInit<uint32_t>(PageBytes);
73}
74
75template<class IntType>
76void
77MipsLiveProcess::argsInit(int pageSize)
78{
79 int intSize = sizeof(IntType);
80
81 // Patch the ld_bias for dynamic executables.
82 updateBias();
83
84 // load object file into target memory
85 objFile->loadSections(initVirtMem);
86
87 typedef AuxVector<IntType> auxv_t;
88 std::vector<auxv_t> auxv;
89
90 ElfObject * elfObject = dynamic_cast<ElfObject *>(objFile);
91 if (elfObject)
92 {
93 // Set the system page size
94 auxv.push_back(auxv_t(M5_AT_PAGESZ, MipsISA::PageBytes));
95 // Set the frequency at which time() increments
96 auxv.push_back(auxv_t(M5_AT_CLKTCK, 100));
97 // For statically linked executables, this is the virtual
98 // address of the program header tables if they appear in the
99 // executable image.
100 auxv.push_back(auxv_t(M5_AT_PHDR, elfObject->programHeaderTable()));
101 DPRINTF(Loader, "auxv at PHDR %08p\n", elfObject->programHeaderTable());
102 // This is the size of a program header entry from the elf file.
103 auxv.push_back(auxv_t(M5_AT_PHENT, elfObject->programHeaderSize()));
104 // This is the number of program headers from the original elf file.
105 auxv.push_back(auxv_t(M5_AT_PHNUM, elfObject->programHeaderCount()));
106 // This is the base address of the ELF interpreter; it should be
107 // zero for static executables or contain the base address for
108 // dynamic executables.
109 auxv.push_back(auxv_t(M5_AT_BASE, getBias()));
110 //The entry point to the program
111 auxv.push_back(auxv_t(M5_AT_ENTRY, objFile->entryPoint()));
112 //Different user and group IDs
113 auxv.push_back(auxv_t(M5_AT_UID, uid()));
114 auxv.push_back(auxv_t(M5_AT_EUID, euid()));
115 auxv.push_back(auxv_t(M5_AT_GID, gid()));
116 auxv.push_back(auxv_t(M5_AT_EGID, egid()));
117 }
118
119 // Calculate how much space we need for arg & env & auxv arrays.
120 int argv_array_size = intSize * (argv.size() + 1);
121 int envp_array_size = intSize * (envp.size() + 1);
122 int auxv_array_size = intSize * 2 * (auxv.size() + 1);
123
124 int arg_data_size = 0;
125 for (vector<string>::size_type i = 0; i < argv.size(); ++i) {
126 arg_data_size += argv[i].size() + 1;
127 }
128 int env_data_size = 0;
129 for (vector<string>::size_type i = 0; i < envp.size(); ++i) {
130 env_data_size += envp[i].size() + 1;
131 }
132
133 int space_needed =
134 argv_array_size +
135 envp_array_size +
136 auxv_array_size +
137 arg_data_size +
138 env_data_size;
139
140 // set bottom of stack
141 stack_min = stack_base - space_needed;
142 // align it
143 stack_min = roundDown(stack_min, pageSize);
144 stack_size = stack_base - stack_min;
145 // map memory
146 allocateMem(stack_min, roundUp(stack_size, pageSize));
147
148 // map out initial stack contents
149 IntType argv_array_base = stack_min + intSize; // room for argc
150 IntType envp_array_base = argv_array_base + argv_array_size;
151 IntType auxv_array_base = envp_array_base + envp_array_size;
152 IntType arg_data_base = auxv_array_base + auxv_array_size;
153 IntType env_data_base = arg_data_base + arg_data_size;
154
155 // write contents to stack
156 IntType argc = argv.size();
157
158 argc = htog((IntType)argc);
159
160 initVirtMem.writeBlob(stack_min, (uint8_t*)&argc, intSize);
161
162 copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem);
163
164 copyStringArray(envp, envp_array_base, env_data_base, initVirtMem);
165
166 // Copy the aux vector
167 for (typename vector<auxv_t>::size_type x = 0; x < auxv.size(); x++) {
168 initVirtMem.writeBlob(auxv_array_base + x * 2 * intSize,
169 (uint8_t*)&(auxv[x].a_type), intSize);
170 initVirtMem.writeBlob(auxv_array_base + (x * 2 + 1) * intSize,
171 (uint8_t*)&(auxv[x].a_val), intSize);
172 }
173
174 // Write out the terminating zeroed auxilliary vector
175 for (unsigned i = 0; i < 2; i++) {
176 const IntType zero = 0;
177 const Addr addr = auxv_array_base + 2 * intSize * (auxv.size() + i);
178 initVirtMem.writeBlob(addr, (uint8_t*)&zero, intSize);
179 }
180
181 ThreadContext *tc = system->getThreadContext(contextIds[0]);
182
183 setSyscallArg(tc, 0, argc);
184 setSyscallArg(tc, 1, argv_array_base);
185 tc->setIntReg(StackPointerReg, stack_min);
186
187 tc->pcState(getStartPC());
188}
189
190
191MipsISA::IntReg
192MipsLiveProcess::getSyscallArg(ThreadContext *tc, int &i)
193{
194 assert(i < 6);
195 return tc->readIntReg(FirstArgumentReg + i++);
196}
197
198void
199MipsLiveProcess::setSyscallArg(ThreadContext *tc,
200 int i, MipsISA::IntReg val)
201{
202 assert(i < 6);
203 tc->setIntReg(FirstArgumentReg + i, val);
204}
205
206void
207MipsLiveProcess::setSyscallReturn(ThreadContext *tc, SyscallReturn sysret)
208{
209 if (sysret.successful()) {
210 // no error
211 tc->setIntReg(SyscallSuccessReg, 0);
212 tc->setIntReg(ReturnValueReg, sysret.returnValue());
213 } else {
214 // got an error, return details
215 tc->setIntReg(SyscallSuccessReg, (IntReg) -1);
216 tc->setIntReg(ReturnValueReg, sysret.errnoValue());
217 }
218}
36#include "base/loader/elf_object.hh"
37#include "base/loader/object_file.hh"
38#include "base/misc.hh"
39#include "cpu/thread_context.hh"
40#include "debug/Loader.hh"
41#include "mem/page_table.hh"
42#include "sim/process.hh"
43#include "sim/process_impl.hh"
44#include "sim/system.hh"
45
46using namespace std;
47using namespace MipsISA;
48
49MipsLiveProcess::MipsLiveProcess(LiveProcessParams * params,
50 ObjectFile *objFile)
51 : LiveProcess(params, objFile)
52{
53 // Set up stack. On MIPS, stack starts at the top of kuseg
54 // user address space. MIPS stack grows down from here
55 stack_base = 0x7FFFFFFF;
56
57 // Set pointer for next thread stack. Reserve 8M for main stack.
58 next_thread_stack_base = stack_base - (8 * 1024 * 1024);
59
60 // Set up break point (Top of Heap)
61 brk_point = objFile->dataBase() + objFile->dataSize() + objFile->bssSize();
62 brk_point = roundUp(brk_point, PageBytes);
63
64 // Set up region for mmaps. Start it 1GB above the top of the heap.
65 mmap_end = brk_point + 0x40000000L;
66}
67
68void
69MipsLiveProcess::initState()
70{
71 LiveProcess::initState();
72
73 argsInit<uint32_t>(PageBytes);
74}
75
76template<class IntType>
77void
78MipsLiveProcess::argsInit(int pageSize)
79{
80 int intSize = sizeof(IntType);
81
82 // Patch the ld_bias for dynamic executables.
83 updateBias();
84
85 // load object file into target memory
86 objFile->loadSections(initVirtMem);
87
88 typedef AuxVector<IntType> auxv_t;
89 std::vector<auxv_t> auxv;
90
91 ElfObject * elfObject = dynamic_cast<ElfObject *>(objFile);
92 if (elfObject)
93 {
94 // Set the system page size
95 auxv.push_back(auxv_t(M5_AT_PAGESZ, MipsISA::PageBytes));
96 // Set the frequency at which time() increments
97 auxv.push_back(auxv_t(M5_AT_CLKTCK, 100));
98 // For statically linked executables, this is the virtual
99 // address of the program header tables if they appear in the
100 // executable image.
101 auxv.push_back(auxv_t(M5_AT_PHDR, elfObject->programHeaderTable()));
102 DPRINTF(Loader, "auxv at PHDR %08p\n", elfObject->programHeaderTable());
103 // This is the size of a program header entry from the elf file.
104 auxv.push_back(auxv_t(M5_AT_PHENT, elfObject->programHeaderSize()));
105 // This is the number of program headers from the original elf file.
106 auxv.push_back(auxv_t(M5_AT_PHNUM, elfObject->programHeaderCount()));
107 // This is the base address of the ELF interpreter; it should be
108 // zero for static executables or contain the base address for
109 // dynamic executables.
110 auxv.push_back(auxv_t(M5_AT_BASE, getBias()));
111 //The entry point to the program
112 auxv.push_back(auxv_t(M5_AT_ENTRY, objFile->entryPoint()));
113 //Different user and group IDs
114 auxv.push_back(auxv_t(M5_AT_UID, uid()));
115 auxv.push_back(auxv_t(M5_AT_EUID, euid()));
116 auxv.push_back(auxv_t(M5_AT_GID, gid()));
117 auxv.push_back(auxv_t(M5_AT_EGID, egid()));
118 }
119
120 // Calculate how much space we need for arg & env & auxv arrays.
121 int argv_array_size = intSize * (argv.size() + 1);
122 int envp_array_size = intSize * (envp.size() + 1);
123 int auxv_array_size = intSize * 2 * (auxv.size() + 1);
124
125 int arg_data_size = 0;
126 for (vector<string>::size_type i = 0; i < argv.size(); ++i) {
127 arg_data_size += argv[i].size() + 1;
128 }
129 int env_data_size = 0;
130 for (vector<string>::size_type i = 0; i < envp.size(); ++i) {
131 env_data_size += envp[i].size() + 1;
132 }
133
134 int space_needed =
135 argv_array_size +
136 envp_array_size +
137 auxv_array_size +
138 arg_data_size +
139 env_data_size;
140
141 // set bottom of stack
142 stack_min = stack_base - space_needed;
143 // align it
144 stack_min = roundDown(stack_min, pageSize);
145 stack_size = stack_base - stack_min;
146 // map memory
147 allocateMem(stack_min, roundUp(stack_size, pageSize));
148
149 // map out initial stack contents
150 IntType argv_array_base = stack_min + intSize; // room for argc
151 IntType envp_array_base = argv_array_base + argv_array_size;
152 IntType auxv_array_base = envp_array_base + envp_array_size;
153 IntType arg_data_base = auxv_array_base + auxv_array_size;
154 IntType env_data_base = arg_data_base + arg_data_size;
155
156 // write contents to stack
157 IntType argc = argv.size();
158
159 argc = htog((IntType)argc);
160
161 initVirtMem.writeBlob(stack_min, (uint8_t*)&argc, intSize);
162
163 copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem);
164
165 copyStringArray(envp, envp_array_base, env_data_base, initVirtMem);
166
167 // Copy the aux vector
168 for (typename vector<auxv_t>::size_type x = 0; x < auxv.size(); x++) {
169 initVirtMem.writeBlob(auxv_array_base + x * 2 * intSize,
170 (uint8_t*)&(auxv[x].a_type), intSize);
171 initVirtMem.writeBlob(auxv_array_base + (x * 2 + 1) * intSize,
172 (uint8_t*)&(auxv[x].a_val), intSize);
173 }
174
175 // Write out the terminating zeroed auxilliary vector
176 for (unsigned i = 0; i < 2; i++) {
177 const IntType zero = 0;
178 const Addr addr = auxv_array_base + 2 * intSize * (auxv.size() + i);
179 initVirtMem.writeBlob(addr, (uint8_t*)&zero, intSize);
180 }
181
182 ThreadContext *tc = system->getThreadContext(contextIds[0]);
183
184 setSyscallArg(tc, 0, argc);
185 setSyscallArg(tc, 1, argv_array_base);
186 tc->setIntReg(StackPointerReg, stack_min);
187
188 tc->pcState(getStartPC());
189}
190
191
192MipsISA::IntReg
193MipsLiveProcess::getSyscallArg(ThreadContext *tc, int &i)
194{
195 assert(i < 6);
196 return tc->readIntReg(FirstArgumentReg + i++);
197}
198
199void
200MipsLiveProcess::setSyscallArg(ThreadContext *tc,
201 int i, MipsISA::IntReg val)
202{
203 assert(i < 6);
204 tc->setIntReg(FirstArgumentReg + i, val);
205}
206
207void
208MipsLiveProcess::setSyscallReturn(ThreadContext *tc, SyscallReturn sysret)
209{
210 if (sysret.successful()) {
211 // no error
212 tc->setIntReg(SyscallSuccessReg, 0);
213 tc->setIntReg(ReturnValueReg, sysret.returnValue());
214 } else {
215 // got an error, return details
216 tc->setIntReg(SyscallSuccessReg, (IntReg) -1);
217 tc->setIntReg(ReturnValueReg, sysret.errnoValue());
218 }
219}