linux关于程序的user,c – “get_user_pages”如何工作(对于linux驱动程序)

在Linux PCI驱动程序上工作,现在我正在尝试使用scatter / gather为DMA编写代码.

目前,我已经了解到要直接从用户空间访问DMA数据,我们需要将用户空间页面固定到内核空间.

要做到这一点,我们有get_user_pages,其完整定义如下:

int get_user_pages(struct task_struct * tsk,

struct mm_struct * mm,

unsigned long start,

int nr_pages,

int write,

int force,

struct page ** pages,

struct vm_area_struct ** vmas);

我的第一个问题是关于struct page **页面.在调用get_user_pages之前,我们需要为页面分配内存(使用kcalloc,例如).

我的第二个问题是关于无符号长启动,在手册页上,它说“启动用户地址”,这是否意味着,如果我在用户空间中声明一个指针,如int * p,那么“起始用户地址”我应该通过到内核​​空间是p?

我的第三个问题也是关于unsigned long start,如果我在第二个问题中理解正确,那么我们如何确保这个地址正好在页面的开头?

所以有三个问题,谢谢你的进步.

解决方法:

My first question is about the struct page ** pages. Here do we need to allocate memory(using kcalloc for ex.) for the pages before calling the get_user_pages?

你可以,但这不是强制性的,数组就足够了(它的大小取决于nr_pages)

如果你想固定4页,结构页面*页面[4];足够.

My second question is about the unsigned long start, on the man page, it says “starting user address”, does it mean that, if I declare a pointer like int *p, the “starting user address” I should pass to kernel space is p?

此参数应指向用户进程拥有的内存(如malloc之后).

My third question is also about the unsigned long start, if I understand correctly in the second question, then how can we make sure that this address begins exactly at the beginning of a page?

我认为你可以用getpagesize功能来做到这一点.

标签:c-3,linux,linux-kernel,linux-device-driver,kernel-module

来源: https://codeday.me/bug/20190628/1312070.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值