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)
161 int processed __unused;
168 char *kaddr = (char *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(*pages));
169 count = min(PAGE_SIZE - off, len);
170 rval = (*f)(arg, kaddr + off, count);
182 cvm_page_contiguous_segment(vm_page_t *pages, size_t skip, int len)
184 if ((skip + len - 1) / PAGE_SIZE > skip / PAGE_SIZE)
187 pages += (skip / PAGE_SIZE);
188 skip -= rounddown(skip, PAGE_SIZE);
189 return (((char *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(*pages))) + skip);
193 * Copy len bytes of data from the vm_page_t array, skipping the first off
194 * bytes, into the pointer cp. Return the number of bytes skipped and copied.
195 * Does not verify the length of the array.
198 cvm_page_copyback(vm_page_t *pages, int off, int len, c_caddr_t cp)
205 count = min(PAGE_SIZE - off, len);
206 bcopy(cp, (char *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(*pages)) + off,
218 * Copy len bytes of data from the pointer cp into the vm_page_t array,
219 * skipping the first off bytes, Return the number of bytes skipped and copied.
220 * Does not verify the length of the array.
223 cvm_page_copydata(vm_page_t *pages, int off, int len, caddr_t cp)
230 count = min(PAGE_SIZE - off, len);
231 bcopy(((char *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(*pages)) + off), cp,
241 #endif /* CRYPTO_MAY_HAVE_VMPAGE */
244 * Given a starting page in an m_epg, determine the length of the
245 * current physically contiguous segment.
247 static __inline size_t
248 m_epg_pages_extent(struct mbuf *m, int idx, u_int pglen)
254 for (i = idx + 1; i < m->m_epg_npgs; i++) {
255 if (m->m_epg_pa[i - 1] + PAGE_SIZE != m->m_epg_pa[i])
257 len += m_epg_pagelen(m, i, 0);
263 m_epg_segment(struct mbuf *m, size_t offset, size_t *len)
265 u_int i, pglen, pgoff;
267 offset += mtod(m, vm_offset_t);
268 if (offset < m->m_epg_hdrlen) {
269 *len = m->m_epg_hdrlen - offset;
270 return (m->m_epg_hdr + offset);
272 offset -= m->m_epg_hdrlen;
273 pgoff = m->m_epg_1st_off;
274 for (i = 0; i < m->m_epg_npgs; i++) {
275 pglen = m_epg_pagelen(m, i, pgoff);
276 if (offset < pglen) {
277 *len = m_epg_pages_extent(m, i, pglen) - offset;
278 return ((void *)PHYS_TO_DMAP(m->m_epg_pa[i] + pgoff +
284 KASSERT(offset <= m->m_epg_trllen, ("%s: offset beyond trailer",
286 *len = m->m_epg_trllen - offset;
287 return (m->m_epg_trail + offset);
290 static __inline void *
291 m_epg_contiguous_subsegment(struct mbuf *m, size_t skip, size_t len)
296 base = m_epg_segment(m, skip, &seglen);
303 crypto_cursor_init(struct crypto_buffer_cursor *cc,
304 const struct crypto_buffer *cb)
306 memset(cc, 0, sizeof(*cc));
307 cc->cc_type = cb->cb_type;
308 switch (cc->cc_type) {
309 case CRYPTO_BUF_CONTIG:
310 cc->cc_buf = cb->cb_buf;
311 cc->cc_buf_len = cb->cb_buf_len;
313 case CRYPTO_BUF_MBUF:
314 case CRYPTO_BUF_SINGLE_MBUF:
315 cc->cc_mbuf = cb->cb_mbuf;
317 case CRYPTO_BUF_VMPAGE:
318 cc->cc_vmpage = cb->cb_vm_page;
319 cc->cc_buf_len = cb->cb_vm_page_len;
320 cc->cc_offset = cb->cb_vm_page_offset;
323 cc->cc_iov = cb->cb_uio->uio_iov;
327 panic("%s: invalid buffer type %d", __func__, cb->cb_type);
333 SDT_PROBE_DEFINE2(opencrypto, criov, cursor_advance, vmpage, "struct crypto_buffer_cursor*", "size_t");
336 crypto_cursor_advance(struct crypto_buffer_cursor *cc, size_t amount)
340 switch (cc->cc_type) {
341 case CRYPTO_BUF_CONTIG:
342 MPASS(cc->cc_buf_len >= amount);
343 cc->cc_buf += amount;
344 cc->cc_buf_len -= amount;
346 case CRYPTO_BUF_MBUF:
348 remain = cc->cc_mbuf->m_len - cc->cc_offset;
349 if (amount < remain) {
350 cc->cc_offset += amount;
354 cc->cc_mbuf = cc->cc_mbuf->m_next;
360 case CRYPTO_BUF_SINGLE_MBUF:
361 MPASS(cc->cc_mbuf->m_len >= cc->cc_offset + amount);
362 cc->cc_offset += amount;
364 case CRYPTO_BUF_VMPAGE:
366 SDT_PROBE2(opencrypto, criov, cursor_advance, vmpage,
368 remain = MIN(PAGE_SIZE - cc->cc_offset, cc->cc_buf_len);
369 if (amount < remain) {
370 cc->cc_buf_len -= amount;
371 cc->cc_offset += amount;
374 cc->cc_buf_len -= remain;
378 if (amount == 0 || cc->cc_buf_len == 0)
384 remain = cc->cc_iov->iov_len - cc->cc_offset;
385 if (amount < remain) {
386 cc->cc_offset += amount;
398 panic("%s: invalid buffer type %d", __func__, cc->cc_type);
405 crypto_cursor_segment(struct crypto_buffer_cursor *cc, size_t *len)
407 switch (cc->cc_type) {
408 case CRYPTO_BUF_CONTIG:
409 *len = cc->cc_buf_len;
411 case CRYPTO_BUF_MBUF:
412 case CRYPTO_BUF_SINGLE_MBUF:
413 if (cc->cc_mbuf == NULL) {
417 if (cc->cc_mbuf->m_flags & M_EXTPG)
418 return (m_epg_segment(cc->cc_mbuf, cc->cc_offset, len));
419 *len = cc->cc_mbuf->m_len - cc->cc_offset;
420 return (mtod(cc->cc_mbuf, char *) + cc->cc_offset);
421 case CRYPTO_BUF_VMPAGE:
422 *len = PAGE_SIZE - cc->cc_offset;
423 return ((char *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(
424 *cc->cc_vmpage)) + cc->cc_offset);
426 *len = cc->cc_iov->iov_len - cc->cc_offset;
427 return ((char *)cc->cc_iov->iov_base + cc->cc_offset);
430 panic("%s: invalid buffer type %d", __func__, cc->cc_type);
438 crypto_cursor_copyback(struct crypto_buffer_cursor *cc, int size,
446 switch (cc->cc_type) {
447 case CRYPTO_BUF_CONTIG:
448 MPASS(cc->cc_buf_len >= size);
449 memcpy(cc->cc_buf, src, size);
451 cc->cc_buf_len -= size;
453 case CRYPTO_BUF_MBUF:
456 * This uses m_copyback() for individual
457 * mbufs so that cc_mbuf and cc_offset are
460 remain = cc->cc_mbuf->m_len - cc->cc_offset;
461 todo = MIN(remain, size);
462 m_copyback(cc->cc_mbuf, cc->cc_offset, todo, src);
465 cc->cc_offset += todo;
469 cc->cc_mbuf = cc->cc_mbuf->m_next;
475 case CRYPTO_BUF_SINGLE_MBUF:
476 MPASS(cc->cc_mbuf->m_len >= cc->cc_offset + size);
477 m_copyback(cc->cc_mbuf, cc->cc_offset, size, src);
478 cc->cc_offset += size;
480 case CRYPTO_BUF_VMPAGE:
482 dst = (char *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(
483 *cc->cc_vmpage)) + cc->cc_offset;
484 remain = MIN(PAGE_SIZE - cc->cc_offset, cc->cc_buf_len);
485 todo = MIN(remain, size);
486 memcpy(dst, src, todo);
488 cc->cc_buf_len -= todo;
490 cc->cc_offset += todo;
502 dst = (char *)cc->cc_iov->iov_base + cc->cc_offset;
503 remain = cc->cc_iov->iov_len - cc->cc_offset;
504 todo = MIN(remain, size);
505 memcpy(dst, src, todo);
508 cc->cc_offset += todo;
520 panic("%s: invalid buffer type %d", __func__, cc->cc_type);
527 crypto_cursor_copydata(struct crypto_buffer_cursor *cc, int size, void *vdst)
534 switch (cc->cc_type) {
535 case CRYPTO_BUF_CONTIG:
536 MPASS(cc->cc_buf_len >= size);
537 memcpy(dst, cc->cc_buf, size);
539 cc->cc_buf_len -= size;
541 case CRYPTO_BUF_MBUF:
544 * This uses m_copydata() for individual
545 * mbufs so that cc_mbuf and cc_offset are
548 remain = cc->cc_mbuf->m_len - cc->cc_offset;
549 todo = MIN(remain, size);
550 m_copydata(cc->cc_mbuf, cc->cc_offset, todo, dst);
553 cc->cc_offset += todo;
557 cc->cc_mbuf = cc->cc_mbuf->m_next;
563 case CRYPTO_BUF_SINGLE_MBUF:
564 MPASS(cc->cc_mbuf->m_len >= cc->cc_offset + size);
565 m_copydata(cc->cc_mbuf, cc->cc_offset, size, dst);
566 cc->cc_offset += size;
568 case CRYPTO_BUF_VMPAGE:
570 src = (char *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(
571 *cc->cc_vmpage)) + cc->cc_offset;
572 remain = MIN(PAGE_SIZE - cc->cc_offset, cc->cc_buf_len);
573 todo = MIN(remain, size);
574 memcpy(dst, src, todo);
576 cc->cc_buf_len -= todo;
578 cc->cc_offset += todo;
590 src = (const char *)cc->cc_iov->iov_base +
592 remain = cc->cc_iov->iov_len - cc->cc_offset;
593 todo = MIN(remain, size);
594 memcpy(dst, src, todo);
597 cc->cc_offset += todo;
609 panic("%s: invalid buffer type %d", __func__, cc->cc_type);
616 * To avoid advancing 'cursor', make a local copy that gets advanced
620 crypto_cursor_copydata_noadv(struct crypto_buffer_cursor *cc, int size,
623 struct crypto_buffer_cursor copy;
626 crypto_cursor_copydata(©, size, vdst);
630 * Apply function f to the data in an iovec list starting "off" bytes from
631 * the beginning, continuing for "len" bytes.
634 cuio_apply(struct uio *uio, int off, int len,
635 int (*f)(void *, const void *, u_int), void *arg)
637 struct iovec *iov = uio->uio_iov;
638 int iol = uio->uio_iovcnt;
644 KASSERT(iol >= 0, ("%s: empty", __func__));
645 count = min(iov->iov_len - off, len);
646 rval = (*f)(arg, ((caddr_t)iov->iov_base) + off, count);
658 crypto_copyback(struct cryptop *crp, int off, int size, const void *src)
660 struct crypto_buffer *cb;
662 if (crp->crp_obuf.cb_type != CRYPTO_BUF_NONE)
666 switch (cb->cb_type) {
667 case CRYPTO_BUF_MBUF:
668 case CRYPTO_BUF_SINGLE_MBUF:
669 m_copyback(cb->cb_mbuf, off, size, src);
671 #if CRYPTO_MAY_HAVE_VMPAGE
672 case CRYPTO_BUF_VMPAGE:
673 MPASS(size <= cb->cb_vm_page_len);
675 cb->cb_vm_page_len + cb->cb_vm_page_offset);
676 cvm_page_copyback(cb->cb_vm_page,
677 off + cb->cb_vm_page_offset, size, src);
679 #endif /* CRYPTO_MAY_HAVE_VMPAGE */
681 cuio_copyback(cb->cb_uio, off, size, src);
683 case CRYPTO_BUF_CONTIG:
684 MPASS(off + size <= cb->cb_buf_len);
685 bcopy(src, cb->cb_buf + off, size);
689 panic("invalid crp buf type %d", cb->cb_type);
696 crypto_copydata(struct cryptop *crp, int off, int size, void *dst)
699 switch (crp->crp_buf.cb_type) {
700 case CRYPTO_BUF_MBUF:
701 case CRYPTO_BUF_SINGLE_MBUF:
702 m_copydata(crp->crp_buf.cb_mbuf, off, size, dst);
704 #if CRYPTO_MAY_HAVE_VMPAGE
705 case CRYPTO_BUF_VMPAGE:
706 MPASS(size <= crp->crp_buf.cb_vm_page_len);
707 MPASS(size + off <= crp->crp_buf.cb_vm_page_len +
708 crp->crp_buf.cb_vm_page_offset);
709 cvm_page_copydata(crp->crp_buf.cb_vm_page,
710 off + crp->crp_buf.cb_vm_page_offset, size, dst);
712 #endif /* CRYPTO_MAY_HAVE_VMPAGE */
714 cuio_copydata(crp->crp_buf.cb_uio, off, size, dst);
716 case CRYPTO_BUF_CONTIG:
717 MPASS(off + size <= crp->crp_buf.cb_buf_len);
718 bcopy(crp->crp_buf.cb_buf + off, dst, size);
722 panic("invalid crp buf type %d", crp->crp_buf.cb_type);
729 crypto_apply_buf(struct crypto_buffer *cb, int off, int len,
730 int (*f)(void *, const void *, u_int), void *arg)
734 switch (cb->cb_type) {
735 case CRYPTO_BUF_MBUF:
736 case CRYPTO_BUF_SINGLE_MBUF:
737 error = m_apply(cb->cb_mbuf, off, len,
738 (int (*)(void *, void *, u_int))f, arg);
741 error = cuio_apply(cb->cb_uio, off, len, f, arg);
743 #if CRYPTO_MAY_HAVE_VMPAGE
744 case CRYPTO_BUF_VMPAGE:
745 error = cvm_page_apply(cb->cb_vm_page,
746 off + cb->cb_vm_page_offset, len, f, arg);
748 #endif /* CRYPTO_MAY_HAVE_VMPAGE */
749 case CRYPTO_BUF_CONTIG:
750 MPASS(off + len <= cb->cb_buf_len);
751 error = (*f)(arg, cb->cb_buf + off, len);
755 panic("invalid crypto buf type %d", cb->cb_type);
764 crypto_apply(struct cryptop *crp, int off, int len,
765 int (*f)(void *, const void *, u_int), void *arg)
767 return (crypto_apply_buf(&crp->crp_buf, off, len, f, arg));
771 m_contiguous_subsegment(struct mbuf *m, size_t skip, size_t len)
775 MPASS(skip <= INT_MAX);
777 m = m_getptr(m, (int)skip, &rel_off);
783 if (skip + len > m->m_len)
786 if (m->m_flags & M_EXTPG)
787 return (m_epg_contiguous_subsegment(m, skip, len));
788 return (mtod(m, char*) + skip);
792 cuio_contiguous_segment(struct uio *uio, size_t skip, size_t len)
796 MPASS(skip <= INT_MAX);
797 idx = cuio_getptr(uio, (int)skip, &rel_off);
803 if (skip + len > uio->uio_iov[idx].iov_len)
805 return ((char *)uio->uio_iov[idx].iov_base + skip);
809 crypto_buffer_contiguous_subsegment(struct crypto_buffer *cb, size_t skip,
813 switch (cb->cb_type) {
814 case CRYPTO_BUF_MBUF:
815 case CRYPTO_BUF_SINGLE_MBUF:
816 return (m_contiguous_subsegment(cb->cb_mbuf, skip, len));
818 return (cuio_contiguous_segment(cb->cb_uio, skip, len));
819 #if CRYPTO_MAY_HAVE_VMPAGE
820 case CRYPTO_BUF_VMPAGE:
821 MPASS(skip + len <= cb->cb_vm_page_len);
822 return (cvm_page_contiguous_segment(cb->cb_vm_page,
823 skip + cb->cb_vm_page_offset, len));
824 #endif /* CRYPTO_MAY_HAVE_VMPAGE */
825 case CRYPTO_BUF_CONTIG:
826 MPASS(skip + len <= cb->cb_buf_len);
827 return (cb->cb_buf + skip);
830 panic("invalid crp buf type %d", cb->cb_type);
837 crypto_contiguous_subsegment(struct cryptop *crp, size_t skip, size_t len)
839 return (crypto_buffer_contiguous_subsegment(&crp->crp_buf, skip, len));