ARM架构Linux内核的异常处理体系结构,可以由arch/arm/kernel/entry-armv.S中的异常向量反应出来,- .equ stubs_offset, __vectors_start + 0x200 - __stubs_start
-
-
.globl __vectors_start
-
__vectors_start:
-
ARM( swi SYS_ERROR0 )
-
THUMB( svc #0 )
-
THUMB( nop )
-
W(b) vector_und + stubs_offset
-
W(ldr) pc, .LCvswi + stubs_offset
-
W(b) vector_pabt + stubs_offset
-
W(b) vector_dabt + stubs_offset
-
W(b) vector_addrexcptn + stubs_offset
-
W(b) vector_irq + stubs_offset
-
W(b) vector_fiq + stubs_offset
-
-
.globl __vectors_end
-
__vectors_end:
对于我来说理解这段代码有难度:
(1)找不到标号vector_xxxx
在网上找了.macro vector_stub, name, mode, correction=0 的相关解释。在arch/arm/kernel/entry-armv.S有宏定义- .macro vector_stub, name, mode, correction=0
-
.align 5
-
-
vector_\name:
-
.if \correction
-
sub lr, lr, #\correction
-
.endif
-
-
@
-
@ Save r0, lr_ (parent PC) and spsr_
-
@ (parent CPSR)
-
@
-
stmia sp, {r0, lr} @ save r0, lr
-
mrs lr, spsr
-
str lr, [sp, #8] @ save spsr
-
-
@
-
@ Prepare for SVC32 mode. IRQs remain disabled.
-
@
-
mrs r0, cpsr
-
eor r0, r0, #(\mode ^ SVC_MODE | PSR_ISETSTATE)
-
msr spsr_cxsf, r0
-
-
@
-
@ the branch table must immediately follow this code
-
@
-
and lr, lr, #0x0f
-
THUMB( adr r0, 1f )
-
THUMB( ldr lr, [r0, lr, lsl #2] )
-
mov r0, sp
-
ARM( ldr lr, [pc, lr, lsl #2] )
-
movs pc, lr @ branch to handler in SVC mode
-
ENDPROC(vector_\name)
-
-
.align 2
-
@ handler addresses follow this label
-
1:
-
.endm
原来vector_xxxx就在arch/arm/kernel/entry-armv.S中。
(2)stubs_offset的理解
vector为异常向量基址,unsigned long vectors = CONFIG_VECTORS_BASE。
在arch/arm/kernel/entry-armv.S中,stubs_offset定义为:
.equ stubs_offset, __vectors_start + 0x200 - __stubs_start????????
在中找到如下解释:
“当汇编器看到B指令后会把要跳转的标签转化为相对于当前PC的偏移量(±32M)写入指令码。从上面的代码可以看到中断向量表和stubs都发生了代码搬
移,所以如果中断向量表中仍然写成b
vector_irq,那么实际执行的时候就无法跳转到搬移后的vector_irq处,因为指令码里写的是原来的偏移量,所以需要把指令码中的偏移量写
成搬移后的。
我们把搬移前的中断向量表中的irq入口地址记
irq_PC,它在中断向量表的偏移量就是irq_PC-vectors_start,
vector_irq在stubs中的偏移量是vector_irq-stubs_start,这两个偏移量在搬移前后是不变的。
搬移后
vectors_start在0xffff0000处,而stubs_start在0xffff0200处,所以搬移后的vector_irq相对于中断
向量中的中断入口地址的偏移量就是,200+vector_irq。在stubs中的偏移量再减去中断入口在向量表中的偏移量,即
200+
vector_irq-stubs_start-irq_PC+vectors_start
= (vector_irq-irq_PC) +
vectors_start+200-stubs_start,
对于括号内的值实际上就是中断向量表中写的vector_irq,减去irq_PC是由汇
编器完成的,而后面的
vectors_start+200-stubs_start就应该是stubs_offset,实际上在entry-armv.S中也是这样定义的。”
汇编伪指令中equ的使用格式是:name EQU expr{,type}
memcpy的函数原型为:void *memcpy(void *dest, const void *src, size_t n);从下面代码的3个memcpy()函数可以看出,vectors~vectors + 0x200存储的异常向量,B vector_,中断向量;vectors + 0x200~vectors + 0x1000存储的具体异常的处理分支,而这个分支视乎只是地址,vector_,跳转表。
arch/arm/kernel/entry-armv.S中的标号,那程序是怎么实现跳转的呢?????
参考http://blog.csdn.net/xavierxiao/article/details/6088050
- void __init early_trap_init(void)
-
{
-
unsigned long vectors = CONFIG_VECTORS_BASE;
-
extern char __stubs_start[], __stubs_end[];
-
extern char __vectors_start[], __vectors_end[];
-
extern char __kuser_helper_start[], __kuser_helper_end[];
-
int kuser_sz = __kuser_helper_end - __kuser_helper_start;
-
-
/*
-
* Copy the vectors, stubs and kuser helpers (in entry-armv.S)
-
* into the vector page, mapped at 0xffff0000, and ensure these
-
* are visible to the instruction stream.
-
*/
-
memcpy((void *)vectors, __vectors_start, __vectors_end - __vectors_start);
-
memcpy((void *)vectors + 0x200, __stubs_start, __stubs_end - __stubs_start);
-
memcpy((void *)vectors + 0x1000 - kuser_sz, __kuser_helper_start, kuser_sz);
-
-
/*
-
* Do processor specific fixups for the kuser helpers
-
*/
-
kuser_get_tls_init(vectors);
-
-
/*
-
* Copy signal return handlers into the vector page, and
-
* set sigreturn to be a pointer to these.
-
*/
-
memcpy((void *)KERN_SIGRETURN_CODE, sigreturn_codes,
-
sizeof(sigreturn_codes));
-
memcpy((void *)KERN_RESTART_CODE, syscall_restart_code,
-
sizeof(syscall_restart_code));
-
-
flush_icache_range(vectors, vectors + PAGE_SIZE);
-
modify_domain(DOMAIN_USER, DOMAIN_CLIENT);
-
}
疑问先放一下,以后再理解。刚才讲述了异常总的入口,及其相关的操作,接下来讲述中断,,,
中断的处理流程如下:
1)发生中断时,CPU执行异常向量vector_irq的代码。
2)在vector_irq里面,最终会调用中断处理的总入口函数asm_do_IRQ。
3)asm_do_IRQ根据中断号调用irq_desc数组项中的handle_irq。
4)handle_irq会使用chip成员中的函数来设置硬件,比如清楚中断,禁止中断,重新使能中断等。
5)handle_irq逐个调用用户在action链表中注册的处理函数。
1)irq_desc结构
在include/linux/irq.h中。
注释很清楚,并且可以知道desc就是descriptor的缩写,方便记忆。- struct timer_rand_state;
-
struct irq_2_iommu;
-
/**
-
* struct irq_desc - interrupt descriptor
-
* @irq: interrupt number for this descriptor
-
* @timer_rand_state: pointer to timer rand state struct
-
* @kstat_irqs: irq stats per cpu
-
* @irq_2_iommu: iommu with this irq
-
* @handle_irq: highlevel irq-events handler [if NULL, __do_IRQ()]
-
* @chip: low level interrupt hardware access
-
* @msi_desc: MSI descriptor
-
* @handler_data: per-IRQ data for the irq_chip methods
-
* @chip_data: platform-specific per-chip private data for the chip
-
* methods, to allow shared chip implementations
-
* @action: the irq action chain
-
* @status: status information
-
* @depth: disable-depth, for nested irq_disable() calls
-
* @wake_depth: enable depth, for multiple set_irq_wake() callers
-
* @irq_count: stats field to detect stalled irqs
-
* @last_unhandled: aging timer for unhandled count
-
* @irqs_unhandled: stats field for spurious unhandled interrupts
-
* @lock: locking for SMP
-
* @affinity: IRQ affinity on SMP
-
* @node: node index useful for balancing
-
* @pending_mask: pending rebalanced interrupts
-
* @threads_active: number of irqaction threads currently running
-
* @wait_for_threads: wait queue for sync_irq to wait for threaded handlers
-
* @dir: /proc/irq/ procfs entry
-
* @name: flow handler name for /proc/interrupts output
-
*/
-
struct irq_desc {
-
unsigned int irq;
-
struct timer_rand_state *timer_rand_state;
-
unsigned int *kstat_irqs;
-
#ifdef CONFIG_INTR_REMAP
-
struct irq_2_iommu *irq_2_iommu;
-
#endif
-
irq_flow_handler_t handle_irq;
-
struct irq_chip *chip;
-
struct msi_desc *msi_desc;
-
void *handler_data;
-
void *chip_data;
-
struct irqaction *action; /* IRQ action list */
-
unsigned int status; /* IRQ status */
-
-
unsigned int depth; /* nested irq disables */
-
unsigned int wake_depth; /* nested wake enables */
-
unsigned int irq_count; /* For detecting broken IRQs */
-
unsigned long last_unhandled; /* Aging timer for unhandled count */
-
unsigned int irqs_unhandled;
-
raw_spinlock_t lock;
-
#ifdef CONFIG_SMP
-
cpumask_var_t affinity;
-
const struct cpumask *affinity_hint;
-
unsigned int node;
-
#ifdef CONFIG_GENERIC_PENDING_IRQ
-
cpumask_var_t pending_mask;
-
#endif
-
#endif
-
atomic_t threads_active;
-
wait_queue_head_t wait_for_threads;
-
#ifdef CONFIG_PROC_FS
-
struct proc_dir_entry *dir;
-
#endif
-
const char *name;
-
} ____cacheline_internodealigned_in_smp;
eg.
buttons.c
- #include <linux/module.h>
-
#include <linux/kernel.h>
-
#include <linux/fs.h>
-
#include <linux/init.h>
-
#include <linux/delay.h>
-
#include <asm/irq.h>
-
#include <linux/interrupt.h>
-
#include <asm/uaccess.h>
-
#include <asm/arch/regs-gpio.h>
-
#include <asm/hardware.h>
-
-
#define DEVICE_NAME "buttons" /* 加载模式后,执行”cat /proc/devices”命令看到的设备名称 */
-
#define BUTTON_MAJOR 232 /* 主设备号 */
-
-
struct button_irq_desc {
-
int irq;
-
unsigned long flags;
-
char *name;
-
};
-
-
/* 用来指定按键所用的外部中断引脚及中断触发方式, 名字 */
-
static struct button_irq_desc button_irqs [] = {
-
{IRQ_EINT19, IRQF_TRIGGER_FALLING, "KEY1"}, /* K1 */
-
{IRQ_EINT11, IRQF_TRIGGER_FALLING, "KEY2"}, /* K2 */
-
{IRQ_EINT2, IRQF_TRIGGER_FALLING, "KEY3"}, /* K3 */
-
{IRQ_EINT0, IRQF_TRIGGER_FALLING, "KEY4"}, /* K4 */
-
};
-
-
/* 按键被按下的次数(准确地说,是发生中断的次数) */
-
static volatile int press_cnt [] = {0, 0, 0, 0};
-
-
/* 等待队列:
-
* 当没有按键被按下时,如果有进程调用s3c24xx_buttons_read函数,
-
* 它将休眠
-
*/
-
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
-
-
/* 中断事件标志, 中断服务程序将它置1,s3c24xx_buttons_read将它清0 */
-
static volatile int ev_press = 0;
-
-
-
static irqreturn_t buttons_interrupt(int irq, void *dev_id)
-
{
-
volatile int *press_cnt = (volatile int *)dev_id;
-
-
*press_cnt = *press_cnt + 1; /* 按键计数加1 */
-
ev_press = 1; /* 表示中断发生了 */
-
wake_up_interruptible(&button_waitq); /* 唤醒休眠的进程 */
-
-
return IRQ_RETVAL(IRQ_HANDLED);
-
}
-
-
-
/* 应用程序对设备文件/dev/buttons执行open(...)时,
-
* 就会调用s3c24xx_buttons_open函数
-
*/
-
static int s3c24xx_buttons_open(struct inode *inode, struct file *file)
-
{
-
int i;
-
int err;
-
-
for (i = 0; i < sizeof(button_irqs)/sizeof(button_irqs[0]); i++) {
-
// 注册中断处理函数
-
err = request_irq(button_irqs[i].irq, buttons_interrupt, button_irqs[i].flags,
-
button_irqs[i].name, (void *)&press_cnt[i]);
-
if (err)
-
break;
-
}
-
-
if (err) {
-
// 释放已经注册的中断
-
i--;
-
for (; i >= 0; i--)
-
free_irq(button_irqs[i].irq, (void *)&press_cnt[i]);
-
return -EBUSY;
-
}
-
-
return 0;
-
}
-
-
-
/* 应用程序对设备文件/dev/buttons执行close(...)时,
-
* 就会调用s3c24xx_buttons_close函数
-
*/
-
static int s3c24xx_buttons_close(struct inode *inode, struct file *file)
-
{
-
int i;
-
-
for (i = 0; i < sizeof(button_irqs)/sizeof(button_irqs[0]); i++) {
-
// 释放已经注册的中断
-
free_irq(button_irqs[i].irq, (void *)&press_cnt[i]);
-
}
-
-
return 0;
-
}
-
-
-
/* 应用程序对设备文件/dev/buttons执行read(...)时,
-
* 就会调用s3c24xx_buttons_read函数
-
*/
-
static int s3c24xx_buttons_read(struct file *filp, char __user *buff,
-
size_t count, loff_t *offp)
-
{
-
unsigned long err;
-
-
/* 如果ev_press等于0,休眠 */
-
wait_event_interruptible(button_waitq, ev_press);
-
-
/* 执行到这里时,ev_press等于1,将它清0 */
-
ev_press = 0;
-
-
/* 将按键状态复制给用户,并清0 */
-
err = copy_to_user(buff, (const void *)press_cnt, min(sizeof(press_cnt), count));
-
memset((void *)press_cnt, 0, sizeof(press_cnt));
-
-
return err ? -EFAULT : 0;
-
}
-
-
/* 这个结构是字符设备驱动程序的核心
-
* 当应用程序操作设备文件时所调用的open、read、write等函数,
-
* 最终会调用这个结构中的对应函数
-
*/
-
static struct file_operations s3c24xx_buttons_fops = {
-
.owner = THIS_MODULE, /* 这是一个宏,指向编译模块时自动创建的__this_module变量 */
-
.open = s3c24xx_buttons_open,
-
.release = s3c24xx_buttons_close,
-
.read = s3c24xx_buttons_read,
-
};
-
-
/*
-
* 执行“insmod s3c24xx_buttons.ko”命令时就会调用这个函数
-
*/
-
static int __init s3c24xx_buttons_init(void)
-
{
-
int ret;
-
-
/* 注册字符设备驱动程序
-
* 参数为主设备号、设备名字、file_operations结构;
-
* 这样,主设备号就和具体的file_operations结构联系起来了,
-
* 操作主设备为BUTTON_MAJOR的设备文件时,就会调用s3c24xx_buttons_fops中的相关成员函数
-
* BUTTON_MAJOR可以设为0,表示由内核自动分配主设备号
-
*/
-
ret = register_chrdev(BUTTON_MAJOR, DEVICE_NAME, &s3c24xx_buttons_fops);
-
if (ret < 0) {
-
printk(DEVICE_NAME " can't register major number\n");
-
return ret;
-
}
-
-
printk(DEVICE_NAME " initialized\n");
-
return 0;
-
}
-
-
/*
-
* 执行”rmmod s3c24xx_buttons.ko”命令时就会调用这个函数
-
*/
-
static void __exit s3c24xx_buttons_exit(void)
-
{
-
/* 卸载驱动程序 */
-
unregister_chrdev(BUTTON_MAJOR, DEVICE_NAME);
-
}
-
-
/* 这两行指定驱动程序的初始化函数和卸载函数 */
-
module_init(s3c24xx_buttons_init);
-
module_exit(s3c24xx_buttons_exit);
-
-
/* 描述驱动程序的一些信息,不是必须的 */
-
MODULE_AUTHOR(""); // 驱动程序的作者
-
MODULE_DESCRIPTION("S3C2410/S3C2440 BUTTON Driver"); // 一些描述信息
-
MODULE_LICENSE("GPL"); // 遵循的协议
buttons_test.c- #include <stdio.h>
-
#include <stdlib.h>
-
#include <unistd.h>
-
#include <sys/ioctl.h>
-
-
int main(int argc, char **argv)
-
{
-
int i;
-
int ret;
-
int fd;
-
int press_cnt[4];
-
-
fd = open("/dev/buttons", 0); // 打开设备
-
if (fd < 0) {
-
printf("Can't open /dev/buttons\n");
-
return -1;
-
}
-
-
// 这是个无限循环,进程有可能在read函数中休眠,当有按键被按下时,它才返回
-
while (1) {
-
// 读出按键被按下的次数
-
ret = read(fd, press_cnt, sizeof(press_cnt));
-
if (ret < 0) {
-
printf("read err!\n");
-
continue;
-
}
-
-
for (i = 0; i < sizeof(press_cnt)/sizeof(press_cnt[0]); i++) {
-
// 如果被按下的次数不为0,打印出来
-
if (press_cnt[i])
-
printf("K%d has been pressed %d times!\n", i+1, press_cnt[i]);
-
}
-
}
-
-
close(fd);
-
return 0;
-
}
对比前面没有中断的LEDS驱动程序,可以更好的掌握~参考博客:
http://www.cnblogs.com/hoys/archive/2011/04/13/2015318.html
阅读(1859) | 评论(0) | 转发(0) |