首页 文章

在IRQ之后,Cortext-M3上的任务切换崩溃

提问于
浏览
2

我已经为我的ARM Cortex-M3操作系统使用了一个exokernel模型 . 当任务想要从UART读取时,它调用库函数,如果没有数据,则执行SVC调用以阻塞任务(这会导致内核将任务放入该IRQ的等待队列并启用IRQ ) . 当中断发生时,等待它的所有任务都将被移动到可运行队列,并再次禁用中断 .

当我有一个固定的任务数组时,这个模型运行正常,但现在我已经转移到链表以允许更多类型的等待队列(例如IPC消息) . 变化中的某些东西正在导致崩溃 . 这是调试输出:

Creating task 0 (idle task)
task0 stack top is 2007cd20
Starting SysTick @ 100Hz
Becoming task 0
Switching to task gsm@2007c008 with SP 2007c3e8
GSM task starting
Switching to task rfid@2007c430 with SP 2007c810
Monitoring RFID reader
Blocking task rfid on IRQ 7
Switching to task gps@2007c858 with SP 2007cc38
Switching to task task0@2007cc80 with SP 2007ccd8
Switching to task gsm@2007c008 with SP 2007c390
Blocking task gsm on IRQ 8
Switching to task gps@2007c858 with SP 2007cc38
Switching to task task0@2007cc80 with SP 2007ccd8
Switching to task gps@2007c858 with SP 2007cc38
Starting GPS tracking
Blocking task gps on IRQ 6
Switching to task task0@2007cc80 with SP 2007ccd8
[... repeats...]
Switching to task task0@2007cc80 with SP 2007ccd8
Unblocking tasks waiting on IRQ 8
Switching to task gsm@2007c008 with SP 2007c3a0
Switching to task task0@2007cc80 with SP 2007ccd8
Switching to task gsm@2007c008 with SP 2007c3a0
Fault: Usage fault
   r0 = 2007c3a0
   r1 = 10007fb8
   r2 = 2007ccd8
   r3 = 10007fb8
  r12 = 00000008
   lr = fffffffd
   pc = 0070c858
  psr = 00000003
 BFAR = e000ed38
 CFSR = 00040000
 DFSR = 00000000
 AFSR = 00000000
SHCSR = 00070008

所以一切都很好,直到中断 . 实际输出取决于哪个UART首先具有数据,但模式是相同的:当发生中断时,当未阻塞的任务切换到第二次时发生故障 .

这是代码的相关部分 . 装配垫片:

zeptos_pendsv_isr:
    push {lr}
    mrs r0, psp
    stmfd r0!, {r4-r11}
    bl zeptos_schedule
    ldmfd r0!, {r4-r11}
    msr psp, r0
    pop {pc}

而C函数:

static void pendsv(void) {
    SCB->ICSR |= 1 << 28;
}

void *zeptos_schedule(void *sp) {
    if (current_task) {
        current_task->sp = sp;
        DL_APPEND(runnable_tasks, current_task);
    }
    current_task = runnable_tasks;
    DL_DELETE(runnable_tasks, current_task);
    zeptos_printf("Switching to task %s@%p with SP %p\n", current_task->name, current_task, current_task->sp);
    return current_task->sp;
}

static void block(void *sp, uint8_t irq) {
    zeptos_printf("Blocking task %s on IRQ %i\n", current_task->name, irq);
    current_task->sp = sp;
    DL_APPEND(irq_blocked_tasks[irq], current_task);
    current_task = 0;
    NVIC_EnableIRQ(irq);
    pendsv();
}

void __attribute__((interrupt)) zeptos_isr(void) {
    int irq = (SCB->ICSR & 0xff) - 16;
    zeptos_printf("Unblocking tasks waiting on IRQ %i\n", irq);
    NVIC_DisableIRQ(irq);
    // NVIC_ClearPendingIRQ(irq);
    DL_CONCAT(runnable_tasks, irq_blocked_tasks[irq]);
    irq_blocked_tasks[irq] = 0;
    pendsv();
}

void __attribute__((interrupt)) zeptos_svc_isr(void) {
    __disable_irq();
    uint32_t *sp = (uint32_t *) __get_PSP();
    uint32_t pc = sp[6];
    uint8_t svc_type = *((uint8_t *) pc - 2);
    switch (svc_type) {
        case 0:
            sleep(sp[0]);
            break;

        case 1:
            block(sp, sp[0]);
            break;

        default:
            zeptos_puts("Bad SVC type\n");
    }
    __enable_irq();
}

void Zeptos_BlockOnIrq(uint8_t irq) {
    asm("svc 1");
}

SVC,SysTick和PendSV分别优先考虑29,30和31 .

故障是INVPC类型的使用故障,这意味着正在使用错误类型的EXC_RETURN值 . 我检查过,每次都是0xfffffffd .

有什么建议?我应该在哪里看?

2 回答

  • 1

    如何保留任务的处理器状态?如果我没记错,你需要在切换时保存CPSR . 您可能需要将其更改为:

    mrs r12, epsr
    stmfd r0!, { r4 - r11, r12 }
    ...
    ldmfd r0!, { r4 - r11, r12 }
    msr r12, epsr
    

    EPSR将包含诸如处理器条件标志和任何IT块状态信息之类的内容 .

  • 1

    我终于找到了问题 . 当我的SVC处理程序调用 block 将任务放在阻止列表上时,该任务的堆栈只有硬件堆叠的寄存器,而不是调度程序在以后再次运行它时所期望的 {r4-r11} .

    快速解决方法是为SVC ISR提供一个汇编填充程序,用于堆叠和取消堆栈额外的寄存器,并使C zeptos_svc_isr 函数返回堆栈指针,如_1595173所做 . 它有效,但现在有些重构 .

相关问题