10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
51443Skchow * Common Development and Distribution License (the "License").
61443Skchow * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
2212230SFrank.Rival@oracle.com * Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate */
2412004Sjiang.liu@intel.com /*
2512004Sjiang.liu@intel.com * Copyright (c) 2010, Intel Corporation.
2612004Sjiang.liu@intel.com * All rights reserved.
2712004Sjiang.liu@intel.com */
280Sstevel@tonic-gate
290Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
300Sstevel@tonic-gate /* All Rights Reserved */
310Sstevel@tonic-gate
320Sstevel@tonic-gate /*
330Sstevel@tonic-gate * Portions of this source code were derived from Berkeley 4.3 BSD
340Sstevel@tonic-gate * under license from the Regents of the University of California.
350Sstevel@tonic-gate */
360Sstevel@tonic-gate
370Sstevel@tonic-gate /*
380Sstevel@tonic-gate * UNIX machine dependent virtual memory support.
390Sstevel@tonic-gate */
400Sstevel@tonic-gate
410Sstevel@tonic-gate #include <sys/types.h>
420Sstevel@tonic-gate #include <sys/param.h>
430Sstevel@tonic-gate #include <sys/systm.h>
440Sstevel@tonic-gate #include <sys/user.h>
450Sstevel@tonic-gate #include <sys/proc.h>
460Sstevel@tonic-gate #include <sys/kmem.h>
470Sstevel@tonic-gate #include <sys/vmem.h>
480Sstevel@tonic-gate #include <sys/buf.h>
490Sstevel@tonic-gate #include <sys/cpuvar.h>
500Sstevel@tonic-gate #include <sys/lgrp.h>
510Sstevel@tonic-gate #include <sys/disp.h>
520Sstevel@tonic-gate #include <sys/vm.h>
530Sstevel@tonic-gate #include <sys/mman.h>
540Sstevel@tonic-gate #include <sys/vnode.h>
550Sstevel@tonic-gate #include <sys/cred.h>
560Sstevel@tonic-gate #include <sys/exec.h>
570Sstevel@tonic-gate #include <sys/exechdr.h>
580Sstevel@tonic-gate #include <sys/debug.h>
592991Ssusans #include <sys/vmsystm.h>
6012230SFrank.Rival@oracle.com #include <sys/swap.h>
61*12342SDave.Plauger@Sun.COM #include <sys/dumphdr.h>
620Sstevel@tonic-gate
630Sstevel@tonic-gate #include <vm/hat.h>
640Sstevel@tonic-gate #include <vm/as.h>
650Sstevel@tonic-gate #include <vm/seg.h>
660Sstevel@tonic-gate #include <vm/seg_kp.h>
670Sstevel@tonic-gate #include <vm/seg_vn.h>
680Sstevel@tonic-gate #include <vm/page.h>
690Sstevel@tonic-gate #include <vm/seg_kmem.h>
700Sstevel@tonic-gate #include <vm/seg_kpm.h>
710Sstevel@tonic-gate #include <vm/vm_dep.h>
720Sstevel@tonic-gate
730Sstevel@tonic-gate #include <sys/cpu.h>
740Sstevel@tonic-gate #include <sys/vm_machparam.h>
750Sstevel@tonic-gate #include <sys/memlist.h>
760Sstevel@tonic-gate #include <sys/bootconf.h> /* XXX the memlist stuff belongs in memlist_plat.h */
770Sstevel@tonic-gate #include <vm/hat_i86.h>
780Sstevel@tonic-gate #include <sys/x86_archext.h>
790Sstevel@tonic-gate #include <sys/elf_386.h>
800Sstevel@tonic-gate #include <sys/cmn_err.h>
810Sstevel@tonic-gate #include <sys/archsystm.h>
820Sstevel@tonic-gate #include <sys/machsystm.h>
830Sstevel@tonic-gate
840Sstevel@tonic-gate #include <sys/vtrace.h>
850Sstevel@tonic-gate #include <sys/ddidmareq.h>
860Sstevel@tonic-gate #include <sys/promif.h>
870Sstevel@tonic-gate #include <sys/memnode.h>
880Sstevel@tonic-gate #include <sys/stack.h>
895084Sjohnlev #include <util/qsort.h>
905084Sjohnlev #include <sys/taskq.h>
915084Sjohnlev
925084Sjohnlev #ifdef __xpv
935084Sjohnlev
945084Sjohnlev #include <sys/hypervisor.h>
955084Sjohnlev #include <sys/xen_mmu.h>
965084Sjohnlev #include <sys/balloon_impl.h>
975084Sjohnlev
985084Sjohnlev /*
995084Sjohnlev * domain 0 pages usable for DMA are kept pre-allocated and kept in
1005084Sjohnlev * distinct lists, ordered by increasing mfn.
1015084Sjohnlev */
1025084Sjohnlev static kmutex_t io_pool_lock;
1035529Ssmaybe static kmutex_t contig_list_lock;
1045084Sjohnlev static page_t *io_pool_4g; /* pool for 32 bit dma limited devices */
1055084Sjohnlev static page_t *io_pool_16m; /* pool for 24 bit dma limited legacy devices */
1065084Sjohnlev static long io_pool_cnt;
1075084Sjohnlev static long io_pool_cnt_max = 0;
1085084Sjohnlev #define DEFAULT_IO_POOL_MIN 128
1095084Sjohnlev static long io_pool_cnt_min = DEFAULT_IO_POOL_MIN;
1105084Sjohnlev static long io_pool_cnt_lowater = 0;
1115084Sjohnlev static long io_pool_shrink_attempts; /* how many times did we try to shrink */
1125084Sjohnlev static long io_pool_shrinks; /* how many times did we really shrink */
1135084Sjohnlev static long io_pool_grows; /* how many times did we grow */
1145084Sjohnlev static mfn_t start_mfn = 1;
1155084Sjohnlev static caddr_t io_pool_kva; /* use to alloc pages when needed */
1165084Sjohnlev
1175084Sjohnlev static int create_contig_pfnlist(uint_t);
1185084Sjohnlev
1195084Sjohnlev /*
1205084Sjohnlev * percentage of phys mem to hold in the i/o pool
1215084Sjohnlev */
1225084Sjohnlev #define DEFAULT_IO_POOL_PCT 2
1235084Sjohnlev static long io_pool_physmem_pct = DEFAULT_IO_POOL_PCT;
1245084Sjohnlev static void page_io_pool_sub(page_t **, page_t *, page_t *);
1255529Ssmaybe int ioalloc_dbg = 0;
1265084Sjohnlev
1275084Sjohnlev #endif /* __xpv */
1280Sstevel@tonic-gate
1292961Sdp78419 uint_t vac_colors = 1;
1300Sstevel@tonic-gate
1310Sstevel@tonic-gate int largepagesupport = 0;
1320Sstevel@tonic-gate extern uint_t page_create_new;
1330Sstevel@tonic-gate extern uint_t page_create_exists;
1340Sstevel@tonic-gate extern uint_t page_create_putbacks;
1353446Smrj /*
1363446Smrj * Allow users to disable the kernel's use of SSE.
1373446Smrj */
1383446Smrj extern int use_sse_pagecopy, use_sse_pagezero;
1390Sstevel@tonic-gate
1405084Sjohnlev /*
14112293SJames.McPherson@Sun.COM * combined memory ranges from mnode and memranges[] to manage single
14212293SJames.McPherson@Sun.COM * mnode/mtype dimension in the page lists.
14312293SJames.McPherson@Sun.COM */
14412293SJames.McPherson@Sun.COM typedef struct {
14512293SJames.McPherson@Sun.COM pfn_t mnr_pfnlo;
14612293SJames.McPherson@Sun.COM pfn_t mnr_pfnhi;
14712293SJames.McPherson@Sun.COM int mnr_mnode;
14812293SJames.McPherson@Sun.COM int mnr_memrange; /* index into memranges[] */
14912293SJames.McPherson@Sun.COM int mnr_next; /* next lower PA mnoderange */
15012293SJames.McPherson@Sun.COM int mnr_exists;
15112293SJames.McPherson@Sun.COM /* maintain page list stats */
15212293SJames.McPherson@Sun.COM pgcnt_t mnr_mt_clpgcnt; /* cache list cnt */
15312293SJames.McPherson@Sun.COM pgcnt_t mnr_mt_flpgcnt[MMU_PAGE_SIZES]; /* free list cnt per szc */
15412293SJames.McPherson@Sun.COM pgcnt_t mnr_mt_totcnt; /* sum of cache and free lists */
15512293SJames.McPherson@Sun.COM #ifdef DEBUG
15612293SJames.McPherson@Sun.COM struct mnr_mts { /* mnode/mtype szc stats */
15712293SJames.McPherson@Sun.COM pgcnt_t mnr_mts_pgcnt;
15812293SJames.McPherson@Sun.COM int mnr_mts_colors;
15912293SJames.McPherson@Sun.COM pgcnt_t *mnr_mtsc_pgcnt;
16012293SJames.McPherson@Sun.COM } *mnr_mts;
16112293SJames.McPherson@Sun.COM #endif
16212293SJames.McPherson@Sun.COM } mnoderange_t;
16312293SJames.McPherson@Sun.COM
16412293SJames.McPherson@Sun.COM #define MEMRANGEHI(mtype) \
16512293SJames.McPherson@Sun.COM ((mtype > 0) ? memranges[mtype - 1] - 1: physmax)
16612293SJames.McPherson@Sun.COM #define MEMRANGELO(mtype) (memranges[mtype])
16712293SJames.McPherson@Sun.COM
16812293SJames.McPherson@Sun.COM #define MTYPE_FREEMEM(mt) (mnoderanges[mt].mnr_mt_totcnt)
16912293SJames.McPherson@Sun.COM
17012293SJames.McPherson@Sun.COM /*
1715084Sjohnlev * As the PC architecture evolved memory up was clumped into several
1725084Sjohnlev * ranges for various historical I/O devices to do DMA.
1735084Sjohnlev * < 16Meg - ISA bus
1745084Sjohnlev * < 2Gig - ???
1755084Sjohnlev * < 4Gig - PCI bus or drivers that don't understand PAE mode
1765084Sjohnlev *
1775084Sjohnlev * These are listed in reverse order, so that we can skip over unused
1785084Sjohnlev * ranges on machines with small memories.
1795084Sjohnlev *
1805084Sjohnlev * For now under the Hypervisor, we'll only ever have one memrange.
1815084Sjohnlev */
1825084Sjohnlev #define PFN_4GIG 0x100000
1835084Sjohnlev #define PFN_16MEG 0x1000
18412004Sjiang.liu@intel.com /* Indices into the memory range (arch_memranges) array. */
18512004Sjiang.liu@intel.com #define MRI_4G 0
18612004Sjiang.liu@intel.com #define MRI_2G 1
18712004Sjiang.liu@intel.com #define MRI_16M 2
18812004Sjiang.liu@intel.com #define MRI_0 3
1895084Sjohnlev static pfn_t arch_memranges[NUM_MEM_RANGES] = {
1905084Sjohnlev PFN_4GIG, /* pfn range for 4G and above */
1915084Sjohnlev 0x80000, /* pfn range for 2G-4G */
1925084Sjohnlev PFN_16MEG, /* pfn range for 16M-2G */
1935084Sjohnlev 0x00000, /* pfn range for 0-16M */
1945084Sjohnlev };
1955084Sjohnlev pfn_t *memranges = &arch_memranges[0];
1965084Sjohnlev int nranges = NUM_MEM_RANGES;
1975084Sjohnlev
1985084Sjohnlev /*
1995084Sjohnlev * This combines mem_node_config and memranges into one data
2005084Sjohnlev * structure to be used for page list management.
2015084Sjohnlev */
2025084Sjohnlev mnoderange_t *mnoderanges;
2035084Sjohnlev int mnoderangecnt;
2045084Sjohnlev int mtype4g;
20512004Sjiang.liu@intel.com int mtype16m;
20612004Sjiang.liu@intel.com int mtypetop; /* index of highest pfn'ed mnoderange */
2075084Sjohnlev
2085084Sjohnlev /*
2095084Sjohnlev * 4g memory management variables for systems with more than 4g of memory:
2105084Sjohnlev *
2115084Sjohnlev * physical memory below 4g is required for 32bit dma devices and, currently,
2125084Sjohnlev * for kmem memory. On systems with more than 4g of memory, the pool of memory
2135084Sjohnlev * below 4g can be depleted without any paging activity given that there is
2145084Sjohnlev * likely to be sufficient memory above 4g.
2155084Sjohnlev *
2165084Sjohnlev * physmax4g is set true if the largest pfn is over 4g. The rest of the
2175084Sjohnlev * 4g memory management code is enabled only when physmax4g is true.
2185084Sjohnlev *
2195084Sjohnlev * maxmem4g is the count of the maximum number of pages on the page lists
2205084Sjohnlev * with physical addresses below 4g. It can be a lot less then 4g given that
2215084Sjohnlev * BIOS may reserve large chunks of space below 4g for hot plug pci devices,
2225084Sjohnlev * agp aperture etc.
2235084Sjohnlev *
2245084Sjohnlev * freemem4g maintains the count of the number of available pages on the
2255084Sjohnlev * page lists with physical addresses below 4g.
2265084Sjohnlev *
2275084Sjohnlev * DESFREE4G specifies the desired amount of below 4g memory. It defaults to
2285084Sjohnlev * 6% (desfree4gshift = 4) of maxmem4g.
2295084Sjohnlev *
2305084Sjohnlev * RESTRICT4G_ALLOC returns true if freemem4g falls below DESFREE4G
2315084Sjohnlev * and the amount of physical memory above 4g is greater than freemem4g.
2325084Sjohnlev * In this case, page_get_* routines will restrict below 4g allocations
2335084Sjohnlev * for requests that don't specifically require it.
2345084Sjohnlev */
2355084Sjohnlev
2365084Sjohnlev #define DESFREE4G (maxmem4g >> desfree4gshift)
2375084Sjohnlev
2385084Sjohnlev #define RESTRICT4G_ALLOC \
2395084Sjohnlev (physmax4g && (freemem4g < DESFREE4G) && ((freemem4g << 1) < freemem))
2405084Sjohnlev
2415084Sjohnlev static pgcnt_t maxmem4g;
2425084Sjohnlev static pgcnt_t freemem4g;
2435084Sjohnlev static int physmax4g;
2445084Sjohnlev static int desfree4gshift = 4; /* maxmem4g shift to derive DESFREE4G */
2455084Sjohnlev
2465084Sjohnlev /*
2475084Sjohnlev * 16m memory management:
2485084Sjohnlev *
2495084Sjohnlev * reserve some amount of physical memory below 16m for legacy devices.
2505084Sjohnlev *
2515084Sjohnlev * RESTRICT16M_ALLOC returns true if an there are sufficient free pages above
2525084Sjohnlev * 16m or if the 16m pool drops below DESFREE16M.
2535084Sjohnlev *
2545084Sjohnlev * In this case, general page allocations via page_get_{free,cache}list
2555084Sjohnlev * routines will be restricted from allocating from the 16m pool. Allocations
2565084Sjohnlev * that require specific pfn ranges (page_get_anylist) and PG_PANIC allocations
2575084Sjohnlev * are not restricted.
2585084Sjohnlev */
2595084Sjohnlev
26012004Sjiang.liu@intel.com #define FREEMEM16M MTYPE_FREEMEM(mtype16m)
2615084Sjohnlev #define DESFREE16M desfree16m
2625084Sjohnlev #define RESTRICT16M_ALLOC(freemem, pgcnt, flags) \
2635084Sjohnlev ((freemem != 0) && ((flags & PG_PANIC) == 0) && \
2645084Sjohnlev ((freemem >= (FREEMEM16M)) || \
2655084Sjohnlev (FREEMEM16M < (DESFREE16M + pgcnt))))
2665084Sjohnlev
2675084Sjohnlev static pgcnt_t desfree16m = 0x380;
2685084Sjohnlev
2695084Sjohnlev /*
2705084Sjohnlev * This can be patched via /etc/system to allow old non-PAE aware device
2715084Sjohnlev * drivers to use kmem_alloc'd memory on 32 bit systems with > 4Gig RAM.
2725084Sjohnlev */
2735084Sjohnlev int restricted_kmemalloc = 0;
2741385Skchow
2750Sstevel@tonic-gate #ifdef VM_STATS
2760Sstevel@tonic-gate struct {
2770Sstevel@tonic-gate ulong_t pga_alloc;
2780Sstevel@tonic-gate ulong_t pga_notfullrange;
2790Sstevel@tonic-gate ulong_t pga_nulldmaattr;
2800Sstevel@tonic-gate ulong_t pga_allocok;
2810Sstevel@tonic-gate ulong_t pga_allocfailed;
2820Sstevel@tonic-gate ulong_t pgma_alloc;
2830Sstevel@tonic-gate ulong_t pgma_allocok;
2840Sstevel@tonic-gate ulong_t pgma_allocfailed;
2850Sstevel@tonic-gate ulong_t pgma_allocempty;
2860Sstevel@tonic-gate } pga_vmstats;
2870Sstevel@tonic-gate #endif
2880Sstevel@tonic-gate
2890Sstevel@tonic-gate uint_t mmu_page_sizes;
2900Sstevel@tonic-gate
2910Sstevel@tonic-gate /* How many page sizes the users can see */
2920Sstevel@tonic-gate uint_t mmu_exported_page_sizes;
2930Sstevel@tonic-gate
2945349Skchow /* page sizes that legacy applications can see */
2955349Skchow uint_t mmu_legacy_page_sizes;
2965349Skchow
297423Sdavemq /*
298423Sdavemq * Number of pages in 1 GB. Don't enable automatic large pages if we have
299423Sdavemq * fewer than this many pages.
300423Sdavemq */
3012991Ssusans pgcnt_t shm_lpg_min_physmem = 1 << (30 - MMU_PAGESHIFT);
3022991Ssusans pgcnt_t privm_lpg_min_physmem = 1 << (30 - MMU_PAGESHIFT);
3032991Ssusans
3042991Ssusans /*
3052991Ssusans * Maximum and default segment size tunables for user private
3062991Ssusans * and shared anon memory, and user text and initialized data.
3072991Ssusans * These can be patched via /etc/system to allow large pages
3082991Ssusans * to be used for mapping application private and shared anon memory.
3092991Ssusans */
3102991Ssusans size_t mcntl0_lpsize = MMU_PAGESIZE;
3112991Ssusans size_t max_uheap_lpsize = MMU_PAGESIZE;
3122991Ssusans size_t default_uheap_lpsize = MMU_PAGESIZE;
3132991Ssusans size_t max_ustack_lpsize = MMU_PAGESIZE;
3142991Ssusans size_t default_ustack_lpsize = MMU_PAGESIZE;
3152991Ssusans size_t max_privmap_lpsize = MMU_PAGESIZE;
3162991Ssusans size_t max_uidata_lpsize = MMU_PAGESIZE;
3172991Ssusans size_t max_utext_lpsize = MMU_PAGESIZE;
3182991Ssusans size_t max_shm_lpsize = MMU_PAGESIZE;
3190Sstevel@tonic-gate
3205084Sjohnlev
3215084Sjohnlev /*
3225084Sjohnlev * initialized by page_coloring_init().
3235084Sjohnlev */
3245084Sjohnlev uint_t page_colors;
3255084Sjohnlev uint_t page_colors_mask;
3265084Sjohnlev uint_t page_coloring_shift;
3275084Sjohnlev int cpu_page_colors;
3285084Sjohnlev static uint_t l2_colors;
3295084Sjohnlev
3305084Sjohnlev /*
3315084Sjohnlev * Page freelists and cachelists are dynamically allocated once mnoderangecnt
3325084Sjohnlev * and page_colors are calculated from the l2 cache n-way set size. Within a
3335084Sjohnlev * mnode range, the page freelist and cachelist are hashed into bins based on
3345084Sjohnlev * color. This makes it easier to search for a page within a specific memory
3355084Sjohnlev * range.
3365084Sjohnlev */
3375084Sjohnlev #define PAGE_COLORS_MIN 16
3385084Sjohnlev
3395084Sjohnlev page_t ****page_freelists;
3405084Sjohnlev page_t ***page_cachelists;
3415084Sjohnlev
3425084Sjohnlev
3435084Sjohnlev /*
3445084Sjohnlev * Used by page layer to know about page sizes
3455084Sjohnlev */
3465084Sjohnlev hw_pagesize_t hw_page_array[MAX_NUM_LEVEL + 1];
3475084Sjohnlev
34812293SJames.McPherson@Sun.COM kmutex_t *fpc_mutex[NPC_MUTEX];
34912293SJames.McPherson@Sun.COM kmutex_t *cpc_mutex[NPC_MUTEX];
3505084Sjohnlev
35112004Sjiang.liu@intel.com /* Lock to protect mnoderanges array for memory DR operations. */
35212004Sjiang.liu@intel.com static kmutex_t mnoderange_lock;
35312004Sjiang.liu@intel.com
3545084Sjohnlev /*
3555084Sjohnlev * Only let one thread at a time try to coalesce large pages, to
3565084Sjohnlev * prevent them from working against each other.
3575084Sjohnlev */
3585084Sjohnlev static kmutex_t contig_lock;
3595084Sjohnlev #define CONTIG_LOCK() mutex_enter(&contig_lock);
3605084Sjohnlev #define CONTIG_UNLOCK() mutex_exit(&contig_lock);
3615084Sjohnlev
3625084Sjohnlev #define PFN_16M (mmu_btop((uint64_t)0x1000000))
3635084Sjohnlev
3640Sstevel@tonic-gate /*
3650Sstevel@tonic-gate * Return the optimum page size for a given mapping
3660Sstevel@tonic-gate */
3670Sstevel@tonic-gate /*ARGSUSED*/
3680Sstevel@tonic-gate size_t
map_pgsz(int maptype,struct proc * p,caddr_t addr,size_t len,int memcntl)3692991Ssusans map_pgsz(int maptype, struct proc *p, caddr_t addr, size_t len, int memcntl)
3700Sstevel@tonic-gate {
3712991Ssusans level_t l = 0;
3722991Ssusans size_t pgsz = MMU_PAGESIZE;
3732991Ssusans size_t max_lpsize;
3742991Ssusans uint_t mszc;
3750Sstevel@tonic-gate
3762991Ssusans ASSERT(maptype != MAPPGSZ_VA);
3772991Ssusans
3782991Ssusans if (maptype != MAPPGSZ_ISM && physmem < privm_lpg_min_physmem) {
3792991Ssusans return (MMU_PAGESIZE);
3802991Ssusans }
3810Sstevel@tonic-gate
3820Sstevel@tonic-gate switch (maptype) {
3832991Ssusans case MAPPGSZ_HEAP:
3840Sstevel@tonic-gate case MAPPGSZ_STK:
3852991Ssusans max_lpsize = memcntl ? mcntl0_lpsize : (maptype ==
3862991Ssusans MAPPGSZ_HEAP ? max_uheap_lpsize : max_ustack_lpsize);
3872991Ssusans if (max_lpsize == MMU_PAGESIZE) {
3882991Ssusans return (MMU_PAGESIZE);
3892991Ssusans }
3902991Ssusans if (len == 0) {
3912991Ssusans len = (maptype == MAPPGSZ_HEAP) ? p->p_brkbase +
3922991Ssusans p->p_brksize - p->p_bssbase : p->p_stksize;
3932991Ssusans }
3942991Ssusans len = (maptype == MAPPGSZ_HEAP) ? MAX(len,
3952991Ssusans default_uheap_lpsize) : MAX(len, default_ustack_lpsize);
3962991Ssusans
3970Sstevel@tonic-gate /*
3980Sstevel@tonic-gate * use the pages size that best fits len
3990Sstevel@tonic-gate */
4005349Skchow for (l = mmu.umax_page_level; l > 0; --l) {
4012991Ssusans if (LEVEL_SIZE(l) > max_lpsize || len < LEVEL_SIZE(l)) {
4020Sstevel@tonic-gate continue;
4032991Ssusans } else {
4042991Ssusans pgsz = LEVEL_SIZE(l);
4052991Ssusans }
4060Sstevel@tonic-gate break;
4070Sstevel@tonic-gate }
4082991Ssusans
4092991Ssusans mszc = (maptype == MAPPGSZ_HEAP ? p->p_brkpageszc :
4102991Ssusans p->p_stkpageszc);
4112991Ssusans if (addr == 0 && (pgsz < hw_page_array[mszc].hp_size)) {
4122991Ssusans pgsz = hw_page_array[mszc].hp_size;
4132991Ssusans }
4142991Ssusans return (pgsz);
4150Sstevel@tonic-gate
4160Sstevel@tonic-gate case MAPPGSZ_ISM:
4175349Skchow for (l = mmu.umax_page_level; l > 0; --l) {
4185349Skchow if (len >= LEVEL_SIZE(l))
4195349Skchow return (LEVEL_SIZE(l));
4205349Skchow }
4215349Skchow return (LEVEL_SIZE(0));
4220Sstevel@tonic-gate }
4232991Ssusans return (pgsz);
4240Sstevel@tonic-gate }
4250Sstevel@tonic-gate
4262991Ssusans static uint_t
map_szcvec(caddr_t addr,size_t size,uintptr_t off,size_t max_lpsize,size_t min_physmem)4272991Ssusans map_szcvec(caddr_t addr, size_t size, uintptr_t off, size_t max_lpsize,
4282991Ssusans size_t min_physmem)
4292991Ssusans {
4302991Ssusans caddr_t eaddr = addr + size;
4312991Ssusans uint_t szcvec = 0;
4322991Ssusans caddr_t raddr;
4332991Ssusans caddr_t readdr;
4342991Ssusans size_t pgsz;
4352991Ssusans int i;
4362991Ssusans
4372991Ssusans if (physmem < min_physmem || max_lpsize <= MMU_PAGESIZE) {
4382991Ssusans return (0);
4392991Ssusans }
4402991Ssusans
4415349Skchow for (i = mmu_exported_page_sizes - 1; i > 0; i--) {
4422991Ssusans pgsz = page_get_pagesize(i);
4432991Ssusans if (pgsz > max_lpsize) {
4442991Ssusans continue;
4452991Ssusans }
4462991Ssusans raddr = (caddr_t)P2ROUNDUP((uintptr_t)addr, pgsz);
4472991Ssusans readdr = (caddr_t)P2ALIGN((uintptr_t)eaddr, pgsz);
4482991Ssusans if (raddr < addr || raddr >= readdr) {
4492991Ssusans continue;
4502991Ssusans }
4512991Ssusans if (P2PHASE((uintptr_t)addr ^ off, pgsz)) {
4522991Ssusans continue;
4532991Ssusans }
4542991Ssusans /*
4552991Ssusans * Set szcvec to the remaining page sizes.
4562991Ssusans */
4572991Ssusans szcvec = ((1 << (i + 1)) - 1) & ~1;
4582991Ssusans break;
4592991Ssusans }
4602991Ssusans return (szcvec);
4612991Ssusans }
4620Sstevel@tonic-gate
4630Sstevel@tonic-gate /*
4640Sstevel@tonic-gate * Return a bit vector of large page size codes that
4650Sstevel@tonic-gate * can be used to map [addr, addr + len) region.
4660Sstevel@tonic-gate */
4670Sstevel@tonic-gate /*ARGSUSED*/
4680Sstevel@tonic-gate uint_t
map_pgszcvec(caddr_t addr,size_t size,uintptr_t off,int flags,int type,int memcntl)4692991Ssusans map_pgszcvec(caddr_t addr, size_t size, uintptr_t off, int flags, int type,
4702991Ssusans int memcntl)
4710Sstevel@tonic-gate {
4722991Ssusans size_t max_lpsize = mcntl0_lpsize;
4730Sstevel@tonic-gate
4742991Ssusans if (mmu.max_page_level == 0)
4750Sstevel@tonic-gate return (0);
4760Sstevel@tonic-gate
4772991Ssusans if (flags & MAP_TEXT) {
4785084Sjohnlev if (!memcntl)
4795084Sjohnlev max_lpsize = max_utext_lpsize;
4805084Sjohnlev return (map_szcvec(addr, size, off, max_lpsize,
4812991Ssusans shm_lpg_min_physmem));
4822991Ssusans
4832991Ssusans } else if (flags & MAP_INITDATA) {
4845084Sjohnlev if (!memcntl)
4855084Sjohnlev max_lpsize = max_uidata_lpsize;
4865084Sjohnlev return (map_szcvec(addr, size, off, max_lpsize,
4872991Ssusans privm_lpg_min_physmem));
4882991Ssusans
4892991Ssusans } else if (type == MAPPGSZC_SHM) {
4905084Sjohnlev if (!memcntl)
4915084Sjohnlev max_lpsize = max_shm_lpsize;
4925084Sjohnlev return (map_szcvec(addr, size, off, max_lpsize,
4932991Ssusans shm_lpg_min_physmem));
4940Sstevel@tonic-gate
4952991Ssusans } else if (type == MAPPGSZC_HEAP) {
4965084Sjohnlev if (!memcntl)
4975084Sjohnlev max_lpsize = max_uheap_lpsize;
4985084Sjohnlev return (map_szcvec(addr, size, off, max_lpsize,
4992991Ssusans privm_lpg_min_physmem));
5002414Saguzovsk
5012991Ssusans } else if (type == MAPPGSZC_STACK) {
5025084Sjohnlev if (!memcntl)
5035084Sjohnlev max_lpsize = max_ustack_lpsize;
5045084Sjohnlev return (map_szcvec(addr, size, off, max_lpsize,
5052991Ssusans privm_lpg_min_physmem));
5062991Ssusans
5072991Ssusans } else {
5085084Sjohnlev if (!memcntl)
5095084Sjohnlev max_lpsize = max_privmap_lpsize;
5105084Sjohnlev return (map_szcvec(addr, size, off, max_lpsize,
5112991Ssusans privm_lpg_min_physmem));
5122414Saguzovsk }
5132414Saguzovsk }
5142414Saguzovsk
5150Sstevel@tonic-gate /*
5160Sstevel@tonic-gate * Handle a pagefault.
5170Sstevel@tonic-gate */
5180Sstevel@tonic-gate faultcode_t
pagefault(caddr_t addr,enum fault_type type,enum seg_rw rw,int iskernel)5190Sstevel@tonic-gate pagefault(
5200Sstevel@tonic-gate caddr_t addr,
5210Sstevel@tonic-gate enum fault_type type,
5220Sstevel@tonic-gate enum seg_rw rw,
5230Sstevel@tonic-gate int iskernel)
5240Sstevel@tonic-gate {
5250Sstevel@tonic-gate struct as *as;
5260Sstevel@tonic-gate struct hat *hat;
5270Sstevel@tonic-gate struct proc *p;
5280Sstevel@tonic-gate kthread_t *t;
5290Sstevel@tonic-gate faultcode_t res;
5300Sstevel@tonic-gate caddr_t base;
5310Sstevel@tonic-gate size_t len;
5320Sstevel@tonic-gate int err;
5330Sstevel@tonic-gate int mapped_red;
5340Sstevel@tonic-gate uintptr_t ea;
5350Sstevel@tonic-gate
5360Sstevel@tonic-gate ASSERT_STACK_ALIGNED();
5370Sstevel@tonic-gate
5380Sstevel@tonic-gate if (INVALID_VADDR(addr))
5390Sstevel@tonic-gate return (FC_NOMAP);
5400Sstevel@tonic-gate
5410Sstevel@tonic-gate mapped_red = segkp_map_red();
5420Sstevel@tonic-gate
5430Sstevel@tonic-gate if (iskernel) {
5440Sstevel@tonic-gate as = &kas;
5450Sstevel@tonic-gate hat = as->a_hat;
5460Sstevel@tonic-gate } else {
5470Sstevel@tonic-gate t = curthread;
5480Sstevel@tonic-gate p = ttoproc(t);
5490Sstevel@tonic-gate as = p->p_as;
5500Sstevel@tonic-gate hat = as->a_hat;
5510Sstevel@tonic-gate }
5520Sstevel@tonic-gate
5530Sstevel@tonic-gate /*
5540Sstevel@tonic-gate * Dispatch pagefault.
5550Sstevel@tonic-gate */
5560Sstevel@tonic-gate res = as_fault(hat, as, addr, 1, type, rw);
5570Sstevel@tonic-gate
5580Sstevel@tonic-gate /*
5590Sstevel@tonic-gate * If this isn't a potential unmapped hole in the user's
5600Sstevel@tonic-gate * UNIX data or stack segments, just return status info.
5610Sstevel@tonic-gate */
5620Sstevel@tonic-gate if (res != FC_NOMAP || iskernel)
5630Sstevel@tonic-gate goto out;
5640Sstevel@tonic-gate
5650Sstevel@tonic-gate /*
5660Sstevel@tonic-gate * Check to see if we happened to faulted on a currently unmapped
5670Sstevel@tonic-gate * part of the UNIX data or stack segments. If so, create a zfod
5680Sstevel@tonic-gate * mapping there and then try calling the fault routine again.
5690Sstevel@tonic-gate */
5700Sstevel@tonic-gate base = p->p_brkbase;
5710Sstevel@tonic-gate len = p->p_brksize;
5720Sstevel@tonic-gate
5730Sstevel@tonic-gate if (addr < base || addr >= base + len) { /* data seg? */
5740Sstevel@tonic-gate base = (caddr_t)p->p_usrstack - p->p_stksize;
5750Sstevel@tonic-gate len = p->p_stksize;
5760Sstevel@tonic-gate if (addr < base || addr >= p->p_usrstack) { /* stack seg? */
5770Sstevel@tonic-gate /* not in either UNIX data or stack segments */
5780Sstevel@tonic-gate res = FC_NOMAP;
5790Sstevel@tonic-gate goto out;
5800Sstevel@tonic-gate }
5810Sstevel@tonic-gate }
5820Sstevel@tonic-gate
5830Sstevel@tonic-gate /*
5840Sstevel@tonic-gate * the rest of this function implements a 3.X 4.X 5.X compatibility
5850Sstevel@tonic-gate * This code is probably not needed anymore
5860Sstevel@tonic-gate */
5870Sstevel@tonic-gate if (p->p_model == DATAMODEL_ILP32) {
5880Sstevel@tonic-gate
5890Sstevel@tonic-gate /* expand the gap to the page boundaries on each side */
5900Sstevel@tonic-gate ea = P2ROUNDUP((uintptr_t)base + len, MMU_PAGESIZE);
5910Sstevel@tonic-gate base = (caddr_t)P2ALIGN((uintptr_t)base, MMU_PAGESIZE);
5920Sstevel@tonic-gate len = ea - (uintptr_t)base;
5930Sstevel@tonic-gate
5940Sstevel@tonic-gate as_rangelock(as);
5950Sstevel@tonic-gate if (as_gap(as, MMU_PAGESIZE, &base, &len, AH_CONTAIN, addr) ==
5960Sstevel@tonic-gate 0) {
5970Sstevel@tonic-gate err = as_map(as, base, len, segvn_create, zfod_argsp);
5980Sstevel@tonic-gate as_rangeunlock(as);
5990Sstevel@tonic-gate if (err) {
6000Sstevel@tonic-gate res = FC_MAKE_ERR(err);
6010Sstevel@tonic-gate goto out;
6020Sstevel@tonic-gate }
6030Sstevel@tonic-gate } else {
6040Sstevel@tonic-gate /*
6050Sstevel@tonic-gate * This page is already mapped by another thread after
6060Sstevel@tonic-gate * we returned from as_fault() above. We just fall
6070Sstevel@tonic-gate * through as_fault() below.
6080Sstevel@tonic-gate */
6090Sstevel@tonic-gate as_rangeunlock(as);
6100Sstevel@tonic-gate }
6110Sstevel@tonic-gate
6120Sstevel@tonic-gate res = as_fault(hat, as, addr, 1, F_INVAL, rw);
6130Sstevel@tonic-gate }
6140Sstevel@tonic-gate
6150Sstevel@tonic-gate out:
6160Sstevel@tonic-gate if (mapped_red)
6170Sstevel@tonic-gate segkp_unmap_red();
6180Sstevel@tonic-gate
6190Sstevel@tonic-gate return (res);
6200Sstevel@tonic-gate }
6210Sstevel@tonic-gate
6220Sstevel@tonic-gate void
map_addr(caddr_t * addrp,size_t len,offset_t off,int vacalign,uint_t flags)6230Sstevel@tonic-gate map_addr(caddr_t *addrp, size_t len, offset_t off, int vacalign, uint_t flags)
6240Sstevel@tonic-gate {
6250Sstevel@tonic-gate struct proc *p = curproc;
6260Sstevel@tonic-gate caddr_t userlimit = (flags & _MAP_LOW32) ?
6270Sstevel@tonic-gate (caddr_t)_userlimit32 : p->p_as->a_userlimit;
6280Sstevel@tonic-gate
6290Sstevel@tonic-gate map_addr_proc(addrp, len, off, vacalign, userlimit, curproc, flags);
6300Sstevel@tonic-gate }
6310Sstevel@tonic-gate
6320Sstevel@tonic-gate /*ARGSUSED*/
6330Sstevel@tonic-gate int
map_addr_vacalign_check(caddr_t addr,u_offset_t off)6340Sstevel@tonic-gate map_addr_vacalign_check(caddr_t addr, u_offset_t off)
6350Sstevel@tonic-gate {
6360Sstevel@tonic-gate return (0);
6370Sstevel@tonic-gate }
6380Sstevel@tonic-gate
6390Sstevel@tonic-gate /*
6400Sstevel@tonic-gate * map_addr_proc() is the routine called when the system is to
6410Sstevel@tonic-gate * choose an address for the user. We will pick an address
6423446Smrj * range which is the highest available below userlimit.
6430Sstevel@tonic-gate *
6445668Smec * Every mapping will have a redzone of a single page on either side of
6455668Smec * the request. This is done to leave one page unmapped between segments.
6465668Smec * This is not required, but it's useful for the user because if their
6475668Smec * program strays across a segment boundary, it will catch a fault
6485668Smec * immediately making debugging a little easier. Currently the redzone
6495668Smec * is mandatory.
6505668Smec *
6510Sstevel@tonic-gate * addrp is a value/result parameter.
6520Sstevel@tonic-gate * On input it is a hint from the user to be used in a completely
6530Sstevel@tonic-gate * machine dependent fashion. We decide to completely ignore this hint.
6545668Smec * If MAP_ALIGN was specified, addrp contains the minimal alignment, which
6555668Smec * must be some "power of two" multiple of pagesize.
6560Sstevel@tonic-gate *
6570Sstevel@tonic-gate * On output it is NULL if no address can be found in the current
6580Sstevel@tonic-gate * processes address space or else an address that is currently
6590Sstevel@tonic-gate * not mapped for len bytes with a page of red zone on either side.
6600Sstevel@tonic-gate *
6615668Smec * vacalign is not needed on x86 (it's for viturally addressed caches)
6620Sstevel@tonic-gate */
6630Sstevel@tonic-gate /*ARGSUSED*/
6640Sstevel@tonic-gate void
map_addr_proc(caddr_t * addrp,size_t len,offset_t off,int vacalign,caddr_t userlimit,struct proc * p,uint_t flags)6650Sstevel@tonic-gate map_addr_proc(
6660Sstevel@tonic-gate caddr_t *addrp,
6670Sstevel@tonic-gate size_t len,
6680Sstevel@tonic-gate offset_t off,
6690Sstevel@tonic-gate int vacalign,
6700Sstevel@tonic-gate caddr_t userlimit,
6710Sstevel@tonic-gate struct proc *p,
6720Sstevel@tonic-gate uint_t flags)
6730Sstevel@tonic-gate {
6740Sstevel@tonic-gate struct as *as = p->p_as;
6750Sstevel@tonic-gate caddr_t addr;
6760Sstevel@tonic-gate caddr_t base;
6770Sstevel@tonic-gate size_t slen;
6780Sstevel@tonic-gate size_t align_amount;
6790Sstevel@tonic-gate
6800Sstevel@tonic-gate ASSERT32(userlimit == as->a_userlimit);
6810Sstevel@tonic-gate
6820Sstevel@tonic-gate base = p->p_brkbase;
6830Sstevel@tonic-gate #if defined(__amd64)
6840Sstevel@tonic-gate /*
6850Sstevel@tonic-gate * XX64 Yes, this needs more work.
6860Sstevel@tonic-gate */
6870Sstevel@tonic-gate if (p->p_model == DATAMODEL_NATIVE) {
6880Sstevel@tonic-gate if (userlimit < as->a_userlimit) {
6890Sstevel@tonic-gate /*
6900Sstevel@tonic-gate * This happens when a program wants to map
6910Sstevel@tonic-gate * something in a range that's accessible to a
6920Sstevel@tonic-gate * program in a smaller address space. For example,
6930Sstevel@tonic-gate * a 64-bit program calling mmap32(2) to guarantee
6940Sstevel@tonic-gate * that the returned address is below 4Gbytes.
6950Sstevel@tonic-gate */
6960Sstevel@tonic-gate ASSERT((uintptr_t)userlimit < ADDRESS_C(0xffffffff));
6970Sstevel@tonic-gate
6980Sstevel@tonic-gate if (userlimit > base)
6990Sstevel@tonic-gate slen = userlimit - base;
7000Sstevel@tonic-gate else {
7010Sstevel@tonic-gate *addrp = NULL;
7020Sstevel@tonic-gate return;
7030Sstevel@tonic-gate }
7040Sstevel@tonic-gate } else {
7050Sstevel@tonic-gate /*
7060Sstevel@tonic-gate * XX64 This layout is probably wrong .. but in
7070Sstevel@tonic-gate * the event we make the amd64 address space look
7080Sstevel@tonic-gate * like sparcv9 i.e. with the stack -above- the
7090Sstevel@tonic-gate * heap, this bit of code might even be correct.
7100Sstevel@tonic-gate */
7110Sstevel@tonic-gate slen = p->p_usrstack - base -
7128947SMichael.Corcoran@Sun.COM ((p->p_stk_ctl + PAGEOFFSET) & PAGEMASK);
7130Sstevel@tonic-gate }
7140Sstevel@tonic-gate } else
7150Sstevel@tonic-gate #endif
7160Sstevel@tonic-gate slen = userlimit - base;
7170Sstevel@tonic-gate
7185668Smec /* Make len be a multiple of PAGESIZE */
7190Sstevel@tonic-gate len = (len + PAGEOFFSET) & PAGEMASK;
7200Sstevel@tonic-gate
7210Sstevel@tonic-gate /*
7220Sstevel@tonic-gate * figure out what the alignment should be
7230Sstevel@tonic-gate *
7240Sstevel@tonic-gate * XX64 -- is there an ELF_AMD64_MAXPGSZ or is it the same????
7250Sstevel@tonic-gate */
7260Sstevel@tonic-gate if (len <= ELF_386_MAXPGSZ) {
7270Sstevel@tonic-gate /*
7280Sstevel@tonic-gate * Align virtual addresses to ensure that ELF shared libraries
7290Sstevel@tonic-gate * are mapped with the appropriate alignment constraints by
7300Sstevel@tonic-gate * the run-time linker.
7310Sstevel@tonic-gate */
7320Sstevel@tonic-gate align_amount = ELF_386_MAXPGSZ;
7330Sstevel@tonic-gate } else {
73411678SSudheer.Abdul-Salam@Sun.COM /*
73511678SSudheer.Abdul-Salam@Sun.COM * For 32-bit processes, only those which have specified
73611678SSudheer.Abdul-Salam@Sun.COM * MAP_ALIGN and an addr will be aligned on a larger page size.
73711678SSudheer.Abdul-Salam@Sun.COM * Not doing so can potentially waste up to 1G of process
73811678SSudheer.Abdul-Salam@Sun.COM * address space.
73911678SSudheer.Abdul-Salam@Sun.COM */
74011678SSudheer.Abdul-Salam@Sun.COM int lvl = (p->p_model == DATAMODEL_ILP32) ? 1 :
74111678SSudheer.Abdul-Salam@Sun.COM mmu.umax_page_level;
74211678SSudheer.Abdul-Salam@Sun.COM
74311678SSudheer.Abdul-Salam@Sun.COM while (lvl && len < LEVEL_SIZE(lvl))
74411678SSudheer.Abdul-Salam@Sun.COM --lvl;
74511678SSudheer.Abdul-Salam@Sun.COM
74611678SSudheer.Abdul-Salam@Sun.COM align_amount = LEVEL_SIZE(lvl);
7470Sstevel@tonic-gate }
7480Sstevel@tonic-gate if ((flags & MAP_ALIGN) && ((uintptr_t)*addrp > align_amount))
7490Sstevel@tonic-gate align_amount = (uintptr_t)*addrp;
7500Sstevel@tonic-gate
7515668Smec ASSERT(ISP2(align_amount));
7525668Smec ASSERT(align_amount == 0 || align_amount >= PAGESIZE);
7535668Smec
7545668Smec off = off & (align_amount - 1);
7550Sstevel@tonic-gate /*
7560Sstevel@tonic-gate * Look for a large enough hole starting below userlimit.
7575668Smec * After finding it, use the upper part.
7580Sstevel@tonic-gate */
7595668Smec if (as_gap_aligned(as, len, &base, &slen, AH_HI, NULL, align_amount,
7605668Smec PAGESIZE, off) == 0) {
7610Sstevel@tonic-gate caddr_t as_addr;
7620Sstevel@tonic-gate
7635668Smec /*
7645668Smec * addr is the highest possible address to use since we have
7655668Smec * a PAGESIZE redzone at the beginning and end.
7665668Smec */
7675668Smec addr = base + slen - (PAGESIZE + len);
7680Sstevel@tonic-gate as_addr = addr;
7690Sstevel@tonic-gate /*
7705668Smec * Round address DOWN to the alignment amount and
7715668Smec * add the offset in.
7725668Smec * If addr is greater than as_addr, len would not be large
7735668Smec * enough to include the redzone, so we must adjust down
7745668Smec * by the alignment amount.
7750Sstevel@tonic-gate */
7760Sstevel@tonic-gate addr = (caddr_t)((uintptr_t)addr & (~(align_amount - 1)));
7775668Smec addr += (uintptr_t)off;
7785668Smec if (addr > as_addr) {
7795668Smec addr -= align_amount;
7805668Smec }
7815668Smec
7825668Smec ASSERT(addr > base);
7835668Smec ASSERT(addr + len < base + slen);
7840Sstevel@tonic-gate ASSERT(((uintptr_t)addr & (align_amount - 1)) ==
7855668Smec ((uintptr_t)(off)));
7860Sstevel@tonic-gate *addrp = addr;
7870Sstevel@tonic-gate } else {
7880Sstevel@tonic-gate *addrp = NULL; /* no more virtual space */
7890Sstevel@tonic-gate }
7900Sstevel@tonic-gate }
7910Sstevel@tonic-gate
7925668Smec int valid_va_range_aligned_wraparound;
7935668Smec
7940Sstevel@tonic-gate /*
7955668Smec * Determine whether [*basep, *basep + *lenp) contains a mappable range of
7965668Smec * addresses at least "minlen" long, where the base of the range is at "off"
7975668Smec * phase from an "align" boundary and there is space for a "redzone"-sized
7985668Smec * redzone on either side of the range. On success, 1 is returned and *basep
7995668Smec * and *lenp are adjusted to describe the acceptable range (including
8005668Smec * the redzone). On failure, 0 is returned.
8010Sstevel@tonic-gate */
8020Sstevel@tonic-gate /*ARGSUSED3*/
8030Sstevel@tonic-gate int
valid_va_range_aligned(caddr_t * basep,size_t * lenp,size_t minlen,int dir,size_t align,size_t redzone,size_t off)8045668Smec valid_va_range_aligned(caddr_t *basep, size_t *lenp, size_t minlen, int dir,
8055668Smec size_t align, size_t redzone, size_t off)
8060Sstevel@tonic-gate {
8070Sstevel@tonic-gate uintptr_t hi, lo;
8085668Smec size_t tot_len;
8095668Smec
8105668Smec ASSERT(align == 0 ? off == 0 : off < align);
8115668Smec ASSERT(ISP2(align));
8125668Smec ASSERT(align == 0 || align >= PAGESIZE);
8130Sstevel@tonic-gate
8140Sstevel@tonic-gate lo = (uintptr_t)*basep;
8150Sstevel@tonic-gate hi = lo + *lenp;
8165668Smec tot_len = minlen + 2 * redzone; /* need at least this much space */
8170Sstevel@tonic-gate
8180Sstevel@tonic-gate /*
8190Sstevel@tonic-gate * If hi rolled over the top, try cutting back.
8200Sstevel@tonic-gate */
8210Sstevel@tonic-gate if (hi < lo) {
8225668Smec *lenp = 0UL - lo - 1UL;
8235668Smec /* See if this really happens. If so, then we figure out why */
8245668Smec valid_va_range_aligned_wraparound++;
8255668Smec hi = lo + *lenp;
8265668Smec }
8275668Smec if (*lenp < tot_len) {
8280Sstevel@tonic-gate return (0);
8290Sstevel@tonic-gate }
8305668Smec
8310Sstevel@tonic-gate #if defined(__amd64)
8320Sstevel@tonic-gate /*
8330Sstevel@tonic-gate * Deal with a possible hole in the address range between
8340Sstevel@tonic-gate * hole_start and hole_end that should never be mapped.
8350Sstevel@tonic-gate */
8360Sstevel@tonic-gate if (lo < hole_start) {
8370Sstevel@tonic-gate if (hi > hole_start) {
8380Sstevel@tonic-gate if (hi < hole_end) {
8390Sstevel@tonic-gate hi = hole_start;
8400Sstevel@tonic-gate } else {
8410Sstevel@tonic-gate /* lo < hole_start && hi >= hole_end */
8420Sstevel@tonic-gate if (dir == AH_LO) {
8430Sstevel@tonic-gate /*
8440Sstevel@tonic-gate * prefer lowest range
8450Sstevel@tonic-gate */
8465668Smec if (hole_start - lo >= tot_len)
8470Sstevel@tonic-gate hi = hole_start;
8485668Smec else if (hi - hole_end >= tot_len)
8490Sstevel@tonic-gate lo = hole_end;
8500Sstevel@tonic-gate else
8510Sstevel@tonic-gate return (0);
8520Sstevel@tonic-gate } else {
8530Sstevel@tonic-gate /*
8540Sstevel@tonic-gate * prefer highest range
8550Sstevel@tonic-gate */
8565668Smec if (hi - hole_end >= tot_len)
8570Sstevel@tonic-gate lo = hole_end;
8585668Smec else if (hole_start - lo >= tot_len)
8590Sstevel@tonic-gate hi = hole_start;
8600Sstevel@tonic-gate else
8610Sstevel@tonic-gate return (0);
8620Sstevel@tonic-gate }
8630Sstevel@tonic-gate }
8640Sstevel@tonic-gate }
8650Sstevel@tonic-gate } else {
8660Sstevel@tonic-gate /* lo >= hole_start */
8670Sstevel@tonic-gate if (hi < hole_end)
8680Sstevel@tonic-gate return (0);
8690Sstevel@tonic-gate if (lo < hole_end)
8700Sstevel@tonic-gate lo = hole_end;
8710Sstevel@tonic-gate }
8725668Smec #endif
8735668Smec
8745668Smec if (hi - lo < tot_len)
8750Sstevel@tonic-gate return (0);
8760Sstevel@tonic-gate
8775668Smec if (align > 1) {
8785668Smec uintptr_t tlo = lo + redzone;
8795668Smec uintptr_t thi = hi - redzone;
8805668Smec tlo = (uintptr_t)P2PHASEUP(tlo, align, off);
8815668Smec if (tlo < lo + redzone) {
8825668Smec return (0);
8835668Smec }
8845668Smec if (thi < tlo || thi - tlo < minlen) {
8855668Smec return (0);
8865668Smec }
8875668Smec }
8885668Smec
8890Sstevel@tonic-gate *basep = (caddr_t)lo;
8900Sstevel@tonic-gate *lenp = hi - lo;
8910Sstevel@tonic-gate return (1);
8920Sstevel@tonic-gate }
8930Sstevel@tonic-gate
8940Sstevel@tonic-gate /*
8955668Smec * Determine whether [*basep, *basep + *lenp) contains a mappable range of
8965668Smec * addresses at least "minlen" long. On success, 1 is returned and *basep
8975668Smec * and *lenp are adjusted to describe the acceptable range. On failure, 0
8985668Smec * is returned.
8995668Smec */
9005668Smec int
valid_va_range(caddr_t * basep,size_t * lenp,size_t minlen,int dir)9015668Smec valid_va_range(caddr_t *basep, size_t *lenp, size_t minlen, int dir)
9025668Smec {
9035668Smec return (valid_va_range_aligned(basep, lenp, minlen, dir, 0, 0, 0));
9045668Smec }
9055668Smec
9065668Smec /*
9070Sstevel@tonic-gate * Determine whether [addr, addr+len] are valid user addresses.
9080Sstevel@tonic-gate */
9090Sstevel@tonic-gate /*ARGSUSED*/
9100Sstevel@tonic-gate int
valid_usr_range(caddr_t addr,size_t len,uint_t prot,struct as * as,caddr_t userlimit)9110Sstevel@tonic-gate valid_usr_range(caddr_t addr, size_t len, uint_t prot, struct as *as,
9120Sstevel@tonic-gate caddr_t userlimit)
9130Sstevel@tonic-gate {
9140Sstevel@tonic-gate caddr_t eaddr = addr + len;
9150Sstevel@tonic-gate
9160Sstevel@tonic-gate if (eaddr <= addr || addr >= userlimit || eaddr > userlimit)
9170Sstevel@tonic-gate return (RANGE_BADADDR);
9180Sstevel@tonic-gate
9190Sstevel@tonic-gate #if defined(__amd64)
9200Sstevel@tonic-gate /*
9210Sstevel@tonic-gate * Check for the VA hole
9220Sstevel@tonic-gate */
9230Sstevel@tonic-gate if (eaddr > (caddr_t)hole_start && addr < (caddr_t)hole_end)
9240Sstevel@tonic-gate return (RANGE_BADADDR);
9250Sstevel@tonic-gate #endif
9260Sstevel@tonic-gate
9270Sstevel@tonic-gate return (RANGE_OKAY);
9280Sstevel@tonic-gate }
9290Sstevel@tonic-gate
9300Sstevel@tonic-gate /*
9310Sstevel@tonic-gate * Return 1 if the page frame is onboard memory, else 0.
9320Sstevel@tonic-gate */
9330Sstevel@tonic-gate int
pf_is_memory(pfn_t pf)9340Sstevel@tonic-gate pf_is_memory(pfn_t pf)
9350Sstevel@tonic-gate {
9363446Smrj if (pfn_is_foreign(pf))
9373446Smrj return (0);
9383446Smrj return (address_in_memlist(phys_install, pfn_to_pa(pf), 1));
9390Sstevel@tonic-gate }
9400Sstevel@tonic-gate
9410Sstevel@tonic-gate /*
9420Sstevel@tonic-gate * return the memrange containing pfn
9430Sstevel@tonic-gate */
9440Sstevel@tonic-gate int
memrange_num(pfn_t pfn)9450Sstevel@tonic-gate memrange_num(pfn_t pfn)
9460Sstevel@tonic-gate {
9470Sstevel@tonic-gate int n;
9480Sstevel@tonic-gate
9490Sstevel@tonic-gate for (n = 0; n < nranges - 1; ++n) {
9500Sstevel@tonic-gate if (pfn >= memranges[n])
9510Sstevel@tonic-gate break;
9520Sstevel@tonic-gate }
9530Sstevel@tonic-gate return (n);
9540Sstevel@tonic-gate }
9550Sstevel@tonic-gate
9560Sstevel@tonic-gate /*
9570Sstevel@tonic-gate * return the mnoderange containing pfn
9580Sstevel@tonic-gate */
9595084Sjohnlev /*ARGSUSED*/
9600Sstevel@tonic-gate int
pfn_2_mtype(pfn_t pfn)9610Sstevel@tonic-gate pfn_2_mtype(pfn_t pfn)
9620Sstevel@tonic-gate {
9635084Sjohnlev #if defined(__xpv)
9645084Sjohnlev return (0);
9655084Sjohnlev #else
9660Sstevel@tonic-gate int n;
9670Sstevel@tonic-gate
96812004Sjiang.liu@intel.com /* Always start from highest pfn and work our way down */
96912004Sjiang.liu@intel.com for (n = mtypetop; n != -1; n = mnoderanges[n].mnr_next) {
9700Sstevel@tonic-gate if (pfn >= mnoderanges[n].mnr_pfnlo) {
9710Sstevel@tonic-gate break;
9720Sstevel@tonic-gate }
9730Sstevel@tonic-gate }
9740Sstevel@tonic-gate return (n);
9755084Sjohnlev #endif
9760Sstevel@tonic-gate }
9770Sstevel@tonic-gate
9785084Sjohnlev #if !defined(__xpv)
9790Sstevel@tonic-gate /*
9800Sstevel@tonic-gate * is_contigpage_free:
9810Sstevel@tonic-gate * returns a page list of contiguous pages. It minimally has to return
9820Sstevel@tonic-gate * minctg pages. Caller determines minctg based on the scatter-gather
9830Sstevel@tonic-gate * list length.
9840Sstevel@tonic-gate *
9850Sstevel@tonic-gate * pfnp is set to the next page frame to search on return.
9860Sstevel@tonic-gate */
9870Sstevel@tonic-gate static page_t *
is_contigpage_free(pfn_t * pfnp,pgcnt_t * pgcnt,pgcnt_t minctg,uint64_t pfnseg,int iolock)9880Sstevel@tonic-gate is_contigpage_free(
9890Sstevel@tonic-gate pfn_t *pfnp,
9900Sstevel@tonic-gate pgcnt_t *pgcnt,
9910Sstevel@tonic-gate pgcnt_t minctg,
9920Sstevel@tonic-gate uint64_t pfnseg,
9930Sstevel@tonic-gate int iolock)
9940Sstevel@tonic-gate {
9950Sstevel@tonic-gate int i = 0;
9960Sstevel@tonic-gate pfn_t pfn = *pfnp;
9970Sstevel@tonic-gate page_t *pp;
9980Sstevel@tonic-gate page_t *plist = NULL;
9990Sstevel@tonic-gate
10000Sstevel@tonic-gate /*
10010Sstevel@tonic-gate * fail if pfn + minctg crosses a segment boundary.
10020Sstevel@tonic-gate * Adjust for next starting pfn to begin at segment boundary.
10030Sstevel@tonic-gate */
10040Sstevel@tonic-gate
10050Sstevel@tonic-gate if (((*pfnp + minctg - 1) & pfnseg) < (*pfnp & pfnseg)) {
10060Sstevel@tonic-gate *pfnp = roundup(*pfnp, pfnseg + 1);
10070Sstevel@tonic-gate return (NULL);
10080Sstevel@tonic-gate }
10090Sstevel@tonic-gate
10100Sstevel@tonic-gate do {
10110Sstevel@tonic-gate retry:
10120Sstevel@tonic-gate pp = page_numtopp_nolock(pfn + i);
1013*12342SDave.Plauger@Sun.COM if ((pp == NULL) || IS_DUMP_PAGE(pp) ||
10140Sstevel@tonic-gate (page_trylock(pp, SE_EXCL) == 0)) {
10150Sstevel@tonic-gate (*pfnp)++;
10160Sstevel@tonic-gate break;
10170Sstevel@tonic-gate }
10180Sstevel@tonic-gate if (page_pptonum(pp) != pfn + i) {
10190Sstevel@tonic-gate page_unlock(pp);
10200Sstevel@tonic-gate goto retry;
10210Sstevel@tonic-gate }
10220Sstevel@tonic-gate
10230Sstevel@tonic-gate if (!(PP_ISFREE(pp))) {
10240Sstevel@tonic-gate page_unlock(pp);
10250Sstevel@tonic-gate (*pfnp)++;
10260Sstevel@tonic-gate break;
10270Sstevel@tonic-gate }
10280Sstevel@tonic-gate
10290Sstevel@tonic-gate if (!PP_ISAGED(pp)) {
10300Sstevel@tonic-gate page_list_sub(pp, PG_CACHE_LIST);
10310Sstevel@tonic-gate page_hashout(pp, (kmutex_t *)NULL);
10320Sstevel@tonic-gate } else {
10330Sstevel@tonic-gate page_list_sub(pp, PG_FREE_LIST);
10340Sstevel@tonic-gate }
10350Sstevel@tonic-gate
10360Sstevel@tonic-gate if (iolock)
10370Sstevel@tonic-gate page_io_lock(pp);
10380Sstevel@tonic-gate page_list_concat(&plist, &pp);
10390Sstevel@tonic-gate
10400Sstevel@tonic-gate /*
10410Sstevel@tonic-gate * exit loop when pgcnt satisfied or segment boundary reached.
10420Sstevel@tonic-gate */
10430Sstevel@tonic-gate
10440Sstevel@tonic-gate } while ((++i < *pgcnt) && ((pfn + i) & pfnseg));
10450Sstevel@tonic-gate
10460Sstevel@tonic-gate *pfnp += i; /* set to next pfn to search */
10470Sstevel@tonic-gate
10480Sstevel@tonic-gate if (i >= minctg) {
10490Sstevel@tonic-gate *pgcnt -= i;
10500Sstevel@tonic-gate return (plist);
10510Sstevel@tonic-gate }
10520Sstevel@tonic-gate
10530Sstevel@tonic-gate /*
10540Sstevel@tonic-gate * failure: minctg not satisfied.
10550Sstevel@tonic-gate *
10560Sstevel@tonic-gate * if next request crosses segment boundary, set next pfn
10570Sstevel@tonic-gate * to search from the segment boundary.
10580Sstevel@tonic-gate */
10590Sstevel@tonic-gate if (((*pfnp + minctg - 1) & pfnseg) < (*pfnp & pfnseg))
10600Sstevel@tonic-gate *pfnp = roundup(*pfnp, pfnseg + 1);
10610Sstevel@tonic-gate
10620Sstevel@tonic-gate /* clean up any pages already allocated */
10630Sstevel@tonic-gate
10640Sstevel@tonic-gate while (plist) {
10650Sstevel@tonic-gate pp = plist;
10660Sstevel@tonic-gate page_sub(&plist, pp);
10670Sstevel@tonic-gate page_list_add(pp, PG_FREE_LIST | PG_LIST_TAIL);
10680Sstevel@tonic-gate if (iolock)
10690Sstevel@tonic-gate page_io_unlock(pp);
10700Sstevel@tonic-gate page_unlock(pp);
10710Sstevel@tonic-gate }
10720Sstevel@tonic-gate
10730Sstevel@tonic-gate return (NULL);
10740Sstevel@tonic-gate }
10755084Sjohnlev #endif /* !__xpv */
10760Sstevel@tonic-gate
10770Sstevel@tonic-gate /*
10780Sstevel@tonic-gate * verify that pages being returned from allocator have correct DMA attribute
10790Sstevel@tonic-gate */
10800Sstevel@tonic-gate #ifndef DEBUG
108112004Sjiang.liu@intel.com #define check_dma(a, b, c) (void)(0)
10820Sstevel@tonic-gate #else
10830Sstevel@tonic-gate static void
check_dma(ddi_dma_attr_t * dma_attr,page_t * pp,int cnt)10840Sstevel@tonic-gate check_dma(ddi_dma_attr_t *dma_attr, page_t *pp, int cnt)
10850Sstevel@tonic-gate {
10860Sstevel@tonic-gate if (dma_attr == NULL)
10870Sstevel@tonic-gate return;
10880Sstevel@tonic-gate
10890Sstevel@tonic-gate while (cnt-- > 0) {
10903446Smrj if (pa_to_ma(pfn_to_pa(pp->p_pagenum)) <
10910Sstevel@tonic-gate dma_attr->dma_attr_addr_lo)
10927240Srh87107 panic("PFN (pp=%p) below dma_attr_addr_lo", (void *)pp);
10933446Smrj if (pa_to_ma(pfn_to_pa(pp->p_pagenum)) >=
10940Sstevel@tonic-gate dma_attr->dma_attr_addr_hi)
10957240Srh87107 panic("PFN (pp=%p) above dma_attr_addr_hi", (void *)pp);
10960Sstevel@tonic-gate pp = pp->p_next;
10970Sstevel@tonic-gate }
10980Sstevel@tonic-gate }
10990Sstevel@tonic-gate #endif
11000Sstevel@tonic-gate
11015084Sjohnlev #if !defined(__xpv)
11020Sstevel@tonic-gate static page_t *
page_get_contigpage(pgcnt_t * pgcnt,ddi_dma_attr_t * mattr,int iolock)11030Sstevel@tonic-gate page_get_contigpage(pgcnt_t *pgcnt, ddi_dma_attr_t *mattr, int iolock)
11040Sstevel@tonic-gate {
11050Sstevel@tonic-gate pfn_t pfn;
11060Sstevel@tonic-gate int sgllen;
11070Sstevel@tonic-gate uint64_t pfnseg;
11080Sstevel@tonic-gate pgcnt_t minctg;
11090Sstevel@tonic-gate page_t *pplist = NULL, *plist;
11100Sstevel@tonic-gate uint64_t lo, hi;
11110Sstevel@tonic-gate pgcnt_t pfnalign = 0;
11120Sstevel@tonic-gate static pfn_t startpfn;
11130Sstevel@tonic-gate static pgcnt_t lastctgcnt;
11140Sstevel@tonic-gate uintptr_t align;
11150Sstevel@tonic-gate
11160Sstevel@tonic-gate CONTIG_LOCK();
11170Sstevel@tonic-gate
11180Sstevel@tonic-gate if (mattr) {
11190Sstevel@tonic-gate lo = mmu_btop((mattr->dma_attr_addr_lo + MMU_PAGEOFFSET));
11200Sstevel@tonic-gate hi = mmu_btop(mattr->dma_attr_addr_hi);
11210Sstevel@tonic-gate if (hi >= physmax)
11220Sstevel@tonic-gate hi = physmax - 1;
11230Sstevel@tonic-gate sgllen = mattr->dma_attr_sgllen;
11240Sstevel@tonic-gate pfnseg = mmu_btop(mattr->dma_attr_seg);
11250Sstevel@tonic-gate
11260Sstevel@tonic-gate align = maxbit(mattr->dma_attr_align, mattr->dma_attr_minxfer);
11270Sstevel@tonic-gate if (align > MMU_PAGESIZE)
11280Sstevel@tonic-gate pfnalign = mmu_btop(align);
11290Sstevel@tonic-gate
11300Sstevel@tonic-gate /*
11310Sstevel@tonic-gate * in order to satisfy the request, must minimally
11320Sstevel@tonic-gate * acquire minctg contiguous pages
11330Sstevel@tonic-gate */
11340Sstevel@tonic-gate minctg = howmany(*pgcnt, sgllen);
11350Sstevel@tonic-gate
11360Sstevel@tonic-gate ASSERT(hi >= lo);
11370Sstevel@tonic-gate
11380Sstevel@tonic-gate /*
11390Sstevel@tonic-gate * start from where last searched if the minctg >= lastctgcnt
11400Sstevel@tonic-gate */
11410Sstevel@tonic-gate if (minctg < lastctgcnt || startpfn < lo || startpfn > hi)
11420Sstevel@tonic-gate startpfn = lo;
11430Sstevel@tonic-gate } else {
11440Sstevel@tonic-gate hi = physmax - 1;
11450Sstevel@tonic-gate lo = 0;
11460Sstevel@tonic-gate sgllen = 1;
11470Sstevel@tonic-gate pfnseg = mmu.highest_pfn;
11480Sstevel@tonic-gate minctg = *pgcnt;
11490Sstevel@tonic-gate
11500Sstevel@tonic-gate if (minctg < lastctgcnt)
11510Sstevel@tonic-gate startpfn = lo;
11520Sstevel@tonic-gate }
11530Sstevel@tonic-gate lastctgcnt = minctg;
11540Sstevel@tonic-gate
11550Sstevel@tonic-gate ASSERT(pfnseg + 1 >= (uint64_t)minctg);
11560Sstevel@tonic-gate
11570Sstevel@tonic-gate /* conserve 16m memory - start search above 16m when possible */
11580Sstevel@tonic-gate if (hi > PFN_16M && startpfn < PFN_16M)
11590Sstevel@tonic-gate startpfn = PFN_16M;
11600Sstevel@tonic-gate
11610Sstevel@tonic-gate pfn = startpfn;
11620Sstevel@tonic-gate if (pfnalign)
11630Sstevel@tonic-gate pfn = P2ROUNDUP(pfn, pfnalign);
11640Sstevel@tonic-gate
11650Sstevel@tonic-gate while (pfn + minctg - 1 <= hi) {
11660Sstevel@tonic-gate
11670Sstevel@tonic-gate plist = is_contigpage_free(&pfn, pgcnt, minctg, pfnseg, iolock);
11680Sstevel@tonic-gate if (plist) {
11690Sstevel@tonic-gate page_list_concat(&pplist, &plist);
11700Sstevel@tonic-gate sgllen--;
11710Sstevel@tonic-gate /*
11720Sstevel@tonic-gate * return when contig pages no longer needed
11730Sstevel@tonic-gate */
11740Sstevel@tonic-gate if (!*pgcnt || ((*pgcnt <= sgllen) && !pfnalign)) {
11750Sstevel@tonic-gate startpfn = pfn;
11760Sstevel@tonic-gate CONTIG_UNLOCK();
11770Sstevel@tonic-gate check_dma(mattr, pplist, *pgcnt);
11780Sstevel@tonic-gate return (pplist);
11790Sstevel@tonic-gate }
11800Sstevel@tonic-gate minctg = howmany(*pgcnt, sgllen);
11810Sstevel@tonic-gate }
11820Sstevel@tonic-gate if (pfnalign)
11830Sstevel@tonic-gate pfn = P2ROUNDUP(pfn, pfnalign);
11840Sstevel@tonic-gate }
11850Sstevel@tonic-gate
11860Sstevel@tonic-gate /* cannot find contig pages in specified range */
11870Sstevel@tonic-gate if (startpfn == lo) {
11880Sstevel@tonic-gate CONTIG_UNLOCK();
11890Sstevel@tonic-gate return (NULL);
11900Sstevel@tonic-gate }
11910Sstevel@tonic-gate
11920Sstevel@tonic-gate /* did not start with lo previously */
11930Sstevel@tonic-gate pfn = lo;
11940Sstevel@tonic-gate if (pfnalign)
11950Sstevel@tonic-gate pfn = P2ROUNDUP(pfn, pfnalign);
11960Sstevel@tonic-gate
11970Sstevel@tonic-gate /* allow search to go above startpfn */
11980Sstevel@tonic-gate while (pfn < startpfn) {
11990Sstevel@tonic-gate
12000Sstevel@tonic-gate plist = is_contigpage_free(&pfn, pgcnt, minctg, pfnseg, iolock);
12010Sstevel@tonic-gate if (plist != NULL) {
12020Sstevel@tonic-gate
12030Sstevel@tonic-gate page_list_concat(&pplist, &plist);
12040Sstevel@tonic-gate sgllen--;
12050Sstevel@tonic-gate
12060Sstevel@tonic-gate /*
12070Sstevel@tonic-gate * return when contig pages no longer needed
12080Sstevel@tonic-gate */
12090Sstevel@tonic-gate if (!*pgcnt || ((*pgcnt <= sgllen) && !pfnalign)) {
12100Sstevel@tonic-gate startpfn = pfn;
12110Sstevel@tonic-gate CONTIG_UNLOCK();
12120Sstevel@tonic-gate check_dma(mattr, pplist, *pgcnt);
12130Sstevel@tonic-gate return (pplist);
12140Sstevel@tonic-gate }
12150Sstevel@tonic-gate minctg = howmany(*pgcnt, sgllen);
12160Sstevel@tonic-gate }
12170Sstevel@tonic-gate if (pfnalign)
12180Sstevel@tonic-gate pfn = P2ROUNDUP(pfn, pfnalign);
12190Sstevel@tonic-gate }
12200Sstevel@tonic-gate CONTIG_UNLOCK();
12210Sstevel@tonic-gate return (NULL);
12220Sstevel@tonic-gate }
12235084Sjohnlev #endif /* !__xpv */
12240Sstevel@tonic-gate
12250Sstevel@tonic-gate /*
12260Sstevel@tonic-gate * mnode_range_cnt() calculates the number of memory ranges for mnode and
12270Sstevel@tonic-gate * memranges[]. Used to determine the size of page lists and mnoderanges.
12280Sstevel@tonic-gate */
12290Sstevel@tonic-gate int
mnode_range_cnt(int mnode)12302961Sdp78419 mnode_range_cnt(int mnode)
12310Sstevel@tonic-gate {
12325084Sjohnlev #if defined(__xpv)
12335084Sjohnlev ASSERT(mnode == 0);
12345084Sjohnlev return (1);
12355084Sjohnlev #else /* __xpv */
12360Sstevel@tonic-gate int mri;
12370Sstevel@tonic-gate int mnrcnt = 0;
12380Sstevel@tonic-gate
12392961Sdp78419 if (mem_node_config[mnode].exists != 0) {
12400Sstevel@tonic-gate mri = nranges - 1;
12410Sstevel@tonic-gate
12420Sstevel@tonic-gate /* find the memranges index below contained in mnode range */
12430Sstevel@tonic-gate
12440Sstevel@tonic-gate while (MEMRANGEHI(mri) < mem_node_config[mnode].physbase)
12450Sstevel@tonic-gate mri--;
12460Sstevel@tonic-gate
12470Sstevel@tonic-gate /*
12480Sstevel@tonic-gate * increment mnode range counter when memranges or mnode
12490Sstevel@tonic-gate * boundary is reached.
12500Sstevel@tonic-gate */
12510Sstevel@tonic-gate while (mri >= 0 &&
12520Sstevel@tonic-gate mem_node_config[mnode].physmax >= MEMRANGELO(mri)) {
12530Sstevel@tonic-gate mnrcnt++;
12540Sstevel@tonic-gate if (mem_node_config[mnode].physmax > MEMRANGEHI(mri))
12550Sstevel@tonic-gate mri--;
12560Sstevel@tonic-gate else
12570Sstevel@tonic-gate break;
12580Sstevel@tonic-gate }
12590Sstevel@tonic-gate }
12602961Sdp78419 ASSERT(mnrcnt <= MAX_MNODE_MRANGES);
12610Sstevel@tonic-gate return (mnrcnt);
12625084Sjohnlev #endif /* __xpv */
12630Sstevel@tonic-gate }
12640Sstevel@tonic-gate
12655084Sjohnlev /*
12665084Sjohnlev * mnode_range_setup() initializes mnoderanges.
12675084Sjohnlev */
12680Sstevel@tonic-gate void
mnode_range_setup(mnoderange_t * mnoderanges)12690Sstevel@tonic-gate mnode_range_setup(mnoderange_t *mnoderanges)
12700Sstevel@tonic-gate {
127112004Sjiang.liu@intel.com mnoderange_t *mp = mnoderanges;
12720Sstevel@tonic-gate int mnode, mri;
127312004Sjiang.liu@intel.com int mindex = 0; /* current index into mnoderanges array */
127412004Sjiang.liu@intel.com int i, j;
127512004Sjiang.liu@intel.com pfn_t hipfn;
127612004Sjiang.liu@intel.com int last, hi;
12770Sstevel@tonic-gate
12780Sstevel@tonic-gate for (mnode = 0; mnode < max_mem_nodes; mnode++) {
12790Sstevel@tonic-gate if (mem_node_config[mnode].exists == 0)
12800Sstevel@tonic-gate continue;
12810Sstevel@tonic-gate
12820Sstevel@tonic-gate mri = nranges - 1;
12830Sstevel@tonic-gate
12840Sstevel@tonic-gate while (MEMRANGEHI(mri) < mem_node_config[mnode].physbase)
12850Sstevel@tonic-gate mri--;
12860Sstevel@tonic-gate
12870Sstevel@tonic-gate while (mri >= 0 && mem_node_config[mnode].physmax >=
12880Sstevel@tonic-gate MEMRANGELO(mri)) {
12895084Sjohnlev mnoderanges->mnr_pfnlo = MAX(MEMRANGELO(mri),
12905084Sjohnlev mem_node_config[mnode].physbase);
12915084Sjohnlev mnoderanges->mnr_pfnhi = MIN(MEMRANGEHI(mri),
12925084Sjohnlev mem_node_config[mnode].physmax);
12930Sstevel@tonic-gate mnoderanges->mnr_mnode = mnode;
12940Sstevel@tonic-gate mnoderanges->mnr_memrange = mri;
129512004Sjiang.liu@intel.com mnoderanges->mnr_exists = 1;
12960Sstevel@tonic-gate mnoderanges++;
129712004Sjiang.liu@intel.com mindex++;
12980Sstevel@tonic-gate if (mem_node_config[mnode].physmax > MEMRANGEHI(mri))
12990Sstevel@tonic-gate mri--;
13000Sstevel@tonic-gate else
13010Sstevel@tonic-gate break;
13020Sstevel@tonic-gate }
13030Sstevel@tonic-gate }
130412004Sjiang.liu@intel.com
130512004Sjiang.liu@intel.com /*
130612004Sjiang.liu@intel.com * For now do a simple sort of the mnoderanges array to fill in
130712004Sjiang.liu@intel.com * the mnr_next fields. Since mindex is expected to be relatively
130812004Sjiang.liu@intel.com * small, using a simple O(N^2) algorithm.
130912004Sjiang.liu@intel.com */
131012004Sjiang.liu@intel.com for (i = 0; i < mindex; i++) {
131112004Sjiang.liu@intel.com if (mp[i].mnr_pfnlo == 0) /* find lowest */
131212004Sjiang.liu@intel.com break;
131312004Sjiang.liu@intel.com }
131412004Sjiang.liu@intel.com ASSERT(i < mindex);
131512004Sjiang.liu@intel.com last = i;
131612004Sjiang.liu@intel.com mtype16m = last;
131712004Sjiang.liu@intel.com mp[last].mnr_next = -1;
131812004Sjiang.liu@intel.com for (i = 0; i < mindex - 1; i++) {
131912004Sjiang.liu@intel.com hipfn = (pfn_t)(-1);
132012004Sjiang.liu@intel.com hi = -1;
132112004Sjiang.liu@intel.com /* find next highest mnode range */
132212004Sjiang.liu@intel.com for (j = 0; j < mindex; j++) {
132312004Sjiang.liu@intel.com if (mp[j].mnr_pfnlo > mp[last].mnr_pfnlo &&
132412004Sjiang.liu@intel.com mp[j].mnr_pfnlo < hipfn) {
132512004Sjiang.liu@intel.com hipfn = mp[j].mnr_pfnlo;
132612004Sjiang.liu@intel.com hi = j;
132712004Sjiang.liu@intel.com }
132812004Sjiang.liu@intel.com }
132912004Sjiang.liu@intel.com mp[hi].mnr_next = last;
133012004Sjiang.liu@intel.com last = hi;
133112004Sjiang.liu@intel.com }
133212004Sjiang.liu@intel.com mtypetop = last;
13330Sstevel@tonic-gate }
13340Sstevel@tonic-gate
133512004Sjiang.liu@intel.com #ifndef __xpv
133612004Sjiang.liu@intel.com /*
133712004Sjiang.liu@intel.com * Update mnoderanges for memory hot-add DR operations.
133812004Sjiang.liu@intel.com */
133912004Sjiang.liu@intel.com static void
mnode_range_add(int mnode)134012004Sjiang.liu@intel.com mnode_range_add(int mnode)
134112004Sjiang.liu@intel.com {
134212004Sjiang.liu@intel.com int *prev;
134312004Sjiang.liu@intel.com int n, mri;
134412004Sjiang.liu@intel.com pfn_t start, end;
134512004Sjiang.liu@intel.com extern void membar_sync(void);
134612004Sjiang.liu@intel.com
134712004Sjiang.liu@intel.com ASSERT(0 <= mnode && mnode < max_mem_nodes);
134812004Sjiang.liu@intel.com ASSERT(mem_node_config[mnode].exists);
134912004Sjiang.liu@intel.com start = mem_node_config[mnode].physbase;
135012004Sjiang.liu@intel.com end = mem_node_config[mnode].physmax;
135112004Sjiang.liu@intel.com ASSERT(start <= end);
135212004Sjiang.liu@intel.com mutex_enter(&mnoderange_lock);
135312004Sjiang.liu@intel.com
135412004Sjiang.liu@intel.com #ifdef DEBUG
135512004Sjiang.liu@intel.com /* Check whether it interleaves with other memory nodes. */
135612004Sjiang.liu@intel.com for (n = mtypetop; n != -1; n = mnoderanges[n].mnr_next) {
135712004Sjiang.liu@intel.com ASSERT(mnoderanges[n].mnr_exists);
135812004Sjiang.liu@intel.com if (mnoderanges[n].mnr_mnode == mnode)
135912004Sjiang.liu@intel.com continue;
136012004Sjiang.liu@intel.com ASSERT(start > mnoderanges[n].mnr_pfnhi ||
136112004Sjiang.liu@intel.com end < mnoderanges[n].mnr_pfnlo);
136212004Sjiang.liu@intel.com }
136312004Sjiang.liu@intel.com #endif /* DEBUG */
136412004Sjiang.liu@intel.com
136512004Sjiang.liu@intel.com mri = nranges - 1;
136612004Sjiang.liu@intel.com while (MEMRANGEHI(mri) < mem_node_config[mnode].physbase)
136712004Sjiang.liu@intel.com mri--;
136812004Sjiang.liu@intel.com while (mri >= 0 && mem_node_config[mnode].physmax >= MEMRANGELO(mri)) {
136912004Sjiang.liu@intel.com /* Check whether mtype already exists. */
137012004Sjiang.liu@intel.com for (n = mtypetop; n != -1; n = mnoderanges[n].mnr_next) {
137112004Sjiang.liu@intel.com if (mnoderanges[n].mnr_mnode == mnode &&
137212004Sjiang.liu@intel.com mnoderanges[n].mnr_memrange == mri) {
137312004Sjiang.liu@intel.com mnoderanges[n].mnr_pfnlo = MAX(MEMRANGELO(mri),
137412004Sjiang.liu@intel.com start);
137512004Sjiang.liu@intel.com mnoderanges[n].mnr_pfnhi = MIN(MEMRANGEHI(mri),
137612004Sjiang.liu@intel.com end);
137712004Sjiang.liu@intel.com break;
137812004Sjiang.liu@intel.com }
137912004Sjiang.liu@intel.com }
138012004Sjiang.liu@intel.com
138112004Sjiang.liu@intel.com /* Add a new entry if it doesn't exist yet. */
138212004Sjiang.liu@intel.com if (n == -1) {
138312004Sjiang.liu@intel.com /* Try to find an unused entry in mnoderanges array. */
138412004Sjiang.liu@intel.com for (n = 0; n < mnoderangecnt; n++) {
138512004Sjiang.liu@intel.com if (mnoderanges[n].mnr_exists == 0)
138612004Sjiang.liu@intel.com break;
138712004Sjiang.liu@intel.com }
138812004Sjiang.liu@intel.com ASSERT(n < mnoderangecnt);
138912004Sjiang.liu@intel.com mnoderanges[n].mnr_pfnlo = MAX(MEMRANGELO(mri), start);
139012004Sjiang.liu@intel.com mnoderanges[n].mnr_pfnhi = MIN(MEMRANGEHI(mri), end);
139112004Sjiang.liu@intel.com mnoderanges[n].mnr_mnode = mnode;
139212004Sjiang.liu@intel.com mnoderanges[n].mnr_memrange = mri;
139312004Sjiang.liu@intel.com mnoderanges[n].mnr_exists = 1;
139412004Sjiang.liu@intel.com /* Page 0 should always be present. */
139512004Sjiang.liu@intel.com for (prev = &mtypetop;
139612004Sjiang.liu@intel.com mnoderanges[*prev].mnr_pfnlo > start;
139712004Sjiang.liu@intel.com prev = &mnoderanges[*prev].mnr_next) {
139812004Sjiang.liu@intel.com ASSERT(mnoderanges[*prev].mnr_next >= 0);
139912004Sjiang.liu@intel.com ASSERT(mnoderanges[*prev].mnr_pfnlo > end);
140012004Sjiang.liu@intel.com }
140112004Sjiang.liu@intel.com mnoderanges[n].mnr_next = *prev;
140212004Sjiang.liu@intel.com membar_sync();
140312004Sjiang.liu@intel.com *prev = n;
140412004Sjiang.liu@intel.com }
140512004Sjiang.liu@intel.com
140612004Sjiang.liu@intel.com if (mem_node_config[mnode].physmax > MEMRANGEHI(mri))
140712004Sjiang.liu@intel.com mri--;
140812004Sjiang.liu@intel.com else
140912004Sjiang.liu@intel.com break;
141012004Sjiang.liu@intel.com }
141112004Sjiang.liu@intel.com
141212004Sjiang.liu@intel.com mutex_exit(&mnoderange_lock);
141312004Sjiang.liu@intel.com }
141412004Sjiang.liu@intel.com
141512004Sjiang.liu@intel.com /*
141612004Sjiang.liu@intel.com * Update mnoderanges for memory hot-removal DR operations.
141712004Sjiang.liu@intel.com */
141812004Sjiang.liu@intel.com static void
mnode_range_del(int mnode)141912004Sjiang.liu@intel.com mnode_range_del(int mnode)
142012004Sjiang.liu@intel.com {
142112004Sjiang.liu@intel.com _NOTE(ARGUNUSED(mnode));
142212004Sjiang.liu@intel.com ASSERT(0 <= mnode && mnode < max_mem_nodes);
142312004Sjiang.liu@intel.com /* TODO: support deletion operation. */
142412004Sjiang.liu@intel.com ASSERT(0);
142512004Sjiang.liu@intel.com }
142612004Sjiang.liu@intel.com
142712004Sjiang.liu@intel.com void
plat_slice_add(pfn_t start,pfn_t end)142812004Sjiang.liu@intel.com plat_slice_add(pfn_t start, pfn_t end)
142912004Sjiang.liu@intel.com {
143012004Sjiang.liu@intel.com mem_node_add_slice(start, end);
143112004Sjiang.liu@intel.com if (plat_dr_enabled()) {
143212004Sjiang.liu@intel.com mnode_range_add(PFN_2_MEM_NODE(start));
143312004Sjiang.liu@intel.com }
143412004Sjiang.liu@intel.com }
143512004Sjiang.liu@intel.com
143612004Sjiang.liu@intel.com void
plat_slice_del(pfn_t start,pfn_t end)143712004Sjiang.liu@intel.com plat_slice_del(pfn_t start, pfn_t end)
143812004Sjiang.liu@intel.com {
143912004Sjiang.liu@intel.com ASSERT(PFN_2_MEM_NODE(start) == PFN_2_MEM_NODE(end));
144012004Sjiang.liu@intel.com ASSERT(plat_dr_enabled());
144112004Sjiang.liu@intel.com mnode_range_del(PFN_2_MEM_NODE(start));
144212004Sjiang.liu@intel.com mem_node_del_slice(start, end);
144312004Sjiang.liu@intel.com }
144412004Sjiang.liu@intel.com #endif /* __xpv */
144512004Sjiang.liu@intel.com
14465084Sjohnlev /*ARGSUSED*/
14475084Sjohnlev int
mtype_init(vnode_t * vp,caddr_t vaddr,uint_t * flags,size_t pgsz)14485084Sjohnlev mtype_init(vnode_t *vp, caddr_t vaddr, uint_t *flags, size_t pgsz)
14495084Sjohnlev {
145012004Sjiang.liu@intel.com int mtype = mtypetop;
14515084Sjohnlev
14525084Sjohnlev #if !defined(__xpv)
14535084Sjohnlev #if defined(__i386)
14545084Sjohnlev /*
14555084Sjohnlev * set the mtype range
145612004Sjiang.liu@intel.com * - kmem requests need to be below 4g if restricted_kmemalloc is set.
14575084Sjohnlev * - for non kmem requests, set range to above 4g if memory below 4g
14585084Sjohnlev * runs low.
14595084Sjohnlev */
14605084Sjohnlev if (restricted_kmemalloc && VN_ISKAS(vp) &&
14615084Sjohnlev (caddr_t)(vaddr) >= kernelheap &&
14625084Sjohnlev (caddr_t)(vaddr) < ekernelheap) {
14635084Sjohnlev ASSERT(physmax4g);
14645084Sjohnlev mtype = mtype4g;
14655084Sjohnlev if (RESTRICT16M_ALLOC(freemem4g - btop(pgsz),
14665084Sjohnlev btop(pgsz), *flags)) {
14675084Sjohnlev *flags |= PGI_MT_RANGE16M;
14685084Sjohnlev } else {
14695084Sjohnlev VM_STAT_ADD(vmm_vmstats.unrestrict16mcnt);
14705084Sjohnlev VM_STAT_COND_ADD((*flags & PG_PANIC),
14715084Sjohnlev vmm_vmstats.pgpanicalloc);
14725084Sjohnlev *flags |= PGI_MT_RANGE0;
14735084Sjohnlev }
14745084Sjohnlev return (mtype);
14755084Sjohnlev }
14765084Sjohnlev #endif /* __i386 */
14775084Sjohnlev
14785084Sjohnlev if (RESTRICT4G_ALLOC) {
14795084Sjohnlev VM_STAT_ADD(vmm_vmstats.restrict4gcnt);
14805084Sjohnlev /* here only for > 4g systems */
14815084Sjohnlev *flags |= PGI_MT_RANGE4G;
14825084Sjohnlev } else if (RESTRICT16M_ALLOC(freemem, btop(pgsz), *flags)) {
14835084Sjohnlev *flags |= PGI_MT_RANGE16M;
14845084Sjohnlev } else {
14855084Sjohnlev VM_STAT_ADD(vmm_vmstats.unrestrict16mcnt);
14865084Sjohnlev VM_STAT_COND_ADD((*flags & PG_PANIC), vmm_vmstats.pgpanicalloc);
14875084Sjohnlev *flags |= PGI_MT_RANGE0;
14885084Sjohnlev }
14895084Sjohnlev #endif /* !__xpv */
14905084Sjohnlev return (mtype);
14915084Sjohnlev }
14925084Sjohnlev
14935084Sjohnlev
14945084Sjohnlev /* mtype init for page_get_replacement_page */
14955084Sjohnlev /*ARGSUSED*/
14965084Sjohnlev int
mtype_pgr_init(int * flags,page_t * pp,int mnode,pgcnt_t pgcnt)14975084Sjohnlev mtype_pgr_init(int *flags, page_t *pp, int mnode, pgcnt_t pgcnt)
14985084Sjohnlev {
149912004Sjiang.liu@intel.com int mtype = mtypetop;
150012004Sjiang.liu@intel.com #if !defined(__xpv)
15015084Sjohnlev if (RESTRICT16M_ALLOC(freemem, pgcnt, *flags)) {
15025084Sjohnlev *flags |= PGI_MT_RANGE16M;
15035084Sjohnlev } else {
15045084Sjohnlev VM_STAT_ADD(vmm_vmstats.unrestrict16mcnt);
15055084Sjohnlev *flags |= PGI_MT_RANGE0;
15065084Sjohnlev }
15075084Sjohnlev #endif
15085084Sjohnlev return (mtype);
15095084Sjohnlev }
15105084Sjohnlev
15110Sstevel@tonic-gate /*
15120Sstevel@tonic-gate * Determine if the mnode range specified in mtype contains memory belonging
15130Sstevel@tonic-gate * to memory node mnode. If flags & PGI_MT_RANGE is set then mtype contains
151412004Sjiang.liu@intel.com * the range from high pfn to 0, 16m or 4g.
15150Sstevel@tonic-gate *
15160Sstevel@tonic-gate * Return first mnode range type index found otherwise return -1 if none found.
15170Sstevel@tonic-gate */
15180Sstevel@tonic-gate int
mtype_func(int mnode,int mtype,uint_t flags)15190Sstevel@tonic-gate mtype_func(int mnode, int mtype, uint_t flags)
15200Sstevel@tonic-gate {
15210Sstevel@tonic-gate if (flags & PGI_MT_RANGE) {
152212004Sjiang.liu@intel.com int mnr_lim = MRI_0;
152312004Sjiang.liu@intel.com
152412004Sjiang.liu@intel.com if (flags & PGI_MT_NEXT) {
152512004Sjiang.liu@intel.com mtype = mnoderanges[mtype].mnr_next;
152612004Sjiang.liu@intel.com }
15275084Sjohnlev if (flags & PGI_MT_RANGE4G)
152812004Sjiang.liu@intel.com mnr_lim = MRI_4G; /* exclude 0-4g range */
15291385Skchow else if (flags & PGI_MT_RANGE16M)
153012004Sjiang.liu@intel.com mnr_lim = MRI_16M; /* exclude 0-16m range */
153112004Sjiang.liu@intel.com while (mtype != -1 &&
153212004Sjiang.liu@intel.com mnoderanges[mtype].mnr_memrange <= mnr_lim) {
15330Sstevel@tonic-gate if (mnoderanges[mtype].mnr_mnode == mnode)
15340Sstevel@tonic-gate return (mtype);
153512004Sjiang.liu@intel.com mtype = mnoderanges[mtype].mnr_next;
15360Sstevel@tonic-gate }
15375084Sjohnlev } else if (mnoderanges[mtype].mnr_mnode == mnode) {
15385084Sjohnlev return (mtype);
15390Sstevel@tonic-gate }
15400Sstevel@tonic-gate return (-1);
15410Sstevel@tonic-gate }
15420Sstevel@tonic-gate
15430Sstevel@tonic-gate /*
15441373Skchow * Update the page list max counts with the pfn range specified by the
154512004Sjiang.liu@intel.com * input parameters.
15461373Skchow */
15471373Skchow void
mtype_modify_max(pfn_t startpfn,long cnt)15481373Skchow mtype_modify_max(pfn_t startpfn, long cnt)
15491373Skchow {
155012004Sjiang.liu@intel.com int mtype;
155112004Sjiang.liu@intel.com pgcnt_t inc;
155212004Sjiang.liu@intel.com spgcnt_t scnt = (spgcnt_t)(cnt);
155312004Sjiang.liu@intel.com pgcnt_t acnt = ABS(scnt);
155412004Sjiang.liu@intel.com pfn_t endpfn = startpfn + acnt;
155512004Sjiang.liu@intel.com pfn_t pfn, lo;
15561373Skchow
15575084Sjohnlev if (!physmax4g)
15585084Sjohnlev return;
15595084Sjohnlev
156012004Sjiang.liu@intel.com mtype = mtypetop;
156112004Sjiang.liu@intel.com for (pfn = endpfn; pfn > startpfn; ) {
156212004Sjiang.liu@intel.com ASSERT(mtype != -1);
156312004Sjiang.liu@intel.com lo = mnoderanges[mtype].mnr_pfnlo;
156412004Sjiang.liu@intel.com if (pfn > lo) {
156512004Sjiang.liu@intel.com if (startpfn >= lo) {
156612004Sjiang.liu@intel.com inc = pfn - startpfn;
15671373Skchow } else {
156812004Sjiang.liu@intel.com inc = pfn - lo;
15691373Skchow }
157012004Sjiang.liu@intel.com if (mnoderanges[mtype].mnr_memrange != MRI_4G) {
157112004Sjiang.liu@intel.com if (scnt > 0)
157212004Sjiang.liu@intel.com maxmem4g += inc;
157312004Sjiang.liu@intel.com else
157412004Sjiang.liu@intel.com maxmem4g -= inc;
157512004Sjiang.liu@intel.com }
157612004Sjiang.liu@intel.com pfn -= inc;
15771373Skchow }
157812004Sjiang.liu@intel.com mtype = mnoderanges[mtype].mnr_next;
15791373Skchow }
15801373Skchow }
15811373Skchow
15825084Sjohnlev int
mtype_2_mrange(int mtype)15835084Sjohnlev mtype_2_mrange(int mtype)
15845084Sjohnlev {
15855084Sjohnlev return (mnoderanges[mtype].mnr_memrange);
15865084Sjohnlev }
15875084Sjohnlev
15885084Sjohnlev void
mnodetype_2_pfn(int mnode,int mtype,pfn_t * pfnlo,pfn_t * pfnhi)15895084Sjohnlev mnodetype_2_pfn(int mnode, int mtype, pfn_t *pfnlo, pfn_t *pfnhi)
15905084Sjohnlev {
159112004Sjiang.liu@intel.com _NOTE(ARGUNUSED(mnode));
15925084Sjohnlev ASSERT(mnoderanges[mtype].mnr_mnode == mnode);
15935084Sjohnlev *pfnlo = mnoderanges[mtype].mnr_pfnlo;
15945084Sjohnlev *pfnhi = mnoderanges[mtype].mnr_pfnhi;
15955084Sjohnlev }
15965084Sjohnlev
15975084Sjohnlev size_t
plcnt_sz(size_t ctrs_sz)15985084Sjohnlev plcnt_sz(size_t ctrs_sz)
15995084Sjohnlev {
16005084Sjohnlev #ifdef DEBUG
16015084Sjohnlev int szc, colors;
16025084Sjohnlev
16035084Sjohnlev ctrs_sz += mnoderangecnt * sizeof (struct mnr_mts) * mmu_page_sizes;
16045084Sjohnlev for (szc = 0; szc < mmu_page_sizes; szc++) {
16055084Sjohnlev colors = page_get_pagecolors(szc);
16065084Sjohnlev ctrs_sz += mnoderangecnt * sizeof (pgcnt_t) * colors;
16075084Sjohnlev }
16085084Sjohnlev #endif
16095084Sjohnlev return (ctrs_sz);
16105084Sjohnlev }
16115084Sjohnlev
16125084Sjohnlev caddr_t
plcnt_init(caddr_t addr)16135084Sjohnlev plcnt_init(caddr_t addr)
16145084Sjohnlev {
16155084Sjohnlev #ifdef DEBUG
16165084Sjohnlev int mt, szc, colors;
16175084Sjohnlev
16185084Sjohnlev for (mt = 0; mt < mnoderangecnt; mt++) {
16195084Sjohnlev mnoderanges[mt].mnr_mts = (struct mnr_mts *)addr;
16205084Sjohnlev addr += (sizeof (struct mnr_mts) * mmu_page_sizes);
16215084Sjohnlev for (szc = 0; szc < mmu_page_sizes; szc++) {
16225084Sjohnlev colors = page_get_pagecolors(szc);
16235084Sjohnlev mnoderanges[mt].mnr_mts[szc].mnr_mts_colors = colors;
16245084Sjohnlev mnoderanges[mt].mnr_mts[szc].mnr_mtsc_pgcnt =
16255084Sjohnlev (pgcnt_t *)addr;
16265084Sjohnlev addr += (sizeof (pgcnt_t) * colors);
16275084Sjohnlev }
16285084Sjohnlev }
16295084Sjohnlev #endif
16305084Sjohnlev return (addr);
16315084Sjohnlev }
16325084Sjohnlev
16335084Sjohnlev void
plcnt_inc_dec(page_t * pp,int mtype,int szc,long cnt,int flags)16345084Sjohnlev plcnt_inc_dec(page_t *pp, int mtype, int szc, long cnt, int flags)
16355084Sjohnlev {
163612004Sjiang.liu@intel.com _NOTE(ARGUNUSED(pp));
16375084Sjohnlev #ifdef DEBUG
16385084Sjohnlev int bin = PP_2_BIN(pp);
16395084Sjohnlev
16405084Sjohnlev atomic_add_long(&mnoderanges[mtype].mnr_mts[szc].mnr_mts_pgcnt, cnt);
16415084Sjohnlev atomic_add_long(&mnoderanges[mtype].mnr_mts[szc].mnr_mtsc_pgcnt[bin],
16425084Sjohnlev cnt);
16435084Sjohnlev #endif
16445084Sjohnlev ASSERT(mtype == PP_2_MTYPE(pp));
164512004Sjiang.liu@intel.com if (physmax4g && mnoderanges[mtype].mnr_memrange != MRI_4G)
16465084Sjohnlev atomic_add_long(&freemem4g, cnt);
16475084Sjohnlev if (flags & PG_CACHE_LIST)
16485084Sjohnlev atomic_add_long(&mnoderanges[mtype].mnr_mt_clpgcnt, cnt);
16495084Sjohnlev else
16505466Skchow atomic_add_long(&mnoderanges[mtype].mnr_mt_flpgcnt[szc], cnt);
16515466Skchow atomic_add_long(&mnoderanges[mtype].mnr_mt_totcnt, cnt);
16525084Sjohnlev }
16535084Sjohnlev
16541373Skchow /*
1655414Skchow * Returns the free page count for mnode
1656414Skchow */
1657414Skchow int
mnode_pgcnt(int mnode)1658414Skchow mnode_pgcnt(int mnode)
1659414Skchow {
166012004Sjiang.liu@intel.com int mtype = mtypetop;
1661414Skchow int flags = PGI_MT_RANGE0;
1662414Skchow pgcnt_t pgcnt = 0;
1663414Skchow
1664414Skchow mtype = mtype_func(mnode, mtype, flags);
1665414Skchow
1666414Skchow while (mtype != -1) {
16671385Skchow pgcnt += MTYPE_FREEMEM(mtype);
1668414Skchow mtype = mtype_func(mnode, mtype, flags | PGI_MT_NEXT);
1669414Skchow }
1670414Skchow return (pgcnt);
1671414Skchow }
1672414Skchow
1673414Skchow /*
16740Sstevel@tonic-gate * Initialize page coloring variables based on the l2 cache parameters.
16750Sstevel@tonic-gate * Calculate and return memory needed for page coloring data structures.
16760Sstevel@tonic-gate */
16770Sstevel@tonic-gate size_t
page_coloring_init(uint_t l2_sz,int l2_linesz,int l2_assoc)16780Sstevel@tonic-gate page_coloring_init(uint_t l2_sz, int l2_linesz, int l2_assoc)
16790Sstevel@tonic-gate {
168012004Sjiang.liu@intel.com _NOTE(ARGUNUSED(l2_linesz));
16810Sstevel@tonic-gate size_t colorsz = 0;
16820Sstevel@tonic-gate int i;
16830Sstevel@tonic-gate int colors;
16840Sstevel@tonic-gate
16855084Sjohnlev #if defined(__xpv)
16865084Sjohnlev /*
16875084Sjohnlev * Hypervisor domains currently don't have any concept of NUMA.
16885084Sjohnlev * Hence we'll act like there is only 1 memrange.
16895084Sjohnlev */
16905084Sjohnlev i = memrange_num(1);
16915084Sjohnlev #else /* !__xpv */
16920Sstevel@tonic-gate /*
16930Sstevel@tonic-gate * Reduce the memory ranges lists if we don't have large amounts
16940Sstevel@tonic-gate * of memory. This avoids searching known empty free lists.
169512004Sjiang.liu@intel.com * To support memory DR operations, we need to keep memory ranges
169612004Sjiang.liu@intel.com * for possible memory hot-add operations.
16970Sstevel@tonic-gate */
169812004Sjiang.liu@intel.com if (plat_dr_physmax > physmax)
169912004Sjiang.liu@intel.com i = memrange_num(plat_dr_physmax);
170012004Sjiang.liu@intel.com else
170112004Sjiang.liu@intel.com i = memrange_num(physmax);
17020Sstevel@tonic-gate #if defined(__i386)
170312004Sjiang.liu@intel.com if (i > MRI_4G)
17040Sstevel@tonic-gate restricted_kmemalloc = 0;
17050Sstevel@tonic-gate #endif
17060Sstevel@tonic-gate /* physmax greater than 4g */
170712004Sjiang.liu@intel.com if (i == MRI_4G)
17080Sstevel@tonic-gate physmax4g = 1;
17095084Sjohnlev #endif /* !__xpv */
17105084Sjohnlev memranges += i;
17115084Sjohnlev nranges -= i;
17120Sstevel@tonic-gate
17135349Skchow ASSERT(mmu_page_sizes <= MMU_PAGE_SIZES);
17145349Skchow
17150Sstevel@tonic-gate ASSERT(ISP2(l2_linesz));
17160Sstevel@tonic-gate ASSERT(l2_sz > MMU_PAGESIZE);
17170Sstevel@tonic-gate
17180Sstevel@tonic-gate /* l2_assoc is 0 for fully associative l2 cache */
17190Sstevel@tonic-gate if (l2_assoc)
17200Sstevel@tonic-gate l2_colors = MAX(1, l2_sz / (l2_assoc * MMU_PAGESIZE));
17210Sstevel@tonic-gate else
17220Sstevel@tonic-gate l2_colors = 1;
17230Sstevel@tonic-gate
17247069Svd224797 ASSERT(ISP2(l2_colors));
17257069Svd224797
17260Sstevel@tonic-gate /* for scalability, configure at least PAGE_COLORS_MIN color bins */
17270Sstevel@tonic-gate page_colors = MAX(l2_colors, PAGE_COLORS_MIN);
17280Sstevel@tonic-gate
17290Sstevel@tonic-gate /*
17300Sstevel@tonic-gate * cpu_page_colors is non-zero when a page color may be spread across
17310Sstevel@tonic-gate * multiple bins.
17320Sstevel@tonic-gate */
17330Sstevel@tonic-gate if (l2_colors < page_colors)
17340Sstevel@tonic-gate cpu_page_colors = l2_colors;
17350Sstevel@tonic-gate
17360Sstevel@tonic-gate ASSERT(ISP2(page_colors));
17370Sstevel@tonic-gate
17380Sstevel@tonic-gate page_colors_mask = page_colors - 1;
17390Sstevel@tonic-gate
17400Sstevel@tonic-gate ASSERT(ISP2(CPUSETSIZE()));
17410Sstevel@tonic-gate page_coloring_shift = lowbit(CPUSETSIZE());
17420Sstevel@tonic-gate
17432961Sdp78419 /* initialize number of colors per page size */
17442961Sdp78419 for (i = 0; i <= mmu.max_page_level; i++) {
17452961Sdp78419 hw_page_array[i].hp_size = LEVEL_SIZE(i);
17462961Sdp78419 hw_page_array[i].hp_shift = LEVEL_SHIFT(i);
17472961Sdp78419 hw_page_array[i].hp_pgcnt = LEVEL_SIZE(i) >> LEVEL_SHIFT(0);
17482961Sdp78419 hw_page_array[i].hp_colors = (page_colors_mask >>
17492961Sdp78419 (hw_page_array[i].hp_shift - hw_page_array[0].hp_shift))
17502961Sdp78419 + 1;
17513717Sdp78419 colorequivszc[i] = 0;
17522961Sdp78419 }
17532961Sdp78419
17542961Sdp78419 /*
17552961Sdp78419 * The value of cpu_page_colors determines if additional color bins
17562961Sdp78419 * need to be checked for a particular color in the page_get routines.
17572961Sdp78419 */
17582961Sdp78419 if (cpu_page_colors != 0) {
17592961Sdp78419
17602961Sdp78419 int a = lowbit(page_colors) - lowbit(cpu_page_colors);
17612961Sdp78419 ASSERT(a > 0);
17622961Sdp78419 ASSERT(a < 16);
17632961Sdp78419
17642961Sdp78419 for (i = 0; i <= mmu.max_page_level; i++) {
17652961Sdp78419 if ((colors = hw_page_array[i].hp_colors) <= 1) {
17662961Sdp78419 colorequivszc[i] = 0;
17672961Sdp78419 continue;
17682961Sdp78419 }
17692961Sdp78419 while ((colors >> a) == 0)
17702961Sdp78419 a--;
17712961Sdp78419 ASSERT(a >= 0);
17722961Sdp78419
17732961Sdp78419 /* higher 4 bits encodes color equiv mask */
17742961Sdp78419 colorequivszc[i] = (a << 4);
17752961Sdp78419 }
17762961Sdp78419 }
17772961Sdp78419
17785084Sjohnlev /* factor in colorequiv to check additional 'equivalent' bins. */
17795084Sjohnlev if (colorequiv > 1) {
17805084Sjohnlev
17815084Sjohnlev int a = lowbit(colorequiv) - 1;
17825084Sjohnlev if (a > 15)
17835084Sjohnlev a = 15;
17845084Sjohnlev
17855084Sjohnlev for (i = 0; i <= mmu.max_page_level; i++) {
17865084Sjohnlev if ((colors = hw_page_array[i].hp_colors) <= 1) {
17875084Sjohnlev continue;
17885084Sjohnlev }
17895084Sjohnlev while ((colors >> a) == 0)
17905084Sjohnlev a--;
17915084Sjohnlev if ((a << 4) > colorequivszc[i]) {
17925084Sjohnlev colorequivszc[i] = (a << 4);
17935084Sjohnlev }
17945084Sjohnlev }
17955084Sjohnlev }
17965084Sjohnlev
17970Sstevel@tonic-gate /* size for mnoderanges */
17982961Sdp78419 for (mnoderangecnt = 0, i = 0; i < max_mem_nodes; i++)
17992961Sdp78419 mnoderangecnt += mnode_range_cnt(i);
180012004Sjiang.liu@intel.com if (plat_dr_support_memory()) {
180112004Sjiang.liu@intel.com /*
180212004Sjiang.liu@intel.com * Reserve enough space for memory DR operations.
180312004Sjiang.liu@intel.com * Two extra mnoderanges for possbile fragmentations,
180412004Sjiang.liu@intel.com * one for the 2G boundary and the other for the 4G boundary.
180512004Sjiang.liu@intel.com * We don't expect a memory board crossing the 16M boundary
180612004Sjiang.liu@intel.com * for memory hot-add operations on x86 platforms.
180712004Sjiang.liu@intel.com */
180812004Sjiang.liu@intel.com mnoderangecnt += 2 + max_mem_nodes - lgrp_plat_node_cnt;
180912004Sjiang.liu@intel.com }
18100Sstevel@tonic-gate colorsz = mnoderangecnt * sizeof (mnoderange_t);
18110Sstevel@tonic-gate
18120Sstevel@tonic-gate /* size for fpc_mutex and cpc_mutex */
18130Sstevel@tonic-gate colorsz += (2 * max_mem_nodes * sizeof (kmutex_t) * NPC_MUTEX);
18140Sstevel@tonic-gate
18150Sstevel@tonic-gate /* size of page_freelists */
18160Sstevel@tonic-gate colorsz += mnoderangecnt * sizeof (page_t ***);
18170Sstevel@tonic-gate colorsz += mnoderangecnt * mmu_page_sizes * sizeof (page_t **);
18180Sstevel@tonic-gate
18190Sstevel@tonic-gate for (i = 0; i < mmu_page_sizes; i++) {
18200Sstevel@tonic-gate colors = page_get_pagecolors(i);
18210Sstevel@tonic-gate colorsz += mnoderangecnt * colors * sizeof (page_t *);
18220Sstevel@tonic-gate }
18230Sstevel@tonic-gate
18240Sstevel@tonic-gate /* size of page_cachelists */
18250Sstevel@tonic-gate colorsz += mnoderangecnt * sizeof (page_t **);
18260Sstevel@tonic-gate colorsz += mnoderangecnt * page_colors * sizeof (page_t *);
18270Sstevel@tonic-gate
18280Sstevel@tonic-gate return (colorsz);
18290Sstevel@tonic-gate }
18300Sstevel@tonic-gate
18310Sstevel@tonic-gate /*
18320Sstevel@tonic-gate * Called once at startup to configure page_coloring data structures and
18330Sstevel@tonic-gate * does the 1st page_free()/page_freelist_add().
18340Sstevel@tonic-gate */
18350Sstevel@tonic-gate void
page_coloring_setup(caddr_t pcmemaddr)18360Sstevel@tonic-gate page_coloring_setup(caddr_t pcmemaddr)
18370Sstevel@tonic-gate {
18380Sstevel@tonic-gate int i;
18390Sstevel@tonic-gate int j;
18400Sstevel@tonic-gate int k;
18410Sstevel@tonic-gate caddr_t addr;
18420Sstevel@tonic-gate int colors;
18430Sstevel@tonic-gate
18440Sstevel@tonic-gate /*
18450Sstevel@tonic-gate * do page coloring setup
18460Sstevel@tonic-gate */
18470Sstevel@tonic-gate addr = pcmemaddr;
18480Sstevel@tonic-gate
18490Sstevel@tonic-gate mnoderanges = (mnoderange_t *)addr;
18500Sstevel@tonic-gate addr += (mnoderangecnt * sizeof (mnoderange_t));
18510Sstevel@tonic-gate
18520Sstevel@tonic-gate mnode_range_setup(mnoderanges);
18530Sstevel@tonic-gate
18540Sstevel@tonic-gate if (physmax4g)
18550Sstevel@tonic-gate mtype4g = pfn_2_mtype(0xfffff);
18560Sstevel@tonic-gate
18570Sstevel@tonic-gate for (k = 0; k < NPC_MUTEX; k++) {
18580Sstevel@tonic-gate fpc_mutex[k] = (kmutex_t *)addr;
18590Sstevel@tonic-gate addr += (max_mem_nodes * sizeof (kmutex_t));
18600Sstevel@tonic-gate }
18610Sstevel@tonic-gate for (k = 0; k < NPC_MUTEX; k++) {
18620Sstevel@tonic-gate cpc_mutex[k] = (kmutex_t *)addr;
18630Sstevel@tonic-gate addr += (max_mem_nodes * sizeof (kmutex_t));
18640Sstevel@tonic-gate }
186512293SJames.McPherson@Sun.COM page_freelists = (page_t ****)addr;
18660Sstevel@tonic-gate addr += (mnoderangecnt * sizeof (page_t ***));
18670Sstevel@tonic-gate
18680Sstevel@tonic-gate page_cachelists = (page_t ***)addr;
18690Sstevel@tonic-gate addr += (mnoderangecnt * sizeof (page_t **));
18700Sstevel@tonic-gate
18710Sstevel@tonic-gate for (i = 0; i < mnoderangecnt; i++) {
187212293SJames.McPherson@Sun.COM page_freelists[i] = (page_t ***)addr;
18730Sstevel@tonic-gate addr += (mmu_page_sizes * sizeof (page_t **));
18740Sstevel@tonic-gate
18750Sstevel@tonic-gate for (j = 0; j < mmu_page_sizes; j++) {
18760Sstevel@tonic-gate colors = page_get_pagecolors(j);
187712293SJames.McPherson@Sun.COM page_freelists[i][j] = (page_t **)addr;
18780Sstevel@tonic-gate addr += (colors * sizeof (page_t *));
18790Sstevel@tonic-gate }
18800Sstevel@tonic-gate page_cachelists[i] = (page_t **)addr;
18810Sstevel@tonic-gate addr += (page_colors * sizeof (page_t *));
18820Sstevel@tonic-gate }
18830Sstevel@tonic-gate }
18840Sstevel@tonic-gate
18855084Sjohnlev #if defined(__xpv)
18865084Sjohnlev /*
18875084Sjohnlev * Give back 10% of the io_pool pages to the free list.
18885084Sjohnlev * Don't shrink the pool below some absolute minimum.
18895084Sjohnlev */
18905084Sjohnlev static void
page_io_pool_shrink()18915084Sjohnlev page_io_pool_shrink()
18925084Sjohnlev {
18935084Sjohnlev int retcnt;
18945084Sjohnlev page_t *pp, *pp_first, *pp_last, **curpool;
18955084Sjohnlev mfn_t mfn;
18965084Sjohnlev int bothpools = 0;
18975084Sjohnlev
18985084Sjohnlev mutex_enter(&io_pool_lock);
18995084Sjohnlev io_pool_shrink_attempts++; /* should be a kstat? */
19005084Sjohnlev retcnt = io_pool_cnt / 10;
19015084Sjohnlev if (io_pool_cnt - retcnt < io_pool_cnt_min)
19025084Sjohnlev retcnt = io_pool_cnt - io_pool_cnt_min;
19035084Sjohnlev if (retcnt <= 0)
19045084Sjohnlev goto done;
19055084Sjohnlev io_pool_shrinks++; /* should be a kstat? */
19065084Sjohnlev curpool = &io_pool_4g;
19075084Sjohnlev domore:
19085084Sjohnlev /*
19095084Sjohnlev * Loop through taking pages from the end of the list
19105084Sjohnlev * (highest mfns) till amount to return reached.
19115084Sjohnlev */
19125084Sjohnlev for (pp = *curpool; pp && retcnt > 0; ) {
19135084Sjohnlev pp_first = pp_last = pp->p_prev;
19145084Sjohnlev if (pp_first == *curpool)
19155084Sjohnlev break;
19165084Sjohnlev retcnt--;
19175084Sjohnlev io_pool_cnt--;
19185084Sjohnlev page_io_pool_sub(curpool, pp_first, pp_last);
19195084Sjohnlev if ((mfn = pfn_to_mfn(pp->p_pagenum)) < start_mfn)
19205084Sjohnlev start_mfn = mfn;
19215084Sjohnlev page_free(pp_first, 1);
19225084Sjohnlev pp = *curpool;
19235084Sjohnlev }
19245084Sjohnlev if (retcnt != 0 && !bothpools) {
19255084Sjohnlev /*
19265084Sjohnlev * If not enough found in less constrained pool try the
19275084Sjohnlev * more constrained one.
19285084Sjohnlev */
19295084Sjohnlev curpool = &io_pool_16m;
19305084Sjohnlev bothpools = 1;
19315084Sjohnlev goto domore;
19325084Sjohnlev }
19335084Sjohnlev done:
19345084Sjohnlev mutex_exit(&io_pool_lock);
19355084Sjohnlev }
19365084Sjohnlev
19375084Sjohnlev #endif /* __xpv */
19385084Sjohnlev
19395084Sjohnlev uint_t
page_create_update_flags_x86(uint_t flags)19405084Sjohnlev page_create_update_flags_x86(uint_t flags)
19415084Sjohnlev {
19425084Sjohnlev #if defined(__xpv)
19435084Sjohnlev /*
19445084Sjohnlev * Check this is an urgent allocation and free pages are depleted.
19455084Sjohnlev */
19465084Sjohnlev if (!(flags & PG_WAIT) && freemem < desfree)
19475084Sjohnlev page_io_pool_shrink();
19485084Sjohnlev #else /* !__xpv */
19495084Sjohnlev /*
19505084Sjohnlev * page_create_get_something may call this because 4g memory may be
19515084Sjohnlev * depleted. Set flags to allow for relocation of base page below
19525084Sjohnlev * 4g if necessary.
19535084Sjohnlev */
19545084Sjohnlev if (physmax4g)
19555084Sjohnlev flags |= (PGI_PGCPSZC0 | PGI_PGCPHIPRI);
19565084Sjohnlev #endif /* __xpv */
19575084Sjohnlev return (flags);
19585084Sjohnlev }
19595084Sjohnlev
19600Sstevel@tonic-gate /*ARGSUSED*/
19610Sstevel@tonic-gate int
bp_color(struct buf * bp)19620Sstevel@tonic-gate bp_color(struct buf *bp)
19630Sstevel@tonic-gate {
19640Sstevel@tonic-gate return (0);
19650Sstevel@tonic-gate }
19660Sstevel@tonic-gate
19675084Sjohnlev #if defined(__xpv)
19685084Sjohnlev
19695084Sjohnlev /*
19705084Sjohnlev * Take pages out of an io_pool
19715084Sjohnlev */
19725084Sjohnlev static void
page_io_pool_sub(page_t ** poolp,page_t * pp_first,page_t * pp_last)19735084Sjohnlev page_io_pool_sub(page_t **poolp, page_t *pp_first, page_t *pp_last)
19745084Sjohnlev {
19755084Sjohnlev if (*poolp == pp_first) {
19765084Sjohnlev *poolp = pp_last->p_next;
19775084Sjohnlev if (*poolp == pp_first)
19785084Sjohnlev *poolp = NULL;
19795084Sjohnlev }
19805084Sjohnlev pp_first->p_prev->p_next = pp_last->p_next;
19815084Sjohnlev pp_last->p_next->p_prev = pp_first->p_prev;
19825084Sjohnlev pp_first->p_prev = pp_last;
19835084Sjohnlev pp_last->p_next = pp_first;
19845084Sjohnlev }
19855084Sjohnlev
19865084Sjohnlev /*
19875084Sjohnlev * Put a page on the io_pool list. The list is ordered by increasing MFN.
19885084Sjohnlev */
19895084Sjohnlev static void
page_io_pool_add(page_t ** poolp,page_t * pp)19905084Sjohnlev page_io_pool_add(page_t **poolp, page_t *pp)
19915084Sjohnlev {
19925084Sjohnlev page_t *look;
19935084Sjohnlev mfn_t mfn = mfn_list[pp->p_pagenum];
19945084Sjohnlev
19955084Sjohnlev if (*poolp == NULL) {
19965084Sjohnlev *poolp = pp;
19975084Sjohnlev pp->p_next = pp;
19985084Sjohnlev pp->p_prev = pp;
19995084Sjohnlev return;
20005084Sjohnlev }
20015084Sjohnlev
20025084Sjohnlev /*
20035084Sjohnlev * Since we try to take pages from the high end of the pool
20045084Sjohnlev * chances are good that the pages to be put on the list will
20055084Sjohnlev * go at or near the end of the list. so start at the end and
20065084Sjohnlev * work backwards.
20075084Sjohnlev */
20085084Sjohnlev look = (*poolp)->p_prev;
20095084Sjohnlev while (mfn < mfn_list[look->p_pagenum]) {
20105084Sjohnlev look = look->p_prev;
20115084Sjohnlev if (look == (*poolp)->p_prev)
20125084Sjohnlev break; /* backed all the way to front of list */
20135084Sjohnlev }
20145084Sjohnlev
20155084Sjohnlev /* insert after look */
20165084Sjohnlev pp->p_prev = look;
20175084Sjohnlev pp->p_next = look->p_next;
20185084Sjohnlev pp->p_next->p_prev = pp;
20195084Sjohnlev look->p_next = pp;
20205084Sjohnlev if (mfn < mfn_list[(*poolp)->p_pagenum]) {
20215084Sjohnlev /*
20225084Sjohnlev * we inserted a new first list element
20235084Sjohnlev * adjust pool pointer to newly inserted element
20245084Sjohnlev */
20255084Sjohnlev *poolp = pp;
20265084Sjohnlev }
20275084Sjohnlev }
20285084Sjohnlev
20295084Sjohnlev /*
20305084Sjohnlev * Add a page to the io_pool. Setting the force flag will force the page
20315084Sjohnlev * into the io_pool no matter what.
20325084Sjohnlev */
20335084Sjohnlev static void
add_page_to_pool(page_t * pp,int force)20345084Sjohnlev add_page_to_pool(page_t *pp, int force)
20355084Sjohnlev {
20365084Sjohnlev page_t *highest;
20375084Sjohnlev page_t *freep = NULL;
20385084Sjohnlev
20395084Sjohnlev mutex_enter(&io_pool_lock);
20405084Sjohnlev /*
20415084Sjohnlev * Always keep the scarce low memory pages
20425084Sjohnlev */
20435084Sjohnlev if (mfn_list[pp->p_pagenum] < PFN_16MEG) {
20445084Sjohnlev ++io_pool_cnt;
20455084Sjohnlev page_io_pool_add(&io_pool_16m, pp);
20465084Sjohnlev goto done;
20475084Sjohnlev }
20486159Ssmaybe if (io_pool_cnt < io_pool_cnt_max || force || io_pool_4g == NULL) {
20495084Sjohnlev ++io_pool_cnt;
20505084Sjohnlev page_io_pool_add(&io_pool_4g, pp);
20515084Sjohnlev } else {
20525084Sjohnlev highest = io_pool_4g->p_prev;
20535084Sjohnlev if (mfn_list[pp->p_pagenum] < mfn_list[highest->p_pagenum]) {
20545084Sjohnlev page_io_pool_sub(&io_pool_4g, highest, highest);
20555084Sjohnlev page_io_pool_add(&io_pool_4g, pp);
20565084Sjohnlev freep = highest;
20575084Sjohnlev } else {
20585084Sjohnlev freep = pp;
20595084Sjohnlev }
20605084Sjohnlev }
20615084Sjohnlev done:
20625084Sjohnlev mutex_exit(&io_pool_lock);
20635084Sjohnlev if (freep)
20645084Sjohnlev page_free(freep, 1);
20655084Sjohnlev }
20665084Sjohnlev
20675084Sjohnlev
20685084Sjohnlev int contig_pfn_cnt; /* no of pfns in the contig pfn list */
20695084Sjohnlev int contig_pfn_max; /* capacity of the contig pfn list */
20705084Sjohnlev int next_alloc_pfn; /* next position in list to start a contig search */
20715084Sjohnlev int contig_pfnlist_updates; /* pfn list update count */
20725084Sjohnlev int contig_pfnlist_builds; /* how many times have we (re)built list */
20735084Sjohnlev int contig_pfnlist_buildfailed; /* how many times has list build failed */
20745084Sjohnlev int create_contig_pending; /* nonzero means taskq creating contig list */
20755084Sjohnlev pfn_t *contig_pfn_list = NULL; /* list of contig pfns in ascending mfn order */
20765084Sjohnlev
20775084Sjohnlev /*
20785084Sjohnlev * Function to use in sorting a list of pfns by their underlying mfns.
20795084Sjohnlev */
20805084Sjohnlev static int
mfn_compare(const void * pfnp1,const void * pfnp2)20815084Sjohnlev mfn_compare(const void *pfnp1, const void *pfnp2)
20825084Sjohnlev {
20835084Sjohnlev mfn_t mfn1 = mfn_list[*(pfn_t *)pfnp1];
20845084Sjohnlev mfn_t mfn2 = mfn_list[*(pfn_t *)pfnp2];
20855084Sjohnlev
20865084Sjohnlev if (mfn1 > mfn2)
20875084Sjohnlev return (1);
20885084Sjohnlev if (mfn1 < mfn2)
20895084Sjohnlev return (-1);
20905084Sjohnlev return (0);
20915084Sjohnlev }
20925084Sjohnlev
20935084Sjohnlev /*
20945084Sjohnlev * Compact the contig_pfn_list by tossing all the non-contiguous
20955084Sjohnlev * elements from the list.
20965084Sjohnlev */
20975084Sjohnlev static void
compact_contig_pfn_list(void)20985084Sjohnlev compact_contig_pfn_list(void)
20995084Sjohnlev {
21005084Sjohnlev pfn_t pfn, lapfn, prev_lapfn;
21015084Sjohnlev mfn_t mfn;
21025084Sjohnlev int i, newcnt = 0;
21035084Sjohnlev
21045084Sjohnlev prev_lapfn = 0;
21055084Sjohnlev for (i = 0; i < contig_pfn_cnt - 1; i++) {
21065084Sjohnlev pfn = contig_pfn_list[i];
21075084Sjohnlev lapfn = contig_pfn_list[i + 1];
21085084Sjohnlev mfn = mfn_list[pfn];
21095084Sjohnlev /*
21105084Sjohnlev * See if next pfn is for a contig mfn
21115084Sjohnlev */
21125084Sjohnlev if (mfn_list[lapfn] != mfn + 1)
21135084Sjohnlev continue;
21145084Sjohnlev /*
21155084Sjohnlev * pfn and lookahead are both put in list
21165084Sjohnlev * unless pfn is the previous lookahead.
21175084Sjohnlev */
21185084Sjohnlev if (pfn != prev_lapfn)
21195084Sjohnlev contig_pfn_list[newcnt++] = pfn;
21205084Sjohnlev contig_pfn_list[newcnt++] = lapfn;
21215084Sjohnlev prev_lapfn = lapfn;
21225084Sjohnlev }
21235084Sjohnlev for (i = newcnt; i < contig_pfn_cnt; i++)
21245084Sjohnlev contig_pfn_list[i] = 0;
21255084Sjohnlev contig_pfn_cnt = newcnt;
21265084Sjohnlev }
21275084Sjohnlev
21285084Sjohnlev /*ARGSUSED*/
21295084Sjohnlev static void
call_create_contiglist(void * arg)21305084Sjohnlev call_create_contiglist(void *arg)
21315084Sjohnlev {
21325084Sjohnlev (void) create_contig_pfnlist(PG_WAIT);
21335084Sjohnlev }
21345084Sjohnlev
21355084Sjohnlev /*
21365084Sjohnlev * Create list of freelist pfns that have underlying
21375084Sjohnlev * contiguous mfns. The list is kept in ascending mfn order.
21385084Sjohnlev * returns 1 if list created else 0.
21395084Sjohnlev */
21405084Sjohnlev static int
create_contig_pfnlist(uint_t flags)21415084Sjohnlev create_contig_pfnlist(uint_t flags)
21425084Sjohnlev {
21435084Sjohnlev pfn_t pfn;
21445084Sjohnlev page_t *pp;
21455529Ssmaybe int ret = 1;
21465529Ssmaybe
21475529Ssmaybe mutex_enter(&contig_list_lock);
21485084Sjohnlev if (contig_pfn_list != NULL)
21495529Ssmaybe goto out;
21505084Sjohnlev contig_pfn_max = freemem + (freemem / 10);
21515084Sjohnlev contig_pfn_list = kmem_zalloc(contig_pfn_max * sizeof (pfn_t),
21525084Sjohnlev (flags & PG_WAIT) ? KM_SLEEP : KM_NOSLEEP);
21535084Sjohnlev if (contig_pfn_list == NULL) {
21545084Sjohnlev /*
21555084Sjohnlev * If we could not create the contig list (because
21565084Sjohnlev * we could not sleep for memory). Dispatch a taskq that can
21575084Sjohnlev * sleep to get the memory.
21585084Sjohnlev */
21595084Sjohnlev if (!create_contig_pending) {
21605084Sjohnlev if (taskq_dispatch(system_taskq, call_create_contiglist,
21615084Sjohnlev NULL, TQ_NOSLEEP) != NULL)
21625084Sjohnlev create_contig_pending = 1;
21635084Sjohnlev }
21645084Sjohnlev contig_pfnlist_buildfailed++; /* count list build failures */
21655529Ssmaybe ret = 0;
21665529Ssmaybe goto out;
21675084Sjohnlev }
21685529Ssmaybe create_contig_pending = 0;
21695084Sjohnlev ASSERT(contig_pfn_cnt == 0);
21705084Sjohnlev for (pfn = 0; pfn < mfn_count; pfn++) {
21715084Sjohnlev pp = page_numtopp_nolock(pfn);
21725084Sjohnlev if (pp == NULL || !PP_ISFREE(pp))
21735084Sjohnlev continue;
21745084Sjohnlev contig_pfn_list[contig_pfn_cnt] = pfn;
21755084Sjohnlev if (++contig_pfn_cnt == contig_pfn_max)
21765084Sjohnlev break;
21775084Sjohnlev }
21789010SStuart.Maybee@Sun.COM /*
21799010SStuart.Maybee@Sun.COM * Sanity check the new list.
21809010SStuart.Maybee@Sun.COM */
21819010SStuart.Maybee@Sun.COM if (contig_pfn_cnt < 2) { /* no contig pfns */
21829010SStuart.Maybee@Sun.COM contig_pfn_cnt = 0;
21839010SStuart.Maybee@Sun.COM contig_pfnlist_buildfailed++;
21849010SStuart.Maybee@Sun.COM kmem_free(contig_pfn_list, contig_pfn_max * sizeof (pfn_t));
21859010SStuart.Maybee@Sun.COM contig_pfn_list = NULL;
21869010SStuart.Maybee@Sun.COM contig_pfn_max = 0;
21879010SStuart.Maybee@Sun.COM ret = 0;
21889010SStuart.Maybee@Sun.COM goto out;
21899010SStuart.Maybee@Sun.COM }
21905084Sjohnlev qsort(contig_pfn_list, contig_pfn_cnt, sizeof (pfn_t), mfn_compare);
21915084Sjohnlev compact_contig_pfn_list();
21925084Sjohnlev /*
21935084Sjohnlev * Make sure next search of the newly created contiguous pfn
21945084Sjohnlev * list starts at the beginning of the list.
21955084Sjohnlev */
21965084Sjohnlev next_alloc_pfn = 0;
21975084Sjohnlev contig_pfnlist_builds++; /* count list builds */
21985529Ssmaybe out:
21995529Ssmaybe mutex_exit(&contig_list_lock);
22005529Ssmaybe return (ret);
22015084Sjohnlev }
22025084Sjohnlev
22035084Sjohnlev
22045084Sjohnlev /*
22055084Sjohnlev * Toss the current contig pfnlist. Someone is about to do a massive
22065084Sjohnlev * update to pfn<->mfn mappings. So we have them destroy the list and lock
22075084Sjohnlev * it till they are done with their update.
22085084Sjohnlev */
22095084Sjohnlev void
clear_and_lock_contig_pfnlist()22105084Sjohnlev clear_and_lock_contig_pfnlist()
22115084Sjohnlev {
22125084Sjohnlev pfn_t *listp = NULL;
22135084Sjohnlev size_t listsize;
22145084Sjohnlev
22155529Ssmaybe mutex_enter(&contig_list_lock);
22165084Sjohnlev if (contig_pfn_list != NULL) {
22175084Sjohnlev listp = contig_pfn_list;
22185084Sjohnlev listsize = contig_pfn_max * sizeof (pfn_t);
22195084Sjohnlev contig_pfn_list = NULL;
22205084Sjohnlev contig_pfn_max = contig_pfn_cnt = 0;
22215084Sjohnlev }
22225084Sjohnlev if (listp != NULL)
22235084Sjohnlev kmem_free(listp, listsize);
22245084Sjohnlev }
22255084Sjohnlev
22265084Sjohnlev /*
22275084Sjohnlev * Unlock the contig_pfn_list. The next attempted use of it will cause
22285084Sjohnlev * it to be re-created.
22295084Sjohnlev */
22305084Sjohnlev void
unlock_contig_pfnlist()22315084Sjohnlev unlock_contig_pfnlist()
22325084Sjohnlev {
22335529Ssmaybe mutex_exit(&contig_list_lock);
22345084Sjohnlev }
22355084Sjohnlev
22365084Sjohnlev /*
22375084Sjohnlev * Update the contiguous pfn list in response to a pfn <-> mfn reassignment
22385084Sjohnlev */
22395084Sjohnlev void
update_contig_pfnlist(pfn_t pfn,mfn_t oldmfn,mfn_t newmfn)22405084Sjohnlev update_contig_pfnlist(pfn_t pfn, mfn_t oldmfn, mfn_t newmfn)
22415084Sjohnlev {
22425084Sjohnlev int probe_hi, probe_lo, probe_pos, insert_after, insert_point;
22435084Sjohnlev pfn_t probe_pfn;
22445084Sjohnlev mfn_t probe_mfn;
22455529Ssmaybe int drop_lock = 0;
22465529Ssmaybe
22475529Ssmaybe if (mutex_owner(&contig_list_lock) != curthread) {
22485529Ssmaybe drop_lock = 1;
22495529Ssmaybe mutex_enter(&contig_list_lock);
22505529Ssmaybe }
22515084Sjohnlev if (contig_pfn_list == NULL)
22525529Ssmaybe goto done;
22535084Sjohnlev contig_pfnlist_updates++;
22545084Sjohnlev /*
22555084Sjohnlev * Find the pfn in the current list. Use a binary chop to locate it.
22565084Sjohnlev */
22575084Sjohnlev probe_hi = contig_pfn_cnt - 1;
22585084Sjohnlev probe_lo = 0;
22595084Sjohnlev probe_pos = (probe_hi + probe_lo) / 2;
22605084Sjohnlev while ((probe_pfn = contig_pfn_list[probe_pos]) != pfn) {
22615084Sjohnlev if (probe_pos == probe_lo) { /* pfn not in list */
22625084Sjohnlev probe_pos = -1;
22635084Sjohnlev break;
22645084Sjohnlev }
22655084Sjohnlev if (pfn_to_mfn(probe_pfn) <= oldmfn)
22665084Sjohnlev probe_lo = probe_pos;
22675084Sjohnlev else
22685084Sjohnlev probe_hi = probe_pos;
22695084Sjohnlev probe_pos = (probe_hi + probe_lo) / 2;
22705084Sjohnlev }
22719010SStuart.Maybee@Sun.COM if (probe_pos >= 0) {
22729010SStuart.Maybee@Sun.COM /*
22739010SStuart.Maybee@Sun.COM * Remove pfn from list and ensure next alloc
22749010SStuart.Maybee@Sun.COM * position stays in bounds.
22759010SStuart.Maybee@Sun.COM */
22769010SStuart.Maybee@Sun.COM if (--contig_pfn_cnt <= next_alloc_pfn)
22779010SStuart.Maybee@Sun.COM next_alloc_pfn = 0;
227810175SStuart.Maybee@Sun.COM if (contig_pfn_cnt < 2) { /* no contig pfns */
227910175SStuart.Maybee@Sun.COM contig_pfn_cnt = 0;
228010175SStuart.Maybee@Sun.COM kmem_free(contig_pfn_list,
228110175SStuart.Maybee@Sun.COM contig_pfn_max * sizeof (pfn_t));
228210175SStuart.Maybee@Sun.COM contig_pfn_list = NULL;
228310175SStuart.Maybee@Sun.COM contig_pfn_max = 0;
228410175SStuart.Maybee@Sun.COM goto done;
228510175SStuart.Maybee@Sun.COM }
22865084Sjohnlev ovbcopy(&contig_pfn_list[probe_pos + 1],
22875084Sjohnlev &contig_pfn_list[probe_pos],
22885084Sjohnlev (contig_pfn_cnt - probe_pos) * sizeof (pfn_t));
22895084Sjohnlev }
22905084Sjohnlev if (newmfn == MFN_INVALID)
22915084Sjohnlev goto done;
22925084Sjohnlev /*
22935084Sjohnlev * Check if new mfn has adjacent mfns in the list
22945084Sjohnlev */
22955084Sjohnlev probe_hi = contig_pfn_cnt - 1;
22965084Sjohnlev probe_lo = 0;
22975084Sjohnlev insert_after = -2;
22985084Sjohnlev do {
22995084Sjohnlev probe_pos = (probe_hi + probe_lo) / 2;
23005084Sjohnlev probe_mfn = pfn_to_mfn(contig_pfn_list[probe_pos]);
23015084Sjohnlev if (newmfn == probe_mfn + 1)
23025084Sjohnlev insert_after = probe_pos;
23035084Sjohnlev else if (newmfn == probe_mfn - 1)
23045084Sjohnlev insert_after = probe_pos - 1;
23055084Sjohnlev if (probe_pos == probe_lo)
23065084Sjohnlev break;
23075084Sjohnlev if (probe_mfn <= newmfn)
23085084Sjohnlev probe_lo = probe_pos;
23095084Sjohnlev else
23105084Sjohnlev probe_hi = probe_pos;
23115084Sjohnlev } while (insert_after == -2);
23125084Sjohnlev /*
23135084Sjohnlev * If there is space in the list and there are adjacent mfns
23145084Sjohnlev * insert the pfn in to its proper place in the list.
23155084Sjohnlev */
23165084Sjohnlev if (insert_after != -2 && contig_pfn_cnt + 1 <= contig_pfn_max) {
23175084Sjohnlev insert_point = insert_after + 1;
23185084Sjohnlev ovbcopy(&contig_pfn_list[insert_point],
23195084Sjohnlev &contig_pfn_list[insert_point + 1],
23205084Sjohnlev (contig_pfn_cnt - insert_point) * sizeof (pfn_t));
23215084Sjohnlev contig_pfn_list[insert_point] = pfn;
23225084Sjohnlev contig_pfn_cnt++;
23235084Sjohnlev }
23245084Sjohnlev done:
23255529Ssmaybe if (drop_lock)
23265529Ssmaybe mutex_exit(&contig_list_lock);
23275084Sjohnlev }
23285084Sjohnlev
23295084Sjohnlev /*
23305084Sjohnlev * Called to (re-)populate the io_pool from the free page lists.
23315084Sjohnlev */
23325084Sjohnlev long
populate_io_pool(void)23335084Sjohnlev populate_io_pool(void)
23345084Sjohnlev {
23355084Sjohnlev pfn_t pfn;
23365084Sjohnlev mfn_t mfn, max_mfn;
23375084Sjohnlev page_t *pp;
23385084Sjohnlev
23395084Sjohnlev /*
23405084Sjohnlev * Figure out the bounds of the pool on first invocation.
23415084Sjohnlev * We use a percentage of memory for the io pool size.
23425084Sjohnlev * we allow that to shrink, but not to less than a fixed minimum
23435084Sjohnlev */
23445084Sjohnlev if (io_pool_cnt_max == 0) {
23455084Sjohnlev io_pool_cnt_max = physmem / (100 / io_pool_physmem_pct);
23465084Sjohnlev io_pool_cnt_lowater = io_pool_cnt_max;
23475084Sjohnlev /*
23485084Sjohnlev * This is the first time in populate_io_pool, grab a va to use
23495084Sjohnlev * when we need to allocate pages.
23505084Sjohnlev */
23515084Sjohnlev io_pool_kva = vmem_alloc(heap_arena, PAGESIZE, VM_SLEEP);
23525084Sjohnlev }
23535084Sjohnlev /*
23545084Sjohnlev * If we are out of pages in the pool, then grow the size of the pool
23555084Sjohnlev */
23566159Ssmaybe if (io_pool_cnt == 0) {
23576159Ssmaybe /*
23586159Ssmaybe * Grow the max size of the io pool by 5%, but never more than
23596159Ssmaybe * 25% of physical memory.
23606159Ssmaybe */
23616159Ssmaybe if (io_pool_cnt_max < physmem / 4)
23626159Ssmaybe io_pool_cnt_max += io_pool_cnt_max / 20;
23636159Ssmaybe }
23645084Sjohnlev io_pool_grows++; /* should be a kstat? */
23655084Sjohnlev
23665084Sjohnlev /*
23675084Sjohnlev * Get highest mfn on this platform, but limit to the 32 bit DMA max.
23685084Sjohnlev */
23695084Sjohnlev (void) mfn_to_pfn(start_mfn);
23705084Sjohnlev max_mfn = MIN(cached_max_mfn, PFN_4GIG);
23715084Sjohnlev for (mfn = start_mfn; mfn < max_mfn; start_mfn = ++mfn) {
23725084Sjohnlev pfn = mfn_to_pfn(mfn);
23735084Sjohnlev if (pfn & PFN_IS_FOREIGN_MFN)
23745084Sjohnlev continue;
23755084Sjohnlev /*
23765084Sjohnlev * try to allocate it from free pages
23775084Sjohnlev */
23785084Sjohnlev pp = page_numtopp_alloc(pfn);
23795084Sjohnlev if (pp == NULL)
23805084Sjohnlev continue;
23815084Sjohnlev PP_CLRFREE(pp);
23825084Sjohnlev add_page_to_pool(pp, 1);
23835084Sjohnlev if (io_pool_cnt >= io_pool_cnt_max)
23845084Sjohnlev break;
23855084Sjohnlev }
23865084Sjohnlev
23875084Sjohnlev return (io_pool_cnt);
23885084Sjohnlev }
23895084Sjohnlev
23905084Sjohnlev /*
23915084Sjohnlev * Destroy a page that was being used for DMA I/O. It may or
23925084Sjohnlev * may not actually go back to the io_pool.
23935084Sjohnlev */
23945084Sjohnlev void
page_destroy_io(page_t * pp)23955084Sjohnlev page_destroy_io(page_t *pp)
23965084Sjohnlev {
23975084Sjohnlev mfn_t mfn = mfn_list[pp->p_pagenum];
23985084Sjohnlev
23995084Sjohnlev /*
24005084Sjohnlev * When the page was alloc'd a reservation was made, release it now
24015084Sjohnlev */
24025084Sjohnlev page_unresv(1);
24035084Sjohnlev /*
24045084Sjohnlev * Unload translations, if any, then hash out the
24055084Sjohnlev * page to erase its identity.
24065084Sjohnlev */
24075084Sjohnlev (void) hat_pageunload(pp, HAT_FORCE_PGUNLOAD);
24085084Sjohnlev page_hashout(pp, NULL);
24095084Sjohnlev
24105084Sjohnlev /*
24115084Sjohnlev * If the page came from the free lists, just put it back to them.
24125084Sjohnlev * DomU pages always go on the free lists as well.
24135084Sjohnlev */
24145084Sjohnlev if (!DOMAIN_IS_INITDOMAIN(xen_info) || mfn >= PFN_4GIG) {
24155084Sjohnlev page_free(pp, 1);
24165084Sjohnlev return;
24175084Sjohnlev }
24185084Sjohnlev
24195084Sjohnlev add_page_to_pool(pp, 0);
24205084Sjohnlev }
24215084Sjohnlev
24225084Sjohnlev
24235084Sjohnlev long contig_searches; /* count of times contig pages requested */
24245084Sjohnlev long contig_search_restarts; /* count of contig ranges tried */
24255084Sjohnlev long contig_search_failed; /* count of contig alloc failures */
24265084Sjohnlev
24275084Sjohnlev /*
242810175SStuart.Maybee@Sun.COM * Free partial page list
242910175SStuart.Maybee@Sun.COM */
243010175SStuart.Maybee@Sun.COM static void
free_partial_list(page_t ** pplist)243110175SStuart.Maybee@Sun.COM free_partial_list(page_t **pplist)
243210175SStuart.Maybee@Sun.COM {
243310175SStuart.Maybee@Sun.COM page_t *pp;
243410175SStuart.Maybee@Sun.COM
243510175SStuart.Maybee@Sun.COM while (*pplist != NULL) {
243610175SStuart.Maybee@Sun.COM pp = *pplist;
243710175SStuart.Maybee@Sun.COM page_io_pool_sub(pplist, pp, pp);
243810175SStuart.Maybee@Sun.COM page_free(pp, 1);
243910175SStuart.Maybee@Sun.COM }
244010175SStuart.Maybee@Sun.COM }
244110175SStuart.Maybee@Sun.COM
244210175SStuart.Maybee@Sun.COM /*
24435084Sjohnlev * Look thru the contiguous pfns that are not part of the io_pool for
24445084Sjohnlev * contiguous free pages. Return a list of the found pages or NULL.
24455084Sjohnlev */
24465084Sjohnlev page_t *
find_contig_free(uint_t npages,uint_t flags,uint64_t pfnseg,pgcnt_t pfnalign)244710175SStuart.Maybee@Sun.COM find_contig_free(uint_t npages, uint_t flags, uint64_t pfnseg,
244810175SStuart.Maybee@Sun.COM pgcnt_t pfnalign)
24495084Sjohnlev {
24505084Sjohnlev page_t *pp, *plist = NULL;
24516282Ssmaybe mfn_t mfn, prev_mfn, start_mfn;
24525084Sjohnlev pfn_t pfn;
24535084Sjohnlev int pages_needed, pages_requested;
24545084Sjohnlev int search_start;
24555084Sjohnlev
24565084Sjohnlev /*
24575084Sjohnlev * create the contig pfn list if not already done
24585084Sjohnlev */
24595529Ssmaybe retry:
24605529Ssmaybe mutex_enter(&contig_list_lock);
24615084Sjohnlev if (contig_pfn_list == NULL) {
24625529Ssmaybe mutex_exit(&contig_list_lock);
24635529Ssmaybe if (!create_contig_pfnlist(flags)) {
24645084Sjohnlev return (NULL);
24655084Sjohnlev }
24665529Ssmaybe goto retry;
24675084Sjohnlev }
24685084Sjohnlev contig_searches++;
24695084Sjohnlev /*
24705084Sjohnlev * Search contiguous pfn list for physically contiguous pages not in
24715084Sjohnlev * the io_pool. Start the search where the last search left off.
24725084Sjohnlev */
24735843Ssmaybe pages_requested = pages_needed = npages;
24745084Sjohnlev search_start = next_alloc_pfn;
24756282Ssmaybe start_mfn = prev_mfn = 0;
24765084Sjohnlev while (pages_needed) {
24775084Sjohnlev pfn = contig_pfn_list[next_alloc_pfn];
24785084Sjohnlev mfn = pfn_to_mfn(pfn);
24796282Ssmaybe /*
24806282Ssmaybe * Check if mfn is first one or contig to previous one and
24816282Ssmaybe * if page corresponding to mfn is free and that mfn
24826282Ssmaybe * range is not crossing a segment boundary.
24836282Ssmaybe */
24845084Sjohnlev if ((prev_mfn == 0 || mfn == prev_mfn + 1) &&
24856282Ssmaybe (pp = page_numtopp_alloc(pfn)) != NULL &&
24866282Ssmaybe !((mfn & pfnseg) < (start_mfn & pfnseg))) {
24875084Sjohnlev PP_CLRFREE(pp);
24885084Sjohnlev page_io_pool_add(&plist, pp);
24895084Sjohnlev pages_needed--;
249010175SStuart.Maybee@Sun.COM if (prev_mfn == 0) {
249110175SStuart.Maybee@Sun.COM if (pfnalign &&
249210175SStuart.Maybee@Sun.COM mfn != P2ROUNDUP(mfn, pfnalign)) {
249310175SStuart.Maybee@Sun.COM /*
249410175SStuart.Maybee@Sun.COM * not properly aligned
249510175SStuart.Maybee@Sun.COM */
249610175SStuart.Maybee@Sun.COM contig_search_restarts++;
249710175SStuart.Maybee@Sun.COM free_partial_list(&plist);
249810175SStuart.Maybee@Sun.COM pages_needed = pages_requested;
249910175SStuart.Maybee@Sun.COM start_mfn = prev_mfn = 0;
250010175SStuart.Maybee@Sun.COM goto skip;
250110175SStuart.Maybee@Sun.COM }
25026282Ssmaybe start_mfn = mfn;
250310175SStuart.Maybee@Sun.COM }
25045084Sjohnlev prev_mfn = mfn;
25055084Sjohnlev } else {
25065084Sjohnlev contig_search_restarts++;
250710175SStuart.Maybee@Sun.COM free_partial_list(&plist);
25085084Sjohnlev pages_needed = pages_requested;
25096282Ssmaybe start_mfn = prev_mfn = 0;
25105084Sjohnlev }
251110175SStuart.Maybee@Sun.COM skip:
25125084Sjohnlev if (++next_alloc_pfn == contig_pfn_cnt)
25135084Sjohnlev next_alloc_pfn = 0;
25145084Sjohnlev if (next_alloc_pfn == search_start)
25155084Sjohnlev break; /* all pfns searched */
25165084Sjohnlev }
25175529Ssmaybe mutex_exit(&contig_list_lock);
25185084Sjohnlev if (pages_needed) {
25195084Sjohnlev contig_search_failed++;
25205084Sjohnlev /*
25215084Sjohnlev * Failed to find enough contig pages.
25225084Sjohnlev * free partial page list
25235084Sjohnlev */
252410175SStuart.Maybee@Sun.COM free_partial_list(&plist);
25255084Sjohnlev }
25265084Sjohnlev return (plist);
25275084Sjohnlev }
25285084Sjohnlev
25295084Sjohnlev /*
25305843Ssmaybe * Search the reserved io pool pages for a page range with the
25315843Ssmaybe * desired characteristics.
25325084Sjohnlev */
25335084Sjohnlev page_t *
page_io_pool_alloc(ddi_dma_attr_t * mattr,int contig,pgcnt_t minctg)25345843Ssmaybe page_io_pool_alloc(ddi_dma_attr_t *mattr, int contig, pgcnt_t minctg)
25355084Sjohnlev {
25365843Ssmaybe page_t *pp_first, *pp_last;
25375843Ssmaybe page_t *pp, **poolp;
25385843Ssmaybe pgcnt_t nwanted, pfnalign;
25395084Sjohnlev uint64_t pfnseg;
25405843Ssmaybe mfn_t mfn, tmfn, hi_mfn, lo_mfn;
25415843Ssmaybe int align, attempt = 0;
25425843Ssmaybe
25435843Ssmaybe if (minctg == 1)
25445843Ssmaybe contig = 0;
25455084Sjohnlev lo_mfn = mmu_btop(mattr->dma_attr_addr_lo);
25465084Sjohnlev hi_mfn = mmu_btop(mattr->dma_attr_addr_hi);
25475843Ssmaybe pfnseg = mmu_btop(mattr->dma_attr_seg);
25485084Sjohnlev align = maxbit(mattr->dma_attr_align, mattr->dma_attr_minxfer);
25495084Sjohnlev if (align > MMU_PAGESIZE)
25505084Sjohnlev pfnalign = mmu_btop(align);
25515843Ssmaybe else
25525843Ssmaybe pfnalign = 0;
25535843Ssmaybe
25545084Sjohnlev try_again:
25555084Sjohnlev /*
25565084Sjohnlev * See if we want pages for a legacy device
25575084Sjohnlev */
25585084Sjohnlev if (hi_mfn < PFN_16MEG)
25595084Sjohnlev poolp = &io_pool_16m;
25605084Sjohnlev else
25615084Sjohnlev poolp = &io_pool_4g;
25625084Sjohnlev try_smaller:
25635084Sjohnlev /*
25645843Ssmaybe * Take pages from I/O pool. We'll use pages from the highest
25655843Ssmaybe * MFN range possible.
25665084Sjohnlev */
25675084Sjohnlev pp_first = pp_last = NULL;
25685084Sjohnlev mutex_enter(&io_pool_lock);
25695843Ssmaybe nwanted = minctg;
25705843Ssmaybe for (pp = *poolp; pp && nwanted > 0; ) {
25715084Sjohnlev pp = pp->p_prev;
25725084Sjohnlev
25735084Sjohnlev /*
25745084Sjohnlev * skip pages above allowable range
25755084Sjohnlev */
25765084Sjohnlev mfn = mfn_list[pp->p_pagenum];
25775084Sjohnlev if (hi_mfn < mfn)
25785084Sjohnlev goto skip;
25795084Sjohnlev
25805084Sjohnlev /*
25815084Sjohnlev * stop at pages below allowable range
25825084Sjohnlev */
25835084Sjohnlev if (lo_mfn > mfn)
25845084Sjohnlev break;
25855084Sjohnlev restart:
25865084Sjohnlev if (pp_last == NULL) {
25875084Sjohnlev /*
25885084Sjohnlev * Check alignment
25895084Sjohnlev */
25905843Ssmaybe tmfn = mfn - (minctg - 1);
25915843Ssmaybe if (pfnalign && tmfn != P2ROUNDUP(tmfn, pfnalign))
25925843Ssmaybe goto skip; /* not properly aligned */
25935084Sjohnlev /*
25945084Sjohnlev * Check segment
25955084Sjohnlev */
25965084Sjohnlev if ((mfn & pfnseg) < (tmfn & pfnseg))
25975843Ssmaybe goto skip; /* crosses seg boundary */
25985084Sjohnlev /*
25995084Sjohnlev * Start building page list
26005084Sjohnlev */
26015084Sjohnlev pp_first = pp_last = pp;
26025843Ssmaybe nwanted--;
26035084Sjohnlev } else {
26045084Sjohnlev /*
26055084Sjohnlev * check physical contiguity if required
26065084Sjohnlev */
26075084Sjohnlev if (contig &&
26085084Sjohnlev mfn_list[pp_first->p_pagenum] != mfn + 1) {
26095084Sjohnlev /*
26105084Sjohnlev * not a contiguous page, restart list.
26115084Sjohnlev */
26125084Sjohnlev pp_last = NULL;
26135843Ssmaybe nwanted = minctg;
26145084Sjohnlev goto restart;
26155084Sjohnlev } else { /* add page to list */
26165084Sjohnlev pp_first = pp;
26175843Ssmaybe nwanted--;
26185084Sjohnlev }
26195084Sjohnlev }
26205084Sjohnlev skip:
26215084Sjohnlev if (pp == *poolp)
26225084Sjohnlev break;
26235084Sjohnlev }
26245084Sjohnlev
26255084Sjohnlev /*
26265084Sjohnlev * If we didn't find memory. Try the more constrained pool, then
26275843Ssmaybe * sweep free pages into the DMA pool and try again.
26285084Sjohnlev */
26295843Ssmaybe if (nwanted != 0) {
26305084Sjohnlev mutex_exit(&io_pool_lock);
26315084Sjohnlev /*
26325843Ssmaybe * If we were looking in the less constrained pool and
26335843Ssmaybe * didn't find pages, try the more constrained pool.
26345084Sjohnlev */
26355084Sjohnlev if (poolp == &io_pool_4g) {
26365084Sjohnlev poolp = &io_pool_16m;
26375084Sjohnlev goto try_smaller;
26385084Sjohnlev }
26395084Sjohnlev kmem_reap();
26405084Sjohnlev if (++attempt < 4) {
26415084Sjohnlev /*
26425084Sjohnlev * Grab some more io_pool pages
26435084Sjohnlev */
26445084Sjohnlev (void) populate_io_pool();
26455843Ssmaybe goto try_again; /* go around and retry */
26465084Sjohnlev }
26475843Ssmaybe return (NULL);
26485084Sjohnlev }
26495084Sjohnlev /*
26505084Sjohnlev * Found the pages, now snip them from the list
26515084Sjohnlev */
26525084Sjohnlev page_io_pool_sub(poolp, pp_first, pp_last);
26535843Ssmaybe io_pool_cnt -= minctg;
26545843Ssmaybe /*
26555843Ssmaybe * reset low water mark
26565843Ssmaybe */
26575084Sjohnlev if (io_pool_cnt < io_pool_cnt_lowater)
26585843Ssmaybe io_pool_cnt_lowater = io_pool_cnt;
26595084Sjohnlev mutex_exit(&io_pool_lock);
26605843Ssmaybe return (pp_first);
26615843Ssmaybe }
26625843Ssmaybe
26635843Ssmaybe page_t *
page_swap_with_hypervisor(struct vnode * vp,u_offset_t off,caddr_t vaddr,ddi_dma_attr_t * mattr,uint_t flags,pgcnt_t minctg)26645843Ssmaybe page_swap_with_hypervisor(struct vnode *vp, u_offset_t off, caddr_t vaddr,
26655843Ssmaybe ddi_dma_attr_t *mattr, uint_t flags, pgcnt_t minctg)
26665843Ssmaybe {
26675843Ssmaybe uint_t kflags;
26685843Ssmaybe int order, extra, extpages, i, contig, nbits, extents;
26695843Ssmaybe page_t *pp, *expp, *pp_first, **pplist = NULL;
26705843Ssmaybe mfn_t *mfnlist = NULL;
26715843Ssmaybe
26725843Ssmaybe contig = flags & PG_PHYSCONTIG;
26735843Ssmaybe if (minctg == 1)
26745843Ssmaybe contig = 0;
26755843Ssmaybe flags &= ~PG_PHYSCONTIG;
26765843Ssmaybe kflags = flags & PG_WAIT ? KM_SLEEP : KM_NOSLEEP;
26775843Ssmaybe /*
26785843Ssmaybe * Hypervisor will allocate extents, if we want contig
26795843Ssmaybe * pages extent must be >= minctg
26805843Ssmaybe */
26815843Ssmaybe if (contig) {
26825843Ssmaybe order = highbit(minctg) - 1;
26835843Ssmaybe if (minctg & ((1 << order) - 1))
26845843Ssmaybe order++;
26855843Ssmaybe extpages = 1 << order;
26865843Ssmaybe } else {
26875843Ssmaybe order = 0;
26885843Ssmaybe extpages = minctg;
26895843Ssmaybe }
26905843Ssmaybe if (extpages > minctg) {
26915843Ssmaybe extra = extpages - minctg;
26925843Ssmaybe if (!page_resv(extra, kflags))
26935843Ssmaybe return (NULL);
26945843Ssmaybe }
26955843Ssmaybe pp_first = NULL;
26965843Ssmaybe pplist = kmem_alloc(extpages * sizeof (page_t *), kflags);
26975843Ssmaybe if (pplist == NULL)
26985843Ssmaybe goto balloon_fail;
26995843Ssmaybe mfnlist = kmem_alloc(extpages * sizeof (mfn_t), kflags);
27005843Ssmaybe if (mfnlist == NULL)
27015843Ssmaybe goto balloon_fail;
27025843Ssmaybe pp = page_create_va(vp, off, minctg * PAGESIZE, flags, &kvseg, vaddr);
27035843Ssmaybe if (pp == NULL)
27045843Ssmaybe goto balloon_fail;
27055843Ssmaybe pp_first = pp;
27065843Ssmaybe if (extpages > minctg) {
27075843Ssmaybe /*
27085843Ssmaybe * fill out the rest of extent pages to swap
27095843Ssmaybe * with the hypervisor
27105843Ssmaybe */
27115843Ssmaybe for (i = 0; i < extra; i++) {
27125843Ssmaybe expp = page_create_va(vp,
27135843Ssmaybe (u_offset_t)(uintptr_t)io_pool_kva,
27145843Ssmaybe PAGESIZE, flags, &kvseg, io_pool_kva);
27155843Ssmaybe if (expp == NULL)
27165843Ssmaybe goto balloon_fail;
27175843Ssmaybe (void) hat_pageunload(expp, HAT_FORCE_PGUNLOAD);
27185843Ssmaybe page_io_unlock(expp);
27195843Ssmaybe page_hashout(expp, NULL);
27205843Ssmaybe page_io_lock(expp);
27215843Ssmaybe /*
27225843Ssmaybe * add page to end of list
27235843Ssmaybe */
27245843Ssmaybe expp->p_prev = pp_first->p_prev;
27255843Ssmaybe expp->p_next = pp_first;
27265843Ssmaybe expp->p_prev->p_next = expp;
27275843Ssmaybe pp_first->p_prev = expp;
27285084Sjohnlev }
27295843Ssmaybe
27305843Ssmaybe }
27315843Ssmaybe for (i = 0; i < extpages; i++) {
27325843Ssmaybe pplist[i] = pp;
27335084Sjohnlev pp = pp->p_next;
27345843Ssmaybe }
27355843Ssmaybe nbits = highbit(mattr->dma_attr_addr_hi);
27365843Ssmaybe extents = contig ? 1 : minctg;
27375843Ssmaybe if (balloon_replace_pages(extents, pplist, nbits, order,
27385843Ssmaybe mfnlist) != extents) {
27395843Ssmaybe if (ioalloc_dbg)
27405843Ssmaybe cmn_err(CE_NOTE, "request to hypervisor"
27415843Ssmaybe " for %d pages, maxaddr %" PRIx64 " failed",
27425843Ssmaybe extpages, mattr->dma_attr_addr_hi);
27435843Ssmaybe goto balloon_fail;
27445843Ssmaybe }
27455843Ssmaybe
27465843Ssmaybe kmem_free(pplist, extpages * sizeof (page_t *));
27475843Ssmaybe kmem_free(mfnlist, extpages * sizeof (mfn_t));
27485843Ssmaybe /*
27495843Ssmaybe * Return any excess pages to free list
27505843Ssmaybe */
27515843Ssmaybe if (extpages > minctg) {
27525843Ssmaybe for (i = 0; i < extra; i++) {
27535843Ssmaybe pp = pp_first->p_prev;
27545843Ssmaybe page_sub(&pp_first, pp);
27555843Ssmaybe page_io_unlock(pp);
27565843Ssmaybe page_unresv(1);
27575843Ssmaybe page_free(pp, 1);
27585843Ssmaybe }
27595843Ssmaybe }
27605084Sjohnlev return (pp_first);
27615084Sjohnlev balloon_fail:
27625084Sjohnlev /*
27635084Sjohnlev * Return pages to free list and return failure
27645084Sjohnlev */
27655084Sjohnlev while (pp_first != NULL) {
27665084Sjohnlev pp = pp_first;
27675084Sjohnlev page_sub(&pp_first, pp);
27685084Sjohnlev page_io_unlock(pp);
27695084Sjohnlev if (pp->p_vnode != NULL)
27705084Sjohnlev page_hashout(pp, NULL);
27715084Sjohnlev page_free(pp, 1);
27725084Sjohnlev }
27735084Sjohnlev if (pplist)
27745084Sjohnlev kmem_free(pplist, extpages * sizeof (page_t *));
27755084Sjohnlev if (mfnlist)
27765084Sjohnlev kmem_free(mfnlist, extpages * sizeof (mfn_t));
27775843Ssmaybe page_unresv(extpages - minctg);
27785843Ssmaybe return (NULL);
27795843Ssmaybe }
27805843Ssmaybe
27815843Ssmaybe static void
return_partial_alloc(page_t * plist)27825843Ssmaybe return_partial_alloc(page_t *plist)
27835843Ssmaybe {
27845843Ssmaybe page_t *pp;
27855843Ssmaybe
27865843Ssmaybe while (plist != NULL) {
27875843Ssmaybe pp = plist;
27885843Ssmaybe page_sub(&plist, pp);
27897173Smrj page_io_unlock(pp);
27905843Ssmaybe page_destroy_io(pp);
27915843Ssmaybe }
27925843Ssmaybe }
27935843Ssmaybe
27945843Ssmaybe static page_t *
page_get_contigpages(struct vnode * vp,u_offset_t off,int * npagesp,uint_t flags,caddr_t vaddr,ddi_dma_attr_t * mattr)27955843Ssmaybe page_get_contigpages(
27965843Ssmaybe struct vnode *vp,
27975843Ssmaybe u_offset_t off,
27985843Ssmaybe int *npagesp,
27995843Ssmaybe uint_t flags,
28005843Ssmaybe caddr_t vaddr,
28015843Ssmaybe ddi_dma_attr_t *mattr)
28025843Ssmaybe {
28035843Ssmaybe mfn_t max_mfn = HYPERVISOR_memory_op(XENMEM_maximum_ram_page, NULL);
28045843Ssmaybe page_t *plist; /* list to return */
28055843Ssmaybe page_t *pp, *mcpl;
28065843Ssmaybe int contig, anyaddr, npages, getone = 0;
28075843Ssmaybe mfn_t lo_mfn;
28085843Ssmaybe mfn_t hi_mfn;
28095843Ssmaybe pgcnt_t pfnalign = 0;
28105843Ssmaybe int align, sgllen;
28115843Ssmaybe uint64_t pfnseg;
28125843Ssmaybe pgcnt_t minctg;
28135843Ssmaybe
28145843Ssmaybe npages = *npagesp;
28155843Ssmaybe ASSERT(mattr != NULL);
28165843Ssmaybe lo_mfn = mmu_btop(mattr->dma_attr_addr_lo);
28175843Ssmaybe hi_mfn = mmu_btop(mattr->dma_attr_addr_hi);
28185843Ssmaybe sgllen = mattr->dma_attr_sgllen;
28195843Ssmaybe pfnseg = mmu_btop(mattr->dma_attr_seg);
28205843Ssmaybe align = maxbit(mattr->dma_attr_align, mattr->dma_attr_minxfer);
28215843Ssmaybe if (align > MMU_PAGESIZE)
28225843Ssmaybe pfnalign = mmu_btop(align);
28235843Ssmaybe
282410175SStuart.Maybee@Sun.COM contig = flags & PG_PHYSCONTIG;
282510175SStuart.Maybee@Sun.COM if (npages == -1) {
282610175SStuart.Maybee@Sun.COM npages = 1;
282710175SStuart.Maybee@Sun.COM pfnalign = 0;
282810175SStuart.Maybee@Sun.COM }
28295843Ssmaybe /*
28305843Ssmaybe * Clear the contig flag if only one page is needed.
28315843Ssmaybe */
28325843Ssmaybe if (npages == 1) {
28335843Ssmaybe getone = 1;
28345843Ssmaybe contig = 0;
28355843Ssmaybe }
28365843Ssmaybe
28375843Ssmaybe /*
28385843Ssmaybe * Check if any page in the system is fine.
28395843Ssmaybe */
284010175SStuart.Maybee@Sun.COM anyaddr = lo_mfn == 0 && hi_mfn >= max_mfn;
284110175SStuart.Maybee@Sun.COM if (!contig && anyaddr && !pfnalign) {
28425843Ssmaybe flags &= ~PG_PHYSCONTIG;
28435843Ssmaybe plist = page_create_va(vp, off, npages * MMU_PAGESIZE,
28445843Ssmaybe flags, &kvseg, vaddr);
28455843Ssmaybe if (plist != NULL) {
28465843Ssmaybe *npagesp = 0;
28475843Ssmaybe return (plist);
28485843Ssmaybe }
28495843Ssmaybe }
28505843Ssmaybe plist = NULL;
28515843Ssmaybe minctg = howmany(npages, sgllen);
28525843Ssmaybe while (npages > sgllen || getone) {
28536015Ssmaybe if (minctg > npages)
28546015Ssmaybe minctg = npages;
28556015Ssmaybe mcpl = NULL;
28565843Ssmaybe /*
285710175SStuart.Maybee@Sun.COM * We could want contig pages with no address range limits.
28585843Ssmaybe */
28596282Ssmaybe if (anyaddr && contig) {
28605843Ssmaybe /*
28615843Ssmaybe * Look for free contig pages to satisfy the request.
28625843Ssmaybe */
286310175SStuart.Maybee@Sun.COM mcpl = find_contig_free(minctg, flags, pfnseg,
286410175SStuart.Maybee@Sun.COM pfnalign);
28655843Ssmaybe }
28665843Ssmaybe /*
28675843Ssmaybe * Try the reserved io pools next
28685843Ssmaybe */
28695843Ssmaybe if (mcpl == NULL)
28705843Ssmaybe mcpl = page_io_pool_alloc(mattr, contig, minctg);
28715843Ssmaybe if (mcpl != NULL) {
28725843Ssmaybe pp = mcpl;
28735843Ssmaybe do {
28745843Ssmaybe if (!page_hashin(pp, vp, off, NULL)) {
28755843Ssmaybe panic("page_get_contigpages:"
28765843Ssmaybe " hashin failed"
28775843Ssmaybe " pp %p, vp %p, off %llx",
28785843Ssmaybe (void *)pp, (void *)vp, off);
28795843Ssmaybe }
28805843Ssmaybe off += MMU_PAGESIZE;
28815843Ssmaybe PP_CLRFREE(pp);
28825843Ssmaybe PP_CLRAGED(pp);
28835843Ssmaybe page_set_props(pp, P_REF);
28845843Ssmaybe page_io_lock(pp);
28855843Ssmaybe pp = pp->p_next;
28865843Ssmaybe } while (pp != mcpl);
28875843Ssmaybe } else {
28885843Ssmaybe /*
28895843Ssmaybe * Hypervisor exchange doesn't handle segment or
28905843Ssmaybe * alignment constraints
28915843Ssmaybe */
28925843Ssmaybe if (mattr->dma_attr_seg < mattr->dma_attr_addr_hi ||
28935843Ssmaybe pfnalign)
28945843Ssmaybe goto fail;
28955843Ssmaybe /*
28965843Ssmaybe * Try exchanging pages with the hypervisor
28975843Ssmaybe */
28985843Ssmaybe mcpl = page_swap_with_hypervisor(vp, off, vaddr, mattr,
28995843Ssmaybe flags, minctg);
29005843Ssmaybe if (mcpl == NULL)
29015843Ssmaybe goto fail;
29025843Ssmaybe off += minctg * MMU_PAGESIZE;
29035843Ssmaybe }
29045843Ssmaybe check_dma(mattr, mcpl, minctg);
29055843Ssmaybe /*
29065843Ssmaybe * Here with a minctg run of contiguous pages, add them to the
29075843Ssmaybe * list we will return for this request.
29085843Ssmaybe */
29095843Ssmaybe page_list_concat(&plist, &mcpl);
29105843Ssmaybe npages -= minctg;
29115843Ssmaybe *npagesp = npages;
29125843Ssmaybe sgllen--;
29136015Ssmaybe if (getone)
29146015Ssmaybe break;
29155843Ssmaybe }
29165843Ssmaybe return (plist);
29175843Ssmaybe fail:
29185843Ssmaybe return_partial_alloc(plist);
29195843Ssmaybe return (NULL);
29205843Ssmaybe }
29215843Ssmaybe
29225843Ssmaybe /*
29235843Ssmaybe * Allocator for domain 0 I/O pages. We match the required
29245843Ssmaybe * DMA attributes and contiguity constraints.
29255843Ssmaybe */
29265843Ssmaybe /*ARGSUSED*/
29275843Ssmaybe page_t *
page_create_io(struct vnode * vp,u_offset_t off,uint_t bytes,uint_t flags,struct as * as,caddr_t vaddr,ddi_dma_attr_t * mattr)29285843Ssmaybe page_create_io(
29295843Ssmaybe struct vnode *vp,
29305843Ssmaybe u_offset_t off,
29315843Ssmaybe uint_t bytes,
29325843Ssmaybe uint_t flags,
29335843Ssmaybe struct as *as,
29345843Ssmaybe caddr_t vaddr,
29355843Ssmaybe ddi_dma_attr_t *mattr)
29365843Ssmaybe {
29375843Ssmaybe page_t *plist = NULL, *pp;
29385843Ssmaybe int npages = 0, contig, anyaddr, pages_req;
29395843Ssmaybe mfn_t lo_mfn;
29405843Ssmaybe mfn_t hi_mfn;
29415843Ssmaybe pgcnt_t pfnalign = 0;
29425843Ssmaybe int align;
29435843Ssmaybe int is_domu = 0;
29445843Ssmaybe int dummy, bytes_got;
29455843Ssmaybe mfn_t max_mfn = HYPERVISOR_memory_op(XENMEM_maximum_ram_page, NULL);
29465843Ssmaybe
29475843Ssmaybe ASSERT(mattr != NULL);
29485843Ssmaybe lo_mfn = mmu_btop(mattr->dma_attr_addr_lo);
29495843Ssmaybe hi_mfn = mmu_btop(mattr->dma_attr_addr_hi);
29505843Ssmaybe align = maxbit(mattr->dma_attr_align, mattr->dma_attr_minxfer);
29515843Ssmaybe if (align > MMU_PAGESIZE)
29525843Ssmaybe pfnalign = mmu_btop(align);
29535843Ssmaybe
29545843Ssmaybe /*
29555843Ssmaybe * Clear the contig flag if only one page is needed or the scatter
29565843Ssmaybe * gather list length is >= npages.
29575843Ssmaybe */
29585843Ssmaybe pages_req = npages = mmu_btopr(bytes);
29595843Ssmaybe contig = (flags & PG_PHYSCONTIG);
29605843Ssmaybe bytes = P2ROUNDUP(bytes, MMU_PAGESIZE);
29615843Ssmaybe if (bytes == MMU_PAGESIZE || mattr->dma_attr_sgllen >= npages)
29625843Ssmaybe contig = 0;
29635843Ssmaybe
29645843Ssmaybe /*
29655843Ssmaybe * Check if any old page in the system is fine.
29665843Ssmaybe * DomU should always go down this path.
29675843Ssmaybe */
29685843Ssmaybe is_domu = !DOMAIN_IS_INITDOMAIN(xen_info);
29695843Ssmaybe anyaddr = lo_mfn == 0 && hi_mfn >= max_mfn && !pfnalign;
29705843Ssmaybe if ((!contig && anyaddr) || is_domu) {
29715843Ssmaybe flags &= ~PG_PHYSCONTIG;
29725843Ssmaybe plist = page_create_va(vp, off, bytes, flags, &kvseg, vaddr);
29735843Ssmaybe if (plist != NULL)
29745843Ssmaybe return (plist);
29755843Ssmaybe else if (is_domu)
29765843Ssmaybe return (NULL); /* no memory available */
29775843Ssmaybe }
29785843Ssmaybe /*
29795843Ssmaybe * DomU should never reach here
29805843Ssmaybe */
29815843Ssmaybe if (contig) {
29825843Ssmaybe plist = page_get_contigpages(vp, off, &npages, flags, vaddr,
29835843Ssmaybe mattr);
29845843Ssmaybe if (plist == NULL)
29855843Ssmaybe goto fail;
29865843Ssmaybe bytes_got = (pages_req - npages) << MMU_PAGESHIFT;
29875843Ssmaybe vaddr += bytes_got;
29885843Ssmaybe off += bytes_got;
29895843Ssmaybe /*
29905843Ssmaybe * We now have all the contiguous pages we need, but
29915843Ssmaybe * we may still need additional non-contiguous pages.
29925843Ssmaybe */
29935843Ssmaybe }
29945843Ssmaybe /*
29955843Ssmaybe * now loop collecting the requested number of pages, these do
29965843Ssmaybe * not have to be contiguous pages but we will use the contig
29975843Ssmaybe * page alloc code to get the pages since it will honor any
29985843Ssmaybe * other constraints the pages may have.
29995843Ssmaybe */
30005843Ssmaybe while (npages--) {
300110175SStuart.Maybee@Sun.COM dummy = -1;
30025843Ssmaybe pp = page_get_contigpages(vp, off, &dummy, flags, vaddr, mattr);
30035843Ssmaybe if (pp == NULL)
30045843Ssmaybe goto fail;
30055843Ssmaybe page_add(&plist, pp);
30065843Ssmaybe vaddr += MMU_PAGESIZE;
30075843Ssmaybe off += MMU_PAGESIZE;
30085843Ssmaybe }
30095843Ssmaybe return (plist);
30105843Ssmaybe fail:
30115843Ssmaybe /*
30125843Ssmaybe * Failed to get enough pages, return ones we did get
30135843Ssmaybe */
30145843Ssmaybe return_partial_alloc(plist);
30155084Sjohnlev return (NULL);
30165084Sjohnlev }
30175084Sjohnlev
30185084Sjohnlev /*
30195084Sjohnlev * Lock and return the page with the highest mfn that we can find. last_mfn
30205084Sjohnlev * holds the last one found, so the next search can start from there. We
30215084Sjohnlev * also keep a counter so that we don't loop forever if the machine has no
30225084Sjohnlev * free pages.
30235084Sjohnlev *
30245084Sjohnlev * This is called from the balloon thread to find pages to give away. new_high
30255084Sjohnlev * is used when new mfn's have been added to the system - we will reset our
30265084Sjohnlev * search if the new mfn's are higher than our current search position.
30275084Sjohnlev */
30285084Sjohnlev page_t *
page_get_high_mfn(mfn_t new_high)30295084Sjohnlev page_get_high_mfn(mfn_t new_high)
30305084Sjohnlev {
30315084Sjohnlev static mfn_t last_mfn = 0;
30325084Sjohnlev pfn_t pfn;
30335084Sjohnlev page_t *pp;
30345084Sjohnlev ulong_t loop_count = 0;
30355084Sjohnlev
30365084Sjohnlev if (new_high > last_mfn)
30375084Sjohnlev last_mfn = new_high;
30385084Sjohnlev
30395084Sjohnlev for (; loop_count < mfn_count; loop_count++, last_mfn--) {
30405084Sjohnlev if (last_mfn == 0) {
30415084Sjohnlev last_mfn = cached_max_mfn;
30425084Sjohnlev }
30435084Sjohnlev
30445084Sjohnlev pfn = mfn_to_pfn(last_mfn);
30455084Sjohnlev if (pfn & PFN_IS_FOREIGN_MFN)
30465084Sjohnlev continue;
30475084Sjohnlev
30485084Sjohnlev /* See if the page is free. If so, lock it. */
30495084Sjohnlev pp = page_numtopp_alloc(pfn);
30505084Sjohnlev if (pp == NULL)
30515084Sjohnlev continue;
30525084Sjohnlev PP_CLRFREE(pp);
30535084Sjohnlev
30545084Sjohnlev ASSERT(PAGE_EXCL(pp));
30555084Sjohnlev ASSERT(pp->p_vnode == NULL);
30565084Sjohnlev ASSERT(!hat_page_is_mapped(pp));
30575084Sjohnlev last_mfn--;
30585084Sjohnlev return (pp);
30595084Sjohnlev }
30605084Sjohnlev return (NULL);
30615084Sjohnlev }
30625084Sjohnlev
30635084Sjohnlev #else /* !__xpv */
30645084Sjohnlev
30650Sstevel@tonic-gate /*
30660Sstevel@tonic-gate * get a page from any list with the given mnode
30670Sstevel@tonic-gate */
30685084Sjohnlev static page_t *
page_get_mnode_anylist(ulong_t origbin,uchar_t szc,uint_t flags,int mnode,int mtype,ddi_dma_attr_t * dma_attr)30690Sstevel@tonic-gate page_get_mnode_anylist(ulong_t origbin, uchar_t szc, uint_t flags,
30700Sstevel@tonic-gate int mnode, int mtype, ddi_dma_attr_t *dma_attr)
30710Sstevel@tonic-gate {
30722961Sdp78419 kmutex_t *pcm;
30732961Sdp78419 int i;
30742961Sdp78419 page_t *pp;
30752961Sdp78419 page_t *first_pp;
30762961Sdp78419 uint64_t pgaddr;
30772961Sdp78419 ulong_t bin;
30782961Sdp78419 int mtypestart;
30792961Sdp78419 int plw_initialized;
30802961Sdp78419 page_list_walker_t plw;
30810Sstevel@tonic-gate
30820Sstevel@tonic-gate VM_STAT_ADD(pga_vmstats.pgma_alloc);
30830Sstevel@tonic-gate
30840Sstevel@tonic-gate ASSERT((flags & PG_MATCH_COLOR) == 0);
30850Sstevel@tonic-gate ASSERT(szc == 0);
30860Sstevel@tonic-gate ASSERT(dma_attr != NULL);
30870Sstevel@tonic-gate
30880Sstevel@tonic-gate MTYPE_START(mnode, mtype, flags);
30890Sstevel@tonic-gate if (mtype < 0) {
30900Sstevel@tonic-gate VM_STAT_ADD(pga_vmstats.pgma_allocempty);
30910Sstevel@tonic-gate return (NULL);
30920Sstevel@tonic-gate }
30930Sstevel@tonic-gate
30940Sstevel@tonic-gate mtypestart = mtype;
30950Sstevel@tonic-gate
30960Sstevel@tonic-gate bin = origbin;
30970Sstevel@tonic-gate
30980Sstevel@tonic-gate /*
30990Sstevel@tonic-gate * check up to page_colors + 1 bins - origbin may be checked twice
31000Sstevel@tonic-gate * because of BIN_STEP skip
31010Sstevel@tonic-gate */
31020Sstevel@tonic-gate do {
31032961Sdp78419 plw_initialized = 0;
31042961Sdp78419
31052961Sdp78419 for (plw.plw_count = 0;
31062961Sdp78419 plw.plw_count < page_colors; plw.plw_count++) {
31072961Sdp78419
310812293SJames.McPherson@Sun.COM if (PAGE_FREELISTS(mnode, szc, bin, mtype) == NULL)
31090Sstevel@tonic-gate goto nextfreebin;
31100Sstevel@tonic-gate
311112293SJames.McPherson@Sun.COM pcm = PC_BIN_MUTEX(mnode, bin, PG_FREE_LIST);
31120Sstevel@tonic-gate mutex_enter(pcm);
311312293SJames.McPherson@Sun.COM pp = PAGE_FREELISTS(mnode, szc, bin, mtype);
31140Sstevel@tonic-gate first_pp = pp;
31150Sstevel@tonic-gate while (pp != NULL) {
3116*12342SDave.Plauger@Sun.COM if (IS_DUMP_PAGE(pp) || page_trylock(pp,
3117*12342SDave.Plauger@Sun.COM SE_EXCL) == 0) {
31180Sstevel@tonic-gate pp = pp->p_next;
31190Sstevel@tonic-gate if (pp == first_pp) {
31200Sstevel@tonic-gate pp = NULL;
31210Sstevel@tonic-gate }
31220Sstevel@tonic-gate continue;
31230Sstevel@tonic-gate }
31240Sstevel@tonic-gate
31250Sstevel@tonic-gate ASSERT(PP_ISFREE(pp));
31260Sstevel@tonic-gate ASSERT(PP_ISAGED(pp));
31270Sstevel@tonic-gate ASSERT(pp->p_vnode == NULL);
31280Sstevel@tonic-gate ASSERT(pp->p_hash == NULL);
31290Sstevel@tonic-gate ASSERT(pp->p_offset == (u_offset_t)-1);
31300Sstevel@tonic-gate ASSERT(pp->p_szc == szc);
31310Sstevel@tonic-gate ASSERT(PFN_2_MEM_NODE(pp->p_pagenum) == mnode);
31320Sstevel@tonic-gate /* check if page within DMA attributes */
31333446Smrj pgaddr = pa_to_ma(pfn_to_pa(pp->p_pagenum));
31340Sstevel@tonic-gate if ((pgaddr >= dma_attr->dma_attr_addr_lo) &&
31350Sstevel@tonic-gate (pgaddr + MMU_PAGESIZE - 1 <=
31360Sstevel@tonic-gate dma_attr->dma_attr_addr_hi)) {
31370Sstevel@tonic-gate break;
31380Sstevel@tonic-gate }
31390Sstevel@tonic-gate
31400Sstevel@tonic-gate /* continue looking */
31410Sstevel@tonic-gate page_unlock(pp);
31420Sstevel@tonic-gate pp = pp->p_next;
31430Sstevel@tonic-gate if (pp == first_pp)
31440Sstevel@tonic-gate pp = NULL;
31450Sstevel@tonic-gate
31460Sstevel@tonic-gate }
31470Sstevel@tonic-gate if (pp != NULL) {
31480Sstevel@tonic-gate ASSERT(mtype == PP_2_MTYPE(pp));
31490Sstevel@tonic-gate ASSERT(pp->p_szc == 0);
31500Sstevel@tonic-gate
31510Sstevel@tonic-gate /* found a page with specified DMA attributes */
315212293SJames.McPherson@Sun.COM page_sub(&PAGE_FREELISTS(mnode, szc, bin,
315312293SJames.McPherson@Sun.COM mtype), pp);
3154414Skchow page_ctr_sub(mnode, mtype, pp, PG_FREE_LIST);
31550Sstevel@tonic-gate
31560Sstevel@tonic-gate if ((PP_ISFREE(pp) == 0) ||
31570Sstevel@tonic-gate (PP_ISAGED(pp) == 0)) {
31580Sstevel@tonic-gate cmn_err(CE_PANIC, "page %p is not free",
31590Sstevel@tonic-gate (void *)pp);
31600Sstevel@tonic-gate }
31610Sstevel@tonic-gate
31620Sstevel@tonic-gate mutex_exit(pcm);
31630Sstevel@tonic-gate check_dma(dma_attr, pp, 1);
31640Sstevel@tonic-gate VM_STAT_ADD(pga_vmstats.pgma_allocok);
31650Sstevel@tonic-gate return (pp);
31660Sstevel@tonic-gate }
31670Sstevel@tonic-gate mutex_exit(pcm);
31680Sstevel@tonic-gate nextfreebin:
31692961Sdp78419 if (plw_initialized == 0) {
31702961Sdp78419 page_list_walk_init(szc, 0, bin, 1, 0, &plw);
31712961Sdp78419 ASSERT(plw.plw_ceq_dif == page_colors);
31722961Sdp78419 plw_initialized = 1;
31732961Sdp78419 }
31740Sstevel@tonic-gate
31752961Sdp78419 if (plw.plw_do_split) {
31762961Sdp78419 pp = page_freelist_split(szc, bin, mnode,
31772961Sdp78419 mtype,
31787656SSherry.Moore@Sun.COM mmu_btop(dma_attr->dma_attr_addr_lo),
31792961Sdp78419 mmu_btop(dma_attr->dma_attr_addr_hi + 1),
31802961Sdp78419 &plw);
31817656SSherry.Moore@Sun.COM if (pp != NULL) {
31827656SSherry.Moore@Sun.COM check_dma(dma_attr, pp, 1);
31832961Sdp78419 return (pp);
31847656SSherry.Moore@Sun.COM }
31852961Sdp78419 }
31862961Sdp78419
31872961Sdp78419 bin = page_list_walk_next_bin(szc, bin, &plw);
31880Sstevel@tonic-gate }
31892961Sdp78419
3190414Skchow MTYPE_NEXT(mnode, mtype, flags);
3191414Skchow } while (mtype >= 0);
31920Sstevel@tonic-gate
31930Sstevel@tonic-gate /* failed to find a page in the freelist; try it in the cachelist */
31940Sstevel@tonic-gate
31950Sstevel@tonic-gate /* reset mtype start for cachelist search */
31960Sstevel@tonic-gate mtype = mtypestart;
31970Sstevel@tonic-gate ASSERT(mtype >= 0);
31980Sstevel@tonic-gate
31990Sstevel@tonic-gate /* start with the bin of matching color */
32000Sstevel@tonic-gate bin = origbin;
32010Sstevel@tonic-gate
32020Sstevel@tonic-gate do {
32030Sstevel@tonic-gate for (i = 0; i <= page_colors; i++) {
32040Sstevel@tonic-gate if (PAGE_CACHELISTS(mnode, bin, mtype) == NULL)
32050Sstevel@tonic-gate goto nextcachebin;
320612293SJames.McPherson@Sun.COM pcm = PC_BIN_MUTEX(mnode, bin, PG_CACHE_LIST);
32070Sstevel@tonic-gate mutex_enter(pcm);
32080Sstevel@tonic-gate pp = PAGE_CACHELISTS(mnode, bin, mtype);
32090Sstevel@tonic-gate first_pp = pp;
32100Sstevel@tonic-gate while (pp != NULL) {
3211*12342SDave.Plauger@Sun.COM if (IS_DUMP_PAGE(pp) || page_trylock(pp,
3212*12342SDave.Plauger@Sun.COM SE_EXCL) == 0) {
32130Sstevel@tonic-gate pp = pp->p_next;
32140Sstevel@tonic-gate if (pp == first_pp)
32159244SSherry.Moore@Sun.COM pp = NULL;
32160Sstevel@tonic-gate continue;
32170Sstevel@tonic-gate }
32180Sstevel@tonic-gate ASSERT(pp->p_vnode);
32190Sstevel@tonic-gate ASSERT(PP_ISAGED(pp) == 0);
32200Sstevel@tonic-gate ASSERT(pp->p_szc == 0);
32210Sstevel@tonic-gate ASSERT(PFN_2_MEM_NODE(pp->p_pagenum) == mnode);
32220Sstevel@tonic-gate
32230Sstevel@tonic-gate /* check if page within DMA attributes */
32240Sstevel@tonic-gate
32253446Smrj pgaddr = pa_to_ma(pfn_to_pa(pp->p_pagenum));
32260Sstevel@tonic-gate if ((pgaddr >= dma_attr->dma_attr_addr_lo) &&
32270Sstevel@tonic-gate (pgaddr + MMU_PAGESIZE - 1 <=
32280Sstevel@tonic-gate dma_attr->dma_attr_addr_hi)) {
32290Sstevel@tonic-gate break;
32300Sstevel@tonic-gate }
32310Sstevel@tonic-gate
32320Sstevel@tonic-gate /* continue looking */
32330Sstevel@tonic-gate page_unlock(pp);
32340Sstevel@tonic-gate pp = pp->p_next;
32350Sstevel@tonic-gate if (pp == first_pp)
32360Sstevel@tonic-gate pp = NULL;
32370Sstevel@tonic-gate }
32380Sstevel@tonic-gate
32390Sstevel@tonic-gate if (pp != NULL) {
32400Sstevel@tonic-gate ASSERT(mtype == PP_2_MTYPE(pp));
32410Sstevel@tonic-gate ASSERT(pp->p_szc == 0);
32420Sstevel@tonic-gate
32430Sstevel@tonic-gate /* found a page with specified DMA attributes */
32440Sstevel@tonic-gate page_sub(&PAGE_CACHELISTS(mnode, bin,
32450Sstevel@tonic-gate mtype), pp);
3246414Skchow page_ctr_sub(mnode, mtype, pp, PG_CACHE_LIST);
32470Sstevel@tonic-gate
32480Sstevel@tonic-gate mutex_exit(pcm);
32490Sstevel@tonic-gate ASSERT(pp->p_vnode);
32500Sstevel@tonic-gate ASSERT(PP_ISAGED(pp) == 0);
32510Sstevel@tonic-gate check_dma(dma_attr, pp, 1);
32520Sstevel@tonic-gate VM_STAT_ADD(pga_vmstats.pgma_allocok);
32530Sstevel@tonic-gate return (pp);
32540Sstevel@tonic-gate }
32550Sstevel@tonic-gate mutex_exit(pcm);
32560Sstevel@tonic-gate nextcachebin:
32570Sstevel@tonic-gate bin += (i == 0) ? BIN_STEP : 1;
32580Sstevel@tonic-gate bin &= page_colors_mask;
32590Sstevel@tonic-gate }
3260414Skchow MTYPE_NEXT(mnode, mtype, flags);
3261414Skchow } while (mtype >= 0);
32620Sstevel@tonic-gate
32630Sstevel@tonic-gate VM_STAT_ADD(pga_vmstats.pgma_allocfailed);
32640Sstevel@tonic-gate return (NULL);
32650Sstevel@tonic-gate }
32660Sstevel@tonic-gate
32670Sstevel@tonic-gate /*
32680Sstevel@tonic-gate * This function is similar to page_get_freelist()/page_get_cachelist()
32690Sstevel@tonic-gate * but it searches both the lists to find a page with the specified
32700Sstevel@tonic-gate * color (or no color) and DMA attributes. The search is done in the
32710Sstevel@tonic-gate * freelist first and then in the cache list within the highest memory
32720Sstevel@tonic-gate * range (based on DMA attributes) before searching in the lower
32730Sstevel@tonic-gate * memory ranges.
32740Sstevel@tonic-gate *
32750Sstevel@tonic-gate * Note: This function is called only by page_create_io().
32760Sstevel@tonic-gate */
32770Sstevel@tonic-gate /*ARGSUSED*/
32785084Sjohnlev static page_t *
page_get_anylist(struct vnode * vp,u_offset_t off,struct as * as,caddr_t vaddr,size_t size,uint_t flags,ddi_dma_attr_t * dma_attr,lgrp_t * lgrp)32790Sstevel@tonic-gate page_get_anylist(struct vnode *vp, u_offset_t off, struct as *as, caddr_t vaddr,
32800Sstevel@tonic-gate size_t size, uint_t flags, ddi_dma_attr_t *dma_attr, lgrp_t *lgrp)
32810Sstevel@tonic-gate {
32820Sstevel@tonic-gate uint_t bin;
32830Sstevel@tonic-gate int mtype;
32840Sstevel@tonic-gate page_t *pp;
32850Sstevel@tonic-gate int n;
32860Sstevel@tonic-gate int m;
32870Sstevel@tonic-gate int szc;
32880Sstevel@tonic-gate int fullrange;
32890Sstevel@tonic-gate int mnode;
32900Sstevel@tonic-gate int local_failed_stat = 0;
32910Sstevel@tonic-gate lgrp_mnode_cookie_t lgrp_cookie;
32920Sstevel@tonic-gate
32930Sstevel@tonic-gate VM_STAT_ADD(pga_vmstats.pga_alloc);
32940Sstevel@tonic-gate
32950Sstevel@tonic-gate /* only base pagesize currently supported */
32960Sstevel@tonic-gate if (size != MMU_PAGESIZE)
32970Sstevel@tonic-gate return (NULL);
32980Sstevel@tonic-gate
32990Sstevel@tonic-gate /*
33000Sstevel@tonic-gate * If we're passed a specific lgroup, we use it. Otherwise,
33010Sstevel@tonic-gate * assume first-touch placement is desired.
33020Sstevel@tonic-gate */
33030Sstevel@tonic-gate if (!LGRP_EXISTS(lgrp))
33040Sstevel@tonic-gate lgrp = lgrp_home_lgrp();
33050Sstevel@tonic-gate
33060Sstevel@tonic-gate /* LINTED */
330712293SJames.McPherson@Sun.COM AS_2_BIN(as, seg, vp, vaddr, bin, 0);
33080Sstevel@tonic-gate
33090Sstevel@tonic-gate /*
33100Sstevel@tonic-gate * Only hold one freelist or cachelist lock at a time, that way we
33110Sstevel@tonic-gate * can start anywhere and not have to worry about lock
33120Sstevel@tonic-gate * ordering.
33130Sstevel@tonic-gate */
33140Sstevel@tonic-gate if (dma_attr == NULL) {
331512004Sjiang.liu@intel.com n = mtype16m;
331612004Sjiang.liu@intel.com m = mtypetop;
33170Sstevel@tonic-gate fullrange = 1;
33180Sstevel@tonic-gate VM_STAT_ADD(pga_vmstats.pga_nulldmaattr);
33190Sstevel@tonic-gate } else {
33200Sstevel@tonic-gate pfn_t pfnlo = mmu_btop(dma_attr->dma_attr_addr_lo);
33210Sstevel@tonic-gate pfn_t pfnhi = mmu_btop(dma_attr->dma_attr_addr_hi);
33220Sstevel@tonic-gate
33230Sstevel@tonic-gate /*
33240Sstevel@tonic-gate * We can guarantee alignment only for page boundary.
33250Sstevel@tonic-gate */
33260Sstevel@tonic-gate if (dma_attr->dma_attr_align > MMU_PAGESIZE)
33270Sstevel@tonic-gate return (NULL);
33280Sstevel@tonic-gate
332912004Sjiang.liu@intel.com /* Sanity check the dma_attr */
333012004Sjiang.liu@intel.com if (pfnlo > pfnhi)
333112004Sjiang.liu@intel.com return (NULL);
333212004Sjiang.liu@intel.com
33330Sstevel@tonic-gate n = pfn_2_mtype(pfnlo);
33340Sstevel@tonic-gate m = pfn_2_mtype(pfnhi);
33350Sstevel@tonic-gate
33360Sstevel@tonic-gate fullrange = ((pfnlo == mnoderanges[n].mnr_pfnlo) &&
33370Sstevel@tonic-gate (pfnhi >= mnoderanges[m].mnr_pfnhi));
33380Sstevel@tonic-gate }
33390Sstevel@tonic-gate VM_STAT_COND_ADD(fullrange == 0, pga_vmstats.pga_notfullrange);
33400Sstevel@tonic-gate
33410Sstevel@tonic-gate szc = 0;
33420Sstevel@tonic-gate
334312004Sjiang.liu@intel.com /* cylcing thru mtype handled by RANGE0 if n == mtype16m */
334412004Sjiang.liu@intel.com if (n == mtype16m) {
33450Sstevel@tonic-gate flags |= PGI_MT_RANGE0;
33460Sstevel@tonic-gate n = m;
33470Sstevel@tonic-gate }
33480Sstevel@tonic-gate
33490Sstevel@tonic-gate /*
33500Sstevel@tonic-gate * Try local memory node first, but try remote if we can't
33510Sstevel@tonic-gate * get a page of the right color.
33520Sstevel@tonic-gate */
33530Sstevel@tonic-gate LGRP_MNODE_COOKIE_INIT(lgrp_cookie, lgrp, LGRP_SRCH_HIER);
33540Sstevel@tonic-gate while ((mnode = lgrp_memnode_choose(&lgrp_cookie)) >= 0) {
33550Sstevel@tonic-gate /*
33560Sstevel@tonic-gate * allocate pages from high pfn to low.
33570Sstevel@tonic-gate */
335812004Sjiang.liu@intel.com mtype = m;
335912004Sjiang.liu@intel.com do {
33600Sstevel@tonic-gate if (fullrange != 0) {
336112293SJames.McPherson@Sun.COM pp = page_get_mnode_freelist(mnode,
33620Sstevel@tonic-gate bin, mtype, szc, flags);
33630Sstevel@tonic-gate if (pp == NULL) {
33640Sstevel@tonic-gate pp = page_get_mnode_cachelist(
33655084Sjohnlev bin, flags, mnode, mtype);
33660Sstevel@tonic-gate }
33670Sstevel@tonic-gate } else {
33680Sstevel@tonic-gate pp = page_get_mnode_anylist(bin, szc,
33690Sstevel@tonic-gate flags, mnode, mtype, dma_attr);
33700Sstevel@tonic-gate }
33710Sstevel@tonic-gate if (pp != NULL) {
33720Sstevel@tonic-gate VM_STAT_ADD(pga_vmstats.pga_allocok);
33730Sstevel@tonic-gate check_dma(dma_attr, pp, 1);
33740Sstevel@tonic-gate return (pp);
33750Sstevel@tonic-gate }
337612004Sjiang.liu@intel.com } while (mtype != n &&
337712004Sjiang.liu@intel.com (mtype = mnoderanges[mtype].mnr_next) != -1);
33780Sstevel@tonic-gate if (!local_failed_stat) {
33790Sstevel@tonic-gate lgrp_stat_add(lgrp->lgrp_id, LGRP_NUM_ALLOC_FAIL, 1);
33800Sstevel@tonic-gate local_failed_stat = 1;
33810Sstevel@tonic-gate }
33820Sstevel@tonic-gate }
33830Sstevel@tonic-gate VM_STAT_ADD(pga_vmstats.pga_allocfailed);
33840Sstevel@tonic-gate
33850Sstevel@tonic-gate return (NULL);
33860Sstevel@tonic-gate }
33870Sstevel@tonic-gate
33880Sstevel@tonic-gate /*
33890Sstevel@tonic-gate * page_create_io()
33900Sstevel@tonic-gate *
33910Sstevel@tonic-gate * This function is a copy of page_create_va() with an additional
33920Sstevel@tonic-gate * argument 'mattr' that specifies DMA memory requirements to
33930Sstevel@tonic-gate * the page list functions. This function is used by the segkmem
33940Sstevel@tonic-gate * allocator so it is only to create new pages (i.e PG_EXCL is
33950Sstevel@tonic-gate * set).
33960Sstevel@tonic-gate *
33970Sstevel@tonic-gate * Note: This interface is currently used by x86 PSM only and is
33980Sstevel@tonic-gate * not fully specified so the commitment level is only for
33990Sstevel@tonic-gate * private interface specific to x86. This interface uses PSM
34000Sstevel@tonic-gate * specific page_get_anylist() interface.
34010Sstevel@tonic-gate */
34020Sstevel@tonic-gate
34030Sstevel@tonic-gate #define PAGE_HASH_SEARCH(index, pp, vp, off) { \
34040Sstevel@tonic-gate for ((pp) = page_hash[(index)]; (pp); (pp) = (pp)->p_hash) { \
34050Sstevel@tonic-gate if ((pp)->p_vnode == (vp) && (pp)->p_offset == (off)) \
34060Sstevel@tonic-gate break; \
34070Sstevel@tonic-gate } \
34080Sstevel@tonic-gate }
34090Sstevel@tonic-gate
34100Sstevel@tonic-gate
34110Sstevel@tonic-gate page_t *
page_create_io(struct vnode * vp,u_offset_t off,uint_t bytes,uint_t flags,struct as * as,caddr_t vaddr,ddi_dma_attr_t * mattr)34120Sstevel@tonic-gate page_create_io(
34130Sstevel@tonic-gate struct vnode *vp,
34140Sstevel@tonic-gate u_offset_t off,
34150Sstevel@tonic-gate uint_t bytes,
34160Sstevel@tonic-gate uint_t flags,
34170Sstevel@tonic-gate struct as *as,
34180Sstevel@tonic-gate caddr_t vaddr,
34190Sstevel@tonic-gate ddi_dma_attr_t *mattr) /* DMA memory attributes if any */
34200Sstevel@tonic-gate {
34210Sstevel@tonic-gate page_t *plist = NULL;
34220Sstevel@tonic-gate uint_t plist_len = 0;
34230Sstevel@tonic-gate pgcnt_t npages;
34240Sstevel@tonic-gate page_t *npp = NULL;
34250Sstevel@tonic-gate uint_t pages_req;
34260Sstevel@tonic-gate page_t *pp;
34270Sstevel@tonic-gate kmutex_t *phm = NULL;
34280Sstevel@tonic-gate uint_t index;
34290Sstevel@tonic-gate
34300Sstevel@tonic-gate TRACE_4(TR_FAC_VM, TR_PAGE_CREATE_START,
34315084Sjohnlev "page_create_start:vp %p off %llx bytes %u flags %x",
34325084Sjohnlev vp, off, bytes, flags);
34330Sstevel@tonic-gate
34340Sstevel@tonic-gate ASSERT((flags & ~(PG_EXCL | PG_WAIT | PG_PHYSCONTIG)) == 0);
34350Sstevel@tonic-gate
34360Sstevel@tonic-gate pages_req = npages = mmu_btopr(bytes);
34370Sstevel@tonic-gate
34380Sstevel@tonic-gate /*
34390Sstevel@tonic-gate * Do the freemem and pcf accounting.
34400Sstevel@tonic-gate */
34410Sstevel@tonic-gate if (!page_create_wait(npages, flags)) {
34420Sstevel@tonic-gate return (NULL);
34430Sstevel@tonic-gate }
34440Sstevel@tonic-gate
34450Sstevel@tonic-gate TRACE_2(TR_FAC_VM, TR_PAGE_CREATE_SUCCESS,
34465084Sjohnlev "page_create_success:vp %p off %llx", vp, off);
34470Sstevel@tonic-gate
34480Sstevel@tonic-gate /*
34490Sstevel@tonic-gate * If satisfying this request has left us with too little
34500Sstevel@tonic-gate * memory, start the wheels turning to get some back. The
34510Sstevel@tonic-gate * first clause of the test prevents waking up the pageout
34520Sstevel@tonic-gate * daemon in situations where it would decide that there's
34530Sstevel@tonic-gate * nothing to do.
34540Sstevel@tonic-gate */
34550Sstevel@tonic-gate if (nscan < desscan && freemem < minfree) {
34560Sstevel@tonic-gate TRACE_1(TR_FAC_VM, TR_PAGEOUT_CV_SIGNAL,
34575084Sjohnlev "pageout_cv_signal:freemem %ld", freemem);
34580Sstevel@tonic-gate cv_signal(&proc_pageout->p_cv);
34590Sstevel@tonic-gate }
34600Sstevel@tonic-gate
34610Sstevel@tonic-gate if (flags & PG_PHYSCONTIG) {
34620Sstevel@tonic-gate
34630Sstevel@tonic-gate plist = page_get_contigpage(&npages, mattr, 1);
34640Sstevel@tonic-gate if (plist == NULL) {
34650Sstevel@tonic-gate page_create_putback(npages);
34660Sstevel@tonic-gate return (NULL);
34670Sstevel@tonic-gate }
34680Sstevel@tonic-gate
34690Sstevel@tonic-gate pp = plist;
34700Sstevel@tonic-gate
34710Sstevel@tonic-gate do {
34720Sstevel@tonic-gate if (!page_hashin(pp, vp, off, NULL)) {
34730Sstevel@tonic-gate panic("pg_creat_io: hashin failed %p %p %llx",
34740Sstevel@tonic-gate (void *)pp, (void *)vp, off);
34750Sstevel@tonic-gate }
34760Sstevel@tonic-gate VM_STAT_ADD(page_create_new);
34770Sstevel@tonic-gate off += MMU_PAGESIZE;
34780Sstevel@tonic-gate PP_CLRFREE(pp);
34790Sstevel@tonic-gate PP_CLRAGED(pp);
34800Sstevel@tonic-gate page_set_props(pp, P_REF);
34810Sstevel@tonic-gate pp = pp->p_next;
34820Sstevel@tonic-gate } while (pp != plist);
34830Sstevel@tonic-gate
34840Sstevel@tonic-gate if (!npages) {
34850Sstevel@tonic-gate check_dma(mattr, plist, pages_req);
34860Sstevel@tonic-gate return (plist);
34870Sstevel@tonic-gate } else {
34880Sstevel@tonic-gate vaddr += (pages_req - npages) << MMU_PAGESHIFT;
34890Sstevel@tonic-gate }
34900Sstevel@tonic-gate
34910Sstevel@tonic-gate /*
34920Sstevel@tonic-gate * fall-thru:
34930Sstevel@tonic-gate *
34940Sstevel@tonic-gate * page_get_contigpage returns when npages <= sgllen.
34950Sstevel@tonic-gate * Grab the rest of the non-contig pages below from anylist.
34960Sstevel@tonic-gate */
34970Sstevel@tonic-gate }
34980Sstevel@tonic-gate
34990Sstevel@tonic-gate /*
35000Sstevel@tonic-gate * Loop around collecting the requested number of pages.
35010Sstevel@tonic-gate * Most of the time, we have to `create' a new page. With
35020Sstevel@tonic-gate * this in mind, pull the page off the free list before
35030Sstevel@tonic-gate * getting the hash lock. This will minimize the hash
35040Sstevel@tonic-gate * lock hold time, nesting, and the like. If it turns
35050Sstevel@tonic-gate * out we don't need the page, we put it back at the end.
35060Sstevel@tonic-gate */
35070Sstevel@tonic-gate while (npages--) {
35080Sstevel@tonic-gate phm = NULL;
35090Sstevel@tonic-gate
35100Sstevel@tonic-gate index = PAGE_HASH_FUNC(vp, off);
35110Sstevel@tonic-gate top:
35120Sstevel@tonic-gate ASSERT(phm == NULL);
35130Sstevel@tonic-gate ASSERT(index == PAGE_HASH_FUNC(vp, off));
35140Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(page_vnode_mutex(vp)));
35150Sstevel@tonic-gate
35160Sstevel@tonic-gate if (npp == NULL) {
35170Sstevel@tonic-gate /*
35180Sstevel@tonic-gate * Try to get the page of any color either from
35190Sstevel@tonic-gate * the freelist or from the cache list.
35200Sstevel@tonic-gate */
35210Sstevel@tonic-gate npp = page_get_anylist(vp, off, as, vaddr, MMU_PAGESIZE,
35220Sstevel@tonic-gate flags & ~PG_MATCH_COLOR, mattr, NULL);
35230Sstevel@tonic-gate if (npp == NULL) {
35240Sstevel@tonic-gate if (mattr == NULL) {
35250Sstevel@tonic-gate /*
35260Sstevel@tonic-gate * Not looking for a special page;
35270Sstevel@tonic-gate * panic!
35280Sstevel@tonic-gate */
35290Sstevel@tonic-gate panic("no page found %d", (int)npages);
35300Sstevel@tonic-gate }
35310Sstevel@tonic-gate /*
35320Sstevel@tonic-gate * No page found! This can happen
35330Sstevel@tonic-gate * if we are looking for a page
35340Sstevel@tonic-gate * within a specific memory range
35350Sstevel@tonic-gate * for DMA purposes. If PG_WAIT is
35360Sstevel@tonic-gate * specified then we wait for a
35370Sstevel@tonic-gate * while and then try again. The
35380Sstevel@tonic-gate * wait could be forever if we
35390Sstevel@tonic-gate * don't get the page(s) we need.
35400Sstevel@tonic-gate *
35410Sstevel@tonic-gate * Note: XXX We really need a mechanism
35420Sstevel@tonic-gate * to wait for pages in the desired
35430Sstevel@tonic-gate * range. For now, we wait for any
35440Sstevel@tonic-gate * pages and see if we can use it.
35450Sstevel@tonic-gate */
35460Sstevel@tonic-gate
35470Sstevel@tonic-gate if ((mattr != NULL) && (flags & PG_WAIT)) {
35480Sstevel@tonic-gate delay(10);
35490Sstevel@tonic-gate goto top;
35500Sstevel@tonic-gate }
35510Sstevel@tonic-gate goto fail; /* undo accounting stuff */
35520Sstevel@tonic-gate }
35530Sstevel@tonic-gate
35540Sstevel@tonic-gate if (PP_ISAGED(npp) == 0) {
35550Sstevel@tonic-gate /*
35560Sstevel@tonic-gate * Since this page came from the
35570Sstevel@tonic-gate * cachelist, we must destroy the
35580Sstevel@tonic-gate * old vnode association.
35590Sstevel@tonic-gate */
35600Sstevel@tonic-gate page_hashout(npp, (kmutex_t *)NULL);
35610Sstevel@tonic-gate }
35620Sstevel@tonic-gate }
35630Sstevel@tonic-gate
35640Sstevel@tonic-gate /*
35650Sstevel@tonic-gate * We own this page!
35660Sstevel@tonic-gate */
35670Sstevel@tonic-gate ASSERT(PAGE_EXCL(npp));
35680Sstevel@tonic-gate ASSERT(npp->p_vnode == NULL);
35690Sstevel@tonic-gate ASSERT(!hat_page_is_mapped(npp));
35700Sstevel@tonic-gate PP_CLRFREE(npp);
35710Sstevel@tonic-gate PP_CLRAGED(npp);
35720Sstevel@tonic-gate
35730Sstevel@tonic-gate /*
35740Sstevel@tonic-gate * Here we have a page in our hot little mits and are
35750Sstevel@tonic-gate * just waiting to stuff it on the appropriate lists.
35760Sstevel@tonic-gate * Get the mutex and check to see if it really does
35770Sstevel@tonic-gate * not exist.
35780Sstevel@tonic-gate */
35790Sstevel@tonic-gate phm = PAGE_HASH_MUTEX(index);
35800Sstevel@tonic-gate mutex_enter(phm);
35810Sstevel@tonic-gate PAGE_HASH_SEARCH(index, pp, vp, off);
35820Sstevel@tonic-gate if (pp == NULL) {
35830Sstevel@tonic-gate VM_STAT_ADD(page_create_new);
35840Sstevel@tonic-gate pp = npp;
35850Sstevel@tonic-gate npp = NULL;
35860Sstevel@tonic-gate if (!page_hashin(pp, vp, off, phm)) {
35870Sstevel@tonic-gate /*
35880Sstevel@tonic-gate * Since we hold the page hash mutex and
35890Sstevel@tonic-gate * just searched for this page, page_hashin
35900Sstevel@tonic-gate * had better not fail. If it does, that
35910Sstevel@tonic-gate * means somethread did not follow the
35920Sstevel@tonic-gate * page hash mutex rules. Panic now and
35930Sstevel@tonic-gate * get it over with. As usual, go down
35940Sstevel@tonic-gate * holding all the locks.
35950Sstevel@tonic-gate */
35960Sstevel@tonic-gate ASSERT(MUTEX_HELD(phm));
35970Sstevel@tonic-gate panic("page_create: hashin fail %p %p %llx %p",
35980Sstevel@tonic-gate (void *)pp, (void *)vp, off, (void *)phm);
35990Sstevel@tonic-gate
36000Sstevel@tonic-gate }
36010Sstevel@tonic-gate ASSERT(MUTEX_HELD(phm));
36020Sstevel@tonic-gate mutex_exit(phm);
36030Sstevel@tonic-gate phm = NULL;
36040Sstevel@tonic-gate
36050Sstevel@tonic-gate /*
36060Sstevel@tonic-gate * Hat layer locking need not be done to set
36070Sstevel@tonic-gate * the following bits since the page is not hashed
36080Sstevel@tonic-gate * and was on the free list (i.e., had no mappings).
36090Sstevel@tonic-gate *
36100Sstevel@tonic-gate * Set the reference bit to protect
36110Sstevel@tonic-gate * against immediate pageout
36120Sstevel@tonic-gate *
36130Sstevel@tonic-gate * XXXmh modify freelist code to set reference
36140Sstevel@tonic-gate * bit so we don't have to do it here.
36150Sstevel@tonic-gate */
36160Sstevel@tonic-gate page_set_props(pp, P_REF);
36170Sstevel@tonic-gate } else {
36180Sstevel@tonic-gate ASSERT(MUTEX_HELD(phm));
36190Sstevel@tonic-gate mutex_exit(phm);
36200Sstevel@tonic-gate phm = NULL;
36210Sstevel@tonic-gate /*
36220Sstevel@tonic-gate * NOTE: This should not happen for pages associated
36230Sstevel@tonic-gate * with kernel vnode 'kvp'.
36240Sstevel@tonic-gate */
36250Sstevel@tonic-gate /* XX64 - to debug why this happens! */
36263290Sjohansen ASSERT(!VN_ISKAS(vp));
36273290Sjohansen if (VN_ISKAS(vp))
36280Sstevel@tonic-gate cmn_err(CE_NOTE,
36290Sstevel@tonic-gate "page_create: page not expected "
36300Sstevel@tonic-gate "in hash list for kernel vnode - pp 0x%p",
36310Sstevel@tonic-gate (void *)pp);
36320Sstevel@tonic-gate VM_STAT_ADD(page_create_exists);
36330Sstevel@tonic-gate goto fail;
36340Sstevel@tonic-gate }
36350Sstevel@tonic-gate
36360Sstevel@tonic-gate /*
36370Sstevel@tonic-gate * Got a page! It is locked. Acquire the i/o
36380Sstevel@tonic-gate * lock since we are going to use the p_next and
36390Sstevel@tonic-gate * p_prev fields to link the requested pages together.
36400Sstevel@tonic-gate */
36410Sstevel@tonic-gate page_io_lock(pp);
36420Sstevel@tonic-gate page_add(&plist, pp);
36430Sstevel@tonic-gate plist = plist->p_next;
36440Sstevel@tonic-gate off += MMU_PAGESIZE;
36450Sstevel@tonic-gate vaddr += MMU_PAGESIZE;
36460Sstevel@tonic-gate }
36470Sstevel@tonic-gate
36480Sstevel@tonic-gate check_dma(mattr, plist, pages_req);
36490Sstevel@tonic-gate return (plist);
36500Sstevel@tonic-gate
36510Sstevel@tonic-gate fail:
36520Sstevel@tonic-gate if (npp != NULL) {
36530Sstevel@tonic-gate /*
36540Sstevel@tonic-gate * Did not need this page after all.
36550Sstevel@tonic-gate * Put it back on the free list.
36560Sstevel@tonic-gate */
36570Sstevel@tonic-gate VM_STAT_ADD(page_create_putbacks);
36580Sstevel@tonic-gate PP_SETFREE(npp);
36590Sstevel@tonic-gate PP_SETAGED(npp);
36600Sstevel@tonic-gate npp->p_offset = (u_offset_t)-1;
36610Sstevel@tonic-gate page_list_add(npp, PG_FREE_LIST | PG_LIST_TAIL);
36620Sstevel@tonic-gate page_unlock(npp);
36630Sstevel@tonic-gate }
36640Sstevel@tonic-gate
36650Sstevel@tonic-gate /*
36660Sstevel@tonic-gate * Give up the pages we already got.
36670Sstevel@tonic-gate */
36680Sstevel@tonic-gate while (plist != NULL) {
36690Sstevel@tonic-gate pp = plist;
36700Sstevel@tonic-gate page_sub(&plist, pp);
36710Sstevel@tonic-gate page_io_unlock(pp);
36720Sstevel@tonic-gate plist_len++;
36730Sstevel@tonic-gate /*LINTED: constant in conditional ctx*/
36740Sstevel@tonic-gate VN_DISPOSE(pp, B_INVAL, 0, kcred);
36750Sstevel@tonic-gate }
36760Sstevel@tonic-gate
36770Sstevel@tonic-gate /*
36780Sstevel@tonic-gate * VN_DISPOSE does freemem accounting for the pages in plist
36790Sstevel@tonic-gate * by calling page_free. So, we need to undo the pcf accounting
36800Sstevel@tonic-gate * for only the remaining pages.
36810Sstevel@tonic-gate */
36820Sstevel@tonic-gate VM_STAT_ADD(page_create_putbacks);
36830Sstevel@tonic-gate page_create_putback(pages_req - plist_len);
36840Sstevel@tonic-gate
36850Sstevel@tonic-gate return (NULL);
36860Sstevel@tonic-gate }
36875084Sjohnlev #endif /* !__xpv */
36880Sstevel@tonic-gate
36890Sstevel@tonic-gate
36900Sstevel@tonic-gate /*
36910Sstevel@tonic-gate * Copy the data from the physical page represented by "frompp" to
36920Sstevel@tonic-gate * that represented by "topp". ppcopy uses CPU->cpu_caddr1 and
36930Sstevel@tonic-gate * CPU->cpu_caddr2. It assumes that no one uses either map at interrupt
36940Sstevel@tonic-gate * level and no one sleeps with an active mapping there.
36950Sstevel@tonic-gate *
36960Sstevel@tonic-gate * Note that the ref/mod bits in the page_t's are not affected by
36970Sstevel@tonic-gate * this operation, hence it is up to the caller to update them appropriately.
36980Sstevel@tonic-gate */
36993253Smec int
ppcopy(page_t * frompp,page_t * topp)37000Sstevel@tonic-gate ppcopy(page_t *frompp, page_t *topp)
37010Sstevel@tonic-gate {
37020Sstevel@tonic-gate caddr_t pp_addr1;
37030Sstevel@tonic-gate caddr_t pp_addr2;
37043446Smrj hat_mempte_t pte1;
37053446Smrj hat_mempte_t pte2;
37060Sstevel@tonic-gate kmutex_t *ppaddr_mutex;
37073253Smec label_t ljb;
37083253Smec int ret = 1;
37090Sstevel@tonic-gate
37100Sstevel@tonic-gate ASSERT_STACK_ALIGNED();
37110Sstevel@tonic-gate ASSERT(PAGE_LOCKED(frompp));
37120Sstevel@tonic-gate ASSERT(PAGE_LOCKED(topp));
37130Sstevel@tonic-gate
37140Sstevel@tonic-gate if (kpm_enable) {
37150Sstevel@tonic-gate pp_addr1 = hat_kpm_page2va(frompp, 0);
37160Sstevel@tonic-gate pp_addr2 = hat_kpm_page2va(topp, 0);
37170Sstevel@tonic-gate kpreempt_disable();
37180Sstevel@tonic-gate } else {
37190Sstevel@tonic-gate /*
37200Sstevel@tonic-gate * disable pre-emption so that CPU can't change
37210Sstevel@tonic-gate */
37220Sstevel@tonic-gate kpreempt_disable();
37230Sstevel@tonic-gate
37240Sstevel@tonic-gate pp_addr1 = CPU->cpu_caddr1;
37250Sstevel@tonic-gate pp_addr2 = CPU->cpu_caddr2;
37263446Smrj pte1 = CPU->cpu_caddr1pte;
37273446Smrj pte2 = CPU->cpu_caddr2pte;
37280Sstevel@tonic-gate
37290Sstevel@tonic-gate ppaddr_mutex = &CPU->cpu_ppaddr_mutex;
37300Sstevel@tonic-gate mutex_enter(ppaddr_mutex);
37310Sstevel@tonic-gate
37320Sstevel@tonic-gate hat_mempte_remap(page_pptonum(frompp), pp_addr1, pte1,
37330Sstevel@tonic-gate PROT_READ | HAT_STORECACHING_OK, HAT_LOAD_NOCONSIST);
37340Sstevel@tonic-gate hat_mempte_remap(page_pptonum(topp), pp_addr2, pte2,
37350Sstevel@tonic-gate PROT_READ | PROT_WRITE | HAT_STORECACHING_OK,
37360Sstevel@tonic-gate HAT_LOAD_NOCONSIST);
37370Sstevel@tonic-gate }
37380Sstevel@tonic-gate
37393253Smec if (on_fault(&ljb)) {
37403253Smec ret = 0;
37413253Smec goto faulted;
37423253Smec }
37430Sstevel@tonic-gate if (use_sse_pagecopy)
37445084Sjohnlev #ifdef __xpv
37455084Sjohnlev page_copy_no_xmm(pp_addr2, pp_addr1);
37465084Sjohnlev #else
37470Sstevel@tonic-gate hwblkpagecopy(pp_addr1, pp_addr2);
37485084Sjohnlev #endif
37490Sstevel@tonic-gate else
37500Sstevel@tonic-gate bcopy(pp_addr1, pp_addr2, PAGESIZE);
37510Sstevel@tonic-gate
37523253Smec no_fault();
37533253Smec faulted:
37543446Smrj if (!kpm_enable) {
37555084Sjohnlev #ifdef __xpv
37565084Sjohnlev /*
37575217Sjosephb * We can't leave unused mappings laying about under the
37585217Sjosephb * hypervisor, so blow them away.
37595084Sjohnlev */
37605217Sjosephb if (HYPERVISOR_update_va_mapping((uintptr_t)pp_addr1, 0,
37615217Sjosephb UVMF_INVLPG | UVMF_LOCAL) < 0)
37625217Sjosephb panic("HYPERVISOR_update_va_mapping() failed");
37635084Sjohnlev if (HYPERVISOR_update_va_mapping((uintptr_t)pp_addr2, 0,
37645084Sjohnlev UVMF_INVLPG | UVMF_LOCAL) < 0)
37655084Sjohnlev panic("HYPERVISOR_update_va_mapping() failed");
37665084Sjohnlev #endif
37670Sstevel@tonic-gate mutex_exit(ppaddr_mutex);
37683446Smrj }
37690Sstevel@tonic-gate kpreempt_enable();
37703253Smec return (ret);
37710Sstevel@tonic-gate }
37720Sstevel@tonic-gate
37735262Srscott void
pagezero(page_t * pp,uint_t off,uint_t len)37745262Srscott pagezero(page_t *pp, uint_t off, uint_t len)
37755262Srscott {
37765262Srscott ASSERT(PAGE_LOCKED(pp));
37775262Srscott pfnzero(page_pptonum(pp), off, len);
37785262Srscott }
37795262Srscott
37800Sstevel@tonic-gate /*
37815262Srscott * Zero the physical page from off to off + len given by pfn
37820Sstevel@tonic-gate * without changing the reference and modified bits of page.
37830Sstevel@tonic-gate *
37840Sstevel@tonic-gate * We use this using CPU private page address #2, see ppcopy() for more info.
37855262Srscott * pfnzero() must not be called at interrupt level.
37860Sstevel@tonic-gate */
37870Sstevel@tonic-gate void
pfnzero(pfn_t pfn,uint_t off,uint_t len)37885262Srscott pfnzero(pfn_t pfn, uint_t off, uint_t len)
37890Sstevel@tonic-gate {
37900Sstevel@tonic-gate caddr_t pp_addr2;
37913446Smrj hat_mempte_t pte2;
37925262Srscott kmutex_t *ppaddr_mutex = NULL;
37930Sstevel@tonic-gate
37940Sstevel@tonic-gate ASSERT_STACK_ALIGNED();
37950Sstevel@tonic-gate ASSERT(len <= MMU_PAGESIZE);
37960Sstevel@tonic-gate ASSERT(off <= MMU_PAGESIZE);
37970Sstevel@tonic-gate ASSERT(off + len <= MMU_PAGESIZE);
37985262Srscott
37995262Srscott if (kpm_enable && !pfn_is_foreign(pfn)) {
38005262Srscott pp_addr2 = hat_kpm_pfn2va(pfn);
38010Sstevel@tonic-gate kpreempt_disable();
38020Sstevel@tonic-gate } else {
38030Sstevel@tonic-gate kpreempt_disable();
38040Sstevel@tonic-gate
38050Sstevel@tonic-gate pp_addr2 = CPU->cpu_caddr2;
38063446Smrj pte2 = CPU->cpu_caddr2pte;
38070Sstevel@tonic-gate
38080Sstevel@tonic-gate ppaddr_mutex = &CPU->cpu_ppaddr_mutex;
38090Sstevel@tonic-gate mutex_enter(ppaddr_mutex);
38100Sstevel@tonic-gate
38115262Srscott hat_mempte_remap(pfn, pp_addr2, pte2,
38120Sstevel@tonic-gate PROT_READ | PROT_WRITE | HAT_STORECACHING_OK,
38130Sstevel@tonic-gate HAT_LOAD_NOCONSIST);
38140Sstevel@tonic-gate }
38150Sstevel@tonic-gate
38163446Smrj if (use_sse_pagezero) {
38175084Sjohnlev #ifdef __xpv
38185084Sjohnlev uint_t rem;
38195084Sjohnlev
38205084Sjohnlev /*
38215084Sjohnlev * zero a byte at a time until properly aligned for
38225084Sjohnlev * block_zero_no_xmm().
38235084Sjohnlev */
38245084Sjohnlev while (!P2NPHASE(off, ((uint_t)BLOCKZEROALIGN)) && len-- > 0)
38255084Sjohnlev pp_addr2[off++] = 0;
38265084Sjohnlev
38275084Sjohnlev /*
38285084Sjohnlev * Now use faster block_zero_no_xmm() for any range
38295084Sjohnlev * that is properly aligned and sized.
38305084Sjohnlev */
38315084Sjohnlev rem = P2PHASE(len, ((uint_t)BLOCKZEROALIGN));
38325084Sjohnlev len -= rem;
38335084Sjohnlev if (len != 0) {
38345084Sjohnlev block_zero_no_xmm(pp_addr2 + off, len);
38355084Sjohnlev off += len;
38365084Sjohnlev }
38375084Sjohnlev
38385084Sjohnlev /*
38395084Sjohnlev * zero remainder with byte stores.
38405084Sjohnlev */
38415084Sjohnlev while (rem-- > 0)
38425084Sjohnlev pp_addr2[off++] = 0;
38435084Sjohnlev #else
38440Sstevel@tonic-gate hwblkclr(pp_addr2 + off, len);
38455084Sjohnlev #endif
38463446Smrj } else {
38470Sstevel@tonic-gate bzero(pp_addr2 + off, len);
38483446Smrj }
38490Sstevel@tonic-gate
38505262Srscott if (!kpm_enable || pfn_is_foreign(pfn)) {
38515084Sjohnlev #ifdef __xpv
38525262Srscott /*
38535262Srscott * On the hypervisor this page might get used for a page
38545262Srscott * table before any intervening change to this mapping,
38555262Srscott * so blow it away.
38565262Srscott */
38575262Srscott if (HYPERVISOR_update_va_mapping((uintptr_t)pp_addr2, 0,
38585262Srscott UVMF_INVLPG) < 0)
38595262Srscott panic("HYPERVISOR_update_va_mapping() failed");
38605084Sjohnlev #endif
38610Sstevel@tonic-gate mutex_exit(ppaddr_mutex);
38625262Srscott }
38635262Srscott
38640Sstevel@tonic-gate kpreempt_enable();
38650Sstevel@tonic-gate }
38660Sstevel@tonic-gate
38670Sstevel@tonic-gate /*
38680Sstevel@tonic-gate * Platform-dependent page scrub call.
38690Sstevel@tonic-gate */
38700Sstevel@tonic-gate void
pagescrub(page_t * pp,uint_t off,uint_t len)38710Sstevel@tonic-gate pagescrub(page_t *pp, uint_t off, uint_t len)
38720Sstevel@tonic-gate {
38730Sstevel@tonic-gate /*
38740Sstevel@tonic-gate * For now, we rely on the fact that pagezero() will
38750Sstevel@tonic-gate * always clear UEs.
38760Sstevel@tonic-gate */
38770Sstevel@tonic-gate pagezero(pp, off, len);
38780Sstevel@tonic-gate }
38790Sstevel@tonic-gate
38800Sstevel@tonic-gate /*
38810Sstevel@tonic-gate * set up two private addresses for use on a given CPU for use in ppcopy()
38820Sstevel@tonic-gate */
38830Sstevel@tonic-gate void
setup_vaddr_for_ppcopy(struct cpu * cpup)38840Sstevel@tonic-gate setup_vaddr_for_ppcopy(struct cpu *cpup)
38850Sstevel@tonic-gate {
38860Sstevel@tonic-gate void *addr;
38873446Smrj hat_mempte_t pte_pa;
38880Sstevel@tonic-gate
38890Sstevel@tonic-gate addr = vmem_alloc(heap_arena, mmu_ptob(1), VM_SLEEP);
38903446Smrj pte_pa = hat_mempte_setup(addr);
38910Sstevel@tonic-gate cpup->cpu_caddr1 = addr;
38923446Smrj cpup->cpu_caddr1pte = pte_pa;
38930Sstevel@tonic-gate
38940Sstevel@tonic-gate addr = vmem_alloc(heap_arena, mmu_ptob(1), VM_SLEEP);
38953446Smrj pte_pa = hat_mempte_setup(addr);
38960Sstevel@tonic-gate cpup->cpu_caddr2 = addr;
38973446Smrj cpup->cpu_caddr2pte = pte_pa;
38980Sstevel@tonic-gate
38990Sstevel@tonic-gate mutex_init(&cpup->cpu_ppaddr_mutex, NULL, MUTEX_DEFAULT, NULL);
39000Sstevel@tonic-gate }
39010Sstevel@tonic-gate
39023446Smrj /*
39033446Smrj * Undo setup_vaddr_for_ppcopy
39043446Smrj */
39053446Smrj void
teardown_vaddr_for_ppcopy(struct cpu * cpup)39063446Smrj teardown_vaddr_for_ppcopy(struct cpu *cpup)
39073446Smrj {
39083446Smrj mutex_destroy(&cpup->cpu_ppaddr_mutex);
39093446Smrj
39103446Smrj hat_mempte_release(cpup->cpu_caddr2, cpup->cpu_caddr2pte);
39113446Smrj cpup->cpu_caddr2pte = 0;
39123446Smrj vmem_free(heap_arena, cpup->cpu_caddr2, mmu_ptob(1));
39133446Smrj cpup->cpu_caddr2 = 0;
39143446Smrj
39153446Smrj hat_mempte_release(cpup->cpu_caddr1, cpup->cpu_caddr1pte);
39163446Smrj cpup->cpu_caddr1pte = 0;
39173446Smrj vmem_free(heap_arena, cpup->cpu_caddr1, mmu_ptob(1));
39183446Smrj cpup->cpu_caddr1 = 0;
39193446Smrj }
39200Sstevel@tonic-gate
39210Sstevel@tonic-gate /*
39220Sstevel@tonic-gate * Function for flushing D-cache when performing module relocations
39230Sstevel@tonic-gate * to an alternate mapping. Unnecessary on Intel / AMD platforms.
39240Sstevel@tonic-gate */
39250Sstevel@tonic-gate void
dcache_flushall()39260Sstevel@tonic-gate dcache_flushall()
39270Sstevel@tonic-gate {}
39283177Sdp78419
39293177Sdp78419 size_t
exec_get_spslew(void)39303177Sdp78419 exec_get_spslew(void)
39313177Sdp78419 {
39323177Sdp78419 return (0);
39333177Sdp78419 }
39343446Smrj
39353446Smrj /*
39363446Smrj * Allocate a memory page. The argument 'seed' can be any pseudo-random
39373446Smrj * number to vary where the pages come from. This is quite a hacked up
39383446Smrj * method -- it works for now, but really needs to be fixed up a bit.
39393446Smrj *
39403446Smrj * We currently use page_create_va() on the kvp with fake offsets,
39413446Smrj * segments and virt address. This is pretty bogus, but was copied from the
39423446Smrj * old hat_i86.c code. A better approach would be to specify either mnode
39433446Smrj * random or mnode local and takes a page from whatever color has the MOST
39443446Smrj * available - this would have a minimal impact on page coloring.
39453446Smrj */
39463446Smrj page_t *
page_get_physical(uintptr_t seed)39479062SVikram.Hegde@Sun.COM page_get_physical(uintptr_t seed)
39483446Smrj {
39493446Smrj page_t *pp;
39509062SVikram.Hegde@Sun.COM u_offset_t offset;
39513446Smrj static struct seg tmpseg;
39523446Smrj static uintptr_t ctr = 0;
39533446Smrj
39543446Smrj /*
39553446Smrj * This code is gross, we really need a simpler page allocator.
39563446Smrj *
39579062SVikram.Hegde@Sun.COM * We need to assign an offset for the page to call page_create_va()
39583446Smrj * To avoid conflicts with other pages, we get creative with the offset.
39597589SVikram.Hegde@Sun.COM * For 32 bits, we need an offset > 4Gig
39607589SVikram.Hegde@Sun.COM * For 64 bits, need an offset somewhere in the VA hole.
39613446Smrj */
39629062SVikram.Hegde@Sun.COM offset = seed;
39639062SVikram.Hegde@Sun.COM if (offset > kernelbase)
39649062SVikram.Hegde@Sun.COM offset -= kernelbase;
39659062SVikram.Hegde@Sun.COM offset <<= MMU_PAGESHIFT;
39669062SVikram.Hegde@Sun.COM #if defined(__amd64)
39679062SVikram.Hegde@Sun.COM offset += mmu.hole_start; /* something in VA hole */
39689062SVikram.Hegde@Sun.COM #else
39699062SVikram.Hegde@Sun.COM offset += 1ULL << 40; /* something > 4 Gig */
39709062SVikram.Hegde@Sun.COM #endif
39719062SVikram.Hegde@Sun.COM
39729062SVikram.Hegde@Sun.COM if (page_resv(1, KM_NOSLEEP) == 0)
39733446Smrj return (NULL);
39743446Smrj
39753446Smrj #ifdef DEBUG
39763446Smrj pp = page_exists(&kvp, offset);
39773446Smrj if (pp != NULL)
39787240Srh87107 panic("page already exists %p", (void *)pp);
39793446Smrj #endif
39803446Smrj
39815084Sjohnlev pp = page_create_va(&kvp, offset, MMU_PAGESIZE, PG_EXCL,
39823446Smrj &tmpseg, (caddr_t)(ctr += MMU_PAGESIZE)); /* changing VA usage */
39837589SVikram.Hegde@Sun.COM if (pp != NULL) {
39847589SVikram.Hegde@Sun.COM page_io_unlock(pp);
39858198SVikram.Hegde@Sun.COM page_downgrade(pp);
39867589SVikram.Hegde@Sun.COM }
39873446Smrj return (pp);
39883446Smrj }
3989