1
2
3
4
5#include <linux/packing.h>
6#include <linux/module.h>
7#include <linux/bitops.h>
8#include <linux/errno.h>
9#include <linux/types.h>
10
11static int get_le_offset(int offset)
12{
13 int closest_multiple_of_4;
14
15 closest_multiple_of_4 = (offset / 4) * 4;
16 offset -= closest_multiple_of_4;
17 return closest_multiple_of_4 + (3 - offset);
18}
19
20static int get_reverse_lsw32_offset(int offset, size_t len)
21{
22 int closest_multiple_of_4;
23 int word_index;
24
25 word_index = offset / 4;
26 closest_multiple_of_4 = word_index * 4;
27 offset -= closest_multiple_of_4;
28 word_index = (len / 4) - word_index - 1;
29 return word_index * 4 + offset;
30}
31
32static u64 bit_reverse(u64 val, unsigned int width)
33{
34 u64 new_val = 0;
35 unsigned int bit;
36 unsigned int i;
37
38 for (i = 0; i < width; i++) {
39 bit = (val & (1 << i)) != 0;
40 new_val |= (bit << (width - i - 1));
41 }
42 return new_val;
43}
44
45static void adjust_for_msb_right_quirk(u64 *to_write, int *box_start_bit,
46 int *box_end_bit, u8 *box_mask)
47{
48 int box_bit_width = *box_start_bit - *box_end_bit + 1;
49 int new_box_start_bit, new_box_end_bit;
50
51 *to_write >>= *box_end_bit;
52 *to_write = bit_reverse(*to_write, box_bit_width);
53 *to_write <<= *box_end_bit;
54
55 new_box_end_bit = box_bit_width - *box_start_bit - 1;
56 new_box_start_bit = box_bit_width - *box_end_bit - 1;
57 *box_mask = GENMASK_ULL(new_box_start_bit, new_box_end_bit);
58 *box_start_bit = new_box_start_bit;
59 *box_end_bit = new_box_end_bit;
60}
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88int packing(void *pbuf, u64 *uval, int startbit, int endbit, size_t pbuflen,
89 enum packing_op op, u8 quirks)
90{
91
92
93
94 u64 value_width;
95
96
97
98 int plogical_first_u8, plogical_last_u8, box;
99
100
101 if (startbit < endbit)
102
103 return -EINVAL;
104
105 value_width = startbit - endbit + 1;
106 if (value_width > 64)
107 return -ERANGE;
108
109
110
111
112
113 if (op == PACK && value_width < 64 && (*uval >= (1ull << value_width)))
114
115
116
117
118 return -ERANGE;
119
120
121 if (op == UNPACK)
122 *uval = 0;
123
124
125
126
127
128 plogical_first_u8 = startbit / 8;
129 plogical_last_u8 = endbit / 8;
130
131 for (box = plogical_first_u8; box >= plogical_last_u8; box--) {
132
133 int box_start_bit, box_end_bit, box_addr;
134 u8 box_mask;
135
136 int proj_start_bit, proj_end_bit;
137 u64 proj_mask;
138
139
140
141
142
143 if (box == plogical_first_u8)
144 box_start_bit = startbit % 8;
145 else
146 box_start_bit = 7;
147 if (box == plogical_last_u8)
148 box_end_bit = endbit % 8;
149 else
150 box_end_bit = 0;
151
152
153
154
155
156
157
158
159 proj_start_bit = ((box * 8) + box_start_bit) - endbit;
160 proj_end_bit = ((box * 8) + box_end_bit) - endbit;
161 proj_mask = GENMASK_ULL(proj_start_bit, proj_end_bit);
162 box_mask = GENMASK_ULL(box_start_bit, box_end_bit);
163
164
165
166
167
168
169 box_addr = pbuflen - box - 1;
170 if (quirks & QUIRK_LITTLE_ENDIAN)
171 box_addr = get_le_offset(box_addr);
172 if (quirks & QUIRK_LSW32_IS_FIRST)
173 box_addr = get_reverse_lsw32_offset(box_addr,
174 pbuflen);
175
176 if (op == UNPACK) {
177 u64 pval;
178
179
180 pval = ((u8 *)pbuf)[box_addr] & box_mask;
181 if (quirks & QUIRK_MSB_ON_THE_RIGHT)
182 adjust_for_msb_right_quirk(&pval,
183 &box_start_bit,
184 &box_end_bit,
185 &box_mask);
186
187 pval >>= box_end_bit;
188 pval <<= proj_end_bit;
189 *uval &= ~proj_mask;
190 *uval |= pval;
191 } else {
192 u64 pval;
193
194
195 pval = (*uval) & proj_mask;
196 pval >>= proj_end_bit;
197 if (quirks & QUIRK_MSB_ON_THE_RIGHT)
198 adjust_for_msb_right_quirk(&pval,
199 &box_start_bit,
200 &box_end_bit,
201 &box_mask);
202
203 pval <<= box_end_bit;
204 ((u8 *)pbuf)[box_addr] &= ~box_mask;
205 ((u8 *)pbuf)[box_addr] |= pval;
206 }
207 }
208 return 0;
209}
210EXPORT_SYMBOL(packing);
211
212MODULE_LICENSE("GPL v2");
213MODULE_DESCRIPTION("Generic bitfield packing and unpacking");
214