vmcp.c 5.6 KB

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