mirror of
https://github.com/ARM-software/arm-trusted-firmware.git
synced 2025-04-15 00:54:22 +00:00
Merge changes from topic "mb/drtm" into integration
* changes: fix(drtm): fix DLME data size check fix(drtm): sort the address-map in ascending order feat(libc): import qsort implementation
This commit is contained in:
commit
6db9aac6c3
6 changed files with 291 additions and 8 deletions
|
@ -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 */
|
||||
|
|
|
@ -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 */
|
||||
|
|
|
@ -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 \
|
||||
|
|
261
lib/libc/qsort.c
Normal file
261
lib/libc/qsort.c
Normal file
|
@ -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 <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#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
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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 <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <plat/common/platform.h>
|
||||
#include <services/drtm_svc.h>
|
||||
|
@ -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));
|
||||
|
|
Loading…
Add table
Reference in a new issue