mshv_root_hv_call.c 26 KB

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