1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (c) 2023, Microsoft Corporation. 4 * 5 * Hypercall helper functions used by the mshv_root module. 6 * 7 * Authors: Microsoft Linux virtualization team 8 */ 9 10 #include <linux/kernel.h> 11 #include <linux/mm.h> 12 #include <linux/export.h> 13 #include <asm/mshyperv.h> 14 15 #include "mshv_root.h" 16 17 /* Determined empirically */ 18 #define HV_UMAP_GPA_PAGES 512 19 20 #define HV_PAGE_COUNT_2M_ALIGNED(pg_count) (!((pg_count) & (0x200 - 1))) 21 22 #define HV_WITHDRAW_BATCH_SIZE (HV_HYP_PAGE_SIZE / sizeof(u64)) 23 #define HV_MAP_GPA_BATCH_SIZE \ 24 ((HV_HYP_PAGE_SIZE - sizeof(struct hv_input_map_gpa_pages)) \ 25 / sizeof(u64)) 26 #define HV_GET_VP_STATE_BATCH_SIZE \ 27 ((HV_HYP_PAGE_SIZE - sizeof(struct hv_input_get_vp_state)) \ 28 / sizeof(u64)) 29 #define HV_SET_VP_STATE_BATCH_SIZE \ 30 ((HV_HYP_PAGE_SIZE - sizeof(struct hv_input_set_vp_state)) \ 31 / sizeof(u64)) 32 #define HV_GET_GPA_ACCESS_STATES_BATCH_SIZE \ 33 ((HV_HYP_PAGE_SIZE - sizeof(union hv_gpa_page_access_state)) \ 34 / sizeof(union hv_gpa_page_access_state)) 35 #define HV_MODIFY_SPARSE_SPA_PAGE_HOST_ACCESS_MAX_PAGE_COUNT \ 36 ((HV_HYP_PAGE_SIZE - \ 37 sizeof(struct hv_input_modify_sparse_spa_page_host_access)) / \ 38 sizeof(u64)) 39 40 int hv_call_withdraw_memory(u64 count, int node, u64 partition_id) 41 { 42 struct hv_input_withdraw_memory *input_page; 43 struct hv_output_withdraw_memory *output_page; 44 struct page *page; 45 u16 completed; 46 u64 status, withdrawn = 0; 47 int i; 48 unsigned long flags; 49 50 page = alloc_page(GFP_KERNEL); 51 if (!page) 52 return -ENOMEM; 53 output_page = page_address(page); 54 55 while (withdrawn < count) { 56 local_irq_save(flags); 57 58 input_page = *this_cpu_ptr(hyperv_pcpu_input_arg); 59 60 memset(input_page, 0, sizeof(*input_page)); 61 input_page->partition_id = partition_id; 62 status = hv_do_rep_hypercall(HVCALL_WITHDRAW_MEMORY, 63 min(count - withdrawn, HV_WITHDRAW_BATCH_SIZE), 64 0, input_page, output_page); 65 66 local_irq_restore(flags); 67 68 completed = hv_repcomp(status); 69 70 for (i = 0; i < completed; i++) 71 __free_page(pfn_to_page(output_page->gpa_page_list[i])); 72 73 if (!hv_result_success(status)) { 74 if (hv_result(status) == HV_STATUS_NO_RESOURCES) 75 status = HV_STATUS_SUCCESS; 76 break; 77 } 78 79 withdrawn += completed; 80 } 81 free_page((unsigned long)output_page); 82 83 trace_mshv_hvcall_withdraw_memory(partition_id, withdrawn, status); 84 85 return hv_result_to_errno(status); 86 } 87 88 int hv_call_create_partition(u64 flags, 89 struct hv_partition_creation_properties creation_properties, 90 union hv_partition_isolation_properties isolation_properties, 91 u64 *partition_id) 92 { 93 struct hv_input_create_partition *input; 94 struct hv_output_create_partition *output; 95 u64 status; 96 int ret; 97 unsigned long irq_flags; 98 99 do { 100 local_irq_save(irq_flags); 101 input = *this_cpu_ptr(hyperv_pcpu_input_arg); 102 output = *this_cpu_ptr(hyperv_pcpu_output_arg); 103 104 memset(input, 0, sizeof(*input)); 105 input->flags = flags; 106 input->compatibility_version = HV_COMPATIBILITY_21_H2; 107 108 memcpy(&input->partition_creation_properties, &creation_properties, 109 sizeof(creation_properties)); 110 111 memcpy(&input->isolation_properties, &isolation_properties, 112 sizeof(isolation_properties)); 113 114 status = hv_do_hypercall(HVCALL_CREATE_PARTITION, 115 input, output); 116 117 if (!hv_result_needs_memory(status)) { 118 if (hv_result_success(status)) 119 *partition_id = output->partition_id; 120 local_irq_restore(irq_flags); 121 ret = hv_result_to_errno(status); 122 break; 123 } 124 local_irq_restore(irq_flags); 125 ret = hv_deposit_memory(hv_current_partition_id, status); 126 } while (!ret); 127 128 trace_mshv_hvcall_create_partition(flags, ret ? ret : *partition_id); 129 130 return ret; 131 } 132 133 int hv_call_initialize_partition(u64 partition_id) 134 { 135 struct hv_input_initialize_partition input; 136 u64 status; 137 int ret; 138 139 input.partition_id = partition_id; 140 141 do { 142 status = hv_do_fast_hypercall8(HVCALL_INITIALIZE_PARTITION, 143 *(u64 *)&input); 144 145 if (!hv_result_needs_memory(status)) { 146 ret = hv_result_to_errno(status); 147 break; 148 } 149 ret = hv_deposit_memory(partition_id, status); 150 } while (!ret); 151 152 trace_mshv_hvcall_initialize_partition(partition_id, status); 153 154 return ret; 155 } 156 157 int hv_call_finalize_partition(u64 partition_id) 158 { 159 struct hv_input_finalize_partition input; 160 u64 status; 161 162 input.partition_id = partition_id; 163 status = hv_do_fast_hypercall8(HVCALL_FINALIZE_PARTITION, 164 *(u64 *)&input); 165 166 trace_mshv_hvcall_finalize_partition(partition_id, status); 167 168 return hv_result_to_errno(status); 169 } 170 171 int hv_call_delete_partition(u64 partition_id) 172 { 173 struct hv_input_delete_partition input; 174 u64 status; 175 176 input.partition_id = partition_id; 177 status = hv_do_fast_hypercall8(HVCALL_DELETE_PARTITION, *(u64 *)&input); 178 179 trace_mshv_hvcall_delete_partition(partition_id, status); 180 181 return hv_result_to_errno(status); 182 } 183 184 /* Ask the hypervisor to map guest ram pages or the guest mmio space */ 185 static int hv_do_map_gpa_hcall(u64 partition_id, u64 gfn, u64 page_struct_count, 186 u32 flags, struct page **pages, u64 mmio_spa) 187 { 188 struct hv_input_map_gpa_pages *input_page; 189 u64 status, *pfnlist; 190 unsigned long irq_flags, large_shift = 0; 191 int ret = 0, done = 0; 192 u64 page_count = page_struct_count; 193 194 if (page_count == 0 || (pages && mmio_spa)) 195 return -EINVAL; 196 197 if (flags & HV_MAP_GPA_LARGE_PAGE) { 198 if (mmio_spa) 199 return -EINVAL; 200 201 if (!HV_PAGE_COUNT_2M_ALIGNED(page_count)) 202 return -EINVAL; 203 204 large_shift = HV_HYP_LARGE_PAGE_SHIFT - HV_HYP_PAGE_SHIFT; 205 page_count >>= large_shift; 206 } 207 208 while (done < page_count) { 209 ulong i, completed, remain = page_count - done; 210 int rep_count = min(remain, HV_MAP_GPA_BATCH_SIZE); 211 212 local_irq_save(irq_flags); 213 input_page = *this_cpu_ptr(hyperv_pcpu_input_arg); 214 215 input_page->target_partition_id = partition_id; 216 input_page->target_gpa_base = gfn + (done << large_shift); 217 input_page->map_flags = flags; 218 pfnlist = input_page->source_gpa_page_list; 219 220 for (i = 0; i < rep_count; i++) 221 if (flags & HV_MAP_GPA_NO_ACCESS) { 222 pfnlist[i] = 0; 223 } else if (pages) { 224 u64 index = (done + i) << large_shift; 225 226 if (index >= page_struct_count) { 227 ret = -EINVAL; 228 break; 229 } 230 pfnlist[i] = page_to_pfn(pages[index]); 231 } else { 232 pfnlist[i] = mmio_spa + done + i; 233 } 234 if (ret) 235 break; 236 237 status = hv_do_rep_hypercall(HVCALL_MAP_GPA_PAGES, rep_count, 0, 238 input_page, NULL); 239 local_irq_restore(irq_flags); 240 241 completed = hv_repcomp(status); 242 243 if (hv_result_needs_memory(status)) { 244 ret = hv_deposit_memory(partition_id, status); 245 if (ret) 246 break; 247 248 } else if (!hv_result_success(status)) { 249 ret = hv_result_to_errno(status); 250 break; 251 } 252 253 done += completed; 254 } 255 256 if (ret && done) { 257 u32 unmap_flags = 0; 258 259 if (flags & HV_MAP_GPA_LARGE_PAGE) 260 unmap_flags |= HV_UNMAP_GPA_LARGE_PAGE; 261 hv_call_unmap_gpa_pages(partition_id, gfn, done, unmap_flags); 262 } 263 264 return ret; 265 } 266 267 /* Ask the hypervisor to map guest ram pages */ 268 int hv_call_map_gpa_pages(u64 partition_id, u64 gpa_target, u64 page_count, 269 u32 flags, struct page **pages) 270 { 271 return hv_do_map_gpa_hcall(partition_id, gpa_target, page_count, 272 flags, pages, 0); 273 } 274 275 /* Ask the hypervisor to map guest mmio space */ 276 int hv_call_map_mmio_pages(u64 partition_id, u64 gfn, u64 mmio_spa, u64 numpgs) 277 { 278 int i; 279 u32 flags = HV_MAP_GPA_READABLE | HV_MAP_GPA_WRITABLE | 280 HV_MAP_GPA_NOT_CACHED; 281 282 for (i = 0; i < numpgs; i++) 283 if (page_is_ram(mmio_spa + i)) 284 return -EINVAL; 285 286 return hv_do_map_gpa_hcall(partition_id, gfn, numpgs, flags, NULL, 287 mmio_spa); 288 } 289 290 int hv_call_unmap_gpa_pages(u64 partition_id, u64 gfn, u64 page_count_4k, 291 u32 flags) 292 { 293 struct hv_input_unmap_gpa_pages *input_page; 294 u64 status, page_count = page_count_4k; 295 unsigned long irq_flags, large_shift = 0; 296 int ret = 0, done = 0; 297 298 if (page_count == 0) 299 return -EINVAL; 300 301 if (flags & HV_UNMAP_GPA_LARGE_PAGE) { 302 if (!HV_PAGE_COUNT_2M_ALIGNED(page_count)) 303 return -EINVAL; 304 305 large_shift = HV_HYP_LARGE_PAGE_SHIFT - HV_HYP_PAGE_SHIFT; 306 page_count >>= large_shift; 307 } 308 309 while (done < page_count) { 310 ulong completed, remain = page_count - done; 311 int rep_count = min(remain, HV_UMAP_GPA_PAGES); 312 313 local_irq_save(irq_flags); 314 input_page = *this_cpu_ptr(hyperv_pcpu_input_arg); 315 316 input_page->target_partition_id = partition_id; 317 input_page->target_gpa_base = gfn + (done << large_shift); 318 input_page->unmap_flags = flags; 319 status = hv_do_rep_hypercall(HVCALL_UNMAP_GPA_PAGES, rep_count, 320 0, input_page, NULL); 321 local_irq_restore(irq_flags); 322 323 completed = hv_repcomp(status); 324 if (!hv_result_success(status)) { 325 ret = hv_result_to_errno(status); 326 break; 327 } 328 329 done += completed; 330 } 331 332 return ret; 333 } 334 335 int hv_call_get_gpa_access_states(u64 partition_id, u32 count, u64 gpa_base_pfn, 336 union hv_gpa_page_access_state_flags state_flags, 337 int *written_total, 338 union hv_gpa_page_access_state *states) 339 { 340 struct hv_input_get_gpa_pages_access_state *input_page; 341 union hv_gpa_page_access_state *output_page; 342 int completed = 0; 343 unsigned long remaining = count; 344 int rep_count, i; 345 u64 status = 0; 346 unsigned long flags; 347 348 *written_total = 0; 349 while (remaining) { 350 local_irq_save(flags); 351 input_page = *this_cpu_ptr(hyperv_pcpu_input_arg); 352 output_page = *this_cpu_ptr(hyperv_pcpu_output_arg); 353 354 input_page->partition_id = partition_id; 355 input_page->hv_gpa_page_number = gpa_base_pfn + *written_total; 356 input_page->flags = state_flags; 357 rep_count = min(remaining, HV_GET_GPA_ACCESS_STATES_BATCH_SIZE); 358 359 status = hv_do_rep_hypercall(HVCALL_GET_GPA_PAGES_ACCESS_STATES, rep_count, 360 0, input_page, output_page); 361 if (!hv_result_success(status)) { 362 local_irq_restore(flags); 363 break; 364 } 365 completed = hv_repcomp(status); 366 for (i = 0; i < completed; ++i) 367 states[i].as_uint8 = output_page[i].as_uint8; 368 369 local_irq_restore(flags); 370 states += completed; 371 *written_total += completed; 372 remaining -= completed; 373 } 374 375 return hv_result_to_errno(status); 376 } 377 378 int hv_call_assert_virtual_interrupt(u64 partition_id, u32 vector, 379 u64 dest_addr, 380 union hv_interrupt_control control) 381 { 382 struct hv_input_assert_virtual_interrupt *input; 383 unsigned long flags; 384 u64 status; 385 386 local_irq_save(flags); 387 input = *this_cpu_ptr(hyperv_pcpu_input_arg); 388 memset(input, 0, sizeof(*input)); 389 input->partition_id = partition_id; 390 input->vector = vector; 391 /* 392 * NOTE: dest_addr only needs to be provided while asserting an 393 * interrupt on x86 platform 394 */ 395 #if IS_ENABLED(CONFIG_X86) 396 input->dest_addr = dest_addr; 397 #endif 398 input->control = control; 399 status = hv_do_hypercall(HVCALL_ASSERT_VIRTUAL_INTERRUPT, input, NULL); 400 local_irq_restore(flags); 401 402 return hv_result_to_errno(status); 403 } 404 405 int hv_call_delete_vp(u64 partition_id, u32 vp_index) 406 { 407 union hv_input_delete_vp input = {}; 408 u64 status; 409 410 input.partition_id = partition_id; 411 input.vp_index = vp_index; 412 413 status = hv_do_fast_hypercall16(HVCALL_DELETE_VP, 414 input.as_uint64[0], input.as_uint64[1]); 415 416 return hv_result_to_errno(status); 417 } 418 EXPORT_SYMBOL_GPL(hv_call_delete_vp); 419 420 int hv_call_get_vp_state(u32 vp_index, u64 partition_id, 421 struct hv_vp_state_data state_data, 422 /* Choose between pages and ret_output */ 423 u64 page_count, struct page **pages, 424 union hv_output_get_vp_state *ret_output) 425 { 426 struct hv_input_get_vp_state *input; 427 union hv_output_get_vp_state *output; 428 u64 status; 429 int i; 430 u64 control; 431 unsigned long flags; 432 int ret = 0; 433 434 if (page_count > HV_GET_VP_STATE_BATCH_SIZE) 435 return -EINVAL; 436 437 if (!page_count && !ret_output) 438 return -EINVAL; 439 440 do { 441 local_irq_save(flags); 442 input = *this_cpu_ptr(hyperv_pcpu_input_arg); 443 output = *this_cpu_ptr(hyperv_pcpu_output_arg); 444 memset(input, 0, sizeof(*input)); 445 memset(output, 0, sizeof(*output)); 446 447 input->partition_id = partition_id; 448 input->vp_index = vp_index; 449 input->state_data = state_data; 450 for (i = 0; i < page_count; i++) 451 input->output_data_pfns[i] = page_to_pfn(pages[i]); 452 453 control = (HVCALL_GET_VP_STATE) | 454 (page_count << HV_HYPERCALL_VARHEAD_OFFSET); 455 456 status = hv_do_hypercall(control, input, output); 457 458 if (!hv_result_needs_memory(status)) { 459 if (hv_result_success(status) && ret_output) 460 memcpy(ret_output, output, sizeof(*output)); 461 462 local_irq_restore(flags); 463 ret = hv_result_to_errno(status); 464 break; 465 } 466 local_irq_restore(flags); 467 468 ret = hv_deposit_memory(partition_id, status); 469 } while (!ret); 470 471 return ret; 472 } 473 474 int hv_call_set_vp_state(u32 vp_index, u64 partition_id, 475 /* Choose between pages and bytes */ 476 struct hv_vp_state_data state_data, u64 page_count, 477 struct page **pages, u32 num_bytes, u8 *bytes) 478 { 479 struct hv_input_set_vp_state *input; 480 u64 status; 481 int i; 482 u64 control; 483 unsigned long flags; 484 int ret = 0; 485 u16 varhead_sz; 486 487 if (page_count > HV_SET_VP_STATE_BATCH_SIZE) 488 return -EINVAL; 489 if (sizeof(*input) + num_bytes > HV_HYP_PAGE_SIZE) 490 return -EINVAL; 491 492 if (num_bytes) 493 /* round up to 8 and divide by 8 */ 494 varhead_sz = (num_bytes + 7) >> 3; 495 else if (page_count) 496 varhead_sz = page_count; 497 else 498 return -EINVAL; 499 500 do { 501 local_irq_save(flags); 502 input = *this_cpu_ptr(hyperv_pcpu_input_arg); 503 memset(input, 0, sizeof(*input)); 504 505 input->partition_id = partition_id; 506 input->vp_index = vp_index; 507 input->state_data = state_data; 508 if (num_bytes) { 509 memcpy((u8 *)input->data, bytes, num_bytes); 510 } else { 511 for (i = 0; i < page_count; i++) 512 input->data[i].pfns = page_to_pfn(pages[i]); 513 } 514 515 control = (HVCALL_SET_VP_STATE) | 516 (varhead_sz << HV_HYPERCALL_VARHEAD_OFFSET); 517 518 status = hv_do_hypercall(control, input, NULL); 519 520 if (!hv_result_needs_memory(status)) { 521 local_irq_restore(flags); 522 ret = hv_result_to_errno(status); 523 break; 524 } 525 local_irq_restore(flags); 526 527 ret = hv_deposit_memory(partition_id, status); 528 } while (!ret); 529 530 return ret; 531 } 532 533 static int hv_call_map_vp_state_page(u64 partition_id, u32 vp_index, u32 type, 534 union hv_input_vtl input_vtl, 535 struct page **state_page) 536 { 537 struct hv_input_map_vp_state_page *input; 538 struct hv_output_map_vp_state_page *output; 539 u64 status; 540 int ret; 541 unsigned long flags; 542 543 do { 544 local_irq_save(flags); 545 546 input = *this_cpu_ptr(hyperv_pcpu_input_arg); 547 output = *this_cpu_ptr(hyperv_pcpu_output_arg); 548 549 memset(input, 0, sizeof(*input)); 550 input->partition_id = partition_id; 551 input->vp_index = vp_index; 552 input->type = type; 553 input->input_vtl = input_vtl; 554 555 if (*state_page) { 556 input->flags.map_location_provided = 1; 557 input->requested_map_location = 558 page_to_pfn(*state_page); 559 } 560 561 status = hv_do_hypercall(HVCALL_MAP_VP_STATE_PAGE, input, 562 output); 563 564 if (!hv_result_needs_memory(status)) { 565 if (hv_result_success(status)) 566 *state_page = pfn_to_page(output->map_location); 567 local_irq_restore(flags); 568 ret = hv_result_to_errno(status); 569 break; 570 } 571 572 local_irq_restore(flags); 573 574 ret = hv_deposit_memory(partition_id, status); 575 } while (!ret); 576 577 trace_mshv_hvcall_map_vp_state_page(partition_id, vp_index, 578 type, status); 579 580 return ret; 581 } 582 583 static bool mshv_use_overlay_gpfn(void) 584 { 585 return hv_l1vh_partition() && 586 mshv_root.vmm_caps.vmm_can_provide_overlay_gpfn; 587 } 588 589 int hv_map_vp_state_page(u64 partition_id, u32 vp_index, u32 type, 590 union hv_input_vtl input_vtl, 591 struct page **state_page) 592 { 593 int ret = 0; 594 struct page *allocated_page = NULL; 595 596 if (mshv_use_overlay_gpfn()) { 597 allocated_page = alloc_page(GFP_KERNEL); 598 if (!allocated_page) 599 return -ENOMEM; 600 *state_page = allocated_page; 601 } else { 602 *state_page = NULL; 603 } 604 605 ret = hv_call_map_vp_state_page(partition_id, vp_index, type, input_vtl, 606 state_page); 607 608 if (ret && allocated_page) { 609 __free_page(allocated_page); 610 *state_page = NULL; 611 } 612 613 return ret; 614 } 615 616 static int hv_call_unmap_vp_state_page(u64 partition_id, u32 vp_index, u32 type, 617 union hv_input_vtl input_vtl) 618 { 619 unsigned long flags; 620 u64 status; 621 struct hv_input_unmap_vp_state_page *input; 622 623 local_irq_save(flags); 624 625 input = *this_cpu_ptr(hyperv_pcpu_input_arg); 626 627 memset(input, 0, sizeof(*input)); 628 629 input->partition_id = partition_id; 630 input->vp_index = vp_index; 631 input->type = type; 632 input->input_vtl = input_vtl; 633 634 status = hv_do_hypercall(HVCALL_UNMAP_VP_STATE_PAGE, input, NULL); 635 636 local_irq_restore(flags); 637 638 return hv_result_to_errno(status); 639 } 640 641 int hv_unmap_vp_state_page(u64 partition_id, u32 vp_index, u32 type, 642 struct page *state_page, union hv_input_vtl input_vtl) 643 { 644 int ret = hv_call_unmap_vp_state_page(partition_id, vp_index, type, input_vtl); 645 646 if (mshv_use_overlay_gpfn() && state_page) 647 __free_page(state_page); 648 649 return ret; 650 } 651 652 int hv_call_get_partition_property_ex(u64 partition_id, u64 property_code, 653 u64 arg, void *property_value, 654 size_t property_value_sz) 655 { 656 u64 status; 657 unsigned long flags; 658 struct hv_input_get_partition_property_ex *input; 659 struct hv_output_get_partition_property_ex *output; 660 661 local_irq_save(flags); 662 input = *this_cpu_ptr(hyperv_pcpu_input_arg); 663 output = *this_cpu_ptr(hyperv_pcpu_output_arg); 664 665 memset(input, 0, sizeof(*input)); 666 input->partition_id = partition_id; 667 input->property_code = property_code; 668 input->arg = arg; 669 status = hv_do_hypercall(HVCALL_GET_PARTITION_PROPERTY_EX, input, output); 670 671 if (!hv_result_success(status)) { 672 local_irq_restore(flags); 673 hv_status_debug(status, "\n"); 674 return hv_result_to_errno(status); 675 } 676 memcpy(property_value, &output->property_value, property_value_sz); 677 678 local_irq_restore(flags); 679 680 return 0; 681 } 682 683 int 684 hv_call_clear_virtual_interrupt(u64 partition_id) 685 { 686 int status; 687 688 status = hv_do_fast_hypercall8(HVCALL_CLEAR_VIRTUAL_INTERRUPT, 689 partition_id); 690 691 return hv_result_to_errno(status); 692 } 693 694 int 695 hv_call_create_port(u64 port_partition_id, union hv_port_id port_id, 696 u64 connection_partition_id, 697 struct hv_port_info *port_info, 698 u8 port_vtl, u8 min_connection_vtl, int node) 699 { 700 struct hv_input_create_port *input; 701 unsigned long flags; 702 int ret = 0; 703 int status; 704 705 do { 706 local_irq_save(flags); 707 input = *this_cpu_ptr(hyperv_pcpu_input_arg); 708 memset(input, 0, sizeof(*input)); 709 710 input->port_partition_id = port_partition_id; 711 input->port_id = port_id; 712 input->connection_partition_id = connection_partition_id; 713 input->port_info = *port_info; 714 input->port_vtl = port_vtl; 715 input->min_connection_vtl = min_connection_vtl; 716 input->proximity_domain_info = hv_numa_node_to_pxm_info(node); 717 status = hv_do_hypercall(HVCALL_CREATE_PORT, input, NULL); 718 local_irq_restore(flags); 719 if (hv_result_success(status)) 720 break; 721 722 if (!hv_result_needs_memory(status)) { 723 ret = hv_result_to_errno(status); 724 break; 725 } 726 ret = hv_deposit_memory(port_partition_id, status); 727 } while (!ret); 728 729 return ret; 730 } 731 732 int 733 hv_call_delete_port(u64 port_partition_id, union hv_port_id port_id) 734 { 735 union hv_input_delete_port input = { 0 }; 736 int status; 737 738 input.port_partition_id = port_partition_id; 739 input.port_id = port_id; 740 status = hv_do_fast_hypercall16(HVCALL_DELETE_PORT, 741 input.as_uint64[0], 742 input.as_uint64[1]); 743 744 return hv_result_to_errno(status); 745 } 746 747 int 748 hv_call_connect_port(u64 port_partition_id, union hv_port_id port_id, 749 u64 connection_partition_id, 750 union hv_connection_id connection_id, 751 struct hv_connection_info *connection_info, 752 u8 connection_vtl, int node) 753 { 754 struct hv_input_connect_port *input; 755 unsigned long flags; 756 int ret = 0, status; 757 758 do { 759 local_irq_save(flags); 760 input = *this_cpu_ptr(hyperv_pcpu_input_arg); 761 memset(input, 0, sizeof(*input)); 762 input->port_partition_id = port_partition_id; 763 input->port_id = port_id; 764 input->connection_partition_id = connection_partition_id; 765 input->connection_id = connection_id; 766 input->connection_info = *connection_info; 767 input->connection_vtl = connection_vtl; 768 input->proximity_domain_info = hv_numa_node_to_pxm_info(node); 769 status = hv_do_hypercall(HVCALL_CONNECT_PORT, input, NULL); 770 771 local_irq_restore(flags); 772 if (hv_result_success(status)) 773 break; 774 775 if (!hv_result_needs_memory(status)) { 776 ret = hv_result_to_errno(status); 777 break; 778 } 779 ret = hv_deposit_memory(connection_partition_id, status); 780 } while (!ret); 781 782 return ret; 783 } 784 785 int 786 hv_call_disconnect_port(u64 connection_partition_id, 787 union hv_connection_id connection_id) 788 { 789 union hv_input_disconnect_port input = { 0 }; 790 int status; 791 792 input.connection_partition_id = connection_partition_id; 793 input.connection_id = connection_id; 794 input.is_doorbell = 1; 795 status = hv_do_fast_hypercall16(HVCALL_DISCONNECT_PORT, 796 input.as_uint64[0], 797 input.as_uint64[1]); 798 799 return hv_result_to_errno(status); 800 } 801 802 int 803 hv_call_notify_port_ring_empty(u32 sint_index) 804 { 805 union hv_input_notify_port_ring_empty input = { 0 }; 806 int status; 807 808 input.sint_index = sint_index; 809 status = hv_do_fast_hypercall8(HVCALL_NOTIFY_PORT_RING_EMPTY, 810 input.as_uint64); 811 812 return hv_result_to_errno(status); 813 } 814 815 /* 816 * Equivalent of hv_call_map_stats_page() for cases when the caller provides 817 * the map location. 818 * 819 * NOTE: This is a newer hypercall that always supports SELF and PARENT stats 820 * areas, unlike hv_call_map_stats_page(). 821 */ 822 static int hv_call_map_stats_page2(enum hv_stats_object_type type, 823 const union hv_stats_object_identity *identity, 824 u64 map_location) 825 { 826 unsigned long flags; 827 struct hv_input_map_stats_page2 *input; 828 u64 status; 829 int ret; 830 831 if (!map_location || !mshv_use_overlay_gpfn()) 832 return -EINVAL; 833 834 do { 835 local_irq_save(flags); 836 input = *this_cpu_ptr(hyperv_pcpu_input_arg); 837 838 memset(input, 0, sizeof(*input)); 839 input->type = type; 840 input->identity = *identity; 841 input->map_location = map_location; 842 843 status = hv_do_hypercall(HVCALL_MAP_STATS_PAGE2, input, NULL); 844 845 local_irq_restore(flags); 846 847 ret = hv_result_to_errno(status); 848 849 if (!ret) 850 break; 851 852 if (!hv_result_needs_memory(status)) { 853 hv_status_debug(status, "\n"); 854 break; 855 } 856 857 ret = hv_deposit_memory(hv_current_partition_id, status); 858 } while (!ret); 859 860 return ret; 861 } 862 863 static int 864 hv_stats_get_area_type(enum hv_stats_object_type type, 865 const union hv_stats_object_identity *identity) 866 { 867 switch (type) { 868 case HV_STATS_OBJECT_HYPERVISOR: 869 return identity->hv.stats_area_type; 870 case HV_STATS_OBJECT_LOGICAL_PROCESSOR: 871 return identity->lp.stats_area_type; 872 case HV_STATS_OBJECT_PARTITION: 873 return identity->partition.stats_area_type; 874 case HV_STATS_OBJECT_VP: 875 return identity->vp.stats_area_type; 876 } 877 878 return -EINVAL; 879 } 880 881 /* 882 * Map a stats page, where the page location is provided by the hypervisor. 883 * 884 * NOTE: The concept of separate SELF and PARENT stats areas does not exist on 885 * older hypervisor versions. All the available stats information can be found 886 * on the SELF page. When attempting to map the PARENT area on a hypervisor 887 * that doesn't support it, return "success" but with a NULL address. The 888 * caller should check for this case and instead fallback to the SELF area 889 * alone. 890 */ 891 static int 892 hv_call_map_stats_page(enum hv_stats_object_type type, 893 const union hv_stats_object_identity *identity, 894 struct hv_stats_page **addr) 895 { 896 unsigned long flags; 897 struct hv_input_map_stats_page *input; 898 struct hv_output_map_stats_page *output; 899 u64 status, pfn; 900 int ret = 0; 901 902 do { 903 local_irq_save(flags); 904 input = *this_cpu_ptr(hyperv_pcpu_input_arg); 905 output = *this_cpu_ptr(hyperv_pcpu_output_arg); 906 907 memset(input, 0, sizeof(*input)); 908 input->type = type; 909 input->identity = *identity; 910 911 status = hv_do_hypercall(HVCALL_MAP_STATS_PAGE, input, output); 912 pfn = output->map_location; 913 914 local_irq_restore(flags); 915 916 if (!hv_result_needs_memory(status)) { 917 if (hv_result_success(status)) 918 break; 919 920 if (hv_stats_get_area_type(type, identity) == HV_STATS_AREA_PARENT && 921 hv_result(status) == HV_STATUS_INVALID_PARAMETER) { 922 *addr = NULL; 923 return 0; 924 } 925 926 hv_status_debug(status, "\n"); 927 return hv_result_to_errno(status); 928 } 929 930 ret = hv_deposit_memory(hv_current_partition_id, status); 931 if (ret) 932 return ret; 933 } while (!ret); 934 935 *addr = page_address(pfn_to_page(pfn)); 936 937 return ret; 938 } 939 940 int hv_map_stats_page(enum hv_stats_object_type type, 941 const union hv_stats_object_identity *identity, 942 struct hv_stats_page **addr) 943 { 944 int ret; 945 struct page *allocated_page = NULL; 946 947 if (!addr) 948 return -EINVAL; 949 950 if (mshv_use_overlay_gpfn()) { 951 allocated_page = alloc_page(GFP_KERNEL); 952 if (!allocated_page) 953 return -ENOMEM; 954 955 ret = hv_call_map_stats_page2(type, identity, 956 page_to_pfn(allocated_page)); 957 *addr = page_address(allocated_page); 958 } else { 959 ret = hv_call_map_stats_page(type, identity, addr); 960 } 961 962 if (ret && allocated_page) { 963 __free_page(allocated_page); 964 *addr = NULL; 965 } 966 967 return ret; 968 } 969 970 static int hv_call_unmap_stats_page(enum hv_stats_object_type type, 971 const union hv_stats_object_identity *identity) 972 { 973 unsigned long flags; 974 struct hv_input_unmap_stats_page *input; 975 u64 status; 976 977 local_irq_save(flags); 978 input = *this_cpu_ptr(hyperv_pcpu_input_arg); 979 980 memset(input, 0, sizeof(*input)); 981 input->type = type; 982 input->identity = *identity; 983 984 status = hv_do_hypercall(HVCALL_UNMAP_STATS_PAGE, input, NULL); 985 local_irq_restore(flags); 986 987 return hv_result_to_errno(status); 988 } 989 990 int hv_unmap_stats_page(enum hv_stats_object_type type, 991 struct hv_stats_page *page_addr, 992 const union hv_stats_object_identity *identity) 993 { 994 int ret; 995 996 ret = hv_call_unmap_stats_page(type, identity); 997 998 if (mshv_use_overlay_gpfn() && page_addr) 999 __free_page(virt_to_page(page_addr)); 1000 1001 return ret; 1002 } 1003 1004 int hv_call_modify_spa_host_access(u64 partition_id, struct page **pages, 1005 u64 page_struct_count, u32 host_access, 1006 u32 flags, u8 acquire) 1007 { 1008 struct hv_input_modify_sparse_spa_page_host_access *input_page; 1009 u64 status; 1010 int done = 0; 1011 unsigned long irq_flags, large_shift = 0; 1012 u64 page_count = page_struct_count; 1013 u16 code = acquire ? HVCALL_ACQUIRE_SPARSE_SPA_PAGE_HOST_ACCESS : 1014 HVCALL_RELEASE_SPARSE_SPA_PAGE_HOST_ACCESS; 1015 1016 if (page_count == 0) 1017 return -EINVAL; 1018 1019 if (flags & HV_MODIFY_SPA_PAGE_HOST_ACCESS_LARGE_PAGE) { 1020 if (!HV_PAGE_COUNT_2M_ALIGNED(page_count)) 1021 return -EINVAL; 1022 large_shift = HV_HYP_LARGE_PAGE_SHIFT - HV_HYP_PAGE_SHIFT; 1023 page_count >>= large_shift; 1024 } 1025 1026 while (done < page_count) { 1027 ulong i, completed, remain = page_count - done; 1028 int rep_count = min(remain, 1029 HV_MODIFY_SPARSE_SPA_PAGE_HOST_ACCESS_MAX_PAGE_COUNT); 1030 1031 local_irq_save(irq_flags); 1032 input_page = *this_cpu_ptr(hyperv_pcpu_input_arg); 1033 1034 memset(input_page, 0, sizeof(*input_page)); 1035 /* Only set the partition id if you are making the pages 1036 * exclusive 1037 */ 1038 if (flags & HV_MODIFY_SPA_PAGE_HOST_ACCESS_MAKE_EXCLUSIVE) 1039 input_page->partition_id = partition_id; 1040 input_page->flags = flags; 1041 input_page->host_access = host_access; 1042 1043 for (i = 0; i < rep_count; i++) { 1044 u64 index = (done + i) << large_shift; 1045 1046 if (index >= page_struct_count) 1047 return -EINVAL; 1048 1049 input_page->spa_page_list[i] = 1050 page_to_pfn(pages[index]); 1051 } 1052 1053 status = hv_do_rep_hypercall(code, rep_count, 0, input_page, 1054 NULL); 1055 local_irq_restore(irq_flags); 1056 1057 completed = hv_repcomp(status); 1058 1059 if (!hv_result_success(status)) 1060 return hv_result_to_errno(status); 1061 1062 done += completed; 1063 } 1064 1065 return 0; 1066 } 1067