2 * Copyright (c) 2003-2004 The Regents of The University of Michigan
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.
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.
32 #include "arch/sparc/asi.hh"
33 #include "arch/sparc/isa_traits.hh"
34 #include "arch/sparc/process.hh"
35 #include "arch/sparc/types.hh"
36 #include "base/loader/object_file.hh"
37 #include "base/loader/elf_object.hh"
38 #include "base/misc.hh"
39 #include "cpu/thread_context.hh"
40 #include "mem/page_table.hh"
41 #include "mem/translating_port.hh"
42 #include "sim/system.hh"
45 using namespace SparcISA
;
48 SparcLiveProcess::SparcLiveProcess(const std::string
&nm
, ObjectFile
*objFile
,
49 System
*_system
, int stdin_fd
, int stdout_fd
, int stderr_fd
,
50 std::vector
<std::string
> &argv
, std::vector
<std::string
> &envp
,
51 const std::string
&cwd
,
52 uint64_t _uid
, uint64_t _euid
, uint64_t _gid
, uint64_t _egid
,
53 uint64_t _pid
, uint64_t _ppid
)
54 : LiveProcess(nm
, objFile
, _system
, stdin_fd
, stdout_fd
, stderr_fd
,
55 argv
, envp
, cwd
, _uid
, _euid
, _gid
, _egid
, _pid
, _ppid
)
58 // XXX all the below need to be updated for SPARC - Ali
59 brk_point
= objFile
->dataBase() + objFile
->dataSize() + objFile
->bssSize();
60 brk_point
= roundUp(brk_point
, VMPageSize
);
62 // Set up stack. On SPARC Linux, stack goes from the top of memory
63 // downward, less the hole for the kernel address space.
64 stack_base
= (Addr
)0x80000000000ULL
;
66 // Set up region for mmaps. Tru64 seems to start just above 0 and
67 // grow up from there.
68 mmap_start
= mmap_end
= 0xfffff80000000000ULL
;
70 // Set pointer for next thread stack. Reserve 8M for main stack.
71 next_thread_stack_base
= stack_base
- (8 * 1024 * 1024);
73 //Initialize these to 0s
79 SparcLiveProcess::startup()
81 argsInit(sizeof(IntReg
), VMPageSize
);
85 //The process runs in user mode
86 threadContexts
[0]->setMiscRegWithEffect(MISCREG_PSTATE
, 0x02);
88 //Setup default FP state
89 threadContexts
[0]->setMiscReg(MISCREG_FSR
, 0);
91 threadContexts
[0]->setMiscReg(MISCREG_TICK
, 0);
94 * Register window management registers
97 //No windows contain info from other programs
98 threadContexts
[0]->setMiscReg(MISCREG_OTHERWIN
, 0);
99 //There are no windows to pop
100 threadContexts
[0]->setMiscReg(MISCREG_CANRESTORE
, 0);
101 //All windows are available to save into
102 threadContexts
[0]->setMiscReg(MISCREG_CANSAVE
, NWindows
- 2);
103 //All windows are "clean"
104 threadContexts
[0]->setMiscReg(MISCREG_CLEANWIN
, NWindows
);
105 //Start with register window 0
106 threadContexts
[0]->setMiscReg(MISCREG_CWP
, 0);
107 //Always use spill and fill traps 0
108 threadContexts
[0]->setMiscReg(MISCREG_WSTATE
, 0);
109 //Set the trap level to 0
110 threadContexts
[0]->setMiscReg(MISCREG_TL
, 0);
111 //Set the ASI register to something fixed
112 threadContexts
[0]->setMiscReg(MISCREG_ASI
, ASI_PRIMARY
);
115 m5_auxv_t
buildAuxVect(int64_t type
, int64_t val
)
118 result
.a_type
= TheISA::htog(type
);
119 result
.a_val
= TheISA::htog(val
);
123 //We only use 19 instructions for the trap handlers, but there would be
124 //space for 32 in a real SPARC trap table.
125 const int numFillInsts
= 32;
126 const int numSpillInsts
= 32;
128 MachInst fillHandler
[numFillInsts
] =
130 htog(0x87802018), //wr %g0, ASI_AIUP, %asi
131 htog(0xe0dba7ff), //ldxa [%sp + BIAS + (0*8)] %asi, %l0
132 htog(0xe2dba807), //ldxa [%sp + BIAS + (1*8)] %asi, %l1
133 htog(0xe4dba80f), //ldxa [%sp + BIAS + (2*8)] %asi, %l2
134 htog(0xe6dba817), //ldxa [%sp + BIAS + (3*8)] %asi, %l3
135 htog(0xe8dba81f), //ldxa [%sp + BIAS + (4*8)] %asi, %l4
136 htog(0xeadba827), //ldxa [%sp + BIAS + (5*8)] %asi, %l5
137 htog(0xecdba82f), //ldxa [%sp + BIAS + (6*8)] %asi, %l6
138 htog(0xeedba837), //ldxa [%sp + BIAS + (7*8)] %asi, %l7
139 htog(0xf0dba83f), //ldxa [%sp + BIAS + (8*8)] %asi, %i0
140 htog(0xf2dba847), //ldxa [%sp + BIAS + (9*8)] %asi, %i1
141 htog(0xf4dba84f), //ldxa [%sp + BIAS + (10*8)] %asi, %i2
142 htog(0xf6dba857), //ldxa [%sp + BIAS + (11*8)] %asi, %i3
143 htog(0xf8dba85f), //ldxa [%sp + BIAS + (12*8)] %asi, %i4
144 htog(0xfadba867), //ldxa [%sp + BIAS + (13*8)] %asi, %i5
145 htog(0xfcdba86f), //ldxa [%sp + BIAS + (14*8)] %asi, %i6
146 htog(0xfedba877), //ldxa [%sp + BIAS + (15*8)] %asi, %i7
147 htog(0x83880000), //restored
148 htog(0x83F00000), //retry
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 htog(0x00000000), //illtrap
158 htog(0x00000000), //illtrap
159 htog(0x00000000), //illtrap
160 htog(0x00000000), //illtrap
161 htog(0x00000000) //illtrap
164 MachInst spillHandler
[numSpillInsts
] =
166 htog(0x87802018), //wr %g0, ASI_AIUP, %asi
167 htog(0xe0f3a7ff), //stxa %l0, [%sp + BIAS + (0*8)] %asi
168 htog(0xe2f3a807), //stxa %l1, [%sp + BIAS + (1*8)] %asi
169 htog(0xe4f3a80f), //stxa %l2, [%sp + BIAS + (2*8)] %asi
170 htog(0xe6f3a817), //stxa %l3, [%sp + BIAS + (3*8)] %asi
171 htog(0xe8f3a81f), //stxa %l4, [%sp + BIAS + (4*8)] %asi
172 htog(0xeaf3a827), //stxa %l5, [%sp + BIAS + (5*8)] %asi
173 htog(0xecf3a82f), //stxa %l6, [%sp + BIAS + (6*8)] %asi
174 htog(0xeef3a837), //stxa %l7, [%sp + BIAS + (7*8)] %asi
175 htog(0xf0f3a83f), //stxa %i0, [%sp + BIAS + (8*8)] %asi
176 htog(0xf2f3a847), //stxa %i1, [%sp + BIAS + (9*8)] %asi
177 htog(0xf4f3a84f), //stxa %i2, [%sp + BIAS + (10*8)] %asi
178 htog(0xf6f3a857), //stxa %i3, [%sp + BIAS + (11*8)] %asi
179 htog(0xf8f3a85f), //stxa %i4, [%sp + BIAS + (12*8)] %asi
180 htog(0xfaf3a867), //stxa %i5, [%sp + BIAS + (13*8)] %asi
181 htog(0xfcf3a86f), //stxa %i6, [%sp + BIAS + (14*8)] %asi
182 htog(0xfef3a877), //stxa %i7, [%sp + BIAS + (15*8)] %asi
183 htog(0x81880000), //saved
184 htog(0x83F00000), //retry
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 htog(0x00000000), //illtrap
194 htog(0x00000000), //illtrap
195 htog(0x00000000), //illtrap
196 htog(0x00000000), //illtrap
197 htog(0x00000000) //illtrap
201 SparcLiveProcess::argsInit(int intSize
, int pageSize
)
211 Addr alignmentMask
= ~(intSize
- 1);
213 // load object file into target memory
214 objFile
->loadSections(initVirtMem
);
216 //These are the auxilliary vector types
221 SPARC_AT_CLKTCK
= 17,
237 M5_HWCAP_SPARC_FLUSH
= 1,
238 M5_HWCAP_SPARC_STBAR
= 2,
239 M5_HWCAP_SPARC_SWAP
= 4,
240 M5_HWCAP_SPARC_MULDIV
= 8,
241 M5_HWCAP_SPARC_V9
= 16,
242 //This one should technically only be set
243 //if there is a cheetah or cheetah_plus tlb,
244 //but we'll use it all the time
245 M5_HWCAP_SPARC_ULTRA3
= 32
248 const int64_t hwcap
=
249 M5_HWCAP_SPARC_FLUSH
|
250 M5_HWCAP_SPARC_STBAR
|
251 M5_HWCAP_SPARC_SWAP
|
252 M5_HWCAP_SPARC_MULDIV
|
254 M5_HWCAP_SPARC_ULTRA3
;
257 //Setup the auxilliary vectors. These will already have endian conversion.
258 //Auxilliary vectors are loaded only for elf formatted executables.
259 ElfObject
* elfObject
= dynamic_cast<ElfObject
*>(objFile
);
262 //Bits which describe the system hardware capabilities
263 auxv
.push_back(buildAuxVect(SPARC_AT_HWCAP
, hwcap
));
264 //The system page size
265 auxv
.push_back(buildAuxVect(SPARC_AT_PAGESZ
, SparcISA::VMPageSize
));
266 //Defined to be 100 in the kernel source.
267 //Frequency at which times() increments
268 auxv
.push_back(buildAuxVect(SPARC_AT_CLKTCK
, 100));
269 // For statically linked executables, this is the virtual address of the
270 // program header tables if they appear in the executable image
271 auxv
.push_back(buildAuxVect(SPARC_AT_PHDR
, elfObject
->programHeaderTable()));
272 // This is the size of a program header entry from the elf file.
273 auxv
.push_back(buildAuxVect(SPARC_AT_PHENT
, elfObject
->programHeaderSize()));
274 // This is the number of program headers from the original elf file.
275 auxv
.push_back(buildAuxVect(SPARC_AT_PHNUM
, elfObject
->programHeaderCount()));
276 //This is the address of the elf "interpreter", It should be set
277 //to 0 for regular executables. It should be something else
278 //(not sure what) for dynamic libraries.
279 auxv
.push_back(buildAuxVect(SPARC_AT_BASE
, 0));
280 //This is hardwired to 0 in the elf loading code in the kernel
281 auxv
.push_back(buildAuxVect(SPARC_AT_FLAGS
, 0));
282 //The entry point to the program
283 auxv
.push_back(buildAuxVect(SPARC_AT_ENTRY
, objFile
->entryPoint()));
284 //Different user and group IDs
285 auxv
.push_back(buildAuxVect(SPARC_AT_UID
, uid()));
286 auxv
.push_back(buildAuxVect(SPARC_AT_EUID
, euid()));
287 auxv
.push_back(buildAuxVect(SPARC_AT_GID
, gid()));
288 auxv
.push_back(buildAuxVect(SPARC_AT_EGID
, egid()));
289 //Whether to enable "secure mode" in the executable
290 auxv
.push_back(buildAuxVect(SPARC_AT_SECURE
, 0));
293 //Figure out how big the initial stack needs to be
295 // The unaccounted for 0 at the top of the stack
296 int mysterious_size
= intSize
;
298 //This is the name of the file which is present on the initial stack
299 //It's purpose is to let the user space linker examine the original file.
300 int file_name_size
= filename
.size() + 1;
302 int env_data_size
= 0;
303 for (int i
= 0; i
< envp
.size(); ++i
) {
304 env_data_size
+= envp
[i
].size() + 1;
306 int arg_data_size
= 0;
307 for (int i
= 0; i
< argv
.size(); ++i
) {
308 arg_data_size
+= argv
[i
].size() + 1;
311 //The info_block needs to be padded so it's size is a multiple of the
312 //alignment mask. Also, it appears that there needs to be at least some
313 //padding, so if the size is already a multiple, we need to increase it
315 int info_block_size
=
319 intSize
) & alignmentMask
;
321 int info_block_padding
=
327 //Each auxilliary vector is two 8 byte words
328 int aux_array_size
= intSize
* 2 * (auxv
.size() + 1);
330 int envp_array_size
= intSize
* (envp
.size() + 1);
331 int argv_array_size
= intSize
* (argv
.size() + 1);
333 int argc_size
= intSize
;
334 int window_save_size
= intSize
* 16;
345 stack_min
= stack_base
- space_needed
;
346 stack_min
&= alignmentMask
;
347 stack_size
= stack_base
- stack_min
;
350 pTable
->allocate(roundDown(stack_min
, pageSize
),
351 roundUp(stack_size
, pageSize
));
353 // map out initial stack contents
354 Addr mysterious_base
= stack_base
- mysterious_size
;
355 Addr file_name_base
= mysterious_base
- file_name_size
;
356 Addr env_data_base
= file_name_base
- env_data_size
;
357 Addr arg_data_base
= env_data_base
- arg_data_size
;
358 Addr auxv_array_base
= arg_data_base
- aux_array_size
- info_block_padding
;
359 Addr envp_array_base
= auxv_array_base
- envp_array_size
;
360 Addr argv_array_base
= envp_array_base
- argv_array_size
;
361 Addr argc_base
= argv_array_base
- argc_size
;
363 // only used in DPRINTF
364 Addr window_save_base
= argc_base
- window_save_size
;
367 DPRINTF(Sparc
, "The addresses of items on the initial stack:\n");
368 DPRINTF(Sparc
, "0x%x - file name\n", file_name_base
);
369 DPRINTF(Sparc
, "0x%x - env data\n", env_data_base
);
370 DPRINTF(Sparc
, "0x%x - arg data\n", arg_data_base
);
371 DPRINTF(Sparc
, "0x%x - auxv array\n", auxv_array_base
);
372 DPRINTF(Sparc
, "0x%x - envp array\n", envp_array_base
);
373 DPRINTF(Sparc
, "0x%x - argv array\n", argv_array_base
);
374 DPRINTF(Sparc
, "0x%x - argc \n", argc_base
);
375 DPRINTF(Sparc
, "0x%x - window save\n", window_save_base
);
376 DPRINTF(Sparc
, "0x%x - stack min\n", stack_min
);
378 // write contents to stack
381 uint64_t argc
= argv
.size();
382 uint64_t guestArgc
= TheISA::htog(argc
);
384 //Write out the mysterious 0
385 uint64_t mysterious_zero
= 0;
386 initVirtMem
->writeBlob(mysterious_base
,
387 (uint8_t*)&mysterious_zero
, mysterious_size
);
389 //Write the file name
390 initVirtMem
->writeString(file_name_base
, filename
.c_str());
393 for(int x
= 0; x
< auxv
.size(); x
++)
395 initVirtMem
->writeBlob(auxv_array_base
+ x
* 2 * intSize
,
396 (uint8_t*)&(auxv
[x
].a_type
), intSize
);
397 initVirtMem
->writeBlob(auxv_array_base
+ (x
* 2 + 1) * intSize
,
398 (uint8_t*)&(auxv
[x
].a_val
), intSize
);
400 //Write out the terminating zeroed auxilliary vector
401 const uint64_t zero
= 0;
402 initVirtMem
->writeBlob(auxv_array_base
+ 2 * intSize
* auxv
.size(),
403 (uint8_t*)&zero
, 2 * intSize
);
405 copyStringArray(envp
, envp_array_base
, env_data_base
, initVirtMem
);
406 copyStringArray(argv
, argv_array_base
, arg_data_base
, initVirtMem
);
408 initVirtMem
->writeBlob(argc_base
, (uint8_t*)&guestArgc
, intSize
);
410 //Stuff the trap handlers into the processes address space.
411 //Since the stack grows down and is the highest area in the processes
412 //address space, we can put stuff above it and stay out of the way.
413 int fillSize
= sizeof(MachInst
) * numFillInsts
;
414 int spillSize
= sizeof(MachInst
) * numSpillInsts
;
415 fillStart
= stack_base
;
416 spillStart
= fillStart
+ fillSize
;
417 initVirtMem
->writeBlob(fillStart
, (uint8_t*)fillHandler
, fillSize
);
418 initVirtMem
->writeBlob(spillStart
, (uint8_t*)spillHandler
, spillSize
);
420 //Set up the thread context to start running the process
421 threadContexts
[0]->setIntReg(ArgumentReg0
, argc
);
422 threadContexts
[0]->setIntReg(ArgumentReg1
, argv_array_base
);
423 threadContexts
[0]->setIntReg(StackPointerReg
, stack_min
- StackBias
);
425 Addr prog_entry
= objFile
->entryPoint();
426 threadContexts
[0]->setPC(prog_entry
);
427 threadContexts
[0]->setNextPC(prog_entry
+ sizeof(MachInst
));
428 threadContexts
[0]->setNextNPC(prog_entry
+ (2 * sizeof(MachInst
)));