162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci#ifndef _ASM_IA64_PGTABLE_H
362306a36Sopenharmony_ci#define _ASM_IA64_PGTABLE_H
462306a36Sopenharmony_ci
562306a36Sopenharmony_ci/*
662306a36Sopenharmony_ci * This file contains the functions and defines necessary to modify and use
762306a36Sopenharmony_ci * the IA-64 page table tree.
862306a36Sopenharmony_ci *
962306a36Sopenharmony_ci * This hopefully works with any (fixed) IA-64 page-size, as defined
1062306a36Sopenharmony_ci * in <asm/page.h>.
1162306a36Sopenharmony_ci *
1262306a36Sopenharmony_ci * Copyright (C) 1998-2005 Hewlett-Packard Co
1362306a36Sopenharmony_ci *	David Mosberger-Tang <davidm@hpl.hp.com>
1462306a36Sopenharmony_ci */
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci#include <asm/mman.h>
1862306a36Sopenharmony_ci#include <asm/page.h>
1962306a36Sopenharmony_ci#include <asm/processor.h>
2062306a36Sopenharmony_ci#include <asm/types.h>
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ci#define IA64_MAX_PHYS_BITS	50	/* max. number of physical address bits (architected) */
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ci/*
2562306a36Sopenharmony_ci * First, define the various bits in a PTE.  Note that the PTE format
2662306a36Sopenharmony_ci * matches the VHPT short format, the firt doubleword of the VHPD long
2762306a36Sopenharmony_ci * format, and the first doubleword of the TLB insertion format.
2862306a36Sopenharmony_ci */
2962306a36Sopenharmony_ci#define _PAGE_P_BIT		0
3062306a36Sopenharmony_ci#define _PAGE_A_BIT		5
3162306a36Sopenharmony_ci#define _PAGE_D_BIT		6
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ci#define _PAGE_P			(1 << _PAGE_P_BIT)	/* page present bit */
3462306a36Sopenharmony_ci#define _PAGE_MA_WB		(0x0 <<  2)	/* write back memory attribute */
3562306a36Sopenharmony_ci#define _PAGE_MA_UC		(0x4 <<  2)	/* uncacheable memory attribute */
3662306a36Sopenharmony_ci#define _PAGE_MA_UCE		(0x5 <<  2)	/* UC exported attribute */
3762306a36Sopenharmony_ci#define _PAGE_MA_WC		(0x6 <<  2)	/* write coalescing memory attribute */
3862306a36Sopenharmony_ci#define _PAGE_MA_NAT		(0x7 <<  2)	/* not-a-thing attribute */
3962306a36Sopenharmony_ci#define _PAGE_MA_MASK		(0x7 <<  2)
4062306a36Sopenharmony_ci#define _PAGE_PL_0		(0 <<  7)	/* privilege level 0 (kernel) */
4162306a36Sopenharmony_ci#define _PAGE_PL_1		(1 <<  7)	/* privilege level 1 (unused) */
4262306a36Sopenharmony_ci#define _PAGE_PL_2		(2 <<  7)	/* privilege level 2 (unused) */
4362306a36Sopenharmony_ci#define _PAGE_PL_3		(3 <<  7)	/* privilege level 3 (user) */
4462306a36Sopenharmony_ci#define _PAGE_PL_MASK		(3 <<  7)
4562306a36Sopenharmony_ci#define _PAGE_AR_R		(0 <<  9)	/* read only */
4662306a36Sopenharmony_ci#define _PAGE_AR_RX		(1 <<  9)	/* read & execute */
4762306a36Sopenharmony_ci#define _PAGE_AR_RW		(2 <<  9)	/* read & write */
4862306a36Sopenharmony_ci#define _PAGE_AR_RWX		(3 <<  9)	/* read, write & execute */
4962306a36Sopenharmony_ci#define _PAGE_AR_R_RW		(4 <<  9)	/* read / read & write */
5062306a36Sopenharmony_ci#define _PAGE_AR_RX_RWX		(5 <<  9)	/* read & exec / read, write & exec */
5162306a36Sopenharmony_ci#define _PAGE_AR_RWX_RW		(6 <<  9)	/* read, write & exec / read & write */
5262306a36Sopenharmony_ci#define _PAGE_AR_X_RX		(7 <<  9)	/* exec & promote / read & exec */
5362306a36Sopenharmony_ci#define _PAGE_AR_MASK		(7 <<  9)
5462306a36Sopenharmony_ci#define _PAGE_AR_SHIFT		9
5562306a36Sopenharmony_ci#define _PAGE_A			(1 << _PAGE_A_BIT)	/* page accessed bit */
5662306a36Sopenharmony_ci#define _PAGE_D			(1 << _PAGE_D_BIT)	/* page dirty bit */
5762306a36Sopenharmony_ci#define _PAGE_PPN_MASK		(((__IA64_UL(1) << IA64_MAX_PHYS_BITS) - 1) & ~0xfffUL)
5862306a36Sopenharmony_ci#define _PAGE_ED		(__IA64_UL(1) << 52)	/* exception deferral */
5962306a36Sopenharmony_ci#define _PAGE_PROTNONE		(__IA64_UL(1) << 63)
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_ci/* We borrow bit 7 to store the exclusive marker in swap PTEs. */
6262306a36Sopenharmony_ci#define _PAGE_SWP_EXCLUSIVE	(1 << 7)
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_ci#define _PFN_MASK		_PAGE_PPN_MASK
6562306a36Sopenharmony_ci/* Mask of bits which may be changed by pte_modify(); the odd bits are there for _PAGE_PROTNONE */
6662306a36Sopenharmony_ci#define _PAGE_CHG_MASK	(_PAGE_P | _PAGE_PROTNONE | _PAGE_PL_MASK | _PAGE_AR_MASK | _PAGE_ED)
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci#define _PAGE_SIZE_4K	12
6962306a36Sopenharmony_ci#define _PAGE_SIZE_8K	13
7062306a36Sopenharmony_ci#define _PAGE_SIZE_16K	14
7162306a36Sopenharmony_ci#define _PAGE_SIZE_64K	16
7262306a36Sopenharmony_ci#define _PAGE_SIZE_256K	18
7362306a36Sopenharmony_ci#define _PAGE_SIZE_1M	20
7462306a36Sopenharmony_ci#define _PAGE_SIZE_4M	22
7562306a36Sopenharmony_ci#define _PAGE_SIZE_16M	24
7662306a36Sopenharmony_ci#define _PAGE_SIZE_64M	26
7762306a36Sopenharmony_ci#define _PAGE_SIZE_256M	28
7862306a36Sopenharmony_ci#define _PAGE_SIZE_1G	30
7962306a36Sopenharmony_ci#define _PAGE_SIZE_4G	32
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ci#define __ACCESS_BITS		_PAGE_ED | _PAGE_A | _PAGE_P | _PAGE_MA_WB
8262306a36Sopenharmony_ci#define __DIRTY_BITS_NO_ED	_PAGE_A | _PAGE_P | _PAGE_D | _PAGE_MA_WB
8362306a36Sopenharmony_ci#define __DIRTY_BITS		_PAGE_ED | __DIRTY_BITS_NO_ED
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_ci/*
8662306a36Sopenharmony_ci * How many pointers will a page table level hold expressed in shift
8762306a36Sopenharmony_ci */
8862306a36Sopenharmony_ci#define PTRS_PER_PTD_SHIFT	(PAGE_SHIFT-3)
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ci/*
9162306a36Sopenharmony_ci * Definitions for fourth level:
9262306a36Sopenharmony_ci */
9362306a36Sopenharmony_ci#define PTRS_PER_PTE	(__IA64_UL(1) << (PTRS_PER_PTD_SHIFT))
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_ci/*
9662306a36Sopenharmony_ci * Definitions for third level:
9762306a36Sopenharmony_ci *
9862306a36Sopenharmony_ci * PMD_SHIFT determines the size of the area a third-level page table
9962306a36Sopenharmony_ci * can map.
10062306a36Sopenharmony_ci */
10162306a36Sopenharmony_ci#define PMD_SHIFT	(PAGE_SHIFT + (PTRS_PER_PTD_SHIFT))
10262306a36Sopenharmony_ci#define PMD_SIZE	(1UL << PMD_SHIFT)
10362306a36Sopenharmony_ci#define PMD_MASK	(~(PMD_SIZE-1))
10462306a36Sopenharmony_ci#define PTRS_PER_PMD	(1UL << (PTRS_PER_PTD_SHIFT))
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_ci#if CONFIG_PGTABLE_LEVELS == 4
10762306a36Sopenharmony_ci/*
10862306a36Sopenharmony_ci * Definitions for second level:
10962306a36Sopenharmony_ci *
11062306a36Sopenharmony_ci * PUD_SHIFT determines the size of the area a second-level page table
11162306a36Sopenharmony_ci * can map.
11262306a36Sopenharmony_ci */
11362306a36Sopenharmony_ci#define PUD_SHIFT	(PMD_SHIFT + (PTRS_PER_PTD_SHIFT))
11462306a36Sopenharmony_ci#define PUD_SIZE	(1UL << PUD_SHIFT)
11562306a36Sopenharmony_ci#define PUD_MASK	(~(PUD_SIZE-1))
11662306a36Sopenharmony_ci#define PTRS_PER_PUD	(1UL << (PTRS_PER_PTD_SHIFT))
11762306a36Sopenharmony_ci#endif
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ci/*
12062306a36Sopenharmony_ci * Definitions for first level:
12162306a36Sopenharmony_ci *
12262306a36Sopenharmony_ci * PGDIR_SHIFT determines what a first-level page table entry can map.
12362306a36Sopenharmony_ci */
12462306a36Sopenharmony_ci#if CONFIG_PGTABLE_LEVELS == 4
12562306a36Sopenharmony_ci#define PGDIR_SHIFT		(PUD_SHIFT + (PTRS_PER_PTD_SHIFT))
12662306a36Sopenharmony_ci#else
12762306a36Sopenharmony_ci#define PGDIR_SHIFT		(PMD_SHIFT + (PTRS_PER_PTD_SHIFT))
12862306a36Sopenharmony_ci#endif
12962306a36Sopenharmony_ci#define PGDIR_SIZE		(__IA64_UL(1) << PGDIR_SHIFT)
13062306a36Sopenharmony_ci#define PGDIR_MASK		(~(PGDIR_SIZE-1))
13162306a36Sopenharmony_ci#define PTRS_PER_PGD_SHIFT	PTRS_PER_PTD_SHIFT
13262306a36Sopenharmony_ci#define PTRS_PER_PGD		(1UL << PTRS_PER_PGD_SHIFT)
13362306a36Sopenharmony_ci#define USER_PTRS_PER_PGD	(5*PTRS_PER_PGD/8)	/* regions 0-4 are user regions */
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_ci/*
13662306a36Sopenharmony_ci * All the normal masks have the "page accessed" bits on, as any time
13762306a36Sopenharmony_ci * they are used, the page is accessed. They are cleared only by the
13862306a36Sopenharmony_ci * page-out routines.
13962306a36Sopenharmony_ci */
14062306a36Sopenharmony_ci#define PAGE_NONE	__pgprot(_PAGE_PROTNONE | _PAGE_A)
14162306a36Sopenharmony_ci#define PAGE_SHARED	__pgprot(__ACCESS_BITS | _PAGE_PL_3 | _PAGE_AR_RW)
14262306a36Sopenharmony_ci#define PAGE_READONLY	__pgprot(__ACCESS_BITS | _PAGE_PL_3 | _PAGE_AR_R)
14362306a36Sopenharmony_ci#define PAGE_COPY	__pgprot(__ACCESS_BITS | _PAGE_PL_3 | _PAGE_AR_R)
14462306a36Sopenharmony_ci#define PAGE_COPY_EXEC	__pgprot(__ACCESS_BITS | _PAGE_PL_3 | _PAGE_AR_RX)
14562306a36Sopenharmony_ci#define PAGE_GATE	__pgprot(__ACCESS_BITS | _PAGE_PL_0 | _PAGE_AR_X_RX)
14662306a36Sopenharmony_ci#define PAGE_KERNEL	__pgprot(__DIRTY_BITS  | _PAGE_PL_0 | _PAGE_AR_RWX)
14762306a36Sopenharmony_ci#define PAGE_KERNELRX	__pgprot(__ACCESS_BITS | _PAGE_PL_0 | _PAGE_AR_RX)
14862306a36Sopenharmony_ci#define PAGE_KERNEL_UC	__pgprot(__DIRTY_BITS  | _PAGE_PL_0 | _PAGE_AR_RWX | \
14962306a36Sopenharmony_ci				 _PAGE_MA_UC)
15062306a36Sopenharmony_ci
15162306a36Sopenharmony_ci# ifndef __ASSEMBLY__
15262306a36Sopenharmony_ci
15362306a36Sopenharmony_ci#include <linux/sched/mm.h>	/* for mm_struct */
15462306a36Sopenharmony_ci#include <linux/bitops.h>
15562306a36Sopenharmony_ci#include <asm/cacheflush.h>
15662306a36Sopenharmony_ci#include <asm/mmu_context.h>
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_ci/*
15962306a36Sopenharmony_ci * Next come the mappings that determine how mmap() protection bits
16062306a36Sopenharmony_ci * (PROT_EXEC, PROT_READ, PROT_WRITE, PROT_NONE) get implemented.  The
16162306a36Sopenharmony_ci * _P version gets used for a private shared memory segment, the _S
16262306a36Sopenharmony_ci * version gets used for a shared memory segment with MAP_SHARED on.
16362306a36Sopenharmony_ci * In a private shared memory segment, we do a copy-on-write if a task
16462306a36Sopenharmony_ci * attempts to write to the page.
16562306a36Sopenharmony_ci */
16662306a36Sopenharmony_ci	/* xwr */
16762306a36Sopenharmony_ci#define pgd_ERROR(e)	printk("%s:%d: bad pgd %016lx.\n", __FILE__, __LINE__, pgd_val(e))
16862306a36Sopenharmony_ci#if CONFIG_PGTABLE_LEVELS == 4
16962306a36Sopenharmony_ci#define pud_ERROR(e)	printk("%s:%d: bad pud %016lx.\n", __FILE__, __LINE__, pud_val(e))
17062306a36Sopenharmony_ci#endif
17162306a36Sopenharmony_ci#define pmd_ERROR(e)	printk("%s:%d: bad pmd %016lx.\n", __FILE__, __LINE__, pmd_val(e))
17262306a36Sopenharmony_ci#define pte_ERROR(e)	printk("%s:%d: bad pte %016lx.\n", __FILE__, __LINE__, pte_val(e))
17362306a36Sopenharmony_ci
17462306a36Sopenharmony_ci
17562306a36Sopenharmony_ci/*
17662306a36Sopenharmony_ci * Some definitions to translate between mem_map, PTEs, and page addresses:
17762306a36Sopenharmony_ci */
17862306a36Sopenharmony_ci
17962306a36Sopenharmony_ci
18062306a36Sopenharmony_ci/* Quick test to see if ADDR is a (potentially) valid physical address. */
18162306a36Sopenharmony_cistatic inline long
18262306a36Sopenharmony_ciia64_phys_addr_valid (unsigned long addr)
18362306a36Sopenharmony_ci{
18462306a36Sopenharmony_ci	return (addr & (local_cpu_data->unimpl_pa_mask)) == 0;
18562306a36Sopenharmony_ci}
18662306a36Sopenharmony_ci
18762306a36Sopenharmony_ci/*
18862306a36Sopenharmony_ci * Now come the defines and routines to manage and access the three-level
18962306a36Sopenharmony_ci * page table.
19062306a36Sopenharmony_ci */
19162306a36Sopenharmony_ci
19262306a36Sopenharmony_ci
19362306a36Sopenharmony_ci#define VMALLOC_START		(RGN_BASE(RGN_GATE) + 0x200000000UL)
19462306a36Sopenharmony_ci#if defined(CONFIG_SPARSEMEM) && defined(CONFIG_SPARSEMEM_VMEMMAP)
19562306a36Sopenharmony_ci/* SPARSEMEM_VMEMMAP uses half of vmalloc... */
19662306a36Sopenharmony_ci# define VMALLOC_END		(RGN_BASE(RGN_GATE) + (1UL << (4*PAGE_SHIFT - 10)))
19762306a36Sopenharmony_ci# define vmemmap		((struct page *)VMALLOC_END)
19862306a36Sopenharmony_ci#else
19962306a36Sopenharmony_ci# define VMALLOC_END		(RGN_BASE(RGN_GATE) + (1UL << (4*PAGE_SHIFT - 9)))
20062306a36Sopenharmony_ci#endif
20162306a36Sopenharmony_ci
20262306a36Sopenharmony_ci/* fs/proc/kcore.c */
20362306a36Sopenharmony_ci#define	kc_vaddr_to_offset(v) ((v) - RGN_BASE(RGN_GATE))
20462306a36Sopenharmony_ci#define	kc_offset_to_vaddr(o) ((o) + RGN_BASE(RGN_GATE))
20562306a36Sopenharmony_ci
20662306a36Sopenharmony_ci#define RGN_MAP_SHIFT (PGDIR_SHIFT + PTRS_PER_PGD_SHIFT - 3)
20762306a36Sopenharmony_ci#define RGN_MAP_LIMIT	((1UL << RGN_MAP_SHIFT) - PAGE_SIZE)	/* per region addr limit */
20862306a36Sopenharmony_ci
20962306a36Sopenharmony_ci#define PFN_PTE_SHIFT	PAGE_SHIFT
21062306a36Sopenharmony_ci/*
21162306a36Sopenharmony_ci * Conversion functions: convert page frame number (pfn) and a protection value to a page
21262306a36Sopenharmony_ci * table entry (pte).
21362306a36Sopenharmony_ci */
21462306a36Sopenharmony_ci#define pfn_pte(pfn, pgprot) \
21562306a36Sopenharmony_ci({ pte_t __pte; pte_val(__pte) = ((pfn) << PAGE_SHIFT) | pgprot_val(pgprot); __pte; })
21662306a36Sopenharmony_ci
21762306a36Sopenharmony_ci/* Extract pfn from pte.  */
21862306a36Sopenharmony_ci#define pte_pfn(_pte)		((pte_val(_pte) & _PFN_MASK) >> PAGE_SHIFT)
21962306a36Sopenharmony_ci
22062306a36Sopenharmony_ci#define mk_pte(page, pgprot)	pfn_pte(page_to_pfn(page), (pgprot))
22162306a36Sopenharmony_ci
22262306a36Sopenharmony_ci/* This takes a physical page address that is used by the remapping functions */
22362306a36Sopenharmony_ci#define mk_pte_phys(physpage, pgprot) \
22462306a36Sopenharmony_ci({ pte_t __pte; pte_val(__pte) = physpage + pgprot_val(pgprot); __pte; })
22562306a36Sopenharmony_ci
22662306a36Sopenharmony_ci#define pte_modify(_pte, newprot) \
22762306a36Sopenharmony_ci	(__pte((pte_val(_pte) & ~_PAGE_CHG_MASK) | (pgprot_val(newprot) & _PAGE_CHG_MASK)))
22862306a36Sopenharmony_ci
22962306a36Sopenharmony_ci#define pte_none(pte) 			(!pte_val(pte))
23062306a36Sopenharmony_ci#define pte_present(pte)		(pte_val(pte) & (_PAGE_P | _PAGE_PROTNONE))
23162306a36Sopenharmony_ci#define pte_clear(mm,addr,pte)		(pte_val(*(pte)) = 0UL)
23262306a36Sopenharmony_ci/* pte_page() returns the "struct page *" corresponding to the PTE: */
23362306a36Sopenharmony_ci#define pte_page(pte)			virt_to_page(((pte_val(pte) & _PFN_MASK) + PAGE_OFFSET))
23462306a36Sopenharmony_ci
23562306a36Sopenharmony_ci#define pmd_none(pmd)			(!pmd_val(pmd))
23662306a36Sopenharmony_ci#define pmd_bad(pmd)			(!ia64_phys_addr_valid(pmd_val(pmd)))
23762306a36Sopenharmony_ci#define pmd_present(pmd)		(pmd_val(pmd) != 0UL)
23862306a36Sopenharmony_ci#define pmd_clear(pmdp)			(pmd_val(*(pmdp)) = 0UL)
23962306a36Sopenharmony_ci#define pmd_page_vaddr(pmd)		((unsigned long) __va(pmd_val(pmd) & _PFN_MASK))
24062306a36Sopenharmony_ci#define pmd_pfn(pmd)			((pmd_val(pmd) & _PFN_MASK) >> PAGE_SHIFT)
24162306a36Sopenharmony_ci#define pmd_page(pmd)			virt_to_page((pmd_val(pmd) + PAGE_OFFSET))
24262306a36Sopenharmony_ci
24362306a36Sopenharmony_ci#define pud_none(pud)			(!pud_val(pud))
24462306a36Sopenharmony_ci#define pud_bad(pud)			(!ia64_phys_addr_valid(pud_val(pud)))
24562306a36Sopenharmony_ci#define pud_present(pud)		(pud_val(pud) != 0UL)
24662306a36Sopenharmony_ci#define pud_clear(pudp)			(pud_val(*(pudp)) = 0UL)
24762306a36Sopenharmony_ci#define pud_pgtable(pud)		((pmd_t *) __va(pud_val(pud) & _PFN_MASK))
24862306a36Sopenharmony_ci#define pud_page(pud)			virt_to_page((pud_val(pud) + PAGE_OFFSET))
24962306a36Sopenharmony_ci
25062306a36Sopenharmony_ci#if CONFIG_PGTABLE_LEVELS == 4
25162306a36Sopenharmony_ci#define p4d_none(p4d)			(!p4d_val(p4d))
25262306a36Sopenharmony_ci#define p4d_bad(p4d)			(!ia64_phys_addr_valid(p4d_val(p4d)))
25362306a36Sopenharmony_ci#define p4d_present(p4d)		(p4d_val(p4d) != 0UL)
25462306a36Sopenharmony_ci#define p4d_clear(p4dp)			(p4d_val(*(p4dp)) = 0UL)
25562306a36Sopenharmony_ci#define p4d_pgtable(p4d)		((pud_t *) __va(p4d_val(p4d) & _PFN_MASK))
25662306a36Sopenharmony_ci#define p4d_page(p4d)			virt_to_page((p4d_val(p4d) + PAGE_OFFSET))
25762306a36Sopenharmony_ci#endif
25862306a36Sopenharmony_ci
25962306a36Sopenharmony_ci/*
26062306a36Sopenharmony_ci * The following have defined behavior only work if pte_present() is true.
26162306a36Sopenharmony_ci */
26262306a36Sopenharmony_ci#define pte_write(pte)	((unsigned) (((pte_val(pte) & _PAGE_AR_MASK) >> _PAGE_AR_SHIFT) - 2) <= 4)
26362306a36Sopenharmony_ci#define pte_exec(pte)		((pte_val(pte) & _PAGE_AR_RX) != 0)
26462306a36Sopenharmony_ci#define pte_dirty(pte)		((pte_val(pte) & _PAGE_D) != 0)
26562306a36Sopenharmony_ci#define pte_young(pte)		((pte_val(pte) & _PAGE_A) != 0)
26662306a36Sopenharmony_ci
26762306a36Sopenharmony_ci/*
26862306a36Sopenharmony_ci * Note: we convert AR_RWX to AR_RX and AR_RW to AR_R by clearing the 2nd bit in the
26962306a36Sopenharmony_ci * access rights:
27062306a36Sopenharmony_ci */
27162306a36Sopenharmony_ci#define pte_wrprotect(pte)	(__pte(pte_val(pte) & ~_PAGE_AR_RW))
27262306a36Sopenharmony_ci#define pte_mkwrite_novma(pte)	(__pte(pte_val(pte) | _PAGE_AR_RW))
27362306a36Sopenharmony_ci#define pte_mkold(pte)		(__pte(pte_val(pte) & ~_PAGE_A))
27462306a36Sopenharmony_ci#define pte_mkyoung(pte)	(__pte(pte_val(pte) | _PAGE_A))
27562306a36Sopenharmony_ci#define pte_mkclean(pte)	(__pte(pte_val(pte) & ~_PAGE_D))
27662306a36Sopenharmony_ci#define pte_mkdirty(pte)	(__pte(pte_val(pte) | _PAGE_D))
27762306a36Sopenharmony_ci#define pte_mkhuge(pte)		(__pte(pte_val(pte)))
27862306a36Sopenharmony_ci
27962306a36Sopenharmony_ci/*
28062306a36Sopenharmony_ci * Because ia64's Icache and Dcache is not coherent (on a cpu), we need to
28162306a36Sopenharmony_ci * sync icache and dcache when we insert *new* executable page.
28262306a36Sopenharmony_ci *  __ia64_sync_icache_dcache() check Pg_arch_1 bit and flush icache
28362306a36Sopenharmony_ci * if necessary.
28462306a36Sopenharmony_ci *
28562306a36Sopenharmony_ci *  set_pte() is also called by the kernel, but we can expect that the kernel
28662306a36Sopenharmony_ci *  flushes icache explicitly if necessary.
28762306a36Sopenharmony_ci */
28862306a36Sopenharmony_ci#define pte_present_exec_user(pte)\
28962306a36Sopenharmony_ci	((pte_val(pte) & (_PAGE_P | _PAGE_PL_MASK | _PAGE_AR_RX)) == \
29062306a36Sopenharmony_ci		(_PAGE_P | _PAGE_PL_3 | _PAGE_AR_RX))
29162306a36Sopenharmony_ci
29262306a36Sopenharmony_ciextern void __ia64_sync_icache_dcache(pte_t pteval);
29362306a36Sopenharmony_cistatic inline void set_pte(pte_t *ptep, pte_t pteval)
29462306a36Sopenharmony_ci{
29562306a36Sopenharmony_ci	/* page is present && page is user  && page is executable
29662306a36Sopenharmony_ci	 * && (page swapin or new page or page migration
29762306a36Sopenharmony_ci	 *	|| copy_on_write with page copying.)
29862306a36Sopenharmony_ci	 */
29962306a36Sopenharmony_ci	if (pte_present_exec_user(pteval) &&
30062306a36Sopenharmony_ci	    (!pte_present(*ptep) ||
30162306a36Sopenharmony_ci		pte_pfn(*ptep) != pte_pfn(pteval)))
30262306a36Sopenharmony_ci		/* load_module() calles flush_icache_range() explicitly*/
30362306a36Sopenharmony_ci		__ia64_sync_icache_dcache(pteval);
30462306a36Sopenharmony_ci	*ptep = pteval;
30562306a36Sopenharmony_ci}
30662306a36Sopenharmony_ci
30762306a36Sopenharmony_ci/*
30862306a36Sopenharmony_ci * Make page protection values cacheable, uncacheable, or write-
30962306a36Sopenharmony_ci * combining.  Note that "protection" is really a misnomer here as the
31062306a36Sopenharmony_ci * protection value contains the memory attribute bits, dirty bits, and
31162306a36Sopenharmony_ci * various other bits as well.
31262306a36Sopenharmony_ci */
31362306a36Sopenharmony_ci#define pgprot_cacheable(prot)		__pgprot((pgprot_val(prot) & ~_PAGE_MA_MASK) | _PAGE_MA_WB)
31462306a36Sopenharmony_ci#define pgprot_noncached(prot)		__pgprot((pgprot_val(prot) & ~_PAGE_MA_MASK) | _PAGE_MA_UC)
31562306a36Sopenharmony_ci#define pgprot_writecombine(prot)	__pgprot((pgprot_val(prot) & ~_PAGE_MA_MASK) | _PAGE_MA_WC)
31662306a36Sopenharmony_ci
31762306a36Sopenharmony_cistruct file;
31862306a36Sopenharmony_ciextern pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
31962306a36Sopenharmony_ci				     unsigned long size, pgprot_t vma_prot);
32062306a36Sopenharmony_ci#define __HAVE_PHYS_MEM_ACCESS_PROT
32162306a36Sopenharmony_ci
32262306a36Sopenharmony_cistatic inline unsigned long
32362306a36Sopenharmony_cipgd_index (unsigned long address)
32462306a36Sopenharmony_ci{
32562306a36Sopenharmony_ci	unsigned long region = address >> 61;
32662306a36Sopenharmony_ci	unsigned long l1index = (address >> PGDIR_SHIFT) & ((PTRS_PER_PGD >> 3) - 1);
32762306a36Sopenharmony_ci
32862306a36Sopenharmony_ci	return (region << (PAGE_SHIFT - 6)) | l1index;
32962306a36Sopenharmony_ci}
33062306a36Sopenharmony_ci#define pgd_index pgd_index
33162306a36Sopenharmony_ci
33262306a36Sopenharmony_ci/*
33362306a36Sopenharmony_ci * In the kernel's mapped region we know everything is in region number 5, so
33462306a36Sopenharmony_ci * as an optimisation its PGD already points to the area for that region.
33562306a36Sopenharmony_ci * However, this also means that we cannot use pgd_index() and we must
33662306a36Sopenharmony_ci * never add the region here.
33762306a36Sopenharmony_ci */
33862306a36Sopenharmony_ci#define pgd_offset_k(addr) \
33962306a36Sopenharmony_ci	(init_mm.pgd + (((addr) >> PGDIR_SHIFT) & (PTRS_PER_PGD - 1)))
34062306a36Sopenharmony_ci
34162306a36Sopenharmony_ci/* Look up a pgd entry in the gate area.  On IA-64, the gate-area
34262306a36Sopenharmony_ci   resides in the kernel-mapped segment, hence we use pgd_offset_k()
34362306a36Sopenharmony_ci   here.  */
34462306a36Sopenharmony_ci#define pgd_offset_gate(mm, addr)	pgd_offset_k(addr)
34562306a36Sopenharmony_ci
34662306a36Sopenharmony_ci/* atomic versions of the some PTE manipulations: */
34762306a36Sopenharmony_ci
34862306a36Sopenharmony_cistatic inline int
34962306a36Sopenharmony_ciptep_test_and_clear_young (struct vm_area_struct *vma, unsigned long addr, pte_t *ptep)
35062306a36Sopenharmony_ci{
35162306a36Sopenharmony_ci#ifdef CONFIG_SMP
35262306a36Sopenharmony_ci	if (!pte_young(*ptep))
35362306a36Sopenharmony_ci		return 0;
35462306a36Sopenharmony_ci	return test_and_clear_bit(_PAGE_A_BIT, ptep);
35562306a36Sopenharmony_ci#else
35662306a36Sopenharmony_ci	pte_t pte = *ptep;
35762306a36Sopenharmony_ci	if (!pte_young(pte))
35862306a36Sopenharmony_ci		return 0;
35962306a36Sopenharmony_ci	set_pte_at(vma->vm_mm, addr, ptep, pte_mkold(pte));
36062306a36Sopenharmony_ci	return 1;
36162306a36Sopenharmony_ci#endif
36262306a36Sopenharmony_ci}
36362306a36Sopenharmony_ci
36462306a36Sopenharmony_cistatic inline pte_t
36562306a36Sopenharmony_ciptep_get_and_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
36662306a36Sopenharmony_ci{
36762306a36Sopenharmony_ci#ifdef CONFIG_SMP
36862306a36Sopenharmony_ci	return __pte(xchg((long *) ptep, 0));
36962306a36Sopenharmony_ci#else
37062306a36Sopenharmony_ci	pte_t pte = *ptep;
37162306a36Sopenharmony_ci	pte_clear(mm, addr, ptep);
37262306a36Sopenharmony_ci	return pte;
37362306a36Sopenharmony_ci#endif
37462306a36Sopenharmony_ci}
37562306a36Sopenharmony_ci
37662306a36Sopenharmony_cistatic inline void
37762306a36Sopenharmony_ciptep_set_wrprotect(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
37862306a36Sopenharmony_ci{
37962306a36Sopenharmony_ci#ifdef CONFIG_SMP
38062306a36Sopenharmony_ci	unsigned long new, old;
38162306a36Sopenharmony_ci
38262306a36Sopenharmony_ci	do {
38362306a36Sopenharmony_ci		old = pte_val(*ptep);
38462306a36Sopenharmony_ci		new = pte_val(pte_wrprotect(__pte (old)));
38562306a36Sopenharmony_ci	} while (cmpxchg((unsigned long *) ptep, old, new) != old);
38662306a36Sopenharmony_ci#else
38762306a36Sopenharmony_ci	pte_t old_pte = *ptep;
38862306a36Sopenharmony_ci	set_pte_at(mm, addr, ptep, pte_wrprotect(old_pte));
38962306a36Sopenharmony_ci#endif
39062306a36Sopenharmony_ci}
39162306a36Sopenharmony_ci
39262306a36Sopenharmony_cistatic inline int
39362306a36Sopenharmony_cipte_same (pte_t a, pte_t b)
39462306a36Sopenharmony_ci{
39562306a36Sopenharmony_ci	return pte_val(a) == pte_val(b);
39662306a36Sopenharmony_ci}
39762306a36Sopenharmony_ci
39862306a36Sopenharmony_ci#define update_mmu_cache_range(vmf, vma, address, ptep, nr) do { } while (0)
39962306a36Sopenharmony_ci#define update_mmu_cache(vma, address, ptep) do { } while (0)
40062306a36Sopenharmony_ci
40162306a36Sopenharmony_ciextern pgd_t swapper_pg_dir[PTRS_PER_PGD];
40262306a36Sopenharmony_ciextern void paging_init (void);
40362306a36Sopenharmony_ci
40462306a36Sopenharmony_ci/*
40562306a36Sopenharmony_ci * Encode/decode swap entries and swap PTEs. Swap PTEs are all PTEs that
40662306a36Sopenharmony_ci * are !pte_none() && !pte_present().
40762306a36Sopenharmony_ci *
40862306a36Sopenharmony_ci * Note: The macros below rely on the fact that MAX_SWAPFILES_SHIFT <= number of
40962306a36Sopenharmony_ci *	 bits in the swap-type field of the swap pte.  It would be nice to
41062306a36Sopenharmony_ci *	 enforce that, but we can't easily include <linux/swap.h> here.
41162306a36Sopenharmony_ci *	 (Of course, better still would be to define MAX_SWAPFILES_SHIFT here...).
41262306a36Sopenharmony_ci *
41362306a36Sopenharmony_ci * Format of swap pte:
41462306a36Sopenharmony_ci *	bit   0   : present bit (must be zero)
41562306a36Sopenharmony_ci *	bits  1- 6: swap type
41662306a36Sopenharmony_ci *	bit   7   : exclusive marker
41762306a36Sopenharmony_ci *	bits  8-62: swap offset
41862306a36Sopenharmony_ci *	bit  63   : _PAGE_PROTNONE bit
41962306a36Sopenharmony_ci */
42062306a36Sopenharmony_ci#define __swp_type(entry)		(((entry).val >> 1) & 0x3f)
42162306a36Sopenharmony_ci#define __swp_offset(entry)		(((entry).val << 1) >> 9)
42262306a36Sopenharmony_ci#define __swp_entry(type, offset)	((swp_entry_t) { ((type & 0x3f) << 1) | \
42362306a36Sopenharmony_ci							 ((long) (offset) << 8) })
42462306a36Sopenharmony_ci#define __pte_to_swp_entry(pte)		((swp_entry_t) { pte_val(pte) })
42562306a36Sopenharmony_ci#define __swp_entry_to_pte(x)		((pte_t) { (x).val })
42662306a36Sopenharmony_ci
42762306a36Sopenharmony_cistatic inline int pte_swp_exclusive(pte_t pte)
42862306a36Sopenharmony_ci{
42962306a36Sopenharmony_ci	return pte_val(pte) & _PAGE_SWP_EXCLUSIVE;
43062306a36Sopenharmony_ci}
43162306a36Sopenharmony_ci
43262306a36Sopenharmony_cistatic inline pte_t pte_swp_mkexclusive(pte_t pte)
43362306a36Sopenharmony_ci{
43462306a36Sopenharmony_ci	pte_val(pte) |= _PAGE_SWP_EXCLUSIVE;
43562306a36Sopenharmony_ci	return pte;
43662306a36Sopenharmony_ci}
43762306a36Sopenharmony_ci
43862306a36Sopenharmony_cistatic inline pte_t pte_swp_clear_exclusive(pte_t pte)
43962306a36Sopenharmony_ci{
44062306a36Sopenharmony_ci	pte_val(pte) &= ~_PAGE_SWP_EXCLUSIVE;
44162306a36Sopenharmony_ci	return pte;
44262306a36Sopenharmony_ci}
44362306a36Sopenharmony_ci
44462306a36Sopenharmony_ci/*
44562306a36Sopenharmony_ci * ZERO_PAGE is a global shared page that is always zero: used
44662306a36Sopenharmony_ci * for zero-mapped memory areas etc..
44762306a36Sopenharmony_ci */
44862306a36Sopenharmony_ciextern unsigned long empty_zero_page[PAGE_SIZE/sizeof(unsigned long)];
44962306a36Sopenharmony_ciextern struct page *zero_page_memmap_ptr;
45062306a36Sopenharmony_ci#define ZERO_PAGE(vaddr) (zero_page_memmap_ptr)
45162306a36Sopenharmony_ci
45262306a36Sopenharmony_ci/* We provide our own get_unmapped_area to cope with VA holes for userland */
45362306a36Sopenharmony_ci#define HAVE_ARCH_UNMAPPED_AREA
45462306a36Sopenharmony_ci
45562306a36Sopenharmony_ci#ifdef CONFIG_HUGETLB_PAGE
45662306a36Sopenharmony_ci#define HUGETLB_PGDIR_SHIFT	(HPAGE_SHIFT + 2*(PAGE_SHIFT-3))
45762306a36Sopenharmony_ci#define HUGETLB_PGDIR_SIZE	(__IA64_UL(1) << HUGETLB_PGDIR_SHIFT)
45862306a36Sopenharmony_ci#define HUGETLB_PGDIR_MASK	(~(HUGETLB_PGDIR_SIZE-1))
45962306a36Sopenharmony_ci#endif
46062306a36Sopenharmony_ci
46162306a36Sopenharmony_ci
46262306a36Sopenharmony_ci#define __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS
46362306a36Sopenharmony_ci/*
46462306a36Sopenharmony_ci * Update PTEP with ENTRY, which is guaranteed to be a less
46562306a36Sopenharmony_ci * restrictive PTE.  That is, ENTRY may have the ACCESSED, DIRTY, and
46662306a36Sopenharmony_ci * WRITABLE bits turned on, when the value at PTEP did not.  The
46762306a36Sopenharmony_ci * WRITABLE bit may only be turned if SAFELY_WRITABLE is TRUE.
46862306a36Sopenharmony_ci *
46962306a36Sopenharmony_ci * SAFELY_WRITABLE is TRUE if we can update the value at PTEP without
47062306a36Sopenharmony_ci * having to worry about races.  On SMP machines, there are only two
47162306a36Sopenharmony_ci * cases where this is true:
47262306a36Sopenharmony_ci *
47362306a36Sopenharmony_ci *	(1) *PTEP has the PRESENT bit turned OFF
47462306a36Sopenharmony_ci *	(2) ENTRY has the DIRTY bit turned ON
47562306a36Sopenharmony_ci *
47662306a36Sopenharmony_ci * On ia64, we could implement this routine with a cmpxchg()-loop
47762306a36Sopenharmony_ci * which ORs in the _PAGE_A/_PAGE_D bit if they're set in ENTRY.
47862306a36Sopenharmony_ci * However, like on x86, we can get a more streamlined version by
47962306a36Sopenharmony_ci * observing that it is OK to drop ACCESSED bit updates when
48062306a36Sopenharmony_ci * SAFELY_WRITABLE is FALSE.  Besides being rare, all that would do is
48162306a36Sopenharmony_ci * result in an extra Access-bit fault, which would then turn on the
48262306a36Sopenharmony_ci * ACCESSED bit in the low-level fault handler (iaccess_bit or
48362306a36Sopenharmony_ci * daccess_bit in ivt.S).
48462306a36Sopenharmony_ci */
48562306a36Sopenharmony_ci#ifdef CONFIG_SMP
48662306a36Sopenharmony_ci# define ptep_set_access_flags(__vma, __addr, __ptep, __entry, __safely_writable) \
48762306a36Sopenharmony_ci({									\
48862306a36Sopenharmony_ci	int __changed = !pte_same(*(__ptep), __entry);			\
48962306a36Sopenharmony_ci	if (__changed && __safely_writable) {				\
49062306a36Sopenharmony_ci		set_pte(__ptep, __entry);				\
49162306a36Sopenharmony_ci		flush_tlb_page(__vma, __addr);				\
49262306a36Sopenharmony_ci	}								\
49362306a36Sopenharmony_ci	__changed;							\
49462306a36Sopenharmony_ci})
49562306a36Sopenharmony_ci#else
49662306a36Sopenharmony_ci# define ptep_set_access_flags(__vma, __addr, __ptep, __entry, __safely_writable) \
49762306a36Sopenharmony_ci({									\
49862306a36Sopenharmony_ci	int __changed = !pte_same(*(__ptep), __entry);			\
49962306a36Sopenharmony_ci	if (__changed) {						\
50062306a36Sopenharmony_ci		set_pte_at((__vma)->vm_mm, (__addr), __ptep, __entry);	\
50162306a36Sopenharmony_ci		flush_tlb_page(__vma, __addr);				\
50262306a36Sopenharmony_ci	}								\
50362306a36Sopenharmony_ci	__changed;							\
50462306a36Sopenharmony_ci})
50562306a36Sopenharmony_ci#endif
50662306a36Sopenharmony_ci# endif /* !__ASSEMBLY__ */
50762306a36Sopenharmony_ci
50862306a36Sopenharmony_ci/*
50962306a36Sopenharmony_ci * Identity-mapped regions use a large page size.  We'll call such large pages
51062306a36Sopenharmony_ci * "granules".  If you can think of a better name that's unambiguous, let me
51162306a36Sopenharmony_ci * know...
51262306a36Sopenharmony_ci */
51362306a36Sopenharmony_ci#if defined(CONFIG_IA64_GRANULE_64MB)
51462306a36Sopenharmony_ci# define IA64_GRANULE_SHIFT	_PAGE_SIZE_64M
51562306a36Sopenharmony_ci#elif defined(CONFIG_IA64_GRANULE_16MB)
51662306a36Sopenharmony_ci# define IA64_GRANULE_SHIFT	_PAGE_SIZE_16M
51762306a36Sopenharmony_ci#endif
51862306a36Sopenharmony_ci#define IA64_GRANULE_SIZE	(1 << IA64_GRANULE_SHIFT)
51962306a36Sopenharmony_ci/*
52062306a36Sopenharmony_ci * log2() of the page size we use to map the kernel image (IA64_TR_KERNEL):
52162306a36Sopenharmony_ci */
52262306a36Sopenharmony_ci#define KERNEL_TR_PAGE_SHIFT	_PAGE_SIZE_64M
52362306a36Sopenharmony_ci#define KERNEL_TR_PAGE_SIZE	(1 << KERNEL_TR_PAGE_SHIFT)
52462306a36Sopenharmony_ci
52562306a36Sopenharmony_ci/* These tell get_user_pages() that the first gate page is accessible from user-level.  */
52662306a36Sopenharmony_ci#define FIXADDR_USER_START	GATE_ADDR
52762306a36Sopenharmony_ci#ifdef HAVE_BUGGY_SEGREL
52862306a36Sopenharmony_ci# define FIXADDR_USER_END	(GATE_ADDR + 2*PAGE_SIZE)
52962306a36Sopenharmony_ci#else
53062306a36Sopenharmony_ci# define FIXADDR_USER_END	(GATE_ADDR + 2*PERCPU_PAGE_SIZE)
53162306a36Sopenharmony_ci#endif
53262306a36Sopenharmony_ci
53362306a36Sopenharmony_ci#define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
53462306a36Sopenharmony_ci#define __HAVE_ARCH_PTEP_GET_AND_CLEAR
53562306a36Sopenharmony_ci#define __HAVE_ARCH_PTEP_SET_WRPROTECT
53662306a36Sopenharmony_ci#define __HAVE_ARCH_PTE_SAME
53762306a36Sopenharmony_ci#define __HAVE_ARCH_PGD_OFFSET_GATE
53862306a36Sopenharmony_ci
53962306a36Sopenharmony_ci
54062306a36Sopenharmony_ci#if CONFIG_PGTABLE_LEVELS == 3
54162306a36Sopenharmony_ci#include <asm-generic/pgtable-nopud.h>
54262306a36Sopenharmony_ci#endif
54362306a36Sopenharmony_ci#include <asm-generic/pgtable-nop4d.h>
54462306a36Sopenharmony_ci
54562306a36Sopenharmony_ci#endif /* _ASM_IA64_PGTABLE_H */
546