1 /*
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 
plat_generate_random_number(void)12 static 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 
plat_get_stack_protector_canary(void)32 u_register_t plat_get_stack_protector_canary(void)
33 {
34 	return  plat_generate_random_number(); /* a 32-bit pattern is returned */
35 }
36