/*-
* Copyright (c) 1997, 2019 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
* NASA Ames Research Center, and by Andrew Doran.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* uvm_pglistalloc: allocate a list of pages
*
* => allocated pages are placed onto an rlist. rlist is
* initialized by uvm_pglistalloc.
* => returns 0 on success or errno on failure
* => implementation allocates a single segment if any constraints are
* imposed by call arguments.
* => doesn't take into account clean non-busy pages on inactive list
* that could be used(?)
* => params:
* size the size of the allocation, rounded to page size.
* low the low address of the allowed allocation range.
* high the high address of the allowed allocation range.
* alignment memory must be aligned to this power-of-two boundary.
* boundary no segment in the allocation may cross this
* power-of-two boundary (relative to zero).
*/
static int
uvm_pglistalloc_c_ps(uvm_physseg_t psi, int num, paddr_t low, paddr_t high,
paddr_t alignment, paddr_t boundary, struct pglist *rlist)
{
long candidate, limit, candidateidx, end, idx;
int skip;
long pagemask;
bool second_pass;
#ifdef DEBUG
paddr_t idxpa, lastidxpa;
paddr_t cidx = 0; /* XXX: GCC */
#endif
#ifdef PGALLOC_VERBOSE
printf("pgalloc: contig %d pgs from psi %d\n", num, psi);
#endif
low = atop(low);
high = atop(high);
/*
* Make sure that physseg falls within with range to be allocated from.
*/
if (high <= uvm_physseg_get_avail_start(psi) ||
low >= uvm_physseg_get_avail_end(psi))
return -1;
/*
* We start our search at the just after where the last allocation
* succeeded.
*/
alignment = atop(alignment);
candidate = roundup2(ulmax(low, uvm_physseg_get_avail_start(psi) +
uvm_physseg_get_start_hint(psi)), alignment);
limit = ulmin(high, uvm_physseg_get_avail_end(psi));
pagemask = ~((boundary >> PAGE_SHIFT) - 1);
skip = 0;
second_pass = false;
for (;;) {
bool ok = true;
signed int cnt;
if (candidate + num > limit) {
if (uvm_physseg_get_start_hint(psi) == 0 || second_pass) {
/*
* We've run past the allowable range.
*/
return 0; /* FAIL = 0 pages*/
}
/*
* We've wrapped around the end of this segment
* so restart at the beginning but now our limit
* is were we started.
*/
second_pass = true;
candidate = roundup2(ulmax(low, uvm_physseg_get_avail_start(psi)), alignment);
limit = ulmin(limit, uvm_physseg_get_avail_start(psi) +
uvm_physseg_get_start_hint(psi));
skip = 0;
continue;
}
if (boundary != 0 &&
((candidate ^ (candidate + num - 1)) & pagemask) != 0) {
/*
* Region crosses boundary. Jump to the boundary
* just crossed and ensure alignment.
*/
candidate = (candidate + num - 1) & pagemask;
candidate = roundup2(candidate, alignment);
skip = 0;
continue;
}
#ifdef DEBUG
/*
* Make sure this is a managed physical page.
*/
if (uvm_physseg_find(candidate, &cidx) != psi)
panic("pgalloc contig: botch1");
if (cidx != candidate - uvm_physseg_get_start(psi))
panic("pgalloc contig: botch2");
if (uvm_physseg_find(candidate + num - 1, &cidx) != psi)
panic("pgalloc contig: botch3");
if (cidx != candidate - uvm_physseg_get_start(psi) + num - 1)
panic("pgalloc contig: botch4");
#endif
candidateidx = candidate - uvm_physseg_get_start(psi);
end = candidateidx + num;
/*
* Found a suitable starting page. See if the range is free.
*/
#ifdef PGALLOC_VERBOSE
printf("%s: psi=%d candidate=%#lx end=%#lx skip=%#x, align=%#"PRIxPADDR,
__func__, psi, candidateidx, end, skip, alignment);
#endif
/*
* We start at the end and work backwards since if we find a
* non-free page, it makes no sense to continue.
*
* But on the plus size we have "vetted" some number of free
* pages. If this iteration fails, we may be able to skip
* testing most of those pages again in the next pass.
*/
for (idx = end - 1; idx >= candidateidx + skip; idx--) {
if (VM_PAGE_IS_FREE(uvm_physseg_get_pg(psi, idx)) == 0) {
ok = false;
break;
}
if (ok) {
while (skip-- > 0) {
KDASSERT(VM_PAGE_IS_FREE(uvm_physseg_get_pg(psi, candidateidx + skip)));
}
#ifdef PGALLOC_VERBOSE
printf(": ok\n");
#endif
break;
}
#ifdef PGALLOC_VERBOSE
printf(": non-free at %#x\n", idx - candidateidx);
#endif
/*
* count the number of pages we can advance
* since we know they aren't all free.
*/
cnt = idx + 1 - candidateidx;
/*
* now round up that to the needed alignment.
*/
cnt = roundup2(cnt, alignment);
/*
* The number of pages we can skip checking
* (might be 0 if cnt > num).
*/
skip = uimax(num - cnt, 0);
candidate += cnt;
}
/*
* we have a chunk of memory that conforms to the requested constraints.
*/
for (idx = candidateidx; idx < end; idx++)
uvm_pglist_add(uvm_physseg_get_pg(psi, idx), rlist);
/*
* the next time we need to search this segment, start after this
* chunk of pages we just allocated.
*/
uvm_physseg_set_start_hint(psi, candidate + num -
uvm_physseg_get_avail_start(psi));
KASSERTMSG(uvm_physseg_get_start_hint(psi) <=
uvm_physseg_get_avail_end(psi) - uvm_physseg_get_avail_start(psi),
"%lx %lu (%#lx) <= %#"PRIxPADDR" - %#"PRIxPADDR" (%#"PRIxPADDR")",
candidate + num,
uvm_physseg_get_start_hint(psi), uvm_physseg_get_start_hint(psi),
uvm_physseg_get_avail_end(psi), uvm_physseg_get_avail_start(psi),
uvm_physseg_get_avail_end(psi) - uvm_physseg_get_avail_start(psi));
#ifdef PGALLOC_VERBOSE
printf("got %d pgs\n", num);
#endif
return num; /* number of pages allocated */
}
/*
* Allocate pages the normal way and for each new page, check if
* the page completes a range satisfying the request.
* The pagedaemon will evict pages as we go and we are very likely
* to get compatible pages eventually.
*/
/*
* Look backward for at most num - 1 pages, back to
* the highest of:
* - the first page in the physseg
* - the specified low address
* - num-1 pages before the one we just allocated
* - the start of the boundary range containing pa
* all rounded up to alignment.
*/
/*
* Make sure that physseg falls within with range to be allocated from.
*/
if (high <= uvm_physseg_get_avail_start(psi) ||
low >= uvm_physseg_get_avail_end(psi))
return -1;
uvm_pglist_add(pg, rlist);
if (--todo == 0) {
break;
}
}
/*
* The next time we need to search this segment,
* start just after the pages we just allocated.
*/
uvm_physseg_set_start_hint(psi, candidate + 1 - uvm_physseg_get_avail_start(psi));
KASSERTMSG(uvm_physseg_get_start_hint(psi) <= uvm_physseg_get_avail_end(psi) -
uvm_physseg_get_avail_start(psi),
"%#lx %lu (%#lx) <= %#"PRIxPADDR" - %#"PRIxPADDR" (%#"PRIxPADDR")",
candidate + 1,
uvm_physseg_get_start_hint(psi),
uvm_physseg_get_start_hint(psi),
uvm_physseg_get_avail_end(psi),
uvm_physseg_get_avail_start(psi),
uvm_physseg_get_avail_end(psi) - uvm_physseg_get_avail_start(psi));
#ifdef PGALLOC_VERBOSE
printf("got %d pgs\n", num - todo);
#endif
return (num - todo); /* number of pages allocated */
}
static int
uvm_pglistalloc_simple(int num, paddr_t low, paddr_t high,
struct pglist *rlist, int waitok)
{
int fl, error;
uvm_physseg_t psi;
int count = 0;