ne_misc_dev.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright 2020-2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. */
  5. /**
  6. * DOC: Enclave lifetime management driver for Nitro Enclaves (NE).
  7. * Nitro is a hypervisor that has been developed by Amazon.
  8. */
  9. #include <linux/anon_inodes.h>
  10. #include <linux/capability.h>
  11. #include <linux/cpu.h>
  12. #include <linux/device.h>
  13. #include <linux/file.h>
  14. #include <linux/hugetlb.h>
  15. #include <linux/limits.h>
  16. #include <linux/list.h>
  17. #include <linux/miscdevice.h>
  18. #include <linux/mm.h>
  19. #include <linux/mman.h>
  20. #include <linux/module.h>
  21. #include <linux/mutex.h>
  22. #include <linux/nitro_enclaves.h>
  23. #include <linux/pci.h>
  24. #include <linux/poll.h>
  25. #include <linux/range.h>
  26. #include <linux/slab.h>
  27. #include <linux/types.h>
  28. #include <uapi/linux/vm_sockets.h>
  29. #include "ne_misc_dev.h"
  30. #include "ne_pci_dev.h"
  31. /**
  32. * NE_CPUS_SIZE - Size for max 128 CPUs, for now, in a cpu-list string, comma
  33. * separated. The NE CPU pool includes CPUs from a single NUMA
  34. * node.
  35. */
  36. #define NE_CPUS_SIZE (512)
  37. /**
  38. * NE_EIF_LOAD_OFFSET - The offset where to copy the Enclave Image Format (EIF)
  39. * image in enclave memory.
  40. */
  41. #define NE_EIF_LOAD_OFFSET (8 * 1024UL * 1024UL)
  42. /**
  43. * NE_MIN_ENCLAVE_MEM_SIZE - The minimum memory size an enclave can be launched
  44. * with.
  45. */
  46. #define NE_MIN_ENCLAVE_MEM_SIZE (64 * 1024UL * 1024UL)
  47. /**
  48. * NE_MIN_MEM_REGION_SIZE - The minimum size of an enclave memory region.
  49. */
  50. #define NE_MIN_MEM_REGION_SIZE (2 * 1024UL * 1024UL)
  51. /**
  52. * NE_PARENT_VM_CID - The CID for the vsock device of the primary / parent VM.
  53. */
  54. #define NE_PARENT_VM_CID (3)
  55. static long ne_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
  56. static const struct file_operations ne_fops = {
  57. .owner = THIS_MODULE,
  58. .llseek = noop_llseek,
  59. .unlocked_ioctl = ne_ioctl,
  60. };
  61. static struct miscdevice ne_misc_dev = {
  62. .minor = MISC_DYNAMIC_MINOR,
  63. .name = "nitro_enclaves",
  64. .fops = &ne_fops,
  65. .mode = 0660,
  66. };
  67. struct ne_devs ne_devs = {
  68. .ne_misc_dev = &ne_misc_dev,
  69. };
  70. /*
  71. * TODO: Update logic to create new sysfs entries instead of using
  72. * a kernel parameter e.g. if multiple sysfs files needed.
  73. */
  74. static int ne_set_kernel_param(const char *val, const struct kernel_param *kp);
  75. static const struct kernel_param_ops ne_cpu_pool_ops = {
  76. .get = param_get_string,
  77. .set = ne_set_kernel_param,
  78. };
  79. static char ne_cpus[NE_CPUS_SIZE];
  80. static struct kparam_string ne_cpus_arg = {
  81. .maxlen = sizeof(ne_cpus),
  82. .string = ne_cpus,
  83. };
  84. module_param_cb(ne_cpus, &ne_cpu_pool_ops, &ne_cpus_arg, 0644);
  85. /* https://www.kernel.org/doc/html/latest/admin-guide/kernel-parameters.html#cpu-lists */
  86. MODULE_PARM_DESC(ne_cpus, "<cpu-list> - CPU pool used for Nitro Enclaves");
  87. /**
  88. * struct ne_cpu_pool - CPU pool used for Nitro Enclaves.
  89. * @avail_threads_per_core: Available full CPU cores to be dedicated to
  90. * enclave(s). The cpumasks from the array, indexed
  91. * by core id, contain all the threads from the
  92. * available cores, that are not set for created
  93. * enclave(s). The full CPU cores are part of the
  94. * NE CPU pool.
  95. * @mutex: Mutex for the access to the NE CPU pool.
  96. * @nr_parent_vm_cores : The size of the available threads per core array.
  97. * The total number of CPU cores available on the
  98. * primary / parent VM.
  99. * @nr_threads_per_core: The number of threads that a full CPU core has.
  100. * @numa_node: NUMA node of the CPUs in the pool.
  101. */
  102. struct ne_cpu_pool {
  103. cpumask_var_t *avail_threads_per_core;
  104. struct mutex mutex;
  105. unsigned int nr_parent_vm_cores;
  106. unsigned int nr_threads_per_core;
  107. int numa_node;
  108. };
  109. static struct ne_cpu_pool ne_cpu_pool;
  110. /**
  111. * struct ne_phys_contig_mem_regions - Contiguous physical memory regions.
  112. * @num: The number of regions that currently has.
  113. * @regions: The array of physical memory regions.
  114. */
  115. struct ne_phys_contig_mem_regions {
  116. unsigned long num;
  117. struct range *regions;
  118. };
  119. /**
  120. * ne_check_enclaves_created() - Verify if at least one enclave has been created.
  121. * @void: No parameters provided.
  122. *
  123. * Context: Process context.
  124. * Return:
  125. * * True if at least one enclave is created.
  126. * * False otherwise.
  127. */
  128. static bool ne_check_enclaves_created(void)
  129. {
  130. struct ne_pci_dev *ne_pci_dev = ne_devs.ne_pci_dev;
  131. bool ret = false;
  132. if (!ne_pci_dev)
  133. return ret;
  134. mutex_lock(&ne_pci_dev->enclaves_list_mutex);
  135. if (!list_empty(&ne_pci_dev->enclaves_list))
  136. ret = true;
  137. mutex_unlock(&ne_pci_dev->enclaves_list_mutex);
  138. return ret;
  139. }
  140. /**
  141. * ne_setup_cpu_pool() - Set the NE CPU pool after handling sanity checks such
  142. * as not sharing CPU cores with the primary / parent VM
  143. * or not using CPU 0, which should remain available for
  144. * the primary / parent VM. Offline the CPUs from the
  145. * pool after the checks passed.
  146. * @ne_cpu_list: The CPU list used for setting NE CPU pool.
  147. *
  148. * Context: Process context.
  149. * Return:
  150. * * 0 on success.
  151. * * Negative return value on failure.
  152. */
  153. static int ne_setup_cpu_pool(const char *ne_cpu_list)
  154. {
  155. int core_id = -1;
  156. unsigned int cpu = 0;
  157. cpumask_var_t cpu_pool;
  158. unsigned int cpu_sibling = 0;
  159. unsigned int i = 0;
  160. int numa_node = -1;
  161. int rc = -EINVAL;
  162. if (!zalloc_cpumask_var(&cpu_pool, GFP_KERNEL))
  163. return -ENOMEM;
  164. mutex_lock(&ne_cpu_pool.mutex);
  165. rc = cpulist_parse(ne_cpu_list, cpu_pool);
  166. if (rc < 0) {
  167. pr_err("%s: Error in cpulist parse [rc=%d]\n", ne_misc_dev.name, rc);
  168. goto free_pool_cpumask;
  169. }
  170. cpu = cpumask_any(cpu_pool);
  171. if (cpu >= nr_cpu_ids) {
  172. pr_err("%s: No CPUs available in CPU pool\n", ne_misc_dev.name);
  173. rc = -EINVAL;
  174. goto free_pool_cpumask;
  175. }
  176. /*
  177. * Check if the CPUs are online, to further get info about them
  178. * e.g. numa node, core id, siblings.
  179. */
  180. for_each_cpu(cpu, cpu_pool)
  181. if (cpu_is_offline(cpu)) {
  182. pr_err("%s: CPU %d is offline, has to be online to get its metadata\n",
  183. ne_misc_dev.name, cpu);
  184. rc = -EINVAL;
  185. goto free_pool_cpumask;
  186. }
  187. /*
  188. * Check if the CPUs from the NE CPU pool are from the same NUMA node.
  189. */
  190. for_each_cpu(cpu, cpu_pool)
  191. if (numa_node < 0) {
  192. numa_node = cpu_to_node(cpu);
  193. if (numa_node < 0) {
  194. pr_err("%s: Invalid NUMA node %d\n",
  195. ne_misc_dev.name, numa_node);
  196. rc = -EINVAL;
  197. goto free_pool_cpumask;
  198. }
  199. } else {
  200. if (numa_node != cpu_to_node(cpu)) {
  201. pr_err("%s: CPUs with different NUMA nodes\n",
  202. ne_misc_dev.name);
  203. rc = -EINVAL;
  204. goto free_pool_cpumask;
  205. }
  206. }
  207. /*
  208. * Check if CPU 0 and its siblings are included in the provided CPU pool
  209. * They should remain available for the primary / parent VM.
  210. */
  211. if (cpumask_test_cpu(0, cpu_pool)) {
  212. pr_err("%s: CPU 0 has to remain available\n", ne_misc_dev.name);
  213. rc = -EINVAL;
  214. goto free_pool_cpumask;
  215. }
  216. for_each_cpu(cpu_sibling, topology_sibling_cpumask(0)) {
  217. if (cpumask_test_cpu(cpu_sibling, cpu_pool)) {
  218. pr_err("%s: CPU sibling %d for CPU 0 is in CPU pool\n",
  219. ne_misc_dev.name, cpu_sibling);
  220. rc = -EINVAL;
  221. goto free_pool_cpumask;
  222. }
  223. }
  224. /*
  225. * Check if CPU siblings are included in the provided CPU pool. The
  226. * expectation is that full CPU cores are made available in the CPU pool
  227. * for enclaves.
  228. */
  229. for_each_cpu(cpu, cpu_pool) {
  230. for_each_cpu(cpu_sibling, topology_sibling_cpumask(cpu)) {
  231. if (!cpumask_test_cpu(cpu_sibling, cpu_pool)) {
  232. pr_err("%s: CPU %d is not in CPU pool\n",
  233. ne_misc_dev.name, cpu_sibling);
  234. rc = -EINVAL;
  235. goto free_pool_cpumask;
  236. }
  237. }
  238. }
  239. /* Calculate the number of threads from a full CPU core. */
  240. cpu = cpumask_any(cpu_pool);
  241. for_each_cpu(cpu_sibling, topology_sibling_cpumask(cpu))
  242. ne_cpu_pool.nr_threads_per_core++;
  243. ne_cpu_pool.nr_parent_vm_cores = nr_cpu_ids / ne_cpu_pool.nr_threads_per_core;
  244. ne_cpu_pool.avail_threads_per_core = kzalloc_objs(*ne_cpu_pool.avail_threads_per_core,
  245. ne_cpu_pool.nr_parent_vm_cores);
  246. if (!ne_cpu_pool.avail_threads_per_core) {
  247. rc = -ENOMEM;
  248. goto free_pool_cpumask;
  249. }
  250. for (i = 0; i < ne_cpu_pool.nr_parent_vm_cores; i++)
  251. if (!zalloc_cpumask_var(&ne_cpu_pool.avail_threads_per_core[i], GFP_KERNEL)) {
  252. rc = -ENOMEM;
  253. goto free_cores_cpumask;
  254. }
  255. /*
  256. * Split the NE CPU pool in threads per core to keep the CPU topology
  257. * after offlining the CPUs.
  258. */
  259. for_each_cpu(cpu, cpu_pool) {
  260. core_id = topology_core_id(cpu);
  261. if (core_id < 0 || core_id >= ne_cpu_pool.nr_parent_vm_cores) {
  262. pr_err("%s: Invalid core id %d for CPU %d\n",
  263. ne_misc_dev.name, core_id, cpu);
  264. rc = -EINVAL;
  265. goto clear_cpumask;
  266. }
  267. cpumask_set_cpu(cpu, ne_cpu_pool.avail_threads_per_core[core_id]);
  268. }
  269. /*
  270. * CPUs that are given to enclave(s) should not be considered online
  271. * by Linux anymore, as the hypervisor will degrade them to floating.
  272. * The physical CPUs (full cores) are carved out of the primary / parent
  273. * VM and given to the enclave VM. The same number of vCPUs would run
  274. * on less pCPUs for the primary / parent VM.
  275. *
  276. * We offline them here, to not degrade performance and expose correct
  277. * topology to Linux and user space.
  278. */
  279. for_each_cpu(cpu, cpu_pool) {
  280. rc = remove_cpu(cpu);
  281. if (rc != 0) {
  282. pr_err("%s: CPU %d is not offlined [rc=%d]\n",
  283. ne_misc_dev.name, cpu, rc);
  284. goto online_cpus;
  285. }
  286. }
  287. free_cpumask_var(cpu_pool);
  288. ne_cpu_pool.numa_node = numa_node;
  289. mutex_unlock(&ne_cpu_pool.mutex);
  290. return 0;
  291. online_cpus:
  292. for_each_cpu(cpu, cpu_pool)
  293. add_cpu(cpu);
  294. clear_cpumask:
  295. for (i = 0; i < ne_cpu_pool.nr_parent_vm_cores; i++)
  296. cpumask_clear(ne_cpu_pool.avail_threads_per_core[i]);
  297. free_cores_cpumask:
  298. for (i = 0; i < ne_cpu_pool.nr_parent_vm_cores; i++)
  299. free_cpumask_var(ne_cpu_pool.avail_threads_per_core[i]);
  300. kfree(ne_cpu_pool.avail_threads_per_core);
  301. free_pool_cpumask:
  302. free_cpumask_var(cpu_pool);
  303. ne_cpu_pool.nr_parent_vm_cores = 0;
  304. ne_cpu_pool.nr_threads_per_core = 0;
  305. ne_cpu_pool.numa_node = -1;
  306. mutex_unlock(&ne_cpu_pool.mutex);
  307. return rc;
  308. }
  309. /**
  310. * ne_teardown_cpu_pool() - Online the CPUs from the NE CPU pool and cleanup the
  311. * CPU pool.
  312. * @void: No parameters provided.
  313. *
  314. * Context: Process context.
  315. */
  316. static void ne_teardown_cpu_pool(void)
  317. {
  318. unsigned int cpu = 0;
  319. unsigned int i = 0;
  320. int rc = -EINVAL;
  321. mutex_lock(&ne_cpu_pool.mutex);
  322. if (!ne_cpu_pool.nr_parent_vm_cores) {
  323. mutex_unlock(&ne_cpu_pool.mutex);
  324. return;
  325. }
  326. for (i = 0; i < ne_cpu_pool.nr_parent_vm_cores; i++) {
  327. for_each_cpu(cpu, ne_cpu_pool.avail_threads_per_core[i]) {
  328. rc = add_cpu(cpu);
  329. if (rc != 0)
  330. pr_err("%s: CPU %d is not onlined [rc=%d]\n",
  331. ne_misc_dev.name, cpu, rc);
  332. }
  333. cpumask_clear(ne_cpu_pool.avail_threads_per_core[i]);
  334. free_cpumask_var(ne_cpu_pool.avail_threads_per_core[i]);
  335. }
  336. kfree(ne_cpu_pool.avail_threads_per_core);
  337. ne_cpu_pool.nr_parent_vm_cores = 0;
  338. ne_cpu_pool.nr_threads_per_core = 0;
  339. ne_cpu_pool.numa_node = -1;
  340. mutex_unlock(&ne_cpu_pool.mutex);
  341. }
  342. /**
  343. * ne_set_kernel_param() - Set the NE CPU pool value via the NE kernel parameter.
  344. * @val: NE CPU pool string value.
  345. * @kp : NE kernel parameter associated with the NE CPU pool.
  346. *
  347. * Context: Process context.
  348. * Return:
  349. * * 0 on success.
  350. * * Negative return value on failure.
  351. */
  352. static int ne_set_kernel_param(const char *val, const struct kernel_param *kp)
  353. {
  354. char error_val[] = "";
  355. int rc = -EINVAL;
  356. if (!capable(CAP_SYS_ADMIN))
  357. return -EPERM;
  358. if (ne_check_enclaves_created()) {
  359. pr_err("%s: The CPU pool is used by enclave(s)\n", ne_misc_dev.name);
  360. return -EPERM;
  361. }
  362. ne_teardown_cpu_pool();
  363. rc = ne_setup_cpu_pool(val);
  364. if (rc < 0) {
  365. pr_err("%s: Error in setup CPU pool [rc=%d]\n", ne_misc_dev.name, rc);
  366. param_set_copystring(error_val, kp);
  367. return rc;
  368. }
  369. rc = param_set_copystring(val, kp);
  370. if (rc < 0) {
  371. pr_err("%s: Error in param set copystring [rc=%d]\n", ne_misc_dev.name, rc);
  372. ne_teardown_cpu_pool();
  373. param_set_copystring(error_val, kp);
  374. return rc;
  375. }
  376. return 0;
  377. }
  378. /**
  379. * ne_donated_cpu() - Check if the provided CPU is already used by the enclave.
  380. * @ne_enclave : Private data associated with the current enclave.
  381. * @cpu: CPU to check if already used.
  382. *
  383. * Context: Process context. This function is called with the ne_enclave mutex held.
  384. * Return:
  385. * * True if the provided CPU is already used by the enclave.
  386. * * False otherwise.
  387. */
  388. static bool ne_donated_cpu(struct ne_enclave *ne_enclave, unsigned int cpu)
  389. {
  390. if (cpumask_test_cpu(cpu, ne_enclave->vcpu_ids))
  391. return true;
  392. return false;
  393. }
  394. /**
  395. * ne_get_unused_core_from_cpu_pool() - Get the id of a full core from the
  396. * NE CPU pool.
  397. * @void: No parameters provided.
  398. *
  399. * Context: Process context. This function is called with the ne_enclave and
  400. * ne_cpu_pool mutexes held.
  401. * Return:
  402. * * Core id.
  403. * * -1 if no CPU core available in the pool.
  404. */
  405. static int ne_get_unused_core_from_cpu_pool(void)
  406. {
  407. int core_id = -1;
  408. unsigned int i = 0;
  409. for (i = 0; i < ne_cpu_pool.nr_parent_vm_cores; i++)
  410. if (!cpumask_empty(ne_cpu_pool.avail_threads_per_core[i])) {
  411. core_id = i;
  412. break;
  413. }
  414. return core_id;
  415. }
  416. /**
  417. * ne_set_enclave_threads_per_core() - Set the threads of the provided core in
  418. * the enclave data structure.
  419. * @ne_enclave : Private data associated with the current enclave.
  420. * @core_id: Core id to get its threads from the NE CPU pool.
  421. * @vcpu_id: vCPU id part of the provided core.
  422. *
  423. * Context: Process context. This function is called with the ne_enclave and
  424. * ne_cpu_pool mutexes held.
  425. * Return:
  426. * * 0 on success.
  427. * * Negative return value on failure.
  428. */
  429. static int ne_set_enclave_threads_per_core(struct ne_enclave *ne_enclave,
  430. int core_id, u32 vcpu_id)
  431. {
  432. unsigned int cpu = 0;
  433. if (core_id < 0 && vcpu_id == 0) {
  434. dev_err_ratelimited(ne_misc_dev.this_device,
  435. "No CPUs available in NE CPU pool\n");
  436. return -NE_ERR_NO_CPUS_AVAIL_IN_POOL;
  437. }
  438. if (core_id < 0) {
  439. dev_err_ratelimited(ne_misc_dev.this_device,
  440. "CPU %d is not in NE CPU pool\n", vcpu_id);
  441. return -NE_ERR_VCPU_NOT_IN_CPU_POOL;
  442. }
  443. if (core_id >= ne_enclave->nr_parent_vm_cores) {
  444. dev_err_ratelimited(ne_misc_dev.this_device,
  445. "Invalid core id %d - ne_enclave\n", core_id);
  446. return -NE_ERR_VCPU_INVALID_CPU_CORE;
  447. }
  448. for_each_cpu(cpu, ne_cpu_pool.avail_threads_per_core[core_id])
  449. cpumask_set_cpu(cpu, ne_enclave->threads_per_core[core_id]);
  450. cpumask_clear(ne_cpu_pool.avail_threads_per_core[core_id]);
  451. return 0;
  452. }
  453. /**
  454. * ne_get_cpu_from_cpu_pool() - Get a CPU from the NE CPU pool, either from the
  455. * remaining sibling(s) of a CPU core or the first
  456. * sibling of a new CPU core.
  457. * @ne_enclave : Private data associated with the current enclave.
  458. * @vcpu_id: vCPU to get from the NE CPU pool.
  459. *
  460. * Context: Process context. This function is called with the ne_enclave mutex held.
  461. * Return:
  462. * * 0 on success.
  463. * * Negative return value on failure.
  464. */
  465. static int ne_get_cpu_from_cpu_pool(struct ne_enclave *ne_enclave, u32 *vcpu_id)
  466. {
  467. int core_id = -1;
  468. unsigned int cpu = 0;
  469. unsigned int i = 0;
  470. int rc = -EINVAL;
  471. /*
  472. * If previously allocated a thread of a core to this enclave, first
  473. * check remaining sibling(s) for new CPU allocations, so that full
  474. * CPU cores are used for the enclave.
  475. */
  476. for (i = 0; i < ne_enclave->nr_parent_vm_cores; i++)
  477. for_each_cpu(cpu, ne_enclave->threads_per_core[i])
  478. if (!ne_donated_cpu(ne_enclave, cpu)) {
  479. *vcpu_id = cpu;
  480. return 0;
  481. }
  482. mutex_lock(&ne_cpu_pool.mutex);
  483. /*
  484. * If no remaining siblings, get a core from the NE CPU pool and keep
  485. * track of all the threads in the enclave threads per core data structure.
  486. */
  487. core_id = ne_get_unused_core_from_cpu_pool();
  488. rc = ne_set_enclave_threads_per_core(ne_enclave, core_id, *vcpu_id);
  489. if (rc < 0)
  490. goto unlock_mutex;
  491. *vcpu_id = cpumask_any(ne_enclave->threads_per_core[core_id]);
  492. rc = 0;
  493. unlock_mutex:
  494. mutex_unlock(&ne_cpu_pool.mutex);
  495. return rc;
  496. }
  497. /**
  498. * ne_get_vcpu_core_from_cpu_pool() - Get from the NE CPU pool the id of the
  499. * core associated with the provided vCPU.
  500. * @vcpu_id: Provided vCPU id to get its associated core id.
  501. *
  502. * Context: Process context. This function is called with the ne_enclave and
  503. * ne_cpu_pool mutexes held.
  504. * Return:
  505. * * Core id.
  506. * * -1 if the provided vCPU is not in the pool.
  507. */
  508. static int ne_get_vcpu_core_from_cpu_pool(u32 vcpu_id)
  509. {
  510. int core_id = -1;
  511. unsigned int i = 0;
  512. for (i = 0; i < ne_cpu_pool.nr_parent_vm_cores; i++)
  513. if (cpumask_test_cpu(vcpu_id, ne_cpu_pool.avail_threads_per_core[i])) {
  514. core_id = i;
  515. break;
  516. }
  517. return core_id;
  518. }
  519. /**
  520. * ne_check_cpu_in_cpu_pool() - Check if the given vCPU is in the available CPUs
  521. * from the pool.
  522. * @ne_enclave : Private data associated with the current enclave.
  523. * @vcpu_id: ID of the vCPU to check if available in the NE CPU pool.
  524. *
  525. * Context: Process context. This function is called with the ne_enclave mutex held.
  526. * Return:
  527. * * 0 on success.
  528. * * Negative return value on failure.
  529. */
  530. static int ne_check_cpu_in_cpu_pool(struct ne_enclave *ne_enclave, u32 vcpu_id)
  531. {
  532. int core_id = -1;
  533. unsigned int i = 0;
  534. int rc = -EINVAL;
  535. if (ne_donated_cpu(ne_enclave, vcpu_id)) {
  536. dev_err_ratelimited(ne_misc_dev.this_device,
  537. "CPU %d already used\n", vcpu_id);
  538. return -NE_ERR_VCPU_ALREADY_USED;
  539. }
  540. /*
  541. * If previously allocated a thread of a core to this enclave, but not
  542. * the full core, first check remaining sibling(s).
  543. */
  544. for (i = 0; i < ne_enclave->nr_parent_vm_cores; i++)
  545. if (cpumask_test_cpu(vcpu_id, ne_enclave->threads_per_core[i]))
  546. return 0;
  547. mutex_lock(&ne_cpu_pool.mutex);
  548. /*
  549. * If no remaining siblings, get from the NE CPU pool the core
  550. * associated with the vCPU and keep track of all the threads in the
  551. * enclave threads per core data structure.
  552. */
  553. core_id = ne_get_vcpu_core_from_cpu_pool(vcpu_id);
  554. rc = ne_set_enclave_threads_per_core(ne_enclave, core_id, vcpu_id);
  555. if (rc < 0)
  556. goto unlock_mutex;
  557. rc = 0;
  558. unlock_mutex:
  559. mutex_unlock(&ne_cpu_pool.mutex);
  560. return rc;
  561. }
  562. /**
  563. * ne_add_vcpu_ioctl() - Add a vCPU to the slot associated with the current
  564. * enclave.
  565. * @ne_enclave : Private data associated with the current enclave.
  566. * @vcpu_id: ID of the CPU to be associated with the given slot,
  567. * apic id on x86.
  568. *
  569. * Context: Process context. This function is called with the ne_enclave mutex held.
  570. * Return:
  571. * * 0 on success.
  572. * * Negative return value on failure.
  573. */
  574. static int ne_add_vcpu_ioctl(struct ne_enclave *ne_enclave, u32 vcpu_id)
  575. {
  576. struct ne_pci_dev_cmd_reply cmd_reply = {};
  577. struct pci_dev *pdev = ne_devs.ne_pci_dev->pdev;
  578. int rc = -EINVAL;
  579. struct slot_add_vcpu_req slot_add_vcpu_req = {};
  580. if (ne_enclave->mm != current->mm)
  581. return -EIO;
  582. slot_add_vcpu_req.slot_uid = ne_enclave->slot_uid;
  583. slot_add_vcpu_req.vcpu_id = vcpu_id;
  584. rc = ne_do_request(pdev, SLOT_ADD_VCPU,
  585. &slot_add_vcpu_req, sizeof(slot_add_vcpu_req),
  586. &cmd_reply, sizeof(cmd_reply));
  587. if (rc < 0) {
  588. dev_err_ratelimited(ne_misc_dev.this_device,
  589. "Error in slot add vCPU [rc=%d]\n", rc);
  590. return rc;
  591. }
  592. cpumask_set_cpu(vcpu_id, ne_enclave->vcpu_ids);
  593. ne_enclave->nr_vcpus++;
  594. return 0;
  595. }
  596. /**
  597. * ne_sanity_check_user_mem_region() - Sanity check the user space memory
  598. * region received during the set user
  599. * memory region ioctl call.
  600. * @ne_enclave : Private data associated with the current enclave.
  601. * @mem_region : User space memory region to be sanity checked.
  602. *
  603. * Context: Process context. This function is called with the ne_enclave mutex held.
  604. * Return:
  605. * * 0 on success.
  606. * * Negative return value on failure.
  607. */
  608. static int ne_sanity_check_user_mem_region(struct ne_enclave *ne_enclave,
  609. struct ne_user_memory_region mem_region)
  610. {
  611. struct ne_mem_region *ne_mem_region = NULL;
  612. if (ne_enclave->mm != current->mm)
  613. return -EIO;
  614. if (mem_region.memory_size & (NE_MIN_MEM_REGION_SIZE - 1)) {
  615. dev_err_ratelimited(ne_misc_dev.this_device,
  616. "User space memory size is not multiple of 2 MiB\n");
  617. return -NE_ERR_INVALID_MEM_REGION_SIZE;
  618. }
  619. if (!IS_ALIGNED(mem_region.userspace_addr, NE_MIN_MEM_REGION_SIZE)) {
  620. dev_err_ratelimited(ne_misc_dev.this_device,
  621. "User space address is not 2 MiB aligned\n");
  622. return -NE_ERR_UNALIGNED_MEM_REGION_ADDR;
  623. }
  624. if ((mem_region.userspace_addr & (NE_MIN_MEM_REGION_SIZE - 1)) ||
  625. !access_ok((void __user *)(unsigned long)mem_region.userspace_addr,
  626. mem_region.memory_size)) {
  627. dev_err_ratelimited(ne_misc_dev.this_device,
  628. "Invalid user space address range\n");
  629. return -NE_ERR_INVALID_MEM_REGION_ADDR;
  630. }
  631. list_for_each_entry(ne_mem_region, &ne_enclave->mem_regions_list,
  632. mem_region_list_entry) {
  633. u64 memory_size = ne_mem_region->memory_size;
  634. u64 userspace_addr = ne_mem_region->userspace_addr;
  635. if ((userspace_addr <= mem_region.userspace_addr &&
  636. mem_region.userspace_addr < (userspace_addr + memory_size)) ||
  637. (mem_region.userspace_addr <= userspace_addr &&
  638. (mem_region.userspace_addr + mem_region.memory_size) > userspace_addr)) {
  639. dev_err_ratelimited(ne_misc_dev.this_device,
  640. "User space memory region already used\n");
  641. return -NE_ERR_MEM_REGION_ALREADY_USED;
  642. }
  643. }
  644. return 0;
  645. }
  646. /**
  647. * ne_sanity_check_user_mem_region_page() - Sanity check a page from the user space
  648. * memory region received during the set
  649. * user memory region ioctl call.
  650. * @ne_enclave : Private data associated with the current enclave.
  651. * @mem_region_page: Page from the user space memory region to be sanity checked.
  652. *
  653. * Context: Process context. This function is called with the ne_enclave mutex held.
  654. * Return:
  655. * * 0 on success.
  656. * * Negative return value on failure.
  657. */
  658. static int ne_sanity_check_user_mem_region_page(struct ne_enclave *ne_enclave,
  659. struct page *mem_region_page)
  660. {
  661. if (!PageHuge(mem_region_page)) {
  662. dev_err_ratelimited(ne_misc_dev.this_device,
  663. "Not a hugetlbfs page\n");
  664. return -NE_ERR_MEM_NOT_HUGE_PAGE;
  665. }
  666. if (page_size(mem_region_page) & (NE_MIN_MEM_REGION_SIZE - 1)) {
  667. dev_err_ratelimited(ne_misc_dev.this_device,
  668. "Page size not multiple of 2 MiB\n");
  669. return -NE_ERR_INVALID_PAGE_SIZE;
  670. }
  671. if (ne_enclave->numa_node != page_to_nid(mem_region_page)) {
  672. dev_err_ratelimited(ne_misc_dev.this_device,
  673. "Page is not from NUMA node %d\n",
  674. ne_enclave->numa_node);
  675. return -NE_ERR_MEM_DIFFERENT_NUMA_NODE;
  676. }
  677. return 0;
  678. }
  679. /**
  680. * ne_sanity_check_phys_mem_region() - Sanity check the start address and the size
  681. * of a physical memory region.
  682. * @phys_mem_region_paddr : Physical start address of the region to be sanity checked.
  683. * @phys_mem_region_size : Length of the region to be sanity checked.
  684. *
  685. * Context: Process context. This function is called with the ne_enclave mutex held.
  686. * Return:
  687. * * 0 on success.
  688. * * Negative return value on failure.
  689. */
  690. static int ne_sanity_check_phys_mem_region(u64 phys_mem_region_paddr,
  691. u64 phys_mem_region_size)
  692. {
  693. if (phys_mem_region_size & (NE_MIN_MEM_REGION_SIZE - 1)) {
  694. dev_err_ratelimited(ne_misc_dev.this_device,
  695. "Physical mem region size is not multiple of 2 MiB\n");
  696. return -EINVAL;
  697. }
  698. if (!IS_ALIGNED(phys_mem_region_paddr, NE_MIN_MEM_REGION_SIZE)) {
  699. dev_err_ratelimited(ne_misc_dev.this_device,
  700. "Physical mem region address is not 2 MiB aligned\n");
  701. return -EINVAL;
  702. }
  703. return 0;
  704. }
  705. /**
  706. * ne_merge_phys_contig_memory_regions() - Add a memory region and merge the adjacent
  707. * regions if they are physically contiguous.
  708. * @phys_contig_regions : Private data associated with the contiguous physical memory regions.
  709. * @page_paddr : Physical start address of the region to be added.
  710. * @page_size : Length of the region to be added.
  711. *
  712. * Context: Process context. This function is called with the ne_enclave mutex held.
  713. * Return:
  714. * * 0 on success.
  715. * * Negative return value on failure.
  716. */
  717. static int
  718. ne_merge_phys_contig_memory_regions(struct ne_phys_contig_mem_regions *phys_contig_regions,
  719. u64 page_paddr, u64 page_size)
  720. {
  721. unsigned long num = phys_contig_regions->num;
  722. int rc = 0;
  723. rc = ne_sanity_check_phys_mem_region(page_paddr, page_size);
  724. if (rc < 0)
  725. return rc;
  726. /* Physically contiguous, just merge */
  727. if (num && (phys_contig_regions->regions[num - 1].end + 1) == page_paddr) {
  728. phys_contig_regions->regions[num - 1].end += page_size;
  729. } else {
  730. phys_contig_regions->regions[num].start = page_paddr;
  731. phys_contig_regions->regions[num].end = page_paddr + page_size - 1;
  732. phys_contig_regions->num++;
  733. }
  734. return 0;
  735. }
  736. /**
  737. * ne_set_user_memory_region_ioctl() - Add user space memory region to the slot
  738. * associated with the current enclave.
  739. * @ne_enclave : Private data associated with the current enclave.
  740. * @mem_region : User space memory region to be associated with the given slot.
  741. *
  742. * Context: Process context. This function is called with the ne_enclave mutex held.
  743. * Return:
  744. * * 0 on success.
  745. * * Negative return value on failure.
  746. */
  747. static int ne_set_user_memory_region_ioctl(struct ne_enclave *ne_enclave,
  748. struct ne_user_memory_region mem_region)
  749. {
  750. long gup_rc = 0;
  751. unsigned long i = 0;
  752. unsigned long max_nr_pages = 0;
  753. unsigned long memory_size = 0;
  754. struct ne_mem_region *ne_mem_region = NULL;
  755. struct pci_dev *pdev = ne_devs.ne_pci_dev->pdev;
  756. struct ne_phys_contig_mem_regions phys_contig_mem_regions = {};
  757. int rc = -EINVAL;
  758. rc = ne_sanity_check_user_mem_region(ne_enclave, mem_region);
  759. if (rc < 0)
  760. return rc;
  761. ne_mem_region = kzalloc_obj(*ne_mem_region);
  762. if (!ne_mem_region)
  763. return -ENOMEM;
  764. max_nr_pages = mem_region.memory_size / NE_MIN_MEM_REGION_SIZE;
  765. ne_mem_region->pages = kzalloc_objs(*ne_mem_region->pages, max_nr_pages);
  766. if (!ne_mem_region->pages) {
  767. rc = -ENOMEM;
  768. goto free_mem_region;
  769. }
  770. phys_contig_mem_regions.regions = kzalloc_objs(*phys_contig_mem_regions.regions,
  771. max_nr_pages);
  772. if (!phys_contig_mem_regions.regions) {
  773. rc = -ENOMEM;
  774. goto free_mem_region;
  775. }
  776. do {
  777. i = ne_mem_region->nr_pages;
  778. if (i == max_nr_pages) {
  779. dev_err_ratelimited(ne_misc_dev.this_device,
  780. "Reached max nr of pages in the pages data struct\n");
  781. rc = -ENOMEM;
  782. goto put_pages;
  783. }
  784. gup_rc = get_user_pages_unlocked(mem_region.userspace_addr + memory_size, 1,
  785. ne_mem_region->pages + i, FOLL_GET);
  786. if (gup_rc < 0) {
  787. rc = gup_rc;
  788. dev_err_ratelimited(ne_misc_dev.this_device,
  789. "Error in get user pages [rc=%d]\n", rc);
  790. goto put_pages;
  791. }
  792. rc = ne_sanity_check_user_mem_region_page(ne_enclave, ne_mem_region->pages[i]);
  793. if (rc < 0)
  794. goto put_pages;
  795. rc = ne_merge_phys_contig_memory_regions(&phys_contig_mem_regions,
  796. page_to_phys(ne_mem_region->pages[i]),
  797. page_size(ne_mem_region->pages[i]));
  798. if (rc < 0)
  799. goto put_pages;
  800. memory_size += page_size(ne_mem_region->pages[i]);
  801. ne_mem_region->nr_pages++;
  802. } while (memory_size < mem_region.memory_size);
  803. if ((ne_enclave->nr_mem_regions + phys_contig_mem_regions.num) >
  804. ne_enclave->max_mem_regions) {
  805. dev_err_ratelimited(ne_misc_dev.this_device,
  806. "Reached max memory regions %lld\n",
  807. ne_enclave->max_mem_regions);
  808. rc = -NE_ERR_MEM_MAX_REGIONS;
  809. goto put_pages;
  810. }
  811. for (i = 0; i < phys_contig_mem_regions.num; i++) {
  812. u64 phys_region_addr = phys_contig_mem_regions.regions[i].start;
  813. u64 phys_region_size = range_len(&phys_contig_mem_regions.regions[i]);
  814. rc = ne_sanity_check_phys_mem_region(phys_region_addr, phys_region_size);
  815. if (rc < 0)
  816. goto put_pages;
  817. }
  818. ne_mem_region->memory_size = mem_region.memory_size;
  819. ne_mem_region->userspace_addr = mem_region.userspace_addr;
  820. list_add(&ne_mem_region->mem_region_list_entry, &ne_enclave->mem_regions_list);
  821. for (i = 0; i < phys_contig_mem_regions.num; i++) {
  822. struct ne_pci_dev_cmd_reply cmd_reply = {};
  823. struct slot_add_mem_req slot_add_mem_req = {};
  824. slot_add_mem_req.slot_uid = ne_enclave->slot_uid;
  825. slot_add_mem_req.paddr = phys_contig_mem_regions.regions[i].start;
  826. slot_add_mem_req.size = range_len(&phys_contig_mem_regions.regions[i]);
  827. rc = ne_do_request(pdev, SLOT_ADD_MEM,
  828. &slot_add_mem_req, sizeof(slot_add_mem_req),
  829. &cmd_reply, sizeof(cmd_reply));
  830. if (rc < 0) {
  831. dev_err_ratelimited(ne_misc_dev.this_device,
  832. "Error in slot add mem [rc=%d]\n", rc);
  833. kfree(phys_contig_mem_regions.regions);
  834. /*
  835. * Exit here without put pages as memory regions may
  836. * already been added.
  837. */
  838. return rc;
  839. }
  840. ne_enclave->mem_size += slot_add_mem_req.size;
  841. ne_enclave->nr_mem_regions++;
  842. }
  843. kfree(phys_contig_mem_regions.regions);
  844. return 0;
  845. put_pages:
  846. for (i = 0; i < ne_mem_region->nr_pages; i++)
  847. put_page(ne_mem_region->pages[i]);
  848. free_mem_region:
  849. kfree(phys_contig_mem_regions.regions);
  850. kfree(ne_mem_region->pages);
  851. kfree(ne_mem_region);
  852. return rc;
  853. }
  854. /**
  855. * ne_start_enclave_ioctl() - Trigger enclave start after the enclave resources,
  856. * such as memory and CPU, have been set.
  857. * @ne_enclave : Private data associated with the current enclave.
  858. * @enclave_start_info : Enclave info that includes enclave cid and flags.
  859. *
  860. * Context: Process context. This function is called with the ne_enclave mutex held.
  861. * Return:
  862. * * 0 on success.
  863. * * Negative return value on failure.
  864. */
  865. static int ne_start_enclave_ioctl(struct ne_enclave *ne_enclave,
  866. struct ne_enclave_start_info *enclave_start_info)
  867. {
  868. struct ne_pci_dev_cmd_reply cmd_reply = {};
  869. unsigned int cpu = 0;
  870. struct enclave_start_req enclave_start_req = {};
  871. unsigned int i = 0;
  872. struct pci_dev *pdev = ne_devs.ne_pci_dev->pdev;
  873. int rc = -EINVAL;
  874. if (!ne_enclave->nr_mem_regions) {
  875. dev_err_ratelimited(ne_misc_dev.this_device,
  876. "Enclave has no mem regions\n");
  877. return -NE_ERR_NO_MEM_REGIONS_ADDED;
  878. }
  879. if (ne_enclave->mem_size < NE_MIN_ENCLAVE_MEM_SIZE) {
  880. dev_err_ratelimited(ne_misc_dev.this_device,
  881. "Enclave memory is less than %ld\n",
  882. NE_MIN_ENCLAVE_MEM_SIZE);
  883. return -NE_ERR_ENCLAVE_MEM_MIN_SIZE;
  884. }
  885. if (!ne_enclave->nr_vcpus) {
  886. dev_err_ratelimited(ne_misc_dev.this_device,
  887. "Enclave has no vCPUs\n");
  888. return -NE_ERR_NO_VCPUS_ADDED;
  889. }
  890. for (i = 0; i < ne_enclave->nr_parent_vm_cores; i++)
  891. for_each_cpu(cpu, ne_enclave->threads_per_core[i])
  892. if (!cpumask_test_cpu(cpu, ne_enclave->vcpu_ids)) {
  893. dev_err_ratelimited(ne_misc_dev.this_device,
  894. "Full CPU cores not used\n");
  895. return -NE_ERR_FULL_CORES_NOT_USED;
  896. }
  897. enclave_start_req.enclave_cid = enclave_start_info->enclave_cid;
  898. enclave_start_req.flags = enclave_start_info->flags;
  899. enclave_start_req.slot_uid = ne_enclave->slot_uid;
  900. rc = ne_do_request(pdev, ENCLAVE_START,
  901. &enclave_start_req, sizeof(enclave_start_req),
  902. &cmd_reply, sizeof(cmd_reply));
  903. if (rc < 0) {
  904. dev_err_ratelimited(ne_misc_dev.this_device,
  905. "Error in enclave start [rc=%d]\n", rc);
  906. return rc;
  907. }
  908. ne_enclave->state = NE_STATE_RUNNING;
  909. enclave_start_info->enclave_cid = cmd_reply.enclave_cid;
  910. return 0;
  911. }
  912. /**
  913. * ne_enclave_ioctl() - Ioctl function provided by the enclave file.
  914. * @file: File associated with this ioctl function.
  915. * @cmd: The command that is set for the ioctl call.
  916. * @arg: The argument that is provided for the ioctl call.
  917. *
  918. * Context: Process context.
  919. * Return:
  920. * * 0 on success.
  921. * * Negative return value on failure.
  922. */
  923. static long ne_enclave_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  924. {
  925. struct ne_enclave *ne_enclave = file->private_data;
  926. switch (cmd) {
  927. case NE_ADD_VCPU: {
  928. int rc = -EINVAL;
  929. u32 vcpu_id = 0;
  930. if (copy_from_user(&vcpu_id, (void __user *)arg, sizeof(vcpu_id)))
  931. return -EFAULT;
  932. mutex_lock(&ne_enclave->enclave_info_mutex);
  933. if (ne_enclave->state != NE_STATE_INIT) {
  934. dev_err_ratelimited(ne_misc_dev.this_device,
  935. "Enclave is not in init state\n");
  936. mutex_unlock(&ne_enclave->enclave_info_mutex);
  937. return -NE_ERR_NOT_IN_INIT_STATE;
  938. }
  939. if (vcpu_id >= (ne_enclave->nr_parent_vm_cores *
  940. ne_enclave->nr_threads_per_core)) {
  941. dev_err_ratelimited(ne_misc_dev.this_device,
  942. "vCPU id higher than max CPU id\n");
  943. mutex_unlock(&ne_enclave->enclave_info_mutex);
  944. return -NE_ERR_INVALID_VCPU;
  945. }
  946. if (!vcpu_id) {
  947. /* Use the CPU pool for choosing a CPU for the enclave. */
  948. rc = ne_get_cpu_from_cpu_pool(ne_enclave, &vcpu_id);
  949. if (rc < 0) {
  950. dev_err_ratelimited(ne_misc_dev.this_device,
  951. "Error in get CPU from pool [rc=%d]\n",
  952. rc);
  953. mutex_unlock(&ne_enclave->enclave_info_mutex);
  954. return rc;
  955. }
  956. } else {
  957. /* Check if the provided vCPU is available in the NE CPU pool. */
  958. rc = ne_check_cpu_in_cpu_pool(ne_enclave, vcpu_id);
  959. if (rc < 0) {
  960. dev_err_ratelimited(ne_misc_dev.this_device,
  961. "Error in check CPU %d in pool [rc=%d]\n",
  962. vcpu_id, rc);
  963. mutex_unlock(&ne_enclave->enclave_info_mutex);
  964. return rc;
  965. }
  966. }
  967. rc = ne_add_vcpu_ioctl(ne_enclave, vcpu_id);
  968. if (rc < 0) {
  969. mutex_unlock(&ne_enclave->enclave_info_mutex);
  970. return rc;
  971. }
  972. mutex_unlock(&ne_enclave->enclave_info_mutex);
  973. if (copy_to_user((void __user *)arg, &vcpu_id, sizeof(vcpu_id)))
  974. return -EFAULT;
  975. return 0;
  976. }
  977. case NE_GET_IMAGE_LOAD_INFO: {
  978. struct ne_image_load_info image_load_info = {};
  979. if (copy_from_user(&image_load_info, (void __user *)arg, sizeof(image_load_info)))
  980. return -EFAULT;
  981. mutex_lock(&ne_enclave->enclave_info_mutex);
  982. if (ne_enclave->state != NE_STATE_INIT) {
  983. dev_err_ratelimited(ne_misc_dev.this_device,
  984. "Enclave is not in init state\n");
  985. mutex_unlock(&ne_enclave->enclave_info_mutex);
  986. return -NE_ERR_NOT_IN_INIT_STATE;
  987. }
  988. mutex_unlock(&ne_enclave->enclave_info_mutex);
  989. if (!image_load_info.flags ||
  990. image_load_info.flags >= NE_IMAGE_LOAD_MAX_FLAG_VAL) {
  991. dev_err_ratelimited(ne_misc_dev.this_device,
  992. "Incorrect flag in enclave image load info\n");
  993. return -NE_ERR_INVALID_FLAG_VALUE;
  994. }
  995. if (image_load_info.flags == NE_EIF_IMAGE)
  996. image_load_info.memory_offset = NE_EIF_LOAD_OFFSET;
  997. if (copy_to_user((void __user *)arg, &image_load_info, sizeof(image_load_info)))
  998. return -EFAULT;
  999. return 0;
  1000. }
  1001. case NE_SET_USER_MEMORY_REGION: {
  1002. struct ne_user_memory_region mem_region = {};
  1003. int rc = -EINVAL;
  1004. if (copy_from_user(&mem_region, (void __user *)arg, sizeof(mem_region)))
  1005. return -EFAULT;
  1006. if (mem_region.flags >= NE_MEMORY_REGION_MAX_FLAG_VAL) {
  1007. dev_err_ratelimited(ne_misc_dev.this_device,
  1008. "Incorrect flag for user memory region\n");
  1009. return -NE_ERR_INVALID_FLAG_VALUE;
  1010. }
  1011. mutex_lock(&ne_enclave->enclave_info_mutex);
  1012. if (ne_enclave->state != NE_STATE_INIT) {
  1013. dev_err_ratelimited(ne_misc_dev.this_device,
  1014. "Enclave is not in init state\n");
  1015. mutex_unlock(&ne_enclave->enclave_info_mutex);
  1016. return -NE_ERR_NOT_IN_INIT_STATE;
  1017. }
  1018. rc = ne_set_user_memory_region_ioctl(ne_enclave, mem_region);
  1019. if (rc < 0) {
  1020. mutex_unlock(&ne_enclave->enclave_info_mutex);
  1021. return rc;
  1022. }
  1023. mutex_unlock(&ne_enclave->enclave_info_mutex);
  1024. return 0;
  1025. }
  1026. case NE_START_ENCLAVE: {
  1027. struct ne_enclave_start_info enclave_start_info = {};
  1028. int rc = -EINVAL;
  1029. if (copy_from_user(&enclave_start_info, (void __user *)arg,
  1030. sizeof(enclave_start_info)))
  1031. return -EFAULT;
  1032. if (enclave_start_info.flags >= NE_ENCLAVE_START_MAX_FLAG_VAL) {
  1033. dev_err_ratelimited(ne_misc_dev.this_device,
  1034. "Incorrect flag in enclave start info\n");
  1035. return -NE_ERR_INVALID_FLAG_VALUE;
  1036. }
  1037. /*
  1038. * Do not use well-known CIDs - 0, 1, 2 - for enclaves.
  1039. * VMADDR_CID_ANY = -1U
  1040. * VMADDR_CID_HYPERVISOR = 0
  1041. * VMADDR_CID_LOCAL = 1
  1042. * VMADDR_CID_HOST = 2
  1043. * Note: 0 is used as a placeholder to auto-generate an enclave CID.
  1044. * http://man7.org/linux/man-pages/man7/vsock.7.html
  1045. */
  1046. if (enclave_start_info.enclave_cid > 0 &&
  1047. enclave_start_info.enclave_cid <= VMADDR_CID_HOST) {
  1048. dev_err_ratelimited(ne_misc_dev.this_device,
  1049. "Well-known CID value, not to be used for enclaves\n");
  1050. return -NE_ERR_INVALID_ENCLAVE_CID;
  1051. }
  1052. if (enclave_start_info.enclave_cid == U32_MAX) {
  1053. dev_err_ratelimited(ne_misc_dev.this_device,
  1054. "Well-known CID value, not to be used for enclaves\n");
  1055. return -NE_ERR_INVALID_ENCLAVE_CID;
  1056. }
  1057. /*
  1058. * Do not use the CID of the primary / parent VM for enclaves.
  1059. */
  1060. if (enclave_start_info.enclave_cid == NE_PARENT_VM_CID) {
  1061. dev_err_ratelimited(ne_misc_dev.this_device,
  1062. "CID of the parent VM, not to be used for enclaves\n");
  1063. return -NE_ERR_INVALID_ENCLAVE_CID;
  1064. }
  1065. /* 64-bit CIDs are not yet supported for the vsock device. */
  1066. if (enclave_start_info.enclave_cid > U32_MAX) {
  1067. dev_err_ratelimited(ne_misc_dev.this_device,
  1068. "64-bit CIDs not yet supported for the vsock device\n");
  1069. return -NE_ERR_INVALID_ENCLAVE_CID;
  1070. }
  1071. mutex_lock(&ne_enclave->enclave_info_mutex);
  1072. if (ne_enclave->state != NE_STATE_INIT) {
  1073. dev_err_ratelimited(ne_misc_dev.this_device,
  1074. "Enclave is not in init state\n");
  1075. mutex_unlock(&ne_enclave->enclave_info_mutex);
  1076. return -NE_ERR_NOT_IN_INIT_STATE;
  1077. }
  1078. rc = ne_start_enclave_ioctl(ne_enclave, &enclave_start_info);
  1079. if (rc < 0) {
  1080. mutex_unlock(&ne_enclave->enclave_info_mutex);
  1081. return rc;
  1082. }
  1083. mutex_unlock(&ne_enclave->enclave_info_mutex);
  1084. if (copy_to_user((void __user *)arg, &enclave_start_info,
  1085. sizeof(enclave_start_info)))
  1086. return -EFAULT;
  1087. return 0;
  1088. }
  1089. default:
  1090. return -ENOTTY;
  1091. }
  1092. return 0;
  1093. }
  1094. /**
  1095. * ne_enclave_remove_all_mem_region_entries() - Remove all memory region entries
  1096. * from the enclave data structure.
  1097. * @ne_enclave : Private data associated with the current enclave.
  1098. *
  1099. * Context: Process context. This function is called with the ne_enclave mutex held.
  1100. */
  1101. static void ne_enclave_remove_all_mem_region_entries(struct ne_enclave *ne_enclave)
  1102. {
  1103. unsigned long i = 0;
  1104. struct ne_mem_region *ne_mem_region = NULL;
  1105. struct ne_mem_region *ne_mem_region_tmp = NULL;
  1106. list_for_each_entry_safe(ne_mem_region, ne_mem_region_tmp,
  1107. &ne_enclave->mem_regions_list,
  1108. mem_region_list_entry) {
  1109. list_del(&ne_mem_region->mem_region_list_entry);
  1110. for (i = 0; i < ne_mem_region->nr_pages; i++)
  1111. put_page(ne_mem_region->pages[i]);
  1112. kfree(ne_mem_region->pages);
  1113. kfree(ne_mem_region);
  1114. }
  1115. }
  1116. /**
  1117. * ne_enclave_remove_all_vcpu_id_entries() - Remove all vCPU id entries from
  1118. * the enclave data structure.
  1119. * @ne_enclave : Private data associated with the current enclave.
  1120. *
  1121. * Context: Process context. This function is called with the ne_enclave mutex held.
  1122. */
  1123. static void ne_enclave_remove_all_vcpu_id_entries(struct ne_enclave *ne_enclave)
  1124. {
  1125. unsigned int cpu = 0;
  1126. unsigned int i = 0;
  1127. mutex_lock(&ne_cpu_pool.mutex);
  1128. for (i = 0; i < ne_enclave->nr_parent_vm_cores; i++) {
  1129. for_each_cpu(cpu, ne_enclave->threads_per_core[i])
  1130. /* Update the available NE CPU pool. */
  1131. cpumask_set_cpu(cpu, ne_cpu_pool.avail_threads_per_core[i]);
  1132. free_cpumask_var(ne_enclave->threads_per_core[i]);
  1133. }
  1134. mutex_unlock(&ne_cpu_pool.mutex);
  1135. kfree(ne_enclave->threads_per_core);
  1136. free_cpumask_var(ne_enclave->vcpu_ids);
  1137. }
  1138. /**
  1139. * ne_pci_dev_remove_enclave_entry() - Remove the enclave entry from the data
  1140. * structure that is part of the NE PCI
  1141. * device private data.
  1142. * @ne_enclave : Private data associated with the current enclave.
  1143. * @ne_pci_dev : Private data associated with the PCI device.
  1144. *
  1145. * Context: Process context. This function is called with the ne_pci_dev enclave
  1146. * mutex held.
  1147. */
  1148. static void ne_pci_dev_remove_enclave_entry(struct ne_enclave *ne_enclave,
  1149. struct ne_pci_dev *ne_pci_dev)
  1150. {
  1151. struct ne_enclave *ne_enclave_entry = NULL;
  1152. struct ne_enclave *ne_enclave_entry_tmp = NULL;
  1153. list_for_each_entry_safe(ne_enclave_entry, ne_enclave_entry_tmp,
  1154. &ne_pci_dev->enclaves_list, enclave_list_entry) {
  1155. if (ne_enclave_entry->slot_uid == ne_enclave->slot_uid) {
  1156. list_del(&ne_enclave_entry->enclave_list_entry);
  1157. break;
  1158. }
  1159. }
  1160. }
  1161. /**
  1162. * ne_enclave_release() - Release function provided by the enclave file.
  1163. * @inode: Inode associated with this file release function.
  1164. * @file: File associated with this release function.
  1165. *
  1166. * Context: Process context.
  1167. * Return:
  1168. * * 0 on success.
  1169. * * Negative return value on failure.
  1170. */
  1171. static int ne_enclave_release(struct inode *inode, struct file *file)
  1172. {
  1173. struct ne_pci_dev_cmd_reply cmd_reply = {};
  1174. struct enclave_stop_req enclave_stop_request = {};
  1175. struct ne_enclave *ne_enclave = file->private_data;
  1176. struct ne_pci_dev *ne_pci_dev = ne_devs.ne_pci_dev;
  1177. struct pci_dev *pdev = ne_pci_dev->pdev;
  1178. int rc = -EINVAL;
  1179. struct slot_free_req slot_free_req = {};
  1180. if (!ne_enclave)
  1181. return 0;
  1182. /*
  1183. * Early exit in case there is an error in the enclave creation logic
  1184. * and fput() is called on the cleanup path.
  1185. */
  1186. if (!ne_enclave->slot_uid)
  1187. return 0;
  1188. /*
  1189. * Acquire the enclave list mutex before the enclave mutex
  1190. * in order to avoid deadlocks with @ref ne_event_work_handler.
  1191. */
  1192. mutex_lock(&ne_pci_dev->enclaves_list_mutex);
  1193. mutex_lock(&ne_enclave->enclave_info_mutex);
  1194. if (ne_enclave->state != NE_STATE_INIT && ne_enclave->state != NE_STATE_STOPPED) {
  1195. enclave_stop_request.slot_uid = ne_enclave->slot_uid;
  1196. rc = ne_do_request(pdev, ENCLAVE_STOP,
  1197. &enclave_stop_request, sizeof(enclave_stop_request),
  1198. &cmd_reply, sizeof(cmd_reply));
  1199. if (rc < 0) {
  1200. dev_err_ratelimited(ne_misc_dev.this_device,
  1201. "Error in enclave stop [rc=%d]\n", rc);
  1202. goto unlock_mutex;
  1203. }
  1204. memset(&cmd_reply, 0, sizeof(cmd_reply));
  1205. }
  1206. slot_free_req.slot_uid = ne_enclave->slot_uid;
  1207. rc = ne_do_request(pdev, SLOT_FREE,
  1208. &slot_free_req, sizeof(slot_free_req),
  1209. &cmd_reply, sizeof(cmd_reply));
  1210. if (rc < 0) {
  1211. dev_err_ratelimited(ne_misc_dev.this_device,
  1212. "Error in slot free [rc=%d]\n", rc);
  1213. goto unlock_mutex;
  1214. }
  1215. ne_pci_dev_remove_enclave_entry(ne_enclave, ne_pci_dev);
  1216. ne_enclave_remove_all_mem_region_entries(ne_enclave);
  1217. ne_enclave_remove_all_vcpu_id_entries(ne_enclave);
  1218. mutex_unlock(&ne_enclave->enclave_info_mutex);
  1219. mutex_unlock(&ne_pci_dev->enclaves_list_mutex);
  1220. kfree(ne_enclave);
  1221. return 0;
  1222. unlock_mutex:
  1223. mutex_unlock(&ne_enclave->enclave_info_mutex);
  1224. mutex_unlock(&ne_pci_dev->enclaves_list_mutex);
  1225. return rc;
  1226. }
  1227. /**
  1228. * ne_enclave_poll() - Poll functionality used for enclave out-of-band events.
  1229. * @file: File associated with this poll function.
  1230. * @wait: Poll table data structure.
  1231. *
  1232. * Context: Process context.
  1233. * Return:
  1234. * * Poll mask.
  1235. */
  1236. static __poll_t ne_enclave_poll(struct file *file, poll_table *wait)
  1237. {
  1238. __poll_t mask = 0;
  1239. struct ne_enclave *ne_enclave = file->private_data;
  1240. poll_wait(file, &ne_enclave->eventq, wait);
  1241. if (ne_enclave->has_event)
  1242. mask |= EPOLLHUP;
  1243. return mask;
  1244. }
  1245. static const struct file_operations ne_enclave_fops = {
  1246. .owner = THIS_MODULE,
  1247. .llseek = noop_llseek,
  1248. .poll = ne_enclave_poll,
  1249. .unlocked_ioctl = ne_enclave_ioctl,
  1250. .release = ne_enclave_release,
  1251. };
  1252. /**
  1253. * ne_create_vm_ioctl() - Alloc slot to be associated with an enclave. Create
  1254. * enclave file descriptor to be further used for enclave
  1255. * resources handling e.g. memory regions and CPUs.
  1256. * @ne_pci_dev : Private data associated with the PCI device.
  1257. * @slot_uid: User pointer to store the generated unique slot id
  1258. * associated with an enclave to.
  1259. *
  1260. * Context: Process context. This function is called with the ne_pci_dev enclave
  1261. * mutex held.
  1262. * Return:
  1263. * * Enclave fd on success.
  1264. * * Negative return value on failure.
  1265. */
  1266. static int ne_create_vm_ioctl(struct ne_pci_dev *ne_pci_dev, u64 __user *slot_uid)
  1267. {
  1268. struct ne_pci_dev_cmd_reply cmd_reply = {};
  1269. int enclave_fd = -1;
  1270. struct file *enclave_file = NULL;
  1271. unsigned int i = 0;
  1272. struct ne_enclave *ne_enclave = NULL;
  1273. struct pci_dev *pdev = ne_pci_dev->pdev;
  1274. int rc = -EINVAL;
  1275. struct slot_alloc_req slot_alloc_req = {};
  1276. mutex_lock(&ne_cpu_pool.mutex);
  1277. for (i = 0; i < ne_cpu_pool.nr_parent_vm_cores; i++)
  1278. if (!cpumask_empty(ne_cpu_pool.avail_threads_per_core[i]))
  1279. break;
  1280. if (i == ne_cpu_pool.nr_parent_vm_cores) {
  1281. dev_err_ratelimited(ne_misc_dev.this_device,
  1282. "No CPUs available in CPU pool\n");
  1283. mutex_unlock(&ne_cpu_pool.mutex);
  1284. return -NE_ERR_NO_CPUS_AVAIL_IN_POOL;
  1285. }
  1286. mutex_unlock(&ne_cpu_pool.mutex);
  1287. ne_enclave = kzalloc_obj(*ne_enclave);
  1288. if (!ne_enclave)
  1289. return -ENOMEM;
  1290. mutex_lock(&ne_cpu_pool.mutex);
  1291. ne_enclave->nr_parent_vm_cores = ne_cpu_pool.nr_parent_vm_cores;
  1292. ne_enclave->nr_threads_per_core = ne_cpu_pool.nr_threads_per_core;
  1293. ne_enclave->numa_node = ne_cpu_pool.numa_node;
  1294. mutex_unlock(&ne_cpu_pool.mutex);
  1295. ne_enclave->threads_per_core = kzalloc_objs(*ne_enclave->threads_per_core,
  1296. ne_enclave->nr_parent_vm_cores);
  1297. if (!ne_enclave->threads_per_core) {
  1298. rc = -ENOMEM;
  1299. goto free_ne_enclave;
  1300. }
  1301. for (i = 0; i < ne_enclave->nr_parent_vm_cores; i++)
  1302. if (!zalloc_cpumask_var(&ne_enclave->threads_per_core[i], GFP_KERNEL)) {
  1303. rc = -ENOMEM;
  1304. goto free_cpumask;
  1305. }
  1306. if (!zalloc_cpumask_var(&ne_enclave->vcpu_ids, GFP_KERNEL)) {
  1307. rc = -ENOMEM;
  1308. goto free_cpumask;
  1309. }
  1310. enclave_fd = get_unused_fd_flags(O_CLOEXEC);
  1311. if (enclave_fd < 0) {
  1312. rc = enclave_fd;
  1313. dev_err_ratelimited(ne_misc_dev.this_device,
  1314. "Error in getting unused fd [rc=%d]\n", rc);
  1315. goto free_cpumask;
  1316. }
  1317. enclave_file = anon_inode_getfile("ne-vm", &ne_enclave_fops, ne_enclave, O_RDWR);
  1318. if (IS_ERR(enclave_file)) {
  1319. rc = PTR_ERR(enclave_file);
  1320. dev_err_ratelimited(ne_misc_dev.this_device,
  1321. "Error in anon inode get file [rc=%d]\n", rc);
  1322. goto put_fd;
  1323. }
  1324. rc = ne_do_request(pdev, SLOT_ALLOC,
  1325. &slot_alloc_req, sizeof(slot_alloc_req),
  1326. &cmd_reply, sizeof(cmd_reply));
  1327. if (rc < 0) {
  1328. dev_err_ratelimited(ne_misc_dev.this_device,
  1329. "Error in slot alloc [rc=%d]\n", rc);
  1330. goto put_file;
  1331. }
  1332. init_waitqueue_head(&ne_enclave->eventq);
  1333. ne_enclave->has_event = false;
  1334. mutex_init(&ne_enclave->enclave_info_mutex);
  1335. ne_enclave->max_mem_regions = cmd_reply.mem_regions;
  1336. INIT_LIST_HEAD(&ne_enclave->mem_regions_list);
  1337. ne_enclave->mm = current->mm;
  1338. ne_enclave->slot_uid = cmd_reply.slot_uid;
  1339. ne_enclave->state = NE_STATE_INIT;
  1340. list_add(&ne_enclave->enclave_list_entry, &ne_pci_dev->enclaves_list);
  1341. if (copy_to_user(slot_uid, &ne_enclave->slot_uid, sizeof(ne_enclave->slot_uid))) {
  1342. /*
  1343. * As we're holding the only reference to 'enclave_file', fput()
  1344. * will call ne_enclave_release() which will do a proper cleanup
  1345. * of all so far allocated resources, leaving only the unused fd
  1346. * for us to free.
  1347. */
  1348. fput(enclave_file);
  1349. put_unused_fd(enclave_fd);
  1350. return -EFAULT;
  1351. }
  1352. fd_install(enclave_fd, enclave_file);
  1353. return enclave_fd;
  1354. put_file:
  1355. fput(enclave_file);
  1356. put_fd:
  1357. put_unused_fd(enclave_fd);
  1358. free_cpumask:
  1359. free_cpumask_var(ne_enclave->vcpu_ids);
  1360. for (i = 0; i < ne_enclave->nr_parent_vm_cores; i++)
  1361. free_cpumask_var(ne_enclave->threads_per_core[i]);
  1362. kfree(ne_enclave->threads_per_core);
  1363. free_ne_enclave:
  1364. kfree(ne_enclave);
  1365. return rc;
  1366. }
  1367. /**
  1368. * ne_ioctl() - Ioctl function provided by the NE misc device.
  1369. * @file: File associated with this ioctl function.
  1370. * @cmd: The command that is set for the ioctl call.
  1371. * @arg: The argument that is provided for the ioctl call.
  1372. *
  1373. * Context: Process context.
  1374. * Return:
  1375. * * Ioctl result (e.g. enclave file descriptor) on success.
  1376. * * Negative return value on failure.
  1377. */
  1378. static long ne_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  1379. {
  1380. switch (cmd) {
  1381. case NE_CREATE_VM: {
  1382. int enclave_fd = -1;
  1383. struct ne_pci_dev *ne_pci_dev = ne_devs.ne_pci_dev;
  1384. u64 __user *slot_uid = (void __user *)arg;
  1385. mutex_lock(&ne_pci_dev->enclaves_list_mutex);
  1386. enclave_fd = ne_create_vm_ioctl(ne_pci_dev, slot_uid);
  1387. mutex_unlock(&ne_pci_dev->enclaves_list_mutex);
  1388. return enclave_fd;
  1389. }
  1390. default:
  1391. return -ENOTTY;
  1392. }
  1393. return 0;
  1394. }
  1395. #if defined(CONFIG_NITRO_ENCLAVES_MISC_DEV_TEST)
  1396. #include "ne_misc_dev_test.c"
  1397. #endif
  1398. static int __init ne_init(void)
  1399. {
  1400. mutex_init(&ne_cpu_pool.mutex);
  1401. return pci_register_driver(&ne_pci_driver);
  1402. }
  1403. static void __exit ne_exit(void)
  1404. {
  1405. pci_unregister_driver(&ne_pci_driver);
  1406. ne_teardown_cpu_pool();
  1407. }
  1408. module_init(ne_init);
  1409. module_exit(ne_exit);
  1410. MODULE_AUTHOR("Amazon.com, Inc. or its affiliates");
  1411. MODULE_DESCRIPTION("Nitro Enclaves Driver");
  1412. MODULE_LICENSE("GPL v2");