1 /* $OpenBSD: criov.c,v 1.9 2002/01/29 15:48:29 jason Exp $ */
4 * Copyright (c) 1999 Theo de Raadt
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
36 #include <sys/errno.h>
37 #include <sys/malloc.h>
38 #include <sys/kernel.h>
41 #include <sys/limits.h>
45 #include <machine/vmparam.h>
48 #include <vm/vm_page.h>
51 #include <opencrypto/cryptodev.h>
53 SDT_PROVIDER_DECLARE(opencrypto);
56 * These macros are only for avoiding code duplication, as we need to skip
57 * given number of bytes in the same way in several functions below.
59 #define CUIO_SKIP() do { \
60 KASSERT(off >= 0, ("%s: off %d < 0", __func__, off)); \
61 KASSERT(len >= 0, ("%s: len %d < 0", __func__, len)); \
63 KASSERT(iol >= 0, ("%s: empty in skip", __func__)); \
64 if (off < iov->iov_len) \
66 off -= iov->iov_len; \
72 #define CVM_PAGE_SKIP() do { \
73 KASSERT(off >= 0, ("%s: off %d < 0", __func__, off)); \
74 KASSERT(len >= 0, ("%s: len %d < 0", __func__, len)); \
76 if (off < PAGE_SIZE) \
78 processed += PAGE_SIZE - off; \
79 off -= PAGE_SIZE - off; \
85 cuio_copydata(struct uio* uio, int off, int len, caddr_t cp)
87 struct iovec *iov = uio->uio_iov;
88 int iol = uio->uio_iovcnt;
93 KASSERT(iol >= 0, ("%s: empty", __func__));
94 count = min(iov->iov_len - off, len);
95 bcopy(((caddr_t)iov->iov_base) + off, cp, count);
105 cuio_copyback(struct uio* uio, int off, int len, c_caddr_t cp)
107 struct iovec *iov = uio->uio_iov;
108 int iol = uio->uio_iovcnt;
113 KASSERT(iol >= 0, ("%s: empty", __func__));
114 count = min(iov->iov_len - off, len);
115 bcopy(cp, ((caddr_t)iov->iov_base) + off, count);
125 * Return the index and offset of location in iovec list.
128 cuio_getptr(struct uio *uio, int loc, int *off)
133 while (loc >= 0 && ind < uio->uio_iovcnt) {
134 len = uio->uio_iov[ind].iov_len;
143 if (ind > 0 && loc == 0) {
145 *off = uio->uio_iov[ind].iov_len;
152 #if CRYPTO_MAY_HAVE_VMPAGE
154 * Apply function f to the data in a vm_page_t list starting "off" bytes from
155 * the beginning, continuing for "len" bytes.
158 cvm_page_apply(vm_page_t *pages, int off, int len,
159 int (*f)(void *, const void *, u_int), void *arg)
167 char *kaddr = (char *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(*pages));
168 count = min(PAGE_SIZE - off, len);
169 rval = (*f)(arg, kaddr + off, count);
181 cvm_page_contiguous_segment(vm_page_t *pages, size_t skip, int len)
183 if ((skip + len - 1) / PAGE_SIZE > skip / PAGE_SIZE)
186 pages += (skip / PAGE_SIZE);
187 skip -= rounddown(skip, PAGE_SIZE);
188 return (((char *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(*pages))) + skip);
192 * Copy len bytes of data from the vm_page_t array, skipping the first off
193 * bytes, into the pointer cp. Return the number of bytes skipped and copied.
194 * Does not verify the length of the array.
197 cvm_page_copyback(vm_page_t *pages, int off, int len, c_caddr_t cp)
204 count = min(PAGE_SIZE - off, len);
205 bcopy(cp, (char *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(*pages)) + off,
217 * Copy len bytes of data from the pointer cp into the vm_page_t array,
218 * skipping the first off bytes, Return the number of bytes skipped and copied.
219 * Does not verify the length of the array.
222 cvm_page_copydata(vm_page_t *pages, int off, int len, caddr_t cp)
229 count = min(PAGE_SIZE - off, len);
230 bcopy(((char *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(*pages)) + off), cp,
240 #endif /* CRYPTO_MAY_HAVE_VMPAGE */
243 * Given a starting page in an m_epg, determine the length of the
244 * current physically contiguous segment.
246 static __inline size_t
247 m_epg_pages_extent(struct mbuf *m, int idx, u_int pglen)
253 for (i = idx + 1; i < m->m_epg_npgs; i++) {
254 if (m->m_epg_pa[i - 1] + PAGE_SIZE != m->m_epg_pa[i])
256 len += m_epg_pagelen(m, i, 0);
262 m_epg_segment(struct mbuf *m, size_t offset, size_t *len)
264 u_int i, pglen, pgoff;
266 offset += mtod(m, vm_offset_t);
267 if (offset < m->m_epg_hdrlen) {
268 *len = m->m_epg_hdrlen - offset;
269 return (m->m_epg_hdr + offset);
271 offset -= m->m_epg_hdrlen;
272 pgoff = m->m_epg_1st_off;
273 for (i = 0; i < m->m_epg_npgs; i++) {
274 pglen = m_epg_pagelen(m, i, pgoff);
275 if (offset < pglen) {
276 *len = m_epg_pages_extent(m, i, pglen) - offset;
277 return ((void *)PHYS_TO_DMAP(m->m_epg_pa[i] + pgoff +
283 KASSERT(offset <= m->m_epg_trllen, ("%s: offset beyond trailer",
285 *len = m->m_epg_trllen - offset;
286 return (m->m_epg_trail + offset);
289 static __inline void *
290 m_epg_contiguous_subsegment(struct mbuf *m, size_t skip, size_t len)
295 base = m_epg_segment(m, skip, &seglen);
302 crypto_cursor_init(struct crypto_buffer_cursor *cc,
303 const struct crypto_buffer *cb)
305 memset(cc, 0, sizeof(*cc));
306 cc->cc_type = cb->cb_type;
307 switch (cc->cc_type) {
308 case CRYPTO_BUF_CONTIG:
309 cc->cc_buf = cb->cb_buf;
310 cc->cc_buf_len = cb->cb_buf_len;
312 case CRYPTO_BUF_MBUF:
313 case CRYPTO_BUF_SINGLE_MBUF:
314 cc->cc_mbuf = cb->cb_mbuf;
316 case CRYPTO_BUF_VMPAGE:
317 cc->cc_vmpage = cb->cb_vm_page;
318 cc->cc_buf_len = cb->cb_vm_page_len;
319 cc->cc_offset = cb->cb_vm_page_offset;
322 cc->cc_iov = cb->cb_uio->uio_iov;
326 panic("%s: invalid buffer type %d", __func__, cb->cb_type);
332 SDT_PROBE_DEFINE2(opencrypto, criov, cursor_advance, vmpage, "struct crypto_buffer_cursor*", "size_t");
335 crypto_cursor_advance(struct crypto_buffer_cursor *cc, size_t amount)
339 switch (cc->cc_type) {
340 case CRYPTO_BUF_CONTIG:
341 MPASS(cc->cc_buf_len >= amount);
342 cc->cc_buf += amount;
343 cc->cc_buf_len -= amount;
345 case CRYPTO_BUF_MBUF:
347 remain = cc->cc_mbuf->m_len - cc->cc_offset;
348 if (amount < remain) {
349 cc->cc_offset += amount;
353 cc->cc_mbuf = cc->cc_mbuf->m_next;
359 case CRYPTO_BUF_SINGLE_MBUF:
360 MPASS(cc->cc_mbuf->m_len >= cc->cc_offset + amount);
361 cc->cc_offset += amount;
363 case CRYPTO_BUF_VMPAGE:
365 SDT_PROBE2(opencrypto, criov, cursor_advance, vmpage,
367 remain = MIN(PAGE_SIZE - cc->cc_offset, cc->cc_buf_len);
368 if (amount < remain) {
369 cc->cc_buf_len -= amount;
370 cc->cc_offset += amount;
373 cc->cc_buf_len -= remain;
377 if (amount == 0 || cc->cc_buf_len == 0)
383 remain = cc->cc_iov->iov_len - cc->cc_offset;
384 if (amount < remain) {
385 cc->cc_offset += amount;
397 panic("%s: invalid buffer type %d", __func__, cc->cc_type);
404 crypto_cursor_segment(struct crypto_buffer_cursor *cc, size_t *len)
406 switch (cc->cc_type) {
407 case CRYPTO_BUF_CONTIG:
408 *len = cc->cc_buf_len;
410 case CRYPTO_BUF_MBUF:
411 case CRYPTO_BUF_SINGLE_MBUF:
412 if (cc->cc_mbuf == NULL) {
416 if (cc->cc_mbuf->m_flags & M_EXTPG)
417 return (m_epg_segment(cc->cc_mbuf, cc->cc_offset, len));
418 *len = cc->cc_mbuf->m_len - cc->cc_offset;
419 return (mtod(cc->cc_mbuf, char *) + cc->cc_offset);
420 case CRYPTO_BUF_VMPAGE:
421 *len = PAGE_SIZE - cc->cc_offset;
422 return ((char *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(
423 *cc->cc_vmpage)) + cc->cc_offset);
425 *len = cc->cc_iov->iov_len - cc->cc_offset;
426 return ((char *)cc->cc_iov->iov_base + cc->cc_offset);
429 panic("%s: invalid buffer type %d", __func__, cc->cc_type);
437 crypto_cursor_segbase(struct crypto_buffer_cursor *cc)
441 return (crypto_cursor_segment(cc, &len));
445 crypto_cursor_seglen(struct crypto_buffer_cursor *cc)
449 crypto_cursor_segment(cc, &len);
454 crypto_cursor_copyback(struct crypto_buffer_cursor *cc, int size,
462 switch (cc->cc_type) {
463 case CRYPTO_BUF_CONTIG:
464 MPASS(cc->cc_buf_len >= size);
465 memcpy(cc->cc_buf, src, size);
467 cc->cc_buf_len -= size;
469 case CRYPTO_BUF_MBUF:
472 * This uses m_copyback() for individual
473 * mbufs so that cc_mbuf and cc_offset are
476 remain = cc->cc_mbuf->m_len - cc->cc_offset;
477 todo = MIN(remain, size);
478 m_copyback(cc->cc_mbuf, cc->cc_offset, todo, src);
481 cc->cc_offset += todo;
485 cc->cc_mbuf = cc->cc_mbuf->m_next;
491 case CRYPTO_BUF_SINGLE_MBUF:
492 MPASS(cc->cc_mbuf->m_len >= cc->cc_offset + size);
493 m_copyback(cc->cc_mbuf, cc->cc_offset, size, src);
494 cc->cc_offset += size;
496 case CRYPTO_BUF_VMPAGE:
498 dst = (char *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(
499 *cc->cc_vmpage)) + cc->cc_offset;
500 remain = MIN(PAGE_SIZE - cc->cc_offset, cc->cc_buf_len);
501 todo = MIN(remain, size);
502 memcpy(dst, src, todo);
504 cc->cc_buf_len -= todo;
506 cc->cc_offset += todo;
518 dst = (char *)cc->cc_iov->iov_base + cc->cc_offset;
519 remain = cc->cc_iov->iov_len - cc->cc_offset;
520 todo = MIN(remain, size);
521 memcpy(dst, src, todo);
524 cc->cc_offset += todo;
536 panic("%s: invalid buffer type %d", __func__, cc->cc_type);
543 crypto_cursor_copydata(struct crypto_buffer_cursor *cc, int size, void *vdst)
550 switch (cc->cc_type) {
551 case CRYPTO_BUF_CONTIG:
552 MPASS(cc->cc_buf_len >= size);
553 memcpy(dst, cc->cc_buf, size);
555 cc->cc_buf_len -= size;
557 case CRYPTO_BUF_MBUF:
560 * This uses m_copydata() for individual
561 * mbufs so that cc_mbuf and cc_offset are
564 remain = cc->cc_mbuf->m_len - cc->cc_offset;
565 todo = MIN(remain, size);
566 m_copydata(cc->cc_mbuf, cc->cc_offset, todo, dst);
569 cc->cc_offset += todo;
573 cc->cc_mbuf = cc->cc_mbuf->m_next;
579 case CRYPTO_BUF_SINGLE_MBUF:
580 MPASS(cc->cc_mbuf->m_len >= cc->cc_offset + size);
581 m_copydata(cc->cc_mbuf, cc->cc_offset, size, dst);
582 cc->cc_offset += size;
584 case CRYPTO_BUF_VMPAGE:
586 src = (char *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(
587 *cc->cc_vmpage)) + cc->cc_offset;
588 remain = MIN(PAGE_SIZE - cc->cc_offset, cc->cc_buf_len);
589 todo = MIN(remain, size);
590 memcpy(dst, src, todo);
592 cc->cc_buf_len -= todo;
594 cc->cc_offset += todo;
606 src = (const char *)cc->cc_iov->iov_base +
608 remain = cc->cc_iov->iov_len - cc->cc_offset;
609 todo = MIN(remain, size);
610 memcpy(dst, src, todo);
613 cc->cc_offset += todo;
625 panic("%s: invalid buffer type %d", __func__, cc->cc_type);
632 * To avoid advancing 'cursor', make a local copy that gets advanced
636 crypto_cursor_copydata_noadv(struct crypto_buffer_cursor *cc, int size,
639 struct crypto_buffer_cursor copy;
642 crypto_cursor_copydata(©, size, vdst);
646 * Apply function f to the data in an iovec list starting "off" bytes from
647 * the beginning, continuing for "len" bytes.
650 cuio_apply(struct uio *uio, int off, int len,
651 int (*f)(void *, const void *, u_int), void *arg)
653 struct iovec *iov = uio->uio_iov;
654 int iol = uio->uio_iovcnt;
660 KASSERT(iol >= 0, ("%s: empty", __func__));
661 count = min(iov->iov_len - off, len);
662 rval = (*f)(arg, ((caddr_t)iov->iov_base) + off, count);
674 crypto_copyback(struct cryptop *crp, int off, int size, const void *src)
676 struct crypto_buffer *cb;
678 if (crp->crp_obuf.cb_type != CRYPTO_BUF_NONE)
682 switch (cb->cb_type) {
683 case CRYPTO_BUF_MBUF:
684 case CRYPTO_BUF_SINGLE_MBUF:
685 m_copyback(cb->cb_mbuf, off, size, src);
687 #if CRYPTO_MAY_HAVE_VMPAGE
688 case CRYPTO_BUF_VMPAGE:
689 MPASS(size <= cb->cb_vm_page_len);
691 cb->cb_vm_page_len + cb->cb_vm_page_offset);
692 cvm_page_copyback(cb->cb_vm_page,
693 off + cb->cb_vm_page_offset, size, src);
695 #endif /* CRYPTO_MAY_HAVE_VMPAGE */
697 cuio_copyback(cb->cb_uio, off, size, src);
699 case CRYPTO_BUF_CONTIG:
700 MPASS(off + size <= cb->cb_buf_len);
701 bcopy(src, cb->cb_buf + off, size);
705 panic("invalid crp buf type %d", cb->cb_type);
712 crypto_copydata(struct cryptop *crp, int off, int size, void *dst)
715 switch (crp->crp_buf.cb_type) {
716 case CRYPTO_BUF_MBUF:
717 case CRYPTO_BUF_SINGLE_MBUF:
718 m_copydata(crp->crp_buf.cb_mbuf, off, size, dst);
720 #if CRYPTO_MAY_HAVE_VMPAGE
721 case CRYPTO_BUF_VMPAGE:
722 MPASS(size <= crp->crp_buf.cb_vm_page_len);
723 MPASS(size + off <= crp->crp_buf.cb_vm_page_len +
724 crp->crp_buf.cb_vm_page_offset);
725 cvm_page_copydata(crp->crp_buf.cb_vm_page,
726 off + crp->crp_buf.cb_vm_page_offset, size, dst);
728 #endif /* CRYPTO_MAY_HAVE_VMPAGE */
730 cuio_copydata(crp->crp_buf.cb_uio, off, size, dst);
732 case CRYPTO_BUF_CONTIG:
733 MPASS(off + size <= crp->crp_buf.cb_buf_len);
734 bcopy(crp->crp_buf.cb_buf + off, dst, size);
738 panic("invalid crp buf type %d", crp->crp_buf.cb_type);
745 crypto_apply_buf(struct crypto_buffer *cb, int off, int len,
746 int (*f)(void *, const void *, u_int), void *arg)
750 switch (cb->cb_type) {
751 case CRYPTO_BUF_MBUF:
752 case CRYPTO_BUF_SINGLE_MBUF:
753 error = m_apply(cb->cb_mbuf, off, len,
754 (int (*)(void *, void *, u_int))f, arg);
757 error = cuio_apply(cb->cb_uio, off, len, f, arg);
759 #if CRYPTO_MAY_HAVE_VMPAGE
760 case CRYPTO_BUF_VMPAGE:
761 error = cvm_page_apply(cb->cb_vm_page,
762 off + cb->cb_vm_page_offset, len, f, arg);
764 #endif /* CRYPTO_MAY_HAVE_VMPAGE */
765 case CRYPTO_BUF_CONTIG:
766 MPASS(off + len <= cb->cb_buf_len);
767 error = (*f)(arg, cb->cb_buf + off, len);
771 panic("invalid crypto buf type %d", cb->cb_type);
780 crypto_apply(struct cryptop *crp, int off, int len,
781 int (*f)(void *, const void *, u_int), void *arg)
783 return (crypto_apply_buf(&crp->crp_buf, off, len, f, arg));
787 m_contiguous_subsegment(struct mbuf *m, size_t skip, size_t len)
791 MPASS(skip <= INT_MAX);
793 m = m_getptr(m, (int)skip, &rel_off);
799 if (skip + len > m->m_len)
802 if (m->m_flags & M_EXTPG)
803 return (m_epg_contiguous_subsegment(m, skip, len));
804 return (mtod(m, char*) + skip);
808 cuio_contiguous_segment(struct uio *uio, size_t skip, size_t len)
812 MPASS(skip <= INT_MAX);
813 idx = cuio_getptr(uio, (int)skip, &rel_off);
819 if (skip + len > uio->uio_iov[idx].iov_len)
821 return ((char *)uio->uio_iov[idx].iov_base + skip);
825 crypto_buffer_contiguous_subsegment(struct crypto_buffer *cb, size_t skip,
829 switch (cb->cb_type) {
830 case CRYPTO_BUF_MBUF:
831 case CRYPTO_BUF_SINGLE_MBUF:
832 return (m_contiguous_subsegment(cb->cb_mbuf, skip, len));
834 return (cuio_contiguous_segment(cb->cb_uio, skip, len));
835 #if CRYPTO_MAY_HAVE_VMPAGE
836 case CRYPTO_BUF_VMPAGE:
837 MPASS(skip + len <= cb->cb_vm_page_len);
838 return (cvm_page_contiguous_segment(cb->cb_vm_page,
839 skip + cb->cb_vm_page_offset, len));
840 #endif /* CRYPTO_MAY_HAVE_VMPAGE */
841 case CRYPTO_BUF_CONTIG:
842 MPASS(skip + len <= cb->cb_buf_len);
843 return (cb->cb_buf + skip);
846 panic("invalid crp buf type %d", cb->cb_type);
853 crypto_contiguous_subsegment(struct cryptop *crp, size_t skip, size_t len)
855 return (crypto_buffer_contiguous_subsegment(&crp->crp_buf, skip, len));