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/param.h>
31 #include <sys/systm.h>
33 #include <sys/errno.h>
34 #include <sys/malloc.h>
35 #include <sys/kernel.h>
38 #include <sys/limits.h>
42 #include <machine/vmparam.h>
45 #include <vm/vm_page.h>
48 #include <opencrypto/cryptodev.h>
50 SDT_PROVIDER_DECLARE(opencrypto);
53 * These macros are only for avoiding code duplication, as we need to skip
54 * given number of bytes in the same way in several functions below.
56 #define CUIO_SKIP() do { \
57 KASSERT(off >= 0, ("%s: off %d < 0", __func__, off)); \
58 KASSERT(len >= 0, ("%s: len %d < 0", __func__, len)); \
60 KASSERT(iol >= 0, ("%s: empty in skip", __func__)); \
61 if (off < iov->iov_len) \
63 off -= iov->iov_len; \
69 #define CVM_PAGE_SKIP() do { \
70 KASSERT(off >= 0, ("%s: off %d < 0", __func__, off)); \
71 KASSERT(len >= 0, ("%s: len %d < 0", __func__, len)); \
73 if (off < PAGE_SIZE) \
75 processed += PAGE_SIZE - off; \
76 off -= PAGE_SIZE - off; \
82 cuio_copydata(struct uio* uio, int off, int len, caddr_t cp)
84 struct iovec *iov = uio->uio_iov;
85 int iol __diagused = uio->uio_iovcnt;
90 KASSERT(iol >= 0, ("%s: empty", __func__));
91 count = min(iov->iov_len - off, len);
92 bcopy(((caddr_t)iov->iov_base) + off, cp, count);
102 cuio_copyback(struct uio* uio, int off, int len, c_caddr_t cp)
104 struct iovec *iov = uio->uio_iov;
105 int iol __diagused = uio->uio_iovcnt;
110 KASSERT(iol >= 0, ("%s: empty", __func__));
111 count = min(iov->iov_len - off, len);
112 bcopy(cp, ((caddr_t)iov->iov_base) + off, count);
122 * Return the index and offset of location in iovec list.
125 cuio_getptr(struct uio *uio, int loc, int *off)
130 while (loc >= 0 && ind < uio->uio_iovcnt) {
131 len = uio->uio_iov[ind].iov_len;
140 if (ind > 0 && loc == 0) {
142 *off = uio->uio_iov[ind].iov_len;
149 #if CRYPTO_MAY_HAVE_VMPAGE
151 * Apply function f to the data in a vm_page_t list starting "off" bytes from
152 * the beginning, continuing for "len" bytes.
155 cvm_page_apply(vm_page_t *pages, int off, int len,
156 int (*f)(void *, const void *, u_int), void *arg)
158 int processed __unused;
165 char *kaddr = (char *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(*pages));
166 count = min(PAGE_SIZE - off, len);
167 rval = (*f)(arg, kaddr + off, count);
179 cvm_page_contiguous_segment(vm_page_t *pages, size_t skip, int len)
181 if ((skip + len - 1) / PAGE_SIZE > skip / PAGE_SIZE)
184 pages += (skip / PAGE_SIZE);
185 skip -= rounddown(skip, PAGE_SIZE);
186 return (((char *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(*pages))) + skip);
190 * Copy len bytes of data from the vm_page_t array, skipping the first off
191 * bytes, into the pointer cp. Return the number of bytes skipped and copied.
192 * Does not verify the length of the array.
195 cvm_page_copyback(vm_page_t *pages, int off, int len, c_caddr_t cp)
202 count = min(PAGE_SIZE - off, len);
203 bcopy(cp, (char *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(*pages)) + off,
215 * Copy len bytes of data from the pointer cp into the vm_page_t array,
216 * skipping the first off bytes, Return the number of bytes skipped and copied.
217 * Does not verify the length of the array.
220 cvm_page_copydata(vm_page_t *pages, int off, int len, caddr_t cp)
227 count = min(PAGE_SIZE - off, len);
228 bcopy(((char *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(*pages)) + off), cp,
238 #endif /* CRYPTO_MAY_HAVE_VMPAGE */
241 * Given a starting page in an m_epg, determine the length of the
242 * current physically contiguous segment.
244 static __inline size_t
245 m_epg_pages_extent(struct mbuf *m, int idx, u_int pglen)
251 for (i = idx + 1; i < m->m_epg_npgs; i++) {
252 if (m->m_epg_pa[i - 1] + PAGE_SIZE != m->m_epg_pa[i])
254 len += m_epg_pagelen(m, i, 0);
260 m_epg_segment(struct mbuf *m, size_t offset, size_t *len)
262 u_int i, pglen, pgoff;
264 offset += mtod(m, vm_offset_t);
265 if (offset < m->m_epg_hdrlen) {
266 *len = m->m_epg_hdrlen - offset;
267 return (m->m_epg_hdr + offset);
269 offset -= m->m_epg_hdrlen;
270 pgoff = m->m_epg_1st_off;
271 for (i = 0; i < m->m_epg_npgs; i++) {
272 pglen = m_epg_pagelen(m, i, pgoff);
273 if (offset < pglen) {
274 *len = m_epg_pages_extent(m, i, pglen) - offset;
275 return ((void *)PHYS_TO_DMAP(m->m_epg_pa[i] + pgoff +
281 KASSERT(offset <= m->m_epg_trllen, ("%s: offset beyond trailer",
283 *len = m->m_epg_trllen - offset;
284 return (m->m_epg_trail + offset);
287 static __inline void *
288 m_epg_contiguous_subsegment(struct mbuf *m, size_t skip, size_t len)
293 base = m_epg_segment(m, skip, &seglen);
300 crypto_cursor_init(struct crypto_buffer_cursor *cc,
301 const struct crypto_buffer *cb)
303 memset(cc, 0, sizeof(*cc));
304 cc->cc_type = cb->cb_type;
305 switch (cc->cc_type) {
306 case CRYPTO_BUF_CONTIG:
307 cc->cc_buf = cb->cb_buf;
308 cc->cc_buf_len = cb->cb_buf_len;
310 case CRYPTO_BUF_MBUF:
311 case CRYPTO_BUF_SINGLE_MBUF:
312 cc->cc_mbuf = cb->cb_mbuf;
314 case CRYPTO_BUF_VMPAGE:
315 cc->cc_vmpage = cb->cb_vm_page;
316 cc->cc_buf_len = cb->cb_vm_page_len;
317 cc->cc_offset = cb->cb_vm_page_offset;
320 cc->cc_iov = cb->cb_uio->uio_iov;
321 cc->cc_buf_len = cb->cb_uio->uio_resid;
325 panic("%s: invalid buffer type %d", __func__, cb->cb_type);
331 SDT_PROBE_DEFINE2(opencrypto, criov, cursor_advance, vmpage, "struct crypto_buffer_cursor*", "size_t");
334 crypto_cursor_advance(struct crypto_buffer_cursor *cc, size_t amount)
338 switch (cc->cc_type) {
339 case CRYPTO_BUF_CONTIG:
340 MPASS(cc->cc_buf_len >= amount);
341 cc->cc_buf += amount;
342 cc->cc_buf_len -= amount;
344 case CRYPTO_BUF_MBUF:
346 remain = cc->cc_mbuf->m_len - cc->cc_offset;
347 if (amount < remain) {
348 cc->cc_offset += amount;
352 cc->cc_mbuf = cc->cc_mbuf->m_next;
358 case CRYPTO_BUF_SINGLE_MBUF:
359 MPASS(cc->cc_mbuf->m_len >= cc->cc_offset + amount);
360 cc->cc_offset += amount;
362 case CRYPTO_BUF_VMPAGE:
364 SDT_PROBE2(opencrypto, criov, cursor_advance, vmpage,
366 remain = MIN(PAGE_SIZE - cc->cc_offset, cc->cc_buf_len);
367 if (amount < remain) {
368 cc->cc_buf_len -= amount;
369 cc->cc_offset += amount;
372 cc->cc_buf_len -= remain;
376 if (amount == 0 || cc->cc_buf_len == 0)
382 remain = cc->cc_iov->iov_len - cc->cc_offset;
383 if (amount < remain) {
384 cc->cc_offset += amount;
387 cc->cc_buf_len -= remain;
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:
409 case CRYPTO_BUF_VMPAGE:
410 if (cc->cc_buf_len == 0) {
415 case CRYPTO_BUF_MBUF:
416 case CRYPTO_BUF_SINGLE_MBUF:
417 if (cc->cc_mbuf == NULL) {
424 panic("%s: invalid buffer type %d", __func__, cc->cc_type);
430 switch (cc->cc_type) {
431 case CRYPTO_BUF_CONTIG:
432 *len = cc->cc_buf_len;
434 case CRYPTO_BUF_MBUF:
435 case CRYPTO_BUF_SINGLE_MBUF:
436 if (cc->cc_mbuf->m_flags & M_EXTPG)
437 return (m_epg_segment(cc->cc_mbuf, cc->cc_offset, len));
438 *len = cc->cc_mbuf->m_len - cc->cc_offset;
439 return (mtod(cc->cc_mbuf, char *) + cc->cc_offset);
440 case CRYPTO_BUF_VMPAGE:
441 *len = PAGE_SIZE - cc->cc_offset;
442 return ((char *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(
443 *cc->cc_vmpage)) + cc->cc_offset);
445 *len = cc->cc_iov->iov_len - cc->cc_offset;
446 return ((char *)cc->cc_iov->iov_base + cc->cc_offset);
448 __assert_unreachable();
453 crypto_cursor_copyback(struct crypto_buffer_cursor *cc, int size,
461 switch (cc->cc_type) {
462 case CRYPTO_BUF_CONTIG:
463 MPASS(cc->cc_buf_len >= size);
464 memcpy(cc->cc_buf, src, size);
466 cc->cc_buf_len -= size;
468 case CRYPTO_BUF_MBUF:
471 * This uses m_copyback() for individual
472 * mbufs so that cc_mbuf and cc_offset are
475 remain = cc->cc_mbuf->m_len - cc->cc_offset;
476 todo = MIN(remain, size);
477 m_copyback(cc->cc_mbuf, cc->cc_offset, todo, src);
480 cc->cc_offset += todo;
484 cc->cc_mbuf = cc->cc_mbuf->m_next;
490 case CRYPTO_BUF_SINGLE_MBUF:
491 MPASS(cc->cc_mbuf->m_len >= cc->cc_offset + size);
492 m_copyback(cc->cc_mbuf, cc->cc_offset, size, src);
493 cc->cc_offset += size;
495 case CRYPTO_BUF_VMPAGE:
497 dst = (char *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(
498 *cc->cc_vmpage)) + cc->cc_offset;
499 remain = MIN(PAGE_SIZE - cc->cc_offset, cc->cc_buf_len);
500 todo = MIN(remain, size);
501 memcpy(dst, src, todo);
503 cc->cc_buf_len -= todo;
505 cc->cc_offset += todo;
517 dst = (char *)cc->cc_iov->iov_base + cc->cc_offset;
518 remain = cc->cc_iov->iov_len - cc->cc_offset;
519 todo = MIN(remain, size);
520 memcpy(dst, src, todo);
522 cc->cc_buf_len -= 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);
612 cc->cc_buf_len -= todo;
614 cc->cc_offset += todo;
626 panic("%s: invalid buffer type %d", __func__, cc->cc_type);
633 * To avoid advancing 'cursor', make a local copy that gets advanced
637 crypto_cursor_copydata_noadv(struct crypto_buffer_cursor *cc, int size,
640 struct crypto_buffer_cursor copy;
643 crypto_cursor_copydata(©, size, vdst);
647 * Apply function f to the data in an iovec list starting "off" bytes from
648 * the beginning, continuing for "len" bytes.
651 cuio_apply(struct uio *uio, int off, int len,
652 int (*f)(void *, const void *, u_int), void *arg)
654 struct iovec *iov = uio->uio_iov;
655 int iol __diagused = uio->uio_iovcnt;
661 KASSERT(iol >= 0, ("%s: empty", __func__));
662 count = min(iov->iov_len - off, len);
663 rval = (*f)(arg, ((caddr_t)iov->iov_base) + off, count);
675 crypto_copyback(struct cryptop *crp, int off, int size, const void *src)
677 struct crypto_buffer *cb;
679 if (crp->crp_obuf.cb_type != CRYPTO_BUF_NONE)
683 switch (cb->cb_type) {
684 case CRYPTO_BUF_MBUF:
685 case CRYPTO_BUF_SINGLE_MBUF:
686 m_copyback(cb->cb_mbuf, off, size, src);
688 #if CRYPTO_MAY_HAVE_VMPAGE
689 case CRYPTO_BUF_VMPAGE:
690 MPASS(size <= cb->cb_vm_page_len);
692 cb->cb_vm_page_len + cb->cb_vm_page_offset);
693 cvm_page_copyback(cb->cb_vm_page,
694 off + cb->cb_vm_page_offset, size, src);
696 #endif /* CRYPTO_MAY_HAVE_VMPAGE */
698 cuio_copyback(cb->cb_uio, off, size, src);
700 case CRYPTO_BUF_CONTIG:
701 MPASS(off + size <= cb->cb_buf_len);
702 bcopy(src, cb->cb_buf + off, size);
706 panic("invalid crp buf type %d", cb->cb_type);
713 crypto_copydata(struct cryptop *crp, int off, int size, void *dst)
716 switch (crp->crp_buf.cb_type) {
717 case CRYPTO_BUF_MBUF:
718 case CRYPTO_BUF_SINGLE_MBUF:
719 m_copydata(crp->crp_buf.cb_mbuf, off, size, dst);
721 #if CRYPTO_MAY_HAVE_VMPAGE
722 case CRYPTO_BUF_VMPAGE:
723 MPASS(size <= crp->crp_buf.cb_vm_page_len);
724 MPASS(size + off <= crp->crp_buf.cb_vm_page_len +
725 crp->crp_buf.cb_vm_page_offset);
726 cvm_page_copydata(crp->crp_buf.cb_vm_page,
727 off + crp->crp_buf.cb_vm_page_offset, size, dst);
729 #endif /* CRYPTO_MAY_HAVE_VMPAGE */
731 cuio_copydata(crp->crp_buf.cb_uio, off, size, dst);
733 case CRYPTO_BUF_CONTIG:
734 MPASS(off + size <= crp->crp_buf.cb_buf_len);
735 bcopy(crp->crp_buf.cb_buf + off, dst, size);
739 panic("invalid crp buf type %d", crp->crp_buf.cb_type);
746 crypto_apply_buf(struct crypto_buffer *cb, int off, int len,
747 int (*f)(void *, const void *, u_int), void *arg)
751 switch (cb->cb_type) {
752 case CRYPTO_BUF_MBUF:
753 case CRYPTO_BUF_SINGLE_MBUF:
754 error = m_apply(cb->cb_mbuf, off, len,
755 (int (*)(void *, void *, u_int))f, arg);
758 error = cuio_apply(cb->cb_uio, off, len, f, arg);
760 #if CRYPTO_MAY_HAVE_VMPAGE
761 case CRYPTO_BUF_VMPAGE:
762 error = cvm_page_apply(cb->cb_vm_page,
763 off + cb->cb_vm_page_offset, len, f, arg);
765 #endif /* CRYPTO_MAY_HAVE_VMPAGE */
766 case CRYPTO_BUF_CONTIG:
767 MPASS(off + len <= cb->cb_buf_len);
768 error = (*f)(arg, cb->cb_buf + off, len);
772 panic("invalid crypto buf type %d", cb->cb_type);
781 crypto_apply(struct cryptop *crp, int off, int len,
782 int (*f)(void *, const void *, u_int), void *arg)
784 return (crypto_apply_buf(&crp->crp_buf, off, len, f, arg));
788 m_contiguous_subsegment(struct mbuf *m, size_t skip, size_t len)
792 MPASS(skip <= INT_MAX);
794 m = m_getptr(m, (int)skip, &rel_off);
800 if (skip + len > m->m_len)
803 if (m->m_flags & M_EXTPG)
804 return (m_epg_contiguous_subsegment(m, skip, len));
805 return (mtod(m, char*) + skip);
809 cuio_contiguous_segment(struct uio *uio, size_t skip, size_t len)
813 MPASS(skip <= INT_MAX);
814 idx = cuio_getptr(uio, (int)skip, &rel_off);
820 if (skip + len > uio->uio_iov[idx].iov_len)
822 return ((char *)uio->uio_iov[idx].iov_base + skip);
826 crypto_buffer_contiguous_subsegment(struct crypto_buffer *cb, size_t skip,
830 switch (cb->cb_type) {
831 case CRYPTO_BUF_MBUF:
832 case CRYPTO_BUF_SINGLE_MBUF:
833 return (m_contiguous_subsegment(cb->cb_mbuf, skip, len));
835 return (cuio_contiguous_segment(cb->cb_uio, skip, len));
836 #if CRYPTO_MAY_HAVE_VMPAGE
837 case CRYPTO_BUF_VMPAGE:
838 MPASS(skip + len <= cb->cb_vm_page_len);
839 return (cvm_page_contiguous_segment(cb->cb_vm_page,
840 skip + cb->cb_vm_page_offset, len));
841 #endif /* CRYPTO_MAY_HAVE_VMPAGE */
842 case CRYPTO_BUF_CONTIG:
843 MPASS(skip + len <= cb->cb_buf_len);
844 return (cb->cb_buf + skip);
847 panic("invalid crp buf type %d", cb->cb_type);
854 crypto_contiguous_subsegment(struct cryptop *crp, size_t skip, size_t len)
856 return (crypto_buffer_contiguous_subsegment(&crp->crp_buf, skip, len));