Professional Documents
Culture Documents
193
Kuruvilla Varghese
194
1
Branch Instruction (for … loop) 195
Kuruvilla Varghese
195
Kuruvilla Varghese
196
2
2-bit prediction Scheme 197
Kuruvilla Varghese
197
Kuruvilla Varghese
198
3
Branch Target Buffer (BTB) 199
Outcome
• Branch Target Buffer
1 Branch PC(63:8) Computed Address FSM- Structure shown here
NSL
BTB is of Direct Mapped
Valid Tag Target Address Dir bits Type, It can be Set
Associative or Fully
63 PC 87 0
associative Type also
Branch
History • This scheme can be
Address Table
(BHT)
extended to
accommodate
Global/Local branch
History.
= Decode
PC + 4
Kuruvilla Varghese
199
200
4
Branch Target Buffer .. 201
• The BTB valid bits must be cleared at the start and during a context switch.
Similarly, the 2-bits for outcome must be initialized to default (e.g., weekly not
taken) at the start and during a context switch. The state assignment for weekly
not taken state can be “00” for ease of implementation.
• When branch address is looked up in BTB, if the valid is ‘0’ or the address tag
does not match, the outcome can be predicted to default (e.g., weekly not taken).
• When branch address is looked up in BTB, if the valid is ‘1’ and the address tag
is matched, the outcome is fed to NSL of FSM, this can be registered at NSL to
update the outcome when the branch decision is computed later. As this time
another branch may be looking up the BTB.
Kuruvilla Varghese
201
202
5
Global Branch Correlation 203
Kuruvilla Varghese
203
if (cond1)
…
if (cond2)
…
if (cond1 AND cond2)
…
• If first and second branches are both taken, then third branch is also
taken.
• If first or second branch is not taken, then third branch is also not
taken.
Kuruvilla Varghese
204
6
Global Branch Correlation .. 205
if (aa == 2) ; B1
aa = 0;
if (bb == 2) ; B2
bb = 0;
if (aa != bb) { ; B3
…
}
Kuruvilla Varghese
205
Kuruvilla Varghese
206
7
Global Branch Correlation 207
Outcome
Computed Address FSM-
NSL
BTB
Valid Tag Target Address Dir Bits
Branch
History
63 PC 87 0 Table (BHT)
1 0 1 1
Address
Global Branch
History Register
= Decode
Kuruvilla Varghese
207
Kuruvilla Varghese
208
8
Local Branch Correlation . 209
Kuruvilla Varghese
209
63 PC 87 0
PC
Address
= Decode
PC + 4
Kuruvilla Varghese
210
9
Tournament Branch Predictors 211
Kuruvilla Varghese
211
Kuruvilla Varghese
212
10
Exceptions 213
213
214
11
Interrupts 215
Kuruvilla Varghese
215
Kuruvilla Varghese
216
12
What to do? 217
Kuruvilla Varghese
217
Kuruvilla Varghese
218
13
RISC-V Exceptions 219
Kuruvilla Varghese
219
220
14
RISC-V Exceptions .. 221
Kuruvilla Varghese
221
ol
dmr dmw
zero
CE
Exception Inst rd1# dt1
Address PC Mem FA ad# do
rd2#
ALU
wr# dt2
NOP wr dt
wr dt
Im FB ALU Control
m
rd memreg
rs1
rs2
Forwarding
Unit
222
15
RISC-V Exceptions … 223
• With five instructions active in any clock cycle, the hard part is to associate an
exception with the appropriate instruction. Also, multiple exceptions can occur
simultaneously in a single clock cycle.
• The solution is to prioritize the exceptions so that it is easy to determine which is
serviced first.
• In RISC-V implementations, the hardware sorts exceptions so that the earliest
instruction is interrupted.
• The MEPC register captures the address of the interrupted instructions, and the
MCAUSE register records the highest priority exception in a clock cycle if more
than one exception occurs.
Kuruvilla Varghese
223
224
16
Hardware-Software Interface 225
• The hardware and the operating system must work in conjunction so that
exceptions behave as you would expect. The hardware contract is normally to
stop the offending instruction in midstream, let all prior instructions complete,
flush all following instructions, set a register to show the cause of the exception,
save the address of the offending instruction, and then branch to a prearranged
address.
• The operating system contract is to look at the cause of the exception and act
appropriately. For an undefined instruction or hardware failure, the operating
system normally kills the program and returns an indicator of the reason. For an
I/O device request or an operating system service call, the operating system saves
the state of the program, performs the desired task, and, at some point in the
future, restores the program to continue execution.
Kuruvilla Varghese
225
• The difficulty of always associating the proper exception with the correct
instruction in pipelined computers has led some computer designers to relax this
requirement in noncritical cases.
• Such processors are said to have imprecise interrupts or imprecise exceptions.
E.g., suppose exception has occurred due to an instruction at EX stage, but if the
hardware, register current PC value into SEPC, it is an imprecise exception.
• RISC-V and the vast majority of computers today support precise interrupts or
precise exceptions.
Kuruvilla Varghese
226
17
RISC-V Interrupt Vector Address 227
Kuruvilla Varghese
227
Kuruvilla Varghese
228
18
Core Local Interrupter (CLINT) 229
Kuruvilla Varghese
229
Kuruvilla Varghese
230
19
Global - Platform Local Interrupt Controller (PLIC) 231
Kuruvilla Varghese
231
Kuruvilla Varghese
232
20
Interrupt Control and Status Registers (CSRs) 233
Kuruvilla Varghese
233
• mip: Interrupt pending register for local interrupts when using CLINT
modes of operation. In CLIC modes, this is hardwired to 0 and
pending interrupts are handled using clicintip[i] memory mapped
registers.
• mtvec: Machine Trap Vector register which holds the base address of
the interrupt vector table, as well as the interrupt mode configuration
(direct or vectored) for CLINT and CLIC controllers.
• All synchronous exceptions also use mtvec as the base address for
exception handling in all CLINT and CLIC modes.
Kuruvilla Varghese
234
21
Interrupt Control and Status Registers (CSRs) 235
Kuruvilla Varghese
235
236
22
Timer Interrupts 237
Kuruvilla Varghese
237
Kuruvilla Varghese
238
23
Exit Behaviour for Interrupt Handlers 239
• At the end of the interrupt handler, the mret instruction will do the
following.
– Restore mepc to pc
– Restore mstatus.mpp to Privilege level
– Restore mstatus.mpie to mie
239
240
Thank You
Kuruvilla Varghese
240
24