buffer.c 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 "buffer.h"
  44. /**
  45. * Initialise a buffer.
  46. *
  47. * @v buffer The buffer to be initialised
  48. * @ret None
  49. * @err None
  50. *
  51. * Set @c buffer->start and @c buffer->end before calling init_buffer().
  52. * init_buffer() will initialise the buffer to the state of being
  53. * empty.
  54. *
  55. */
  56. void init_buffer ( struct buffer *buffer ) {
  57. char tail = 1;
  58. buffer->fill = 0;
  59. if ( buffer->end != buffer->start )
  60. copy_to_phys ( buffer->start, &tail, sizeof ( tail ) );
  61. DBG ( "BUFFER [%x,%x) initialised\n", buffer->start, buffer->end );
  62. }
  63. /**
  64. * Split a free block.
  65. *
  66. * @v desc A descriptor for the free block
  67. * @v block Start address of the block
  68. * @v split Address at which to split the block
  69. * @ret None
  70. * @err None
  71. *
  72. * Split a free block into two separate free blocks. If the split
  73. * point lies outside the block, no action is taken; this is not an
  74. * error.
  75. *
  76. * @b NOTE: It is the reponsibility of the caller to ensure that there
  77. * is enough room in each of the two portions for a free block
  78. * descriptor (a @c struct @c buffer_free_block, except in the case of
  79. * a tail block which requires only a one byte descriptor). If the
  80. * caller fails to do this, data corruption will occur.
  81. *
  82. * In practice, this means that the granularity at which blocks are
  83. * split must be at least @c sizeof(struct @c buffer_free_block).
  84. *
  85. */
  86. static void split_free_block ( struct buffer_free_block *desc,
  87. physaddr_t block, physaddr_t split ) {
  88. /* If split point is before start of block, do nothing */
  89. if ( split <= block )
  90. return;
  91. /* If split point is after end of block, do nothing */
  92. if ( split >= desc->end )
  93. return;
  94. DBG ( "BUFFER splitting [%x,%x) -> [%x,%x) [%x,%x)\n",
  95. block, desc->end, block, split, split, desc->end );
  96. /* Create descriptor for new free block */
  97. copy_to_phys ( split, &desc->tail, sizeof ( desc->tail ) );
  98. if ( ! desc->tail )
  99. copy_to_phys ( split, desc, sizeof ( *desc ) );
  100. /* Update descriptor for old free block */
  101. desc->tail = 0;
  102. desc->next_free = split;
  103. desc->end = split;
  104. copy_to_phys ( block, desc, sizeof ( *desc ) );
  105. }
  106. /**
  107. * Mark a free block as used.
  108. *
  109. * @v buffer The buffer containing the block
  110. * @v desc A descriptor for the free block
  111. * @v prev_block Address of the previous block
  112. * @ret None
  113. * @err None
  114. *
  115. * Marks a free block as used, i.e. removes it from the free list.
  116. *
  117. */
  118. static inline void unfree_block ( struct buffer *buffer,
  119. struct buffer_free_block *desc,
  120. physaddr_t prev_block ) {
  121. struct buffer_free_block prev_desc;
  122. /* If this is the first block, just update buffer->fill */
  123. if ( ! prev_block ) {
  124. DBG ( "BUFFER marking [%x,%x) as used\n",
  125. buffer->start + buffer->fill, desc->end );
  126. buffer->fill = desc->next_free - buffer->start;
  127. return;
  128. }
  129. /* Get descriptor for previous block (which cannot be a tail block) */
  130. copy_from_phys ( &prev_desc, prev_block, sizeof ( prev_desc ) );
  131. DBG ( "BUFFER marking [%x,%x) as used\n",
  132. prev_desc.next_free, desc->end );
  133. /* Modify descriptor for previous block and write it back */
  134. prev_desc.next_free = desc->next_free;
  135. copy_to_phys ( prev_block, &prev_desc, sizeof ( prev_desc ) );
  136. }
  137. /**
  138. * Write data into a buffer.
  139. *
  140. * @v buffer The buffer into which to write the data
  141. * @v data The data to be written
  142. * @v offset Offset within the buffer at which to write the data
  143. * @v len Length of data to be written
  144. * @ret True Data was successfully written
  145. * @ret False Data was not written
  146. * @err ENOMEM Buffer is too small to contain the data
  147. *
  148. * Writes a block of data into the buffer. The block need not be
  149. * aligned to any particular boundary, or be of any particular size,
  150. * and it may overlap blocks already in the buffer (i.e. duplicate
  151. * calls to fill_buffer() are explicitly permitted).
  152. *
  153. * @c buffer->fill will be updated to indicate the fill level of the
  154. * buffer, i.e. the offset to the first gap within the buffer. If the
  155. * filesize is known (e.g. as with the SLAM protocol), you can test
  156. * for end-of-file by checking for @c buffer->fill==filesize. If the
  157. * filesize is not known, but there is a well-defined end-of-file test
  158. * (e.g. as with the TFTP protocol), you can read @c buffer->fill to
  159. * determine the final filesize. If blocks are known to be delivered
  160. * in a strictly sequential order with no packet loss or duplication,
  161. * then you can pass in @c offset==buffer->fill.
  162. *
  163. * @b NOTE: It is the caller's responsibility to ensure that the
  164. * boundaries between data blocks are more than @c sizeof(struct @c
  165. * buffer_free_block) apart. If this condition is not satisfied, data
  166. * corruption will occur. (See split_free_block() for details.)
  167. *
  168. * In practice this is not a problem. Callers of fill_buffer() will
  169. * be download protocols such as TFTP, and very few protocols have a
  170. * block size smaller than @c sizeof(struct @c buffer_free_block).
  171. *
  172. */
  173. int fill_buffer ( struct buffer *buffer, const void *data,
  174. off_t offset, size_t len ) {
  175. struct buffer_free_block desc;
  176. physaddr_t block, prev_block;
  177. physaddr_t data_start, data_end;
  178. /* Calculate start and end addresses of data */
  179. data_start = buffer->start + offset;
  180. data_end = data_start + len;
  181. DBG ( "BUFFER [%x,%x) writing portion [%x,%x)\n",
  182. buffer->start, buffer->end, data_start, data_end );
  183. /* Check buffer bounds */
  184. if ( data_end > buffer->end ) {
  185. DBG ( "BUFFER [%x,%x) too small for data!\n",
  186. buffer->start, buffer->end );
  187. errno = ENOMEM;
  188. return 0;
  189. }
  190. /* Iterate through the buffer's free blocks */
  191. prev_block = 0;
  192. block = buffer->start + buffer->fill;
  193. while ( block < buffer->end ) {
  194. /* Read block descriptor */
  195. desc.next_free = buffer->end;
  196. desc.end = buffer->end;
  197. copy_from_phys ( &desc.tail, block, sizeof ( desc.tail ) );
  198. if ( ! desc.tail )
  199. copy_from_phys ( &desc, block, sizeof ( desc ) );
  200. /* Split block at data start and end markers */
  201. split_free_block ( &desc, block, data_start );
  202. split_free_block ( &desc, block, data_end );
  203. /* Block is now either completely contained by or
  204. * completely outside the data area
  205. */
  206. if ( ( block >= data_start ) && ( block < data_end ) ) {
  207. /* Block is within the data area */
  208. unfree_block ( buffer, &desc, prev_block );
  209. copy_to_phys ( block, data + ( block - data_start ),
  210. desc.end - block );
  211. } else {
  212. /* Block is outside the data area */
  213. prev_block = block;
  214. }
  215. /* Move to next free block */
  216. block = desc.next_free;
  217. }
  218. DBG ( "BUFFER [%x,%x) full up to %x\n",
  219. buffer->start, buffer->end, buffer->start + buffer->fill );
  220. return 1;
  221. }