From 277713e0ae3b9a20d661cbd9e214112f67bed35b Mon Sep 17 00:00:00 2001 From: Manish V Badarkhe Date: Tue, 21 Jan 2025 10:27:45 +0000 Subject: [PATCH 1/3] feat(libc): import qsort implementation Import qsort implementation from FreeBSD[1] to libc. [1]: https://cgit.freebsd.org/src/tree/lib/libc/stdlib/qsort.c Change-Id: Ia0d8e2d1c40c679844c0746db1b669cda672a482 Signed-off-by: Manish V Badarkhe --- include/lib/libc/cdefs.h | 5 +- include/lib/libc/stdlib.h | 2 + lib/libc/libc_common.mk | 3 +- lib/libc/qsort.c | 261 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 269 insertions(+), 2 deletions(-) create mode 100644 lib/libc/qsort.c diff --git a/include/lib/libc/cdefs.h b/include/lib/libc/cdefs.h index b11d0727d..97b78249a 100644 --- a/include/lib/libc/cdefs.h +++ b/include/lib/libc/cdefs.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2022, Arm Limited and Contributors. All rights reserved. + * Copyright (c) 2018-2025, Arm Limited and Contributors. All rights reserved. * * SPDX-License-Identifier: BSD-3-Clause */ @@ -36,4 +36,7 @@ #define __STRING(x) #x #define __XSTRING(x) __STRING(x) +#define __predict_true(exp) (exp) +#define __predict_false(exp) (exp) + #endif /* CDEFS_H */ diff --git a/include/lib/libc/stdlib.h b/include/lib/libc/stdlib.h index 4e5a824fa..cf30ef32a 100644 --- a/include/lib/libc/stdlib.h +++ b/include/lib/libc/stdlib.h @@ -29,4 +29,6 @@ long strtol(const char *nptr, char **endptr, int base); unsigned long strtoul(const char *nptr, char **endptr, int base); long long strtoll(const char *nptr, char **endptr, int base); unsigned long long strtoull(const char *nptr, char **endptr, int base); +void qsort(void *, size_t, size_t, + int (* _Nonnull)(const void *, const void *)); #endif /* STDLIB_H */ diff --git a/lib/libc/libc_common.mk b/lib/libc/libc_common.mk index 4879818da..5f44bd517 100644 --- a/lib/libc/libc_common.mk +++ b/lib/libc/libc_common.mk @@ -1,5 +1,5 @@ # -# Copyright (c) 2024, Arm Limited and Contributors. All rights reserved. +# Copyright (c) 2024-2025, Arm Limited and Contributors. All rights reserved. # # SPDX-License-Identifier: BSD-3-Clause # @@ -17,6 +17,7 @@ LIBC_SRCS := $(addprefix lib/libc/, \ printf.c \ putchar.c \ puts.c \ + qsort.c \ snprintf.c \ strchr.c \ strcmp.c \ diff --git a/lib/libc/qsort.c b/lib/libc/qsort.c new file mode 100644 index 000000000..40569f408 --- /dev/null +++ b/lib/libc/qsort.c @@ -0,0 +1,261 @@ +/*- + * SPDX-License-Identifier: BSD-3-Clause + * + * 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 +#include +#include +#include +#include + +#if defined(I_AM_QSORT_R) +typedef int cmp_t(const void *, const void *, void *); +#elif defined(I_AM_QSORT_R_COMPAT) +typedef int cmp_t(void *, const void *, const void *); +#elif defined(I_AM_QSORT_S) +typedef int cmp_t(const void *, const void *, void *); +#else +typedef int cmp_t(const void *, const void *); +#endif +static inline char *med3(char *, char *, char *, cmp_t *, void *); + +#define MIN(a, b) ((a) < (b) ? a : b) + +/* + * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function". + */ + +static inline void +swapfunc(char *a, char *b, size_t es) +{ + char t; + + do { + t = *a; + *a++ = *b; + *b++ = t; + } while (--es > 0); +} + +#define vecswap(a, b, n) \ + if ((n) > 0) swapfunc(a, b, n) + +#if defined(I_AM_QSORT_R) +#define CMP(t, x, y) (cmp((x), (y), (t))) +#elif defined(I_AM_QSORT_R_COMPAT) +#define CMP(t, x, y) (cmp((t), (x), (y))) +#elif defined(I_AM_QSORT_S) +#define CMP(t, x, y) (cmp((x), (y), (t))) +#else +#define CMP(t, x, y) (cmp((x), (y))) +#endif + +static inline char * +med3(char *a, char *b, char *c, cmp_t *cmp, void *thunk +#if !defined(I_AM_QSORT_R) && !defined(I_AM_QSORT_R_COMPAT) && !defined(I_AM_QSORT_S) +__unused +#endif +) +{ + return CMP(thunk, a, b) < 0 ? + (CMP(thunk, b, c) < 0 ? b : (CMP(thunk, a, c) < 0 ? c : a )) + :(CMP(thunk, b, c) > 0 ? b : (CMP(thunk, a, c) < 0 ? a : c )); +} + +/* + * The actual qsort() implementation is static to avoid preemptible calls when + * recursing. Also give them different names for improved debugging. + */ +#if defined(I_AM_QSORT_R) +#define local_qsort local_qsort_r +#elif defined(I_AM_QSORT_R_COMPAT) +#define local_qsort local_qsort_r_compat +#elif defined(I_AM_QSORT_S) +#define local_qsort local_qsort_s +#endif +static void +local_qsort(void *a, size_t n, size_t es, cmp_t *cmp, void *thunk) +{ + char *pa, *pb, *pc, *pd, *pl, *pm, *pn; + size_t d1, d2; + int cmp_result; + int swap_cnt; + + /* if there are less than 2 elements, then sorting is not needed */ + if (__predict_false(n < 2)) + return; +loop: + 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(thunk, pl - es, pl) > 0; + pl -= es) + swapfunc(pl, pl - es, es); + return; + } + pm = (char *)a + (n / 2) * es; + if (n > 7) { + pl = a; + pn = (char *)a + (n - 1) * es; + if (n > 40) { + size_t d = (n / 8) * es; + + pl = med3(pl, pl + d, pl + 2 * d, cmp, thunk); + pm = med3(pm - d, pm, pm + d, cmp, thunk); + pn = med3(pn - 2 * d, pn - d, pn, cmp, thunk); + } + pm = med3(pl, pm, pn, cmp, thunk); + } + swapfunc(a, pm, es); + pa = pb = (char *)a + es; + + pc = pd = (char *)a + (n - 1) * es; + for (;;) { + while (pb <= pc && (cmp_result = CMP(thunk, pb, a)) <= 0) { + if (cmp_result == 0) { + swap_cnt = 1; + swapfunc(pa, pb, es); + pa += es; + } + pb += es; + } + while (pb <= pc && (cmp_result = CMP(thunk, pc, a)) >= 0) { + if (cmp_result == 0) { + swap_cnt = 1; + swapfunc(pc, pd, es); + pd -= es; + } + pc -= es; + } + if (pb > pc) + break; + swapfunc(pb, pc, es); + 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(thunk, pl - es, pl) > 0; + pl -= es) + swapfunc(pl, pl - es, es); + return; + } + + pn = (char *)a + n * es; + d1 = MIN(pa - (char *)a, pb - pa); + vecswap(a, pb - d1, d1); + /* + * Cast es to preserve signedness of right-hand side of MIN() + * expression, to avoid sign ambiguity in the implied comparison. es + * is safely within [0, SSIZE_MAX]. + */ + d1 = MIN(pd - pc, pn - pd - (ssize_t)es); + vecswap(pb, pn - d1, d1); + + d1 = pb - pa; + d2 = pd - pc; + if (d1 <= d2) { + /* Recurse on left partition, then iterate on right partition */ + if (d1 > es) { + local_qsort(a, d1 / es, es, cmp, thunk); + } + if (d2 > es) { + /* Iterate rather than recurse to save stack space */ + /* qsort(pn - d2, d2 / es, es, cmp); */ + a = pn - d2; + n = d2 / es; + goto loop; + } + } else { + /* Recurse on right partition, then iterate on left partition */ + if (d2 > es) { + local_qsort(pn - d2, d2 / es, es, cmp, thunk); + } + if (d1 > es) { + /* Iterate rather than recurse to save stack space */ + /* qsort(a, d1 / es, es, cmp); */ + n = d1 / es; + goto loop; + } + } +} + +#if defined(I_AM_QSORT_R) +void +(qsort_r)(void *a, size_t n, size_t es, cmp_t *cmp, void *thunk) +{ + local_qsort_r(a, n, es, cmp, thunk); +} +#elif defined(I_AM_QSORT_R_COMPAT) +void +__qsort_r_compat(void *a, size_t n, size_t es, void *thunk, cmp_t *cmp) +{ + local_qsort_r_compat(a, n, es, cmp, thunk); +} +#elif defined(I_AM_QSORT_S) +errno_t +qsort_s(void *a, rsize_t n, rsize_t es, cmp_t *cmp, void *thunk) +{ + if (n > RSIZE_MAX) { + __throw_constraint_handler_s("qsort_s : n > RSIZE_MAX", EINVAL); + return (EINVAL); + } else if (es > RSIZE_MAX) { + __throw_constraint_handler_s("qsort_s : es > RSIZE_MAX", + EINVAL); + return (EINVAL); + } else if (n != 0) { + if (a == NULL) { + __throw_constraint_handler_s("qsort_s : a == NULL", + EINVAL); + return (EINVAL); + } else if (cmp == NULL) { + __throw_constraint_handler_s("qsort_s : cmp == NULL", + EINVAL); + return (EINVAL); + } else if (es <= 0) { + __throw_constraint_handler_s("qsort_s : es <= 0", + EINVAL); + return (EINVAL); + } + } + + local_qsort_s(a, n, es, cmp, thunk); + return (0); +} +#else +void +qsort(void *a, size_t n, size_t es, cmp_t *cmp) +{ + local_qsort(a, n, es, cmp, NULL); +} +#endif From 7cf37848140d3ba29b5967b46acbc5464b0b04b0 Mon Sep 17 00:00:00 2001 From: Manish V Badarkhe Date: Thu, 12 Dec 2024 08:44:59 +0000 Subject: [PATCH 2/3] fix(drtm): sort the address-map in ascending order As per the specification the address map region in the DLME data must be sorted. Change-Id: Ibf39dad33ef7ce739d6ec8632198df55a4e8a1c3 Signed-off-by: Manish V Badarkhe --- services/std_svc/drtm/drtm_res_address_map.c | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/services/std_svc/drtm/drtm_res_address_map.c b/services/std_svc/drtm/drtm_res_address_map.c index 86367061e..52e6eab33 100644 --- a/services/std_svc/drtm/drtm_res_address_map.c +++ b/services/std_svc/drtm/drtm_res_address_map.c @@ -1,10 +1,11 @@ /* - * Copyright (c) 2022 Arm Limited. All rights reserved. + * Copyright (c) 2022-2025 Arm Limited. All rights reserved. * * SPDX-License-Identifier: BSD-3-Clause */ #include +#include #include #include @@ -23,6 +24,20 @@ static uint8_t drtm_address_map[DRTM_ADDRESS_MAP_SIZE]; static uint64_t drtm_address_map_size; +static int compare_regions(const void *a, const void *b) +{ + const drtm_mem_region_t *region_a = (const drtm_mem_region_t *)a; + const drtm_mem_region_t *region_b = (const drtm_mem_region_t *)b; + + if (region_a->region_address < region_b->region_address) { + return -1; + } else if (region_a->region_address > region_b->region_address) { + return 1; + } else { + return 0; + } +} + drtm_memory_region_descriptor_table_t *drtm_build_address_map(void) { /* Set up pointer to DRTM memory map. */ @@ -75,6 +90,9 @@ drtm_memory_region_descriptor_table_t *drtm_build_address_map(void) map->num_regions = i; + qsort(map->region, map->num_regions, sizeof(drtm_mem_region_t), + compare_regions); + /* Store total size of address map. */ drtm_address_map_size = sizeof(drtm_memory_region_descriptor_table_t); drtm_address_map_size += (i * sizeof(drtm_mem_region_t)); From 28e8f9d93329b45e8dc9bf6ee1f3d14bde9adda2 Mon Sep 17 00:00:00 2001 From: Manish V Badarkhe Date: Thu, 6 Feb 2025 17:02:16 +0000 Subject: [PATCH 3/3] fix(drtm): fix DLME data size check dlme_data_min_size is currently defined in pages but is being compared against byte sizes in the code. This patch corrects this issue. Change-Id: Ib250ef6efedf321706624dfca263e8042a25f6d1 Signed-off-by: Manish V Badarkhe --- services/std_svc/drtm/drtm_main.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/services/std_svc/drtm/drtm_main.c b/services/std_svc/drtm/drtm_main.c index f58f615c6..37f2a2fe5 100644 --- a/services/std_svc/drtm/drtm_main.c +++ b/services/std_svc/drtm/drtm_main.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2024 Arm Limited. All rights reserved. + * Copyright (c) 2022-2025 Arm Limited. All rights reserved. * * SPDX-License-Identifier: BSD-3-Clause * @@ -112,8 +112,6 @@ int drtm_setup(void) dlme_data_hdr_init.dlme_tcb_hashes_table_size + dlme_data_hdr_init.dlme_impdef_region_size; - dlme_data_min_size = page_align(dlme_data_min_size, UP)/PAGE_SIZE; - /* Fill out platform DRTM features structure */ /* Only support default PCR schema (0x1) in this implementation. */ ARM_DRTM_TPM_FEATURES_SET_PCR_SCHEMA(plat_drtm_features.tpm_features, @@ -123,7 +121,7 @@ int drtm_setup(void) ARM_DRTM_TPM_FEATURES_SET_FW_HASH(plat_drtm_features.tpm_features, plat_tpm_feat->firmware_hash_algorithm); ARM_DRTM_MIN_MEM_REQ_SET_MIN_DLME_DATA_SIZE(plat_drtm_features.minimum_memory_requirement, - dlme_data_min_size); + page_align(dlme_data_min_size, UP)/PAGE_SIZE); ARM_DRTM_MIN_MEM_REQ_SET_DCE_SIZE(plat_drtm_features.minimum_memory_requirement, plat_drtm_get_min_size_normal_world_dce()); ARM_DRTM_DMA_PROT_FEATURES_SET_MAX_REGIONS(plat_drtm_features.dma_prot_features, @@ -237,7 +235,7 @@ static enum drtm_retc drtm_dl_prepare_dlme_data(const struct_drtm_dl_args *args) */ if (dlme_data_max_size < dlme_data_min_size) { ERROR("%s: assertion failed:" - " dlme_data_max_size (%ld) < dlme_data_total_bytes_req (%ld)\n", + " dlme_data_max_size (%ld) < dlme_data_min_size (%ld)\n", __func__, dlme_data_max_size, dlme_data_min_size); panic(); }