vmcp.c 5.6 KB

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