vtophys.cc revision 924
1/* 2 * Copyright (c) 2002-2004 The Regents of The University of Michigan 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer; 9 * redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution; 12 * neither the name of the copyright holders nor the names of its 13 * contributors may be used to endorse or promote products derived from 14 * this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29#include <string> 30 31#include "targetarch/pmap.h" 32 33#include "cpu/exec_context.hh" 34#include "mem/functional_mem/physical_memory.hh" 35#include "base/trace.hh" 36#include "targetarch/vtophys.hh" 37 38using namespace std; 39 40inline Addr 41level3_index(Addr vaddr) 42{ return (vaddr >> ALPHA_PGSHIFT) & PTEMASK; } 43 44inline Addr 45level2_index(Addr vaddr) 46{ return (vaddr >> (ALPHA_PGSHIFT + NPTEPG_SHIFT)) & PTEMASK; } 47 48inline Addr 49level1_index(Addr vaddr) 50{ return (vaddr >> (ALPHA_PGSHIFT + 2 * NPTEPG_SHIFT)) & PTEMASK; } 51 52Addr 53kernel_pte_lookup(PhysicalMemory *pmem, Addr ptbr, Addr vaddr) 54{ 55 uint64_t level1_map = ptbr; 56 Addr level1_pte = level1_map + (level1_index(vaddr) << PTESHIFT); 57 58 uint64_t level1 = pmem->phys_read_qword(level1_pte); 59 if (!entry_valid(level1)) { 60 DPRINTF(VtoPhys, "level 1 PTE not valid, va = %#\n", vaddr); 61 return 0; 62 } 63 64 uint64_t level2_map = PMAP_PTE_PA(level1); 65 Addr level2_pte = level2_map + (level2_index(vaddr) << PTESHIFT); 66 uint64_t level2 = pmem->phys_read_qword(level2_pte); 67 if (!entry_valid(level2)) { 68 DPRINTF(VtoPhys, "level 2 PTE not valid, va = %#x\n", vaddr); 69 return 0; 70 } 71 72 uint64_t level3_map = PMAP_PTE_PA(level2); 73 Addr level3_pte = level3_map + (level3_index(vaddr) << PTESHIFT); 74 75 return level3_pte; 76} 77 78Addr 79vtophys(PhysicalMemory *xc, Addr vaddr) 80{ 81 Addr paddr = 0; 82 if (vaddr < ALPHA_K0SEG_BASE) 83 DPRINTF(VtoPhys, "vtophys: invalid vaddr %#x", vaddr); 84 else if (vaddr < ALPHA_K1SEG_BASE) 85 paddr = ALPHA_K0SEG_TO_PHYS(vaddr); 86 else 87 panic("vtophys: ptbr is not set on virtual lookup"); 88 89 DPRINTF(VtoPhys, "vtophys(%#x) -> %#x\n", vaddr, paddr); 90 91 return paddr; 92} 93 94Addr 95vtophys(ExecContext *xc, Addr vaddr) 96{ 97 Addr ptbr = xc->regs.ipr[AlphaISA::IPR_PALtemp20]; 98 Addr paddr = 0; 99 //@todo Andrew couldn't remember why he commented some of this code 100 //so I put it back in. Perhaps something to do with gdb debugging? 101 if (PC_PAL(vaddr)) { 102 paddr = vaddr & ~ULL(1); 103 } else if (!ptbr) { 104 paddr = vaddr; 105 } else { 106 if (vaddr >= ALPHA_K0SEG_BASE && vaddr <= ALPHA_K0SEG_END) { 107 paddr = ALPHA_K0SEG_TO_PHYS(vaddr); 108 } else { 109 Addr pte = kernel_pte_lookup(xc->physmem, ptbr, vaddr); 110 uint64_t entry = xc->physmem->phys_read_qword(pte); 111 if (pte && entry_valid(entry)) 112 paddr = PMAP_PTE_PA(entry) | (vaddr & PGOFSET); 113 } 114 } 115 116 117 DPRINTF(VtoPhys, "vtophys(%#x) -> %#x\n", vaddr, paddr); 118 119 return paddr; 120} 121 122uint8_t * 123ptomem(ExecContext *xc, Addr paddr, size_t len) 124{ 125 return xc->physmem->dma_addr(paddr, len); 126} 127 128uint8_t * 129vtomem(ExecContext *xc, Addr vaddr, size_t len) 130{ 131 Addr paddr = vtophys(xc, vaddr); 132 return xc->physmem->dma_addr(paddr, len); 133} 134 135void 136CopyData(ExecContext *xc, void *dest, Addr vaddr, size_t cplen) 137{ 138 Addr paddr; 139 char *dmaaddr; 140 char *dst = (char *)dest; 141 int len; 142 143 paddr = vtophys(xc, vaddr); 144 len = min((int)(ALPHA_PGBYTES - (paddr & PGOFSET)), (int)cplen); 145 dmaaddr = (char *)xc->physmem->dma_addr(paddr, len); 146 assert(dmaaddr); 147 148 memcpy(dst, dmaaddr, len); 149 if (len == cplen) 150 return; 151 152 cplen -= len; 153 dst += len; 154 vaddr += len; 155 156 while (cplen > ALPHA_PGBYTES) { 157 paddr = vtophys(xc, vaddr); 158 dmaaddr = (char *)xc->physmem->dma_addr(paddr, ALPHA_PGBYTES); 159 assert(dmaaddr); 160 161 memcpy(dst, dmaaddr, ALPHA_PGBYTES); 162 cplen -= ALPHA_PGBYTES; 163 dst += ALPHA_PGBYTES; 164 vaddr += ALPHA_PGBYTES; 165 } 166 167 if (cplen > 0) { 168 paddr = vtophys(xc, vaddr); 169 dmaaddr = (char *)xc->physmem->dma_addr(paddr, cplen); 170 assert(dmaaddr); 171 172 memcpy(dst, dmaaddr, cplen); 173 } 174} 175 176void 177CopyString(ExecContext *xc, char *dst, Addr vaddr, size_t maxlen) 178{ 179 Addr paddr; 180 char *dmaaddr; 181 int len; 182 183 paddr = vtophys(xc, vaddr); 184 len = min((int)(ALPHA_PGBYTES - (paddr & PGOFSET)), (int)maxlen); 185 dmaaddr = (char *)xc->physmem->dma_addr(paddr, len); 186 assert(dmaaddr); 187 188 char *term = (char *)memchr(dmaaddr, 0, len); 189 if (term) 190 len = term - dmaaddr + 1; 191 192 memcpy(dst, dmaaddr, len); 193 194 if (term || len == maxlen) 195 return; 196 197 maxlen -= len; 198 dst += len; 199 vaddr += len; 200 201 while (maxlen > ALPHA_PGBYTES) { 202 paddr = vtophys(xc, vaddr); 203 dmaaddr = (char *)xc->physmem->dma_addr(paddr, ALPHA_PGBYTES); 204 assert(dmaaddr); 205 206 char *term = (char *)memchr(dmaaddr, 0, ALPHA_PGBYTES); 207 len = term ? (term - dmaaddr + 1) : ALPHA_PGBYTES; 208 209 memcpy(dst, dmaaddr, len); 210 if (term) 211 return; 212 213 maxlen -= ALPHA_PGBYTES; 214 dst += ALPHA_PGBYTES; 215 vaddr += ALPHA_PGBYTES; 216 } 217 218 if (maxlen > 0) { 219 paddr = vtophys(xc, vaddr); 220 dmaaddr = (char *)xc->physmem->dma_addr(paddr, maxlen); 221 assert(dmaaddr); 222 223 char *term = (char *)memchr(dmaaddr, 0, maxlen); 224 len = term ? (term - dmaaddr + 1) : maxlen; 225 226 memcpy(dst, dmaaddr, len); 227 228 maxlen -= len; 229 } 230 231 if (maxlen == 0) 232 dst[maxlen] = '\0'; 233} 234