0

我将通过特定方式(对于 POC)在内核模块和用户空间程序之间传递数据。

我写了一个模块,它分配了一个struct page使用alloc_page()函数,并在上面写了一些数据。我的模块代码如下

#include <linux/module.h>    // included for all kernel modules
#include <linux/kernel.h>    // included for KERN_INFO
#include <linux/init.h>      // included for __init and __exit macros
#include<linux/kthread.h>
#include<linux/sched.h>
#include<linux/delay.h>
#include<linux/slab.h>
#include <linux/mm_types.h>
#include <linux/mm.h>

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("POC");

struct task_struct *task1;
struct page * p1;

int cpu, data_pass;

int thread_function_one(void *data_old)
{
    char * data;
    int i;

    p1 = alloc_page(GFP_KERNEL);
    data = (char *)page_address(p1);

    for(i = 0;i < 5; i++)
        data[i] = 'x';

    while(!kthread_should_stop()){
        printk("Thread 1\n");

        printk("Virt = %p\n", page_to_virt(p1));
        printk("Phys %llx\n", page_to_phys(p1));
        printk("page_address %p\n", page_address(p1));
        printk("offset_in_page %ld\n", offset_in_page(p1));

        for(i = 0;i < 5; i++)
            printk("data %c", data[i]);
        printk("\n");

       msleep(5000);
    }
    printk(KERN_INFO "EXIT from thread function 1\n");
    return 0;
}


static int __init hello_init(void)
{
    printk(KERN_INFO "Hello world!\n");
    task1 = kthread_create(&thread_function_one,(void *)&data_pass,"one");
    wake_up_process(task1);
    return 0;    // Non-zero return means that the module couldn't be loaded.
}

static void __exit hello_cleanup(void)
{
    printk(KERN_INFO "Cleaning up module.\n");
    kthread_stop(task1);
}

module_init(hello_init);
module_exit(hello_cleanup);

我得到输出,

[  201.976198] Phys 3c183c000
[  201.976200] page_address 0000000066f018d8
[  201.976202] offset_in_page 3840
[  201.976204] data x
[  201.976206] data x
[  201.976207] data x
[  201.976209] data x
[  201.976210] data x

在用户方面,我正在尝试使用mmap该物理内存并读取数据。我为用户端代码引用了这个问题。

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>

int main(int argc, char *argv[]) {
    if (argc < 3) {
        printf("Usage: %s <phys_addr> <offset>\n", argv[0]);
        return 0;
    }

    off_t offset = strtoul(argv[1], NULL, 0);
    size_t len = strtoul(argv[2], NULL, 0);

    // Truncate offset to a multiple of the page size, or mmap will fail.
    size_t pagesize = sysconf(_SC_PAGE_SIZE);
    off_t page_base = (offset / pagesize) * pagesize;
    off_t page_offset = offset - page_base;

    int fd = open("/dev/mem", O_SYNC);
    unsigned char *mem = mmap(NULL, page_offset + len, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, page_base);
    if (mem == MAP_FAILED) {
        perror("Can't map memory");
        return -1;
    }

    size_t i;
    for (i = 0; i < 10; ++i)
    {
        printf("%02x \n", (int)mem[page_offset + i]);
        printf("char 1 %c\n", mem[page_offset + i]);
        printf("char 2 %c\n", mem[i]);
        printf("\n\n");
    }

    return 0;
}

但是,用户端的输出是垃圾。

f0
char 1 ▒
char 2 S


53
char 1 S
char 2 ▒


ff
char 1 ▒
char 2


00
char 1
char 2 ▒


f0
char 1 ▒
char 2 S

我究竟做错了什么?

4

0 回答 0