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