问题描述
我在裸机应用中遇到内存问题。
int var = 1;
int *p_var = &(var);
同一个文件有一个函数(IRQ 处理程序),其中下列语句为真:
var == 1
*p_var != var
期望的行为:*p_var 不应该等于 var 吗?
感谢任何帮助!
更新: 感谢您到目前为止的回复。
由于问题出在内存中,这里是我的代码的一些相关部分:
这是我的切入点。也许 el1_entry 不好?
#include "mm.h"
#include "arm/sysreg.h"
.section ".text.boot"
.globl _start
_start:
mrs x0,mpidr_el1 // Get info about the cpu from reg mpidr_el1
and x0,x0,#0xFF // Extract the ID of the cpu
cmp x0,#0 // Comopare the id of the cpu core with 0
beq master // If the core's id == 0,branch to mater
b proc_hang // If the core' id != 0,branch to hang
master:
// disable the MMU
ldr x0,=SCTLR_VALUE_MMU_disABLED
msr sctlr_el1,x0
// EL1 will execute at Aarch64
ldr x0,=HCR_VALUE
msr hcr_el2,x0
// EL1 will execute at Aarch64; and EL0 and 1 will be "non secure"
ldr x0,=SCR_VALUE
msr scr_el3,x0
// Configure EL1 to use its own stack and disable interrupts when EL1
ldr x0,=SPSR_VALUE
msr spsr_el3,x0
// Jump to el1_entry
adr x0,el1_entry
msr elr_el3,x0
eret
el1_entry:
/* Clean memory from addresses bss_begin to bss_end */
adr x0,bss_begin
ldr x1,=bss_end
sub x1,x1,x0
bl memzero // Call function to clean memory
mov sp,#LOW_MEMORY // Initialize stack pointer
bl kernel_main // Transfer control to kernel_main function
b proc_hang
proc_hang:
b proc_hang
向量表,在中断之间保存和恢复状态。也许保存和恢复有问题?
#include "entry.h"
/* Macro to save EL1 state when an exception happens */
.macro kernel_entry
sub sp,sp,#S_FRAME_SIZE
stp x0,[sp,#16 * 0]
stp x2,x3,#16 * 1]
stp x4,x5,#16 * 2]
stp x6,x7,#16 * 3]
stp x8,x9,#16 * 4]
stp x10,x11,#16 * 5]
stp x12,x13,#16 * 6]
stp x14,x15,#16 * 7]
stp x16,x17,#16 * 8]
stp x18,x19,#16 * 9]
stp x20,x21,#16 * 10]
stp x22,x23,#16 * 11]
stp x24,x25,#16 * 12]
stp x26,x27,#16 * 13]
stp x28,x29,#16 * 14]
mrs x22,elr_el1
mrs x23,spsr_el1
stp x30,x22,#16 * 15]
str x23,#16 * 16]
.endm
/* Macro to restore EL1 state and return from the exception handling */
.macro kernel_exit
ldr x23,#16 * 16]
ldp x30,#16 * 15]
msr elr_el1,x22
msr spsr_el1,x23
ldp x0,#16 * 0]
ldp x2,#16 * 1]
ldp x4,#16 * 2]
ldp x6,#16 * 3]
ldp x8,#16 * 4]
ldp x10,#16 * 5]
ldp x12,#16 * 6]
ldp x14,#16 * 7]
ldp x16,#16 * 8]
ldp x18,#16 * 9]
ldp x20,#16 * 10]
ldp x22,#16 * 11]
ldp x24,#16 * 12]
ldp x26,#16 * 13]
ldp x28,#16 * 14]
add sp,#S_FRAME_SIZE
eret
.endm
/*
* Exception vectors table is initialized in this file...,but I'm not including the code in this post.
*/
...
// IRQ Handler
el1_irq:
kernel_entry
bl handle_irq
kernel_exit
handle_irq
...
void handle_irq(void) {
u32 irq;
irq = REGS_IRQ->irq0_pending_0;
while(irq) {
if(irq & SYS_TIMER_IRQ_1) {
irq &= ~SYS_TIMER_IRQ_1;
handle_timer_1();
}
}
}
handle_timer_1
...
void handle_timer_1(void)
{
// Set the next timer interrupt
currrent_value_1 += interval_1;
REGS_TIMER->compare[1] = currrent_value_1;
REGS_TIMER->control_status |= SYS_TIMER_IRQ_1;
timer_tick();
}
timer_tick()(观察到意外行为的地方)
...
int var = 1;
int *p_var = &(var); // Current task being executed
void timer_tick(void)
{
char buff[] = "0000000000000000";
parse_int(*p_var,buff,16);
uart_send_string("*p_var: ");
uart_send_string(buff);
uart_send_string("\n");
parse_int(var,16);
uart_send_string("var: ");
uart_send_string(buff);
uart_send_string("\n");
if(p_var == &var) {
uart_send_string("equal: true");
} else {
uart_send_string("equal: false");
}
}
“最小示例”有点广泛,所以我不能在这里直接发布。我把它放在 github(我链接到入口点)
解决方法
我是不是在不知不觉中把内存弄乱了?
是的。