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