Merge zizzer.eecs.umich.edu:/bk/newmem
[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 //Initialize these to 0s
71 fillStart = 0;
72 spillStart = 0;
73 }
74
75 void
76 SparcLiveProcess::startup()
77 {
78 argsInit(MachineBytes, VMPageSize);
79
80 //From the SPARC ABI
81
82 //The process runs in user mode
83 threadContexts[0]->setMiscRegWithEffect(MISCREG_PSTATE, 0x02);
84
85 //Setup default FP state
86 threadContexts[0]->setMiscReg(MISCREG_FSR, 0);
87
88 threadContexts[0]->setMiscReg(MISCREG_TICK, 0);
89 //
90 /*
91 * Register window management registers
92 */
93
94 //No windows contain info from other programs
95 threadContexts[0]->setMiscReg(MISCREG_OTHERWIN, 0);
96 //There are no windows to pop
97 threadContexts[0]->setMiscReg(MISCREG_CANRESTORE, 0);
98 //All windows are available to save into
99 threadContexts[0]->setMiscReg(MISCREG_CANSAVE, NWindows - 2);
100 //All windows are "clean"
101 threadContexts[0]->setMiscReg(MISCREG_CLEANWIN, NWindows);
102 //Start with register window 0
103 threadContexts[0]->setMiscReg(MISCREG_CWP, 0);
104 //Always use spill and fill traps 0
105 threadContexts[0]->setMiscReg(MISCREG_WSTATE, 0);
106 //Set the trap level to 0
107 threadContexts[0]->setMiscReg(MISCREG_TL, 0);
108 }
109
110 m5_auxv_t buildAuxVect(int64_t type, int64_t val)
111 {
112 m5_auxv_t result;
113 result.a_type = TheISA::htog(type);
114 result.a_val = TheISA::htog(val);
115 return result;
116 }
117
118 //We only use 19 instructions for the trap handlers, but there would be
119 //space for 32 in a real SPARC trap table.
120 const int numFillInsts = 32;
121 const int numSpillInsts = 32;
122
123 MachInst fillHandler[numFillInsts] =
124 {
125 htog(0x87802018), //wr %g0, ASI_AIUP, %asi
126 htog(0xe0dba7ff), //ldxa [%sp + BIAS + (0*8)] %asi, %l0
127 htog(0xe2dba807), //ldxa [%sp + BIAS + (1*8)] %asi, %l1
128 htog(0xe4dba80f), //ldxa [%sp + BIAS + (2*8)] %asi, %l2
129 htog(0xe6dba817), //ldxa [%sp + BIAS + (3*8)] %asi, %l3
130 htog(0xe8dba81f), //ldxa [%sp + BIAS + (4*8)] %asi, %l4
131 htog(0xeadba827), //ldxa [%sp + BIAS + (5*8)] %asi, %l5
132 htog(0xecdba82f), //ldxa [%sp + BIAS + (6*8)] %asi, %l6
133 htog(0xeedba837), //ldxa [%sp + BIAS + (7*8)] %asi, %l7
134 htog(0xf0dba83f), //ldxa [%sp + BIAS + (8*8)] %asi, %i0
135 htog(0xf2dba847), //ldxa [%sp + BIAS + (9*8)] %asi, %i1
136 htog(0xf4dba84f), //ldxa [%sp + BIAS + (10*8)] %asi, %i2
137 htog(0xf6dba857), //ldxa [%sp + BIAS + (11*8)] %asi, %i3
138 htog(0xf8dba85f), //ldxa [%sp + BIAS + (12*8)] %asi, %i4
139 htog(0xfadba867), //ldxa [%sp + BIAS + (13*8)] %asi, %i5
140 htog(0xfcdba86f), //ldxa [%sp + BIAS + (14*8)] %asi, %i6
141 htog(0xfedba877), //ldxa [%sp + BIAS + (15*8)] %asi, %i7
142 htog(0x83880000), //restored
143 htog(0x83F00000), //retry
144 htog(0x00000000), //illtrap
145 htog(0x00000000), //illtrap
146 htog(0x00000000), //illtrap
147 htog(0x00000000), //illtrap
148 htog(0x00000000), //illtrap
149 htog(0x00000000), //illtrap
150 htog(0x00000000), //illtrap
151 htog(0x00000000), //illtrap
152 htog(0x00000000), //illtrap
153 htog(0x00000000), //illtrap
154 htog(0x00000000), //illtrap
155 htog(0x00000000), //illtrap
156 htog(0x00000000) //illtrap
157 };
158
159 MachInst spillHandler[numSpillInsts] =
160 {
161 htog(0x87802018), //wr %g0, ASI_AIUP, %asi
162 htog(0xe0f3a7ff), //stxa %l0, [%sp + BIAS + (0*8)] %asi
163 htog(0xe2f3a807), //stxa %l1, [%sp + BIAS + (1*8)] %asi
164 htog(0xe4f3a80f), //stxa %l2, [%sp + BIAS + (2*8)] %asi
165 htog(0xe6f3a817), //stxa %l3, [%sp + BIAS + (3*8)] %asi
166 htog(0xe8f3a81f), //stxa %l4, [%sp + BIAS + (4*8)] %asi
167 htog(0xeaf3a827), //stxa %l5, [%sp + BIAS + (5*8)] %asi
168 htog(0xecf3a82f), //stxa %l6, [%sp + BIAS + (6*8)] %asi
169 htog(0xeef3a837), //stxa %l7, [%sp + BIAS + (7*8)] %asi
170 htog(0xf0f3a83f), //stxa %i0, [%sp + BIAS + (8*8)] %asi
171 htog(0xf2f3a847), //stxa %i1, [%sp + BIAS + (9*8)] %asi
172 htog(0xf4f3a84f), //stxa %i2, [%sp + BIAS + (10*8)] %asi
173 htog(0xf6f3a857), //stxa %i3, [%sp + BIAS + (11*8)] %asi
174 htog(0xf8f3a85f), //stxa %i4, [%sp + BIAS + (12*8)] %asi
175 htog(0xfaf3a867), //stxa %i5, [%sp + BIAS + (13*8)] %asi
176 htog(0xfcf3a86f), //stxa %i6, [%sp + BIAS + (14*8)] %asi
177 htog(0xfef3a877), //stxa %i7, [%sp + BIAS + (15*8)] %asi
178 htog(0x81880000), //saved
179 htog(0x83F00000), //retry
180 htog(0x00000000), //illtrap
181 htog(0x00000000), //illtrap
182 htog(0x00000000), //illtrap
183 htog(0x00000000), //illtrap
184 htog(0x00000000), //illtrap
185 htog(0x00000000), //illtrap
186 htog(0x00000000), //illtrap
187 htog(0x00000000), //illtrap
188 htog(0x00000000), //illtrap
189 htog(0x00000000), //illtrap
190 htog(0x00000000), //illtrap
191 htog(0x00000000), //illtrap
192 htog(0x00000000) //illtrap
193 };
194
195 void
196 SparcLiveProcess::argsInit(int intSize, int pageSize)
197 {
198 Process::startup();
199
200 string filename;
201 if(argv.size() < 1)
202 filename = "";
203 else
204 filename = argv[0];
205
206 Addr alignmentMask = ~(intSize - 1);
207
208 // load object file into target memory
209 objFile->loadSections(initVirtMem);
210
211 //These are the auxilliary vector types
212 enum auxTypes
213 {
214 SPARC_AT_HWCAP = 16,
215 SPARC_AT_PAGESZ = 6,
216 SPARC_AT_CLKTCK = 17,
217 SPARC_AT_PHDR = 3,
218 SPARC_AT_PHENT = 4,
219 SPARC_AT_PHNUM = 5,
220 SPARC_AT_BASE = 7,
221 SPARC_AT_FLAGS = 8,
222 SPARC_AT_ENTRY = 9,
223 SPARC_AT_UID = 11,
224 SPARC_AT_EUID = 12,
225 SPARC_AT_GID = 13,
226 SPARC_AT_EGID = 14,
227 SPARC_AT_SECURE = 23
228 };
229
230 enum hardwareCaps
231 {
232 M5_HWCAP_SPARC_FLUSH = 1,
233 M5_HWCAP_SPARC_STBAR = 2,
234 M5_HWCAP_SPARC_SWAP = 4,
235 M5_HWCAP_SPARC_MULDIV = 8,
236 M5_HWCAP_SPARC_V9 = 16,
237 //This one should technically only be set
238 //if there is a cheetah or cheetah_plus tlb,
239 //but we'll use it all the time
240 M5_HWCAP_SPARC_ULTRA3 = 32
241 };
242
243 const int64_t hwcap =
244 M5_HWCAP_SPARC_FLUSH |
245 M5_HWCAP_SPARC_STBAR |
246 M5_HWCAP_SPARC_SWAP |
247 M5_HWCAP_SPARC_MULDIV |
248 M5_HWCAP_SPARC_V9 |
249 M5_HWCAP_SPARC_ULTRA3;
250
251
252 //Setup the auxilliary vectors. These will already have endian conversion.
253 //Auxilliary vectors are loaded only for elf formatted executables.
254 ElfObject * elfObject = dynamic_cast<ElfObject *>(objFile);
255 if(elfObject)
256 {
257 //Bits which describe the system hardware capabilities
258 auxv.push_back(buildAuxVect(SPARC_AT_HWCAP, hwcap));
259 //The system page size
260 auxv.push_back(buildAuxVect(SPARC_AT_PAGESZ, SparcISA::VMPageSize));
261 //Defined to be 100 in the kernel source.
262 //Frequency at which times() increments
263 auxv.push_back(buildAuxVect(SPARC_AT_CLKTCK, 100));
264 // For statically linked executables, this is the virtual address of the
265 // program header tables if they appear in the executable image
266 auxv.push_back(buildAuxVect(SPARC_AT_PHDR, elfObject->programHeaderTable()));
267 // This is the size of a program header entry from the elf file.
268 auxv.push_back(buildAuxVect(SPARC_AT_PHENT, elfObject->programHeaderSize()));
269 // This is the number of program headers from the original elf file.
270 auxv.push_back(buildAuxVect(SPARC_AT_PHNUM, elfObject->programHeaderCount()));
271 //This is the address of the elf "interpreter", It should be set
272 //to 0 for regular executables. It should be something else
273 //(not sure what) for dynamic libraries.
274 auxv.push_back(buildAuxVect(SPARC_AT_BASE, 0));
275 //This is hardwired to 0 in the elf loading code in the kernel
276 auxv.push_back(buildAuxVect(SPARC_AT_FLAGS, 0));
277 //The entry point to the program
278 auxv.push_back(buildAuxVect(SPARC_AT_ENTRY, objFile->entryPoint()));
279 //Different user and group IDs
280 auxv.push_back(buildAuxVect(SPARC_AT_UID, uid()));
281 auxv.push_back(buildAuxVect(SPARC_AT_EUID, euid()));
282 auxv.push_back(buildAuxVect(SPARC_AT_GID, gid()));
283 auxv.push_back(buildAuxVect(SPARC_AT_EGID, egid()));
284 //Whether to enable "secure mode" in the executable
285 auxv.push_back(buildAuxVect(SPARC_AT_SECURE, 0));
286 }
287
288 //Figure out how big the initial stack needs to be
289
290 // The unaccounted for 0 at the top of the stack
291 int mysterious_size = intSize;
292
293 //This is the name of the file which is present on the initial stack
294 //It's purpose is to let the user space linker examine the original file.
295 int file_name_size = filename.size() + 1;
296
297 int env_data_size = 0;
298 for (int i = 0; i < envp.size(); ++i) {
299 env_data_size += envp[i].size() + 1;
300 }
301 int arg_data_size = 0;
302 for (int i = 0; i < argv.size(); ++i) {
303 arg_data_size += argv[i].size() + 1;
304 }
305
306 //The info_block needs to be padded so it's size is a multiple of the
307 //alignment mask. Also, it appears that there needs to be at least some
308 //padding, so if the size is already a multiple, we need to increase it
309 //anyway.
310 int info_block_size =
311 (file_name_size +
312 env_data_size +
313 arg_data_size +
314 intSize) & alignmentMask;
315
316 int info_block_padding =
317 info_block_size -
318 file_name_size -
319 env_data_size -
320 arg_data_size;
321
322 //Each auxilliary vector is two 8 byte words
323 int aux_array_size = intSize * 2 * (auxv.size() + 1);
324
325 int envp_array_size = intSize * (envp.size() + 1);
326 int argv_array_size = intSize * (argv.size() + 1);
327
328 int argc_size = intSize;
329 int window_save_size = intSize * 16;
330
331 int space_needed =
332 mysterious_size +
333 info_block_size +
334 aux_array_size +
335 envp_array_size +
336 argv_array_size +
337 argc_size +
338 window_save_size;
339
340 stack_min = stack_base - space_needed;
341 stack_min &= alignmentMask;
342 stack_size = stack_base - stack_min;
343
344 // map memory
345 pTable->allocate(roundDown(stack_min, pageSize),
346 roundUp(stack_size, pageSize));
347
348 // map out initial stack contents
349 Addr mysterious_base = stack_base - mysterious_size;
350 Addr file_name_base = mysterious_base - file_name_size;
351 Addr env_data_base = file_name_base - env_data_size;
352 Addr arg_data_base = env_data_base - arg_data_size;
353 Addr auxv_array_base = arg_data_base - aux_array_size - info_block_padding;
354 Addr envp_array_base = auxv_array_base - envp_array_size;
355 Addr argv_array_base = envp_array_base - argv_array_size;
356 Addr argc_base = argv_array_base - argc_size;
357 #ifndef NDEBUG
358 // only used in DPRINTF
359 Addr window_save_base = argc_base - window_save_size;
360 #endif
361
362 DPRINTF(Sparc, "The addresses of items on the initial stack:\n");
363 DPRINTF(Sparc, "0x%x - file name\n", file_name_base);
364 DPRINTF(Sparc, "0x%x - env data\n", env_data_base);
365 DPRINTF(Sparc, "0x%x - arg data\n", arg_data_base);
366 DPRINTF(Sparc, "0x%x - auxv array\n", auxv_array_base);
367 DPRINTF(Sparc, "0x%x - envp array\n", envp_array_base);
368 DPRINTF(Sparc, "0x%x - argv array\n", argv_array_base);
369 DPRINTF(Sparc, "0x%x - argc \n", argc_base);
370 DPRINTF(Sparc, "0x%x - window save\n", window_save_base);
371 DPRINTF(Sparc, "0x%x - stack min\n", stack_min);
372
373 // write contents to stack
374
375 // figure out argc
376 uint64_t argc = argv.size();
377 uint64_t guestArgc = TheISA::htog(argc);
378
379 //Write out the mysterious 0
380 uint64_t mysterious_zero = 0;
381 initVirtMem->writeBlob(mysterious_base,
382 (uint8_t*)&mysterious_zero, mysterious_size);
383
384 //Write the file name
385 initVirtMem->writeString(file_name_base, filename.c_str());
386
387 //Copy the aux stuff
388 for(int x = 0; x < auxv.size(); x++)
389 {
390 initVirtMem->writeBlob(auxv_array_base + x * 2 * intSize,
391 (uint8_t*)&(auxv[x].a_type), intSize);
392 initVirtMem->writeBlob(auxv_array_base + (x * 2 + 1) * intSize,
393 (uint8_t*)&(auxv[x].a_val), intSize);
394 }
395 //Write out the terminating zeroed auxilliary vector
396 const uint64_t zero = 0;
397 initVirtMem->writeBlob(auxv_array_base + 2 * intSize * auxv.size(),
398 (uint8_t*)&zero, 2 * intSize);
399
400 copyStringArray(envp, envp_array_base, env_data_base, initVirtMem);
401 copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem);
402
403 initVirtMem->writeBlob(argc_base, (uint8_t*)&guestArgc, intSize);
404
405 //Stuff the trap handlers into the processes address space.
406 //Since the stack grows down and is the highest area in the processes
407 //address space, we can put stuff above it and stay out of the way.
408 int fillSize = sizeof(MachInst) * numFillInsts;
409 int spillSize = sizeof(MachInst) * numSpillInsts;
410 fillStart = stack_base;
411 spillStart = fillStart + fillSize;
412 initVirtMem->writeBlob(fillStart, (uint8_t*)fillHandler, fillSize);
413 initVirtMem->writeBlob(spillStart, (uint8_t*)spillHandler, spillSize);
414
415 //Set up the thread context to start running the process
416 threadContexts[0]->setIntReg(ArgumentReg0, argc);
417 threadContexts[0]->setIntReg(ArgumentReg1, argv_array_base);
418 threadContexts[0]->setIntReg(StackPointerReg, stack_min - StackBias);
419
420 Addr prog_entry = objFile->entryPoint();
421 threadContexts[0]->setPC(prog_entry);
422 threadContexts[0]->setNextPC(prog_entry + sizeof(MachInst));
423 threadContexts[0]->setNextNPC(prog_entry + (2 * sizeof(MachInst)));
424
425 // num_processes++;
426 }