/* $NetBSD: loadfile_machdep.c,v 1.17 2022/04/29 20:24:02 rin Exp $ */
/*-
* Copyright (c) 2005 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This work is based on the code contributed by Robert Drehmel to the
* FreeBSD project.
*
* 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.
*/
/*
* Check if a memory region is already mapped. Return length and virtual
* address of unmapped sub-region, if any.
*/
static uint64_t
kvamap_extract(vaddr_t va, vsize_t len, vaddr_t *new_va)
{
int i;
*new_va = va;
for (i = 0; (len > 0) && (i < MAXSEGNUM); i++) {
if (kvamap[i].start == 0)
break;
if ((kvamap[i].start <= va) && (va < kvamap[i].end)) {
uint64_t va_len = kvamap[i].end - va;
len = (va_len < len) ? len - va_len : 0;
*new_va = kvamap[i].end;
}
}
return len;
}
/*
* Record new kernel mapping.
*/
static void
kvamap_enter(uint64_t va, uint64_t len)
{
int i;
DPRINTF(("kvamap_enter: %d@%p\n", (int)len, (void*)(u_long)va));
for (i = 0; (len > 0) && (i < MAXSEGNUM); i++) {
if (kvamap[i].start == 0) {
kvamap[i].start = va;
kvamap[i].end = va + len;
break;
}
}
if (i == MAXSEGNUM) {
panic("Too many allocations requested.");
}
}
hv_mach_desc((paddr_t)NULL, &len); /* Trick to get actual length */
if ( !len ) {
panic("init_tlb: hv_mach_desc() failed");
}
pa = OF_alloc_phys(len, 16);
if ( pa == -1 ) {
panic("OF_alloc_phys() failed");
}
hv_rc = hv_mach_desc(pa, &len);
if (hv_rc != H_EOK) {
panic("hv_mach_desc() failed");
}
/* XXX dig out TLB node info - 64 is ok for loading the kernel */
dtlb_slot_max = itlb_slot_max = 64;
}
#endif
/*
* Map requested memory region with permanent 4MB pages.
*/
static int
mmu_mapin(vaddr_t rva, vsize_t len)
{
len = roundup2(len + (rva & PAGE_MASK_4M), PAGE_SIZE_4M);
rva &= ~PAGE_MASK_4M;
/*
* Map requested memory region with permanent 4MB pages - sun4u.
*/
static int
mmu_mapin_sun4u(vaddr_t rva, vsize_t len)
{
uint64_t data;
paddr_t pa;
vaddr_t va, mva;
for (pa = (paddr_t)-1; len > 0; rva = va) {
if ( (len = kvamap_extract(rva, len, &va)) == 0) {
/* The rest is already mapped */
break;
}
if (dtlb_va_to_pa(va) == (u_long)-1 ||
itlb_va_to_pa(va) == (u_long)-1) {
/* Allocate a physical page, claim the virtual area */
if (pa == (paddr_t)-1) {
pa = OF_alloc_phys(PAGE_SIZE_4M, PAGE_SIZE_4M);
if (pa == (paddr_t)-1)
panic("out of memory");
mva = OF_claim_virt(va, PAGE_SIZE_4M);
if (mva != va) {
panic("can't claim virtual page "
"(wanted %#lx, got %#lx)",
va, mva);
}
/* The mappings may have changed, be paranoid. */
continue;
}
/*
* Actually, we can only allocate two pages less at
* most (depending on the kernel TSB size).
*/
if (dtlb_slot >= dtlb_slot_max)
panic("mmu_mapin: out of dtlb_slots");
if (itlb_slot >= itlb_slot_max)
panic("mmu_mapin: out of itlb_slots");
DPRINTF(("mmu_mapin: 0x%lx:0x%x.0x%x\n", va,
hi(pa), lo(pa)));
len -= len > PAGE_SIZE_4M ? PAGE_SIZE_4M : len;
va += PAGE_SIZE_4M;
}
if (pa != (paddr_t)-1) {
OF_free_phys(pa, PAGE_SIZE_4M);
}
return (0);
}
#ifdef SUN4V
/*
* Map requested memory region with permanent 4MB pages - sun4v.
*/
static int
mmu_mapin_sun4v(vaddr_t rva, vsize_t len)
{
uint64_t data;
paddr_t pa;
vaddr_t va, mva;
int64_t hv_rc;
for (pa = (paddr_t)-1; len > 0; rva = va) {
if ( (len = kvamap_extract(rva, len, &va)) == 0) {
/* The rest is already mapped */
break;
}
/* Allocate a physical page, claim the virtual area */
if (pa == (paddr_t)-1) {
pa = OF_alloc_phys(PAGE_SIZE_4M, PAGE_SIZE_4M);
if (pa == (paddr_t)-1)
panic("out of memory");
mva = OF_claim_virt(va, PAGE_SIZE_4M);
if (mva != va) {
panic("can't claim virtual page "
"(wanted %#lx, got %#lx)",
va, mva);
}
}
/*
* Actually, we can only allocate two pages less at
* most (depending on the kernel TSB size).
*/
if (dtlb_slot >= dtlb_slot_max)
panic("mmu_mapin: out of dtlb_slots");
if (itlb_slot >= itlb_slot_max)
panic("mmu_mapin: out of itlb_slots");
DPRINTF(("mmu_mapin: 0x%lx:0x%x.0x%x\n", va,
hi(pa), lo(pa)));
dtlb_slot = itlb_slot = 0;
for (i = 0; i < MAXSEGNUM; i++) {
/* XXX return all mappings to PROM and unmap the pages! */
kvamap[i].start = kvamap[i].end = 0;
}
}
/*
* Claim requested memory region in OpenFirmware allocation pool.
*/
static int
ofw_mapin(vaddr_t rva, vsize_t len)
{
vaddr_t va;
dtlb_slot = itlb_slot = 0;
for (i = 0; i < MAXSEGNUM; i++) {
OF_release((void*)(u_long)kvamap[i].start,
(u_int)(kvamap[i].end - kvamap[i].start));
kvamap[i].start = kvamap[i].end = 0;
}
}
/*
* NOP implementation exists solely for kernel header loading sake. Here
* we use alloc() interface to allocate memory and avoid doing some dangerous
* things.
*/
static ssize_t
nop_read(int f, void *addr, size_t size)
{
return read(f, addr, size);
}
static void*
nop_memcpy(void *dst, const void *src, size_t size)
{
/*
* Real NOP to make LOAD_HDR work: loadfile_elfXX copies ELF headers
* right after the highest kernel address which will not be mapped with
* nop_XXX operations.
*/
return (dst);
}
/*
* Remove write permissions from text mappings in the dTLB.
* Add entries in the iTLB.
*/
void
sparc64_finalize_tlb(u_long data_va)
{
#ifdef SUN4V
if ( sun4v )
sparc64_finalize_tlb_sun4v(data_va);
else
#endif
sparc64_finalize_tlb_sun4u(data_va);
}
/*
* Remove write permissions from text mappings in the dTLB - sun4u.
* Add entries in the iTLB.
*/
void
sparc64_finalize_tlb_sun4u(u_long data_va)
{
int i;
int64_t data;
bool writable_text = false;
for (i = 0; i < dtlb_slot; i++) {
if (dtlb_store[i].te_va >= data_va) {
/*
* If (for whatever reason) the start of the
* writable section is right at the start of
* the kernel, we need to map it into the ITLB
* nevertheless (and don't make it readonly).
*/
if (i == 0 && dtlb_store[i].te_va == data_va)
writable_text = true;
else
continue;
}
#ifdef SUN4V
/*
* Remove write permissions from text mappings in the dTLB - sun4v.
* Add entries in the iTLB.
*/
void
sparc64_finalize_tlb_sun4v(u_long data_va)
{
int i;
int64_t data;
bool writable_text = false;
int64_t hv_rc;
for (i = 0; i < dtlb_slot; i++) {
if (dtlb_store[i].te_va >= data_va) {
/*
* If (for whatever reason) the start of the
* writable section is right at the start of
* the kernel, we need to map it into the ITLB
* nevertheless (and don't make it readonly).
*/
if (i == 0 && dtlb_store[i].te_va == data_va)
writable_text = true;
else
continue;
}
if ((bi_itlb == NULL) || (bi_dtlb == NULL)) {
panic("Out of memory in sparc64_bi_add.\n");
}
for (i = 0; i < itlb_slot; i++) {
bi_itlb->tlb[i].te_va = itlb_store[i].te_va;
bi_itlb->tlb[i].te_pa = itlb_store[i].te_pa;
}
bi_add(bi_itlb, BTINFO_ITLB, itlb_size);
for (i = 0; i < dtlb_slot; i++) {
bi_dtlb->tlb[i].te_va = dtlb_store[i].te_va;
bi_dtlb->tlb[i].te_pa = dtlb_store[i].te_pa;
}
bi_add(bi_dtlb, BTINFO_DTLB, dtlb_size);
}
/*
* Choose kernel image mapping strategy:
*
* LOADFILE_NOP_ALLOCATOR To load kernel image headers
* LOADFILE_OFW_ALLOCATOR To map the kernel by OpenFirmware means
* LOADFILE_MMU_ALLOCATOR To use permanent 4MB mappings
*/
void
loadfile_set_allocator(int type)
{
if (type >= (sizeof(memswa) / sizeof(struct memsw))) {
panic("Bad allocator request.\n");
}
/*
* Release all memory claimed by previous allocator and schedule
* another allocator for succeeding memory allocation calls.
*/
(*memsw->freeall)();
memsw = &memswa[type];
}