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 <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 = kmalloc(count + 1, GFP_KERNEL);
  78. if (!cmd)
  79. return -ENOMEM;
  80. if (copy_from_user(cmd, buff, count)) {
  81. kfree(cmd);
  82. return -EFAULT;
  83. }
  84. cmd[count] = '\0';
  85. session = file->private_data;
  86. if (mutex_lock_interruptible(&session->mutex)) {
  87. kfree(cmd);
  88. return -ERESTARTSYS;
  89. }
  90. if (!session->response)
  91. session->response = (char *)__get_free_pages(GFP_KERNEL
  92. | __GFP_REPEAT | GFP_DMA,
  93. get_order(session->bufsize));
  94. if (!session->response) {
  95. mutex_unlock(&session->mutex);
  96. kfree(cmd);
  97. return -ENOMEM;
  98. }
  99. debug_text_event(vmcp_debug, 1, cmd);
  100. session->resp_size = cpcmd(cmd, session->response, session->bufsize,
  101. &session->resp_code);
  102. mutex_unlock(&session->mutex);
  103. kfree(cmd);
  104. *ppos = 0; /* reset the file pointer after a command */
  105. return count;
  106. }
  107. /*
  108. * These ioctls are available, as the semantics of the diagnose 8 call
  109. * does not fit very well into a Linux call. Diagnose X'08' is described in
  110. * CP Programming Services SC24-6084-00
  111. *
  112. * VMCP_GETCODE: gives the CP return code back to user space
  113. * VMCP_SETBUF: sets the response buffer for the next write call. diagnose 8
  114. * expects adjacent pages in real storage and to make matters worse, we
  115. * dont know the size of the response. Therefore we default to PAGESIZE and
  116. * let userspace to change the response size, if userspace expects a bigger
  117. * response
  118. */
  119. static long vmcp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  120. {
  121. struct vmcp_session *session;
  122. int __user *argp;
  123. int temp;
  124. session = file->private_data;
  125. if (is_compat_task())
  126. argp = compat_ptr(arg);
  127. else
  128. argp = (int __user *)arg;
  129. if (mutex_lock_interruptible(&session->mutex))
  130. return -ERESTARTSYS;
  131. switch (cmd) {
  132. case VMCP_GETCODE:
  133. temp = session->resp_code;
  134. mutex_unlock(&session->mutex);
  135. return put_user(temp, argp);
  136. case VMCP_SETBUF:
  137. free_pages((unsigned long)session->response,
  138. get_order(session->bufsize));
  139. session->response=NULL;
  140. temp = get_user(session->bufsize, argp);
  141. if (get_order(session->bufsize) > 8) {
  142. session->bufsize = PAGE_SIZE;
  143. temp = -EINVAL;
  144. }
  145. mutex_unlock(&session->mutex);
  146. return temp;
  147. case VMCP_GETSIZE:
  148. temp = session->resp_size;
  149. mutex_unlock(&session->mutex);
  150. return put_user(temp, argp);
  151. default:
  152. mutex_unlock(&session->mutex);
  153. return -ENOIOCTLCMD;
  154. }
  155. }
  156. static const struct file_operations vmcp_fops = {
  157. .owner = THIS_MODULE,
  158. .open = vmcp_open,
  159. .release = vmcp_release,
  160. .read = vmcp_read,
  161. .write = vmcp_write,
  162. .unlocked_ioctl = vmcp_ioctl,
  163. .compat_ioctl = vmcp_ioctl,
  164. .llseek = no_llseek,
  165. };
  166. static struct miscdevice vmcp_dev = {
  167. .name = "vmcp",
  168. .minor = MISC_DYNAMIC_MINOR,
  169. .fops = &vmcp_fops,
  170. };
  171. static int __init vmcp_init(void)
  172. {
  173. int ret;
  174. if (!MACHINE_IS_VM)
  175. return 0;
  176. vmcp_debug = debug_register("vmcp", 1, 1, 240);
  177. if (!vmcp_debug)
  178. return -ENOMEM;
  179. ret = debug_register_view(vmcp_debug, &debug_hex_ascii_view);
  180. if (ret) {
  181. debug_unregister(vmcp_debug);
  182. return ret;
  183. }
  184. ret = misc_register(&vmcp_dev);
  185. if (ret)
  186. debug_unregister(vmcp_debug);
  187. return ret;
  188. }
  189. device_initcall(vmcp_init);