Interrupt gates and Trap gates(May 21)

To provide controlled access to code segments with different privilege levels, the processor provides special set of descriptors called gate descriptors. There are four kinds of gate descriptors:

• Call gates

• Trap gates

• Interrupt gates

• Task gates


An interrupt gate or trap gate references an exception- or interrupt-handler procedure that runs in the context of the currently executing task. The segment selector for the gate points to a segment descriptor for an executable code segment in either the GDT or the current LDT. The offset field of the gate descriptor points to the beginning of the exception- or interrupt-handling procedure.


When the processor performs a call to the exception- or interrupt-handler procedure:

• If the handler procedure is going to be executed at a numerically lower privilege level, a stack switch occurs. When the stack switch occurs:

a. The segment selector and stack pointer for the stack to be used by the handler are obtained from the TSS

for the currently executing task. On this new stack, the processor pushes the stack segment selector and

stack pointer of the interrupted procedure.

b. The processor then saves the current state of the EFLAGS, CS, and EIP registers on the new stack 

c. If an exception causes an error code to be saved, it is pushed on the new stack after the EIP value.

• If the handler procedure is going to be executed at the same privilege level as the interrupted procedure:

a. The processor saves the current state of the EFLAGS, CS, and EIP registers on the current stack 

b. If an exception causes an error code to be saved, it is pushed on the current stack after the EIP value.


To return from an exception- or interrupt-handler procedure, the handler must use the IRET (or IRETD) instruction. The IRET instruction is similar to the RET instruction except that it restores the saved flags into the EFLAGS register. The IOPL field of the EFLAGS register is restored only if the CPL is 0. The IF flag is changed only if the CPL is less than or equal to the IOPL. If a stack switch occurred when calling the handler procedure, the IRET instruction switches back to the interrupted procedure’s stack on the return.
TIM8 break interrupt和TIM12 global interrupt是针对不同的定时器的中断。在使用这些中断之前,需要先配置好对应的定时器。 对于TIM8 break interrupt,可以按照以下步骤使用: 1. 配置TIM8定时器,使其工作在所需的模式下。 2. 使能TIM8的中断,并设置中断优先级。 3. 在中断服务函数中进行处理,例如重置计数器或执行其他操作。 示例代码: ```c // 配置TIM8定时器 TIM8->CR1 |= TIM_CR1_ARPE; // 自动重载寄存器使能 TIM8->CR1 |= TIM_CR1_URS; // 仅用于更新事件触发中断 TIM8->DIER |= TIM_DIER_UIE; // 允许更新中断 TIM8->BDTR |= TIM_BDTR_BKE; // 使能break功能 TIM8->BDTR |= TIM_BDTR_BKP; // 使能break事件 TIM8->EGR |= TIM_EGR_UG; // 产生更新事件,初始化定时器 // 使能TIM8中断 NVIC_EnableIRQ(TIM8_BRK_TIM12_IRQn); // 使能TIM8的break中断 NVIC_SetPriority(TIM8_BRK_TIM12_IRQn, 2); // 设置中断优先级 // 中断服务函数 void TIM8_BRK_TIM12_IRQHandler(void) { // 处理中断 TIM8->SR &= ~TIM_SR_UIF; // 清除中断标志 // ... } ``` 对于TIM12 global interrupt,可以按照以下步骤使用: 1. 配置TIM12定时器,使其工作在所需的模式下。 2. 使能TIM12的中断,并设置中断优先级。 3. 在中断服务函数中进行处理,例如重置计数器或执行其他操作。 示例代码: ```c // 配置TIM12定时器 TIM12->CR1 |= TIM_CR1_CEN; // 使能定时器 TIM12->DIER |= TIM_DIER_UIE; // 允许更新中断 TIM12->PSC = 1000; // 设置预分频器 TIM12->ARR = 5000; // 设置自动重载寄存器,产生5000个时钟周期后触发中断 // 使能TIM12中断 NVIC_EnableIRQ(TIM8_BRK_TIM12_IRQn); // 使能TIM12的中断 NVIC_SetPriority(TIM8_BRK_TIM12_IRQn, 2); // 设置中断优先级 // 中断服务函数 void TIM8_BRK_TIM12_IRQHandler(void) { // 处理中断 TIM12->SR &= ~TIM_SR_UIF; // 清除中断标志 // ... } ``` 需要注意的是,不同的定时器可能有不同的中断类型和中断优先级,需要根据实际情况进行配置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值