3 * https://bugs.libre-soc.org/show_bug.cgi?id=325
4 * https://bugs.libre-soc.org/show_bug.cgi?id=344
5 * https://libre-soc.org/openpower/isa/fixedtrap/
8 from nmigen
import (Module
, Signal
, Cat
, Mux
, Const
, signed
)
9 from nmutil
.pipemodbase
import PipeModBase
10 from nmutil
.extend
import exts
11 from soc
.fu
.trap
.pipe_data
import TrapInputData
, TrapOutputData
12 from soc
.fu
.branch
.main_stage
import br_ext
13 from soc
.decoder
.power_enums
import InternalOp
15 from soc
.decoder
.power_fields
import DecodeFields
16 from soc
.decoder
.power_fieldsn
import SignalBitRange
18 from soc
.decoder
.power_decoder2
import (TT_FP
, TT_PRIV
, TT_TRAP
, TT_ADDR
)
20 # Listed in V3.0B Book III Chap 4.2.1
22 MSR_SF
= (63 - 0) # Sixty-Four bit mode
23 MSR_HV
= (63 - 3) # Hypervisor state
24 MSR_S
= (63 - 41) # Secure state
25 MSR_EE
= (63 - 48) # External interrupt Enable
26 MSR_PR
= (63 - 49) # PRoblem state
27 MSR_FP
= (63 - 50) # FP available
28 MSR_ME
= (63 - 51) # Machine Check int enable
29 MSR_IR
= (63 - 58) # Instruction Relocation
30 MSR_DR
= (63 - 59) # Data Relocation
31 MSR_PMM
= (63 - 60) # Performance Monitor Mark
32 MSR_RI
= (63 - 62) # Recoverable Interrupt
33 MSR_LE
= (63 - 63) # Little Endian
35 # Listed in V3.0B Book III 7.5.9 "Program Interrupt"
37 # note that these correspond to trap_input_record.traptype bits 0,1,2,3
40 PI_FP
= (63 - 43) # 1 if FP exception
41 PI_PRIV
= (63 - 45) # 1 if privileged interrupt
42 PI_TRAP
= (63 - 46) # 1 if exception is "trap" type
43 PI_ADR
= (63 - 47) # 0 if SRR0 = address of instruction causing exception
46 def msr_copy(msr_o
, msr_i
, zero_me
=True):
49 -- Defined MSR bits are classified as either full func-
50 -- tion or partial function. Full function MSR bits are
51 -- saved in SRR1 or HSRR1 when an interrupt other
52 -- than a System Call Vectored interrupt occurs and
53 -- restored by rfscv, rfid, or hrfid, while partial func-
54 -- tion MSR bits are not saved or restored.
55 -- Full function MSR bits lie in the range 0:32, 37:41, and
56 -- 48:63, and partial function MSR bits lie in the range
57 -- 33:36 and 42:47. (Note this is IBM bit numbering).
58 msr_out := (others => '0');
59 msr_out(63 downto 31) := msr(63 downto 31);
60 msr_out(26 downto 22) := msr(26 downto 22);
61 msr_out(15 downto 0) := msr(15 downto 0);
66 for stt
, end
in [(0,16), (22, 27), (31, 64)]:
67 l
.append(msr_o
[stt
:end
].eq(msr_i
[stt
:end
]))
71 def msr_check_pr(m
, msr
):
72 """msr_check_pr: checks "problem state"
75 with m
.If(msr
[MSR_PR
]):
76 comb
+= msr
[MSR_EE
].eq(1) # set external interrupt bit
77 comb
+= msr
[MSR_IR
].eq(1) # set instruction relocation bit
78 comb
+= msr
[MSR_DR
].eq(1) # set data relocation bit
81 class TrapMainStage(PipeModBase
):
82 def __init__(self
, pspec
):
83 super().__init
__(pspec
, "main")
84 self
.fields
= DecodeFields(SignalBitRange
, [self
.i
.ctx
.op
.insn
])
85 self
.fields
.create_specs()
87 def trap(self
, m
, return_addr
, trap_addr
):
88 """trap """ # TODO add descriptive docstring
90 nia_o
, srr0_o
, srr1_o
= self
.o
.nia
, self
.o
.srr0
, self
.o
.srr1
93 comb
+= nia_o
.data
.eq(trap_addr
)
94 comb
+= nia_o
.ok
.eq(1)
96 # addr to begin from on return
97 comb
+= srr0_o
.data
.eq(return_addr
)
98 comb
+= srr0_o
.ok
.eq(1)
100 # take a copy of the current MSR in SRR1
101 comb
+= msr_copy(srr1_o
.data
, msr_i
) # old MSR
102 comb
+= srr1_o
.ok
.eq(1)
105 return TrapInputData(self
.pspec
)
108 return TrapOutputData(self
.pspec
)
110 def elaborate(self
, platform
):
115 # convenience variables
116 a_i
, b_i
, cia_i
, msr_i
= self
.i
.a
, self
.i
.b
, self
.i
.cia
, self
.i
.msr
117 o
, msr_o
, nia_o
= self
.o
.o
, self
.o
.msr
, self
.o
.nia
118 srr0_o
, srr1_o
= self
.o
.srr0
, self
.o
.srr1
119 traptype
, trapaddr
= op
.traptype
, op
.trapaddr
121 # take copy of D-Form TO field
122 i_fields
= self
.fields
.FormD
123 to
= Signal(i_fields
.TO
[0:-1].shape())
124 comb
+= to
.eq(i_fields
.TO
[0:-1])
126 # signed/unsigned temporaries for RA and RB
127 a_s
= Signal(signed(64), reset_less
=True)
128 b_s
= Signal(signed(64), reset_less
=True)
130 a
= Signal(64, reset_less
=True)
131 b
= Signal(64, reset_less
=True)
133 # set up A and B comparison (truncate/sign-extend if 32 bit)
134 with m
.If(op
.is_32bit
):
135 comb
+= a_s
.eq(exts(a_i
, 32, 64))
136 comb
+= b_s
.eq(exts(b_i
, 32, 64))
137 comb
+= a
.eq(a_i
[0:32])
138 comb
+= b
.eq(b_i
[0:32])
145 # establish comparison bits
146 lt_s
= Signal(reset_less
=True)
147 gt_s
= Signal(reset_less
=True)
148 lt_u
= Signal(reset_less
=True)
149 gt_u
= Signal(reset_less
=True)
150 equal
= Signal(reset_less
=True)
152 comb
+= lt_s
.eq(a_s
< b_s
)
153 comb
+= gt_s
.eq(a_s
> b_s
)
154 comb
+= lt_u
.eq(a
< b
)
155 comb
+= gt_u
.eq(a
> b
)
156 comb
+= equal
.eq(a
== b
)
158 # They're in reverse bit order because POWER.
159 # Check V3.0B Book 1, Appendix C.6 for chart
160 trap_bits
= Signal(5, reset_less
=True)
161 comb
+= trap_bits
.eq(Cat(gt_u
, lt_u
, equal
, gt_s
, lt_s
))
163 # establish if the trap should go ahead (any tests requested in TO)
164 # or if traptype is set already
165 should_trap
= Signal(reset_less
=True)
166 comb
+= should_trap
.eq((trap_bits
& to
).any() | traptype
.any())
168 # TODO: some #defines for the bits n stuff.
171 with m
.Case(InternalOp
.OP_TRAP
):
172 # trap instructions (tw, twi, td, tdi)
173 with m
.If(should_trap
):
174 # generate trap-type program interrupt
175 self
.trap(trapaddr
<<4, cia_i
)
176 with m
.If(traptype
== 0):
177 # say trap occurred (see 3.0B Book III 7.5.9)
178 comb
+= srr1_o
.data
[PI_TRAP
].eq(1)
179 with m
.If(traptype
& TT_PRIV
):
180 comb
+= srr1_o
.data
[PI_PRIV
].eq(1)
181 with m
.If(traptype
& TT_FP
):
182 comb
+= srr1_o
.data
[PI_FP
].eq(1)
183 with m
.If(traptype
& TT_ADDR
):
184 comb
+= srr1_o
.data
[PI_ADDR
].eq(1)
187 with m
.Case(InternalOp
.OP_MTMSR
):
188 L
= self
.fields
.FormX
.L
[0:-1] # X-Form field L
190 # just update EE and RI
191 comb
+= msr_o
.data
[MSR_EE
].eq(a_i
[MSR_EE
])
192 comb
+= msr_o
.data
[MSR_RI
].eq(a_i
[MSR_RI
])
194 # Architecture says to leave out bits 3 (HV), 51 (ME)
195 # and 63 (LE) (IBM bit numbering)
196 for stt
, end
in [(1,12), (13, 60), (61, 64)]:
197 comb
+= msr_o
.data
[stt
:end
].eq(a_i
[stt
:end
])
198 msr_check_pr(m
, msr_o
.data
)
199 comb
+= msr_o
.ok
.eq(1)
202 with m
.Case(InternalOp
.OP_MFMSR
):
203 # TODO: some of the bits need zeroing? apparently not
204 comb
+= o
.data
.eq(msr_i
)
207 with m
.Case(InternalOp
.OP_RFID
):
208 # XXX f_out.virt_mode <= b_in(MSR_IR) or b_in(MSR_PR);
209 # XXX f_out.priv_mode <= not b_in(MSR_PR);
211 # return addr was in srr0
212 comb
+= nia_o
.data
.eq(br_ext(srr0_i
[2:]))
213 comb
+= nia_o
.ok
.eq(1)
215 comb
+= msr_copy(msr_o
.data
, srr1_i
, zero_me
=False) # don't zero
216 msr_check_pr(m
, msr_o
.data
)
217 comb
+= msr_o
.ok
.eq(1)
219 with m
.Case(InternalOp
.OP_SC
):
220 # TODO: scv must generate illegal instruction. this is
221 # the decoder's job, not ours, here.
223 # jump to the trap address, return at cia+4
224 self
.trap(0xc00, cia_i
+4)
227 #with m.Case(InternalOp.OP_ADDPCIS):
230 comb
+= self
.o
.ctx
.eq(self
.i
.ctx
)