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
, trap_addr
, return_addr
):
88 """trap """ # TODO add descriptive docstring
91 nia_o
, srr0_o
, srr1_o
= self
.o
.nia
, self
.o
.srr0
, self
.o
.srr1
94 comb
+= nia_o
.data
.eq(trap_addr
)
95 comb
+= nia_o
.ok
.eq(1)
97 # addr to begin from on return
98 comb
+= srr0_o
.data
.eq(return_addr
)
99 comb
+= srr0_o
.ok
.eq(1)
101 # take a copy of the current MSR in SRR1
102 comb
+= msr_copy(srr1_o
.data
, msr_i
) # old MSR
103 comb
+= srr1_o
.ok
.eq(1)
106 return TrapInputData(self
.pspec
)
109 return TrapOutputData(self
.pspec
)
111 def elaborate(self
, platform
):
116 # convenience variables
117 a_i
, b_i
, cia_i
, msr_i
= self
.i
.a
, self
.i
.b
, self
.i
.cia
, self
.i
.msr
118 srr0_i
, srr1_i
= self
.i
.srr0
, self
.i
.srr1
119 o
, msr_o
, nia_o
= self
.o
.o
, self
.o
.msr
, self
.o
.nia
120 srr0_o
, srr1_o
= self
.o
.srr0
, self
.o
.srr1
121 traptype
, trapaddr
= op
.traptype
, op
.trapaddr
123 # take copy of D-Form TO field
124 i_fields
= self
.fields
.FormD
125 to
= Signal(i_fields
.TO
[0:-1].shape())
126 comb
+= to
.eq(i_fields
.TO
[0:-1])
128 # signed/unsigned temporaries for RA and RB
129 a_s
= Signal(signed(64), reset_less
=True)
130 b_s
= Signal(signed(64), reset_less
=True)
132 a
= Signal(64, reset_less
=True)
133 b
= Signal(64, reset_less
=True)
135 # set up A and B comparison (truncate/sign-extend if 32 bit)
136 with m
.If(op
.is_32bit
):
137 comb
+= a_s
.eq(exts(a_i
, 32, 64))
138 comb
+= b_s
.eq(exts(b_i
, 32, 64))
139 comb
+= a
.eq(a_i
[0:32])
140 comb
+= b
.eq(b_i
[0:32])
147 # establish comparison bits
148 lt_s
= Signal(reset_less
=True)
149 gt_s
= Signal(reset_less
=True)
150 lt_u
= Signal(reset_less
=True)
151 gt_u
= Signal(reset_less
=True)
152 equal
= Signal(reset_less
=True)
154 comb
+= lt_s
.eq(a_s
< b_s
)
155 comb
+= gt_s
.eq(a_s
> b_s
)
156 comb
+= lt_u
.eq(a
< b
)
157 comb
+= gt_u
.eq(a
> b
)
158 comb
+= equal
.eq(a
== b
)
160 # They're in reverse bit order because POWER.
161 # Check V3.0B Book 1, Appendix C.6 for chart
162 trap_bits
= Signal(5, reset_less
=True)
163 comb
+= trap_bits
.eq(Cat(gt_u
, lt_u
, equal
, gt_s
, lt_s
))
165 # establish if the trap should go ahead (any tests requested in TO)
166 # or if traptype is set already
167 should_trap
= Signal(reset_less
=True)
168 comb
+= should_trap
.eq((trap_bits
& to
).any() | traptype
.any())
170 # TODO: some #defines for the bits n stuff.
171 with m
.Switch(op
.insn_type
):
173 with m
.Case(InternalOp
.OP_TRAP
):
174 # trap instructions (tw, twi, td, tdi)
175 with m
.If(should_trap
):
176 # generate trap-type program interrupt
177 self
.trap(m
, trapaddr
<<4, cia_i
)
178 with m
.If(traptype
== 0):
179 # say trap occurred (see 3.0B Book III 7.5.9)
180 comb
+= srr1_o
.data
[PI_TRAP
].eq(1)
181 with m
.If(traptype
& TT_PRIV
):
182 comb
+= srr1_o
.data
[PI_PRIV
].eq(1)
183 with m
.If(traptype
& TT_FP
):
184 comb
+= srr1_o
.data
[PI_FP
].eq(1)
185 with m
.If(traptype
& TT_ADDR
):
186 comb
+= srr1_o
.data
[PI_ADR
].eq(1)
189 with m
.Case(InternalOp
.OP_MTMSRD
):
190 L
= self
.fields
.FormX
.L
[0:-1] # X-Form field L
192 # just update EE and RI
193 comb
+= msr_o
.data
[MSR_EE
].eq(a_i
[MSR_EE
])
194 comb
+= msr_o
.data
[MSR_RI
].eq(a_i
[MSR_RI
])
196 # Architecture says to leave out bits 3 (HV), 51 (ME)
197 # and 63 (LE) (IBM bit numbering)
198 for stt
, end
in [(1,12), (13, 60), (61, 64)]:
199 comb
+= msr_o
.data
[stt
:end
].eq(a_i
[stt
:end
])
200 msr_check_pr(m
, msr_o
.data
)
201 comb
+= msr_o
.ok
.eq(1)
204 with m
.Case(InternalOp
.OP_MFMSR
):
205 # TODO: some of the bits need zeroing? apparently not
206 comb
+= o
.data
.eq(msr_i
)
209 with m
.Case(InternalOp
.OP_RFID
):
210 # XXX f_out.virt_mode <= b_in(MSR_IR) or b_in(MSR_PR);
211 # XXX f_out.priv_mode <= not b_in(MSR_PR);
213 # return addr was in srr0
214 comb
+= nia_o
.data
.eq(br_ext(srr0_i
[2:]))
215 comb
+= nia_o
.ok
.eq(1)
217 comb
+= msr_copy(msr_o
.data
, srr1_i
, zero_me
=False) # don't zero
218 msr_check_pr(m
, msr_o
.data
)
219 comb
+= msr_o
.ok
.eq(1)
221 # TODO (later) - add OP_SC
222 #with m.Case(InternalOp.OP_SC):
223 # # TODO: scv must generate illegal instruction. this is
224 # # the decoder's job, not ours, here.
226 # # jump to the trap address, return at cia+4
227 # self.trap(m, 0xc00, cia_i+4)
230 #with m.Case(InternalOp.OP_ADDPCIS):
233 comb
+= self
.o
.ctx
.eq(self
.i
.ctx
)