xv6-增加系统调用

panic

以下代码报错

1
2
lapicid 2: panic: mycpu called with interrupts enabled
80103937 8010394f 80104a7d 80105b51 8010589e 0 0 0 0 0

1
2
3
4
5
int 
getcpuid()
{
return cpuid();
}
  • 原因:mycpu()要求在关闭中断时调用(函数开头检查 IF),而某处(这里是 scheduler 开头)在中断允许的情况下调用了它,导致 panic。
  • 修复:在调用 mycpu()前禁用中断(pushcli),调用后恢复(popcli)。把 scheduler 开头修改如下。
  • 说明:这样能保证在读取/比较本 CPU 的 LAPIC id 时不会被中断重入。若还有其它在未禁中断情况下直接调用 mycpu() 的地方,也请同样处理或确保调用者已禁中断。
    1
    2
    3
    4
    5
    6
    7
    8
    int 
    getcpuid()
    {
    pushcli();
    int id = cpuid();
    popcli();
    return id;
    }