se_translating_port_proxy.cc revision 2423
1/*
2 * Copyright (c) 2001-2005 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#include "arch/faults.hh"
31#include "base/chunk_generator.hh"
32#include "mem/port.hh"
33#include "mem/translating_port.hh"
34#include "mem/page_table.hh"
35
36using namespace TheISA;
37
38TranslatingPort::TranslatingPort(Port *_port, PageTable *p_table)
39    : port(_port), pTable(p_table)
40{ }
41
42TranslatingPort::~TranslatingPort()
43{ }
44
45Fault
46TranslatingPort::readBlobFunctional(Addr addr, uint8_t *p, int size)
47{
48    Addr paddr;
49    int prevSize = 0;
50
51    for (ChunkGenerator gen(addr, size, VMPageSize); !gen.done(); gen.next()) {
52
53        if (!pTable->translate(gen.addr(),paddr))
54            return genMachineCheckFault();
55
56        port->readBlobFunctional(paddr, p + prevSize, gen.size());
57        prevSize += gen.size();
58    }
59
60    return NoFault;
61}
62
63Fault
64TranslatingPort::writeBlobFunctional(Addr addr, uint8_t *p, int size,
65                                     bool alloc)
66{
67    Addr paddr;
68    int prevSize = 0;
69
70    for (ChunkGenerator gen(addr, size, VMPageSize); !gen.done(); gen.next()) {
71
72        if (!pTable->translate(gen.addr(), paddr)) {
73            if (alloc) {
74                pTable->allocate(roundDown(gen.addr(), VMPageSize),
75                                 VMPageSize);
76                pTable->translate(gen.addr(), paddr);
77            } else {
78                return genMachineCheckFault();
79            }
80        }
81
82        port->writeBlobFunctional(paddr, p + prevSize, gen.size());
83        prevSize += gen.size();
84    }
85
86    return NoFault;
87}
88
89
90Fault
91TranslatingPort::memsetBlobFunctional(Addr addr, uint8_t val, int size,
92                                      bool alloc)
93{
94    Addr paddr;
95
96    for (ChunkGenerator gen(addr, size, VMPageSize); !gen.done(); gen.next()) {
97
98        if (!pTable->translate(gen.addr(), paddr)) {
99            if (alloc) {
100                pTable->allocate(roundDown(gen.addr(), VMPageSize),
101                                 VMPageSize);
102                pTable->translate(gen.addr(), paddr);
103            } else {
104                return genMachineCheckFault();
105            }
106        }
107
108        port->memsetBlobFunctional(paddr, val, gen.size());
109    }
110
111    return NoFault;
112}
113
114
115Fault
116TranslatingPort::writeStringFunctional(Addr addr, const char *str)
117{
118    Addr paddr,vaddr;
119    uint8_t c;
120
121    vaddr = addr;
122
123    do {
124        c = *str++;
125        if (!pTable->translate(vaddr++,paddr))
126            return genMachineCheckFault();
127
128        port->writeBlobFunctional(paddr, &c, 1);
129    } while (c);
130
131    return NoFault;
132}
133
134Fault
135TranslatingPort::readStringFunctional(std::string &str, Addr addr)
136{
137    Addr paddr,vaddr;
138    uint8_t c;
139
140    vaddr = addr;
141
142    do {
143        if (!pTable->translate(vaddr++,paddr))
144            return genMachineCheckFault();
145
146        port->readBlobFunctional(paddr, &c, 1);
147        str += c;
148    } while (c);
149
150    return NoFault;
151}
152
153