vmcp.c revision a114a9d69d1362546b9dda651028acb311ddb7af
1/* 2 * Copyright IBM Corp. 2004,2007 3 * Interface implementation for communication with the z/VM control program 4 * Author(s): Christian Borntraeger <borntraeger@de.ibm.com> 5 * 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/kernel.h> 17#include <linux/miscdevice.h> 18#include <linux/module.h> 19#include <asm/cpcmd.h> 20#include <asm/debug.h> 21#include <asm/uaccess.h> 22#include "vmcp.h" 23 24MODULE_LICENSE("GPL"); 25MODULE_AUTHOR("Christian Borntraeger <borntraeger@de.ibm.com>"); 26MODULE_DESCRIPTION("z/VM CP interface"); 27 28#define PRINTK_HEADER "vmcp: " 29 30static debug_info_t *vmcp_debug; 31 32static int vmcp_open(struct inode *inode, struct file *file) 33{ 34 struct vmcp_session *session; 35 36 if (!capable(CAP_SYS_ADMIN)) 37 return -EPERM; 38 39 session = kmalloc(sizeof(*session), GFP_KERNEL); 40 if (!session) 41 return -ENOMEM; 42 43 session->bufsize = PAGE_SIZE; 44 session->response = NULL; 45 session->resp_size = 0; 46 mutex_init(&session->mutex); 47 file->private_data = session; 48 return nonseekable_open(inode, file); 49} 50 51static int vmcp_release(struct inode *inode, struct file *file) 52{ 53 struct vmcp_session *session; 54 55 session = (struct vmcp_session *)file->private_data; 56 file->private_data = NULL; 57 free_pages((unsigned long)session->response, get_order(session->bufsize)); 58 kfree(session); 59 return 0; 60} 61 62static ssize_t 63vmcp_read(struct file *file, char __user *buff, size_t count, loff_t *ppos) 64{ 65 ssize_t ret; 66 size_t size; 67 struct vmcp_session *session; 68 69 session = file->private_data; 70 if (mutex_lock_interruptible(&session->mutex)) 71 return -ERESTARTSYS; 72 if (!session->response) { 73 mutex_unlock(&session->mutex); 74 return 0; 75 } 76 size = min_t(size_t, session->resp_size, session->bufsize); 77 ret = simple_read_from_buffer(buff, count, ppos, 78 session->response, size); 79 80 mutex_unlock(&session->mutex); 81 82 return ret; 83} 84 85static ssize_t 86vmcp_write(struct file *file, const char __user *buff, size_t count, 87 loff_t *ppos) 88{ 89 char *cmd; 90 struct vmcp_session *session; 91 92 if (count > 240) 93 return -EINVAL; 94 cmd = kmalloc(count + 1, GFP_KERNEL); 95 if (!cmd) 96 return -ENOMEM; 97 if (copy_from_user(cmd, buff, count)) { 98 kfree(cmd); 99 return -EFAULT; 100 } 101 cmd[count] = '\0'; 102 session = (struct vmcp_session *)file->private_data; 103 if (mutex_lock_interruptible(&session->mutex)) { 104 kfree(cmd); 105 return -ERESTARTSYS; 106 } 107 if (!session->response) 108 session->response = (char *)__get_free_pages(GFP_KERNEL 109 | __GFP_REPEAT | GFP_DMA, 110 get_order(session->bufsize)); 111 if (!session->response) { 112 mutex_unlock(&session->mutex); 113 kfree(cmd); 114 return -ENOMEM; 115 } 116 debug_text_event(vmcp_debug, 1, cmd); 117 session->resp_size = cpcmd(cmd, session->response, session->bufsize, 118 &session->resp_code); 119 mutex_unlock(&session->mutex); 120 kfree(cmd); 121 *ppos = 0; /* reset the file pointer after a command */ 122 return count; 123} 124 125 126/* 127 * These ioctls are available, as the semantics of the diagnose 8 call 128 * does not fit very well into a Linux call. Diagnose X'08' is described in 129 * CP Programming Services SC24-6084-00 130 * 131 * VMCP_GETCODE: gives the CP return code back to user space 132 * VMCP_SETBUF: sets the response buffer for the next write call. diagnose 8 133 * expects adjacent pages in real storage and to make matters worse, we 134 * dont know the size of the response. Therefore we default to PAGESIZE and 135 * let userspace to change the response size, if userspace expects a bigger 136 * response 137 */ 138static long vmcp_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 139{ 140 struct vmcp_session *session; 141 int temp; 142 143 session = (struct vmcp_session *)file->private_data; 144 if (mutex_lock_interruptible(&session->mutex)) 145 return -ERESTARTSYS; 146 switch (cmd) { 147 case VMCP_GETCODE: 148 temp = session->resp_code; 149 mutex_unlock(&session->mutex); 150 return put_user(temp, (int __user *)arg); 151 case VMCP_SETBUF: 152 free_pages((unsigned long)session->response, 153 get_order(session->bufsize)); 154 session->response=NULL; 155 temp = get_user(session->bufsize, (int __user *)arg); 156 if (get_order(session->bufsize) > 8) { 157 session->bufsize = PAGE_SIZE; 158 temp = -EINVAL; 159 } 160 mutex_unlock(&session->mutex); 161 return temp; 162 case VMCP_GETSIZE: 163 temp = session->resp_size; 164 mutex_unlock(&session->mutex); 165 return put_user(temp, (int __user *)arg); 166 default: 167 mutex_unlock(&session->mutex); 168 return -ENOIOCTLCMD; 169 } 170} 171 172static const struct file_operations vmcp_fops = { 173 .owner = THIS_MODULE, 174 .open = vmcp_open, 175 .release = vmcp_release, 176 .read = vmcp_read, 177 .write = vmcp_write, 178 .unlocked_ioctl = vmcp_ioctl, 179 .compat_ioctl = vmcp_ioctl, 180}; 181 182static struct miscdevice vmcp_dev = { 183 .name = "vmcp", 184 .minor = MISC_DYNAMIC_MINOR, 185 .fops = &vmcp_fops, 186}; 187 188static int __init vmcp_init(void) 189{ 190 int ret; 191 192 if (!MACHINE_IS_VM) { 193 PRINT_WARN("z/VM CP interface is only available under z/VM\n"); 194 return -ENODEV; 195 } 196 197 vmcp_debug = debug_register("vmcp", 1, 1, 240); 198 if (!vmcp_debug) 199 return -ENOMEM; 200 201 ret = debug_register_view(vmcp_debug, &debug_hex_ascii_view); 202 if (ret) { 203 debug_unregister(vmcp_debug); 204 return ret; 205 } 206 207 ret = misc_register(&vmcp_dev); 208 if (ret) { 209 debug_unregister(vmcp_debug); 210 return ret; 211 } 212 213 return 0; 214} 215 216static void __exit vmcp_exit(void) 217{ 218 misc_deregister(&vmcp_dev); 219 debug_unregister(vmcp_debug); 220} 221 222module_init(vmcp_init); 223module_exit(vmcp_exit); 224