vmcp.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 <[email protected]>
  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. if (!p)
  42. return 1;
  43. vmcp_cma_size = ALIGN(memparse(p, NULL), PAGE_SIZE);
  44. return 0;
  45. }
  46. early_param("vmcp_cma", early_parse_vmcp_cma);
  47. void __init vmcp_cma_reserve(void)
  48. {
  49. if (!MACHINE_IS_VM)
  50. return;
  51. cma_declare_contiguous(0, vmcp_cma_size, 0, 0, 0, false, "vmcp", &vmcp_cma);
  52. }
  53. static void vmcp_response_alloc(struct vmcp_session *session)
  54. {
  55. struct page *page = NULL;
  56. int nr_pages, order;
  57. order = get_order(session->bufsize);
  58. nr_pages = ALIGN(session->bufsize, PAGE_SIZE) >> PAGE_SHIFT;
  59. /*
  60. * For anything below order 3 allocations rely on the buddy
  61. * allocator. If such low-order allocations can't be handled
  62. * anymore the system won't work anyway.
  63. */
  64. if (order > 2)
  65. page = cma_alloc(vmcp_cma, nr_pages, 0, false);
  66. if (page) {
  67. session->response = (char *)page_to_virt(page);
  68. session->cma_alloc = 1;
  69. return;
  70. }
  71. session->response = (char *)__get_free_pages(GFP_KERNEL | __GFP_RETRY_MAYFAIL, order);
  72. }
  73. static void vmcp_response_free(struct vmcp_session *session)
  74. {
  75. int nr_pages, order;
  76. struct page *page;
  77. if (!session->response)
  78. return;
  79. order = get_order(session->bufsize);
  80. nr_pages = ALIGN(session->bufsize, PAGE_SIZE) >> PAGE_SHIFT;
  81. if (session->cma_alloc) {
  82. page = virt_to_page((unsigned long)session->response);
  83. cma_release(vmcp_cma, page, nr_pages);
  84. session->cma_alloc = 0;
  85. } else {
  86. free_pages((unsigned long)session->response, order);
  87. }
  88. session->response = NULL;
  89. }
  90. static int vmcp_open(struct inode *inode, struct file *file)
  91. {
  92. struct vmcp_session *session;
  93. if (!capable(CAP_SYS_ADMIN))
  94. return -EPERM;
  95. session = kmalloc(sizeof(*session), GFP_KERNEL);
  96. if (!session)
  97. return -ENOMEM;
  98. session->bufsize = PAGE_SIZE;
  99. session->response = NULL;
  100. session->resp_size = 0;
  101. mutex_init(&session->mutex);
  102. file->private_data = session;
  103. return nonseekable_open(inode, file);
  104. }
  105. static int vmcp_release(struct inode *inode, struct file *file)
  106. {
  107. struct vmcp_session *session;
  108. session = file->private_data;
  109. file->private_data = NULL;
  110. vmcp_response_free(session);
  111. kfree(session);
  112. return 0;
  113. }
  114. static ssize_t
  115. vmcp_read(struct file *file, char __user *buff, size_t count, loff_t *ppos)
  116. {
  117. ssize_t ret;
  118. size_t size;
  119. struct vmcp_session *session;
  120. session = file->private_data;
  121. if (mutex_lock_interruptible(&session->mutex))
  122. return -ERESTARTSYS;
  123. if (!session->response) {
  124. mutex_unlock(&session->mutex);
  125. return 0;
  126. }
  127. size = min_t(size_t, session->resp_size, session->bufsize);
  128. ret = simple_read_from_buffer(buff, count, ppos,
  129. session->response, size);
  130. mutex_unlock(&session->mutex);
  131. return ret;
  132. }
  133. static ssize_t
  134. vmcp_write(struct file *file, const char __user *buff, size_t count,
  135. loff_t *ppos)
  136. {
  137. char *cmd;
  138. struct vmcp_session *session;
  139. if (count > 240)
  140. return -EINVAL;
  141. cmd = memdup_user_nul(buff, count);
  142. if (IS_ERR(cmd))
  143. return PTR_ERR(cmd);
  144. session = file->private_data;
  145. if (mutex_lock_interruptible(&session->mutex)) {
  146. kfree(cmd);
  147. return -ERESTARTSYS;
  148. }
  149. if (!session->response)
  150. vmcp_response_alloc(session);
  151. if (!session->response) {
  152. mutex_unlock(&session->mutex);
  153. kfree(cmd);
  154. return -ENOMEM;
  155. }
  156. debug_text_event(vmcp_debug, 1, cmd);
  157. session->resp_size = cpcmd(cmd, session->response, session->bufsize,
  158. &session->resp_code);
  159. mutex_unlock(&session->mutex);
  160. kfree(cmd);
  161. *ppos = 0; /* reset the file pointer after a command */
  162. return count;
  163. }
  164. /*
  165. * These ioctls are available, as the semantics of the diagnose 8 call
  166. * does not fit very well into a Linux call. Diagnose X'08' is described in
  167. * CP Programming Services SC24-6084-00
  168. *
  169. * VMCP_GETCODE: gives the CP return code back to user space
  170. * VMCP_SETBUF: sets the response buffer for the next write call. diagnose 8
  171. * expects adjacent pages in real storage and to make matters worse, we
  172. * dont know the size of the response. Therefore we default to PAGESIZE and
  173. * let userspace to change the response size, if userspace expects a bigger
  174. * response
  175. */
  176. static long vmcp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  177. {
  178. struct vmcp_session *session;
  179. int ret = -ENOTTY;
  180. int __user *argp;
  181. session = file->private_data;
  182. if (is_compat_task())
  183. argp = compat_ptr(arg);
  184. else
  185. argp = (int __user *)arg;
  186. if (mutex_lock_interruptible(&session->mutex))
  187. return -ERESTARTSYS;
  188. switch (cmd) {
  189. case VMCP_GETCODE:
  190. ret = put_user(session->resp_code, argp);
  191. break;
  192. case VMCP_SETBUF:
  193. vmcp_response_free(session);
  194. ret = get_user(session->bufsize, argp);
  195. if (ret)
  196. session->bufsize = PAGE_SIZE;
  197. if (!session->bufsize || get_order(session->bufsize) > 8) {
  198. session->bufsize = PAGE_SIZE;
  199. ret = -EINVAL;
  200. }
  201. break;
  202. case VMCP_GETSIZE:
  203. ret = put_user(session->resp_size, argp);
  204. break;
  205. default:
  206. break;
  207. }
  208. mutex_unlock(&session->mutex);
  209. return ret;
  210. }
  211. static const struct file_operations vmcp_fops = {
  212. .owner = THIS_MODULE,
  213. .open = vmcp_open,
  214. .release = vmcp_release,
  215. .read = vmcp_read,
  216. .write = vmcp_write,
  217. .unlocked_ioctl = vmcp_ioctl,
  218. .compat_ioctl = vmcp_ioctl,
  219. .llseek = no_llseek,
  220. };
  221. static struct miscdevice vmcp_dev = {
  222. .name = "vmcp",
  223. .minor = MISC_DYNAMIC_MINOR,
  224. .fops = &vmcp_fops,
  225. };
  226. static int __init vmcp_init(void)
  227. {
  228. int ret;
  229. if (!MACHINE_IS_VM)
  230. return 0;
  231. vmcp_debug = debug_register("vmcp", 1, 1, 240);
  232. if (!vmcp_debug)
  233. return -ENOMEM;
  234. ret = debug_register_view(vmcp_debug, &debug_hex_ascii_view);
  235. if (ret) {
  236. debug_unregister(vmcp_debug);
  237. return ret;
  238. }
  239. ret = misc_register(&vmcp_dev);
  240. if (ret)
  241. debug_unregister(vmcp_debug);
  242. return ret;
  243. }
  244. device_initcall(vmcp_init);