
Linux内核中,异常处理主要由两个文件完成,entry.S和traps.c,当然还有一些其它异常处理函数分布于fault.c, memory.c等等。entry.S包含异常的入口、进入异常处理C函数前的压栈、退出C函数前的出栈、一些fork函数相关的处理代码(暂不分析)、任务切换汇编处理过程(cpu_switch_to函数,暂不分析)。traps.c主要包含异常处理C函数。
本文主要分析entry.S,对于traps.c作简要介绍。
1.2 执行kernel_entry之前的栈
1.3 执行kernel_entry时的栈
1.4 执行kernel_exit 时的栈
1.5 entry.s代码分析
/*
* Low-level exception handling code
*
* Copyright (C) 2012 ARM Ltd.
* Authors: Catalin Marinas <catalin.marinas@arm.com>
* Will Deacon <will.deacon@arm.com>
*
* This program is free softwareyou can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTYwithout even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <linux/init.h>
#include <linux/linkage.h>
#include <asm/assembler.h>
#include <asm/asm-offsets.h>
#include <asm/errno.h>
#include <asm/thread_info.h>
#include <asm/unistd.h>
#include <asm/unistd32.h>
/*
* Bad Abort numbers
*-----------------
*/
#define BAD_SYNC 0
#define BAD_IRQ 1
#define BAD_FIQ 2
#define BAD_ERROR 3
//根据该结构体内容
/*
struct pt_regs {
union {
struct user_pt_regs user_regs//结构体user_pt_regs和结构体pt_regs内容一样
struct { //共用体存储31个通用寄存器,外加sp,pc,pstate三个特殊寄存器
//该结构体用于异常处理的压栈d栈 *** 作
u64 regs[31]
u64 sp
u64 pc
u64 pstate
}
}
u64 orig_x0
u64 syscallno
}
*/
//S_FRAME_SIZE定义在asm-offsets.c中,DEFINE(S_FRAME_SIZE,sizeof(struct pt_regs))
//即结构体pt_regs的大小,结构体pt_regs的定义见上面
//S_LR定义:DEFINE(S_LR,offsetof(struct pt_regs, regs[30]))
//即31号寄存器在结构体pt_regs中的偏移量
//阅读以下内容请参考图1 和图2
.macro kernel_entry, el, regsize = 64
sub sp, sp, #S_FRAME_SIZE - S_LR // room for LR, SP, SPSR, ELR,见图2中sp'指向的位置
.if \regsize == 32
mov w0, w0 // zero upper 32 bits of x0
.endif
/*
*.macro push, xreg1, xreg2 //压栈两个寄存器
*stp \xreg1, \xreg2, [sp, #-16]! //注意!!!push指令也改变sp的值!!!
*.endm
*/
push x28, x29 //进行压栈 *** 作,push也是一个宏定义,因为ARMv8没有push指令,用stp代替
push x26, x27
push x24, x25
push x22, x23
push x20, x21
push x18, x19
push x16, x17
push x14, x15
push x12, x13
push x10, x11
push x8, x9
push x6, x7
push x4, x5
push x2, x3
push x0, x1 //此时sp指向位置见图2中sp''
.if \el == 0 //如果异常级是el0,把el0的sp栈指针给x21寄存器
mrs x21, sp_el0
.else
add x21, sp, #S_FRAME_SIZE //如果异常级不是el0,把sp指针指向的地方加上pt_regs大小后的地址放入x21,
//即指向没进入kernel_entry函数钱的sp指向的位置,见图2中x21指向的地址
.endif
mrs x22, elr_el1 //把el1的lr寄存器给x22
mrs x23, spsr_el1 //把spsr给x23
stp lr, x21, [sp, #S_LR] //把lr,x21寄存器存入sp+S_LR指向的地方
stp x22, x23, [sp, #S_PC] //把lr,存入sp+s_PC指向的位置,用于异常返回
/*
* Set syscallno to -1 by default (overridden later if real syscall).
*/
.if \el == 0
mvn x21, xzr
str x21, [sp, #S_SYSCALLNO]
.endif
/*
* Registers that may be useful after this macro is invoked:
*
* x21 - aborted SP
* x22 - aborted PC
* x23 - aborted PSTATE
*/
.endm
.macro kernel_exit, el, ret = 0
//把此时sp(即图2中sp'')+S_PC位置处开始的16字节内容分别给x21,x22
//即把栈中存的x21和x22内容取出来
ldp x21, x22, [sp, #S_PC] // load ELR, SPSR
.if \el == 0
ldr x23, [sp, #S_SP] // load return stack pointer,取出
.endif
.if \ret
ldr x1, [sp, #S_X1] // preserve x0 (syscall return),如果ret=1,则保存x0,用于系统调用,暂不分析
add sp, sp, S_X2
.else
pop x0, x1 //如果ret=0,d出x0,x1
.endif
pop x2, x3 // load the rest of the registers
pop x4, x5
pop x6, x7
pop x8, x9
msr elr_el1, x21 // set up the return data,把前面d出的x21,x22分别赋值给elr_el1,spsr_el1
msr spsr_el1, x22
.if \el == 0
msr sp_el0, x23
.endif
pop x10, x11
pop x12, x13
pop x14, x15
pop x16, x17
pop x18, x19
pop x20, x21
pop x22, x23
pop x24, x25
pop x26, x27
pop x28, x29
ldr lr, [sp], #S_FRAME_SIZE - S_LR // load LR and restore SP,把lrd出
eret // return to kernel,异常返回,该指令会把lr给pc,完成跳转
.endm
.macro get_thread_info, rd
mov \rd, sp
and \rd, \rd, #~((1 <<13) - 1) // top of 8K stack
.endm
/*
* These are the registers used in the syscall handler, and allow us to
* have in theory up to 7 arguments to a function - x0 to x6.
*
* x7 is reserved for the system call number in 32-bit mode.
*/
sc_nr .req x25 // number of system calls
scno .req x26 // syscall number
stbl .req x27 // syscall table pointer
tsk .req x28 // current thread_info
/*
* Interrupt handling.
*/
.macro irq_handler
ldr x1, handle_arch_irq
mov x0, sp
blr x1
.endm
.text
/*
* Exception vectors.
*/
.macro ventry label //这里是2^7对齐,即对齐到内存地址的0x80
.align 7
b \label
.endm
.align 11
/* ENTRY也是一个宏,定义在include/linkage.h中
* #ifndef ENTRY
* #define ENTRY(name) \
* .globl name\
* ALIGN\
* name:
* #endif
在ARM Linux内核中使用硬件断点一般的CPU都支持硬件断点,也就是通过处理器提供专门断点寄存器保存一个地址,处理器在执行程序过程,会不断去匹配,可以设置成不同的模式来触发程序中断,如执行到这个地址,读这个地址或写这个地址, 并且可以设置长度,按8位,16位,或32位来触发。和软件断点比,好处是可以支持读写断点,程序断点不需要改写内存,可以设在ROM中,在虚拟地址映射前也可设置等等。
X86, ARMv5架构以上都可以支持硬件断点,如是ARM9上可以支持2个,最新ARMv8规范指定2-8个。
除了Jtag调试器可以支持硬件断点,在Linux内核中,也可以支持硬件断点。
相关的配置 CONFIG_HAVE_HW_BREAKPOIN
下面以3.18 Android内核上的ARM64为例,列举具体步骤:
在3.18上,这个配置没有写入menuconfig,所以首先修改Kconfig
kernel/arch/arm64/Kconfig.debug,加入
config HAVE_HW_BREAKPOINT
bool "Hardware Breakpoint support"
default y
help
If this option is hardware breakpoint
If in doubt, say N.123456123456
2.然后在已编译过的Android根目录环境下:
make -C kernel O=../out/target/product/<chipset>/obj/KERNEL_OBJ ARCH=arm64 CROSS_COMPILE=aarch64-linux-android- KCFLAGS=-mno-android -j8 menuconfig 11
在”Kernel hacking”菜单下可以找到这一选项,同时还要把
Sample kernel code/Build kernel hardware breakpoint examples -- loadable module only11
选上。
因为目前安卓内核都已经把模块签名打开, 为了方便调试,可能暂时关掉:
Main Menu/Enable loadable module support - Require modules to be validly signed11
3.然后重编内核和bootimage,同进把samples module:data_breakpoint.ko也编出来
make -C kernel O=../out/target/product/<chipset>/obj/KERNEL_OBJ ARCH=arm64 CROSS_COMPILE=aarch64-linux-android- KCFLAGS=-mno-android -j8 m=samples11
把生成的bootimage重新烧入手机,并且把data_breakpoint.ko也推送入
adb push /out/target/product/<chipset>/obj/KERNEL_OBJ/samples/hw_breakpoint/data_breakpoint.ko /data/test11
4.接下来就可以使用了,用法是插入data_breakpoint.ko模块,用符号作参数
如:
insmod hw_breakpoint.ko ksym=totalram_pages
totalram_pages变量是内存总的页大小,在cat /proc/meminfo时会读它
这时就会触发硬件断点,内核会打印中整个调用栈。
5.data_breakpoint.ko中,缺省是读写断点,可以在这个sample code的基础上,继续增强,通过不同的参数在使能只读断点或只写断点,包括长度。
代码位于内核中samples/hw_breakpoint/data_breakpoint.c
通过HW_BREAKPOINT_W和HW_BREAKPOINT_R来设置
static int __init hw_break_module_init(void)
{
int ret
struct perf_event_attr attr
hw_breakpoint_init(&attr)
attr.bp_addr = kallsyms_lookup_name(ksym_name)
attr.bp_len = HW_BREAKPOINT_LEN_4
attr.bp_type = HW_BREAKPOINT_W | HW_BREAKPOINT_R
使用Qemu模拟Cortex-A9运行U-boot和Linux 作者来源于网络我的开发环境: Ubuntu-12.04 所有软件包为最新
1. 安装GNU工具链
sudo apt-get insatll gcc-arm-linux-gnueabi
sudo apt-get insatll g++-arm-linux-gnueabi
安装完成后会在 /usr/arm-linux-gnueabi/ 目录下生成库文件、头文件等。 我安装的GCC版本为:
arm-linux-gnueabi-gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
Copyright (C) 2011 Free Software Foundation, Inc.
2. 安装Qemu模拟器
sudo apt-get install qemu qemu-system qemu-utils
这时应该已经可以运行qemu-system-arm命令了, 其版本为:
qemu-system-arm --version
QEMU emulator version 1.0.50 (Debian 1.0.50-2012.03-0ubuntu2), Copyright (c) 2003-2008 Fabrice Bellard
3. 编译和运行U-boot:
到 ftp://ftp.denx.de/pub/u-boot/ 下载最新版本的U-Boot源代码, 我用的目前最新版本 u-boot-2012.04.tar.bz2
解压后进入源代码目录,在Makefile里面增加两行:
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)