我想知道在基于arm的微控制器上编写SVC调用的正确方法。
到目前为止,我的理解是ARM有一个异常向量表,这意味着任何程序中的第一个指令都必须是适当处理程序的分支:
RESET ;Handles reset
UNDEFINED ;Undefined instructions
SVC BL SVC_Entry
PRE_ABORT ;Prefetch abort
DAT_ABORT ;Data abort
然后,每次运行SVC指令时,模式切换为supervisor, SVC提供的编号存储在R0中,程序将分支到相应的处理程序:
;== Handling SVC calls ========================================================
Max_SVC EQU 1
SVC_Entry CMP R0, #Max_SVC ;Check upper limit
BHI SVC_end ;Does nothing if unknown
ADD R0, PC, R0, LSL #2 ;Calculate table address
LDR PC, [R0, #0]
Jump_table DEFW SVC_0 ;Halt
DEFW SVC_1 ;Print string
;== SVC calls ================================================================
SVC_1 B SVC_end
SVC_end MOVS PC, LR ;Exiting
那么,如果我们有这些指令:
ADR R1, string ;R1 points to the string
SVC 1 ;SVC_1 handles the printing
程序必须切换到主管模式,将数字"1"存储在R0中,然后按照跳转表分支到SVC_1,运行代码并切换回用户模式。
正确吗?我做得对吗?
到目前为止,我遇到的问题是,我的编译器对这行说"操作符预期":
SVC BL SVC_Entry
关于这个主题的信息很难在互联网上找到,我只是想知道如何正确使用ARM微控制器上的SVC调用。
非常感谢。
EDIT:底层处理器是ARM9时钟,大约240 MHz。它生活在AT91微控制器。它所在的实验室板已被修改以适应我大学的需要。
代码通过串行端口使用定制程序加载到板上。该程序还允许调试。
如前所述,不要使用BL跳转到SVC条目,使用b。有一个例程先决定确定SVC编号。(我的程序叫做SVC_dispatcher)。你在哪个大学读书?我将尝试彻底地解释这一点,不假设你知道多少或不知道多少。我已经输入了正确的术语,所以如果我的评论不清楚或者你想要更深入的信息,你可以谷歌更多信息。我不确定用冒号标记的方法,我习惯了一个旧的指令集。
好运SVC_dispatcher
PUSH {LR} ; always save your LR
LDR R14, [LR, #-4] ; its been stacked, so we can it (LR is R14)
; the link register is the line after the SVC instruction
; above, we load the instruction that is one before the
; link register (#-4 preindexed load) means instruction (SVC 1) into R14.
; Use bit clear to remove the mnemonic from the 32 bit instruction,
; leaving the data (1)
BIC R14, R14, #&FF000000
SVC_entry
CMP R14, #Max_SVC
BHI SVC_unknown
ADR R1, Jump_Table ; use this format, never add to the PC
LDR PC, [R1, R14, LSL #2]
; Beware: PC can be changed by IRQs **AT ANY TIME**
Jump_Table ; you know the drill here
DEFW SVC_0
DEFW SVC_1
SVC_0 B SVC_end
SVC_1 BL printString ; consider stacking registers that printString
B SVC_end ; will corrupt
SVC_end POP {LR} ; restore link register
MOV PC, LR
只是补充一下Yoker上面的回答来澄清一下:
正如Yoker回答的例子所示,传统上来自SVC指令的数字不存储在R0中(如您在问题中所述),您应该从代码中检索它,直接从SVC指令读取即时值。
要做到这一点,您需要在返回地址之前获得指向指令的指针,例如,如果您有这些指令:0x800010: ADR R1, string ;R1 points to the string
0x800014: SVC 1 ;SVC_1 handles the printing
0x800018: ADD R1, R2 ;The next instruction after the SVC where LR will point to
返回地址将是LR=0x800018,然后我们可以检索SVC指令的地址LR-4=0x800014,读取该地址内容(这是SVC 1指令)并只获得它的第一个字节(我们忽略SVC操作码,只获得直接值)。
因此,在Yoker的例子中,我们可以看到下面的指令正是这样做的:
LDR R14, [LR, #-4]
BIC R14, R14, #&FF000000
下面是另一个在cortex M0中使用C代码的例子(拇指指令):
void SVC_Handler(void)
{
// Get stack pointer, assuming we the thread that generated
// the svc call was using the psp stack instead of msp
unsigned int *stack;
asm volatile ("MRS %0, pspnt" : "=rm" (stack) );
// Stack frame contains:
// r0, r1, r2, r3, r12, r14, the return address and xPSR
// - Stacked R0 = stack[0]
// - Stacked R1 = stack[1]
// - Stacked R2 = stack[2]
// - Stacked R3 = stack[3]
// - Stacked R12 = stack[4]
// - Stacked LR = stack[5]
// - Stacked PC = stack[6]
// - Stacked xPSR= stack[7]
// Thumb instructions have 2 bytes instead of 4, then get
// the first byte of the instruction right before the
// instruction pointed by the stacked PC
unsigned int svc_number = ((char *)svc_args[6])[-2];
switch(svc_number) {
case 0:
...
break;
case 1:
...
break;
}