vmcp.c 5.5 KB

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