共2条
1/1 1 跳转至页
,VXWORKS,NUCLEUS,keil,for,arm,01a,ccd,13581980230, 告别在21ic只问问题的日子,原创swi软件中断理解分析
问
Swi软件中断理解
什么是软件中断不说了,自己看书去,直接切入主题
以sh.c中的G_action()为例
其调用swi_run(0x200,address),通过platform.h中的
__swi(SemiSWI) void swi_run(int op, int addr)调用,传递的参数0x200和address在软件中断服务程序中会使用,R0会被传入0x200,R1传入address,SemiSWI为软件中断号.
在PC得到swi的信号的时候,pc立即跳转到0x08地址,通常这个地址范围在cpu启动的时候会摆放异常中断处理程序,对应软件中断应该是一条跳转指令,
Vectors.s文件
……
LDR PC,Swi_Addr
……
Swi_Addr DCD TrapSwi ;在Swi_Addr中摆放了TrapSwi的地址
TrapSwi是Switrap.s文件中的一个段,用来解析不同的软件中断
INCLUDE except_h.s
EXPORT TrapSWI
IMPORT uputchar
IMPORT print_sys_buf
IMPORT fil_sys_buf
IMPORT clock, WEAK
IMPORT FileCount
IMPORT heap_info
IMPORT semi_stack
IMPORT command_buffer
THUMB_AWARE EQU 1
AIC_IMR EQU 0xFFF82114
AIC_MECR EQU 0xFFF82120
AIC_MDCR EQU 0xFFF82124
AREA Traps, CODE, READONLY
; SWI handler
TrapSWI
STMFD sp!, {r0-r1} ;保护寄存器R0,R1防止被修改
LDR r0, [lr, #-4] ;得到swi指令的数据
BIC r0, r0, #0xff000000 ;取出低24位
LDR r1, =SWI_Angel
CMP r0, r1 比较软件中断号
LDMFD sp!, {r0-r1} ;弹出R0,R1
BNE ExitSWI ;如果不是正确的软件中断号则跳入SWI结束程序
;根据R0也就是swi_run的第一个参数判断是哪种中断程序
CMP r0, #angel_SWIreason_EnterSVC
BEQ SWIEnterOS
CMP r0, #SYS_FILE_OPEN
BEQ Sys_FileOpenSWI
CMP r0, #SYS_READ_SWI
BEQ Sys_ReadSWI
CMP r0, #SYS_WRITE_SWI
BEQ Sys_WriteSWI
CMP r0, #angel_SWI_SYS_WRITEC
BEQ ttywrch_SWI
CMP r0, #SYS_CLOCK
BEQ readtime_SWI
CMP r0, #0x200
BEQ sh_run_SWI
CMP r0, #0x18
BEQ sh_return_SWI
CMP r0, #0x15
BEQ sh_get_command_SWI
CMP r0, #angel_SWI_SYS_HEAPINFO
BNE ExitSWI
STMFD sp!, {r0-r4}
LDR r0, [r1]
LDR r4, =heap_info
LDR r3, [r4], #4 ; heap_base
LDR r2, [r4], #4 ; heap_limit
STR r3, [r0], #4 ; heap_limit
STR r2, [r0], #4 ; heap_base
LDR r2, [r4], #4 ; stack_base
LDR r3, [r4], #4 ; stack_limit
STR r2, [r0], #4 ; stack_base
STR r3, [r0]
LDMFD sp!, {r0-r4}
ExitSWI
MOV r0, #0
MOVS pc, lr
SWIEnterOS
STMFD sp!, {r0}
MRS r0, SPSR
TST r0, #PSR_T_bit
ORRNE r0, r0, #0x13
MSRNE SPSR_cxsf, r0
LDMFD sp!, {r0}
MOVNES pc, lr
MOV pc, lr
Sys_FileOpenSWI
STMFD sp!, {r4, lr}
LDR r0, =FileCount
LDR r4, [r0]
STR r4, [r0]
CMP r4, #0x03
MOVGT r0, #0xffffffff
MOVLT r0, #0x00000000
LDMFD sp!, {r4, lr}
MOVS pc, lr
ttywrch_SWI
STMFD sp!, {r0, lr}
LDR r0, [r1]
BL uputchar
LDMFD sp!, {r0, lr}
MOVS pc, lr
Sys_WriteSWI
STMFD sp!, {r1-r3, lr}
;MOV r0, r2
LDR r0, [r1,#8]
LDR r1, [r1,#4]
BL print_sys_buf
LDMFD sp!, {r1-r3, lr}
MOVS pc,lr
Sys_ReadSWI
STMFD sp!, {lr}
;MOV r0, r2
LDR r0, [r1,#8]
LDR r1, [r1,#4]
BL fil_sys_buf
LDMFD sp!, {lr}
MOVS pc, lr
readtime_SWI
STMFD sp!, {r1-r3, lr}
MRS r1, SPSR
BIC r1, r1, #0x80
MSR SPSR_cxsf, r1
BL clock
LDMFD sp!, {r1-r3, lr}
MOVS pc, lr
sh_run_SWI ;目的软件中断程序
STMFD sp!, {r2-r3,lr} ;入占保护
LDR r2, =AIC_IMR ;中断掩码寄存器
LDR r2, [r2]
MRS r3, SPSR
STMFD sp!, {r2,r3} ;IMR和SPSR入栈保护
LDR r2, =AIC_MDCR ;中断掩码禁止寄存器
LDR r3, =0x7FFFE ;禁止所有中断
STR r3, [r2]
MOV r2, sp
LDR sp, =semi_stack
LDR sp, [sp]
STMFD sp!, {r2}
MOV pc, r1 ;PC=0x8000,实现了跳转
LDMFD sp!, {r2-r3,pc} ;返回,不过本例是跳转PC也就没有返回的意义了
sh_return_SWI
LDR sp, =semi_stack
LDR sp, [sp]
LDR sp, [sp,#-4]
LDMFD sp!, {r2,r3}
MSR SPSR_cxsf, r3
LDR r3, =AIC_MECR
STR r2, [r3]
LDMFD sp!, {r2-r3,pc}^
sh_get_command_SWI
LDR r0, =command_buffer
LDR r1, [r1]
MOV r3, #0x100
0
LDRB r2, [r0],#1
STRB r2, [r1],#1
SUBS r3, r3, #1
BNE %B0
MOV r0, #0
MOVS pc, lr
END
答 1: 好~~~
什么是软件中断不说了,自己看书去,直接切入主题
以sh.c中的G_action()为例
其调用swi_run(0x200,address),通过platform.h中的
__swi(SemiSWI) void swi_run(int op, int addr)调用,传递的参数0x200和address在软件中断服务程序中会使用,R0会被传入0x200,R1传入address,SemiSWI为软件中断号.
在PC得到swi的信号的时候,pc立即跳转到0x08地址,通常这个地址范围在cpu启动的时候会摆放异常中断处理程序,对应软件中断应该是一条跳转指令,
Vectors.s文件
……
LDR PC,Swi_Addr
……
Swi_Addr DCD TrapSwi ;在Swi_Addr中摆放了TrapSwi的地址
TrapSwi是Switrap.s文件中的一个段,用来解析不同的软件中断
INCLUDE except_h.s
EXPORT TrapSWI
IMPORT uputchar
IMPORT print_sys_buf
IMPORT fil_sys_buf
IMPORT clock, WEAK
IMPORT FileCount
IMPORT heap_info
IMPORT semi_stack
IMPORT command_buffer
THUMB_AWARE EQU 1
AIC_IMR EQU 0xFFF82114
AIC_MECR EQU 0xFFF82120
AIC_MDCR EQU 0xFFF82124
AREA Traps, CODE, READONLY
; SWI handler
TrapSWI
STMFD sp!, {r0-r1} ;保护寄存器R0,R1防止被修改
LDR r0, [lr, #-4] ;得到swi指令的数据
BIC r0, r0, #0xff000000 ;取出低24位
LDR r1, =SWI_Angel
CMP r0, r1 比较软件中断号
LDMFD sp!, {r0-r1} ;弹出R0,R1
BNE ExitSWI ;如果不是正确的软件中断号则跳入SWI结束程序
;根据R0也就是swi_run的第一个参数判断是哪种中断程序
CMP r0, #angel_SWIreason_EnterSVC
BEQ SWIEnterOS
CMP r0, #SYS_FILE_OPEN
BEQ Sys_FileOpenSWI
CMP r0, #SYS_READ_SWI
BEQ Sys_ReadSWI
CMP r0, #SYS_WRITE_SWI
BEQ Sys_WriteSWI
CMP r0, #angel_SWI_SYS_WRITEC
BEQ ttywrch_SWI
CMP r0, #SYS_CLOCK
BEQ readtime_SWI
CMP r0, #0x200
BEQ sh_run_SWI
CMP r0, #0x18
BEQ sh_return_SWI
CMP r0, #0x15
BEQ sh_get_command_SWI
CMP r0, #angel_SWI_SYS_HEAPINFO
BNE ExitSWI
STMFD sp!, {r0-r4}
LDR r0, [r1]
LDR r4, =heap_info
LDR r3, [r4], #4 ; heap_base
LDR r2, [r4], #4 ; heap_limit
STR r3, [r0], #4 ; heap_limit
STR r2, [r0], #4 ; heap_base
LDR r2, [r4], #4 ; stack_base
LDR r3, [r4], #4 ; stack_limit
STR r2, [r0], #4 ; stack_base
STR r3, [r0]
LDMFD sp!, {r0-r4}
ExitSWI
MOV r0, #0
MOVS pc, lr
SWIEnterOS
STMFD sp!, {r0}
MRS r0, SPSR
TST r0, #PSR_T_bit
ORRNE r0, r0, #0x13
MSRNE SPSR_cxsf, r0
LDMFD sp!, {r0}
MOVNES pc, lr
MOV pc, lr
Sys_FileOpenSWI
STMFD sp!, {r4, lr}
LDR r0, =FileCount
LDR r4, [r0]
STR r4, [r0]
CMP r4, #0x03
MOVGT r0, #0xffffffff
MOVLT r0, #0x00000000
LDMFD sp!, {r4, lr}
MOVS pc, lr
ttywrch_SWI
STMFD sp!, {r0, lr}
LDR r0, [r1]
BL uputchar
LDMFD sp!, {r0, lr}
MOVS pc, lr
Sys_WriteSWI
STMFD sp!, {r1-r3, lr}
;MOV r0, r2
LDR r0, [r1,#8]
LDR r1, [r1,#4]
BL print_sys_buf
LDMFD sp!, {r1-r3, lr}
MOVS pc,lr
Sys_ReadSWI
STMFD sp!, {lr}
;MOV r0, r2
LDR r0, [r1,#8]
LDR r1, [r1,#4]
BL fil_sys_buf
LDMFD sp!, {lr}
MOVS pc, lr
readtime_SWI
STMFD sp!, {r1-r3, lr}
MRS r1, SPSR
BIC r1, r1, #0x80
MSR SPSR_cxsf, r1
BL clock
LDMFD sp!, {r1-r3, lr}
MOVS pc, lr
sh_run_SWI ;目的软件中断程序
STMFD sp!, {r2-r3,lr} ;入占保护
LDR r2, =AIC_IMR ;中断掩码寄存器
LDR r2, [r2]
MRS r3, SPSR
STMFD sp!, {r2,r3} ;IMR和SPSR入栈保护
LDR r2, =AIC_MDCR ;中断掩码禁止寄存器
LDR r3, =0x7FFFE ;禁止所有中断
STR r3, [r2]
MOV r2, sp
LDR sp, =semi_stack
LDR sp, [sp]
STMFD sp!, {r2}
MOV pc, r1 ;PC=0x8000,实现了跳转
LDMFD sp!, {r2-r3,pc} ;返回,不过本例是跳转PC也就没有返回的意义了
sh_return_SWI
LDR sp, =semi_stack
LDR sp, [sp]
LDR sp, [sp,#-4]
LDMFD sp!, {r2,r3}
MSR SPSR_cxsf, r3
LDR r3, =AIC_MECR
STR r2, [r3]
LDMFD sp!, {r2-r3,pc}^
sh_get_command_SWI
LDR r0, =command_buffer
LDR r1, [r1]
MOV r3, #0x100
0
LDRB r2, [r0],#1
STRB r2, [r1],#1
SUBS r3, r3, #1
BNE %B0
MOV r0, #0
MOVS pc, lr
END
答 1: 好~~~
共2条
1/1 1 跳转至页
回复
有奖活动 | |
---|---|
【有奖活动——B站互动赢积分】活动开启啦! | |
【有奖活动】分享技术经验,兑换京东卡 | |
话不多说,快进群! | |
请大声喊出:我要开发板! | |
【有奖活动】EEPW网站征稿正在进行时,欢迎踊跃投稿啦 | |
奖!发布技术笔记,技术评测贴换取您心仪的礼品 | |
打赏了!打赏了!打赏了! |