1 /* SPDX-License-Identifier: GPL-2.0 */ 2 3 /* 4 * Linux-specific definitions for managing interactions with Microsoft's 5 * Hyper-V hypervisor. The definitions in this file are architecture 6 * independent. See arch/<arch>/include/asm/mshyperv.h for definitions 7 * that are specific to architecture <arch>. 8 * 9 * Definitions that are specified in the Hyper-V Top Level Functional 10 * Spec (TLFS) should not go in this file, but should instead go in 11 * hyperv-tlfs.h. 12 * 13 * Copyright (C) 2019, Microsoft, Inc. 14 * 15 * Author : Michael Kelley <mikelley@microsoft.com> 16 */ 17 18 #ifndef _ASM_GENERIC_MSHYPERV_H 19 #define _ASM_GENERIC_MSHYPERV_H 20 21 #include <linux/types.h> 22 #include <linux/atomic.h> 23 #include <linux/bitops.h> 24 #include <linux/cpumask.h> 25 #include <asm/ptrace.h> 26 #include <asm/hyperv-tlfs.h> 27 28 struct ms_hyperv_info { 29 u32 features; 30 u32 priv_high; 31 u32 misc_features; 32 u32 hints; 33 u32 nested_features; 34 u32 max_vp_index; 35 u32 max_lp_index; 36 u32 isolation_config_a; 37 u32 isolation_config_b; 38 }; 39 extern struct ms_hyperv_info ms_hyperv; 40 41 extern u64 hv_do_hypercall(u64 control, void *inputaddr, void *outputaddr); 42 extern u64 hv_do_fast_hypercall8(u16 control, u64 input8); 43 44 /* 45 * Rep hypercalls. Callers of this functions are supposed to ensure that 46 * rep_count and varhead_size comply with Hyper-V hypercall definition. 47 */ 48 static inline u64 hv_do_rep_hypercall(u16 code, u16 rep_count, u16 varhead_size, 49 void *input, void *output) 50 { 51 u64 control = code; 52 u64 status; 53 u16 rep_comp; 54 55 control |= (u64)varhead_size << HV_HYPERCALL_VARHEAD_OFFSET; 56 control |= (u64)rep_count << HV_HYPERCALL_REP_COMP_OFFSET; 57 58 do { 59 status = hv_do_hypercall(control, input, output); 60 if ((status & HV_HYPERCALL_RESULT_MASK) != HV_STATUS_SUCCESS) 61 return status; 62 63 /* Bits 32-43 of status have 'Reps completed' data. */ 64 rep_comp = (status & HV_HYPERCALL_REP_COMP_MASK) >> 65 HV_HYPERCALL_REP_COMP_OFFSET; 66 67 control &= ~HV_HYPERCALL_REP_START_MASK; 68 control |= (u64)rep_comp << HV_HYPERCALL_REP_START_OFFSET; 69 70 touch_nmi_watchdog(); 71 } while (rep_comp < rep_count); 72 73 return status; 74 } 75 76 /* Generate the guest OS identifier as described in the Hyper-V TLFS */ 77 static inline __u64 generate_guest_id(__u64 d_info1, __u64 kernel_version, 78 __u64 d_info2) 79 { 80 __u64 guest_id = 0; 81 82 guest_id = (((__u64)HV_LINUX_VENDOR_ID) << 48); 83 guest_id |= (d_info1 << 48); 84 guest_id |= (kernel_version << 16); 85 guest_id |= d_info2; 86 87 return guest_id; 88 } 89 90 91 /* Free the message slot and signal end-of-message if required */ 92 static inline void vmbus_signal_eom(struct hv_message *msg, u32 old_msg_type) 93 { 94 /* 95 * On crash we're reading some other CPU's message page and we need 96 * to be careful: this other CPU may already had cleared the header 97 * and the host may already had delivered some other message there. 98 * In case we blindly write msg->header.message_type we're going 99 * to lose it. We can still lose a message of the same type but 100 * we count on the fact that there can only be one 101 * CHANNELMSG_UNLOAD_RESPONSE and we don't care about other messages 102 * on crash. 103 */ 104 if (cmpxchg(&msg->header.message_type, old_msg_type, 105 HVMSG_NONE) != old_msg_type) 106 return; 107 108 /* 109 * The cmxchg() above does an implicit memory barrier to 110 * ensure the write to MessageType (ie set to 111 * HVMSG_NONE) happens before we read the 112 * MessagePending and EOMing. Otherwise, the EOMing 113 * will not deliver any more messages since there is 114 * no empty slot 115 */ 116 if (msg->header.message_flags.msg_pending) { 117 /* 118 * This will cause message queue rescan to 119 * possibly deliver another msg from the 120 * hypervisor 121 */ 122 hv_set_register(HV_REGISTER_EOM, 0); 123 } 124 } 125 126 void hv_setup_vmbus_handler(void (*handler)(void)); 127 void hv_remove_vmbus_handler(void); 128 void hv_setup_stimer0_handler(void (*handler)(void)); 129 void hv_remove_stimer0_handler(void); 130 131 void hv_setup_kexec_handler(void (*handler)(void)); 132 void hv_remove_kexec_handler(void); 133 void hv_setup_crash_handler(void (*handler)(struct pt_regs *regs)); 134 void hv_remove_crash_handler(void); 135 136 extern int vmbus_interrupt; 137 extern int vmbus_irq; 138 139 #if IS_ENABLED(CONFIG_HYPERV) 140 /* 141 * Hypervisor's notion of virtual processor ID is different from 142 * Linux' notion of CPU ID. This information can only be retrieved 143 * in the context of the calling CPU. Setup a map for easy access 144 * to this information. 145 */ 146 extern u32 *hv_vp_index; 147 extern u32 hv_max_vp_index; 148 149 /* Sentinel value for an uninitialized entry in hv_vp_index array */ 150 #define VP_INVAL U32_MAX 151 152 void *hv_alloc_hyperv_page(void); 153 void *hv_alloc_hyperv_zeroed_page(void); 154 void hv_free_hyperv_page(unsigned long addr); 155 156 /** 157 * hv_cpu_number_to_vp_number() - Map CPU to VP. 158 * @cpu_number: CPU number in Linux terms 159 * 160 * This function returns the mapping between the Linux processor 161 * number and the hypervisor's virtual processor number, useful 162 * in making hypercalls and such that talk about specific 163 * processors. 164 * 165 * Return: Virtual processor number in Hyper-V terms 166 */ 167 static inline int hv_cpu_number_to_vp_number(int cpu_number) 168 { 169 return hv_vp_index[cpu_number]; 170 } 171 172 static inline int cpumask_to_vpset(struct hv_vpset *vpset, 173 const struct cpumask *cpus) 174 { 175 int cpu, vcpu, vcpu_bank, vcpu_offset, nr_bank = 1; 176 177 /* valid_bank_mask can represent up to 64 banks */ 178 if (hv_max_vp_index / 64 >= 64) 179 return 0; 180 181 /* 182 * Clear all banks up to the maximum possible bank as hv_tlb_flush_ex 183 * structs are not cleared between calls, we risk flushing unneeded 184 * vCPUs otherwise. 185 */ 186 for (vcpu_bank = 0; vcpu_bank <= hv_max_vp_index / 64; vcpu_bank++) 187 vpset->bank_contents[vcpu_bank] = 0; 188 189 /* 190 * Some banks may end up being empty but this is acceptable. 191 */ 192 for_each_cpu(cpu, cpus) { 193 vcpu = hv_cpu_number_to_vp_number(cpu); 194 if (vcpu == VP_INVAL) 195 return -1; 196 vcpu_bank = vcpu / 64; 197 vcpu_offset = vcpu % 64; 198 __set_bit(vcpu_offset, (unsigned long *) 199 &vpset->bank_contents[vcpu_bank]); 200 if (vcpu_bank >= nr_bank) 201 nr_bank = vcpu_bank + 1; 202 } 203 vpset->valid_bank_mask = GENMASK_ULL(nr_bank - 1, 0); 204 return nr_bank; 205 } 206 207 void hyperv_report_panic(struct pt_regs *regs, long err, bool in_die); 208 bool hv_is_hyperv_initialized(void); 209 bool hv_is_hibernation_supported(void); 210 enum hv_isolation_type hv_get_isolation_type(void); 211 bool hv_is_isolation_supported(void); 212 void hyperv_cleanup(void); 213 bool hv_query_ext_cap(u64 cap_query); 214 #else /* CONFIG_HYPERV */ 215 static inline bool hv_is_hyperv_initialized(void) { return false; } 216 static inline bool hv_is_hibernation_supported(void) { return false; } 217 static inline void hyperv_cleanup(void) {} 218 #endif /* CONFIG_HYPERV */ 219 220 #endif 221