温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

什么是linux异常体系结构

发布时间:2021-10-22 10:36:58 来源:亿速云 阅读:119 作者:柒染 栏目:互联网科技

这期内容当中小编将会给大家带来有关什么是linux异常体系结构,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

以arm处理器为例, 外部中断和处理器内核异常(soc内部)都属于异常, 异常都是相对于主程序来讲的, 当soc正常执行主程序时, 中断和异常都可以打断它, 依据异常对于主程序所体现出来的"中断"性质可以区分出中断和异常的区别:

异常: 由于soc本身的内核活动产生的, 如当执行主程序时候由于arm soc预取指令/数据而产生异常等, 这个异常来自soc的内核, 所以对于soc内核来说是"同步"的.

中断: 当soc在执行主程序的时候, 各种片上外设和外部中断引脚可以产生一个异常来中断soc当前正在执行的程序, 这个异常信号来自soc的内核以外, 所以对于soc内核来说他是"异步"的

在arm架构的linux系统下, 共使用了5种异常: 1. 未定义指令异常 2. 指令预取中止异常 3. 数据访问中止异常 4. 中断异常 5. swi异常. 

(1) linux中断体系结构

linux内核在(linux)/include/asm-arm/arch-s3c2410/irqs.h 中将所有的中断统一编号, 另外 linux使用一个结构体数组来描述中断, 并且这个数组的下标就是中断号. 每个数组项对应一组中断,

file: (linux)/include/linux/irq.h 
struct irq_desc {
	irq_flow_handler_t	handle_irq;     /* 当前中断的处理函数入口 */
	struct irq_chip		*chip;          /* 底层的硬件访问 */
        ...
	struct irqaction	*action;	/* 用户提供的中断处理函数链表 */
	unsigned int		status;		/* IRQ 状态 */
        ...
	const char		*name;          /* cat /proc/interrupts 显示的中断名称 */
} ____cacheline_internodealigned_in_smp;

其中的 chip 提供了底层的硬件访问:

file: (linux)/include/linux/irq.h
struct irq_chip {
	const char	*name;	                            
	unsigned int	(*startup)(unsigned int irq);    /* 启动中断, 缺省为 "enable" */
	void		(*shutdown)(unsigned int irq);   /* 关闭中断, 缺省为 "disable" */
	void		(*enable)(unsigned int irq);     /* 使能中断, 缺省为 "unmask" */
	void		(*disable)(unsigned int irq);    /* 关闭中断, 缺省为 "mask" */
	void		(*ack)(unsigned int irq);        /* 响应中断, 通常为清除当前中断使得可以接收下一个中断 */
	void		(*mask)(unsigned int irq);       /* 屏蔽中断源 */
	void		(*mask_ack)(unsigned int irq);   /* 屏蔽和响应中断 */
	void		(*unmask)(unsigned int irq);     /* 开启中断源 */
	...
};

另外irqaction结构体为:

file: (linux)/include/linux/interrupt.h 
struct irqaction {
	irq_handler_t handler;    //用户注册的中断处理函数
	unsigned long flags;	  //中断标志
	cpumask_t mask;	          //用于smp
	const char *name;         //用户注册的名字 "cat /proc/interrupts" 可以看到
	void *dev_id;	          //用户传给上面的handler参数,还可以用来区分共享中断
	struct irqaction *next;   //指向下一个irqaction结构
	int irq;                  //中断号
	struct proc_dir_entry *dir;
};

中断执行的流程是: 中断到来时总中断入口函数 asm_do_IRQ() 根据 中断号 找到 irq_desc 结构体数组中的对应项, 并调用 irq_desc 结构体中的 handle_irq() 函数, handle_irq 函数使用 chip 结构体中的函数 清除/屏蔽/重新使能中断, 最后一一调用 action 链表中注册的中断处理函数.

什么是linux异常体系结构

(2) 中断体系结构初始化

在 (linux)/init/main.c 的 start_kernel() 函数中的 trap_init()  / init_IRQ()  来设置异常的处理函数. 

1. trap_init()  和 early_trap_init() 函数 通过下边语句将异常向量复制到 0xffff0000 地址处, 将中断服务函数复制到 0xffff0000 + 0x200地址处

file: (linux)/arch/arm/kernel 
memcpy((void *)vectors, __vectors_start, __vectors_end - __vectors_start);
memcpy((void *)vectors + 0x200, __stubs_start, __stubs_end - __stubs_start);

其中start和end如下:

.globl	__vectors_start
__vectors_start:
	swi	SYS_ERROR0
	b	vector_und + stubs_offset
	ldr	pc, .LCvswi + stubs_offset
	b	vector_pabt + stubs_offset
	b	vector_dabt + stubs_offset
	b	vector_addrexcptn + stubs_offset
	b	vector_irq + stubs_offset
	b	vector_fiq + stubs_offset

	.globl	__vectors_end
__vectors_end:

负责初始化的为 init_IRQ,因其与具体开发板密切相关,所以需要单独列出来.  它用来初始化中断的框架,设置各个中断的默认处理函数. 当发生中断时候进入中断总入口 asm_do_IRQ()  调用init_IRQ()设置的函数

file: (linux)/arch/arm/kernel/irq.c
void __init init_IRQ(void)
{
	int irq;
	for (irq = 0; irq < NR_IRQS; irq++)
		irq_desc[irq].status |= IRQ_NOREQUEST | IRQ_NOPROBE;	
	init_arch_irq();
}

1 for循环中将 irq_desc[] 数组中每一项的状态都设置为 IRQ_NOREQUEST | IRQ_NOPROBE (未请求 | 未探测)

2 init_arch_irq 其实是一个函数指针, 定义如下: 

file: (linux)/arch/arm/kernel/irq.c 
void (*init_arch_irq)(void) __initdata = NULL;

2440移植好的linux系统在启动的时候通过(linux)/arch/arm/kernel/setup.c 的 setup_arch() 获取到 machine_desc 结构体, 然后将 init_arch_irq 这个函数指针初始化为 s3c24xx_init_irq() . 

file: (linux/arch/plat-s3c24xx/irq.c 的 s3c24xx_init_irq)
void __init s3c24xx_init_irq(void)
{
    ...
    /* first, clear all interrupts pending... */
    ...
    /* register the main interrupts */
    ...
    /* setup the cascade irq handlers */
    set_irq_chained_handler(IRQ_EINT4t7, s3c_irq_demux_extint4t7);
    set_irq_chained_handler(IRQ_EINT8t23, s3c_irq_demux_extint8);
    ...
    /* external interrupts */
    //外部中断 0 ~ 3
    for (irqno = IRQ_EINT0; irqno <= IRQ_EINT3; irqno++) {
	irqdbf("registering irq %d (ext int)\n", irqno);
	set_irq_chip(irqno, &s3c_irq_eint0t4);
	set_irq_handler(irqno, handle_edge_irq);
	set_irq_flags(irqno, IRQF_VALID);
    }
    //外部中断4 ~ 23
    for (irqno = IRQ_EINT4; irqno <= IRQ_EINT23; irqno++) {
	irqdbf("registering irq %d (extended s3c irq)\n", irqno);
	set_irq_chip(irqno, &s3c_irqext_chip);
	set_irq_handler(irqno, handle_edge_irq);
	set_irq_flags(irqno, IRQF_VALID);
    }
    /* register the uart interrupts */
    ...
}

1. 清除所有中断的 pending 位

2. 进一步设置 irq_desc[] 中元素的 chip , handle_irq字段.

set_irq_chip(irqno, &s3c_irqext_chip)  结果是: irq_desc[irqno]. chip = &s3c_irqext_chip; 之后就可以使用 irq_desc[irqno].chip 结构体的成员来设置触发方式/使能中断/禁止中断等

set_irq_handler(irqno, handle_edge_irq) 结果是: irq_desc[irqno].handle_irq = handle_edge_irq; 这是该中断号对应的中断函数入口

set_irq_flags(irqno, IRQF_VALID) 结果是: 消除 irqno 对应的IRQ_NOREQUEST标志, 告诉系统该中断被可申请使用了.中断申请的时候会查看该标志如果设置了表示中断尚不可用

执行完 init_IRQ之后 irq_desc[] 中各个数组项的chip, handle_irq字段都被设置好了.

(3) 用户注册中断处理函数流程

驱动程序使用 request_irq 来向内核注册中断处理函数, request_irq 根据中断号找到 irq_desc[] 数组项, 在数组项的 action 链表中添加一个表项.

file: (linux)/kernel/irq/manage.c
int request_irq(unsigned int irq, irq_handle_t handler, unsinged long irqflags, const char *devname, void *dev_id)
{
    struct irqaction *action;             //创建 irqaction 结构体指针   
    ...
    action = kmalloc(sizeof(struct irqaction), GFP_ATOMIC);//填充 irqaction 结构体各个元素
    action->handler = handler;    
    action->flags = irqflags;
    cpus_clear(action->mask);
    action->name = devname;
    action->next = NULL;
    action->dev_id = dev_id;
    
    retval = setup_irq(irq, action);      
    ...
}

setup_irq函数有几个功能: 

1. 将 action 连接入 irq_desc[irq]的action链表中

2. irq_desc[].chip 结构体中没有被 init_irq() 设置的元素设置为默认值

3. 使用传入的 irqflags 作为参数 调用 chip->set_type来设置中断: 设置为外部中断, 设置触发方式(高电平/低电平/上升沿/下降沿)

4. 启动中断, 调用chip->startup 或 chip->enable 使能中断, 

总之, 调用了request_irq之后: irq_desc[]的action结构体已链入action链表, 中断触发方式也设置好了, 中断已被使能. 现在中断已经可以发生并处理了

(4) 中断具体的执行流程

中断的总入口是 asm_do_IRQ()

file: (linux)/arch/arm/kernel/irq.c 
asmlinkage void __exception asm_do_IRQ(unsigned int irq, struct pt_regs *regs){
        ...
	struct irq_desc *desc = irq_desc + irq;
        ...
        desc_handle_irq(irq, desc);
        ...
}

file: (linux)/include/asm-arm/mach/irq.h
static inline void desc_handle_irq(unsigned int irq, struct irq_desc *desc){
	desc->handle_irq(irq, desc);
}

irq 取值范围: IRQ0_EINT0~(IRQ_EINT0+31).  因为2440的INTPND共32bit, 但是irq_desc[]却远不止32个元素, 因为有些个中断是共用同一个位, 如 EINT8~23. 所以当 asm_do_IRQ 的 irq 是对应于"一组"子中断的时候, irq_desc[irq].handle_irq 函数还需要判断出到底是哪个子中断申请的中断, 假设该子中断号为irqno 那么继续调用 irq_decs[irqno].handle_irq 来处理

  以外部中断EINT8~23为例来讲解中断调用的详细过程:

1 当EINT8~23 中任意一个中断触发的时候, INTOFFSET 寄存器的值都是 5.  asm_do_IRQ 的 irq 就是IRQ_EINT0+5 即 IRQ_EINT8t23, 然后调用 irq_desc[IRQ_EINT0t23].handle_irq 来处理

2 irq_desc[IRQ_EINT8t23].handle_irq 设置是在中断初始化的时候完成的: init_IRQ -> init_arch_irq() 该函数指针在linux启动的时候被赋值为 s3c24xx_init_irq ->

file: (linux)/arch/plat-s3c24xx/irq.c
    set_irq_chained_handler(IRQ_EINT4t7, s3c_irq_demux_extint4t7);
    set_irq_chained_handler(IRQ_EINT8t23, s3c_irq_demux_extint8); //irq_desc[IRQ_EINT8t23].handle_irq 设置为 s3c_irq_demux_extint8
    set_irq_chained_handler(IRQ_UART0, s3c_irq_demux_uart0);
    set_irq_chained_handler(IRQ_UART1, s3c_irq_demux_uart1);
    set_irq_chained_handler(IRQ_UART2, s3c_irq_demux_uart2);
    set_irq_chained_handler(IRQ_ADCPARENT, s3c_irq_demux_adc);

接下来看看 s3c_irq_demux_extint8 做了些什么

static void s3c_irq_demux_extint8(unsigned int irq, struct irq_desc *desc){
	unsigned long eintpnd = __raw_readl(S3C24XX_EINTPEND);
	unsigned long eintmsk = __raw_readl(S3C24XX_EINTMASK);

	eintpnd &= ~eintmsk;
	eintpnd &= ~0xff;	/* ignore lower irqs */

	/* we may as well handle all the pending IRQs here */
	while (eintpnd) {
		irq = __ffs(eintpnd);
		eintpnd &= ~(1<<irq);

		irq += (IRQ_EINT4 - 4);
		desc_handle_irq(irq, irq_desc + irq);
	}
}

他首先读取了EINTPEND/EINTMASK寄存器, 查找出具体发生的中断源, 然后重新计算中断号, 最后使用新计算出的这个中断号调用 irq_desc[新中断号].handle_irq

 s3c_irq_demux_extint8 与 handle_edge_irq / handle_level_irq 是什么关系?

s3c_irq_demux_extint8最后调用 irq_desc[新中断号].handle_irq 这个入口函数就是在 s3c24xx_init_irq 中定义的中断入口函数 set_irq_handler(irqno, handle_edge_irq); 中的 handle_edge_irq 

上述就是小编为大家分享的什么是linux异常体系结构了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI