aboutsummaryrefslogtreecommitdiff
path: root/lib/libkvm/kvm_minidump_powerpc64_hpt.c
blob: baa2fd1ebc08b796784f08fb717c4b7aa23a7a1a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
/*-
 * Copyright (c) 2006 Peter Wemm
 * Copyright (c) 2019 Leandro Lupori
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * From: FreeBSD: src/lib/libkvm/kvm_minidump_riscv.c
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <vm/vm.h>

#include <kvm.h>

#include <limits.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "../../sys/powerpc/include/minidump.h"
#include "kvm_private.h"
#include "kvm_powerpc64.h"

/*
 * PowerPC64 HPT machine dependent routines for kvm and minidumps.
 *
 * Address Translation parameters:
 *
 * b = 12 (SLB base page size: 4 KB)
 * b = 24 (SLB base page size: 16 MB)
 * p = 12 (page size: 4 KB)
 * p = 24 (page size: 16 MB)
 * s = 28 (segment size: 256 MB)
 */

/* Large (huge) page params */
#define	LP_PAGE_SHIFT		24
#define	LP_PAGE_SIZE		(1ULL << LP_PAGE_SHIFT)
#define	LP_PAGE_MASK		0x00ffffffULL

/* SLB */

#define	SEGMENT_LENGTH		0x10000000ULL

#define	round_seg(x)		roundup2((uint64_t)(x), SEGMENT_LENGTH)

/* Virtual real-mode VSID in LPARs */
#define	VSID_VRMA		0x1ffffffULL

#define	SLBV_L			0x0000000000000100ULL /* Large page selector */
#define	SLBV_CLASS		0x0000000000000080ULL /* Class selector */
#define	SLBV_LP_MASK		0x0000000000000030ULL
#define	SLBV_VSID_MASK		0x3ffffffffffff000ULL /* Virtual SegID mask */
#define	SLBV_VSID_SHIFT		12

#define	SLBE_B_MASK		0x0000000006000000ULL
#define	SLBE_B_256MB		0x0000000000000000ULL
#define	SLBE_VALID		0x0000000008000000ULL /* SLB entry valid */
#define	SLBE_INDEX_MASK		0x0000000000000fffULL /* SLB index mask */
#define	SLBE_ESID_MASK		0xfffffffff0000000ULL /* Effective SegID mask */
#define	SLBE_ESID_SHIFT		28

/* PTE */

#define	LPTEH_VSID_SHIFT	12
#define	LPTEH_AVPN_MASK		0xffffffffffffff80ULL
#define	LPTEH_B_MASK		0xc000000000000000ULL
#define	LPTEH_B_256MB		0x0000000000000000ULL
#define	LPTEH_BIG		0x0000000000000004ULL	/* 4KB/16MB page */
#define	LPTEH_HID		0x0000000000000002ULL
#define	LPTEH_VALID		0x0000000000000001ULL

#define	LPTEL_RPGN		0xfffffffffffff000ULL
#define	LPTEL_LP_MASK		0x00000000000ff000ULL
#define	LPTEL_NOEXEC		0x0000000000000004ULL

/* Supervisor        (U: RW, S: RW) */
#define	LPTEL_BW		0x0000000000000002ULL

/* Both Read Only    (U: RO, S: RO) */
#define	LPTEL_BR		0x0000000000000003ULL

#define	LPTEL_RW		LPTEL_BW
#define	LPTEL_RO		LPTEL_BR

/*
 * PTE AVA field manipulation macros.
 *
 * AVA[0:54] = PTEH[2:56]
 * AVA[VSID] = AVA[0:49] = PTEH[2:51]
 * AVA[PAGE] = AVA[50:54] = PTEH[52:56]
 */
#define	PTEH_AVA_VSID_MASK	0x3ffffffffffff000UL
#define	PTEH_AVA_VSID_SHIFT	12
#define	PTEH_AVA_VSID(p) \
	(((p) & PTEH_AVA_VSID_MASK) >> PTEH_AVA_VSID_SHIFT)

#define	PTEH_AVA_PAGE_MASK	0x0000000000000f80UL
#define	PTEH_AVA_PAGE_SHIFT	7
#define	PTEH_AVA_PAGE(p) \
	(((p) & PTEH_AVA_PAGE_MASK) >> PTEH_AVA_PAGE_SHIFT)

/* Masks to obtain the Physical Address from PTE low 64-bit word. */
#define	PTEL_PA_MASK		0x0ffffffffffff000UL
#define	PTEL_LP_PA_MASK		0x0fffffffff000000UL

#define	PTE_HASH_MASK		0x0000007fffffffffUL

/*
 * Number of AVA/VA page bits to shift right, in order to leave only the
 * ones that should be considered.
 *
 * q = MIN(54, 77-b) (PowerISA v2.07B, 5.7.7.3)
 * n = q + 1 - 50 (VSID size in bits)
 * s(ava) = 5 - n
 * s(va) = (28 - b) - n
 *
 * q: bit number of lower limit of VA/AVA bits to compare
 * n: number of AVA/VA page bits to compare
 * s: shift amount
 * 28 - b: VA page size in bits
 */
#define	AVA_PAGE_SHIFT(b)	(5 - (MIN(54, 77-(b)) + 1 - 50))
#define	VA_PAGE_SHIFT(b)	(28 - (b) - (MIN(54, 77-(b)) + 1 - 50))

/* Kernel ESID -> VSID mapping */
#define	KERNEL_VSID_BIT	0x0000001000000000UL /* Bit set in all kernel VSIDs */
#define	KERNEL_VSID(esid) ((((((uint64_t)esid << 8) | ((uint64_t)esid >> 28)) \
				* 0x13bbUL) & (KERNEL_VSID_BIT - 1)) | \
				KERNEL_VSID_BIT)

/* Types */

typedef uint64_t	ppc64_physaddr_t;

typedef struct {
	uint64_t slbv;
	uint64_t slbe;
} ppc64_slb_entry_t;

typedef struct {
	uint64_t pte_hi;
	uint64_t pte_lo;
} ppc64_pt_entry_t;

struct hpt_data {
	ppc64_slb_entry_t *slbs;
	uint32_t slbsize;
};


static void
slb_fill(ppc64_slb_entry_t *slb, uint64_t ea, uint64_t i)
{
	uint64_t esid;

	esid = ea >> SLBE_ESID_SHIFT;
	slb->slbv = KERNEL_VSID(esid) << SLBV_VSID_SHIFT;
	slb->slbe = (esid << SLBE_ESID_SHIFT) | SLBE_VALID | i;
}

static int
slb_init(kvm_t *kd)
{
	struct minidumphdr *hdr;
	struct hpt_data *data;
	ppc64_slb_entry_t *slb;
	uint32_t slbsize;
	uint64_t ea, i, maxmem;

	hdr = &kd->vmst->hdr;
	data = PPC64_MMU_DATA(kd);

	/* Alloc SLBs */
	maxmem = hdr->bitmapsize * 8 * PPC64_PAGE_SIZE;
	slbsize = round_seg(hdr->kernend + 1 - hdr->kernbase + maxmem) /
	    SEGMENT_LENGTH * sizeof(ppc64_slb_entry_t);
	data->slbs = _kvm_malloc(kd, slbsize);
	if (data->slbs == NULL) {
		_kvm_err(kd, kd->program, "cannot allocate slbs");
		return (-1);
	}
	data->slbsize = slbsize;

	dprintf("%s: maxmem=0x%jx, segs=%jd, slbsize=0x%jx\n",
	    __func__, (uintmax_t)maxmem,
	    (uintmax_t)slbsize / sizeof(ppc64_slb_entry_t), (uintmax_t)slbsize);

	/*
	 * Generate needed SLB entries.
	 *
	 * When translating addresses from EA to VA to PA, the needed SLB
	 * entry could be generated on the fly, but this is not the case
	 * for the walk_pages method, that needs to search the SLB entry
	 * by VSID, in order to find out the EA from a PTE.
	 */

	/* VM area */
	for (ea = hdr->kernbase, i = 0, slb = data->slbs;
	    ea < hdr->kernend; ea += SEGMENT_LENGTH, i++, slb++)
		slb_fill(slb, ea, i);

	/* DMAP area */
	for (ea = hdr->dmapbase;
	    ea < MIN(hdr->dmapend, hdr->dmapbase + maxmem);
	    ea += SEGMENT_LENGTH, i++, slb++) {
		slb_fill(slb, ea, i);
		if (hdr->hw_direct_map)
			slb->slbv |= SLBV_L;
	}

	return (0);
}

static void
ppc64mmu_hpt_cleanup(kvm_t *kd)
{
	struct hpt_data *data;

	if (kd->vmst == NULL)
		return;

	data = PPC64_MMU_DATA(kd);
	free(data->slbs);
	free(data);
	PPC64_MMU_DATA(kd) = NULL;
}

static int
ppc64mmu_hpt_init(kvm_t *kd)
{
	struct hpt_data *data;
	struct minidumphdr *hdr;

	hdr = &kd->vmst->hdr;

	/* Alloc MMU data */
	data = _kvm_malloc(kd, sizeof(*data));
	if (data == NULL) {
		_kvm_err(kd, kd->program, "cannot allocate MMU data");
		return (-1);
	}
	data->slbs = NULL;
	PPC64_MMU_DATA(kd) = data;

	if (slb_init(kd) == -1)
		goto failed;

	return (0);

failed:
	ppc64mmu_hpt_cleanup(kd);
	return (-1);
}

static ppc64_slb_entry_t *
slb_search(kvm_t *kd, kvaddr_t ea)
{
	struct hpt_data *data;
	ppc64_slb_entry_t *slb;
	int i, n;

	data = PPC64_MMU_DATA(kd);
	slb = data->slbs;
	n = data->slbsize / sizeof(ppc64_slb_entry_t);

	/* SLB search */
	for (i = 0; i < n; i++, slb++) {
		if ((slb->slbe & SLBE_VALID) == 0)
			continue;

		/* Compare 36-bit ESID of EA with segment one (64-s) */
		if ((slb->slbe & SLBE_ESID_MASK) != (ea & SLBE_ESID_MASK))
			continue;

		/* Match found */
		dprintf("SEG#%02d: slbv=0x%016jx, slbe=0x%016jx\n",
		    i, (uintmax_t)slb->slbv, (uintmax_t)slb->slbe);
		break;
	}

	/* SLB not found */
	if (i == n) {
		_kvm_err(kd, kd->program, "%s: segment not found for EA 0x%jx",
		    __func__, (uintmax_t)ea);
		return (NULL);
	}
	return (slb);
}

static ppc64_pt_entry_t
pte_get(kvm_t *kd, u_long ptex)
{
	ppc64_pt_entry_t pte, *p;

	p = _kvm_pmap_get(kd, ptex, sizeof(pte));
	pte.pte_hi = be64toh(p->pte_hi);
	pte.pte_lo = be64toh(p->pte_lo);
	return (pte);
}

static int
pte_search(kvm_t *kd, ppc64_slb_entry_t *slb, uint64_t hid, kvaddr_t ea,
    ppc64_pt_entry_t *p)
{
	uint64_t hash, hmask;
	uint64_t pteg, ptex;
	uint64_t va_vsid, va_page;
	int b;
	int ava_pg_shift, va_pg_shift;
	ppc64_pt_entry_t pte;

	/*
	 * Get VA:
	 *
	 * va(78) = va_vsid(50) || va_page(s-b) || offset(b)
	 *
	 * va_vsid: 50-bit VSID (78-s)
	 * va_page: (s-b)-bit VA page
	 */
	b = slb->slbv & SLBV_L? LP_PAGE_SHIFT : PPC64_PAGE_SHIFT;
	va_vsid = (slb->slbv & SLBV_VSID_MASK) >> SLBV_VSID_SHIFT;
	va_page = (ea & ~SLBE_ESID_MASK) >> b;

	dprintf("%s: hid=0x%jx, ea=0x%016jx, b=%d, va_vsid=0x%010jx, "
	    "va_page=0x%04jx\n",
	    __func__, (uintmax_t)hid, (uintmax_t)ea, b,
	    (uintmax_t)va_vsid, (uintmax_t)va_page);

	/*
	 * Get hash:
	 *
	 * Primary hash: va_vsid(11:49) ^ va_page(s-b)
	 * Secondary hash: ~primary_hash
	 */
	hash = (va_vsid & PTE_HASH_MASK) ^ va_page;
	if (hid)
		hash = ~hash & PTE_HASH_MASK;

	/*
	 * Get PTEG:
	 *
	 * pteg = (hash(0:38) & hmask) << 3
	 *
	 * hmask (hash mask): mask generated from HTABSIZE || 11*0b1
	 * hmask = number_of_ptegs - 1
	 */
	hmask = kd->vmst->hdr.pmapsize / (8 * sizeof(ppc64_pt_entry_t)) - 1;
	pteg = (hash & hmask) << 3;

	ava_pg_shift = AVA_PAGE_SHIFT(b);
	va_pg_shift = VA_PAGE_SHIFT(b);

	dprintf("%s: hash=0x%010jx, hmask=0x%010jx, (hash & hmask)=0x%010jx, "
	    "pteg=0x%011jx, ava_pg_shift=%d, va_pg_shift=%d\n",
	    __func__, (uintmax_t)hash, (uintmax_t)hmask,
	    (uintmax_t)(hash & hmask), (uintmax_t)pteg,
	    ava_pg_shift, va_pg_shift);

	/* Search PTEG */
	for (ptex = pteg; ptex < pteg + 8; ptex++) {
		pte = pte_get(kd, ptex);

		/* Check H, V and B */
		if ((pte.pte_hi & LPTEH_HID) != hid ||
		    (pte.pte_hi & LPTEH_VALID) == 0 ||
		    (pte.pte_hi & LPTEH_B_MASK) != LPTEH_B_256MB)
			continue;

		/* Compare AVA with VA */
		if (PTEH_AVA_VSID(pte.pte_hi) != va_vsid ||
		    (PTEH_AVA_PAGE(pte.pte_hi) >> ava_pg_shift) !=
		    (va_page >> va_pg_shift))
			continue;

		/*
		 * Check if PTE[L] matches SLBV[L].
		 *
		 * Note: this check ignores PTE[LP], as does the kernel.
		 */
		if (b == PPC64_PAGE_SHIFT) {
			if (pte.pte_hi & LPTEH_BIG)
				continue;
		} else if ((pte.pte_hi & LPTEH_BIG) == 0)
			continue;

		/* Match found */
		dprintf("%s: PTE found: ptex=0x%jx, pteh=0x%016jx, "
		    "ptel=0x%016jx\n",
		    __func__, (uintmax_t)ptex, (uintmax_t)pte.pte_hi,
		    (uintmax_t)pte.pte_lo);
		break;
	}

	/* Not found? */
	if (ptex == pteg + 8) {
		/* Try secondary hash */
		if (hid == 0)
			return (pte_search(kd, slb, LPTEH_HID, ea, p));
		else {
			_kvm_err(kd, kd->program,
			    "%s: pte not found", __func__);
			return (-1);
		}
	}

	/* PTE found */
	*p = pte;
	return (0);
}

static int
pte_lookup(kvm_t *kd, kvaddr_t ea, ppc64_pt_entry_t *pte)
{
	ppc64_slb_entry_t *slb;

	/* First, find SLB */
	if ((slb = slb_search(kd, ea)) == NULL)
		return (-1);

	/* Next, find PTE */
	return (pte_search(kd, slb, 0, ea, pte));
}

static int
ppc64mmu_hpt_kvatop(kvm_t *kd, kvaddr_t va, off_t *pa)
{
	struct minidumphdr *hdr;
	struct vmstate *vm;
	ppc64_pt_entry_t pte;
	ppc64_physaddr_t pgoff, pgpa;
	off_t ptoff;
	int err;

	vm = kd->vmst;
	hdr = &vm->hdr;
	pgoff = va & PPC64_PAGE_MASK;

	dprintf("%s: va=0x%016jx\n", __func__, (uintmax_t)va);

	/*
	 * A common use case of libkvm is to first find a symbol address
	 * from the kernel image and then use kvatop to translate it and
	 * to be able to fetch its corresponding data.
	 *
	 * The problem is that, in PowerPC64 case, the addresses of relocated
	 * data won't match those in the kernel image. This is handled here by
	 * adding the relocation offset to those addresses.
	 */
	if (va < hdr->dmapbase)
		va += hdr->startkernel - PPC64_KERNBASE;

	/* Handle DMAP */
	if (va >= hdr->dmapbase && va <= hdr->dmapend) {
		pgpa = (va & ~hdr->dmapbase) & ~PPC64_PAGE_MASK;
		ptoff = _kvm_pt_find(kd, pgpa, PPC64_PAGE_SIZE);
		if (ptoff == -1) {
			_kvm_err(kd, kd->program, "%s: "
			    "direct map address 0x%jx not in minidump",
			    __func__, (uintmax_t)va);
			goto invalid;
		}
		*pa = ptoff + pgoff;
		return (PPC64_PAGE_SIZE - pgoff);
	/* Translate VA to PA */
	} else if (va >= hdr->kernbase) {
		if ((err = pte_lookup(kd, va, &pte)) == -1) {
			_kvm_err(kd, kd->program,
			    "%s: pte not valid", __func__);
			goto invalid;
		}

		if (pte.pte_hi & LPTEH_BIG)
			pgpa = (pte.pte_lo & PTEL_LP_PA_MASK) |
			    (va & ~PPC64_PAGE_MASK & LP_PAGE_MASK);
		else
			pgpa = pte.pte_lo & PTEL_PA_MASK;
		dprintf("%s: pgpa=0x%016jx\n", __func__, (uintmax_t)pgpa);

		ptoff = _kvm_pt_find(kd, pgpa, PPC64_PAGE_SIZE);
		if (ptoff == -1) {
			_kvm_err(kd, kd->program, "%s: "
			    "physical address 0x%jx not in minidump",
			    __func__, (uintmax_t)pgpa);
			goto invalid;
		}
		*pa = ptoff + pgoff;
		return (PPC64_PAGE_SIZE - pgoff);
	} else {
		_kvm_err(kd, kd->program,
		    "%s: virtual address 0x%jx not minidumped",
		    __func__, (uintmax_t)va);
		goto invalid;
	}

invalid:
	_kvm_err(kd, 0, "invalid address (0x%jx)", (uintmax_t)va);
	return (0);
}

static vm_prot_t
entry_to_prot(ppc64_pt_entry_t *pte)
{
	vm_prot_t prot = VM_PROT_READ;

	if (pte->pte_lo & LPTEL_RW)
		prot |= VM_PROT_WRITE;
	if ((pte->pte_lo & LPTEL_NOEXEC) != 0)
		prot |= VM_PROT_EXECUTE;
	return (prot);
}

static ppc64_slb_entry_t *
slb_vsid_search(kvm_t *kd, uint64_t vsid)
{
	struct hpt_data *data;
	ppc64_slb_entry_t *slb;
	int i, n;

	data = PPC64_MMU_DATA(kd);
	slb = data->slbs;
	n = data->slbsize / sizeof(ppc64_slb_entry_t);
	vsid <<= SLBV_VSID_SHIFT;

	/* SLB search */
	for (i = 0; i < n; i++, slb++) {
		/* Check if valid and compare VSID */
		if ((slb->slbe & SLBE_VALID) &&
		    (slb->slbv & SLBV_VSID_MASK) == vsid)
			break;
	}

	/* SLB not found */
	if (i == n) {
		_kvm_err(kd, kd->program,
		    "%s: segment not found for VSID 0x%jx",
		    __func__, (uintmax_t)vsid >> SLBV_VSID_SHIFT);
		return (NULL);
	}
	return (slb);
}

static u_long
get_ea(kvm_t *kd, ppc64_pt_entry_t *pte, u_long ptex)
{
	ppc64_slb_entry_t *slb;
	uint64_t ea, hash, vsid;
	int b, shift;

	/* Find SLB */
	vsid = PTEH_AVA_VSID(pte->pte_hi);
	if ((slb = slb_vsid_search(kd, vsid)) == NULL)
		return (~0UL);

	/* Get ESID part of EA */
	ea = slb->slbe & SLBE_ESID_MASK;

	b = slb->slbv & SLBV_L? LP_PAGE_SHIFT : PPC64_PAGE_SHIFT;

	/*
	 * If there are less than 64K PTEGs (16-bit), the upper bits of
	 * EA page must be obtained from PTEH's AVA.
	 */
	if (kd->vmst->hdr.pmapsize / (8 * sizeof(ppc64_pt_entry_t)) <
	    0x10000U) {
		/*
		 * Add 0 to 5 EA bits, right after VSID.
		 * b == 12: 5 bits
		 * b == 24: 4 bits
		 */
		shift = AVA_PAGE_SHIFT(b);
		ea |= (PTEH_AVA_PAGE(pte->pte_hi) >> shift) <<
		    (SLBE_ESID_SHIFT - 5 + shift);
	}

	/* Get VA page from hash and add to EA. */
	hash = (ptex & ~7) >> 3;
	if (pte->pte_hi & LPTEH_HID)
		hash = ~hash & PTE_HASH_MASK;
	ea |= ((hash ^ (vsid & PTE_HASH_MASK)) << b) & ~SLBE_ESID_MASK;
	return (ea);
}

static int
ppc64mmu_hpt_walk_pages(kvm_t *kd, kvm_walk_pages_cb_t *cb, void *arg)
{
	struct vmstate *vm;
	int ret;
	unsigned int pagesz;
	u_long dva, pa, va;
	u_long ptex, nptes;
	uint64_t vsid;

	ret = 0;
	vm = kd->vmst;
	nptes = vm->hdr.pmapsize / sizeof(ppc64_pt_entry_t);

	/* Walk through PTEs */
	for (ptex = 0; ptex < nptes; ptex++) {
		ppc64_pt_entry_t pte = pte_get(kd, ptex);
		if ((pte.pte_hi & LPTEH_VALID) == 0)
			continue;

		/* Skip non-kernel related pages, as well as VRMA ones */
		vsid = PTEH_AVA_VSID(pte.pte_hi);
		if ((vsid & KERNEL_VSID_BIT) == 0 ||
		    (vsid >> PPC64_PAGE_SHIFT) == VSID_VRMA)
			continue;

		/* Retrieve page's VA (EA on PPC64 terminology) */
		if ((va = get_ea(kd, &pte, ptex)) == ~0UL)
			goto out;

		/* Get PA and page size */
		if (pte.pte_hi & LPTEH_BIG) {
			pa = pte.pte_lo & PTEL_LP_PA_MASK;
			pagesz = LP_PAGE_SIZE;
		} else {
			pa = pte.pte_lo & PTEL_PA_MASK;
			pagesz = PPC64_PAGE_SIZE;
		}

		/* Get DMAP address */
		dva = vm->hdr.dmapbase + pa;

		if (!_kvm_visit_cb(kd, cb, arg, pa, va, dva,
		    entry_to_prot(&pte), pagesz, 0))
			goto out;
	}
	ret = 1;

out:
	return (ret);
}


static struct ppc64_mmu_ops ops = {
	.init		= ppc64mmu_hpt_init,
	.cleanup	= ppc64mmu_hpt_cleanup,
	.kvatop		= ppc64mmu_hpt_kvatop,
	.walk_pages	= ppc64mmu_hpt_walk_pages,
};
struct ppc64_mmu_ops *ppc64_mmu_ops_hpt = &ops;