ARM32,phys_to_virt,无法处理虚拟地址上的内核页面调度请求

问题描述

我正在实现https://apenwarr.ca/log/20190216的变体。长话短说,主要的想法是在内存中留出空间,以便在软重启/出现紧急情况后保留信息并检索该信息。

就我而言,我只想保留一些变量,以免重新引导到另一个变量。因此,我已经研究了此机制的一个简单变体来完成这项工作。该代码只是原始补丁的复制粘贴,并进行了一些原始改编。我添加一个系统调用以进入内核模式以执行此代码(此处未显示)。

    struct logbits {
        int magic; /* needed to verify the memory across reboots */
        int state;
        int nb_reboot;
    };
    
    #define PERSIST_SEARCH_START 0
    #ifdef CONfig_NO_BOOTMEM
    #define PERSIST_SEARCH_END 0x5e000000
    #else
    #define PERSIST_SEARCH_END 0xfe000000
    #endif
    #define PERSIST_SEARCH_JUMP (4*1024)
    #define PERSIST_MAGIC 0xba5eba11
    
    /*
     * arm uses one memory model,mips uses another
     */
    phys_addr_t physmem_reserve(phys_addr_t size) {
    #ifdef CONfig_NO_BOOTMEM
        phys_addr_t alloc;
        alloc = memblock_find_in_range_node(size,SMP_CACHE_BYTES,PERSIST_SEARCH_START,PERSIST_SEARCH_END,NUMA_NO_NODE);
        if (!alloc) return alloc;
        if (memblock_reserve(alloc,size)) {
            pr_err("info_keeper: memblock_reserve Failed\n");
            return 0;
        }
        return alloc;
    #else
        unsigned long where;
        for (where = PERSIST_SEARCH_END - size;
             where >= PERSIST_SEARCH_START && where <= PERSIST_SEARCH_END - size;
             where -= PERSIST_SEARCH_JUMP) {
            if (reserve_bootmem(where,size,BOOTMEM_EXCLUSIVE))
                continue;
            else
                return where;
        }
        return 0;
    #endif
    }
    
    struct logbits *log_buf_alloc(char **new_logbuf)
    {
        char *buf;
        phys_addr_t alloc;
        unsigned long size = sizeof(struct logbits);
        unsigned long full_size = size;
        struct logbits *new_logbits;
    
        alloc = physmem_reserve(full_size);
        if (alloc) {
            printk(KERN_INFO "info_keeper: memory reserved @ 0x%08x\n",alloc);
            buf = phys_to_virt(alloc);
            if(buf){
                *new_logbuf = buf;
                new_logbits = (void*)buf;
                printk(KERN_INFO "info_keeper: memory virtual @ 0x%08x\n",buf);
                if (new_logbits->magic != PERSIST_MAGIC) {
                    printk(KERN_INFO "info_keeper: header invalid," "cleared.\n");
                    memset(buf,full_size);
                    memset(new_logbits,sizeof(*new_logbits));
                    new_logbits->magic = PERSIST_MAGIC;
                } else {
                    printk(KERN_INFO "info_keeper: header valid; " "state=%d\n" "nb_reboot=%d\n",new_logbits->state,new_logbits->nb_reboot);
                }
                return new_logbits;
            }else{
                printk(KERN_ERR "info_keeper: Failed to get phys to virt");
                buf = alloc_bootmem(full_size);
                *new_logbuf = buf;
                new_logbits = (struct logbits*)(buf);
                memset(buf,full_size);
            }
        } else {
            /* replace the buffer */
            printk(KERN_ERR "info_keeper: Failed to reserve bootmem " "area. disabled.\n");
            buf = alloc_bootmem(full_size);
            *new_logbuf = buf;
            new_logbits = (struct logbits*)(buf);
            memset(buf,full_size);
        }
        return new_logbits;
    }

执行后,physmem_reserve函数成功并返回一个内存区域。然后,我从phys_to_virt获得了物理到虚拟内存的映射。然后,当我尝试访问内存时,出现此Unable to handle kernel paging request at virtual address错误

以下是示例输出

[   42.489639] info_keeper: memory reserved @ 0x5dffffc0
[   42.494781] info_keeper: memory virtual @ 0x0dffffc0
[   42.499778] Unable to handle kernel paging request at virtual address 0dffffc0

有什么想法吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)