Thomas Gleixner | 09c434b | 2019-05-19 13:08:20 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * arch/um/drivers/mmapper_kern.c |
| 4 | * |
| 5 | * BRIEF MODULE DESCRIPTION |
| 6 | * |
| 7 | * Copyright (C) 2000 RidgeRun, Inc. |
| 8 | * Author: RidgeRun, Inc. |
| 9 | * Greg Lonnon glonnon@ridgerun.com or info@ridgerun.com |
| 10 | * |
| 11 | */ |
| 12 | |
Jeff Dike | cb8fa61 | 2007-10-16 01:27:34 -0700 | [diff] [blame] | 13 | #include <linux/stddef.h> |
| 14 | #include <linux/types.h> |
Alexey Dobriyan | 4e950f6 | 2007-07-30 02:36:13 +0400 | [diff] [blame] | 15 | #include <linux/fs.h> |
Jeff Dike | cb8fa61 | 2007-10-16 01:27:34 -0700 | [diff] [blame] | 16 | #include <linux/init.h> |
Jeff Dike | 3df5952 | 2005-06-08 15:47:50 -0700 | [diff] [blame] | 17 | #include <linux/miscdevice.h> |
Jeff Dike | cb8fa61 | 2007-10-16 01:27:34 -0700 | [diff] [blame] | 18 | #include <linux/module.h> |
| 19 | #include <linux/mm.h> |
Thomas Gleixner | d63c489 | 2009-10-10 15:36:34 +0000 | [diff] [blame] | 20 | |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 21 | #include <linux/uaccess.h> |
Al Viro | 37185b3 | 2012-10-08 03:27:32 +0100 | [diff] [blame] | 22 | #include <mem_user.h> |
Jeff Dike | cb8fa61 | 2007-10-16 01:27:34 -0700 | [diff] [blame] | 23 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | /* These are set in mmapper_init, which is called at boot time */ |
| 25 | static unsigned long mmapper_size; |
Jeff Dike | cb8fa61 | 2007-10-16 01:27:34 -0700 | [diff] [blame] | 26 | static unsigned long p_buf; |
| 27 | static char *v_buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
Jeff Dike | cb8fa61 | 2007-10-16 01:27:34 -0700 | [diff] [blame] | 29 | static ssize_t mmapper_read(struct file *file, char __user *buf, size_t count, |
| 30 | loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | { |
Al Viro | 6a029a9 | 2005-08-27 06:48:15 +0100 | [diff] [blame] | 32 | return simple_read_from_buffer(buf, count, ppos, v_buf, mmapper_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | } |
| 34 | |
Jeff Dike | cb8fa61 | 2007-10-16 01:27:34 -0700 | [diff] [blame] | 35 | static ssize_t mmapper_write(struct file *file, const char __user *buf, |
| 36 | size_t count, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | { |
Al Viro | 6a029a9 | 2005-08-27 06:48:15 +0100 | [diff] [blame] | 38 | if (*ppos > mmapper_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | return -EINVAL; |
| 40 | |
Akinobu Mita | 0388fae | 2011-01-12 16:59:28 -0800 | [diff] [blame] | 41 | return simple_write_to_buffer(v_buf, mmapper_size, ppos, buf, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | } |
| 43 | |
Arnd Bergmann | d5f1d54 | 2010-04-27 16:24:23 +0200 | [diff] [blame] | 44 | static long mmapper_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | { |
Jeff Dike | cb8fa61 | 2007-10-16 01:27:34 -0700 | [diff] [blame] | 46 | return -ENOIOCTLCMD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Jeff Dike | cb8fa61 | 2007-10-16 01:27:34 -0700 | [diff] [blame] | 49 | static int mmapper_mmap(struct file *file, struct vm_area_struct *vma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | { |
| 51 | int ret = -EINVAL; |
| 52 | int size; |
| 53 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | if (vma->vm_pgoff != 0) |
| 55 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
Jeff Dike | cb8fa61 | 2007-10-16 01:27:34 -0700 | [diff] [blame] | 57 | size = vma->vm_end - vma->vm_start; |
| 58 | if (size > mmapper_size) |
| 59 | return -EFAULT; |
| 60 | |
| 61 | /* |
| 62 | * XXX A comment above remap_pfn_range says it should only be |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | * called when the mm semaphore is held |
| 64 | */ |
| 65 | if (remap_pfn_range(vma, vma->vm_start, p_buf >> PAGE_SHIFT, size, |
Jeff Dike | cb8fa61 | 2007-10-16 01:27:34 -0700 | [diff] [blame] | 66 | vma->vm_page_prot)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | goto out; |
| 68 | ret = 0; |
| 69 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | return ret; |
| 71 | } |
| 72 | |
Jeff Dike | cb8fa61 | 2007-10-16 01:27:34 -0700 | [diff] [blame] | 73 | static int mmapper_open(struct inode *inode, struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | { |
| 75 | return 0; |
| 76 | } |
| 77 | |
Jeff Dike | cb8fa61 | 2007-10-16 01:27:34 -0700 | [diff] [blame] | 78 | static int mmapper_release(struct inode *inode, struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | { |
| 80 | return 0; |
| 81 | } |
| 82 | |
Jeff Dike | 5e7672e | 2006-09-27 01:50:33 -0700 | [diff] [blame] | 83 | static const struct file_operations mmapper_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | .owner = THIS_MODULE, |
| 85 | .read = mmapper_read, |
| 86 | .write = mmapper_write, |
Arnd Bergmann | d5f1d54 | 2010-04-27 16:24:23 +0200 | [diff] [blame] | 87 | .unlocked_ioctl = mmapper_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | .mmap = mmapper_mmap, |
| 89 | .open = mmapper_open, |
| 90 | .release = mmapper_release, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 91 | .llseek = default_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | }; |
| 93 | |
Jeff Dike | cb8fa61 | 2007-10-16 01:27:34 -0700 | [diff] [blame] | 94 | /* |
| 95 | * No locking needed - only used (and modified) by below initcall and exitcall. |
| 96 | */ |
Paolo 'Blaisorblade' Giarrusso | 1ba0ce6 | 2006-10-19 23:28:26 -0700 | [diff] [blame] | 97 | static struct miscdevice mmapper_dev = { |
Jeff Dike | 3df5952 | 2005-06-08 15:47:50 -0700 | [diff] [blame] | 98 | .minor = MISC_DYNAMIC_MINOR, |
| 99 | .name = "mmapper", |
| 100 | .fops = &mmapper_fops |
| 101 | }; |
| 102 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | static int __init mmapper_init(void) |
| 104 | { |
Jeff Dike | 3df5952 | 2005-06-08 15:47:50 -0700 | [diff] [blame] | 105 | int err; |
| 106 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | printk(KERN_INFO "Mapper v0.1\n"); |
| 108 | |
| 109 | v_buf = (char *) find_iomem("mmapper", &mmapper_size); |
Jeff Dike | cb8fa61 | 2007-10-16 01:27:34 -0700 | [diff] [blame] | 110 | if (mmapper_size == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | printk(KERN_ERR "mmapper_init - find_iomem failed\n"); |
Thomas Gleixner | d63c489 | 2009-10-10 15:36:34 +0000 | [diff] [blame] | 112 | return -ENODEV; |
Jeff Dike | 3df5952 | 2005-06-08 15:47:50 -0700 | [diff] [blame] | 113 | } |
Thomas Gleixner | d63c489 | 2009-10-10 15:36:34 +0000 | [diff] [blame] | 114 | p_buf = __pa(v_buf); |
Jeff Dike | 3df5952 | 2005-06-08 15:47:50 -0700 | [diff] [blame] | 115 | |
| 116 | err = misc_register(&mmapper_dev); |
Jeff Dike | cb8fa61 | 2007-10-16 01:27:34 -0700 | [diff] [blame] | 117 | if (err) { |
Jeff Dike | 3df5952 | 2005-06-08 15:47:50 -0700 | [diff] [blame] | 118 | printk(KERN_ERR "mmapper - misc_register failed, err = %d\n", |
| 119 | err); |
Justin P. Mattock | 6eab04a | 2011-04-08 19:49:08 -0700 | [diff] [blame] | 120 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | } |
Jeff Dike | 3df5952 | 2005-06-08 15:47:50 -0700 | [diff] [blame] | 122 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | static void mmapper_exit(void) |
| 126 | { |
Jeff Dike | 3df5952 | 2005-06-08 15:47:50 -0700 | [diff] [blame] | 127 | misc_deregister(&mmapper_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | module_init(mmapper_init); |
| 131 | module_exit(mmapper_exit); |
| 132 | |
| 133 | MODULE_AUTHOR("Greg Lonnon <glonnon@ridgerun.com>"); |
| 134 | MODULE_DESCRIPTION("DSPLinux simulator mmapper driver"); |
Randy Dunlap | 07ecb79 | 2011-01-12 16:59:27 -0800 | [diff] [blame] | 135 | MODULE_LICENSE("GPL"); |