compat_qcedev.c 15 KB

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