162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci#ifndef _ASM_X86_PAGE_64_H
362306a36Sopenharmony_ci#define _ASM_X86_PAGE_64_H
462306a36Sopenharmony_ci
562306a36Sopenharmony_ci#include <asm/page_64_types.h>
662306a36Sopenharmony_ci
762306a36Sopenharmony_ci#ifndef __ASSEMBLY__
862306a36Sopenharmony_ci#include <asm/cpufeatures.h>
962306a36Sopenharmony_ci#include <asm/alternative.h>
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci#include <linux/kmsan-checks.h>
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ci/* duplicated to the one in bootmem.h */
1462306a36Sopenharmony_ciextern unsigned long max_pfn;
1562306a36Sopenharmony_ciextern unsigned long phys_base;
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ciextern unsigned long page_offset_base;
1862306a36Sopenharmony_ciextern unsigned long vmalloc_base;
1962306a36Sopenharmony_ciextern unsigned long vmemmap_base;
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_cistatic __always_inline unsigned long __phys_addr_nodebug(unsigned long x)
2262306a36Sopenharmony_ci{
2362306a36Sopenharmony_ci	unsigned long y = x - __START_KERNEL_map;
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci	/* use the carry flag to determine if x was < __START_KERNEL_map */
2662306a36Sopenharmony_ci	x = y + ((x > y) ? phys_base : (__START_KERNEL_map - PAGE_OFFSET));
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci	return x;
2962306a36Sopenharmony_ci}
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ci#ifdef CONFIG_DEBUG_VIRTUAL
3262306a36Sopenharmony_ciextern unsigned long __phys_addr(unsigned long);
3362306a36Sopenharmony_ciextern unsigned long __phys_addr_symbol(unsigned long);
3462306a36Sopenharmony_ci#else
3562306a36Sopenharmony_ci#define __phys_addr(x)		__phys_addr_nodebug(x)
3662306a36Sopenharmony_ci#define __phys_addr_symbol(x) \
3762306a36Sopenharmony_ci	((unsigned long)(x) - __START_KERNEL_map + phys_base)
3862306a36Sopenharmony_ci#endif
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ci#define __phys_reloc_hide(x)	(x)
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_civoid clear_page_orig(void *page);
4362306a36Sopenharmony_civoid clear_page_rep(void *page);
4462306a36Sopenharmony_civoid clear_page_erms(void *page);
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_cistatic inline void clear_page(void *page)
4762306a36Sopenharmony_ci{
4862306a36Sopenharmony_ci	/*
4962306a36Sopenharmony_ci	 * Clean up KMSAN metadata for the page being cleared. The assembly call
5062306a36Sopenharmony_ci	 * below clobbers @page, so we perform unpoisoning before it.
5162306a36Sopenharmony_ci	 */
5262306a36Sopenharmony_ci	kmsan_unpoison_memory(page, PAGE_SIZE);
5362306a36Sopenharmony_ci	alternative_call_2(clear_page_orig,
5462306a36Sopenharmony_ci			   clear_page_rep, X86_FEATURE_REP_GOOD,
5562306a36Sopenharmony_ci			   clear_page_erms, X86_FEATURE_ERMS,
5662306a36Sopenharmony_ci			   "=D" (page),
5762306a36Sopenharmony_ci			   "0" (page)
5862306a36Sopenharmony_ci			   : "cc", "memory", "rax", "rcx");
5962306a36Sopenharmony_ci}
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_civoid copy_page(void *to, void *from);
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci#ifdef CONFIG_X86_5LEVEL
6462306a36Sopenharmony_ci/*
6562306a36Sopenharmony_ci * User space process size.  This is the first address outside the user range.
6662306a36Sopenharmony_ci * There are a few constraints that determine this:
6762306a36Sopenharmony_ci *
6862306a36Sopenharmony_ci * On Intel CPUs, if a SYSCALL instruction is at the highest canonical
6962306a36Sopenharmony_ci * address, then that syscall will enter the kernel with a
7062306a36Sopenharmony_ci * non-canonical return address, and SYSRET will explode dangerously.
7162306a36Sopenharmony_ci * We avoid this particular problem by preventing anything
7262306a36Sopenharmony_ci * from being mapped at the maximum canonical address.
7362306a36Sopenharmony_ci *
7462306a36Sopenharmony_ci * On AMD CPUs in the Ryzen family, there's a nasty bug in which the
7562306a36Sopenharmony_ci * CPUs malfunction if they execute code from the highest canonical page.
7662306a36Sopenharmony_ci * They'll speculate right off the end of the canonical space, and
7762306a36Sopenharmony_ci * bad things happen.  This is worked around in the same way as the
7862306a36Sopenharmony_ci * Intel problem.
7962306a36Sopenharmony_ci *
8062306a36Sopenharmony_ci * With page table isolation enabled, we map the LDT in ... [stay tuned]
8162306a36Sopenharmony_ci */
8262306a36Sopenharmony_cistatic __always_inline unsigned long task_size_max(void)
8362306a36Sopenharmony_ci{
8462306a36Sopenharmony_ci	unsigned long ret;
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_ci	alternative_io("movq %[small],%0","movq %[large],%0",
8762306a36Sopenharmony_ci			X86_FEATURE_LA57,
8862306a36Sopenharmony_ci			"=r" (ret),
8962306a36Sopenharmony_ci			[small] "i" ((1ul << 47)-PAGE_SIZE),
9062306a36Sopenharmony_ci			[large] "i" ((1ul << 56)-PAGE_SIZE));
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ci	return ret;
9362306a36Sopenharmony_ci}
9462306a36Sopenharmony_ci#endif	/* CONFIG_X86_5LEVEL */
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_ci#endif	/* !__ASSEMBLY__ */
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_ci#ifdef CONFIG_X86_VSYSCALL_EMULATION
9962306a36Sopenharmony_ci# define __HAVE_ARCH_GATE_AREA 1
10062306a36Sopenharmony_ci#endif
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_ci#endif /* _ASM_X86_PAGE_64_H */
103