se_translating_port_proxy.cc revision 8539
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 * Authors: Ron Dreslinski
29 *          Steve Reinhardt
30 */
31
32#include <string>
33
34#include "base/chunk_generator.hh"
35#include "config/the_isa.hh"
36#include "mem/page_table.hh"
37#include "mem/port.hh"
38#include "mem/translating_port.hh"
39#include "sim/process.hh"
40
41using namespace TheISA;
42
43TranslatingPort::TranslatingPort(const std::string &_name,
44                                 Process *p, AllocType alloc)
45    : FunctionalPort(_name), pTable(p->pTable), process(p),
46      allocating(alloc)
47{ }
48
49TranslatingPort::~TranslatingPort()
50{ }
51
52bool
53TranslatingPort::tryReadBlob(Addr addr, uint8_t *p, int size)
54{
55    Addr paddr;
56    int prevSize = 0;
57
58    for (ChunkGenerator gen(addr, size, VMPageSize); !gen.done(); gen.next()) {
59
60        if (!pTable->translate(gen.addr(),paddr))
61            return false;
62
63        Port::readBlob(paddr, p + prevSize, gen.size());
64        prevSize += gen.size();
65    }
66
67    return true;
68}
69
70void
71TranslatingPort::readBlob(Addr addr, uint8_t *p, int size)
72{
73    if (!tryReadBlob(addr, p, size))
74        fatal("readBlob(0x%x, ...) failed", addr);
75}
76
77
78bool
79TranslatingPort::tryWriteBlob(Addr addr, uint8_t *p, int size)
80{
81
82    Addr paddr;
83    int prevSize = 0;
84
85    for (ChunkGenerator gen(addr, size, VMPageSize); !gen.done(); gen.next()) {
86
87        if (!pTable->translate(gen.addr(), paddr)) {
88            if (allocating == Always) {
89                pTable->allocate(roundDown(gen.addr(), VMPageSize),
90                                 VMPageSize);
91            } else if (allocating == NextPage) {
92                // check if we've accessed the next page on the stack
93                if (!process->fixupStackFault(gen.addr()))
94                    panic("Page table fault when accessing virtual address %#x "
95                            "during functional write\n", gen.addr());
96            } else {
97                return false;
98            }
99            pTable->translate(gen.addr(), paddr);
100        }
101
102        Port::writeBlob(paddr, p + prevSize, gen.size());
103        prevSize += gen.size();
104    }
105
106    return true;
107}
108
109
110void
111TranslatingPort::writeBlob(Addr addr, uint8_t *p, int size)
112{
113    if (!tryWriteBlob(addr, p, size))
114        fatal("writeBlob(0x%x, ...) failed", addr);
115}
116
117bool
118TranslatingPort::tryMemsetBlob(Addr addr, uint8_t val, int size)
119{
120    Addr paddr;
121
122    for (ChunkGenerator gen(addr, size, VMPageSize); !gen.done(); gen.next()) {
123
124        if (!pTable->translate(gen.addr(), paddr)) {
125            if (allocating == Always) {
126                pTable->allocate(roundDown(gen.addr(), VMPageSize),
127                                 VMPageSize);
128                pTable->translate(gen.addr(), paddr);
129            } else {
130                return false;
131            }
132        }
133
134        Port::memsetBlob(paddr, val, gen.size());
135    }
136
137    return true;
138}
139
140void
141TranslatingPort::memsetBlob(Addr addr, uint8_t val, int size)
142{
143    if (!tryMemsetBlob(addr, val, size))
144        fatal("memsetBlob(0x%x, ...) failed", addr);
145}
146
147
148bool
149TranslatingPort::tryWriteString(Addr addr, const char *str)
150{
151    Addr paddr,vaddr;
152    uint8_t c;
153
154    vaddr = addr;
155
156    do {
157        c = *str++;
158        if (!pTable->translate(vaddr++,paddr))
159            return false;
160
161        Port::writeBlob(paddr, &c, 1);
162    } while (c);
163
164    return true;
165}
166
167void
168TranslatingPort::writeString(Addr addr, const char *str)
169{
170    if (!tryWriteString(addr, str))
171        fatal("writeString(0x%x, ...) failed", addr);
172}
173
174bool
175TranslatingPort::tryReadString(std::string &str, Addr addr)
176{
177    Addr paddr,vaddr;
178    uint8_t c;
179
180    vaddr = addr;
181
182    do {
183        if (!pTable->translate(vaddr++,paddr))
184            return false;
185
186        Port::readBlob(paddr, &c, 1);
187        str += c;
188    } while (c);
189
190    return true;
191}
192
193void
194TranslatingPort::readString(std::string &str, Addr addr)
195{
196    if (!tryReadString(str, addr))
197        fatal("readString(0x%x, ...) failed", addr);
198}
199
200