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