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