硬件上的中断:一根线
实际的处理器并不是 “无情地执行指令”
“响应”
IDT[n]
指定的地址,并设置处理器状态 (例如关闭中断)PC = (mtvec & ~0xf)
mcause.Interrupt = 1
“更多的处理器内部状态”
struct MiniRV32IMAState {
uint32_t regs[32], pc;
uint32_t mstatus;
uint32_t cyclel, cycleh;
uint32_t timerl, timerh, timermatchl, timermatchh;
uint32_t mscratch, mtvec, mie, mip;
uint32_t mepc, mtval, mcause;
// Note: only a few bits are used. (Machine = 3, User = 0)
// Bits 0..1 = privilege.
// Bit 2 = WFI (Wait for interrupt)
// Bit 3+ = Load/Store reservation LSBs.
uint32_t extraflags;
};
操作系统内核 (代码)
应用程序
FL_IF
)#GP(0)
If CPL is greater than IOPL and less than 3asm volatile ("cli");
隔离出一块 “处理事件” 的代码执行时空
bool cte_init(Context *(*handler)(Event ev, Context *ctx));
bool ienabled(void);
void iset(bool enable);
中断引入了并发
状态
状态迁移
假设 race-freedom
实现原子性:
printf("Hello\n");
这件事没有大家想的简单