Skip to content

Adding x86_64 support to the project, there is a trap problem in m_2_0. #13

@MTttth

Description

@MTttth

Can't trap in m_2_0

Before entering the user program, below jobs are done:

  • new a space for user program, copy the kernel space at the same time.
    let mut uspace = axmm::new_user_aspace().unwrap();
    pub fn new_user_aspace() -> AxResult<AddrSpace> {
        let mut aspace = AddrSpace::new_empty(VirtAddr::from(USER_ASPACE_BASE), USER_ASPACE_SIZE)?;
        aspace.copy_mappings_from(&kernel_aspace().lock())?;
        Ok(aspace)
    }
  • Load user app binary file into address space.
    if let Err(e) = load_user_app("/sbin/origin", &mut uspace) {
        panic!("Cannot load app! {:?}", e);
    }
  • Init user stack
    let ustack_top = init_user_stack(&mut uspace, false).
    fn init_user_stack(uspace: &mut AddrSpace, populating: bool) -> io::Result<VirtAddr> {
        let ustack_top = uspace.end();
        let ustack_vaddr = ustack_top - crate::USER_STACK_SIZE;
        uspace.map_alloc(
            ustack_vaddr,
            crate::USER_STACK_SIZE,
            MappingFlags::READ | MappingFlags::WRITE | MappingFlags::USER,
            populating,
        ).unwrap();
        Ok(ustack_top)
    }
  • Spawn user task, at the same time alloc the kernel stack for the task
    let user_task = task::spawn_user_task(
        Arc::new(Mutex::new(uspace)),
        #[cfg(target_arch = "riscv64")]
        UspaceContext::new(APP_ENTRY.into(), ustack_top),
        #[cfg(target_arch = "aarch64")]
        UspaceContext::new(APP_ENTRY.into(), ustack_top, 0),
        #[cfg(target_arch = "x86_64")]
        UspaceContext::new(APP_ENTRY, ustack_top, 0),
    );
    pub fn spawn_user_task(aspace: Arc<Mutex<AddrSpace>>, uctx: UspaceContext) -> AxTaskRef {
        let mut task = TaskInner::new(
            || {
                let curr = axtask::current();
                let kstack_top = curr.kernel_stack_top().unwrap();
                unsafe { curr.task_ext().uctx.enter_uspace(kstack_top) };
            },
            "userboot".into(),
            crate::KERNEL_STACK_SIZE,
        );
        task.ctx_mut()
            .set_page_table_root(aspace.lock().page_table_root());
        task.init_task_ext(TaskExt::new(uctx, aspace));
        axtask::spawn_task(task)
    }

Here goes the problem

When I try to access the address in user stack 0x3fffffffc0, which is alloc with populating = false, it should have caused a page fault trap. But actually when I step into the following with rsp = 0x3fffffffc0 , the qemu reset.

"mov    qword ptr [rsp], 93"

QEMU LOG

I think the IDT and GDT, and TSS, are all set up correctly. I can access the address of IDT and GDT, and the contents of TSS.rsp0 are set to the address of the kernel stack top .The content is correct. unfortunately, still can't get into the trap handler.

check_exception old: 0xffffffff new 0xe
     8: v=0e e=0006 i=0 cpl=3 IP=0033:0000000000001004 pc=0000000000001004 SP=002b:0000003fffffffc0 CR2=0000003fffffffc0
RAX=0000000000000000 RBX=0000000000000000 RCX=0000000000000000 RDX=0000000000000000
RSI=0000000000000000 RDI=0000000000000000 RBP=0000000000000000 RSP=0000003fffffffc0
R8 =0000000000000000 R9 =0000000000000000 R10=0000000000000000 R11=0000000000000000
R12=0000000000000000 R13=0000000000000000 R14=0000000000000000 R15=0000000000000000
RIP=0000000000001004 RFL=00000206 [-----P-] CPL=3 II=0 A20=1 SMM=0 HLT=0
ES =0000 0000000000000000 00000000 00000000
CS =0033 0000000000000000 ffffffff 00affb00 DPL=3 CS64 [-RA]
SS =002b 0000000000000000 ffffffff 00cff300 DPL=3 DS   [-WA]
DS =0000 0000000000000000 00000000 00000000
FS =0000 0000000000000000 00000000 00000000
GS =0000 0000000000000000 00000000 00000000
LDT=0000 0000000000000000 0000ffff 00008200 DPL=0 LDT
TR =0038 ffffff800025f00c 00000067 00008900 DPL=0 TSS64-avl
GDT=     ffffff800025f080 0000007f
IDT=     ffffff80002a0870 00000fff
CR0=80010033 CR2=0000003fffffffc0 CR3=000000000045d000 CR4=000000a0
DR0=0000000000000000 DR1=0000000000000000 DR2=0000000000000000 DR3=0000000000000000 
DR6=00000000ffff0ff0 DR7=0000000000000400
CCS=0000000000000004 CCD=0000003fffffffc0 CCO=EFLAGS
EFER=0000000000000d01
check_exception old: 0xe new 0xe
     9: v=08 e=0000 i=0 cpl=3 IP=0033:0000000000001004 pc=0000000000001004 SP=002b:0000003fffffffc0 env->regs[R_EAX]=0000000000000000
RAX=0000000000000000 RBX=0000000000000000 RCX=0000000000000000 RDX=0000000000000000
RSI=0000000000000000 RDI=0000000000000000 RBP=0000000000000000 RSP=0000003fffffffc0
R8 =0000000000000000 R9 =0000000000000000 R10=0000000000000000 R11=0000000000000000
R12=0000000000000000 R13=0000000000000000 R14=0000000000000000 R15=0000000000000000
RIP=0000000000001004 RFL=00000206 [-----P-] CPL=3 II=0 A20=1 SMM=0 HLT=0
ES =0000 0000000000000000 00000000 00000000
CS =0033 0000000000000000 ffffffff 00affb00 DPL=3 CS64 [-RA]
SS =002b 0000000000000000 ffffffff 00cff300 DPL=3 DS   [-WA]
DS =0000 0000000000000000 00000000 00000000
FS =0000 0000000000000000 00000000 00000000
GS =0000 0000000000000000 00000000 00000000
LDT=0000 0000000000000000 0000ffff 00008200 DPL=0 LDT
TR =0038 ffffff800025f00c 00000067 00008900 DPL=0 TSS64-avl
GDT=     ffffff800025f080 0000007f
IDT=     ffffff80002a0870 00000fff
CR0=80010033 CR2=fffffffffffffff8 CR3=000000000045d000 CR4=000000a0
DR0=0000000000000000 DR1=0000000000000000 DR2=0000000000000000 DR3=0000000000000000 
DR6=00000000ffff0ff0 DR7=0000000000000400
CCS=0000000000000004 CCD=0000003fffffffc0 CCO=EFLAGS
EFER=0000000000000d01
check_exception old: 0x8 new 0xe
Triple fault

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions