nommu-mmap.txt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. =============================
  2. NO-MMU MEMORY MAPPING SUPPORT
  3. =============================
  4. The kernel has limited support for memory mapping under no-MMU conditions, such
  5. as are used in uClinux environments. From the userspace point of view, memory
  6. mapping is made use of in conjunction with the mmap() system call, the shmat()
  7. call and the execve() system call. From the kernel's point of view, execve()
  8. mapping is actually performed by the binfmt drivers, which call back into the
  9. mmap() routines to do the actual work.
  10. Memory mapping behaviour also involves the way fork(), vfork(), clone() and
  11. ptrace() work. Under uClinux there is no fork(), and clone() must be supplied
  12. the CLONE_VM flag.
  13. The behaviour is similar between the MMU and no-MMU cases, but not identical;
  14. and it's also much more restricted in the latter case:
  15. (*) Anonymous mapping, MAP_PRIVATE
  16. In the MMU case: VM regions backed by arbitrary pages; copy-on-write
  17. across fork.
  18. In the no-MMU case: VM regions backed by arbitrary contiguous runs of
  19. pages.
  20. (*) Anonymous mapping, MAP_SHARED
  21. These behave very much like private mappings, except that they're
  22. shared across fork() or clone() without CLONE_VM in the MMU case. Since
  23. the no-MMU case doesn't support these, behaviour is identical to
  24. MAP_PRIVATE there.
  25. (*) File, MAP_PRIVATE, PROT_READ / PROT_EXEC, !PROT_WRITE
  26. In the MMU case: VM regions backed by pages read from file; changes to
  27. the underlying file are reflected in the mapping; copied across fork.
  28. In the no-MMU case:
  29. - If one exists, the kernel will re-use an existing mapping to the
  30. same segment of the same file if that has compatible permissions,
  31. even if this was created by another process.
  32. - If possible, the file mapping will be directly on the backing device
  33. if the backing device has the BDI_CAP_MAP_DIRECT capability and
  34. appropriate mapping protection capabilities. Ramfs, romfs, cramfs
  35. and mtd might all permit this.
  36. - If the backing device device can't or won't permit direct sharing,
  37. but does have the BDI_CAP_MAP_COPY capability, then a copy of the
  38. appropriate bit of the file will be read into a contiguous bit of
  39. memory and any extraneous space beyond the EOF will be cleared
  40. - Writes to the file do not affect the mapping; writes to the mapping
  41. are visible in other processes (no MMU protection), but should not
  42. happen.
  43. (*) File, MAP_PRIVATE, PROT_READ / PROT_EXEC, PROT_WRITE
  44. In the MMU case: like the non-PROT_WRITE case, except that the pages in
  45. question get copied before the write actually happens. From that point
  46. on writes to the file underneath that page no longer get reflected into
  47. the mapping's backing pages. The page is then backed by swap instead.
  48. In the no-MMU case: works much like the non-PROT_WRITE case, except
  49. that a copy is always taken and never shared.
  50. (*) Regular file / blockdev, MAP_SHARED, PROT_READ / PROT_EXEC / PROT_WRITE
  51. In the MMU case: VM regions backed by pages read from file; changes to
  52. pages written back to file; writes to file reflected into pages backing
  53. mapping; shared across fork.
  54. In the no-MMU case: not supported.
  55. (*) Memory backed regular file, MAP_SHARED, PROT_READ / PROT_EXEC / PROT_WRITE
  56. In the MMU case: As for ordinary regular files.
  57. In the no-MMU case: The filesystem providing the memory-backed file
  58. (such as ramfs or tmpfs) may choose to honour an open, truncate, mmap
  59. sequence by providing a contiguous sequence of pages to map. In that
  60. case, a shared-writable memory mapping will be possible. It will work
  61. as for the MMU case. If the filesystem does not provide any such
  62. support, then the mapping request will be denied.
  63. (*) Memory backed blockdev, MAP_SHARED, PROT_READ / PROT_EXEC / PROT_WRITE
  64. In the MMU case: As for ordinary regular files.
  65. In the no-MMU case: As for memory backed regular files, but the
  66. blockdev must be able to provide a contiguous run of pages without
  67. truncate being called. The ramdisk driver could do this if it allocated
  68. all its memory as a contiguous array upfront.
  69. (*) Memory backed chardev, MAP_SHARED, PROT_READ / PROT_EXEC / PROT_WRITE
  70. In the MMU case: As for ordinary regular files.
  71. In the no-MMU case: The character device driver may choose to honour
  72. the mmap() by providing direct access to the underlying device if it
  73. provides memory or quasi-memory that can be accessed directly. Examples
  74. of such are frame buffers and flash devices. If the driver does not
  75. provide any such support, then the mapping request will be denied.
  76. ============================
  77. FURTHER NOTES ON NO-MMU MMAP
  78. ============================
  79. (*) A request for a private mapping of a file may return a buffer that is not
  80. page-aligned. This is because XIP may take place, and the data may not be
  81. paged aligned in the backing store.
  82. (*) A request for an anonymous mapping will always be page aligned. If
  83. possible the size of the request should be a power of two otherwise some
  84. of the space may be wasted as the kernel must allocate a power-of-2
  85. granule but will only discard the excess if appropriately configured as
  86. this has an effect on fragmentation.
  87. (*) A list of all the private copy and anonymous mappings on the system is
  88. visible through /proc/maps in no-MMU mode.
  89. (*) A list of all the mappings in use by a process is visible through
  90. /proc/<pid>/maps in no-MMU mode.
  91. (*) Supplying MAP_FIXED or a requesting a particular mapping address will
  92. result in an error.
  93. (*) Files mapped privately usually have to have a read method provided by the
  94. driver or filesystem so that the contents can be read into the memory
  95. allocated if mmap() chooses not to map the backing device directly. An
  96. error will result if they don't. This is most likely to be encountered
  97. with character device files, pipes, fifos and sockets.
  98. ==========================
  99. INTERPROCESS SHARED MEMORY
  100. ==========================
  101. Both SYSV IPC SHM shared memory and POSIX shared memory is supported in NOMMU
  102. mode. The former through the usual mechanism, the latter through files created
  103. on ramfs or tmpfs mounts.
  104. =======
  105. FUTEXES
  106. =======
  107. Futexes are supported in NOMMU mode if the arch supports them. An error will
  108. be given if an address passed to the futex system call lies outside the
  109. mappings made by a process or if the mapping in which the address lies does not
  110. support futexes (such as an I/O chardev mapping).
  111. =============
  112. NO-MMU MREMAP
  113. =============
  114. The mremap() function is partially supported. It may change the size of a
  115. mapping, and may move it[*] if MREMAP_MAYMOVE is specified and if the new size
  116. of the mapping exceeds the size of the slab object currently occupied by the
  117. memory to which the mapping refers, or if a smaller slab object could be used.
  118. MREMAP_FIXED is not supported, though it is ignored if there's no change of
  119. address and the object does not need to be moved.
  120. Shared mappings may not be moved. Shareable mappings may not be moved either,
  121. even if they are not currently shared.
  122. The mremap() function must be given an exact match for base address and size of
  123. a previously mapped object. It may not be used to create holes in existing
  124. mappings, move parts of existing mappings or resize parts of mappings. It must
  125. act on a complete mapping.
  126. [*] Not currently supported.
  127. ============================================
  128. PROVIDING SHAREABLE CHARACTER DEVICE SUPPORT
  129. ============================================
  130. To provide shareable character device support, a driver must provide a
  131. file->f_op->get_unmapped_area() operation. The mmap() routines will call this
  132. to get a proposed address for the mapping. This may return an error if it
  133. doesn't wish to honour the mapping because it's too long, at a weird offset,
  134. under some unsupported combination of flags or whatever.
  135. The driver should also provide backing device information with capabilities set
  136. to indicate the permitted types of mapping on such devices. The default is
  137. assumed to be readable and writable, not executable, and only shareable
  138. directly (can't be copied).
  139. The file->f_op->mmap() operation will be called to actually inaugurate the
  140. mapping. It can be rejected at that point. Returning the ENOSYS error will
  141. cause the mapping to be copied instead if BDI_CAP_MAP_COPY is specified.
  142. The vm_ops->close() routine will be invoked when the last mapping on a chardev
  143. is removed. An existing mapping will be shared, partially or not, if possible
  144. without notifying the driver.
  145. It is permitted also for the file->f_op->get_unmapped_area() operation to
  146. return -ENOSYS. This will be taken to mean that this operation just doesn't
  147. want to handle it, despite the fact it's got an operation. For instance, it
  148. might try directing the call to a secondary driver which turns out not to
  149. implement it. Such is the case for the framebuffer driver which attempts to
  150. direct the call to the device-specific driver. Under such circumstances, the
  151. mapping request will be rejected if BDI_CAP_MAP_COPY is not specified, and a
  152. copy mapped otherwise.
  153. IMPORTANT NOTE:
  154. Some types of device may present a different appearance to anyone
  155. looking at them in certain modes. Flash chips can be like this; for
  156. instance if they're in programming or erase mode, you might see the
  157. status reflected in the mapping, instead of the data.
  158. In such a case, care must be taken lest userspace see a shared or a
  159. private mapping showing such information when the driver is busy
  160. controlling the device. Remember especially: private executable
  161. mappings may still be mapped directly off the device under some
  162. circumstances!
  163. ==============================================
  164. PROVIDING SHAREABLE MEMORY-BACKED FILE SUPPORT
  165. ==============================================
  166. Provision of shared mappings on memory backed files is similar to the provision
  167. of support for shared mapped character devices. The main difference is that the
  168. filesystem providing the service will probably allocate a contiguous collection
  169. of pages and permit mappings to be made on that.
  170. It is recommended that a truncate operation applied to such a file that
  171. increases the file size, if that file is empty, be taken as a request to gather
  172. enough pages to honour a mapping. This is required to support POSIX shared
  173. memory.
  174. Memory backed devices are indicated by the mapping's backing device info having
  175. the memory_backed flag set.
  176. ========================================
  177. PROVIDING SHAREABLE BLOCK DEVICE SUPPORT
  178. ========================================
  179. Provision of shared mappings on block device files is exactly the same as for
  180. character devices. If there isn't a real device underneath, then the driver
  181. should allocate sufficient contiguous memory to honour any supported mapping.
  182. =================================
  183. ADJUSTING PAGE TRIMMING BEHAVIOUR
  184. =================================
  185. NOMMU mmap automatically rounds up to the nearest power-of-2 number of pages
  186. when performing an allocation. This can have adverse effects on memory
  187. fragmentation, and as such, is left configurable. The default behaviour is to
  188. aggressively trim allocations and discard any excess pages back in to the page
  189. allocator. In order to retain finer-grained control over fragmentation, this
  190. behaviour can either be disabled completely, or bumped up to a higher page
  191. watermark where trimming begins.
  192. Page trimming behaviour is configurable via the sysctl `vm.nr_trim_pages'.