vmcp.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. size_t tocopy;
  57. struct vmcp_session *session;
  58. session = (struct vmcp_session *)file->private_data;
  59. if (mutex_lock_interruptible(&session->mutex))
  60. return -ERESTARTSYS;
  61. if (!session->response) {
  62. mutex_unlock(&session->mutex);
  63. return 0;
  64. }
  65. if (*ppos > session->resp_size) {
  66. mutex_unlock(&session->mutex);
  67. return 0;
  68. }
  69. tocopy = min(session->resp_size - (size_t) (*ppos), count);
  70. tocopy = min(tocopy, session->bufsize - (size_t) (*ppos));
  71. if (copy_to_user(buff, session->response + (*ppos), tocopy)) {
  72. mutex_unlock(&session->mutex);
  73. return -EFAULT;
  74. }
  75. mutex_unlock(&session->mutex);
  76. *ppos += tocopy;
  77. return tocopy;
  78. }
  79. static ssize_t
  80. vmcp_write(struct file *file, const char __user *buff, size_t count,
  81. loff_t *ppos)
  82. {
  83. char *cmd;
  84. struct vmcp_session *session;
  85. if (count > 240)
  86. return -EINVAL;
  87. cmd = kmalloc(count + 1, GFP_KERNEL);
  88. if (!cmd)
  89. return -ENOMEM;
  90. if (copy_from_user(cmd, buff, count)) {
  91. kfree(cmd);
  92. return -EFAULT;
  93. }
  94. cmd[count] = '\0';
  95. session = (struct vmcp_session *)file->private_data;
  96. if (mutex_lock_interruptible(&session->mutex)) {
  97. kfree(cmd);
  98. return -ERESTARTSYS;
  99. }
  100. if (!session->response)
  101. session->response = (char *)__get_free_pages(GFP_KERNEL
  102. | __GFP_REPEAT | GFP_DMA,
  103. get_order(session->bufsize));
  104. if (!session->response) {
  105. mutex_unlock(&session->mutex);
  106. kfree(cmd);
  107. return -ENOMEM;
  108. }
  109. debug_text_event(vmcp_debug, 1, cmd);
  110. session->resp_size = cpcmd(cmd, session->response, session->bufsize,
  111. &session->resp_code);
  112. mutex_unlock(&session->mutex);
  113. kfree(cmd);
  114. *ppos = 0; /* reset the file pointer after a command */
  115. return count;
  116. }
  117. /*
  118. * These ioctls are available, as the semantics of the diagnose 8 call
  119. * does not fit very well into a Linux call. Diagnose X'08' is described in
  120. * CP Programming Services SC24-6084-00
  121. *
  122. * VMCP_GETCODE: gives the CP return code back to user space
  123. * VMCP_SETBUF: sets the response buffer for the next write call. diagnose 8
  124. * expects adjacent pages in real storage and to make matters worse, we
  125. * dont know the size of the response. Therefore we default to PAGESIZE and
  126. * let userspace to change the response size, if userspace expects a bigger
  127. * response
  128. */
  129. static long vmcp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  130. {
  131. struct vmcp_session *session;
  132. int temp;
  133. session = (struct vmcp_session *)file->private_data;
  134. if (mutex_lock_interruptible(&session->mutex))
  135. return -ERESTARTSYS;
  136. switch (cmd) {
  137. case VMCP_GETCODE:
  138. temp = session->resp_code;
  139. mutex_unlock(&session->mutex);
  140. return put_user(temp, (int __user *)arg);
  141. case VMCP_SETBUF:
  142. free_pages((unsigned long)session->response,
  143. get_order(session->bufsize));
  144. session->response=NULL;
  145. temp = get_user(session->bufsize, (int __user *)arg);
  146. if (get_order(session->bufsize) > 8) {
  147. session->bufsize = PAGE_SIZE;
  148. temp = -EINVAL;
  149. }
  150. mutex_unlock(&session->mutex);
  151. return temp;
  152. case VMCP_GETSIZE:
  153. temp = session->resp_size;
  154. mutex_unlock(&session->mutex);
  155. return put_user(temp, (int __user *)arg);
  156. default:
  157. mutex_unlock(&session->mutex);
  158. return -ENOIOCTLCMD;
  159. }
  160. }
  161. static const struct file_operations vmcp_fops = {
  162. .owner = THIS_MODULE,
  163. .open = vmcp_open,
  164. .release = vmcp_release,
  165. .read = vmcp_read,
  166. .write = vmcp_write,
  167. .unlocked_ioctl = vmcp_ioctl,
  168. .compat_ioctl = vmcp_ioctl,
  169. };
  170. static struct miscdevice vmcp_dev = {
  171. .name = "vmcp",
  172. .minor = MISC_DYNAMIC_MINOR,
  173. .fops = &vmcp_fops,
  174. };
  175. static int __init vmcp_init(void)
  176. {
  177. int ret;
  178. if (!MACHINE_IS_VM) {
  179. PRINT_WARN("z/VM CP interface is only available under z/VM\n");
  180. return -ENODEV;
  181. }
  182. vmcp_debug = debug_register("vmcp", 1, 1, 240);
  183. if (!vmcp_debug) {
  184. PRINT_ERR("z/VM CP interface not loaded. Could not register "
  185. "debug feature\n");
  186. return -ENOMEM;
  187. }
  188. ret = debug_register_view(vmcp_debug, &debug_hex_ascii_view);
  189. if (ret) {
  190. PRINT_ERR("z/VM CP interface not loaded. Could not register "
  191. "debug feature view. Error code: %d\n", ret);
  192. debug_unregister(vmcp_debug);
  193. return ret;
  194. }
  195. ret = misc_register(&vmcp_dev);
  196. if (ret) {
  197. PRINT_ERR("z/VM CP interface not loaded. Could not register "
  198. "misc device. Error code: %d\n", ret);
  199. debug_unregister(vmcp_debug);
  200. return ret;
  201. }
  202. PRINT_INFO("z/VM CP interface loaded\n");
  203. return 0;
  204. }
  205. static void __exit vmcp_exit(void)
  206. {
  207. misc_deregister(&vmcp_dev);
  208. debug_unregister(vmcp_debug);
  209. PRINT_INFO("z/VM CP interface unloaded.\n");
  210. }
  211. module_init(vmcp_init);
  212. module_exit(vmcp_exit);