Path: blob/master/arch/powerpc/mm/ptdump/hashpagetable.c
26481 views
// SPDX-License-Identifier: GPL-2.0-only1/*2* Copyright 2016, Rashmica Gupta, IBM Corp.3*4* This traverses the kernel virtual memory and dumps the pages that are in5* the hash pagetable, along with their flags to6* /sys/kernel/debug/kernel_hash_pagetable.7*8* If radix is enabled then there is no hash page table and so no debugfs file9* is generated.10*/11#include <linux/debugfs.h>12#include <linux/fs.h>13#include <linux/io.h>14#include <linux/mm.h>15#include <linux/sched.h>16#include <linux/seq_file.h>17#include <linux/const.h>18#include <asm/page.h>19#include <asm/plpar_wrappers.h>20#include <linux/memblock.h>21#include <asm/firmware.h>22#include <asm/pgalloc.h>2324struct pg_state {25struct seq_file *seq;26const struct addr_marker *marker;27unsigned long start_address;28unsigned int level;29u64 current_flags;30};3132struct addr_marker {33unsigned long start_address;34const char *name;35};3637static struct addr_marker address_markers[] = {38{ 0, "Start of kernel VM" },39{ 0, "vmalloc() Area" },40{ 0, "vmalloc() End" },41{ 0, "isa I/O start" },42{ 0, "isa I/O end" },43{ 0, "phb I/O start" },44{ 0, "phb I/O end" },45{ 0, "I/O remap start" },46{ 0, "I/O remap end" },47{ 0, "vmemmap start" },48{ -1, NULL },49};5051struct flag_info {52u64 mask;53u64 val;54const char *set;55const char *clear;56bool is_val;57int shift;58};5960static const struct flag_info v_flag_array[] = {61{62.mask = SLB_VSID_B,63.val = SLB_VSID_B_256M,64.set = "ssize: 256M",65.clear = "ssize: 1T ",66}, {67.mask = HPTE_V_SECONDARY,68.val = HPTE_V_SECONDARY,69.set = "secondary",70.clear = "primary ",71}, {72.mask = HPTE_V_VALID,73.val = HPTE_V_VALID,74.set = "valid ",75.clear = "invalid",76}, {77.mask = HPTE_V_BOLTED,78.val = HPTE_V_BOLTED,79.set = "bolted",80.clear = "",81}82};8384static const struct flag_info r_flag_array[] = {85{86.mask = HPTE_R_PP0 | HPTE_R_PP,87.val = PP_RWXX,88.set = "prot:RW--",89}, {90.mask = HPTE_R_PP0 | HPTE_R_PP,91.val = PP_RWRX,92.set = "prot:RWR-",93}, {94.mask = HPTE_R_PP0 | HPTE_R_PP,95.val = PP_RWRW,96.set = "prot:RWRW",97}, {98.mask = HPTE_R_PP0 | HPTE_R_PP,99.val = PP_RXRX,100.set = "prot:R-R-",101}, {102.mask = HPTE_R_PP0 | HPTE_R_PP,103.val = PP_RXXX,104.set = "prot:R---",105}, {106.mask = HPTE_R_KEY_HI | HPTE_R_KEY_LO,107.val = HPTE_R_KEY_HI | HPTE_R_KEY_LO,108.set = "key",109.clear = "",110.is_val = true,111}, {112.mask = HPTE_R_R,113.val = HPTE_R_R,114.set = "ref",115.clear = " ",116}, {117.mask = HPTE_R_C,118.val = HPTE_R_C,119.set = "changed",120.clear = " ",121}, {122.mask = HPTE_R_N,123.val = HPTE_R_N,124.set = "no execute",125}, {126.mask = HPTE_R_WIMG,127.val = HPTE_R_W,128.set = "writethru",129}, {130.mask = HPTE_R_WIMG,131.val = HPTE_R_I,132.set = "no cache",133}, {134.mask = HPTE_R_WIMG,135.val = HPTE_R_G,136.set = "guarded",137}138};139140static int calculate_pagesize(struct pg_state *st, int ps, char s[])141{142static const char units[] = "BKMGTPE";143const char *unit = units;144145while (ps > 9 && unit[1]) {146ps -= 10;147unit++;148}149seq_printf(st->seq, " %s_ps: %i%c\t", s, 1<<ps, *unit);150return ps;151}152153static void dump_flag_info(struct pg_state *st, const struct flag_info154*flag, u64 pte, int num)155{156unsigned int i;157158for (i = 0; i < num; i++, flag++) {159const char *s = NULL;160u64 val;161162/* flag not defined so don't check it */163if (flag->mask == 0)164continue;165/* Some 'flags' are actually values */166if (flag->is_val) {167val = pte & flag->val;168if (flag->shift)169val = val >> flag->shift;170seq_printf(st->seq, " %s:%llx", flag->set, val);171} else {172if ((pte & flag->mask) == flag->val)173s = flag->set;174else175s = flag->clear;176if (s)177seq_printf(st->seq, " %s", s);178}179}180}181182static void dump_hpte_info(struct pg_state *st, unsigned long ea, u64 v, u64 r,183unsigned long rpn, int bps, int aps, unsigned long lp)184{185int aps_index;186187while (ea >= st->marker[1].start_address) {188st->marker++;189seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);190}191seq_printf(st->seq, "0x%lx:\t", ea);192seq_printf(st->seq, "AVPN:%llx\t", HPTE_V_AVPN_VAL(v));193dump_flag_info(st, v_flag_array, v, ARRAY_SIZE(v_flag_array));194seq_printf(st->seq, " rpn: %lx\t", rpn);195dump_flag_info(st, r_flag_array, r, ARRAY_SIZE(r_flag_array));196197calculate_pagesize(st, bps, "base");198aps_index = calculate_pagesize(st, aps, "actual");199if (aps_index != 2)200seq_printf(st->seq, "LP enc: %lx", lp);201seq_putc(st->seq, '\n');202}203204205static int native_find(unsigned long ea, int psize, bool primary, u64 *v, u64206*r)207{208struct hash_pte *hptep;209unsigned long hash, vsid, vpn, hpte_group, want_v, hpte_v;210int i, ssize = mmu_kernel_ssize;211unsigned long shift = mmu_psize_defs[psize].shift;212213/* calculate hash */214vsid = get_kernel_vsid(ea, ssize);215vpn = hpt_vpn(ea, vsid, ssize);216hash = hpt_hash(vpn, shift, ssize);217want_v = hpte_encode_avpn(vpn, psize, ssize);218219/* to check in the secondary hash table, we invert the hash */220if (!primary)221hash = ~hash;222hpte_group = (hash & htab_hash_mask) * HPTES_PER_GROUP;223for (i = 0; i < HPTES_PER_GROUP; i++) {224hptep = htab_address + hpte_group;225hpte_v = be64_to_cpu(hptep->v);226227if (HPTE_V_COMPARE(hpte_v, want_v) && (hpte_v & HPTE_V_VALID)) {228/* HPTE matches */229*v = be64_to_cpu(hptep->v);230*r = be64_to_cpu(hptep->r);231return 0;232}233++hpte_group;234}235return -1;236}237238static int pseries_find(unsigned long ea, int psize, bool primary, u64 *v, u64 *r)239{240struct {241unsigned long v;242unsigned long r;243} ptes[4];244unsigned long vsid, vpn, hash, hpte_group, want_v;245int i, j, ssize = mmu_kernel_ssize;246long lpar_rc = 0;247unsigned long shift = mmu_psize_defs[psize].shift;248249/* calculate hash */250vsid = get_kernel_vsid(ea, ssize);251vpn = hpt_vpn(ea, vsid, ssize);252hash = hpt_hash(vpn, shift, ssize);253want_v = hpte_encode_avpn(vpn, psize, ssize);254255/* to check in the secondary hash table, we invert the hash */256if (!primary)257hash = ~hash;258hpte_group = (hash & htab_hash_mask) * HPTES_PER_GROUP;259/* see if we can find an entry in the hpte with this hash */260for (i = 0; i < HPTES_PER_GROUP; i += 4, hpte_group += 4) {261lpar_rc = plpar_pte_read_4(0, hpte_group, (void *)ptes);262263if (lpar_rc)264continue;265for (j = 0; j < 4; j++) {266if (HPTE_V_COMPARE(ptes[j].v, want_v) &&267(ptes[j].v & HPTE_V_VALID)) {268/* HPTE matches */269*v = ptes[j].v;270*r = ptes[j].r;271return 0;272}273}274}275return -1;276}277278static void decode_r(int bps, unsigned long r, unsigned long *rpn, int *aps,279unsigned long *lp_bits)280{281struct mmu_psize_def entry;282unsigned long arpn, mask, lp;283int penc = -2, idx = 0, shift;284285/*.286* The LP field has 8 bits. Depending on the actual page size, some of287* these bits are concatenated with the APRN to get the RPN. The rest288* of the bits in the LP field is the LP value and is an encoding for289* the base page size and the actual page size.290*291* - find the mmu entry for our base page size292* - go through all page encodings and use the associated mask to293* find an encoding that matches our encoding in the LP field.294*/295arpn = (r & HPTE_R_RPN) >> HPTE_R_RPN_SHIFT;296lp = arpn & 0xff;297298entry = mmu_psize_defs[bps];299while (idx < MMU_PAGE_COUNT) {300penc = entry.penc[idx];301if ((penc != -1) && (mmu_psize_defs[idx].shift)) {302shift = mmu_psize_defs[idx].shift - HPTE_R_RPN_SHIFT;303mask = (0x1 << (shift)) - 1;304if ((lp & mask) == penc) {305*aps = mmu_psize_to_shift(idx);306*lp_bits = lp & mask;307*rpn = arpn >> shift;308return;309}310}311idx++;312}313}314315static int base_hpte_find(unsigned long ea, int psize, bool primary, u64 *v,316u64 *r)317{318if (IS_ENABLED(CONFIG_PPC_PSERIES) && firmware_has_feature(FW_FEATURE_LPAR))319return pseries_find(ea, psize, primary, v, r);320321return native_find(ea, psize, primary, v, r);322}323324static unsigned long hpte_find(struct pg_state *st, unsigned long ea, int psize)325{326unsigned long slot;327u64 v = 0, r = 0;328unsigned long rpn, lp_bits;329int base_psize = 0, actual_psize = 0;330331if (ea < PAGE_OFFSET)332return -1;333334/* Look in primary table */335slot = base_hpte_find(ea, psize, true, &v, &r);336337/* Look in secondary table */338if (slot == -1)339slot = base_hpte_find(ea, psize, false, &v, &r);340341/* No entry found */342if (slot == -1)343return -1;344345/*346* We found an entry in the hash page table:347* - check that this has the same base page348* - find the actual page size349* - find the RPN350*/351base_psize = mmu_psize_to_shift(psize);352353if ((v & HPTE_V_LARGE) == HPTE_V_LARGE) {354decode_r(psize, r, &rpn, &actual_psize, &lp_bits);355} else {356/* 4K actual page size */357actual_psize = 12;358rpn = (r & HPTE_R_RPN) >> HPTE_R_RPN_SHIFT;359/* In this case there are no LP bits */360lp_bits = -1;361}362/*363* We didn't find a matching encoding, so the PTE we found isn't for364* this address.365*/366if (actual_psize == -1)367return -1;368369dump_hpte_info(st, ea, v, r, rpn, base_psize, actual_psize, lp_bits);370return 0;371}372373static void walk_pte(struct pg_state *st, pmd_t *pmd, unsigned long start)374{375pte_t *pte = pte_offset_kernel(pmd, 0);376unsigned long addr, pteval, psize;377int i, status;378379for (i = 0; i < PTRS_PER_PTE; i++, pte++) {380addr = start + i * PAGE_SIZE;381pteval = pte_val(*pte);382383if (addr < VMALLOC_END)384psize = mmu_vmalloc_psize;385else386psize = mmu_io_psize;387388/* check for secret 4K mappings */389if (IS_ENABLED(CONFIG_PPC_64K_PAGES) &&390((pteval & H_PAGE_COMBO) == H_PAGE_COMBO ||391(pteval & H_PAGE_4K_PFN) == H_PAGE_4K_PFN))392psize = mmu_io_psize;393394/* check for hashpte */395status = hpte_find(st, addr, psize);396397if (((pteval & H_PAGE_HASHPTE) != H_PAGE_HASHPTE)398&& (status != -1)) {399/* found a hpte that is not in the linux page tables */400seq_printf(st->seq, "page probably bolted before linux"401" pagetables were set: addr:%lx, pteval:%lx\n",402addr, pteval);403}404}405}406407static void walk_pmd(struct pg_state *st, pud_t *pud, unsigned long start)408{409pmd_t *pmd = pmd_offset(pud, 0);410unsigned long addr;411unsigned int i;412413for (i = 0; i < PTRS_PER_PMD; i++, pmd++) {414addr = start + i * PMD_SIZE;415if (!pmd_none(*pmd))416/* pmd exists */417walk_pte(st, pmd, addr);418}419}420421static void walk_pud(struct pg_state *st, p4d_t *p4d, unsigned long start)422{423pud_t *pud = pud_offset(p4d, 0);424unsigned long addr;425unsigned int i;426427for (i = 0; i < PTRS_PER_PUD; i++, pud++) {428addr = start + i * PUD_SIZE;429if (!pud_none(*pud))430/* pud exists */431walk_pmd(st, pud, addr);432}433}434435static void walk_p4d(struct pg_state *st, pgd_t *pgd, unsigned long start)436{437p4d_t *p4d = p4d_offset(pgd, 0);438unsigned long addr;439unsigned int i;440441for (i = 0; i < PTRS_PER_P4D; i++, p4d++) {442addr = start + i * P4D_SIZE;443if (!p4d_none(*p4d))444/* p4d exists */445walk_pud(st, p4d, addr);446}447}448449static void walk_pagetables(struct pg_state *st)450{451pgd_t *pgd = pgd_offset_k(0UL);452unsigned int i;453unsigned long addr;454455/*456* Traverse the linux pagetable structure and dump pages that are in457* the hash pagetable.458*/459for (i = 0; i < PTRS_PER_PGD; i++, pgd++) {460addr = KERN_VIRT_START + i * PGDIR_SIZE;461if (!pgd_none(*pgd))462/* pgd exists */463walk_p4d(st, pgd, addr);464}465}466467468static void walk_linearmapping(struct pg_state *st)469{470unsigned long addr;471472/*473* Traverse the linear mapping section of virtual memory and dump pages474* that are in the hash pagetable.475*/476unsigned long psize = 1 << mmu_psize_defs[mmu_linear_psize].shift;477478for (addr = PAGE_OFFSET; addr < PAGE_OFFSET +479memblock_end_of_DRAM(); addr += psize)480hpte_find(st, addr, mmu_linear_psize);481}482483static void walk_vmemmap(struct pg_state *st)484{485struct vmemmap_backing *ptr = vmemmap_list;486487if (!IS_ENABLED(CONFIG_SPARSEMEM_VMEMMAP))488return;489/*490* Traverse the vmemmaped memory and dump pages that are in the hash491* pagetable.492*/493while (ptr) {494hpte_find(st, ptr->virt_addr, mmu_vmemmap_psize);495ptr = ptr->list;496}497seq_puts(st->seq, "---[ vmemmap end ]---\n");498}499500static void populate_markers(void)501{502address_markers[0].start_address = PAGE_OFFSET;503address_markers[1].start_address = VMALLOC_START;504address_markers[2].start_address = VMALLOC_END;505address_markers[3].start_address = ISA_IO_BASE;506address_markers[4].start_address = ISA_IO_END;507address_markers[5].start_address = PHB_IO_BASE;508address_markers[6].start_address = PHB_IO_END;509address_markers[7].start_address = IOREMAP_BASE;510address_markers[8].start_address = IOREMAP_END;511address_markers[9].start_address = H_VMEMMAP_START;512}513514static int ptdump_show(struct seq_file *m, void *v)515{516struct pg_state st = {517.seq = m,518.start_address = PAGE_OFFSET,519.marker = address_markers,520};521/*522* Traverse the 0xc, 0xd and 0xf areas of the kernel virtual memory and523* dump pages that are in the hash pagetable.524*/525walk_linearmapping(&st);526walk_pagetables(&st);527walk_vmemmap(&st);528return 0;529}530531DEFINE_SHOW_ATTRIBUTE(ptdump);532533static int ptdump_init(void)534{535if (!radix_enabled()) {536populate_markers();537debugfs_create_file("kernel_hash_pagetable", 0400, NULL, NULL,538&ptdump_fops);539}540return 0;541}542device_initcall(ptdump_init);543544545