vtophys.cc revision 1110
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 "arch/alpha/pmap.h" 32#include "arch/alpha/vtophys.hh" 33#include "base/trace.hh" 34#include "cpu/exec_context.hh" 35#include "mem/functional_mem/physical_memory.hh" 36 37using namespace std; 38 39inline Addr 40level3_index(Addr vaddr) 41{ return (vaddr >> ALPHA_PGSHIFT) & PTEMASK; } 42 43inline Addr 44level2_index(Addr vaddr) 45{ return (vaddr >> (ALPHA_PGSHIFT + NPTEPG_SHIFT)) & PTEMASK; } 46 47inline Addr 48level1_index(Addr vaddr) 49{ return (vaddr >> (ALPHA_PGSHIFT + 2 * NPTEPG_SHIFT)) & PTEMASK; } 50 51Addr 52kernel_pte_lookup(PhysicalMemory *pmem, Addr ptbr, Addr vaddr) 53{ 54 uint64_t level1_map = ptbr; 55 Addr level1_pte = level1_map + (level1_index(vaddr) << PTESHIFT); 56 57 uint64_t level1 = pmem->phys_read_qword(level1_pte); 58 if (!entry_valid(level1)) { 59 DPRINTF(VtoPhys, "level 1 PTE not valid, va = %#\n", vaddr); 60 return 0; 61 } 62 63 uint64_t level2_map = PMAP_PTE_PA(level1); 64 Addr level2_pte = level2_map + (level2_index(vaddr) << PTESHIFT); 65 uint64_t level2 = pmem->phys_read_qword(level2_pte); 66 if (!entry_valid(level2)) { 67 DPRINTF(VtoPhys, "level 2 PTE not valid, va = %#x\n", vaddr); 68 return 0; 69 } 70 71 uint64_t level3_map = PMAP_PTE_PA(level2); 72 Addr level3_pte = level3_map + (level3_index(vaddr) << PTESHIFT); 73 74 return level3_pte; 75} 76 77Addr 78vtophys(PhysicalMemory *xc, Addr vaddr) 79{ 80 Addr paddr = 0; 81 if (vaddr < ALPHA_K0SEG_BASE) 82 DPRINTF(VtoPhys, "vtophys: invalid vaddr %#x", vaddr); 83 else if (vaddr < ALPHA_K1SEG_BASE) 84 paddr = ALPHA_K0SEG_TO_PHYS(vaddr); 85 else 86 panic("vtophys: ptbr is not set on virtual lookup"); 87 88 DPRINTF(VtoPhys, "vtophys(%#x) -> %#x\n", vaddr, paddr); 89 90 return paddr; 91} 92 93Addr 94vtophys(ExecContext *xc, Addr vaddr) 95{ 96 Addr ptbr = xc->regs.ipr[AlphaISA::IPR_PALtemp20]; 97 Addr paddr = 0; 98 //@todo Andrew couldn't remember why he commented some of this code 99 //so I put it back in. Perhaps something to do with gdb debugging? 100 if (PC_PAL(vaddr) && (vaddr < PAL_MAX)) { 101 paddr = vaddr & ~ULL(1); 102 } else { 103 if (vaddr >= ALPHA_K0SEG_BASE && vaddr <= ALPHA_K0SEG_END) { 104 paddr = ALPHA_K0SEG_TO_PHYS(vaddr); 105 } else if (!ptbr) { 106 paddr = vaddr; 107 } else { 108 Addr pte = kernel_pte_lookup(xc->physmem, ptbr, vaddr); 109 uint64_t entry = xc->physmem->phys_read_qword(pte); 110 if (pte && entry_valid(entry)) 111 paddr = PMAP_PTE_PA(entry) | (vaddr & ALPHA_PGOFSET); 112 } 113 } 114 115 116 DPRINTF(VtoPhys, "vtophys(%#x) -> %#x\n", vaddr, paddr); 117 118 return paddr; 119} 120 121uint8_t * 122ptomem(ExecContext *xc, Addr paddr, size_t len) 123{ 124 return xc->physmem->dma_addr(paddr, len); 125} 126 127uint8_t * 128vtomem(ExecContext *xc, Addr vaddr, size_t len) 129{ 130 Addr paddr = vtophys(xc, vaddr); 131 return xc->physmem->dma_addr(paddr, len); 132} 133 134void 135CopyOut(ExecContext *xc, void *dest, Addr src, size_t cplen) 136{ 137 Addr paddr; 138 char *dmaaddr; 139 char *dst = (char *)dest; 140 int len; 141 142 paddr = vtophys(xc, src); 143 len = min((int)(ALPHA_PGBYTES - (paddr & ALPHA_PGOFSET)), (int)cplen); 144 dmaaddr = (char *)xc->physmem->dma_addr(paddr, len); 145 assert(dmaaddr); 146 147 memcpy(dst, dmaaddr, len); 148 if (len == cplen) 149 return; 150 151 cplen -= len; 152 dst += len; 153 src += len; 154 155 while (cplen > ALPHA_PGBYTES) { 156 paddr = vtophys(xc, src); 157 dmaaddr = (char *)xc->physmem->dma_addr(paddr, ALPHA_PGBYTES); 158 assert(dmaaddr); 159 160 memcpy(dst, dmaaddr, ALPHA_PGBYTES); 161 cplen -= ALPHA_PGBYTES; 162 dst += ALPHA_PGBYTES; 163 src += ALPHA_PGBYTES; 164 } 165 166 if (cplen > 0) { 167 paddr = vtophys(xc, src); 168 dmaaddr = (char *)xc->physmem->dma_addr(paddr, cplen); 169 assert(dmaaddr); 170 171 memcpy(dst, dmaaddr, cplen); 172 } 173} 174 175void 176CopyIn(ExecContext *xc, Addr dest, void *source, size_t cplen) 177{ 178 Addr paddr; 179 char *dmaaddr; 180 char *src = (char *)source; 181 int len; 182 183 paddr = vtophys(xc, dest); 184 len = min((int)(ALPHA_PGBYTES - (paddr & ALPHA_PGOFSET)), (int)cplen); 185 dmaaddr = (char *)xc->physmem->dma_addr(paddr, len); 186 assert(dmaaddr); 187 188 memcpy(dmaaddr, src, len); 189 if (len == cplen) 190 return; 191 192 cplen -= len; 193 src += len; 194 dest += len; 195 196 while (cplen > ALPHA_PGBYTES) { 197 paddr = vtophys(xc, dest); 198 dmaaddr = (char *)xc->physmem->dma_addr(paddr, ALPHA_PGBYTES); 199 assert(dmaaddr); 200 201 memcpy(dmaaddr, src, ALPHA_PGBYTES); 202 cplen -= ALPHA_PGBYTES; 203 src += ALPHA_PGBYTES; 204 dest += ALPHA_PGBYTES; 205 } 206 207 if (cplen > 0) { 208 paddr = vtophys(xc, dest); 209 dmaaddr = (char *)xc->physmem->dma_addr(paddr, cplen); 210 assert(dmaaddr); 211 212 memcpy(dmaaddr, src, cplen); 213 } 214} 215 216void 217CopyString(ExecContext *xc, char *dst, Addr vaddr, size_t maxlen) 218{ 219 Addr paddr; 220 char *dmaaddr; 221 int len; 222 223 paddr = vtophys(xc, vaddr); 224 len = min((int)(ALPHA_PGBYTES - (paddr & ALPHA_PGOFSET)), (int)maxlen); 225 dmaaddr = (char *)xc->physmem->dma_addr(paddr, len); 226 assert(dmaaddr); 227 228 char *term = (char *)memchr(dmaaddr, 0, len); 229 if (term) 230 len = term - dmaaddr + 1; 231 232 memcpy(dst, dmaaddr, len); 233 234 if (term || len == maxlen) 235 return; 236 237 maxlen -= len; 238 dst += len; 239 vaddr += len; 240 241 while (maxlen > ALPHA_PGBYTES) { 242 paddr = vtophys(xc, vaddr); 243 dmaaddr = (char *)xc->physmem->dma_addr(paddr, ALPHA_PGBYTES); 244 assert(dmaaddr); 245 246 char *term = (char *)memchr(dmaaddr, 0, ALPHA_PGBYTES); 247 len = term ? (term - dmaaddr + 1) : ALPHA_PGBYTES; 248 249 memcpy(dst, dmaaddr, len); 250 if (term) 251 return; 252 253 maxlen -= ALPHA_PGBYTES; 254 dst += ALPHA_PGBYTES; 255 vaddr += ALPHA_PGBYTES; 256 } 257 258 if (maxlen > 0) { 259 paddr = vtophys(xc, vaddr); 260 dmaaddr = (char *)xc->physmem->dma_addr(paddr, maxlen); 261 assert(dmaaddr); 262 263 char *term = (char *)memchr(dmaaddr, 0, maxlen); 264 len = term ? (term - dmaaddr + 1) : maxlen; 265 266 memcpy(dst, dmaaddr, len); 267 268 maxlen -= len; 269 } 270 271 if (maxlen == 0) 272 dst[maxlen] = '\0'; 273} 274