陷入入口, 系统调用会触发下面逻辑

reg_t trap_handler(reg_t epc, reg_t cause, struct context *cxt)
{
    reg_t return_pc = epc;
    reg_t cause_code = cause & MCAUSE_MASK_ECODE;
    
    if (cause & MCAUSE_MASK_INTERRUPT) {
    } else {
        printf("Sync exceptions! Code = %ld\n", cause_code);
        switch (cause_code) {
        case 8:
            uart_puts("System call from U-mode!\n");
            do_syscall(cxt);
            return_pc += 4;
            break;
        default:
            panic("OOPS! What can I do!");
            //return_pc += 4;
        }
    }

    return return_pc;
}

系统调用的具体逻辑实现逻辑

void do_syscall(struct context *cxt)
{
    uint32_t syscall_num = cxt->a7; 

    switch (syscall_num) {
    case SYS_gethid:
        cxt->a0 = sys_gethid((unsigned int *)(cxt->a0));
        break;
    default:
        printf("Unknown syscall no: %d\n", syscall_num);
        cxt->a0 = -1;
    }

    return;
}

系统调用-系统层面封装

#define SYS_gethid    1  # 系统调用编号,

.global gethid # 汇编定义的系统函数
gethid:
    li a7, SYS_gethid
    ecall  #会触发trap,
    ret

putao
5 声望0 粉丝

推动世界向前发展,改善民生。