vmcp.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright IBM Corp. 2004, 2010
  4. * Interface implementation for communication with the z/VM control program
  5. *
  6. * Author(s): Christian Borntraeger <borntraeger@de.ibm.com>
  7. *
  8. * z/VMs CP offers the possibility to issue commands via the diagnose code 8
  9. * this driver implements a character device that issues these commands and
  10. * returns the answer of CP.
  11. *
  12. * The idea of this driver is based on cpint from Neale Ferguson and #CP in CMS
  13. */
  14. #include <linux/fs.h>
  15. #include <linux/init.h>
  16. #include <linux/compat.h>
  17. #include <linux/kernel.h>
  18. #include <linux/miscdevice.h>
  19. #include <linux/slab.h>
  20. #include <linux/uaccess.h>
  21. #include <linux/export.h>
  22. #include <linux/mutex.h>
  23. #include <linux/cma.h>
  24. #include <linux/mm.h>
  25. #include <asm/cpcmd.h>
  26. #include <asm/debug.h>
  27. #include <asm/vmcp.h>
  28. struct vmcp_session {
  29. char *response;
  30. unsigned int bufsize;
  31. unsigned int cma_alloc : 1;
  32. int resp_size;
  33. int resp_code;
  34. struct mutex mutex;
  35. };
  36. static debug_info_t *vmcp_debug;
  37. static unsigned long vmcp_cma_size __initdata = CONFIG_VMCP_CMA_SIZE * 1024 * 1024;
  38. static struct cma *vmcp_cma;
  39. static int __init early_parse_vmcp_cma(char *p)
  40. {
  41. vmcp_cma_size = ALIGN(memparse(p, NULL), PAGE_SIZE);
  42. return 0;
  43. }
  44. early_param("vmcp_cma", early_parse_vmcp_cma);
  45. void __init vmcp_cma_reserve(void)
  46. {
  47. if (!MACHINE_IS_VM)
  48. return;
  49. cma_declare_contiguous(0, vmcp_cma_size, 0, 0, 0, false, "vmcp", &vmcp_cma);
  50. }
  51. static void vmcp_response_alloc(struct vmcp_session *session)
  52. {
  53. struct page *page = NULL;
  54. int nr_pages, order;
  55. order = get_order(session->bufsize);
  56. nr_pages = ALIGN(session->bufsize, PAGE_SIZE) >> PAGE_SHIFT;
  57. /*
  58. * For anything below order 3 allocations rely on the buddy
  59. * allocator. If such low-order allocations can't be handled
  60. * anymore the system won't work anyway.
  61. */
  62. if (order > 2)
  63. page = cma_alloc(vmcp_cma, nr_pages, 0, false);
  64. if (page) {
  65. session->response = (char *)page_to_phys(page);
  66. session->cma_alloc = 1;
  67. return;
  68. }
  69. session->response = (char *)__get_free_pages(GFP_KERNEL | __GFP_RETRY_MAYFAIL, order);
  70. }
  71. static void vmcp_response_free(struct vmcp_session *session)
  72. {
  73. int nr_pages, order;
  74. struct page *page;
  75. if (!session->response)
  76. return;
  77. order = get_order(session->bufsize);
  78. nr_pages = ALIGN(session->bufsize, PAGE_SIZE) >> PAGE_SHIFT;
  79. if (session->cma_alloc) {
  80. page = phys_to_page((unsigned long)session->response);
  81. cma_release(vmcp_cma, page, nr_pages);
  82. session->cma_alloc = 0;
  83. } else {
  84. free_pages((unsigned long)session->response, order);
  85. }
  86. session->response = NULL;
  87. }
  88. static int vmcp_open(struct inode *inode, struct file *file)
  89. {
  90. struct vmcp_session *session;
  91. if (!capable(CAP_SYS_ADMIN))
  92. return -EPERM;
  93. session = kmalloc(sizeof(*session), GFP_KERNEL);
  94. if (!session)
  95. return -ENOMEM;
  96. session->bufsize = PAGE_SIZE;
  97. session->response = NULL;
  98. session->resp_size = 0;
  99. mutex_init(&session->mutex);
  100. file->private_data = session;
  101. return nonseekable_open(inode, file);
  102. }
  103. static int vmcp_release(struct inode *inode, struct file *file)
  104. {
  105. struct vmcp_session *session;
  106. session = file->private_data;
  107. file->private_data = NULL;
  108. vmcp_response_free(session);
  109. kfree(session);
  110. return 0;
  111. }
  112. static ssize_t
  113. vmcp_read(struct file *file, char __user *buff, size_t count, loff_t *ppos)
  114. {
  115. ssize_t ret;
  116. size_t size;
  117. struct vmcp_session *session;
  118. session = file->private_data;
  119. if (mutex_lock_interruptible(&session->mutex))
  120. return -ERESTARTSYS;
  121. if (!session->response) {
  122. mutex_unlock(&session->mutex);
  123. return 0;
  124. }
  125. size = min_t(size_t, session->resp_size, session->bufsize);
  126. ret = simple_read_from_buffer(buff, count, ppos,
  127. session->response, size);
  128. mutex_unlock(&session->mutex);
  129. return ret;
  130. }
  131. static ssize_t
  132. vmcp_write(struct file *file, const char __user *buff, size_t count,
  133. loff_t *ppos)
  134. {
  135. char *cmd;
  136. struct vmcp_session *session;
  137. if (count > 240)
  138. return -EINVAL;
  139. cmd = memdup_user_nul(buff, count);
  140. if (IS_ERR(cmd))
  141. return PTR_ERR(cmd);
  142. session = file->private_data;
  143. if (mutex_lock_interruptible(&session->mutex)) {
  144. kfree(cmd);
  145. return -ERESTARTSYS;
  146. }
  147. if (!session->response)
  148. vmcp_response_alloc(session);
  149. if (!session->response) {
  150. mutex_unlock(&session->mutex);
  151. kfree(cmd);
  152. return -ENOMEM;
  153. }
  154. debug_text_event(vmcp_debug, 1, cmd);
  155. session->resp_size = cpcmd(cmd, session->response, session->bufsize,
  156. &session->resp_code);
  157. mutex_unlock(&session->mutex);
  158. kfree(cmd);
  159. *ppos = 0; /* reset the file pointer after a command */
  160. return count;
  161. }
  162. /*
  163. * These ioctls are available, as the semantics of the diagnose 8 call
  164. * does not fit very well into a Linux call. Diagnose X'08' is described in
  165. * CP Programming Services SC24-6084-00
  166. *
  167. * VMCP_GETCODE: gives the CP return code back to user space
  168. * VMCP_SETBUF: sets the response buffer for the next write call. diagnose 8
  169. * expects adjacent pages in real storage and to make matters worse, we
  170. * dont know the size of the response. Therefore we default to PAGESIZE and
  171. * let userspace to change the response size, if userspace expects a bigger
  172. * response
  173. */
  174. static long vmcp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  175. {
  176. struct vmcp_session *session;
  177. int ret = -ENOTTY;
  178. int __user *argp;
  179. session = file->private_data;
  180. if (is_compat_task())
  181. argp = compat_ptr(arg);
  182. else
  183. argp = (int __user *)arg;
  184. if (mutex_lock_interruptible(&session->mutex))
  185. return -ERESTARTSYS;
  186. switch (cmd) {
  187. case VMCP_GETCODE:
  188. ret = put_user(session->resp_code, argp);
  189. break;
  190. case VMCP_SETBUF:
  191. vmcp_response_free(session);
  192. ret = get_user(session->bufsize, argp);
  193. if (ret)
  194. session->bufsize = PAGE_SIZE;
  195. if (!session->bufsize || get_order(session->bufsize) > 8) {
  196. session->bufsize = PAGE_SIZE;
  197. ret = -EINVAL;
  198. }
  199. break;
  200. case VMCP_GETSIZE:
  201. ret = put_user(session->resp_size, argp);
  202. break;
  203. default:
  204. break;
  205. }
  206. mutex_unlock(&session->mutex);
  207. return ret;
  208. }
  209. static const struct file_operations vmcp_fops = {
  210. .owner = THIS_MODULE,
  211. .open = vmcp_open,
  212. .release = vmcp_release,
  213. .read = vmcp_read,
  214. .write = vmcp_write,
  215. .unlocked_ioctl = vmcp_ioctl,
  216. .compat_ioctl = vmcp_ioctl,
  217. .llseek = no_llseek,
  218. };
  219. static struct miscdevice vmcp_dev = {
  220. .name = "vmcp",
  221. .minor = MISC_DYNAMIC_MINOR,
  222. .fops = &vmcp_fops,
  223. };
  224. static int __init vmcp_init(void)
  225. {
  226. int ret;
  227. if (!MACHINE_IS_VM)
  228. return 0;
  229. vmcp_debug = debug_register("vmcp", 1, 1, 240);
  230. if (!vmcp_debug)
  231. return -ENOMEM;
  232. ret = debug_register_view(vmcp_debug, &debug_hex_ascii_view);
  233. if (ret) {
  234. debug_unregister(vmcp_debug);
  235. return ret;
  236. }
  237. ret = misc_register(&vmcp_dev);
  238. if (ret)
  239. debug_unregister(vmcp_debug);
  240. return ret;
  241. }
  242. device_initcall(vmcp_init);