Add TrapMainStage.trap() convenience function to set trap address and PC
[soc.git] / src / soc / fu / trap / main_stage.py
1 """Trap Pipeline
2
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/
6 """
7
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
14
15 from soc.decoder.power_fields import DecodeFields
16 from soc.decoder.power_fieldsn import SignalBitRange
17
18
19 # Listed in V3.0B Book III Chap 4.2.1
20 # MSR bit numbers
21 MSR_SF = (63 - 0) # Sixty-Four bit mode
22 MSR_HV = (63 - 3) # Hypervisor state
23 MSR_S = (63 - 41) # Secure state
24 MSR_EE = (63 - 48) # External interrupt Enable
25 MSR_PR = (63 - 49) # PRoblem state
26 MSR_FP = (63 - 50) # FP available
27 MSR_ME = (63 - 51) # Machine Check int enable
28 MSR_IR = (63 - 58) # Instruction Relocation
29 MSR_DR = (63 - 59) # Data Relocation
30 MSR_PMM = (63 - 60) # Performance Monitor Mark
31 MSR_RI = (63 - 62) # Recoverable Interrupt
32 MSR_LE = (63 - 63) # Little Endian
33
34
35 def msr_copy(msr_o, msr_i, zero_me=True):
36 """
37 -- ISA says this:
38 -- Defined MSR bits are classified as either full func-
39 -- tion or partial function. Full function MSR bits are
40 -- saved in SRR1 or HSRR1 when an interrupt other
41 -- than a System Call Vectored interrupt occurs and
42 -- restored by rfscv, rfid, or hrfid, while partial func-
43 -- tion MSR bits are not saved or restored.
44 -- Full function MSR bits lie in the range 0:32, 37:41, and
45 -- 48:63, and partial function MSR bits lie in the range
46 -- 33:36 and 42:47. (Note this is IBM bit numbering).
47 msr_out := (others => '0');
48 msr_out(63 downto 31) := msr(63 downto 31);
49 msr_out(26 downto 22) := msr(26 downto 22);
50 msr_out(15 downto 0) := msr(15 downto 0);
51 """
52 l = []
53 if zero_me:
54 l.append(msr_o.eq(0))
55 for stt, end in [(0,16), (22, 27), (31, 64)]:
56 l.append(msr_o[stt:end].eq(msr_i[stt:end]))
57 return l
58
59
60 def msr_check_pr(m, msr_o):
61 """msr_check_pr: checks "problem state"
62 """
63 comb = m.d.comb
64 with m.If(msrdata[MSR_PR]):
65 comb += msr[MSR_EE].eq(1) # set external interrupt bit
66 comb += msr[MSR_IR].eq(1) # set instruction relocation bit
67 comb += msr[MSR_DR].eq(1) # set data relocation bit
68
69
70 class TrapMainStage(PipeModBase):
71 def __init__(self, pspec):
72 super().__init__(pspec, "main")
73 self.fields = DecodeFields(SignalBitRange, [self.i.ctx.op.insn])
74 self.fields.create_specs()
75
76 def trap(self, m, addr, trap_addr):
77 comb = m.d.comb
78 nia_o, srr0_o = self.o.nia, self.o.srr0
79
80 comb += nia_o.data.eq(trap_addr)
81 comb += nia_o.ok.eq(1)
82
83 comb += srr0_o.data.eq(addr) # addr to begin from on return
84 comb += srro_o.ok.eq(1)
85
86
87 def ispec(self):
88 return TrapInputData(self.pspec)
89
90 def ospec(self):
91 return TrapOutputData(self.pspec)
92
93 def elaborate(self, platform):
94 m = Module()
95 comb = m.d.comb
96 op = self.i.ctx.op
97
98 # convenience variables
99 a_i, b_i, cia_i, msr_i = self.i.a, self.i.b, self.i.cia, self.i.msr
100 o, msr_o, nia_o = self.o.o, self.o.msr, self.o.nia
101 srr0_o, srr1_o = self.o.srr0, self.o.srr1
102
103 # take copy of D-Form TO field
104 i_fields = self.fields.FormD
105 to = Signal(i_fields.TO[0:-1].shape())
106 comb += to.eq(i_fields.TO[0:-1])
107
108 # signed/unsigned temporaries for RA and RB
109 a_s = Signal(signed(64), reset_less=True)
110 b_s = Signal(signed(64), reset_less=True)
111
112 a = Signal(64, reset_less=True)
113 b = Signal(64, reset_less=True)
114
115 # set up A and B comparison (truncate/sign-extend if 32 bit)
116 with m.If(op.is_32bit):
117 comb += a_s.eq(exts(a_i, 32, 64))
118 comb += b_s.eq(exts(b_i, 32, 64))
119 comb += a.eq(a_i[0:32])
120 comb += b.eq(b_i[0:32])
121 with m.Else():
122 comb += a_s.eq(a_i)
123 comb += b_s.eq(b_i)
124 comb += a.eq(a_i)
125 comb += b.eq(b_i)
126
127 # establish comparison bits
128 lt_s = Signal(reset_less=True)
129 gt_s = Signal(reset_less=True)
130 lt_u = Signal(reset_less=True)
131 gt_u = Signal(reset_less=True)
132 equal = Signal(reset_less=True)
133
134 comb += lt_s.eq(a_s < b_s)
135 comb += gt_s.eq(a_s > b_s)
136 comb += lt_u.eq(a < b)
137 comb += gt_u.eq(a > b)
138 comb += equal.eq(a == b)
139
140 # They're in reverse bit order because POWER.
141 # Check V3.0B Book 1, Appendix C.6 for chart
142 trap_bits = Signal(5)
143 comb += trap_bits.eq(Cat(gt_u, lt_u, equal, gt_s, lt_s))
144
145 # establish if the trap should go ahead (any tests requested in TO)
146 should_trap = Signal()
147 comb += should_trap.eq((trap_bits & to).any())
148
149 # TODO: some #defines for the bits n stuff.
150 with m.Switch(op):
151 #### trap ####
152 with m.Case(InternalOp.OP_TRAP):
153 # trap instructions (tw, twi, td, tdi)
154 with m.If(should_trap):
155 # generate trap-type program interrupt
156
157 # change the PC to trap address 0x700
158 comb += nia_o.data.eq(0x700) # trap address
159 comb += nia_o.ok.eq(1)
160
161 # take a copy of the current MSR in SRR1
162 comb += msr_copy(srr1_o.data, msr_i) # old MSR
163 # set bit 46 to say trap occurred
164 comb += srr1_o.data[63-46].eq(1) # XXX which bit?
165 comb += srr1_o.ok.eq(1)
166
167 # take a copy of the current PC in SRR0
168 comb += srr0_o.data.eq(cia_i) # old PC
169 comb += srr0_o.ok.eq(1)
170
171 # move to MSR
172 with m.Case(InternalOp.OP_MTMSR):
173 L = self.fields.FormX.L[0:-1] # X-Form field L
174 with m.If(L):
175 # just update EE and RI
176 comb += msr_o.data[MSR_EE].eq(a_i[MSR_EE])
177 comb += msr_o.data[MSR_RI].eq(a_i[MSR_RI])
178 with m.Else():
179 # Architecture says to leave out bits 3 (HV), 51 (ME)
180 # and 63 (LE) (IBM bit numbering)
181 for stt, end in [(1,12), (13, 60), (61, 64)]:
182 comb += msr_o.data[stt:end].eq(a_i[stt:end])
183 msr_check_pr(m, msr_o.data)
184 comb += msr_o.ok.eq(1)
185
186 # move from MSR
187 with m.Case(InternalOp.OP_MFMSR):
188 # TODO: some of the bits need zeroing? apparently not
189 comb += o.data.eq(msr_i)
190 comb += o.ok.eq(1)
191
192 with m.Case(InternalOp.OP_RFID):
193 # XXX f_out.virt_mode <= b_in(MSR_IR) or b_in(MSR_PR);
194 # XXX f_out.priv_mode <= not b_in(MSR_PR);
195
196 # return addr was in srr0
197 comb += nia_o.data.eq(br_ext(srr0_i[2:]))
198 comb += nia_o.ok.eq(1)
199 # MSR was in srr1
200 comb += msr_copy(msr_o.data, srr1_i, zero_me=False) # don't zero
201 msr_check_pr(m, msr_o.data)
202 comb += msr_o.ok.eq(1)
203
204 with m.Case(InternalOp.OP_SC):
205 # TODO: scv must generate illegal instruction. this is
206 # the decoder's job, not ours, here.
207
208 # jump to the trap address
209 comb += nia_o.eq(0xC00) # trap address
210 comb += nia_o.ok.eq(1)
211 # keep a copy of the MSR in SRR1
212 comb += msr_copy(srr1_o.data, msr_i)
213 comb += srr1_o.ok.eq(1)
214 # and store the (next-after-return) PC in SRR0
215 comb += srr0_o.data.eq(cia_i+4) # addr to begin from on return
216 comb += srr0_o.ok.eq(1)
217
218 # TODO (later)
219 #with m.Case(InternalOp.OP_ADDPCIS):
220 # pass
221
222 comb += self.o.ctx.eq(self.i.ctx)
223
224 return m