cache_blk.hh revision 2810:5befce12ad70
1/* 2 * Copyright (c) 2003-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: Erik Hallnor 29 */ 30 31/** @file 32 * Definitions of a simple cache block class. 33 */ 34 35#ifndef __CACHE_BLK_HH__ 36#define __CACHE_BLK_HH__ 37 38#include "sim/root.hh" // for Tick 39#include "arch/isa_traits.hh" // for Addr 40#include "cpu/exec_context.hh" 41 42/** 43 * Cache block status bit assignments 44 */ 45enum CacheBlkStatusBits { 46 /** valid, readable */ 47 BlkValid = 0x01, 48 /** write permission */ 49 BlkWritable = 0x02, 50 /** dirty (modified) */ 51 BlkDirty = 0x04, 52 /** compressed */ 53 BlkCompressed = 0x08, 54 /** block was referenced */ 55 BlkReferenced = 0x10, 56 /** block was a hardware prefetch yet unaccessed*/ 57 BlkHWPrefetched = 0x20 58}; 59 60/** 61 * A Basic Cache block. 62 * Contains the tag, status, and a pointer to data. 63 */ 64class CacheBlk 65{ 66 public: 67 /** The address space ID of this block. */ 68 int asid; 69 /** Data block tag value. */ 70 Addr tag; 71 /** 72 * Contains a copy of the data in this block for easy access. This is used 73 * for efficient execution when the data could be actually stored in 74 * another format (COW, compressed, sub-blocked, etc). In all cases the 75 * data stored here should be kept consistant with the actual data 76 * referenced by this block. 77 */ 78 uint8_t *data; 79 /** the number of bytes stored in this block. */ 80 int size; 81 82 /** block state: OR of CacheBlkStatusBit */ 83 typedef unsigned State; 84 85 /** The current status of this block. @sa CacheBlockStatusBits */ 86 State status; 87 88 /** Which curTick will this block be accessable */ 89 Tick whenReady; 90 91 /** Save the exec context so that writebacks can use them. */ 92 ExecContext *xc; 93 94 /** 95 * The set this block belongs to. 96 * @todo Move this into subclasses when we fix CacheTags to use them. 97 */ 98 int set; 99 100 /** Number of references to this block since it was brought in. */ 101 int refCount; 102 103 CacheBlk() 104 : asid(-1), tag(0), data(0) ,size(0), status(0), whenReady(0), xc(0), 105 set(-1), refCount(0) 106 {} 107 108 /** 109 * Copy the state of the given block into this one. 110 * @param rhs The block to copy. 111 * @return a const reference to this block. 112 */ 113 const CacheBlk& operator=(const CacheBlk& rhs) 114 { 115 asid = rhs.asid; 116 tag = rhs.tag; 117 data = rhs.data; 118 size = rhs.size; 119 status = rhs.status; 120 whenReady = rhs.whenReady; 121 xc = rhs.xc; 122 set = rhs.set; 123 refCount = rhs.refCount; 124 return *this; 125 } 126 127 /** 128 * Checks the write permissions of this block. 129 * @return True if the block is writable. 130 */ 131 bool isWritable() const 132 { 133 const int needed_bits = BlkWritable | BlkValid; 134 return (status & needed_bits) == needed_bits; 135 } 136 137 /** 138 * Checks that a block is valid (readable). 139 * @return True if the block is valid. 140 */ 141 bool isValid() const 142 { 143 return (status & BlkValid) != 0; 144 } 145 146 /** 147 * Check to see if a block has been written. 148 * @return True if the block is dirty. 149 */ 150 bool isModified() const 151 { 152 return (status & BlkDirty) != 0; 153 } 154 155 /** 156 * Check to see if this block contains compressed data. 157 * @return True iF the block's data is compressed. 158 */ 159 bool isCompressed() const 160 { 161 return (status & BlkCompressed) != 0; 162 } 163 164 /** 165 * Check if this block has been referenced. 166 * @return True if the block has been referenced. 167 */ 168 bool isReferenced() const 169 { 170 return (status & BlkReferenced) != 0; 171 } 172 173 /** 174 * Check if this block was the result of a hardware prefetch, yet to 175 * be touched. 176 * @return True if the block was a hardware prefetch, unaccesed. 177 */ 178 bool isPrefetch() const 179 { 180 return (status & BlkHWPrefetched) != 0; 181 } 182 183 184}; 185 186/** 187 * Output a CacheBlk to the given ostream. 188 * @param out The stream for the output. 189 * @param blk The cache block to print. 190 * 191 * @return The output stream. 192 */ 193inline std::ostream & 194operator<<(std::ostream &out, const CacheBlk &blk) 195{ 196 out << std::hex << std::endl; 197 out << " Tag: " << blk.tag << std::endl; 198 out << " Status: " << blk.status << std::endl; 199 200 return(out << std::dec); 201} 202 203#endif //__CACHE_BLK_HH__ 204