Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /** @file
  2. *
  3. * Buffer internals.
  4. *
  5. * A buffer consists of a single, contiguous area of memory, some of
  6. * which is "filled" and the remainder of which is "free". The
  7. * "filled" and "free" spaces are not necessarily contiguous.
  8. *
  9. * When a buffer is initialised via init_buffer(), it consists of a
  10. * single free space. As data is added to the buffer via
  11. * fill_buffer(), this free space decreases and can become fragmented.
  12. *
  13. * Each free block within a buffer starts with a "tail byte". If the
  14. * tail byte is non-zero, this indicates that the free block is the
  15. * tail of the buffer, i.e. occupies all the remaining space up to the
  16. * end of the buffer. When the tail byte is non-zero, it indicates
  17. * that a descriptor (a @c struct @c buffer_free_block) follows the
  18. * tail byte. The descriptor describes the size of the free block and
  19. * the address of the next free block.
  20. *
  21. * We cannot simply always start a free block with a descriptor,
  22. * because it is conceivable that we will, at some point, encounter a
  23. * situation in which the final free block of a buffer is too small to
  24. * contain a descriptor. Consider a protocol with a blocksize of 512
  25. * downloading a 1025-byte file into a 1025-byte buffer. Suppose that
  26. * the first two blocks are received; we have now filled 1024 of the
  27. * 1025 bytes in the buffer, and our only free block consists of the
  28. * 1025th byte. Using a "tail byte" solves this problem.
  29. *
  30. *
  31. * Note that the rather convoluted way of manipulating the buffer
  32. * descriptors (using copy_{to,from}_phys rather than straightforward
  33. * pointers) is needed to cope with operation as a PXE stack, when we
  34. * may be running in real mode or 16-bit protected mode, and therefore
  35. * cannot directly access arbitrary areas of memory using simple
  36. * pointers.
  37. *
  38. */
  39. #include "stddef.h"
  40. #include "string.h"
  41. #include "io.h"
  42. #include "errno.h"
  43. #include <assert.h>
  44. #include "buffer.h"
  45. /**
  46. * Initialise a buffer.
  47. *
  48. * @v buffer The buffer to be initialised
  49. * @ret None -
  50. * @err None -
  51. *
  52. * Set @c buffer->start and @c buffer->end before calling init_buffer().
  53. * init_buffer() will initialise the buffer to the state of being
  54. * empty.
  55. *
  56. */
  57. void init_buffer ( struct buffer *buffer ) {
  58. char tail = 1;
  59. buffer->fill = 0;
  60. if ( buffer->end != buffer->start )
  61. copy_to_phys ( buffer->start, &tail, sizeof ( tail ) );
  62. DBG ( "BUFFER [%x,%x) initialised\n", buffer->start, buffer->end );
  63. }
  64. /**
  65. * Move to the next block in the free list
  66. *
  67. * @v block The current free block
  68. * @v buffer The buffer
  69. * @ret True Successfully moved to the next free block
  70. * @ret False There are no more free blocks
  71. * @ret block The next free block
  72. * @err None -
  73. *
  74. * Move to the next block in the free block list, filling in @c block
  75. * with the descriptor for this next block. If the next block is the
  76. * tail block, @c block will be filled with the values calculated for
  77. * the tail block, otherwise the descriptor will be read from the free
  78. * block itself.
  79. *
  80. * If there are no more free blocks, next_free_block() returns False
  81. * and leaves @c block with invalid contents.
  82. *
  83. * Set <tt> block->next = buffer->start + buffer->fill </tt> for the
  84. * first call to next_free_block().
  85. */
  86. static inline int next_free_block ( struct buffer_free_block *block,
  87. struct buffer *buffer ) {
  88. /* Move to next block */
  89. block->start = block->next;
  90. /* If at end of buffer, return 0 */
  91. if ( block->start >= buffer->end )
  92. return 0;
  93. /* Set up ->next and ->end as for a tail block */
  94. block->next = block->end = buffer->end;
  95. /* Read tail marker from block */
  96. copy_from_phys ( &block->tail, block->start, sizeof ( block->tail ) );
  97. /* If not a tail block, read whole block descriptor from block */
  98. if ( ! block->tail ) {
  99. copy_from_phys ( block, block->start, sizeof ( *block ) );
  100. }
  101. return 1;
  102. }
  103. /**
  104. * Store a free block descriptor
  105. *
  106. * @v block The free block descriptor to store
  107. * @ret None -
  108. * @err None -
  109. *
  110. * Writes a free block descriptor back to a free block. If the block
  111. * is a tail block, only the tail marker will be written, otherwise
  112. * the whole block descriptor will be written.
  113. */
  114. static inline void store_free_block ( struct buffer_free_block *block ) {
  115. copy_to_phys ( block->start, block,
  116. ( block->tail ?
  117. sizeof ( block->tail ) : sizeof ( *block ) ) );
  118. }
  119. /**
  120. * Write data into a buffer.
  121. *
  122. * @v buffer The buffer into which to write the data
  123. * @v data The data to be written
  124. * @v offset Offset within the buffer at which to write the data
  125. * @v len Length of data to be written
  126. * @ret True Data was successfully written
  127. * @ret False Data was not written
  128. * @err ENOMEM Buffer is too small to contain the data
  129. *
  130. * Writes a block of data into the buffer. The block need not be
  131. * aligned to any particular boundary, or be of any particular size,
  132. * and it may overlap blocks already in the buffer (i.e. duplicate
  133. * calls to fill_buffer() are explicitly permitted).
  134. *
  135. * @c buffer->fill will be updated to indicate the fill level of the
  136. * buffer, i.e. the offset to the first gap within the buffer. If the
  137. * filesize is known (e.g. as with the SLAM protocol), you can test
  138. * for end-of-file by checking for @c buffer->fill==filesize. If the
  139. * filesize is not known, but there is a well-defined end-of-file test
  140. * (e.g. as with the TFTP protocol), you can read @c buffer->fill to
  141. * determine the final filesize. If blocks are known to be delivered
  142. * in a strictly sequential order with no packet loss or duplication,
  143. * then you can pass in @c offset==buffer->fill.
  144. *
  145. * @b NOTE: It is the caller's responsibility to ensure that the
  146. * boundaries between data blocks are more than @c sizeof(struct @c
  147. * buffer_free_block) apart. If this condition is not satisfied, data
  148. * corruption will occur.
  149. *
  150. * In practice this is not a problem. Callers of fill_buffer() will
  151. * be download protocols such as TFTP, and very few protocols have a
  152. * block size smaller than @c sizeof(struct @c buffer_free_block).
  153. *
  154. */
  155. int fill_buffer ( struct buffer *buffer, const void *data,
  156. off_t offset, size_t len ) {
  157. struct buffer_free_block block, before, after;
  158. physaddr_t data_start, data_end;
  159. /* Calculate start and end addresses of data */
  160. data_start = buffer->start + offset;
  161. data_end = data_start + len;
  162. DBG ( "BUFFER [%x,%x) writing portion [%x,%x)\n",
  163. buffer->start, buffer->end, data_start, data_end );
  164. /* Check buffer bounds */
  165. if ( data_end > buffer->end ) {
  166. DBG ( "BUFFER [%x,%x) too small for data!\n",
  167. buffer->start, buffer->end );
  168. errno = ENOMEM;
  169. return 0;
  170. }
  171. /* Find 'before' and 'after' blocks, if any */
  172. before.start = before.end = 0;
  173. after.start = after.end = buffer->end;
  174. block.next = buffer->start + buffer->fill;
  175. while ( next_free_block ( &block, buffer ) ) {
  176. if ( ( block.start < data_start ) &&
  177. ( block.start >= before.start ) )
  178. memcpy ( &before, &block, sizeof ( before ) );
  179. if ( ( block.end > data_end ) &&
  180. ( block.end <= after.end ) )
  181. memcpy ( &after, &block, sizeof ( after ) );
  182. }
  183. /* Truncate 'before' and 'after' blocks around data. */
  184. if ( data_start < before.end )
  185. before.end = data_start;
  186. if ( data_end > after.start )
  187. after.start = data_end;
  188. /* Link 'after' block to 'before' block */
  189. before.next = after.start;
  190. /* Write back 'before' block, if any */
  191. if ( before.start ) {
  192. before.tail = 0;
  193. assert ( ( before.end - before.start ) >=
  194. sizeof ( struct buffer_free_block ) );
  195. store_free_block ( &before );
  196. } else {
  197. buffer->fill = before.next - buffer->start;
  198. }
  199. /* Write back 'after' block, if any */
  200. if ( after.start < buffer->end ) {
  201. assert ( after.tail ||
  202. ( ( after.end - after.start ) >=
  203. sizeof ( struct buffer_free_block ) ) );
  204. store_free_block ( &after );
  205. }
  206. DBG ( "BUFFER [%x,%x) before [%x,%x) after [%x,%x)\n",
  207. buffer->start, buffer->end, before.start, before.end,
  208. after.start, after.end );
  209. /* Copy data into buffer */
  210. copy_to_phys ( data_start, data, len );
  211. DBG ( "BUFFER [%x,%x) full up to %x\n",
  212. buffer->start, buffer->end, buffer->start + buffer->fill );
  213. return 1;
  214. }