18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * GCC stack protector support.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Stack protector works by putting predefined pattern at the start of
68c2ecf20Sopenharmony_ci * the stack frame and verifying that it hasn't been overwritten when
78c2ecf20Sopenharmony_ci * returning from the function.  The pattern is called stack canary
88c2ecf20Sopenharmony_ci * and gcc expects it to be defined by a global variable called
98c2ecf20Sopenharmony_ci * "__stack_chk_guard" on LoongArch. This unfortunately means that on SMP
108c2ecf20Sopenharmony_ci * we cannot have a different canary value per task.
118c2ecf20Sopenharmony_ci */
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#ifndef _ASM_STACKPROTECTOR_H
148c2ecf20Sopenharmony_ci#define _ASM_STACKPROTECTOR_H 1
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#include <linux/random.h>
178c2ecf20Sopenharmony_ci#include <linux/version.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ciextern unsigned long __stack_chk_guard;
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci/*
228c2ecf20Sopenharmony_ci * Initialize the stackprotector canary value.
238c2ecf20Sopenharmony_ci *
248c2ecf20Sopenharmony_ci * NOTE: this must only be called from functions that never return,
258c2ecf20Sopenharmony_ci * and it must always be inlined.
268c2ecf20Sopenharmony_ci */
278c2ecf20Sopenharmony_cistatic __always_inline void boot_init_stack_canary(void)
288c2ecf20Sopenharmony_ci{
298c2ecf20Sopenharmony_ci	unsigned long canary;
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci	/* Try to get a semi random initial value. */
328c2ecf20Sopenharmony_ci	get_random_bytes(&canary, sizeof(canary));
338c2ecf20Sopenharmony_ci	canary ^= LINUX_VERSION_CODE;
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci	current->stack_canary = canary;
368c2ecf20Sopenharmony_ci	__stack_chk_guard = current->stack_canary;
378c2ecf20Sopenharmony_ci}
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci#endif	/* _ASM_STACKPROTECTOR_H */
40