memory.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Based on arch/arm/include/asm/memory.h
  4. *
  5. * Copyright (C) 2000-2002 Russell King
  6. * Copyright (C) 2012 ARM Ltd.
  7. *
  8. * Note: this file should not be included by non-asm/.h files
  9. */
  10. #ifndef __ASM_MEMORY_H
  11. #define __ASM_MEMORY_H
  12. #include <linux/const.h>
  13. #include <linux/sizes.h>
  14. #include <asm/page-def.h>
  15. /*
  16. * Size of the PCI I/O space. This must remain a power of two so that
  17. * IO_SPACE_LIMIT acts as a mask for the low bits of I/O addresses.
  18. */
  19. #define PCI_IO_SIZE SZ_16M
  20. /*
  21. * VMEMMAP_SIZE - allows the whole linear region to be covered by
  22. * a struct page array
  23. *
  24. * If we are configured with a 52-bit kernel VA then our VMEMMAP_SIZE
  25. * needs to cover the memory region from the beginning of the 52-bit
  26. * PAGE_OFFSET all the way to PAGE_END for 48-bit. This allows us to
  27. * keep a constant PAGE_OFFSET and "fallback" to using the higher end
  28. * of the VMEMMAP where 52-bit support is not available in hardware.
  29. */
  30. #define VMEMMAP_RANGE (_PAGE_END(VA_BITS_MIN) - PAGE_OFFSET)
  31. #define VMEMMAP_SIZE ((VMEMMAP_RANGE >> PAGE_SHIFT) * sizeof(struct page))
  32. /*
  33. * PAGE_OFFSET - the virtual address of the start of the linear map, at the
  34. * start of the TTBR1 address space.
  35. * PAGE_END - the end of the linear map, where all other kernel mappings begin.
  36. * KIMAGE_VADDR - the virtual address of the start of the kernel image.
  37. * VA_BITS - the maximum number of bits for virtual addresses.
  38. */
  39. #define VA_BITS (CONFIG_ARM64_VA_BITS)
  40. #define _PAGE_OFFSET(va) (-(UL(1) << (va)))
  41. #define PAGE_OFFSET (_PAGE_OFFSET(VA_BITS))
  42. #define KIMAGE_VADDR (MODULES_END)
  43. #define MODULES_END (MODULES_VADDR + MODULES_VSIZE)
  44. #define MODULES_VADDR (_PAGE_END(VA_BITS_MIN))
  45. #define MODULES_VSIZE (SZ_2G)
  46. #define VMEMMAP_START (VMEMMAP_END - VMEMMAP_SIZE)
  47. #define VMEMMAP_END (-UL(SZ_1G))
  48. #define PCI_IO_START (VMEMMAP_END + SZ_8M)
  49. #define PCI_IO_END (PCI_IO_START + PCI_IO_SIZE)
  50. #define FIXADDR_TOP (-UL(SZ_8M))
  51. #if VA_BITS > 48
  52. #ifdef CONFIG_ARM64_16K_PAGES
  53. #define VA_BITS_MIN (47)
  54. #else
  55. #define VA_BITS_MIN (48)
  56. #endif
  57. #else
  58. #define VA_BITS_MIN (VA_BITS)
  59. #endif
  60. #define _PAGE_END(va) (-(UL(1) << ((va) - 1)))
  61. #define KERNEL_START _text
  62. #define KERNEL_END _end
  63. /*
  64. * Generic and Software Tag-Based KASAN modes require 1/8th and 1/16th of the
  65. * kernel virtual address space for storing the shadow memory respectively.
  66. *
  67. * The mapping between a virtual memory address and its corresponding shadow
  68. * memory address is defined based on the formula:
  69. *
  70. * shadow_addr = (addr >> KASAN_SHADOW_SCALE_SHIFT) + KASAN_SHADOW_OFFSET
  71. *
  72. * where KASAN_SHADOW_SCALE_SHIFT is the order of the number of bits that map
  73. * to a single shadow byte and KASAN_SHADOW_OFFSET is a constant that offsets
  74. * the mapping. Note that KASAN_SHADOW_OFFSET does not point to the start of
  75. * the shadow memory region.
  76. *
  77. * Based on this mapping, we define two constants:
  78. *
  79. * KASAN_SHADOW_START: the start of the shadow memory region;
  80. * KASAN_SHADOW_END: the end of the shadow memory region.
  81. *
  82. * KASAN_SHADOW_END is defined first as the shadow address that corresponds to
  83. * the upper bound of possible virtual kernel memory addresses UL(1) << 64
  84. * according to the mapping formula.
  85. *
  86. * KASAN_SHADOW_START is defined second based on KASAN_SHADOW_END. The shadow
  87. * memory start must map to the lowest possible kernel virtual memory address
  88. * and thus it depends on the actual bitness of the address space.
  89. *
  90. * As KASAN inserts redzones between stack variables, this increases the stack
  91. * memory usage significantly. Thus, we double the (minimum) stack size.
  92. */
  93. #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
  94. #define KASAN_SHADOW_OFFSET _AC(CONFIG_KASAN_SHADOW_OFFSET, UL)
  95. #define KASAN_SHADOW_END ((UL(1) << (64 - KASAN_SHADOW_SCALE_SHIFT)) + KASAN_SHADOW_OFFSET)
  96. #define _KASAN_SHADOW_START(va) (KASAN_SHADOW_END - (UL(1) << ((va) - KASAN_SHADOW_SCALE_SHIFT)))
  97. #define KASAN_SHADOW_START _KASAN_SHADOW_START(vabits_actual)
  98. #define PAGE_END KASAN_SHADOW_START
  99. #define KASAN_THREAD_SHIFT 1
  100. #else
  101. #define KASAN_THREAD_SHIFT 0
  102. #define PAGE_END (_PAGE_END(VA_BITS_MIN))
  103. #endif /* CONFIG_KASAN */
  104. #define PHYSMEM_END __pa(PAGE_END - 1)
  105. #define MIN_THREAD_SHIFT (14 + KASAN_THREAD_SHIFT)
  106. /*
  107. * VMAP'd stacks are allocated at page granularity, so we must ensure that such
  108. * stacks are a multiple of page size.
  109. */
  110. #if defined(CONFIG_VMAP_STACK) && (MIN_THREAD_SHIFT < PAGE_SHIFT)
  111. #define THREAD_SHIFT PAGE_SHIFT
  112. #else
  113. #define THREAD_SHIFT MIN_THREAD_SHIFT
  114. #endif
  115. #if THREAD_SHIFT >= PAGE_SHIFT
  116. #define THREAD_SIZE_ORDER (THREAD_SHIFT - PAGE_SHIFT)
  117. #endif
  118. #define THREAD_SIZE (UL(1) << THREAD_SHIFT)
  119. /*
  120. * By aligning VMAP'd stacks to 2 * THREAD_SIZE, we can detect overflow by
  121. * checking sp & (1 << THREAD_SHIFT), which we can do cheaply in the entry
  122. * assembly.
  123. */
  124. #ifdef CONFIG_VMAP_STACK
  125. #define THREAD_ALIGN (2 * THREAD_SIZE)
  126. #else
  127. #define THREAD_ALIGN THREAD_SIZE
  128. #endif
  129. #define IRQ_STACK_SIZE THREAD_SIZE
  130. #define OVERFLOW_STACK_SIZE SZ_4K
  131. /*
  132. * With the minimum frame size of [x29, x30], exactly half the combined
  133. * sizes of the hyp and overflow stacks is the maximum size needed to
  134. * save the unwinded stacktrace; plus an additional entry to delimit the
  135. * end.
  136. */
  137. #define NVHE_STACKTRACE_SIZE ((OVERFLOW_STACK_SIZE + PAGE_SIZE) / 2 + sizeof(long))
  138. /*
  139. * Alignment of kernel segments (e.g. .text, .data).
  140. *
  141. * 4 KB granule: 16 level 3 entries, with contiguous bit
  142. * 16 KB granule: 4 level 3 entries, without contiguous bit
  143. * 64 KB granule: 1 level 3 entry
  144. */
  145. #define SEGMENT_ALIGN SZ_64K
  146. /*
  147. * Memory types available.
  148. *
  149. * IMPORTANT: MT_NORMAL must be index 0 since vm_get_page_prot() may 'or' in
  150. * the MT_NORMAL_TAGGED memory type for PROT_MTE mappings. Note
  151. * that protection_map[] only contains MT_NORMAL attributes.
  152. */
  153. #define MT_NORMAL 0
  154. #define MT_NORMAL_TAGGED 1
  155. #define MT_NORMAL_NC 2
  156. #define MT_DEVICE_nGnRnE 3
  157. #define MT_DEVICE_nGnRE 4
  158. /*
  159. * Memory types for Stage-2 translation
  160. */
  161. #define MT_S2_NORMAL 0xf
  162. #define MT_S2_NORMAL_NC 0x5
  163. #define MT_S2_DEVICE_nGnRE 0x1
  164. /*
  165. * Memory types for Stage-2 translation when ID_AA64MMFR2_EL1.FWB is 0001
  166. * Stage-2 enforces Normal-WB and Device-nGnRE
  167. */
  168. #define MT_S2_FWB_NORMAL 6
  169. #define MT_S2_FWB_NORMAL_NC 5
  170. #define MT_S2_FWB_DEVICE_nGnRE 1
  171. #ifdef CONFIG_ARM64_4K_PAGES
  172. #define IOREMAP_MAX_ORDER (PUD_SHIFT)
  173. #else
  174. #define IOREMAP_MAX_ORDER (PMD_SHIFT)
  175. #endif
  176. /*
  177. * Open-coded (swapper_pg_dir - reserved_pg_dir) as this cannot be calculated
  178. * until link time.
  179. */
  180. #define RESERVED_SWAPPER_OFFSET (PAGE_SIZE)
  181. /*
  182. * Open-coded (swapper_pg_dir - tramp_pg_dir) as this cannot be calculated
  183. * until link time.
  184. */
  185. #define TRAMP_SWAPPER_OFFSET (2 * PAGE_SIZE)
  186. #ifndef __ASSEMBLY__
  187. #include <linux/bitops.h>
  188. #include <linux/compiler.h>
  189. #include <linux/mmdebug.h>
  190. #include <linux/types.h>
  191. #include <asm/boot.h>
  192. #include <asm/bug.h>
  193. #include <asm/sections.h>
  194. #include <asm/sysreg.h>
  195. static inline u64 __pure read_tcr(void)
  196. {
  197. u64 tcr;
  198. // read_sysreg() uses asm volatile, so avoid it here
  199. asm("mrs %0, tcr_el1" : "=r"(tcr));
  200. return tcr;
  201. }
  202. #if VA_BITS > 48
  203. // For reasons of #include hell, we can't use TCR_T1SZ_OFFSET/TCR_T1SZ_MASK here
  204. #define vabits_actual (64 - ((read_tcr() >> 16) & 63))
  205. #else
  206. #define vabits_actual ((u64)VA_BITS)
  207. #endif
  208. extern s64 memstart_addr;
  209. /* PHYS_OFFSET - the physical address of the start of memory. */
  210. #define PHYS_OFFSET ({ VM_BUG_ON(memstart_addr & 1); memstart_addr; })
  211. /* the offset between the kernel virtual and physical mappings */
  212. extern u64 kimage_voffset;
  213. static inline unsigned long kaslr_offset(void)
  214. {
  215. return (u64)&_text - KIMAGE_VADDR;
  216. }
  217. #ifdef CONFIG_RANDOMIZE_BASE
  218. void kaslr_init(void);
  219. static inline bool kaslr_enabled(void)
  220. {
  221. extern bool __kaslr_is_enabled;
  222. return __kaslr_is_enabled;
  223. }
  224. #else
  225. static inline void kaslr_init(void) { }
  226. static inline bool kaslr_enabled(void) { return false; }
  227. #endif
  228. /*
  229. * Allow all memory at the discovery stage. We will clip it later.
  230. */
  231. #define MIN_MEMBLOCK_ADDR 0
  232. #define MAX_MEMBLOCK_ADDR U64_MAX
  233. /*
  234. * PFNs are used to describe any physical page; this means
  235. * PFN 0 == physical address 0.
  236. *
  237. * This is the PFN of the first RAM page in the kernel
  238. * direct-mapped view. We assume this is the first page
  239. * of RAM in the mem_map as well.
  240. */
  241. #define PHYS_PFN_OFFSET (PHYS_OFFSET >> PAGE_SHIFT)
  242. /*
  243. * When dealing with data aborts, watchpoints, or instruction traps we may end
  244. * up with a tagged userland pointer. Clear the tag to get a sane pointer to
  245. * pass on to access_ok(), for instance.
  246. */
  247. #define __untagged_addr(addr) \
  248. ((__force __typeof__(addr))sign_extend64((__force u64)(addr), 55))
  249. #define untagged_addr(addr) ({ \
  250. u64 __addr = (__force u64)(addr); \
  251. __addr &= __untagged_addr(__addr); \
  252. (__force __typeof__(addr))__addr; \
  253. })
  254. #if defined(CONFIG_KASAN_SW_TAGS) || defined(CONFIG_KASAN_HW_TAGS)
  255. #define __tag_shifted(tag) ((u64)(tag) << 56)
  256. #define __tag_reset(addr) __untagged_addr(addr)
  257. #define __tag_get(addr) (__u8)((u64)(addr) >> 56)
  258. #else
  259. #define __tag_shifted(tag) 0UL
  260. #define __tag_reset(addr) (addr)
  261. #define __tag_get(addr) 0
  262. #endif /* CONFIG_KASAN_SW_TAGS || CONFIG_KASAN_HW_TAGS */
  263. static inline const void *__tag_set(const void *addr, u8 tag)
  264. {
  265. u64 __addr = (u64)addr & ~__tag_shifted(0xff);
  266. return (const void *)(__addr | __tag_shifted(tag));
  267. }
  268. #ifdef CONFIG_KASAN_HW_TAGS
  269. #define arch_enable_tag_checks_sync() mte_enable_kernel_sync()
  270. #define arch_enable_tag_checks_async() mte_enable_kernel_async()
  271. #define arch_enable_tag_checks_asymm() mte_enable_kernel_asymm()
  272. #define arch_suppress_tag_checks_start() mte_enable_tco()
  273. #define arch_suppress_tag_checks_stop() mte_disable_tco()
  274. #define arch_force_async_tag_fault() mte_check_tfsr_exit()
  275. #define arch_get_random_tag() mte_get_random_tag()
  276. #define arch_get_mem_tag(addr) mte_get_mem_tag(addr)
  277. #define arch_set_mem_tag_range(addr, size, tag, init) \
  278. mte_set_mem_tag_range((addr), (size), (tag), (init))
  279. #endif /* CONFIG_KASAN_HW_TAGS */
  280. /*
  281. * Physical vs virtual RAM address space conversion. These are
  282. * private definitions which should NOT be used outside memory.h
  283. * files. Use virt_to_phys/phys_to_virt/__pa/__va instead.
  284. */
  285. /*
  286. * Check whether an arbitrary address is within the linear map, which
  287. * lives in the [PAGE_OFFSET, PAGE_END) interval at the bottom of the
  288. * kernel's TTBR1 address range.
  289. */
  290. #define __is_lm_address(addr) (((u64)(addr) - PAGE_OFFSET) < (PAGE_END - PAGE_OFFSET))
  291. #define __lm_to_phys(addr) (((addr) - PAGE_OFFSET) + PHYS_OFFSET)
  292. #define __kimg_to_phys(addr) ((addr) - kimage_voffset)
  293. #define __virt_to_phys_nodebug(x) ({ \
  294. phys_addr_t __x = (phys_addr_t)(__tag_reset(x)); \
  295. __is_lm_address(__x) ? __lm_to_phys(__x) : __kimg_to_phys(__x); \
  296. })
  297. #define __pa_symbol_nodebug(x) __kimg_to_phys((phys_addr_t)(x))
  298. #ifdef CONFIG_DEBUG_VIRTUAL
  299. extern phys_addr_t __virt_to_phys(unsigned long x);
  300. extern phys_addr_t __phys_addr_symbol(unsigned long x);
  301. #else
  302. #define __virt_to_phys(x) __virt_to_phys_nodebug(x)
  303. #define __phys_addr_symbol(x) __pa_symbol_nodebug(x)
  304. #endif /* CONFIG_DEBUG_VIRTUAL */
  305. #define __phys_to_virt(x) ((unsigned long)((x) - PHYS_OFFSET) | PAGE_OFFSET)
  306. #define __phys_to_kimg(x) ((unsigned long)((x) + kimage_voffset))
  307. /*
  308. * Convert a page to/from a physical address
  309. */
  310. #define page_to_phys(page) (__pfn_to_phys(page_to_pfn(page)))
  311. #define phys_to_page(phys) (pfn_to_page(__phys_to_pfn(phys)))
  312. /*
  313. * Note: Drivers should NOT use these. They are the wrong
  314. * translation for translating DMA addresses. Use the driver
  315. * DMA support - see dma-mapping.h.
  316. */
  317. #define virt_to_phys virt_to_phys
  318. static inline phys_addr_t virt_to_phys(const volatile void *x)
  319. {
  320. return __virt_to_phys((unsigned long)(x));
  321. }
  322. #define phys_to_virt phys_to_virt
  323. static inline void *phys_to_virt(phys_addr_t x)
  324. {
  325. return (void *)(__phys_to_virt(x));
  326. }
  327. /* Needed already here for resolving __phys_to_pfn() in virt_to_pfn() */
  328. #include <asm-generic/memory_model.h>
  329. static inline unsigned long virt_to_pfn(const void *kaddr)
  330. {
  331. return __phys_to_pfn(virt_to_phys(kaddr));
  332. }
  333. /*
  334. * Drivers should NOT use these either.
  335. */
  336. #define __pa(x) __virt_to_phys((unsigned long)(x))
  337. #define __pa_symbol(x) __phys_addr_symbol(RELOC_HIDE((unsigned long)(x), 0))
  338. #define __pa_nodebug(x) __virt_to_phys_nodebug((unsigned long)(x))
  339. #define __va(x) ((void *)__phys_to_virt((phys_addr_t)(x)))
  340. #define pfn_to_kaddr(pfn) __va((pfn) << PAGE_SHIFT)
  341. #define sym_to_pfn(x) __phys_to_pfn(__pa_symbol(x))
  342. /*
  343. * virt_to_page(x) convert a _valid_ virtual address to struct page *
  344. * virt_addr_valid(x) indicates whether a virtual address is valid
  345. */
  346. #define ARCH_PFN_OFFSET ((unsigned long)PHYS_PFN_OFFSET)
  347. #if defined(CONFIG_DEBUG_VIRTUAL)
  348. #define page_to_virt(x) ({ \
  349. __typeof__(x) __page = x; \
  350. void *__addr = __va(page_to_phys(__page)); \
  351. (void *)__tag_set((const void *)__addr, page_kasan_tag(__page));\
  352. })
  353. #define virt_to_page(x) pfn_to_page(virt_to_pfn(x))
  354. #else
  355. #define page_to_virt(x) ({ \
  356. __typeof__(x) __page = x; \
  357. u64 __idx = ((u64)__page - VMEMMAP_START) / sizeof(struct page);\
  358. u64 __addr = PAGE_OFFSET + (__idx * PAGE_SIZE); \
  359. (void *)__tag_set((const void *)__addr, page_kasan_tag(__page));\
  360. })
  361. #define virt_to_page(x) ({ \
  362. u64 __idx = (__tag_reset((u64)x) - PAGE_OFFSET) / PAGE_SIZE; \
  363. u64 __addr = VMEMMAP_START + (__idx * sizeof(struct page)); \
  364. (struct page *)__addr; \
  365. })
  366. #endif /* CONFIG_DEBUG_VIRTUAL */
  367. #define virt_addr_valid(addr) ({ \
  368. __typeof__(addr) __addr = __tag_reset(addr); \
  369. __is_lm_address(__addr) && pfn_is_map_memory(virt_to_pfn(__addr)); \
  370. })
  371. void dump_mem_limit(void);
  372. #endif /* !ASSEMBLY */
  373. /*
  374. * Given that the GIC architecture permits ITS implementations that can only be
  375. * configured with a LPI table address once, GICv3 systems with many CPUs may
  376. * end up reserving a lot of different regions after a kexec for their LPI
  377. * tables (one per CPU), as we are forced to reuse the same memory after kexec
  378. * (and thus reserve it persistently with EFI beforehand)
  379. */
  380. #if defined(CONFIG_EFI) && defined(CONFIG_ARM_GIC_V3_ITS)
  381. # define INIT_MEMBLOCK_RESERVED_REGIONS (INIT_MEMBLOCK_REGIONS + NR_CPUS + 1)
  382. #endif
  383. /*
  384. * memory regions which marked with flag MEMBLOCK_NOMAP(for example, the memory
  385. * of the EFI_UNUSABLE_MEMORY type) may divide a continuous memory block into
  386. * multiple parts. As a result, the number of memory regions is large.
  387. */
  388. #ifdef CONFIG_EFI
  389. #define INIT_MEMBLOCK_MEMORY_REGIONS (INIT_MEMBLOCK_REGIONS * 8)
  390. #endif
  391. #endif /* __ASM_MEMORY_H */