1 /* SPDX-License-Identifier: (GPL-2.0 OR CDDL-1.0) */
2 /*
3  * vboxguest vmm-req and hgcm-call code, VBoxGuestR0LibHGCMInternal.cpp,
4  * VBoxGuestR0LibGenericRequest.cpp and RTErrConvertToErrno.cpp in vbox svn.
5  *
6  * Copyright (C) 2006-2016 Oracle Corporation
7  */
8 
9 #include <linux/errno.h>
10 #include <linux/io.h>
11 #include <linux/kernel.h>
12 #include <linux/mm.h>
13 #include <linux/module.h>
14 #include <linux/sizes.h>
15 #include <linux/slab.h>
16 #include <linux/uaccess.h>
17 #include <linux/vmalloc.h>
18 #include <linux/vbox_err.h>
19 #include <linux/vbox_utils.h>
20 #include "vboxguest_core.h"
21 
22 /* Get the pointer to the first parameter of a HGCM call request. */
23 #define VMMDEV_HGCM_CALL_PARMS(a) \
24 	((struct vmmdev_hgcm_function_parameter *)( \
25 		(u8 *)(a) + sizeof(struct vmmdev_hgcm_call)))
26 
27 /* The max parameter buffer size for a user request. */
28 #define VBG_MAX_HGCM_USER_PARM		(24 * SZ_1M)
29 /* The max parameter buffer size for a kernel request. */
30 #define VBG_MAX_HGCM_KERNEL_PARM	(16 * SZ_1M)
31 
32 #define VBG_DEBUG_PORT			0x504
33 
34 /* This protects vbg_log_buf and serializes VBG_DEBUG_PORT accesses */
35 static DEFINE_SPINLOCK(vbg_log_lock);
36 static char vbg_log_buf[128];
37 
38 #define VBG_LOG(name, pr_func) \
39 void name(const char *fmt, ...)						\
40 {									\
41 	unsigned long flags;						\
42 	va_list args;							\
43 	int i, count;							\
44 									\
45 	va_start(args, fmt);						\
46 	spin_lock_irqsave(&vbg_log_lock, flags);			\
47 									\
48 	count = vscnprintf(vbg_log_buf, sizeof(vbg_log_buf), fmt, args);\
49 	for (i = 0; i < count; i++)					\
50 		outb(vbg_log_buf[i], VBG_DEBUG_PORT);			\
51 									\
52 	pr_func("%s", vbg_log_buf);					\
53 									\
54 	spin_unlock_irqrestore(&vbg_log_lock, flags);			\
55 	va_end(args);							\
56 }									\
57 EXPORT_SYMBOL(name)
58 
59 VBG_LOG(vbg_info, pr_info);
60 VBG_LOG(vbg_warn, pr_warn);
61 VBG_LOG(vbg_err, pr_err);
62 #if defined(DEBUG) && !defined(CONFIG_DYNAMIC_DEBUG)
63 VBG_LOG(vbg_debug, pr_debug);
64 #endif
65 
66 void *vbg_req_alloc(size_t len, enum vmmdev_request_type req_type,
67 		    u32 requestor)
68 {
69 	struct vmmdev_request_header *req;
70 	int order = get_order(PAGE_ALIGN(len));
71 
72 	req = (void *)__get_free_pages(GFP_KERNEL | GFP_DMA32, order);
73 	if (!req)
74 		return NULL;
75 
76 	memset(req, 0xaa, len);
77 
78 	req->size = len;
79 	req->version = VMMDEV_REQUEST_HEADER_VERSION;
80 	req->request_type = req_type;
81 	req->rc = VERR_GENERAL_FAILURE;
82 	req->reserved1 = 0;
83 	req->requestor = requestor;
84 
85 	return req;
86 }
87 
88 void vbg_req_free(void *req, size_t len)
89 {
90 	if (!req)
91 		return;
92 
93 	free_pages((unsigned long)req, get_order(PAGE_ALIGN(len)));
94 }
95 
96 /* Note this function returns a VBox status code, not a negative errno!! */
97 int vbg_req_perform(struct vbg_dev *gdev, void *req)
98 {
99 	unsigned long phys_req = virt_to_phys(req);
100 
101 	outl(phys_req, gdev->io_port + VMMDEV_PORT_OFF_REQUEST);
102 	/*
103 	 * The host changes the request as a result of the outl, make sure
104 	 * the outl and any reads of the req happen in the correct order.
105 	 */
106 	mb();
107 
108 	return ((struct vmmdev_request_header *)req)->rc;
109 }
110 
111 static bool hgcm_req_done(struct vbg_dev *gdev,
112 			  struct vmmdev_hgcmreq_header *header)
113 {
114 	unsigned long flags;
115 	bool done;
116 
117 	spin_lock_irqsave(&gdev->event_spinlock, flags);
118 	done = header->flags & VMMDEV_HGCM_REQ_DONE;
119 	spin_unlock_irqrestore(&gdev->event_spinlock, flags);
120 
121 	return done;
122 }
123 
124 int vbg_hgcm_connect(struct vbg_dev *gdev, u32 requestor,
125 		     struct vmmdev_hgcm_service_location *loc,
126 		     u32 *client_id, int *vbox_status)
127 {
128 	struct vmmdev_hgcm_connect *hgcm_connect = NULL;
129 	int rc;
130 
131 	hgcm_connect = vbg_req_alloc(sizeof(*hgcm_connect),
132 				     VMMDEVREQ_HGCM_CONNECT, requestor);
133 	if (!hgcm_connect)
134 		return -ENOMEM;
135 
136 	hgcm_connect->header.flags = 0;
137 	memcpy(&hgcm_connect->loc, loc, sizeof(*loc));
138 	hgcm_connect->client_id = 0;
139 
140 	rc = vbg_req_perform(gdev, hgcm_connect);
141 
142 	if (rc == VINF_HGCM_ASYNC_EXECUTE)
143 		wait_event(gdev->hgcm_wq,
144 			   hgcm_req_done(gdev, &hgcm_connect->header));
145 
146 	if (rc >= 0) {
147 		*client_id = hgcm_connect->client_id;
148 		rc = hgcm_connect->header.result;
149 	}
150 
151 	vbg_req_free(hgcm_connect, sizeof(*hgcm_connect));
152 
153 	*vbox_status = rc;
154 	return 0;
155 }
156 EXPORT_SYMBOL(vbg_hgcm_connect);
157 
158 int vbg_hgcm_disconnect(struct vbg_dev *gdev, u32 requestor,
159 			u32 client_id, int *vbox_status)
160 {
161 	struct vmmdev_hgcm_disconnect *hgcm_disconnect = NULL;
162 	int rc;
163 
164 	hgcm_disconnect = vbg_req_alloc(sizeof(*hgcm_disconnect),
165 					VMMDEVREQ_HGCM_DISCONNECT,
166 					requestor);
167 	if (!hgcm_disconnect)
168 		return -ENOMEM;
169 
170 	hgcm_disconnect->header.flags = 0;
171 	hgcm_disconnect->client_id = client_id;
172 
173 	rc = vbg_req_perform(gdev, hgcm_disconnect);
174 
175 	if (rc == VINF_HGCM_ASYNC_EXECUTE)
176 		wait_event(gdev->hgcm_wq,
177 			   hgcm_req_done(gdev, &hgcm_disconnect->header));
178 
179 	if (rc >= 0)
180 		rc = hgcm_disconnect->header.result;
181 
182 	vbg_req_free(hgcm_disconnect, sizeof(*hgcm_disconnect));
183 
184 	*vbox_status = rc;
185 	return 0;
186 }
187 EXPORT_SYMBOL(vbg_hgcm_disconnect);
188 
189 static u32 hgcm_call_buf_size_in_pages(void *buf, u32 len)
190 {
191 	u32 size = PAGE_ALIGN(len + ((unsigned long)buf & ~PAGE_MASK));
192 
193 	return size >> PAGE_SHIFT;
194 }
195 
196 static void hgcm_call_add_pagelist_size(void *buf, u32 len, size_t *extra)
197 {
198 	u32 page_count;
199 
200 	page_count = hgcm_call_buf_size_in_pages(buf, len);
201 	*extra += offsetof(struct vmmdev_hgcm_pagelist, pages[page_count]);
202 }
203 
204 static int hgcm_call_preprocess_linaddr(
205 	const struct vmmdev_hgcm_function_parameter *src_parm,
206 	void **bounce_buf_ret, size_t *extra)
207 {
208 	void *buf, *bounce_buf;
209 	bool copy_in;
210 	u32 len;
211 	int ret;
212 
213 	buf = (void *)src_parm->u.pointer.u.linear_addr;
214 	len = src_parm->u.pointer.size;
215 	copy_in = src_parm->type != VMMDEV_HGCM_PARM_TYPE_LINADDR_OUT;
216 
217 	if (len > VBG_MAX_HGCM_USER_PARM)
218 		return -E2BIG;
219 
220 	bounce_buf = kvmalloc(len, GFP_KERNEL);
221 	if (!bounce_buf)
222 		return -ENOMEM;
223 
224 	*bounce_buf_ret = bounce_buf;
225 
226 	if (copy_in) {
227 		ret = copy_from_user(bounce_buf, (void __user *)buf, len);
228 		if (ret)
229 			return -EFAULT;
230 	} else {
231 		memset(bounce_buf, 0, len);
232 	}
233 
234 	hgcm_call_add_pagelist_size(bounce_buf, len, extra);
235 	return 0;
236 }
237 
238 /**
239  * Preprocesses the HGCM call, validate parameters, alloc bounce buffers and
240  * figure out how much extra storage we need for page lists.
241  * Return: 0 or negative errno value.
242  * @src_parm:         Pointer to source function call parameters
243  * @parm_count:       Number of function call parameters.
244  * @bounce_bufs_ret:  Where to return the allocated bouncebuffer array
245  * @extra:            Where to return the extra request space needed for
246  *                    physical page lists.
247  */
248 static int hgcm_call_preprocess(
249 	const struct vmmdev_hgcm_function_parameter *src_parm,
250 	u32 parm_count, void ***bounce_bufs_ret, size_t *extra)
251 {
252 	void *buf, **bounce_bufs = NULL;
253 	u32 i, len;
254 	int ret;
255 
256 	for (i = 0; i < parm_count; i++, src_parm++) {
257 		switch (src_parm->type) {
258 		case VMMDEV_HGCM_PARM_TYPE_32BIT:
259 		case VMMDEV_HGCM_PARM_TYPE_64BIT:
260 			break;
261 
262 		case VMMDEV_HGCM_PARM_TYPE_LINADDR:
263 		case VMMDEV_HGCM_PARM_TYPE_LINADDR_IN:
264 		case VMMDEV_HGCM_PARM_TYPE_LINADDR_OUT:
265 			if (!bounce_bufs) {
266 				bounce_bufs = kcalloc(parm_count,
267 						      sizeof(void *),
268 						      GFP_KERNEL);
269 				if (!bounce_bufs)
270 					return -ENOMEM;
271 
272 				*bounce_bufs_ret = bounce_bufs;
273 			}
274 
275 			ret = hgcm_call_preprocess_linaddr(src_parm,
276 							   &bounce_bufs[i],
277 							   extra);
278 			if (ret)
279 				return ret;
280 
281 			break;
282 
283 		case VMMDEV_HGCM_PARM_TYPE_LINADDR_KERNEL:
284 		case VMMDEV_HGCM_PARM_TYPE_LINADDR_KERNEL_IN:
285 		case VMMDEV_HGCM_PARM_TYPE_LINADDR_KERNEL_OUT:
286 			buf = (void *)src_parm->u.pointer.u.linear_addr;
287 			len = src_parm->u.pointer.size;
288 			if (WARN_ON(len > VBG_MAX_HGCM_KERNEL_PARM))
289 				return -E2BIG;
290 
291 			hgcm_call_add_pagelist_size(buf, len, extra);
292 			break;
293 
294 		default:
295 			return -EINVAL;
296 		}
297 	}
298 
299 	return 0;
300 }
301 
302 /**
303  * Translates linear address types to page list direction flags.
304  *
305  * Return: page list flags.
306  * @type:  The type.
307  */
308 static u32 hgcm_call_linear_addr_type_to_pagelist_flags(
309 	enum vmmdev_hgcm_function_parameter_type type)
310 {
311 	switch (type) {
312 	default:
313 		WARN_ON(1);
314 		fallthrough;
315 	case VMMDEV_HGCM_PARM_TYPE_LINADDR:
316 	case VMMDEV_HGCM_PARM_TYPE_LINADDR_KERNEL:
317 		return VMMDEV_HGCM_F_PARM_DIRECTION_BOTH;
318 
319 	case VMMDEV_HGCM_PARM_TYPE_LINADDR_IN:
320 	case VMMDEV_HGCM_PARM_TYPE_LINADDR_KERNEL_IN:
321 		return VMMDEV_HGCM_F_PARM_DIRECTION_TO_HOST;
322 
323 	case VMMDEV_HGCM_PARM_TYPE_LINADDR_OUT:
324 	case VMMDEV_HGCM_PARM_TYPE_LINADDR_KERNEL_OUT:
325 		return VMMDEV_HGCM_F_PARM_DIRECTION_FROM_HOST;
326 	}
327 }
328 
329 static void hgcm_call_init_linaddr(struct vmmdev_hgcm_call *call,
330 	struct vmmdev_hgcm_function_parameter *dst_parm, void *buf, u32 len,
331 	enum vmmdev_hgcm_function_parameter_type type, u32 *off_extra)
332 {
333 	struct vmmdev_hgcm_pagelist *dst_pg_lst;
334 	struct page *page;
335 	bool is_vmalloc;
336 	u32 i, page_count;
337 
338 	dst_parm->type = type;
339 
340 	if (len == 0) {
341 		dst_parm->u.pointer.size = 0;
342 		dst_parm->u.pointer.u.linear_addr = 0;
343 		return;
344 	}
345 
346 	dst_pg_lst = (void *)call + *off_extra;
347 	page_count = hgcm_call_buf_size_in_pages(buf, len);
348 	is_vmalloc = is_vmalloc_addr(buf);
349 
350 	dst_parm->type = VMMDEV_HGCM_PARM_TYPE_PAGELIST;
351 	dst_parm->u.page_list.size = len;
352 	dst_parm->u.page_list.offset = *off_extra;
353 	dst_pg_lst->flags = hgcm_call_linear_addr_type_to_pagelist_flags(type);
354 	dst_pg_lst->offset_first_page = (unsigned long)buf & ~PAGE_MASK;
355 	dst_pg_lst->page_count = page_count;
356 
357 	for (i = 0; i < page_count; i++) {
358 		if (is_vmalloc)
359 			page = vmalloc_to_page(buf);
360 		else
361 			page = virt_to_page(buf);
362 
363 		dst_pg_lst->pages[i] = page_to_phys(page);
364 		buf += PAGE_SIZE;
365 	}
366 
367 	*off_extra += offsetof(struct vmmdev_hgcm_pagelist, pages[page_count]);
368 }
369 
370 /**
371  * Initializes the call request that we're sending to the host.
372  * @call:            The call to initialize.
373  * @client_id:       The client ID of the caller.
374  * @function:        The function number of the function to call.
375  * @src_parm:        Pointer to source function call parameters.
376  * @parm_count:      Number of function call parameters.
377  * @bounce_bufs:     The bouncebuffer array.
378  */
379 static void hgcm_call_init_call(
380 	struct vmmdev_hgcm_call *call, u32 client_id, u32 function,
381 	const struct vmmdev_hgcm_function_parameter *src_parm,
382 	u32 parm_count, void **bounce_bufs)
383 {
384 	struct vmmdev_hgcm_function_parameter *dst_parm =
385 		VMMDEV_HGCM_CALL_PARMS(call);
386 	u32 i, off_extra = (uintptr_t)(dst_parm + parm_count) - (uintptr_t)call;
387 	void *buf;
388 
389 	call->header.flags = 0;
390 	call->header.result = VINF_SUCCESS;
391 	call->client_id = client_id;
392 	call->function = function;
393 	call->parm_count = parm_count;
394 
395 	for (i = 0; i < parm_count; i++, src_parm++, dst_parm++) {
396 		switch (src_parm->type) {
397 		case VMMDEV_HGCM_PARM_TYPE_32BIT:
398 		case VMMDEV_HGCM_PARM_TYPE_64BIT:
399 			*dst_parm = *src_parm;
400 			break;
401 
402 		case VMMDEV_HGCM_PARM_TYPE_LINADDR:
403 		case VMMDEV_HGCM_PARM_TYPE_LINADDR_IN:
404 		case VMMDEV_HGCM_PARM_TYPE_LINADDR_OUT:
405 			hgcm_call_init_linaddr(call, dst_parm, bounce_bufs[i],
406 					       src_parm->u.pointer.size,
407 					       src_parm->type, &off_extra);
408 			break;
409 
410 		case VMMDEV_HGCM_PARM_TYPE_LINADDR_KERNEL:
411 		case VMMDEV_HGCM_PARM_TYPE_LINADDR_KERNEL_IN:
412 		case VMMDEV_HGCM_PARM_TYPE_LINADDR_KERNEL_OUT:
413 			buf = (void *)src_parm->u.pointer.u.linear_addr;
414 			hgcm_call_init_linaddr(call, dst_parm, buf,
415 					       src_parm->u.pointer.size,
416 					       src_parm->type, &off_extra);
417 			break;
418 
419 		default:
420 			WARN_ON(1);
421 			dst_parm->type = VMMDEV_HGCM_PARM_TYPE_INVALID;
422 		}
423 	}
424 }
425 
426 /**
427  * Tries to cancel a pending HGCM call.
428  *
429  * Return: VBox status code
430  */
431 static int hgcm_cancel_call(struct vbg_dev *gdev, struct vmmdev_hgcm_call *call)
432 {
433 	int rc;
434 
435 	/*
436 	 * We use a pre-allocated request for cancellations, which is
437 	 * protected by cancel_req_mutex. This means that all cancellations
438 	 * get serialized, this should be fine since they should be rare.
439 	 */
440 	mutex_lock(&gdev->cancel_req_mutex);
441 	gdev->cancel_req->phys_req_to_cancel = virt_to_phys(call);
442 	rc = vbg_req_perform(gdev, gdev->cancel_req);
443 	mutex_unlock(&gdev->cancel_req_mutex);
444 
445 	if (rc == VERR_NOT_IMPLEMENTED) {
446 		call->header.flags |= VMMDEV_HGCM_REQ_CANCELLED;
447 		call->header.header.request_type = VMMDEVREQ_HGCM_CANCEL;
448 
449 		rc = vbg_req_perform(gdev, call);
450 		if (rc == VERR_INVALID_PARAMETER)
451 			rc = VERR_NOT_FOUND;
452 	}
453 
454 	if (rc >= 0)
455 		call->header.flags |= VMMDEV_HGCM_REQ_CANCELLED;
456 
457 	return rc;
458 }
459 
460 /**
461  * Performs the call and completion wait.
462  * Return: 0 or negative errno value.
463  * @gdev:        The VBoxGuest device extension.
464  * @call:        The call to execute.
465  * @timeout_ms:  Timeout in ms.
466  * @leak_it:     Where to return the leak it / free it, indicator.
467  *               Cancellation fun.
468  */
469 static int vbg_hgcm_do_call(struct vbg_dev *gdev, struct vmmdev_hgcm_call *call,
470 			    u32 timeout_ms, bool *leak_it)
471 {
472 	int rc, cancel_rc, ret;
473 	long timeout;
474 
475 	*leak_it = false;
476 
477 	rc = vbg_req_perform(gdev, call);
478 
479 	/*
480 	 * If the call failed, then pretend success. Upper layers will
481 	 * interpret the result code in the packet.
482 	 */
483 	if (rc < 0) {
484 		call->header.result = rc;
485 		return 0;
486 	}
487 
488 	if (rc != VINF_HGCM_ASYNC_EXECUTE)
489 		return 0;
490 
491 	/* Host decided to process the request asynchronously, wait for it */
492 	if (timeout_ms == U32_MAX)
493 		timeout = MAX_SCHEDULE_TIMEOUT;
494 	else
495 		timeout = msecs_to_jiffies(timeout_ms);
496 
497 	timeout = wait_event_interruptible_timeout(
498 					gdev->hgcm_wq,
499 					hgcm_req_done(gdev, &call->header),
500 					timeout);
501 
502 	/* timeout > 0 means hgcm_req_done has returned true, so success */
503 	if (timeout > 0)
504 		return 0;
505 
506 	if (timeout == 0)
507 		ret = -ETIMEDOUT;
508 	else
509 		ret = -EINTR;
510 
511 	/* Cancel the request */
512 	cancel_rc = hgcm_cancel_call(gdev, call);
513 	if (cancel_rc >= 0)
514 		return ret;
515 
516 	/*
517 	 * Failed to cancel, this should mean that the cancel has lost the
518 	 * race with normal completion, wait while the host completes it.
519 	 */
520 	if (cancel_rc == VERR_NOT_FOUND || cancel_rc == VERR_SEM_DESTROYED)
521 		timeout = msecs_to_jiffies(500);
522 	else
523 		timeout = msecs_to_jiffies(2000);
524 
525 	timeout = wait_event_timeout(gdev->hgcm_wq,
526 				     hgcm_req_done(gdev, &call->header),
527 				     timeout);
528 
529 	if (WARN_ON(timeout == 0)) {
530 		/* We really should never get here */
531 		vbg_err("%s: Call timedout and cancellation failed, leaking the request\n",
532 			__func__);
533 		*leak_it = true;
534 		return ret;
535 	}
536 
537 	/* The call has completed normally after all */
538 	return 0;
539 }
540 
541 /**
542  * Copies the result of the call back to the caller info structure and user
543  * buffers.
544  * Return: 0 or negative errno value.
545  * @call:            HGCM call request.
546  * @dst_parm:        Pointer to function call parameters destination.
547  * @parm_count:      Number of function call parameters.
548  * @bounce_bufs:     The bouncebuffer array.
549  */
550 static int hgcm_call_copy_back_result(
551 	const struct vmmdev_hgcm_call *call,
552 	struct vmmdev_hgcm_function_parameter *dst_parm,
553 	u32 parm_count, void **bounce_bufs)
554 {
555 	const struct vmmdev_hgcm_function_parameter *src_parm =
556 		VMMDEV_HGCM_CALL_PARMS(call);
557 	void __user *p;
558 	int ret;
559 	u32 i;
560 
561 	/* Copy back parameters. */
562 	for (i = 0; i < parm_count; i++, src_parm++, dst_parm++) {
563 		switch (dst_parm->type) {
564 		case VMMDEV_HGCM_PARM_TYPE_32BIT:
565 		case VMMDEV_HGCM_PARM_TYPE_64BIT:
566 			*dst_parm = *src_parm;
567 			break;
568 
569 		case VMMDEV_HGCM_PARM_TYPE_PAGELIST:
570 			dst_parm->u.page_list.size = src_parm->u.page_list.size;
571 			break;
572 
573 		case VMMDEV_HGCM_PARM_TYPE_LINADDR_IN:
574 		case VMMDEV_HGCM_PARM_TYPE_LINADDR_KERNEL:
575 		case VMMDEV_HGCM_PARM_TYPE_LINADDR_KERNEL_IN:
576 		case VMMDEV_HGCM_PARM_TYPE_LINADDR_KERNEL_OUT:
577 			dst_parm->u.pointer.size = src_parm->u.pointer.size;
578 			break;
579 
580 		case VMMDEV_HGCM_PARM_TYPE_LINADDR:
581 		case VMMDEV_HGCM_PARM_TYPE_LINADDR_OUT:
582 			dst_parm->u.pointer.size = src_parm->u.pointer.size;
583 
584 			p = (void __user *)dst_parm->u.pointer.u.linear_addr;
585 			ret = copy_to_user(p, bounce_bufs[i],
586 					   min(src_parm->u.pointer.size,
587 					       dst_parm->u.pointer.size));
588 			if (ret)
589 				return -EFAULT;
590 			break;
591 
592 		default:
593 			WARN_ON(1);
594 			return -EINVAL;
595 		}
596 	}
597 
598 	return 0;
599 }
600 
601 int vbg_hgcm_call(struct vbg_dev *gdev, u32 requestor, u32 client_id,
602 		  u32 function, u32 timeout_ms,
603 		  struct vmmdev_hgcm_function_parameter *parms, u32 parm_count,
604 		  int *vbox_status)
605 {
606 	struct vmmdev_hgcm_call *call;
607 	void **bounce_bufs = NULL;
608 	bool leak_it;
609 	size_t size;
610 	int i, ret;
611 
612 	size = sizeof(struct vmmdev_hgcm_call) +
613 		   parm_count * sizeof(struct vmmdev_hgcm_function_parameter);
614 	/*
615 	 * Validate and buffer the parameters for the call. This also increases
616 	 * call_size with the amount of extra space needed for page lists.
617 	 */
618 	ret = hgcm_call_preprocess(parms, parm_count, &bounce_bufs, &size);
619 	if (ret) {
620 		/* Even on error bounce bufs may still have been allocated */
621 		goto free_bounce_bufs;
622 	}
623 
624 	call = vbg_req_alloc(size, VMMDEVREQ_HGCM_CALL, requestor);
625 	if (!call) {
626 		ret = -ENOMEM;
627 		goto free_bounce_bufs;
628 	}
629 
630 	hgcm_call_init_call(call, client_id, function, parms, parm_count,
631 			    bounce_bufs);
632 
633 	ret = vbg_hgcm_do_call(gdev, call, timeout_ms, &leak_it);
634 	if (ret == 0) {
635 		*vbox_status = call->header.result;
636 		ret = hgcm_call_copy_back_result(call, parms, parm_count,
637 						 bounce_bufs);
638 	}
639 
640 	if (!leak_it)
641 		vbg_req_free(call, size);
642 
643 free_bounce_bufs:
644 	if (bounce_bufs) {
645 		for (i = 0; i < parm_count; i++)
646 			kvfree(bounce_bufs[i]);
647 		kfree(bounce_bufs);
648 	}
649 
650 	return ret;
651 }
652 EXPORT_SYMBOL(vbg_hgcm_call);
653 
654 #ifdef CONFIG_COMPAT
655 int vbg_hgcm_call32(
656 	struct vbg_dev *gdev, u32 requestor, u32 client_id, u32 function,
657 	u32 timeout_ms, struct vmmdev_hgcm_function_parameter32 *parm32,
658 	u32 parm_count, int *vbox_status)
659 {
660 	struct vmmdev_hgcm_function_parameter *parm64 = NULL;
661 	u32 i, size;
662 	int ret = 0;
663 
664 	/* KISS allocate a temporary request and convert the parameters. */
665 	size = parm_count * sizeof(struct vmmdev_hgcm_function_parameter);
666 	parm64 = kzalloc(size, GFP_KERNEL);
667 	if (!parm64)
668 		return -ENOMEM;
669 
670 	for (i = 0; i < parm_count; i++) {
671 		switch (parm32[i].type) {
672 		case VMMDEV_HGCM_PARM_TYPE_32BIT:
673 			parm64[i].type = VMMDEV_HGCM_PARM_TYPE_32BIT;
674 			parm64[i].u.value32 = parm32[i].u.value32;
675 			break;
676 
677 		case VMMDEV_HGCM_PARM_TYPE_64BIT:
678 			parm64[i].type = VMMDEV_HGCM_PARM_TYPE_64BIT;
679 			parm64[i].u.value64 = parm32[i].u.value64;
680 			break;
681 
682 		case VMMDEV_HGCM_PARM_TYPE_LINADDR_OUT:
683 		case VMMDEV_HGCM_PARM_TYPE_LINADDR:
684 		case VMMDEV_HGCM_PARM_TYPE_LINADDR_IN:
685 			parm64[i].type = parm32[i].type;
686 			parm64[i].u.pointer.size = parm32[i].u.pointer.size;
687 			parm64[i].u.pointer.u.linear_addr =
688 			    parm32[i].u.pointer.u.linear_addr;
689 			break;
690 
691 		default:
692 			ret = -EINVAL;
693 		}
694 		if (ret < 0)
695 			goto out_free;
696 	}
697 
698 	ret = vbg_hgcm_call(gdev, requestor, client_id, function, timeout_ms,
699 			    parm64, parm_count, vbox_status);
700 	if (ret < 0)
701 		goto out_free;
702 
703 	/* Copy back. */
704 	for (i = 0; i < parm_count; i++, parm32++, parm64++) {
705 		switch (parm64[i].type) {
706 		case VMMDEV_HGCM_PARM_TYPE_32BIT:
707 			parm32[i].u.value32 = parm64[i].u.value32;
708 			break;
709 
710 		case VMMDEV_HGCM_PARM_TYPE_64BIT:
711 			parm32[i].u.value64 = parm64[i].u.value64;
712 			break;
713 
714 		case VMMDEV_HGCM_PARM_TYPE_LINADDR_OUT:
715 		case VMMDEV_HGCM_PARM_TYPE_LINADDR:
716 		case VMMDEV_HGCM_PARM_TYPE_LINADDR_IN:
717 			parm32[i].u.pointer.size = parm64[i].u.pointer.size;
718 			break;
719 
720 		default:
721 			WARN_ON(1);
722 			ret = -EINVAL;
723 		}
724 	}
725 
726 out_free:
727 	kfree(parm64);
728 	return ret;
729 }
730 #endif
731 
732 static const int vbg_status_code_to_errno_table[] = {
733 	[-VERR_ACCESS_DENIED]                            = -EPERM,
734 	[-VERR_FILE_NOT_FOUND]                           = -ENOENT,
735 	[-VERR_PROCESS_NOT_FOUND]                        = -ESRCH,
736 	[-VERR_INTERRUPTED]                              = -EINTR,
737 	[-VERR_DEV_IO_ERROR]                             = -EIO,
738 	[-VERR_TOO_MUCH_DATA]                            = -E2BIG,
739 	[-VERR_BAD_EXE_FORMAT]                           = -ENOEXEC,
740 	[-VERR_INVALID_HANDLE]                           = -EBADF,
741 	[-VERR_TRY_AGAIN]                                = -EAGAIN,
742 	[-VERR_NO_MEMORY]                                = -ENOMEM,
743 	[-VERR_INVALID_POINTER]                          = -EFAULT,
744 	[-VERR_RESOURCE_BUSY]                            = -EBUSY,
745 	[-VERR_ALREADY_EXISTS]                           = -EEXIST,
746 	[-VERR_NOT_SAME_DEVICE]                          = -EXDEV,
747 	[-VERR_NOT_A_DIRECTORY]                          = -ENOTDIR,
748 	[-VERR_PATH_NOT_FOUND]                           = -ENOTDIR,
749 	[-VERR_INVALID_NAME]                             = -ENOENT,
750 	[-VERR_IS_A_DIRECTORY]                           = -EISDIR,
751 	[-VERR_INVALID_PARAMETER]                        = -EINVAL,
752 	[-VERR_TOO_MANY_OPEN_FILES]                      = -ENFILE,
753 	[-VERR_INVALID_FUNCTION]                         = -ENOTTY,
754 	[-VERR_SHARING_VIOLATION]                        = -ETXTBSY,
755 	[-VERR_FILE_TOO_BIG]                             = -EFBIG,
756 	[-VERR_DISK_FULL]                                = -ENOSPC,
757 	[-VERR_SEEK_ON_DEVICE]                           = -ESPIPE,
758 	[-VERR_WRITE_PROTECT]                            = -EROFS,
759 	[-VERR_BROKEN_PIPE]                              = -EPIPE,
760 	[-VERR_DEADLOCK]                                 = -EDEADLK,
761 	[-VERR_FILENAME_TOO_LONG]                        = -ENAMETOOLONG,
762 	[-VERR_FILE_LOCK_FAILED]                         = -ENOLCK,
763 	[-VERR_NOT_IMPLEMENTED]                          = -ENOSYS,
764 	[-VERR_NOT_SUPPORTED]                            = -ENOSYS,
765 	[-VERR_DIR_NOT_EMPTY]                            = -ENOTEMPTY,
766 	[-VERR_TOO_MANY_SYMLINKS]                        = -ELOOP,
767 	[-VERR_NO_MORE_FILES]				 = -ENODATA,
768 	[-VERR_NO_DATA]                                  = -ENODATA,
769 	[-VERR_NET_NO_NETWORK]                           = -ENONET,
770 	[-VERR_NET_NOT_UNIQUE_NAME]                      = -ENOTUNIQ,
771 	[-VERR_NO_TRANSLATION]                           = -EILSEQ,
772 	[-VERR_NET_NOT_SOCKET]                           = -ENOTSOCK,
773 	[-VERR_NET_DEST_ADDRESS_REQUIRED]                = -EDESTADDRREQ,
774 	[-VERR_NET_MSG_SIZE]                             = -EMSGSIZE,
775 	[-VERR_NET_PROTOCOL_TYPE]                        = -EPROTOTYPE,
776 	[-VERR_NET_PROTOCOL_NOT_AVAILABLE]               = -ENOPROTOOPT,
777 	[-VERR_NET_PROTOCOL_NOT_SUPPORTED]               = -EPROTONOSUPPORT,
778 	[-VERR_NET_SOCKET_TYPE_NOT_SUPPORTED]            = -ESOCKTNOSUPPORT,
779 	[-VERR_NET_OPERATION_NOT_SUPPORTED]              = -EOPNOTSUPP,
780 	[-VERR_NET_PROTOCOL_FAMILY_NOT_SUPPORTED]        = -EPFNOSUPPORT,
781 	[-VERR_NET_ADDRESS_FAMILY_NOT_SUPPORTED]         = -EAFNOSUPPORT,
782 	[-VERR_NET_ADDRESS_IN_USE]                       = -EADDRINUSE,
783 	[-VERR_NET_ADDRESS_NOT_AVAILABLE]                = -EADDRNOTAVAIL,
784 	[-VERR_NET_DOWN]                                 = -ENETDOWN,
785 	[-VERR_NET_UNREACHABLE]                          = -ENETUNREACH,
786 	[-VERR_NET_CONNECTION_RESET]                     = -ENETRESET,
787 	[-VERR_NET_CONNECTION_ABORTED]                   = -ECONNABORTED,
788 	[-VERR_NET_CONNECTION_RESET_BY_PEER]             = -ECONNRESET,
789 	[-VERR_NET_NO_BUFFER_SPACE]                      = -ENOBUFS,
790 	[-VERR_NET_ALREADY_CONNECTED]                    = -EISCONN,
791 	[-VERR_NET_NOT_CONNECTED]                        = -ENOTCONN,
792 	[-VERR_NET_SHUTDOWN]                             = -ESHUTDOWN,
793 	[-VERR_NET_TOO_MANY_REFERENCES]                  = -ETOOMANYREFS,
794 	[-VERR_TIMEOUT]                                  = -ETIMEDOUT,
795 	[-VERR_NET_CONNECTION_REFUSED]                   = -ECONNREFUSED,
796 	[-VERR_NET_HOST_DOWN]                            = -EHOSTDOWN,
797 	[-VERR_NET_HOST_UNREACHABLE]                     = -EHOSTUNREACH,
798 	[-VERR_NET_ALREADY_IN_PROGRESS]                  = -EALREADY,
799 	[-VERR_NET_IN_PROGRESS]                          = -EINPROGRESS,
800 	[-VERR_MEDIA_NOT_PRESENT]                        = -ENOMEDIUM,
801 	[-VERR_MEDIA_NOT_RECOGNIZED]                     = -EMEDIUMTYPE,
802 };
803 
804 int vbg_status_code_to_errno(int rc)
805 {
806 	if (rc >= 0)
807 		return 0;
808 
809 	rc = -rc;
810 	if (rc >= ARRAY_SIZE(vbg_status_code_to_errno_table) ||
811 	    vbg_status_code_to_errno_table[rc] == 0) {
812 		vbg_warn("%s: Unhandled err %d\n", __func__, -rc);
813 		return -EPROTO;
814 	}
815 
816 	return vbg_status_code_to_errno_table[rc];
817 }
818 EXPORT_SYMBOL(vbg_status_code_to_errno);
819