Skip to content

Commit de50ce2

Browse files
Vidossuryasaimadhu
authored andcommitted
x86/boot: Copy kstrtoull() to boot/string.c
Copy kstrtoull() and the other necessary functions from lib/kstrtox.c to boot/string.c so that code in boot/ can use kstrtoull() and the old simple_strtoull() can gradually be phased out. Using div_u64() from math64.h directly will cause the dividend to be handled as a 64-bit value and cause the infamous __divdi3 linker error due to gcc trying to use its library function for the 64-bit division. Therefore, separate the dividend into an upper and lower part. [ bp: Rewrite commit message. ] Signed-off-by: Chao Fan <fanc.fnst@cn.fujitsu.com> Signed-off-by: Borislav Petkov <bp@suse.de> Cc: bhe@redhat.com Cc: caoj.fnst@cn.fujitsu.com Cc: "H. Peter Anvin" <hpa@zytor.com> Cc: indou.takao@jp.fujitsu.com Cc: Ingo Molnar <mingo@redhat.com> Cc: kasong@redhat.com Cc: Kees Cook <keescook@chromium.org> Cc: msys.mizuma@gmail.com Cc: Thomas Gleixner <tglx@linutronix.de> Cc: x86-ml <x86@kernel.org> Link: https://lkml.kernel.org/r/20190123110850.12433-2-fanc.fnst@cn.fujitsu.com
1 parent ac09c5f commit de50ce2

2 files changed

Lines changed: 142 additions & 0 deletions

File tree

‎arch/x86/boot/string.c‎

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@
1313
*/
1414

1515
#include <linux/types.h>
16+
#include <linux/kernel.h>
17+
#include <linux/errno.h>
1618
#include <asm/asm.h>
1719
#include "ctype.h"
1820
#include "string.h"
1921

22+
#define KSTRTOX_OVERFLOW (1U << 31)
23+
2024
/*
2125
* Undef these macros so that the functions that we provide
2226
* here will have the correct names regardless of how string.h
@@ -187,3 +191,140 @@ char *strchr(const char *s, int c)
187191
return NULL;
188192
return (char *)s;
189193
}
194+
195+
static inline u64 __div_u64_rem(u64 dividend, u32 divisor, u32 *remainder)
196+
{
197+
union {
198+
u64 v64;
199+
u32 v32[2];
200+
} d = { dividend };
201+
u32 upper;
202+
203+
upper = d.v32[1];
204+
d.v32[1] = 0;
205+
if (upper >= divisor) {
206+
d.v32[1] = upper / divisor;
207+
upper %= divisor;
208+
}
209+
asm ("divl %2" : "=a" (d.v32[0]), "=d" (*remainder) :
210+
"rm" (divisor), "0" (d.v32[0]), "1" (upper));
211+
return d.v64;
212+
}
213+
214+
static inline u64 __div_u64(u64 dividend, u32 divisor)
215+
{
216+
u32 remainder;
217+
218+
return __div_u64_rem(dividend, divisor, &remainder);
219+
}
220+
221+
static inline char _tolower(const char c)
222+
{
223+
return c | 0x20;
224+
}
225+
226+
static const char *_parse_integer_fixup_radix(const char *s, unsigned int *base)
227+
{
228+
if (*base == 0) {
229+
if (s[0] == '0') {
230+
if (_tolower(s[1]) == 'x' && isxdigit(s[2]))
231+
*base = 16;
232+
else
233+
*base = 8;
234+
} else
235+
*base = 10;
236+
}
237+
if (*base == 16 && s[0] == '0' && _tolower(s[1]) == 'x')
238+
s += 2;
239+
return s;
240+
}
241+
242+
/*
243+
* Convert non-negative integer string representation in explicitly given radix
244+
* to an integer.
245+
* Return number of characters consumed maybe or-ed with overflow bit.
246+
* If overflow occurs, result integer (incorrect) is still returned.
247+
*
248+
* Don't you dare use this function.
249+
*/
250+
static unsigned int _parse_integer(const char *s,
251+
unsigned int base,
252+
unsigned long long *p)
253+
{
254+
unsigned long long res;
255+
unsigned int rv;
256+
257+
res = 0;
258+
rv = 0;
259+
while (1) {
260+
unsigned int c = *s;
261+
unsigned int lc = c | 0x20; /* don't tolower() this line */
262+
unsigned int val;
263+
264+
if ('0' <= c && c <= '9')
265+
val = c - '0';
266+
else if ('a' <= lc && lc <= 'f')
267+
val = lc - 'a' + 10;
268+
else
269+
break;
270+
271+
if (val >= base)
272+
break;
273+
/*
274+
* Check for overflow only if we are within range of
275+
* it in the max base we support (16)
276+
*/
277+
if (unlikely(res & (~0ull << 60))) {
278+
if (res > __div_u64(ULLONG_MAX - val, base))
279+
rv |= KSTRTOX_OVERFLOW;
280+
}
281+
res = res * base + val;
282+
rv++;
283+
s++;
284+
}
285+
*p = res;
286+
return rv;
287+
}
288+
289+
static int _kstrtoull(const char *s, unsigned int base, unsigned long long *res)
290+
{
291+
unsigned long long _res;
292+
unsigned int rv;
293+
294+
s = _parse_integer_fixup_radix(s, &base);
295+
rv = _parse_integer(s, base, &_res);
296+
if (rv & KSTRTOX_OVERFLOW)
297+
return -ERANGE;
298+
if (rv == 0)
299+
return -EINVAL;
300+
s += rv;
301+
if (*s == '\n')
302+
s++;
303+
if (*s)
304+
return -EINVAL;
305+
*res = _res;
306+
return 0;
307+
}
308+
309+
/**
310+
* kstrtoull - convert a string to an unsigned long long
311+
* @s: The start of the string. The string must be null-terminated, and may also
312+
* include a single newline before its terminating null. The first character
313+
* may also be a plus sign, but not a minus sign.
314+
* @base: The number base to use. The maximum supported base is 16. If base is
315+
* given as 0, then the base of the string is automatically detected with the
316+
* conventional semantics - If it begins with 0x the number will be parsed as a
317+
* hexadecimal (case insensitive), if it otherwise begins with 0, it will be
318+
* parsed as an octal number. Otherwise it will be parsed as a decimal.
319+
* @res: Where to write the result of the conversion on success.
320+
*
321+
* Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
322+
* Used as a replacement for the obsolete simple_strtoull. Return code must
323+
* be checked.
324+
*/
325+
int kstrtoull(const char *s, unsigned int base, unsigned long long *res)
326+
{
327+
if (s[0] == '+')
328+
s++;
329+
return _kstrtoull(s, base, res);
330+
}

‎arch/x86/boot/string.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,5 @@ extern unsigned int atou(const char *s);
2929
extern unsigned long long simple_strtoull(const char *cp, char **endp,
3030
unsigned int base);
3131

32+
int kstrtoull(const char *s, unsigned int base, unsigned long long *res);
3233
#endif /* BOOT_STRING_H */

0 commit comments

Comments
 (0)