vmcp.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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/compat.h>
  16. #include <linux/kernel.h>
  17. #include <linux/miscdevice.h>
  18. #include <linux/slab.h>
  19. #include <linux/export.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. static debug_info_t *vmcp_debug;
  26. static int vmcp_open(struct inode *inode, struct file *file)
  27. {
  28. struct vmcp_session *session;
  29. if (!capable(CAP_SYS_ADMIN))
  30. return -EPERM;
  31. session = kmalloc(sizeof(*session), GFP_KERNEL);
  32. if (!session)
  33. return -ENOMEM;
  34. session->bufsize = PAGE_SIZE;
  35. session->response = NULL;
  36. session->resp_size = 0;
  37. mutex_init(&session->mutex);
  38. file->private_data = session;
  39. return nonseekable_open(inode, file);
  40. }
  41. static int vmcp_release(struct inode *inode, struct file *file)
  42. {
  43. struct vmcp_session *session;
  44. session = file->private_data;
  45. file->private_data = NULL;
  46. free_pages((unsigned long)session->response, get_order(session->bufsize));
  47. kfree(session);
  48. return 0;
  49. }
  50. static ssize_t
  51. vmcp_read(struct file *file, char __user *buff, size_t count, loff_t *ppos)
  52. {
  53. ssize_t ret;
  54. size_t size;
  55. struct vmcp_session *session;
  56. session = file->private_data;
  57. if (mutex_lock_interruptible(&session->mutex))
  58. return -ERESTARTSYS;
  59. if (!session->response) {
  60. mutex_unlock(&session->mutex);
  61. return 0;
  62. }
  63. size = min_t(size_t, session->resp_size, session->bufsize);
  64. ret = simple_read_from_buffer(buff, count, ppos,
  65. session->response, size);
  66. mutex_unlock(&session->mutex);
  67. return ret;
  68. }
  69. static ssize_t
  70. vmcp_write(struct file *file, const char __user *buff, size_t count,
  71. loff_t *ppos)
  72. {
  73. char *cmd;
  74. struct vmcp_session *session;
  75. if (count > 240)
  76. return -EINVAL;
  77. cmd = memdup_user_nul(buff, count);
  78. if (IS_ERR(cmd))
  79. return PTR_ERR(cmd);
  80. session = file->private_data;
  81. if (mutex_lock_interruptible(&session->mutex)) {
  82. kfree(cmd);
  83. return -ERESTARTSYS;
  84. }
  85. if (!session->response)
  86. session->response = (char *)__get_free_pages(GFP_KERNEL
  87. | __GFP_REPEAT | GFP_DMA,
  88. get_order(session->bufsize));
  89. if (!session->response) {
  90. mutex_unlock(&session->mutex);
  91. kfree(cmd);
  92. return -ENOMEM;
  93. }
  94. debug_text_event(vmcp_debug, 1, cmd);
  95. session->resp_size = cpcmd(cmd, session->response, session->bufsize,
  96. &session->resp_code);
  97. mutex_unlock(&session->mutex);
  98. kfree(cmd);
  99. *ppos = 0; /* reset the file pointer after a command */
  100. return count;
  101. }
  102. /*
  103. * These ioctls are available, as the semantics of the diagnose 8 call
  104. * does not fit very well into a Linux call. Diagnose X'08' is described in
  105. * CP Programming Services SC24-6084-00
  106. *
  107. * VMCP_GETCODE: gives the CP return code back to user space
  108. * VMCP_SETBUF: sets the response buffer for the next write call. diagnose 8
  109. * expects adjacent pages in real storage and to make matters worse, we
  110. * dont know the size of the response. Therefore we default to PAGESIZE and
  111. * let userspace to change the response size, if userspace expects a bigger
  112. * response
  113. */
  114. static long vmcp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  115. {
  116. struct vmcp_session *session;
  117. int __user *argp;
  118. int temp;
  119. session = file->private_data;
  120. if (is_compat_task())
  121. argp = compat_ptr(arg);
  122. else
  123. argp = (int __user *)arg;
  124. if (mutex_lock_interruptible(&session->mutex))
  125. return -ERESTARTSYS;
  126. switch (cmd) {
  127. case VMCP_GETCODE:
  128. temp = session->resp_code;
  129. mutex_unlock(&session->mutex);
  130. return put_user(temp, argp);
  131. case VMCP_SETBUF:
  132. free_pages((unsigned long)session->response,
  133. get_order(session->bufsize));
  134. session->response=NULL;
  135. temp = get_user(session->bufsize, argp);
  136. if (get_order(session->bufsize) > 8) {
  137. session->bufsize = PAGE_SIZE;
  138. temp = -EINVAL;
  139. }
  140. mutex_unlock(&session->mutex);
  141. return temp;
  142. case VMCP_GETSIZE:
  143. temp = session->resp_size;
  144. mutex_unlock(&session->mutex);
  145. return put_user(temp, argp);
  146. default:
  147. mutex_unlock(&session->mutex);
  148. return -ENOIOCTLCMD;
  149. }
  150. }
  151. static const struct file_operations vmcp_fops = {
  152. .owner = THIS_MODULE,
  153. .open = vmcp_open,
  154. .release = vmcp_release,
  155. .read = vmcp_read,
  156. .write = vmcp_write,
  157. .unlocked_ioctl = vmcp_ioctl,
  158. .compat_ioctl = vmcp_ioctl,
  159. .llseek = no_llseek,
  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. return 0;
  171. vmcp_debug = debug_register("vmcp", 1, 1, 240);
  172. if (!vmcp_debug)
  173. return -ENOMEM;
  174. ret = debug_register_view(vmcp_debug, &debug_hex_ascii_view);
  175. if (ret) {
  176. debug_unregister(vmcp_debug);
  177. return ret;
  178. }
  179. ret = misc_register(&vmcp_dev);
  180. if (ret)
  181. debug_unregister(vmcp_debug);
  182. return ret;
  183. }
  184. device_initcall(vmcp_init);