Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/sys/powerpc/aim/mmu_oea64.c
106751 views
1
/*-
2
* SPDX-License-Identifier: BSD-2-Clause
3
*
4
* Copyright (c) 2008-2015 Nathan Whitehorn
5
* All rights reserved.
6
*
7
* Redistribution and use in source and binary forms, with or without
8
* modification, are permitted provided that the following conditions
9
* are met:
10
*
11
* 1. Redistributions of source code must retain the above copyright
12
* notice, this list of conditions and the following disclaimer.
13
* 2. Redistributions in binary form must reproduce the above copyright
14
* notice, this list of conditions and the following disclaimer in the
15
* documentation and/or other materials provided with the distribution.
16
*
17
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
*/
28
29
#include <sys/cdefs.h>
30
/*
31
* Manages physical address maps.
32
*
33
* Since the information managed by this module is also stored by the
34
* logical address mapping module, this module may throw away valid virtual
35
* to physical mappings at almost any time. However, invalidations of
36
* mappings must be done as requested.
37
*
38
* In order to cope with hardware architectures which make virtual to
39
* physical map invalidates expensive, this module may delay invalidate
40
* reduced protection operations until such time as they are actually
41
* necessary. This module is given full information as to which processors
42
* are currently using which maps, and to when physical maps must be made
43
* correct.
44
*/
45
46
#include "opt_kstack_pages.h"
47
48
#include <sys/param.h>
49
#include <sys/kernel.h>
50
#include <sys/conf.h>
51
#include <sys/queue.h>
52
#include <sys/cpuset.h>
53
#include <sys/kerneldump.h>
54
#include <sys/ktr.h>
55
#include <sys/lock.h>
56
#include <sys/msgbuf.h>
57
#include <sys/malloc.h>
58
#include <sys/mman.h>
59
#include <sys/mutex.h>
60
#include <sys/proc.h>
61
#include <sys/rwlock.h>
62
#include <sys/sched.h>
63
#include <sys/sysctl.h>
64
#include <sys/systm.h>
65
#include <sys/vmmeter.h>
66
#include <sys/smp.h>
67
#include <sys/reboot.h>
68
69
#include <sys/kdb.h>
70
71
#include <dev/ofw/openfirm.h>
72
73
#include <vm/vm.h>
74
#include <vm/pmap.h>
75
#include <vm/vm_param.h>
76
#include <vm/vm_kern.h>
77
#include <vm/vm_page.h>
78
#include <vm/vm_phys.h>
79
#include <vm/vm_map.h>
80
#include <vm/vm_object.h>
81
#include <vm/vm_extern.h>
82
#include <vm/vm_pageout.h>
83
#include <vm/vm_dumpset.h>
84
#include <vm/vm_radix.h>
85
#include <vm/vm_reserv.h>
86
#include <vm/uma.h>
87
88
#include <machine/_inttypes.h>
89
#include <machine/cpu.h>
90
#include <machine/ifunc.h>
91
#include <machine/platform.h>
92
#include <machine/frame.h>
93
#include <machine/md_var.h>
94
#include <machine/psl.h>
95
#include <machine/bat.h>
96
#include <machine/hid.h>
97
#include <machine/pte.h>
98
#include <machine/sr.h>
99
#include <machine/trap.h>
100
#include <machine/mmuvar.h>
101
102
#include "mmu_oea64.h"
103
104
void moea64_release_vsid(uint64_t vsid);
105
uintptr_t moea64_get_unique_vsid(void);
106
107
#define DISABLE_TRANS(msr) msr = mfmsr(); mtmsr(msr & ~PSL_DR)
108
#define ENABLE_TRANS(msr) mtmsr(msr)
109
110
#define VSID_MAKE(sr, hash) ((sr) | (((hash) & 0xfffff) << 4))
111
#define VSID_TO_HASH(vsid) (((vsid) >> 4) & 0xfffff)
112
#define VSID_HASH_MASK 0x0000007fffffffffULL
113
114
/*
115
* Locking semantics:
116
*
117
* There are two locks of interest: the page locks and the pmap locks, which
118
* protect their individual PVO lists and are locked in that order. The contents
119
* of all PVO entries are protected by the locks of their respective pmaps.
120
* The pmap of any PVO is guaranteed not to change so long as the PVO is linked
121
* into any list.
122
*
123
*/
124
125
#define PV_LOCK_COUNT MAXCPU
126
static struct mtx_padalign pv_lock[PV_LOCK_COUNT];
127
128
#define PV_LOCK_SHIFT HPT_SP_SHIFT
129
#define pa_index(pa) ((pa) >> PV_LOCK_SHIFT)
130
131
/*
132
* Cheap NUMA-izing of the pv locks, to reduce contention across domains.
133
* NUMA domains on POWER9 appear to be indexed as sparse memory spaces, with the
134
* index at (N << 45).
135
*/
136
#ifdef __powerpc64__
137
#define PV_LOCK_IDX(pa) ((pa_index(pa) * (((pa) >> 45) + 1)) % PV_LOCK_COUNT)
138
#else
139
#define PV_LOCK_IDX(pa) (pa_index(pa) % PV_LOCK_COUNT)
140
#endif
141
#define PV_LOCKPTR(pa) ((struct mtx *)(&pv_lock[PV_LOCK_IDX(pa)]))
142
#define PV_LOCK(pa) mtx_lock(PV_LOCKPTR(pa))
143
#define PV_UNLOCK(pa) mtx_unlock(PV_LOCKPTR(pa))
144
#define PV_LOCKASSERT(pa) mtx_assert(PV_LOCKPTR(pa), MA_OWNED)
145
#define PV_PAGE_LOCK(m) PV_LOCK(VM_PAGE_TO_PHYS(m))
146
#define PV_PAGE_UNLOCK(m) PV_UNLOCK(VM_PAGE_TO_PHYS(m))
147
#define PV_PAGE_LOCKASSERT(m) PV_LOCKASSERT(VM_PAGE_TO_PHYS(m))
148
149
struct ofw_map {
150
cell_t om_va;
151
cell_t om_len;
152
uint64_t om_pa;
153
cell_t om_mode;
154
};
155
156
extern unsigned char _etext[];
157
extern unsigned char _end[];
158
159
extern void *slbtrap, *slbtrapend;
160
161
/*
162
* Map of physical memory regions.
163
*/
164
static struct mem_region *regions;
165
static struct mem_region *pregions;
166
static struct numa_mem_region *numa_pregions;
167
static int regions_sz, pregions_sz, numapregions_sz;
168
169
u_int phys_avail_count;
170
171
extern void bs_remap_earlyboot(void);
172
173
/*
174
* Lock for the SLB tables.
175
*/
176
struct mtx moea64_slb_mutex;
177
178
/*
179
* PTEG data.
180
*/
181
u_long moea64_pteg_count;
182
u_long moea64_pteg_mask;
183
184
/*
185
* PVO data.
186
*/
187
188
uma_zone_t moea64_pvo_zone; /* zone for pvo entries */
189
190
static struct pvo_entry *moea64_bpvo_pool;
191
static int moea64_bpvo_pool_index = 0;
192
static int moea64_bpvo_pool_size = 0;
193
SYSCTL_INT(_machdep, OID_AUTO, moea64_allocated_bpvo_entries, CTLFLAG_RD,
194
&moea64_bpvo_pool_index, 0, "");
195
196
#define BPVO_POOL_SIZE 327680 /* Sensible historical default value */
197
#define BPVO_POOL_EXPANSION_FACTOR 3
198
#define VSID_NBPW (sizeof(u_int32_t) * 8)
199
#ifdef __powerpc64__
200
#define NVSIDS (NPMAPS * 16)
201
#define VSID_HASHMASK 0xffffffffUL
202
#else
203
#define NVSIDS NPMAPS
204
#define VSID_HASHMASK 0xfffffUL
205
#endif
206
static u_int moea64_vsid_bitmap[NVSIDS / VSID_NBPW];
207
208
static bool moea64_initialized = false;
209
210
#ifdef MOEA64_STATS
211
/*
212
* Statistics.
213
*/
214
u_int moea64_pte_valid = 0;
215
u_int moea64_pte_overflow = 0;
216
u_int moea64_pvo_entries = 0;
217
u_int moea64_pvo_enter_calls = 0;
218
u_int moea64_pvo_remove_calls = 0;
219
SYSCTL_INT(_machdep, OID_AUTO, moea64_pte_valid, CTLFLAG_RD,
220
&moea64_pte_valid, 0, "");
221
SYSCTL_INT(_machdep, OID_AUTO, moea64_pte_overflow, CTLFLAG_RD,
222
&moea64_pte_overflow, 0, "");
223
SYSCTL_INT(_machdep, OID_AUTO, moea64_pvo_entries, CTLFLAG_RD,
224
&moea64_pvo_entries, 0, "");
225
SYSCTL_INT(_machdep, OID_AUTO, moea64_pvo_enter_calls, CTLFLAG_RD,
226
&moea64_pvo_enter_calls, 0, "");
227
SYSCTL_INT(_machdep, OID_AUTO, moea64_pvo_remove_calls, CTLFLAG_RD,
228
&moea64_pvo_remove_calls, 0, "");
229
#endif
230
231
vm_offset_t moea64_scratchpage_va[2];
232
struct pvo_entry *moea64_scratchpage_pvo[2];
233
struct mtx moea64_scratchpage_mtx;
234
235
uint64_t moea64_large_page_mask = 0;
236
uint64_t moea64_large_page_size = 0;
237
int moea64_large_page_shift = 0;
238
bool moea64_has_lp_4k_16m = false;
239
240
/*
241
* PVO calls.
242
*/
243
static int moea64_pvo_enter(struct pvo_entry *pvo,
244
struct pvo_head *pvo_head, struct pvo_entry **oldpvo);
245
static void moea64_pvo_remove_from_pmap(struct pvo_entry *pvo);
246
static void moea64_pvo_remove_from_page(struct pvo_entry *pvo);
247
static void moea64_pvo_remove_from_page_locked(
248
struct pvo_entry *pvo, vm_page_t m);
249
static struct pvo_entry *moea64_pvo_find_va(pmap_t, vm_offset_t);
250
251
/*
252
* Utility routines.
253
*/
254
static bool moea64_query_bit(vm_page_t, uint64_t);
255
static u_int moea64_clear_bit(vm_page_t, uint64_t);
256
static void moea64_kremove(vm_offset_t);
257
static void moea64_syncicache(pmap_t pmap, vm_offset_t va,
258
vm_paddr_t pa, vm_size_t sz);
259
static void moea64_pmap_init_qpages(void *);
260
static void moea64_remove_locked(pmap_t, vm_offset_t,
261
vm_offset_t, struct pvo_dlist *);
262
263
/*
264
* Superpages data and routines.
265
*/
266
267
/*
268
* PVO flags (in vaddr) that must match for promotion to succeed.
269
* Note that protection bits are checked separately, as they reside in
270
* another field.
271
*/
272
#define PVO_FLAGS_PROMOTE (PVO_WIRED | PVO_MANAGED | PVO_PTEGIDX_VALID)
273
274
#define PVO_IS_SP(pvo) (((pvo)->pvo_vaddr & PVO_LARGE) && \
275
(pvo)->pvo_pmap != kernel_pmap)
276
277
/* Get physical address from PVO. */
278
#define PVO_PADDR(pvo) moea64_pvo_paddr(pvo)
279
280
/* MD page flag indicating that the page is a superpage. */
281
#define MDPG_ATTR_SP 0x40000000
282
283
SYSCTL_DECL(_vm_pmap);
284
285
static SYSCTL_NODE(_vm_pmap, OID_AUTO, sp, CTLFLAG_RD, 0,
286
"SP page mapping counters");
287
288
static u_long sp_demotions;
289
SYSCTL_ULONG(_vm_pmap_sp, OID_AUTO, demotions, CTLFLAG_RD,
290
&sp_demotions, 0, "SP page demotions");
291
292
static u_long sp_mappings;
293
SYSCTL_ULONG(_vm_pmap_sp, OID_AUTO, mappings, CTLFLAG_RD,
294
&sp_mappings, 0, "SP page mappings");
295
296
static u_long sp_p_failures;
297
SYSCTL_ULONG(_vm_pmap_sp, OID_AUTO, p_failures, CTLFLAG_RD,
298
&sp_p_failures, 0, "SP page promotion failures");
299
300
static u_long sp_p_fail_pa;
301
SYSCTL_ULONG(_vm_pmap_sp, OID_AUTO, p_fail_pa, CTLFLAG_RD,
302
&sp_p_fail_pa, 0, "SP page promotion failure: PAs don't match");
303
304
static u_long sp_p_fail_flags;
305
SYSCTL_ULONG(_vm_pmap_sp, OID_AUTO, p_fail_flags, CTLFLAG_RD,
306
&sp_p_fail_flags, 0, "SP page promotion failure: page flags don't match");
307
308
static u_long sp_p_fail_prot;
309
SYSCTL_ULONG(_vm_pmap_sp, OID_AUTO, p_fail_prot, CTLFLAG_RD,
310
&sp_p_fail_prot, 0,
311
"SP page promotion failure: page protections don't match");
312
313
static u_long sp_p_fail_wimg;
314
SYSCTL_ULONG(_vm_pmap_sp, OID_AUTO, p_fail_wimg, CTLFLAG_RD,
315
&sp_p_fail_wimg, 0, "SP page promotion failure: WIMG bits don't match");
316
317
static u_long sp_promotions;
318
SYSCTL_ULONG(_vm_pmap_sp, OID_AUTO, promotions, CTLFLAG_RD,
319
&sp_promotions, 0, "SP page promotions");
320
321
static bool moea64_ps_enabled(pmap_t);
322
static void moea64_align_superpage(vm_object_t, vm_ooffset_t,
323
vm_offset_t *, vm_size_t);
324
325
static int moea64_sp_enter(pmap_t pmap, vm_offset_t va,
326
vm_page_t m, vm_prot_t prot, u_int flags, int8_t psind);
327
static struct pvo_entry *moea64_sp_remove(struct pvo_entry *sp,
328
struct pvo_dlist *tofree);
329
330
#if VM_NRESERVLEVEL > 0
331
static void moea64_sp_promote(pmap_t pmap, vm_offset_t va, vm_page_t m);
332
#endif
333
static void moea64_sp_demote_aligned(struct pvo_entry *sp);
334
static void moea64_sp_demote(struct pvo_entry *pvo);
335
336
static struct pvo_entry *moea64_sp_unwire(struct pvo_entry *sp);
337
static struct pvo_entry *moea64_sp_protect(struct pvo_entry *sp,
338
vm_prot_t prot);
339
340
static int64_t moea64_sp_query(struct pvo_entry *pvo, uint64_t ptebit);
341
static int64_t moea64_sp_clear(struct pvo_entry *pvo, vm_page_t m,
342
uint64_t ptebit);
343
344
static __inline bool moea64_sp_pvo_in_range(struct pvo_entry *pvo,
345
vm_offset_t sva, vm_offset_t eva);
346
347
/*
348
* Kernel MMU interface
349
*/
350
void moea64_clear_modify(vm_page_t);
351
void moea64_copy_page(vm_page_t, vm_page_t);
352
void moea64_copy_page_dmap(vm_page_t, vm_page_t);
353
void moea64_copy_pages(vm_page_t *ma, vm_offset_t a_offset,
354
vm_page_t *mb, vm_offset_t b_offset, int xfersize);
355
void moea64_copy_pages_dmap(vm_page_t *ma, vm_offset_t a_offset,
356
vm_page_t *mb, vm_offset_t b_offset, int xfersize);
357
int moea64_enter(pmap_t, vm_offset_t, vm_page_t, vm_prot_t,
358
u_int flags, int8_t psind);
359
void moea64_enter_object(pmap_t, vm_offset_t, vm_offset_t, vm_page_t,
360
vm_prot_t);
361
void moea64_enter_quick(pmap_t, vm_offset_t, vm_page_t, vm_prot_t);
362
vm_paddr_t moea64_extract(pmap_t, vm_offset_t);
363
vm_page_t moea64_extract_and_hold(pmap_t, vm_offset_t, vm_prot_t);
364
void moea64_init(void);
365
bool moea64_is_modified(vm_page_t);
366
bool moea64_is_prefaultable(pmap_t, vm_offset_t);
367
bool moea64_is_referenced(vm_page_t);
368
int moea64_ts_referenced(vm_page_t);
369
vm_offset_t moea64_map(vm_offset_t *, vm_paddr_t, vm_paddr_t, int);
370
bool moea64_page_exists_quick(pmap_t, vm_page_t);
371
void moea64_page_init(vm_page_t);
372
int moea64_page_wired_mappings(vm_page_t);
373
int moea64_pinit(pmap_t);
374
void moea64_pinit0(pmap_t);
375
void moea64_protect(pmap_t, vm_offset_t, vm_offset_t, vm_prot_t);
376
void moea64_qenter(vm_offset_t, vm_page_t *, int);
377
void moea64_qremove(vm_offset_t, int);
378
void moea64_release(pmap_t);
379
void moea64_remove(pmap_t, vm_offset_t, vm_offset_t);
380
void moea64_remove_pages(pmap_t);
381
void moea64_remove_all(vm_page_t);
382
void moea64_remove_write(vm_page_t);
383
void moea64_unwire(pmap_t, vm_offset_t, vm_offset_t);
384
void moea64_zero_page(vm_page_t);
385
void moea64_zero_page_dmap(vm_page_t);
386
void moea64_zero_page_area(vm_page_t, int, int);
387
void moea64_activate(struct thread *);
388
void moea64_deactivate(struct thread *);
389
void *moea64_mapdev(vm_paddr_t, vm_size_t);
390
void *moea64_mapdev_attr(vm_paddr_t, vm_size_t, vm_memattr_t);
391
void moea64_unmapdev(void *, vm_size_t);
392
vm_paddr_t moea64_kextract(vm_offset_t);
393
void moea64_page_set_memattr(vm_page_t m, vm_memattr_t ma);
394
void moea64_kenter_attr(vm_offset_t, vm_paddr_t, vm_memattr_t ma);
395
void moea64_kenter(vm_offset_t, vm_paddr_t);
396
int moea64_dev_direct_mapped(vm_paddr_t, vm_size_t);
397
static void moea64_sync_icache(pmap_t, vm_offset_t, vm_size_t);
398
void moea64_dumpsys_map(vm_paddr_t pa, size_t sz,
399
void **va);
400
void moea64_scan_init(void);
401
vm_offset_t moea64_quick_enter_page(vm_page_t m);
402
vm_offset_t moea64_quick_enter_page_dmap(vm_page_t m);
403
void moea64_quick_remove_page(vm_offset_t addr);
404
bool moea64_page_is_mapped(vm_page_t m);
405
static int moea64_map_user_ptr(pmap_t pm,
406
volatile const void *uaddr, void **kaddr, size_t ulen, size_t *klen);
407
static int moea64_decode_kernel_ptr(vm_offset_t addr,
408
int *is_user, vm_offset_t *decoded_addr);
409
static size_t moea64_scan_pmap(struct bitset *dump_bitset);
410
static void *moea64_dump_pmap_init(unsigned blkpgs);
411
#ifdef __powerpc64__
412
static void moea64_page_array_startup(long);
413
#endif
414
static int moea64_mincore(pmap_t, vm_offset_t, vm_paddr_t *);
415
416
static struct pmap_funcs moea64_methods = {
417
.clear_modify = moea64_clear_modify,
418
.copy_page = moea64_copy_page,
419
.copy_pages = moea64_copy_pages,
420
.enter = moea64_enter,
421
.enter_object = moea64_enter_object,
422
.enter_quick = moea64_enter_quick,
423
.extract = moea64_extract,
424
.extract_and_hold = moea64_extract_and_hold,
425
.init = moea64_init,
426
.is_modified = moea64_is_modified,
427
.is_prefaultable = moea64_is_prefaultable,
428
.is_referenced = moea64_is_referenced,
429
.ts_referenced = moea64_ts_referenced,
430
.map = moea64_map,
431
.mincore = moea64_mincore,
432
.page_exists_quick = moea64_page_exists_quick,
433
.page_init = moea64_page_init,
434
.page_wired_mappings = moea64_page_wired_mappings,
435
.pinit = moea64_pinit,
436
.pinit0 = moea64_pinit0,
437
.protect = moea64_protect,
438
.qenter = moea64_qenter,
439
.qremove = moea64_qremove,
440
.release = moea64_release,
441
.remove = moea64_remove,
442
.remove_pages = moea64_remove_pages,
443
.remove_all = moea64_remove_all,
444
.remove_write = moea64_remove_write,
445
.sync_icache = moea64_sync_icache,
446
.unwire = moea64_unwire,
447
.zero_page = moea64_zero_page,
448
.zero_page_area = moea64_zero_page_area,
449
.activate = moea64_activate,
450
.deactivate = moea64_deactivate,
451
.page_set_memattr = moea64_page_set_memattr,
452
.quick_enter_page = moea64_quick_enter_page,
453
.quick_remove_page = moea64_quick_remove_page,
454
.page_is_mapped = moea64_page_is_mapped,
455
#ifdef __powerpc64__
456
.page_array_startup = moea64_page_array_startup,
457
#endif
458
.ps_enabled = moea64_ps_enabled,
459
.align_superpage = moea64_align_superpage,
460
461
/* Internal interfaces */
462
.mapdev = moea64_mapdev,
463
.mapdev_attr = moea64_mapdev_attr,
464
.unmapdev = moea64_unmapdev,
465
.kextract = moea64_kextract,
466
.kenter = moea64_kenter,
467
.kenter_attr = moea64_kenter_attr,
468
.dev_direct_mapped = moea64_dev_direct_mapped,
469
.dumpsys_pa_init = moea64_scan_init,
470
.dumpsys_scan_pmap = moea64_scan_pmap,
471
.dumpsys_dump_pmap_init = moea64_dump_pmap_init,
472
.dumpsys_map_chunk = moea64_dumpsys_map,
473
.map_user_ptr = moea64_map_user_ptr,
474
.decode_kernel_ptr = moea64_decode_kernel_ptr,
475
};
476
477
MMU_DEF(oea64_mmu, "mmu_oea64_base", moea64_methods);
478
479
/*
480
* Get physical address from PVO.
481
*
482
* For superpages, the lower bits are not stored on pvo_pte.pa and must be
483
* obtained from VA.
484
*/
485
static __always_inline vm_paddr_t
486
moea64_pvo_paddr(struct pvo_entry *pvo)
487
{
488
vm_paddr_t pa;
489
490
pa = (pvo)->pvo_pte.pa & LPTE_RPGN;
491
492
if (PVO_IS_SP(pvo)) {
493
pa &= ~HPT_SP_MASK; /* This is needed to clear LPTE_LP bits. */
494
pa |= PVO_VADDR(pvo) & HPT_SP_MASK;
495
}
496
return (pa);
497
}
498
499
static struct pvo_head *
500
vm_page_to_pvoh(vm_page_t m)
501
{
502
503
mtx_assert(PV_LOCKPTR(VM_PAGE_TO_PHYS(m)), MA_OWNED);
504
return (&m->md.mdpg_pvoh);
505
}
506
507
static struct pvo_entry *
508
alloc_pvo_entry(int bootstrap)
509
{
510
struct pvo_entry *pvo;
511
512
if (!moea64_initialized || bootstrap) {
513
if (moea64_bpvo_pool_index >= moea64_bpvo_pool_size) {
514
panic("%s: bpvo pool exhausted, index=%d, size=%d, bytes=%zd."
515
"Try setting machdep.moea64_bpvo_pool_size tunable",
516
__func__, moea64_bpvo_pool_index,
517
moea64_bpvo_pool_size,
518
moea64_bpvo_pool_size * sizeof(struct pvo_entry));
519
}
520
pvo = &moea64_bpvo_pool[
521
atomic_fetchadd_int(&moea64_bpvo_pool_index, 1)];
522
bzero(pvo, sizeof(*pvo));
523
pvo->pvo_vaddr = PVO_BOOTSTRAP;
524
} else
525
pvo = uma_zalloc(moea64_pvo_zone, M_NOWAIT | M_ZERO);
526
527
return (pvo);
528
}
529
530
static void
531
init_pvo_entry(struct pvo_entry *pvo, pmap_t pmap, vm_offset_t va)
532
{
533
uint64_t vsid;
534
uint64_t hash;
535
int shift;
536
537
PMAP_LOCK_ASSERT(pmap, MA_OWNED);
538
539
pvo->pvo_pmap = pmap;
540
va &= ~ADDR_POFF;
541
pvo->pvo_vaddr |= va;
542
vsid = va_to_vsid(pmap, va);
543
pvo->pvo_vpn = (uint64_t)((va & ADDR_PIDX) >> ADDR_PIDX_SHFT)
544
| (vsid << 16);
545
546
if (pmap == kernel_pmap && (pvo->pvo_vaddr & PVO_LARGE) != 0)
547
shift = moea64_large_page_shift;
548
else
549
shift = ADDR_PIDX_SHFT;
550
hash = (vsid & VSID_HASH_MASK) ^ (((uint64_t)va & ADDR_PIDX) >> shift);
551
pvo->pvo_pte.slot = (hash & moea64_pteg_mask) << 3;
552
}
553
554
static void
555
free_pvo_entry(struct pvo_entry *pvo)
556
{
557
558
if (!(pvo->pvo_vaddr & PVO_BOOTSTRAP))
559
uma_zfree(moea64_pvo_zone, pvo);
560
}
561
562
void
563
moea64_pte_from_pvo(const struct pvo_entry *pvo, struct lpte *lpte)
564
{
565
566
lpte->pte_hi = moea64_pte_vpn_from_pvo_vpn(pvo);
567
lpte->pte_hi |= LPTE_VALID;
568
569
if (pvo->pvo_vaddr & PVO_LARGE)
570
lpte->pte_hi |= LPTE_BIG;
571
if (pvo->pvo_vaddr & PVO_WIRED)
572
lpte->pte_hi |= LPTE_WIRED;
573
if (pvo->pvo_vaddr & PVO_HID)
574
lpte->pte_hi |= LPTE_HID;
575
576
lpte->pte_lo = pvo->pvo_pte.pa; /* Includes WIMG bits */
577
if (pvo->pvo_pte.prot & VM_PROT_WRITE)
578
lpte->pte_lo |= LPTE_BW;
579
else
580
lpte->pte_lo |= LPTE_BR;
581
582
if (!(pvo->pvo_pte.prot & VM_PROT_EXECUTE))
583
lpte->pte_lo |= LPTE_NOEXEC;
584
}
585
586
static __inline uint64_t
587
moea64_calc_wimg(vm_paddr_t pa, vm_memattr_t ma)
588
{
589
uint64_t pte_lo;
590
int i;
591
592
if (ma != VM_MEMATTR_DEFAULT) {
593
switch (ma) {
594
case VM_MEMATTR_UNCACHEABLE:
595
return (LPTE_I | LPTE_G);
596
case VM_MEMATTR_CACHEABLE:
597
return (LPTE_M);
598
case VM_MEMATTR_WRITE_COMBINING:
599
case VM_MEMATTR_WRITE_BACK:
600
case VM_MEMATTR_PREFETCHABLE:
601
return (LPTE_I);
602
case VM_MEMATTR_WRITE_THROUGH:
603
return (LPTE_W | LPTE_M);
604
}
605
}
606
607
/*
608
* Assume the page is cache inhibited and access is guarded unless
609
* it's in our available memory array.
610
*/
611
pte_lo = LPTE_I | LPTE_G;
612
for (i = 0; i < pregions_sz; i++) {
613
if ((pa >= pregions[i].mr_start) &&
614
(pa < (pregions[i].mr_start + pregions[i].mr_size))) {
615
pte_lo &= ~(LPTE_I | LPTE_G);
616
pte_lo |= LPTE_M;
617
break;
618
}
619
}
620
621
return pte_lo;
622
}
623
624
/*
625
* Quick sort callout for comparing memory regions.
626
*/
627
static int om_cmp(const void *a, const void *b);
628
629
static int
630
om_cmp(const void *a, const void *b)
631
{
632
const struct ofw_map *mapa;
633
const struct ofw_map *mapb;
634
635
mapa = a;
636
mapb = b;
637
if (mapa->om_pa < mapb->om_pa)
638
return (-1);
639
else if (mapa->om_pa > mapb->om_pa)
640
return (1);
641
else
642
return (0);
643
}
644
645
static void
646
moea64_add_ofw_mappings(phandle_t mmu, size_t sz)
647
{
648
struct ofw_map translations[sz/(4*sizeof(cell_t))]; /*>= 4 cells per */
649
pcell_t acells, trans_cells[sz/sizeof(cell_t)];
650
struct pvo_entry *pvo;
651
register_t msr;
652
vm_offset_t off;
653
vm_paddr_t pa_base;
654
int i, j;
655
656
bzero(translations, sz);
657
OF_getencprop(OF_finddevice("/"), "#address-cells", &acells,
658
sizeof(acells));
659
if (OF_getencprop(mmu, "translations", trans_cells, sz) == -1)
660
panic("moea64_bootstrap: can't get ofw translations");
661
662
CTR0(KTR_PMAP, "moea64_add_ofw_mappings: translations");
663
sz /= sizeof(cell_t);
664
for (i = 0, j = 0; i < sz; j++) {
665
translations[j].om_va = trans_cells[i++];
666
translations[j].om_len = trans_cells[i++];
667
translations[j].om_pa = trans_cells[i++];
668
if (acells == 2) {
669
translations[j].om_pa <<= 32;
670
translations[j].om_pa |= trans_cells[i++];
671
}
672
translations[j].om_mode = trans_cells[i++];
673
}
674
KASSERT(i == sz, ("Translations map has incorrect cell count (%d/%zd)",
675
i, sz));
676
677
sz = j;
678
qsort(translations, sz, sizeof (*translations), om_cmp);
679
680
for (i = 0; i < sz; i++) {
681
pa_base = translations[i].om_pa;
682
#ifndef __powerpc64__
683
if ((translations[i].om_pa >> 32) != 0)
684
panic("OFW translations above 32-bit boundary!");
685
#endif
686
687
if (pa_base % PAGE_SIZE)
688
panic("OFW translation not page-aligned (phys)!");
689
if (translations[i].om_va % PAGE_SIZE)
690
panic("OFW translation not page-aligned (virt)!");
691
692
CTR3(KTR_PMAP, "translation: pa=%#zx va=%#x len=%#x",
693
pa_base, translations[i].om_va, translations[i].om_len);
694
695
/* Now enter the pages for this mapping */
696
697
DISABLE_TRANS(msr);
698
for (off = 0; off < translations[i].om_len; off += PAGE_SIZE) {
699
/* If this address is direct-mapped, skip remapping */
700
if (hw_direct_map &&
701
translations[i].om_va == PHYS_TO_DMAP(pa_base) &&
702
moea64_calc_wimg(pa_base + off, VM_MEMATTR_DEFAULT)
703
== LPTE_M)
704
continue;
705
706
PMAP_LOCK(kernel_pmap);
707
pvo = moea64_pvo_find_va(kernel_pmap,
708
translations[i].om_va + off);
709
PMAP_UNLOCK(kernel_pmap);
710
if (pvo != NULL)
711
continue;
712
713
moea64_kenter(translations[i].om_va + off,
714
pa_base + off);
715
}
716
ENABLE_TRANS(msr);
717
}
718
}
719
720
#ifdef __powerpc64__
721
static void
722
moea64_probe_large_page(void)
723
{
724
uint16_t pvr = mfpvr() >> 16;
725
726
switch (pvr) {
727
case IBM970:
728
case IBM970FX:
729
case IBM970MP:
730
powerpc_sync(); isync();
731
mtspr(SPR_HID4, mfspr(SPR_HID4) & ~HID4_970_DISABLE_LG_PG);
732
powerpc_sync(); isync();
733
734
/* FALLTHROUGH */
735
default:
736
if (moea64_large_page_size == 0) {
737
moea64_large_page_size = 0x1000000; /* 16 MB */
738
moea64_large_page_shift = 24;
739
}
740
}
741
742
moea64_large_page_mask = moea64_large_page_size - 1;
743
}
744
745
static void
746
moea64_bootstrap_slb_prefault(vm_offset_t va, int large)
747
{
748
struct slb *cache;
749
struct slb entry;
750
uint64_t esid, slbe;
751
uint64_t i;
752
753
cache = PCPU_GET(aim.slb);
754
esid = va >> ADDR_SR_SHFT;
755
slbe = (esid << SLBE_ESID_SHIFT) | SLBE_VALID;
756
757
for (i = 0; i < 64; i++) {
758
if (cache[i].slbe == (slbe | i))
759
return;
760
}
761
762
entry.slbe = slbe;
763
entry.slbv = KERNEL_VSID(esid) << SLBV_VSID_SHIFT;
764
if (large)
765
entry.slbv |= SLBV_L;
766
767
slb_insert_kernel(entry.slbe, entry.slbv);
768
}
769
#endif
770
771
static int
772
moea64_kenter_large(vm_offset_t va, vm_paddr_t pa, uint64_t attr, int bootstrap)
773
{
774
struct pvo_entry *pvo;
775
uint64_t pte_lo;
776
int error;
777
778
pte_lo = LPTE_M;
779
pte_lo |= attr;
780
781
pvo = alloc_pvo_entry(bootstrap);
782
pvo->pvo_vaddr |= PVO_WIRED | PVO_LARGE;
783
init_pvo_entry(pvo, kernel_pmap, va);
784
785
pvo->pvo_pte.prot = VM_PROT_READ | VM_PROT_WRITE |
786
VM_PROT_EXECUTE;
787
pvo->pvo_pte.pa = pa | pte_lo;
788
error = moea64_pvo_enter(pvo, NULL, NULL);
789
if (error != 0)
790
panic("Error %d inserting large page\n", error);
791
return (0);
792
}
793
794
static void
795
moea64_setup_direct_map(vm_offset_t kernelstart,
796
vm_offset_t kernelend)
797
{
798
register_t msr;
799
vm_paddr_t pa, pkernelstart, pkernelend;
800
vm_offset_t size, off;
801
uint64_t pte_lo;
802
int i;
803
804
if (moea64_large_page_size == 0)
805
hw_direct_map = 0;
806
807
DISABLE_TRANS(msr);
808
if (hw_direct_map) {
809
PMAP_LOCK(kernel_pmap);
810
for (i = 0; i < pregions_sz; i++) {
811
for (pa = pregions[i].mr_start; pa < pregions[i].mr_start +
812
pregions[i].mr_size; pa += moea64_large_page_size) {
813
pte_lo = LPTE_M;
814
if (pa & moea64_large_page_mask) {
815
pa &= moea64_large_page_mask;
816
pte_lo |= LPTE_G;
817
}
818
if (pa + moea64_large_page_size >
819
pregions[i].mr_start + pregions[i].mr_size)
820
pte_lo |= LPTE_G;
821
822
moea64_kenter_large(PHYS_TO_DMAP(pa), pa, pte_lo, 1);
823
}
824
}
825
PMAP_UNLOCK(kernel_pmap);
826
}
827
828
/*
829
* Make sure the kernel and BPVO pool stay mapped on systems either
830
* without a direct map or on which the kernel is not already executing
831
* out of the direct-mapped region.
832
*/
833
if (kernelstart < DMAP_BASE_ADDRESS) {
834
/*
835
* For pre-dmap execution, we need to use identity mapping
836
* because we will be operating with the mmu on but in the
837
* wrong address configuration until we __restartkernel().
838
*/
839
for (pa = kernelstart & ~PAGE_MASK; pa < kernelend;
840
pa += PAGE_SIZE)
841
moea64_kenter(pa, pa);
842
} else if (!hw_direct_map) {
843
pkernelstart = kernelstart & ~DMAP_BASE_ADDRESS;
844
pkernelend = kernelend & ~DMAP_BASE_ADDRESS;
845
for (pa = pkernelstart & ~PAGE_MASK; pa < pkernelend;
846
pa += PAGE_SIZE)
847
moea64_kenter(pa | DMAP_BASE_ADDRESS, pa);
848
}
849
850
if (!hw_direct_map) {
851
size = moea64_bpvo_pool_size*sizeof(struct pvo_entry);
852
off = (vm_offset_t)(moea64_bpvo_pool);
853
for (pa = off; pa < off + size; pa += PAGE_SIZE)
854
moea64_kenter(pa, pa);
855
856
/* Map exception vectors */
857
for (pa = EXC_RSVD; pa < EXC_LAST; pa += PAGE_SIZE)
858
moea64_kenter(pa | DMAP_BASE_ADDRESS, pa);
859
}
860
ENABLE_TRANS(msr);
861
862
/*
863
* Allow user to override unmapped_buf_allowed for testing.
864
* XXXKIB Only direct map implementation was tested.
865
*/
866
if (!TUNABLE_INT_FETCH("vfs.unmapped_buf_allowed",
867
&unmapped_buf_allowed))
868
unmapped_buf_allowed = hw_direct_map;
869
}
870
871
/* Quick sort callout for comparing physical addresses. */
872
static int
873
pa_cmp(const void *a, const void *b)
874
{
875
const vm_paddr_t *pa = a, *pb = b;
876
877
if (*pa < *pb)
878
return (-1);
879
else if (*pa > *pb)
880
return (1);
881
else
882
return (0);
883
}
884
885
void
886
moea64_early_bootstrap(vm_offset_t kernelstart, vm_offset_t kernelend)
887
{
888
int i, j;
889
vm_size_t physsz, hwphyssz;
890
vm_paddr_t kernelphysstart, kernelphysend;
891
int rm_pavail;
892
893
/* Level 0 reservations consist of 4096 pages (16MB superpage). */
894
vm_level_0_order = VM_LEVEL_0_ORDER_HPT;
895
896
#ifndef __powerpc64__
897
/* We don't have a direct map since there is no BAT */
898
hw_direct_map = 0;
899
900
/* Make sure battable is zero, since we have no BAT */
901
for (i = 0; i < 16; i++) {
902
battable[i].batu = 0;
903
battable[i].batl = 0;
904
}
905
#else
906
/* Install trap handlers for SLBs */
907
bcopy(&slbtrap, (void *)EXC_DSE,(size_t)&slbtrapend - (size_t)&slbtrap);
908
bcopy(&slbtrap, (void *)EXC_ISE,(size_t)&slbtrapend - (size_t)&slbtrap);
909
__syncicache((void *)EXC_DSE, 0x80);
910
__syncicache((void *)EXC_ISE, 0x80);
911
#endif
912
913
kernelphysstart = kernelstart & ~DMAP_BASE_ADDRESS;
914
kernelphysend = kernelend & ~DMAP_BASE_ADDRESS;
915
916
/* Get physical memory regions from firmware */
917
mem_regions(&pregions, &pregions_sz, &regions, &regions_sz);
918
CTR0(KTR_PMAP, "moea64_bootstrap: physical memory");
919
920
if (PHYS_AVAIL_ENTRIES < regions_sz)
921
panic("moea64_bootstrap: phys_avail too small");
922
923
phys_avail_count = 0;
924
physsz = 0;
925
hwphyssz = 0;
926
TUNABLE_ULONG_FETCH("hw.physmem", (u_long *) &hwphyssz);
927
for (i = 0, j = 0; i < regions_sz; i++, j += 2) {
928
CTR3(KTR_PMAP, "region: %#zx - %#zx (%#zx)",
929
regions[i].mr_start, regions[i].mr_start +
930
regions[i].mr_size, regions[i].mr_size);
931
if (hwphyssz != 0 &&
932
(physsz + regions[i].mr_size) >= hwphyssz) {
933
if (physsz < hwphyssz) {
934
phys_avail[j] = regions[i].mr_start;
935
phys_avail[j + 1] = regions[i].mr_start +
936
hwphyssz - physsz;
937
physsz = hwphyssz;
938
phys_avail_count++;
939
dump_avail[j] = phys_avail[j];
940
dump_avail[j + 1] = phys_avail[j + 1];
941
}
942
break;
943
}
944
phys_avail[j] = regions[i].mr_start;
945
phys_avail[j + 1] = regions[i].mr_start + regions[i].mr_size;
946
phys_avail_count++;
947
physsz += regions[i].mr_size;
948
dump_avail[j] = phys_avail[j];
949
dump_avail[j + 1] = phys_avail[j + 1];
950
}
951
952
/* Check for overlap with the kernel and exception vectors */
953
rm_pavail = 0;
954
for (j = 0; j < 2*phys_avail_count; j+=2) {
955
if (phys_avail[j] < EXC_LAST)
956
phys_avail[j] += EXC_LAST;
957
958
if (phys_avail[j] >= kernelphysstart &&
959
phys_avail[j+1] <= kernelphysend) {
960
phys_avail[j] = phys_avail[j+1] = ~0;
961
rm_pavail++;
962
continue;
963
}
964
965
if (kernelphysstart >= phys_avail[j] &&
966
kernelphysstart < phys_avail[j+1]) {
967
if (kernelphysend < phys_avail[j+1]) {
968
phys_avail[2*phys_avail_count] =
969
(kernelphysend & ~PAGE_MASK) + PAGE_SIZE;
970
phys_avail[2*phys_avail_count + 1] =
971
phys_avail[j+1];
972
phys_avail_count++;
973
}
974
975
phys_avail[j+1] = kernelphysstart & ~PAGE_MASK;
976
}
977
978
if (kernelphysend >= phys_avail[j] &&
979
kernelphysend < phys_avail[j+1]) {
980
if (kernelphysstart > phys_avail[j]) {
981
phys_avail[2*phys_avail_count] = phys_avail[j];
982
phys_avail[2*phys_avail_count + 1] =
983
kernelphysstart & ~PAGE_MASK;
984
phys_avail_count++;
985
}
986
987
phys_avail[j] = (kernelphysend & ~PAGE_MASK) +
988
PAGE_SIZE;
989
}
990
}
991
992
/* Remove physical available regions marked for removal (~0) */
993
if (rm_pavail) {
994
qsort(phys_avail, 2*phys_avail_count, sizeof(phys_avail[0]),
995
pa_cmp);
996
phys_avail_count -= rm_pavail;
997
for (i = 2*phys_avail_count;
998
i < 2*(phys_avail_count + rm_pavail); i+=2)
999
phys_avail[i] = phys_avail[i+1] = 0;
1000
}
1001
1002
physmem = btoc(physsz);
1003
1004
#ifdef PTEGCOUNT
1005
moea64_pteg_count = PTEGCOUNT;
1006
#else
1007
moea64_pteg_count = 0x1000;
1008
1009
while (moea64_pteg_count < physmem)
1010
moea64_pteg_count <<= 1;
1011
1012
moea64_pteg_count >>= 1;
1013
#endif /* PTEGCOUNT */
1014
}
1015
1016
void
1017
moea64_mid_bootstrap(vm_offset_t kernelstart, vm_offset_t kernelend)
1018
{
1019
int i;
1020
1021
/*
1022
* Set PTEG mask
1023
*/
1024
moea64_pteg_mask = moea64_pteg_count - 1;
1025
1026
/*
1027
* Initialize SLB table lock and page locks
1028
*/
1029
mtx_init(&moea64_slb_mutex, "SLB table", NULL, MTX_DEF);
1030
for (i = 0; i < PV_LOCK_COUNT; i++)
1031
mtx_init(&pv_lock[i], "page pv", NULL, MTX_DEF);
1032
1033
/*
1034
* Initialise the bootstrap pvo pool.
1035
*/
1036
TUNABLE_INT_FETCH("machdep.moea64_bpvo_pool_size", &moea64_bpvo_pool_size);
1037
if (moea64_bpvo_pool_size == 0) {
1038
if (!hw_direct_map)
1039
moea64_bpvo_pool_size = ((ptoa((uintmax_t)physmem) * sizeof(struct vm_page)) /
1040
(PAGE_SIZE * PAGE_SIZE)) * BPVO_POOL_EXPANSION_FACTOR;
1041
else
1042
moea64_bpvo_pool_size = BPVO_POOL_SIZE;
1043
}
1044
1045
if (boothowto & RB_VERBOSE) {
1046
printf("mmu_oea64: bpvo pool entries = %d, bpvo pool size = %zu MB\n",
1047
moea64_bpvo_pool_size,
1048
moea64_bpvo_pool_size*sizeof(struct pvo_entry) / 1048576);
1049
}
1050
1051
moea64_bpvo_pool = (struct pvo_entry *)moea64_bootstrap_alloc(
1052
moea64_bpvo_pool_size*sizeof(struct pvo_entry), PAGE_SIZE);
1053
moea64_bpvo_pool_index = 0;
1054
1055
/* Place at address usable through the direct map */
1056
if (hw_direct_map)
1057
moea64_bpvo_pool = (struct pvo_entry *)
1058
PHYS_TO_DMAP((uintptr_t)moea64_bpvo_pool);
1059
1060
/*
1061
* Make sure kernel vsid is allocated as well as VSID 0.
1062
*/
1063
#ifndef __powerpc64__
1064
moea64_vsid_bitmap[(KERNEL_VSIDBITS & (NVSIDS - 1)) / VSID_NBPW]
1065
|= 1 << (KERNEL_VSIDBITS % VSID_NBPW);
1066
moea64_vsid_bitmap[0] |= 1;
1067
#endif
1068
1069
/*
1070
* Initialize the kernel pmap (which is statically allocated).
1071
*/
1072
#ifdef __powerpc64__
1073
for (i = 0; i < 64; i++) {
1074
pcpup->pc_aim.slb[i].slbv = 0;
1075
pcpup->pc_aim.slb[i].slbe = 0;
1076
}
1077
#else
1078
for (i = 0; i < 16; i++)
1079
kernel_pmap->pm_sr[i] = EMPTY_SEGMENT + i;
1080
#endif
1081
1082
kernel_pmap->pmap_phys = kernel_pmap;
1083
CPU_FILL(&kernel_pmap->pm_active);
1084
RB_INIT(&kernel_pmap->pmap_pvo);
1085
1086
PMAP_LOCK_INIT(kernel_pmap);
1087
1088
/*
1089
* Now map in all the other buffers we allocated earlier
1090
*/
1091
1092
moea64_setup_direct_map(kernelstart, kernelend);
1093
}
1094
1095
void
1096
moea64_late_bootstrap(vm_offset_t kernelstart, vm_offset_t kernelend)
1097
{
1098
ihandle_t mmui;
1099
phandle_t chosen;
1100
phandle_t mmu;
1101
ssize_t sz;
1102
int i;
1103
vm_offset_t pa, va;
1104
void *dpcpu;
1105
1106
/*
1107
* Set up the Open Firmware pmap and add its mappings if not in real
1108
* mode.
1109
*/
1110
1111
chosen = OF_finddevice("/chosen");
1112
if (chosen != -1 && OF_getencprop(chosen, "mmu", &mmui, 4) != -1) {
1113
mmu = OF_instance_to_package(mmui);
1114
if (mmu == -1 ||
1115
(sz = OF_getproplen(mmu, "translations")) == -1)
1116
sz = 0;
1117
if (sz > 6144 /* tmpstksz - 2 KB headroom */)
1118
panic("moea64_bootstrap: too many ofw translations");
1119
1120
if (sz > 0)
1121
moea64_add_ofw_mappings(mmu, sz);
1122
}
1123
1124
/*
1125
* Calculate the last available physical address.
1126
*/
1127
Maxmem = 0;
1128
for (i = 0; phys_avail[i + 1] != 0; i += 2)
1129
Maxmem = MAX(Maxmem, powerpc_btop(phys_avail[i + 1]));
1130
1131
/*
1132
* Initialize MMU.
1133
*/
1134
pmap_cpu_bootstrap(0);
1135
mtmsr(mfmsr() | PSL_DR | PSL_IR);
1136
pmap_bootstrapped++;
1137
1138
/*
1139
* Set the start and end of kva.
1140
*/
1141
virtual_avail = VM_MIN_KERNEL_ADDRESS;
1142
virtual_end = VM_MAX_SAFE_KERNEL_ADDRESS;
1143
1144
/*
1145
* Map the entire KVA range into the SLB. We must not fault there.
1146
*/
1147
#ifdef __powerpc64__
1148
for (va = virtual_avail; va < virtual_end; va += SEGMENT_LENGTH)
1149
moea64_bootstrap_slb_prefault(va, 0);
1150
#endif
1151
1152
/*
1153
* Remap any early IO mappings (console framebuffer, etc.)
1154
*/
1155
bs_remap_earlyboot();
1156
1157
/*
1158
* Figure out how far we can extend virtual_end into segment 16
1159
* without running into existing mappings. Segment 16 is guaranteed
1160
* to contain neither RAM nor devices (at least on Apple hardware),
1161
* but will generally contain some OFW mappings we should not
1162
* step on.
1163
*/
1164
1165
#ifndef __powerpc64__ /* KVA is in high memory on PPC64 */
1166
PMAP_LOCK(kernel_pmap);
1167
while (virtual_end < VM_MAX_KERNEL_ADDRESS &&
1168
moea64_pvo_find_va(kernel_pmap, virtual_end+1) == NULL)
1169
virtual_end += PAGE_SIZE;
1170
PMAP_UNLOCK(kernel_pmap);
1171
#endif
1172
1173
/*
1174
* Allocate a kernel stack with a guard page for thread0 and map it
1175
* into the kernel page map.
1176
*/
1177
pa = moea64_bootstrap_alloc(kstack_pages * PAGE_SIZE, PAGE_SIZE);
1178
va = virtual_avail + KSTACK_GUARD_PAGES * PAGE_SIZE;
1179
virtual_avail = va + kstack_pages * PAGE_SIZE;
1180
CTR2(KTR_PMAP, "moea64_bootstrap: kstack0 at %#x (%#x)", pa, va);
1181
thread0.td_kstack = va;
1182
thread0.td_kstack_pages = kstack_pages;
1183
for (i = 0; i < kstack_pages; i++) {
1184
moea64_kenter(va, pa);
1185
pa += PAGE_SIZE;
1186
va += PAGE_SIZE;
1187
}
1188
1189
/*
1190
* Allocate virtual address space for the message buffer.
1191
*/
1192
pa = msgbuf_phys = moea64_bootstrap_alloc(msgbufsize, PAGE_SIZE);
1193
msgbufp = (struct msgbuf *)virtual_avail;
1194
va = virtual_avail;
1195
virtual_avail += round_page(msgbufsize);
1196
while (va < virtual_avail) {
1197
moea64_kenter(va, pa);
1198
pa += PAGE_SIZE;
1199
va += PAGE_SIZE;
1200
}
1201
1202
/*
1203
* Allocate virtual address space for the dynamic percpu area.
1204
*/
1205
pa = moea64_bootstrap_alloc(DPCPU_SIZE, PAGE_SIZE);
1206
dpcpu = (void *)virtual_avail;
1207
va = virtual_avail;
1208
virtual_avail += DPCPU_SIZE;
1209
while (va < virtual_avail) {
1210
moea64_kenter(va, pa);
1211
pa += PAGE_SIZE;
1212
va += PAGE_SIZE;
1213
}
1214
dpcpu_init(dpcpu, curcpu);
1215
1216
crashdumpmap = (caddr_t)virtual_avail;
1217
virtual_avail += MAXDUMPPGS * PAGE_SIZE;
1218
1219
/*
1220
* Allocate some things for page zeroing. We put this directly
1221
* in the page table and use MOEA64_PTE_REPLACE to avoid any
1222
* of the PVO book-keeping or other parts of the VM system
1223
* from even knowing that this hack exists.
1224
*/
1225
1226
if (!hw_direct_map) {
1227
mtx_init(&moea64_scratchpage_mtx, "pvo zero page", NULL,
1228
MTX_DEF);
1229
for (i = 0; i < 2; i++) {
1230
moea64_scratchpage_va[i] = (virtual_end+1) - PAGE_SIZE;
1231
virtual_end -= PAGE_SIZE;
1232
1233
moea64_kenter(moea64_scratchpage_va[i], 0);
1234
1235
PMAP_LOCK(kernel_pmap);
1236
moea64_scratchpage_pvo[i] = moea64_pvo_find_va(
1237
kernel_pmap, (vm_offset_t)moea64_scratchpage_va[i]);
1238
PMAP_UNLOCK(kernel_pmap);
1239
}
1240
}
1241
1242
numa_mem_regions(&numa_pregions, &numapregions_sz);
1243
}
1244
1245
static void
1246
moea64_pmap_init_qpages(void *dummy __unused)
1247
{
1248
struct pcpu *pc;
1249
int i;
1250
1251
if (hw_direct_map)
1252
return;
1253
1254
CPU_FOREACH(i) {
1255
pc = pcpu_find(i);
1256
pc->pc_qmap_addr = kva_alloc(PAGE_SIZE);
1257
if (pc->pc_qmap_addr == 0)
1258
panic("pmap_init_qpages: unable to allocate KVA");
1259
PMAP_LOCK(kernel_pmap);
1260
pc->pc_aim.qmap_pvo =
1261
moea64_pvo_find_va(kernel_pmap, pc->pc_qmap_addr);
1262
PMAP_UNLOCK(kernel_pmap);
1263
mtx_init(&pc->pc_aim.qmap_lock, "qmap lock", NULL, MTX_DEF);
1264
}
1265
}
1266
1267
SYSINIT(qpages_init, SI_SUB_CPU, SI_ORDER_ANY, moea64_pmap_init_qpages, NULL);
1268
1269
/*
1270
* Activate a user pmap. This mostly involves setting some non-CPU
1271
* state.
1272
*/
1273
void
1274
moea64_activate(struct thread *td)
1275
{
1276
pmap_t pm;
1277
1278
pm = &td->td_proc->p_vmspace->vm_pmap;
1279
CPU_SET(PCPU_GET(cpuid), &pm->pm_active);
1280
1281
#ifdef __powerpc64__
1282
PCPU_SET(aim.userslb, pm->pm_slb);
1283
__asm __volatile("slbmte %0, %1; isync" ::
1284
"r"(td->td_pcb->pcb_cpu.aim.usr_vsid), "r"(USER_SLB_SLBE));
1285
#else
1286
PCPU_SET(curpmap, pm->pmap_phys);
1287
mtsrin(USER_SR << ADDR_SR_SHFT, td->td_pcb->pcb_cpu.aim.usr_vsid);
1288
#endif
1289
}
1290
1291
void
1292
moea64_deactivate(struct thread *td)
1293
{
1294
pmap_t pm;
1295
1296
__asm __volatile("isync; slbie %0" :: "r"(USER_ADDR));
1297
1298
pm = &td->td_proc->p_vmspace->vm_pmap;
1299
CPU_CLR(PCPU_GET(cpuid), &pm->pm_active);
1300
#ifdef __powerpc64__
1301
PCPU_SET(aim.userslb, NULL);
1302
#else
1303
PCPU_SET(curpmap, NULL);
1304
#endif
1305
}
1306
1307
void
1308
moea64_unwire(pmap_t pm, vm_offset_t sva, vm_offset_t eva)
1309
{
1310
struct pvo_entry key, *pvo;
1311
vm_page_t m;
1312
int64_t refchg;
1313
1314
key.pvo_vaddr = sva;
1315
PMAP_LOCK(pm);
1316
for (pvo = RB_NFIND(pvo_tree, &pm->pmap_pvo, &key);
1317
pvo != NULL && PVO_VADDR(pvo) < eva;
1318
pvo = RB_NEXT(pvo_tree, &pm->pmap_pvo, pvo)) {
1319
if (PVO_IS_SP(pvo)) {
1320
if (moea64_sp_pvo_in_range(pvo, sva, eva)) {
1321
pvo = moea64_sp_unwire(pvo);
1322
continue;
1323
} else {
1324
CTR1(KTR_PMAP, "%s: demote before unwire",
1325
__func__);
1326
moea64_sp_demote(pvo);
1327
}
1328
}
1329
1330
if ((pvo->pvo_vaddr & PVO_WIRED) == 0)
1331
panic("moea64_unwire: pvo %p is missing PVO_WIRED",
1332
pvo);
1333
pvo->pvo_vaddr &= ~PVO_WIRED;
1334
refchg = moea64_pte_replace(pvo, 0 /* No invalidation */);
1335
if ((pvo->pvo_vaddr & PVO_MANAGED) &&
1336
(pvo->pvo_pte.prot & VM_PROT_WRITE)) {
1337
if (refchg < 0)
1338
refchg = LPTE_CHG;
1339
m = PHYS_TO_VM_PAGE(PVO_PADDR(pvo));
1340
1341
refchg |= atomic_readandclear_32(&m->md.mdpg_attrs);
1342
if (refchg & LPTE_CHG)
1343
vm_page_dirty(m);
1344
if (refchg & LPTE_REF)
1345
vm_page_aflag_set(m, PGA_REFERENCED);
1346
}
1347
pm->pm_stats.wired_count--;
1348
}
1349
PMAP_UNLOCK(pm);
1350
}
1351
1352
static int
1353
moea64_mincore(pmap_t pmap, vm_offset_t addr, vm_paddr_t *pap)
1354
{
1355
struct pvo_entry *pvo;
1356
vm_paddr_t pa;
1357
vm_page_t m;
1358
int val;
1359
bool managed;
1360
1361
PMAP_LOCK(pmap);
1362
1363
pvo = moea64_pvo_find_va(pmap, addr);
1364
if (pvo != NULL) {
1365
pa = PVO_PADDR(pvo);
1366
m = PHYS_TO_VM_PAGE(pa);
1367
managed = (pvo->pvo_vaddr & PVO_MANAGED) == PVO_MANAGED;
1368
if (PVO_IS_SP(pvo))
1369
val = MINCORE_INCORE | MINCORE_PSIND(1);
1370
else
1371
val = MINCORE_INCORE;
1372
} else {
1373
PMAP_UNLOCK(pmap);
1374
return (0);
1375
}
1376
1377
PMAP_UNLOCK(pmap);
1378
1379
if (m == NULL)
1380
return (0);
1381
1382
if (managed) {
1383
if (moea64_is_modified(m))
1384
val |= MINCORE_MODIFIED | MINCORE_MODIFIED_OTHER;
1385
1386
if (moea64_is_referenced(m))
1387
val |= MINCORE_REFERENCED | MINCORE_REFERENCED_OTHER;
1388
}
1389
1390
if ((val & (MINCORE_MODIFIED_OTHER | MINCORE_REFERENCED_OTHER)) !=
1391
(MINCORE_MODIFIED_OTHER | MINCORE_REFERENCED_OTHER) &&
1392
managed) {
1393
*pap = pa;
1394
}
1395
1396
return (val);
1397
}
1398
1399
/*
1400
* This goes through and sets the physical address of our
1401
* special scratch PTE to the PA we want to zero or copy. Because
1402
* of locking issues (this can get called in pvo_enter() by
1403
* the UMA allocator), we can't use most other utility functions here
1404
*/
1405
1406
static __inline
1407
void moea64_set_scratchpage_pa(int which, vm_paddr_t pa)
1408
{
1409
struct pvo_entry *pvo;
1410
1411
KASSERT(!hw_direct_map, ("Using OEA64 scratchpage with a direct map!"));
1412
mtx_assert(&moea64_scratchpage_mtx, MA_OWNED);
1413
1414
pvo = moea64_scratchpage_pvo[which];
1415
PMAP_LOCK(pvo->pvo_pmap);
1416
pvo->pvo_pte.pa =
1417
moea64_calc_wimg(pa, VM_MEMATTR_DEFAULT) | (uint64_t)pa;
1418
moea64_pte_replace(pvo, MOEA64_PTE_INVALIDATE);
1419
PMAP_UNLOCK(pvo->pvo_pmap);
1420
isync();
1421
}
1422
1423
void
1424
moea64_copy_page(vm_page_t msrc, vm_page_t mdst)
1425
{
1426
mtx_lock(&moea64_scratchpage_mtx);
1427
1428
moea64_set_scratchpage_pa(0, VM_PAGE_TO_PHYS(msrc));
1429
moea64_set_scratchpage_pa(1, VM_PAGE_TO_PHYS(mdst));
1430
1431
bcopy((void *)moea64_scratchpage_va[0],
1432
(void *)moea64_scratchpage_va[1], PAGE_SIZE);
1433
1434
mtx_unlock(&moea64_scratchpage_mtx);
1435
}
1436
1437
void
1438
moea64_copy_page_dmap(vm_page_t msrc, vm_page_t mdst)
1439
{
1440
vm_offset_t dst;
1441
vm_offset_t src;
1442
1443
dst = VM_PAGE_TO_PHYS(mdst);
1444
src = VM_PAGE_TO_PHYS(msrc);
1445
1446
bcopy((void *)PHYS_TO_DMAP(src), (void *)PHYS_TO_DMAP(dst),
1447
PAGE_SIZE);
1448
}
1449
1450
inline void
1451
moea64_copy_pages_dmap(vm_page_t *ma, vm_offset_t a_offset,
1452
vm_page_t *mb, vm_offset_t b_offset, int xfersize)
1453
{
1454
void *a_cp, *b_cp;
1455
vm_offset_t a_pg_offset, b_pg_offset;
1456
int cnt;
1457
1458
while (xfersize > 0) {
1459
a_pg_offset = a_offset & PAGE_MASK;
1460
cnt = min(xfersize, PAGE_SIZE - a_pg_offset);
1461
a_cp = (char *)(uintptr_t)PHYS_TO_DMAP(
1462
VM_PAGE_TO_PHYS(ma[a_offset >> PAGE_SHIFT])) +
1463
a_pg_offset;
1464
b_pg_offset = b_offset & PAGE_MASK;
1465
cnt = min(cnt, PAGE_SIZE - b_pg_offset);
1466
b_cp = (char *)(uintptr_t)PHYS_TO_DMAP(
1467
VM_PAGE_TO_PHYS(mb[b_offset >> PAGE_SHIFT])) +
1468
b_pg_offset;
1469
bcopy(a_cp, b_cp, cnt);
1470
a_offset += cnt;
1471
b_offset += cnt;
1472
xfersize -= cnt;
1473
}
1474
}
1475
1476
void
1477
moea64_copy_pages(vm_page_t *ma, vm_offset_t a_offset,
1478
vm_page_t *mb, vm_offset_t b_offset, int xfersize)
1479
{
1480
void *a_cp, *b_cp;
1481
vm_offset_t a_pg_offset, b_pg_offset;
1482
int cnt;
1483
1484
mtx_lock(&moea64_scratchpage_mtx);
1485
while (xfersize > 0) {
1486
a_pg_offset = a_offset & PAGE_MASK;
1487
cnt = min(xfersize, PAGE_SIZE - a_pg_offset);
1488
moea64_set_scratchpage_pa(0,
1489
VM_PAGE_TO_PHYS(ma[a_offset >> PAGE_SHIFT]));
1490
a_cp = (char *)moea64_scratchpage_va[0] + a_pg_offset;
1491
b_pg_offset = b_offset & PAGE_MASK;
1492
cnt = min(cnt, PAGE_SIZE - b_pg_offset);
1493
moea64_set_scratchpage_pa(1,
1494
VM_PAGE_TO_PHYS(mb[b_offset >> PAGE_SHIFT]));
1495
b_cp = (char *)moea64_scratchpage_va[1] + b_pg_offset;
1496
bcopy(a_cp, b_cp, cnt);
1497
a_offset += cnt;
1498
b_offset += cnt;
1499
xfersize -= cnt;
1500
}
1501
mtx_unlock(&moea64_scratchpage_mtx);
1502
}
1503
1504
void
1505
moea64_zero_page_area(vm_page_t m, int off, int size)
1506
{
1507
vm_paddr_t pa = VM_PAGE_TO_PHYS(m);
1508
1509
if (size + off > PAGE_SIZE)
1510
panic("moea64_zero_page: size + off > PAGE_SIZE");
1511
1512
if (hw_direct_map) {
1513
bzero((caddr_t)(uintptr_t)PHYS_TO_DMAP(pa) + off, size);
1514
} else {
1515
mtx_lock(&moea64_scratchpage_mtx);
1516
moea64_set_scratchpage_pa(0, pa);
1517
bzero((caddr_t)moea64_scratchpage_va[0] + off, size);
1518
mtx_unlock(&moea64_scratchpage_mtx);
1519
}
1520
}
1521
1522
/*
1523
* Zero a page of physical memory by temporarily mapping it
1524
*/
1525
void
1526
moea64_zero_page(vm_page_t m)
1527
{
1528
vm_paddr_t pa = VM_PAGE_TO_PHYS(m);
1529
vm_offset_t va;
1530
1531
mtx_lock(&moea64_scratchpage_mtx);
1532
1533
moea64_set_scratchpage_pa(0, pa);
1534
va = moea64_scratchpage_va[0];
1535
1536
bzero((void *)va, PAGE_SIZE);
1537
1538
mtx_unlock(&moea64_scratchpage_mtx);
1539
}
1540
1541
void
1542
moea64_zero_page_dmap(vm_page_t m)
1543
{
1544
vm_paddr_t pa = VM_PAGE_TO_PHYS(m);
1545
vm_offset_t va;
1546
1547
va = PHYS_TO_DMAP(pa);
1548
bzero((void *)va, PAGE_SIZE);
1549
}
1550
1551
vm_offset_t
1552
moea64_quick_enter_page(vm_page_t m)
1553
{
1554
struct pvo_entry *pvo;
1555
vm_paddr_t pa = VM_PAGE_TO_PHYS(m);
1556
1557
/*
1558
* MOEA64_PTE_REPLACE does some locking, so we can't just grab
1559
* a critical section and access the PCPU data like on i386.
1560
* Instead, pin the thread and grab the PCPU lock to prevent
1561
* a preempting thread from using the same PCPU data.
1562
*/
1563
sched_pin();
1564
1565
mtx_assert(PCPU_PTR(aim.qmap_lock), MA_NOTOWNED);
1566
pvo = PCPU_GET(aim.qmap_pvo);
1567
1568
mtx_lock(PCPU_PTR(aim.qmap_lock));
1569
pvo->pvo_pte.pa = moea64_calc_wimg(pa, pmap_page_get_memattr(m)) |
1570
(uint64_t)pa;
1571
moea64_pte_replace(pvo, MOEA64_PTE_INVALIDATE);
1572
isync();
1573
1574
return (PCPU_GET(qmap_addr));
1575
}
1576
1577
vm_offset_t
1578
moea64_quick_enter_page_dmap(vm_page_t m)
1579
{
1580
1581
return (PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m)));
1582
}
1583
1584
void
1585
moea64_quick_remove_page(vm_offset_t addr)
1586
{
1587
1588
mtx_assert(PCPU_PTR(aim.qmap_lock), MA_OWNED);
1589
KASSERT(PCPU_GET(qmap_addr) == addr,
1590
("moea64_quick_remove_page: invalid address"));
1591
mtx_unlock(PCPU_PTR(aim.qmap_lock));
1592
sched_unpin();
1593
}
1594
1595
bool
1596
moea64_page_is_mapped(vm_page_t m)
1597
{
1598
return (!LIST_EMPTY(&(m)->md.mdpg_pvoh));
1599
}
1600
1601
/*
1602
* Map the given physical page at the specified virtual address in the
1603
* target pmap with the protection requested. If specified the page
1604
* will be wired down.
1605
*/
1606
1607
int
1608
moea64_enter(pmap_t pmap, vm_offset_t va, vm_page_t m,
1609
vm_prot_t prot, u_int flags, int8_t psind)
1610
{
1611
struct pvo_entry *pvo, *oldpvo, *tpvo;
1612
struct pvo_head *pvo_head;
1613
uint64_t pte_lo;
1614
int error;
1615
vm_paddr_t pa;
1616
1617
if ((m->oflags & VPO_UNMANAGED) == 0) {
1618
if ((flags & PMAP_ENTER_QUICK_LOCKED) == 0)
1619
VM_PAGE_OBJECT_BUSY_ASSERT(m);
1620
else
1621
VM_OBJECT_ASSERT_LOCKED(m->object);
1622
}
1623
1624
if (psind > 0)
1625
return (moea64_sp_enter(pmap, va, m, prot, flags, psind));
1626
1627
pvo = alloc_pvo_entry(0);
1628
if (pvo == NULL)
1629
return (KERN_RESOURCE_SHORTAGE);
1630
pvo->pvo_pmap = NULL; /* to be filled in later */
1631
pvo->pvo_pte.prot = prot;
1632
1633
pa = VM_PAGE_TO_PHYS(m);
1634
pte_lo = moea64_calc_wimg(pa, pmap_page_get_memattr(m));
1635
pvo->pvo_pte.pa = pa | pte_lo;
1636
1637
if ((flags & PMAP_ENTER_WIRED) != 0)
1638
pvo->pvo_vaddr |= PVO_WIRED;
1639
1640
if ((m->oflags & VPO_UNMANAGED) != 0 || !moea64_initialized) {
1641
pvo_head = NULL;
1642
} else {
1643
pvo_head = &m->md.mdpg_pvoh;
1644
pvo->pvo_vaddr |= PVO_MANAGED;
1645
}
1646
1647
PV_LOCK(pa);
1648
PMAP_LOCK(pmap);
1649
if (pvo->pvo_pmap == NULL)
1650
init_pvo_entry(pvo, pmap, va);
1651
1652
if (moea64_ps_enabled(pmap) &&
1653
(tpvo = moea64_pvo_find_va(pmap, va & ~HPT_SP_MASK)) != NULL &&
1654
PVO_IS_SP(tpvo)) {
1655
/* Demote SP before entering a regular page */
1656
CTR2(KTR_PMAP, "%s: demote before enter: va=%#jx",
1657
__func__, (uintmax_t)va);
1658
moea64_sp_demote_aligned(tpvo);
1659
}
1660
1661
if (prot & VM_PROT_WRITE)
1662
if (pmap_bootstrapped &&
1663
(m->oflags & VPO_UNMANAGED) == 0)
1664
vm_page_aflag_set(m, PGA_WRITEABLE);
1665
1666
error = moea64_pvo_enter(pvo, pvo_head, &oldpvo);
1667
if (error == EEXIST) {
1668
if (oldpvo->pvo_vaddr == pvo->pvo_vaddr &&
1669
oldpvo->pvo_pte.pa == pvo->pvo_pte.pa &&
1670
oldpvo->pvo_pte.prot == prot) {
1671
/* Identical mapping already exists */
1672
error = 0;
1673
1674
/* If not in page table, reinsert it */
1675
if (moea64_pte_synch(oldpvo) < 0) {
1676
STAT_MOEA64(moea64_pte_overflow--);
1677
moea64_pte_insert(oldpvo);
1678
}
1679
1680
/* Then just clean up and go home */
1681
PMAP_UNLOCK(pmap);
1682
PV_UNLOCK(pa);
1683
free_pvo_entry(pvo);
1684
pvo = NULL;
1685
goto out;
1686
} else {
1687
/* Otherwise, need to kill it first */
1688
KASSERT(oldpvo->pvo_pmap == pmap, ("pmap of old "
1689
"mapping does not match new mapping"));
1690
moea64_pvo_remove_from_pmap(oldpvo);
1691
moea64_pvo_enter(pvo, pvo_head, NULL);
1692
}
1693
}
1694
PMAP_UNLOCK(pmap);
1695
PV_UNLOCK(pa);
1696
1697
/* Free any dead pages */
1698
if (error == EEXIST) {
1699
moea64_pvo_remove_from_page(oldpvo);
1700
free_pvo_entry(oldpvo);
1701
}
1702
1703
out:
1704
/*
1705
* Flush the page from the instruction cache if this page is
1706
* mapped executable and cacheable.
1707
*/
1708
if (pmap != kernel_pmap && (m->a.flags & PGA_EXECUTABLE) == 0 &&
1709
(pte_lo & (LPTE_I | LPTE_G | LPTE_NOEXEC)) == 0) {
1710
vm_page_aflag_set(m, PGA_EXECUTABLE);
1711
moea64_syncicache(pmap, va, pa, PAGE_SIZE);
1712
}
1713
1714
#if VM_NRESERVLEVEL > 0
1715
/*
1716
* Try to promote pages.
1717
*
1718
* If the VA of the entered page is not aligned with its PA,
1719
* don't try page promotion as it is not possible.
1720
* This reduces the number of promotion failures dramatically.
1721
*
1722
* Ignore VM_PROT_NO_PROMOTE unless PMAP_ENTER_QUICK_LOCKED.
1723
*/
1724
if (moea64_ps_enabled(pmap) && pmap != kernel_pmap && pvo != NULL &&
1725
(pvo->pvo_vaddr & PVO_MANAGED) != 0 &&
1726
(va & HPT_SP_MASK) == (pa & HPT_SP_MASK) &&
1727
((prot & VM_PROT_NO_PROMOTE) == 0 ||
1728
(flags & PMAP_ENTER_QUICK_LOCKED) == 0) &&
1729
(m->flags & PG_FICTITIOUS) == 0 &&
1730
vm_reserv_level_iffullpop(m) == 0)
1731
moea64_sp_promote(pmap, va, m);
1732
#endif
1733
1734
return (KERN_SUCCESS);
1735
}
1736
1737
static void
1738
moea64_syncicache(pmap_t pmap, vm_offset_t va, vm_paddr_t pa,
1739
vm_size_t sz)
1740
{
1741
1742
/*
1743
* This is much trickier than on older systems because
1744
* we can't sync the icache on physical addresses directly
1745
* without a direct map. Instead we check a couple of cases
1746
* where the memory is already mapped in and, failing that,
1747
* use the same trick we use for page zeroing to create
1748
* a temporary mapping for this physical address.
1749
*/
1750
1751
if (!pmap_bootstrapped) {
1752
/*
1753
* If PMAP is not bootstrapped, we are likely to be
1754
* in real mode.
1755
*/
1756
__syncicache((void *)(uintptr_t)pa, sz);
1757
} else if (pmap == kernel_pmap) {
1758
__syncicache((void *)va, sz);
1759
} else if (hw_direct_map) {
1760
__syncicache((void *)(uintptr_t)PHYS_TO_DMAP(pa), sz);
1761
} else {
1762
/* Use the scratch page to set up a temp mapping */
1763
1764
mtx_lock(&moea64_scratchpage_mtx);
1765
1766
moea64_set_scratchpage_pa(1, pa & ~ADDR_POFF);
1767
__syncicache((void *)(moea64_scratchpage_va[1] +
1768
(va & ADDR_POFF)), sz);
1769
1770
mtx_unlock(&moea64_scratchpage_mtx);
1771
}
1772
}
1773
1774
/*
1775
* Maps a sequence of resident pages belonging to the same object.
1776
* The sequence begins with the given page m_start. This page is
1777
* mapped at the given virtual address start. Each subsequent page is
1778
* mapped at a virtual address that is offset from start by the same
1779
* amount as the page is offset from m_start within the object. The
1780
* last page in the sequence is the page with the largest offset from
1781
* m_start that can be mapped at a virtual address less than the given
1782
* virtual address end. Not every virtual page between start and end
1783
* is mapped; only those for which a resident page exists with the
1784
* corresponding offset from m_start are mapped.
1785
*/
1786
void
1787
moea64_enter_object(pmap_t pm, vm_offset_t start, vm_offset_t end,
1788
vm_page_t m_start, vm_prot_t prot)
1789
{
1790
struct pctrie_iter pages;
1791
vm_page_t m;
1792
vm_offset_t va;
1793
int8_t psind;
1794
1795
VM_OBJECT_ASSERT_LOCKED(m_start->object);
1796
1797
vm_page_iter_limit_init(&pages, m_start->object,
1798
m_start->pindex + atop(end - start));
1799
m = vm_radix_iter_lookup(&pages, m_start->pindex);
1800
while (m != NULL) {
1801
va = start + ptoa(m->pindex - m_start->pindex);
1802
if ((va & HPT_SP_MASK) == 0 && va + HPT_SP_SIZE <= end &&
1803
m->psind == 1 && moea64_ps_enabled(pm))
1804
psind = 1;
1805
else
1806
psind = 0;
1807
moea64_enter(pm, va, m, prot &
1808
(VM_PROT_READ | VM_PROT_EXECUTE),
1809
PMAP_ENTER_NOSLEEP | PMAP_ENTER_QUICK_LOCKED, psind);
1810
if (psind == 1)
1811
m = vm_radix_iter_jump(&pages, HPT_SP_SIZE / PAGE_SIZE);
1812
else
1813
m = vm_radix_iter_step(&pages);
1814
}
1815
}
1816
1817
void
1818
moea64_enter_quick(pmap_t pm, vm_offset_t va, vm_page_t m,
1819
vm_prot_t prot)
1820
{
1821
1822
moea64_enter(pm, va, m, prot & (VM_PROT_READ | VM_PROT_EXECUTE |
1823
VM_PROT_NO_PROMOTE), PMAP_ENTER_NOSLEEP | PMAP_ENTER_QUICK_LOCKED,
1824
0);
1825
}
1826
1827
vm_paddr_t
1828
moea64_extract(pmap_t pm, vm_offset_t va)
1829
{
1830
struct pvo_entry *pvo;
1831
vm_paddr_t pa;
1832
1833
PMAP_LOCK(pm);
1834
pvo = moea64_pvo_find_va(pm, va);
1835
if (pvo == NULL)
1836
pa = 0;
1837
else
1838
pa = PVO_PADDR(pvo) | (va - PVO_VADDR(pvo));
1839
PMAP_UNLOCK(pm);
1840
1841
return (pa);
1842
}
1843
1844
/*
1845
* Atomically extract and hold the physical page with the given
1846
* pmap and virtual address pair if that mapping permits the given
1847
* protection.
1848
*/
1849
vm_page_t
1850
moea64_extract_and_hold(pmap_t pmap, vm_offset_t va, vm_prot_t prot)
1851
{
1852
struct pvo_entry *pvo;
1853
vm_page_t m;
1854
1855
m = NULL;
1856
PMAP_LOCK(pmap);
1857
pvo = moea64_pvo_find_va(pmap, va & ~ADDR_POFF);
1858
if (pvo != NULL && (pvo->pvo_pte.prot & prot) == prot) {
1859
m = PHYS_TO_VM_PAGE(PVO_PADDR(pvo));
1860
if (!vm_page_wire_mapped(m))
1861
m = NULL;
1862
}
1863
PMAP_UNLOCK(pmap);
1864
return (m);
1865
}
1866
1867
static void *
1868
moea64_uma_page_alloc(uma_zone_t zone, vm_size_t bytes, int domain,
1869
uint8_t *flags, int wait)
1870
{
1871
struct pvo_entry *pvo;
1872
vm_offset_t va;
1873
vm_page_t m;
1874
int needed_lock;
1875
1876
/*
1877
* This entire routine is a horrible hack to avoid bothering kmem
1878
* for new KVA addresses. Because this can get called from inside
1879
* kmem allocation routines, calling kmem for a new address here
1880
* can lead to multiply locking non-recursive mutexes.
1881
*/
1882
1883
*flags = UMA_SLAB_PRIV;
1884
needed_lock = !PMAP_LOCKED(kernel_pmap);
1885
1886
m = vm_page_alloc_noobj_domain(domain, malloc2vm_flags(wait) |
1887
VM_ALLOC_WIRED);
1888
if (m == NULL)
1889
return (NULL);
1890
1891
va = VM_PAGE_TO_PHYS(m);
1892
1893
pvo = alloc_pvo_entry(1 /* bootstrap */);
1894
1895
pvo->pvo_pte.prot = VM_PROT_READ | VM_PROT_WRITE;
1896
pvo->pvo_pte.pa = VM_PAGE_TO_PHYS(m) | LPTE_M;
1897
1898
if (needed_lock)
1899
PMAP_LOCK(kernel_pmap);
1900
1901
init_pvo_entry(pvo, kernel_pmap, va);
1902
pvo->pvo_vaddr |= PVO_WIRED;
1903
1904
moea64_pvo_enter(pvo, NULL, NULL);
1905
1906
if (needed_lock)
1907
PMAP_UNLOCK(kernel_pmap);
1908
1909
return (void *)va;
1910
}
1911
1912
extern int elf32_nxstack;
1913
1914
void
1915
moea64_init(void)
1916
{
1917
1918
CTR0(KTR_PMAP, "moea64_init");
1919
1920
moea64_pvo_zone = uma_zcreate("UPVO entry", sizeof (struct pvo_entry),
1921
NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
1922
UMA_ZONE_VM | UMA_ZONE_NOFREE);
1923
1924
/*
1925
* Are large page mappings enabled?
1926
*
1927
* While HPT superpages are not better tested, leave it disabled by
1928
* default.
1929
*/
1930
superpages_enabled = 0;
1931
TUNABLE_INT_FETCH("vm.pmap.superpages_enabled", &superpages_enabled);
1932
if (superpages_enabled) {
1933
KASSERT(MAXPAGESIZES > 1 && pagesizes[1] == 0,
1934
("moea64_init: can't assign to pagesizes[1]"));
1935
1936
if (moea64_large_page_size == 0) {
1937
printf("mmu_oea64: HW does not support large pages. "
1938
"Disabling superpages...\n");
1939
superpages_enabled = 0;
1940
} else if (!moea64_has_lp_4k_16m) {
1941
printf("mmu_oea64: "
1942
"HW does not support mixed 4KB/16MB page sizes. "
1943
"Disabling superpages...\n");
1944
superpages_enabled = 0;
1945
} else
1946
pagesizes[1] = HPT_SP_SIZE;
1947
}
1948
1949
if (!hw_direct_map) {
1950
uma_zone_set_allocf(moea64_pvo_zone, moea64_uma_page_alloc);
1951
}
1952
1953
#ifdef COMPAT_FREEBSD32
1954
elf32_nxstack = 1;
1955
#endif
1956
1957
moea64_initialized = true;
1958
}
1959
1960
bool
1961
moea64_is_referenced(vm_page_t m)
1962
{
1963
1964
KASSERT((m->oflags & VPO_UNMANAGED) == 0,
1965
("moea64_is_referenced: page %p is not managed", m));
1966
1967
return (moea64_query_bit(m, LPTE_REF));
1968
}
1969
1970
bool
1971
moea64_is_modified(vm_page_t m)
1972
{
1973
1974
KASSERT((m->oflags & VPO_UNMANAGED) == 0,
1975
("moea64_is_modified: page %p is not managed", m));
1976
1977
/*
1978
* If the page is not busied then this check is racy.
1979
*/
1980
if (!pmap_page_is_write_mapped(m))
1981
return (false);
1982
1983
return (moea64_query_bit(m, LPTE_CHG));
1984
}
1985
1986
bool
1987
moea64_is_prefaultable(pmap_t pmap, vm_offset_t va)
1988
{
1989
struct pvo_entry *pvo;
1990
bool rv = true;
1991
1992
PMAP_LOCK(pmap);
1993
pvo = moea64_pvo_find_va(pmap, va & ~ADDR_POFF);
1994
if (pvo != NULL)
1995
rv = false;
1996
PMAP_UNLOCK(pmap);
1997
return (rv);
1998
}
1999
2000
void
2001
moea64_clear_modify(vm_page_t m)
2002
{
2003
2004
KASSERT((m->oflags & VPO_UNMANAGED) == 0,
2005
("moea64_clear_modify: page %p is not managed", m));
2006
vm_page_assert_busied(m);
2007
2008
if (!pmap_page_is_write_mapped(m))
2009
return;
2010
moea64_clear_bit(m, LPTE_CHG);
2011
}
2012
2013
/*
2014
* Clear the write and modified bits in each of the given page's mappings.
2015
*/
2016
void
2017
moea64_remove_write(vm_page_t m)
2018
{
2019
struct pvo_entry *pvo;
2020
int64_t refchg, ret;
2021
pmap_t pmap;
2022
2023
KASSERT((m->oflags & VPO_UNMANAGED) == 0,
2024
("moea64_remove_write: page %p is not managed", m));
2025
vm_page_assert_busied(m);
2026
2027
if (!pmap_page_is_write_mapped(m))
2028
return;
2029
2030
powerpc_sync();
2031
PV_PAGE_LOCK(m);
2032
refchg = 0;
2033
LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
2034
pmap = pvo->pvo_pmap;
2035
PMAP_LOCK(pmap);
2036
if (!(pvo->pvo_vaddr & PVO_DEAD) &&
2037
(pvo->pvo_pte.prot & VM_PROT_WRITE)) {
2038
if (PVO_IS_SP(pvo)) {
2039
CTR1(KTR_PMAP, "%s: demote before remwr",
2040
__func__);
2041
moea64_sp_demote(pvo);
2042
}
2043
pvo->pvo_pte.prot &= ~VM_PROT_WRITE;
2044
ret = moea64_pte_replace(pvo, MOEA64_PTE_PROT_UPDATE);
2045
if (ret < 0)
2046
ret = LPTE_CHG;
2047
refchg |= ret;
2048
if (pvo->pvo_pmap == kernel_pmap)
2049
isync();
2050
}
2051
PMAP_UNLOCK(pmap);
2052
}
2053
if ((refchg | atomic_readandclear_32(&m->md.mdpg_attrs)) & LPTE_CHG)
2054
vm_page_dirty(m);
2055
vm_page_aflag_clear(m, PGA_WRITEABLE);
2056
PV_PAGE_UNLOCK(m);
2057
}
2058
2059
/*
2060
* moea64_ts_referenced:
2061
*
2062
* Return a count of reference bits for a page, clearing those bits.
2063
* It is not necessary for every reference bit to be cleared, but it
2064
* is necessary that 0 only be returned when there are truly no
2065
* reference bits set.
2066
*
2067
* XXX: The exact number of bits to check and clear is a matter that
2068
* should be tested and standardized at some point in the future for
2069
* optimal aging of shared pages.
2070
*/
2071
int
2072
moea64_ts_referenced(vm_page_t m)
2073
{
2074
2075
KASSERT((m->oflags & VPO_UNMANAGED) == 0,
2076
("moea64_ts_referenced: page %p is not managed", m));
2077
return (moea64_clear_bit(m, LPTE_REF));
2078
}
2079
2080
/*
2081
* Modify the WIMG settings of all mappings for a page.
2082
*/
2083
void
2084
moea64_page_set_memattr(vm_page_t m, vm_memattr_t ma)
2085
{
2086
struct pvo_entry *pvo;
2087
int64_t refchg;
2088
pmap_t pmap;
2089
uint64_t lo;
2090
2091
CTR3(KTR_PMAP, "%s: pa=%#jx, ma=%#x",
2092
__func__, (uintmax_t)VM_PAGE_TO_PHYS(m), ma);
2093
2094
if (m->md.mdpg_cache_attrs == ma)
2095
return;
2096
2097
if ((m->oflags & VPO_UNMANAGED) != 0) {
2098
m->md.mdpg_cache_attrs = ma;
2099
return;
2100
}
2101
2102
lo = moea64_calc_wimg(VM_PAGE_TO_PHYS(m), ma);
2103
2104
PV_PAGE_LOCK(m);
2105
LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
2106
pmap = pvo->pvo_pmap;
2107
PMAP_LOCK(pmap);
2108
if (!(pvo->pvo_vaddr & PVO_DEAD)) {
2109
if (PVO_IS_SP(pvo)) {
2110
CTR1(KTR_PMAP,
2111
"%s: demote before set_memattr", __func__);
2112
moea64_sp_demote(pvo);
2113
}
2114
pvo->pvo_pte.pa &= ~LPTE_WIMG;
2115
pvo->pvo_pte.pa |= lo;
2116
refchg = moea64_pte_replace(pvo, MOEA64_PTE_INVALIDATE);
2117
if (refchg < 0)
2118
refchg = (pvo->pvo_pte.prot & VM_PROT_WRITE) ?
2119
LPTE_CHG : 0;
2120
if ((pvo->pvo_vaddr & PVO_MANAGED) &&
2121
(pvo->pvo_pte.prot & VM_PROT_WRITE)) {
2122
refchg |=
2123
atomic_readandclear_32(&m->md.mdpg_attrs);
2124
if (refchg & LPTE_CHG)
2125
vm_page_dirty(m);
2126
if (refchg & LPTE_REF)
2127
vm_page_aflag_set(m, PGA_REFERENCED);
2128
}
2129
if (pvo->pvo_pmap == kernel_pmap)
2130
isync();
2131
}
2132
PMAP_UNLOCK(pmap);
2133
}
2134
m->md.mdpg_cache_attrs = ma;
2135
PV_PAGE_UNLOCK(m);
2136
}
2137
2138
/*
2139
* Map a wired page into kernel virtual address space.
2140
*/
2141
void
2142
moea64_kenter_attr(vm_offset_t va, vm_paddr_t pa, vm_memattr_t ma)
2143
{
2144
int error;
2145
struct pvo_entry *pvo, *oldpvo;
2146
2147
do {
2148
pvo = alloc_pvo_entry(0);
2149
if (pvo == NULL)
2150
vm_wait(NULL);
2151
} while (pvo == NULL);
2152
pvo->pvo_pte.prot = VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE;
2153
pvo->pvo_pte.pa = (pa & ~ADDR_POFF) | moea64_calc_wimg(pa, ma);
2154
pvo->pvo_vaddr |= PVO_WIRED;
2155
2156
PMAP_LOCK(kernel_pmap);
2157
oldpvo = moea64_pvo_find_va(kernel_pmap, va);
2158
if (oldpvo != NULL)
2159
moea64_pvo_remove_from_pmap(oldpvo);
2160
init_pvo_entry(pvo, kernel_pmap, va);
2161
error = moea64_pvo_enter(pvo, NULL, NULL);
2162
PMAP_UNLOCK(kernel_pmap);
2163
2164
/* Free any dead pages */
2165
if (oldpvo != NULL) {
2166
moea64_pvo_remove_from_page(oldpvo);
2167
free_pvo_entry(oldpvo);
2168
}
2169
2170
if (error != 0)
2171
panic("moea64_kenter: failed to enter va %#zx pa %#jx: %d", va,
2172
(uintmax_t)pa, error);
2173
}
2174
2175
void
2176
moea64_kenter(vm_offset_t va, vm_paddr_t pa)
2177
{
2178
2179
moea64_kenter_attr(va, pa, VM_MEMATTR_DEFAULT);
2180
}
2181
2182
/*
2183
* Extract the physical page address associated with the given kernel virtual
2184
* address.
2185
*/
2186
vm_paddr_t
2187
moea64_kextract(vm_offset_t va)
2188
{
2189
struct pvo_entry *pvo;
2190
vm_paddr_t pa;
2191
2192
/*
2193
* Shortcut the direct-mapped case when applicable. We never put
2194
* anything but 1:1 (or 62-bit aliased) mappings below
2195
* VM_MIN_KERNEL_ADDRESS.
2196
*/
2197
if (va < VM_MIN_KERNEL_ADDRESS)
2198
return (va & ~DMAP_BASE_ADDRESS);
2199
2200
PMAP_LOCK(kernel_pmap);
2201
pvo = moea64_pvo_find_va(kernel_pmap, va);
2202
KASSERT(pvo != NULL, ("moea64_kextract: no addr found for %#" PRIxPTR,
2203
va));
2204
pa = PVO_PADDR(pvo) | (va - PVO_VADDR(pvo));
2205
PMAP_UNLOCK(kernel_pmap);
2206
return (pa);
2207
}
2208
2209
/*
2210
* Remove a wired page from kernel virtual address space.
2211
*/
2212
void
2213
moea64_kremove(vm_offset_t va)
2214
{
2215
moea64_remove(kernel_pmap, va, va + PAGE_SIZE);
2216
}
2217
2218
/*
2219
* Provide a kernel pointer corresponding to a given userland pointer.
2220
* The returned pointer is valid until the next time this function is
2221
* called in this thread. This is used internally in copyin/copyout.
2222
*/
2223
static int
2224
moea64_map_user_ptr(pmap_t pm, volatile const void *uaddr,
2225
void **kaddr, size_t ulen, size_t *klen)
2226
{
2227
size_t l;
2228
#ifdef __powerpc64__
2229
struct slb *slb;
2230
#endif
2231
register_t slbv;
2232
2233
*kaddr = (char *)USER_ADDR + ((uintptr_t)uaddr & ~SEGMENT_MASK);
2234
l = ((char *)USER_ADDR + SEGMENT_LENGTH) - (char *)(*kaddr);
2235
if (l > ulen)
2236
l = ulen;
2237
if (klen)
2238
*klen = l;
2239
else if (l != ulen)
2240
return (EFAULT);
2241
2242
#ifdef __powerpc64__
2243
/* Try lockless look-up first */
2244
slb = user_va_to_slb_entry(pm, (vm_offset_t)uaddr);
2245
2246
if (slb == NULL) {
2247
/* If it isn't there, we need to pre-fault the VSID */
2248
PMAP_LOCK(pm);
2249
slbv = va_to_vsid(pm, (vm_offset_t)uaddr) << SLBV_VSID_SHIFT;
2250
PMAP_UNLOCK(pm);
2251
} else {
2252
slbv = slb->slbv;
2253
}
2254
2255
/* Mark segment no-execute */
2256
slbv |= SLBV_N;
2257
#else
2258
slbv = va_to_vsid(pm, (vm_offset_t)uaddr);
2259
2260
/* Mark segment no-execute */
2261
slbv |= SR_N;
2262
#endif
2263
2264
/* If we have already set this VSID, we can just return */
2265
if (curthread->td_pcb->pcb_cpu.aim.usr_vsid == slbv)
2266
return (0);
2267
2268
__asm __volatile("isync");
2269
curthread->td_pcb->pcb_cpu.aim.usr_segm =
2270
(uintptr_t)uaddr >> ADDR_SR_SHFT;
2271
curthread->td_pcb->pcb_cpu.aim.usr_vsid = slbv;
2272
#ifdef __powerpc64__
2273
__asm __volatile ("slbie %0; slbmte %1, %2; isync" ::
2274
"r"(USER_ADDR), "r"(slbv), "r"(USER_SLB_SLBE));
2275
#else
2276
__asm __volatile("mtsr %0,%1; isync" :: "n"(USER_SR), "r"(slbv));
2277
#endif
2278
2279
return (0);
2280
}
2281
2282
/*
2283
* Figure out where a given kernel pointer (usually in a fault) points
2284
* to from the VM's perspective, potentially remapping into userland's
2285
* address space.
2286
*/
2287
static int
2288
moea64_decode_kernel_ptr(vm_offset_t addr, int *is_user,
2289
vm_offset_t *decoded_addr)
2290
{
2291
vm_offset_t user_sr;
2292
2293
if ((addr >> ADDR_SR_SHFT) == (USER_ADDR >> ADDR_SR_SHFT)) {
2294
user_sr = curthread->td_pcb->pcb_cpu.aim.usr_segm;
2295
addr &= ADDR_PIDX | ADDR_POFF;
2296
addr |= user_sr << ADDR_SR_SHFT;
2297
*decoded_addr = addr;
2298
*is_user = 1;
2299
} else {
2300
*decoded_addr = addr;
2301
*is_user = 0;
2302
}
2303
2304
return (0);
2305
}
2306
2307
/*
2308
* Map a range of physical addresses into kernel virtual address space.
2309
*
2310
* The value passed in *virt is a suggested virtual address for the mapping.
2311
* Architectures which can support a direct-mapped physical to virtual region
2312
* can return the appropriate address within that region, leaving '*virt'
2313
* unchanged. Other architectures should map the pages starting at '*virt' and
2314
* update '*virt' with the first usable address after the mapped region.
2315
*/
2316
vm_offset_t
2317
moea64_map(vm_offset_t *virt, vm_paddr_t pa_start,
2318
vm_paddr_t pa_end, int prot)
2319
{
2320
vm_offset_t sva, va;
2321
2322
if (hw_direct_map) {
2323
/*
2324
* Check if every page in the region is covered by the direct
2325
* map. The direct map covers all of physical memory. Use
2326
* moea64_calc_wimg() as a shortcut to see if the page is in
2327
* physical memory as a way to see if the direct map covers it.
2328
*/
2329
for (va = pa_start; va < pa_end; va += PAGE_SIZE)
2330
if (moea64_calc_wimg(va, VM_MEMATTR_DEFAULT) != LPTE_M)
2331
break;
2332
if (va == pa_end)
2333
return (PHYS_TO_DMAP(pa_start));
2334
}
2335
sva = *virt;
2336
va = sva;
2337
/* XXX respect prot argument */
2338
for (; pa_start < pa_end; pa_start += PAGE_SIZE, va += PAGE_SIZE)
2339
moea64_kenter(va, pa_start);
2340
*virt = va;
2341
2342
return (sva);
2343
}
2344
2345
/*
2346
* Returns true if the pmap's pv is one of the first
2347
* 16 pvs linked to from this page. This count may
2348
* be changed upwards or downwards in the future; it
2349
* is only necessary that true be returned for a small
2350
* subset of pmaps for proper page aging.
2351
*/
2352
bool
2353
moea64_page_exists_quick(pmap_t pmap, vm_page_t m)
2354
{
2355
int loops;
2356
struct pvo_entry *pvo;
2357
bool rv;
2358
2359
KASSERT((m->oflags & VPO_UNMANAGED) == 0,
2360
("moea64_page_exists_quick: page %p is not managed", m));
2361
loops = 0;
2362
rv = false;
2363
PV_PAGE_LOCK(m);
2364
LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
2365
if (!(pvo->pvo_vaddr & PVO_DEAD) && pvo->pvo_pmap == pmap) {
2366
rv = true;
2367
break;
2368
}
2369
if (++loops >= 16)
2370
break;
2371
}
2372
PV_PAGE_UNLOCK(m);
2373
return (rv);
2374
}
2375
2376
void
2377
moea64_page_init(vm_page_t m)
2378
{
2379
2380
m->md.mdpg_attrs = 0;
2381
m->md.mdpg_cache_attrs = VM_MEMATTR_DEFAULT;
2382
LIST_INIT(&m->md.mdpg_pvoh);
2383
}
2384
2385
/*
2386
* Return the number of managed mappings to the given physical page
2387
* that are wired.
2388
*/
2389
int
2390
moea64_page_wired_mappings(vm_page_t m)
2391
{
2392
struct pvo_entry *pvo;
2393
int count;
2394
2395
count = 0;
2396
if ((m->oflags & VPO_UNMANAGED) != 0)
2397
return (count);
2398
PV_PAGE_LOCK(m);
2399
LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink)
2400
if ((pvo->pvo_vaddr & (PVO_DEAD | PVO_WIRED)) == PVO_WIRED)
2401
count++;
2402
PV_PAGE_UNLOCK(m);
2403
return (count);
2404
}
2405
2406
static uintptr_t moea64_vsidcontext;
2407
2408
uintptr_t
2409
moea64_get_unique_vsid(void) {
2410
u_int entropy;
2411
register_t hash;
2412
uint32_t mask;
2413
int i;
2414
2415
entropy = 0;
2416
__asm __volatile("mftb %0" : "=r"(entropy));
2417
2418
mtx_lock(&moea64_slb_mutex);
2419
for (i = 0; i < NVSIDS; i += VSID_NBPW) {
2420
u_int n;
2421
2422
/*
2423
* Create a new value by multiplying by a prime and adding in
2424
* entropy from the timebase register. This is to make the
2425
* VSID more random so that the PT hash function collides
2426
* less often. (Note that the prime casues gcc to do shifts
2427
* instead of a multiply.)
2428
*/
2429
moea64_vsidcontext = (moea64_vsidcontext * 0x1105) + entropy;
2430
hash = moea64_vsidcontext & (NVSIDS - 1);
2431
if (hash == 0) /* 0 is special, avoid it */
2432
continue;
2433
n = hash >> 5;
2434
mask = 1 << (hash & (VSID_NBPW - 1));
2435
hash = (moea64_vsidcontext & VSID_HASHMASK);
2436
if (moea64_vsid_bitmap[n] & mask) { /* collision? */
2437
/* anything free in this bucket? */
2438
if (moea64_vsid_bitmap[n] == 0xffffffff) {
2439
entropy = (moea64_vsidcontext >> 20);
2440
continue;
2441
}
2442
i = ffs(~moea64_vsid_bitmap[n]) - 1;
2443
mask = 1 << i;
2444
hash &= rounddown2(VSID_HASHMASK, VSID_NBPW);
2445
hash |= i;
2446
}
2447
if (hash == VSID_VRMA) /* also special, avoid this too */
2448
continue;
2449
KASSERT(!(moea64_vsid_bitmap[n] & mask),
2450
("Allocating in-use VSID %#zx\n", hash));
2451
moea64_vsid_bitmap[n] |= mask;
2452
mtx_unlock(&moea64_slb_mutex);
2453
return (hash);
2454
}
2455
2456
mtx_unlock(&moea64_slb_mutex);
2457
panic("%s: out of segments",__func__);
2458
}
2459
2460
#ifdef __powerpc64__
2461
int
2462
moea64_pinit(pmap_t pmap)
2463
{
2464
2465
RB_INIT(&pmap->pmap_pvo);
2466
2467
pmap->pm_slb_tree_root = slb_alloc_tree();
2468
pmap->pm_slb = slb_alloc_user_cache();
2469
pmap->pm_slb_len = 0;
2470
2471
return (1);
2472
}
2473
#else
2474
int
2475
moea64_pinit(pmap_t pmap)
2476
{
2477
int i;
2478
uint32_t hash;
2479
2480
RB_INIT(&pmap->pmap_pvo);
2481
2482
if (pmap_bootstrapped)
2483
pmap->pmap_phys = (pmap_t)moea64_kextract((vm_offset_t)pmap);
2484
else
2485
pmap->pmap_phys = pmap;
2486
2487
/*
2488
* Allocate some segment registers for this pmap.
2489
*/
2490
hash = moea64_get_unique_vsid();
2491
2492
for (i = 0; i < 16; i++)
2493
pmap->pm_sr[i] = VSID_MAKE(i, hash);
2494
2495
KASSERT(pmap->pm_sr[0] != 0, ("moea64_pinit: pm_sr[0] = 0"));
2496
2497
return (1);
2498
}
2499
#endif
2500
2501
/*
2502
* Initialize the pmap associated with process 0.
2503
*/
2504
void
2505
moea64_pinit0(pmap_t pm)
2506
{
2507
2508
PMAP_LOCK_INIT(pm);
2509
moea64_pinit(pm);
2510
bzero(&pm->pm_stats, sizeof(pm->pm_stats));
2511
}
2512
2513
/*
2514
* Set the physical protection on the specified range of this map as requested.
2515
*/
2516
static void
2517
moea64_pvo_protect( pmap_t pm, struct pvo_entry *pvo, vm_prot_t prot)
2518
{
2519
struct vm_page *pg;
2520
vm_prot_t oldprot;
2521
int32_t refchg;
2522
2523
PMAP_LOCK_ASSERT(pm, MA_OWNED);
2524
2525
/*
2526
* Change the protection of the page.
2527
*/
2528
oldprot = pvo->pvo_pte.prot;
2529
pvo->pvo_pte.prot = prot;
2530
pg = PHYS_TO_VM_PAGE(PVO_PADDR(pvo));
2531
2532
/*
2533
* If the PVO is in the page table, update mapping
2534
*/
2535
refchg = moea64_pte_replace(pvo, MOEA64_PTE_PROT_UPDATE);
2536
if (refchg < 0)
2537
refchg = (oldprot & VM_PROT_WRITE) ? LPTE_CHG : 0;
2538
2539
if (pm != kernel_pmap && pg != NULL &&
2540
(pg->a.flags & PGA_EXECUTABLE) == 0 &&
2541
(pvo->pvo_pte.pa & (LPTE_I | LPTE_G | LPTE_NOEXEC)) == 0) {
2542
if ((pg->oflags & VPO_UNMANAGED) == 0)
2543
vm_page_aflag_set(pg, PGA_EXECUTABLE);
2544
moea64_syncicache(pm, PVO_VADDR(pvo),
2545
PVO_PADDR(pvo), PAGE_SIZE);
2546
}
2547
2548
/*
2549
* Update vm about the REF/CHG bits if the page is managed and we have
2550
* removed write access.
2551
*/
2552
if (pg != NULL && (pvo->pvo_vaddr & PVO_MANAGED) &&
2553
(oldprot & VM_PROT_WRITE)) {
2554
refchg |= atomic_readandclear_32(&pg->md.mdpg_attrs);
2555
if (refchg & LPTE_CHG)
2556
vm_page_dirty(pg);
2557
if (refchg & LPTE_REF)
2558
vm_page_aflag_set(pg, PGA_REFERENCED);
2559
}
2560
}
2561
2562
void
2563
moea64_protect(pmap_t pm, vm_offset_t sva, vm_offset_t eva,
2564
vm_prot_t prot)
2565
{
2566
struct pvo_entry *pvo, key;
2567
2568
CTR4(KTR_PMAP, "moea64_protect: pm=%p sva=%#x eva=%#x prot=%#x", pm,
2569
sva, eva, prot);
2570
2571
KASSERT(pm == &curproc->p_vmspace->vm_pmap || pm == kernel_pmap,
2572
("moea64_protect: non current pmap"));
2573
2574
if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
2575
moea64_remove(pm, sva, eva);
2576
return;
2577
}
2578
2579
PMAP_LOCK(pm);
2580
key.pvo_vaddr = sva;
2581
for (pvo = RB_NFIND(pvo_tree, &pm->pmap_pvo, &key);
2582
pvo != NULL && PVO_VADDR(pvo) < eva;
2583
pvo = RB_NEXT(pvo_tree, &pm->pmap_pvo, pvo)) {
2584
if (PVO_IS_SP(pvo)) {
2585
if (moea64_sp_pvo_in_range(pvo, sva, eva)) {
2586
pvo = moea64_sp_protect(pvo, prot);
2587
continue;
2588
} else {
2589
CTR1(KTR_PMAP, "%s: demote before protect",
2590
__func__);
2591
moea64_sp_demote(pvo);
2592
}
2593
}
2594
moea64_pvo_protect(pm, pvo, prot);
2595
}
2596
PMAP_UNLOCK(pm);
2597
}
2598
2599
/*
2600
* Map a list of wired pages into kernel virtual address space. This is
2601
* intended for temporary mappings which do not need page modification or
2602
* references recorded. Existing mappings in the region are overwritten.
2603
*/
2604
void
2605
moea64_qenter(vm_offset_t va, vm_page_t *m, int count)
2606
{
2607
while (count-- > 0) {
2608
moea64_kenter(va, VM_PAGE_TO_PHYS(*m));
2609
va += PAGE_SIZE;
2610
m++;
2611
}
2612
}
2613
2614
/*
2615
* Remove page mappings from kernel virtual address space. Intended for
2616
* temporary mappings entered by moea64_qenter.
2617
*/
2618
void
2619
moea64_qremove(vm_offset_t va, int count)
2620
{
2621
while (count-- > 0) {
2622
moea64_kremove(va);
2623
va += PAGE_SIZE;
2624
}
2625
}
2626
2627
void
2628
moea64_release_vsid(uint64_t vsid)
2629
{
2630
int idx, mask;
2631
2632
mtx_lock(&moea64_slb_mutex);
2633
idx = vsid & (NVSIDS-1);
2634
mask = 1 << (idx % VSID_NBPW);
2635
idx /= VSID_NBPW;
2636
KASSERT(moea64_vsid_bitmap[idx] & mask,
2637
("Freeing unallocated VSID %#jx", vsid));
2638
moea64_vsid_bitmap[idx] &= ~mask;
2639
mtx_unlock(&moea64_slb_mutex);
2640
}
2641
2642
void
2643
moea64_release(pmap_t pmap)
2644
{
2645
2646
/*
2647
* Free segment registers' VSIDs
2648
*/
2649
#ifdef __powerpc64__
2650
slb_free_tree(pmap);
2651
slb_free_user_cache(pmap->pm_slb);
2652
#else
2653
KASSERT(pmap->pm_sr[0] != 0, ("moea64_release: pm_sr[0] = 0"));
2654
2655
moea64_release_vsid(VSID_TO_HASH(pmap->pm_sr[0]));
2656
#endif
2657
}
2658
2659
/*
2660
* Remove all pages mapped by the specified pmap
2661
*/
2662
void
2663
moea64_remove_pages(pmap_t pm)
2664
{
2665
struct pvo_entry *pvo, *tpvo;
2666
struct pvo_dlist tofree;
2667
2668
SLIST_INIT(&tofree);
2669
2670
PMAP_LOCK(pm);
2671
RB_FOREACH_SAFE(pvo, pvo_tree, &pm->pmap_pvo, tpvo) {
2672
if (pvo->pvo_vaddr & PVO_WIRED)
2673
continue;
2674
2675
/*
2676
* For locking reasons, remove this from the page table and
2677
* pmap, but save delinking from the vm_page for a second
2678
* pass
2679
*/
2680
moea64_pvo_remove_from_pmap(pvo);
2681
SLIST_INSERT_HEAD(&tofree, pvo, pvo_dlink);
2682
}
2683
PMAP_UNLOCK(pm);
2684
2685
while (!SLIST_EMPTY(&tofree)) {
2686
pvo = SLIST_FIRST(&tofree);
2687
SLIST_REMOVE_HEAD(&tofree, pvo_dlink);
2688
moea64_pvo_remove_from_page(pvo);
2689
free_pvo_entry(pvo);
2690
}
2691
}
2692
2693
static void
2694
moea64_remove_locked(pmap_t pm, vm_offset_t sva, vm_offset_t eva,
2695
struct pvo_dlist *tofree)
2696
{
2697
struct pvo_entry *pvo, *tpvo, key;
2698
2699
PMAP_LOCK_ASSERT(pm, MA_OWNED);
2700
2701
key.pvo_vaddr = sva;
2702
for (pvo = RB_NFIND(pvo_tree, &pm->pmap_pvo, &key);
2703
pvo != NULL && PVO_VADDR(pvo) < eva; pvo = tpvo) {
2704
if (PVO_IS_SP(pvo)) {
2705
if (moea64_sp_pvo_in_range(pvo, sva, eva)) {
2706
tpvo = moea64_sp_remove(pvo, tofree);
2707
continue;
2708
} else {
2709
CTR1(KTR_PMAP, "%s: demote before remove",
2710
__func__);
2711
moea64_sp_demote(pvo);
2712
}
2713
}
2714
tpvo = RB_NEXT(pvo_tree, &pm->pmap_pvo, pvo);
2715
2716
/*
2717
* For locking reasons, remove this from the page table and
2718
* pmap, but save delinking from the vm_page for a second
2719
* pass
2720
*/
2721
moea64_pvo_remove_from_pmap(pvo);
2722
SLIST_INSERT_HEAD(tofree, pvo, pvo_dlink);
2723
}
2724
}
2725
2726
/*
2727
* Remove the given range of addresses from the specified map.
2728
*/
2729
void
2730
moea64_remove(pmap_t pm, vm_offset_t sva, vm_offset_t eva)
2731
{
2732
struct pvo_entry *pvo;
2733
struct pvo_dlist tofree;
2734
2735
/*
2736
* Perform an unsynchronized read. This is, however, safe.
2737
*/
2738
if (pm->pm_stats.resident_count == 0)
2739
return;
2740
2741
SLIST_INIT(&tofree);
2742
PMAP_LOCK(pm);
2743
moea64_remove_locked(pm, sva, eva, &tofree);
2744
PMAP_UNLOCK(pm);
2745
2746
while (!SLIST_EMPTY(&tofree)) {
2747
pvo = SLIST_FIRST(&tofree);
2748
SLIST_REMOVE_HEAD(&tofree, pvo_dlink);
2749
moea64_pvo_remove_from_page(pvo);
2750
free_pvo_entry(pvo);
2751
}
2752
}
2753
2754
/*
2755
* Remove physical page from all pmaps in which it resides. moea64_pvo_remove()
2756
* will reflect changes in pte's back to the vm_page.
2757
*/
2758
void
2759
moea64_remove_all(vm_page_t m)
2760
{
2761
struct pvo_entry *pvo, *next_pvo;
2762
struct pvo_head freequeue;
2763
int wasdead;
2764
pmap_t pmap;
2765
2766
LIST_INIT(&freequeue);
2767
2768
PV_PAGE_LOCK(m);
2769
LIST_FOREACH_SAFE(pvo, vm_page_to_pvoh(m), pvo_vlink, next_pvo) {
2770
pmap = pvo->pvo_pmap;
2771
PMAP_LOCK(pmap);
2772
wasdead = (pvo->pvo_vaddr & PVO_DEAD);
2773
if (!wasdead) {
2774
if (PVO_IS_SP(pvo)) {
2775
CTR1(KTR_PMAP, "%s: demote before remove_all",
2776
__func__);
2777
moea64_sp_demote(pvo);
2778
}
2779
moea64_pvo_remove_from_pmap(pvo);
2780
}
2781
moea64_pvo_remove_from_page_locked(pvo, m);
2782
if (!wasdead)
2783
LIST_INSERT_HEAD(&freequeue, pvo, pvo_vlink);
2784
PMAP_UNLOCK(pmap);
2785
2786
}
2787
KASSERT(!pmap_page_is_mapped(m), ("Page still has mappings"));
2788
KASSERT((m->a.flags & PGA_WRITEABLE) == 0, ("Page still writable"));
2789
PV_PAGE_UNLOCK(m);
2790
2791
/* Clean up UMA allocations */
2792
LIST_FOREACH_SAFE(pvo, &freequeue, pvo_vlink, next_pvo)
2793
free_pvo_entry(pvo);
2794
}
2795
2796
/*
2797
* Allocate a physical page of memory directly from the phys_avail map.
2798
* Can only be called from moea64_bootstrap before avail start and end are
2799
* calculated.
2800
*/
2801
vm_offset_t
2802
moea64_bootstrap_alloc(vm_size_t size, vm_size_t align)
2803
{
2804
vm_offset_t s, e;
2805
int i, j;
2806
2807
size = round_page(size);
2808
for (i = 0; phys_avail[i + 1] != 0; i += 2) {
2809
if (align != 0)
2810
s = roundup2(phys_avail[i], align);
2811
else
2812
s = phys_avail[i];
2813
e = s + size;
2814
2815
if (s < phys_avail[i] || e > phys_avail[i + 1])
2816
continue;
2817
2818
if (s + size > platform_real_maxaddr())
2819
continue;
2820
2821
if (s == phys_avail[i]) {
2822
phys_avail[i] += size;
2823
} else if (e == phys_avail[i + 1]) {
2824
phys_avail[i + 1] -= size;
2825
} else {
2826
for (j = phys_avail_count * 2; j > i; j -= 2) {
2827
phys_avail[j] = phys_avail[j - 2];
2828
phys_avail[j + 1] = phys_avail[j - 1];
2829
}
2830
2831
phys_avail[i + 3] = phys_avail[i + 1];
2832
phys_avail[i + 1] = s;
2833
phys_avail[i + 2] = e;
2834
phys_avail_count++;
2835
}
2836
2837
return (s);
2838
}
2839
panic("moea64_bootstrap_alloc: could not allocate memory");
2840
}
2841
2842
static int
2843
moea64_pvo_enter(struct pvo_entry *pvo, struct pvo_head *pvo_head,
2844
struct pvo_entry **oldpvop)
2845
{
2846
struct pvo_entry *old_pvo;
2847
int err;
2848
2849
PMAP_LOCK_ASSERT(pvo->pvo_pmap, MA_OWNED);
2850
2851
STAT_MOEA64(moea64_pvo_enter_calls++);
2852
2853
/*
2854
* Add to pmap list
2855
*/
2856
old_pvo = RB_INSERT(pvo_tree, &pvo->pvo_pmap->pmap_pvo, pvo);
2857
2858
if (old_pvo != NULL) {
2859
if (oldpvop != NULL)
2860
*oldpvop = old_pvo;
2861
return (EEXIST);
2862
}
2863
2864
if (pvo_head != NULL) {
2865
LIST_INSERT_HEAD(pvo_head, pvo, pvo_vlink);
2866
}
2867
2868
if (pvo->pvo_vaddr & PVO_WIRED)
2869
pvo->pvo_pmap->pm_stats.wired_count++;
2870
pvo->pvo_pmap->pm_stats.resident_count++;
2871
2872
/*
2873
* Insert it into the hardware page table
2874
*/
2875
err = moea64_pte_insert(pvo);
2876
if (err != 0) {
2877
panic("moea64_pvo_enter: overflow");
2878
}
2879
2880
STAT_MOEA64(moea64_pvo_entries++);
2881
2882
if (pvo->pvo_pmap == kernel_pmap)
2883
isync();
2884
2885
#ifdef __powerpc64__
2886
/*
2887
* Make sure all our bootstrap mappings are in the SLB as soon
2888
* as virtual memory is switched on.
2889
*/
2890
if (!pmap_bootstrapped)
2891
moea64_bootstrap_slb_prefault(PVO_VADDR(pvo),
2892
pvo->pvo_vaddr & PVO_LARGE);
2893
#endif
2894
2895
return (0);
2896
}
2897
2898
static void
2899
moea64_pvo_remove_from_pmap(struct pvo_entry *pvo)
2900
{
2901
struct vm_page *pg;
2902
int32_t refchg;
2903
2904
KASSERT(pvo->pvo_pmap != NULL, ("Trying to remove PVO with no pmap"));
2905
PMAP_LOCK_ASSERT(pvo->pvo_pmap, MA_OWNED);
2906
KASSERT(!(pvo->pvo_vaddr & PVO_DEAD), ("Trying to remove dead PVO"));
2907
2908
/*
2909
* If there is an active pte entry, we need to deactivate it
2910
*/
2911
refchg = moea64_pte_unset(pvo);
2912
if (refchg < 0) {
2913
/*
2914
* If it was evicted from the page table, be pessimistic and
2915
* dirty the page.
2916
*/
2917
if (pvo->pvo_pte.prot & VM_PROT_WRITE)
2918
refchg = LPTE_CHG;
2919
else
2920
refchg = 0;
2921
}
2922
2923
/*
2924
* Update our statistics.
2925
*/
2926
pvo->pvo_pmap->pm_stats.resident_count--;
2927
if (pvo->pvo_vaddr & PVO_WIRED)
2928
pvo->pvo_pmap->pm_stats.wired_count--;
2929
2930
/*
2931
* Remove this PVO from the pmap list.
2932
*/
2933
RB_REMOVE(pvo_tree, &pvo->pvo_pmap->pmap_pvo, pvo);
2934
2935
/*
2936
* Mark this for the next sweep
2937
*/
2938
pvo->pvo_vaddr |= PVO_DEAD;
2939
2940
/* Send RC bits to VM */
2941
if ((pvo->pvo_vaddr & PVO_MANAGED) &&
2942
(pvo->pvo_pte.prot & VM_PROT_WRITE)) {
2943
pg = PHYS_TO_VM_PAGE(PVO_PADDR(pvo));
2944
if (pg != NULL) {
2945
refchg |= atomic_readandclear_32(&pg->md.mdpg_attrs);
2946
if (refchg & LPTE_CHG)
2947
vm_page_dirty(pg);
2948
if (refchg & LPTE_REF)
2949
vm_page_aflag_set(pg, PGA_REFERENCED);
2950
}
2951
}
2952
}
2953
2954
static inline void
2955
moea64_pvo_remove_from_page_locked(struct pvo_entry *pvo,
2956
vm_page_t m)
2957
{
2958
2959
KASSERT(pvo->pvo_vaddr & PVO_DEAD, ("Trying to delink live page"));
2960
2961
/* Use NULL pmaps as a sentinel for races in page deletion */
2962
if (pvo->pvo_pmap == NULL)
2963
return;
2964
pvo->pvo_pmap = NULL;
2965
2966
/*
2967
* Update vm about page writeability/executability if managed
2968
*/
2969
PV_LOCKASSERT(PVO_PADDR(pvo));
2970
if (pvo->pvo_vaddr & PVO_MANAGED) {
2971
if (m != NULL) {
2972
LIST_REMOVE(pvo, pvo_vlink);
2973
if (LIST_EMPTY(vm_page_to_pvoh(m)))
2974
vm_page_aflag_clear(m,
2975
PGA_WRITEABLE | PGA_EXECUTABLE);
2976
}
2977
}
2978
2979
STAT_MOEA64(moea64_pvo_entries--);
2980
STAT_MOEA64(moea64_pvo_remove_calls++);
2981
}
2982
2983
static void
2984
moea64_pvo_remove_from_page(struct pvo_entry *pvo)
2985
{
2986
vm_page_t pg = NULL;
2987
2988
if (pvo->pvo_vaddr & PVO_MANAGED)
2989
pg = PHYS_TO_VM_PAGE(PVO_PADDR(pvo));
2990
2991
PV_LOCK(PVO_PADDR(pvo));
2992
moea64_pvo_remove_from_page_locked(pvo, pg);
2993
PV_UNLOCK(PVO_PADDR(pvo));
2994
}
2995
2996
static struct pvo_entry *
2997
moea64_pvo_find_va(pmap_t pm, vm_offset_t va)
2998
{
2999
struct pvo_entry key;
3000
3001
PMAP_LOCK_ASSERT(pm, MA_OWNED);
3002
3003
key.pvo_vaddr = va & ~ADDR_POFF;
3004
return (RB_FIND(pvo_tree, &pm->pmap_pvo, &key));
3005
}
3006
3007
static bool
3008
moea64_query_bit(vm_page_t m, uint64_t ptebit)
3009
{
3010
struct pvo_entry *pvo;
3011
int64_t ret;
3012
bool rv;
3013
vm_page_t sp;
3014
3015
/*
3016
* See if this bit is stored in the page already.
3017
*
3018
* For superpages, the bit is stored in the first vm page.
3019
*/
3020
if ((m->md.mdpg_attrs & ptebit) != 0 ||
3021
((sp = PHYS_TO_VM_PAGE(VM_PAGE_TO_PHYS(m) & ~HPT_SP_MASK)) != NULL &&
3022
(sp->md.mdpg_attrs & (ptebit | MDPG_ATTR_SP)) ==
3023
(ptebit | MDPG_ATTR_SP)))
3024
return (true);
3025
3026
/*
3027
* Examine each PTE. Sync so that any pending REF/CHG bits are
3028
* flushed to the PTEs.
3029
*/
3030
rv = false;
3031
powerpc_sync();
3032
PV_PAGE_LOCK(m);
3033
LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
3034
if (PVO_IS_SP(pvo)) {
3035
ret = moea64_sp_query(pvo, ptebit);
3036
/*
3037
* If SP was not demoted, check its REF/CHG bits here.
3038
*/
3039
if (ret != -1) {
3040
if ((ret & ptebit) != 0) {
3041
rv = true;
3042
break;
3043
}
3044
continue;
3045
}
3046
/* else, fallthrough */
3047
}
3048
3049
ret = 0;
3050
3051
/*
3052
* See if this pvo has a valid PTE. if so, fetch the
3053
* REF/CHG bits from the valid PTE. If the appropriate
3054
* ptebit is set, return success.
3055
*/
3056
PMAP_LOCK(pvo->pvo_pmap);
3057
if (!(pvo->pvo_vaddr & PVO_DEAD))
3058
ret = moea64_pte_synch(pvo);
3059
PMAP_UNLOCK(pvo->pvo_pmap);
3060
3061
if (ret > 0) {
3062
atomic_set_32(&m->md.mdpg_attrs,
3063
ret & (LPTE_CHG | LPTE_REF));
3064
if (ret & ptebit) {
3065
rv = true;
3066
break;
3067
}
3068
}
3069
}
3070
PV_PAGE_UNLOCK(m);
3071
3072
return (rv);
3073
}
3074
3075
static u_int
3076
moea64_clear_bit(vm_page_t m, u_int64_t ptebit)
3077
{
3078
u_int count;
3079
struct pvo_entry *pvo;
3080
int64_t ret;
3081
3082
/*
3083
* Sync so that any pending REF/CHG bits are flushed to the PTEs (so
3084
* we can reset the right ones).
3085
*/
3086
powerpc_sync();
3087
3088
/*
3089
* For each pvo entry, clear the pte's ptebit.
3090
*/
3091
count = 0;
3092
PV_PAGE_LOCK(m);
3093
LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) {
3094
if (PVO_IS_SP(pvo)) {
3095
if ((ret = moea64_sp_clear(pvo, m, ptebit)) != -1) {
3096
count += ret;
3097
continue;
3098
}
3099
}
3100
ret = 0;
3101
3102
PMAP_LOCK(pvo->pvo_pmap);
3103
if (!(pvo->pvo_vaddr & PVO_DEAD))
3104
ret = moea64_pte_clear(pvo, ptebit);
3105
PMAP_UNLOCK(pvo->pvo_pmap);
3106
3107
if (ret > 0 && (ret & ptebit))
3108
count++;
3109
}
3110
atomic_clear_32(&m->md.mdpg_attrs, ptebit);
3111
PV_PAGE_UNLOCK(m);
3112
3113
return (count);
3114
}
3115
3116
int
3117
moea64_dev_direct_mapped(vm_paddr_t pa, vm_size_t size)
3118
{
3119
struct pvo_entry *pvo, key;
3120
vm_offset_t ppa;
3121
int error = 0;
3122
3123
if (hw_direct_map && mem_valid(pa, size) == 0)
3124
return (0);
3125
3126
PMAP_LOCK(kernel_pmap);
3127
ppa = pa & ~ADDR_POFF;
3128
key.pvo_vaddr = DMAP_BASE_ADDRESS + ppa;
3129
for (pvo = RB_FIND(pvo_tree, &kernel_pmap->pmap_pvo, &key);
3130
ppa < pa + size; ppa += PAGE_SIZE,
3131
pvo = RB_NEXT(pvo_tree, &kernel_pmap->pmap_pvo, pvo)) {
3132
if (pvo == NULL || PVO_PADDR(pvo) != ppa) {
3133
error = EFAULT;
3134
break;
3135
}
3136
}
3137
PMAP_UNLOCK(kernel_pmap);
3138
3139
return (error);
3140
}
3141
3142
/*
3143
* Map a set of physical memory pages into the kernel virtual
3144
* address space. Return a pointer to where it is mapped. This
3145
* routine is intended to be used for mapping device memory,
3146
* NOT real memory.
3147
*/
3148
void *
3149
moea64_mapdev_attr(vm_paddr_t pa, vm_size_t size, vm_memattr_t ma)
3150
{
3151
vm_offset_t va, tmpva, ppa, offset;
3152
3153
ppa = trunc_page(pa);
3154
offset = pa & PAGE_MASK;
3155
size = roundup2(offset + size, PAGE_SIZE);
3156
3157
va = kva_alloc(size);
3158
3159
if (!va)
3160
panic("moea64_mapdev: Couldn't alloc kernel virtual memory");
3161
3162
for (tmpva = va; size > 0;) {
3163
moea64_kenter_attr(tmpva, ppa, ma);
3164
size -= PAGE_SIZE;
3165
tmpva += PAGE_SIZE;
3166
ppa += PAGE_SIZE;
3167
}
3168
3169
return ((void *)(va + offset));
3170
}
3171
3172
void *
3173
moea64_mapdev(vm_paddr_t pa, vm_size_t size)
3174
{
3175
3176
return moea64_mapdev_attr(pa, size, VM_MEMATTR_DEFAULT);
3177
}
3178
3179
void
3180
moea64_unmapdev(void *p, vm_size_t size)
3181
{
3182
vm_offset_t base, offset, va;
3183
3184
va = (vm_offset_t)p;
3185
base = trunc_page(va);
3186
offset = va & PAGE_MASK;
3187
size = roundup2(offset + size, PAGE_SIZE);
3188
3189
moea64_qremove(base, atop(size));
3190
kva_free(base, size);
3191
}
3192
3193
void
3194
moea64_sync_icache(pmap_t pm, vm_offset_t va, vm_size_t sz)
3195
{
3196
struct pvo_entry *pvo;
3197
vm_offset_t lim;
3198
vm_paddr_t pa;
3199
vm_size_t len;
3200
3201
if (__predict_false(pm == NULL))
3202
pm = &curthread->td_proc->p_vmspace->vm_pmap;
3203
3204
PMAP_LOCK(pm);
3205
while (sz > 0) {
3206
lim = round_page(va+1);
3207
len = MIN(lim - va, sz);
3208
pvo = moea64_pvo_find_va(pm, va & ~ADDR_POFF);
3209
if (pvo != NULL && !(pvo->pvo_pte.pa & LPTE_I)) {
3210
pa = PVO_PADDR(pvo) | (va & ADDR_POFF);
3211
moea64_syncicache(pm, va, pa, len);
3212
}
3213
va += len;
3214
sz -= len;
3215
}
3216
PMAP_UNLOCK(pm);
3217
}
3218
3219
void
3220
moea64_dumpsys_map(vm_paddr_t pa, size_t sz, void **va)
3221
{
3222
3223
*va = (void *)(uintptr_t)pa;
3224
}
3225
3226
extern struct dump_pa dump_map[PHYS_AVAIL_SZ + 1];
3227
3228
void
3229
moea64_scan_init(void)
3230
{
3231
struct pvo_entry *pvo;
3232
vm_offset_t va;
3233
int i;
3234
3235
if (!do_minidump) {
3236
/* Initialize phys. segments for dumpsys(). */
3237
memset(&dump_map, 0, sizeof(dump_map));
3238
mem_regions(&pregions, &pregions_sz, &regions, &regions_sz);
3239
for (i = 0; i < pregions_sz; i++) {
3240
dump_map[i].pa_start = pregions[i].mr_start;
3241
dump_map[i].pa_size = pregions[i].mr_size;
3242
}
3243
return;
3244
}
3245
3246
/* Virtual segments for minidumps: */
3247
memset(&dump_map, 0, sizeof(dump_map));
3248
3249
/* 1st: kernel .data and .bss. */
3250
dump_map[0].pa_start = trunc_page((uintptr_t)_etext);
3251
dump_map[0].pa_size = round_page((uintptr_t)_end) -
3252
dump_map[0].pa_start;
3253
3254
/* 2nd: msgbuf and tables (see pmap_bootstrap()). */
3255
dump_map[1].pa_start = (vm_paddr_t)(uintptr_t)msgbufp->msg_ptr;
3256
dump_map[1].pa_size = round_page(msgbufp->msg_size);
3257
3258
/* 3rd: kernel VM. */
3259
va = dump_map[1].pa_start + dump_map[1].pa_size;
3260
/* Find start of next chunk (from va). */
3261
while (va < virtual_end) {
3262
/* Don't dump the buffer cache. */
3263
if (va >= kmi.buffer_sva && va < kmi.buffer_eva) {
3264
va = kmi.buffer_eva;
3265
continue;
3266
}
3267
pvo = moea64_pvo_find_va(kernel_pmap, va & ~ADDR_POFF);
3268
if (pvo != NULL && !(pvo->pvo_vaddr & PVO_DEAD))
3269
break;
3270
va += PAGE_SIZE;
3271
}
3272
if (va < virtual_end) {
3273
dump_map[2].pa_start = va;
3274
va += PAGE_SIZE;
3275
/* Find last page in chunk. */
3276
while (va < virtual_end) {
3277
/* Don't run into the buffer cache. */
3278
if (va == kmi.buffer_sva)
3279
break;
3280
pvo = moea64_pvo_find_va(kernel_pmap, va & ~ADDR_POFF);
3281
if (pvo == NULL || (pvo->pvo_vaddr & PVO_DEAD))
3282
break;
3283
va += PAGE_SIZE;
3284
}
3285
dump_map[2].pa_size = va - dump_map[2].pa_start;
3286
}
3287
}
3288
3289
#ifdef __powerpc64__
3290
3291
static size_t
3292
moea64_scan_pmap(struct bitset *dump_bitset)
3293
{
3294
struct pvo_entry *pvo;
3295
vm_paddr_t pa, pa_end;
3296
vm_offset_t va, pgva, kstart, kend, kstart_lp, kend_lp;
3297
uint64_t lpsize;
3298
3299
lpsize = moea64_large_page_size;
3300
kstart = trunc_page((vm_offset_t)_etext);
3301
kend = round_page((vm_offset_t)_end);
3302
kstart_lp = kstart & ~moea64_large_page_mask;
3303
kend_lp = (kend + moea64_large_page_mask) & ~moea64_large_page_mask;
3304
3305
CTR4(KTR_PMAP, "moea64_scan_pmap: kstart=0x%016lx, kend=0x%016lx, "
3306
"kstart_lp=0x%016lx, kend_lp=0x%016lx",
3307
kstart, kend, kstart_lp, kend_lp);
3308
3309
PMAP_LOCK(kernel_pmap);
3310
RB_FOREACH(pvo, pvo_tree, &kernel_pmap->pmap_pvo) {
3311
va = pvo->pvo_vaddr;
3312
3313
if (va & PVO_DEAD)
3314
continue;
3315
3316
/* Skip DMAP (except kernel area) */
3317
if (va >= DMAP_BASE_ADDRESS && va <= DMAP_MAX_ADDRESS) {
3318
if (va & PVO_LARGE) {
3319
pgva = va & ~moea64_large_page_mask;
3320
if (pgva < kstart_lp || pgva >= kend_lp)
3321
continue;
3322
} else {
3323
pgva = trunc_page(va);
3324
if (pgva < kstart || pgva >= kend)
3325
continue;
3326
}
3327
}
3328
3329
pa = PVO_PADDR(pvo);
3330
3331
if (va & PVO_LARGE) {
3332
pa_end = pa + lpsize;
3333
for (; pa < pa_end; pa += PAGE_SIZE) {
3334
if (vm_phys_is_dumpable(pa))
3335
vm_page_dump_add(dump_bitset, pa);
3336
}
3337
} else {
3338
if (vm_phys_is_dumpable(pa))
3339
vm_page_dump_add(dump_bitset, pa);
3340
}
3341
}
3342
PMAP_UNLOCK(kernel_pmap);
3343
3344
return (sizeof(struct lpte) * moea64_pteg_count * 8);
3345
}
3346
3347
static struct dump_context dump_ctx;
3348
3349
static void *
3350
moea64_dump_pmap_init(unsigned blkpgs)
3351
{
3352
dump_ctx.ptex = 0;
3353
dump_ctx.ptex_end = moea64_pteg_count * 8;
3354
dump_ctx.blksz = blkpgs * PAGE_SIZE;
3355
return (&dump_ctx);
3356
}
3357
3358
#else
3359
3360
static size_t
3361
moea64_scan_pmap(struct bitset *dump_bitset __unused)
3362
{
3363
return (0);
3364
}
3365
3366
static void *
3367
moea64_dump_pmap_init(unsigned blkpgs)
3368
{
3369
return (NULL);
3370
}
3371
3372
#endif
3373
3374
#ifdef __powerpc64__
3375
static void
3376
moea64_map_range(vm_offset_t va, vm_paddr_t pa, vm_size_t npages)
3377
{
3378
3379
for (; npages > 0; --npages) {
3380
if (moea64_large_page_size != 0 &&
3381
(pa & moea64_large_page_mask) == 0 &&
3382
(va & moea64_large_page_mask) == 0 &&
3383
npages >= (moea64_large_page_size >> PAGE_SHIFT)) {
3384
PMAP_LOCK(kernel_pmap);
3385
moea64_kenter_large(va, pa, 0, 0);
3386
PMAP_UNLOCK(kernel_pmap);
3387
pa += moea64_large_page_size;
3388
va += moea64_large_page_size;
3389
npages -= (moea64_large_page_size >> PAGE_SHIFT) - 1;
3390
} else {
3391
moea64_kenter(va, pa);
3392
pa += PAGE_SIZE;
3393
va += PAGE_SIZE;
3394
}
3395
}
3396
}
3397
3398
static void
3399
moea64_page_array_startup(long pages)
3400
{
3401
long dom_pages[MAXMEMDOM];
3402
vm_paddr_t pa;
3403
vm_offset_t va, vm_page_base;
3404
vm_size_t needed, size;
3405
int domain;
3406
int i;
3407
3408
vm_page_base = 0xd000000000000000ULL;
3409
3410
/* Short-circuit single-domain systems. */
3411
if (vm_ndomains == 1) {
3412
size = round_page(pages * sizeof(struct vm_page));
3413
pa = vm_phys_early_alloc(0, size);
3414
vm_page_base = moea64_map(&vm_page_base,
3415
pa, pa + size, VM_PROT_READ | VM_PROT_WRITE);
3416
vm_page_array_size = pages;
3417
vm_page_array = (vm_page_t)vm_page_base;
3418
return;
3419
}
3420
3421
for (i = 0; i < MAXMEMDOM; i++)
3422
dom_pages[i] = 0;
3423
3424
/* Now get the number of pages required per domain. */
3425
for (i = 0; i < vm_phys_nsegs; i++) {
3426
domain = vm_phys_segs[i].domain;
3427
KASSERT(domain < MAXMEMDOM,
3428
("Invalid vm_phys_segs NUMA domain %d!\n", domain));
3429
/* Get size of vm_page_array needed for this segment. */
3430
size = btoc(vm_phys_segs[i].end - vm_phys_segs[i].start);
3431
dom_pages[domain] += size;
3432
}
3433
3434
for (i = 0; phys_avail[i + 1] != 0; i+= 2) {
3435
domain = vm_phys_domain(phys_avail[i]);
3436
KASSERT(domain < MAXMEMDOM,
3437
("Invalid phys_avail NUMA domain %d!\n", domain));
3438
size = btoc(phys_avail[i + 1] - phys_avail[i]);
3439
dom_pages[domain] += size;
3440
}
3441
3442
/*
3443
* Map in chunks that can get us all 16MB pages. There will be some
3444
* overlap between domains, but that's acceptable for now.
3445
*/
3446
vm_page_array_size = 0;
3447
va = vm_page_base;
3448
for (i = 0; i < MAXMEMDOM && vm_page_array_size < pages; i++) {
3449
if (dom_pages[i] == 0)
3450
continue;
3451
size = ulmin(pages - vm_page_array_size, dom_pages[i]);
3452
size = round_page(size * sizeof(struct vm_page));
3453
needed = size;
3454
size = roundup2(size, moea64_large_page_size);
3455
pa = vm_phys_early_alloc(i, size);
3456
vm_page_array_size += size / sizeof(struct vm_page);
3457
moea64_map_range(va, pa, size >> PAGE_SHIFT);
3458
/* Scoot up domain 0, to reduce the domain page overlap. */
3459
if (i == 0)
3460
vm_page_base += size - needed;
3461
va += size;
3462
}
3463
vm_page_array = (vm_page_t)vm_page_base;
3464
vm_page_array_size = pages;
3465
}
3466
#endif
3467
3468
static int64_t
3469
moea64_null_method(void)
3470
{
3471
return (0);
3472
}
3473
3474
static int64_t moea64_pte_replace_default(struct pvo_entry *pvo, int flags)
3475
{
3476
int64_t refchg;
3477
3478
refchg = moea64_pte_unset(pvo);
3479
moea64_pte_insert(pvo);
3480
3481
return (refchg);
3482
}
3483
3484
struct moea64_funcs *moea64_ops;
3485
3486
#define DEFINE_OEA64_IFUNC(ret, func, args, def) \
3487
DEFINE_IFUNC(, ret, moea64_##func, args) { \
3488
moea64_##func##_t f; \
3489
if (moea64_ops == NULL) \
3490
return ((moea64_##func##_t)def); \
3491
f = moea64_ops->func; \
3492
return (f != NULL ? f : (moea64_##func##_t)def);\
3493
}
3494
3495
void
3496
moea64_install(void)
3497
{
3498
#ifdef __powerpc64__
3499
if (hw_direct_map == -1) {
3500
moea64_probe_large_page();
3501
3502
/* Use a direct map if we have large page support */
3503
if (moea64_large_page_size > 0)
3504
hw_direct_map = 1;
3505
else
3506
hw_direct_map = 0;
3507
}
3508
#endif
3509
3510
/*
3511
* Default to non-DMAP, and switch over to DMAP functions once we know
3512
* we have DMAP.
3513
*/
3514
if (hw_direct_map) {
3515
moea64_methods.quick_enter_page = moea64_quick_enter_page_dmap;
3516
moea64_methods.quick_remove_page = NULL;
3517
moea64_methods.copy_page = moea64_copy_page_dmap;
3518
moea64_methods.zero_page = moea64_zero_page_dmap;
3519
moea64_methods.copy_pages = moea64_copy_pages_dmap;
3520
}
3521
}
3522
3523
DEFINE_OEA64_IFUNC(int64_t, pte_replace, (struct pvo_entry *, int),
3524
moea64_pte_replace_default)
3525
DEFINE_OEA64_IFUNC(int64_t, pte_insert, (struct pvo_entry *), moea64_null_method)
3526
DEFINE_OEA64_IFUNC(int64_t, pte_unset, (struct pvo_entry *), moea64_null_method)
3527
DEFINE_OEA64_IFUNC(int64_t, pte_clear, (struct pvo_entry *, uint64_t),
3528
moea64_null_method)
3529
DEFINE_OEA64_IFUNC(int64_t, pte_synch, (struct pvo_entry *), moea64_null_method)
3530
DEFINE_OEA64_IFUNC(int64_t, pte_insert_sp, (struct pvo_entry *), moea64_null_method)
3531
DEFINE_OEA64_IFUNC(int64_t, pte_unset_sp, (struct pvo_entry *), moea64_null_method)
3532
DEFINE_OEA64_IFUNC(int64_t, pte_replace_sp, (struct pvo_entry *), moea64_null_method)
3533
3534
/* Superpage functions */
3535
3536
/* MMU interface */
3537
3538
static bool
3539
moea64_ps_enabled(pmap_t pmap)
3540
{
3541
return (superpages_enabled);
3542
}
3543
3544
static void
3545
moea64_align_superpage(vm_object_t object, vm_ooffset_t offset,
3546
vm_offset_t *addr, vm_size_t size)
3547
{
3548
vm_offset_t sp_offset;
3549
3550
if (size < HPT_SP_SIZE)
3551
return;
3552
3553
CTR4(KTR_PMAP, "%s: offs=%#jx, addr=%p, size=%#jx",
3554
__func__, (uintmax_t)offset, addr, (uintmax_t)size);
3555
3556
if (object != NULL && (object->flags & OBJ_COLORED) != 0)
3557
offset += ptoa(object->pg_color);
3558
sp_offset = offset & HPT_SP_MASK;
3559
if (size - ((HPT_SP_SIZE - sp_offset) & HPT_SP_MASK) < HPT_SP_SIZE ||
3560
(*addr & HPT_SP_MASK) == sp_offset)
3561
return;
3562
if ((*addr & HPT_SP_MASK) < sp_offset)
3563
*addr = (*addr & ~HPT_SP_MASK) + sp_offset;
3564
else
3565
*addr = ((*addr + HPT_SP_MASK) & ~HPT_SP_MASK) + sp_offset;
3566
}
3567
3568
/* Helpers */
3569
3570
static __inline void
3571
moea64_pvo_cleanup(struct pvo_dlist *tofree)
3572
{
3573
struct pvo_entry *pvo;
3574
3575
/* clean up */
3576
while (!SLIST_EMPTY(tofree)) {
3577
pvo = SLIST_FIRST(tofree);
3578
SLIST_REMOVE_HEAD(tofree, pvo_dlink);
3579
if (pvo->pvo_vaddr & PVO_DEAD)
3580
moea64_pvo_remove_from_page(pvo);
3581
free_pvo_entry(pvo);
3582
}
3583
}
3584
3585
static __inline uint16_t
3586
pvo_to_vmpage_flags(struct pvo_entry *pvo)
3587
{
3588
uint16_t flags;
3589
3590
flags = 0;
3591
if ((pvo->pvo_pte.prot & VM_PROT_WRITE) != 0)
3592
flags |= PGA_WRITEABLE;
3593
if ((pvo->pvo_pte.prot & VM_PROT_EXECUTE) != 0)
3594
flags |= PGA_EXECUTABLE;
3595
3596
return (flags);
3597
}
3598
3599
/*
3600
* Check if the given pvo and its superpage are in sva-eva range.
3601
*/
3602
static __inline bool
3603
moea64_sp_pvo_in_range(struct pvo_entry *pvo, vm_offset_t sva, vm_offset_t eva)
3604
{
3605
vm_offset_t spva;
3606
3607
spva = PVO_VADDR(pvo) & ~HPT_SP_MASK;
3608
if (spva >= sva && spva + HPT_SP_SIZE <= eva) {
3609
/*
3610
* Because this function is intended to be called from loops
3611
* that iterate over ordered pvo entries, if the condition
3612
* above is true then the pvo must be the first of its
3613
* superpage.
3614
*/
3615
KASSERT(PVO_VADDR(pvo) == spva,
3616
("%s: unexpected unaligned superpage pvo", __func__));
3617
return (true);
3618
}
3619
return (false);
3620
}
3621
3622
/*
3623
* Update vm about the REF/CHG bits if the superpage is managed and
3624
* has (or had) write access.
3625
*/
3626
static void
3627
moea64_sp_refchg_process(struct pvo_entry *sp, vm_page_t m,
3628
int64_t sp_refchg, vm_prot_t prot)
3629
{
3630
vm_page_t m_end;
3631
int64_t refchg;
3632
3633
if ((sp->pvo_vaddr & PVO_MANAGED) != 0 && (prot & VM_PROT_WRITE) != 0) {
3634
for (m_end = &m[HPT_SP_PAGES]; m < m_end; m++) {
3635
refchg = sp_refchg |
3636
atomic_readandclear_32(&m->md.mdpg_attrs);
3637
if (refchg & LPTE_CHG)
3638
vm_page_dirty(m);
3639
if (refchg & LPTE_REF)
3640
vm_page_aflag_set(m, PGA_REFERENCED);
3641
}
3642
}
3643
}
3644
3645
/* Superpage ops */
3646
3647
static int
3648
moea64_sp_enter(pmap_t pmap, vm_offset_t va, vm_page_t m,
3649
vm_prot_t prot, u_int flags, int8_t psind)
3650
{
3651
struct pvo_entry *pvo, **pvos;
3652
struct pvo_head *pvo_head;
3653
vm_offset_t sva;
3654
vm_page_t sm;
3655
vm_paddr_t pa, spa;
3656
bool sync;
3657
struct pvo_dlist tofree;
3658
int error __diagused, i;
3659
uint16_t aflags;
3660
3661
KASSERT((va & HPT_SP_MASK) == 0, ("%s: va %#jx unaligned",
3662
__func__, (uintmax_t)va));
3663
KASSERT(psind == 1, ("%s: invalid psind: %d", __func__, psind));
3664
KASSERT(m->psind == 1, ("%s: invalid m->psind: %d",
3665
__func__, m->psind));
3666
KASSERT(pmap != kernel_pmap,
3667
("%s: function called with kernel pmap", __func__));
3668
3669
CTR5(KTR_PMAP, "%s: va=%#jx, pa=%#jx, prot=%#x, flags=%#x, psind=1",
3670
__func__, (uintmax_t)va, (uintmax_t)VM_PAGE_TO_PHYS(m),
3671
prot, flags);
3672
3673
SLIST_INIT(&tofree);
3674
3675
sva = va;
3676
sm = m;
3677
spa = pa = VM_PAGE_TO_PHYS(sm);
3678
3679
/* Try to allocate all PVOs first, to make failure handling easier. */
3680
pvos = malloc(HPT_SP_PAGES * sizeof(struct pvo_entry *), M_TEMP,
3681
M_NOWAIT);
3682
if (pvos == NULL) {
3683
CTR1(KTR_PMAP, "%s: failed to alloc pvo array", __func__);
3684
return (KERN_RESOURCE_SHORTAGE);
3685
}
3686
3687
for (i = 0; i < HPT_SP_PAGES; i++) {
3688
pvos[i] = alloc_pvo_entry(0);
3689
if (pvos[i] == NULL) {
3690
CTR1(KTR_PMAP, "%s: failed to alloc pvo", __func__);
3691
for (i = i - 1; i >= 0; i--)
3692
free_pvo_entry(pvos[i]);
3693
free(pvos, M_TEMP);
3694
return (KERN_RESOURCE_SHORTAGE);
3695
}
3696
}
3697
3698
PV_LOCK(spa);
3699
PMAP_LOCK(pmap);
3700
3701
/* Note: moea64_remove_locked() also clears cached REF/CHG bits. */
3702
moea64_remove_locked(pmap, va, va + HPT_SP_SIZE, &tofree);
3703
3704
/* Enter pages */
3705
for (i = 0; i < HPT_SP_PAGES;
3706
i++, va += PAGE_SIZE, pa += PAGE_SIZE, m++) {
3707
pvo = pvos[i];
3708
3709
pvo->pvo_pte.prot = prot;
3710
pvo->pvo_pte.pa = (pa & ~HPT_SP_MASK) | LPTE_LP_4K_16M |
3711
moea64_calc_wimg(pa, pmap_page_get_memattr(m));
3712
3713
if ((flags & PMAP_ENTER_WIRED) != 0)
3714
pvo->pvo_vaddr |= PVO_WIRED;
3715
pvo->pvo_vaddr |= PVO_LARGE;
3716
3717
if ((m->oflags & VPO_UNMANAGED) != 0)
3718
pvo_head = NULL;
3719
else {
3720
pvo_head = &m->md.mdpg_pvoh;
3721
pvo->pvo_vaddr |= PVO_MANAGED;
3722
}
3723
3724
init_pvo_entry(pvo, pmap, va);
3725
3726
error = moea64_pvo_enter(pvo, pvo_head, NULL);
3727
/*
3728
* All superpage PVOs were previously removed, so no errors
3729
* should occur while inserting the new ones.
3730
*/
3731
KASSERT(error == 0, ("%s: unexpected error "
3732
"when inserting superpage PVO: %d",
3733
__func__, error));
3734
}
3735
3736
PMAP_UNLOCK(pmap);
3737
PV_UNLOCK(spa);
3738
3739
sync = (sm->a.flags & PGA_EXECUTABLE) == 0;
3740
/* Note: moea64_pvo_cleanup() also clears page prot. flags. */
3741
moea64_pvo_cleanup(&tofree);
3742
pvo = pvos[0];
3743
3744
/* Set vm page flags */
3745
aflags = pvo_to_vmpage_flags(pvo);
3746
if (aflags != 0)
3747
for (m = sm; m < &sm[HPT_SP_PAGES]; m++)
3748
vm_page_aflag_set(m, aflags);
3749
3750
/*
3751
* Flush the page from the instruction cache if this page is
3752
* mapped executable and cacheable.
3753
*/
3754
if (sync && (pvo->pvo_pte.pa & (LPTE_I | LPTE_G | LPTE_NOEXEC)) == 0)
3755
moea64_syncicache(pmap, sva, spa, HPT_SP_SIZE);
3756
3757
atomic_add_long(&sp_mappings, 1);
3758
CTR3(KTR_PMAP, "%s: SP success for va %#jx in pmap %p",
3759
__func__, (uintmax_t)sva, pmap);
3760
3761
free(pvos, M_TEMP);
3762
return (KERN_SUCCESS);
3763
}
3764
3765
#if VM_NRESERVLEVEL > 0
3766
static void
3767
moea64_sp_promote(pmap_t pmap, vm_offset_t va, vm_page_t m)
3768
{
3769
struct pvo_entry *first, *pvo;
3770
vm_paddr_t pa, pa_end;
3771
vm_offset_t sva, va_end;
3772
int64_t sp_refchg;
3773
3774
/* This CTR may generate a lot of output. */
3775
/* CTR2(KTR_PMAP, "%s: va=%#jx", __func__, (uintmax_t)va); */
3776
3777
va &= ~HPT_SP_MASK;
3778
sva = va;
3779
/* Get superpage */
3780
pa = VM_PAGE_TO_PHYS(m) & ~HPT_SP_MASK;
3781
m = PHYS_TO_VM_PAGE(pa);
3782
3783
PMAP_LOCK(pmap);
3784
3785
/*
3786
* Check if all pages meet promotion criteria.
3787
*
3788
* XXX In some cases the loop below may be executed for each or most
3789
* of the entered pages of a superpage, which can be expensive
3790
* (although it was not profiled) and need some optimization.
3791
*
3792
* Some cases where this seems to happen are:
3793
* - When a superpage is first entered read-only and later becomes
3794
* read-write.
3795
* - When some of the superpage's virtual addresses map to previously
3796
* wired/cached pages while others map to pages allocated from a
3797
* different physical address range. A common scenario where this
3798
* happens is when mmap'ing a file that is already present in FS
3799
* block cache and doesn't fill a superpage.
3800
*/
3801
first = pvo = moea64_pvo_find_va(pmap, sva);
3802
for (pa_end = pa + HPT_SP_SIZE;
3803
pa < pa_end; pa += PAGE_SIZE, va += PAGE_SIZE) {
3804
if (pvo == NULL || (pvo->pvo_vaddr & PVO_DEAD) != 0) {
3805
CTR3(KTR_PMAP,
3806
"%s: NULL or dead PVO: pmap=%p, va=%#jx",
3807
__func__, pmap, (uintmax_t)va);
3808
goto error;
3809
}
3810
if (PVO_PADDR(pvo) != pa) {
3811
CTR5(KTR_PMAP, "%s: PAs don't match: "
3812
"pmap=%p, va=%#jx, pvo_pa=%#jx, exp_pa=%#jx",
3813
__func__, pmap, (uintmax_t)va,
3814
(uintmax_t)PVO_PADDR(pvo), (uintmax_t)pa);
3815
atomic_add_long(&sp_p_fail_pa, 1);
3816
goto error;
3817
}
3818
if ((first->pvo_vaddr & PVO_FLAGS_PROMOTE) !=
3819
(pvo->pvo_vaddr & PVO_FLAGS_PROMOTE)) {
3820
CTR5(KTR_PMAP, "%s: PVO flags don't match: "
3821
"pmap=%p, va=%#jx, pvo_flags=%#jx, exp_flags=%#jx",
3822
__func__, pmap, (uintmax_t)va,
3823
(uintmax_t)(pvo->pvo_vaddr & PVO_FLAGS_PROMOTE),
3824
(uintmax_t)(first->pvo_vaddr & PVO_FLAGS_PROMOTE));
3825
atomic_add_long(&sp_p_fail_flags, 1);
3826
goto error;
3827
}
3828
if (first->pvo_pte.prot != pvo->pvo_pte.prot) {
3829
CTR5(KTR_PMAP, "%s: PVO protections don't match: "
3830
"pmap=%p, va=%#jx, pvo_prot=%#x, exp_prot=%#x",
3831
__func__, pmap, (uintmax_t)va,
3832
pvo->pvo_pte.prot, first->pvo_pte.prot);
3833
atomic_add_long(&sp_p_fail_prot, 1);
3834
goto error;
3835
}
3836
if ((first->pvo_pte.pa & LPTE_WIMG) !=
3837
(pvo->pvo_pte.pa & LPTE_WIMG)) {
3838
CTR5(KTR_PMAP, "%s: WIMG bits don't match: "
3839
"pmap=%p, va=%#jx, pvo_wimg=%#jx, exp_wimg=%#jx",
3840
__func__, pmap, (uintmax_t)va,
3841
(uintmax_t)(pvo->pvo_pte.pa & LPTE_WIMG),
3842
(uintmax_t)(first->pvo_pte.pa & LPTE_WIMG));
3843
atomic_add_long(&sp_p_fail_wimg, 1);
3844
goto error;
3845
}
3846
3847
pvo = RB_NEXT(pvo_tree, &pmap->pmap_pvo, pvo);
3848
}
3849
3850
/* All OK, promote. */
3851
3852
/*
3853
* Handle superpage REF/CHG bits. If REF or CHG is set in
3854
* any page, then it must be set in the superpage.
3855
*
3856
* Instead of querying each page, we take advantage of two facts:
3857
* 1- If a page is being promoted, it was referenced.
3858
* 2- If promoted pages are writable, they were modified.
3859
*/
3860
sp_refchg = LPTE_REF |
3861
((first->pvo_pte.prot & VM_PROT_WRITE) != 0 ? LPTE_CHG : 0);
3862
3863
/* Promote pages */
3864
3865
for (pvo = first, va_end = PVO_VADDR(pvo) + HPT_SP_SIZE;
3866
pvo != NULL && PVO_VADDR(pvo) < va_end;
3867
pvo = RB_NEXT(pvo_tree, &pmap->pmap_pvo, pvo)) {
3868
pvo->pvo_pte.pa &= ADDR_POFF | ~HPT_SP_MASK;
3869
pvo->pvo_pte.pa |= LPTE_LP_4K_16M;
3870
pvo->pvo_vaddr |= PVO_LARGE;
3871
}
3872
moea64_pte_replace_sp(first);
3873
3874
/* Send REF/CHG bits to VM */
3875
moea64_sp_refchg_process(first, m, sp_refchg, first->pvo_pte.prot);
3876
3877
/* Use first page to cache REF/CHG bits */
3878
atomic_set_32(&m->md.mdpg_attrs, sp_refchg | MDPG_ATTR_SP);
3879
3880
PMAP_UNLOCK(pmap);
3881
3882
atomic_add_long(&sp_mappings, 1);
3883
atomic_add_long(&sp_promotions, 1);
3884
CTR3(KTR_PMAP, "%s: success for va %#jx in pmap %p",
3885
__func__, (uintmax_t)sva, pmap);
3886
return;
3887
3888
error:
3889
atomic_add_long(&sp_p_failures, 1);
3890
PMAP_UNLOCK(pmap);
3891
}
3892
#endif
3893
3894
static void
3895
moea64_sp_demote_aligned(struct pvo_entry *sp)
3896
{
3897
struct pvo_entry *pvo;
3898
vm_offset_t va, va_end;
3899
vm_paddr_t pa;
3900
vm_page_t m;
3901
pmap_t pmap __diagused;
3902
int64_t refchg;
3903
3904
CTR2(KTR_PMAP, "%s: va=%#jx", __func__, (uintmax_t)PVO_VADDR(sp));
3905
3906
pmap = sp->pvo_pmap;
3907
PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3908
3909
pvo = sp;
3910
3911
/* Demote pages */
3912
3913
va = PVO_VADDR(pvo);
3914
pa = PVO_PADDR(pvo);
3915
m = PHYS_TO_VM_PAGE(pa);
3916
3917
for (pvo = sp, va_end = va + HPT_SP_SIZE;
3918
pvo != NULL && PVO_VADDR(pvo) < va_end;
3919
pvo = RB_NEXT(pvo_tree, &pmap->pmap_pvo, pvo),
3920
va += PAGE_SIZE, pa += PAGE_SIZE) {
3921
KASSERT(pvo && PVO_VADDR(pvo) == va,
3922
("%s: missing PVO for va %#jx", __func__, (uintmax_t)va));
3923
3924
pvo->pvo_vaddr &= ~PVO_LARGE;
3925
pvo->pvo_pte.pa &= ~LPTE_RPGN;
3926
pvo->pvo_pte.pa |= pa;
3927
3928
}
3929
refchg = moea64_pte_replace_sp(sp);
3930
3931
/*
3932
* Clear SP flag
3933
*
3934
* XXX It is possible that another pmap has this page mapped as
3935
* part of a superpage, but as the SP flag is used only for
3936
* caching SP REF/CHG bits, that will be queried if not set
3937
* in cache, it should be ok to clear it here.
3938
*/
3939
atomic_clear_32(&m->md.mdpg_attrs, MDPG_ATTR_SP);
3940
3941
/*
3942
* Handle superpage REF/CHG bits. A bit set in the superpage
3943
* means all pages should consider it set.
3944
*/
3945
moea64_sp_refchg_process(sp, m, refchg, sp->pvo_pte.prot);
3946
3947
atomic_add_long(&sp_demotions, 1);
3948
CTR3(KTR_PMAP, "%s: success for va %#jx in pmap %p",
3949
__func__, (uintmax_t)PVO_VADDR(sp), pmap);
3950
}
3951
3952
static void
3953
moea64_sp_demote(struct pvo_entry *pvo)
3954
{
3955
PMAP_LOCK_ASSERT(pvo->pvo_pmap, MA_OWNED);
3956
3957
if ((PVO_VADDR(pvo) & HPT_SP_MASK) != 0) {
3958
pvo = moea64_pvo_find_va(pvo->pvo_pmap,
3959
PVO_VADDR(pvo) & ~HPT_SP_MASK);
3960
KASSERT(pvo != NULL, ("%s: missing PVO for va %#jx",
3961
__func__, (uintmax_t)(PVO_VADDR(pvo) & ~HPT_SP_MASK)));
3962
}
3963
moea64_sp_demote_aligned(pvo);
3964
}
3965
3966
static struct pvo_entry *
3967
moea64_sp_unwire(struct pvo_entry *sp)
3968
{
3969
struct pvo_entry *pvo, *prev;
3970
vm_offset_t eva;
3971
pmap_t pm;
3972
int64_t ret, refchg;
3973
3974
CTR2(KTR_PMAP, "%s: va=%#jx", __func__, (uintmax_t)PVO_VADDR(sp));
3975
3976
pm = sp->pvo_pmap;
3977
PMAP_LOCK_ASSERT(pm, MA_OWNED);
3978
3979
eva = PVO_VADDR(sp) + HPT_SP_SIZE;
3980
refchg = 0;
3981
for (pvo = sp; pvo != NULL && PVO_VADDR(pvo) < eva;
3982
prev = pvo, pvo = RB_NEXT(pvo_tree, &pm->pmap_pvo, pvo)) {
3983
if ((pvo->pvo_vaddr & PVO_WIRED) == 0)
3984
panic("%s: pvo %p is missing PVO_WIRED",
3985
__func__, pvo);
3986
pvo->pvo_vaddr &= ~PVO_WIRED;
3987
3988
ret = moea64_pte_replace(pvo, 0 /* No invalidation */);
3989
if (ret < 0)
3990
refchg |= LPTE_CHG;
3991
else
3992
refchg |= ret;
3993
3994
pm->pm_stats.wired_count--;
3995
}
3996
3997
/* Send REF/CHG bits to VM */
3998
moea64_sp_refchg_process(sp, PHYS_TO_VM_PAGE(PVO_PADDR(sp)),
3999
refchg, sp->pvo_pte.prot);
4000
4001
return (prev);
4002
}
4003
4004
static struct pvo_entry *
4005
moea64_sp_protect(struct pvo_entry *sp, vm_prot_t prot)
4006
{
4007
struct pvo_entry *pvo, *prev;
4008
vm_offset_t eva;
4009
pmap_t pm;
4010
vm_page_t m, m_end;
4011
int64_t ret, refchg;
4012
vm_prot_t oldprot;
4013
4014
CTR3(KTR_PMAP, "%s: va=%#jx, prot=%x",
4015
__func__, (uintmax_t)PVO_VADDR(sp), prot);
4016
4017
pm = sp->pvo_pmap;
4018
PMAP_LOCK_ASSERT(pm, MA_OWNED);
4019
4020
oldprot = sp->pvo_pte.prot;
4021
m = PHYS_TO_VM_PAGE(PVO_PADDR(sp));
4022
KASSERT(m != NULL, ("%s: missing vm page for pa %#jx",
4023
__func__, (uintmax_t)PVO_PADDR(sp)));
4024
eva = PVO_VADDR(sp) + HPT_SP_SIZE;
4025
refchg = 0;
4026
4027
for (pvo = sp; pvo != NULL && PVO_VADDR(pvo) < eva;
4028
prev = pvo, pvo = RB_NEXT(pvo_tree, &pm->pmap_pvo, pvo)) {
4029
pvo->pvo_pte.prot = prot;
4030
/*
4031
* If the PVO is in the page table, update mapping
4032
*/
4033
ret = moea64_pte_replace(pvo, MOEA64_PTE_PROT_UPDATE);
4034
if (ret < 0)
4035
refchg |= LPTE_CHG;
4036
else
4037
refchg |= ret;
4038
}
4039
4040
/* Send REF/CHG bits to VM */
4041
moea64_sp_refchg_process(sp, m, refchg, oldprot);
4042
4043
/* Handle pages that became executable */
4044
if ((m->a.flags & PGA_EXECUTABLE) == 0 &&
4045
(sp->pvo_pte.pa & (LPTE_I | LPTE_G | LPTE_NOEXEC)) == 0) {
4046
if ((m->oflags & VPO_UNMANAGED) == 0)
4047
for (m_end = &m[HPT_SP_PAGES]; m < m_end; m++)
4048
vm_page_aflag_set(m, PGA_EXECUTABLE);
4049
moea64_syncicache(pm, PVO_VADDR(sp), PVO_PADDR(sp),
4050
HPT_SP_SIZE);
4051
}
4052
4053
return (prev);
4054
}
4055
4056
static struct pvo_entry *
4057
moea64_sp_remove(struct pvo_entry *sp, struct pvo_dlist *tofree)
4058
{
4059
struct pvo_entry *pvo, *tpvo;
4060
vm_offset_t eva;
4061
pmap_t pm __diagused;
4062
4063
CTR2(KTR_PMAP, "%s: va=%#jx", __func__, (uintmax_t)PVO_VADDR(sp));
4064
4065
pm = sp->pvo_pmap;
4066
PMAP_LOCK_ASSERT(pm, MA_OWNED);
4067
4068
eva = PVO_VADDR(sp) + HPT_SP_SIZE;
4069
for (pvo = sp; pvo != NULL && PVO_VADDR(pvo) < eva; pvo = tpvo) {
4070
tpvo = RB_NEXT(pvo_tree, &pm->pmap_pvo, pvo);
4071
4072
/*
4073
* For locking reasons, remove this from the page table and
4074
* pmap, but save delinking from the vm_page for a second
4075
* pass
4076
*/
4077
moea64_pvo_remove_from_pmap(pvo);
4078
SLIST_INSERT_HEAD(tofree, pvo, pvo_dlink);
4079
}
4080
4081
/*
4082
* Clear SP bit
4083
*
4084
* XXX See comment in moea64_sp_demote_aligned() for why it's
4085
* ok to always clear the SP bit on remove/demote.
4086
*/
4087
atomic_clear_32(&PHYS_TO_VM_PAGE(PVO_PADDR(sp))->md.mdpg_attrs,
4088
MDPG_ATTR_SP);
4089
4090
return (tpvo);
4091
}
4092
4093
static int64_t
4094
moea64_sp_query_locked(struct pvo_entry *pvo, uint64_t ptebit)
4095
{
4096
int64_t refchg, ret;
4097
vm_offset_t eva;
4098
vm_page_t m;
4099
pmap_t pmap;
4100
struct pvo_entry *sp;
4101
4102
pmap = pvo->pvo_pmap;
4103
PMAP_LOCK_ASSERT(pmap, MA_OWNED);
4104
4105
/* Get first SP PVO */
4106
if ((PVO_VADDR(pvo) & HPT_SP_MASK) != 0) {
4107
sp = moea64_pvo_find_va(pmap, PVO_VADDR(pvo) & ~HPT_SP_MASK);
4108
KASSERT(sp != NULL, ("%s: missing PVO for va %#jx",
4109
__func__, (uintmax_t)(PVO_VADDR(pvo) & ~HPT_SP_MASK)));
4110
} else
4111
sp = pvo;
4112
eva = PVO_VADDR(sp) + HPT_SP_SIZE;
4113
4114
refchg = 0;
4115
for (pvo = sp; pvo != NULL && PVO_VADDR(pvo) < eva;
4116
pvo = RB_NEXT(pvo_tree, &pmap->pmap_pvo, pvo)) {
4117
ret = moea64_pte_synch(pvo);
4118
if (ret > 0) {
4119
refchg |= ret & (LPTE_CHG | LPTE_REF);
4120
if ((refchg & ptebit) != 0)
4121
break;
4122
}
4123
}
4124
4125
/* Save results */
4126
if (refchg != 0) {
4127
m = PHYS_TO_VM_PAGE(PVO_PADDR(sp));
4128
atomic_set_32(&m->md.mdpg_attrs, refchg | MDPG_ATTR_SP);
4129
}
4130
4131
return (refchg);
4132
}
4133
4134
static int64_t
4135
moea64_sp_query(struct pvo_entry *pvo, uint64_t ptebit)
4136
{
4137
int64_t refchg;
4138
pmap_t pmap;
4139
4140
pmap = pvo->pvo_pmap;
4141
PMAP_LOCK(pmap);
4142
4143
/*
4144
* Check if SP was demoted/removed before pmap lock was acquired.
4145
*/
4146
if (!PVO_IS_SP(pvo) || (pvo->pvo_vaddr & PVO_DEAD) != 0) {
4147
CTR2(KTR_PMAP, "%s: demoted/removed: pa=%#jx",
4148
__func__, (uintmax_t)PVO_PADDR(pvo));
4149
PMAP_UNLOCK(pmap);
4150
return (-1);
4151
}
4152
4153
refchg = moea64_sp_query_locked(pvo, ptebit);
4154
PMAP_UNLOCK(pmap);
4155
4156
CTR4(KTR_PMAP, "%s: va=%#jx, pa=%#jx: refchg=%#jx",
4157
__func__, (uintmax_t)PVO_VADDR(pvo),
4158
(uintmax_t)PVO_PADDR(pvo), (uintmax_t)refchg);
4159
4160
return (refchg);
4161
}
4162
4163
static int64_t
4164
moea64_sp_pvo_clear(struct pvo_entry *pvo, uint64_t ptebit)
4165
{
4166
int64_t refchg, ret;
4167
pmap_t pmap;
4168
struct pvo_entry *sp;
4169
vm_offset_t eva;
4170
vm_page_t m;
4171
4172
pmap = pvo->pvo_pmap;
4173
PMAP_LOCK(pmap);
4174
4175
/*
4176
* Check if SP was demoted/removed before pmap lock was acquired.
4177
*/
4178
if (!PVO_IS_SP(pvo) || (pvo->pvo_vaddr & PVO_DEAD) != 0) {
4179
CTR2(KTR_PMAP, "%s: demoted/removed: pa=%#jx",
4180
__func__, (uintmax_t)PVO_PADDR(pvo));
4181
PMAP_UNLOCK(pmap);
4182
return (-1);
4183
}
4184
4185
/* Get first SP PVO */
4186
if ((PVO_VADDR(pvo) & HPT_SP_MASK) != 0) {
4187
sp = moea64_pvo_find_va(pmap, PVO_VADDR(pvo) & ~HPT_SP_MASK);
4188
KASSERT(sp != NULL, ("%s: missing PVO for va %#jx",
4189
__func__, (uintmax_t)(PVO_VADDR(pvo) & ~HPT_SP_MASK)));
4190
} else
4191
sp = pvo;
4192
eva = PVO_VADDR(sp) + HPT_SP_SIZE;
4193
4194
refchg = 0;
4195
for (pvo = sp; pvo != NULL && PVO_VADDR(pvo) < eva;
4196
pvo = RB_NEXT(pvo_tree, &pmap->pmap_pvo, pvo)) {
4197
ret = moea64_pte_clear(pvo, ptebit);
4198
if (ret > 0)
4199
refchg |= ret & (LPTE_CHG | LPTE_REF);
4200
}
4201
4202
m = PHYS_TO_VM_PAGE(PVO_PADDR(sp));
4203
atomic_clear_32(&m->md.mdpg_attrs, ptebit);
4204
PMAP_UNLOCK(pmap);
4205
4206
CTR4(KTR_PMAP, "%s: va=%#jx, pa=%#jx: refchg=%#jx",
4207
__func__, (uintmax_t)PVO_VADDR(sp),
4208
(uintmax_t)PVO_PADDR(sp), (uintmax_t)refchg);
4209
4210
return (refchg);
4211
}
4212
4213
static int64_t
4214
moea64_sp_clear(struct pvo_entry *pvo, vm_page_t m, uint64_t ptebit)
4215
{
4216
int64_t count, ret;
4217
pmap_t pmap;
4218
4219
count = 0;
4220
pmap = pvo->pvo_pmap;
4221
4222
/*
4223
* Since this reference bit is shared by 4096 4KB pages, it
4224
* should not be cleared every time it is tested. Apply a
4225
* simple "hash" function on the physical page number, the
4226
* virtual superpage number, and the pmap address to select
4227
* one 4KB page out of the 4096 on which testing the
4228
* reference bit will result in clearing that reference bit.
4229
* This function is designed to avoid the selection of the
4230
* same 4KB page for every 16MB page mapping.
4231
*
4232
* Always leave the reference bit of a wired mapping set, as
4233
* the current state of its reference bit won't affect page
4234
* replacement.
4235
*/
4236
if (ptebit == LPTE_REF && (((VM_PAGE_TO_PHYS(m) >> PAGE_SHIFT) ^
4237
(PVO_VADDR(pvo) >> HPT_SP_SHIFT) ^ (uintptr_t)pmap) &
4238
(HPT_SP_PAGES - 1)) == 0 && (pvo->pvo_vaddr & PVO_WIRED) == 0) {
4239
if ((ret = moea64_sp_pvo_clear(pvo, ptebit)) == -1)
4240
return (-1);
4241
4242
if ((ret & ptebit) != 0)
4243
count++;
4244
4245
/*
4246
* If this page was not selected by the hash function, then assume
4247
* its REF bit was set.
4248
*/
4249
} else if (ptebit == LPTE_REF) {
4250
count++;
4251
4252
/*
4253
* To clear the CHG bit of a single SP page, first it must be demoted.
4254
* But if no CHG bit is set, no bit clear and thus no SP demotion is
4255
* needed.
4256
*/
4257
} else {
4258
CTR4(KTR_PMAP, "%s: ptebit=%#jx, va=%#jx, pa=%#jx",
4259
__func__, (uintmax_t)ptebit, (uintmax_t)PVO_VADDR(pvo),
4260
(uintmax_t)PVO_PADDR(pvo));
4261
4262
PMAP_LOCK(pmap);
4263
4264
/*
4265
* Make sure SP wasn't demoted/removed before pmap lock
4266
* was acquired.
4267
*/
4268
if (!PVO_IS_SP(pvo) || (pvo->pvo_vaddr & PVO_DEAD) != 0) {
4269
CTR2(KTR_PMAP, "%s: demoted/removed: pa=%#jx",
4270
__func__, (uintmax_t)PVO_PADDR(pvo));
4271
PMAP_UNLOCK(pmap);
4272
return (-1);
4273
}
4274
4275
ret = moea64_sp_query_locked(pvo, ptebit);
4276
if ((ret & ptebit) != 0)
4277
count++;
4278
else {
4279
PMAP_UNLOCK(pmap);
4280
return (0);
4281
}
4282
4283
moea64_sp_demote(pvo);
4284
moea64_pte_clear(pvo, ptebit);
4285
4286
/*
4287
* Write protect the mapping to a single page so that a
4288
* subsequent write access may repromote.
4289
*/
4290
if ((pvo->pvo_vaddr & PVO_WIRED) == 0)
4291
moea64_pvo_protect(pmap, pvo,
4292
pvo->pvo_pte.prot & ~VM_PROT_WRITE);
4293
4294
PMAP_UNLOCK(pmap);
4295
}
4296
4297
return (count);
4298
}
4299
4300