compat_qcedev.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. /*
  2. * QTI CE 32-bit compatibility syscall for 64-bit systems
  3. *
  4. * Copyright (c) 2014-2018, The Linux Foundation. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 and
  8. * only version 2 as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/uaccess.h>
  18. #include <linux/qcedev.h>
  19. #include <linux/compat.h>
  20. #include "compat_qcedev.h"
  21. static int compat_get_qcedev_pmem_info(
  22. struct compat_qcedev_pmem_info __user *pmem32,
  23. struct qcedev_pmem_info __user *pmem)
  24. {
  25. compat_ulong_t offset;
  26. compat_int_t fd_src;
  27. compat_int_t fd_dst;
  28. int err = 0, i = 0;
  29. uint32_t len;
  30. err |= get_user(fd_src, &pmem32->fd_src);
  31. err |= put_user(fd_src, &pmem->fd_src);
  32. for (i = 0; i < QCEDEV_MAX_BUFFERS; i++) {
  33. err |= get_user(offset, &pmem32->src[i].offset);
  34. err |= put_user(offset, &pmem->src[i].offset);
  35. err |= get_user(len, &pmem32->src[i].len);
  36. err |= put_user(len, &pmem->src[i].len);
  37. }
  38. err |= get_user(fd_dst, &pmem32->fd_dst);
  39. err |= put_user(fd_dst, &pmem->fd_dst);
  40. for (i = 0; i < QCEDEV_MAX_BUFFERS; i++) {
  41. err |= get_user(offset, &pmem32->dst[i].offset);
  42. err |= put_user(offset, &pmem->dst[i].offset);
  43. err |= get_user(len, &pmem32->dst[i].len);
  44. err |= put_user(len, &pmem->dst[i].len);
  45. }
  46. return err;
  47. }
  48. static int compat_put_qcedev_pmem_info(
  49. struct compat_qcedev_pmem_info __user *pmem32,
  50. struct qcedev_pmem_info __user *pmem)
  51. {
  52. compat_ulong_t offset;
  53. compat_int_t fd_src;
  54. compat_int_t fd_dst;
  55. int err = 0, i = 0;
  56. uint32_t len;
  57. err |= get_user(fd_src, &pmem->fd_src);
  58. err |= put_user(fd_src, &pmem32->fd_src);
  59. for (i = 0; i < QCEDEV_MAX_BUFFERS; i++) {
  60. err |= get_user(offset, &pmem->src[i].offset);
  61. err |= put_user(offset, &pmem32->src[i].offset);
  62. err |= get_user(len, &pmem->src[i].len);
  63. err |= put_user(len, &pmem32->src[i].len);
  64. }
  65. err |= get_user(fd_dst, &pmem->fd_dst);
  66. err |= put_user(fd_dst, &pmem32->fd_dst);
  67. for (i = 0; i < QCEDEV_MAX_BUFFERS; i++) {
  68. err |= get_user(offset, &pmem->dst[i].offset);
  69. err |= put_user(offset, &pmem32->dst[i].offset);
  70. err |= get_user(len, &pmem->dst[i].len);
  71. err |= put_user(len, &pmem32->dst[i].len);
  72. }
  73. return err;
  74. }
  75. static int compat_get_qcedev_vbuf_info(
  76. struct compat_qcedev_vbuf_info __user *vbuf32,
  77. struct qcedev_vbuf_info __user *vbuf)
  78. {
  79. compat_uptr_t vaddr;
  80. int err = 0, i = 0;
  81. uint32_t len;
  82. for (i = 0; i < QCEDEV_MAX_BUFFERS; i++) {
  83. err |= get_user(vaddr, &vbuf32->src[i].vaddr);
  84. err |= put_user(vaddr, (compat_uptr_t *)&vbuf->src[i].vaddr);
  85. err |= get_user(len, &vbuf32->src[i].len);
  86. err |= put_user(len, &vbuf->src[i].len);
  87. }
  88. for (i = 0; i < QCEDEV_MAX_BUFFERS; i++) {
  89. err |= get_user(vaddr, &vbuf32->dst[i].vaddr);
  90. err |= put_user(vaddr, (compat_uptr_t *)&vbuf->dst[i].vaddr);
  91. err |= get_user(len, &vbuf32->dst[i].len);
  92. err |= put_user(len, &vbuf->dst[i].len);
  93. }
  94. return err;
  95. }
  96. static int compat_put_qcedev_vbuf_info(
  97. struct compat_qcedev_vbuf_info __user *vbuf32,
  98. struct qcedev_vbuf_info __user *vbuf)
  99. {
  100. compat_uptr_t vaddr;
  101. int err = 0, i = 0;
  102. uint32_t len;
  103. for (i = 0; i < QCEDEV_MAX_BUFFERS; i++) {
  104. err |= get_user(vaddr, (compat_uptr_t *)&vbuf->src[i].vaddr);
  105. err |= put_user(vaddr, &vbuf32->src[i].vaddr);
  106. err |= get_user(len, &vbuf->src[i].len);
  107. err |= put_user(len, &vbuf32->src[i].len);
  108. }
  109. for (i = 0; i < QCEDEV_MAX_BUFFERS; i++) {
  110. err |= get_user(vaddr, (compat_uptr_t *)&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 = 0, i = 0;
  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 = 0, i = 0;
  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 = 0, 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 = 0, 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 = 0;
  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, (compat_uptr_t *)&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 *)&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 = 0;
  331. uint8_t digest;
  332. uint32_t len;
  333. for (i = 0; i < QCEDEV_MAX_BUFFERS; i++) {
  334. err |= get_user(vaddr, (compat_uptr_t *)&data->data[i].vaddr);
  335. err |= put_user(vaddr, &data32->data[i].vaddr);
  336. err |= get_user(len, &data->data[i].len);
  337. err |= put_user(len, &data32->data[i].len);
  338. }
  339. err |= get_user(entries, &data->entries);
  340. err |= put_user(entries, &data32->entries);
  341. err |= get_user(data_len, &data->data_len);
  342. err |= put_user(data_len, &data32->data_len);
  343. for (i = 0; i < QCEDEV_MAX_SHA_DIGEST; i++) {
  344. err |= get_user(digest, &(data->digest[i]));
  345. err |= put_user(digest, &(data32->digest[i]));
  346. }
  347. err |= get_user(diglen, &data->diglen);
  348. err |= put_user(diglen, &data32->diglen);
  349. err |= get_user(authkey, (compat_uptr_t *)&data->authkey);
  350. err |= put_user(authkey, &data32->authkey);
  351. err |= get_user(authklen, &data->authklen);
  352. err |= put_user(authklen, &data32->authklen);
  353. err |= get_user(alg, &data->alg);
  354. err |= put_user(alg, &data32->alg);
  355. return err;
  356. }
  357. static unsigned int convert_cmd(unsigned int cmd)
  358. {
  359. switch (cmd) {
  360. case COMPAT_QCEDEV_IOCTL_ENC_REQ:
  361. return QCEDEV_IOCTL_ENC_REQ;
  362. case COMPAT_QCEDEV_IOCTL_DEC_REQ:
  363. return QCEDEV_IOCTL_DEC_REQ;
  364. case COMPAT_QCEDEV_IOCTL_SHA_INIT_REQ:
  365. return QCEDEV_IOCTL_SHA_INIT_REQ;
  366. case COMPAT_QCEDEV_IOCTL_SHA_UPDATE_REQ:
  367. return QCEDEV_IOCTL_SHA_UPDATE_REQ;
  368. case COMPAT_QCEDEV_IOCTL_SHA_FINAL_REQ:
  369. return QCEDEV_IOCTL_SHA_FINAL_REQ;
  370. case COMPAT_QCEDEV_IOCTL_GET_SHA_REQ:
  371. return QCEDEV_IOCTL_GET_SHA_REQ;
  372. case COMPAT_QCEDEV_IOCTL_GET_CMAC_REQ:
  373. return QCEDEV_IOCTL_GET_CMAC_REQ;
  374. case COMPAT_QCEDEV_IOCTL_MAP_BUF_REQ:
  375. return QCEDEV_IOCTL_MAP_BUF_REQ;
  376. case COMPAT_QCEDEV_IOCTL_UNMAP_BUF_REQ:
  377. return QCEDEV_IOCTL_UNMAP_BUF_REQ;
  378. default:
  379. return cmd;
  380. }
  381. }
  382. long compat_qcedev_ioctl(struct file *file,
  383. unsigned int cmd, unsigned long arg)
  384. {
  385. long ret;
  386. switch (cmd) {
  387. case COMPAT_QCEDEV_IOCTL_ENC_REQ:
  388. case COMPAT_QCEDEV_IOCTL_DEC_REQ: {
  389. struct compat_qcedev_cipher_op_req __user *data32;
  390. struct qcedev_cipher_op_req __user *data;
  391. int err;
  392. data32 = compat_ptr(arg);
  393. data = compat_alloc_user_space(sizeof(*data));
  394. if (!data)
  395. return -EFAULT;
  396. err = compat_get_qcedev_cipher_op_req(data32, data);
  397. if (err)
  398. return err;
  399. ret = qcedev_ioctl(file, convert_cmd(cmd), (unsigned long)data);
  400. err = compat_put_qcedev_cipher_op_req(data32, data);
  401. return ret ? ret : err;
  402. }
  403. case COMPAT_QCEDEV_IOCTL_SHA_INIT_REQ:
  404. case COMPAT_QCEDEV_IOCTL_SHA_UPDATE_REQ:
  405. case COMPAT_QCEDEV_IOCTL_SHA_FINAL_REQ:
  406. case COMPAT_QCEDEV_IOCTL_GET_CMAC_REQ:
  407. case COMPAT_QCEDEV_IOCTL_GET_SHA_REQ: {
  408. struct compat_qcedev_sha_op_req __user *data32;
  409. struct qcedev_sha_op_req __user *data;
  410. int err;
  411. data32 = compat_ptr(arg);
  412. data = compat_alloc_user_space(sizeof(*data));
  413. if (!data)
  414. return -EFAULT;
  415. err = compat_get_qcedev_sha_op_req(data32, data);
  416. if (err)
  417. return err;
  418. ret = qcedev_ioctl(file, convert_cmd(cmd), (unsigned long)data);
  419. err = compat_put_qcedev_sha_op_req(data32, data);
  420. return ret ? ret : err;
  421. }
  422. case COMPAT_QCEDEV_IOCTL_MAP_BUF_REQ: {
  423. struct compat_qcedev_map_buf_req __user *data32;
  424. struct qcedev_map_buf_req __user *data;
  425. int err;
  426. data32 = compat_ptr(arg);
  427. data = compat_alloc_user_space(sizeof(*data));
  428. if (!data)
  429. return -EINVAL;
  430. err = compat_xfer_qcedev_map_buf_req(data32, data, true);
  431. if (err)
  432. return err;
  433. ret = qcedev_ioctl(file, convert_cmd(cmd), (unsigned long)data);
  434. err = compat_xfer_qcedev_map_buf_req(data32, data, false);
  435. return ret ? ret : err;
  436. break;
  437. }
  438. case COMPAT_QCEDEV_IOCTL_UNMAP_BUF_REQ: {
  439. struct compat_qcedev_unmap_buf_req __user *data32;
  440. struct qcedev_unmap_buf_req __user *data;
  441. int err;
  442. data32 = compat_ptr(arg);
  443. data = compat_alloc_user_space(sizeof(*data));
  444. if (!data)
  445. return -EINVAL;
  446. err = compat_xfer_qcedev_unmap_buf_req(data32, data, true);
  447. if (err)
  448. return err;
  449. ret = qcedev_ioctl(file, convert_cmd(cmd), (unsigned long)data);
  450. err = compat_xfer_qcedev_unmap_buf_req(data32, data, false);
  451. return ret ? ret : err;
  452. break;
  453. }
  454. default:
  455. return -ENOIOCTLCMD;
  456. }
  457. return 0;
  458. }
  459. EXPORT_SYMBOL(compat_qcedev_ioctl);
  460. MODULE_LICENSE("GPL v2");
  461. MODULE_DESCRIPTION("QTI 32-64 Compatibility for Crypto driver");