uvm_io.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* $OpenBSD: uvm_io.c,v 1.25 2015/03/14 03:38:53 jsg Exp $ */
  2. /* $NetBSD: uvm_io.c,v 1.12 2000/06/27 17:29:23 mrg Exp $ */
  3. /*
  4. * Copyright (c) 1997 Charles D. Cranor and Washington University.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  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. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  18. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  19. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  21. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  25. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. *
  27. * from: Id: uvm_io.c,v 1.1.2.2 1997/12/30 12:02:00 mrg Exp
  28. */
  29. /*
  30. * uvm_io.c: uvm i/o ops
  31. */
  32. #include <sys/param.h>
  33. #include <sys/systm.h>
  34. #include <sys/mman.h>
  35. #include <sys/uio.h>
  36. #include <uvm/uvm.h>
  37. /*
  38. * functions
  39. */
  40. /*
  41. * uvm_io: perform I/O on a map
  42. *
  43. * => caller must have a reference to "map" so that it doesn't go away
  44. * while we are working.
  45. */
  46. int
  47. uvm_io(vm_map_t map, struct uio *uio, int flags)
  48. {
  49. vaddr_t baseva, endva, pageoffset, kva;
  50. vsize_t chunksz, togo, sz;
  51. struct uvm_map_deadq dead_entries;
  52. int error, extractflags;
  53. /*
  54. * step 0: sanity checks and set up for copy loop. start with a
  55. * large chunk size. if we have trouble finding vm space we will
  56. * reduce it.
  57. */
  58. if (uio->uio_resid == 0)
  59. return(0);
  60. togo = uio->uio_resid;
  61. baseva = (vaddr_t) uio->uio_offset;
  62. endva = baseva + (togo - 1);
  63. if (endva < baseva) /* wrap around? */
  64. return(EIO);
  65. if (baseva >= VM_MAXUSER_ADDRESS)
  66. return(0);
  67. if (endva >= VM_MAXUSER_ADDRESS)
  68. /* EOF truncate */
  69. togo = togo - (endva - VM_MAXUSER_ADDRESS + 1);
  70. pageoffset = baseva & PAGE_MASK;
  71. baseva = trunc_page(baseva);
  72. chunksz = min(round_page(togo + pageoffset), MAXBSIZE);
  73. error = 0;
  74. extractflags = 0;
  75. if (flags & UVM_IO_FIXPROT)
  76. extractflags |= UVM_EXTRACT_FIXPROT;
  77. /* step 1: main loop... while we've got data to move */
  78. for (/*null*/; togo > 0 ; pageoffset = 0) {
  79. /* step 2: extract mappings from the map into kernel_map */
  80. error = uvm_map_extract(map, baseva, chunksz, &kva,
  81. extractflags);
  82. if (error) {
  83. /* retry with a smaller chunk... */
  84. if (error == ENOMEM && chunksz > PAGE_SIZE) {
  85. chunksz = trunc_page(chunksz / 2);
  86. if (chunksz < PAGE_SIZE)
  87. chunksz = PAGE_SIZE;
  88. continue;
  89. }
  90. break;
  91. }
  92. /* step 3: move a chunk of data */
  93. sz = chunksz - pageoffset;
  94. if (sz > togo)
  95. sz = togo;
  96. error = uiomovei((caddr_t) (kva + pageoffset), sz, uio);
  97. togo -= sz;
  98. baseva += chunksz;
  99. /* step 4: unmap the area of kernel memory */
  100. vm_map_lock(kernel_map);
  101. TAILQ_INIT(&dead_entries);
  102. uvm_unmap_remove(kernel_map, kva, kva+chunksz,
  103. &dead_entries, FALSE, TRUE);
  104. vm_map_unlock(kernel_map);
  105. uvm_unmap_detach(&dead_entries, AMAP_REFALL);
  106. /*
  107. * We defer checking the error return from uiomove until
  108. * here so that we won't leak memory.
  109. */
  110. if (error)
  111. break;
  112. }
  113. return (error);
  114. }