1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Initialize MMU support.
4 *
5 * Copyright (C) 1998-2003 Hewlett-Packard Co
6 * David Mosberger-Tang <davidm@hpl.hp.com>
7 */
8 #include <linux/kernel.h>
9 #include <linux/init.h>
10
11 #include <linux/dma-map-ops.h>
12 #include <linux/dmar.h>
13 #include <linux/efi.h>
14 #include <linux/elf.h>
15 #include <linux/memblock.h>
16 #include <linux/mm.h>
17 #include <linux/sched/signal.h>
18 #include <linux/mmzone.h>
19 #include <linux/module.h>
20 #include <linux/personality.h>
21 #include <linux/reboot.h>
22 #include <linux/slab.h>
23 #include <linux/swap.h>
24 #include <linux/proc_fs.h>
25 #include <linux/bitops.h>
26 #include <linux/kexec.h>
27 #include <linux/swiotlb.h>
28
29 #include <asm/dma.h>
30 #include <asm/efi.h>
31 #include <asm/io.h>
32 #include <asm/numa.h>
33 #include <asm/patch.h>
34 #include <asm/pgalloc.h>
35 #include <asm/sal.h>
36 #include <asm/sections.h>
37 #include <asm/tlb.h>
38 #include <linux/uaccess.h>
39 #include <asm/unistd.h>
40 #include <asm/mca.h>
41
42 extern void ia64_tlb_init (void);
43
44 unsigned long MAX_DMA_ADDRESS = PAGE_OFFSET + 0x100000000UL;
45
46 struct page *zero_page_memmap_ptr; /* map entry for zero page */
47 EXPORT_SYMBOL(zero_page_memmap_ptr);
48
49 void
__ia64_sync_icache_dcache(pte_t pte)50 __ia64_sync_icache_dcache (pte_t pte)
51 {
52 unsigned long addr;
53 struct folio *folio;
54
55 folio = page_folio(pte_page(pte));
56 addr = (unsigned long)folio_address(folio);
57
58 if (test_bit(PG_arch_1, &folio->flags))
59 return; /* i-cache is already coherent with d-cache */
60
61 flush_icache_range(addr, addr + folio_size(folio));
62 set_bit(PG_arch_1, &folio->flags); /* mark page as clean */
63 }
64
65 /*
66 * Since DMA is i-cache coherent, any (complete) folios that were written via
67 * DMA can be marked as "clean" so that lazy_mmu_prot_update() doesn't have to
68 * flush them when they get mapped into an executable vm-area.
69 */
arch_dma_mark_clean(phys_addr_t paddr,size_t size)70 void arch_dma_mark_clean(phys_addr_t paddr, size_t size)
71 {
72 unsigned long pfn = PHYS_PFN(paddr);
73 struct folio *folio = page_folio(pfn_to_page(pfn));
74 ssize_t left = size;
75 size_t offset = offset_in_folio(folio, paddr);
76
77 if (offset) {
78 left -= folio_size(folio) - offset;
79 if (left <= 0)
80 return;
81 folio = folio_next(folio);
82 }
83
84 while (left >= (ssize_t)folio_size(folio)) {
85 left -= folio_size(folio);
86 set_bit(PG_arch_1, &pfn_to_page(pfn)->flags);
87 if (!left)
88 break;
89 folio = folio_next(folio);
90 }
91 }
92
93 inline void
ia64_set_rbs_bot(void)94 ia64_set_rbs_bot (void)
95 {
96 unsigned long stack_size = rlimit_max(RLIMIT_STACK) & -16;
97
98 if (stack_size > MAX_USER_STACK_SIZE)
99 stack_size = MAX_USER_STACK_SIZE;
100 current->thread.rbs_bot = PAGE_ALIGN(current->mm->start_stack - stack_size);
101 }
102
103 /*
104 * This performs some platform-dependent address space initialization.
105 * On IA-64, we want to setup the VM area for the register backing
106 * store (which grows upwards) and install the gateway page which is
107 * used for signal trampolines, etc.
108 */
109 void
ia64_init_addr_space(void)110 ia64_init_addr_space (void)
111 {
112 struct vm_area_struct *vma;
113
114 ia64_set_rbs_bot();
115
116 /*
117 * If we're out of memory and kmem_cache_alloc() returns NULL, we simply ignore
118 * the problem. When the process attempts to write to the register backing store
119 * for the first time, it will get a SEGFAULT in this case.
120 */
121 vma = vm_area_alloc(current->mm);
122 if (vma) {
123 vma_set_anonymous(vma);
124 vma->vm_start = current->thread.rbs_bot & PAGE_MASK;
125 vma->vm_end = vma->vm_start + PAGE_SIZE;
126 vm_flags_init(vma, VM_DATA_DEFAULT_FLAGS|VM_GROWSUP|VM_ACCOUNT);
127 vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
128 mmap_write_lock(current->mm);
129 if (insert_vm_struct(current->mm, vma)) {
130 mmap_write_unlock(current->mm);
131 vm_area_free(vma);
132 return;
133 }
134 mmap_write_unlock(current->mm);
135 }
136
137 /* map NaT-page at address zero to speed up speculative dereferencing of NULL: */
138 if (!(current->personality & MMAP_PAGE_ZERO)) {
139 vma = vm_area_alloc(current->mm);
140 if (vma) {
141 vma_set_anonymous(vma);
142 vma->vm_end = PAGE_SIZE;
143 vma->vm_page_prot = __pgprot(pgprot_val(PAGE_READONLY) | _PAGE_MA_NAT);
144 vm_flags_init(vma, VM_READ | VM_MAYREAD | VM_IO |
145 VM_DONTEXPAND | VM_DONTDUMP);
146 mmap_write_lock(current->mm);
147 if (insert_vm_struct(current->mm, vma)) {
148 mmap_write_unlock(current->mm);
149 vm_area_free(vma);
150 return;
151 }
152 mmap_write_unlock(current->mm);
153 }
154 }
155 }
156
157 void
free_initmem(void)158 free_initmem (void)
159 {
160 free_reserved_area(ia64_imva(__init_begin), ia64_imva(__init_end),
161 -1, "unused kernel");
162 }
163
164 void __init
free_initrd_mem(unsigned long start,unsigned long end)165 free_initrd_mem (unsigned long start, unsigned long end)
166 {
167 /*
168 * EFI uses 4KB pages while the kernel can use 4KB or bigger.
169 * Thus EFI and the kernel may have different page sizes. It is
170 * therefore possible to have the initrd share the same page as
171 * the end of the kernel (given current setup).
172 *
173 * To avoid freeing/using the wrong page (kernel sized) we:
174 * - align up the beginning of initrd
175 * - align down the end of initrd
176 *
177 * | |
178 * |=============| a000
179 * | |
180 * | |
181 * | | 9000
182 * |/////////////|
183 * |/////////////|
184 * |=============| 8000
185 * |///INITRD////|
186 * |/////////////|
187 * |/////////////| 7000
188 * | |
189 * |KKKKKKKKKKKKK|
190 * |=============| 6000
191 * |KKKKKKKKKKKKK|
192 * |KKKKKKKKKKKKK|
193 * K=kernel using 8KB pages
194 *
195 * In this example, we must free page 8000 ONLY. So we must align up
196 * initrd_start and keep initrd_end as is.
197 */
198 start = PAGE_ALIGN(start);
199 end = end & PAGE_MASK;
200
201 if (start < end)
202 printk(KERN_INFO "Freeing initrd memory: %ldkB freed\n", (end - start) >> 10);
203
204 for (; start < end; start += PAGE_SIZE) {
205 if (!virt_addr_valid(start))
206 continue;
207 free_reserved_page(virt_to_page(start));
208 }
209 }
210
211 /*
212 * This installs a clean page in the kernel's page table.
213 */
214 static struct page * __init
put_kernel_page(struct page * page,unsigned long address,pgprot_t pgprot)215 put_kernel_page (struct page *page, unsigned long address, pgprot_t pgprot)
216 {
217 pgd_t *pgd;
218 p4d_t *p4d;
219 pud_t *pud;
220 pmd_t *pmd;
221 pte_t *pte;
222
223 pgd = pgd_offset_k(address); /* note: this is NOT pgd_offset()! */
224
225 {
226 p4d = p4d_alloc(&init_mm, pgd, address);
227 if (!p4d)
228 goto out;
229 pud = pud_alloc(&init_mm, p4d, address);
230 if (!pud)
231 goto out;
232 pmd = pmd_alloc(&init_mm, pud, address);
233 if (!pmd)
234 goto out;
235 pte = pte_alloc_kernel(pmd, address);
236 if (!pte)
237 goto out;
238 if (!pte_none(*pte))
239 goto out;
240 set_pte(pte, mk_pte(page, pgprot));
241 }
242 out:
243 /* no need for flush_tlb */
244 return page;
245 }
246
247 static void __init
setup_gate(void)248 setup_gate (void)
249 {
250 struct page *page;
251
252 /*
253 * Map the gate page twice: once read-only to export the ELF
254 * headers etc. and once execute-only page to enable
255 * privilege-promotion via "epc":
256 */
257 page = virt_to_page(ia64_imva(__start_gate_section));
258 put_kernel_page(page, GATE_ADDR, PAGE_READONLY);
259 #ifdef HAVE_BUGGY_SEGREL
260 page = virt_to_page(ia64_imva(__start_gate_section + PAGE_SIZE));
261 put_kernel_page(page, GATE_ADDR + PAGE_SIZE, PAGE_GATE);
262 #else
263 put_kernel_page(page, GATE_ADDR + PERCPU_PAGE_SIZE, PAGE_GATE);
264 /* Fill in the holes (if any) with read-only zero pages: */
265 {
266 unsigned long addr;
267
268 for (addr = GATE_ADDR + PAGE_SIZE;
269 addr < GATE_ADDR + PERCPU_PAGE_SIZE;
270 addr += PAGE_SIZE)
271 {
272 put_kernel_page(ZERO_PAGE(0), addr,
273 PAGE_READONLY);
274 put_kernel_page(ZERO_PAGE(0), addr + PERCPU_PAGE_SIZE,
275 PAGE_READONLY);
276 }
277 }
278 #endif
279 ia64_patch_gate();
280 }
281
282 static struct vm_area_struct gate_vma;
283
gate_vma_init(void)284 static int __init gate_vma_init(void)
285 {
286 vma_init(&gate_vma, NULL);
287 gate_vma.vm_start = FIXADDR_USER_START;
288 gate_vma.vm_end = FIXADDR_USER_END;
289 vm_flags_init(&gate_vma, VM_READ | VM_MAYREAD | VM_EXEC | VM_MAYEXEC);
290 gate_vma.vm_page_prot = __pgprot(__ACCESS_BITS | _PAGE_PL_3 | _PAGE_AR_RX);
291
292 return 0;
293 }
294 __initcall(gate_vma_init);
295
get_gate_vma(struct mm_struct * mm)296 struct vm_area_struct *get_gate_vma(struct mm_struct *mm)
297 {
298 return &gate_vma;
299 }
300
in_gate_area_no_mm(unsigned long addr)301 int in_gate_area_no_mm(unsigned long addr)
302 {
303 if ((addr >= FIXADDR_USER_START) && (addr < FIXADDR_USER_END))
304 return 1;
305 return 0;
306 }
307
in_gate_area(struct mm_struct * mm,unsigned long addr)308 int in_gate_area(struct mm_struct *mm, unsigned long addr)
309 {
310 return in_gate_area_no_mm(addr);
311 }
312
ia64_mmu_init(void * my_cpu_data)313 void ia64_mmu_init(void *my_cpu_data)
314 {
315 unsigned long pta, impl_va_bits;
316 extern void tlb_init(void);
317
318 #ifdef CONFIG_DISABLE_VHPT
319 # define VHPT_ENABLE_BIT 0
320 #else
321 # define VHPT_ENABLE_BIT 1
322 #endif
323
324 /*
325 * Check if the virtually mapped linear page table (VMLPT) overlaps with a mapped
326 * address space. The IA-64 architecture guarantees that at least 50 bits of
327 * virtual address space are implemented but if we pick a large enough page size
328 * (e.g., 64KB), the mapped address space is big enough that it will overlap with
329 * VMLPT. I assume that once we run on machines big enough to warrant 64KB pages,
330 * IMPL_VA_MSB will be significantly bigger, so this is unlikely to become a
331 * problem in practice. Alternatively, we could truncate the top of the mapped
332 * address space to not permit mappings that would overlap with the VMLPT.
333 * --davidm 00/12/06
334 */
335 # define pte_bits 3
336 # define mapped_space_bits (3*(PAGE_SHIFT - pte_bits) + PAGE_SHIFT)
337 /*
338 * The virtual page table has to cover the entire implemented address space within
339 * a region even though not all of this space may be mappable. The reason for
340 * this is that the Access bit and Dirty bit fault handlers perform
341 * non-speculative accesses to the virtual page table, so the address range of the
342 * virtual page table itself needs to be covered by virtual page table.
343 */
344 # define vmlpt_bits (impl_va_bits - PAGE_SHIFT + pte_bits)
345 # define POW2(n) (1ULL << (n))
346
347 impl_va_bits = ffz(~(local_cpu_data->unimpl_va_mask | (7UL << 61)));
348
349 if (impl_va_bits < 51 || impl_va_bits > 61)
350 panic("CPU has bogus IMPL_VA_MSB value of %lu!\n", impl_va_bits - 1);
351 /*
352 * mapped_space_bits - PAGE_SHIFT is the total number of ptes we need,
353 * which must fit into "vmlpt_bits - pte_bits" slots. Second half of
354 * the test makes sure that our mapped space doesn't overlap the
355 * unimplemented hole in the middle of the region.
356 */
357 if ((mapped_space_bits - PAGE_SHIFT > vmlpt_bits - pte_bits) ||
358 (mapped_space_bits > impl_va_bits - 1))
359 panic("Cannot build a big enough virtual-linear page table"
360 " to cover mapped address space.\n"
361 " Try using a smaller page size.\n");
362
363
364 /* place the VMLPT at the end of each page-table mapped region: */
365 pta = POW2(61) - POW2(vmlpt_bits);
366
367 /*
368 * Set the (virtually mapped linear) page table address. Bit
369 * 8 selects between the short and long format, bits 2-7 the
370 * size of the table, and bit 0 whether the VHPT walker is
371 * enabled.
372 */
373 ia64_set_pta(pta | (0 << 8) | (vmlpt_bits << 2) | VHPT_ENABLE_BIT);
374
375 ia64_tlb_init();
376
377 #ifdef CONFIG_HUGETLB_PAGE
378 ia64_set_rr(HPAGE_REGION_BASE, HPAGE_SHIFT << 2);
379 ia64_srlz_d();
380 #endif
381 }
382
register_active_ranges(u64 start,u64 len,int nid)383 int __init register_active_ranges(u64 start, u64 len, int nid)
384 {
385 u64 end = start + len;
386
387 #ifdef CONFIG_KEXEC
388 if (start > crashk_res.start && start < crashk_res.end)
389 start = crashk_res.end;
390 if (end > crashk_res.start && end < crashk_res.end)
391 end = crashk_res.start;
392 #endif
393
394 if (start < end)
395 memblock_add_node(__pa(start), end - start, nid, MEMBLOCK_NONE);
396 return 0;
397 }
398
399 int
find_max_min_low_pfn(u64 start,u64 end,void * arg)400 find_max_min_low_pfn (u64 start, u64 end, void *arg)
401 {
402 unsigned long pfn_start, pfn_end;
403 #ifdef CONFIG_FLATMEM
404 pfn_start = (PAGE_ALIGN(__pa(start))) >> PAGE_SHIFT;
405 pfn_end = (PAGE_ALIGN(__pa(end - 1))) >> PAGE_SHIFT;
406 #else
407 pfn_start = GRANULEROUNDDOWN(__pa(start)) >> PAGE_SHIFT;
408 pfn_end = GRANULEROUNDUP(__pa(end - 1)) >> PAGE_SHIFT;
409 #endif
410 min_low_pfn = min(min_low_pfn, pfn_start);
411 max_low_pfn = max(max_low_pfn, pfn_end);
412 return 0;
413 }
414
415 /*
416 * Boot command-line option "nolwsys" can be used to disable the use of any light-weight
417 * system call handler. When this option is in effect, all fsyscalls will end up bubbling
418 * down into the kernel and calling the normal (heavy-weight) syscall handler. This is
419 * useful for performance testing, but conceivably could also come in handy for debugging
420 * purposes.
421 */
422
423 static int nolwsys __initdata;
424
425 static int __init
nolwsys_setup(char * s)426 nolwsys_setup (char *s)
427 {
428 nolwsys = 1;
429 return 1;
430 }
431
432 __setup("nolwsys", nolwsys_setup);
433
434 void __init
mem_init(void)435 mem_init (void)
436 {
437 int i;
438
439 BUG_ON(PTRS_PER_PGD * sizeof(pgd_t) != PAGE_SIZE);
440 BUG_ON(PTRS_PER_PMD * sizeof(pmd_t) != PAGE_SIZE);
441 BUG_ON(PTRS_PER_PTE * sizeof(pte_t) != PAGE_SIZE);
442
443 /*
444 * This needs to be called _after_ the command line has been parsed but
445 * _before_ any drivers that may need the PCI DMA interface are
446 * initialized or bootmem has been freed.
447 */
448 do {
449 #ifdef CONFIG_INTEL_IOMMU
450 detect_intel_iommu();
451 if (iommu_detected)
452 break;
453 #endif
454 swiotlb_init(true, SWIOTLB_VERBOSE);
455 } while (0);
456
457 #ifdef CONFIG_FLATMEM
458 BUG_ON(!mem_map);
459 #endif
460
461 set_max_mapnr(max_low_pfn);
462 high_memory = __va(max_low_pfn * PAGE_SIZE);
463 memblock_free_all();
464
465 /*
466 * For fsyscall entrypoints with no light-weight handler, use the ordinary
467 * (heavy-weight) handler, but mark it by setting bit 0, so the fsyscall entry
468 * code can tell them apart.
469 */
470 for (i = 0; i < NR_syscalls; ++i) {
471 extern unsigned long fsyscall_table[NR_syscalls];
472 extern unsigned long sys_call_table[NR_syscalls];
473
474 if (!fsyscall_table[i] || nolwsys)
475 fsyscall_table[i] = sys_call_table[i] | 1;
476 }
477 setup_gate();
478 }
479
480 #ifdef CONFIG_MEMORY_HOTPLUG
arch_add_memory(int nid,u64 start,u64 size,struct mhp_params * params)481 int arch_add_memory(int nid, u64 start, u64 size,
482 struct mhp_params *params)
483 {
484 unsigned long start_pfn = start >> PAGE_SHIFT;
485 unsigned long nr_pages = size >> PAGE_SHIFT;
486 int ret;
487
488 if (WARN_ON_ONCE(params->pgprot.pgprot != PAGE_KERNEL.pgprot))
489 return -EINVAL;
490
491 ret = __add_pages(nid, start_pfn, nr_pages, params);
492 if (ret)
493 printk("%s: Problem encountered in __add_pages() as ret=%d\n",
494 __func__, ret);
495
496 return ret;
497 }
498
arch_remove_memory(u64 start,u64 size,struct vmem_altmap * altmap)499 void arch_remove_memory(u64 start, u64 size, struct vmem_altmap *altmap)
500 {
501 unsigned long start_pfn = start >> PAGE_SHIFT;
502 unsigned long nr_pages = size >> PAGE_SHIFT;
503
504 __remove_pages(start_pfn, nr_pages, altmap);
505 }
506 #endif
507
508 static const pgprot_t protection_map[16] = {
509 [VM_NONE] = PAGE_NONE,
510 [VM_READ] = PAGE_READONLY,
511 [VM_WRITE] = PAGE_READONLY,
512 [VM_WRITE | VM_READ] = PAGE_READONLY,
513 [VM_EXEC] = __pgprot(__ACCESS_BITS | _PAGE_PL_3 |
514 _PAGE_AR_X_RX),
515 [VM_EXEC | VM_READ] = __pgprot(__ACCESS_BITS | _PAGE_PL_3 |
516 _PAGE_AR_RX),
517 [VM_EXEC | VM_WRITE] = PAGE_COPY_EXEC,
518 [VM_EXEC | VM_WRITE | VM_READ] = PAGE_COPY_EXEC,
519 [VM_SHARED] = PAGE_NONE,
520 [VM_SHARED | VM_READ] = PAGE_READONLY,
521 [VM_SHARED | VM_WRITE] = PAGE_SHARED,
522 [VM_SHARED | VM_WRITE | VM_READ] = PAGE_SHARED,
523 [VM_SHARED | VM_EXEC] = __pgprot(__ACCESS_BITS | _PAGE_PL_3 |
524 _PAGE_AR_X_RX),
525 [VM_SHARED | VM_EXEC | VM_READ] = __pgprot(__ACCESS_BITS | _PAGE_PL_3 |
526 _PAGE_AR_RX),
527 [VM_SHARED | VM_EXEC | VM_WRITE] = __pgprot(__ACCESS_BITS | _PAGE_PL_3 |
528 _PAGE_AR_RWX),
529 [VM_SHARED | VM_EXEC | VM_WRITE | VM_READ] = __pgprot(__ACCESS_BITS | _PAGE_PL_3 |
530 _PAGE_AR_RWX)
531 };
532 DECLARE_VM_GET_PAGE_PROT
533