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/process.hh"
34 #include "arch/sparc/asi.hh"
35 #include "arch/sparc/handlers.hh"
36 #include "arch/sparc/isa_traits.hh"
37 #include "arch/sparc/registers.hh"
38 #include "arch/sparc/types.hh"
39 #include "base/loader/elf_object.hh"
40 #include "base/loader/object_file.hh"
41 #include "base/misc.hh"
42 #include "cpu/thread_context.hh"
43 #include "debug/Stack.hh"
44 #include "mem/page_table.hh"
45 #include "sim/process_impl.hh"
46 #include "sim/syscall_return.hh"
47 #include "sim/system.hh"
50 using namespace SparcISA
;
52 static const int FirstArgumentReg
= 8;
55 SparcLiveProcess::SparcLiveProcess(LiveProcessParams
* params
,
56 ObjectFile
*objFile
, Addr _StackBias
)
57 : LiveProcess(params
, objFile
), StackBias(_StackBias
)
60 // XXX all the below need to be updated for SPARC - Ali
61 brk_point
= objFile
->dataBase() + objFile
->dataSize() + objFile
->bssSize();
62 brk_point
= roundUp(brk_point
, PageBytes
);
64 // Set pointer for next thread stack. Reserve 8M for main stack.
65 next_thread_stack_base
= stack_base
- (8 * 1024 * 1024);
67 // Initialize these to 0s
73 SparcLiveProcess::handleTrap(int trapNum
, ThreadContext
*tc
)
75 PCState pc
= tc
->pcState();
77 case 0x01: // Software breakpoint
78 warn("Software breakpoint encountered at pc %#x.\n", pc
.pc());
80 case 0x02: // Division by zero
81 warn("Software signaled a division by zero at pc %#x.\n", pc
.pc());
83 case 0x03: // Flush window trap
86 case 0x04: // Clean windows
87 warn("Ignoring process request for clean register "
88 "windows at pc %#x.\n", pc
.pc());
90 case 0x05: // Range check
91 warn("Software signaled a range check at pc %#x.\n", pc
.pc());
93 case 0x06: // Fix alignment
94 warn("Ignoring process request for os assisted unaligned accesses "
95 "at pc %#x.\n", pc
.pc());
97 case 0x07: // Integer overflow
98 warn("Software signaled an integer overflow at pc %#x.\n", pc
.pc());
100 case 0x32: // Get integer condition codes
101 warn("Ignoring process request to get the integer condition codes "
102 "at pc %#x.\n", pc
.pc());
104 case 0x33: // Set integer condition codes
105 warn("Ignoring process request to set the integer condition codes "
106 "at pc %#x.\n", pc
.pc());
109 panic("Unimplemented trap to operating system: trap number %#x.\n", trapNum
);
114 SparcLiveProcess::initState()
116 LiveProcess::initState();
118 ThreadContext
*tc
= system
->getThreadContext(contextIds
[0]);
119 // From the SPARC ABI
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
->setMiscReg(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
->setMiscReg(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::initState()
162 SparcLiveProcess::initState();
164 ThreadContext
*tc
= system
->getThreadContext(contextIds
[0]);
165 // The process runs in user mode with 32 bit addresses
169 tc
->setMiscReg(MISCREG_PSTATE
, pstate
);
171 argsInit(32 / 8, PageBytes
);
175 Sparc64LiveProcess::initState()
177 SparcLiveProcess::initState();
179 ThreadContext
*tc
= system
->getThreadContext(contextIds
[0]);
180 // The process runs in user mode
183 tc
->setMiscReg(MISCREG_PSTATE
, pstate
);
185 argsInit(sizeof(IntReg
), PageBytes
);
188 template<class IntType
>
190 SparcLiveProcess::argsInit(int pageSize
)
192 int intSize
= sizeof(IntType
);
194 typedef AuxVector
<IntType
> auxv_t
;
196 std::vector
<auxv_t
> auxv
;
204 // Even for a 32 bit process, the ABI says we still need to
205 // maintain double word alignment of the stack pointer.
208 // Patch the ld_bias for dynamic executables.
211 // load object file into target memory
212 objFile
->loadSections(initVirtMem
);
216 M5_HWCAP_SPARC_FLUSH
= 1,
217 M5_HWCAP_SPARC_STBAR
= 2,
218 M5_HWCAP_SPARC_SWAP
= 4,
219 M5_HWCAP_SPARC_MULDIV
= 8,
220 M5_HWCAP_SPARC_V9
= 16,
221 // This one should technically only be set
222 // if there is a cheetah or cheetah_plus tlb,
223 // but we'll use it all the time
224 M5_HWCAP_SPARC_ULTRA3
= 32
227 const int64_t hwcap
=
228 M5_HWCAP_SPARC_FLUSH
|
229 M5_HWCAP_SPARC_STBAR
|
230 M5_HWCAP_SPARC_SWAP
|
231 M5_HWCAP_SPARC_MULDIV
|
233 M5_HWCAP_SPARC_ULTRA3
;
235 // Setup the auxilliary vectors. These will already have endian conversion.
236 // Auxilliary vectors are loaded only for elf formatted executables.
237 ElfObject
* elfObject
= dynamic_cast<ElfObject
*>(objFile
);
239 // Bits which describe the system hardware capabilities
240 auxv
.push_back(auxv_t(M5_AT_HWCAP
, hwcap
));
241 // The system page size
242 auxv
.push_back(auxv_t(M5_AT_PAGESZ
, SparcISA::PageBytes
));
243 // Defined to be 100 in the kernel source.
244 // Frequency at which times() increments
245 auxv
.push_back(auxv_t(M5_AT_CLKTCK
, 100));
246 // For statically linked executables, this is the virtual address of the
247 // program header tables if they appear in the executable image
248 auxv
.push_back(auxv_t(M5_AT_PHDR
, elfObject
->programHeaderTable()));
249 // This is the size of a program header entry from the elf file.
250 auxv
.push_back(auxv_t(M5_AT_PHENT
, elfObject
->programHeaderSize()));
251 // This is the number of program headers from the original elf file.
252 auxv
.push_back(auxv_t(M5_AT_PHNUM
, elfObject
->programHeaderCount()));
253 // This is the base address of the ELF interpreter; it should be
254 // zero for static executables or contain the base address for
255 // dynamic executables.
256 auxv
.push_back(auxv_t(M5_AT_BASE
, getBias()));
257 // This is hardwired to 0 in the elf loading code in the kernel
258 auxv
.push_back(auxv_t(M5_AT_FLAGS
, 0));
259 // The entry point to the program
260 auxv
.push_back(auxv_t(M5_AT_ENTRY
, objFile
->entryPoint()));
261 // Different user and group IDs
262 auxv
.push_back(auxv_t(M5_AT_UID
, uid()));
263 auxv
.push_back(auxv_t(M5_AT_EUID
, euid()));
264 auxv
.push_back(auxv_t(M5_AT_GID
, gid()));
265 auxv
.push_back(auxv_t(M5_AT_EGID
, egid()));
266 // Whether to enable "secure mode" in the executable
267 auxv
.push_back(auxv_t(M5_AT_SECURE
, 0));
270 // Figure out how big the initial stack needs to be
272 // The unaccounted for 8 byte 0 at the top of the stack
275 // This is the name of the file which is present on the initial stack
276 // It's purpose is to let the user space linker examine the original file.
277 int file_name_size
= filename
.size() + 1;
279 int env_data_size
= 0;
280 for (int i
= 0; i
< envp
.size(); ++i
) {
281 env_data_size
+= envp
[i
].size() + 1;
283 int arg_data_size
= 0;
284 for (int i
= 0; i
< argv
.size(); ++i
) {
285 arg_data_size
+= argv
[i
].size() + 1;
289 int base_info_block_size
=
290 sentry_size
+ file_name_size
+ env_data_size
+ arg_data_size
;
292 int info_block_size
= roundUp(base_info_block_size
, align
);
294 int info_block_padding
= info_block_size
- base_info_block_size
;
296 // Each auxilliary vector is two words
297 int aux_array_size
= intSize
* 2 * (auxv
.size() + 1);
299 int envp_array_size
= intSize
* (envp
.size() + 1);
300 int argv_array_size
= intSize
* (argv
.size() + 1);
302 int argc_size
= intSize
;
303 int window_save_size
= intSize
* 16;
305 // Figure out the size of the contents of the actual initial frame
313 // There needs to be padding after the auxiliary vector data so that the
314 // very bottom of the stack is aligned properly.
315 int aligned_partial_size
= roundUp(frame_size
, align
);
316 int aux_padding
= aligned_partial_size
- frame_size
;
323 stack_min
= stack_base
- space_needed
;
324 stack_min
= roundDown(stack_min
, align
);
325 stack_size
= stack_base
- stack_min
;
327 // Allocate space for the stack
328 allocateMem(roundDown(stack_min
, pageSize
), 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());
373 // Copy the aux stuff
374 for (int x
= 0; x
< auxv
.size(); x
++) {
375 initVirtMem
.writeBlob(auxv_array_base
+ x
* 2 * intSize
,
376 (uint8_t*)&(auxv
[x
].a_type
), intSize
);
377 initVirtMem
.writeBlob(auxv_array_base
+ (x
* 2 + 1) * intSize
,
378 (uint8_t*)&(auxv
[x
].a_val
), intSize
);
381 // Write out the terminating zeroed auxilliary vector
382 const IntType zero
= 0;
383 initVirtMem
.writeBlob(auxv_array_base
+ intSize
* 2 * auxv
.size(),
384 (uint8_t*)&zero
, intSize
);
385 initVirtMem
.writeBlob(auxv_array_base
+ intSize
* (2 * auxv
.size() + 1),
386 (uint8_t*)&zero
, intSize
);
388 copyStringArray(envp
, envp_array_base
, env_data_base
, initVirtMem
);
389 copyStringArray(argv
, argv_array_base
, arg_data_base
, initVirtMem
);
391 initVirtMem
.writeBlob(argc_base
, (uint8_t*)&guestArgc
, intSize
);
393 // Set up space for the trap handlers into the processes address space.
394 // Since the stack grows down and there is reserved address space abov
395 // it, we can put stuff above it and stay out of the way.
396 fillStart
= stack_base
;
397 spillStart
= fillStart
+ sizeof(MachInst
) * numFillInsts
;
399 ThreadContext
*tc
= system
->getThreadContext(contextIds
[0]);
400 // Set up the thread context to start running the process
401 // assert(NumArgumentRegs >= 2);
402 // tc->setIntReg(ArgumentReg[0], argc);
403 // tc->setIntReg(ArgumentReg[1], argv_array_base);
404 tc
->setIntReg(StackPointerReg
, stack_min
- StackBias
);
406 // %g1 is a pointer to a function that should be run at exit. Since we
407 // don't have anything like that, it should be set to 0.
410 tc
->pcState(getStartPC());
412 // Align the "stack_min" to a page boundary.
413 stack_min
= roundDown(stack_min
, pageSize
);
419 Sparc64LiveProcess::argsInit(int intSize
, int pageSize
)
421 SparcLiveProcess::argsInit
<uint64_t>(pageSize
);
423 // Stuff the trap handlers into the process address space
424 initVirtMem
.writeBlob(fillStart
,
425 (uint8_t*)fillHandler64
, sizeof(MachInst
) * numFillInsts
);
426 initVirtMem
.writeBlob(spillStart
,
427 (uint8_t*)spillHandler64
, sizeof(MachInst
) * numSpillInsts
);
431 Sparc32LiveProcess::argsInit(int intSize
, int pageSize
)
433 SparcLiveProcess::argsInit
<uint32_t>(pageSize
);
435 // Stuff the trap handlers into the process address space
436 initVirtMem
.writeBlob(fillStart
,
437 (uint8_t*)fillHandler32
, sizeof(MachInst
) * numFillInsts
);
438 initVirtMem
.writeBlob(spillStart
,
439 (uint8_t*)spillHandler32
, sizeof(MachInst
) * numSpillInsts
);
442 void Sparc32LiveProcess::flushWindows(ThreadContext
*tc
)
444 IntReg Cansave
= tc
->readIntReg(NumIntArchRegs
+ 3);
445 IntReg Canrestore
= tc
->readIntReg(NumIntArchRegs
+ 4);
446 IntReg Otherwin
= tc
->readIntReg(NumIntArchRegs
+ 6);
447 MiscReg CWP
= tc
->readMiscReg(MISCREG_CWP
);
448 MiscReg origCWP
= CWP
;
449 CWP
= (CWP
+ Cansave
+ 2) % NWindows
;
450 while (NWindows
- 2 - Cansave
!= 0) {
452 panic("Otherwin non-zero.\n");
454 tc
->setMiscReg(MISCREG_CWP
, CWP
);
456 IntReg sp
= tc
->readIntReg(StackPointerReg
);
457 for (int index
= 16; index
< 32; index
++) {
458 uint32_t regVal
= tc
->readIntReg(index
);
459 regVal
= htog(regVal
);
460 if (!tc
->getMemProxy().tryWriteBlob(
461 sp
+ (index
- 16) * 4, (uint8_t *)®Val
, 4)) {
462 warn("Failed to save register to the stack when "
463 "flushing windows.\n");
468 CWP
= (CWP
+ 1) % NWindows
;
471 tc
->setIntReg(NumIntArchRegs
+ 3, Cansave
);
472 tc
->setIntReg(NumIntArchRegs
+ 4, Canrestore
);
473 tc
->setMiscReg(MISCREG_CWP
, origCWP
);
477 Sparc64LiveProcess::flushWindows(ThreadContext
*tc
)
479 IntReg Cansave
= tc
->readIntReg(NumIntArchRegs
+ 3);
480 IntReg Canrestore
= tc
->readIntReg(NumIntArchRegs
+ 4);
481 IntReg Otherwin
= tc
->readIntReg(NumIntArchRegs
+ 6);
482 MiscReg CWP
= tc
->readMiscReg(MISCREG_CWP
);
483 MiscReg origCWP
= CWP
;
484 CWP
= (CWP
+ Cansave
+ 2) % NWindows
;
485 while (NWindows
- 2 - Cansave
!= 0) {
487 panic("Otherwin non-zero.\n");
489 tc
->setMiscReg(MISCREG_CWP
, CWP
);
491 IntReg sp
= tc
->readIntReg(StackPointerReg
);
492 for (int index
= 16; index
< 32; index
++) {
493 IntReg regVal
= tc
->readIntReg(index
);
494 regVal
= htog(regVal
);
495 if (!tc
->getMemProxy().tryWriteBlob(
496 sp
+ 2047 + (index
- 16) * 8, (uint8_t *)®Val
, 8)) {
497 warn("Failed to save register to the stack when "
498 "flushing windows.\n");
503 CWP
= (CWP
+ 1) % NWindows
;
506 tc
->setIntReg(NumIntArchRegs
+ 3, Cansave
);
507 tc
->setIntReg(NumIntArchRegs
+ 4, Canrestore
);
508 tc
->setMiscReg(MISCREG_CWP
, origCWP
);
512 Sparc32LiveProcess::getSyscallArg(ThreadContext
*tc
, int &i
)
515 return bits(tc
->readIntReg(FirstArgumentReg
+ i
++), 31, 0);
519 Sparc32LiveProcess::setSyscallArg(ThreadContext
*tc
, int i
, IntReg val
)
522 tc
->setIntReg(FirstArgumentReg
+ i
, bits(val
, 31, 0));
526 Sparc64LiveProcess::getSyscallArg(ThreadContext
*tc
, int &i
)
529 return tc
->readIntReg(FirstArgumentReg
+ i
++);
533 Sparc64LiveProcess::setSyscallArg(ThreadContext
*tc
, int i
, IntReg val
)
536 tc
->setIntReg(FirstArgumentReg
+ i
, val
);
540 SparcLiveProcess::setSyscallReturn(ThreadContext
*tc
, SyscallReturn sysret
)
542 // check for error condition. SPARC syscall convention is to
543 // indicate success/failure in reg the carry bit of the ccr
544 // and put the return value itself in the standard return value reg ().
545 PSTATE pstate
= tc
->readMiscRegNoEffect(MISCREG_PSTATE
);
546 if (sysret
.successful()) {
547 // no error, clear XCC.C
548 tc
->setIntReg(NumIntArchRegs
+ 2,
549 tc
->readIntReg(NumIntArchRegs
+ 2) & 0xEE);
550 IntReg val
= sysret
.returnValue();
552 val
= bits(val
, 31, 0);
553 tc
->setIntReg(ReturnValueReg
, val
);
555 // got an error, set XCC.C
556 tc
->setIntReg(NumIntArchRegs
+ 2,
557 tc
->readIntReg(NumIntArchRegs
+ 2) | 0x11);
558 IntReg val
= sysret
.errnoValue();
560 val
= bits(val
, 31, 0);
561 tc
->setIntReg(ReturnValueReg
, val
);