blob: 393235e9dd824389300ac1adca7bdc95b28ff143 [file] [log] [blame]
Abdellatif El Khlifiad9b8e52021-04-21 17:20:43 +01001/*
Vishnu Banavath2b651ea2022-01-19 18:43:12 +00002 * Copyright (c) 2021-2022, ARM Limited and Contributors. All rights reserved.
Abdellatif El Khlifiad9b8e52021-04-21 17:20:43 +01003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <stdint.h>
8
9#include <arch_helpers.h>
10#include <plat/common/platform.h>
11
12static uint32_t plat_generate_random_number(void)
13{
14 uintptr_t return_addr = (uintptr_t)__builtin_return_address(0U);
15 uintptr_t frame_addr = (uintptr_t)__builtin_frame_address(0U);
16 uint64_t cntpct = read_cntpct_el0();
17
18 /* Generate 32-bit pattern: saving the 2 least significant bytes
19 * in random_lo and random_hi
20 */
21 uint16_t random_lo = (uint16_t)(
22 (((uint64_t)return_addr) << 13) ^ frame_addr ^ cntpct
23 );
24
25 uint16_t random_hi = (uint16_t)(
26 (((uint64_t)frame_addr) << 15) ^ return_addr ^ cntpct
27 );
28
29 return (((uint32_t)random_hi) << 16) | random_lo;
30}
31
32u_register_t plat_get_stack_protector_canary(void)
33{
34 return plat_generate_random_number(); /* a 32-bit pattern returned */
35}