compat_qcedev.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * QTI CE 32-bit compatibility syscall for 64-bit systems
  4. *
  5. * Copyright (c) 2014-2020, The Linux Foundation. All rights reserved.
  6. * Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/uaccess.h>
  11. #include "linux/qcedev.h"
  12. #include <linux/compat.h>
  13. #include "linux/compat_qcedev.h"
  14. static void *compat_alloc_user_space(int size){
  15. return NULL;
  16. }
  17. static int compat_get_qcedev_pmem_info(
  18. struct compat_qcedev_pmem_info __user *pmem32,
  19. struct qcedev_pmem_info __user *pmem)
  20. {
  21. compat_ulong_t offset;
  22. compat_int_t fd_src;
  23. compat_int_t fd_dst;
  24. int err, i;
  25. uint32_t len;
  26. err = get_user(fd_src, &pmem32->fd_src);
  27. err |= put_user(fd_src, &pmem->fd_src);
  28. for (i = 0; i < QCEDEV_MAX_BUFFERS; i++) {
  29. err |= get_user(offset, &pmem32->src[i].offset);
  30. err |= put_user(offset, &pmem->src[i].offset);
  31. err |= get_user(len, &pmem32->src[i].len);
  32. err |= put_user(len, &pmem->src[i].len);
  33. }
  34. err |= get_user(fd_dst, &pmem32->fd_dst);
  35. err |= put_user(fd_dst, &pmem->fd_dst);
  36. for (i = 0; i < QCEDEV_MAX_BUFFERS; i++) {
  37. err |= get_user(offset, &pmem32->dst[i].offset);
  38. err |= put_user(offset, &pmem->dst[i].offset);
  39. err |= get_user(len, &pmem32->dst[i].len);
  40. err |= put_user(len, &pmem->dst[i].len);
  41. }
  42. return err;
  43. }
  44. static int compat_put_qcedev_pmem_info(
  45. struct compat_qcedev_pmem_info __user *pmem32,
  46. struct qcedev_pmem_info __user *pmem)
  47. {
  48. compat_ulong_t offset;
  49. compat_int_t fd_src;
  50. compat_int_t fd_dst;
  51. int err, i;
  52. uint32_t len;
  53. err = get_user(fd_src, &pmem->fd_src);
  54. err |= put_user(fd_src, &pmem32->fd_src);
  55. for (i = 0; i < QCEDEV_MAX_BUFFERS; i++) {
  56. err |= get_user(offset, &pmem->src[i].offset);
  57. err |= put_user(offset, &pmem32->src[i].offset);
  58. err |= get_user(len, &pmem->src[i].len);
  59. err |= put_user(len, &pmem32->src[i].len);
  60. }
  61. err |= get_user(fd_dst, &pmem->fd_dst);
  62. err |= put_user(fd_dst, &pmem32->fd_dst);
  63. for (i = 0; i < QCEDEV_MAX_BUFFERS; i++) {
  64. err |= get_user(offset, &pmem->dst[i].offset);
  65. err |= put_user(offset, &pmem32->dst[i].offset);
  66. err |= get_user(len, &pmem->dst[i].len);
  67. err |= put_user(len, &pmem32->dst[i].len);
  68. }
  69. return err;
  70. }
  71. static int compat_get_qcedev_vbuf_info(
  72. struct compat_qcedev_vbuf_info __user *vbuf32,
  73. struct qcedev_vbuf_info __user *vbuf)
  74. {
  75. compat_uptr_t vaddr;
  76. int err = 0, i;
  77. uint32_t len;
  78. for (i = 0; i < QCEDEV_MAX_BUFFERS; i++) {
  79. err |= get_user(vaddr, &vbuf32->src[i].vaddr);
  80. err |= put_user(vaddr,
  81. (compat_uptr_t __user *)&vbuf->src[i].vaddr);
  82. err |= get_user(len, &vbuf32->src[i].len);
  83. err |= put_user(len, &vbuf->src[i].len);
  84. }
  85. for (i = 0; i < QCEDEV_MAX_BUFFERS; i++) {
  86. err |= get_user(vaddr, &vbuf32->dst[i].vaddr);
  87. err |= put_user(vaddr,
  88. (compat_uptr_t __user *)&vbuf->dst[i].vaddr);
  89. err |= get_user(len, &vbuf32->dst[i].len);
  90. err |= put_user(len, &vbuf->dst[i].len);
  91. }
  92. return err;
  93. }
  94. static int compat_put_qcedev_vbuf_info(
  95. struct compat_qcedev_vbuf_info __user *vbuf32,
  96. struct qcedev_vbuf_info __user *vbuf)
  97. {
  98. compat_uptr_t vaddr;
  99. int err = 0, i;
  100. uint32_t len;
  101. for (i = 0; i < QCEDEV_MAX_BUFFERS; i++) {
  102. err |= get_user(vaddr,
  103. (compat_uptr_t __user *)&vbuf->src[i].vaddr);
  104. err |= put_user(vaddr, &vbuf32->src[i].vaddr);
  105. err |= get_user(len, &vbuf->src[i].len);
  106. err |= put_user(len, &vbuf32->src[i].len);
  107. }
  108. for (i = 0; i < QCEDEV_MAX_BUFFERS; i++) {
  109. err |= get_user(vaddr,
  110. (compat_uptr_t __user *)&vbuf->dst[i].vaddr);
  111. err |= put_user(vaddr, &vbuf32->dst[i].vaddr);
  112. err |= get_user(len, &vbuf->dst[i].len);
  113. err |= put_user(len, &vbuf32->dst[i].len);
  114. }
  115. return err;
  116. }
  117. static int compat_get_qcedev_cipher_op_req(
  118. struct compat_qcedev_cipher_op_req __user *data32,
  119. struct qcedev_cipher_op_req __user *data)
  120. {
  121. enum qcedev_cipher_mode_enum mode;
  122. enum qcedev_cipher_alg_enum alg;
  123. compat_ulong_t byteoffset;
  124. enum qcedev_oper_enum op;
  125. compat_ulong_t data_len;
  126. compat_ulong_t encklen;
  127. compat_ulong_t entries;
  128. compat_ulong_t ivlen;
  129. uint8_t in_place_op;
  130. int err, i;
  131. uint8_t use_pmem;
  132. uint8_t enckey;
  133. uint8_t iv;
  134. err = get_user(use_pmem, &data32->use_pmem);
  135. err |= put_user(use_pmem, &data->use_pmem);
  136. if (use_pmem)
  137. err |= compat_get_qcedev_pmem_info(&data32->pmem, &data->pmem);
  138. else
  139. err |= compat_get_qcedev_vbuf_info(&data32->vbuf, &data->vbuf);
  140. err |= get_user(entries, &data32->entries);
  141. err |= put_user(entries, &data->entries);
  142. err |= get_user(data_len, &data32->data_len);
  143. err |= put_user(data_len, &data->data_len);
  144. err |= get_user(in_place_op, &data32->in_place_op);
  145. err |= put_user(in_place_op, &data->in_place_op);
  146. for (i = 0; i < QCEDEV_MAX_KEY_SIZE; i++) {
  147. err |= get_user(enckey, &(data32->enckey[i]));
  148. err |= put_user(enckey, &(data->enckey[i]));
  149. }
  150. err |= get_user(encklen, &data32->encklen);
  151. err |= put_user(encklen, &data->encklen);
  152. for (i = 0; i < QCEDEV_MAX_IV_SIZE; i++) {
  153. err |= get_user(iv, &(data32->iv[i]));
  154. err |= put_user(iv, &(data->iv[i]));
  155. }
  156. err |= get_user(ivlen, &data32->ivlen);
  157. err |= put_user(ivlen, &data->ivlen);
  158. err |= get_user(byteoffset, &data32->byteoffset);
  159. err |= put_user(byteoffset, &data->byteoffset);
  160. err |= get_user(alg, &data32->alg);
  161. err |= put_user(alg, &data->alg);
  162. err |= get_user(mode, &data32->mode);
  163. err |= put_user(mode, &data->mode);
  164. err |= get_user(op, &data32->op);
  165. err |= put_user(op, &data->op);
  166. return err;
  167. }
  168. static int compat_put_qcedev_cipher_op_req(
  169. struct compat_qcedev_cipher_op_req __user *data32,
  170. struct qcedev_cipher_op_req __user *data)
  171. {
  172. enum qcedev_cipher_mode_enum mode;
  173. enum qcedev_cipher_alg_enum alg;
  174. compat_ulong_t byteoffset;
  175. enum qcedev_oper_enum op;
  176. compat_ulong_t data_len;
  177. compat_ulong_t encklen;
  178. compat_ulong_t entries;
  179. compat_ulong_t ivlen;
  180. uint8_t in_place_op;
  181. int err, i;
  182. uint8_t use_pmem;
  183. uint8_t enckey;
  184. uint8_t iv;
  185. err = get_user(use_pmem, &data->use_pmem);
  186. err |= put_user(use_pmem, &data32->use_pmem);
  187. if (use_pmem)
  188. err |= compat_put_qcedev_pmem_info(&data32->pmem, &data->pmem);
  189. else
  190. err |= compat_put_qcedev_vbuf_info(&data32->vbuf, &data->vbuf);
  191. err |= get_user(entries, &data->entries);
  192. err |= put_user(entries, &data32->entries);
  193. err |= get_user(data_len, &data->data_len);
  194. err |= put_user(data_len, &data32->data_len);
  195. err |= get_user(in_place_op, &data->in_place_op);
  196. err |= put_user(in_place_op, &data32->in_place_op);
  197. for (i = 0; i < QCEDEV_MAX_KEY_SIZE; i++) {
  198. err |= get_user(enckey, &(data->enckey[i]));
  199. err |= put_user(enckey, &(data32->enckey[i]));
  200. }
  201. err |= get_user(encklen, &data->encklen);
  202. err |= put_user(encklen, &data32->encklen);
  203. for (i = 0; i < QCEDEV_MAX_IV_SIZE; i++) {
  204. err |= get_user(iv, &(data->iv[i]));
  205. err |= put_user(iv, &(data32->iv[i]));
  206. }
  207. err |= get_user(ivlen, &data->ivlen);
  208. err |= put_user(ivlen, &data32->ivlen);
  209. err |= get_user(byteoffset, &data->byteoffset);
  210. err |= put_user(byteoffset, &data32->byteoffset);
  211. err |= get_user(alg, &data->alg);
  212. err |= put_user(alg, &data32->alg);
  213. err |= get_user(mode, &data->mode);
  214. err |= put_user(mode, &data32->mode);
  215. err |= get_user(op, &data->op);
  216. err |= put_user(op, &data32->op);
  217. return err;
  218. }
  219. static int compat_xfer_qcedev_map_buf_req(
  220. struct compat_qcedev_map_buf_req __user *data32,
  221. struct qcedev_map_buf_req __user *data, bool to_get)
  222. {
  223. int rc = 0, i, fd = -1;
  224. uint32_t fd_size, fd_offset, num_fds, buf_vaddr;
  225. if (to_get) {
  226. /* copy from compat struct */
  227. for (i = 0; i < QCEDEV_MAX_BUFFERS; i++) {
  228. rc |= get_user(fd, &data32->fd[i]);
  229. rc |= put_user(fd, &data->fd[i]);
  230. rc |= get_user(fd_size, &data32->fd_size[i]);
  231. rc |= put_user(fd_size, &data->fd_size[i]);
  232. rc |= get_user(fd_offset, &data32->fd_offset[i]);
  233. rc |= put_user(fd_offset, &data->fd_offset[i]);
  234. rc |= get_user(buf_vaddr, &data32->buf_vaddr[i]);
  235. rc |= put_user(buf_vaddr, &data->buf_vaddr[i]);
  236. }
  237. rc |= get_user(num_fds, &data32->num_fds);
  238. rc |= put_user(num_fds, &data->num_fds);
  239. } else {
  240. /* copy to compat struct */
  241. for (i = 0; i < QCEDEV_MAX_BUFFERS; i++) {
  242. rc |= get_user(fd, &data->fd[i]);
  243. rc |= put_user(fd, &data32->fd[i]);
  244. rc |= get_user(fd_size, &data->fd_size[i]);
  245. rc |= put_user(fd_size, &data32->fd_size[i]);
  246. rc |= get_user(fd_offset, &data->fd_offset[i]);
  247. rc |= put_user(fd_offset, &data32->fd_offset[i]);
  248. rc |= get_user(buf_vaddr, &data->buf_vaddr[i]);
  249. rc |= put_user(buf_vaddr, &data32->buf_vaddr[i]);
  250. }
  251. rc |= get_user(num_fds, &data->num_fds);
  252. rc |= put_user(num_fds, &data32->num_fds);
  253. }
  254. return rc;
  255. }
  256. static int compat_xfer_qcedev_unmap_buf_req(
  257. struct compat_qcedev_unmap_buf_req __user *data32,
  258. struct qcedev_unmap_buf_req __user *data, bool to_get)
  259. {
  260. int i, rc = 0, fd = -1;
  261. uint32_t num_fds;
  262. if (to_get) {
  263. /* copy from compat struct */
  264. for (i = 0; i < QCEDEV_MAX_BUFFERS; i++) {
  265. rc |= get_user(fd, &data32->fd[i]);
  266. rc |= put_user(fd, &data->fd[i]);
  267. }
  268. rc |= get_user(num_fds, &data32->num_fds);
  269. rc |= put_user(num_fds, &data->num_fds);
  270. } else {
  271. /* copy to compat struct */
  272. for (i = 0; i < QCEDEV_MAX_BUFFERS; i++) {
  273. rc |= get_user(fd, &data->fd[i]);
  274. rc |= put_user(fd, &data32->fd[i]);
  275. }
  276. rc |= get_user(num_fds, &data->num_fds);
  277. rc |= put_user(num_fds, &data32->num_fds);
  278. }
  279. return rc;
  280. }
  281. static int compat_get_qcedev_sha_op_req(
  282. struct compat_qcedev_sha_op_req __user *data32,
  283. struct qcedev_sha_op_req __user *data)
  284. {
  285. enum qcedev_sha_alg_enum alg;
  286. compat_ulong_t authklen;
  287. compat_ulong_t data_len;
  288. compat_ulong_t entries;
  289. compat_ulong_t diglen;
  290. compat_uptr_t authkey;
  291. compat_uptr_t vaddr;
  292. int err = 0, i;
  293. uint8_t digest;
  294. uint32_t len;
  295. for (i = 0; i < QCEDEV_MAX_BUFFERS; i++) {
  296. err |= get_user(vaddr, &data32->data[i].vaddr);
  297. err |= put_user(vaddr,
  298. (compat_uptr_t __user *)&data->data[i].vaddr);
  299. err |= get_user(len, &data32->data[i].len);
  300. err |= put_user(len, &data->data[i].len);
  301. }
  302. err |= get_user(entries, &data32->entries);
  303. err |= put_user(entries, &data->entries);
  304. err |= get_user(data_len, &data32->data_len);
  305. err |= put_user(data_len, &data->data_len);
  306. for (i = 0; i < QCEDEV_MAX_SHA_DIGEST; i++) {
  307. err |= get_user(digest, &(data32->digest[i]));
  308. err |= put_user(digest, &(data->digest[i]));
  309. }
  310. err |= get_user(diglen, &data32->diglen);
  311. err |= put_user(diglen, &data->diglen);
  312. err |= get_user(authkey, &data32->authkey);
  313. err |= put_user(authkey, (compat_uptr_t __user *)&data->authkey);
  314. err |= get_user(authklen, &data32->authklen);
  315. err |= put_user(authklen, &data->authklen);
  316. err |= get_user(alg, &data32->alg);
  317. err |= put_user(alg, &data->alg);
  318. return err;
  319. }
  320. static int compat_put_qcedev_sha_op_req(
  321. struct compat_qcedev_sha_op_req __user *data32,
  322. struct qcedev_sha_op_req __user *data)
  323. {
  324. enum qcedev_sha_alg_enum alg;
  325. compat_ulong_t authklen;
  326. compat_ulong_t data_len;
  327. compat_ulong_t entries;
  328. compat_ulong_t diglen;
  329. compat_uptr_t authkey;
  330. compat_uptr_t vaddr;
  331. int err = 0, i;
  332. uint8_t digest;
  333. uint32_t len;
  334. for (i = 0; i < QCEDEV_MAX_BUFFERS; i++) {
  335. err |= get_user(vaddr,
  336. (compat_uptr_t __user *)&data->data[i].vaddr);
  337. err |= put_user(vaddr, &data32->data[i].vaddr);
  338. err |= get_user(len, &data->data[i].len);
  339. err |= put_user(len, &data32->data[i].len);
  340. }
  341. err |= get_user(entries, &data->entries);
  342. err |= put_user(entries, &data32->entries);
  343. err |= get_user(data_len, &data->data_len);
  344. err |= put_user(data_len, &data32->data_len);
  345. for (i = 0; i < QCEDEV_MAX_SHA_DIGEST; i++) {
  346. err |= get_user(digest, &(data->digest[i]));
  347. err |= put_user(digest, &(data32->digest[i]));
  348. }
  349. err |= get_user(diglen, &data->diglen);
  350. err |= put_user(diglen, &data32->diglen);
  351. err |= get_user(authkey,
  352. (compat_uptr_t __user *)&data->authkey);
  353. err |= put_user(authkey, &data32->authkey);
  354. err |= get_user(authklen, &data->authklen);
  355. err |= put_user(authklen, &data32->authklen);
  356. err |= get_user(alg, &data->alg);
  357. err |= put_user(alg, &data32->alg);
  358. return err;
  359. }
  360. static unsigned int convert_cmd(unsigned int cmd)
  361. {
  362. switch (cmd) {
  363. case COMPAT_QCEDEV_IOCTL_ENC_REQ:
  364. return QCEDEV_IOCTL_ENC_REQ;
  365. case COMPAT_QCEDEV_IOCTL_DEC_REQ:
  366. return QCEDEV_IOCTL_DEC_REQ;
  367. case COMPAT_QCEDEV_IOCTL_SHA_INIT_REQ:
  368. return QCEDEV_IOCTL_SHA_INIT_REQ;
  369. case COMPAT_QCEDEV_IOCTL_SHA_UPDATE_REQ:
  370. return QCEDEV_IOCTL_SHA_UPDATE_REQ;
  371. case COMPAT_QCEDEV_IOCTL_SHA_FINAL_REQ:
  372. return QCEDEV_IOCTL_SHA_FINAL_REQ;
  373. case COMPAT_QCEDEV_IOCTL_GET_SHA_REQ:
  374. return QCEDEV_IOCTL_GET_SHA_REQ;
  375. case COMPAT_QCEDEV_IOCTL_GET_CMAC_REQ:
  376. return QCEDEV_IOCTL_GET_CMAC_REQ;
  377. case COMPAT_QCEDEV_IOCTL_MAP_BUF_REQ:
  378. return QCEDEV_IOCTL_MAP_BUF_REQ;
  379. case COMPAT_QCEDEV_IOCTL_UNMAP_BUF_REQ:
  380. return QCEDEV_IOCTL_UNMAP_BUF_REQ;
  381. default:
  382. return cmd;
  383. }
  384. }
  385. long compat_qcedev_ioctl(struct file *file,
  386. unsigned int cmd, unsigned long arg)
  387. {
  388. long ret;
  389. switch (cmd) {
  390. case COMPAT_QCEDEV_IOCTL_ENC_REQ:
  391. case COMPAT_QCEDEV_IOCTL_DEC_REQ: {
  392. struct compat_qcedev_cipher_op_req __user *data32;
  393. struct qcedev_cipher_op_req __user *data;
  394. int err;
  395. data32 = compat_ptr(arg);
  396. data = compat_alloc_user_space(sizeof(*data));
  397. if (!data)
  398. return -EFAULT;
  399. err = compat_get_qcedev_cipher_op_req(data32, data);
  400. if (err)
  401. return err;
  402. ret = qcedev_ioctl(file, convert_cmd(cmd), (unsigned long)data);
  403. err = compat_put_qcedev_cipher_op_req(data32, data);
  404. return ret ? ret : err;
  405. }
  406. case COMPAT_QCEDEV_IOCTL_SHA_INIT_REQ:
  407. case COMPAT_QCEDEV_IOCTL_SHA_UPDATE_REQ:
  408. case COMPAT_QCEDEV_IOCTL_SHA_FINAL_REQ:
  409. case COMPAT_QCEDEV_IOCTL_GET_CMAC_REQ:
  410. case COMPAT_QCEDEV_IOCTL_GET_SHA_REQ: {
  411. struct compat_qcedev_sha_op_req __user *data32;
  412. struct qcedev_sha_op_req __user *data;
  413. int err;
  414. data32 = compat_ptr(arg);
  415. data = compat_alloc_user_space(sizeof(*data));
  416. if (!data)
  417. return -EFAULT;
  418. err = compat_get_qcedev_sha_op_req(data32, data);
  419. if (err)
  420. return err;
  421. ret = qcedev_ioctl(file, convert_cmd(cmd), (unsigned long)data);
  422. err = compat_put_qcedev_sha_op_req(data32, data);
  423. return ret ? ret : err;
  424. }
  425. case COMPAT_QCEDEV_IOCTL_MAP_BUF_REQ: {
  426. struct compat_qcedev_map_buf_req __user *data32;
  427. struct qcedev_map_buf_req __user *data;
  428. int err;
  429. data32 = compat_ptr(arg);
  430. data = compat_alloc_user_space(sizeof(*data));
  431. if (!data)
  432. return -EINVAL;
  433. err = compat_xfer_qcedev_map_buf_req(data32, data, true);
  434. if (err)
  435. return err;
  436. ret = qcedev_ioctl(file, convert_cmd(cmd), (unsigned long)data);
  437. err = compat_xfer_qcedev_map_buf_req(data32, data, false);
  438. return ret ? ret : err;
  439. break;
  440. }
  441. case COMPAT_QCEDEV_IOCTL_UNMAP_BUF_REQ: {
  442. struct compat_qcedev_unmap_buf_req __user *data32;
  443. struct qcedev_unmap_buf_req __user *data;
  444. int err;
  445. data32 = compat_ptr(arg);
  446. data = compat_alloc_user_space(sizeof(*data));
  447. if (!data)
  448. return -EINVAL;
  449. err = compat_xfer_qcedev_unmap_buf_req(data32, data, true);
  450. if (err)
  451. return err;
  452. ret = qcedev_ioctl(file, convert_cmd(cmd), (unsigned long)data);
  453. err = compat_xfer_qcedev_unmap_buf_req(data32, data, false);
  454. return ret ? ret : err;
  455. break;
  456. }
  457. default:
  458. return -ENOIOCTLCMD;
  459. }
  460. return 0;
  461. }