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