1 /* -*- C -*-
2 * mmap.c -- memory mapping for the scullv char module
3 *
4 * Copyright (C) 2001 Alessandro Rubini and Jonathan Corbet
5 * Copyright (C) 2001 O'Reilly & Associates
6 *
7 * The source code in this file can be freely used, adapted,
8 * and redistributed in source or binary form, so long as an
9 * acknowledgment appears in derived source files. The citation
10 * should list that the code comes from the book "Linux Device
11 * Drivers" by Alessandro Rubini and Jonathan Corbet, published
12 * by O'Reilly & Associates. No warranty is attached;
13 * we cannot take responsibility for errors or fitness for use.
14 *
15 * $Id: _mmap.c.in,v 1.13 2004/10/18 18:07:36 corbet Exp $
16 */
17
18 #include <linux/config.h>
19 #include <linux/module.h>
20
21 #include <linux/mm.h> /* everything */
22 #include <linux/errno.h> /* error codes */
23 #include <asm/pgtable.h>
24
25 #include "scullv.h" /* local definitions */
26
27
28 /*
29 * open and close: just keep track of how many times the device is
30 * mapped, to avoid releasing it.
31 */
32
33 void scullv_vma_open(struct vm_area_struct *vma)
34 {
35 struct scullv_dev *dev = vma->vm_private_data;
36
37 dev->vmas++;
38 }
39
40 void scullv_vma_close(struct vm_area_struct *vma)
41 {
42 struct scullv_dev *dev = vma->vm_private_data;
43
44 dev->vmas--;
45 }
46
47 /*
48 * The nopage method: the core of the file. It retrieves the
49 * page required from the scullv device and returns it to the
50 * user. The count for the page must be incremented, because
51 * it is automatically decremented at page unmap.
52 *
53 * For this reason, "order" must be zero. Otherwise, only the first
54 * page has its count incremented, and the allocating module must
55 * release it as a whole block. Therefore, it isn't possible to map
56 * pages from a multipage block: when they are unmapped, their count
57 * is individually decreased, and would drop to 0.
58 */
59
60 struct page *scullv_vma_nopage(struct vm_area_struct *vma,
61 unsigned long address, int *type)
62 {
63 unsigned long offset;
64 struct scullv_dev *ptr, *dev = vma->vm_private_data;
65 struct page *page = NOPAGE_SIGBUS;
66 void *pageptr = NULL; /* default to "missing" */
67
68 down(&dev->sem);
69 offset = (address - vma->vm_start) + (vma->vm_pgoff << PAGE_SHIFT);
70 if (offset >= dev->size) goto out; /* out of range */
71
72 /*
73 * Now retrieve the scullv device from the list,then the page.
74 * If the device has holes, the process receives a SIGBUS when
75 * accessing the hole.
76 */
77 offset >>= PAGE_SHIFT; /* offset is a number of pages */
78 for (ptr = dev; ptr && offset >= dev->qset;) {
79 ptr = ptr->next;
80 offset -= dev->qset;
81 }
82 if (ptr && ptr->data) pageptr = ptr->data[offset];
83 if (!pageptr) goto out; /* hole or end-of-file */
84
85 /*
86 * After scullv lookup, "page" is now the address of the page
87 * needed by the current process. Since it's a vmalloc address,
88 * turn it into a struct page.
89 */
90 page = vmalloc_to_page(pageptr);
91
92 /* got it, now increment the count */
93 get_page(page);
94 if (type)
95 *type = VM_FAULT_MINOR;
96 out:
97 up(&dev->sem);
98 return page;
99 }
100
101
102
103 struct vm_operations_struct scullv_vm_ops = {
104 .open = scullv_vma_open,
105 .close = scullv_vma_close,
106 .nopage = scullv_vma_nopage,
107 };
108
109
110 int scullv_mmap(struct file *filp, struct vm_area_struct *vma)
111 {
112
113 /* don't do anything here: "nopage" will set up page table entries */
114 vma->vm_ops = &scullv_vm_ops;
115 vma->vm_flags |= VM_RESERVED;
116 vma->vm_private_data = filp->private_data;
117 scullv_vma_open(vma);
118 return 0;
119 }
120
121
|
This page was automatically generated by the
LXR engine.
|