异常与中断 - ustcstaff.ustc.edu.cn/~llxx/cod/courseware/05_interrption1.pdf ·...

48
异常与中断 异常并不意外!——llxx [email protected]

Upload: others

Post on 20-Jun-2020

43 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 异常与中断 - USTCstaff.ustc.edu.cn/~llxx/cod/courseware/05_Interrption1.pdf · MIPS的异常与中断 External events 中断,读总线错,。 Memory translation exceptions

异常与中断

异常并不意外!——llxx

[email protected]

Page 2: 异常与中断 - USTCstaff.ustc.edu.cn/~llxx/cod/courseware/05_Interrption1.pdf · MIPS的异常与中断 External events 中断,读总线错,。 Memory translation exceptions

SimpleMIPSSystem

Page 3: 异常与中断 - USTCstaff.ustc.edu.cn/~llxx/cod/courseware/05_Interrption1.pdf · MIPS的异常与中断 External events 中断,读总线错,。 Memory translation exceptions

程序如何执行的?用到哪些设备?

• C语言计算机?

Page 4: 异常与中断 - USTCstaff.ustc.edu.cn/~llxx/cod/courseware/05_Interrption1.pdf · MIPS的异常与中断 External events 中断,读总线错,。 Memory translation exceptions

内容提要

• 异常与中断的基本概念

• 异常与中断响应的基本过程

–顺序语义

–机构–机构

• 多周期异常处理

• 流水线异常处理

• 示例:MIPS实现

Page 5: 异常与中断 - USTCstaff.ustc.edu.cn/~llxx/cod/courseware/05_Interrption1.pdf · MIPS的异常与中断 External events 中断,读总线错,。 Memory translation exceptions

Terms:exceptions、interrupts、traps

• 改变正常指令执行流(Transfer of Control)的意外事件。

– 暂停当前程序的执行,转而执行其他程序;

– 其他程序执行完成后,恢复被中断程序的执行。

• RISC:MIPS/ARM/RISC-V统称exceptions,含

– 异常Exceptions:内部事件

• 指令异常,如溢出• 指令异常,如溢出

• 陷阱Traps:syscall,断点break,自陷TEQ

– 中断Interrupts:外部事件

• I/O中断,设备故障

• CISC:x86统称interrupt,含

– 外部中断:硬中断(hardware interrupt)

• 可屏蔽中断,不可屏蔽中断NMI

– 内部中断:软中断(software interrupt)

• 程序异常,系统调用 INT n,指令断点(int 3调试)

Page 6: 异常与中断 - USTCstaff.ustc.edu.cn/~llxx/cod/courseware/05_Interrption1.pdf · MIPS的异常与中断 External events 中断,读总线错,。 Memory translation exceptions

MIPS的异常与中断 External events

中断,读总线错,。。。

Memory translation exceptions 缺页,越界,。。。

Other unusual program conditions for the kernel to fix 须内核处理的不常见程序状态

Program or hardware-detected errors Program or hardware-detected errors 非法指令、溢出、对齐等

Data integrity problems(Checksum etc.) 校验错

System Calls and traps

Page 7: 异常与中断 - USTCstaff.ustc.edu.cn/~llxx/cod/courseware/05_Interrption1.pdf · MIPS的异常与中断 External events 中断,读总线错,。 Memory translation exceptions

指令异常响应行为:同步• 可恢复异常:当前指令故障,OS处理后返回当前指令,如图“故障”• 不可恢复异常:当前异常无法修复,交用户处理,如图“异常中止”• 陷阱:由陷阱指令触发调用系统服务,返回下一条指令,如图“陷阱”

7

Page 8: 异常与中断 - USTCstaff.ustc.edu.cn/~llxx/cod/courseware/05_Interrption1.pdf · MIPS的异常与中断 External events 中断,读总线错,。 Memory translation exceptions

不可恢复异常:异常终止

Page 9: 异常与中断 - USTCstaff.ustc.edu.cn/~llxx/cod/courseware/05_Interrption1.pdf · MIPS的异常与中断 External events 中断,读总线错,。 Memory translation exceptions

系统调用:应用程序与OS交互

Page 10: 异常与中断 - USTCstaff.ustc.edu.cn/~llxx/cod/courseware/05_Interrption1.pdf · MIPS的异常与中断 External events 中断,读总线错,。 Memory translation exceptions

SPIM的系统调用:附录B.9

• SPIM:MIPS-32仿真器

–汇编程序调试、执行

–标准设备I/O服务

• SYSCALL Step• SYSCALL Step

– $v0=srv#

– $a0~3=arg

– syscall

– $v0=返回值

Page 11: 异常与中断 - USTCstaff.ustc.edu.cn/~llxx/cod/courseware/05_Interrption1.pdf · MIPS的异常与中断 External events 中断,读总线错,。 Memory translation exceptions

外部中断响应行为:异步

当前程序

中断服务程序

• 中断嵌套

• 中断与过程调用?• 控制转移,传参/通信

• 系统状态与模式

Page 12: 异常与中断 - USTCstaff.ustc.edu.cn/~llxx/cod/courseware/05_Interrption1.pdf · MIPS的异常与中断 External events 中断,读总线错,。 Memory translation exceptions

外部中断源:可屏蔽、不可屏蔽

• 可屏蔽– 时钟,外设请求

• 不可屏蔽– 电源,访存、外设错误

Page 13: 异常与中断 - USTCstaff.ustc.edu.cn/~llxx/cod/courseware/05_Interrption1.pdf · MIPS的异常与中断 External events 中断,读总线错,。 Memory translation exceptions

异常发生与响应时刻

• 同步:立即响应– 异常:可能要返回当前指令

• 停止,重启

– 陷阱:返回下一条指令

• 异步:指令周期结束才响应• 异步:指令周期结束才响应– 中断:返回下一条指令,停止

Page 14: 异常与中断 - USTCstaff.ustc.edu.cn/~llxx/cod/courseware/05_Interrption1.pdf · MIPS的异常与中断 External events 中断,读总线错,。 Memory translation exceptions

异常的5种属性:H&P CA图A.27

Page 15: 异常与中断 - USTCstaff.ustc.edu.cn/~llxx/cod/courseware/05_Interrption1.pdf · MIPS的异常与中断 External events 中断,读总线错,。 Memory translation exceptions

The Basics of Exception Handling• 异常处理

– 检测并记录异常原因

– 记录断点位置:异常指令地址(?)

– 关中断(判优?)

– 建立处理环境:user mode => kernel mode– 建立处理环境:user mode => kernel mode

– 转ESR/ISR入口:OS/user,非向量式/向量式1.保存现场:RF

2.清当前中断;开中断

3.进行异常服务

4.关中断

5.恢复现场:RF

6.返回断点处(?)执行:iret、eret

– 开中断,恢复用户模式,恢复程序工作状态

Page 16: 异常与中断 - USTCstaff.ustc.edu.cn/~llxx/cod/courseware/05_Interrption1.pdf · MIPS的异常与中断 External events 中断,读总线错,。 Memory translation exceptions

Simp

le Interru

pt

Pro

cessing

Simp

le Interru

pt

Pro

cessing

Page 17: 异常与中断 - USTCstaff.ustc.edu.cn/~llxx/cod/courseware/05_Interrption1.pdf · MIPS的异常与中断 External events 中断,读总线错,。 Memory translation exceptions

保存断点与现场 恢复断点与现场

Page 18: 异常与中断 - USTCstaff.ustc.edu.cn/~llxx/cod/courseware/05_Interrption1.pdf · MIPS的异常与中断 External events 中断,读总线错,。 Memory translation exceptions

8086/88中断

Page 19: 异常与中断 - USTCstaff.ustc.edu.cn/~llxx/cod/courseware/05_Interrption1.pdf · MIPS的异常与中断 External events 中断,读总线错,。 Memory translation exceptions

例:时钟中断(也称“软时钟tick”)

• 由可编程定时/计数器产生的INT

–维持系统时间(间隔大约10ms,更新RTC)

–多任务分时共享CPU

Page 20: 异常与中断 - USTCstaff.ustc.edu.cn/~llxx/cod/courseware/05_Interrption1.pdf · MIPS的异常与中断 External events 中断,读总线错,。 Memory translation exceptions

X86架构的ISR示例

Page 21: 异常与中断 - USTCstaff.ustc.edu.cn/~llxx/cod/courseware/05_Interrption1.pdf · MIPS的异常与中断 External events 中断,读总线错,。 Memory translation exceptions

MIPS的异常处理:图2-14

• $26..$27:$k0(cause),$k1(EPC)

Page 22: 异常与中断 - USTCstaff.ustc.edu.cn/~llxx/cod/courseware/05_Interrption1.pdf · MIPS的异常与中断 External events 中断,读总线错,。 Memory translation exceptions

MIPS R3000 & Coprocessors0

Page 23: 异常与中断 - USTCstaff.ustc.edu.cn/~llxx/cod/courseware/05_Interrption1.pdf · MIPS的异常与中断 External events 中断,读总线错,。 Memory translation exceptions

Co-processor(COD4附录B.7)

• Contains registers useful for handling exceptions– Includes the status register, cause register, BadVaddr, and

EPC (Exception Program Counter).

• Not accessible in user mode.

• 读写模式:read-modify-write cycle

January 2014 23

Page 24: 异常与中断 - USTCstaff.ustc.edu.cn/~llxx/cod/courseware/05_Interrption1.pdf · MIPS的异常与中断 External events 中断,读总线错,。 Memory translation exceptions

Important Exceptions registers

Page 25: 异常与中断 - USTCstaff.ustc.edu.cn/~llxx/cod/courseware/05_Interrption1.pdf · MIPS的异常与中断 External events 中断,读总线错,。 Memory translation exceptions

CPU与CP0的接口

• Special Instructions

– mfc0:Move from coprocessor 0

– mtc0:Move to coprocessor 0

– eret:Return from exception (goes to EPC)

• CP0 Hazard:存在延迟槽(一个周期)

mfc0 k0, $cause

nop /* mfc0指令执行速度慢,延迟槽中加一个空操作 */

mov t0, k0 /* 将cause送t0,进行下一步操作 */

• mfc0、mtc0实现?

Page 26: 异常与中断 - USTCstaff.ustc.edu.cn/~llxx/cod/courseware/05_Interrption1.pdf · MIPS的异常与中断 External events 中断,读总线错,。 Memory translation exceptions

MIPS ISR例:读一个键

Page 27: 异常与中断 - USTCstaff.ustc.edu.cn/~llxx/cod/courseware/05_Interrption1.pdf · MIPS的异常与中断 External events 中断,读总线错,。 Memory translation exceptions

多周期总图

IorD

MemRead

MemWrite

MemtoReg

PCWriteCond

PCWrite

IRWrite

ALUOp

ALUSrcB

ALUSrcA

RegDst

PCSource

RegWrite

Control

Outputs

Op [5– 0]

Instruction [31-26]

M u x

0

2

Jump address [31-0]Instruction [25– 0] 26 28

Shift left 2

1

npc

[email protected] 27

Shift left 2

PCM u x

0

1

RegistersWrite register

Write data

Read data 1

Read data 2

Read register 1

Read register 2

Instruction [15– 11]

M u x

0

1

M u x

0

1

4

Instruction [15– 0]

Sign

extend

3216

Instruction [25– 21]

Instruction [20– 16]

Instruction [15– 0]

Instruction

register

ALU

control

ALU result

ALU

Zero

Memory

data

register

A

B

[31-26]

Instruction [5– 0]

PC [31-28]

1 M u x

0

3

2

M u x

0

1ALUOut

Memory

MemData

Write data

Address

beq

Page 28: 异常与中断 - USTCstaff.ustc.edu.cn/~llxx/cod/courseware/05_Interrption1.pdf · MIPS的异常与中断 External events 中断,读总线错,。 Memory translation exceptions

多周期控制器的MooreFSM,

每个状态需要一个时钟周期。

PCWritePCSource = 10

ALUSrcA = 1ALUSrcB = 00ALUOp = 01PCWriteCond

PCSource = 01

ALUSrcA =1ALUSrcB = 00ALUOp= 10

ALUSrcA = 1ALUSrcB = 10ALUOp = 00

ALUSrcA = 0ALUSrcB = 11ALUOp = 00

MemReadALUSrcA = 0

IorD = 0IRWrite

ALUSrcB = 01ALUOp = 00

PCWritePCSource = 00

Instruction fetchInstruction decode/

register fetch

Jumpcompletion

BranchcompletionExecution

Memory addresscomputation (O

p=

'J')

W')

01

9862

Start

[email protected] 28

RegDst = 1RegWrite

MemtoReg = 0

MemWriteIorD = 1

MemReadIorD = 1

RegDst=0RegWrite

MemtoReg=1

Memoryaccess

Memoryaccess R-type completion

Write-back step

(Op

='L

W

4

753

Page 29: 异常与中断 - USTCstaff.ustc.edu.cn/~llxx/cod/courseware/05_Interrption1.pdf · MIPS的异常与中断 External events 中断,读总线错,。 Memory translation exceptions

多周期MIPS的指令异常处理

• 异常处理– 断点保存:将异常指令的地址保存在EPC寄存器中– 异常识别:Cause寄存器

• 非法指令:在ID周期• 算术溢出:在EXE周期

– 异常服务程序:根据cause分别处理– 异常服务程序:根据cause分别处理• 异常处理程序地址:0x800 0180• 停止异常程序的执行并报告错误等

– 非法指令:为用户程序提供某些服务– 溢出:报错

• 返回:ERET (return-from-environment)

• 数据通路、控制信号、状态机?– EPCWrite– CauseWrite– IntCause:0-非法指令,1-溢出

Page 30: 异常与中断 - USTCstaff.ustc.edu.cn/~llxx/cod/courseware/05_Interrption1.pdf · MIPS的异常与中断 External events 中断,读总线错,。 Memory translation exceptions

Exceptions Handling in Multi-Cycle MIPS

[email protected] 30

Page 31: 异常与中断 - USTCstaff.ustc.edu.cn/~llxx/cod/courseware/05_Interrption1.pdf · MIPS的异常与中断 External events 中断,读总线错,。 Memory translation exceptions

异常处理

ALUSrcA = 1ALUSrcB = 00

ALUOp = 01

PCWriteCondPCSource = 01

ALUSrcA = 1

ALUSrcB = 00

ALUOp = 10

ALUSrcA = 1ALUSrcB = 00

ALUOp = 00

ALUSrcA = 0

ALUSrcB = 11

ALUOp = 00

MemReadALUSrcA = 0

IorD = 0IRWrite

ALUSrcB = 01ALUOp = 00

PCWritePCSource = 00

Instruction fetchInstruction decode/

Register fetch

Jump

completion

Branch

completionExecutionMemory address

computation (Op

=' J

' )

W' )

01

9862

Start

PCWrite

PCSource = 10

[email protected] 31

理控制

RegDst = 1

RegWrite

MemtoReg = 0

MemWrite

IorD = 1

MemRead

IorD = 1

RegWrite

MemtoReg = 1RegDst = 0

Memory

access

Memory

access R-type completion

Write-back step

(Op

=' L

W

4

7 11 1053

Overflow

Overflow

ALUSrcA = 0ALUSrcB = 01ALUOp = 01

EPCWritePCWrite

PCSource = 11

IntCause = 0CauseWrite

ALUSrcA = 0

ALUSrcB = 01ALUOp = 01

EPCWrite

PCWrite

PCSource = 11

IntCause = 1

CauseWrite

Page 32: 异常与中断 - USTCstaff.ustc.edu.cn/~llxx/cod/courseware/05_Interrption1.pdf · MIPS的异常与中断 External events 中断,读总线错,。 Memory translation exceptions

Multicycle Interrupts:中断周期

• 保存EPC和CAUSE

Page 33: 异常与中断 - USTCstaff.ustc.edu.cn/~llxx/cod/courseware/05_Interrption1.pdf · MIPS的异常与中断 External events 中断,读总线错,。 Memory translation exceptions

非流水异常和中断操作的顺序语义

• 停止与重启–状态确定:顺序模型

• 指“体系结构可见状态”• 之前的指令都已执行完成

– 已经提交其状态

• 之后的指令还没有发射

33

• 之后的指令还没有发射– 没有改变任何机器状态

–断点精确:= PC/nPC• 指令异常:同步响应

– 返回:当前指令(缺页),下一条指令(syscall)– 不返回:(溢出)

• 中断:异步响应,“不与特定指令相关”?– 返回nPC,或不返回

• 现场清晰:PSW,RF,SP

Page 34: 异常与中断 - USTCstaff.ustc.edu.cn/~llxx/cod/courseware/05_Interrption1.pdf · MIPS的异常与中断 External events 中断,读总线错,。 Memory translation exceptions

流水线的精确异常

• 保证异常发生的顺序与指令的逻辑顺序相同– 之前的指令执行完成– 之后的指令取消– 记录异常流水线段(EPC,cause)

Page 35: 异常与中断 - USTCstaff.ustc.edu.cn/~llxx/cod/courseware/05_Interrption1.pdf · MIPS的异常与中断 External events 中断,读总线错,。 Memory translation exceptions

例:add overflow异常处理

• 将异常视为一种控制相关– 暂停指令流中导致异常的指令

– 执行完之前的所有指令

– flush之后的所有指令

• IF.Flush,ID.Flush,EX.Flush

35

• IF.Flush,ID.Flush,EX.Flush

– 记录异常原因:Cause

– 保存断点:EPC= add指令PC+4• 精确!

– 转异常处理程序:非向量中断(MIPS)

– 异常返回:EPC-4,重新执行add• “溢出”一般放弃,其他(缺页)可能继续

• 遵从“简约原则”– 硬件仅提供最少支持,其他由OS负责(如分析Cause)

Page 36: 异常与中断 - USTCstaff.ustc.edu.cn/~llxx/cod/courseware/05_Interrption1.pdf · MIPS的异常与中断 External events 中断,读总线错,。 Memory translation exceptions

add指令溢出异常的响应

• 第一个周期:检测,记录(EPC、CAUSE),排空,ESR入口写PC• 第二个周期:取ESR的第一条指令

January 2014 36

Page 37: 异常与中断 - USTCstaff.ustc.edu.cn/~llxx/cod/courseware/05_Interrption1.pdf · MIPS的异常与中断 External events 中断,读总线错,。 Memory translation exceptions

add指令overflow:flush IF/ID/EX

OF标志

37

图4-66

Page 38: 异常与中断 - USTCstaff.ustc.edu.cn/~llxx/cod/courseware/05_Interrption1.pdf · MIPS的异常与中断 External events 中断,读总线错,。 Memory translation exceptions

overflow detected

flush IDflush IFdeasserting add

flush EX

COD图4-67,CC6

38

nop

January 2014

Page 39: 异常与中断 - USTCstaff.ustc.edu.cn/~llxx/cod/courseware/05_Interrption1.pdf · MIPS的异常与中断 External events 中断,读总线错,。 Memory translation exceptions

first instruction of exception routine

COD图4-67,CC7,bug?

39

Page 40: 异常与中断 - USTCstaff.ustc.edu.cn/~llxx/cod/courseware/05_Interrption1.pdf · MIPS的异常与中断 External events 中断,读总线错,。 Memory translation exceptions

上例的异常处理的问题

• 只是EX段的溢出异常– IF段:访存异常

– ID段:非法指令

– EX段:溢出– EX段:溢出

– MEM段:访存异常

– WB段:无(写寄存器错?)

• 单指令异常!

• 多指令异常?

• 中断?–嵌套?

Page 41: 异常与中断 - USTCstaff.ustc.edu.cn/~llxx/cod/courseware/05_Interrption1.pdf · MIPS的异常与中断 External events 中断,读总线错,。 Memory translation exceptions

流水线多指令异常与中断处理• 多指令异常三问题:“顺序执行”只是一种抽象,断点和状态难以确定

• 状态不一致:

– 异常指令:在异常响应前,已经改变了系统的部分状态。左:lw设置了0标志

– 后续指令:在产生异常指令完成之前改变了系统的部分状态

– 左:假设lw在MEM段产生异常,此时,其后的ALU指令可能已经设置了zero标志

• 顺序不一致:错序,异常发生顺序与指令执行顺序不相同,谁先处理?断点?

– 右:后一条指令(di为未定义)先异常,前一条指令后异常,应按程序指令顺序处理– 右:后一条指令(di为未定义)先异常,前一条指令后异常,应按程序指令顺序处理

• 控制相关:延迟槽指令异常,转移预测失败可能清空该异常的指令

– 中断异步发生:断点可非精确(“不与特定指令相关”)?程序调试?

Page 42: 异常与中断 - USTCstaff.ustc.edu.cn/~llxx/cod/courseware/05_Interrption1.pdf · MIPS的异常与中断 External events 中断,读总线错,。 Memory translation exceptions

MIPS实现:多个异常,精确异常

• MIPS采用提交点技术实现精确异常,简化了设计– 提交点:M段(可否其他段?)

• 多个异常:先发生的异常并不立即处理,只是被标记• 流水线中最深的指令引起的异常最优先• 中断:在M段检测(符合“异步”语义)

– 断点:EPC = PC or nPC?– = MEM– 中断断点 =当前MEM段指令

• 返回后要重新执行!(中断优先级高于异常)

Page 43: 异常与中断 - USTCstaff.ustc.edu.cn/~llxx/cod/courseware/05_Interrption1.pdf · MIPS的异常与中断 External events 中断,读总线错,。 Memory translation exceptions

Recap: Speculating on Exceptions

• Prediction mechanism– Exceptions are rare, so simply predicting no exceptions is

very accurate!

• Check prediction mechanism– Exceptions detected at end of instruction execution

pipeline, special hardware for various exception typespipeline, special hardware for various exception types

• Recovery mechanism– Only write architectural state at commit point, so can

throw away partially executed instructions after exception

– Launch exception handler after flushing pipeline

• Bypassing allows use of uncommitted instructionresults by following instructions

Page 44: 异常与中断 - USTCstaff.ustc.edu.cn/~llxx/cod/courseware/05_Interrption1.pdf · MIPS的异常与中断 External events 中断,读总线错,。 Memory translation exceptions

Architectural Features for OS• Types of Arch Support

– Manipulating privileged machine state• Protected instructions• Manipulate device registers, TLB entries, etc.

– Generating and handling “events”• Interrupts, exceptions, system calls, etc.• Respond to external events• CPU requires software intervention to handle fault or trap• CPU requires software intervention to handle fault or trap

• Features that directly support the OS include– Protection (kernel/user mode)– Protected instructions– Memory protection– System calls– Interrupts and exceptions– Timer (clock)– I/O control and operation– Synchronization (atomic instructions)

Page 45: 异常与中断 - USTCstaff.ustc.edu.cn/~llxx/cod/courseware/05_Interrption1.pdf · MIPS的异常与中断 External events 中断,读总线错,。 Memory translation exceptions

PH processor:Pont@Univ of Leicester

Page 46: 异常与中断 - USTCstaff.ustc.edu.cn/~llxx/cod/courseware/05_Interrption1.pdf · MIPS的异常与中断 External events 中断,读总线错,。 Memory translation exceptions

仿真器

Page 47: 异常与中断 - USTCstaff.ustc.edu.cn/~llxx/cod/courseware/05_Interrption1.pdf · MIPS的异常与中断 External events 中断,读总线错,。 Memory translation exceptions

小结• 中断周期要完成哪些微操作?

• 多周期状态机中,出现溢出的指令是否将错误结果写回?

• 多周期中状态机中,如何响应中断?

• 指令顺序执行,中断“精确”;指令流水执行,中断“精确”或“非精确”可选

• 流水线是否存在“中断周期”?

• 为何提交点是M段?• 为何提交点是M段?

• EPC和cause应该在哪个段?异常检测电路?

• mips异常返回指令eret如何实现?

• 异常与中断同时发生,优先级?

• (分支)延迟槽中的指令发生异常,EPC = ?

• 比较中断、异常、陷阱、过程调用

– 请求时间、响应时间,断点与现场,返回点,同步异步,中断周期、系统状态,参数传递,控制转移?

• 作业:唐(8.23、8.24)、4.25.1

Page 48: 异常与中断 - USTCstaff.ustc.edu.cn/~llxx/cod/courseware/05_Interrption1.pdf · MIPS的异常与中断 External events 中断,读总线错,。 Memory translation exceptions

[email protected]@ustc.edu.cn 48/41