[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Xen-devel] mmap in PV xen-4.0.1



On Wed, Aug 10, 2011 at 2:12 AM, Wei Liu <liuw@xxxxxxxxx> wrote:
> On Tue, Aug 09, 2011 at 11:29:51PM -0700, Eric Camachat wrote:
>> Hi,
>>
>> I have a problem to map kernel memory to userspace via /dev/mem.
>> The mmap() succeeded, but when I try to access it, the program will
>> hang forever (until press ctrl-c to terminate it).
>>
>> # memtest-user
>> memtest_vma_open: virt 0x7fbc90085000, phys 0x3eee8000
>> paddr = 0x3eee8000
>> Âmem = 0x7fbc90089000
>> Âmap = 0x7fbc90085000
>> map[0]= 4c4c4c4c
>> map[1]= 4c4c4c4c
>> *** Hang here, it cannot (finish) access the memory mapped via /dev/mem ***
>>
>> My test source below, and it runs properly on HVM, VirtualBox, QEM and
>> physical machines.
>> What mistake I did?
>>
>> My kernel module look like this:
>> =================================================================================
>
> [...snip...]
>
>> memtest_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
>> Â Â Â Â Â Â Â unsigned long arg)
>> {
>> Â Â Â Âint ret = -ENOIOCTLCMD;
>> Â Â Â Âphys_addr_t *paddr;
>> Â Â Â Âunsigned long *vaddr;
>> Â Â Â Âuint32_t *size;
>>
>> Â Â Â Âswitch(cmd) {
>> Â Â Â Âcase MEMTEST_DMA_SIZE:
>> Â Â Â Â Â Â Â Âsize = (uint32_t*)arg;
>> Â Â Â Â Â Â Â Â*size = _size;
>
> Though your output shows that this assignment works, shouldn't this
> kind of direct assignment across kernel space and user land be
> avoided? It is bad practice to do direct assignment I think.
>
> copy_{from,to}_user should do the job.
>

I want share some memory between kernel and user applications, so I am
trying to use mmap instead of copy_{from,to} multiple times.

>> Â Â Â Â Â Â Â Âret = 0;
>> Â Â Â Â Â Â Â Âbreak;
>> Â Â Â Âcase MEMTEST_DMA_PADDR:
>> Â Â Â Â Â Â Â Âpaddr = (phys_addr_t*)arg;
>> Â Â Â Â Â Â Â Â*paddr = _pbase;
>> Â Â Â Â Â Â Â Âret = 0;
>> Â Â Â Â Â Â Â Âbreak;
>> Â Â Â Âcase MEMTEST_DMA_VADDR:
>> Â Â Â Â Â Â Â Âvaddr = (unsigned long*)arg;
>> Â Â Â Â Â Â Â Â*vaddr = _vbase;
>> Â Â Â Â Â Â Â Âret = 0;
>> Â Â Â Â Â Â Â Âbreak;
>> Â Â Â Â}
>> Â Â Â Âreturn ret;
>> }
>>
>>
>
> [...snip...]
>
>> static struct file_operations memtest_fops = {
>>    Â.owner     Â= THIS_MODULE,
>>    Â.llseek     = no_llseek,
>>    Â.ioctl     Â= memtest_ioctl,
>
> My kernel doesn't have field called 'ioctl' in file_operations.
>
> So which kernel do you use? 2.6.18? I don't have old kernel at the
> moment so I can't help you much...

I am using linux-2.5.32.24 kernel.

>
>>    Â.mmap      = memtest_mmap,
>> };
>>
>>
>
> [...snip...]
>
>> static void __exit memtest_exit(void)
>> {
>> Â Â Â Âif (_vbase != 0)
>> Â Â Â Â Â Â Â Âfree_page(_vbase);
>
> I suppose you should use free_pages here, since you use
> __get_free_pages when allocating.
>
>> Â Â Â Âunregister_chrdev(MEMTEST_MAJOR, MEMTEST_NAME);
>> }
>>
>>
>> MODULE_LICENSE("GPL");
>>
>> module_init(memtest_init);
>> module_exit(memtest_exit);
>> =================================================================================
>>
>> Here is my user program:
>>
>> =================================================================================
>>
>
> [...snip...]
>
>> Â Â Â if (map)
>> Â Â Â {
>> Â Â Â Â Â Â Â printf("map[0]= %x\n", map[0]);
>> Â Â Â Â Â Â Â printf("map[1]= %x\n", map[1]);
>
> This confuses me. You did write different values in _vbase[0],
> _vbase[1]. But the output '4C4C4C4C' shows that the value is 'L'.
>
> I just skimmed the output and the code. I don't run your code since I
> don't have a suitible environment at the moment...
>
> Wei.
>
_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-devel

 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.