How to access user space memory from the Linux kernel?

后端 未结 4 1727
我寻月下人不归
我寻月下人不归 2021-02-01 10:20

I know that copy_to_user/copy_from_user, get_user/put_user functions are for this purpose.

My question is that, given

4条回答
  •  爱一瞬间的悲伤
    2021-02-01 10:34

    Different user space application has different page table.

    1. you need to get user space program pid.
    2. search addree in the pid's page table.

    Below is a sample code to translate user space virtual address's into physical address. It works at x86 platform.

    taskpid = find_get_pid(curpid);
    task = pid_task(taskpid, PIDTYPE_PID );
    mm = get_task_mm(task);
    down_read(&mm->mmap_sem);
    
    start_vaddr = vaddr;
    end_vaddr = 0xC0000000;
    
    while( start_vaddr < end_vaddr){
        u32 end;
    
        end = (( start_vaddr + PMD_SIZE) & PMD_MASK);
    
        if( end < start_vaddr || end > end_vaddr)
            end = end_vaddr;
    
        ret = walk_pgd(start_vaddr, end, mm);
        if(ret != 0){
            printk("ret: %08x \n", ret);
            break;
        }
    
        start_vaddr = end;
    
    }
    
    up_read(&mm->mmap_sem);
    
    paddr = ret;
    kaddr = __va(paddr);
    mmput(mm);
    

提交回复
热议问题