Skip to content

Commit 8387ce0

Browse files
tialaliuw
authored andcommitted
x86/hyperv: Set Virtual Trust Level in VMBus init message
SEV-SNP guests on Hyper-V can run at multiple Virtual Trust Levels (VTL). During boot, get the VTL at which we're running using the GET_VP_REGISTERs hypercall, and save the value for future use. Then during VMBus initialization, set the VTL with the saved value as required in the VMBus init message. Reviewed-by: Dexuan Cui <decui@microsoft.com> Reviewed-by: Michael Kelley <mikelley@microsoft.com> Signed-off-by: Tianyu Lan <tiala@microsoft.com> Signed-off-by: Wei Liu <wei.liu@kernel.org> Link: https://lore.kernel.org/r/20230818102919.1318039-3-ltykernel@gmail.com
1 parent d6e2d65 commit 8387ce0

File tree

5 files changed

+45
-2
lines changed

5 files changed

+45
-2
lines changed

arch/x86/hyperv/hv_init.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,36 @@ static void __init hv_get_partition_id(void)
378378
local_irq_restore(flags);
379379
}
380380

381+
static u8 __init get_vtl(void)
382+
{
383+
u64 control = HV_HYPERCALL_REP_COMP_1 | HVCALL_GET_VP_REGISTERS;
384+
struct hv_get_vp_registers_input *input;
385+
struct hv_get_vp_registers_output *output;
386+
unsigned long flags;
387+
u64 ret;
388+
389+
local_irq_save(flags);
390+
input = *this_cpu_ptr(hyperv_pcpu_input_arg);
391+
output = (struct hv_get_vp_registers_output *)input;
392+
393+
memset(input, 0, struct_size(input, element, 1));
394+
input->header.partitionid = HV_PARTITION_ID_SELF;
395+
input->header.vpindex = HV_VP_INDEX_SELF;
396+
input->header.inputvtl = 0;
397+
input->element[0].name0 = HV_X64_REGISTER_VSM_VP_STATUS;
398+
399+
ret = hv_do_hypercall(control, input, output);
400+
if (hv_result_success(ret)) {
401+
ret = output->as64.low & HV_X64_VTL_MASK;
402+
} else {
403+
pr_err("Failed to get VTL(%lld) and set VTL to zero by default.\n", ret);
404+
ret = 0;
405+
}
406+
407+
local_irq_restore(flags);
408+
return ret;
409+
}
410+
381411
/*
382412
* This function is to be invoked early in the boot sequence after the
383413
* hypervisor has been detected.
@@ -506,6 +536,10 @@ void __init hyperv_init(void)
506536
/* Query the VMs extended capability once, so that it can be cached. */
507537
hv_query_ext_cap(0);
508538

539+
/* Find the VTL */
540+
if (hv_isolation_type_en_snp())
541+
ms_hyperv.vtl = get_vtl();
542+
509543
return;
510544

511545
clean_guest_os_id:

arch/x86/include/asm/hyperv-tlfs.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,13 @@ enum hv_isolation_type {
301301
#define HV_X64_MSR_TIME_REF_COUNT HV_REGISTER_TIME_REF_COUNT
302302
#define HV_X64_MSR_REFERENCE_TSC HV_REGISTER_REFERENCE_TSC
303303

304+
/*
305+
* Registers are only accessible via HVCALL_GET_VP_REGISTERS hvcall and
306+
* there is not associated MSR address.
307+
*/
308+
#define HV_X64_REGISTER_VSM_VP_STATUS 0x000D0003
309+
#define HV_X64_VTL_MASK GENMASK(3, 0)
310+
304311
/* Hyper-V memory host visibility */
305312
enum hv_mem_host_visibility {
306313
VMBUS_PAGE_NOT_VISIBLE = 0,

drivers/hv/connection.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ int vmbus_negotiate_version(struct vmbus_channel_msginfo *msginfo, u32 version)
9898
*/
9999
if (version >= VERSION_WIN10_V5) {
100100
msg->msg_sint = VMBUS_MESSAGE_SINT;
101+
msg->msg_vtl = ms_hyperv.vtl;
101102
vmbus_connection.msg_conn_id = VMBUS_MESSAGE_CONNECTION_ID_4;
102103
} else {
103104
msg->interrupt_page = virt_to_phys(vmbus_connection.int_page);

include/asm-generic/mshyperv.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ struct ms_hyperv_info {
5454
};
5555
};
5656
u64 shared_gpa_boundary;
57+
u8 vtl;
5758
};
5859
extern struct ms_hyperv_info ms_hyperv;
5960
extern bool hv_nested;

include/linux/hyperv.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -665,8 +665,8 @@ struct vmbus_channel_initiate_contact {
665665
u64 interrupt_page;
666666
struct {
667667
u8 msg_sint;
668-
u8 padding1[3];
669-
u32 padding2;
668+
u8 msg_vtl;
669+
u8 reserved[6];
670670
};
671671
};
672672
u64 monitor_page1;

0 commit comments

Comments
 (0)