18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci
38c2ecf20Sopenharmony_ci#ifndef _ASM_RISCV_STACKPROTECTOR_H
48c2ecf20Sopenharmony_ci#define _ASM_RISCV_STACKPROTECTOR_H
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <linux/random.h>
78c2ecf20Sopenharmony_ci#include <linux/version.h>
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ciextern unsigned long __stack_chk_guard;
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci/*
128c2ecf20Sopenharmony_ci * Initialize the stackprotector canary value.
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * NOTE: this must only be called from functions that never return,
158c2ecf20Sopenharmony_ci * and it must always be inlined.
168c2ecf20Sopenharmony_ci */
178c2ecf20Sopenharmony_cistatic __always_inline void boot_init_stack_canary(void)
188c2ecf20Sopenharmony_ci{
198c2ecf20Sopenharmony_ci	unsigned long canary;
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci	/* Try to get a semi random initial value. */
228c2ecf20Sopenharmony_ci	get_random_bytes(&canary, sizeof(canary));
238c2ecf20Sopenharmony_ci	canary ^= LINUX_VERSION_CODE;
248c2ecf20Sopenharmony_ci	canary &= CANARY_MASK;
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci	current->stack_canary = canary;
278c2ecf20Sopenharmony_ci	__stack_chk_guard = current->stack_canary;
288c2ecf20Sopenharmony_ci}
298c2ecf20Sopenharmony_ci#endif /* _ASM_RISCV_STACKPROTECTOR_H */
30