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_copyback(struct crypto_buffer_cursor *cc, int size,
445 switch (cc->cc_type) {
446 case CRYPTO_BUF_CONTIG:
447 MPASS(cc->cc_buf_len >= size);
448 memcpy(cc->cc_buf, src, size);
450 cc->cc_buf_len -= size;
452 case CRYPTO_BUF_MBUF:
455 * This uses m_copyback() for individual
456 * mbufs so that cc_mbuf and cc_offset are
459 remain = cc->cc_mbuf->m_len - cc->cc_offset;
460 todo = MIN(remain, size);
461 m_copyback(cc->cc_mbuf, cc->cc_offset, todo, src);
464 cc->cc_offset += todo;
468 cc->cc_mbuf = cc->cc_mbuf->m_next;
474 case CRYPTO_BUF_SINGLE_MBUF:
475 MPASS(cc->cc_mbuf->m_len >= cc->cc_offset + size);
476 m_copyback(cc->cc_mbuf, cc->cc_offset, size, src);
477 cc->cc_offset += size;
479 case CRYPTO_BUF_VMPAGE:
481 dst = (char *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(
482 *cc->cc_vmpage)) + cc->cc_offset;
483 remain = MIN(PAGE_SIZE - cc->cc_offset, cc->cc_buf_len);
484 todo = MIN(remain, size);
485 memcpy(dst, src, todo);
487 cc->cc_buf_len -= todo;
489 cc->cc_offset += todo;
501 dst = (char *)cc->cc_iov->iov_base + cc->cc_offset;
502 remain = cc->cc_iov->iov_len - cc->cc_offset;
503 todo = MIN(remain, size);
504 memcpy(dst, src, todo);
507 cc->cc_offset += todo;
519 panic("%s: invalid buffer type %d", __func__, cc->cc_type);
526 crypto_cursor_copydata(struct crypto_buffer_cursor *cc, int size, void *vdst)
533 switch (cc->cc_type) {
534 case CRYPTO_BUF_CONTIG:
535 MPASS(cc->cc_buf_len >= size);
536 memcpy(dst, cc->cc_buf, size);
538 cc->cc_buf_len -= size;
540 case CRYPTO_BUF_MBUF:
543 * This uses m_copydata() for individual
544 * mbufs so that cc_mbuf and cc_offset are
547 remain = cc->cc_mbuf->m_len - cc->cc_offset;
548 todo = MIN(remain, size);
549 m_copydata(cc->cc_mbuf, cc->cc_offset, todo, dst);
552 cc->cc_offset += todo;
556 cc->cc_mbuf = cc->cc_mbuf->m_next;
562 case CRYPTO_BUF_SINGLE_MBUF:
563 MPASS(cc->cc_mbuf->m_len >= cc->cc_offset + size);
564 m_copydata(cc->cc_mbuf, cc->cc_offset, size, dst);
565 cc->cc_offset += size;
567 case CRYPTO_BUF_VMPAGE:
569 src = (char *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(
570 *cc->cc_vmpage)) + cc->cc_offset;
571 remain = MIN(PAGE_SIZE - cc->cc_offset, cc->cc_buf_len);
572 todo = MIN(remain, size);
573 memcpy(dst, src, todo);
575 cc->cc_buf_len -= todo;
577 cc->cc_offset += todo;
589 src = (const char *)cc->cc_iov->iov_base +
591 remain = cc->cc_iov->iov_len - cc->cc_offset;
592 todo = MIN(remain, size);
593 memcpy(dst, src, todo);
596 cc->cc_offset += todo;
608 panic("%s: invalid buffer type %d", __func__, cc->cc_type);
615 * To avoid advancing 'cursor', make a local copy that gets advanced
619 crypto_cursor_copydata_noadv(struct crypto_buffer_cursor *cc, int size,
622 struct crypto_buffer_cursor copy;
625 crypto_cursor_copydata(©, size, vdst);
629 * Apply function f to the data in an iovec list starting "off" bytes from
630 * the beginning, continuing for "len" bytes.
633 cuio_apply(struct uio *uio, int off, int len,
634 int (*f)(void *, const void *, u_int), void *arg)
636 struct iovec *iov = uio->uio_iov;
637 int iol = uio->uio_iovcnt;
643 KASSERT(iol >= 0, ("%s: empty", __func__));
644 count = min(iov->iov_len - off, len);
645 rval = (*f)(arg, ((caddr_t)iov->iov_base) + off, count);
657 crypto_copyback(struct cryptop *crp, int off, int size, const void *src)
659 struct crypto_buffer *cb;
661 if (crp->crp_obuf.cb_type != CRYPTO_BUF_NONE)
665 switch (cb->cb_type) {
666 case CRYPTO_BUF_MBUF:
667 case CRYPTO_BUF_SINGLE_MBUF:
668 m_copyback(cb->cb_mbuf, off, size, src);
670 #if CRYPTO_MAY_HAVE_VMPAGE
671 case CRYPTO_BUF_VMPAGE:
672 MPASS(size <= cb->cb_vm_page_len);
674 cb->cb_vm_page_len + cb->cb_vm_page_offset);
675 cvm_page_copyback(cb->cb_vm_page,
676 off + cb->cb_vm_page_offset, size, src);
678 #endif /* CRYPTO_MAY_HAVE_VMPAGE */
680 cuio_copyback(cb->cb_uio, off, size, src);
682 case CRYPTO_BUF_CONTIG:
683 MPASS(off + size <= cb->cb_buf_len);
684 bcopy(src, cb->cb_buf + off, size);
688 panic("invalid crp buf type %d", cb->cb_type);
695 crypto_copydata(struct cryptop *crp, int off, int size, void *dst)
698 switch (crp->crp_buf.cb_type) {
699 case CRYPTO_BUF_MBUF:
700 case CRYPTO_BUF_SINGLE_MBUF:
701 m_copydata(crp->crp_buf.cb_mbuf, off, size, dst);
703 #if CRYPTO_MAY_HAVE_VMPAGE
704 case CRYPTO_BUF_VMPAGE:
705 MPASS(size <= crp->crp_buf.cb_vm_page_len);
706 MPASS(size + off <= crp->crp_buf.cb_vm_page_len +
707 crp->crp_buf.cb_vm_page_offset);
708 cvm_page_copydata(crp->crp_buf.cb_vm_page,
709 off + crp->crp_buf.cb_vm_page_offset, size, dst);
711 #endif /* CRYPTO_MAY_HAVE_VMPAGE */
713 cuio_copydata(crp->crp_buf.cb_uio, off, size, dst);
715 case CRYPTO_BUF_CONTIG:
716 MPASS(off + size <= crp->crp_buf.cb_buf_len);
717 bcopy(crp->crp_buf.cb_buf + off, dst, size);
721 panic("invalid crp buf type %d", crp->crp_buf.cb_type);
728 crypto_apply_buf(struct crypto_buffer *cb, int off, int len,
729 int (*f)(void *, const void *, u_int), void *arg)
733 switch (cb->cb_type) {
734 case CRYPTO_BUF_MBUF:
735 case CRYPTO_BUF_SINGLE_MBUF:
736 error = m_apply(cb->cb_mbuf, off, len,
737 (int (*)(void *, void *, u_int))f, arg);
740 error = cuio_apply(cb->cb_uio, off, len, f, arg);
742 #if CRYPTO_MAY_HAVE_VMPAGE
743 case CRYPTO_BUF_VMPAGE:
744 error = cvm_page_apply(cb->cb_vm_page,
745 off + cb->cb_vm_page_offset, len, f, arg);
747 #endif /* CRYPTO_MAY_HAVE_VMPAGE */
748 case CRYPTO_BUF_CONTIG:
749 MPASS(off + len <= cb->cb_buf_len);
750 error = (*f)(arg, cb->cb_buf + off, len);
754 panic("invalid crypto buf type %d", cb->cb_type);
763 crypto_apply(struct cryptop *crp, int off, int len,
764 int (*f)(void *, const void *, u_int), void *arg)
766 return (crypto_apply_buf(&crp->crp_buf, off, len, f, arg));
770 m_contiguous_subsegment(struct mbuf *m, size_t skip, size_t len)
774 MPASS(skip <= INT_MAX);
776 m = m_getptr(m, (int)skip, &rel_off);
782 if (skip + len > m->m_len)
785 if (m->m_flags & M_EXTPG)
786 return (m_epg_contiguous_subsegment(m, skip, len));
787 return (mtod(m, char*) + skip);
791 cuio_contiguous_segment(struct uio *uio, size_t skip, size_t len)
795 MPASS(skip <= INT_MAX);
796 idx = cuio_getptr(uio, (int)skip, &rel_off);
802 if (skip + len > uio->uio_iov[idx].iov_len)
804 return ((char *)uio->uio_iov[idx].iov_base + skip);
808 crypto_buffer_contiguous_subsegment(struct crypto_buffer *cb, size_t skip,
812 switch (cb->cb_type) {
813 case CRYPTO_BUF_MBUF:
814 case CRYPTO_BUF_SINGLE_MBUF:
815 return (m_contiguous_subsegment(cb->cb_mbuf, skip, len));
817 return (cuio_contiguous_segment(cb->cb_uio, skip, len));
818 #if CRYPTO_MAY_HAVE_VMPAGE
819 case CRYPTO_BUF_VMPAGE:
820 MPASS(skip + len <= cb->cb_vm_page_len);
821 return (cvm_page_contiguous_segment(cb->cb_vm_page,
822 skip + cb->cb_vm_page_offset, len));
823 #endif /* CRYPTO_MAY_HAVE_VMPAGE */
824 case CRYPTO_BUF_CONTIG:
825 MPASS(skip + len <= cb->cb_buf_len);
826 return (cb->cb_buf + skip);
829 panic("invalid crp buf type %d", cb->cb_type);
836 crypto_contiguous_subsegment(struct cryptop *crp, size_t skip, size_t len)
838 return (crypto_buffer_contiguous_subsegment(&crp->crp_buf, skip, len));