blob: 6fd09da5b62c525308d331589069414534a1ffea [file] [log] [blame]
Morten Borup Petersen406ac202020-01-29 16:44:17 +00001/*
2 * Copyright (c) 2020, ARM Limited and Contributors. All rights reserved.
3 *
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 is returned */
35}