vmcp.c 5.6 KB

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