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 9 #include <asm/apic.h> 10 #include <asm/boot.h> 11 #include <asm/desc.h> 12 #include <asm/i8259.h> 13 #include <asm/mshyperv.h> 14 #include <asm/realmode.h> 15 #include <asm/reboot.h> 16 #include <../kernel/smpboot.h> 17 18 extern struct boot_params boot_params; 19 static struct real_mode_header hv_vtl_real_mode_header; 20 21 static bool __init hv_vtl_msi_ext_dest_id(void) 22 { 23 return true; 24 } 25 26 /* 27 * The `native_machine_emergency_restart` function from `reboot.c` writes 28 * to the physical address 0x472 to indicate the type of reboot for the 29 * firmware. We cannot have that in VSM as the memory composition might 30 * be more generic, and such write effectively corrupts the memory thus 31 * making diagnostics harder at the very least. 32 */ 33 static void __noreturn hv_vtl_emergency_restart(void) 34 { 35 /* 36 * Cause a triple fault and the immediate reset. Here the code does not run 37 * on the top of any firmware, whereby cannot reach out to its services. 38 * The inifinite loop is for the improbable case that the triple fault does 39 * not work and have to preserve the state intact for debugging. 40 */ 41 for (;;) { 42 idt_invalidate(); 43 __asm__ __volatile__("int3"); 44 } 45 } 46 47 /* 48 * The only way to restart in the VTL mode is to triple fault as the kernel runs 49 * as firmware. 50 */ 51 static void __noreturn hv_vtl_restart(char __maybe_unused *cmd) 52 { 53 hv_vtl_emergency_restart(); 54 } 55 56 void __init hv_vtl_init_platform(void) 57 { 58 pr_info("Linux runs in Hyper-V Virtual Trust Level\n"); 59 60 x86_platform.realmode_reserve = x86_init_noop; 61 x86_platform.realmode_init = x86_init_noop; 62 x86_init.irqs.pre_vector_init = x86_init_noop; 63 x86_init.timers.timer_init = x86_init_noop; 64 x86_init.resources.probe_roms = x86_init_noop; 65 66 /* Avoid searching for BIOS MP tables */ 67 x86_init.mpparse.find_mptable = x86_init_noop; 68 x86_init.mpparse.early_parse_smp_cfg = x86_init_noop; 69 70 x86_platform.get_wallclock = get_rtc_noop; 71 x86_platform.set_wallclock = set_rtc_noop; 72 x86_platform.get_nmi_reason = hv_get_nmi_reason; 73 74 x86_platform.legacy.i8042 = X86_LEGACY_I8042_PLATFORM_ABSENT; 75 x86_platform.legacy.rtc = 0; 76 x86_platform.legacy.warm_reset = 0; 77 x86_platform.legacy.reserve_bios_regions = 0; 78 x86_platform.legacy.devices.pnpbios = 0; 79 80 x86_init.hyper.msi_ext_dest_id = hv_vtl_msi_ext_dest_id; 81 } 82 83 static inline u64 hv_vtl_system_desc_base(struct ldttss_desc *desc) 84 { 85 return ((u64)desc->base3 << 32) | ((u64)desc->base2 << 24) | 86 (desc->base1 << 16) | desc->base0; 87 } 88 89 static inline u32 hv_vtl_system_desc_limit(struct ldttss_desc *desc) 90 { 91 return ((u32)desc->limit1 << 16) | (u32)desc->limit0; 92 } 93 94 typedef void (*secondary_startup_64_fn)(void*, void*); 95 static void hv_vtl_ap_entry(void) 96 { 97 ((secondary_startup_64_fn)secondary_startup_64)(&boot_params, &boot_params); 98 } 99 100 static int hv_vtl_bringup_vcpu(u32 target_vp_index, int cpu, u64 eip_ignored) 101 { 102 u64 status; 103 int ret = 0; 104 struct hv_enable_vp_vtl *input; 105 unsigned long irq_flags; 106 107 struct desc_ptr gdt_ptr; 108 struct desc_ptr idt_ptr; 109 110 struct ldttss_desc *tss; 111 struct ldttss_desc *ldt; 112 struct desc_struct *gdt; 113 114 struct task_struct *idle = idle_thread_get(cpu); 115 u64 rsp = (unsigned long)idle->thread.sp; 116 117 u64 rip = (u64)&hv_vtl_ap_entry; 118 119 native_store_gdt(&gdt_ptr); 120 store_idt(&idt_ptr); 121 122 gdt = (struct desc_struct *)((void *)(gdt_ptr.address)); 123 tss = (struct ldttss_desc *)(gdt + GDT_ENTRY_TSS); 124 ldt = (struct ldttss_desc *)(gdt + GDT_ENTRY_LDT); 125 126 local_irq_save(irq_flags); 127 128 input = *this_cpu_ptr(hyperv_pcpu_input_arg); 129 memset(input, 0, sizeof(*input)); 130 131 input->partition_id = HV_PARTITION_ID_SELF; 132 input->vp_index = target_vp_index; 133 input->target_vtl.target_vtl = HV_VTL_MGMT; 134 135 /* 136 * The x86_64 Linux kernel follows the 16-bit -> 32-bit -> 64-bit 137 * mode transition sequence after waking up an AP with SIPI whose 138 * vector points to the 16-bit AP startup trampoline code. Here in 139 * VTL2, we can't perform that sequence as the AP has to start in 140 * the 64-bit mode. 141 * 142 * To make this happen, we tell the hypervisor to load a valid 64-bit 143 * context (most of which is just magic numbers from the CPU manual) 144 * so that AP jumps right to the 64-bit entry of the kernel, and the 145 * control registers are loaded with values that let the AP fetch the 146 * code and data and carry on with work it gets assigned. 147 */ 148 149 input->vp_context.rip = rip; 150 input->vp_context.rsp = rsp; 151 input->vp_context.rflags = 0x0000000000000002; 152 input->vp_context.efer = __rdmsr(MSR_EFER); 153 input->vp_context.cr0 = native_read_cr0(); 154 input->vp_context.cr3 = __native_read_cr3(); 155 input->vp_context.cr4 = native_read_cr4(); 156 input->vp_context.msr_cr_pat = __rdmsr(MSR_IA32_CR_PAT); 157 input->vp_context.idtr.limit = idt_ptr.size; 158 input->vp_context.idtr.base = idt_ptr.address; 159 input->vp_context.gdtr.limit = gdt_ptr.size; 160 input->vp_context.gdtr.base = gdt_ptr.address; 161 162 /* Non-system desc (64bit), long, code, present */ 163 input->vp_context.cs.selector = __KERNEL_CS; 164 input->vp_context.cs.base = 0; 165 input->vp_context.cs.limit = 0xffffffff; 166 input->vp_context.cs.attributes = 0xa09b; 167 /* Non-system desc (64bit), data, present, granularity, default */ 168 input->vp_context.ss.selector = __KERNEL_DS; 169 input->vp_context.ss.base = 0; 170 input->vp_context.ss.limit = 0xffffffff; 171 input->vp_context.ss.attributes = 0xc093; 172 173 /* System desc (128bit), present, LDT */ 174 input->vp_context.ldtr.selector = GDT_ENTRY_LDT * 8; 175 input->vp_context.ldtr.base = hv_vtl_system_desc_base(ldt); 176 input->vp_context.ldtr.limit = hv_vtl_system_desc_limit(ldt); 177 input->vp_context.ldtr.attributes = 0x82; 178 179 /* System desc (128bit), present, TSS, 0x8b - busy, 0x89 -- default */ 180 input->vp_context.tr.selector = GDT_ENTRY_TSS * 8; 181 input->vp_context.tr.base = hv_vtl_system_desc_base(tss); 182 input->vp_context.tr.limit = hv_vtl_system_desc_limit(tss); 183 input->vp_context.tr.attributes = 0x8b; 184 185 status = hv_do_hypercall(HVCALL_ENABLE_VP_VTL, input, NULL); 186 187 if (!hv_result_success(status) && 188 hv_result(status) != HV_STATUS_VTL_ALREADY_ENABLED) { 189 pr_err("HVCALL_ENABLE_VP_VTL failed for VP : %d ! [Err: %#llx\n]", 190 target_vp_index, status); 191 ret = -EINVAL; 192 goto free_lock; 193 } 194 195 status = hv_do_hypercall(HVCALL_START_VP, input, NULL); 196 197 if (!hv_result_success(status)) { 198 pr_err("HVCALL_START_VP failed for VP : %d ! [Err: %#llx]\n", 199 target_vp_index, status); 200 ret = -EINVAL; 201 } 202 203 free_lock: 204 local_irq_restore(irq_flags); 205 206 return ret; 207 } 208 209 static int hv_vtl_apicid_to_vp_id(u32 apic_id) 210 { 211 u64 control; 212 u64 status; 213 unsigned long irq_flags; 214 struct hv_get_vp_from_apic_id_in *input; 215 u32 *output, ret; 216 217 local_irq_save(irq_flags); 218 219 input = *this_cpu_ptr(hyperv_pcpu_input_arg); 220 memset(input, 0, sizeof(*input)); 221 input->partition_id = HV_PARTITION_ID_SELF; 222 input->apic_ids[0] = apic_id; 223 224 output = *this_cpu_ptr(hyperv_pcpu_output_arg); 225 226 control = HV_HYPERCALL_REP_COMP_1 | HVCALL_GET_VP_ID_FROM_APIC_ID; 227 status = hv_do_hypercall(control, input, output); 228 ret = output[0]; 229 230 local_irq_restore(irq_flags); 231 232 if (!hv_result_success(status)) { 233 pr_err("failed to get vp id from apic id %d, status %#llx\n", 234 apic_id, status); 235 return -EINVAL; 236 } 237 238 return ret; 239 } 240 241 static int hv_vtl_wakeup_secondary_cpu(u32 apicid, unsigned long start_eip) 242 { 243 int vp_id, cpu; 244 245 /* Find the logical CPU for the APIC ID */ 246 for_each_present_cpu(cpu) { 247 if (arch_match_cpu_phys_id(cpu, apicid)) 248 break; 249 } 250 if (cpu >= nr_cpu_ids) 251 return -EINVAL; 252 253 pr_debug("Bringing up CPU with APIC ID %d in VTL2...\n", apicid); 254 vp_id = hv_vtl_apicid_to_vp_id(apicid); 255 256 if (vp_id < 0) { 257 pr_err("Couldn't find CPU with APIC ID %d\n", apicid); 258 return -EINVAL; 259 } 260 if (vp_id > ms_hyperv.max_vp_index) { 261 pr_err("Invalid CPU id %d for APIC ID %d\n", vp_id, apicid); 262 return -EINVAL; 263 } 264 265 return hv_vtl_bringup_vcpu(vp_id, cpu, start_eip); 266 } 267 268 int __init hv_vtl_early_init(void) 269 { 270 machine_ops.emergency_restart = hv_vtl_emergency_restart; 271 machine_ops.restart = hv_vtl_restart; 272 273 /* 274 * `boot_cpu_has` returns the runtime feature support, 275 * and here is the earliest it can be used. 276 */ 277 if (cpu_feature_enabled(X86_FEATURE_XSAVE)) 278 panic("XSAVE has to be disabled as it is not supported by this module.\n" 279 "Please add 'noxsave' to the kernel command line.\n"); 280 281 real_mode_header = &hv_vtl_real_mode_header; 282 apic_update_callback(wakeup_secondary_cpu_64, hv_vtl_wakeup_secondary_cpu); 283 284 return 0; 285 } 286