vmcp.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * Copyright IBM Corp. 2004,2010
  3. * Interface implementation for communication with the z/VM control program
  4. *
  5. * Author(s): Christian Borntraeger <borntraeger@de.ibm.com>
  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. #include <linux/fs.h>
  14. #include <linux/init.h>
  15. #include <linux/kernel.h>
  16. #include <linux/miscdevice.h>
  17. #include <linux/slab.h>
  18. #include <asm/compat.h>
  19. #include <asm/cpcmd.h>
  20. #include <asm/debug.h>
  21. #include <asm/uaccess.h>
  22. #include "vmcp.h"
  23. static debug_info_t *vmcp_debug;
  24. static int vmcp_open(struct inode *inode, struct file *file)
  25. {
  26. struct vmcp_session *session;
  27. if (!capable(CAP_SYS_ADMIN))
  28. return -EPERM;
  29. session = kmalloc(sizeof(*session), GFP_KERNEL);
  30. if (!session)
  31. return -ENOMEM;
  32. session->bufsize = PAGE_SIZE;
  33. session->response = NULL;
  34. session->resp_size = 0;
  35. mutex_init(&session->mutex);
  36. file->private_data = session;
  37. return nonseekable_open(inode, file);
  38. }
  39. static int vmcp_release(struct inode *inode, struct file *file)
  40. {
  41. struct vmcp_session *session;
  42. session = file->private_data;
  43. file->private_data = NULL;
  44. free_pages((unsigned long)session->response, get_order(session->bufsize));
  45. kfree(session);
  46. return 0;
  47. }
  48. static ssize_t
  49. vmcp_read(struct file *file, char __user *buff, size_t count, loff_t *ppos)
  50. {
  51. ssize_t ret;
  52. size_t size;
  53. struct vmcp_session *session;
  54. session = file->private_data;
  55. if (mutex_lock_interruptible(&session->mutex))
  56. return -ERESTARTSYS;
  57. if (!session->response) {
  58. mutex_unlock(&session->mutex);
  59. return 0;
  60. }
  61. size = min_t(size_t, session->resp_size, session->bufsize);
  62. ret = simple_read_from_buffer(buff, count, ppos,
  63. session->response, size);
  64. mutex_unlock(&session->mutex);
  65. return ret;
  66. }
  67. static ssize_t
  68. vmcp_write(struct file *file, const char __user *buff, size_t count,
  69. loff_t *ppos)
  70. {
  71. char *cmd;
  72. struct vmcp_session *session;
  73. if (count > 240)
  74. return -EINVAL;
  75. cmd = kmalloc(count + 1, GFP_KERNEL);
  76. if (!cmd)
  77. return -ENOMEM;
  78. if (copy_from_user(cmd, buff, count)) {
  79. kfree(cmd);
  80. return -EFAULT;
  81. }
  82. cmd[count] = '\0';
  83. session = file->private_data;
  84. if (mutex_lock_interruptible(&session->mutex)) {
  85. kfree(cmd);
  86. return -ERESTARTSYS;
  87. }
  88. if (!session->response)
  89. session->response = (char *)__get_free_pages(GFP_KERNEL
  90. | __GFP_REPEAT | GFP_DMA,
  91. get_order(session->bufsize));
  92. if (!session->response) {
  93. mutex_unlock(&session->mutex);
  94. kfree(cmd);
  95. return -ENOMEM;
  96. }
  97. debug_text_event(vmcp_debug, 1, cmd);
  98. session->resp_size = cpcmd(cmd, session->response, session->bufsize,
  99. &session->resp_code);
  100. mutex_unlock(&session->mutex);
  101. kfree(cmd);
  102. *ppos = 0; /* reset the file pointer after a command */
  103. return count;
  104. }
  105. /*
  106. * These ioctls are available, as the semantics of the diagnose 8 call
  107. * does not fit very well into a Linux call. Diagnose X'08' is described in
  108. * CP Programming Services SC24-6084-00
  109. *
  110. * VMCP_GETCODE: gives the CP return code back to user space
  111. * VMCP_SETBUF: sets the response buffer for the next write call. diagnose 8
  112. * expects adjacent pages in real storage and to make matters worse, we
  113. * dont know the size of the response. Therefore we default to PAGESIZE and
  114. * let userspace to change the response size, if userspace expects a bigger
  115. * response
  116. */
  117. static long vmcp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  118. {
  119. struct vmcp_session *session;
  120. int __user *argp;
  121. int temp;
  122. session = file->private_data;
  123. if (is_compat_task())
  124. argp = compat_ptr(arg);
  125. else
  126. argp = (int __user *)arg;
  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, argp);
  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, argp);
  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, argp);
  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. .llseek = no_llseek,
  163. };
  164. static struct miscdevice vmcp_dev = {
  165. .name = "vmcp",
  166. .minor = MISC_DYNAMIC_MINOR,
  167. .fops = &vmcp_fops,
  168. };
  169. static int __init vmcp_init(void)
  170. {
  171. int ret;
  172. if (!MACHINE_IS_VM)
  173. return 0;
  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. device_initcall(vmcp_init);