qemu with hax to log dma reads & writes jcs.org/2018/11/12/vfio

bitmap: provide to_le/from_le helpers

Provide helpers to convert bitmaps to little endian format. It can be
used when we want to send one bitmap via network to some other hosts.

One thing to mention is that, these helpers only solve the problem of
endianess, but it does not solve the problem of different word size on
machines (the bitmaps managing same count of bits may contains different
size when malloced). So we need to take care of the size alignment issue
on the callers for now.

Signed-off-by: Peter Xu <peterx@redhat.com>
Reviewed-by: Juan Quintela <quintela@redhat.com>
Signed-off-by: Juan Quintela <quintela@redhat.com>

authored by

Peter Xu and committed by
Juan Quintela
d7788151 fc7deeea

+39
+7
include/qemu/bitmap.h
··· 39 39 * bitmap_clear(dst, pos, nbits) Clear specified bit area 40 40 * bitmap_test_and_clear_atomic(dst, pos, nbits) Test and clear area 41 41 * bitmap_find_next_zero_area(buf, len, pos, n, mask) Find bit free area 42 + * bitmap_to_le(dst, src, nbits) Convert bitmap to little endian 43 + * bitmap_from_le(dst, src, nbits) Convert bitmap from little endian 42 44 */ 43 45 44 46 /* ··· 246 248 bitmap_clear(new, old_nbits, new_nbits - old_nbits); 247 249 return new; 248 250 } 251 + 252 + void bitmap_to_le(unsigned long *dst, const unsigned long *src, 253 + long nbits); 254 + void bitmap_from_le(unsigned long *dst, const unsigned long *src, 255 + long nbits); 249 256 250 257 #endif /* BITMAP_H */
+32
util/bitmap.c
··· 370 370 371 371 return result; 372 372 } 373 + 374 + static void bitmap_to_from_le(unsigned long *dst, 375 + const unsigned long *src, long nbits) 376 + { 377 + long len = BITS_TO_LONGS(nbits); 378 + 379 + #ifdef HOST_WORDS_BIGENDIAN 380 + long index; 381 + 382 + for (index = 0; index < len; index++) { 383 + # if HOST_LONG_BITS == 64 384 + dst[index] = bswap64(src[index]); 385 + # else 386 + dst[index] = bswap32(src[index]); 387 + # endif 388 + } 389 + #else 390 + memcpy(dst, src, len * sizeof(unsigned long)); 391 + #endif 392 + } 393 + 394 + void bitmap_from_le(unsigned long *dst, const unsigned long *src, 395 + long nbits) 396 + { 397 + bitmap_to_from_le(dst, src, nbits); 398 + } 399 + 400 + void bitmap_to_le(unsigned long *dst, const unsigned long *src, 401 + long nbits) 402 + { 403 + bitmap_to_from_le(dst, src, nbits); 404 + }