lib/packing.c

Source file repositories/reference/linux-study-clean/lib/packing.c

File Facts

System
Linux kernel
Corpus path
lib/packing.c
Extension
.c
Size
16865 bytes
Lines
479
Domain
Kernel Services
Bucket
lib
Inferred role
Kernel Services: exported/initcall integration point
Status
integration implementation candidate

Why This File Exists

Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.

Dependency Surface

Detected Declarations

Annotated Snippet

if (quirks & QUIRK_MSB_ON_THE_RIGHT) {
			pval = bitrev8(pval);
			box_mask = bitrev8(box_mask);
		}

		((u8 *)pbuf)[box_addr] &= ~box_mask;
		((u8 *)pbuf)[box_addr] |= pval;
	}
}

/**
 * pack - Pack u64 number into bitfield of buffer.
 *
 * @pbuf: Pointer to a buffer holding the packed value.
 * @uval: CPU-readable unpacked value to pack.
 * @startbit: The index (in logical notation, compensated for quirks) where
 *	      the packed value starts within pbuf. Must be larger than, or
 *	      equal to, endbit.
 * @endbit: The index (in logical notation, compensated for quirks) where
 *	    the packed value ends within pbuf. Must be smaller than, or equal
 *	    to, startbit.
 * @pbuflen: The length in bytes of the packed buffer pointed to by @pbuf.
 * @quirks: A bit mask of QUIRK_LITTLE_ENDIAN, QUIRK_LSW32_IS_FIRST and
 *	    QUIRK_MSB_ON_THE_RIGHT.
 *
 * Return: 0 on success, EINVAL or ERANGE if called incorrectly. Assuming
 *	   correct usage, return code may be discarded. The @pbuf memory will
 *	   be modified on success.
 */
int pack(void *pbuf, u64 uval, size_t startbit, size_t endbit, size_t pbuflen,
	 u8 quirks)
{
	/* startbit is expected to be larger than endbit, and both are
	 * expected to be within the logically addressable range of the buffer.
	 */
	if (unlikely(startbit < endbit || startbit >= BITS_PER_BYTE * pbuflen))
		/* Invalid function call */
		return -EINVAL;

	if (unlikely(startbit - endbit >= 64))
		return -ERANGE;

	__pack(pbuf, uval, startbit, endbit, pbuflen, quirks);

	return 0;
}
EXPORT_SYMBOL(pack);

static void __unpack(const void *pbuf, u64 *uval, size_t startbit, size_t endbit,
		     size_t pbuflen, u8 quirks)
{
	/* Logical byte indices corresponding to the
	 * start and end of the field.
	 */
	int plogical_first_u8 = startbit / BITS_PER_BYTE;
	int plogical_last_u8 = endbit / BITS_PER_BYTE;
	int box;

	/* Initialize parameter */
	*uval = 0;

	/* Iterate through an idealistic view of the pbuf as an u64 with
	 * no quirks, u8 by u8 (aligned at u8 boundaries), from high to low
	 * logical bit significance. "box" denotes the current logical u8.
	 */
	for (box = plogical_first_u8; box >= plogical_last_u8; box--) {
		/* Bit indices into the currently accessed 8-bit box */
		size_t box_start_bit, box_end_bit, box_addr;
		u8  box_mask;
		/* Corresponding bits from the unpacked u64 parameter */
		size_t proj_start_bit, proj_end_bit;
		u64 proj_mask;
		u64 pval;

		/* This u8 may need to be accessed in its entirety
		 * (from bit 7 to bit 0), or not, depending on the
		 * input arguments startbit and endbit.
		 */
		if (box == plogical_first_u8)
			box_start_bit = startbit % BITS_PER_BYTE;
		else
			box_start_bit = 7;
		if (box == plogical_last_u8)
			box_end_bit = endbit % BITS_PER_BYTE;
		else
			box_end_bit = 0;

		/* We have determined the box bit start and end.
		 * Now we calculate where this (masked) u8 box would fit
		 * in the unpacked (CPU-readable) u64 - the u8 box's

Annotation

Implementation Notes