memory.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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_SHIFT (PAGE_SHIFT - STRUCT_PAGE_MAX_SHIFT)
  31. #define VMEMMAP_SIZE ((_PAGE_END(VA_BITS_MIN) - PAGE_OFFSET) >> VMEMMAP_SHIFT)
  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_128M)
  46. #define VMEMMAP_START (-(UL(1) << (VA_BITS - VMEMMAP_SHIFT)))
  47. #define VMEMMAP_END (VMEMMAP_START + VMEMMAP_SIZE)
  48. #define PCI_IO_END (VMEMMAP_START - SZ_8M)
  49. #define PCI_IO_START (PCI_IO_END - PCI_IO_SIZE)
  50. #define FIXADDR_TOP (VMEMMAP_START - SZ_32M)
  51. #if VA_BITS > 48
  52. #define VA_BITS_MIN (48)
  53. #else
  54. #define VA_BITS_MIN (VA_BITS)
  55. #endif
  56. #define _PAGE_END(va) (-(UL(1) << ((va) - 1)))
  57. #define KERNEL_START _text
  58. #define KERNEL_END _end
  59. /*
  60. * Generic and tag-based KASAN require 1/8th and 1/16th of the kernel virtual
  61. * address space for the shadow region respectively. They can bloat the stack
  62. * significantly, so double the (minimum) stack size when they are in use.
  63. */
  64. #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
  65. #define KASAN_SHADOW_OFFSET _AC(CONFIG_KASAN_SHADOW_OFFSET, UL)
  66. #define KASAN_SHADOW_END ((UL(1) << (64 - KASAN_SHADOW_SCALE_SHIFT)) \
  67. + KASAN_SHADOW_OFFSET)
  68. #define PAGE_END (KASAN_SHADOW_END - (1UL << (vabits_actual - KASAN_SHADOW_SCALE_SHIFT)))
  69. #define KASAN_THREAD_SHIFT 1
  70. #else
  71. #define KASAN_THREAD_SHIFT 0
  72. #define PAGE_END (_PAGE_END(VA_BITS_MIN))
  73. #endif /* CONFIG_KASAN */
  74. #define MIN_THREAD_SHIFT (14 + KASAN_THREAD_SHIFT)
  75. /*
  76. * VMAP'd stacks are allocated at page granularity, so we must ensure that such
  77. * stacks are a multiple of page size.
  78. */
  79. #if defined(CONFIG_VMAP_STACK) && (MIN_THREAD_SHIFT < PAGE_SHIFT)
  80. #define THREAD_SHIFT PAGE_SHIFT
  81. #else
  82. #define THREAD_SHIFT MIN_THREAD_SHIFT
  83. #endif
  84. #if THREAD_SHIFT >= PAGE_SHIFT
  85. #define THREAD_SIZE_ORDER (THREAD_SHIFT - PAGE_SHIFT)
  86. #endif
  87. #define THREAD_SIZE (UL(1) << THREAD_SHIFT)
  88. /*
  89. * By aligning VMAP'd stacks to 2 * THREAD_SIZE, we can detect overflow by
  90. * checking sp & (1 << THREAD_SHIFT), which we can do cheaply in the entry
  91. * assembly.
  92. */
  93. #ifdef CONFIG_VMAP_STACK
  94. #define THREAD_ALIGN (2 * THREAD_SIZE)
  95. #else
  96. #define THREAD_ALIGN THREAD_SIZE
  97. #endif
  98. #define IRQ_STACK_SIZE THREAD_SIZE
  99. #define OVERFLOW_STACK_SIZE SZ_4K
  100. #if PAGE_SIZE == SZ_4K
  101. #define NVHE_STACK_SHIFT (PAGE_SHIFT + 1)
  102. #else
  103. #define NVHE_STACK_SHIFT PAGE_SHIFT
  104. #endif
  105. #define NVHE_STACK_SIZE (UL(1) << NVHE_STACK_SHIFT)
  106. /*
  107. * With the minimum frame size of [x29, x30], exactly half the combined
  108. * sizes of the hyp and overflow stacks is the maximum size needed to
  109. * save the unwinded stacktrace; plus an additional entry to delimit the
  110. * end.
  111. */
  112. #define NVHE_STACKTRACE_SIZE ((OVERFLOW_STACK_SIZE + NVHE_STACK_SIZE) / 2 + sizeof(long))
  113. /*
  114. * Alignment of kernel segments (e.g. .text, .data).
  115. *
  116. * 4 KB granule: 16 level 3 entries, with contiguous bit
  117. * 16 KB granule: 4 level 3 entries, without contiguous bit
  118. * 64 KB granule: 1 level 3 entry
  119. */
  120. #define SEGMENT_ALIGN SZ_64K
  121. /*
  122. * Memory types available.
  123. *
  124. * IMPORTANT: MT_NORMAL must be index 0 since vm_get_page_prot() may 'or' in
  125. * the MT_NORMAL_TAGGED memory type for PROT_MTE mappings. Note
  126. * that protection_map[] only contains MT_NORMAL attributes.
  127. */
  128. #define MT_NORMAL 0
  129. #define MT_NORMAL_TAGGED 1
  130. #define MT_NORMAL_NC 2
  131. #define MT_DEVICE_nGnRnE 3
  132. #define MT_DEVICE_nGnRE 4
  133. #define MT_NORMAL_iNC_oWB 5
  134. /*
  135. * Memory types for Stage-2 translation
  136. */
  137. #define MT_S2_NORMAL 0xf
  138. #define MT_S2_NORMAL_NC 0x5
  139. #define MT_S2_DEVICE_nGnRE 0x1
  140. /*
  141. * Memory types for Stage-2 translation when ID_AA64MMFR2_EL1.FWB is 0001
  142. * Stage-2 enforces Normal-WB and Device-nGnRE
  143. */
  144. #define MT_S2_FWB_NORMAL 6
  145. #define MT_S2_FWB_NORMAL_NC 5
  146. #define MT_S2_FWB_DEVICE_nGnRE 1
  147. #ifdef CONFIG_ARM64_4K_PAGES
  148. #define IOREMAP_MAX_ORDER (PUD_SHIFT)
  149. #else
  150. #define IOREMAP_MAX_ORDER (PMD_SHIFT)
  151. #endif
  152. /*
  153. * Open-coded (swapper_pg_dir - reserved_pg_dir) as this cannot be calculated
  154. * until link time.
  155. */
  156. #define RESERVED_SWAPPER_OFFSET (PAGE_SIZE)
  157. /*
  158. * Open-coded (swapper_pg_dir - tramp_pg_dir) as this cannot be calculated
  159. * until link time.
  160. */
  161. #define TRAMP_SWAPPER_OFFSET (2 * PAGE_SIZE)
  162. #ifndef __ASSEMBLY__
  163. #include <linux/bitops.h>
  164. #include <linux/compiler.h>
  165. #include <linux/mmdebug.h>
  166. #include <linux/types.h>
  167. #include <asm/bug.h>
  168. #if VA_BITS > 48
  169. extern u64 vabits_actual;
  170. #else
  171. #define vabits_actual ((u64)VA_BITS)
  172. #endif
  173. extern s64 memstart_addr;
  174. /* PHYS_OFFSET - the physical address of the start of memory. */
  175. #define PHYS_OFFSET ({ VM_BUG_ON(memstart_addr & 1); memstart_addr; })
  176. /* the virtual base of the kernel image */
  177. extern u64 kimage_vaddr;
  178. /* the offset between the kernel virtual and physical mappings */
  179. extern u64 kimage_voffset;
  180. static inline unsigned long kaslr_offset(void)
  181. {
  182. return kimage_vaddr - KIMAGE_VADDR;
  183. }
  184. /*
  185. * Allow all memory at the discovery stage. We will clip it later.
  186. */
  187. #define MIN_MEMBLOCK_ADDR 0
  188. #define MAX_MEMBLOCK_ADDR U64_MAX
  189. /*
  190. * PFNs are used to describe any physical page; this means
  191. * PFN 0 == physical address 0.
  192. *
  193. * This is the PFN of the first RAM page in the kernel
  194. * direct-mapped view. We assume this is the first page
  195. * of RAM in the mem_map as well.
  196. */
  197. #define PHYS_PFN_OFFSET (PHYS_OFFSET >> PAGE_SHIFT)
  198. /*
  199. * When dealing with data aborts, watchpoints, or instruction traps we may end
  200. * up with a tagged userland pointer. Clear the tag to get a sane pointer to
  201. * pass on to access_ok(), for instance.
  202. */
  203. #define __untagged_addr(addr) \
  204. ((__force __typeof__(addr))sign_extend64((__force u64)(addr), 55))
  205. #define untagged_addr(addr) ({ \
  206. u64 __addr = (__force u64)(addr); \
  207. __addr &= __untagged_addr(__addr); \
  208. (__force __typeof__(addr))__addr; \
  209. })
  210. #if defined(CONFIG_KASAN_SW_TAGS) || defined(CONFIG_KASAN_HW_TAGS)
  211. #define __tag_shifted(tag) ((u64)(tag) << 56)
  212. #define __tag_reset(addr) __untagged_addr(addr)
  213. #define __tag_get(addr) (__u8)((u64)(addr) >> 56)
  214. #else
  215. #define __tag_shifted(tag) 0UL
  216. #define __tag_reset(addr) (addr)
  217. #define __tag_get(addr) 0
  218. #endif /* CONFIG_KASAN_SW_TAGS || CONFIG_KASAN_HW_TAGS */
  219. static inline const void *__tag_set(const void *addr, u8 tag)
  220. {
  221. u64 __addr = (u64)addr & ~__tag_shifted(0xff);
  222. return (const void *)(__addr | __tag_shifted(tag));
  223. }
  224. #ifdef CONFIG_KASAN_HW_TAGS
  225. #define arch_enable_tag_checks_sync() mte_enable_kernel_sync()
  226. #define arch_enable_tag_checks_async() mte_enable_kernel_async()
  227. #define arch_enable_tag_checks_asymm() mte_enable_kernel_asymm()
  228. #define arch_suppress_tag_checks_start() mte_enable_tco()
  229. #define arch_suppress_tag_checks_stop() mte_disable_tco()
  230. #define arch_force_async_tag_fault() mte_check_tfsr_exit()
  231. #define arch_get_random_tag() mte_get_random_tag()
  232. #define arch_get_mem_tag(addr) mte_get_mem_tag(addr)
  233. #define arch_set_mem_tag_range(addr, size, tag, init) \
  234. mte_set_mem_tag_range((addr), (size), (tag), (init))
  235. #endif /* CONFIG_KASAN_HW_TAGS */
  236. /*
  237. * Physical vs virtual RAM address space conversion. These are
  238. * private definitions which should NOT be used outside memory.h
  239. * files. Use virt_to_phys/phys_to_virt/__pa/__va instead.
  240. */
  241. /*
  242. * Check whether an arbitrary address is within the linear map, which
  243. * lives in the [PAGE_OFFSET, PAGE_END) interval at the bottom of the
  244. * kernel's TTBR1 address range.
  245. */
  246. #define __is_lm_address(addr) (((u64)(addr) - PAGE_OFFSET) < (PAGE_END - PAGE_OFFSET))
  247. #define __lm_to_phys(addr) (((addr) - PAGE_OFFSET) + PHYS_OFFSET)
  248. #define __kimg_to_phys(addr) ((addr) - kimage_voffset)
  249. #define __virt_to_phys_nodebug(x) ({ \
  250. phys_addr_t __x = (phys_addr_t)(__tag_reset(x)); \
  251. __is_lm_address(__x) ? __lm_to_phys(__x) : __kimg_to_phys(__x); \
  252. })
  253. #define __pa_symbol_nodebug(x) __kimg_to_phys((phys_addr_t)(x))
  254. #ifdef CONFIG_DEBUG_VIRTUAL
  255. extern phys_addr_t __virt_to_phys(unsigned long x);
  256. extern phys_addr_t __phys_addr_symbol(unsigned long x);
  257. #else
  258. #define __virt_to_phys(x) __virt_to_phys_nodebug(x)
  259. #define __phys_addr_symbol(x) __pa_symbol_nodebug(x)
  260. #endif /* CONFIG_DEBUG_VIRTUAL */
  261. #define __phys_to_virt(x) ((unsigned long)((x) - PHYS_OFFSET) | PAGE_OFFSET)
  262. #define __phys_to_kimg(x) ((unsigned long)((x) + kimage_voffset))
  263. /*
  264. * Convert a page to/from a physical address
  265. */
  266. #define page_to_phys(page) (__pfn_to_phys(page_to_pfn(page)))
  267. #define phys_to_page(phys) (pfn_to_page(__phys_to_pfn(phys)))
  268. /*
  269. * Note: Drivers should NOT use these. They are the wrong
  270. * translation for translating DMA addresses. Use the driver
  271. * DMA support - see dma-mapping.h.
  272. */
  273. #define virt_to_phys virt_to_phys
  274. static inline phys_addr_t virt_to_phys(const volatile void *x)
  275. {
  276. return __virt_to_phys((unsigned long)(x));
  277. }
  278. #define phys_to_virt phys_to_virt
  279. static inline void *phys_to_virt(phys_addr_t x)
  280. {
  281. return (void *)(__phys_to_virt(x));
  282. }
  283. /*
  284. * Drivers should NOT use these either.
  285. */
  286. #define __pa(x) __virt_to_phys((unsigned long)(x))
  287. #define __pa_symbol(x) __phys_addr_symbol(RELOC_HIDE((unsigned long)(x), 0))
  288. #define __pa_nodebug(x) __virt_to_phys_nodebug((unsigned long)(x))
  289. #define __va(x) ((void *)__phys_to_virt((phys_addr_t)(x)))
  290. #define pfn_to_kaddr(pfn) __va((pfn) << PAGE_SHIFT)
  291. #define virt_to_pfn(x) __phys_to_pfn(__virt_to_phys((unsigned long)(x)))
  292. #define sym_to_pfn(x) __phys_to_pfn(__pa_symbol(x))
  293. /*
  294. * virt_to_page(x) convert a _valid_ virtual address to struct page *
  295. * virt_addr_valid(x) indicates whether a virtual address is valid
  296. */
  297. #define ARCH_PFN_OFFSET ((unsigned long)PHYS_PFN_OFFSET)
  298. #if defined(CONFIG_DEBUG_VIRTUAL)
  299. #define page_to_virt(x) ({ \
  300. __typeof__(x) __page = x; \
  301. void *__addr = __va(page_to_phys(__page)); \
  302. (void *)__tag_set((const void *)__addr, page_kasan_tag(__page));\
  303. })
  304. #define virt_to_page(x) pfn_to_page(virt_to_pfn(x))
  305. #else
  306. #define page_to_virt(x) ({ \
  307. __typeof__(x) __page = x; \
  308. u64 __idx = ((u64)__page - VMEMMAP_START) / sizeof(struct page);\
  309. u64 __addr = PAGE_OFFSET + (__idx * PAGE_SIZE); \
  310. (void *)__tag_set((const void *)__addr, page_kasan_tag(__page));\
  311. })
  312. #define virt_to_page(x) ({ \
  313. u64 __idx = (__tag_reset((u64)x) - PAGE_OFFSET) / PAGE_SIZE; \
  314. u64 __addr = VMEMMAP_START + (__idx * sizeof(struct page)); \
  315. (struct page *)__addr; \
  316. })
  317. #endif /* CONFIG_DEBUG_VIRTUAL */
  318. #define virt_addr_valid(addr) ({ \
  319. __typeof__(addr) __addr = __tag_reset(addr); \
  320. __is_lm_address(__addr) && pfn_is_map_memory(virt_to_pfn(__addr)); \
  321. })
  322. void dump_mem_limit(void);
  323. static inline bool defer_reserve_crashkernel(void)
  324. {
  325. return IS_ENABLED(CONFIG_ZONE_DMA) || IS_ENABLED(CONFIG_ZONE_DMA32);
  326. }
  327. #endif /* !ASSEMBLY */
  328. /*
  329. * Given that the GIC architecture permits ITS implementations that can only be
  330. * configured with a LPI table address once, GICv3 systems with many CPUs may
  331. * end up reserving a lot of different regions after a kexec for their LPI
  332. * tables (one per CPU), as we are forced to reuse the same memory after kexec
  333. * (and thus reserve it persistently with EFI beforehand)
  334. */
  335. #if defined(CONFIG_EFI) && defined(CONFIG_ARM_GIC_V3_ITS)
  336. # define INIT_MEMBLOCK_RESERVED_REGIONS (INIT_MEMBLOCK_REGIONS + NR_CPUS + 1)
  337. #endif
  338. /*
  339. * memory regions which marked with flag MEMBLOCK_NOMAP(for example, the memory
  340. * of the EFI_UNUSABLE_MEMORY type) may divide a continuous memory block into
  341. * multiple parts. As a result, the number of memory regions is large.
  342. */
  343. #ifdef CONFIG_EFI
  344. #define INIT_MEMBLOCK_MEMORY_REGIONS (INIT_MEMBLOCK_REGIONS * 8)
  345. #endif
  346. #include <asm-generic/memory_model.h>
  347. #endif /* __ASM_MEMORY_H */