vmcp.c 5.1 KB

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