2 * Copyright (c) 2003-2005 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 #ifndef __ARCH_SPARC_REGFILE_HH__
33 #define __ARCH_SPARC_REGFILE_HH__
35 #include "arch/sparc/exceptions.hh"
36 #include "arch/sparc/faults.hh"
37 #include "base/trace.hh"
38 #include "sim/byteswap.hh"
39 #include "cpu/cpuevent.hh"
40 #include "sim/host.hh"
47 typedef uint8_t RegIndex;
49 // MAXTL - maximum trap level
55 // NWINDOWS - number of register windows, can be 3 to 32
56 const int NWindows = 32;
59 const int AsrStart = 0;
60 const int PrStart = 32;
61 const int HprStart = 64;
62 const int MiscStart = 96;
65 const uint64_t Bit64 = 0x8000000000000000;
70 static const int FrameOffsetBits = 3;
71 static const int FrameNumBits = 2;
73 static const int RegsPerFrame = 1 << FrameOffsetBits;
74 static const int FrameNumMask =
75 (FrameNumBits == sizeof(int)) ?
77 (1 << FrameNumBits) - 1;
78 static const int FrameOffsetMask =
79 (FrameOffsetBits == sizeof(int)) ?
81 (1 << FrameOffsetBits) - 1;
83 IntReg regGlobals[MaxGL][RegsPerFrame];
84 IntReg regSegments[2 * NWindows][RegsPerFrame];
86 enum regFrame {Globals, Outputs, Locals, Inputs, NumFrames};
88 IntReg * regView[NumFrames];
90 static const int RegGlobalOffset = 0;
91 static const int FrameOffset = MaxGL * RegsPerFrame;
92 int offset[NumFrames];
96 int flattenIndex(int reg)
98 int flatIndex = offset[reg >> FrameOffsetBits]
99 | (reg & FrameOffsetMask);
100 DPRINTF(Sparc, "Flattened index %d into %d.\n", reg, flatIndex);
107 for (x = 0; x < MaxGL; x++)
108 memset(regGlobals[x], 0, sizeof(regGlobals[x]));
109 for(int x = 0; x < 2 * NWindows; x++)
110 bzero(regSegments[x], sizeof(regSegments[x]));
116 regView[Globals] = regGlobals[0];
121 IntReg readReg(int intReg)
124 regView[intReg >> FrameOffsetBits][intReg & FrameOffsetMask];
125 DPRINTF(Sparc, "Read register %d = 0x%x\n", intReg, val);
129 Fault setReg(int intReg, const IntReg &val)
132 DPRINTF(Sparc, "Wrote register %d = 0x%x\n", intReg, val);
133 regView[intReg >> FrameOffsetBits][intReg & FrameOffsetMask] = val;
137 //This doesn't effect the actual CWP register.
138 //It's purpose is to adjust the view of the register file
139 //to what it would be if CWP = cwp.
142 int index = ((NWindows - cwp) % NWindows) * 2;
143 offset[Outputs] = FrameOffset + (index * RegsPerFrame);
144 offset[Locals] = FrameOffset + ((index+1) * RegsPerFrame);
145 offset[Inputs] = FrameOffset +
146 (((index+2) % (NWindows * 2)) * RegsPerFrame);
147 regView[Outputs] = regSegments[index];
148 regView[Locals] = regSegments[index+1];
149 regView[Inputs] = regSegments[(index+2) % (NWindows * 2)];
151 DPRINTF(Sparc, "Changed the CWP value to %d\n", cwp);
154 void setGlobals(int gl)
157 DPRINTF(Sparc, "Now using %d globals", gl);
159 regView[Globals] = regGlobals[gl];
160 offset[Globals] = RegGlobalOffset + gl * RegsPerFrame;
163 void serialize(std::ostream &os);
165 void unserialize(Checkpoint *cp, const std::string §ion);
168 typedef float float32_t;
169 typedef double float64_t;
170 //FIXME long double refers to a 10 byte float, rather than a
171 //16 byte float as required. This data type may have to be emulated.
172 typedef double float128_t;
177 static const int SingleWidth = 32;
178 static const int DoubleWidth = 64;
179 static const int QuadWidth = 128;
183 //Since the floating point registers overlap each other,
184 //A generic storage space is used. The float to be returned is
185 //pulled from the appropriate section of this region.
186 char regSpace[SingleWidth / 8 * NumFloatRegs];
192 bzero(regSpace, sizeof(regSpace));
195 FloatReg readReg(int floatReg, int width)
197 //In each of these cases, we have to copy the value into a temporary
198 //variable. This is because we may otherwise try to access an
199 //unaligned portion of memory.
204 memcpy(&result32, regSpace + 4 * floatReg, width);
205 return htog(result32);
208 memcpy(&result64, regSpace + 4 * floatReg, width);
209 return htog(result64);
211 float128_t result128;
212 memcpy(&result128, regSpace + 4 * floatReg, width);
213 return htog(result128);
215 panic("Attempted to read a %d bit floating point register!", width);
219 FloatRegBits readRegBits(int floatReg, int width)
221 //In each of these cases, we have to copy the value into a temporary
222 //variable. This is because we may otherwise try to access an
223 //unaligned portion of memory.
228 memcpy(&result32, regSpace + 4 * floatReg, width);
229 return htog(result32);
232 memcpy(&result64, regSpace + 4 * floatReg, width);
233 return htog(result64);
236 memcpy(&result128, regSpace + 4 * floatReg, width);
237 return htog(result128);
239 panic("Attempted to read a %d bit floating point register!", width);
243 Fault setReg(int floatReg, const FloatReg &val, int width)
245 //In each of these cases, we have to copy the value into a temporary
246 //variable. This is because we may otherwise try to access an
247 //unaligned portion of memory.
251 uint32_t result32 = gtoh((uint32_t)val);
252 memcpy(regSpace + 4 * floatReg, &result32, width);
254 uint64_t result64 = gtoh((uint64_t)val);
255 memcpy(regSpace + 4 * floatReg, &result64, width);
257 uint64_t result128 = gtoh((uint64_t)val);
258 memcpy(regSpace + 4 * floatReg, &result128, width);
260 panic("Attempted to read a %d bit floating point register!", width);
265 Fault setRegBits(int floatReg, const FloatRegBits &val, int width)
267 //In each of these cases, we have to copy the value into a temporary
268 //variable. This is because we may otherwise try to access an
269 //unaligned portion of memory.
273 uint32_t result32 = gtoh((uint32_t)val);
274 memcpy(regSpace + 4 * floatReg, &result32, width);
276 uint64_t result64 = gtoh((uint64_t)val);
277 memcpy(regSpace + 4 * floatReg, &result64, width);
279 uint64_t result128 = gtoh((uint64_t)val);
280 memcpy(regSpace + 4 * floatReg, &result128, width);
282 panic("Attempted to read a %d bit floating point register!", width);
287 void serialize(std::ostream &os);
289 void unserialize(Checkpoint *cp, const std::string §ion);
294 /** Ancillary State Registers */
295 MISCREG_Y = AsrStart + 0,
296 MISCREG_CCR = AsrStart + 2,
297 MISCREG_ASI = AsrStart + 3,
298 MISCREG_TICK = AsrStart + 4,
299 MISCREG_PC = AsrStart + 5,
300 MISCREG_FPRS = AsrStart + 6,
301 MISCREG_PCR = AsrStart + 16,
302 MISCREG_PIC = AsrStart + 17,
303 MISCREG_GSR = AsrStart + 19,
304 MISCREG_SOFTINT_SET = AsrStart + 20,
305 MISCREG_SOFTINT_CLR = AsrStart + 21,
306 MISCREG_SOFTINT = AsrStart + 22,
307 MISCREG_TICK_CMPR = AsrStart + 23,
308 MISCREG_STICK = AsrStart + 24,
309 MISCREG_STICK_CMPR = AsrStart + 25,
311 /** Privilged Registers */
312 MISCREG_TPC = PrStart + 0,
313 MISCREG_TNPC = PrStart + 1,
314 MISCREG_TSTATE = PrStart + 2,
315 MISCREG_TT = PrStart + 3,
316 MISCREG_PRIVTICK = PrStart + 4,
317 MISCREG_TBA = PrStart + 5,
318 MISCREG_PSTATE = PrStart + 6,
319 MISCREG_TL = PrStart + 7,
320 MISCREG_PIL = PrStart + 8,
321 MISCREG_CWP = PrStart + 9,
322 MISCREG_CANSAVE = PrStart + 10,
323 MISCREG_CANRESTORE = PrStart + 11,
324 MISCREG_CLEANWIN = PrStart + 12,
325 MISCREG_OTHERWIN = PrStart + 13,
326 MISCREG_WSTATE = PrStart + 14,
327 MISCREG_GL = PrStart + 16,
329 /** Hyper privileged registers */
330 MISCREG_HPSTATE = HprStart + 0,
331 MISCREG_HTSTATE = HprStart + 1,
332 MISCREG_HINTP = HprStart + 3,
333 MISCREG_HTBA = HprStart + 5,
334 MISCREG_HVER = HprStart + 6,
335 MISCREG_STRAND_STS_REG = HprStart + 16,
336 MISCREG_HSTICK_CMPR = HprStart + 31,
338 /** Floating Point Status Register */
339 MISCREG_FSR = MiscStart + 0
343 // The control registers, broken out into fields
350 uint64_t y; // Y (used in obsolete multiplication)
352 uint64_t value:32; // The actual value stored in y
353 uint64_t :32; // reserved bits
357 uint8_t ccr; // Condition Code Register
360 uint8_t icc:4; // 32-bit condition codes
362 uint8_t c:1; // Carry
363 uint8_t v:1; // Overflow
365 uint8_t n:1; // Negative
369 uint8_t xcc:4; // 64-bit condition codes
371 uint8_t c:1; // Carry
372 uint8_t v:1; // Overflow
374 uint8_t n:1; // Negative
379 uint8_t asi; // Address Space Identifier
381 uint64_t tick; // Hardware clock-tick counter
383 int64_t counter:63; // Clock-tick count
384 uint64_t npt:1; // Non-priveleged trap
388 uint8_t fprs; // Floating-Point Register State
390 uint8_t dl:1; // Dirty lower
391 uint8_t du:1; // Dirty upper
392 uint8_t fef:1; // FPRS enable floating-Point
399 uint64_t int_level:14;
404 uint64_t tick_cmpr; // Hardware tick compare registers
406 uint64_t tick_cmpr:63; // Clock-tick count
407 uint64_t int_dis:1; // Non-priveleged trap
411 uint64_t stick; // Hardware clock-tick counter
413 int64_t :63; // Not used, storage in SparcSystem
414 uint64_t npt:1; // Non-priveleged trap
418 uint64_t stick_cmpr; // Hardware tick compare registers
420 uint64_t tick_cmpr:63; // Clock-tick count
421 uint64_t int_dis:1; // Non-priveleged trap
426 /* Privileged Registers */
427 uint64_t tpc[MaxTL]; // Trap Program Counter (value from
428 // previous trap level)
429 uint64_t tnpc[MaxTL]; // Trap Next Program Counter (value from
430 // previous trap level)
432 uint64_t tstate[MaxTL]; // Trap State
434 //Values are from previous trap level
435 uint64_t cwp:5; // Current Window Pointer
436 uint64_t :3; // Reserved bits
437 uint64_t pstate:13; // Process State
438 uint64_t :3; // Reserved bits
439 uint64_t asi:8; // Address Space Identifier
440 uint64_t ccr:8; // Condition Code Register
441 uint64_t gl:8; // Global level
442 } tstateFields[MaxTL];
444 uint16_t tt[MaxTL]; // Trap Type (Type of trap which occured
445 // on the previous level)
446 uint64_t tba; // Trap Base Address
449 uint16_t pstate; // Process State Register
451 uint16_t :1; // reserved
452 uint16_t ie:1; // Interrupt enable
453 uint16_t priv:1; // Privelege mode
454 uint16_t am:1; // Address mask
455 uint16_t pef:1; // PSTATE enable floating-point
456 uint16_t :1; // reserved2
457 uint16_t mm:2; // Memory Model
458 uint16_t tle:1; // Trap little-endian
459 uint16_t cle:1; // Current little-endian
462 uint8_t tl; // Trap Level
463 uint8_t pil; // Process Interrupt Register
464 uint8_t cwp; // Current Window Pointer
465 uint8_t cansave; // Savable windows
466 uint8_t canrestore; // Restorable windows
467 uint8_t cleanwin; // Clean windows
468 uint8_t otherwin; // Other windows
470 uint8_t wstate; // Window State
472 uint8_t normal:3; // Bits TT<4:2> are set to on a normal
473 // register window trap
474 uint8_t other:3; // Bits TT<4:2> are set to on an "otherwin"
475 // register window trap
478 uint8_t gl; // Global level register
481 /** Hyperprivileged Registers */
483 uint64_t hpstate; // Hyperprivileged State Register
496 uint64_t htstate[MaxTL]; // Hyperprivileged Trap State Register
498 uint64_t htba; // Hyperprivileged Trap Base Address register
500 uint64_t hstick_cmpr; // Hardware tick compare registers
502 uint64_t tick_cmpr:63; // Clock-tick count
503 uint64_t int_dis:1; // Non-priveleged trap
507 uint64_t strandStatusReg; // Per strand status register
510 /** Floating point misc registers. */
512 uint64_t fsr; // Floating-Point State Register
515 uint64_t cexc:5; // Current excpetion
517 uint64_t nxc:1; // Inexact
518 uint64_t dzc:1; // Divide by zero
519 uint64_t ufc:1; // Underflow
520 uint64_t ofc:1; // Overflow
521 uint64_t nvc:1; // Invalid operand
525 uint64_t aexc:5; // Accrued exception
527 uint64_t nxc:1; // Inexact
528 uint64_t dzc:1; // Divide by zero
529 uint64_t ufc:1; // Underflow
530 uint64_t ofc:1; // Overflow
531 uint64_t nvc:1; // Invalid operand
534 uint64_t fcc0:2; // Floating-Point condtion codes
535 uint64_t :1; // Reserved bits
536 uint64_t qne:1; // Deferred trap queue not empty
537 // with no queue, it should read 0
538 uint64_t ftt:3; // Floating-Point trap type
539 uint64_t ver:3; // Version (of the FPU)
540 uint64_t :2; // Reserved bits
541 uint64_t ns:1; // Nonstandard floating point
543 uint64_t tem:5; // Trap Enable Mask
545 uint64_t nxm:1; // Inexact
546 uint64_t dzm:1; // Divide by zero
547 uint64_t ufm:1; // Underflow
548 uint64_t ofm:1; // Overflow
549 uint64_t nvm:1; // Invalid operand
552 uint64_t :2; // Reserved bits
553 uint64_t rd:2; // Rounding direction
554 uint64_t fcc1:2; // Floating-Point condition codes
555 uint64_t fcc2:2; // Floating-Point condition codes
556 uint64_t fcc3:2; // Floating-Point condition codes
557 uint64_t :26; // Reserved bits
561 // These need to check the int_dis field and if 0 then
562 // set appropriate bit in softint and checkinterrutps on the cpu
564 /** Process a tick compare event and generate an interrupt on the cpu if
566 void processTickCompare(ExecContext *xc);
567 void processSTickCompare(ExecContext *xc);
568 void processHSTickCompare(ExecContext *xc);
570 typedef CpuEventWrapper<MiscRegFile,
571 &MiscRegFile::processTickCompare> TickCompareEvent;
572 TickCompareEvent *tickCompare;
574 typedef CpuEventWrapper<MiscRegFile,
575 &MiscRegFile::processSTickCompare> STickCompareEvent;
576 STickCompareEvent *sTickCompare;
578 typedef CpuEventWrapper<MiscRegFile,
579 &MiscRegFile::processHSTickCompare> HSTickCompareEvent;
580 HSTickCompareEvent *hSTickCompare;
582 /** Fullsystem only register version of ReadRegWithEffect() */
583 MiscReg readFSRegWithEffect(int miscReg, Fault &fault, ExecContext *xc);
584 /** Fullsystem only register version of SetRegWithEffect() */
585 Fault setFSRegWithEffect(int miscReg, const MiscReg &val,
592 pstateFields.pef = 0; //No FPU
593 //pstateFields.pef = 1; //FPU
595 //For SPARC, when a system is first started, there is a power
596 //on reset Trap which sets the processor into the following state.
597 //Bits that aren't set aren't defined on startup.
601 tickFields.counter = 0; //The TICK register is unreadable bya
602 tickFields.npt = 1; //The TICK register is unreadable by by !priv
604 softint = 0; // Clear all the soft interrupt bits
605 tick_cmprFields.int_dis = 1; // disable timer compare interrupts
606 tick_cmprFields.tick_cmpr = 0; // Reset to 0 for pretty printing
607 stickFields.npt = 1; //The TICK register is unreadable by by !priv
608 stick_cmprFields.int_dis = 1; // disable timer compare interrupts
609 stick_cmprFields.tick_cmpr = 0; // Reset to 0 for pretty printing
612 tt[tl] = power_on_reset;
613 pstate = 0; // fields 0 but pef
614 pstateFields.pef = 1;
617 hpstateFields.red = 1;
618 hpstateFields.hpriv = 1;
619 hpstateFields.tlz = 0; // this is a guess
621 hintp = 0; // no interrupts pending
622 hstick_cmprFields.int_dis = 1; // disable timer compare interrupts
623 hstick_cmprFields.tick_cmpr = 0; // Reset to 0 for pretty printing
626 /* //This sets up the initial state of the processor for usermode processes
627 pstateFields.priv = 0; //Process runs in user mode
628 pstateFields.ie = 1; //Interrupts are enabled
629 fsrFields.rd = 0; //Round to nearest
630 fsrFields.tem = 0; //Floating point traps not enabled
631 fsrFields.ns = 0; //Non standard mode off
632 fsrFields.qne = 0; //Floating point queue is empty
633 fsrFields.aexc = 0; //No accrued exceptions
634 fsrFields.cexc = 0; //No current exceptions
636 //Register window management registers
637 otherwin = 0; //No windows contain info from other programs
638 canrestore = 0; //There are no windows to pop
639 cansave = MaxTL - 2; //All windows are available to save into
649 /** read a value out of an either an SE or FS IPR. No checking is done
650 * about SE vs. FS as this is mostly used to copy the regfile. Thus more
651 * register are copied that are necessary for FS. However this prevents
652 * a bunch of ifdefs and is rarely called so is not performance
654 MiscReg readReg(int miscReg);
656 /** Read a value from an IPR. Only the SE iprs are here and the rest
657 * are are readFSRegWithEffect (which is called by readRegWithEffect()).
658 * Checking is done for permission based on state bits in the miscreg
660 MiscReg readRegWithEffect(int miscReg, Fault &fault, ExecContext *xc);
662 /** write a value into an either an SE or FS IPR. No checking is done
663 * about SE vs. FS as this is mostly used to copy the regfile. Thus more
664 * register are copied that are necessary for FS. However this prevents
665 * a bunch of ifdefs and is rarely called so is not performance
667 Fault setReg(int miscReg, const MiscReg &val);
669 /** Write a value into an IPR. Only the SE iprs are here and the rest
670 * are are setFSRegWithEffect (which is called by setRegWithEffect()).
671 * Checking is done for permission based on state bits in the miscreg
673 Fault setRegWithEffect(int miscReg,
674 const MiscReg &val, ExecContext * xc);
676 void serialize(std::ostream & os);
678 void unserialize(Checkpoint * cp, const std::string & section);
680 void copyMiscRegs(ExecContext * xc);
682 bool isHyperPriv() { return hpstateFields.hpriv; }
683 bool isPriv() { return hpstateFields.hpriv || pstateFields.priv; }
684 bool isNonPriv() { return !isPriv(); }
697 Addr pc; // Program Counter
698 Addr npc; // Next Program Counter
717 void setNextPC(Addr val)
727 void setNextNPC(Addr val)
733 IntRegFile intRegFile; // integer register file
734 FloatRegFile floatRegFile; // floating point register file
735 MiscRegFile miscRegFile; // control register file
742 floatRegFile.clear();
745 int FlattenIntIndex(int reg)
747 return intRegFile.flattenIndex(reg);
750 MiscReg readMiscReg(int miscReg)
752 return miscRegFile.readReg(miscReg);
755 MiscReg readMiscRegWithEffect(int miscReg,
756 Fault &fault, ExecContext *xc)
758 return miscRegFile.readRegWithEffect(miscReg, fault, xc);
761 Fault setMiscReg(int miscReg, const MiscReg &val)
763 return miscRegFile.setReg(miscReg, val);
766 Fault setMiscRegWithEffect(int miscReg, const MiscReg &val,
769 return miscRegFile.setRegWithEffect(miscReg, val, xc);
772 FloatReg readFloatReg(int floatReg, int width)
774 return floatRegFile.readReg(floatReg, width);
777 FloatReg readFloatReg(int floatReg)
779 //Use the "natural" width of a single float
780 return floatRegFile.readReg(floatReg, FloatRegFile::SingleWidth);
783 FloatRegBits readFloatRegBits(int floatReg, int width)
785 return floatRegFile.readRegBits(floatReg, width);
788 FloatRegBits readFloatRegBits(int floatReg)
790 //Use the "natural" width of a single float
791 return floatRegFile.readRegBits(floatReg,
792 FloatRegFile::SingleWidth);
795 Fault setFloatReg(int floatReg, const FloatReg &val, int width)
797 return floatRegFile.setReg(floatReg, val, width);
800 Fault setFloatReg(int floatReg, const FloatReg &val)
802 //Use the "natural" width of a single float
803 return setFloatReg(floatReg, val, FloatRegFile::SingleWidth);
806 Fault setFloatRegBits(int floatReg, const FloatRegBits &val, int width)
808 return floatRegFile.setRegBits(floatReg, val, width);
811 Fault setFloatRegBits(int floatReg, const FloatRegBits &val)
813 //Use the "natural" width of a single float
814 return floatRegFile.setRegBits(floatReg, val,
815 FloatRegFile::SingleWidth);
818 IntReg readIntReg(int intReg)
820 return intRegFile.readReg(intReg);
823 Fault setIntReg(int intReg, const IntReg &val)
825 return intRegFile.setReg(intReg, val);
828 void serialize(std::ostream &os);
829 void unserialize(Checkpoint *cp, const std::string §ion);
838 typedef int ContextVal;
840 void changeContext(ContextParam param, ContextVal val)
845 intRegFile.setCWP(val);
847 case CONTEXT_GLOBALS:
848 intRegFile.setGlobals(val);
851 panic("Tried to set illegal context parameter in the SPARC regfile.\n");
856 void copyRegs(ExecContext *src, ExecContext *dest);
858 void copyMiscRegs(ExecContext *src, ExecContext *dest);
860 int InterruptLevel(uint64_t softint);
862 } // namespace SparcISA