[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [[UNIKRAFT] v4] lib/isrlib: Introduce stdlib irq-safe rutines
From: Cristian Vijelie <cristianvijelie@xxxxxxxxx> This patch introduces stdlib interrupt-context-safe functions, based on nolibc. I removed abort, as it made no sense to have another abort function. Signed-off-by: Cristian Vijelie <cristianvijelie@xxxxxxxxx> --- include/uk/isr/stdlib.h | 63 ++++++ lib/isrlib/Makefile.uk | 2 + lib/isrlib/qsort.c | 157 +++++++++++++++ lib/isrlib/stdlib.c | 417 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 639 insertions(+) create mode 100644 include/uk/isr/stdlib.h create mode 100644 lib/isrlib/qsort.c create mode 100644 lib/isrlib/stdlib.c diff --git a/include/uk/isr/stdlib.h b/include/uk/isr/stdlib.h new file mode 100644 index 0000000..30eb443 --- /dev/null +++ b/include/uk/isr/stdlib.h @@ -0,0 +1,63 @@ +/* SPDX-License-Identifier: BSD-2-Clause */ +/* + * Copyright (c) 2009 Citrix Systems, Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef __UK_ISR_STDLIB_H__ +#define __UK_ISR_STDLIB_H__ + +#include <stdlib.h> + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * Convert a string to an unsigned long integer. + * Ignores `locale' stuff. Assumes that the upper and lower case + * alphabets and digits are each contiguous. + * + * @nptr: The start of the string + * @endptr: A pointer to the end of the parsed string will be placed here + * @base: The number base to use + */ +long strtol_isr(const char *nptr, char **endptr, int base); +unsigned long strtoul_isr(const char *nptr, char **endptr, int base); +long long strtoll_isr(const char *nptr, char **endptr, int base); +unsigned long long strtoull_isr(const char *nptr, char **endptr, int base); + +/** + * Convert a string to an integer + * @s: The start of the string + */ +int atoi_isr(const char *s); + +void qsort_isr(void *base, size_t nmemb, size_t size, + int (*compar)(const void *, const void *)); + +#ifdef __cplusplus +} +#endif + +#endif /* __UK_ISR_STDLIB_H__ */ diff --git a/lib/isrlib/Makefile.uk b/lib/isrlib/Makefile.uk index 26f19e8..9b60e04 100644 --- a/lib/isrlib/Makefile.uk +++ b/lib/isrlib/Makefile.uk @@ -6,3 +6,5 @@ CINCLUDES-$(CONFIG_LIBISRLIB) += $(LIBISRLIB_GLOBAL_INCLUDES) CXXINCLUDES-$(CONFIG_LIBISRLIB) += $(LIBISRLIB_GLOBAL_INCLUDES) LIBISRLIB_SRCS-y += $(LIBISRLIB_BASE)/string.c|isr +LIBISRLIB_SRCS-y += $(LIBISRLIB_BASE)/stdlib.c|isr +LIBISRLIB_SRCS-y += $(LIBISRLIB_BASE)/qsort.c|isr diff --git a/lib/isrlib/qsort.c b/lib/isrlib/qsort.c new file mode 100644 index 0000000..418e217 --- /dev/null +++ b/lib/isrlib/qsort.c @@ -0,0 +1,157 @@ +/* $OpenBSD: qsort.c,v 1.10 2005/08/08 08:05:37 espie Exp $ */ +/*- + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ +#include <sys/types.h> +#include <uk/isr/stdlib.h> +static inline char *med3_isr(char *, char *, char *, int (*) + (const void *, const void *)); +static inline void swapfunc_isr(char *, char *, int, int); +#define min(a, b) ((a) < (b) ? a : b) +/* + * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function". + */ +#define swapcode(TYPE, parmi, parmj, n) { \ + long i = (n) / sizeof(TYPE); \ + TYPE *pi = (TYPE *) (parmi); \ + TYPE *pj = (TYPE *) (parmj); \ + do { \ + TYPE t = *pi; \ + *pi++ = *pj; \ + *pj++ = t; \ + } while (--i > 0); \ +} +#define SWAPINIT(a, es) (swaptype = ((char *)a - (char *)0) % sizeof(long) || \ + es % sizeof(long) ? 2 : es == sizeof(long) ? 0 : 1) +static inline void +swapfunc_isr(char *a, char *b, int n, int swaptype) +{ + if (swaptype <= 1) + swapcode(long, a, b, n) + else + swapcode(char, a, b, n) +} +#define swap(a, b) \ + do { \ + if (swaptype == 0) { \ + long t = *(long *)(a); \ + *(long *)(a) = *(long *)(b); \ + *(long *)(b) = t; \ + } else \ + swapfunc_isr(a, b, es, swaptype); \ + } while (0) + +#define vecswap(a, b, n) \ + do { \ + if ((n) > 0) \ + swapfunc_isr(a, b, n, swaptype); \ + } while (0) +static inline char * +med3_isr(char *a, char *b, char *c, int (*cmp)(const void *, const void *)) +{ + return cmp(a, b) < 0 ? + (cmp(b, c) < 0 ? b : (cmp(a, c) < 0 ? c : a)) + : (cmp(b, c) > 0 ? b : (cmp(a, c) < 0 ? a : c)); +} +void +qsort_isr(void *aa, size_t n, size_t es, int (*cmp)(const void *, const void *)) +{ + char *pa, *pb, *pc, *pd, *pl, *pm, *pn; + int d, r, swaptype, swap_cnt; + char *a = aa; +loop: + SWAPINIT(a, es); + swap_cnt = 0; + if (n < 7) { + for (pm = (char *)a + es; pm < (char *) a + n * es; pm += es) + for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0; + pl -= es) + swap(pl, pl - es); + return; + } + pm = (char *)a + (n / 2) * es; + if (n > 7) { + pl = (char *)a; + pn = (char *)a + (n - 1) * es; + if (n > 40) { + d = (n / 8) * es; + pl = med3_isr(pl, pl + d, pl + 2 * d, cmp); + pm = med3_isr(pm - d, pm, pm + d, cmp); + pn = med3_isr(pn - 2 * d, pn - d, pn, cmp); + } + pm = med3_isr(pl, pm, pn, cmp); + } + swap(a, pm); + pa = pb = (char *)a + es; + + pc = pd = (char *)a + (n - 1) * es; + for (;;) { + while (pb <= pc && (r = cmp(pb, a)) <= 0) { + if (r == 0) { + swap_cnt = 1; + swap(pa, pb); + pa += es; + } + pb += es; + } + while (pb <= pc && (r = cmp(pc, a)) >= 0) { + if (r == 0) { + swap_cnt = 1; + swap(pc, pd); + pd -= es; + } + pc -= es; + } + if (pb > pc) + break; + swap(pb, pc); + swap_cnt = 1; + pb += es; + pc -= es; + } + if (swap_cnt == 0) { /* Switch to insertion sort */ + for (pm = (char *) a + es; pm < (char *) a + n * es; pm += es) + for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0; + pl -= es) + swap(pl, pl - es); + return; + } + pn = (char *)a + n * es; + r = min(pa - (char *)a, pb - pa); + vecswap(a, pb - r, r); + r = min(pd - pc, pn - pd - (int)es); + vecswap(pb, pn - r, r); + if ((r = pb - pa) > (int)es) + qsort_isr(a, r / es, es, cmp); + if ((r = pd - pc) > (int)es) { + /* Iterate rather than recurse to save stack space */ + a = pn - r; + n = r / es; + goto loop; + } +} diff --git a/lib/isrlib/stdlib.c b/lib/isrlib/stdlib.c new file mode 100644 index 0000000..d1aad75 --- /dev/null +++ b/lib/isrlib/stdlib.c @@ -0,0 +1,417 @@ +/* SPDX-License-Identifier: BSD-3-Clause */ +/* + **************************************************************************** + * + * File: printf.c + * Author: Juergen Gross <jgross@xxxxxxxx> + * + * Date: Jun 2016 + * + * Environment: Xen Minimal OS + * Description: Library functions for printing + * (FreeBSD port) + * + **************************************************************************** + */ + +/*- + * Copyright (c) 1990, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Chris Torek. + * + * Copyright (c) 2011 The FreeBSD Foundation + * All rights reserved. + * Portions of this software were developed by David Chisnall + * under sponsorship from the FreeBSD Foundation. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <errno.h> +#include <stdint.h> +#include <stdlib.h> +#include <limits.h> +#include <ctype.h> +#include <uk/print.h> +#include <uk/plat/bootstrap.h> +#include <uk/arch/limits.h> + +#define __DECONST(type, var) ((type)(uintptr_t)(const void *)(var)) + +/* + * Convert a string to an unsigned long integer. + * + * Ignores `locale' stuff. Assumes that the upper and lower case + * alphabets and digits are each contiguous. + */ +unsigned long strtoul_isr(const char *nptr, char **endptr, int base) +{ + const char *s = nptr; + unsigned long acc; + unsigned char c; + unsigned long cutoff; + int neg = 0, any, cutlim; + + if (base < 0 || base == 1 || base > 36) { + errno = -EINVAL; + any = 0; + acc = 0; + goto exit; + } + + /* + * See strtol for comments as to the logic used. + */ + do { + c = *s++; + } while (isspace(c)); + if (c == '-') { + neg = 1; + c = *s++; + } else if (c == '+') + c = *s++; + if ((base == 0 || base == 16) && c == '0' && (*s == 'x' || *s == 'X')) { + c = s[1]; + s += 2; + base = 16; + } + if (base == 0) + base = c == '0' ? 8 : 10; + cutoff = (unsigned long)ULONG_MAX / (unsigned long)base; + cutlim = (unsigned long)ULONG_MAX % (unsigned long)base; + for (acc = 0, any = 0;; c = *s++) { + if (!isascii(c)) + break; + if (isdigit(c)) + c -= '0'; + else if (isalpha(c)) + c -= isupper(c) ? 'A' - 10 : 'a' - 10; + else + break; + if (c >= base) + break; + if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) + any = -1; + else { + any = 1; + acc *= base; + acc += c; + } + } + if (any < 0) { + acc = ULONG_MAX; + errno = ERANGE; + } else if (neg) + acc = -acc; +exit: + if (endptr != 0) + *endptr = __DECONST(char *, any ? s - 1 : nptr); + return acc; +} + +long strtol_isr(const char *nptr, char **endptr, int base) +{ + const char *s; + unsigned long acc; + unsigned char c; + unsigned long qbase, cutoff; + int neg, any, cutlim; + + s = nptr; + if (base < 0 || base == 1 || base > 36) { + errno = -EINVAL; + any = 0; + acc = 0; + goto exit; + } + + /* + * Skip white space and pick up leading +/- sign if any. + * If base is 0, allow 0x for hex and 0 for octal, else + * assume decimal; if base is already 16, allow 0x. + */ + do { + c = *s++; + } while (isspace(c)); + if (c == '-') { + neg = 1; + c = *s++; + } else { + neg = 0; + if (c == '+') + c = *s++; + } + if ((base == 0 || base == 16) && c == '0' && (*s == 'x' || *s == 'X')) { + c = s[1]; + s += 2; + base = 16; + } + if (base == 0) + base = c == '0' ? 8 : 10; + + /* + * Compute the cutoff value between legal numbers and illegal + * numbers. That is the largest legal value, divided by the + * base. An input number that is greater than this value, if + * followed by a legal input character, is too big. One that + * is equal to this value may be valid or not; the limit + * between valid and invalid numbers is then based on the last + * digit. For instance, if the range for quads is + * [-2147483648..2147483647] and the input base + * is 10, cutoff will be set to 2147483647 and cutlim to + * either 7 (neg==0) or 8 (neg==1), meaning that if we have + * accumulated a value > 2147483647, or equal but the + * next digit is > 7 (or 8), the number is too big, and we will + * return a range error. + * + * Set any if any `digits' consumed; make it negative to indicate + * overflow. + */ + qbase = (unsigned int)base; + cutoff = neg + ? (unsigned long)LONG_MAX + - (unsigned long)(LONG_MIN + LONG_MAX) + : LONG_MAX; + cutlim = cutoff % qbase; + cutoff /= qbase; + for (acc = 0, any = 0;; c = *s++) { + if (!isascii(c)) + break; + if (isdigit(c)) + c -= '0'; + else if (isalpha(c)) + c -= isupper(c) ? 'A' - 10 : 'a' - 10; + else + break; + if (c >= base) + break; + if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) + any = -1; + else { + any = 1; + acc *= qbase; + acc += c; + } + } + if (any < 0) { + acc = neg ? LONG_MIN : LONG_MAX; + errno = ERANGE; + } else if (neg) + acc = -acc; + +exit: + if (endptr != 0) + *endptr = __DECONST(char *, any ? s - 1 : nptr); + return acc; +} + +/* + * Convert a string to a long long integer. + * + * Ignores `locale' stuff. Assumes that the upper and lower case + * alphabets and digits are each contiguous. + */ +long long strtoll_isr(const char *nptr, char **endptr, int base) +{ + const char *s; + unsigned long long acc; + unsigned char c; + unsigned long long qbase, cutoff; + int neg, any, cutlim; + + s = nptr; + if (base < 0 || base == 1 || base > 36) { + errno = -EINVAL; + any = 0; + acc = 0; + goto exit; + } + /* + * Skip white space and pick up leading +/- sign if any. + * If base is 0, allow 0x for hex and 0 for octal, else + * assume decimal; if base is already 16, allow 0x. + */ + do { + c = *s++; + } while (isspace(c)); + if (c == '-') { + neg = 1; + c = *s++; + } else { + neg = 0; + if (c == '+') + c = *s++; + } + if ((base == 0 || base == 16) && c == '0' && (*s == 'x' || *s == 'X')) { + c = s[1]; + s += 2; + base = 16; + } + if (base == 0) + base = c == '0' ? 8 : 10; + + /* + * Compute the cutoff value between legal numbers and illegal + * numbers. That is the largest legal value, divided by the + * base. An input number that is greater than this value, if + * followed by a legal input character, is too big. One that + * is equal to this value may be valid or not; the limit + * between valid and invalid numbers is then based on the last + * digit. For instance, if the range for quads is + * [-9223372036854775808..9223372036854775807] and the input base + * is 10, cutoff will be set to 922337203685477580 and cutlim to + * either 7 (neg==0) or 8 (neg==1), meaning that if we have + * accumulated a value > 922337203685477580, or equal but the + * next digit is > 7 (or 8), the number is too big, and we will + * return a range error. + * + * Set any if any `digits' consumed; make it negative to indicate + * overflow. + */ + qbase = (unsigned int)base; + cutoff = neg + ? (unsigned long long)LLONG_MAX + - (unsigned long long)(LLONG_MIN + LLONG_MAX) + : LLONG_MAX; + cutlim = cutoff % qbase; + cutoff /= qbase; + for (acc = 0, any = 0;; c = *s++) { + if (!isascii(c)) + break; + if (isdigit(c)) + c -= '0'; + else if (isalpha(c)) + c -= isupper(c) ? 'A' - 10 : 'a' - 10; + else + break; + if (c >= base) + break; + if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) + any = -1; + else { + any = 1; + acc *= qbase; + acc += c; + } + } + if (any < 0) { + errno = ERANGE; + acc = neg ? LLONG_MIN : LLONG_MAX; + } else if (neg) + acc = -acc; + +exit: + if (endptr != 0) + *endptr = __DECONST(char *, any ? s - 1 : nptr); + return acc; +} + +/* + * Convert a string to an unsigned long long integer. + * + * Ignores `locale' stuff. Assumes that the upper and lower case + * alphabets and digits are each contiguous. + */ +unsigned long long strtoull_isr(const char *nptr, char **endptr, int base) +{ + const char *s = nptr; + unsigned long long acc; + unsigned char c; + unsigned long long qbase, cutoff; + int neg, any, cutlim; + + if (base < 0 || base == 1 || base > 36) { + errno = -EINVAL; + any = 0; + acc = 0; + goto exit; + } + /* + * See strtoq for comments as to the logic used. + */ + do { + c = *s++; + } while (isspace(c)); + if (c == '-') { + neg = 1; + c = *s++; + } else { + neg = 0; + if (c == '+') + c = *s++; + } + if ((base == 0 || base == 16) && c == '0' && (*s == 'x' || *s == 'X')) { + c = s[1]; + s += 2; + base = 16; + } + if (base == 0) + base = c == '0' ? 8 : 10; + qbase = (unsigned int)base; + cutoff = (unsigned long long)ULLONG_MAX / qbase; + cutlim = (unsigned long long)ULLONG_MAX % qbase; + for (acc = 0, any = 0;; c = *s++) { + if (!isascii(c)) + break; + if (isdigit(c)) + c -= '0'; + else if (isalpha(c)) + c -= isupper(c) ? 'A' - 10 : 'a' - 10; + else + break; + if (c >= base) + break; + if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) + any = -1; + else { + any = 1; + acc *= qbase; + acc += c; + } + } + if (any < 0) { + errno = ERANGE; + acc = ULLONG_MAX; + } else if (neg) + acc = -acc; + +exit: + if (endptr != 0) + *endptr = __DECONST(char *, any ? s - 1 : nptr); + return acc; +} + +int atoi_isr(const char *s) +{ + long long atoll; + + atoll = strtoll_isr(s, NULL, 10); + atoll = (atoll > __I_MAX) ? __I_MAX : atoll; + atoll = (atoll < __I_MIN) ? __I_MIN : atoll; + + return (int) atoll; +} -- 2.25.1
|
Lists.xenproject.org is hosted with RackSpace, monitoring our |