xref: /arm-trusted-firmware/plat/qemu/common/qemu_stack_protector.c (revision 91f16700b400a8c0651d24a598fc48ee2997a0d7)
1*91f16700Schasinglulu /*
2*91f16700Schasinglulu  * Copyright (c) 2021, ARM Limited and Contributors. All rights reserved.
3*91f16700Schasinglulu  *
4*91f16700Schasinglulu  * SPDX-License-Identifier: BSD-3-Clause
5*91f16700Schasinglulu  */
6*91f16700Schasinglulu 
7*91f16700Schasinglulu #include <stdint.h>
8*91f16700Schasinglulu 
9*91f16700Schasinglulu #include <arch_helpers.h>
10*91f16700Schasinglulu #include <arch_features.h>
11*91f16700Schasinglulu #include <plat/common/platform.h>
12*91f16700Schasinglulu 
13*91f16700Schasinglulu #define RANDOM_CANARY_VALUE ((u_register_t) 3288484550995823360ULL)
14*91f16700Schasinglulu 
15*91f16700Schasinglulu u_register_t plat_get_stack_protector_canary(void)
16*91f16700Schasinglulu {
17*91f16700Schasinglulu 	/* Use the RNDR instruction if the CPU supports it */
18*91f16700Schasinglulu 	if (is_feat_rng_supported()) {
19*91f16700Schasinglulu 		return read_rndr();
20*91f16700Schasinglulu 	}
21*91f16700Schasinglulu 
22*91f16700Schasinglulu 	/*
23*91f16700Schasinglulu 	 * Ideally, a random number should be returned above. If a random
24*91f16700Schasinglulu 	 * number generator is not supported, return instead a
25*91f16700Schasinglulu 	 * combination of a timer's value and a compile-time constant.
26*91f16700Schasinglulu 	 * This is better than nothing but not necessarily really secure.
27*91f16700Schasinglulu 	 */
28*91f16700Schasinglulu 	return RANDOM_CANARY_VALUE ^ read_cntpct_el0();
29*91f16700Schasinglulu }
30*91f16700Schasinglulu 
31