1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright IBM Corp. 2004, 2010
4 * Interface implementation for communication with the z/VM control program
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.
12 * The idea of this driver is based on cpint from Neale Ferguson and #CP in CMS
16 #include <linux/init.h>
17 #include <linux/compat.h>
18 #include <linux/kernel.h>
19 #include <linux/miscdevice.h>
20 #include <linux/slab.h>
21 #include <linux/uaccess.h>
22 #include <linux/export.h>
23 #include <linux/mutex.h>
24 #include <linux/cma.h>
26 #include <asm/cpcmd.h>
27 #include <asm/debug.h>
33 unsigned int cma_alloc : 1;
39 static debug_info_t *vmcp_debug;
41 static unsigned long vmcp_cma_size __initdata = CONFIG_VMCP_CMA_SIZE * 1024 * 1024;
42 static struct cma *vmcp_cma;
44 static int __init early_parse_vmcp_cma(char *p)
48 vmcp_cma_size = ALIGN(memparse(p, NULL), PAGE_SIZE);
51 early_param("vmcp_cma", early_parse_vmcp_cma);
53 void __init vmcp_cma_reserve(void)
57 cma_declare_contiguous(0, vmcp_cma_size, 0, 0, 0, false, "vmcp", &vmcp_cma);
60 static void vmcp_response_alloc(struct vmcp_session *session)
62 struct page *page = NULL;
65 order = get_order(session->bufsize);
66 nr_pages = ALIGN(session->bufsize, PAGE_SIZE) >> PAGE_SHIFT;
68 * For anything below order 3 allocations rely on the buddy
69 * allocator. If such low-order allocations can't be handled
70 * anymore the system won't work anyway.
73 page = cma_alloc(vmcp_cma, nr_pages, 0, false);
75 session->response = (char *)page_to_phys(page);
76 session->cma_alloc = 1;
79 session->response = (char *)__get_free_pages(GFP_KERNEL | __GFP_RETRY_MAYFAIL, order);
82 static void vmcp_response_free(struct vmcp_session *session)
87 if (!session->response)
89 order = get_order(session->bufsize);
90 nr_pages = ALIGN(session->bufsize, PAGE_SIZE) >> PAGE_SHIFT;
91 if (session->cma_alloc) {
92 page = phys_to_page((unsigned long)session->response);
93 cma_release(vmcp_cma, page, nr_pages);
94 session->cma_alloc = 0;
96 free_pages((unsigned long)session->response, order);
98 session->response = NULL;
101 static int vmcp_open(struct inode *inode, struct file *file)
103 struct vmcp_session *session;
105 if (!capable(CAP_SYS_ADMIN))
108 session = kmalloc(sizeof(*session), GFP_KERNEL);
112 session->bufsize = PAGE_SIZE;
113 session->response = NULL;
114 session->resp_size = 0;
115 mutex_init(&session->mutex);
116 file->private_data = session;
117 return nonseekable_open(inode, file);
120 static int vmcp_release(struct inode *inode, struct file *file)
122 struct vmcp_session *session;
124 session = file->private_data;
125 file->private_data = NULL;
126 vmcp_response_free(session);
132 vmcp_read(struct file *file, char __user *buff, size_t count, loff_t *ppos)
136 struct vmcp_session *session;
138 session = file->private_data;
139 if (mutex_lock_interruptible(&session->mutex))
141 if (!session->response) {
142 mutex_unlock(&session->mutex);
145 size = min_t(size_t, session->resp_size, session->bufsize);
146 ret = simple_read_from_buffer(buff, count, ppos,
147 session->response, size);
149 mutex_unlock(&session->mutex);
155 vmcp_write(struct file *file, const char __user *buff, size_t count,
159 struct vmcp_session *session;
163 cmd = memdup_user_nul(buff, count);
166 session = file->private_data;
167 if (mutex_lock_interruptible(&session->mutex)) {
171 if (!session->response)
172 vmcp_response_alloc(session);
173 if (!session->response) {
174 mutex_unlock(&session->mutex);
178 debug_text_event(vmcp_debug, 1, cmd);
179 session->resp_size = cpcmd(cmd, session->response, session->bufsize,
180 &session->resp_code);
181 mutex_unlock(&session->mutex);
183 *ppos = 0; /* reset the file pointer after a command */
189 * These ioctls are available, as the semantics of the diagnose 8 call
190 * does not fit very well into a Linux call. Diagnose X'08' is described in
191 * CP Programming Services SC24-6084-00
193 * VMCP_GETCODE: gives the CP return code back to user space
194 * VMCP_SETBUF: sets the response buffer for the next write call. diagnose 8
195 * expects adjacent pages in real storage and to make matters worse, we
196 * dont know the size of the response. Therefore we default to PAGESIZE and
197 * let userspace to change the response size, if userspace expects a bigger
200 static long vmcp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
202 struct vmcp_session *session;
206 session = file->private_data;
207 if (is_compat_task())
208 argp = compat_ptr(arg);
210 argp = (int __user *)arg;
211 if (mutex_lock_interruptible(&session->mutex))
215 ret = put_user(session->resp_code, argp);
218 vmcp_response_free(session);
219 ret = get_user(session->bufsize, argp);
221 session->bufsize = PAGE_SIZE;
222 if (!session->bufsize || get_order(session->bufsize) > 8) {
223 session->bufsize = PAGE_SIZE;
228 ret = put_user(session->resp_size, argp);
233 mutex_unlock(&session->mutex);
237 static const struct file_operations vmcp_fops = {
238 .owner = THIS_MODULE,
240 .release = vmcp_release,
243 .unlocked_ioctl = vmcp_ioctl,
244 .compat_ioctl = vmcp_ioctl,
248 static struct miscdevice vmcp_dev = {
250 .minor = MISC_DYNAMIC_MINOR,
254 static int __init vmcp_init(void)
261 vmcp_debug = debug_register("vmcp", 1, 1, 240);
265 ret = debug_register_view(vmcp_debug, &debug_hex_ascii_view);
267 debug_unregister(vmcp_debug);
271 ret = misc_register(&vmcp_dev);
273 debug_unregister(vmcp_debug);
276 device_initcall(vmcp_init);