vmcp.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Copyright (C) 2004,2005 IBM Corporation
  3. * Interface implementation for communication with the v/VM control program
  4. * Author(s): Christian Borntraeger <cborntra@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 <asm/cpcmd.h>
  18. #include <asm/debug.h>
  19. #include <asm/uaccess.h>
  20. #include "vmcp.h"
  21. MODULE_LICENSE("GPL");
  22. MODULE_AUTHOR("Christian Borntraeger <cborntra@de.ibm.com>");
  23. MODULE_DESCRIPTION("z/VM CP interface");
  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. init_MUTEX(&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 = (struct vmcp_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. size_t tocopy;
  53. struct vmcp_session *session;
  54. session = (struct vmcp_session *)file->private_data;
  55. if (down_interruptible(&session->mutex))
  56. return -ERESTARTSYS;
  57. if (!session->response) {
  58. up(&session->mutex);
  59. return 0;
  60. }
  61. if (*ppos > session->resp_size) {
  62. up(&session->mutex);
  63. return 0;
  64. }
  65. tocopy = min(session->resp_size - (size_t) (*ppos), count);
  66. tocopy = min(tocopy,session->bufsize - (size_t) (*ppos));
  67. if (copy_to_user(buff, session->response + (*ppos), tocopy)) {
  68. up(&session->mutex);
  69. return -EFAULT;
  70. }
  71. up(&session->mutex);
  72. *ppos += tocopy;
  73. return tocopy;
  74. }
  75. static ssize_t
  76. vmcp_write(struct file *file, const char __user * buff, size_t count,
  77. loff_t * ppos)
  78. {
  79. char *cmd;
  80. struct vmcp_session *session;
  81. if (count > 240)
  82. return -EINVAL;
  83. cmd = kmalloc(count + 1, GFP_KERNEL);
  84. if (!cmd)
  85. return -ENOMEM;
  86. if (copy_from_user(cmd, buff, count)) {
  87. kfree(cmd);
  88. return -EFAULT;
  89. }
  90. cmd[count] = '\0';
  91. session = (struct vmcp_session *)file->private_data;
  92. if (down_interruptible(&session->mutex))
  93. return -ERESTARTSYS;
  94. if (!session->response)
  95. session->response = (char *)__get_free_pages(GFP_KERNEL
  96. | __GFP_REPEAT | GFP_DMA,
  97. get_order(session->bufsize));
  98. if (!session->response) {
  99. up(&session->mutex);
  100. kfree(cmd);
  101. return -ENOMEM;
  102. }
  103. debug_text_event(vmcp_debug, 1, cmd);
  104. session->resp_size = __cpcmd(cmd, session->response,
  105. session->bufsize,
  106. &session->resp_code);
  107. up(&session->mutex);
  108. kfree(cmd);
  109. *ppos = 0; /* reset the file pointer after a command */
  110. return count;
  111. }
  112. /*
  113. * These ioctls are available, as the semantics of the diagnose 8 call
  114. * does not fit very well into a Linux call. Diagnose X'08' is described in
  115. * CP Programming Services SC24-6084-00
  116. *
  117. * VMCP_GETCODE: gives the CP return code back to user space
  118. * VMCP_SETBUF: sets the response buffer for the next write call. diagnose 8
  119. * expects adjacent pages in real storage and to make matters worse, we
  120. * dont know the size of the response. Therefore we default to PAGESIZE and
  121. * let userspace to change the response size, if userspace expects a bigger
  122. * response
  123. */
  124. static long vmcp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  125. {
  126. struct vmcp_session *session;
  127. int temp;
  128. session = (struct vmcp_session *)file->private_data;
  129. if (down_interruptible(&session->mutex))
  130. return -ERESTARTSYS;
  131. switch (cmd) {
  132. case VMCP_GETCODE:
  133. temp = session->resp_code;
  134. up(&session->mutex);
  135. return put_user(temp, (int __user *)arg);
  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, (int __user *)arg);
  141. if (get_order(session->bufsize) > 8) {
  142. session->bufsize = PAGE_SIZE;
  143. temp = -EINVAL;
  144. }
  145. up(&session->mutex);
  146. return temp;
  147. case VMCP_GETSIZE:
  148. temp = session->resp_size;
  149. up(&session->mutex);
  150. return put_user(temp, (int __user *)arg);
  151. default:
  152. up(&session->mutex);
  153. return -ENOIOCTLCMD;
  154. }
  155. }
  156. static struct file_operations vmcp_fops = {
  157. .owner = THIS_MODULE,
  158. .open = &vmcp_open,
  159. .release = &vmcp_release,
  160. .read = &vmcp_read,
  161. .llseek = &no_llseek,
  162. .write = &vmcp_write,
  163. .unlocked_ioctl = &vmcp_ioctl,
  164. .compat_ioctl = &vmcp_ioctl
  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. printk(KERN_WARNING
  176. "z/VM CP interface is only available under z/VM\n");
  177. return -ENODEV;
  178. }
  179. ret = misc_register(&vmcp_dev);
  180. if (!ret)
  181. printk(KERN_INFO "z/VM CP interface loaded\n");
  182. else
  183. printk(KERN_WARNING
  184. "z/VM CP interface not loaded. Could not register misc device.\n");
  185. vmcp_debug = debug_register("vmcp", 1, 1, 240);
  186. debug_register_view(vmcp_debug, &debug_hex_ascii_view);
  187. return ret;
  188. }
  189. static void __exit vmcp_exit(void)
  190. {
  191. WARN_ON(misc_deregister(&vmcp_dev) != 0);
  192. debug_unregister(vmcp_debug);
  193. printk(KERN_INFO "z/VM CP interface unloaded.\n");
  194. }
  195. module_init(vmcp_init);
  196. module_exit(vmcp_exit);