You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

mapper.c 7.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*********************************************************************/
  2. /* Copyright 2009, 2010 The University of Texas at Austin. */
  3. /* All rights reserved. */
  4. /* */
  5. /* Redistribution and use in source and binary forms, with or */
  6. /* without modification, are permitted provided that the following */
  7. /* conditions are met: */
  8. /* */
  9. /* 1. Redistributions of source code must retain the above */
  10. /* copyright notice, this list of conditions and the following */
  11. /* disclaimer. */
  12. /* */
  13. /* 2. Redistributions in binary form must reproduce the above */
  14. /* copyright notice, this list of conditions and the following */
  15. /* disclaimer in the documentation and/or other materials */
  16. /* provided with the distribution. */
  17. /* */
  18. /* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY OF TEXAS AT */
  19. /* AUSTIN ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, */
  20. /* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */
  21. /* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
  22. /* DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OF TEXAS AT */
  23. /* AUSTIN OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, */
  24. /* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
  25. /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE */
  26. /* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR */
  27. /* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF */
  28. /* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
  29. /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT */
  30. /* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE */
  31. /* POSSIBILITY OF SUCH DAMAGE. */
  32. /* */
  33. /* The views and conclusions contained in the software and */
  34. /* documentation are those of the authors and should not be */
  35. /* interpreted as representing official policies, either expressed */
  36. /* or implied, of The University of Texas at Austin. */
  37. /*********************************************************************/
  38. #include <linux/version.h>
  39. #include <linux/module.h>
  40. #include <linux/sched.h>
  41. #include <asm/pgtable.h>
  42. #include <linux/init.h>
  43. #include <linux/fs.h>
  44. #include <linux/cdev.h>
  45. #include <linux/spinlock.h>
  46. #include <linux/mm.h>
  47. #ifdef CONFIG_BIGPHYS_AREA
  48. #include <linux/bigphysarea.h>
  49. #endif
  50. #include <asm/current.h>
  51. #ifdef MODVERSIONS
  52. #include <linux/modversions.h>
  53. #endif
  54. #include <asm/io.h>
  55. typedef struct {
  56. pid_t pid;
  57. #ifndef CONFIG_BIGPHYS_AREA
  58. long size;
  59. #endif
  60. caddr_t address;
  61. } buffer_t;
  62. #define MAX_BUFF_SIZE 1024
  63. #define MAX_LENGTH (4UL << 20)
  64. static spinlock_t lock __attribute__((aligned(64)));
  65. static buffer_t buffer[MAX_BUFF_SIZE];
  66. static dev_t mapper_dev;
  67. static struct cdev mapper_cdev;
  68. static int mapper_open (struct inode *inode, struct file *fp){ return 0;}
  69. static int mapper_release(struct inode *inode, struct file *fp){
  70. int pos;
  71. #ifndef CONFIG_BIGPHYS_AREA
  72. caddr_t addr;
  73. #endif
  74. // printk("Releasing memory... %d\n", current -> tgid);
  75. spin_lock(&lock);
  76. for (pos = 0; pos < MAX_BUFF_SIZE; pos ++) {
  77. if (buffer[pos].pid == (pid_t) current -> tgid) {
  78. #ifdef CONFIG_BIGPHYS_AREA
  79. bigphysarea_free_pages(buffer[pos].address);
  80. #else
  81. for (addr = buffer[pos].address; addr < buffer[pos].address + buffer[pos].size; addr += PAGE_SIZE) {
  82. ClearPageReserved(virt_to_page(addr));
  83. }
  84. kfree(buffer[pos].address);
  85. buffer[pos].size = 0;
  86. #endif
  87. buffer[pos].pid = 0;
  88. buffer[pos].address = 0;
  89. }
  90. }
  91. spin_unlock(&lock);
  92. return 0;
  93. }
  94. int mapper_mapper(struct file *fp, struct vm_area_struct *vma){
  95. int ret, pos;
  96. caddr_t alloc_addr;
  97. #ifndef CONFIG_BIGPHYS_AREA
  98. caddr_t addr;
  99. #endif
  100. long all_length, length, current_addr;
  101. all_length = vma->vm_end - vma->vm_start;
  102. current_addr = vma -> vm_start;
  103. spin_lock(&lock);
  104. while (all_length > 0) {
  105. length = all_length;
  106. if (length > MAX_LENGTH) length = MAX_LENGTH;
  107. all_length -= MAX_LENGTH;
  108. // printk("Allocating memory... %d\n", length);
  109. pos = 0;
  110. while ((pos < MAX_BUFF_SIZE) && (buffer[pos].address != 0)) pos ++;
  111. if (pos >= MAX_BUFF_SIZE) {
  112. printk("Memory Allocator : too much memory allocation requested.\n");
  113. spin_unlock(&lock);
  114. return -EIO;
  115. }
  116. #ifdef CONFIG_BIGPHYS_AREA
  117. alloc_addr = (caddr_t)bigphysarea_alloc_pages(length >> PAGE_SHIFT, 1, GFP_KERNEL);
  118. #else
  119. alloc_addr = (caddr_t)kmalloc(length, GFP_KERNEL);
  120. #endif
  121. if (alloc_addr == (caddr_t)NULL) {
  122. spin_unlock(&lock);
  123. return -EIO;
  124. }
  125. #ifndef CONFIG_BIGPHYS_AREA
  126. for (addr = alloc_addr; addr < alloc_addr + length; addr += PAGE_SIZE) {
  127. clear_page(addr);
  128. SetPageReserved(virt_to_page(addr));
  129. }
  130. #endif
  131. if ((ret = remap_pfn_range(vma,
  132. current_addr,
  133. virt_to_phys((void *)alloc_addr) >> PAGE_SHIFT,
  134. length,
  135. PAGE_SHARED)) < 0) {
  136. #ifdef CONFIG_BIGPHYS_AREA
  137. bigphysarea_free_pages((caddr_t)alloc_addr);
  138. #else
  139. for (addr = alloc_addr; addr < alloc_addr + length; addr += PAGE_SIZE) ClearPageReserved(virt_to_page(addr));
  140. kfree((caddr_t)alloc_addr);
  141. #endif
  142. spin_unlock(&lock);
  143. return ret;
  144. }
  145. buffer[pos].pid = current -> tgid;
  146. buffer[pos].address = alloc_addr;
  147. #ifndef CONFIG_BIGPHYS_AREA
  148. buffer[pos].size = length;
  149. #endif
  150. current_addr += length;
  151. }
  152. spin_unlock(&lock);
  153. return 0;
  154. }
  155. static struct file_operations mapper_fops = {
  156. .open = mapper_open,
  157. .release = mapper_release,
  158. .mmap = mapper_mapper,
  159. .owner = THIS_MODULE,
  160. };
  161. static int __init mapper_init(void){
  162. int ret, i;
  163. ret = alloc_chrdev_region(&mapper_dev, 0, 1, "mapper");
  164. cdev_init(&mapper_cdev, &mapper_fops);
  165. ret = cdev_add(&mapper_cdev, mapper_dev, 1);
  166. spin_lock_init(&lock);
  167. for (i = 0; i < MAX_BUFF_SIZE; i++) {
  168. buffer[i].pid = 0;
  169. #ifndef CONFIG_BIGPHYS_AREA
  170. buffer[i].size = 0;
  171. #endif
  172. buffer[i].address = 0;
  173. }
  174. return ret;
  175. }
  176. static void __exit mapper_exit(void){
  177. int pos;
  178. for (pos = 0; pos < MAX_BUFF_SIZE; pos ++) {
  179. if (buffer[pos].address != 0) {
  180. #ifdef CONFIG_BIGPHYS_AREA
  181. bigphysarea_free_pages(buffer[pos].address);
  182. #else
  183. kfree(buffer[pos].address);
  184. #endif
  185. }
  186. }
  187. cdev_del(&mapper_cdev);
  188. unregister_chrdev_region(mapper_dev, 1);
  189. }
  190. module_init(mapper_init);
  191. module_exit(mapper_exit);
  192. MODULE_DESCRIPTION("BigPhysArea User Mapping Driver");
  193. MODULE_LICENSE("Unknown");