vtophys.cc revision 924
12810Srdreslin@umich.edu/*
212500Snikos.nikoleris@arm.com * Copyright (c) 2002-2004 The Regents of The University of Michigan
311051Sandreas.hansson@arm.com * All rights reserved.
411051Sandreas.hansson@arm.com *
511051Sandreas.hansson@arm.com * Redistribution and use in source and binary forms, with or without
611051Sandreas.hansson@arm.com * modification, are permitted provided that the following conditions are
711051Sandreas.hansson@arm.com * met: redistributions of source code must retain the above copyright
811051Sandreas.hansson@arm.com * notice, this list of conditions and the following disclaimer;
911051Sandreas.hansson@arm.com * redistributions in binary form must reproduce the above copyright
1011051Sandreas.hansson@arm.com * notice, this list of conditions and the following disclaimer in the
1111051Sandreas.hansson@arm.com * documentation and/or other materials provided with the distribution;
1211051Sandreas.hansson@arm.com * neither the name of the copyright holders nor the names of its
1311051Sandreas.hansson@arm.com * contributors may be used to endorse or promote products derived from
1411051Sandreas.hansson@arm.com * this software without specific prior written permission.
1511051Sandreas.hansson@arm.com *
162810Srdreslin@umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172810Srdreslin@umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182810Srdreslin@umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192810Srdreslin@umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202810Srdreslin@umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212810Srdreslin@umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222810Srdreslin@umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232810Srdreslin@umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242810Srdreslin@umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252810Srdreslin@umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262810Srdreslin@umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272810Srdreslin@umich.edu */
282810Srdreslin@umich.edu
292810Srdreslin@umich.edu#include <string>
302810Srdreslin@umich.edu
312810Srdreslin@umich.edu#include "targetarch/pmap.h"
322810Srdreslin@umich.edu
332810Srdreslin@umich.edu#include "cpu/exec_context.hh"
342810Srdreslin@umich.edu#include "mem/functional_mem/physical_memory.hh"
352810Srdreslin@umich.edu#include "base/trace.hh"
362810Srdreslin@umich.edu#include "targetarch/vtophys.hh"
372810Srdreslin@umich.edu
382810Srdreslin@umich.eduusing namespace std;
392810Srdreslin@umich.edu
402810Srdreslin@umich.eduinline Addr
412810Srdreslin@umich.edulevel3_index(Addr vaddr)
4211051Sandreas.hansson@arm.com{ return (vaddr >> ALPHA_PGSHIFT) & PTEMASK; }
4311051Sandreas.hansson@arm.com
442810Srdreslin@umich.eduinline Addr
4511051Sandreas.hansson@arm.comlevel2_index(Addr vaddr)
4611051Sandreas.hansson@arm.com{ return (vaddr >> (ALPHA_PGSHIFT + NPTEPG_SHIFT)) & PTEMASK; }
4712349Snikos.nikoleris@arm.com
482810Srdreslin@umich.eduinline Addr
492810Srdreslin@umich.edulevel1_index(Addr vaddr)
502810Srdreslin@umich.edu{ return (vaddr >> (ALPHA_PGSHIFT + 2 * NPTEPG_SHIFT)) & PTEMASK; }
512810Srdreslin@umich.edu
5211051Sandreas.hansson@arm.comAddr
532810Srdreslin@umich.edukernel_pte_lookup(PhysicalMemory *pmem, Addr ptbr, Addr vaddr)
542810Srdreslin@umich.edu{
5511051Sandreas.hansson@arm.com    uint64_t level1_map = ptbr;
562810Srdreslin@umich.edu    Addr level1_pte = level1_map + (level1_index(vaddr) << PTESHIFT);
5712334Sgabeblack@google.com
5811051Sandreas.hansson@arm.com    uint64_t level1 = pmem->phys_read_qword(level1_pte);
5911051Sandreas.hansson@arm.com    if (!entry_valid(level1)) {
6011051Sandreas.hansson@arm.com        DPRINTF(VtoPhys, "level 1 PTE not valid, va = %#\n", vaddr);
6111051Sandreas.hansson@arm.com        return 0;
6211288Ssteve.reinhardt@amd.com    }
6311051Sandreas.hansson@arm.com
6411051Sandreas.hansson@arm.com    uint64_t level2_map = PMAP_PTE_PA(level1);
6511051Sandreas.hansson@arm.com    Addr level2_pte = level2_map + (level2_index(vaddr) << PTESHIFT);
6611051Sandreas.hansson@arm.com    uint64_t level2 = pmem->phys_read_qword(level2_pte);
6711051Sandreas.hansson@arm.com    if (!entry_valid(level2)) {
6811053Sandreas.hansson@arm.com        DPRINTF(VtoPhys, "level 2 PTE not valid, va = %#x\n", vaddr);
6911053Sandreas.hansson@arm.com        return 0;
7011051Sandreas.hansson@arm.com    }
7111051Sandreas.hansson@arm.com
7211051Sandreas.hansson@arm.com    uint64_t level3_map = PMAP_PTE_PA(level2);
7311197Sandreas.hansson@arm.com    Addr level3_pte = level3_map + (level3_index(vaddr) << PTESHIFT);
7411197Sandreas.hansson@arm.com
7511199Sandreas.hansson@arm.com    return level3_pte;
7611197Sandreas.hansson@arm.com}
7712084Sspwilson2@wisc.edu
7812084Sspwilson2@wisc.eduAddr
7911197Sandreas.hansson@arm.comvtophys(PhysicalMemory *xc, Addr vaddr)
8011051Sandreas.hansson@arm.com{
8111051Sandreas.hansson@arm.com    Addr paddr = 0;
8211051Sandreas.hansson@arm.com    if (vaddr < ALPHA_K0SEG_BASE)
8311051Sandreas.hansson@arm.com        DPRINTF(VtoPhys, "vtophys: invalid vaddr %#x", vaddr);
8411051Sandreas.hansson@arm.com    else if (vaddr < ALPHA_K1SEG_BASE)
8511051Sandreas.hansson@arm.com        paddr = ALPHA_K0SEG_TO_PHYS(vaddr);
8611051Sandreas.hansson@arm.com    else
8711051Sandreas.hansson@arm.com        panic("vtophys: ptbr is not set on virtual lookup");
8811051Sandreas.hansson@arm.com
8911051Sandreas.hansson@arm.com    DPRINTF(VtoPhys, "vtophys(%#x) -> %#x\n", vaddr, paddr);
9011051Sandreas.hansson@arm.com
9111051Sandreas.hansson@arm.com    return paddr;
9211051Sandreas.hansson@arm.com}
9311051Sandreas.hansson@arm.com
9411051Sandreas.hansson@arm.comAddr
9511051Sandreas.hansson@arm.comvtophys(ExecContext *xc, Addr vaddr)
9611051Sandreas.hansson@arm.com{
9711051Sandreas.hansson@arm.com    Addr ptbr = xc->regs.ipr[AlphaISA::IPR_PALtemp20];
9811051Sandreas.hansson@arm.com    Addr paddr = 0;
9911051Sandreas.hansson@arm.com    //@todo Andrew couldn't remember why he commented some of this code
10011051Sandreas.hansson@arm.com    //so I put it back in. Perhaps something to do with gdb debugging?
10111051Sandreas.hansson@arm.com    if (PC_PAL(vaddr)) {
10211051Sandreas.hansson@arm.com        paddr = vaddr & ~ULL(1);
10311051Sandreas.hansson@arm.com    } else if (!ptbr) {
10411051Sandreas.hansson@arm.com        paddr = vaddr;
10511051Sandreas.hansson@arm.com    } else {
10611051Sandreas.hansson@arm.com        if (vaddr >= ALPHA_K0SEG_BASE && vaddr <= ALPHA_K0SEG_END) {
10711051Sandreas.hansson@arm.com            paddr = ALPHA_K0SEG_TO_PHYS(vaddr);
10811051Sandreas.hansson@arm.com        } else {
10911051Sandreas.hansson@arm.com            Addr pte = kernel_pte_lookup(xc->physmem, ptbr, vaddr);
11011051Sandreas.hansson@arm.com            uint64_t entry = xc->physmem->phys_read_qword(pte);
11111051Sandreas.hansson@arm.com            if (pte && entry_valid(entry))
11211051Sandreas.hansson@arm.com                paddr = PMAP_PTE_PA(entry) | (vaddr & PGOFSET);
11311051Sandreas.hansson@arm.com        }
11411051Sandreas.hansson@arm.com    }
11511051Sandreas.hansson@arm.com
11611051Sandreas.hansson@arm.com
11711051Sandreas.hansson@arm.com    DPRINTF(VtoPhys, "vtophys(%#x) -> %#x\n", vaddr, paddr);
11811051Sandreas.hansson@arm.com
11911051Sandreas.hansson@arm.com    return paddr;
12011051Sandreas.hansson@arm.com}
12111051Sandreas.hansson@arm.com
12211051Sandreas.hansson@arm.comuint8_t *
12311051Sandreas.hansson@arm.comptomem(ExecContext *xc, Addr paddr, size_t len)
12411051Sandreas.hansson@arm.com{
12511051Sandreas.hansson@arm.com    return xc->physmem->dma_addr(paddr, len);
12611051Sandreas.hansson@arm.com}
12711051Sandreas.hansson@arm.com
12811051Sandreas.hansson@arm.comuint8_t *
12911051Sandreas.hansson@arm.comvtomem(ExecContext *xc, Addr vaddr, size_t len)
13011051Sandreas.hansson@arm.com{
13111051Sandreas.hansson@arm.com    Addr paddr = vtophys(xc, vaddr);
13211051Sandreas.hansson@arm.com    return xc->physmem->dma_addr(paddr, len);
13311051Sandreas.hansson@arm.com}
13411051Sandreas.hansson@arm.com
13511051Sandreas.hansson@arm.comvoid
13611051Sandreas.hansson@arm.comCopyData(ExecContext *xc, void *dest, Addr vaddr, size_t cplen)
13711051Sandreas.hansson@arm.com{
13811051Sandreas.hansson@arm.com    Addr paddr;
13911051Sandreas.hansson@arm.com    char *dmaaddr;
14011051Sandreas.hansson@arm.com    char *dst = (char *)dest;
14111051Sandreas.hansson@arm.com    int len;
14211051Sandreas.hansson@arm.com
14311051Sandreas.hansson@arm.com    paddr = vtophys(xc, vaddr);
14411051Sandreas.hansson@arm.com    len = min((int)(ALPHA_PGBYTES - (paddr & PGOFSET)), (int)cplen);
14511051Sandreas.hansson@arm.com    dmaaddr = (char *)xc->physmem->dma_addr(paddr, len);
14611051Sandreas.hansson@arm.com    assert(dmaaddr);
14711051Sandreas.hansson@arm.com
14811051Sandreas.hansson@arm.com    memcpy(dst, dmaaddr, len);
14911051Sandreas.hansson@arm.com    if (len == cplen)
15011051Sandreas.hansson@arm.com        return;
15111601Sandreas.hansson@arm.com
15211601Sandreas.hansson@arm.com    cplen -= len;
15311051Sandreas.hansson@arm.com    dst += len;
15411051Sandreas.hansson@arm.com    vaddr += len;
15511051Sandreas.hansson@arm.com
15611051Sandreas.hansson@arm.com    while (cplen > ALPHA_PGBYTES) {
15711051Sandreas.hansson@arm.com        paddr = vtophys(xc, vaddr);
15811051Sandreas.hansson@arm.com        dmaaddr = (char *)xc->physmem->dma_addr(paddr, ALPHA_PGBYTES);
15911051Sandreas.hansson@arm.com        assert(dmaaddr);
16011051Sandreas.hansson@arm.com
16111051Sandreas.hansson@arm.com        memcpy(dst, dmaaddr, ALPHA_PGBYTES);
16211051Sandreas.hansson@arm.com        cplen -= ALPHA_PGBYTES;
16311284Sandreas.hansson@arm.com        dst += ALPHA_PGBYTES;
16411051Sandreas.hansson@arm.com        vaddr += ALPHA_PGBYTES;
16511051Sandreas.hansson@arm.com    }
16611051Sandreas.hansson@arm.com
16711051Sandreas.hansson@arm.com    if (cplen > 0) {
16811051Sandreas.hansson@arm.com        paddr = vtophys(xc, vaddr);
16911051Sandreas.hansson@arm.com        dmaaddr = (char *)xc->physmem->dma_addr(paddr, cplen);
17011051Sandreas.hansson@arm.com        assert(dmaaddr);
17111284Sandreas.hansson@arm.com
17211284Sandreas.hansson@arm.com        memcpy(dst, dmaaddr, cplen);
17311284Sandreas.hansson@arm.com    }
17411284Sandreas.hansson@arm.com}
17511051Sandreas.hansson@arm.com
17611284Sandreas.hansson@arm.comvoid
17711051Sandreas.hansson@arm.comCopyString(ExecContext *xc, char *dst, Addr vaddr, size_t maxlen)
17811051Sandreas.hansson@arm.com{
17911051Sandreas.hansson@arm.com    Addr paddr;
18011284Sandreas.hansson@arm.com    char *dmaaddr;
18111284Sandreas.hansson@arm.com    int len;
18211284Sandreas.hansson@arm.com
18311284Sandreas.hansson@arm.com    paddr = vtophys(xc, vaddr);
18411051Sandreas.hansson@arm.com    len = min((int)(ALPHA_PGBYTES - (paddr & PGOFSET)), (int)maxlen);
18511744Snikos.nikoleris@arm.com    dmaaddr = (char *)xc->physmem->dma_addr(paddr, len);
18611051Sandreas.hansson@arm.com    assert(dmaaddr);
18711051Sandreas.hansson@arm.com
18811051Sandreas.hansson@arm.com    char *term = (char *)memchr(dmaaddr, 0, len);
18911051Sandreas.hansson@arm.com    if (term)
19011286Sandreas.hansson@arm.com        len = term - dmaaddr + 1;
19111286Sandreas.hansson@arm.com
19211286Sandreas.hansson@arm.com    memcpy(dst, dmaaddr, len);
19311051Sandreas.hansson@arm.com
19411286Sandreas.hansson@arm.com    if (term || len == maxlen)
19511600Sandreas.hansson@arm.com        return;
19611600Sandreas.hansson@arm.com
19711051Sandreas.hansson@arm.com    maxlen -= len;
19811051Sandreas.hansson@arm.com    dst += len;
19911051Sandreas.hansson@arm.com    vaddr += len;
20011284Sandreas.hansson@arm.com
20111051Sandreas.hansson@arm.com    while (maxlen > ALPHA_PGBYTES) {
20211051Sandreas.hansson@arm.com        paddr = vtophys(xc, vaddr);
20311051Sandreas.hansson@arm.com        dmaaddr = (char *)xc->physmem->dma_addr(paddr, ALPHA_PGBYTES);
20411602Sandreas.hansson@arm.com        assert(dmaaddr);
20511051Sandreas.hansson@arm.com
20611051Sandreas.hansson@arm.com        char *term = (char *)memchr(dmaaddr, 0, ALPHA_PGBYTES);
20711284Sandreas.hansson@arm.com        len = term ? (term - dmaaddr + 1) : ALPHA_PGBYTES;
20811051Sandreas.hansson@arm.com
20911284Sandreas.hansson@arm.com        memcpy(dst, dmaaddr, len);
21011602Sandreas.hansson@arm.com        if (term)
21111051Sandreas.hansson@arm.com            return;
21211051Sandreas.hansson@arm.com
21311284Sandreas.hansson@arm.com        maxlen -= ALPHA_PGBYTES;
21411051Sandreas.hansson@arm.com        dst += ALPHA_PGBYTES;
21511284Sandreas.hansson@arm.com        vaddr += ALPHA_PGBYTES;
21611284Sandreas.hansson@arm.com    }
21711284Sandreas.hansson@arm.com
21811051Sandreas.hansson@arm.com    if (maxlen > 0) {
21911051Sandreas.hansson@arm.com        paddr = vtophys(xc, vaddr);
22011051Sandreas.hansson@arm.com        dmaaddr = (char *)xc->physmem->dma_addr(paddr, maxlen);
22111284Sandreas.hansson@arm.com        assert(dmaaddr);
22211284Sandreas.hansson@arm.com
22311284Sandreas.hansson@arm.com        char *term = (char *)memchr(dmaaddr, 0, maxlen);
22411284Sandreas.hansson@arm.com        len = term ? (term - dmaaddr + 1) : maxlen;
22511051Sandreas.hansson@arm.com
22611051Sandreas.hansson@arm.com        memcpy(dst, dmaaddr, len);
22711051Sandreas.hansson@arm.com
22811284Sandreas.hansson@arm.com        maxlen -= len;
22911284Sandreas.hansson@arm.com    }
23011284Sandreas.hansson@arm.com
23111197Sandreas.hansson@arm.com    if (maxlen == 0)
23211601Sandreas.hansson@arm.com        dst[maxlen] = '\0';
23311601Sandreas.hansson@arm.com}
23411601Sandreas.hansson@arm.com