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/handlers.hh"
34 #include "arch/sparc/isa_traits.hh"
35 #include "arch/sparc/process.hh"
36 #include "arch/sparc/types.hh"
37 #include "base/loader/object_file.hh"
38 #include "base/loader/elf_object.hh"
39 #include "base/misc.hh"
40 #include "cpu/thread_context.hh"
41 #include "mem/page_table.hh"
42 #include "sim/process_impl.hh"
43 #include "mem/translating_port.hh"
44 #include "sim/system.hh"
47 using namespace SparcISA
;
49 static const int FirstArgumentReg
= 8;
50 static const int ReturnValueReg
= 8;
53 SparcLiveProcess::SparcLiveProcess(LiveProcessParams
* params
,
54 ObjectFile
*objFile
, Addr _StackBias
)
55 : LiveProcess(params
, objFile
), StackBias(_StackBias
)
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 pointer for next thread stack. Reserve 8M for main stack.
63 next_thread_stack_base
= stack_base
- (8 * 1024 * 1024);
65 //Initialize these to 0s
70 void SparcLiveProcess::handleTrap(int trapNum
, ThreadContext
*tc
)
74 case 0x01: //Software breakpoint
75 warn("Software breakpoint encountered at pc %#x.\n", tc
->readPC());
77 case 0x02: //Division by zero
78 warn("Software signaled a division by zero at pc %#x.\n",
81 case 0x03: //Flush window trap
84 case 0x04: //Clean windows
85 warn("Ignoring process request for clean register "
86 "windows at pc %#x.\n", tc
->readPC());
88 case 0x05: //Range check
89 warn("Software signaled a range check at pc %#x.\n",
92 case 0x06: //Fix alignment
93 warn("Ignoring process request for os assisted unaligned accesses "
94 "at pc %#x.\n", tc
->readPC());
96 case 0x07: //Integer overflow
97 warn("Software signaled an integer overflow at pc %#x.\n",
100 case 0x32: //Get integer condition codes
101 warn("Ignoring process request to get the integer condition codes "
102 "at pc %#x.\n", tc
->readPC());
104 case 0x33: //Set integer condition codes
105 warn("Ignoring process request to set the integer condition codes "
106 "at pc %#x.\n", tc
->readPC());
109 panic("Unimplemented trap to operating system: trap number %#x.\n", trapNum
);
114 SparcLiveProcess::startup()
118 ThreadContext
*tc
= system
->getThreadContext(contextIds
[0]);
121 //Setup default FP state
122 tc
->setMiscRegNoEffect(MISCREG_FSR
, 0);
124 tc
->setMiscRegNoEffect(MISCREG_TICK
, 0);
127 * Register window management registers
130 //No windows contain info from other programs
131 //tc->setMiscRegNoEffect(MISCREG_OTHERWIN, 0);
132 tc
->setIntReg(NumIntArchRegs
+ 6, 0);
133 //There are no windows to pop
134 //tc->setMiscRegNoEffect(MISCREG_CANRESTORE, 0);
135 tc
->setIntReg(NumIntArchRegs
+ 4, 0);
136 //All windows are available to save into
137 //tc->setMiscRegNoEffect(MISCREG_CANSAVE, NWindows - 2);
138 tc
->setIntReg(NumIntArchRegs
+ 3, NWindows
- 2);
139 //All windows are "clean"
140 //tc->setMiscRegNoEffect(MISCREG_CLEANWIN, NWindows);
141 tc
->setIntReg(NumIntArchRegs
+ 5, NWindows
);
142 //Start with register window 0
143 tc
->setMiscRegNoEffect(MISCREG_CWP
, 0);
144 //Always use spill and fill traps 0
145 //tc->setMiscRegNoEffect(MISCREG_WSTATE, 0);
146 tc
->setIntReg(NumIntArchRegs
+ 7, 0);
147 //Set the trap level to 0
148 tc
->setMiscRegNoEffect(MISCREG_TL
, 0);
149 //Set the ASI register to something fixed
150 tc
->setMiscRegNoEffect(MISCREG_ASI
, ASI_PRIMARY
);
153 * T1 specific registers
155 //Turn on the icache, dcache, dtb translation, and itb translation.
156 tc
->setMiscRegNoEffect(MISCREG_MMU_LSU_CTRL
, 15);
160 Sparc32LiveProcess::startup()
162 if (checkpointRestored
)
165 SparcLiveProcess::startup();
167 ThreadContext
*tc
= system
->getThreadContext(contextIds
[0]);
168 //The process runs in user mode with 32 bit addresses
169 tc
->setMiscReg(MISCREG_PSTATE
, 0x0a);
171 argsInit(32 / 8, VMPageSize
);
175 Sparc64LiveProcess::startup()
177 if (checkpointRestored
)
180 SparcLiveProcess::startup();
182 ThreadContext
*tc
= system
->getThreadContext(contextIds
[0]);
183 //The process runs in user mode
184 tc
->setMiscReg(MISCREG_PSTATE
, 0x02);
186 argsInit(sizeof(IntReg
), VMPageSize
);
189 template<class IntType
>
191 SparcLiveProcess::argsInit(int pageSize
)
193 int intSize
= sizeof(IntType
);
195 typedef AuxVector
<IntType
> auxv_t
;
197 std::vector
<auxv_t
> auxv
;
205 //Even for a 32 bit process, the ABI says we still need to
206 //maintain double word alignment of the stack pointer.
209 // load object file into target memory
210 objFile
->loadSections(initVirtMem
);
214 M5_HWCAP_SPARC_FLUSH
= 1,
215 M5_HWCAP_SPARC_STBAR
= 2,
216 M5_HWCAP_SPARC_SWAP
= 4,
217 M5_HWCAP_SPARC_MULDIV
= 8,
218 M5_HWCAP_SPARC_V9
= 16,
219 //This one should technically only be set
220 //if there is a cheetah or cheetah_plus tlb,
221 //but we'll use it all the time
222 M5_HWCAP_SPARC_ULTRA3
= 32
225 const int64_t hwcap
=
226 M5_HWCAP_SPARC_FLUSH
|
227 M5_HWCAP_SPARC_STBAR
|
228 M5_HWCAP_SPARC_SWAP
|
229 M5_HWCAP_SPARC_MULDIV
|
231 M5_HWCAP_SPARC_ULTRA3
;
233 //Setup the auxilliary vectors. These will already have endian conversion.
234 //Auxilliary vectors are loaded only for elf formatted executables.
235 ElfObject
* elfObject
= dynamic_cast<ElfObject
*>(objFile
);
238 //Bits which describe the system hardware capabilities
239 auxv
.push_back(auxv_t(M5_AT_HWCAP
, hwcap
));
240 //The system page size
241 auxv
.push_back(auxv_t(M5_AT_PAGESZ
, SparcISA::VMPageSize
));
242 //Defined to be 100 in the kernel source.
243 //Frequency at which times() increments
244 auxv
.push_back(auxv_t(M5_AT_CLKTCK
, 100));
245 // For statically linked executables, this is the virtual address of the
246 // program header tables if they appear in the executable image
247 auxv
.push_back(auxv_t(M5_AT_PHDR
, elfObject
->programHeaderTable()));
248 // This is the size of a program header entry from the elf file.
249 auxv
.push_back(auxv_t(M5_AT_PHENT
, elfObject
->programHeaderSize()));
250 // This is the number of program headers from the original elf file.
251 auxv
.push_back(auxv_t(M5_AT_PHNUM
, elfObject
->programHeaderCount()));
252 //This is the address of the elf "interpreter", It should be set
253 //to 0 for regular executables. It should be something else
254 //(not sure what) for dynamic libraries.
255 auxv
.push_back(auxv_t(M5_AT_BASE
, 0));
256 //This is hardwired to 0 in the elf loading code in the kernel
257 auxv
.push_back(auxv_t(M5_AT_FLAGS
, 0));
258 //The entry point to the program
259 auxv
.push_back(auxv_t(M5_AT_ENTRY
, objFile
->entryPoint()));
260 //Different user and group IDs
261 auxv
.push_back(auxv_t(M5_AT_UID
, uid()));
262 auxv
.push_back(auxv_t(M5_AT_EUID
, euid()));
263 auxv
.push_back(auxv_t(M5_AT_GID
, gid()));
264 auxv
.push_back(auxv_t(M5_AT_EGID
, egid()));
265 //Whether to enable "secure mode" in the executable
266 auxv
.push_back(auxv_t(M5_AT_SECURE
, 0));
269 //Figure out how big the initial stack needs to be
271 // The unaccounted for 8 byte 0 at the top of the stack
274 //This is the name of the file which is present on the initial stack
275 //It's purpose is to let the user space linker examine the original file.
276 int file_name_size
= filename
.size() + 1;
278 int env_data_size
= 0;
279 for (int i
= 0; i
< envp
.size(); ++i
) {
280 env_data_size
+= envp
[i
].size() + 1;
282 int arg_data_size
= 0;
283 for (int i
= 0; i
< argv
.size(); ++i
) {
284 arg_data_size
+= argv
[i
].size() + 1;
288 int base_info_block_size
=
289 sentry_size
+ file_name_size
+ env_data_size
+ arg_data_size
;
291 int info_block_size
= roundUp(base_info_block_size
, align
);
293 int info_block_padding
= info_block_size
- base_info_block_size
;
295 //Each auxilliary vector is two words
296 int aux_array_size
= intSize
* 2 * (auxv
.size() + 1);
298 int envp_array_size
= intSize
* (envp
.size() + 1);
299 int argv_array_size
= intSize
* (argv
.size() + 1);
301 int argc_size
= intSize
;
302 int window_save_size
= intSize
* 16;
304 //Figure out the size of the contents of the actual initial frame
312 //There needs to be padding after the auxiliary vector data so that the
313 //very bottom of the stack is aligned properly.
314 int aligned_partial_size
= roundUp(frame_size
, align
);
315 int aux_padding
= aligned_partial_size
- frame_size
;
322 stack_min
= stack_base
- space_needed
;
323 stack_min
= roundDown(stack_min
, align
);
324 stack_size
= stack_base
- stack_min
;
326 // Allocate space for the stack
327 pTable
->allocate(roundDown(stack_min
, pageSize
),
328 roundUp(stack_size
, pageSize
));
330 // map out initial stack contents
331 IntType sentry_base
= stack_base
- sentry_size
;
332 IntType file_name_base
= sentry_base
- file_name_size
;
333 IntType env_data_base
= file_name_base
- env_data_size
;
334 IntType arg_data_base
= env_data_base
- arg_data_size
;
335 IntType auxv_array_base
= arg_data_base
-
336 info_block_padding
- aux_array_size
- aux_padding
;
337 IntType envp_array_base
= auxv_array_base
- envp_array_size
;
338 IntType argv_array_base
= envp_array_base
- argv_array_size
;
339 IntType argc_base
= argv_array_base
- argc_size
;
341 IntType window_save_base
= argc_base
- window_save_size
;
344 DPRINTF(Stack
, "The addresses of items on the initial stack:\n");
345 DPRINTF(Stack
, "%#x - sentry NULL\n", sentry_base
);
346 DPRINTF(Stack
, "filename = %s\n", filename
);
347 DPRINTF(Stack
, "%#x - file name\n", file_name_base
);
348 DPRINTF(Stack
, "%#x - env data\n", env_data_base
);
349 DPRINTF(Stack
, "%#x - arg data\n", arg_data_base
);
350 DPRINTF(Stack
, "%#x - auxv array\n", auxv_array_base
);
351 DPRINTF(Stack
, "%#x - envp array\n", envp_array_base
);
352 DPRINTF(Stack
, "%#x - argv array\n", argv_array_base
);
353 DPRINTF(Stack
, "%#x - argc \n", argc_base
);
354 DPRINTF(Stack
, "%#x - window save\n", window_save_base
);
355 DPRINTF(Stack
, "%#x - stack min\n", stack_min
);
357 assert(window_save_base
== stack_min
);
359 // write contents to stack
362 IntType argc
= argv
.size();
363 IntType guestArgc
= SparcISA::htog(argc
);
365 //Write out the sentry void *
366 uint64_t sentry_NULL
= 0;
367 initVirtMem
->writeBlob(sentry_base
,
368 (uint8_t*)&sentry_NULL
, sentry_size
);
370 //Write the file name
371 initVirtMem
->writeString(file_name_base
, filename
.c_str());
374 for(int x
= 0; x
< auxv
.size(); x
++)
376 initVirtMem
->writeBlob(auxv_array_base
+ x
* 2 * intSize
,
377 (uint8_t*)&(auxv
[x
].a_type
), intSize
);
378 initVirtMem
->writeBlob(auxv_array_base
+ (x
* 2 + 1) * intSize
,
379 (uint8_t*)&(auxv
[x
].a_val
), intSize
);
382 //Write out the terminating zeroed auxilliary vector
383 const IntType zero
= 0;
384 initVirtMem
->writeBlob(auxv_array_base
+ intSize
* 2 * auxv
.size(),
385 (uint8_t*)&zero
, intSize
);
386 initVirtMem
->writeBlob(auxv_array_base
+ intSize
* (2 * auxv
.size() + 1),
387 (uint8_t*)&zero
, intSize
);
389 copyStringArray(envp
, envp_array_base
, env_data_base
, initVirtMem
);
390 copyStringArray(argv
, argv_array_base
, arg_data_base
, initVirtMem
);
392 initVirtMem
->writeBlob(argc_base
, (uint8_t*)&guestArgc
, intSize
);
394 //Set up space for the trap handlers into the processes address space.
395 //Since the stack grows down and there is reserved address space abov
396 //it, we can put stuff above it and stay out of the way.
397 fillStart
= stack_base
;
398 spillStart
= fillStart
+ sizeof(MachInst
) * numFillInsts
;
400 ThreadContext
*tc
= system
->getThreadContext(contextIds
[0]);
401 //Set up the thread context to start running the process
402 //assert(NumArgumentRegs >= 2);
403 //tc->setIntReg(ArgumentReg[0], argc);
404 //tc->setIntReg(ArgumentReg[1], argv_array_base);
405 tc
->setIntReg(StackPointerReg
, stack_min
- StackBias
);
407 // %g1 is a pointer to a function that should be run at exit. Since we
408 // don't have anything like that, it should be set to 0.
411 Addr prog_entry
= objFile
->entryPoint();
412 tc
->setPC(prog_entry
);
413 tc
->setNextPC(prog_entry
+ sizeof(MachInst
));
414 tc
->setNextNPC(prog_entry
+ (2 * sizeof(MachInst
)));
416 //Align the "stack_min" to a page boundary.
417 stack_min
= roundDown(stack_min
, pageSize
);
423 Sparc64LiveProcess::argsInit(int intSize
, int pageSize
)
425 SparcLiveProcess::argsInit
<uint64_t>(pageSize
);
427 // Stuff the trap handlers into the process address space
428 initVirtMem
->writeBlob(fillStart
,
429 (uint8_t*)fillHandler64
, sizeof(MachInst
) * numFillInsts
);
430 initVirtMem
->writeBlob(spillStart
,
431 (uint8_t*)spillHandler64
, sizeof(MachInst
) * numSpillInsts
);
435 Sparc32LiveProcess::argsInit(int intSize
, int pageSize
)
437 SparcLiveProcess::argsInit
<uint32_t>(pageSize
);
439 // Stuff the trap handlers into the process address space
440 initVirtMem
->writeBlob(fillStart
,
441 (uint8_t*)fillHandler32
, sizeof(MachInst
) * numFillInsts
);
442 initVirtMem
->writeBlob(spillStart
,
443 (uint8_t*)spillHandler32
, sizeof(MachInst
) * numSpillInsts
);
446 void Sparc32LiveProcess::flushWindows(ThreadContext
*tc
)
448 IntReg Cansave
= tc
->readIntReg(NumIntArchRegs
+ 3);
449 IntReg Canrestore
= tc
->readIntReg(NumIntArchRegs
+ 4);
450 IntReg Otherwin
= tc
->readIntReg(NumIntArchRegs
+ 6);
451 MiscReg CWP
= tc
->readMiscReg(MISCREG_CWP
);
452 MiscReg origCWP
= CWP
;
453 CWP
= (CWP
+ Cansave
+ 2) % NWindows
;
454 while(NWindows
- 2 - Cansave
!= 0)
457 panic("Otherwin non-zero.\n");
459 tc
->setMiscReg(MISCREG_CWP
, CWP
);
461 IntReg sp
= tc
->readIntReg(StackPointerReg
);
462 for (int index
= 16; index
< 32; index
++) {
463 uint32_t regVal
= tc
->readIntReg(index
);
464 regVal
= htog(regVal
);
465 if (!tc
->getMemPort()->tryWriteBlob(
466 sp
+ (index
- 16) * 4, (uint8_t *)®Val
, 4)) {
467 warn("Failed to save register to the stack when "
468 "flushing windows.\n");
473 CWP
= (CWP
+ 1) % NWindows
;
476 tc
->setIntReg(NumIntArchRegs
+ 3, Cansave
);
477 tc
->setIntReg(NumIntArchRegs
+ 4, Canrestore
);
478 tc
->setMiscReg(MISCREG_CWP
, origCWP
);
481 void Sparc64LiveProcess::flushWindows(ThreadContext
*tc
)
483 IntReg Cansave
= tc
->readIntReg(NumIntArchRegs
+ 3);
484 IntReg Canrestore
= tc
->readIntReg(NumIntArchRegs
+ 4);
485 IntReg Otherwin
= tc
->readIntReg(NumIntArchRegs
+ 6);
486 MiscReg CWP
= tc
->readMiscReg(MISCREG_CWP
);
487 MiscReg origCWP
= CWP
;
488 CWP
= (CWP
+ Cansave
+ 2) % NWindows
;
489 while(NWindows
- 2 - Cansave
!= 0)
492 panic("Otherwin non-zero.\n");
494 tc
->setMiscReg(MISCREG_CWP
, CWP
);
496 IntReg sp
= tc
->readIntReg(StackPointerReg
);
497 for (int index
= 16; index
< 32; index
++) {
498 IntReg regVal
= tc
->readIntReg(index
);
499 regVal
= htog(regVal
);
500 if (!tc
->getMemPort()->tryWriteBlob(
501 sp
+ 2047 + (index
- 16) * 8, (uint8_t *)®Val
, 8)) {
502 warn("Failed to save register to the stack when "
503 "flushing windows.\n");
508 CWP
= (CWP
+ 1) % NWindows
;
511 tc
->setIntReg(NumIntArchRegs
+ 3, Cansave
);
512 tc
->setIntReg(NumIntArchRegs
+ 4, Canrestore
);
513 tc
->setMiscReg(MISCREG_CWP
, origCWP
);
517 Sparc32LiveProcess::getSyscallArg(ThreadContext
*tc
, int i
)
520 return bits(tc
->readIntReg(FirstArgumentReg
+ i
), 31, 0);
524 Sparc32LiveProcess::setSyscallArg(ThreadContext
*tc
, int i
, IntReg val
)
527 tc
->setIntReg(FirstArgumentReg
+ i
, bits(val
, 31, 0));
531 Sparc64LiveProcess::getSyscallArg(ThreadContext
*tc
, int i
)
534 return tc
->readIntReg(FirstArgumentReg
+ i
);
538 Sparc64LiveProcess::setSyscallArg(ThreadContext
*tc
, int i
, IntReg val
)
541 tc
->setIntReg(FirstArgumentReg
+ i
, val
);
545 SparcLiveProcess::setSyscallReturn(ThreadContext
*tc
,
546 SyscallReturn return_value
)
548 // check for error condition. SPARC syscall convention is to
549 // indicate success/failure in reg the carry bit of the ccr
550 // and put the return value itself in the standard return value reg ().
551 if (return_value
.successful()) {
552 // no error, clear XCC.C
553 tc
->setIntReg(NumIntArchRegs
+ 2,
554 tc
->readIntReg(NumIntArchRegs
+ 2) & 0xEE);
555 //tc->setMiscRegNoEffect(MISCREG_CCR, tc->readMiscRegNoEffect(MISCREG_CCR) & 0xEE);
556 IntReg val
= return_value
.value();
557 if (bits(tc
->readMiscRegNoEffect(
558 SparcISA::MISCREG_PSTATE
), 3, 3)) {
559 val
= bits(val
, 31, 0);
561 tc
->setIntReg(ReturnValueReg
, val
);
563 // got an error, set XCC.C
564 tc
->setIntReg(NumIntArchRegs
+ 2,
565 tc
->readIntReg(NumIntArchRegs
+ 2) | 0x11);
566 //tc->setMiscRegNoEffect(MISCREG_CCR, tc->readMiscRegNoEffect(MISCREG_CCR) | 0x11);
567 IntReg val
= -return_value
.value();
568 if (bits(tc
->readMiscRegNoEffect(
569 SparcISA::MISCREG_PSTATE
), 3, 3)) {
570 val
= bits(val
, 31, 0);
572 tc
->setIntReg(ReturnValueReg
, val
);