xref: /linux/drivers/s390/char/vmcp.c (revision 375d5d57545f53d2bc3e15ada0e9da360fc68bea)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright IBM Corp. 2004, 2010
4  * Interface implementation for communication with the z/VM control program
5  *
6  * Author(s): Christian Borntraeger <borntraeger@de.ibm.com>
7  *
8  * z/VMs CP offers the possibility to issue commands via the diagnose code 8
9  * this driver implements a character device that issues these commands and
10  * returns the answer of CP.
11  *
12  * The idea of this driver is based on cpint from Neale Ferguson and #CP in CMS
13  */
14 
15 #include <linux/fs.h>
16 #include <linux/init.h>
17 #include <linux/kernel.h>
18 #include <linux/miscdevice.h>
19 #include <linux/slab.h>
20 #include <linux/uaccess.h>
21 #include <linux/mutex.h>
22 #include <linux/cma.h>
23 #include <linux/mm.h>
24 #include <asm/machine.h>
25 #include <asm/cpcmd.h>
26 #include <asm/debug.h>
27 #include <asm/vmcp.h>
28 
29 struct vmcp_session {
30 	char *response;
31 	unsigned int bufsize;
32 	unsigned int cma_alloc : 1;
33 	int resp_size;
34 	int resp_code;
35 	struct mutex mutex;
36 };
37 
38 static debug_info_t *vmcp_debug;
39 
40 static unsigned long vmcp_cma_size __initdata = CONFIG_VMCP_CMA_SIZE * 1024 * 1024;
41 static struct cma *vmcp_cma;
42 
early_parse_vmcp_cma(char * p)43 static int __init early_parse_vmcp_cma(char *p)
44 {
45 	if (!p)
46 		return 1;
47 	vmcp_cma_size = ALIGN(memparse(p, NULL), PAGE_SIZE);
48 	return 0;
49 }
50 early_param("vmcp_cma", early_parse_vmcp_cma);
51 
vmcp_cma_reserve(void)52 void __init vmcp_cma_reserve(void)
53 {
54 	if (!machine_is_vm())
55 		return;
56 	cma_declare_contiguous(0, vmcp_cma_size, 0, 0, 0, false, "vmcp", &vmcp_cma);
57 }
58 
vmcp_response_alloc(struct vmcp_session * session)59 static void vmcp_response_alloc(struct vmcp_session *session)
60 {
61 	struct page *page = NULL;
62 	int nr_pages, order;
63 
64 	order = get_order(session->bufsize);
65 	nr_pages = ALIGN(session->bufsize, PAGE_SIZE) >> PAGE_SHIFT;
66 	/*
67 	 * For anything below order 3 allocations rely on the buddy
68 	 * allocator. If such low-order allocations can't be handled
69 	 * anymore the system won't work anyway.
70 	 */
71 	if (order > 2)
72 		page = cma_alloc(vmcp_cma, nr_pages, 0, false);
73 	if (page) {
74 		session->response = (char *)page_to_virt(page);
75 		session->cma_alloc = 1;
76 		return;
77 	}
78 	session->response = (char *)__get_free_pages(GFP_KERNEL | __GFP_RETRY_MAYFAIL, order);
79 }
80 
vmcp_response_free(struct vmcp_session * session)81 static void vmcp_response_free(struct vmcp_session *session)
82 {
83 	int nr_pages, order;
84 	struct page *page;
85 
86 	if (!session->response)
87 		return;
88 	order = get_order(session->bufsize);
89 	nr_pages = ALIGN(session->bufsize, PAGE_SIZE) >> PAGE_SHIFT;
90 	if (session->cma_alloc) {
91 		page = virt_to_page(session->response);
92 		cma_release(vmcp_cma, page, nr_pages);
93 		session->cma_alloc = 0;
94 	} else {
95 		free_pages((unsigned long)session->response, order);
96 	}
97 	session->response = NULL;
98 }
99 
vmcp_open(struct inode * inode,struct file * file)100 static int vmcp_open(struct inode *inode, struct file *file)
101 {
102 	struct vmcp_session *session;
103 
104 	if (!capable(CAP_SYS_ADMIN))
105 		return -EPERM;
106 
107 	session = kzalloc_obj(*session);
108 	if (!session)
109 		return -ENOMEM;
110 
111 	session->bufsize = PAGE_SIZE;
112 	mutex_init(&session->mutex);
113 	file->private_data = session;
114 	return nonseekable_open(inode, file);
115 }
116 
vmcp_release(struct inode * inode,struct file * file)117 static int vmcp_release(struct inode *inode, struct file *file)
118 {
119 	struct vmcp_session *session;
120 
121 	session = file->private_data;
122 	file->private_data = NULL;
123 	vmcp_response_free(session);
124 	kfree(session);
125 	return 0;
126 }
127 
128 static ssize_t
vmcp_read(struct file * file,char __user * buff,size_t count,loff_t * ppos)129 vmcp_read(struct file *file, char __user *buff, size_t count, loff_t *ppos)
130 {
131 	ssize_t ret;
132 	size_t size;
133 	struct vmcp_session *session;
134 
135 	session = file->private_data;
136 	if (mutex_lock_interruptible(&session->mutex))
137 		return -ERESTARTSYS;
138 	if (!session->response) {
139 		mutex_unlock(&session->mutex);
140 		return 0;
141 	}
142 	size = min_t(size_t, session->resp_size, session->bufsize);
143 	ret = simple_read_from_buffer(buff, count, ppos,
144 					session->response, size);
145 
146 	mutex_unlock(&session->mutex);
147 
148 	return ret;
149 }
150 
151 static ssize_t
vmcp_write(struct file * file,const char __user * buff,size_t count,loff_t * ppos)152 vmcp_write(struct file *file, const char __user *buff, size_t count,
153 	   loff_t *ppos)
154 {
155 	char *cmd;
156 	struct vmcp_session *session;
157 
158 	if (count > 240)
159 		return -EINVAL;
160 	cmd = memdup_user_nul(buff, count);
161 	if (IS_ERR(cmd))
162 		return PTR_ERR(cmd);
163 	session = file->private_data;
164 	if (mutex_lock_interruptible(&session->mutex)) {
165 		kfree(cmd);
166 		return -ERESTARTSYS;
167 	}
168 	if (!session->response)
169 		vmcp_response_alloc(session);
170 	if (!session->response) {
171 		mutex_unlock(&session->mutex);
172 		kfree(cmd);
173 		return -ENOMEM;
174 	}
175 	debug_text_event(vmcp_debug, 1, cmd);
176 	session->resp_size = cpcmd(cmd, session->response, session->bufsize,
177 				   &session->resp_code);
178 	mutex_unlock(&session->mutex);
179 	kfree(cmd);
180 	*ppos = 0;		/* reset the file pointer after a command */
181 	return count;
182 }
183 
184 
185 /*
186  * These ioctls are available, as the semantics of the diagnose 8 call
187  * does not fit very well into a Linux call. Diagnose X'08' is described in
188  * CP Programming Services SC24-6084-00
189  *
190  * VMCP_GETCODE: gives the CP return code back to user space
191  * VMCP_SETBUF: sets the response buffer for the next write call. diagnose 8
192  * expects adjacent pages in real storage and to make matters worse, we
193  * dont know the size of the response. Therefore we default to PAGESIZE and
194  * let userspace to change the response size, if userspace expects a bigger
195  * response
196  */
vmcp_ioctl(struct file * file,unsigned int cmd,unsigned long arg)197 static long vmcp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
198 {
199 	struct vmcp_session *session;
200 	int ret = -ENOTTY;
201 	int __user *argp;
202 
203 	session = file->private_data;
204 	argp = (int __user *)arg;
205 	if (mutex_lock_interruptible(&session->mutex))
206 		return -ERESTARTSYS;
207 	switch (cmd) {
208 	case VMCP_GETCODE:
209 		ret = put_user(session->resp_code, argp);
210 		break;
211 	case VMCP_SETBUF:
212 		vmcp_response_free(session);
213 		ret = get_user(session->bufsize, argp);
214 		if (ret)
215 			session->bufsize = PAGE_SIZE;
216 		if (!session->bufsize || get_order(session->bufsize) > 8) {
217 			session->bufsize = PAGE_SIZE;
218 			ret = -EINVAL;
219 		}
220 		break;
221 	case VMCP_GETSIZE:
222 		ret = put_user(session->resp_size, argp);
223 		break;
224 	default:
225 		break;
226 	}
227 	mutex_unlock(&session->mutex);
228 	return ret;
229 }
230 
231 static const struct file_operations vmcp_fops = {
232 	.owner		= THIS_MODULE,
233 	.open		= vmcp_open,
234 	.release	= vmcp_release,
235 	.read		= vmcp_read,
236 	.write		= vmcp_write,
237 	.unlocked_ioctl	= vmcp_ioctl,
238 };
239 
240 static struct miscdevice vmcp_dev = {
241 	.name	= "vmcp",
242 	.minor	= MISC_DYNAMIC_MINOR,
243 	.fops	= &vmcp_fops,
244 };
245 
vmcp_init(void)246 static int __init vmcp_init(void)
247 {
248 	int ret;
249 
250 	if (!machine_is_vm())
251 		return 0;
252 
253 	vmcp_debug = debug_register("vmcp", 1, 1, 240);
254 	if (!vmcp_debug)
255 		return -ENOMEM;
256 
257 	ret = debug_register_view(vmcp_debug, &debug_hex_ascii_view);
258 	if (ret) {
259 		debug_unregister(vmcp_debug);
260 		return ret;
261 	}
262 
263 	ret = misc_register(&vmcp_dev);
264 	if (ret)
265 		debug_unregister(vmcp_debug);
266 	return ret;
267 }
268 device_initcall(vmcp_init);
269