mm_disk.cc revision 8641:4d3ecac1abec
1/*
2 * Copyright (c) 2006 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: Ali Saidi
29 */
30
31/** @file
32 * This device acts as a disk similar to the memory mapped disk device
33 * in legion. Any access is translated to an offset in the disk image.
34 */
35
36#include <cstring>
37
38#include "base/trace.hh"
39#include "debug/IdeDisk.hh"
40#include "dev/sparc/mm_disk.hh"
41#include "dev/platform.hh"
42#include "mem/packet_access.hh"
43#include "mem/port.hh"
44#include "sim/byteswap.hh"
45#include "sim/system.hh"
46
47MmDisk::MmDisk(const Params *p)
48    : BasicPioDevice(p), image(p->image), curSector((off_t)-1), dirty(false)
49{
50    std::memset(&diskData, 0, SectorSize);
51    pioSize = image->size() * SectorSize;
52}
53
54Tick
55MmDisk::read(PacketPtr pkt)
56{
57    Addr accessAddr;
58    off_t sector;
59    uint16_t d16;
60    uint32_t d32;
61    uint64_t d64;
62
63    assert(pkt->getAddr() >= pioAddr && pkt->getAddr() < pioAddr + pioSize);
64    accessAddr = pkt->getAddr() - pioAddr;
65
66    sector = accessAddr / SectorSize;
67
68    if (sector != curSector) {
69        if (dirty) {
70#ifndef NDEBUG
71            off_t bytes_written =
72#endif
73                image->write(diskData, curSector);
74            assert(bytes_written == SectorSize);
75        }
76#ifndef NDEBUG
77        off_t bytes_read =
78#endif
79            image->read(diskData, sector);
80        assert(bytes_read == SectorSize);
81        curSector = sector;
82    }
83    switch (pkt->getSize()) {
84      case sizeof(uint8_t):
85        pkt->set(diskData[accessAddr % SectorSize]);
86        DPRINTF(IdeDisk, "reading byte %#x value= %#x\n", accessAddr, diskData[accessAddr %
87                SectorSize]);
88        break;
89      case sizeof(uint16_t):
90        memcpy(&d16, diskData + (accessAddr % SectorSize), 2);
91        pkt->set(htobe(d16));
92        DPRINTF(IdeDisk, "reading word %#x value= %#x\n", accessAddr, d16);
93        break;
94      case sizeof(uint32_t):
95        memcpy(&d32, diskData + (accessAddr % SectorSize), 4);
96        pkt->set(htobe(d32));
97        DPRINTF(IdeDisk, "reading dword %#x value= %#x\n", accessAddr, d32);
98        break;
99      case sizeof(uint64_t):
100        memcpy(&d64, diskData + (accessAddr % SectorSize), 8);
101        pkt->set(htobe(d64));
102        DPRINTF(IdeDisk, "reading qword %#x value= %#x\n", accessAddr, d64);
103        break;
104      default:
105        panic("Invalid access size\n");
106    }
107
108    pkt->makeAtomicResponse();
109    return pioDelay;
110}
111
112Tick
113MmDisk::write(PacketPtr pkt)
114{
115    Addr accessAddr;
116    off_t sector;
117    uint16_t d16;
118    uint32_t d32;
119    uint64_t d64;
120
121    assert(pkt->getAddr() >= pioAddr && pkt->getAddr() < pioAddr + pioSize);
122    accessAddr = pkt->getAddr() - pioAddr;
123
124    sector = accessAddr / SectorSize;
125
126    if (sector != curSector) {
127        if (dirty) {
128#ifndef NDEBUG
129            off_t bytes_written =
130#endif
131                image->write(diskData, curSector);
132            assert(bytes_written == SectorSize);
133        }
134#ifndef NDEBUG
135        off_t bytes_read =
136#endif
137            image->read(diskData,  sector);
138        assert(bytes_read == SectorSize);
139        curSector = sector;
140    }
141    dirty = true;
142
143    switch (pkt->getSize()) {
144      case sizeof(uint8_t):
145        diskData[accessAddr % SectorSize] = htobe(pkt->get<uint8_t>());
146        DPRINTF(IdeDisk, "writing byte %#x value= %#x\n", accessAddr, diskData[accessAddr %
147                SectorSize]);
148        break;
149      case sizeof(uint16_t):
150        d16 = htobe(pkt->get<uint16_t>());
151        memcpy(diskData + (accessAddr % SectorSize), &d16, 2);
152        DPRINTF(IdeDisk, "writing word %#x value= %#x\n", accessAddr, d16);
153        break;
154      case sizeof(uint32_t):
155        d32 = htobe(pkt->get<uint32_t>());
156        memcpy(diskData + (accessAddr % SectorSize), &d32, 4);
157        DPRINTF(IdeDisk, "writing dword %#x value= %#x\n", accessAddr, d32);
158        break;
159      case sizeof(uint64_t):
160        d64 = htobe(pkt->get<uint64_t>());
161        memcpy(diskData + (accessAddr % SectorSize), &d64, 8);
162        DPRINTF(IdeDisk, "writing qword %#x value= %#x\n", accessAddr, d64);
163        break;
164      default:
165        panic("Invalid access size\n");
166    }
167
168    pkt->makeAtomicResponse();
169    return pioDelay;
170}
171
172void
173MmDisk::serialize(std::ostream &os)
174{
175    // just write any dirty changes to the cow layer it will take care of
176    // serialization
177    if (dirty) {
178#ifndef NDEBUG
179        int bytes_read =
180#endif
181            image->write(diskData, curSector);
182        assert(bytes_read == SectorSize);
183    }
184}
185
186MmDisk *
187MmDiskParams::create()
188{
189    return new MmDisk(this);
190}
191