Linux Audio

Check our new training course

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