vmcp.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. #include <linux/fs.h>
  13. #include <linux/init.h>
  14. #include <linux/kernel.h>
  15. #include <linux/miscdevice.h>
  16. #include <linux/module.h>
  17. #include <linux/smp_lock.h>
  18. #include <asm/cpcmd.h>
  19. #include <asm/debug.h>
  20. #include <asm/uaccess.h>
  21. #include "vmcp.h"
  22. MODULE_LICENSE("GPL");
  23. MODULE_AUTHOR("Christian Borntraeger <borntraeger@de.ibm.com>");
  24. MODULE_DESCRIPTION("z/VM CP interface");
  25. #define PRINTK_HEADER "vmcp: "
  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. lock_kernel();
  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. unlock_kernel();
  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 temp;
  126. session = (struct vmcp_session *)file->private_data;
  127. if (mutex_lock_interruptible(&session->mutex))
  128. return -ERESTARTSYS;
  129. switch (cmd) {
  130. case VMCP_GETCODE:
  131. temp = session->resp_code;
  132. mutex_unlock(&session->mutex);
  133. return put_user(temp, (int __user *)arg);
  134. case VMCP_SETBUF:
  135. free_pages((unsigned long)session->response,
  136. get_order(session->bufsize));
  137. session->response=NULL;
  138. temp = get_user(session->bufsize, (int __user *)arg);
  139. if (get_order(session->bufsize) > 8) {
  140. session->bufsize = PAGE_SIZE;
  141. temp = -EINVAL;
  142. }
  143. mutex_unlock(&session->mutex);
  144. return temp;
  145. case VMCP_GETSIZE:
  146. temp = session->resp_size;
  147. mutex_unlock(&session->mutex);
  148. return put_user(temp, (int __user *)arg);
  149. default:
  150. mutex_unlock(&session->mutex);
  151. return -ENOIOCTLCMD;
  152. }
  153. }
  154. static const struct file_operations vmcp_fops = {
  155. .owner = THIS_MODULE,
  156. .open = vmcp_open,
  157. .release = vmcp_release,
  158. .read = vmcp_read,
  159. .write = vmcp_write,
  160. .unlocked_ioctl = vmcp_ioctl,
  161. .compat_ioctl = vmcp_ioctl,
  162. };
  163. static struct miscdevice vmcp_dev = {
  164. .name = "vmcp",
  165. .minor = MISC_DYNAMIC_MINOR,
  166. .fops = &vmcp_fops,
  167. };
  168. static int __init vmcp_init(void)
  169. {
  170. int ret;
  171. if (!MACHINE_IS_VM) {
  172. PRINT_WARN("z/VM CP interface is only available under z/VM\n");
  173. return -ENODEV;
  174. }
  175. vmcp_debug = debug_register("vmcp", 1, 1, 240);
  176. if (!vmcp_debug)
  177. return -ENOMEM;
  178. ret = debug_register_view(vmcp_debug, &debug_hex_ascii_view);
  179. if (ret) {
  180. debug_unregister(vmcp_debug);
  181. return ret;
  182. }
  183. ret = misc_register(&vmcp_dev);
  184. if (ret) {
  185. debug_unregister(vmcp_debug);
  186. return ret;
  187. }
  188. return 0;
  189. }
  190. static void __exit vmcp_exit(void)
  191. {
  192. misc_deregister(&vmcp_dev);
  193. debug_unregister(vmcp_debug);
  194. }
  195. module_init(vmcp_init);
  196. module_exit(vmcp_exit);