shared.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * AMD Encrypted Register State Support
  4. *
  5. * Author: Joerg Roedel <jroedel@suse.de>
  6. *
  7. * This file is not compiled stand-alone. It contains code shared
  8. * between the pre-decompression boot code and the running Linux kernel
  9. * and is included directly into both code-bases.
  10. */
  11. #include <asm/setup_data.h>
  12. #ifndef __BOOT_COMPRESSED
  13. #define error(v) pr_err(v)
  14. #define has_cpuflag(f) boot_cpu_has(f)
  15. #define sev_printk(fmt, ...) printk(fmt, ##__VA_ARGS__)
  16. #define sev_printk_rtl(fmt, ...) printk_ratelimited(fmt, ##__VA_ARGS__)
  17. #else
  18. #undef WARN
  19. #define WARN(condition, format...) (!!(condition))
  20. #define sev_printk(fmt, ...)
  21. #define sev_printk_rtl(fmt, ...)
  22. #undef vc_forward_exception
  23. #define vc_forward_exception(c) panic("SNP: Hypervisor requested exception\n")
  24. #endif
  25. /*
  26. * SVSM related information:
  27. * When running under an SVSM, the VMPL that Linux is executing at must be
  28. * non-zero. The VMPL is therefore used to indicate the presence of an SVSM.
  29. *
  30. * During boot, the page tables are set up as identity mapped and later
  31. * changed to use kernel virtual addresses. Maintain separate virtual and
  32. * physical addresses for the CAA to allow SVSM functions to be used during
  33. * early boot, both with identity mapped virtual addresses and proper kernel
  34. * virtual addresses.
  35. */
  36. u8 snp_vmpl __ro_after_init;
  37. EXPORT_SYMBOL_GPL(snp_vmpl);
  38. static struct svsm_ca *boot_svsm_caa __ro_after_init;
  39. static u64 boot_svsm_caa_pa __ro_after_init;
  40. static struct svsm_ca *svsm_get_caa(void);
  41. static u64 svsm_get_caa_pa(void);
  42. static int svsm_perform_call_protocol(struct svsm_call *call);
  43. /* I/O parameters for CPUID-related helpers */
  44. struct cpuid_leaf {
  45. u32 fn;
  46. u32 subfn;
  47. u32 eax;
  48. u32 ebx;
  49. u32 ecx;
  50. u32 edx;
  51. };
  52. /*
  53. * Individual entries of the SNP CPUID table, as defined by the SNP
  54. * Firmware ABI, Revision 0.9, Section 7.1, Table 14.
  55. */
  56. struct snp_cpuid_fn {
  57. u32 eax_in;
  58. u32 ecx_in;
  59. u64 xcr0_in;
  60. u64 xss_in;
  61. u32 eax;
  62. u32 ebx;
  63. u32 ecx;
  64. u32 edx;
  65. u64 __reserved;
  66. } __packed;
  67. /*
  68. * SNP CPUID table, as defined by the SNP Firmware ABI, Revision 0.9,
  69. * Section 8.14.2.6. Also noted there is the SNP firmware-enforced limit
  70. * of 64 entries per CPUID table.
  71. */
  72. #define SNP_CPUID_COUNT_MAX 64
  73. struct snp_cpuid_table {
  74. u32 count;
  75. u32 __reserved1;
  76. u64 __reserved2;
  77. struct snp_cpuid_fn fn[SNP_CPUID_COUNT_MAX];
  78. } __packed;
  79. /*
  80. * Since feature negotiation related variables are set early in the boot
  81. * process they must reside in the .data section so as not to be zeroed
  82. * out when the .bss section is later cleared.
  83. *
  84. * GHCB protocol version negotiated with the hypervisor.
  85. */
  86. static u16 ghcb_version __ro_after_init;
  87. /* Copy of the SNP firmware's CPUID page. */
  88. static struct snp_cpuid_table cpuid_table_copy __ro_after_init;
  89. /*
  90. * These will be initialized based on CPUID table so that non-present
  91. * all-zero leaves (for sparse tables) can be differentiated from
  92. * invalid/out-of-range leaves. This is needed since all-zero leaves
  93. * still need to be post-processed.
  94. */
  95. static u32 cpuid_std_range_max __ro_after_init;
  96. static u32 cpuid_hyp_range_max __ro_after_init;
  97. static u32 cpuid_ext_range_max __ro_after_init;
  98. static bool __init sev_es_check_cpu_features(void)
  99. {
  100. if (!has_cpuflag(X86_FEATURE_RDRAND)) {
  101. error("RDRAND instruction not supported - no trusted source of randomness available\n");
  102. return false;
  103. }
  104. return true;
  105. }
  106. static void __head __noreturn
  107. sev_es_terminate(unsigned int set, unsigned int reason)
  108. {
  109. u64 val = GHCB_MSR_TERM_REQ;
  110. /* Tell the hypervisor what went wrong. */
  111. val |= GHCB_SEV_TERM_REASON(set, reason);
  112. /* Request Guest Termination from Hypervisor */
  113. sev_es_wr_ghcb_msr(val);
  114. VMGEXIT();
  115. while (true)
  116. asm volatile("hlt\n" : : : "memory");
  117. }
  118. /*
  119. * The hypervisor features are available from GHCB version 2 onward.
  120. */
  121. static u64 get_hv_features(void)
  122. {
  123. u64 val;
  124. if (ghcb_version < 2)
  125. return 0;
  126. sev_es_wr_ghcb_msr(GHCB_MSR_HV_FT_REQ);
  127. VMGEXIT();
  128. val = sev_es_rd_ghcb_msr();
  129. if (GHCB_RESP_CODE(val) != GHCB_MSR_HV_FT_RESP)
  130. return 0;
  131. return GHCB_MSR_HV_FT_RESP_VAL(val);
  132. }
  133. static void snp_register_ghcb_early(unsigned long paddr)
  134. {
  135. unsigned long pfn = paddr >> PAGE_SHIFT;
  136. u64 val;
  137. sev_es_wr_ghcb_msr(GHCB_MSR_REG_GPA_REQ_VAL(pfn));
  138. VMGEXIT();
  139. val = sev_es_rd_ghcb_msr();
  140. /* If the response GPA is not ours then abort the guest */
  141. if ((GHCB_RESP_CODE(val) != GHCB_MSR_REG_GPA_RESP) ||
  142. (GHCB_MSR_REG_GPA_RESP_VAL(val) != pfn))
  143. sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_REGISTER);
  144. }
  145. static bool sev_es_negotiate_protocol(void)
  146. {
  147. u64 val;
  148. /* Do the GHCB protocol version negotiation */
  149. sev_es_wr_ghcb_msr(GHCB_MSR_SEV_INFO_REQ);
  150. VMGEXIT();
  151. val = sev_es_rd_ghcb_msr();
  152. if (GHCB_MSR_INFO(val) != GHCB_MSR_SEV_INFO_RESP)
  153. return false;
  154. if (GHCB_MSR_PROTO_MAX(val) < GHCB_PROTOCOL_MIN ||
  155. GHCB_MSR_PROTO_MIN(val) > GHCB_PROTOCOL_MAX)
  156. return false;
  157. ghcb_version = min_t(size_t, GHCB_MSR_PROTO_MAX(val), GHCB_PROTOCOL_MAX);
  158. return true;
  159. }
  160. static __always_inline void vc_ghcb_invalidate(struct ghcb *ghcb)
  161. {
  162. ghcb->save.sw_exit_code = 0;
  163. __builtin_memset(ghcb->save.valid_bitmap, 0, sizeof(ghcb->save.valid_bitmap));
  164. }
  165. static bool vc_decoding_needed(unsigned long exit_code)
  166. {
  167. /* Exceptions don't require to decode the instruction */
  168. return !(exit_code >= SVM_EXIT_EXCP_BASE &&
  169. exit_code <= SVM_EXIT_LAST_EXCP);
  170. }
  171. static enum es_result vc_init_em_ctxt(struct es_em_ctxt *ctxt,
  172. struct pt_regs *regs,
  173. unsigned long exit_code)
  174. {
  175. enum es_result ret = ES_OK;
  176. memset(ctxt, 0, sizeof(*ctxt));
  177. ctxt->regs = regs;
  178. if (vc_decoding_needed(exit_code))
  179. ret = vc_decode_insn(ctxt);
  180. return ret;
  181. }
  182. static void vc_finish_insn(struct es_em_ctxt *ctxt)
  183. {
  184. ctxt->regs->ip += ctxt->insn.length;
  185. }
  186. static enum es_result verify_exception_info(struct ghcb *ghcb, struct es_em_ctxt *ctxt)
  187. {
  188. u32 ret;
  189. ret = ghcb->save.sw_exit_info_1 & GENMASK_ULL(31, 0);
  190. if (!ret)
  191. return ES_OK;
  192. if (ret == 1) {
  193. u64 info = ghcb->save.sw_exit_info_2;
  194. unsigned long v = info & SVM_EVTINJ_VEC_MASK;
  195. /* Check if exception information from hypervisor is sane. */
  196. if ((info & SVM_EVTINJ_VALID) &&
  197. ((v == X86_TRAP_GP) || (v == X86_TRAP_UD)) &&
  198. ((info & SVM_EVTINJ_TYPE_MASK) == SVM_EVTINJ_TYPE_EXEPT)) {
  199. ctxt->fi.vector = v;
  200. if (info & SVM_EVTINJ_VALID_ERR)
  201. ctxt->fi.error_code = info >> 32;
  202. return ES_EXCEPTION;
  203. }
  204. }
  205. return ES_VMM_ERROR;
  206. }
  207. static inline int svsm_process_result_codes(struct svsm_call *call)
  208. {
  209. switch (call->rax_out) {
  210. case SVSM_SUCCESS:
  211. return 0;
  212. case SVSM_ERR_INCOMPLETE:
  213. case SVSM_ERR_BUSY:
  214. return -EAGAIN;
  215. default:
  216. return -EINVAL;
  217. }
  218. }
  219. /*
  220. * Issue a VMGEXIT to call the SVSM:
  221. * - Load the SVSM register state (RAX, RCX, RDX, R8 and R9)
  222. * - Set the CA call pending field to 1
  223. * - Issue VMGEXIT
  224. * - Save the SVSM return register state (RAX, RCX, RDX, R8 and R9)
  225. * - Perform atomic exchange of the CA call pending field
  226. *
  227. * - See the "Secure VM Service Module for SEV-SNP Guests" specification for
  228. * details on the calling convention.
  229. * - The calling convention loosely follows the Microsoft X64 calling
  230. * convention by putting arguments in RCX, RDX, R8 and R9.
  231. * - RAX specifies the SVSM protocol/callid as input and the return code
  232. * as output.
  233. */
  234. static __always_inline void svsm_issue_call(struct svsm_call *call, u8 *pending)
  235. {
  236. register unsigned long rax asm("rax") = call->rax;
  237. register unsigned long rcx asm("rcx") = call->rcx;
  238. register unsigned long rdx asm("rdx") = call->rdx;
  239. register unsigned long r8 asm("r8") = call->r8;
  240. register unsigned long r9 asm("r9") = call->r9;
  241. call->caa->call_pending = 1;
  242. asm volatile("rep; vmmcall\n\t"
  243. : "+r" (rax), "+r" (rcx), "+r" (rdx), "+r" (r8), "+r" (r9)
  244. : : "memory");
  245. *pending = xchg(&call->caa->call_pending, *pending);
  246. call->rax_out = rax;
  247. call->rcx_out = rcx;
  248. call->rdx_out = rdx;
  249. call->r8_out = r8;
  250. call->r9_out = r9;
  251. }
  252. static int svsm_perform_msr_protocol(struct svsm_call *call)
  253. {
  254. u8 pending = 0;
  255. u64 val, resp;
  256. /*
  257. * When using the MSR protocol, be sure to save and restore
  258. * the current MSR value.
  259. */
  260. val = sev_es_rd_ghcb_msr();
  261. sev_es_wr_ghcb_msr(GHCB_MSR_VMPL_REQ_LEVEL(0));
  262. svsm_issue_call(call, &pending);
  263. resp = sev_es_rd_ghcb_msr();
  264. sev_es_wr_ghcb_msr(val);
  265. if (pending)
  266. return -EINVAL;
  267. if (GHCB_RESP_CODE(resp) != GHCB_MSR_VMPL_RESP)
  268. return -EINVAL;
  269. if (GHCB_MSR_VMPL_RESP_VAL(resp))
  270. return -EINVAL;
  271. return svsm_process_result_codes(call);
  272. }
  273. static int svsm_perform_ghcb_protocol(struct ghcb *ghcb, struct svsm_call *call)
  274. {
  275. struct es_em_ctxt ctxt;
  276. u8 pending = 0;
  277. vc_ghcb_invalidate(ghcb);
  278. /*
  279. * Fill in protocol and format specifiers. This can be called very early
  280. * in the boot, so use rip-relative references as needed.
  281. */
  282. ghcb->protocol_version = RIP_REL_REF(ghcb_version);
  283. ghcb->ghcb_usage = GHCB_DEFAULT_USAGE;
  284. ghcb_set_sw_exit_code(ghcb, SVM_VMGEXIT_SNP_RUN_VMPL);
  285. ghcb_set_sw_exit_info_1(ghcb, 0);
  286. ghcb_set_sw_exit_info_2(ghcb, 0);
  287. sev_es_wr_ghcb_msr(__pa(ghcb));
  288. svsm_issue_call(call, &pending);
  289. if (pending)
  290. return -EINVAL;
  291. switch (verify_exception_info(ghcb, &ctxt)) {
  292. case ES_OK:
  293. break;
  294. case ES_EXCEPTION:
  295. vc_forward_exception(&ctxt);
  296. fallthrough;
  297. default:
  298. return -EINVAL;
  299. }
  300. return svsm_process_result_codes(call);
  301. }
  302. static enum es_result sev_es_ghcb_hv_call(struct ghcb *ghcb,
  303. struct es_em_ctxt *ctxt,
  304. u64 exit_code, u64 exit_info_1,
  305. u64 exit_info_2)
  306. {
  307. /* Fill in protocol and format specifiers */
  308. ghcb->protocol_version = ghcb_version;
  309. ghcb->ghcb_usage = GHCB_DEFAULT_USAGE;
  310. ghcb_set_sw_exit_code(ghcb, exit_code);
  311. ghcb_set_sw_exit_info_1(ghcb, exit_info_1);
  312. ghcb_set_sw_exit_info_2(ghcb, exit_info_2);
  313. sev_es_wr_ghcb_msr(__pa(ghcb));
  314. VMGEXIT();
  315. return verify_exception_info(ghcb, ctxt);
  316. }
  317. static int __sev_cpuid_hv(u32 fn, int reg_idx, u32 *reg)
  318. {
  319. u64 val;
  320. sev_es_wr_ghcb_msr(GHCB_CPUID_REQ(fn, reg_idx));
  321. VMGEXIT();
  322. val = sev_es_rd_ghcb_msr();
  323. if (GHCB_RESP_CODE(val) != GHCB_MSR_CPUID_RESP)
  324. return -EIO;
  325. *reg = (val >> 32);
  326. return 0;
  327. }
  328. static int __sev_cpuid_hv_msr(struct cpuid_leaf *leaf)
  329. {
  330. int ret;
  331. /*
  332. * MSR protocol does not support fetching non-zero subfunctions, but is
  333. * sufficient to handle current early-boot cases. Should that change,
  334. * make sure to report an error rather than ignoring the index and
  335. * grabbing random values. If this issue arises in the future, handling
  336. * can be added here to use GHCB-page protocol for cases that occur late
  337. * enough in boot that GHCB page is available.
  338. */
  339. if (cpuid_function_is_indexed(leaf->fn) && leaf->subfn)
  340. return -EINVAL;
  341. ret = __sev_cpuid_hv(leaf->fn, GHCB_CPUID_REQ_EAX, &leaf->eax);
  342. ret = ret ? : __sev_cpuid_hv(leaf->fn, GHCB_CPUID_REQ_EBX, &leaf->ebx);
  343. ret = ret ? : __sev_cpuid_hv(leaf->fn, GHCB_CPUID_REQ_ECX, &leaf->ecx);
  344. ret = ret ? : __sev_cpuid_hv(leaf->fn, GHCB_CPUID_REQ_EDX, &leaf->edx);
  345. return ret;
  346. }
  347. static int __sev_cpuid_hv_ghcb(struct ghcb *ghcb, struct es_em_ctxt *ctxt, struct cpuid_leaf *leaf)
  348. {
  349. u32 cr4 = native_read_cr4();
  350. int ret;
  351. ghcb_set_rax(ghcb, leaf->fn);
  352. ghcb_set_rcx(ghcb, leaf->subfn);
  353. if (cr4 & X86_CR4_OSXSAVE)
  354. /* Safe to read xcr0 */
  355. ghcb_set_xcr0(ghcb, xgetbv(XCR_XFEATURE_ENABLED_MASK));
  356. else
  357. /* xgetbv will cause #UD - use reset value for xcr0 */
  358. ghcb_set_xcr0(ghcb, 1);
  359. ret = sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_CPUID, 0, 0);
  360. if (ret != ES_OK)
  361. return ret;
  362. if (!(ghcb_rax_is_valid(ghcb) &&
  363. ghcb_rbx_is_valid(ghcb) &&
  364. ghcb_rcx_is_valid(ghcb) &&
  365. ghcb_rdx_is_valid(ghcb)))
  366. return ES_VMM_ERROR;
  367. leaf->eax = ghcb->save.rax;
  368. leaf->ebx = ghcb->save.rbx;
  369. leaf->ecx = ghcb->save.rcx;
  370. leaf->edx = ghcb->save.rdx;
  371. return ES_OK;
  372. }
  373. static int sev_cpuid_hv(struct ghcb *ghcb, struct es_em_ctxt *ctxt, struct cpuid_leaf *leaf)
  374. {
  375. return ghcb ? __sev_cpuid_hv_ghcb(ghcb, ctxt, leaf)
  376. : __sev_cpuid_hv_msr(leaf);
  377. }
  378. /*
  379. * This may be called early while still running on the initial identity
  380. * mapping. Use RIP-relative addressing to obtain the correct address
  381. * while running with the initial identity mapping as well as the
  382. * switch-over to kernel virtual addresses later.
  383. */
  384. static const struct snp_cpuid_table *snp_cpuid_get_table(void)
  385. {
  386. return &RIP_REL_REF(cpuid_table_copy);
  387. }
  388. /*
  389. * The SNP Firmware ABI, Revision 0.9, Section 7.1, details the use of
  390. * XCR0_IN and XSS_IN to encode multiple versions of 0xD subfunctions 0
  391. * and 1 based on the corresponding features enabled by a particular
  392. * combination of XCR0 and XSS registers so that a guest can look up the
  393. * version corresponding to the features currently enabled in its XCR0/XSS
  394. * registers. The only values that differ between these versions/table
  395. * entries is the enabled XSAVE area size advertised via EBX.
  396. *
  397. * While hypervisors may choose to make use of this support, it is more
  398. * robust/secure for a guest to simply find the entry corresponding to the
  399. * base/legacy XSAVE area size (XCR0=1 or XCR0=3), and then calculate the
  400. * XSAVE area size using subfunctions 2 through 64, as documented in APM
  401. * Volume 3, Rev 3.31, Appendix E.3.8, which is what is done here.
  402. *
  403. * Since base/legacy XSAVE area size is documented as 0x240, use that value
  404. * directly rather than relying on the base size in the CPUID table.
  405. *
  406. * Return: XSAVE area size on success, 0 otherwise.
  407. */
  408. static u32 snp_cpuid_calc_xsave_size(u64 xfeatures_en, bool compacted)
  409. {
  410. const struct snp_cpuid_table *cpuid_table = snp_cpuid_get_table();
  411. u64 xfeatures_found = 0;
  412. u32 xsave_size = 0x240;
  413. int i;
  414. for (i = 0; i < cpuid_table->count; i++) {
  415. const struct snp_cpuid_fn *e = &cpuid_table->fn[i];
  416. if (!(e->eax_in == 0xD && e->ecx_in > 1 && e->ecx_in < 64))
  417. continue;
  418. if (!(xfeatures_en & (BIT_ULL(e->ecx_in))))
  419. continue;
  420. if (xfeatures_found & (BIT_ULL(e->ecx_in)))
  421. continue;
  422. xfeatures_found |= (BIT_ULL(e->ecx_in));
  423. if (compacted)
  424. xsave_size += e->eax;
  425. else
  426. xsave_size = max(xsave_size, e->eax + e->ebx);
  427. }
  428. /*
  429. * Either the guest set unsupported XCR0/XSS bits, or the corresponding
  430. * entries in the CPUID table were not present. This is not a valid
  431. * state to be in.
  432. */
  433. if (xfeatures_found != (xfeatures_en & GENMASK_ULL(63, 2)))
  434. return 0;
  435. return xsave_size;
  436. }
  437. static bool __head
  438. snp_cpuid_get_validated_func(struct cpuid_leaf *leaf)
  439. {
  440. const struct snp_cpuid_table *cpuid_table = snp_cpuid_get_table();
  441. int i;
  442. for (i = 0; i < cpuid_table->count; i++) {
  443. const struct snp_cpuid_fn *e = &cpuid_table->fn[i];
  444. if (e->eax_in != leaf->fn)
  445. continue;
  446. if (cpuid_function_is_indexed(leaf->fn) && e->ecx_in != leaf->subfn)
  447. continue;
  448. /*
  449. * For 0xD subfunctions 0 and 1, only use the entry corresponding
  450. * to the base/legacy XSAVE area size (XCR0=1 or XCR0=3, XSS=0).
  451. * See the comments above snp_cpuid_calc_xsave_size() for more
  452. * details.
  453. */
  454. if (e->eax_in == 0xD && (e->ecx_in == 0 || e->ecx_in == 1))
  455. if (!(e->xcr0_in == 1 || e->xcr0_in == 3) || e->xss_in)
  456. continue;
  457. leaf->eax = e->eax;
  458. leaf->ebx = e->ebx;
  459. leaf->ecx = e->ecx;
  460. leaf->edx = e->edx;
  461. return true;
  462. }
  463. return false;
  464. }
  465. static void snp_cpuid_hv(struct ghcb *ghcb, struct es_em_ctxt *ctxt, struct cpuid_leaf *leaf)
  466. {
  467. if (sev_cpuid_hv(ghcb, ctxt, leaf))
  468. sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_CPUID_HV);
  469. }
  470. static int snp_cpuid_postprocess(struct ghcb *ghcb, struct es_em_ctxt *ctxt,
  471. struct cpuid_leaf *leaf)
  472. {
  473. struct cpuid_leaf leaf_hv = *leaf;
  474. switch (leaf->fn) {
  475. case 0x1:
  476. snp_cpuid_hv(ghcb, ctxt, &leaf_hv);
  477. /* initial APIC ID */
  478. leaf->ebx = (leaf_hv.ebx & GENMASK(31, 24)) | (leaf->ebx & GENMASK(23, 0));
  479. /* APIC enabled bit */
  480. leaf->edx = (leaf_hv.edx & BIT(9)) | (leaf->edx & ~BIT(9));
  481. /* OSXSAVE enabled bit */
  482. if (native_read_cr4() & X86_CR4_OSXSAVE)
  483. leaf->ecx |= BIT(27);
  484. break;
  485. case 0x7:
  486. /* OSPKE enabled bit */
  487. leaf->ecx &= ~BIT(4);
  488. if (native_read_cr4() & X86_CR4_PKE)
  489. leaf->ecx |= BIT(4);
  490. break;
  491. case 0xB:
  492. leaf_hv.subfn = 0;
  493. snp_cpuid_hv(ghcb, ctxt, &leaf_hv);
  494. /* extended APIC ID */
  495. leaf->edx = leaf_hv.edx;
  496. break;
  497. case 0xD: {
  498. bool compacted = false;
  499. u64 xcr0 = 1, xss = 0;
  500. u32 xsave_size;
  501. if (leaf->subfn != 0 && leaf->subfn != 1)
  502. return 0;
  503. if (native_read_cr4() & X86_CR4_OSXSAVE)
  504. xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK);
  505. if (leaf->subfn == 1) {
  506. /* Get XSS value if XSAVES is enabled. */
  507. if (leaf->eax & BIT(3)) {
  508. unsigned long lo, hi;
  509. asm volatile("rdmsr" : "=a" (lo), "=d" (hi)
  510. : "c" (MSR_IA32_XSS));
  511. xss = (hi << 32) | lo;
  512. }
  513. /*
  514. * The PPR and APM aren't clear on what size should be
  515. * encoded in 0xD:0x1:EBX when compaction is not enabled
  516. * by either XSAVEC (feature bit 1) or XSAVES (feature
  517. * bit 3) since SNP-capable hardware has these feature
  518. * bits fixed as 1. KVM sets it to 0 in this case, but
  519. * to avoid this becoming an issue it's safer to simply
  520. * treat this as unsupported for SNP guests.
  521. */
  522. if (!(leaf->eax & (BIT(1) | BIT(3))))
  523. return -EINVAL;
  524. compacted = true;
  525. }
  526. xsave_size = snp_cpuid_calc_xsave_size(xcr0 | xss, compacted);
  527. if (!xsave_size)
  528. return -EINVAL;
  529. leaf->ebx = xsave_size;
  530. }
  531. break;
  532. case 0x8000001E:
  533. snp_cpuid_hv(ghcb, ctxt, &leaf_hv);
  534. /* extended APIC ID */
  535. leaf->eax = leaf_hv.eax;
  536. /* compute ID */
  537. leaf->ebx = (leaf->ebx & GENMASK(31, 8)) | (leaf_hv.ebx & GENMASK(7, 0));
  538. /* node ID */
  539. leaf->ecx = (leaf->ecx & GENMASK(31, 8)) | (leaf_hv.ecx & GENMASK(7, 0));
  540. break;
  541. default:
  542. /* No fix-ups needed, use values as-is. */
  543. break;
  544. }
  545. return 0;
  546. }
  547. /*
  548. * Returns -EOPNOTSUPP if feature not enabled. Any other non-zero return value
  549. * should be treated as fatal by caller.
  550. */
  551. static int __head
  552. snp_cpuid(struct ghcb *ghcb, struct es_em_ctxt *ctxt, struct cpuid_leaf *leaf)
  553. {
  554. const struct snp_cpuid_table *cpuid_table = snp_cpuid_get_table();
  555. if (!cpuid_table->count)
  556. return -EOPNOTSUPP;
  557. if (!snp_cpuid_get_validated_func(leaf)) {
  558. /*
  559. * Some hypervisors will avoid keeping track of CPUID entries
  560. * where all values are zero, since they can be handled the
  561. * same as out-of-range values (all-zero). This is useful here
  562. * as well as it allows virtually all guest configurations to
  563. * work using a single SNP CPUID table.
  564. *
  565. * To allow for this, there is a need to distinguish between
  566. * out-of-range entries and in-range zero entries, since the
  567. * CPUID table entries are only a template that may need to be
  568. * augmented with additional values for things like
  569. * CPU-specific information during post-processing. So if it's
  570. * not in the table, set the values to zero. Then, if they are
  571. * within a valid CPUID range, proceed with post-processing
  572. * using zeros as the initial values. Otherwise, skip
  573. * post-processing and just return zeros immediately.
  574. */
  575. leaf->eax = leaf->ebx = leaf->ecx = leaf->edx = 0;
  576. /* Skip post-processing for out-of-range zero leafs. */
  577. if (!(leaf->fn <= RIP_REL_REF(cpuid_std_range_max) ||
  578. (leaf->fn >= 0x40000000 && leaf->fn <= RIP_REL_REF(cpuid_hyp_range_max)) ||
  579. (leaf->fn >= 0x80000000 && leaf->fn <= RIP_REL_REF(cpuid_ext_range_max))))
  580. return 0;
  581. }
  582. return snp_cpuid_postprocess(ghcb, ctxt, leaf);
  583. }
  584. /*
  585. * Boot VC Handler - This is the first VC handler during boot, there is no GHCB
  586. * page yet, so it only supports the MSR based communication with the
  587. * hypervisor and only the CPUID exit-code.
  588. */
  589. void __head do_vc_no_ghcb(struct pt_regs *regs, unsigned long exit_code)
  590. {
  591. unsigned int subfn = lower_bits(regs->cx, 32);
  592. unsigned int fn = lower_bits(regs->ax, 32);
  593. u16 opcode = *(unsigned short *)regs->ip;
  594. struct cpuid_leaf leaf;
  595. int ret;
  596. /* Only CPUID is supported via MSR protocol */
  597. if (exit_code != SVM_EXIT_CPUID)
  598. goto fail;
  599. /* Is it really a CPUID insn? */
  600. if (opcode != 0xa20f)
  601. goto fail;
  602. leaf.fn = fn;
  603. leaf.subfn = subfn;
  604. ret = snp_cpuid(NULL, NULL, &leaf);
  605. if (!ret)
  606. goto cpuid_done;
  607. if (ret != -EOPNOTSUPP)
  608. goto fail;
  609. if (__sev_cpuid_hv_msr(&leaf))
  610. goto fail;
  611. cpuid_done:
  612. regs->ax = leaf.eax;
  613. regs->bx = leaf.ebx;
  614. regs->cx = leaf.ecx;
  615. regs->dx = leaf.edx;
  616. /*
  617. * This is a VC handler and the #VC is only raised when SEV-ES is
  618. * active, which means SEV must be active too. Do sanity checks on the
  619. * CPUID results to make sure the hypervisor does not trick the kernel
  620. * into the no-sev path. This could map sensitive data unencrypted and
  621. * make it accessible to the hypervisor.
  622. *
  623. * In particular, check for:
  624. * - Availability of CPUID leaf 0x8000001f
  625. * - SEV CPUID bit.
  626. *
  627. * The hypervisor might still report the wrong C-bit position, but this
  628. * can't be checked here.
  629. */
  630. if (fn == 0x80000000 && (regs->ax < 0x8000001f))
  631. /* SEV leaf check */
  632. goto fail;
  633. else if ((fn == 0x8000001f && !(regs->ax & BIT(1))))
  634. /* SEV bit */
  635. goto fail;
  636. /* Skip over the CPUID two-byte opcode */
  637. regs->ip += 2;
  638. return;
  639. fail:
  640. /* Terminate the guest */
  641. sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_GEN_REQ);
  642. }
  643. static enum es_result vc_insn_string_check(struct es_em_ctxt *ctxt,
  644. unsigned long address,
  645. bool write)
  646. {
  647. if (user_mode(ctxt->regs) && fault_in_kernel_space(address)) {
  648. ctxt->fi.vector = X86_TRAP_PF;
  649. ctxt->fi.error_code = X86_PF_USER;
  650. ctxt->fi.cr2 = address;
  651. if (write)
  652. ctxt->fi.error_code |= X86_PF_WRITE;
  653. return ES_EXCEPTION;
  654. }
  655. return ES_OK;
  656. }
  657. static enum es_result vc_insn_string_read(struct es_em_ctxt *ctxt,
  658. void *src, char *buf,
  659. unsigned int data_size,
  660. unsigned int count,
  661. bool backwards)
  662. {
  663. int i, b = backwards ? -1 : 1;
  664. unsigned long address = (unsigned long)src;
  665. enum es_result ret;
  666. ret = vc_insn_string_check(ctxt, address, false);
  667. if (ret != ES_OK)
  668. return ret;
  669. for (i = 0; i < count; i++) {
  670. void *s = src + (i * data_size * b);
  671. char *d = buf + (i * data_size);
  672. ret = vc_read_mem(ctxt, s, d, data_size);
  673. if (ret != ES_OK)
  674. break;
  675. }
  676. return ret;
  677. }
  678. static enum es_result vc_insn_string_write(struct es_em_ctxt *ctxt,
  679. void *dst, char *buf,
  680. unsigned int data_size,
  681. unsigned int count,
  682. bool backwards)
  683. {
  684. int i, s = backwards ? -1 : 1;
  685. unsigned long address = (unsigned long)dst;
  686. enum es_result ret;
  687. ret = vc_insn_string_check(ctxt, address, true);
  688. if (ret != ES_OK)
  689. return ret;
  690. for (i = 0; i < count; i++) {
  691. void *d = dst + (i * data_size * s);
  692. char *b = buf + (i * data_size);
  693. ret = vc_write_mem(ctxt, d, b, data_size);
  694. if (ret != ES_OK)
  695. break;
  696. }
  697. return ret;
  698. }
  699. #define IOIO_TYPE_STR BIT(2)
  700. #define IOIO_TYPE_IN 1
  701. #define IOIO_TYPE_INS (IOIO_TYPE_IN | IOIO_TYPE_STR)
  702. #define IOIO_TYPE_OUT 0
  703. #define IOIO_TYPE_OUTS (IOIO_TYPE_OUT | IOIO_TYPE_STR)
  704. #define IOIO_REP BIT(3)
  705. #define IOIO_ADDR_64 BIT(9)
  706. #define IOIO_ADDR_32 BIT(8)
  707. #define IOIO_ADDR_16 BIT(7)
  708. #define IOIO_DATA_32 BIT(6)
  709. #define IOIO_DATA_16 BIT(5)
  710. #define IOIO_DATA_8 BIT(4)
  711. #define IOIO_SEG_ES (0 << 10)
  712. #define IOIO_SEG_DS (3 << 10)
  713. static enum es_result vc_ioio_exitinfo(struct es_em_ctxt *ctxt, u64 *exitinfo)
  714. {
  715. struct insn *insn = &ctxt->insn;
  716. size_t size;
  717. u64 port;
  718. *exitinfo = 0;
  719. switch (insn->opcode.bytes[0]) {
  720. /* INS opcodes */
  721. case 0x6c:
  722. case 0x6d:
  723. *exitinfo |= IOIO_TYPE_INS;
  724. *exitinfo |= IOIO_SEG_ES;
  725. port = ctxt->regs->dx & 0xffff;
  726. break;
  727. /* OUTS opcodes */
  728. case 0x6e:
  729. case 0x6f:
  730. *exitinfo |= IOIO_TYPE_OUTS;
  731. *exitinfo |= IOIO_SEG_DS;
  732. port = ctxt->regs->dx & 0xffff;
  733. break;
  734. /* IN immediate opcodes */
  735. case 0xe4:
  736. case 0xe5:
  737. *exitinfo |= IOIO_TYPE_IN;
  738. port = (u8)insn->immediate.value & 0xffff;
  739. break;
  740. /* OUT immediate opcodes */
  741. case 0xe6:
  742. case 0xe7:
  743. *exitinfo |= IOIO_TYPE_OUT;
  744. port = (u8)insn->immediate.value & 0xffff;
  745. break;
  746. /* IN register opcodes */
  747. case 0xec:
  748. case 0xed:
  749. *exitinfo |= IOIO_TYPE_IN;
  750. port = ctxt->regs->dx & 0xffff;
  751. break;
  752. /* OUT register opcodes */
  753. case 0xee:
  754. case 0xef:
  755. *exitinfo |= IOIO_TYPE_OUT;
  756. port = ctxt->regs->dx & 0xffff;
  757. break;
  758. default:
  759. return ES_DECODE_FAILED;
  760. }
  761. *exitinfo |= port << 16;
  762. switch (insn->opcode.bytes[0]) {
  763. case 0x6c:
  764. case 0x6e:
  765. case 0xe4:
  766. case 0xe6:
  767. case 0xec:
  768. case 0xee:
  769. /* Single byte opcodes */
  770. *exitinfo |= IOIO_DATA_8;
  771. size = 1;
  772. break;
  773. default:
  774. /* Length determined by instruction parsing */
  775. *exitinfo |= (insn->opnd_bytes == 2) ? IOIO_DATA_16
  776. : IOIO_DATA_32;
  777. size = (insn->opnd_bytes == 2) ? 2 : 4;
  778. }
  779. switch (insn->addr_bytes) {
  780. case 2:
  781. *exitinfo |= IOIO_ADDR_16;
  782. break;
  783. case 4:
  784. *exitinfo |= IOIO_ADDR_32;
  785. break;
  786. case 8:
  787. *exitinfo |= IOIO_ADDR_64;
  788. break;
  789. }
  790. if (insn_has_rep_prefix(insn))
  791. *exitinfo |= IOIO_REP;
  792. return vc_ioio_check(ctxt, (u16)port, size);
  793. }
  794. static enum es_result vc_handle_ioio(struct ghcb *ghcb, struct es_em_ctxt *ctxt)
  795. {
  796. struct pt_regs *regs = ctxt->regs;
  797. u64 exit_info_1, exit_info_2;
  798. enum es_result ret;
  799. ret = vc_ioio_exitinfo(ctxt, &exit_info_1);
  800. if (ret != ES_OK)
  801. return ret;
  802. if (exit_info_1 & IOIO_TYPE_STR) {
  803. /* (REP) INS/OUTS */
  804. bool df = ((regs->flags & X86_EFLAGS_DF) == X86_EFLAGS_DF);
  805. unsigned int io_bytes, exit_bytes;
  806. unsigned int ghcb_count, op_count;
  807. unsigned long es_base;
  808. u64 sw_scratch;
  809. /*
  810. * For the string variants with rep prefix the amount of in/out
  811. * operations per #VC exception is limited so that the kernel
  812. * has a chance to take interrupts and re-schedule while the
  813. * instruction is emulated.
  814. */
  815. io_bytes = (exit_info_1 >> 4) & 0x7;
  816. ghcb_count = sizeof(ghcb->shared_buffer) / io_bytes;
  817. op_count = (exit_info_1 & IOIO_REP) ? regs->cx : 1;
  818. exit_info_2 = min(op_count, ghcb_count);
  819. exit_bytes = exit_info_2 * io_bytes;
  820. es_base = insn_get_seg_base(ctxt->regs, INAT_SEG_REG_ES);
  821. /* Read bytes of OUTS into the shared buffer */
  822. if (!(exit_info_1 & IOIO_TYPE_IN)) {
  823. ret = vc_insn_string_read(ctxt,
  824. (void *)(es_base + regs->si),
  825. ghcb->shared_buffer, io_bytes,
  826. exit_info_2, df);
  827. if (ret)
  828. return ret;
  829. }
  830. /*
  831. * Issue an VMGEXIT to the HV to consume the bytes from the
  832. * shared buffer or to have it write them into the shared buffer
  833. * depending on the instruction: OUTS or INS.
  834. */
  835. sw_scratch = __pa(ghcb) + offsetof(struct ghcb, shared_buffer);
  836. ghcb_set_sw_scratch(ghcb, sw_scratch);
  837. ret = sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_IOIO,
  838. exit_info_1, exit_info_2);
  839. if (ret != ES_OK)
  840. return ret;
  841. /* Read bytes from shared buffer into the guest's destination. */
  842. if (exit_info_1 & IOIO_TYPE_IN) {
  843. ret = vc_insn_string_write(ctxt,
  844. (void *)(es_base + regs->di),
  845. ghcb->shared_buffer, io_bytes,
  846. exit_info_2, df);
  847. if (ret)
  848. return ret;
  849. if (df)
  850. regs->di -= exit_bytes;
  851. else
  852. regs->di += exit_bytes;
  853. } else {
  854. if (df)
  855. regs->si -= exit_bytes;
  856. else
  857. regs->si += exit_bytes;
  858. }
  859. if (exit_info_1 & IOIO_REP)
  860. regs->cx -= exit_info_2;
  861. ret = regs->cx ? ES_RETRY : ES_OK;
  862. } else {
  863. /* IN/OUT into/from rAX */
  864. int bits = (exit_info_1 & 0x70) >> 1;
  865. u64 rax = 0;
  866. if (!(exit_info_1 & IOIO_TYPE_IN))
  867. rax = lower_bits(regs->ax, bits);
  868. ghcb_set_rax(ghcb, rax);
  869. ret = sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_IOIO, exit_info_1, 0);
  870. if (ret != ES_OK)
  871. return ret;
  872. if (exit_info_1 & IOIO_TYPE_IN) {
  873. if (!ghcb_rax_is_valid(ghcb))
  874. return ES_VMM_ERROR;
  875. regs->ax = lower_bits(ghcb->save.rax, bits);
  876. }
  877. }
  878. return ret;
  879. }
  880. static int vc_handle_cpuid_snp(struct ghcb *ghcb, struct es_em_ctxt *ctxt)
  881. {
  882. struct pt_regs *regs = ctxt->regs;
  883. struct cpuid_leaf leaf;
  884. int ret;
  885. leaf.fn = regs->ax;
  886. leaf.subfn = regs->cx;
  887. ret = snp_cpuid(ghcb, ctxt, &leaf);
  888. if (!ret) {
  889. regs->ax = leaf.eax;
  890. regs->bx = leaf.ebx;
  891. regs->cx = leaf.ecx;
  892. regs->dx = leaf.edx;
  893. }
  894. return ret;
  895. }
  896. static enum es_result vc_handle_cpuid(struct ghcb *ghcb,
  897. struct es_em_ctxt *ctxt)
  898. {
  899. struct pt_regs *regs = ctxt->regs;
  900. u32 cr4 = native_read_cr4();
  901. enum es_result ret;
  902. int snp_cpuid_ret;
  903. snp_cpuid_ret = vc_handle_cpuid_snp(ghcb, ctxt);
  904. if (!snp_cpuid_ret)
  905. return ES_OK;
  906. if (snp_cpuid_ret != -EOPNOTSUPP)
  907. return ES_VMM_ERROR;
  908. ghcb_set_rax(ghcb, regs->ax);
  909. ghcb_set_rcx(ghcb, regs->cx);
  910. if (cr4 & X86_CR4_OSXSAVE)
  911. /* Safe to read xcr0 */
  912. ghcb_set_xcr0(ghcb, xgetbv(XCR_XFEATURE_ENABLED_MASK));
  913. else
  914. /* xgetbv will cause #GP - use reset value for xcr0 */
  915. ghcb_set_xcr0(ghcb, 1);
  916. ret = sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_CPUID, 0, 0);
  917. if (ret != ES_OK)
  918. return ret;
  919. if (!(ghcb_rax_is_valid(ghcb) &&
  920. ghcb_rbx_is_valid(ghcb) &&
  921. ghcb_rcx_is_valid(ghcb) &&
  922. ghcb_rdx_is_valid(ghcb)))
  923. return ES_VMM_ERROR;
  924. regs->ax = ghcb->save.rax;
  925. regs->bx = ghcb->save.rbx;
  926. regs->cx = ghcb->save.rcx;
  927. regs->dx = ghcb->save.rdx;
  928. return ES_OK;
  929. }
  930. static enum es_result vc_handle_rdtsc(struct ghcb *ghcb,
  931. struct es_em_ctxt *ctxt,
  932. unsigned long exit_code)
  933. {
  934. bool rdtscp = (exit_code == SVM_EXIT_RDTSCP);
  935. enum es_result ret;
  936. ret = sev_es_ghcb_hv_call(ghcb, ctxt, exit_code, 0, 0);
  937. if (ret != ES_OK)
  938. return ret;
  939. if (!(ghcb_rax_is_valid(ghcb) && ghcb_rdx_is_valid(ghcb) &&
  940. (!rdtscp || ghcb_rcx_is_valid(ghcb))))
  941. return ES_VMM_ERROR;
  942. ctxt->regs->ax = ghcb->save.rax;
  943. ctxt->regs->dx = ghcb->save.rdx;
  944. if (rdtscp)
  945. ctxt->regs->cx = ghcb->save.rcx;
  946. return ES_OK;
  947. }
  948. struct cc_setup_data {
  949. struct setup_data header;
  950. u32 cc_blob_address;
  951. };
  952. /*
  953. * Search for a Confidential Computing blob passed in as a setup_data entry
  954. * via the Linux Boot Protocol.
  955. */
  956. static __head
  957. struct cc_blob_sev_info *find_cc_blob_setup_data(struct boot_params *bp)
  958. {
  959. struct cc_setup_data *sd = NULL;
  960. struct setup_data *hdr;
  961. hdr = (struct setup_data *)bp->hdr.setup_data;
  962. while (hdr) {
  963. if (hdr->type == SETUP_CC_BLOB) {
  964. sd = (struct cc_setup_data *)hdr;
  965. return (struct cc_blob_sev_info *)(unsigned long)sd->cc_blob_address;
  966. }
  967. hdr = (struct setup_data *)hdr->next;
  968. }
  969. return NULL;
  970. }
  971. /*
  972. * Initialize the kernel's copy of the SNP CPUID table, and set up the
  973. * pointer that will be used to access it.
  974. *
  975. * Maintaining a direct mapping of the SNP CPUID table used by firmware would
  976. * be possible as an alternative, but the approach is brittle since the
  977. * mapping needs to be updated in sync with all the changes to virtual memory
  978. * layout and related mapping facilities throughout the boot process.
  979. */
  980. static void __head setup_cpuid_table(const struct cc_blob_sev_info *cc_info)
  981. {
  982. const struct snp_cpuid_table *cpuid_table_fw, *cpuid_table;
  983. int i;
  984. if (!cc_info || !cc_info->cpuid_phys || cc_info->cpuid_len < PAGE_SIZE)
  985. sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_CPUID);
  986. cpuid_table_fw = (const struct snp_cpuid_table *)cc_info->cpuid_phys;
  987. if (!cpuid_table_fw->count || cpuid_table_fw->count > SNP_CPUID_COUNT_MAX)
  988. sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_CPUID);
  989. cpuid_table = snp_cpuid_get_table();
  990. memcpy((void *)cpuid_table, cpuid_table_fw, sizeof(*cpuid_table));
  991. /* Initialize CPUID ranges for range-checking. */
  992. for (i = 0; i < cpuid_table->count; i++) {
  993. const struct snp_cpuid_fn *fn = &cpuid_table->fn[i];
  994. if (fn->eax_in == 0x0)
  995. RIP_REL_REF(cpuid_std_range_max) = fn->eax;
  996. else if (fn->eax_in == 0x40000000)
  997. RIP_REL_REF(cpuid_hyp_range_max) = fn->eax;
  998. else if (fn->eax_in == 0x80000000)
  999. RIP_REL_REF(cpuid_ext_range_max) = fn->eax;
  1000. }
  1001. }
  1002. static inline void __pval_terminate(u64 pfn, bool action, unsigned int page_size,
  1003. int ret, u64 svsm_ret)
  1004. {
  1005. WARN(1, "PVALIDATE failure: pfn: 0x%llx, action: %u, size: %u, ret: %d, svsm_ret: 0x%llx\n",
  1006. pfn, action, page_size, ret, svsm_ret);
  1007. sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_PVALIDATE);
  1008. }
  1009. static void svsm_pval_terminate(struct svsm_pvalidate_call *pc, int ret, u64 svsm_ret)
  1010. {
  1011. unsigned int page_size;
  1012. bool action;
  1013. u64 pfn;
  1014. pfn = pc->entry[pc->cur_index].pfn;
  1015. action = pc->entry[pc->cur_index].action;
  1016. page_size = pc->entry[pc->cur_index].page_size;
  1017. __pval_terminate(pfn, action, page_size, ret, svsm_ret);
  1018. }
  1019. static inline void sev_evict_cache(void *va, int npages)
  1020. {
  1021. volatile u8 val __always_unused;
  1022. u8 *bytes = va;
  1023. int page_idx;
  1024. /*
  1025. * For SEV guests, a read from the first/last cache-lines of a 4K page
  1026. * using the guest key is sufficient to cause a flush of all cache-lines
  1027. * associated with that 4K page without incurring all the overhead of a
  1028. * full CLFLUSH sequence.
  1029. */
  1030. for (page_idx = 0; page_idx < npages; page_idx++) {
  1031. val = bytes[page_idx * PAGE_SIZE];
  1032. val = bytes[page_idx * PAGE_SIZE + PAGE_SIZE - 1];
  1033. }
  1034. }
  1035. static void svsm_pval_4k_page(unsigned long paddr, bool validate)
  1036. {
  1037. struct svsm_pvalidate_call *pc;
  1038. struct svsm_call call = {};
  1039. unsigned long flags;
  1040. u64 pc_pa;
  1041. int ret;
  1042. /*
  1043. * This can be called very early in the boot, use native functions in
  1044. * order to avoid paravirt issues.
  1045. */
  1046. flags = native_local_irq_save();
  1047. call.caa = svsm_get_caa();
  1048. pc = (struct svsm_pvalidate_call *)call.caa->svsm_buffer;
  1049. pc_pa = svsm_get_caa_pa() + offsetof(struct svsm_ca, svsm_buffer);
  1050. pc->num_entries = 1;
  1051. pc->cur_index = 0;
  1052. pc->entry[0].page_size = RMP_PG_SIZE_4K;
  1053. pc->entry[0].action = validate;
  1054. pc->entry[0].ignore_cf = 0;
  1055. pc->entry[0].rsvd = 0;
  1056. pc->entry[0].pfn = paddr >> PAGE_SHIFT;
  1057. /* Protocol 0, Call ID 1 */
  1058. call.rax = SVSM_CORE_CALL(SVSM_CORE_PVALIDATE);
  1059. call.rcx = pc_pa;
  1060. ret = svsm_perform_call_protocol(&call);
  1061. if (ret)
  1062. svsm_pval_terminate(pc, ret, call.rax_out);
  1063. native_local_irq_restore(flags);
  1064. }
  1065. static void pvalidate_4k_page(unsigned long vaddr, unsigned long paddr, bool validate)
  1066. {
  1067. int ret;
  1068. /*
  1069. * This can be called very early during boot, so use rIP-relative
  1070. * references as needed.
  1071. */
  1072. if (RIP_REL_REF(snp_vmpl)) {
  1073. svsm_pval_4k_page(paddr, validate);
  1074. } else {
  1075. ret = pvalidate(vaddr, RMP_PG_SIZE_4K, validate);
  1076. if (ret)
  1077. __pval_terminate(PHYS_PFN(paddr), validate, RMP_PG_SIZE_4K, ret, 0);
  1078. }
  1079. /*
  1080. * If validating memory (making it private) and affected by the
  1081. * cache-coherency vulnerability, perform the cache eviction mitigation.
  1082. */
  1083. if (validate && !has_cpuflag(X86_FEATURE_COHERENCY_SFW_NO))
  1084. sev_evict_cache((void *)vaddr, 1);
  1085. }
  1086. static void pval_pages(struct snp_psc_desc *desc)
  1087. {
  1088. struct psc_entry *e;
  1089. unsigned long vaddr;
  1090. unsigned int size;
  1091. unsigned int i;
  1092. bool validate;
  1093. u64 pfn;
  1094. int rc;
  1095. for (i = 0; i <= desc->hdr.end_entry; i++) {
  1096. e = &desc->entries[i];
  1097. pfn = e->gfn;
  1098. vaddr = (unsigned long)pfn_to_kaddr(pfn);
  1099. size = e->pagesize ? RMP_PG_SIZE_2M : RMP_PG_SIZE_4K;
  1100. validate = e->operation == SNP_PAGE_STATE_PRIVATE;
  1101. rc = pvalidate(vaddr, size, validate);
  1102. if (!rc)
  1103. continue;
  1104. if (rc == PVALIDATE_FAIL_SIZEMISMATCH && size == RMP_PG_SIZE_2M) {
  1105. unsigned long vaddr_end = vaddr + PMD_SIZE;
  1106. for (; vaddr < vaddr_end; vaddr += PAGE_SIZE, pfn++) {
  1107. rc = pvalidate(vaddr, RMP_PG_SIZE_4K, validate);
  1108. if (rc)
  1109. __pval_terminate(pfn, validate, RMP_PG_SIZE_4K, rc, 0);
  1110. }
  1111. } else {
  1112. __pval_terminate(pfn, validate, size, rc, 0);
  1113. }
  1114. }
  1115. }
  1116. static u64 svsm_build_ca_from_pfn_range(u64 pfn, u64 pfn_end, bool action,
  1117. struct svsm_pvalidate_call *pc)
  1118. {
  1119. struct svsm_pvalidate_entry *pe;
  1120. /* Nothing in the CA yet */
  1121. pc->num_entries = 0;
  1122. pc->cur_index = 0;
  1123. pe = &pc->entry[0];
  1124. while (pfn < pfn_end) {
  1125. pe->page_size = RMP_PG_SIZE_4K;
  1126. pe->action = action;
  1127. pe->ignore_cf = 0;
  1128. pe->rsvd = 0;
  1129. pe->pfn = pfn;
  1130. pe++;
  1131. pfn++;
  1132. pc->num_entries++;
  1133. if (pc->num_entries == SVSM_PVALIDATE_MAX_COUNT)
  1134. break;
  1135. }
  1136. return pfn;
  1137. }
  1138. static int svsm_build_ca_from_psc_desc(struct snp_psc_desc *desc, unsigned int desc_entry,
  1139. struct svsm_pvalidate_call *pc)
  1140. {
  1141. struct svsm_pvalidate_entry *pe;
  1142. struct psc_entry *e;
  1143. /* Nothing in the CA yet */
  1144. pc->num_entries = 0;
  1145. pc->cur_index = 0;
  1146. pe = &pc->entry[0];
  1147. e = &desc->entries[desc_entry];
  1148. while (desc_entry <= desc->hdr.end_entry) {
  1149. pe->page_size = e->pagesize ? RMP_PG_SIZE_2M : RMP_PG_SIZE_4K;
  1150. pe->action = e->operation == SNP_PAGE_STATE_PRIVATE;
  1151. pe->ignore_cf = 0;
  1152. pe->rsvd = 0;
  1153. pe->pfn = e->gfn;
  1154. pe++;
  1155. e++;
  1156. desc_entry++;
  1157. pc->num_entries++;
  1158. if (pc->num_entries == SVSM_PVALIDATE_MAX_COUNT)
  1159. break;
  1160. }
  1161. return desc_entry;
  1162. }
  1163. static void svsm_pval_pages(struct snp_psc_desc *desc)
  1164. {
  1165. struct svsm_pvalidate_entry pv_4k[VMGEXIT_PSC_MAX_ENTRY];
  1166. unsigned int i, pv_4k_count = 0;
  1167. struct svsm_pvalidate_call *pc;
  1168. struct svsm_call call = {};
  1169. unsigned long flags;
  1170. bool action;
  1171. u64 pc_pa;
  1172. int ret;
  1173. /*
  1174. * This can be called very early in the boot, use native functions in
  1175. * order to avoid paravirt issues.
  1176. */
  1177. flags = native_local_irq_save();
  1178. /*
  1179. * The SVSM calling area (CA) can support processing 510 entries at a
  1180. * time. Loop through the Page State Change descriptor until the CA is
  1181. * full or the last entry in the descriptor is reached, at which time
  1182. * the SVSM is invoked. This repeats until all entries in the descriptor
  1183. * are processed.
  1184. */
  1185. call.caa = svsm_get_caa();
  1186. pc = (struct svsm_pvalidate_call *)call.caa->svsm_buffer;
  1187. pc_pa = svsm_get_caa_pa() + offsetof(struct svsm_ca, svsm_buffer);
  1188. /* Protocol 0, Call ID 1 */
  1189. call.rax = SVSM_CORE_CALL(SVSM_CORE_PVALIDATE);
  1190. call.rcx = pc_pa;
  1191. for (i = 0; i <= desc->hdr.end_entry;) {
  1192. i = svsm_build_ca_from_psc_desc(desc, i, pc);
  1193. do {
  1194. ret = svsm_perform_call_protocol(&call);
  1195. if (!ret)
  1196. continue;
  1197. /*
  1198. * Check if the entry failed because of an RMP mismatch (a
  1199. * PVALIDATE at 2M was requested, but the page is mapped in
  1200. * the RMP as 4K).
  1201. */
  1202. if (call.rax_out == SVSM_PVALIDATE_FAIL_SIZEMISMATCH &&
  1203. pc->entry[pc->cur_index].page_size == RMP_PG_SIZE_2M) {
  1204. /* Save this entry for post-processing at 4K */
  1205. pv_4k[pv_4k_count++] = pc->entry[pc->cur_index];
  1206. /* Skip to the next one unless at the end of the list */
  1207. pc->cur_index++;
  1208. if (pc->cur_index < pc->num_entries)
  1209. ret = -EAGAIN;
  1210. else
  1211. ret = 0;
  1212. }
  1213. } while (ret == -EAGAIN);
  1214. if (ret)
  1215. svsm_pval_terminate(pc, ret, call.rax_out);
  1216. }
  1217. /* Process any entries that failed to be validated at 2M and validate them at 4K */
  1218. for (i = 0; i < pv_4k_count; i++) {
  1219. u64 pfn, pfn_end;
  1220. action = pv_4k[i].action;
  1221. pfn = pv_4k[i].pfn;
  1222. pfn_end = pfn + 512;
  1223. while (pfn < pfn_end) {
  1224. pfn = svsm_build_ca_from_pfn_range(pfn, pfn_end, action, pc);
  1225. ret = svsm_perform_call_protocol(&call);
  1226. if (ret)
  1227. svsm_pval_terminate(pc, ret, call.rax_out);
  1228. }
  1229. }
  1230. native_local_irq_restore(flags);
  1231. }
  1232. static void pvalidate_pages(struct snp_psc_desc *desc)
  1233. {
  1234. struct psc_entry *e;
  1235. unsigned int i;
  1236. if (snp_vmpl)
  1237. svsm_pval_pages(desc);
  1238. else
  1239. pval_pages(desc);
  1240. /*
  1241. * If not affected by the cache-coherency vulnerability there is no need
  1242. * to perform the cache eviction mitigation.
  1243. */
  1244. if (cpu_feature_enabled(X86_FEATURE_COHERENCY_SFW_NO))
  1245. return;
  1246. for (i = 0; i <= desc->hdr.end_entry; i++) {
  1247. e = &desc->entries[i];
  1248. /*
  1249. * If validating memory (making it private) perform the cache
  1250. * eviction mitigation.
  1251. */
  1252. if (e->operation == SNP_PAGE_STATE_PRIVATE)
  1253. sev_evict_cache(pfn_to_kaddr(e->gfn), e->pagesize ? 512 : 1);
  1254. }
  1255. }
  1256. static int vmgexit_psc(struct ghcb *ghcb, struct snp_psc_desc *desc)
  1257. {
  1258. int cur_entry, end_entry, ret = 0;
  1259. struct snp_psc_desc *data;
  1260. struct es_em_ctxt ctxt;
  1261. vc_ghcb_invalidate(ghcb);
  1262. /* Copy the input desc into GHCB shared buffer */
  1263. data = (struct snp_psc_desc *)ghcb->shared_buffer;
  1264. memcpy(ghcb->shared_buffer, desc, min_t(int, GHCB_SHARED_BUF_SIZE, sizeof(*desc)));
  1265. /*
  1266. * As per the GHCB specification, the hypervisor can resume the guest
  1267. * before processing all the entries. Check whether all the entries
  1268. * are processed. If not, then keep retrying. Note, the hypervisor
  1269. * will update the data memory directly to indicate the status, so
  1270. * reference the data->hdr everywhere.
  1271. *
  1272. * The strategy here is to wait for the hypervisor to change the page
  1273. * state in the RMP table before guest accesses the memory pages. If the
  1274. * page state change was not successful, then later memory access will
  1275. * result in a crash.
  1276. */
  1277. cur_entry = data->hdr.cur_entry;
  1278. end_entry = data->hdr.end_entry;
  1279. while (data->hdr.cur_entry <= data->hdr.end_entry) {
  1280. ghcb_set_sw_scratch(ghcb, (u64)__pa(data));
  1281. /* This will advance the shared buffer data points to. */
  1282. ret = sev_es_ghcb_hv_call(ghcb, &ctxt, SVM_VMGEXIT_PSC, 0, 0);
  1283. /*
  1284. * Page State Change VMGEXIT can pass error code through
  1285. * exit_info_2.
  1286. */
  1287. if (WARN(ret || ghcb->save.sw_exit_info_2,
  1288. "SNP: PSC failed ret=%d exit_info_2=%llx\n",
  1289. ret, ghcb->save.sw_exit_info_2)) {
  1290. ret = 1;
  1291. goto out;
  1292. }
  1293. /* Verify that reserved bit is not set */
  1294. if (WARN(data->hdr.reserved, "Reserved bit is set in the PSC header\n")) {
  1295. ret = 1;
  1296. goto out;
  1297. }
  1298. /*
  1299. * Sanity check that entry processing is not going backwards.
  1300. * This will happen only if hypervisor is tricking us.
  1301. */
  1302. if (WARN(data->hdr.end_entry > end_entry || cur_entry > data->hdr.cur_entry,
  1303. "SNP: PSC processing going backward, end_entry %d (got %d) cur_entry %d (got %d)\n",
  1304. end_entry, data->hdr.end_entry, cur_entry, data->hdr.cur_entry)) {
  1305. ret = 1;
  1306. goto out;
  1307. }
  1308. }
  1309. out:
  1310. return ret;
  1311. }
  1312. static enum es_result vc_check_opcode_bytes(struct es_em_ctxt *ctxt,
  1313. unsigned long exit_code)
  1314. {
  1315. unsigned int opcode = (unsigned int)ctxt->insn.opcode.value;
  1316. u8 modrm = ctxt->insn.modrm.value;
  1317. switch (exit_code) {
  1318. case SVM_EXIT_IOIO:
  1319. case SVM_EXIT_NPF:
  1320. /* handled separately */
  1321. return ES_OK;
  1322. case SVM_EXIT_CPUID:
  1323. if (opcode == 0xa20f)
  1324. return ES_OK;
  1325. break;
  1326. case SVM_EXIT_INVD:
  1327. if (opcode == 0x080f)
  1328. return ES_OK;
  1329. break;
  1330. case SVM_EXIT_MONITOR:
  1331. /* MONITOR and MONITORX instructions generate the same error code */
  1332. if (opcode == 0x010f && (modrm == 0xc8 || modrm == 0xfa))
  1333. return ES_OK;
  1334. break;
  1335. case SVM_EXIT_MWAIT:
  1336. /* MWAIT and MWAITX instructions generate the same error code */
  1337. if (opcode == 0x010f && (modrm == 0xc9 || modrm == 0xfb))
  1338. return ES_OK;
  1339. break;
  1340. case SVM_EXIT_MSR:
  1341. /* RDMSR */
  1342. if (opcode == 0x320f ||
  1343. /* WRMSR */
  1344. opcode == 0x300f)
  1345. return ES_OK;
  1346. break;
  1347. case SVM_EXIT_RDPMC:
  1348. if (opcode == 0x330f)
  1349. return ES_OK;
  1350. break;
  1351. case SVM_EXIT_RDTSC:
  1352. if (opcode == 0x310f)
  1353. return ES_OK;
  1354. break;
  1355. case SVM_EXIT_RDTSCP:
  1356. if (opcode == 0x010f && modrm == 0xf9)
  1357. return ES_OK;
  1358. break;
  1359. case SVM_EXIT_READ_DR7:
  1360. if (opcode == 0x210f &&
  1361. X86_MODRM_REG(ctxt->insn.modrm.value) == 7)
  1362. return ES_OK;
  1363. break;
  1364. case SVM_EXIT_VMMCALL:
  1365. if (opcode == 0x010f && modrm == 0xd9)
  1366. return ES_OK;
  1367. break;
  1368. case SVM_EXIT_WRITE_DR7:
  1369. if (opcode == 0x230f &&
  1370. X86_MODRM_REG(ctxt->insn.modrm.value) == 7)
  1371. return ES_OK;
  1372. break;
  1373. case SVM_EXIT_WBINVD:
  1374. if (opcode == 0x90f)
  1375. return ES_OK;
  1376. break;
  1377. default:
  1378. break;
  1379. }
  1380. sev_printk(KERN_ERR "Wrong/unhandled opcode bytes: 0x%x, exit_code: 0x%lx, rIP: 0x%lx\n",
  1381. opcode, exit_code, ctxt->regs->ip);
  1382. return ES_UNSUPPORTED;
  1383. }
  1384. /*
  1385. * Maintain the GPA of the SVSM Calling Area (CA) in order to utilize the SVSM
  1386. * services needed when not running in VMPL0.
  1387. */
  1388. static bool __head svsm_setup_ca(const struct cc_blob_sev_info *cc_info)
  1389. {
  1390. struct snp_secrets_page *secrets_page;
  1391. struct snp_cpuid_table *cpuid_table;
  1392. unsigned int i;
  1393. u64 caa;
  1394. BUILD_BUG_ON(sizeof(*secrets_page) != PAGE_SIZE);
  1395. /*
  1396. * Check if running at VMPL0.
  1397. *
  1398. * Use RMPADJUST (see the rmpadjust() function for a description of what
  1399. * the instruction does) to update the VMPL1 permissions of a page. If
  1400. * the guest is running at VMPL0, this will succeed and implies there is
  1401. * no SVSM. If the guest is running at any other VMPL, this will fail.
  1402. * Linux SNP guests only ever run at a single VMPL level so permission mask
  1403. * changes of a lesser-privileged VMPL are a don't-care.
  1404. *
  1405. * Use a rip-relative reference to obtain the proper address, since this
  1406. * routine is running identity mapped when called, both by the decompressor
  1407. * code and the early kernel code.
  1408. */
  1409. if (!rmpadjust((unsigned long)&RIP_REL_REF(boot_ghcb_page), RMP_PG_SIZE_4K, 1))
  1410. return false;
  1411. /*
  1412. * Not running at VMPL0, ensure everything has been properly supplied
  1413. * for running under an SVSM.
  1414. */
  1415. if (!cc_info || !cc_info->secrets_phys || cc_info->secrets_len != PAGE_SIZE)
  1416. sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_SECRETS_PAGE);
  1417. secrets_page = (struct snp_secrets_page *)cc_info->secrets_phys;
  1418. if (!secrets_page->svsm_size)
  1419. sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_NO_SVSM);
  1420. if (!secrets_page->svsm_guest_vmpl)
  1421. sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_SVSM_VMPL0);
  1422. RIP_REL_REF(snp_vmpl) = secrets_page->svsm_guest_vmpl;
  1423. caa = secrets_page->svsm_caa;
  1424. /*
  1425. * An open-coded PAGE_ALIGNED() in order to avoid including
  1426. * kernel-proper headers into the decompressor.
  1427. */
  1428. if (caa & (PAGE_SIZE - 1))
  1429. sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_SVSM_CAA);
  1430. /*
  1431. * The CA is identity mapped when this routine is called, both by the
  1432. * decompressor code and the early kernel code.
  1433. */
  1434. RIP_REL_REF(boot_svsm_caa) = (struct svsm_ca *)caa;
  1435. RIP_REL_REF(boot_svsm_caa_pa) = caa;
  1436. /* Advertise the SVSM presence via CPUID. */
  1437. cpuid_table = (struct snp_cpuid_table *)snp_cpuid_get_table();
  1438. for (i = 0; i < cpuid_table->count; i++) {
  1439. struct snp_cpuid_fn *fn = &cpuid_table->fn[i];
  1440. if (fn->eax_in == 0x8000001f)
  1441. fn->eax |= BIT(28);
  1442. }
  1443. return true;
  1444. }