1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * Initialize MMU support.
41da177e4SLinus Torvalds *
51da177e4SLinus Torvalds * Copyright (C) 1998-2003 Hewlett-Packard Co
61da177e4SLinus Torvalds * David Mosberger-Tang <davidm@hpl.hp.com>
71da177e4SLinus Torvalds */
81da177e4SLinus Torvalds #include <linux/kernel.h>
91da177e4SLinus Torvalds #include <linux/init.h>
101da177e4SLinus Torvalds
119f4df96bSChristoph Hellwig #include <linux/dma-map-ops.h>
12974f83ecSChristoph Hellwig #include <linux/dmar.h>
131da177e4SLinus Torvalds #include <linux/efi.h>
141da177e4SLinus Torvalds #include <linux/elf.h>
1598e4ae8aSTejun Heo #include <linux/memblock.h>
161da177e4SLinus Torvalds #include <linux/mm.h>
173f07c014SIngo Molnar #include <linux/sched/signal.h>
181da177e4SLinus Torvalds #include <linux/mmzone.h>
191da177e4SLinus Torvalds #include <linux/module.h>
201da177e4SLinus Torvalds #include <linux/personality.h>
211da177e4SLinus Torvalds #include <linux/reboot.h>
221da177e4SLinus Torvalds #include <linux/slab.h>
231da177e4SLinus Torvalds #include <linux/swap.h>
241da177e4SLinus Torvalds #include <linux/proc_fs.h>
251da177e4SLinus Torvalds #include <linux/bitops.h>
26139b8304SBob Picco #include <linux/kexec.h>
27974f83ecSChristoph Hellwig #include <linux/swiotlb.h>
281da177e4SLinus Torvalds
291da177e4SLinus Torvalds #include <asm/dma.h>
308ff059b8SArd Biesheuvel #include <asm/efi.h>
311da177e4SLinus Torvalds #include <asm/io.h>
321da177e4SLinus Torvalds #include <asm/numa.h>
331da177e4SLinus Torvalds #include <asm/patch.h>
341da177e4SLinus Torvalds #include <asm/pgalloc.h>
351da177e4SLinus Torvalds #include <asm/sal.h>
361da177e4SLinus Torvalds #include <asm/sections.h>
371da177e4SLinus Torvalds #include <asm/tlb.h>
387c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
391da177e4SLinus Torvalds #include <asm/unistd.h>
401da177e4SLinus Torvalds #include <asm/mca.h>
411da177e4SLinus Torvalds
421da177e4SLinus Torvalds extern void ia64_tlb_init (void);
431da177e4SLinus Torvalds
441da177e4SLinus Torvalds unsigned long MAX_DMA_ADDRESS = PAGE_OFFSET + 0x100000000UL;
451da177e4SLinus Torvalds
461da177e4SLinus Torvalds struct page *zero_page_memmap_ptr; /* map entry for zero page */
471da177e4SLinus Torvalds EXPORT_SYMBOL(zero_page_memmap_ptr);
481da177e4SLinus Torvalds
491da177e4SLinus Torvalds void
__ia64_sync_icache_dcache(pte_t pte)50954ffcb3SKAMEZAWA Hiroyuki __ia64_sync_icache_dcache (pte_t pte)
511da177e4SLinus Torvalds {
521da177e4SLinus Torvalds unsigned long addr;
53*87639783SMatthew Wilcox (Oracle) struct folio *folio;
541da177e4SLinus Torvalds
55*87639783SMatthew Wilcox (Oracle) folio = page_folio(pte_page(pte));
56*87639783SMatthew Wilcox (Oracle) addr = (unsigned long)folio_address(folio);
571da177e4SLinus Torvalds
58*87639783SMatthew Wilcox (Oracle) if (test_bit(PG_arch_1, &folio->flags))
591da177e4SLinus Torvalds return; /* i-cache is already coherent with d-cache */
601da177e4SLinus Torvalds
61*87639783SMatthew Wilcox (Oracle) flush_icache_range(addr, addr + folio_size(folio));
62*87639783SMatthew Wilcox (Oracle) set_bit(PG_arch_1, &folio->flags); /* mark page as clean */
631da177e4SLinus Torvalds }
641da177e4SLinus Torvalds
65cde14bbfSJan Beulich /*
66*87639783SMatthew Wilcox (Oracle) * Since DMA is i-cache coherent, any (complete) folios that were written via
67cde14bbfSJan Beulich * DMA can be marked as "clean" so that lazy_mmu_prot_update() doesn't have to
68cde14bbfSJan Beulich * flush them when they get mapped into an executable vm-area.
69cde14bbfSJan Beulich */
arch_dma_mark_clean(phys_addr_t paddr,size_t size)70abdaf11aSChristoph Hellwig void arch_dma_mark_clean(phys_addr_t paddr, size_t size)
71cde14bbfSJan Beulich {
7268c60834SChristoph Hellwig unsigned long pfn = PHYS_PFN(paddr);
73*87639783SMatthew Wilcox (Oracle) struct folio *folio = page_folio(pfn_to_page(pfn));
74*87639783SMatthew Wilcox (Oracle) ssize_t left = size;
75*87639783SMatthew Wilcox (Oracle) size_t offset = offset_in_folio(folio, paddr);
76cde14bbfSJan Beulich
77*87639783SMatthew Wilcox (Oracle) if (offset) {
78*87639783SMatthew Wilcox (Oracle) left -= folio_size(folio) - offset;
79*87639783SMatthew Wilcox (Oracle) if (left <= 0)
80*87639783SMatthew Wilcox (Oracle) return;
81*87639783SMatthew Wilcox (Oracle) folio = folio_next(folio);
82*87639783SMatthew Wilcox (Oracle) }
83*87639783SMatthew Wilcox (Oracle)
84*87639783SMatthew Wilcox (Oracle) while (left >= (ssize_t)folio_size(folio)) {
85*87639783SMatthew Wilcox (Oracle) left -= folio_size(folio);
8668c60834SChristoph Hellwig set_bit(PG_arch_1, &pfn_to_page(pfn)->flags);
87*87639783SMatthew Wilcox (Oracle) if (!left)
88*87639783SMatthew Wilcox (Oracle) break;
89*87639783SMatthew Wilcox (Oracle) folio = folio_next(folio);
90*87639783SMatthew Wilcox (Oracle) }
91cde14bbfSJan Beulich }
92cde14bbfSJan Beulich
931da177e4SLinus Torvalds inline void
ia64_set_rbs_bot(void)941da177e4SLinus Torvalds ia64_set_rbs_bot (void)
951da177e4SLinus Torvalds {
9602b763b8SJiri Slaby unsigned long stack_size = rlimit_max(RLIMIT_STACK) & -16;
971da177e4SLinus Torvalds
981da177e4SLinus Torvalds if (stack_size > MAX_USER_STACK_SIZE)
991da177e4SLinus Torvalds stack_size = MAX_USER_STACK_SIZE;
10083d2cd3dSKAMEZAWA Hiroyuki current->thread.rbs_bot = PAGE_ALIGN(current->mm->start_stack - stack_size);
1011da177e4SLinus Torvalds }
1021da177e4SLinus Torvalds
1031da177e4SLinus Torvalds /*
1041da177e4SLinus Torvalds * This performs some platform-dependent address space initialization.
1051da177e4SLinus Torvalds * On IA-64, we want to setup the VM area for the register backing
1061da177e4SLinus Torvalds * store (which grows upwards) and install the gateway page which is
1071da177e4SLinus Torvalds * used for signal trampolines, etc.
1081da177e4SLinus Torvalds */
1091da177e4SLinus Torvalds void
ia64_init_addr_space(void)1101da177e4SLinus Torvalds ia64_init_addr_space (void)
1111da177e4SLinus Torvalds {
1121da177e4SLinus Torvalds struct vm_area_struct *vma;
1131da177e4SLinus Torvalds
1141da177e4SLinus Torvalds ia64_set_rbs_bot();
1151da177e4SLinus Torvalds
1161da177e4SLinus Torvalds /*
1171da177e4SLinus Torvalds * If we're out of memory and kmem_cache_alloc() returns NULL, we simply ignore
1181da177e4SLinus Torvalds * the problem. When the process attempts to write to the register backing store
1191da177e4SLinus Torvalds * for the first time, it will get a SEGFAULT in this case.
1201da177e4SLinus Torvalds */
121490fc053SLinus Torvalds vma = vm_area_alloc(current->mm);
1221da177e4SLinus Torvalds if (vma) {
123ebad825cSLinus Torvalds vma_set_anonymous(vma);
1241da177e4SLinus Torvalds vma->vm_start = current->thread.rbs_bot & PAGE_MASK;
1251da177e4SLinus Torvalds vma->vm_end = vma->vm_start + PAGE_SIZE;
1261c71222eSSuren Baghdasaryan vm_flags_init(vma, VM_DATA_DEFAULT_FLAGS|VM_GROWSUP|VM_ACCOUNT);
1273ed75eb8SColy Li vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
128d8ed45c5SMichel Lespinasse mmap_write_lock(current->mm);
1291da177e4SLinus Torvalds if (insert_vm_struct(current->mm, vma)) {
130d8ed45c5SMichel Lespinasse mmap_write_unlock(current->mm);
1313928d4f5SLinus Torvalds vm_area_free(vma);
1321da177e4SLinus Torvalds return;
1331da177e4SLinus Torvalds }
134d8ed45c5SMichel Lespinasse mmap_write_unlock(current->mm);
1351da177e4SLinus Torvalds }
1361da177e4SLinus Torvalds
1371da177e4SLinus Torvalds /* map NaT-page at address zero to speed up speculative dereferencing of NULL: */
1381da177e4SLinus Torvalds if (!(current->personality & MMAP_PAGE_ZERO)) {
139490fc053SLinus Torvalds vma = vm_area_alloc(current->mm);
1401da177e4SLinus Torvalds if (vma) {
141ebad825cSLinus Torvalds vma_set_anonymous(vma);
1421da177e4SLinus Torvalds vma->vm_end = PAGE_SIZE;
1431da177e4SLinus Torvalds vma->vm_page_prot = __pgprot(pgprot_val(PAGE_READONLY) | _PAGE_MA_NAT);
1441c71222eSSuren Baghdasaryan vm_flags_init(vma, VM_READ | VM_MAYREAD | VM_IO |
1451c71222eSSuren Baghdasaryan VM_DONTEXPAND | VM_DONTDUMP);
146d8ed45c5SMichel Lespinasse mmap_write_lock(current->mm);
1471da177e4SLinus Torvalds if (insert_vm_struct(current->mm, vma)) {
148d8ed45c5SMichel Lespinasse mmap_write_unlock(current->mm);
1493928d4f5SLinus Torvalds vm_area_free(vma);
1501da177e4SLinus Torvalds return;
1511da177e4SLinus Torvalds }
152d8ed45c5SMichel Lespinasse mmap_write_unlock(current->mm);
1531da177e4SLinus Torvalds }
1541da177e4SLinus Torvalds }
1551da177e4SLinus Torvalds }
1561da177e4SLinus Torvalds
1571da177e4SLinus Torvalds void
free_initmem(void)1581da177e4SLinus Torvalds free_initmem (void)
1591da177e4SLinus Torvalds {
16011199692SJiang Liu free_reserved_area(ia64_imva(__init_begin), ia64_imva(__init_end),
161dbe67df4SJiang Liu -1, "unused kernel");
1621da177e4SLinus Torvalds }
1631da177e4SLinus Torvalds
164dae28066SChen, Kenneth W void __init
free_initrd_mem(unsigned long start,unsigned long end)1651da177e4SLinus Torvalds free_initrd_mem (unsigned long start, unsigned long end)
1661da177e4SLinus Torvalds {
1671da177e4SLinus Torvalds /*
1681da177e4SLinus Torvalds * EFI uses 4KB pages while the kernel can use 4KB or bigger.
1691da177e4SLinus Torvalds * Thus EFI and the kernel may have different page sizes. It is
1701da177e4SLinus Torvalds * therefore possible to have the initrd share the same page as
1711da177e4SLinus Torvalds * the end of the kernel (given current setup).
1721da177e4SLinus Torvalds *
1731da177e4SLinus Torvalds * To avoid freeing/using the wrong page (kernel sized) we:
1741da177e4SLinus Torvalds * - align up the beginning of initrd
1751da177e4SLinus Torvalds * - align down the end of initrd
1761da177e4SLinus Torvalds *
1771da177e4SLinus Torvalds * | |
1781da177e4SLinus Torvalds * |=============| a000
1791da177e4SLinus Torvalds * | |
1801da177e4SLinus Torvalds * | |
1811da177e4SLinus Torvalds * | | 9000
1821da177e4SLinus Torvalds * |/////////////|
1831da177e4SLinus Torvalds * |/////////////|
1841da177e4SLinus Torvalds * |=============| 8000
1851da177e4SLinus Torvalds * |///INITRD////|
1861da177e4SLinus Torvalds * |/////////////|
1871da177e4SLinus Torvalds * |/////////////| 7000
1881da177e4SLinus Torvalds * | |
1891da177e4SLinus Torvalds * |KKKKKKKKKKKKK|
1901da177e4SLinus Torvalds * |=============| 6000
1911da177e4SLinus Torvalds * |KKKKKKKKKKKKK|
1921da177e4SLinus Torvalds * |KKKKKKKKKKKKK|
1931da177e4SLinus Torvalds * K=kernel using 8KB pages
1941da177e4SLinus Torvalds *
1951da177e4SLinus Torvalds * In this example, we must free page 8000 ONLY. So we must align up
1961da177e4SLinus Torvalds * initrd_start and keep initrd_end as is.
1971da177e4SLinus Torvalds */
1981da177e4SLinus Torvalds start = PAGE_ALIGN(start);
1991da177e4SLinus Torvalds end = end & PAGE_MASK;
2001da177e4SLinus Torvalds
2011da177e4SLinus Torvalds if (start < end)
2021da177e4SLinus Torvalds printk(KERN_INFO "Freeing initrd memory: %ldkB freed\n", (end - start) >> 10);
2031da177e4SLinus Torvalds
2041da177e4SLinus Torvalds for (; start < end; start += PAGE_SIZE) {
2051da177e4SLinus Torvalds if (!virt_addr_valid(start))
2061da177e4SLinus Torvalds continue;
20766f62594SJiang Liu free_reserved_page(virt_to_page(start));
2081da177e4SLinus Torvalds }
2091da177e4SLinus Torvalds }
2101da177e4SLinus Torvalds
2111da177e4SLinus Torvalds /*
2121da177e4SLinus Torvalds * This installs a clean page in the kernel's page table.
2131da177e4SLinus Torvalds */
214dae28066SChen, Kenneth W static struct page * __init
put_kernel_page(struct page * page,unsigned long address,pgprot_t pgprot)2151da177e4SLinus Torvalds put_kernel_page (struct page *page, unsigned long address, pgprot_t pgprot)
2161da177e4SLinus Torvalds {
2171da177e4SLinus Torvalds pgd_t *pgd;
218c03ab9e3SMike Rapoport p4d_t *p4d;
2191da177e4SLinus Torvalds pud_t *pud;
2201da177e4SLinus Torvalds pmd_t *pmd;
2211da177e4SLinus Torvalds pte_t *pte;
2221da177e4SLinus Torvalds
2231da177e4SLinus Torvalds pgd = pgd_offset_k(address); /* note: this is NOT pgd_offset()! */
2241da177e4SLinus Torvalds
2251da177e4SLinus Torvalds {
226c03ab9e3SMike Rapoport p4d = p4d_alloc(&init_mm, pgd, address);
227c03ab9e3SMike Rapoport if (!p4d)
228c03ab9e3SMike Rapoport goto out;
229c03ab9e3SMike Rapoport pud = pud_alloc(&init_mm, p4d, address);
2301da177e4SLinus Torvalds if (!pud)
2311da177e4SLinus Torvalds goto out;
2321da177e4SLinus Torvalds pmd = pmd_alloc(&init_mm, pud, address);
2331da177e4SLinus Torvalds if (!pmd)
2341da177e4SLinus Torvalds goto out;
235872fec16SHugh Dickins pte = pte_alloc_kernel(pmd, address);
2361da177e4SLinus Torvalds if (!pte)
2371da177e4SLinus Torvalds goto out;
238872fec16SHugh Dickins if (!pte_none(*pte))
2391da177e4SLinus Torvalds goto out;
2401da177e4SLinus Torvalds set_pte(pte, mk_pte(page, pgprot));
2411da177e4SLinus Torvalds }
242872fec16SHugh Dickins out:
2431da177e4SLinus Torvalds /* no need for flush_tlb */
2441da177e4SLinus Torvalds return page;
2451da177e4SLinus Torvalds }
2461da177e4SLinus Torvalds
247914a4ea4SChen, Kenneth W static void __init
setup_gate(void)2481da177e4SLinus Torvalds setup_gate (void)
2491da177e4SLinus Torvalds {
2501da177e4SLinus Torvalds struct page *page;
2511da177e4SLinus Torvalds
2521da177e4SLinus Torvalds /*
253ad597bd5SDavid Mosberger-Tang * Map the gate page twice: once read-only to export the ELF
254ad597bd5SDavid Mosberger-Tang * headers etc. and once execute-only page to enable
255ad597bd5SDavid Mosberger-Tang * privilege-promotion via "epc":
2561da177e4SLinus Torvalds */
257e55645ecSLuis R. Rodriguez page = virt_to_page(ia64_imva(__start_gate_section));
2581da177e4SLinus Torvalds put_kernel_page(page, GATE_ADDR, PAGE_READONLY);
2591da177e4SLinus Torvalds #ifdef HAVE_BUGGY_SEGREL
260e55645ecSLuis R. Rodriguez page = virt_to_page(ia64_imva(__start_gate_section + PAGE_SIZE));
2611da177e4SLinus Torvalds put_kernel_page(page, GATE_ADDR + PAGE_SIZE, PAGE_GATE);
2621da177e4SLinus Torvalds #else
2631da177e4SLinus Torvalds put_kernel_page(page, GATE_ADDR + PERCPU_PAGE_SIZE, PAGE_GATE);
264ad597bd5SDavid Mosberger-Tang /* Fill in the holes (if any) with read-only zero pages: */
265ad597bd5SDavid Mosberger-Tang {
266ad597bd5SDavid Mosberger-Tang unsigned long addr;
267ad597bd5SDavid Mosberger-Tang
268ad597bd5SDavid Mosberger-Tang for (addr = GATE_ADDR + PAGE_SIZE;
269ad597bd5SDavid Mosberger-Tang addr < GATE_ADDR + PERCPU_PAGE_SIZE;
270ad597bd5SDavid Mosberger-Tang addr += PAGE_SIZE)
271ad597bd5SDavid Mosberger-Tang {
272ad597bd5SDavid Mosberger-Tang put_kernel_page(ZERO_PAGE(0), addr,
273ad597bd5SDavid Mosberger-Tang PAGE_READONLY);
274ad597bd5SDavid Mosberger-Tang put_kernel_page(ZERO_PAGE(0), addr + PERCPU_PAGE_SIZE,
275ad597bd5SDavid Mosberger-Tang PAGE_READONLY);
276ad597bd5SDavid Mosberger-Tang }
277ad597bd5SDavid Mosberger-Tang }
2781da177e4SLinus Torvalds #endif
2791da177e4SLinus Torvalds ia64_patch_gate();
2801da177e4SLinus Torvalds }
2811da177e4SLinus Torvalds
282a6c19dfeSAndy Lutomirski static struct vm_area_struct gate_vma;
283a6c19dfeSAndy Lutomirski
gate_vma_init(void)284a6c19dfeSAndy Lutomirski static int __init gate_vma_init(void)
285a6c19dfeSAndy Lutomirski {
2862c4541e2SKirill A. Shutemov vma_init(&gate_vma, NULL);
287a6c19dfeSAndy Lutomirski gate_vma.vm_start = FIXADDR_USER_START;
288a6c19dfeSAndy Lutomirski gate_vma.vm_end = FIXADDR_USER_END;
2891c71222eSSuren Baghdasaryan vm_flags_init(&gate_vma, VM_READ | VM_MAYREAD | VM_EXEC | VM_MAYEXEC);
290c619b82cSAnshuman Khandual gate_vma.vm_page_prot = __pgprot(__ACCESS_BITS | _PAGE_PL_3 | _PAGE_AR_RX);
291a6c19dfeSAndy Lutomirski
292a6c19dfeSAndy Lutomirski return 0;
293a6c19dfeSAndy Lutomirski }
294a6c19dfeSAndy Lutomirski __initcall(gate_vma_init);
295a6c19dfeSAndy Lutomirski
get_gate_vma(struct mm_struct * mm)296a6c19dfeSAndy Lutomirski struct vm_area_struct *get_gate_vma(struct mm_struct *mm)
297a6c19dfeSAndy Lutomirski {
298a6c19dfeSAndy Lutomirski return &gate_vma;
299a6c19dfeSAndy Lutomirski }
300a6c19dfeSAndy Lutomirski
in_gate_area_no_mm(unsigned long addr)301a6c19dfeSAndy Lutomirski int in_gate_area_no_mm(unsigned long addr)
302a6c19dfeSAndy Lutomirski {
303a6c19dfeSAndy Lutomirski if ((addr >= FIXADDR_USER_START) && (addr < FIXADDR_USER_END))
304a6c19dfeSAndy Lutomirski return 1;
305a6c19dfeSAndy Lutomirski return 0;
306a6c19dfeSAndy Lutomirski }
307a6c19dfeSAndy Lutomirski
in_gate_area(struct mm_struct * mm,unsigned long addr)308a6c19dfeSAndy Lutomirski int in_gate_area(struct mm_struct *mm, unsigned long addr)
309a6c19dfeSAndy Lutomirski {
310a6c19dfeSAndy Lutomirski return in_gate_area_no_mm(addr);
311a6c19dfeSAndy Lutomirski }
312a6c19dfeSAndy Lutomirski
ia64_mmu_init(void * my_cpu_data)3135b5e76e9SGreg Kroah-Hartman void ia64_mmu_init(void *my_cpu_data)
3141da177e4SLinus Torvalds {
31500b65985SChen, Kenneth W unsigned long pta, impl_va_bits;
3165b5e76e9SGreg Kroah-Hartman extern void tlb_init(void);
3171da177e4SLinus Torvalds
3181da177e4SLinus Torvalds #ifdef CONFIG_DISABLE_VHPT
3191da177e4SLinus Torvalds # define VHPT_ENABLE_BIT 0
3201da177e4SLinus Torvalds #else
3211da177e4SLinus Torvalds # define VHPT_ENABLE_BIT 1
3221da177e4SLinus Torvalds #endif
3231da177e4SLinus Torvalds
3241da177e4SLinus Torvalds /*
3251da177e4SLinus Torvalds * Check if the virtually mapped linear page table (VMLPT) overlaps with a mapped
3261da177e4SLinus Torvalds * address space. The IA-64 architecture guarantees that at least 50 bits of
3271da177e4SLinus Torvalds * virtual address space are implemented but if we pick a large enough page size
3281da177e4SLinus Torvalds * (e.g., 64KB), the mapped address space is big enough that it will overlap with
3291da177e4SLinus Torvalds * VMLPT. I assume that once we run on machines big enough to warrant 64KB pages,
3301da177e4SLinus Torvalds * IMPL_VA_MSB will be significantly bigger, so this is unlikely to become a
3311da177e4SLinus Torvalds * problem in practice. Alternatively, we could truncate the top of the mapped
3321da177e4SLinus Torvalds * address space to not permit mappings that would overlap with the VMLPT.
3331da177e4SLinus Torvalds * --davidm 00/12/06
3341da177e4SLinus Torvalds */
3351da177e4SLinus Torvalds # define pte_bits 3
3361da177e4SLinus Torvalds # define mapped_space_bits (3*(PAGE_SHIFT - pte_bits) + PAGE_SHIFT)
3371da177e4SLinus Torvalds /*
3381da177e4SLinus Torvalds * The virtual page table has to cover the entire implemented address space within
3391da177e4SLinus Torvalds * a region even though not all of this space may be mappable. The reason for
3401da177e4SLinus Torvalds * this is that the Access bit and Dirty bit fault handlers perform
3411da177e4SLinus Torvalds * non-speculative accesses to the virtual page table, so the address range of the
3421da177e4SLinus Torvalds * virtual page table itself needs to be covered by virtual page table.
3431da177e4SLinus Torvalds */
3441da177e4SLinus Torvalds # define vmlpt_bits (impl_va_bits - PAGE_SHIFT + pte_bits)
3451da177e4SLinus Torvalds # define POW2(n) (1ULL << (n))
3461da177e4SLinus Torvalds
3471da177e4SLinus Torvalds impl_va_bits = ffz(~(local_cpu_data->unimpl_va_mask | (7UL << 61)));
3481da177e4SLinus Torvalds
3491da177e4SLinus Torvalds if (impl_va_bits < 51 || impl_va_bits > 61)
3501da177e4SLinus Torvalds panic("CPU has bogus IMPL_VA_MSB value of %lu!\n", impl_va_bits - 1);
3516cf07a8cSPeter Chubb /*
3526cf07a8cSPeter Chubb * mapped_space_bits - PAGE_SHIFT is the total number of ptes we need,
3536cf07a8cSPeter Chubb * which must fit into "vmlpt_bits - pte_bits" slots. Second half of
3546cf07a8cSPeter Chubb * the test makes sure that our mapped space doesn't overlap the
3556cf07a8cSPeter Chubb * unimplemented hole in the middle of the region.
3566cf07a8cSPeter Chubb */
3576cf07a8cSPeter Chubb if ((mapped_space_bits - PAGE_SHIFT > vmlpt_bits - pte_bits) ||
3586cf07a8cSPeter Chubb (mapped_space_bits > impl_va_bits - 1))
3596cf07a8cSPeter Chubb panic("Cannot build a big enough virtual-linear page table"
3606cf07a8cSPeter Chubb " to cover mapped address space.\n"
3616cf07a8cSPeter Chubb " Try using a smaller page size.\n");
3626cf07a8cSPeter Chubb
3631da177e4SLinus Torvalds
3641da177e4SLinus Torvalds /* place the VMLPT at the end of each page-table mapped region: */
3651da177e4SLinus Torvalds pta = POW2(61) - POW2(vmlpt_bits);
3661da177e4SLinus Torvalds
3671da177e4SLinus Torvalds /*
3681da177e4SLinus Torvalds * Set the (virtually mapped linear) page table address. Bit
3691da177e4SLinus Torvalds * 8 selects between the short and long format, bits 2-7 the
3701da177e4SLinus Torvalds * size of the table, and bit 0 whether the VHPT walker is
3711da177e4SLinus Torvalds * enabled.
3721da177e4SLinus Torvalds */
3731da177e4SLinus Torvalds ia64_set_pta(pta | (0 << 8) | (vmlpt_bits << 2) | VHPT_ENABLE_BIT);
3741da177e4SLinus Torvalds
3751da177e4SLinus Torvalds ia64_tlb_init();
3761da177e4SLinus Torvalds
3771da177e4SLinus Torvalds #ifdef CONFIG_HUGETLB_PAGE
3781da177e4SLinus Torvalds ia64_set_rr(HPAGE_REGION_BASE, HPAGE_SHIFT << 2);
3791da177e4SLinus Torvalds ia64_srlz_d();
3801da177e4SLinus Torvalds #endif
3811da177e4SLinus Torvalds }
3821da177e4SLinus Torvalds
register_active_ranges(u64 start,u64 len,int nid)383e088a4adSMatthew Wilcox int __init register_active_ranges(u64 start, u64 len, int nid)
38405e0caadSMel Gorman {
38598075d24SZoltan Menyhart u64 end = start + len;
386139b8304SBob Picco
387139b8304SBob Picco #ifdef CONFIG_KEXEC
388139b8304SBob Picco if (start > crashk_res.start && start < crashk_res.end)
389139b8304SBob Picco start = crashk_res.end;
390139b8304SBob Picco if (end > crashk_res.start && end < crashk_res.end)
391139b8304SBob Picco end = crashk_res.start;
392139b8304SBob Picco #endif
393139b8304SBob Picco
394139b8304SBob Picco if (start < end)
395952eea9bSDavid Hildenbrand memblock_add_node(__pa(start), end - start, nid, MEMBLOCK_NONE);
39605e0caadSMel Gorman return 0;
39705e0caadSMel Gorman }
3981da177e4SLinus Torvalds
399a3f5c338SZou Nan hai int
find_max_min_low_pfn(u64 start,u64 end,void * arg)400e088a4adSMatthew Wilcox find_max_min_low_pfn (u64 start, u64 end, void *arg)
401a3f5c338SZou Nan hai {
402a3f5c338SZou Nan hai unsigned long pfn_start, pfn_end;
403a3f5c338SZou Nan hai #ifdef CONFIG_FLATMEM
404a3f5c338SZou Nan hai pfn_start = (PAGE_ALIGN(__pa(start))) >> PAGE_SHIFT;
405a3f5c338SZou Nan hai pfn_end = (PAGE_ALIGN(__pa(end - 1))) >> PAGE_SHIFT;
406a3f5c338SZou Nan hai #else
407a3f5c338SZou Nan hai pfn_start = GRANULEROUNDDOWN(__pa(start)) >> PAGE_SHIFT;
408a3f5c338SZou Nan hai pfn_end = GRANULEROUNDUP(__pa(end - 1)) >> PAGE_SHIFT;
409a3f5c338SZou Nan hai #endif
410a3f5c338SZou Nan hai min_low_pfn = min(min_low_pfn, pfn_start);
411a3f5c338SZou Nan hai max_low_pfn = max(max_low_pfn, pfn_end);
412a3f5c338SZou Nan hai return 0;
413a3f5c338SZou Nan hai }
414a3f5c338SZou Nan hai
4151da177e4SLinus Torvalds /*
4161da177e4SLinus Torvalds * Boot command-line option "nolwsys" can be used to disable the use of any light-weight
4171da177e4SLinus Torvalds * system call handler. When this option is in effect, all fsyscalls will end up bubbling
4181da177e4SLinus Torvalds * down into the kernel and calling the normal (heavy-weight) syscall handler. This is
4191da177e4SLinus Torvalds * useful for performance testing, but conceivably could also come in handy for debugging
4201da177e4SLinus Torvalds * purposes.
4211da177e4SLinus Torvalds */
4221da177e4SLinus Torvalds
42303906ea0SChen, Kenneth W static int nolwsys __initdata;
4241da177e4SLinus Torvalds
4251da177e4SLinus Torvalds static int __init
nolwsys_setup(char * s)4261da177e4SLinus Torvalds nolwsys_setup (char *s)
4271da177e4SLinus Torvalds {
4281da177e4SLinus Torvalds nolwsys = 1;
4291da177e4SLinus Torvalds return 1;
4301da177e4SLinus Torvalds }
4311da177e4SLinus Torvalds
4321da177e4SLinus Torvalds __setup("nolwsys", nolwsys_setup);
4331da177e4SLinus Torvalds
434dae28066SChen, Kenneth W void __init
mem_init(void)4351da177e4SLinus Torvalds mem_init (void)
4361da177e4SLinus Torvalds {
4371da177e4SLinus Torvalds int i;
4381da177e4SLinus Torvalds
439fde740e4SRobin Holt BUG_ON(PTRS_PER_PGD * sizeof(pgd_t) != PAGE_SIZE);
440fde740e4SRobin Holt BUG_ON(PTRS_PER_PMD * sizeof(pmd_t) != PAGE_SIZE);
441fde740e4SRobin Holt BUG_ON(PTRS_PER_PTE * sizeof(pte_t) != PAGE_SIZE);
442fde740e4SRobin Holt
4431da177e4SLinus Torvalds /*
444974f83ecSChristoph Hellwig * This needs to be called _after_ the command line has been parsed but
445974f83ecSChristoph Hellwig * _before_ any drivers that may need the PCI DMA interface are
446974f83ecSChristoph Hellwig * initialized or bootmem has been freed.
4471da177e4SLinus Torvalds */
4486d073dadSSergei Trofimovich do {
449974f83ecSChristoph Hellwig #ifdef CONFIG_INTEL_IOMMU
450974f83ecSChristoph Hellwig detect_intel_iommu();
4516d073dadSSergei Trofimovich if (iommu_detected)
4526d073dadSSergei Trofimovich break;
453974f83ecSChristoph Hellwig #endif
454c6af2aa9SChristoph Hellwig swiotlb_init(true, SWIOTLB_VERBOSE);
4556d073dadSSergei Trofimovich } while (0);
4561da177e4SLinus Torvalds
4572d4b1fa2SBob Picco #ifdef CONFIG_FLATMEM
45880a03e29SStoyan Gaydarov BUG_ON(!mem_map);
4591da177e4SLinus Torvalds #endif
4601da177e4SLinus Torvalds
461b57b63a2SJiang Liu set_max_mapnr(max_low_pfn);
4621da177e4SLinus Torvalds high_memory = __va(max_low_pfn * PAGE_SIZE);
463c6ffc5caSMike Rapoport memblock_free_all();
4641da177e4SLinus Torvalds
4651da177e4SLinus Torvalds /*
4660af96a02SJulia Lawall * For fsyscall entrypoints with no light-weight handler, use the ordinary
4671da177e4SLinus Torvalds * (heavy-weight) handler, but mark it by setting bit 0, so the fsyscall entry
4681da177e4SLinus Torvalds * code can tell them apart.
4691da177e4SLinus Torvalds */
4701da177e4SLinus Torvalds for (i = 0; i < NR_syscalls; ++i) {
471e55645ecSLuis R. Rodriguez extern unsigned long fsyscall_table[NR_syscalls];
4721da177e4SLinus Torvalds extern unsigned long sys_call_table[NR_syscalls];
4731da177e4SLinus Torvalds
4741da177e4SLinus Torvalds if (!fsyscall_table[i] || nolwsys)
4751da177e4SLinus Torvalds fsyscall_table[i] = sys_call_table[i] | 1;
4761da177e4SLinus Torvalds }
4771da177e4SLinus Torvalds setup_gate();
4781da177e4SLinus Torvalds }
4791681b8e1SYasunori Goto
4801681b8e1SYasunori Goto #ifdef CONFIG_MEMORY_HOTPLUG
arch_add_memory(int nid,u64 start,u64 size,struct mhp_params * params)481940519f0SMichal Hocko int arch_add_memory(int nid, u64 start, u64 size,
482f5637d3bSLogan Gunthorpe struct mhp_params *params)
4831681b8e1SYasunori Goto {
4841681b8e1SYasunori Goto unsigned long start_pfn = start >> PAGE_SHIFT;
4851681b8e1SYasunori Goto unsigned long nr_pages = size >> PAGE_SHIFT;
4861681b8e1SYasunori Goto int ret;
4871681b8e1SYasunori Goto
488bfeb022fSLogan Gunthorpe if (WARN_ON_ONCE(params->pgprot.pgprot != PAGE_KERNEL.pgprot))
489bfeb022fSLogan Gunthorpe return -EINVAL;
490bfeb022fSLogan Gunthorpe
491f5637d3bSLogan Gunthorpe ret = __add_pages(nid, start_pfn, nr_pages, params);
4921681b8e1SYasunori Goto if (ret)
4931681b8e1SYasunori Goto printk("%s: Problem encountered in __add_pages() as ret=%d\n",
494d4ed8084SHarvey Harrison __func__, ret);
4951681b8e1SYasunori Goto
4961681b8e1SYasunori Goto return ret;
4971681b8e1SYasunori Goto }
49824d335caSWen Congyang
arch_remove_memory(u64 start,u64 size,struct vmem_altmap * altmap)49965a2aa5fSDavid Hildenbrand void arch_remove_memory(u64 start, u64 size, struct vmem_altmap *altmap)
50024d335caSWen Congyang {
50124d335caSWen Congyang unsigned long start_pfn = start >> PAGE_SHIFT;
50224d335caSWen Congyang unsigned long nr_pages = size >> PAGE_SHIFT;
50324d335caSWen Congyang
504feee6b29SDavid Hildenbrand __remove_pages(start_pfn, nr_pages, altmap);
50524d335caSWen Congyang }
50624d335caSWen Congyang #endif
507c619b82cSAnshuman Khandual
508c619b82cSAnshuman Khandual static const pgprot_t protection_map[16] = {
509c619b82cSAnshuman Khandual [VM_NONE] = PAGE_NONE,
510c619b82cSAnshuman Khandual [VM_READ] = PAGE_READONLY,
511c619b82cSAnshuman Khandual [VM_WRITE] = PAGE_READONLY,
512c619b82cSAnshuman Khandual [VM_WRITE | VM_READ] = PAGE_READONLY,
513c619b82cSAnshuman Khandual [VM_EXEC] = __pgprot(__ACCESS_BITS | _PAGE_PL_3 |
514c619b82cSAnshuman Khandual _PAGE_AR_X_RX),
515c619b82cSAnshuman Khandual [VM_EXEC | VM_READ] = __pgprot(__ACCESS_BITS | _PAGE_PL_3 |
516c619b82cSAnshuman Khandual _PAGE_AR_RX),
517c619b82cSAnshuman Khandual [VM_EXEC | VM_WRITE] = PAGE_COPY_EXEC,
518c619b82cSAnshuman Khandual [VM_EXEC | VM_WRITE | VM_READ] = PAGE_COPY_EXEC,
519c619b82cSAnshuman Khandual [VM_SHARED] = PAGE_NONE,
520c619b82cSAnshuman Khandual [VM_SHARED | VM_READ] = PAGE_READONLY,
521c619b82cSAnshuman Khandual [VM_SHARED | VM_WRITE] = PAGE_SHARED,
522c619b82cSAnshuman Khandual [VM_SHARED | VM_WRITE | VM_READ] = PAGE_SHARED,
523c619b82cSAnshuman Khandual [VM_SHARED | VM_EXEC] = __pgprot(__ACCESS_BITS | _PAGE_PL_3 |
524c619b82cSAnshuman Khandual _PAGE_AR_X_RX),
525c619b82cSAnshuman Khandual [VM_SHARED | VM_EXEC | VM_READ] = __pgprot(__ACCESS_BITS | _PAGE_PL_3 |
526c619b82cSAnshuman Khandual _PAGE_AR_RX),
527c619b82cSAnshuman Khandual [VM_SHARED | VM_EXEC | VM_WRITE] = __pgprot(__ACCESS_BITS | _PAGE_PL_3 |
528c619b82cSAnshuman Khandual _PAGE_AR_RWX),
529c619b82cSAnshuman Khandual [VM_SHARED | VM_EXEC | VM_WRITE | VM_READ] = __pgprot(__ACCESS_BITS | _PAGE_PL_3 |
530c619b82cSAnshuman Khandual _PAGE_AR_RWX)
531c619b82cSAnshuman Khandual };
532c619b82cSAnshuman Khandual DECLARE_VM_GET_PAGE_PROT
533