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