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
;
50 SparcLiveProcess::SparcLiveProcess(LiveProcessParams
* params
,
51 ObjectFile
*objFile
, Addr _StackBias
)
52 : LiveProcess(params
, objFile
), StackBias(_StackBias
)
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
);
59 // Set pointer for next thread stack. Reserve 8M for main stack.
60 next_thread_stack_base
= stack_base
- (8 * 1024 * 1024);
62 //Initialize these to 0s
67 void SparcLiveProcess::handleTrap(int trapNum
, ThreadContext
*tc
)
71 case 0x01: //Software breakpoint
72 warn("Software breakpoint encountered at pc %#x.\n", tc
->readPC());
74 case 0x02: //Division by zero
75 warn("Software signaled a division by zero at pc %#x.\n",
78 case 0x03: //Flush window trap
81 case 0x04: //Clean windows
82 warn("Ignoring process request for clean register "
83 "windows at pc %#x.\n", tc
->readPC());
85 case 0x05: //Range check
86 warn("Software signaled a range check at pc %#x.\n",
89 case 0x06: //Fix alignment
90 warn("Ignoring process request for os assisted unaligned accesses "
91 "at pc %#x.\n", tc
->readPC());
93 case 0x07: //Integer overflow
94 warn("Software signaled an integer overflow at pc %#x.\n",
97 case 0x32: //Get integer condition codes
98 warn("Ignoring process request to get the integer condition codes "
99 "at pc %#x.\n", tc
->readPC());
101 case 0x33: //Set integer condition codes
102 warn("Ignoring process request to set the integer condition codes "
103 "at pc %#x.\n", tc
->readPC());
106 panic("Unimplemented trap to operating system: trap number %#x.\n", trapNum
);
111 SparcLiveProcess::startup()
115 ThreadContext
*tc
= system
->getThreadContext(contextIds
[0]);
118 //Setup default FP state
119 tc
->setMiscRegNoEffect(MISCREG_FSR
, 0);
121 tc
->setMiscRegNoEffect(MISCREG_TICK
, 0);
124 * Register window management registers
127 //No windows contain info from other programs
128 //tc->setMiscRegNoEffect(MISCREG_OTHERWIN, 0);
129 tc
->setIntReg(NumIntArchRegs
+ 6, 0);
130 //There are no windows to pop
131 //tc->setMiscRegNoEffect(MISCREG_CANRESTORE, 0);
132 tc
->setIntReg(NumIntArchRegs
+ 4, 0);
133 //All windows are available to save into
134 //tc->setMiscRegNoEffect(MISCREG_CANSAVE, NWindows - 2);
135 tc
->setIntReg(NumIntArchRegs
+ 3, NWindows
- 2);
136 //All windows are "clean"
137 //tc->setMiscRegNoEffect(MISCREG_CLEANWIN, NWindows);
138 tc
->setIntReg(NumIntArchRegs
+ 5, NWindows
);
139 //Start with register window 0
140 tc
->setMiscRegNoEffect(MISCREG_CWP
, 0);
141 //Always use spill and fill traps 0
142 //tc->setMiscRegNoEffect(MISCREG_WSTATE, 0);
143 tc
->setIntReg(NumIntArchRegs
+ 7, 0);
144 //Set the trap level to 0
145 tc
->setMiscRegNoEffect(MISCREG_TL
, 0);
146 //Set the ASI register to something fixed
147 tc
->setMiscRegNoEffect(MISCREG_ASI
, ASI_PRIMARY
);
150 * T1 specific registers
152 //Turn on the icache, dcache, dtb translation, and itb translation.
153 tc
->setMiscRegNoEffect(MISCREG_MMU_LSU_CTRL
, 15);
157 Sparc32LiveProcess::startup()
159 if (checkpointRestored
)
162 SparcLiveProcess::startup();
164 ThreadContext
*tc
= system
->getThreadContext(contextIds
[0]);
165 //The process runs in user mode with 32 bit addresses
166 tc
->setMiscReg(MISCREG_PSTATE
, 0x0a);
168 argsInit(32 / 8, VMPageSize
);
172 Sparc64LiveProcess::startup()
174 if (checkpointRestored
)
177 SparcLiveProcess::startup();
179 ThreadContext
*tc
= system
->getThreadContext(contextIds
[0]);
180 //The process runs in user mode
181 tc
->setMiscReg(MISCREG_PSTATE
, 0x02);
183 argsInit(sizeof(IntReg
), VMPageSize
);
186 template<class IntType
>
188 SparcLiveProcess::argsInit(int pageSize
)
190 int intSize
= sizeof(IntType
);
192 typedef AuxVector
<IntType
> auxv_t
;
194 std::vector
<auxv_t
> auxv
;
202 //Even for a 32 bit process, the ABI says we still need to
203 //maintain double word alignment of the stack pointer.
206 // load object file into target memory
207 objFile
->loadSections(initVirtMem
);
211 M5_HWCAP_SPARC_FLUSH
= 1,
212 M5_HWCAP_SPARC_STBAR
= 2,
213 M5_HWCAP_SPARC_SWAP
= 4,
214 M5_HWCAP_SPARC_MULDIV
= 8,
215 M5_HWCAP_SPARC_V9
= 16,
216 //This one should technically only be set
217 //if there is a cheetah or cheetah_plus tlb,
218 //but we'll use it all the time
219 M5_HWCAP_SPARC_ULTRA3
= 32
222 const int64_t hwcap
=
223 M5_HWCAP_SPARC_FLUSH
|
224 M5_HWCAP_SPARC_STBAR
|
225 M5_HWCAP_SPARC_SWAP
|
226 M5_HWCAP_SPARC_MULDIV
|
228 M5_HWCAP_SPARC_ULTRA3
;
230 //Setup the auxilliary vectors. These will already have endian conversion.
231 //Auxilliary vectors are loaded only for elf formatted executables.
232 ElfObject
* elfObject
= dynamic_cast<ElfObject
*>(objFile
);
235 //Bits which describe the system hardware capabilities
236 auxv
.push_back(auxv_t(M5_AT_HWCAP
, hwcap
));
237 //The system page size
238 auxv
.push_back(auxv_t(M5_AT_PAGESZ
, SparcISA::VMPageSize
));
239 //Defined to be 100 in the kernel source.
240 //Frequency at which times() increments
241 auxv
.push_back(auxv_t(M5_AT_CLKTCK
, 100));
242 // For statically linked executables, this is the virtual address of the
243 // program header tables if they appear in the executable image
244 auxv
.push_back(auxv_t(M5_AT_PHDR
, elfObject
->programHeaderTable()));
245 // This is the size of a program header entry from the elf file.
246 auxv
.push_back(auxv_t(M5_AT_PHENT
, elfObject
->programHeaderSize()));
247 // This is the number of program headers from the original elf file.
248 auxv
.push_back(auxv_t(M5_AT_PHNUM
, elfObject
->programHeaderCount()));
249 //This is the address of the elf "interpreter", It should be set
250 //to 0 for regular executables. It should be something else
251 //(not sure what) for dynamic libraries.
252 auxv
.push_back(auxv_t(M5_AT_BASE
, 0));
253 //This is hardwired to 0 in the elf loading code in the kernel
254 auxv
.push_back(auxv_t(M5_AT_FLAGS
, 0));
255 //The entry point to the program
256 auxv
.push_back(auxv_t(M5_AT_ENTRY
, objFile
->entryPoint()));
257 //Different user and group IDs
258 auxv
.push_back(auxv_t(M5_AT_UID
, uid()));
259 auxv
.push_back(auxv_t(M5_AT_EUID
, euid()));
260 auxv
.push_back(auxv_t(M5_AT_GID
, gid()));
261 auxv
.push_back(auxv_t(M5_AT_EGID
, egid()));
262 //Whether to enable "secure mode" in the executable
263 auxv
.push_back(auxv_t(M5_AT_SECURE
, 0));
266 //Figure out how big the initial stack needs to be
268 // The unaccounted for 8 byte 0 at the top of the stack
271 //This is the name of the file which is present on the initial stack
272 //It's purpose is to let the user space linker examine the original file.
273 int file_name_size
= filename
.size() + 1;
275 int env_data_size
= 0;
276 for (int i
= 0; i
< envp
.size(); ++i
) {
277 env_data_size
+= envp
[i
].size() + 1;
279 int arg_data_size
= 0;
280 for (int i
= 0; i
< argv
.size(); ++i
) {
281 arg_data_size
+= argv
[i
].size() + 1;
285 int base_info_block_size
=
286 sentry_size
+ file_name_size
+ env_data_size
+ arg_data_size
;
288 int info_block_size
= roundUp(base_info_block_size
, align
);
290 int info_block_padding
= info_block_size
- base_info_block_size
;
292 //Each auxilliary vector is two words
293 int aux_array_size
= intSize
* 2 * (auxv
.size() + 1);
295 int envp_array_size
= intSize
* (envp
.size() + 1);
296 int argv_array_size
= intSize
* (argv
.size() + 1);
298 int argc_size
= intSize
;
299 int window_save_size
= intSize
* 16;
301 //Figure out the size of the contents of the actual initial frame
309 //There needs to be padding after the auxiliary vector data so that the
310 //very bottom of the stack is aligned properly.
311 int aligned_partial_size
= roundUp(frame_size
, align
);
312 int aux_padding
= aligned_partial_size
- frame_size
;
319 stack_min
= stack_base
- space_needed
;
320 stack_min
= roundDown(stack_min
, align
);
321 stack_size
= stack_base
- stack_min
;
323 // Allocate space for the stack
324 pTable
->allocate(roundDown(stack_min
, pageSize
),
325 roundUp(stack_size
, pageSize
));
327 // map out initial stack contents
328 IntType sentry_base
= stack_base
- sentry_size
;
329 IntType file_name_base
= sentry_base
- file_name_size
;
330 IntType env_data_base
= file_name_base
- env_data_size
;
331 IntType arg_data_base
= env_data_base
- arg_data_size
;
332 IntType auxv_array_base
= arg_data_base
-
333 info_block_padding
- aux_array_size
- aux_padding
;
334 IntType envp_array_base
= auxv_array_base
- envp_array_size
;
335 IntType argv_array_base
= envp_array_base
- argv_array_size
;
336 IntType argc_base
= argv_array_base
- argc_size
;
338 IntType window_save_base
= argc_base
- window_save_size
;
341 DPRINTF(Sparc
, "The addresses of items on the initial stack:\n");
342 DPRINTF(Sparc
, "%#x - sentry NULL\n", sentry_base
);
343 DPRINTF(Sparc
, "filename = %s\n", filename
);
344 DPRINTF(Sparc
, "%#x - file name\n", file_name_base
);
345 DPRINTF(Sparc
, "%#x - env data\n", env_data_base
);
346 DPRINTF(Sparc
, "%#x - arg data\n", arg_data_base
);
347 DPRINTF(Sparc
, "%#x - auxv array\n", auxv_array_base
);
348 DPRINTF(Sparc
, "%#x - envp array\n", envp_array_base
);
349 DPRINTF(Sparc
, "%#x - argv array\n", argv_array_base
);
350 DPRINTF(Sparc
, "%#x - argc \n", argc_base
);
351 DPRINTF(Sparc
, "%#x - window save\n", window_save_base
);
352 DPRINTF(Sparc
, "%#x - stack min\n", stack_min
);
354 assert(window_save_base
== stack_min
);
356 // write contents to stack
359 IntType argc
= argv
.size();
360 IntType guestArgc
= SparcISA::htog(argc
);
362 //Write out the sentry void *
363 uint64_t sentry_NULL
= 0;
364 initVirtMem
->writeBlob(sentry_base
,
365 (uint8_t*)&sentry_NULL
, sentry_size
);
367 //Write the file name
368 initVirtMem
->writeString(file_name_base
, filename
.c_str());
371 for(int x
= 0; x
< auxv
.size(); x
++)
373 initVirtMem
->writeBlob(auxv_array_base
+ x
* 2 * intSize
,
374 (uint8_t*)&(auxv
[x
].a_type
), intSize
);
375 initVirtMem
->writeBlob(auxv_array_base
+ (x
* 2 + 1) * intSize
,
376 (uint8_t*)&(auxv
[x
].a_val
), intSize
);
379 //Write out the terminating zeroed auxilliary vector
380 const IntType zero
= 0;
381 initVirtMem
->writeBlob(auxv_array_base
+ intSize
* 2 * auxv
.size(),
382 (uint8_t*)&zero
, intSize
);
383 initVirtMem
->writeBlob(auxv_array_base
+ intSize
* (2 * auxv
.size() + 1),
384 (uint8_t*)&zero
, intSize
);
386 copyStringArray(envp
, envp_array_base
, env_data_base
, initVirtMem
);
387 copyStringArray(argv
, argv_array_base
, arg_data_base
, initVirtMem
);
389 initVirtMem
->writeBlob(argc_base
, (uint8_t*)&guestArgc
, intSize
);
391 //Set up space for the trap handlers into the processes address space.
392 //Since the stack grows down and there is reserved address space abov
393 //it, we can put stuff above it and stay out of the way.
394 fillStart
= stack_base
;
395 spillStart
= fillStart
+ sizeof(MachInst
) * numFillInsts
;
397 ThreadContext
*tc
= system
->getThreadContext(contextIds
[0]);
398 //Set up the thread context to start running the process
399 //assert(NumArgumentRegs >= 2);
400 //tc->setIntReg(ArgumentReg[0], argc);
401 //tc->setIntReg(ArgumentReg[1], argv_array_base);
402 tc
->setIntReg(StackPointerReg
, stack_min
- StackBias
);
404 // %g1 is a pointer to a function that should be run at exit. Since we
405 // don't have anything like that, it should be set to 0.
408 Addr prog_entry
= objFile
->entryPoint();
409 tc
->setPC(prog_entry
);
410 tc
->setNextPC(prog_entry
+ sizeof(MachInst
));
411 tc
->setNextNPC(prog_entry
+ (2 * sizeof(MachInst
)));
413 //Align the "stack_min" to a page boundary.
414 stack_min
= roundDown(stack_min
, pageSize
);
420 Sparc64LiveProcess::argsInit(int intSize
, int pageSize
)
422 SparcLiveProcess::argsInit
<uint64_t>(pageSize
);
424 // Stuff the trap handlers into the process address space
425 initVirtMem
->writeBlob(fillStart
,
426 (uint8_t*)fillHandler64
, sizeof(MachInst
) * numFillInsts
);
427 initVirtMem
->writeBlob(spillStart
,
428 (uint8_t*)spillHandler64
, sizeof(MachInst
) * numSpillInsts
);
432 Sparc32LiveProcess::argsInit(int intSize
, int pageSize
)
434 SparcLiveProcess::argsInit
<uint32_t>(pageSize
);
436 // Stuff the trap handlers into the process address space
437 initVirtMem
->writeBlob(fillStart
,
438 (uint8_t*)fillHandler32
, sizeof(MachInst
) * numFillInsts
);
439 initVirtMem
->writeBlob(spillStart
,
440 (uint8_t*)spillHandler32
, sizeof(MachInst
) * numSpillInsts
);
443 void Sparc32LiveProcess::flushWindows(ThreadContext
*tc
)
445 IntReg Cansave
= tc
->readIntReg(NumIntArchRegs
+ 3);
446 IntReg Canrestore
= tc
->readIntReg(NumIntArchRegs
+ 4);
447 IntReg Otherwin
= tc
->readIntReg(NumIntArchRegs
+ 6);
448 MiscReg CWP
= tc
->readMiscReg(MISCREG_CWP
);
449 MiscReg origCWP
= CWP
;
450 CWP
= (CWP
+ Cansave
+ 2) % NWindows
;
451 while(NWindows
- 2 - Cansave
!= 0)
454 panic("Otherwin non-zero.\n");
456 tc
->setMiscReg(MISCREG_CWP
, CWP
);
458 IntReg sp
= tc
->readIntReg(StackPointerReg
);
459 for (int index
= 16; index
< 32; index
++) {
460 uint32_t regVal
= tc
->readIntReg(index
);
461 regVal
= htog(regVal
);
462 if (!tc
->getMemPort()->tryWriteBlob(
463 sp
+ (index
- 16) * 4, (uint8_t *)®Val
, 4)) {
464 warn("Failed to save register to the stack when "
465 "flushing windows.\n");
470 CWP
= (CWP
+ 1) % NWindows
;
473 tc
->setIntReg(NumIntArchRegs
+ 3, Cansave
);
474 tc
->setIntReg(NumIntArchRegs
+ 4, Canrestore
);
475 tc
->setMiscReg(MISCREG_CWP
, origCWP
);
478 void Sparc64LiveProcess::flushWindows(ThreadContext
*tc
)
480 IntReg Cansave
= tc
->readIntReg(NumIntArchRegs
+ 3);
481 IntReg Canrestore
= tc
->readIntReg(NumIntArchRegs
+ 4);
482 IntReg Otherwin
= tc
->readIntReg(NumIntArchRegs
+ 6);
483 MiscReg CWP
= tc
->readMiscReg(MISCREG_CWP
);
484 MiscReg origCWP
= CWP
;
485 CWP
= (CWP
+ Cansave
+ 2) % NWindows
;
486 while(NWindows
- 2 - Cansave
!= 0)
489 panic("Otherwin non-zero.\n");
491 tc
->setMiscReg(MISCREG_CWP
, CWP
);
493 IntReg sp
= tc
->readIntReg(StackPointerReg
);
494 for (int index
= 16; index
< 32; index
++) {
495 IntReg regVal
= tc
->readIntReg(index
);
496 regVal
= htog(regVal
);
497 if (!tc
->getMemPort()->tryWriteBlob(
498 sp
+ 2047 + (index
- 16) * 8, (uint8_t *)®Val
, 8)) {
499 warn("Failed to save register to the stack when "
500 "flushing windows.\n");
505 CWP
= (CWP
+ 1) % NWindows
;
508 tc
->setIntReg(NumIntArchRegs
+ 3, Cansave
);
509 tc
->setIntReg(NumIntArchRegs
+ 4, Canrestore
);
510 tc
->setMiscReg(MISCREG_CWP
, origCWP
);