uio.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
  3. * The President and Fellows of Harvard College.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. #include <types.h>
  30. #include <lib.h>
  31. #include <uio.h>
  32. #include <proc.h>
  33. #include <current.h>
  34. #include <copyinout.h>
  35. /*
  36. * See uio.h for a description.
  37. */
  38. int
  39. uiomove(void *ptr, size_t n, struct uio *uio)
  40. {
  41. struct iovec *iov;
  42. size_t size;
  43. int result;
  44. if (uio->uio_rw != UIO_READ && uio->uio_rw != UIO_WRITE) {
  45. panic("uiomove: Invalid uio_rw %d\n", (int) uio->uio_rw);
  46. }
  47. if (uio->uio_segflg==UIO_SYSSPACE) {
  48. KASSERT(uio->uio_space == NULL);
  49. }
  50. else {
  51. KASSERT(uio->uio_space == curproc_getas());
  52. }
  53. while (n > 0 && uio->uio_resid > 0) {
  54. /* get the first iovec */
  55. iov = uio->uio_iov;
  56. size = iov->iov_len;
  57. if (size > n) {
  58. size = n;
  59. }
  60. if (size == 0) {
  61. /* move to the next iovec and try again */
  62. uio->uio_iov++;
  63. uio->uio_iovcnt--;
  64. if (uio->uio_iovcnt == 0) {
  65. /*
  66. * This should only happen if you set
  67. * uio_resid incorrectly (to more than
  68. * the total length of buffers the uio
  69. * points to).
  70. */
  71. panic("uiomove: ran out of buffers\n");
  72. }
  73. continue;
  74. }
  75. switch (uio->uio_segflg) {
  76. case UIO_SYSSPACE:
  77. result = 0;
  78. if (uio->uio_rw == UIO_READ) {
  79. memmove(iov->iov_kbase, ptr, size);
  80. }
  81. else {
  82. memmove(ptr, iov->iov_kbase, size);
  83. }
  84. iov->iov_kbase = ((char *)iov->iov_kbase+size);
  85. break;
  86. case UIO_USERSPACE:
  87. case UIO_USERISPACE:
  88. if (uio->uio_rw == UIO_READ) {
  89. result = copyout(ptr, iov->iov_ubase,size);
  90. }
  91. else {
  92. result = copyin(iov->iov_ubase, ptr, size);
  93. }
  94. if (result) {
  95. return result;
  96. }
  97. iov->iov_ubase += size;
  98. break;
  99. default:
  100. panic("uiomove: Invalid uio_segflg %d\n",
  101. (int)uio->uio_segflg);
  102. }
  103. iov->iov_len -= size;
  104. uio->uio_resid -= size;
  105. uio->uio_offset += size;
  106. ptr = ((char *)ptr + size);
  107. n -= size;
  108. }
  109. return 0;
  110. }
  111. int
  112. uiomovezeros(size_t n, struct uio *uio)
  113. {
  114. /* static, so initialized as zero */
  115. static char zeros[16];
  116. size_t amt;
  117. int result;
  118. /* This only makes sense when reading */
  119. KASSERT(uio->uio_rw == UIO_READ);
  120. while (n > 0) {
  121. amt = sizeof(zeros);
  122. if (amt > n) {
  123. amt = n;
  124. }
  125. result = uiomove(zeros, amt, uio);
  126. if (result) {
  127. return result;
  128. }
  129. n -= amt;
  130. }
  131. return 0;
  132. }
  133. /*
  134. * Convenience function to initialize an iovec and uio for kernel I/O.
  135. */
  136. void
  137. uio_kinit(struct iovec *iov, struct uio *u,
  138. void *kbuf, size_t len, off_t pos, enum uio_rw rw)
  139. {
  140. iov->iov_kbase = kbuf;
  141. iov->iov_len = len;
  142. u->uio_iov = iov;
  143. u->uio_iovcnt = 1;
  144. u->uio_offset = pos;
  145. u->uio_resid = len;
  146. u->uio_segflg = UIO_SYSSPACE;
  147. u->uio_rw = rw;
  148. u->uio_space = NULL;
  149. }