当前位置:网站首页>龙芯处理器内核中断讲解
龙芯处理器内核中断讲解
2022-06-12 17:08:00 【_kerneler】
/*
*声明:
*
*文章转自:大、猫
*
*链接:https://blog.csdn.net/tongxin1101124/article/details/104993993
*
*转载用法:只用于备份和学习
*/
龙芯处理器内核中断讲解
龙芯处理器内核中断讲解
这里以龙芯处理器ls2k1000为例讲解
1、和中断相关的协处理器
① 原因寄存器(Cause)
IP7-0指出等待的中断。该位将保持不变直到中断撤除。IP0~IP1 是软中断位,IP2-IP6 每位代表一组中断源,IP7 代表一个内部计时器。
ExcCode 是一个 5 位的编码,告诉哪种异常产生了,若有异常产生并且 ExcCode 为 0 时说明是中断异常。
Cause 寄存器的 ExcCode 域
| ExcCode | 助记符 | 描述 |
|---|---|---|
| 0 | INT | 中断 |
| 1 | MOD | TLB 修改例外 |
| 2 | TLBL | TLB 例外(读或者取指令) |
| 3 | TLBS | TLB 例外(存储) |
| 4~7 | … … … | … … … |
| 8 | SYS | 系统调用例外 |
| 9~31 | … … … | … … … |
② 状态寄存器(SR)
BEV 启动时异常向量,当BEV==1时,CPU用ROM空间的异常入口。
IM7-0 中断屏蔽:8位字段,决定哪组中断有请求时可以触发一个异常。例如IM2置 1,cpu才可以接收到该位对应的那组中断源。
IE使能全局中断,只有该位置1时,CPU才可以接受中断异常。若ERL和EXL置1,即使该位置1也会禁止所有中断。
EXL当产生异常时CPU会置1该位,目的是迫使CPU进入内核模式并且禁用中断,这些都是由硬件自动完成的,该位清0需要使用指令eret。
2、硬件工作
产生异常时cpu硬件主动完成的工作:
① 设置 EPC(异常返回地址寄存器),
② 置 SR(EXL)位迫使 CPU 进入内核模式并且禁用中断
③ 设置 Cause 寄存器,使得软件能看到异常的原因
④ CPU 从异常入口点取指令执行
cpu会跳到异常处理入口取指令,内核下异常处理入口设置为0x80000180,并且按照规定这段代码不能超过 128 个字节,以上详情参考*《MIPS 体系结构透视》
3、软件工作
内核启动中会调用trap_init函数
===>start_kernel (init/main.c)
===>trap_init (arch/mips/kernel/traps.c)
===>set_handler
set_except_vector
- 1
- 2
- 3
- 4
trap_init函数中将异常处理程序填入到异常入口点,这个函数位于arch/mips/kernel/traps.c中,在该函数中有以下语句:
set_handler(0x180, &except_vec3_generic, 0x80);
- 1
语句的作用是把函数excpet_vec3_generic拷贝到异常入口点:0x80000180,该函数就是异常处理程序,函数位于 arch/mips/kernel/genex.S中,该函数很短不会超过 128 个字节。
NESTED(except_vec3_generic, 0, sp)
.set push
.set noat
... ... ... ...
mfc0 k1, CP0_CAUSE /*读取原因寄存器*/
andi k1, k1, 0x7c /*取出异常编码*/
... ... ... ...
PTR_L k0, exception_handlers(k1) /*根据异常编码把相应的异常函数的地址放到 K0 寄存器中*/
jr k0 /*根据异常编码处理不同的异常*/
.set pop
END(except_vec3_generic)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
exception_handlers定义在arch/mips/kernel/traps.c,我们把它的定义列出来:
unsigned long exception_handlers[32];
- 1
exception_handlers是具有32个元素的数组,而原因寄存器的ExcCode也是32,exception_handlers[32]每个数组元素对应一种异常,比如exception_handlers[0]对应中断或exception_handlers[8]对用系统调用。当发生异常时需要exception_handlers[32]数组,所以在发生异常前必须初始化这个数组,数组的初始化在函数在trap_init函数中,列出来和初始化exception_handlers[32]数组有关的代码:
set_except_vector(0, handle_int); /*handle_int中断处理函数*/
set_except_vector(1, handle_tlbm);
set_except_vector(2, handle_tlbl);
set_except_vector(3, handle_tlbs);
set_except_vector(4, handle_adel);
set_except_vector(5, handle_ades);
set_except_vector(6, handle_ibe);
set_except_vector(7, handle_dbe);
set_except_vector(8, handle_sys);
set_except_vector(9, handle_bp);
... ... ... ...
当发生中断时候就会根据ExcCode选择handle_int函数执行,函数的定义位在 arch/mips/kernel/genex.S,handle_int函数的关键部分并做出相应的注释:
NESTED(handle_int, PT_SIZE, sp)
... ... ... ...
SAVE_ALL /*该函数的定义在 include/asm/stackframe.h*/
/*由于中断发生的随机型,并且需要从中断中返回,所以当发生中断时,必须把现场环境保存起来,主要包括各种寄存器等等该函数就是这个作用*/
CLI
TRACE_IRQS_OFF
LONG_L s0, TI_REGS($28)
LONG_S sp, TI_REGS($28)
PTR_LA ra, ret_from_irq /*该函数定义在 arch/mips/kernel/entry.S*/
/*把函数地址存到 ra 中,当从函数plat_irq_dispatch 中返回后就会执行 ret_from_irq 中执行,ra寄存器就是存储子程序返回指令*/
PTR_LA v0, plat_irq_dispatch
jr v0
END(handle_int)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
函数plat_irq_dispatch定义在arch/mips/loongson2/irq.c中,函数根据原因寄存器(IP7-0)执行中断处理程序。
asmlinkage void plat_irq_dispatch(void)
{
unsigned int cp0_cause;
unsigned int cp0_status;
unsigned int cp0_cause_saved;
cp0_cause_saved = read_c0_cause() & ST0_IM ;
cp0_status = read_c0_status();
cp0_cause = cp0_cause_saved & cp0_status;
if (cp0_cause & STATUSF_IP7)
ip7_dispatch();
#ifdef CONFIG_SMP
else if (cp0_cause & STATUSF_IP6)
ip6_dispatch();
#endif
else if (cp0_cause & STATUSF_IP5)
ip5_dispatch();
else
ip4_dispatch();
}
中断产生后内核找驱动中定义的中断处理函数流程:
==>handle_int (arch/mips/kernel/genex.S)
==>plat_irq_dispatch (arch/mips/loongson2/irq.c)
==>ip*_dispatch (arch/mips/loongson2/irq.c)
==> do_IRQ (arch/mips/kernel/irq.c)
==>generic_handle_irq (kernel/irq/irqdesc.c)
==>generic_handle_irq_desc (include/linux/irqdesc.h)
==> desc->handle_irq
- 1
- 2
- 3
- 4
- 5
- 6
- 7
desc->handle_irq 由来
==>start_kernel (init/main.c)
==>init_IRQ (arch/mips/kernel/irq.c)
==>arch_init_irq (arch/mips/loongson2/irq.c)
==>setup_irq_default (arch/mips/loongson2/irq.c)
==> irq_set_chip_and_handler (include/linux/irq.h)
==>irq_set_chip_and_handler_name (kernel/irq/chip.c)
==> irq_set_chip
__irq_set_handler
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
__irq_set_handler里定义了desc->handle_irq = handle,而handle为传参数,实际是handle_level_irq函数。
==>handle_level_irq (kernel/irq/chip.c)
==>handle_irq_event (kernel/irq/handle.c)
==>handle_irq_event_percpu
==>__handle_irq_event_percpu
==> action->handler 调用注册的中断处理函数
- 1
- 2
- 3
- 4
- 5
以上大致介绍了当中断异常发生时,内核是怎样精确的找到用request_irq函数注册的中断处理函数。
4、外设中断
① 中断注册
在3.10内核中用于申请中断的函数是request_irq,函数原型在include/linux/interrupt.h中定义:
static inline int __must_check
request_irq(unsigned int irq, irq_handler_t handler, unsigned long flags,
const char *name, void *dev)
{
return request_threaded_irq(irq, handler, NULL, flags, name, dev);
}
- 1
- 2
- 3
- 4
- 5
- 6
irq:是要申请的硬件中断号
handler:是向系统注册的中断处理函数,是一个回调函数,中断发生时,系统调用这个函数,dev_id参数将被传递给它。
irqflags:是中断处理的属性,include/linux/interrupt.h中定义所有中断处理的属性。
devname:设置中断名称,通常是设备驱动程序的名称,在cat /proc/interrupts中可以看到此名称。
dev_id:在中断共享时会用到,一般设置为这个设备的设备结构体或者NULL。
request_irq函数返回0表示成功,返回-INVAL表示中断号无效或处理函数指针为NULL,返回-EBUSY表示中断已经被占用且不能共享。
request_threaded_irq函数原型在kernel/irq/manage.c中定义,该函数中将注册的中断处理函数handler赋值给了action->handler。
龙芯2K1000芯片最多支持64个中断源,以统一方式进行管理,各外设中断号可以在arch/mips/include/asm/mach-loongson2/irq.h查找。
② 中断释放
注销函数定义在kernel/irq/manage.c中定义:
void free_irq(unsigned int irq, void *dev_id)
- 1
返回值:函数运行正常时返回 0 ,否则返回对应错误的负值。
边栏推荐
- The significance of writing technology blog
- (8) Goto keyword
- Download PHP source code of leaf sharing station
- Gerrit triggers Jenkins sonarqube scan
- (八)goto关键字
- Kill program errors in the cradle with spotbugs
- 三代DRI的变化
- ftrace
- [BSP video tutorial] stm32h7 video tutorial Issue 8: the last issue of the MDK theme, the new generation of debugging technologies event recorder and RTT, and using stm32cubemx to generate project tem
- R语言使用epiDisplay包的tabpct函数生成二维列联表并使用马赛克图可视化列联表(二维列联表、边际频数、以及按行、按列的比例)、自定义设置ylab参数设置Y轴的轴标签文本(y axis)
猜你喜欢

Uniapp壁纸小程序源码/双端微信抖音小程序源码

su直接切换到超级管理员模式,这样很多报错都可以避免了

How to view, modify, and delete SSH

Installation and use of rolabelimg

Uniapp wallpaper applet source code / double ended wechat Tiktok applet source code

Atlas conflict Remote Code Execution Vulnerability (cve-2022-26134) vulnerability recurrence

多种Qt的开发方式,你选择哪种?

JVM memory model and local memory

使用GCC的PGO(Profile-guided Optimization)优化整个系统

Atlassian Confluence 远程代码执行漏洞(CVE-2022-26134)漏洞复现
随机推荐
Quick start sweep crawler framework
5-5配置Mysql复制 基于日志点的复制
(六)控制语句if/else switch
R语言使用epiDisplay包的summ函数计算dataframe中指定变量在不同分组变量下的描述性统计汇总信息并可视化有序点图(名称、有效值个数、均值、中位数、标准差、最大值、最小值)
R语言使用ggplot2可视化dataframe数据中特定数据列的密度图(曲线)、并使用xlim参数指定X轴的范围
布局管理中的sizePolicy的策略问题
R language uses the sum function of epidisplay package to calculate the descriptive statistical summary information of the specified variables in dataframe under different grouped variables and visual
A variety of Qt development methods, which do you choose?
MIPS 通用寄存器 + 指令
Su directly switches to super administrator mode, so that many error reports can be avoided
selenium元素定位
qemu+gdb小节
Google browser debugging skills
R语言使用epiDisplay包的tableStack函数基于分组变量生成统计分析表(包含描述性统计分析、假设检验、不同数据使用不同的统计量和假设检验方法)、自定义配置是否显示统计检验内容
Gerrit触发Jenkins SonarQube扫描
(三)Golang - 数据类型
Crazy temporary products: super low price, big scuffle and new hope
How to view, modify, and delete SSH
JVM memory model and local memory
(五)输出和输出



