hv_vtl.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2023, Microsoft Corporation.
  4. *
  5. * Author:
  6. * Saurabh Sengar <ssengar@microsoft.com>
  7. */
  8. #include <asm/apic.h>
  9. #include <asm/boot.h>
  10. #include <asm/desc.h>
  11. #include <asm/i8259.h>
  12. #include <asm/mshyperv.h>
  13. #include <asm/realmode.h>
  14. #include <../kernel/smpboot.h>
  15. extern struct boot_params boot_params;
  16. static struct real_mode_header hv_vtl_real_mode_header;
  17. static bool __init hv_vtl_msi_ext_dest_id(void)
  18. {
  19. return true;
  20. }
  21. void __init hv_vtl_init_platform(void)
  22. {
  23. pr_info("Linux runs in Hyper-V Virtual Trust Level\n");
  24. x86_platform.realmode_reserve = x86_init_noop;
  25. x86_platform.realmode_init = x86_init_noop;
  26. x86_init.irqs.pre_vector_init = x86_init_noop;
  27. x86_init.timers.timer_init = x86_init_noop;
  28. x86_init.resources.probe_roms = x86_init_noop;
  29. /* Avoid searching for BIOS MP tables */
  30. x86_init.mpparse.find_mptable = x86_init_noop;
  31. x86_init.mpparse.early_parse_smp_cfg = x86_init_noop;
  32. x86_platform.get_wallclock = get_rtc_noop;
  33. x86_platform.set_wallclock = set_rtc_noop;
  34. x86_platform.get_nmi_reason = hv_get_nmi_reason;
  35. x86_platform.legacy.i8042 = X86_LEGACY_I8042_PLATFORM_ABSENT;
  36. x86_platform.legacy.rtc = 0;
  37. x86_platform.legacy.warm_reset = 0;
  38. x86_platform.legacy.reserve_bios_regions = 0;
  39. x86_platform.legacy.devices.pnpbios = 0;
  40. x86_init.hyper.msi_ext_dest_id = hv_vtl_msi_ext_dest_id;
  41. }
  42. static inline u64 hv_vtl_system_desc_base(struct ldttss_desc *desc)
  43. {
  44. return ((u64)desc->base3 << 32) | ((u64)desc->base2 << 24) |
  45. (desc->base1 << 16) | desc->base0;
  46. }
  47. static inline u32 hv_vtl_system_desc_limit(struct ldttss_desc *desc)
  48. {
  49. return ((u32)desc->limit1 << 16) | (u32)desc->limit0;
  50. }
  51. typedef void (*secondary_startup_64_fn)(void*, void*);
  52. static void hv_vtl_ap_entry(void)
  53. {
  54. ((secondary_startup_64_fn)secondary_startup_64)(&boot_params, &boot_params);
  55. }
  56. static int hv_vtl_bringup_vcpu(u32 target_vp_index, int cpu, u64 eip_ignored)
  57. {
  58. u64 status;
  59. int ret = 0;
  60. struct hv_enable_vp_vtl *input;
  61. unsigned long irq_flags;
  62. struct desc_ptr gdt_ptr;
  63. struct desc_ptr idt_ptr;
  64. struct ldttss_desc *tss;
  65. struct ldttss_desc *ldt;
  66. struct desc_struct *gdt;
  67. struct task_struct *idle = idle_thread_get(cpu);
  68. u64 rsp = (unsigned long)idle->thread.sp;
  69. u64 rip = (u64)&hv_vtl_ap_entry;
  70. native_store_gdt(&gdt_ptr);
  71. store_idt(&idt_ptr);
  72. gdt = (struct desc_struct *)((void *)(gdt_ptr.address));
  73. tss = (struct ldttss_desc *)(gdt + GDT_ENTRY_TSS);
  74. ldt = (struct ldttss_desc *)(gdt + GDT_ENTRY_LDT);
  75. local_irq_save(irq_flags);
  76. input = *this_cpu_ptr(hyperv_pcpu_input_arg);
  77. memset(input, 0, sizeof(*input));
  78. input->partition_id = HV_PARTITION_ID_SELF;
  79. input->vp_index = target_vp_index;
  80. input->target_vtl.target_vtl = HV_VTL_MGMT;
  81. /*
  82. * The x86_64 Linux kernel follows the 16-bit -> 32-bit -> 64-bit
  83. * mode transition sequence after waking up an AP with SIPI whose
  84. * vector points to the 16-bit AP startup trampoline code. Here in
  85. * VTL2, we can't perform that sequence as the AP has to start in
  86. * the 64-bit mode.
  87. *
  88. * To make this happen, we tell the hypervisor to load a valid 64-bit
  89. * context (most of which is just magic numbers from the CPU manual)
  90. * so that AP jumps right to the 64-bit entry of the kernel, and the
  91. * control registers are loaded with values that let the AP fetch the
  92. * code and data and carry on with work it gets assigned.
  93. */
  94. input->vp_context.rip = rip;
  95. input->vp_context.rsp = rsp;
  96. input->vp_context.rflags = 0x0000000000000002;
  97. input->vp_context.efer = __rdmsr(MSR_EFER);
  98. input->vp_context.cr0 = native_read_cr0();
  99. input->vp_context.cr3 = __native_read_cr3();
  100. input->vp_context.cr4 = native_read_cr4();
  101. input->vp_context.msr_cr_pat = __rdmsr(MSR_IA32_CR_PAT);
  102. input->vp_context.idtr.limit = idt_ptr.size;
  103. input->vp_context.idtr.base = idt_ptr.address;
  104. input->vp_context.gdtr.limit = gdt_ptr.size;
  105. input->vp_context.gdtr.base = gdt_ptr.address;
  106. /* Non-system desc (64bit), long, code, present */
  107. input->vp_context.cs.selector = __KERNEL_CS;
  108. input->vp_context.cs.base = 0;
  109. input->vp_context.cs.limit = 0xffffffff;
  110. input->vp_context.cs.attributes = 0xa09b;
  111. /* Non-system desc (64bit), data, present, granularity, default */
  112. input->vp_context.ss.selector = __KERNEL_DS;
  113. input->vp_context.ss.base = 0;
  114. input->vp_context.ss.limit = 0xffffffff;
  115. input->vp_context.ss.attributes = 0xc093;
  116. /* System desc (128bit), present, LDT */
  117. input->vp_context.ldtr.selector = GDT_ENTRY_LDT * 8;
  118. input->vp_context.ldtr.base = hv_vtl_system_desc_base(ldt);
  119. input->vp_context.ldtr.limit = hv_vtl_system_desc_limit(ldt);
  120. input->vp_context.ldtr.attributes = 0x82;
  121. /* System desc (128bit), present, TSS, 0x8b - busy, 0x89 -- default */
  122. input->vp_context.tr.selector = GDT_ENTRY_TSS * 8;
  123. input->vp_context.tr.base = hv_vtl_system_desc_base(tss);
  124. input->vp_context.tr.limit = hv_vtl_system_desc_limit(tss);
  125. input->vp_context.tr.attributes = 0x8b;
  126. status = hv_do_hypercall(HVCALL_ENABLE_VP_VTL, input, NULL);
  127. if (!hv_result_success(status) &&
  128. hv_result(status) != HV_STATUS_VTL_ALREADY_ENABLED) {
  129. pr_err("HVCALL_ENABLE_VP_VTL failed for VP : %d ! [Err: %#llx\n]",
  130. target_vp_index, status);
  131. ret = -EINVAL;
  132. goto free_lock;
  133. }
  134. status = hv_do_hypercall(HVCALL_START_VP, input, NULL);
  135. if (!hv_result_success(status)) {
  136. pr_err("HVCALL_START_VP failed for VP : %d ! [Err: %#llx]\n",
  137. target_vp_index, status);
  138. ret = -EINVAL;
  139. }
  140. free_lock:
  141. local_irq_restore(irq_flags);
  142. return ret;
  143. }
  144. static int hv_vtl_wakeup_secondary_cpu(u32 apicid, unsigned long start_eip)
  145. {
  146. int vp_index, cpu;
  147. /* Find the logical CPU for the APIC ID */
  148. for_each_present_cpu(cpu) {
  149. if (arch_match_cpu_phys_id(cpu, apicid))
  150. break;
  151. }
  152. if (cpu >= nr_cpu_ids)
  153. return -EINVAL;
  154. pr_debug("Bringing up CPU with APIC ID %d in VTL2...\n", apicid);
  155. vp_index = hv_apicid_to_vp_index(apicid);
  156. if (vp_index < 0) {
  157. pr_err("Couldn't find CPU with APIC ID %d\n", apicid);
  158. return -EINVAL;
  159. }
  160. if (vp_index > ms_hyperv.max_vp_index) {
  161. pr_err("Invalid CPU id %d for APIC ID %d\n", vp_index, apicid);
  162. return -EINVAL;
  163. }
  164. return hv_vtl_bringup_vcpu(vp_index, cpu, start_eip);
  165. }
  166. int __init hv_vtl_early_init(void)
  167. {
  168. /*
  169. * `boot_cpu_has` returns the runtime feature support,
  170. * and here is the earliest it can be used.
  171. */
  172. if (cpu_feature_enabled(X86_FEATURE_XSAVE))
  173. panic("XSAVE has to be disabled as it is not supported by this module.\n"
  174. "Please add 'noxsave' to the kernel command line.\n");
  175. real_mode_header = &hv_vtl_real_mode_header;
  176. apic_update_callback(wakeup_secondary_cpu_64, hv_vtl_wakeup_secondary_cpu);
  177. return 0;
  178. }