rcar_stack_protector.c 800 B

123456789101112131415161718192021222324252627282930313233343536
  1. /*
  2. * Copyright (c) 2021-2023, Renesas Electronics Corporation. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <stdint.h>
  7. #include <arch_helpers.h>
  8. #include <common/debug.h>
  9. #define RANDOM_CANARY_VALUE ((u_register_t)0xDFF5FC8A720E205EULL)
  10. u_register_t plat_get_stack_protector_canary(void)
  11. {
  12. u_register_t cnt;
  13. u_register_t seed;
  14. u_register_t mul;
  15. u_register_t ret;
  16. uintptr_t val1 = (uintptr_t)__builtin_return_address(0U);
  17. uintptr_t val2 = (uintptr_t)__builtin_frame_address(0U);
  18. cnt = read_cntpct_el0();
  19. seed = (cnt ^ RANDOM_CANARY_VALUE) & ULONG_MAX;
  20. ret = seed;
  21. if ((ULONG_MAX/val1) > seed) {
  22. mul = (u_register_t)(val1 * seed);
  23. if ((mul < ULONG_MAX) &&
  24. ((ULONG_MAX - (u_register_t)mul) > val2)) {
  25. ret = mul + val2;
  26. }
  27. }
  28. return ret;
  29. }