ARM: 7409/1: Do not call flush_cache_user_range with mmap_sem held
[linux/fpc-iii.git] / drivers / s390 / char / vmcp.c
blob84e569c9c150cb2e37ef5f10a539e7ba7838e4cb
1 /*
2 * Copyright IBM Corp. 2004,2010
3 * Interface implementation for communication with the z/VM control program
5 * Author(s): Christian Borntraeger <borntraeger@de.ibm.com>
7 * z/VMs CP offers the possibility to issue commands via the diagnose code 8
8 * this driver implements a character device that issues these commands and
9 * returns the answer of CP.
11 * The idea of this driver is based on cpint from Neale Ferguson and #CP in CMS
14 #include <linux/fs.h>
15 #include <linux/init.h>
16 #include <linux/compat.h>
17 #include <linux/kernel.h>
18 #include <linux/miscdevice.h>
19 #include <linux/slab.h>
20 #include <asm/compat.h>
21 #include <asm/cpcmd.h>
22 #include <asm/debug.h>
23 #include <asm/uaccess.h>
24 #include "vmcp.h"
26 static debug_info_t *vmcp_debug;
28 static int vmcp_open(struct inode *inode, struct file *file)
30 struct vmcp_session *session;
32 if (!capable(CAP_SYS_ADMIN))
33 return -EPERM;
35 session = kmalloc(sizeof(*session), GFP_KERNEL);
36 if (!session)
37 return -ENOMEM;
39 session->bufsize = PAGE_SIZE;
40 session->response = NULL;
41 session->resp_size = 0;
42 mutex_init(&session->mutex);
43 file->private_data = session;
44 return nonseekable_open(inode, file);
47 static int vmcp_release(struct inode *inode, struct file *file)
49 struct vmcp_session *session;
51 session = file->private_data;
52 file->private_data = NULL;
53 free_pages((unsigned long)session->response, get_order(session->bufsize));
54 kfree(session);
55 return 0;
58 static ssize_t
59 vmcp_read(struct file *file, char __user *buff, size_t count, loff_t *ppos)
61 ssize_t ret;
62 size_t size;
63 struct vmcp_session *session;
65 session = file->private_data;
66 if (mutex_lock_interruptible(&session->mutex))
67 return -ERESTARTSYS;
68 if (!session->response) {
69 mutex_unlock(&session->mutex);
70 return 0;
72 size = min_t(size_t, session->resp_size, session->bufsize);
73 ret = simple_read_from_buffer(buff, count, ppos,
74 session->response, size);
76 mutex_unlock(&session->mutex);
78 return ret;
81 static ssize_t
82 vmcp_write(struct file *file, const char __user *buff, size_t count,
83 loff_t *ppos)
85 char *cmd;
86 struct vmcp_session *session;
88 if (count > 240)
89 return -EINVAL;
90 cmd = kmalloc(count + 1, GFP_KERNEL);
91 if (!cmd)
92 return -ENOMEM;
93 if (copy_from_user(cmd, buff, count)) {
94 kfree(cmd);
95 return -EFAULT;
97 cmd[count] = '\0';
98 session = file->private_data;
99 if (mutex_lock_interruptible(&session->mutex)) {
100 kfree(cmd);
101 return -ERESTARTSYS;
103 if (!session->response)
104 session->response = (char *)__get_free_pages(GFP_KERNEL
105 | __GFP_REPEAT | GFP_DMA,
106 get_order(session->bufsize));
107 if (!session->response) {
108 mutex_unlock(&session->mutex);
109 kfree(cmd);
110 return -ENOMEM;
112 debug_text_event(vmcp_debug, 1, cmd);
113 session->resp_size = cpcmd(cmd, session->response, session->bufsize,
114 &session->resp_code);
115 mutex_unlock(&session->mutex);
116 kfree(cmd);
117 *ppos = 0; /* reset the file pointer after a command */
118 return count;
123 * These ioctls are available, as the semantics of the diagnose 8 call
124 * does not fit very well into a Linux call. Diagnose X'08' is described in
125 * CP Programming Services SC24-6084-00
127 * VMCP_GETCODE: gives the CP return code back to user space
128 * VMCP_SETBUF: sets the response buffer for the next write call. diagnose 8
129 * expects adjacent pages in real storage and to make matters worse, we
130 * dont know the size of the response. Therefore we default to PAGESIZE and
131 * let userspace to change the response size, if userspace expects a bigger
132 * response
134 static long vmcp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
136 struct vmcp_session *session;
137 int __user *argp;
138 int temp;
140 session = file->private_data;
141 if (is_compat_task())
142 argp = compat_ptr(arg);
143 else
144 argp = (int __user *)arg;
145 if (mutex_lock_interruptible(&session->mutex))
146 return -ERESTARTSYS;
147 switch (cmd) {
148 case VMCP_GETCODE:
149 temp = session->resp_code;
150 mutex_unlock(&session->mutex);
151 return put_user(temp, argp);
152 case VMCP_SETBUF:
153 free_pages((unsigned long)session->response,
154 get_order(session->bufsize));
155 session->response=NULL;
156 temp = get_user(session->bufsize, argp);
157 if (get_order(session->bufsize) > 8) {
158 session->bufsize = PAGE_SIZE;
159 temp = -EINVAL;
161 mutex_unlock(&session->mutex);
162 return temp;
163 case VMCP_GETSIZE:
164 temp = session->resp_size;
165 mutex_unlock(&session->mutex);
166 return put_user(temp, argp);
167 default:
168 mutex_unlock(&session->mutex);
169 return -ENOIOCTLCMD;
173 static const struct file_operations vmcp_fops = {
174 .owner = THIS_MODULE,
175 .open = vmcp_open,
176 .release = vmcp_release,
177 .read = vmcp_read,
178 .write = vmcp_write,
179 .unlocked_ioctl = vmcp_ioctl,
180 .compat_ioctl = vmcp_ioctl,
181 .llseek = no_llseek,
184 static struct miscdevice vmcp_dev = {
185 .name = "vmcp",
186 .minor = MISC_DYNAMIC_MINOR,
187 .fops = &vmcp_fops,
190 static int __init vmcp_init(void)
192 int ret;
194 if (!MACHINE_IS_VM)
195 return 0;
197 vmcp_debug = debug_register("vmcp", 1, 1, 240);
198 if (!vmcp_debug)
199 return -ENOMEM;
201 ret = debug_register_view(vmcp_debug, &debug_hex_ascii_view);
202 if (ret) {
203 debug_unregister(vmcp_debug);
204 return ret;
207 ret = misc_register(&vmcp_dev);
208 if (ret)
209 debug_unregister(vmcp_debug);
210 return ret;
212 device_initcall(vmcp_init);