ne_misc_dev.c 47 KB

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