sev-dev.c 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * AMD Secure Encrypted Virtualization (SEV) interface
  4. *
  5. * Copyright (C) 2016,2019 Advanced Micro Devices, Inc.
  6. *
  7. * Author: Brijesh Singh <brijesh.singh@amd.com>
  8. */
  9. #include <linux/bitfield.h>
  10. #include <linux/module.h>
  11. #include <linux/kernel.h>
  12. #include <linux/kthread.h>
  13. #include <linux/sched.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/spinlock_types.h>
  17. #include <linux/types.h>
  18. #include <linux/mutex.h>
  19. #include <linux/delay.h>
  20. #include <linux/hw_random.h>
  21. #include <linux/ccp.h>
  22. #include <linux/firmware.h>
  23. #include <linux/panic_notifier.h>
  24. #include <linux/gfp.h>
  25. #include <linux/cpufeature.h>
  26. #include <linux/fs.h>
  27. #include <linux/fs_struct.h>
  28. #include <linux/psp.h>
  29. #include <linux/amd-iommu.h>
  30. #include <linux/crash_dump.h>
  31. #include <asm/smp.h>
  32. #include <asm/cacheflush.h>
  33. #include <asm/e820/types.h>
  34. #include <asm/sev.h>
  35. #include <asm/msr.h>
  36. #include "psp-dev.h"
  37. #include "sev-dev.h"
  38. #define DEVICE_NAME "sev"
  39. #define SEV_FW_FILE "amd/sev.fw"
  40. #define SEV_FW_NAME_SIZE 64
  41. /* Minimum firmware version required for the SEV-SNP support */
  42. #define SNP_MIN_API_MAJOR 1
  43. #define SNP_MIN_API_MINOR 51
  44. /*
  45. * Maximum number of firmware-writable buffers that might be specified
  46. * in the parameters of a legacy SEV command buffer.
  47. */
  48. #define CMD_BUF_FW_WRITABLE_MAX 2
  49. /* Leave room in the descriptor array for an end-of-list indicator. */
  50. #define CMD_BUF_DESC_MAX (CMD_BUF_FW_WRITABLE_MAX + 1)
  51. static DEFINE_MUTEX(sev_cmd_mutex);
  52. static struct sev_misc_dev *misc_dev;
  53. static int psp_cmd_timeout = 100;
  54. module_param(psp_cmd_timeout, int, 0644);
  55. MODULE_PARM_DESC(psp_cmd_timeout, " default timeout value, in seconds, for PSP commands");
  56. static int psp_probe_timeout = 5;
  57. module_param(psp_probe_timeout, int, 0644);
  58. MODULE_PARM_DESC(psp_probe_timeout, " default timeout value, in seconds, during PSP device probe");
  59. static char *init_ex_path;
  60. module_param(init_ex_path, charp, 0444);
  61. MODULE_PARM_DESC(init_ex_path, " Path for INIT_EX data; if set try INIT_EX");
  62. static bool psp_init_on_probe = true;
  63. module_param(psp_init_on_probe, bool, 0444);
  64. MODULE_PARM_DESC(psp_init_on_probe, " if true, the PSP will be initialized on module init. Else the PSP will be initialized on the first command requiring it");
  65. #if IS_ENABLED(CONFIG_PCI_TSM)
  66. static bool sev_tio_enabled = true;
  67. module_param_named(tio, sev_tio_enabled, bool, 0444);
  68. MODULE_PARM_DESC(tio, "Enables TIO in SNP_INIT_EX");
  69. #else
  70. static const bool sev_tio_enabled = false;
  71. #endif
  72. MODULE_FIRMWARE("amd/amd_sev_fam17h_model0xh.sbin"); /* 1st gen EPYC */
  73. MODULE_FIRMWARE("amd/amd_sev_fam17h_model3xh.sbin"); /* 2nd gen EPYC */
  74. MODULE_FIRMWARE("amd/amd_sev_fam19h_model0xh.sbin"); /* 3rd gen EPYC */
  75. MODULE_FIRMWARE("amd/amd_sev_fam19h_model1xh.sbin"); /* 4th gen EPYC */
  76. static bool psp_dead;
  77. static int psp_timeout;
  78. enum snp_hv_fixed_pages_state {
  79. ALLOCATED,
  80. HV_FIXED,
  81. };
  82. struct snp_hv_fixed_pages_entry {
  83. struct list_head list;
  84. struct page *page;
  85. unsigned int order;
  86. bool free;
  87. enum snp_hv_fixed_pages_state page_state;
  88. };
  89. static LIST_HEAD(snp_hv_fixed_pages);
  90. /* Trusted Memory Region (TMR):
  91. * The TMR is a 1MB area that must be 1MB aligned. Use the page allocator
  92. * to allocate the memory, which will return aligned memory for the specified
  93. * allocation order.
  94. *
  95. * When SEV-SNP is enabled the TMR needs to be 2MB aligned and 2MB sized.
  96. */
  97. #define SEV_TMR_SIZE (1024 * 1024)
  98. #define SNP_TMR_SIZE (2 * 1024 * 1024)
  99. static void *sev_es_tmr;
  100. static size_t sev_es_tmr_size = SEV_TMR_SIZE;
  101. /* INIT_EX NV Storage:
  102. * The NV Storage is a 32Kb area and must be 4Kb page aligned. Use the page
  103. * allocator to allocate the memory, which will return aligned memory for the
  104. * specified allocation order.
  105. */
  106. #define NV_LENGTH (32 * 1024)
  107. static void *sev_init_ex_buffer;
  108. static void __sev_firmware_shutdown(struct sev_device *sev, bool panic);
  109. static int snp_shutdown_on_panic(struct notifier_block *nb,
  110. unsigned long reason, void *arg);
  111. static struct notifier_block snp_panic_notifier = {
  112. .notifier_call = snp_shutdown_on_panic,
  113. };
  114. static inline bool sev_version_greater_or_equal(u8 maj, u8 min)
  115. {
  116. struct sev_device *sev = psp_master->sev_data;
  117. if (sev->api_major > maj)
  118. return true;
  119. if (sev->api_major == maj && sev->api_minor >= min)
  120. return true;
  121. return false;
  122. }
  123. static void sev_irq_handler(int irq, void *data, unsigned int status)
  124. {
  125. struct sev_device *sev = data;
  126. int reg;
  127. /* Check if it is command completion: */
  128. if (!(status & SEV_CMD_COMPLETE))
  129. return;
  130. /* Check if it is SEV command completion: */
  131. reg = ioread32(sev->io_regs + sev->vdata->cmdresp_reg);
  132. if (FIELD_GET(PSP_CMDRESP_RESP, reg)) {
  133. sev->int_rcvd = 1;
  134. wake_up(&sev->int_queue);
  135. }
  136. }
  137. static int sev_wait_cmd_ioc(struct sev_device *sev,
  138. unsigned int *reg, unsigned int timeout)
  139. {
  140. int ret;
  141. /*
  142. * If invoked during panic handling, local interrupts are disabled,
  143. * so the PSP command completion interrupt can't be used. Poll for
  144. * PSP command completion instead.
  145. */
  146. if (irqs_disabled()) {
  147. unsigned long timeout_usecs = (timeout * USEC_PER_SEC) / 10;
  148. /* Poll for SEV command completion: */
  149. while (timeout_usecs--) {
  150. *reg = ioread32(sev->io_regs + sev->vdata->cmdresp_reg);
  151. if (*reg & PSP_CMDRESP_RESP)
  152. return 0;
  153. udelay(10);
  154. }
  155. return -ETIMEDOUT;
  156. }
  157. ret = wait_event_timeout(sev->int_queue,
  158. sev->int_rcvd, timeout * HZ);
  159. if (!ret)
  160. return -ETIMEDOUT;
  161. *reg = ioread32(sev->io_regs + sev->vdata->cmdresp_reg);
  162. return 0;
  163. }
  164. static int sev_cmd_buffer_len(int cmd)
  165. {
  166. switch (cmd) {
  167. case SEV_CMD_INIT: return sizeof(struct sev_data_init);
  168. case SEV_CMD_INIT_EX: return sizeof(struct sev_data_init_ex);
  169. case SEV_CMD_SNP_SHUTDOWN_EX: return sizeof(struct sev_data_snp_shutdown_ex);
  170. case SEV_CMD_SNP_INIT_EX: return sizeof(struct sev_data_snp_init_ex);
  171. case SEV_CMD_PLATFORM_STATUS: return sizeof(struct sev_user_data_status);
  172. case SEV_CMD_PEK_CSR: return sizeof(struct sev_data_pek_csr);
  173. case SEV_CMD_PEK_CERT_IMPORT: return sizeof(struct sev_data_pek_cert_import);
  174. case SEV_CMD_PDH_CERT_EXPORT: return sizeof(struct sev_data_pdh_cert_export);
  175. case SEV_CMD_LAUNCH_START: return sizeof(struct sev_data_launch_start);
  176. case SEV_CMD_LAUNCH_UPDATE_DATA: return sizeof(struct sev_data_launch_update_data);
  177. case SEV_CMD_LAUNCH_UPDATE_VMSA: return sizeof(struct sev_data_launch_update_vmsa);
  178. case SEV_CMD_LAUNCH_FINISH: return sizeof(struct sev_data_launch_finish);
  179. case SEV_CMD_LAUNCH_MEASURE: return sizeof(struct sev_data_launch_measure);
  180. case SEV_CMD_ACTIVATE: return sizeof(struct sev_data_activate);
  181. case SEV_CMD_DEACTIVATE: return sizeof(struct sev_data_deactivate);
  182. case SEV_CMD_DECOMMISSION: return sizeof(struct sev_data_decommission);
  183. case SEV_CMD_GUEST_STATUS: return sizeof(struct sev_data_guest_status);
  184. case SEV_CMD_DBG_DECRYPT: return sizeof(struct sev_data_dbg);
  185. case SEV_CMD_DBG_ENCRYPT: return sizeof(struct sev_data_dbg);
  186. case SEV_CMD_SEND_START: return sizeof(struct sev_data_send_start);
  187. case SEV_CMD_SEND_UPDATE_DATA: return sizeof(struct sev_data_send_update_data);
  188. case SEV_CMD_SEND_UPDATE_VMSA: return sizeof(struct sev_data_send_update_vmsa);
  189. case SEV_CMD_SEND_FINISH: return sizeof(struct sev_data_send_finish);
  190. case SEV_CMD_RECEIVE_START: return sizeof(struct sev_data_receive_start);
  191. case SEV_CMD_RECEIVE_FINISH: return sizeof(struct sev_data_receive_finish);
  192. case SEV_CMD_RECEIVE_UPDATE_DATA: return sizeof(struct sev_data_receive_update_data);
  193. case SEV_CMD_RECEIVE_UPDATE_VMSA: return sizeof(struct sev_data_receive_update_vmsa);
  194. case SEV_CMD_LAUNCH_UPDATE_SECRET: return sizeof(struct sev_data_launch_secret);
  195. case SEV_CMD_DOWNLOAD_FIRMWARE: return sizeof(struct sev_data_download_firmware);
  196. case SEV_CMD_GET_ID: return sizeof(struct sev_data_get_id);
  197. case SEV_CMD_ATTESTATION_REPORT: return sizeof(struct sev_data_attestation_report);
  198. case SEV_CMD_SEND_CANCEL: return sizeof(struct sev_data_send_cancel);
  199. case SEV_CMD_SNP_GCTX_CREATE: return sizeof(struct sev_data_snp_addr);
  200. case SEV_CMD_SNP_LAUNCH_START: return sizeof(struct sev_data_snp_launch_start);
  201. case SEV_CMD_SNP_LAUNCH_UPDATE: return sizeof(struct sev_data_snp_launch_update);
  202. case SEV_CMD_SNP_ACTIVATE: return sizeof(struct sev_data_snp_activate);
  203. case SEV_CMD_SNP_DECOMMISSION: return sizeof(struct sev_data_snp_addr);
  204. case SEV_CMD_SNP_PAGE_RECLAIM: return sizeof(struct sev_data_snp_page_reclaim);
  205. case SEV_CMD_SNP_GUEST_STATUS: return sizeof(struct sev_data_snp_guest_status);
  206. case SEV_CMD_SNP_LAUNCH_FINISH: return sizeof(struct sev_data_snp_launch_finish);
  207. case SEV_CMD_SNP_DBG_DECRYPT: return sizeof(struct sev_data_snp_dbg);
  208. case SEV_CMD_SNP_DBG_ENCRYPT: return sizeof(struct sev_data_snp_dbg);
  209. case SEV_CMD_SNP_PAGE_UNSMASH: return sizeof(struct sev_data_snp_page_unsmash);
  210. case SEV_CMD_SNP_PLATFORM_STATUS: return sizeof(struct sev_data_snp_addr);
  211. case SEV_CMD_SNP_GUEST_REQUEST: return sizeof(struct sev_data_snp_guest_request);
  212. case SEV_CMD_SNP_CONFIG: return sizeof(struct sev_user_data_snp_config);
  213. case SEV_CMD_SNP_COMMIT: return sizeof(struct sev_data_snp_commit);
  214. case SEV_CMD_SNP_FEATURE_INFO: return sizeof(struct sev_data_snp_feature_info);
  215. case SEV_CMD_SNP_VLEK_LOAD: return sizeof(struct sev_user_data_snp_vlek_load);
  216. default: return sev_tio_cmd_buffer_len(cmd);
  217. }
  218. return 0;
  219. }
  220. static struct file *open_file_as_root(const char *filename, int flags, umode_t mode)
  221. {
  222. struct path root __free(path_put) = {};
  223. task_lock(&init_task);
  224. get_fs_root(init_task.fs, &root);
  225. task_unlock(&init_task);
  226. CLASS(prepare_creds, cred)();
  227. if (!cred)
  228. return ERR_PTR(-ENOMEM);
  229. cred->fsuid = GLOBAL_ROOT_UID;
  230. scoped_with_creds(cred)
  231. return file_open_root(&root, filename, flags, mode);
  232. }
  233. static int sev_read_init_ex_file(void)
  234. {
  235. struct sev_device *sev = psp_master->sev_data;
  236. struct file *fp;
  237. ssize_t nread;
  238. lockdep_assert_held(&sev_cmd_mutex);
  239. if (!sev_init_ex_buffer)
  240. return -EOPNOTSUPP;
  241. fp = open_file_as_root(init_ex_path, O_RDONLY, 0);
  242. if (IS_ERR(fp)) {
  243. int ret = PTR_ERR(fp);
  244. if (ret == -ENOENT) {
  245. dev_info(sev->dev,
  246. "SEV: %s does not exist and will be created later.\n",
  247. init_ex_path);
  248. ret = 0;
  249. } else {
  250. dev_err(sev->dev,
  251. "SEV: could not open %s for read, error %d\n",
  252. init_ex_path, ret);
  253. }
  254. return ret;
  255. }
  256. nread = kernel_read(fp, sev_init_ex_buffer, NV_LENGTH, NULL);
  257. if (nread != NV_LENGTH) {
  258. dev_info(sev->dev,
  259. "SEV: could not read %u bytes to non volatile memory area, ret %ld\n",
  260. NV_LENGTH, nread);
  261. }
  262. dev_dbg(sev->dev, "SEV: read %ld bytes from NV file\n", nread);
  263. filp_close(fp, NULL);
  264. return 0;
  265. }
  266. static int sev_write_init_ex_file(void)
  267. {
  268. struct sev_device *sev = psp_master->sev_data;
  269. struct file *fp;
  270. loff_t offset = 0;
  271. ssize_t nwrite;
  272. lockdep_assert_held(&sev_cmd_mutex);
  273. if (!sev_init_ex_buffer)
  274. return 0;
  275. fp = open_file_as_root(init_ex_path, O_CREAT | O_WRONLY, 0600);
  276. if (IS_ERR(fp)) {
  277. int ret = PTR_ERR(fp);
  278. dev_err(sev->dev,
  279. "SEV: could not open file for write, error %d\n",
  280. ret);
  281. return ret;
  282. }
  283. nwrite = kernel_write(fp, sev_init_ex_buffer, NV_LENGTH, &offset);
  284. vfs_fsync(fp, 0);
  285. filp_close(fp, NULL);
  286. if (nwrite != NV_LENGTH) {
  287. dev_err(sev->dev,
  288. "SEV: failed to write %u bytes to non volatile memory area, ret %ld\n",
  289. NV_LENGTH, nwrite);
  290. return -EIO;
  291. }
  292. dev_dbg(sev->dev, "SEV: write successful to NV file\n");
  293. return 0;
  294. }
  295. static int sev_write_init_ex_file_if_required(int cmd_id)
  296. {
  297. lockdep_assert_held(&sev_cmd_mutex);
  298. if (!sev_init_ex_buffer)
  299. return 0;
  300. /*
  301. * Only a few platform commands modify the SPI/NV area, but none of the
  302. * non-platform commands do. Only INIT(_EX), PLATFORM_RESET, PEK_GEN,
  303. * PEK_CERT_IMPORT, and PDH_GEN do.
  304. */
  305. switch (cmd_id) {
  306. case SEV_CMD_FACTORY_RESET:
  307. case SEV_CMD_INIT_EX:
  308. case SEV_CMD_PDH_GEN:
  309. case SEV_CMD_PEK_CERT_IMPORT:
  310. case SEV_CMD_PEK_GEN:
  311. break;
  312. default:
  313. return 0;
  314. }
  315. return sev_write_init_ex_file();
  316. }
  317. int snp_reclaim_pages(unsigned long paddr, unsigned int npages, bool locked)
  318. {
  319. int ret, err, i;
  320. paddr = __sme_clr(ALIGN_DOWN(paddr, PAGE_SIZE));
  321. for (i = 0; i < npages; i++, paddr += PAGE_SIZE) {
  322. struct sev_data_snp_page_reclaim data = {0};
  323. data.paddr = paddr;
  324. if (locked)
  325. ret = __sev_do_cmd_locked(SEV_CMD_SNP_PAGE_RECLAIM, &data, &err);
  326. else
  327. ret = sev_do_cmd(SEV_CMD_SNP_PAGE_RECLAIM, &data, &err);
  328. if (ret)
  329. goto cleanup;
  330. ret = rmp_make_shared(__phys_to_pfn(paddr), PG_LEVEL_4K);
  331. if (ret)
  332. goto cleanup;
  333. }
  334. return 0;
  335. cleanup:
  336. /*
  337. * If there was a failure reclaiming the page then it is no longer safe
  338. * to release it back to the system; leak it instead.
  339. */
  340. snp_leak_pages(__phys_to_pfn(paddr), npages - i);
  341. return ret;
  342. }
  343. EXPORT_SYMBOL_GPL(snp_reclaim_pages);
  344. static int rmp_mark_pages_firmware(unsigned long paddr, unsigned int npages, bool locked)
  345. {
  346. unsigned long pfn = __sme_clr(paddr) >> PAGE_SHIFT;
  347. int rc, i;
  348. for (i = 0; i < npages; i++, pfn++) {
  349. rc = rmp_make_private(pfn, 0, PG_LEVEL_4K, 0, true);
  350. if (rc)
  351. goto cleanup;
  352. }
  353. return 0;
  354. cleanup:
  355. /*
  356. * Try unrolling the firmware state changes by
  357. * reclaiming the pages which were already changed to the
  358. * firmware state.
  359. */
  360. snp_reclaim_pages(paddr, i, locked);
  361. return rc;
  362. }
  363. static struct page *__snp_alloc_firmware_pages(gfp_t gfp_mask, int order, bool locked)
  364. {
  365. unsigned long npages = 1ul << order, paddr;
  366. struct sev_device *sev;
  367. struct page *page;
  368. if (!psp_master || !psp_master->sev_data)
  369. return NULL;
  370. page = alloc_pages(gfp_mask, order);
  371. if (!page)
  372. return NULL;
  373. /* If SEV-SNP is initialized then add the page in RMP table. */
  374. sev = psp_master->sev_data;
  375. if (!sev->snp_initialized)
  376. return page;
  377. paddr = __pa((unsigned long)page_address(page));
  378. if (rmp_mark_pages_firmware(paddr, npages, locked))
  379. return NULL;
  380. return page;
  381. }
  382. void *snp_alloc_firmware_page(gfp_t gfp_mask)
  383. {
  384. struct page *page;
  385. page = __snp_alloc_firmware_pages(gfp_mask, 0, false);
  386. return page ? page_address(page) : NULL;
  387. }
  388. EXPORT_SYMBOL_GPL(snp_alloc_firmware_page);
  389. static void __snp_free_firmware_pages(struct page *page, int order, bool locked)
  390. {
  391. struct sev_device *sev = psp_master->sev_data;
  392. unsigned long paddr, npages = 1ul << order;
  393. if (!page)
  394. return;
  395. paddr = __pa((unsigned long)page_address(page));
  396. if (sev->snp_initialized &&
  397. snp_reclaim_pages(paddr, npages, locked))
  398. return;
  399. __free_pages(page, order);
  400. }
  401. void snp_free_firmware_page(void *addr)
  402. {
  403. if (!addr)
  404. return;
  405. __snp_free_firmware_pages(virt_to_page(addr), 0, false);
  406. }
  407. EXPORT_SYMBOL_GPL(snp_free_firmware_page);
  408. static void *sev_fw_alloc(unsigned long len)
  409. {
  410. struct page *page;
  411. page = __snp_alloc_firmware_pages(GFP_KERNEL, get_order(len), true);
  412. if (!page)
  413. return NULL;
  414. return page_address(page);
  415. }
  416. /**
  417. * struct cmd_buf_desc - descriptors for managing legacy SEV command address
  418. * parameters corresponding to buffers that may be written to by firmware.
  419. *
  420. * @paddr_ptr: pointer to the address parameter in the command buffer which may
  421. * need to be saved/restored depending on whether a bounce buffer
  422. * is used. In the case of a bounce buffer, the command buffer
  423. * needs to be updated with the address of the new bounce buffer
  424. * snp_map_cmd_buf_desc() has allocated specifically for it. Must
  425. * be NULL if this descriptor is only an end-of-list indicator.
  426. *
  427. * @paddr_orig: storage for the original address parameter, which can be used to
  428. * restore the original value in @paddr_ptr in cases where it is
  429. * replaced with the address of a bounce buffer.
  430. *
  431. * @len: length of buffer located at the address originally stored at @paddr_ptr
  432. *
  433. * @guest_owned: true if the address corresponds to guest-owned pages, in which
  434. * case bounce buffers are not needed.
  435. */
  436. struct cmd_buf_desc {
  437. u64 *paddr_ptr;
  438. u64 paddr_orig;
  439. u32 len;
  440. bool guest_owned;
  441. };
  442. /*
  443. * If a legacy SEV command parameter is a memory address, those pages in
  444. * turn need to be transitioned to/from firmware-owned before/after
  445. * executing the firmware command.
  446. *
  447. * Additionally, in cases where those pages are not guest-owned, a bounce
  448. * buffer is needed in place of the original memory address parameter.
  449. *
  450. * A set of descriptors are used to keep track of this handling, and
  451. * initialized here based on the specific commands being executed.
  452. */
  453. static void snp_populate_cmd_buf_desc_list(int cmd, void *cmd_buf,
  454. struct cmd_buf_desc *desc_list)
  455. {
  456. switch (cmd) {
  457. case SEV_CMD_PDH_CERT_EXPORT: {
  458. struct sev_data_pdh_cert_export *data = cmd_buf;
  459. desc_list[0].paddr_ptr = &data->pdh_cert_address;
  460. desc_list[0].len = data->pdh_cert_len;
  461. desc_list[1].paddr_ptr = &data->cert_chain_address;
  462. desc_list[1].len = data->cert_chain_len;
  463. break;
  464. }
  465. case SEV_CMD_GET_ID: {
  466. struct sev_data_get_id *data = cmd_buf;
  467. desc_list[0].paddr_ptr = &data->address;
  468. desc_list[0].len = data->len;
  469. break;
  470. }
  471. case SEV_CMD_PEK_CSR: {
  472. struct sev_data_pek_csr *data = cmd_buf;
  473. desc_list[0].paddr_ptr = &data->address;
  474. desc_list[0].len = data->len;
  475. break;
  476. }
  477. case SEV_CMD_LAUNCH_UPDATE_DATA: {
  478. struct sev_data_launch_update_data *data = cmd_buf;
  479. desc_list[0].paddr_ptr = &data->address;
  480. desc_list[0].len = data->len;
  481. desc_list[0].guest_owned = true;
  482. break;
  483. }
  484. case SEV_CMD_LAUNCH_UPDATE_VMSA: {
  485. struct sev_data_launch_update_vmsa *data = cmd_buf;
  486. desc_list[0].paddr_ptr = &data->address;
  487. desc_list[0].len = data->len;
  488. desc_list[0].guest_owned = true;
  489. break;
  490. }
  491. case SEV_CMD_LAUNCH_MEASURE: {
  492. struct sev_data_launch_measure *data = cmd_buf;
  493. desc_list[0].paddr_ptr = &data->address;
  494. desc_list[0].len = data->len;
  495. break;
  496. }
  497. case SEV_CMD_LAUNCH_UPDATE_SECRET: {
  498. struct sev_data_launch_secret *data = cmd_buf;
  499. desc_list[0].paddr_ptr = &data->guest_address;
  500. desc_list[0].len = data->guest_len;
  501. desc_list[0].guest_owned = true;
  502. break;
  503. }
  504. case SEV_CMD_DBG_DECRYPT: {
  505. struct sev_data_dbg *data = cmd_buf;
  506. desc_list[0].paddr_ptr = &data->dst_addr;
  507. desc_list[0].len = data->len;
  508. desc_list[0].guest_owned = true;
  509. break;
  510. }
  511. case SEV_CMD_DBG_ENCRYPT: {
  512. struct sev_data_dbg *data = cmd_buf;
  513. desc_list[0].paddr_ptr = &data->dst_addr;
  514. desc_list[0].len = data->len;
  515. desc_list[0].guest_owned = true;
  516. break;
  517. }
  518. case SEV_CMD_ATTESTATION_REPORT: {
  519. struct sev_data_attestation_report *data = cmd_buf;
  520. desc_list[0].paddr_ptr = &data->address;
  521. desc_list[0].len = data->len;
  522. break;
  523. }
  524. case SEV_CMD_SEND_START: {
  525. struct sev_data_send_start *data = cmd_buf;
  526. desc_list[0].paddr_ptr = &data->session_address;
  527. desc_list[0].len = data->session_len;
  528. break;
  529. }
  530. case SEV_CMD_SEND_UPDATE_DATA: {
  531. struct sev_data_send_update_data *data = cmd_buf;
  532. desc_list[0].paddr_ptr = &data->hdr_address;
  533. desc_list[0].len = data->hdr_len;
  534. desc_list[1].paddr_ptr = &data->trans_address;
  535. desc_list[1].len = data->trans_len;
  536. break;
  537. }
  538. case SEV_CMD_SEND_UPDATE_VMSA: {
  539. struct sev_data_send_update_vmsa *data = cmd_buf;
  540. desc_list[0].paddr_ptr = &data->hdr_address;
  541. desc_list[0].len = data->hdr_len;
  542. desc_list[1].paddr_ptr = &data->trans_address;
  543. desc_list[1].len = data->trans_len;
  544. break;
  545. }
  546. case SEV_CMD_RECEIVE_UPDATE_DATA: {
  547. struct sev_data_receive_update_data *data = cmd_buf;
  548. desc_list[0].paddr_ptr = &data->guest_address;
  549. desc_list[0].len = data->guest_len;
  550. desc_list[0].guest_owned = true;
  551. break;
  552. }
  553. case SEV_CMD_RECEIVE_UPDATE_VMSA: {
  554. struct sev_data_receive_update_vmsa *data = cmd_buf;
  555. desc_list[0].paddr_ptr = &data->guest_address;
  556. desc_list[0].len = data->guest_len;
  557. desc_list[0].guest_owned = true;
  558. break;
  559. }
  560. default:
  561. break;
  562. }
  563. }
  564. static int snp_map_cmd_buf_desc(struct cmd_buf_desc *desc)
  565. {
  566. unsigned int npages;
  567. if (!desc->len)
  568. return 0;
  569. /* Allocate a bounce buffer if this isn't a guest owned page. */
  570. if (!desc->guest_owned) {
  571. struct page *page;
  572. page = alloc_pages(GFP_KERNEL_ACCOUNT, get_order(desc->len));
  573. if (!page) {
  574. pr_warn("Failed to allocate bounce buffer for SEV legacy command.\n");
  575. return -ENOMEM;
  576. }
  577. desc->paddr_orig = *desc->paddr_ptr;
  578. *desc->paddr_ptr = __psp_pa(page_to_virt(page));
  579. }
  580. npages = PAGE_ALIGN(desc->len) >> PAGE_SHIFT;
  581. /* Transition the buffer to firmware-owned. */
  582. if (rmp_mark_pages_firmware(*desc->paddr_ptr, npages, true)) {
  583. pr_warn("Error moving pages to firmware-owned state for SEV legacy command.\n");
  584. return -EFAULT;
  585. }
  586. return 0;
  587. }
  588. static int snp_unmap_cmd_buf_desc(struct cmd_buf_desc *desc)
  589. {
  590. unsigned int npages;
  591. if (!desc->len)
  592. return 0;
  593. npages = PAGE_ALIGN(desc->len) >> PAGE_SHIFT;
  594. /* Transition the buffers back to hypervisor-owned. */
  595. if (snp_reclaim_pages(*desc->paddr_ptr, npages, true)) {
  596. pr_warn("Failed to reclaim firmware-owned pages while issuing SEV legacy command.\n");
  597. return -EFAULT;
  598. }
  599. /* Copy data from bounce buffer and then free it. */
  600. if (!desc->guest_owned) {
  601. void *bounce_buf = __va(__sme_clr(*desc->paddr_ptr));
  602. void *dst_buf = __va(__sme_clr(desc->paddr_orig));
  603. memcpy(dst_buf, bounce_buf, desc->len);
  604. __free_pages(virt_to_page(bounce_buf), get_order(desc->len));
  605. /* Restore the original address in the command buffer. */
  606. *desc->paddr_ptr = desc->paddr_orig;
  607. }
  608. return 0;
  609. }
  610. static int snp_map_cmd_buf_desc_list(int cmd, void *cmd_buf, struct cmd_buf_desc *desc_list)
  611. {
  612. int i;
  613. snp_populate_cmd_buf_desc_list(cmd, cmd_buf, desc_list);
  614. for (i = 0; i < CMD_BUF_DESC_MAX; i++) {
  615. struct cmd_buf_desc *desc = &desc_list[i];
  616. if (!desc->paddr_ptr)
  617. break;
  618. if (snp_map_cmd_buf_desc(desc))
  619. goto err_unmap;
  620. }
  621. return 0;
  622. err_unmap:
  623. for (i--; i >= 0; i--)
  624. snp_unmap_cmd_buf_desc(&desc_list[i]);
  625. return -EFAULT;
  626. }
  627. static int snp_unmap_cmd_buf_desc_list(struct cmd_buf_desc *desc_list)
  628. {
  629. int i, ret = 0;
  630. for (i = 0; i < CMD_BUF_DESC_MAX; i++) {
  631. struct cmd_buf_desc *desc = &desc_list[i];
  632. if (!desc->paddr_ptr)
  633. break;
  634. if (snp_unmap_cmd_buf_desc(&desc_list[i]))
  635. ret = -EFAULT;
  636. }
  637. return ret;
  638. }
  639. static bool sev_cmd_buf_writable(int cmd)
  640. {
  641. switch (cmd) {
  642. case SEV_CMD_PLATFORM_STATUS:
  643. case SEV_CMD_GUEST_STATUS:
  644. case SEV_CMD_LAUNCH_START:
  645. case SEV_CMD_RECEIVE_START:
  646. case SEV_CMD_LAUNCH_MEASURE:
  647. case SEV_CMD_SEND_START:
  648. case SEV_CMD_SEND_UPDATE_DATA:
  649. case SEV_CMD_SEND_UPDATE_VMSA:
  650. case SEV_CMD_PEK_CSR:
  651. case SEV_CMD_PDH_CERT_EXPORT:
  652. case SEV_CMD_GET_ID:
  653. case SEV_CMD_ATTESTATION_REPORT:
  654. return true;
  655. default:
  656. return false;
  657. }
  658. }
  659. /* After SNP is INIT'ed, the behavior of legacy SEV commands is changed. */
  660. static bool snp_legacy_handling_needed(int cmd)
  661. {
  662. struct sev_device *sev = psp_master->sev_data;
  663. return cmd < SEV_CMD_SNP_INIT && sev->snp_initialized;
  664. }
  665. static int snp_prep_cmd_buf(int cmd, void *cmd_buf, struct cmd_buf_desc *desc_list)
  666. {
  667. if (!snp_legacy_handling_needed(cmd))
  668. return 0;
  669. if (snp_map_cmd_buf_desc_list(cmd, cmd_buf, desc_list))
  670. return -EFAULT;
  671. /*
  672. * Before command execution, the command buffer needs to be put into
  673. * the firmware-owned state.
  674. */
  675. if (sev_cmd_buf_writable(cmd)) {
  676. if (rmp_mark_pages_firmware(__pa(cmd_buf), 1, true))
  677. return -EFAULT;
  678. }
  679. return 0;
  680. }
  681. static int snp_reclaim_cmd_buf(int cmd, void *cmd_buf)
  682. {
  683. if (!snp_legacy_handling_needed(cmd))
  684. return 0;
  685. /*
  686. * After command completion, the command buffer needs to be put back
  687. * into the hypervisor-owned state.
  688. */
  689. if (sev_cmd_buf_writable(cmd))
  690. if (snp_reclaim_pages(__pa(cmd_buf), 1, true))
  691. return -EFAULT;
  692. return 0;
  693. }
  694. int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret)
  695. {
  696. struct cmd_buf_desc desc_list[CMD_BUF_DESC_MAX] = {0};
  697. struct psp_device *psp = psp_master;
  698. struct sev_device *sev;
  699. unsigned int cmdbuff_hi, cmdbuff_lo;
  700. unsigned int phys_lsb, phys_msb;
  701. unsigned int reg;
  702. void *cmd_buf;
  703. int buf_len;
  704. int ret = 0;
  705. if (!psp || !psp->sev_data)
  706. return -ENODEV;
  707. if (psp_dead)
  708. return -EBUSY;
  709. sev = psp->sev_data;
  710. buf_len = sev_cmd_buffer_len(cmd);
  711. if (WARN_ON_ONCE(!data != !buf_len))
  712. return -EINVAL;
  713. /*
  714. * Copy the incoming data to driver's scratch buffer as __pa() will not
  715. * work for some memory, e.g. vmalloc'd addresses, and @data may not be
  716. * physically contiguous.
  717. */
  718. if (data) {
  719. /*
  720. * Commands are generally issued one at a time and require the
  721. * sev_cmd_mutex, but there could be recursive firmware requests
  722. * due to SEV_CMD_SNP_PAGE_RECLAIM needing to be issued while
  723. * preparing buffers for another command. This is the only known
  724. * case of nesting in the current code, so exactly one
  725. * additional command buffer is available for that purpose.
  726. */
  727. if (!sev->cmd_buf_active) {
  728. cmd_buf = sev->cmd_buf;
  729. sev->cmd_buf_active = true;
  730. } else if (!sev->cmd_buf_backup_active) {
  731. cmd_buf = sev->cmd_buf_backup;
  732. sev->cmd_buf_backup_active = true;
  733. } else {
  734. dev_err(sev->dev,
  735. "SEV: too many firmware commands in progress, no command buffers available.\n");
  736. return -EBUSY;
  737. }
  738. memcpy(cmd_buf, data, buf_len);
  739. /*
  740. * The behavior of the SEV-legacy commands is altered when the
  741. * SNP firmware is in the INIT state.
  742. */
  743. ret = snp_prep_cmd_buf(cmd, cmd_buf, desc_list);
  744. if (ret) {
  745. dev_err(sev->dev,
  746. "SEV: failed to prepare buffer for legacy command 0x%x. Error: %d\n",
  747. cmd, ret);
  748. return ret;
  749. }
  750. } else {
  751. cmd_buf = sev->cmd_buf;
  752. }
  753. /* Get the physical address of the command buffer */
  754. phys_lsb = data ? lower_32_bits(__psp_pa(cmd_buf)) : 0;
  755. phys_msb = data ? upper_32_bits(__psp_pa(cmd_buf)) : 0;
  756. dev_dbg(sev->dev, "sev command id %#x buffer 0x%08x%08x timeout %us\n",
  757. cmd, phys_msb, phys_lsb, psp_timeout);
  758. print_hex_dump_debug("(in): ", DUMP_PREFIX_OFFSET, 16, 2, data,
  759. buf_len, false);
  760. iowrite32(phys_lsb, sev->io_regs + sev->vdata->cmdbuff_addr_lo_reg);
  761. iowrite32(phys_msb, sev->io_regs + sev->vdata->cmdbuff_addr_hi_reg);
  762. sev->int_rcvd = 0;
  763. reg = FIELD_PREP(SEV_CMDRESP_CMD, cmd);
  764. /*
  765. * If invoked during panic handling, local interrupts are disabled so
  766. * the PSP command completion interrupt can't be used.
  767. * sev_wait_cmd_ioc() already checks for interrupts disabled and
  768. * polls for PSP command completion. Ensure we do not request an
  769. * interrupt from the PSP if irqs disabled.
  770. */
  771. if (!irqs_disabled())
  772. reg |= SEV_CMDRESP_IOC;
  773. iowrite32(reg, sev->io_regs + sev->vdata->cmdresp_reg);
  774. /* wait for command completion */
  775. ret = sev_wait_cmd_ioc(sev, &reg, psp_timeout);
  776. if (ret) {
  777. if (psp_ret)
  778. *psp_ret = 0;
  779. dev_err(sev->dev, "sev command %#x timed out, disabling PSP\n", cmd);
  780. psp_dead = true;
  781. return ret;
  782. }
  783. psp_timeout = psp_cmd_timeout;
  784. if (psp_ret)
  785. *psp_ret = FIELD_GET(PSP_CMDRESP_STS, reg);
  786. if (FIELD_GET(PSP_CMDRESP_STS, reg)) {
  787. dev_dbg(sev->dev, "sev command %#x failed (%#010lx)\n",
  788. cmd, FIELD_GET(PSP_CMDRESP_STS, reg));
  789. /*
  790. * PSP firmware may report additional error information in the
  791. * command buffer registers on error. Print contents of command
  792. * buffer registers if they changed.
  793. */
  794. cmdbuff_hi = ioread32(sev->io_regs + sev->vdata->cmdbuff_addr_hi_reg);
  795. cmdbuff_lo = ioread32(sev->io_regs + sev->vdata->cmdbuff_addr_lo_reg);
  796. if (cmdbuff_hi != phys_msb || cmdbuff_lo != phys_lsb) {
  797. dev_dbg(sev->dev, "Additional error information reported in cmdbuff:");
  798. dev_dbg(sev->dev, " cmdbuff hi: %#010x\n", cmdbuff_hi);
  799. dev_dbg(sev->dev, " cmdbuff lo: %#010x\n", cmdbuff_lo);
  800. }
  801. ret = -EIO;
  802. } else {
  803. ret = sev_write_init_ex_file_if_required(cmd);
  804. }
  805. /*
  806. * Copy potential output from the PSP back to data. Do this even on
  807. * failure in case the caller wants to glean something from the error.
  808. */
  809. if (data) {
  810. int ret_reclaim;
  811. /*
  812. * Restore the page state after the command completes.
  813. */
  814. ret_reclaim = snp_reclaim_cmd_buf(cmd, cmd_buf);
  815. if (ret_reclaim) {
  816. dev_err(sev->dev,
  817. "SEV: failed to reclaim buffer for legacy command %#x. Error: %d\n",
  818. cmd, ret_reclaim);
  819. return ret_reclaim;
  820. }
  821. memcpy(data, cmd_buf, buf_len);
  822. if (sev->cmd_buf_backup_active)
  823. sev->cmd_buf_backup_active = false;
  824. else
  825. sev->cmd_buf_active = false;
  826. if (snp_unmap_cmd_buf_desc_list(desc_list))
  827. return -EFAULT;
  828. }
  829. print_hex_dump_debug("(out): ", DUMP_PREFIX_OFFSET, 16, 2, data,
  830. buf_len, false);
  831. return ret;
  832. }
  833. int sev_do_cmd(int cmd, void *data, int *psp_ret)
  834. {
  835. int rc;
  836. mutex_lock(&sev_cmd_mutex);
  837. rc = __sev_do_cmd_locked(cmd, data, psp_ret);
  838. mutex_unlock(&sev_cmd_mutex);
  839. return rc;
  840. }
  841. EXPORT_SYMBOL_GPL(sev_do_cmd);
  842. static int __sev_init_locked(int *error)
  843. {
  844. struct sev_data_init data;
  845. memset(&data, 0, sizeof(data));
  846. if (sev_es_tmr) {
  847. /*
  848. * Do not include the encryption mask on the physical
  849. * address of the TMR (firmware should clear it anyway).
  850. */
  851. data.tmr_address = __pa(sev_es_tmr);
  852. data.flags |= SEV_INIT_FLAGS_SEV_ES;
  853. data.tmr_len = sev_es_tmr_size;
  854. }
  855. return __sev_do_cmd_locked(SEV_CMD_INIT, &data, error);
  856. }
  857. static int __sev_init_ex_locked(int *error)
  858. {
  859. struct sev_data_init_ex data;
  860. memset(&data, 0, sizeof(data));
  861. data.length = sizeof(data);
  862. data.nv_address = __psp_pa(sev_init_ex_buffer);
  863. data.nv_len = NV_LENGTH;
  864. if (sev_es_tmr) {
  865. /*
  866. * Do not include the encryption mask on the physical
  867. * address of the TMR (firmware should clear it anyway).
  868. */
  869. data.tmr_address = __pa(sev_es_tmr);
  870. data.flags |= SEV_INIT_FLAGS_SEV_ES;
  871. data.tmr_len = sev_es_tmr_size;
  872. }
  873. return __sev_do_cmd_locked(SEV_CMD_INIT_EX, &data, error);
  874. }
  875. static inline int __sev_do_init_locked(int *psp_ret)
  876. {
  877. if (sev_init_ex_buffer)
  878. return __sev_init_ex_locked(psp_ret);
  879. else
  880. return __sev_init_locked(psp_ret);
  881. }
  882. static void snp_set_hsave_pa(void *arg)
  883. {
  884. wrmsrq(MSR_VM_HSAVE_PA, 0);
  885. }
  886. /* Hypervisor Fixed pages API interface */
  887. static void snp_hv_fixed_pages_state_update(struct sev_device *sev,
  888. enum snp_hv_fixed_pages_state page_state)
  889. {
  890. struct snp_hv_fixed_pages_entry *entry;
  891. /* List is protected by sev_cmd_mutex */
  892. lockdep_assert_held(&sev_cmd_mutex);
  893. if (list_empty(&snp_hv_fixed_pages))
  894. return;
  895. list_for_each_entry(entry, &snp_hv_fixed_pages, list)
  896. entry->page_state = page_state;
  897. }
  898. /*
  899. * Allocate HV_FIXED pages in 2MB aligned sizes to ensure the whole
  900. * 2MB pages are marked as HV_FIXED.
  901. */
  902. struct page *snp_alloc_hv_fixed_pages(unsigned int num_2mb_pages)
  903. {
  904. struct psp_device *psp_master = psp_get_master_device();
  905. struct snp_hv_fixed_pages_entry *entry;
  906. unsigned int order;
  907. struct page *page;
  908. if (!psp_master)
  909. return NULL;
  910. order = get_order(PMD_SIZE * num_2mb_pages);
  911. /*
  912. * SNP_INIT_EX is protected by sev_cmd_mutex, therefore this list
  913. * also needs to be protected using the same mutex.
  914. */
  915. guard(mutex)(&sev_cmd_mutex);
  916. /*
  917. * This API uses SNP_INIT_EX to transition allocated pages to HV_Fixed
  918. * page state, fail if SNP is already initialized.
  919. */
  920. if (psp_master->sev_data &&
  921. ((struct sev_device *)psp_master->sev_data)->snp_initialized)
  922. return NULL;
  923. /* Re-use freed pages that match the request */
  924. list_for_each_entry(entry, &snp_hv_fixed_pages, list) {
  925. /* Hypervisor fixed page allocator implements exact fit policy */
  926. if (entry->order == order && entry->free) {
  927. entry->free = false;
  928. memset(page_address(entry->page), 0,
  929. (1 << entry->order) * PAGE_SIZE);
  930. return entry->page;
  931. }
  932. }
  933. page = alloc_pages(GFP_KERNEL | __GFP_ZERO, order);
  934. if (!page)
  935. return NULL;
  936. entry = kzalloc_obj(*entry);
  937. if (!entry) {
  938. __free_pages(page, order);
  939. return NULL;
  940. }
  941. entry->page = page;
  942. entry->order = order;
  943. list_add_tail(&entry->list, &snp_hv_fixed_pages);
  944. return page;
  945. }
  946. void snp_free_hv_fixed_pages(struct page *page)
  947. {
  948. struct psp_device *psp_master = psp_get_master_device();
  949. struct snp_hv_fixed_pages_entry *entry, *nentry;
  950. if (!psp_master)
  951. return;
  952. /*
  953. * SNP_INIT_EX is protected by sev_cmd_mutex, therefore this list
  954. * also needs to be protected using the same mutex.
  955. */
  956. guard(mutex)(&sev_cmd_mutex);
  957. list_for_each_entry_safe(entry, nentry, &snp_hv_fixed_pages, list) {
  958. if (entry->page != page)
  959. continue;
  960. /*
  961. * HV_FIXED page state cannot be changed until reboot
  962. * and they cannot be used by an SNP guest, so they cannot
  963. * be returned back to the page allocator.
  964. * Mark the pages as free internally to allow possible re-use.
  965. */
  966. if (entry->page_state == HV_FIXED) {
  967. entry->free = true;
  968. } else {
  969. __free_pages(page, entry->order);
  970. list_del(&entry->list);
  971. kfree(entry);
  972. }
  973. return;
  974. }
  975. }
  976. static void snp_add_hv_fixed_pages(struct sev_device *sev, struct sev_data_range_list *range_list)
  977. {
  978. struct snp_hv_fixed_pages_entry *entry;
  979. struct sev_data_range *range;
  980. int num_elements;
  981. lockdep_assert_held(&sev_cmd_mutex);
  982. if (list_empty(&snp_hv_fixed_pages))
  983. return;
  984. num_elements = list_count_nodes(&snp_hv_fixed_pages) +
  985. range_list->num_elements;
  986. /*
  987. * Ensure the list of HV_FIXED pages that will be passed to firmware
  988. * do not exceed the page-sized argument buffer.
  989. */
  990. if (num_elements * sizeof(*range) + sizeof(*range_list) > PAGE_SIZE) {
  991. dev_warn(sev->dev, "Additional HV_Fixed pages cannot be accommodated, omitting\n");
  992. return;
  993. }
  994. range = &range_list->ranges[range_list->num_elements];
  995. list_for_each_entry(entry, &snp_hv_fixed_pages, list) {
  996. range->base = page_to_pfn(entry->page) << PAGE_SHIFT;
  997. range->page_count = 1 << entry->order;
  998. range++;
  999. }
  1000. range_list->num_elements = num_elements;
  1001. }
  1002. static void snp_leak_hv_fixed_pages(void)
  1003. {
  1004. struct snp_hv_fixed_pages_entry *entry;
  1005. /* List is protected by sev_cmd_mutex */
  1006. lockdep_assert_held(&sev_cmd_mutex);
  1007. if (list_empty(&snp_hv_fixed_pages))
  1008. return;
  1009. list_for_each_entry(entry, &snp_hv_fixed_pages, list)
  1010. if (entry->page_state == HV_FIXED)
  1011. __snp_leak_pages(page_to_pfn(entry->page),
  1012. 1 << entry->order, false);
  1013. }
  1014. bool sev_is_snp_ciphertext_hiding_supported(void)
  1015. {
  1016. struct psp_device *psp = psp_master;
  1017. struct sev_device *sev;
  1018. if (!psp || !psp->sev_data)
  1019. return false;
  1020. sev = psp->sev_data;
  1021. /*
  1022. * Feature information indicates if CipherTextHiding feature is
  1023. * supported by the SEV firmware and additionally platform status
  1024. * indicates if CipherTextHiding feature is enabled in the
  1025. * Platform BIOS.
  1026. */
  1027. return ((sev->snp_feat_info_0.ecx & SNP_CIPHER_TEXT_HIDING_SUPPORTED) &&
  1028. sev->snp_plat_status.ciphertext_hiding_cap);
  1029. }
  1030. EXPORT_SYMBOL_GPL(sev_is_snp_ciphertext_hiding_supported);
  1031. static int snp_get_platform_data(struct sev_device *sev, int *error)
  1032. {
  1033. struct sev_data_snp_feature_info snp_feat_info;
  1034. struct snp_feature_info *feat_info;
  1035. struct sev_data_snp_addr buf;
  1036. struct page *page;
  1037. int rc;
  1038. /*
  1039. * This function is expected to be called before SNP is
  1040. * initialized.
  1041. */
  1042. if (sev->snp_initialized)
  1043. return -EINVAL;
  1044. buf.address = __psp_pa(&sev->snp_plat_status);
  1045. rc = sev_do_cmd(SEV_CMD_SNP_PLATFORM_STATUS, &buf, error);
  1046. if (rc) {
  1047. dev_err(sev->dev, "SNP PLATFORM_STATUS command failed, ret = %d, error = %#x\n",
  1048. rc, *error);
  1049. return rc;
  1050. }
  1051. sev->api_major = sev->snp_plat_status.api_major;
  1052. sev->api_minor = sev->snp_plat_status.api_minor;
  1053. sev->build = sev->snp_plat_status.build_id;
  1054. /*
  1055. * Do feature discovery of the currently loaded firmware,
  1056. * and cache feature information from CPUID 0x8000_0024,
  1057. * sub-function 0.
  1058. */
  1059. if (!sev->snp_plat_status.feature_info)
  1060. return 0;
  1061. /*
  1062. * Use dynamically allocated structure for the SNP_FEATURE_INFO
  1063. * command to ensure structure is 8-byte aligned, and does not
  1064. * cross a page boundary.
  1065. */
  1066. page = alloc_page(GFP_KERNEL);
  1067. if (!page)
  1068. return -ENOMEM;
  1069. feat_info = page_address(page);
  1070. snp_feat_info.length = sizeof(snp_feat_info);
  1071. snp_feat_info.ecx_in = 0;
  1072. snp_feat_info.feature_info_paddr = __psp_pa(feat_info);
  1073. rc = sev_do_cmd(SEV_CMD_SNP_FEATURE_INFO, &snp_feat_info, error);
  1074. if (!rc)
  1075. sev->snp_feat_info_0 = *feat_info;
  1076. else
  1077. dev_err(sev->dev, "SNP FEATURE_INFO command failed, ret = %d, error = %#x\n",
  1078. rc, *error);
  1079. __free_page(page);
  1080. return rc;
  1081. }
  1082. static int snp_filter_reserved_mem_regions(struct resource *rs, void *arg)
  1083. {
  1084. struct sev_data_range_list *range_list = arg;
  1085. struct sev_data_range *range = &range_list->ranges[range_list->num_elements];
  1086. size_t size;
  1087. /*
  1088. * Ensure the list of HV_FIXED pages that will be passed to firmware
  1089. * do not exceed the page-sized argument buffer.
  1090. */
  1091. if ((range_list->num_elements * sizeof(struct sev_data_range) +
  1092. sizeof(struct sev_data_range_list)) > PAGE_SIZE)
  1093. return -E2BIG;
  1094. switch (rs->desc) {
  1095. case E820_TYPE_RESERVED:
  1096. case E820_TYPE_PMEM:
  1097. case E820_TYPE_ACPI:
  1098. range->base = rs->start & PAGE_MASK;
  1099. size = PAGE_ALIGN((rs->end + 1) - rs->start);
  1100. range->page_count = size >> PAGE_SHIFT;
  1101. range_list->num_elements++;
  1102. break;
  1103. default:
  1104. break;
  1105. }
  1106. return 0;
  1107. }
  1108. static int __sev_snp_init_locked(int *error, unsigned int max_snp_asid)
  1109. {
  1110. struct sev_data_range_list *snp_range_list __free(kfree) = NULL;
  1111. struct psp_device *psp = psp_master;
  1112. struct sev_data_snp_init_ex data;
  1113. struct sev_device *sev;
  1114. void *arg = &data;
  1115. int cmd, rc = 0;
  1116. if (!cc_platform_has(CC_ATTR_HOST_SEV_SNP))
  1117. return -ENODEV;
  1118. sev = psp->sev_data;
  1119. if (sev->snp_initialized)
  1120. return 0;
  1121. if (!sev_version_greater_or_equal(SNP_MIN_API_MAJOR, SNP_MIN_API_MINOR)) {
  1122. dev_dbg(sev->dev, "SEV-SNP support requires firmware version >= %d:%d\n",
  1123. SNP_MIN_API_MAJOR, SNP_MIN_API_MINOR);
  1124. return -EOPNOTSUPP;
  1125. }
  1126. /* SNP_INIT requires MSR_VM_HSAVE_PA to be cleared on all CPUs. */
  1127. on_each_cpu(snp_set_hsave_pa, NULL, 1);
  1128. /*
  1129. * Starting in SNP firmware v1.52, the SNP_INIT_EX command takes a list
  1130. * of system physical address ranges to convert into HV-fixed page
  1131. * states during the RMP initialization. For instance, the memory that
  1132. * UEFI reserves should be included in the that list. This allows system
  1133. * components that occasionally write to memory (e.g. logging to UEFI
  1134. * reserved regions) to not fail due to RMP initialization and SNP
  1135. * enablement.
  1136. *
  1137. */
  1138. if (sev_version_greater_or_equal(SNP_MIN_API_MAJOR, 52)) {
  1139. bool tio_supp = !!(sev->snp_feat_info_0.ebx & SNP_SEV_TIO_SUPPORTED);
  1140. /*
  1141. * Firmware checks that the pages containing the ranges enumerated
  1142. * in the RANGES structure are either in the default page state or in the
  1143. * firmware page state.
  1144. */
  1145. snp_range_list = kzalloc(PAGE_SIZE, GFP_KERNEL);
  1146. if (!snp_range_list) {
  1147. dev_err(sev->dev,
  1148. "SEV: SNP_INIT_EX range list memory allocation failed\n");
  1149. return -ENOMEM;
  1150. }
  1151. /*
  1152. * Retrieve all reserved memory regions from the e820 memory map
  1153. * to be setup as HV-fixed pages.
  1154. */
  1155. rc = walk_iomem_res_desc(IORES_DESC_NONE, IORESOURCE_MEM, 0, ~0,
  1156. snp_range_list, snp_filter_reserved_mem_regions);
  1157. if (rc) {
  1158. dev_err(sev->dev,
  1159. "SEV: SNP_INIT_EX walk_iomem_res_desc failed rc = %d\n", rc);
  1160. return rc;
  1161. }
  1162. /*
  1163. * Add HV_Fixed pages from other PSP sub-devices, such as SFS to the
  1164. * HV_Fixed page list.
  1165. */
  1166. snp_add_hv_fixed_pages(sev, snp_range_list);
  1167. memset(&data, 0, sizeof(data));
  1168. if (max_snp_asid) {
  1169. data.ciphertext_hiding_en = 1;
  1170. data.max_snp_asid = max_snp_asid;
  1171. }
  1172. data.init_rmp = 1;
  1173. data.list_paddr_en = 1;
  1174. data.list_paddr = __psp_pa(snp_range_list);
  1175. data.tio_en = tio_supp && sev_tio_enabled && amd_iommu_sev_tio_supported();
  1176. /*
  1177. * When psp_init_on_probe is disabled, the userspace calling
  1178. * SEV ioctl can inadvertently shut down SNP and SEV-TIO causing
  1179. * unexpected state loss.
  1180. */
  1181. if (data.tio_en && !psp_init_on_probe)
  1182. dev_warn(sev->dev, "SEV-TIO as incompatible with psp_init_on_probe=0\n");
  1183. cmd = SEV_CMD_SNP_INIT_EX;
  1184. } else {
  1185. cmd = SEV_CMD_SNP_INIT;
  1186. arg = NULL;
  1187. }
  1188. /*
  1189. * The following sequence must be issued before launching the first SNP
  1190. * guest to ensure all dirty cache lines are flushed, including from
  1191. * updates to the RMP table itself via the RMPUPDATE instruction:
  1192. *
  1193. * - WBINVD on all running CPUs
  1194. * - SEV_CMD_SNP_INIT[_EX] firmware command
  1195. * - WBINVD on all running CPUs
  1196. * - SEV_CMD_SNP_DF_FLUSH firmware command
  1197. */
  1198. wbinvd_on_all_cpus();
  1199. rc = __sev_do_cmd_locked(cmd, arg, error);
  1200. if (rc) {
  1201. dev_err(sev->dev, "SEV-SNP: %s failed rc %d, error %#x\n",
  1202. cmd == SEV_CMD_SNP_INIT_EX ? "SNP_INIT_EX" : "SNP_INIT",
  1203. rc, *error);
  1204. return rc;
  1205. }
  1206. /* Prepare for first SNP guest launch after INIT. */
  1207. wbinvd_on_all_cpus();
  1208. rc = __sev_do_cmd_locked(SEV_CMD_SNP_DF_FLUSH, NULL, error);
  1209. if (rc) {
  1210. dev_err(sev->dev, "SEV-SNP: SNP_DF_FLUSH failed rc %d, error %#x\n",
  1211. rc, *error);
  1212. return rc;
  1213. }
  1214. snp_hv_fixed_pages_state_update(sev, HV_FIXED);
  1215. sev->snp_initialized = true;
  1216. dev_dbg(sev->dev, "SEV-SNP firmware initialized, SEV-TIO is %s\n",
  1217. data.tio_en ? "enabled" : "disabled");
  1218. dev_info(sev->dev, "SEV-SNP API:%d.%d build:%d\n", sev->api_major,
  1219. sev->api_minor, sev->build);
  1220. atomic_notifier_chain_register(&panic_notifier_list,
  1221. &snp_panic_notifier);
  1222. if (data.tio_en) {
  1223. /*
  1224. * This executes with the sev_cmd_mutex held so down the stack
  1225. * snp_reclaim_pages(locked=false) might be needed (which is extremely
  1226. * unlikely) but will cause a deadlock.
  1227. * Instead of exporting __snp_alloc_firmware_pages(), allocate a page
  1228. * for this one call here.
  1229. */
  1230. void *tio_status = page_address(__snp_alloc_firmware_pages(
  1231. GFP_KERNEL_ACCOUNT | __GFP_ZERO, 0, true));
  1232. if (tio_status) {
  1233. sev_tsm_init_locked(sev, tio_status);
  1234. __snp_free_firmware_pages(virt_to_page(tio_status), 0, true);
  1235. }
  1236. }
  1237. sev_es_tmr_size = SNP_TMR_SIZE;
  1238. return 0;
  1239. }
  1240. static void __sev_platform_init_handle_tmr(struct sev_device *sev)
  1241. {
  1242. if (sev_es_tmr)
  1243. return;
  1244. /* Obtain the TMR memory area for SEV-ES use */
  1245. sev_es_tmr = sev_fw_alloc(sev_es_tmr_size);
  1246. if (sev_es_tmr) {
  1247. /* Must flush the cache before giving it to the firmware */
  1248. if (!sev->snp_initialized)
  1249. clflush_cache_range(sev_es_tmr, sev_es_tmr_size);
  1250. } else {
  1251. dev_warn(sev->dev, "SEV: TMR allocation failed, SEV-ES support unavailable\n");
  1252. }
  1253. }
  1254. /*
  1255. * If an init_ex_path is provided allocate a buffer for the file and
  1256. * read in the contents. Additionally, if SNP is initialized, convert
  1257. * the buffer pages to firmware pages.
  1258. */
  1259. static int __sev_platform_init_handle_init_ex_path(struct sev_device *sev)
  1260. {
  1261. struct page *page;
  1262. int rc;
  1263. if (!init_ex_path)
  1264. return 0;
  1265. if (sev_init_ex_buffer)
  1266. return 0;
  1267. page = alloc_pages(GFP_KERNEL, get_order(NV_LENGTH));
  1268. if (!page) {
  1269. dev_err(sev->dev, "SEV: INIT_EX NV memory allocation failed\n");
  1270. return -ENOMEM;
  1271. }
  1272. sev_init_ex_buffer = page_address(page);
  1273. rc = sev_read_init_ex_file();
  1274. if (rc)
  1275. return rc;
  1276. /* If SEV-SNP is initialized, transition to firmware page. */
  1277. if (sev->snp_initialized) {
  1278. unsigned long npages;
  1279. npages = 1UL << get_order(NV_LENGTH);
  1280. if (rmp_mark_pages_firmware(__pa(sev_init_ex_buffer), npages, false)) {
  1281. dev_err(sev->dev, "SEV: INIT_EX NV memory page state change failed.\n");
  1282. return -ENOMEM;
  1283. }
  1284. }
  1285. return 0;
  1286. }
  1287. static int __sev_platform_init_locked(int *error)
  1288. {
  1289. int rc, psp_ret, dfflush_error;
  1290. struct sev_device *sev;
  1291. psp_ret = dfflush_error = SEV_RET_NO_FW_CALL;
  1292. if (!psp_master || !psp_master->sev_data)
  1293. return -ENODEV;
  1294. sev = psp_master->sev_data;
  1295. if (sev->sev_plat_status.state == SEV_STATE_INIT)
  1296. return 0;
  1297. __sev_platform_init_handle_tmr(sev);
  1298. rc = __sev_platform_init_handle_init_ex_path(sev);
  1299. if (rc)
  1300. return rc;
  1301. rc = __sev_do_init_locked(&psp_ret);
  1302. if (rc && psp_ret == SEV_RET_SECURE_DATA_INVALID) {
  1303. /*
  1304. * Initialization command returned an integrity check failure
  1305. * status code, meaning that firmware load and validation of SEV
  1306. * related persistent data has failed. Retrying the
  1307. * initialization function should succeed by replacing the state
  1308. * with a reset state.
  1309. */
  1310. dev_err(sev->dev,
  1311. "SEV: retrying INIT command because of SECURE_DATA_INVALID error. Retrying once to reset PSP SEV state.");
  1312. rc = __sev_do_init_locked(&psp_ret);
  1313. }
  1314. if (error)
  1315. *error = psp_ret;
  1316. if (rc) {
  1317. dev_err(sev->dev, "SEV: %s failed %#x, rc %d\n",
  1318. sev_init_ex_buffer ? "INIT_EX" : "INIT", psp_ret, rc);
  1319. return rc;
  1320. }
  1321. sev->sev_plat_status.state = SEV_STATE_INIT;
  1322. /* Prepare for first SEV guest launch after INIT */
  1323. wbinvd_on_all_cpus();
  1324. rc = __sev_do_cmd_locked(SEV_CMD_DF_FLUSH, NULL, &dfflush_error);
  1325. if (rc) {
  1326. dev_err(sev->dev, "SEV: DF_FLUSH failed %#x, rc %d\n",
  1327. dfflush_error, rc);
  1328. return rc;
  1329. }
  1330. dev_dbg(sev->dev, "SEV firmware initialized\n");
  1331. dev_info(sev->dev, "SEV API:%d.%d build:%d\n", sev->api_major,
  1332. sev->api_minor, sev->build);
  1333. return 0;
  1334. }
  1335. static int _sev_platform_init_locked(struct sev_platform_init_args *args)
  1336. {
  1337. struct sev_device *sev;
  1338. int rc;
  1339. if (!psp_master || !psp_master->sev_data)
  1340. return -ENODEV;
  1341. /*
  1342. * Skip SNP/SEV initialization under a kdump kernel as SEV/SNP
  1343. * may already be initialized in the previous kernel. Since no
  1344. * SNP/SEV guests are run under a kdump kernel, there is no
  1345. * need to initialize SNP or SEV during kdump boot.
  1346. */
  1347. if (is_kdump_kernel())
  1348. return 0;
  1349. sev = psp_master->sev_data;
  1350. if (sev->sev_plat_status.state == SEV_STATE_INIT)
  1351. return 0;
  1352. rc = __sev_snp_init_locked(&args->error, args->max_snp_asid);
  1353. if (rc && rc != -ENODEV)
  1354. return rc;
  1355. /* Defer legacy SEV/SEV-ES support if allowed by caller/module. */
  1356. if (args->probe && !psp_init_on_probe)
  1357. return 0;
  1358. return __sev_platform_init_locked(&args->error);
  1359. }
  1360. int sev_platform_init(struct sev_platform_init_args *args)
  1361. {
  1362. int rc;
  1363. mutex_lock(&sev_cmd_mutex);
  1364. rc = _sev_platform_init_locked(args);
  1365. mutex_unlock(&sev_cmd_mutex);
  1366. return rc;
  1367. }
  1368. EXPORT_SYMBOL_GPL(sev_platform_init);
  1369. static int __sev_platform_shutdown_locked(int *error)
  1370. {
  1371. struct psp_device *psp = psp_master;
  1372. struct sev_device *sev;
  1373. int ret;
  1374. if (!psp || !psp->sev_data)
  1375. return 0;
  1376. sev = psp->sev_data;
  1377. if (sev->sev_plat_status.state == SEV_STATE_UNINIT)
  1378. return 0;
  1379. ret = __sev_do_cmd_locked(SEV_CMD_SHUTDOWN, NULL, error);
  1380. if (ret) {
  1381. dev_err(sev->dev, "SEV: failed to SHUTDOWN error %#x, rc %d\n",
  1382. *error, ret);
  1383. return ret;
  1384. }
  1385. sev->sev_plat_status.state = SEV_STATE_UNINIT;
  1386. dev_dbg(sev->dev, "SEV firmware shutdown\n");
  1387. return ret;
  1388. }
  1389. static int sev_get_platform_state(int *state, int *error)
  1390. {
  1391. struct sev_user_data_status data;
  1392. int rc;
  1393. rc = __sev_do_cmd_locked(SEV_CMD_PLATFORM_STATUS, &data, error);
  1394. if (rc)
  1395. return rc;
  1396. *state = data.state;
  1397. return rc;
  1398. }
  1399. static int sev_move_to_init_state(struct sev_issue_cmd *argp, bool *shutdown_required)
  1400. {
  1401. struct sev_platform_init_args init_args = {0};
  1402. int rc;
  1403. rc = _sev_platform_init_locked(&init_args);
  1404. if (rc) {
  1405. argp->error = SEV_RET_INVALID_PLATFORM_STATE;
  1406. return rc;
  1407. }
  1408. *shutdown_required = true;
  1409. return 0;
  1410. }
  1411. static int snp_move_to_init_state(struct sev_issue_cmd *argp, bool *shutdown_required)
  1412. {
  1413. int error, rc;
  1414. rc = __sev_snp_init_locked(&error, 0);
  1415. if (rc) {
  1416. argp->error = SEV_RET_INVALID_PLATFORM_STATE;
  1417. return rc;
  1418. }
  1419. *shutdown_required = true;
  1420. return 0;
  1421. }
  1422. static int sev_ioctl_do_reset(struct sev_issue_cmd *argp, bool writable)
  1423. {
  1424. int state, rc;
  1425. if (!writable)
  1426. return -EPERM;
  1427. /*
  1428. * The SEV spec requires that FACTORY_RESET must be issued in
  1429. * UNINIT state. Before we go further lets check if any guest is
  1430. * active.
  1431. *
  1432. * If FW is in WORKING state then deny the request otherwise issue
  1433. * SHUTDOWN command do INIT -> UNINIT before issuing the FACTORY_RESET.
  1434. *
  1435. */
  1436. rc = sev_get_platform_state(&state, &argp->error);
  1437. if (rc)
  1438. return rc;
  1439. if (state == SEV_STATE_WORKING)
  1440. return -EBUSY;
  1441. if (state == SEV_STATE_INIT) {
  1442. rc = __sev_platform_shutdown_locked(&argp->error);
  1443. if (rc)
  1444. return rc;
  1445. }
  1446. return __sev_do_cmd_locked(SEV_CMD_FACTORY_RESET, NULL, &argp->error);
  1447. }
  1448. static int sev_ioctl_do_platform_status(struct sev_issue_cmd *argp)
  1449. {
  1450. struct sev_user_data_status data;
  1451. int ret;
  1452. memset(&data, 0, sizeof(data));
  1453. ret = __sev_do_cmd_locked(SEV_CMD_PLATFORM_STATUS, &data, &argp->error);
  1454. if (ret)
  1455. return ret;
  1456. if (copy_to_user((void __user *)argp->data, &data, sizeof(data)))
  1457. ret = -EFAULT;
  1458. return ret;
  1459. }
  1460. static int sev_ioctl_do_pek_pdh_gen(int cmd, struct sev_issue_cmd *argp, bool writable)
  1461. {
  1462. struct sev_device *sev = psp_master->sev_data;
  1463. bool shutdown_required = false;
  1464. int rc;
  1465. if (!writable)
  1466. return -EPERM;
  1467. if (sev->sev_plat_status.state == SEV_STATE_UNINIT) {
  1468. rc = sev_move_to_init_state(argp, &shutdown_required);
  1469. if (rc)
  1470. return rc;
  1471. }
  1472. rc = __sev_do_cmd_locked(cmd, NULL, &argp->error);
  1473. if (shutdown_required)
  1474. __sev_firmware_shutdown(sev, false);
  1475. return rc;
  1476. }
  1477. static int sev_ioctl_do_pek_csr(struct sev_issue_cmd *argp, bool writable)
  1478. {
  1479. struct sev_device *sev = psp_master->sev_data;
  1480. struct sev_user_data_pek_csr input;
  1481. bool shutdown_required = false;
  1482. struct sev_data_pek_csr data;
  1483. void __user *input_address;
  1484. void *blob = NULL;
  1485. int ret;
  1486. if (!writable)
  1487. return -EPERM;
  1488. if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
  1489. return -EFAULT;
  1490. memset(&data, 0, sizeof(data));
  1491. /* userspace wants to query CSR length */
  1492. if (!input.address || !input.length)
  1493. goto cmd;
  1494. /* allocate a physically contiguous buffer to store the CSR blob */
  1495. input_address = (void __user *)input.address;
  1496. if (input.length > SEV_FW_BLOB_MAX_SIZE)
  1497. return -EFAULT;
  1498. blob = kzalloc(input.length, GFP_KERNEL);
  1499. if (!blob)
  1500. return -ENOMEM;
  1501. data.address = __psp_pa(blob);
  1502. data.len = input.length;
  1503. cmd:
  1504. if (sev->sev_plat_status.state == SEV_STATE_UNINIT) {
  1505. ret = sev_move_to_init_state(argp, &shutdown_required);
  1506. if (ret)
  1507. goto e_free_blob;
  1508. }
  1509. ret = __sev_do_cmd_locked(SEV_CMD_PEK_CSR, &data, &argp->error);
  1510. /* If we query the CSR length, FW responded with expected data. */
  1511. input.length = data.len;
  1512. if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) {
  1513. ret = -EFAULT;
  1514. goto e_free_blob;
  1515. }
  1516. if (blob) {
  1517. if (copy_to_user(input_address, blob, input.length))
  1518. ret = -EFAULT;
  1519. }
  1520. e_free_blob:
  1521. if (shutdown_required)
  1522. __sev_firmware_shutdown(sev, false);
  1523. kfree(blob);
  1524. return ret;
  1525. }
  1526. void *psp_copy_user_blob(u64 uaddr, u32 len)
  1527. {
  1528. if (!uaddr || !len)
  1529. return ERR_PTR(-EINVAL);
  1530. /* verify that blob length does not exceed our limit */
  1531. if (len > SEV_FW_BLOB_MAX_SIZE)
  1532. return ERR_PTR(-EINVAL);
  1533. return memdup_user((void __user *)uaddr, len);
  1534. }
  1535. EXPORT_SYMBOL_GPL(psp_copy_user_blob);
  1536. static int sev_get_api_version(void)
  1537. {
  1538. struct sev_device *sev = psp_master->sev_data;
  1539. struct sev_user_data_status status;
  1540. int error = 0, ret;
  1541. /*
  1542. * Cache SNP platform status and SNP feature information
  1543. * if SNP is available.
  1544. */
  1545. if (cc_platform_has(CC_ATTR_HOST_SEV_SNP)) {
  1546. ret = snp_get_platform_data(sev, &error);
  1547. if (ret)
  1548. return 1;
  1549. }
  1550. ret = sev_platform_status(&status, &error);
  1551. if (ret) {
  1552. dev_err(sev->dev,
  1553. "SEV: failed to get status. Error: %#x\n", error);
  1554. return 1;
  1555. }
  1556. /* Cache SEV platform status */
  1557. sev->sev_plat_status = status;
  1558. sev->api_major = status.api_major;
  1559. sev->api_minor = status.api_minor;
  1560. sev->build = status.build;
  1561. return 0;
  1562. }
  1563. static int sev_get_firmware(struct device *dev,
  1564. const struct firmware **firmware)
  1565. {
  1566. char fw_name_specific[SEV_FW_NAME_SIZE];
  1567. char fw_name_subset[SEV_FW_NAME_SIZE];
  1568. snprintf(fw_name_specific, sizeof(fw_name_specific),
  1569. "amd/amd_sev_fam%.2xh_model%.2xh.sbin",
  1570. boot_cpu_data.x86, boot_cpu_data.x86_model);
  1571. snprintf(fw_name_subset, sizeof(fw_name_subset),
  1572. "amd/amd_sev_fam%.2xh_model%.1xxh.sbin",
  1573. boot_cpu_data.x86, (boot_cpu_data.x86_model & 0xf0) >> 4);
  1574. /* Check for SEV FW for a particular model.
  1575. * Ex. amd_sev_fam17h_model00h.sbin for Family 17h Model 00h
  1576. *
  1577. * or
  1578. *
  1579. * Check for SEV FW common to a subset of models.
  1580. * Ex. amd_sev_fam17h_model0xh.sbin for
  1581. * Family 17h Model 00h -- Family 17h Model 0Fh
  1582. *
  1583. * or
  1584. *
  1585. * Fall-back to using generic name: sev.fw
  1586. */
  1587. if ((firmware_request_nowarn(firmware, fw_name_specific, dev) >= 0) ||
  1588. (firmware_request_nowarn(firmware, fw_name_subset, dev) >= 0) ||
  1589. (firmware_request_nowarn(firmware, SEV_FW_FILE, dev) >= 0))
  1590. return 0;
  1591. return -ENOENT;
  1592. }
  1593. /* Don't fail if SEV FW couldn't be updated. Continue with existing SEV FW */
  1594. static int sev_update_firmware(struct device *dev)
  1595. {
  1596. struct sev_data_download_firmware *data;
  1597. const struct firmware *firmware;
  1598. int ret, error, order;
  1599. struct page *p;
  1600. u64 data_size;
  1601. if (!sev_version_greater_or_equal(0, 15)) {
  1602. dev_dbg(dev, "DOWNLOAD_FIRMWARE not supported\n");
  1603. return -1;
  1604. }
  1605. if (sev_get_firmware(dev, &firmware) == -ENOENT) {
  1606. dev_dbg(dev, "No SEV firmware file present\n");
  1607. return -1;
  1608. }
  1609. /*
  1610. * SEV FW expects the physical address given to it to be 32
  1611. * byte aligned. Memory allocated has structure placed at the
  1612. * beginning followed by the firmware being passed to the SEV
  1613. * FW. Allocate enough memory for data structure + alignment
  1614. * padding + SEV FW.
  1615. */
  1616. data_size = ALIGN(sizeof(struct sev_data_download_firmware), 32);
  1617. order = get_order(firmware->size + data_size);
  1618. p = alloc_pages(GFP_KERNEL, order);
  1619. if (!p) {
  1620. ret = -1;
  1621. goto fw_err;
  1622. }
  1623. /*
  1624. * Copy firmware data to a kernel allocated contiguous
  1625. * memory region.
  1626. */
  1627. data = page_address(p);
  1628. memcpy(page_address(p) + data_size, firmware->data, firmware->size);
  1629. data->address = __psp_pa(page_address(p) + data_size);
  1630. data->len = firmware->size;
  1631. ret = sev_do_cmd(SEV_CMD_DOWNLOAD_FIRMWARE, data, &error);
  1632. /*
  1633. * A quirk for fixing the committed TCB version, when upgrading from
  1634. * earlier firmware version than 1.50.
  1635. */
  1636. if (!ret && !sev_version_greater_or_equal(1, 50))
  1637. ret = sev_do_cmd(SEV_CMD_DOWNLOAD_FIRMWARE, data, &error);
  1638. if (ret)
  1639. dev_dbg(dev, "Failed to update SEV firmware: %#x\n", error);
  1640. __free_pages(p, order);
  1641. fw_err:
  1642. release_firmware(firmware);
  1643. return ret;
  1644. }
  1645. static int __sev_snp_shutdown_locked(int *error, bool panic)
  1646. {
  1647. struct psp_device *psp = psp_master;
  1648. struct sev_device *sev;
  1649. struct sev_data_snp_shutdown_ex data;
  1650. int ret;
  1651. if (!psp || !psp->sev_data)
  1652. return 0;
  1653. sev = psp->sev_data;
  1654. if (!sev->snp_initialized)
  1655. return 0;
  1656. memset(&data, 0, sizeof(data));
  1657. data.len = sizeof(data);
  1658. data.iommu_snp_shutdown = 1;
  1659. /*
  1660. * If invoked during panic handling, local interrupts are disabled
  1661. * and all CPUs are stopped, so wbinvd_on_all_cpus() can't be called.
  1662. * In that case, a wbinvd() is done on remote CPUs via the NMI
  1663. * callback, so only a local wbinvd() is needed here.
  1664. */
  1665. if (!panic)
  1666. wbinvd_on_all_cpus();
  1667. else
  1668. wbinvd();
  1669. ret = __sev_do_cmd_locked(SEV_CMD_SNP_SHUTDOWN_EX, &data, error);
  1670. /* SHUTDOWN may require DF_FLUSH */
  1671. if (*error == SEV_RET_DFFLUSH_REQUIRED) {
  1672. int dfflush_error = SEV_RET_NO_FW_CALL;
  1673. ret = __sev_do_cmd_locked(SEV_CMD_SNP_DF_FLUSH, NULL, &dfflush_error);
  1674. if (ret) {
  1675. dev_err(sev->dev, "SEV-SNP DF_FLUSH failed, ret = %d, error = %#x\n",
  1676. ret, dfflush_error);
  1677. return ret;
  1678. }
  1679. /* reissue the shutdown command */
  1680. ret = __sev_do_cmd_locked(SEV_CMD_SNP_SHUTDOWN_EX, &data,
  1681. error);
  1682. }
  1683. if (ret) {
  1684. dev_err(sev->dev, "SEV-SNP firmware shutdown failed, rc %d, error %#x\n",
  1685. ret, *error);
  1686. return ret;
  1687. }
  1688. /*
  1689. * SNP_SHUTDOWN_EX with IOMMU_SNP_SHUTDOWN set to 1 disables SNP
  1690. * enforcement by the IOMMU and also transitions all pages
  1691. * associated with the IOMMU to the Reclaim state.
  1692. * Firmware was transitioning the IOMMU pages to Hypervisor state
  1693. * before version 1.53. But, accounting for the number of assigned
  1694. * 4kB pages in a 2M page was done incorrectly by not transitioning
  1695. * to the Reclaim state. This resulted in RMP #PF when later accessing
  1696. * the 2M page containing those pages during kexec boot. Hence, the
  1697. * firmware now transitions these pages to Reclaim state and hypervisor
  1698. * needs to transition these pages to shared state. SNP Firmware
  1699. * version 1.53 and above are needed for kexec boot.
  1700. */
  1701. ret = amd_iommu_snp_disable();
  1702. if (ret) {
  1703. dev_err(sev->dev, "SNP IOMMU shutdown failed\n");
  1704. return ret;
  1705. }
  1706. snp_leak_hv_fixed_pages();
  1707. sev->snp_initialized = false;
  1708. dev_dbg(sev->dev, "SEV-SNP firmware shutdown\n");
  1709. /*
  1710. * __sev_snp_shutdown_locked() deadlocks when it tries to unregister
  1711. * itself during panic as the panic notifier is called with RCU read
  1712. * lock held and notifier unregistration does RCU synchronization.
  1713. */
  1714. if (!panic)
  1715. atomic_notifier_chain_unregister(&panic_notifier_list,
  1716. &snp_panic_notifier);
  1717. /* Reset TMR size back to default */
  1718. sev_es_tmr_size = SEV_TMR_SIZE;
  1719. return ret;
  1720. }
  1721. static int sev_ioctl_do_pek_import(struct sev_issue_cmd *argp, bool writable)
  1722. {
  1723. struct sev_device *sev = psp_master->sev_data;
  1724. struct sev_user_data_pek_cert_import input;
  1725. struct sev_data_pek_cert_import data;
  1726. bool shutdown_required = false;
  1727. void *pek_blob, *oca_blob;
  1728. int ret;
  1729. if (!writable)
  1730. return -EPERM;
  1731. if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
  1732. return -EFAULT;
  1733. /* copy PEK certificate blobs from userspace */
  1734. pek_blob = psp_copy_user_blob(input.pek_cert_address, input.pek_cert_len);
  1735. if (IS_ERR(pek_blob))
  1736. return PTR_ERR(pek_blob);
  1737. data.reserved = 0;
  1738. data.pek_cert_address = __psp_pa(pek_blob);
  1739. data.pek_cert_len = input.pek_cert_len;
  1740. /* copy PEK certificate blobs from userspace */
  1741. oca_blob = psp_copy_user_blob(input.oca_cert_address, input.oca_cert_len);
  1742. if (IS_ERR(oca_blob)) {
  1743. ret = PTR_ERR(oca_blob);
  1744. goto e_free_pek;
  1745. }
  1746. data.oca_cert_address = __psp_pa(oca_blob);
  1747. data.oca_cert_len = input.oca_cert_len;
  1748. /* If platform is not in INIT state then transition it to INIT */
  1749. if (sev->sev_plat_status.state != SEV_STATE_INIT) {
  1750. ret = sev_move_to_init_state(argp, &shutdown_required);
  1751. if (ret)
  1752. goto e_free_oca;
  1753. }
  1754. ret = __sev_do_cmd_locked(SEV_CMD_PEK_CERT_IMPORT, &data, &argp->error);
  1755. e_free_oca:
  1756. if (shutdown_required)
  1757. __sev_firmware_shutdown(sev, false);
  1758. kfree(oca_blob);
  1759. e_free_pek:
  1760. kfree(pek_blob);
  1761. return ret;
  1762. }
  1763. static int sev_ioctl_do_get_id2(struct sev_issue_cmd *argp)
  1764. {
  1765. struct sev_user_data_get_id2 input;
  1766. struct sev_data_get_id data;
  1767. void __user *input_address;
  1768. void *id_blob = NULL;
  1769. int ret;
  1770. /* SEV GET_ID is available from SEV API v0.16 and up */
  1771. if (!sev_version_greater_or_equal(0, 16))
  1772. return -ENOTSUPP;
  1773. if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
  1774. return -EFAULT;
  1775. input_address = (void __user *)input.address;
  1776. if (input.address && input.length) {
  1777. /*
  1778. * The length of the ID shouldn't be assumed by software since
  1779. * it may change in the future. The allocation size is limited
  1780. * to 1 << (PAGE_SHIFT + MAX_PAGE_ORDER) by the page allocator.
  1781. * If the allocation fails, simply return ENOMEM rather than
  1782. * warning in the kernel log.
  1783. */
  1784. id_blob = kzalloc(input.length, GFP_KERNEL | __GFP_NOWARN);
  1785. if (!id_blob)
  1786. return -ENOMEM;
  1787. data.address = __psp_pa(id_blob);
  1788. data.len = input.length;
  1789. } else {
  1790. data.address = 0;
  1791. data.len = 0;
  1792. }
  1793. ret = __sev_do_cmd_locked(SEV_CMD_GET_ID, &data, &argp->error);
  1794. /*
  1795. * Firmware will return the length of the ID value (either the minimum
  1796. * required length or the actual length written), return it to the user.
  1797. */
  1798. input.length = data.len;
  1799. if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) {
  1800. ret = -EFAULT;
  1801. goto e_free;
  1802. }
  1803. if (id_blob) {
  1804. if (copy_to_user(input_address, id_blob, data.len)) {
  1805. ret = -EFAULT;
  1806. goto e_free;
  1807. }
  1808. }
  1809. e_free:
  1810. kfree(id_blob);
  1811. return ret;
  1812. }
  1813. static int sev_ioctl_do_get_id(struct sev_issue_cmd *argp)
  1814. {
  1815. struct sev_data_get_id *data;
  1816. u64 data_size, user_size;
  1817. void *id_blob, *mem;
  1818. int ret;
  1819. /* SEV GET_ID available from SEV API v0.16 and up */
  1820. if (!sev_version_greater_or_equal(0, 16))
  1821. return -ENOTSUPP;
  1822. /* SEV FW expects the buffer it fills with the ID to be
  1823. * 8-byte aligned. Memory allocated should be enough to
  1824. * hold data structure + alignment padding + memory
  1825. * where SEV FW writes the ID.
  1826. */
  1827. data_size = ALIGN(sizeof(struct sev_data_get_id), 8);
  1828. user_size = sizeof(struct sev_user_data_get_id);
  1829. mem = kzalloc(data_size + user_size, GFP_KERNEL);
  1830. if (!mem)
  1831. return -ENOMEM;
  1832. data = mem;
  1833. id_blob = mem + data_size;
  1834. data->address = __psp_pa(id_blob);
  1835. data->len = user_size;
  1836. ret = __sev_do_cmd_locked(SEV_CMD_GET_ID, data, &argp->error);
  1837. if (!ret) {
  1838. if (copy_to_user((void __user *)argp->data, id_blob, data->len))
  1839. ret = -EFAULT;
  1840. }
  1841. kfree(mem);
  1842. return ret;
  1843. }
  1844. static int sev_ioctl_do_pdh_export(struct sev_issue_cmd *argp, bool writable)
  1845. {
  1846. struct sev_device *sev = psp_master->sev_data;
  1847. struct sev_user_data_pdh_cert_export input;
  1848. void *pdh_blob = NULL, *cert_blob = NULL;
  1849. struct sev_data_pdh_cert_export data;
  1850. void __user *input_cert_chain_address;
  1851. void __user *input_pdh_cert_address;
  1852. bool shutdown_required = false;
  1853. int ret;
  1854. if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
  1855. return -EFAULT;
  1856. memset(&data, 0, sizeof(data));
  1857. input_pdh_cert_address = (void __user *)input.pdh_cert_address;
  1858. input_cert_chain_address = (void __user *)input.cert_chain_address;
  1859. /* Userspace wants to query the certificate length. */
  1860. if (!input.pdh_cert_address ||
  1861. !input.pdh_cert_len ||
  1862. !input.cert_chain_address)
  1863. goto cmd;
  1864. /* Allocate a physically contiguous buffer to store the PDH blob. */
  1865. if (input.pdh_cert_len > SEV_FW_BLOB_MAX_SIZE)
  1866. return -EFAULT;
  1867. /* Allocate a physically contiguous buffer to store the cert chain blob. */
  1868. if (input.cert_chain_len > SEV_FW_BLOB_MAX_SIZE)
  1869. return -EFAULT;
  1870. pdh_blob = kzalloc(input.pdh_cert_len, GFP_KERNEL);
  1871. if (!pdh_blob)
  1872. return -ENOMEM;
  1873. data.pdh_cert_address = __psp_pa(pdh_blob);
  1874. data.pdh_cert_len = input.pdh_cert_len;
  1875. cert_blob = kzalloc(input.cert_chain_len, GFP_KERNEL);
  1876. if (!cert_blob) {
  1877. ret = -ENOMEM;
  1878. goto e_free_pdh;
  1879. }
  1880. data.cert_chain_address = __psp_pa(cert_blob);
  1881. data.cert_chain_len = input.cert_chain_len;
  1882. cmd:
  1883. /* If platform is not in INIT state then transition it to INIT. */
  1884. if (sev->sev_plat_status.state != SEV_STATE_INIT) {
  1885. if (!writable) {
  1886. ret = -EPERM;
  1887. goto e_free_cert;
  1888. }
  1889. ret = sev_move_to_init_state(argp, &shutdown_required);
  1890. if (ret)
  1891. goto e_free_cert;
  1892. }
  1893. ret = __sev_do_cmd_locked(SEV_CMD_PDH_CERT_EXPORT, &data, &argp->error);
  1894. /* If we query the length, FW responded with expected data. */
  1895. input.cert_chain_len = data.cert_chain_len;
  1896. input.pdh_cert_len = data.pdh_cert_len;
  1897. if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) {
  1898. ret = -EFAULT;
  1899. goto e_free_cert;
  1900. }
  1901. if (pdh_blob) {
  1902. if (copy_to_user(input_pdh_cert_address,
  1903. pdh_blob, input.pdh_cert_len)) {
  1904. ret = -EFAULT;
  1905. goto e_free_cert;
  1906. }
  1907. }
  1908. if (cert_blob) {
  1909. if (copy_to_user(input_cert_chain_address,
  1910. cert_blob, input.cert_chain_len))
  1911. ret = -EFAULT;
  1912. }
  1913. e_free_cert:
  1914. if (shutdown_required)
  1915. __sev_firmware_shutdown(sev, false);
  1916. kfree(cert_blob);
  1917. e_free_pdh:
  1918. kfree(pdh_blob);
  1919. return ret;
  1920. }
  1921. static int sev_ioctl_do_snp_platform_status(struct sev_issue_cmd *argp)
  1922. {
  1923. struct sev_device *sev = psp_master->sev_data;
  1924. struct sev_data_snp_addr buf;
  1925. struct page *status_page;
  1926. void *data;
  1927. int ret;
  1928. if (!argp->data)
  1929. return -EINVAL;
  1930. status_page = alloc_page(GFP_KERNEL_ACCOUNT);
  1931. if (!status_page)
  1932. return -ENOMEM;
  1933. data = page_address(status_page);
  1934. /*
  1935. * SNP_PLATFORM_STATUS can be executed in any SNP state. But if executed
  1936. * when SNP has been initialized, the status page must be firmware-owned.
  1937. */
  1938. if (sev->snp_initialized) {
  1939. /*
  1940. * Firmware expects the status page to be in Firmware state,
  1941. * otherwise it will report an error INVALID_PAGE_STATE.
  1942. */
  1943. if (rmp_mark_pages_firmware(__pa(data), 1, true)) {
  1944. ret = -EFAULT;
  1945. goto cleanup;
  1946. }
  1947. }
  1948. buf.address = __psp_pa(data);
  1949. ret = __sev_do_cmd_locked(SEV_CMD_SNP_PLATFORM_STATUS, &buf, &argp->error);
  1950. if (sev->snp_initialized) {
  1951. /*
  1952. * The status page will be in Reclaim state on success, or left
  1953. * in Firmware state on failure. Use snp_reclaim_pages() to
  1954. * transition either case back to Hypervisor-owned state.
  1955. */
  1956. if (snp_reclaim_pages(__pa(data), 1, true))
  1957. return -EFAULT;
  1958. }
  1959. if (ret)
  1960. goto cleanup;
  1961. if (copy_to_user((void __user *)argp->data, data,
  1962. sizeof(struct sev_user_data_snp_status)))
  1963. ret = -EFAULT;
  1964. cleanup:
  1965. __free_pages(status_page, 0);
  1966. return ret;
  1967. }
  1968. static int sev_ioctl_do_snp_commit(struct sev_issue_cmd *argp)
  1969. {
  1970. struct sev_device *sev = psp_master->sev_data;
  1971. struct sev_data_snp_commit buf;
  1972. bool shutdown_required = false;
  1973. int ret, error;
  1974. if (!sev->snp_initialized) {
  1975. ret = snp_move_to_init_state(argp, &shutdown_required);
  1976. if (ret)
  1977. return ret;
  1978. }
  1979. buf.len = sizeof(buf);
  1980. ret = __sev_do_cmd_locked(SEV_CMD_SNP_COMMIT, &buf, &argp->error);
  1981. if (shutdown_required)
  1982. __sev_snp_shutdown_locked(&error, false);
  1983. return ret;
  1984. }
  1985. static int sev_ioctl_do_snp_set_config(struct sev_issue_cmd *argp, bool writable)
  1986. {
  1987. struct sev_device *sev = psp_master->sev_data;
  1988. struct sev_user_data_snp_config config;
  1989. bool shutdown_required = false;
  1990. int ret, error;
  1991. if (!argp->data)
  1992. return -EINVAL;
  1993. if (!writable)
  1994. return -EPERM;
  1995. if (copy_from_user(&config, (void __user *)argp->data, sizeof(config)))
  1996. return -EFAULT;
  1997. if (!sev->snp_initialized) {
  1998. ret = snp_move_to_init_state(argp, &shutdown_required);
  1999. if (ret)
  2000. return ret;
  2001. }
  2002. ret = __sev_do_cmd_locked(SEV_CMD_SNP_CONFIG, &config, &argp->error);
  2003. if (shutdown_required)
  2004. __sev_snp_shutdown_locked(&error, false);
  2005. return ret;
  2006. }
  2007. static int sev_ioctl_do_snp_vlek_load(struct sev_issue_cmd *argp, bool writable)
  2008. {
  2009. struct sev_device *sev = psp_master->sev_data;
  2010. struct sev_user_data_snp_vlek_load input;
  2011. bool shutdown_required = false;
  2012. int ret, error;
  2013. void *blob;
  2014. if (!argp->data)
  2015. return -EINVAL;
  2016. if (!writable)
  2017. return -EPERM;
  2018. if (copy_from_user(&input, u64_to_user_ptr(argp->data), sizeof(input)))
  2019. return -EFAULT;
  2020. if (input.len != sizeof(input) || input.vlek_wrapped_version != 0)
  2021. return -EINVAL;
  2022. blob = psp_copy_user_blob(input.vlek_wrapped_address,
  2023. sizeof(struct sev_user_data_snp_wrapped_vlek_hashstick));
  2024. if (IS_ERR(blob))
  2025. return PTR_ERR(blob);
  2026. input.vlek_wrapped_address = __psp_pa(blob);
  2027. if (!sev->snp_initialized) {
  2028. ret = snp_move_to_init_state(argp, &shutdown_required);
  2029. if (ret)
  2030. goto cleanup;
  2031. }
  2032. ret = __sev_do_cmd_locked(SEV_CMD_SNP_VLEK_LOAD, &input, &argp->error);
  2033. if (shutdown_required)
  2034. __sev_snp_shutdown_locked(&error, false);
  2035. cleanup:
  2036. kfree(blob);
  2037. return ret;
  2038. }
  2039. static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg)
  2040. {
  2041. void __user *argp = (void __user *)arg;
  2042. struct sev_issue_cmd input;
  2043. int ret = -EFAULT;
  2044. bool writable = file->f_mode & FMODE_WRITE;
  2045. if (!psp_master || !psp_master->sev_data)
  2046. return -ENODEV;
  2047. if (ioctl != SEV_ISSUE_CMD)
  2048. return -EINVAL;
  2049. if (copy_from_user(&input, argp, sizeof(struct sev_issue_cmd)))
  2050. return -EFAULT;
  2051. if (input.cmd > SEV_MAX)
  2052. return -EINVAL;
  2053. mutex_lock(&sev_cmd_mutex);
  2054. switch (input.cmd) {
  2055. case SEV_FACTORY_RESET:
  2056. ret = sev_ioctl_do_reset(&input, writable);
  2057. break;
  2058. case SEV_PLATFORM_STATUS:
  2059. ret = sev_ioctl_do_platform_status(&input);
  2060. break;
  2061. case SEV_PEK_GEN:
  2062. ret = sev_ioctl_do_pek_pdh_gen(SEV_CMD_PEK_GEN, &input, writable);
  2063. break;
  2064. case SEV_PDH_GEN:
  2065. ret = sev_ioctl_do_pek_pdh_gen(SEV_CMD_PDH_GEN, &input, writable);
  2066. break;
  2067. case SEV_PEK_CSR:
  2068. ret = sev_ioctl_do_pek_csr(&input, writable);
  2069. break;
  2070. case SEV_PEK_CERT_IMPORT:
  2071. ret = sev_ioctl_do_pek_import(&input, writable);
  2072. break;
  2073. case SEV_PDH_CERT_EXPORT:
  2074. ret = sev_ioctl_do_pdh_export(&input, writable);
  2075. break;
  2076. case SEV_GET_ID:
  2077. pr_warn_once("SEV_GET_ID command is deprecated, use SEV_GET_ID2\n");
  2078. ret = sev_ioctl_do_get_id(&input);
  2079. break;
  2080. case SEV_GET_ID2:
  2081. ret = sev_ioctl_do_get_id2(&input);
  2082. break;
  2083. case SNP_PLATFORM_STATUS:
  2084. ret = sev_ioctl_do_snp_platform_status(&input);
  2085. break;
  2086. case SNP_COMMIT:
  2087. ret = sev_ioctl_do_snp_commit(&input);
  2088. break;
  2089. case SNP_SET_CONFIG:
  2090. ret = sev_ioctl_do_snp_set_config(&input, writable);
  2091. break;
  2092. case SNP_VLEK_LOAD:
  2093. ret = sev_ioctl_do_snp_vlek_load(&input, writable);
  2094. break;
  2095. default:
  2096. ret = -EINVAL;
  2097. goto out;
  2098. }
  2099. if (copy_to_user(argp, &input, sizeof(struct sev_issue_cmd)))
  2100. ret = -EFAULT;
  2101. out:
  2102. mutex_unlock(&sev_cmd_mutex);
  2103. return ret;
  2104. }
  2105. static const struct file_operations sev_fops = {
  2106. .owner = THIS_MODULE,
  2107. .unlocked_ioctl = sev_ioctl,
  2108. };
  2109. int sev_platform_status(struct sev_user_data_status *data, int *error)
  2110. {
  2111. return sev_do_cmd(SEV_CMD_PLATFORM_STATUS, data, error);
  2112. }
  2113. EXPORT_SYMBOL_GPL(sev_platform_status);
  2114. int sev_guest_deactivate(struct sev_data_deactivate *data, int *error)
  2115. {
  2116. return sev_do_cmd(SEV_CMD_DEACTIVATE, data, error);
  2117. }
  2118. EXPORT_SYMBOL_GPL(sev_guest_deactivate);
  2119. int sev_guest_activate(struct sev_data_activate *data, int *error)
  2120. {
  2121. return sev_do_cmd(SEV_CMD_ACTIVATE, data, error);
  2122. }
  2123. EXPORT_SYMBOL_GPL(sev_guest_activate);
  2124. int sev_guest_decommission(struct sev_data_decommission *data, int *error)
  2125. {
  2126. return sev_do_cmd(SEV_CMD_DECOMMISSION, data, error);
  2127. }
  2128. EXPORT_SYMBOL_GPL(sev_guest_decommission);
  2129. int sev_guest_df_flush(int *error)
  2130. {
  2131. return sev_do_cmd(SEV_CMD_DF_FLUSH, NULL, error);
  2132. }
  2133. EXPORT_SYMBOL_GPL(sev_guest_df_flush);
  2134. static void sev_exit(struct kref *ref)
  2135. {
  2136. misc_deregister(&misc_dev->misc);
  2137. kfree(misc_dev);
  2138. misc_dev = NULL;
  2139. }
  2140. static int sev_misc_init(struct sev_device *sev)
  2141. {
  2142. struct device *dev = sev->dev;
  2143. int ret;
  2144. /*
  2145. * SEV feature support can be detected on multiple devices but the SEV
  2146. * FW commands must be issued on the master. During probe, we do not
  2147. * know the master hence we create /dev/sev on the first device probe.
  2148. * sev_do_cmd() finds the right master device to which to issue the
  2149. * command to the firmware.
  2150. */
  2151. if (!misc_dev) {
  2152. struct miscdevice *misc;
  2153. misc_dev = kzalloc_obj(*misc_dev);
  2154. if (!misc_dev)
  2155. return -ENOMEM;
  2156. misc = &misc_dev->misc;
  2157. misc->minor = MISC_DYNAMIC_MINOR;
  2158. misc->name = DEVICE_NAME;
  2159. misc->fops = &sev_fops;
  2160. ret = misc_register(misc);
  2161. if (ret)
  2162. return ret;
  2163. kref_init(&misc_dev->refcount);
  2164. } else {
  2165. kref_get(&misc_dev->refcount);
  2166. }
  2167. init_waitqueue_head(&sev->int_queue);
  2168. sev->misc = misc_dev;
  2169. dev_dbg(dev, "registered SEV device\n");
  2170. return 0;
  2171. }
  2172. int sev_dev_init(struct psp_device *psp)
  2173. {
  2174. struct device *dev = psp->dev;
  2175. struct sev_device *sev;
  2176. int ret = -ENOMEM;
  2177. if (!boot_cpu_has(X86_FEATURE_SEV)) {
  2178. dev_info_once(dev, "SEV: memory encryption not enabled by BIOS\n");
  2179. return 0;
  2180. }
  2181. sev = devm_kzalloc(dev, sizeof(*sev), GFP_KERNEL);
  2182. if (!sev)
  2183. goto e_err;
  2184. sev->cmd_buf = (void *)devm_get_free_pages(dev, GFP_KERNEL, 1);
  2185. if (!sev->cmd_buf)
  2186. goto e_sev;
  2187. sev->cmd_buf_backup = (uint8_t *)sev->cmd_buf + PAGE_SIZE;
  2188. psp->sev_data = sev;
  2189. sev->dev = dev;
  2190. sev->psp = psp;
  2191. sev->io_regs = psp->io_regs;
  2192. sev->vdata = (struct sev_vdata *)psp->vdata->sev;
  2193. if (!sev->vdata) {
  2194. ret = -ENODEV;
  2195. dev_err(dev, "sev: missing driver data\n");
  2196. goto e_buf;
  2197. }
  2198. psp_set_sev_irq_handler(psp, sev_irq_handler, sev);
  2199. ret = sev_misc_init(sev);
  2200. if (ret)
  2201. goto e_irq;
  2202. dev_notice(dev, "sev enabled\n");
  2203. return 0;
  2204. e_irq:
  2205. psp_clear_sev_irq_handler(psp);
  2206. e_buf:
  2207. devm_free_pages(dev, (unsigned long)sev->cmd_buf);
  2208. e_sev:
  2209. devm_kfree(dev, sev);
  2210. e_err:
  2211. psp->sev_data = NULL;
  2212. dev_notice(dev, "sev initialization failed\n");
  2213. return ret;
  2214. }
  2215. static void __sev_firmware_shutdown(struct sev_device *sev, bool panic)
  2216. {
  2217. int error;
  2218. __sev_platform_shutdown_locked(&error);
  2219. if (sev_es_tmr) {
  2220. /*
  2221. * The TMR area was encrypted, flush it from the cache.
  2222. *
  2223. * If invoked during panic handling, local interrupts are
  2224. * disabled and all CPUs are stopped, so wbinvd_on_all_cpus()
  2225. * can't be used. In that case, wbinvd() is done on remote CPUs
  2226. * via the NMI callback, and done for this CPU later during
  2227. * SNP shutdown, so wbinvd_on_all_cpus() can be skipped.
  2228. */
  2229. if (!panic)
  2230. wbinvd_on_all_cpus();
  2231. __snp_free_firmware_pages(virt_to_page(sev_es_tmr),
  2232. get_order(sev_es_tmr_size),
  2233. true);
  2234. sev_es_tmr = NULL;
  2235. }
  2236. if (sev_init_ex_buffer) {
  2237. __snp_free_firmware_pages(virt_to_page(sev_init_ex_buffer),
  2238. get_order(NV_LENGTH),
  2239. true);
  2240. sev_init_ex_buffer = NULL;
  2241. }
  2242. __sev_snp_shutdown_locked(&error, panic);
  2243. }
  2244. static void sev_firmware_shutdown(struct sev_device *sev)
  2245. {
  2246. /*
  2247. * Calling without sev_cmd_mutex held as TSM will likely try disconnecting
  2248. * IDE and this ends up calling sev_do_cmd() which locks sev_cmd_mutex.
  2249. */
  2250. if (sev->tio_status)
  2251. sev_tsm_uninit(sev);
  2252. mutex_lock(&sev_cmd_mutex);
  2253. __sev_firmware_shutdown(sev, false);
  2254. kfree(sev->tio_status);
  2255. sev->tio_status = NULL;
  2256. mutex_unlock(&sev_cmd_mutex);
  2257. }
  2258. void sev_platform_shutdown(void)
  2259. {
  2260. if (!psp_master || !psp_master->sev_data)
  2261. return;
  2262. sev_firmware_shutdown(psp_master->sev_data);
  2263. }
  2264. EXPORT_SYMBOL_GPL(sev_platform_shutdown);
  2265. u64 sev_get_snp_policy_bits(void)
  2266. {
  2267. struct psp_device *psp = psp_master;
  2268. struct sev_device *sev;
  2269. u64 policy_bits;
  2270. if (!cc_platform_has(CC_ATTR_HOST_SEV_SNP))
  2271. return 0;
  2272. if (!psp || !psp->sev_data)
  2273. return 0;
  2274. sev = psp->sev_data;
  2275. policy_bits = SNP_POLICY_MASK_BASE;
  2276. if (sev->snp_plat_status.feature_info) {
  2277. if (sev->snp_feat_info_0.ecx & SNP_RAPL_DISABLE_SUPPORTED)
  2278. policy_bits |= SNP_POLICY_MASK_RAPL_DIS;
  2279. if (sev->snp_feat_info_0.ecx & SNP_CIPHER_TEXT_HIDING_SUPPORTED)
  2280. policy_bits |= SNP_POLICY_MASK_CIPHERTEXT_HIDING_DRAM;
  2281. if (sev->snp_feat_info_0.ecx & SNP_AES_256_XTS_POLICY_SUPPORTED)
  2282. policy_bits |= SNP_POLICY_MASK_MEM_AES_256_XTS;
  2283. if (sev->snp_feat_info_0.ecx & SNP_CXL_ALLOW_POLICY_SUPPORTED)
  2284. policy_bits |= SNP_POLICY_MASK_CXL_ALLOW;
  2285. if (sev_version_greater_or_equal(1, 58))
  2286. policy_bits |= SNP_POLICY_MASK_PAGE_SWAP_DISABLE;
  2287. }
  2288. return policy_bits;
  2289. }
  2290. EXPORT_SYMBOL_GPL(sev_get_snp_policy_bits);
  2291. void sev_dev_destroy(struct psp_device *psp)
  2292. {
  2293. struct sev_device *sev = psp->sev_data;
  2294. if (!sev)
  2295. return;
  2296. sev_firmware_shutdown(sev);
  2297. if (sev->misc)
  2298. kref_put(&misc_dev->refcount, sev_exit);
  2299. psp_clear_sev_irq_handler(psp);
  2300. }
  2301. static int snp_shutdown_on_panic(struct notifier_block *nb,
  2302. unsigned long reason, void *arg)
  2303. {
  2304. struct sev_device *sev = psp_master->sev_data;
  2305. /*
  2306. * If sev_cmd_mutex is already acquired, then it's likely
  2307. * another PSP command is in flight and issuing a shutdown
  2308. * would fail in unexpected ways. Rather than create even
  2309. * more confusion during a panic, just bail out here.
  2310. */
  2311. if (mutex_is_locked(&sev_cmd_mutex))
  2312. return NOTIFY_DONE;
  2313. __sev_firmware_shutdown(sev, true);
  2314. return NOTIFY_DONE;
  2315. }
  2316. int sev_issue_cmd_external_user(struct file *filep, unsigned int cmd,
  2317. void *data, int *error)
  2318. {
  2319. if (!filep || filep->f_op != &sev_fops)
  2320. return -EBADF;
  2321. return sev_do_cmd(cmd, data, error);
  2322. }
  2323. EXPORT_SYMBOL_GPL(sev_issue_cmd_external_user);
  2324. void sev_pci_init(void)
  2325. {
  2326. struct sev_device *sev = psp_master->sev_data;
  2327. u8 api_major, api_minor, build;
  2328. if (!sev)
  2329. return;
  2330. psp_timeout = psp_probe_timeout;
  2331. if (sev_get_api_version())
  2332. goto err;
  2333. api_major = sev->api_major;
  2334. api_minor = sev->api_minor;
  2335. build = sev->build;
  2336. if (sev_update_firmware(sev->dev) == 0)
  2337. sev_get_api_version();
  2338. if (api_major != sev->api_major || api_minor != sev->api_minor ||
  2339. build != sev->build)
  2340. dev_info(sev->dev, "SEV firmware updated from %d.%d.%d to %d.%d.%d\n",
  2341. api_major, api_minor, build,
  2342. sev->api_major, sev->api_minor, sev->build);
  2343. return;
  2344. err:
  2345. sev_dev_destroy(psp_master);
  2346. psp_master->sev_data = NULL;
  2347. }
  2348. void sev_pci_exit(void)
  2349. {
  2350. struct sev_device *sev = psp_master->sev_data;
  2351. if (!sev)
  2352. return;
  2353. sev_firmware_shutdown(sev);
  2354. }