| 1 |
2 |
alfik |
/*
|
| 2 |
|
|
* Copyright (c) 2014, Aleksander Osman
|
| 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 met:
|
| 7 |
|
|
*
|
| 8 |
|
|
* * Redistributions of source code must retain the above copyright notice, this
|
| 9 |
|
|
* list of conditions and the following disclaimer.
|
| 10 |
|
|
*
|
| 11 |
|
|
* * Redistributions in binary form must reproduce the above copyright notice,
|
| 12 |
|
|
* this list of conditions and the following disclaimer in the documentation
|
| 13 |
|
|
* and/or other materials provided with the distribution.
|
| 14 |
|
|
*
|
| 15 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
| 16 |
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
| 17 |
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
| 18 |
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
| 19 |
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
| 20 |
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
| 21 |
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
| 22 |
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
| 23 |
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
| 24 |
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
| 25 |
|
|
*/
|
| 26 |
|
|
|
| 27 |
|
|
package ao486.module.memory.test;
|
| 28 |
|
|
|
| 29 |
|
|
import ao486.module.memory.AvalonListener;
|
| 30 |
|
|
import ao486.module.memory.GlobalListener;
|
| 31 |
|
|
import ao486.module.memory.Input;
|
| 32 |
|
|
import ao486.module.memory.Listener;
|
| 33 |
|
|
import ao486.module.memory.Output;
|
| 34 |
|
|
import ao486.module.memory.PrefetchListener;
|
| 35 |
|
|
import ao486.module.memory.Test;
|
| 36 |
|
|
import java.util.HashSet;
|
| 37 |
|
|
import java.util.LinkedList;
|
| 38 |
|
|
import java.util.Random;
|
| 39 |
|
|
|
| 40 |
|
|
class TestTLBCodeState {
|
| 41 |
|
|
|
| 42 |
|
|
TestTLBCodeState(Random random) {
|
| 43 |
|
|
this.random = random;
|
| 44 |
|
|
|
| 45 |
|
|
do {
|
| 46 |
|
|
cr3_base = random.nextLong();
|
| 47 |
|
|
cr3_base &= 0xFFFFF000L;
|
| 48 |
|
|
} while(Test.address_not_in_cache(cr3_base) || Test.address_not_in_cache(cr3_base + 0x1000L));
|
| 49 |
|
|
|
| 50 |
|
|
cr3_pwt = true;
|
| 51 |
|
|
cr3_pcd = true;
|
| 52 |
|
|
|
| 53 |
|
|
linear_set = new HashSet<>();
|
| 54 |
|
|
}
|
| 55 |
|
|
|
| 56 |
|
|
boolean overlap(long a1, long a2, long b1, long b2) {
|
| 57 |
|
|
if(b2 <= a2 && b2 >= a1) return true;
|
| 58 |
|
|
if(b1 <= a2 && b1 >= a1) return true;
|
| 59 |
|
|
return false;
|
| 60 |
|
|
}
|
| 61 |
|
|
|
| 62 |
|
|
void prepare(AvalonListener avalon, Long linear_requested) {
|
| 63 |
|
|
|
| 64 |
|
|
//pde
|
| 65 |
|
|
do {
|
| 66 |
|
|
pde = random.nextLong();
|
| 67 |
|
|
pde &= 0xFFFFF000L;
|
| 68 |
|
|
} while(Test.address_not_in_cache(pde) || Test.address_not_in_cache(pde + 0x1000L) || overlap(cr3_base, cr3_base+ 0x1000L, pde, pde + 0x1000L));
|
| 69 |
|
|
|
| 70 |
|
|
pde_pwt = true;
|
| 71 |
|
|
pde_pcd = true;
|
| 72 |
|
|
pde_present = true;
|
| 73 |
|
|
pde_rw = false;
|
| 74 |
|
|
pde_su = false;
|
| 75 |
|
|
pde_accessed= false;
|
| 76 |
|
|
|
| 77 |
|
|
//pte
|
| 78 |
|
|
do {
|
| 79 |
|
|
pte = random.nextLong();
|
| 80 |
|
|
pte &= 0xFFFFF000L;
|
| 81 |
|
|
} while(Test.address_not_in_cache(pte) || Test.address_not_in_cache(pte + 0x1000L) ||
|
| 82 |
|
|
overlap(cr3_base, cr3_base+ 0x1000L, pte, pte + 0x1000L) || overlap(pde, pde+ 0x1000L, pte, pte + 0x1000L));
|
| 83 |
|
|
|
| 84 |
|
|
pte_pwt = true;
|
| 85 |
|
|
pte_pcd = true;
|
| 86 |
|
|
pte_present = true;
|
| 87 |
|
|
pte_rw = false;
|
| 88 |
|
|
pte_su = false;
|
| 89 |
|
|
pte_accessed= false;
|
| 90 |
|
|
pte_dirty = false;
|
| 91 |
|
|
|
| 92 |
|
|
//linear
|
| 93 |
|
|
if(linear_requested != null) {
|
| 94 |
|
|
linear = linear_requested;
|
| 95 |
|
|
}
|
| 96 |
|
|
else {
|
| 97 |
|
|
do {
|
| 98 |
|
|
linear = random.nextLong();
|
| 99 |
|
|
linear &= 0xFFFFFFFFL;
|
| 100 |
|
|
} while(linear_set.contains(linear & 0xFFFFF000L));
|
| 101 |
|
|
}
|
| 102 |
|
|
|
| 103 |
|
|
linear_set.add(linear & 0xFFFFF000L);
|
| 104 |
|
|
|
| 105 |
|
|
linear_pde = (linear >> 22) & 0x3FFL;
|
| 106 |
|
|
linear_pte = (linear >> 12) & 0x3FFL;
|
| 107 |
|
|
|
| 108 |
|
|
length = (int)(16 - (linear & 0xFL));
|
| 109 |
|
|
if(length > 8) length = 8;
|
| 110 |
|
|
length = 1 + random.nextInt(length);
|
| 111 |
|
|
|
| 112 |
|
|
physical = pte | (linear & 0xFFFL);
|
| 113 |
|
|
|
| 114 |
|
|
System.out.printf("Preparing for linear: %x, physical: %x\n", linear, physical);
|
| 115 |
|
|
|
| 116 |
|
|
//----------
|
| 117 |
|
|
|
| 118 |
|
|
long pde_value = pde;
|
| 119 |
|
|
if(pde_present) pde_value |= 0x1L;
|
| 120 |
|
|
if(pde_rw) pde_value |= 0x2L;
|
| 121 |
|
|
if(pde_su) pde_value |= 0x4L;
|
| 122 |
|
|
if(pde_pwt) pde_value |= 0x8L;
|
| 123 |
|
|
if(pde_pcd) pde_value |= 0x10L;
|
| 124 |
|
|
if(pde_accessed)pde_value |= 0x20L;
|
| 125 |
|
|
|
| 126 |
|
|
avalon.set_memory(cr3_base | (linear_pde << 2), pde_value, 4);
|
| 127 |
|
|
|
| 128 |
|
|
System.out.printf("set: [%x] = %x\n", cr3_base | (linear_pde << 2), pde_value);
|
| 129 |
|
|
|
| 130 |
|
|
//System.out.printf("linear: %x\n", linear);
|
| 131 |
|
|
//System.out.printf("pde addr[%x] = %x\n", cr3_base | (linear_pde << 2), pde_value);
|
| 132 |
|
|
|
| 133 |
|
|
long pte_value = pte;
|
| 134 |
|
|
if(pte_present) pte_value |= 0x1L;
|
| 135 |
|
|
if(pte_rw) pte_value |= 0x2L;
|
| 136 |
|
|
if(pte_su) pte_value |= 0x4L;
|
| 137 |
|
|
if(pte_pwt) pte_value |= 0x8L;
|
| 138 |
|
|
if(pte_pcd) pte_value |= 0x10L;
|
| 139 |
|
|
if(pte_accessed)pte_value |= 0x20L;
|
| 140 |
|
|
if(pte_dirty) pte_value |= 0x40L;
|
| 141 |
|
|
|
| 142 |
|
|
avalon.set_memory(pde | (linear_pte << 2), pte_value, 4);
|
| 143 |
|
|
|
| 144 |
|
|
System.out.printf("set: [%x] = %x\n", pde | (linear_pte << 2), pte_value);
|
| 145 |
|
|
System.out.println("------");
|
| 146 |
|
|
}
|
| 147 |
|
|
|
| 148 |
|
|
Random random;
|
| 149 |
|
|
|
| 150 |
|
|
long cr3_base;
|
| 151 |
|
|
boolean cr3_pwt;
|
| 152 |
|
|
boolean cr3_pcd;
|
| 153 |
|
|
|
| 154 |
|
|
long pde;
|
| 155 |
|
|
boolean pde_pwt;
|
| 156 |
|
|
boolean pde_pcd;
|
| 157 |
|
|
boolean pde_present;
|
| 158 |
|
|
boolean pde_rw;
|
| 159 |
|
|
boolean pde_su;
|
| 160 |
|
|
boolean pde_accessed;
|
| 161 |
|
|
|
| 162 |
|
|
long pte;
|
| 163 |
|
|
boolean pte_pwt;
|
| 164 |
|
|
boolean pte_pcd;
|
| 165 |
|
|
boolean pte_present;
|
| 166 |
|
|
boolean pte_rw;
|
| 167 |
|
|
boolean pte_su;
|
| 168 |
|
|
boolean pte_accessed;
|
| 169 |
|
|
boolean pte_dirty;
|
| 170 |
|
|
|
| 171 |
|
|
long linear;
|
| 172 |
|
|
long linear_pde;
|
| 173 |
|
|
long linear_pte;
|
| 174 |
|
|
|
| 175 |
|
|
long physical;
|
| 176 |
|
|
|
| 177 |
|
|
int length;
|
| 178 |
|
|
|
| 179 |
|
|
HashSet<Long> linear_set;
|
| 180 |
|
|
}
|
| 181 |
|
|
|
| 182 |
|
|
public class TestTLBCode extends Test implements Listener, PrefetchListener.PrefetchInterface {
|
| 183 |
|
|
|
| 184 |
|
|
void go(long seed) throws Exception {
|
| 185 |
|
|
random = new Random(seed);
|
| 186 |
|
|
|
| 187 |
|
|
avalon = new AvalonListener(random, this);
|
| 188 |
|
|
prefetch = new PrefetchListener();
|
| 189 |
|
|
global = new GlobalListener();
|
| 190 |
|
|
|
| 191 |
|
|
finish_cycle = 0;
|
| 192 |
|
|
counter = 1;
|
| 193 |
|
|
|
| 194 |
|
|
state = new TestTLBCodeState(random);
|
| 195 |
|
|
code_stream = new LinkedList<>();
|
| 196 |
|
|
|
| 197 |
|
|
global.set(true, false, false, true, true, false, state.cr3_base, state.cr3_pcd, state.cr3_pwt, false, true);
|
| 198 |
|
|
|
| 199 |
|
|
//System.out.printf("addr: %x\n", pde | (linear_pde << 2));
|
| 200 |
|
|
|
| 201 |
|
|
long cs_base = 0xFFFF0000L;
|
| 202 |
|
|
long eip = 0x0FFF0L;
|
| 203 |
|
|
long cpl = 0;
|
| 204 |
|
|
long cs_limit= eip + 16;
|
| 205 |
|
|
|
| 206 |
|
|
state.prepare(avalon, cs_base + eip);
|
| 207 |
|
|
|
| 208 |
|
|
for(int i=0; i<16; i++) {
|
| 209 |
|
|
long b = avalon.get_memory(state.physical + i);
|
| 210 |
|
|
code_stream.add((byte)b);
|
| 211 |
|
|
|
| 212 |
|
|
//System.out.printf("[%x] = %x\n", cs_base + eip + i, b);
|
| 213 |
|
|
}
|
| 214 |
|
|
|
| 215 |
|
|
prefetch.prefetch(cpl, eip, cs_base, cs_limit, this);
|
| 216 |
|
|
|
| 217 |
|
|
//----------
|
| 218 |
|
|
|
| 219 |
|
|
LinkedList<Listener> listeners = new LinkedList<>();
|
| 220 |
|
|
listeners.add(avalon);
|
| 221 |
|
|
listeners.add(prefetch);
|
| 222 |
|
|
listeners.add(global);
|
| 223 |
|
|
listeners.add(this);
|
| 224 |
|
|
|
| 225 |
|
|
run_simulation(listeners);
|
| 226 |
|
|
}
|
| 227 |
|
|
|
| 228 |
|
|
@Override
|
| 229 |
|
|
public void prefetched(int cycle, long value, long size) throws Exception {
|
| 230 |
|
|
for(int i=0; i<size; i++) {
|
| 231 |
|
|
if(code_stream.isEmpty()) throw new Exception("Code stream is empty: i: " + i);
|
| 232 |
|
|
|
| 233 |
|
|
byte expected = code_stream.pop();
|
| 234 |
|
|
if(expected != (byte)value) throw new Exception("Invalid prefetched code: i: " + i + String.format(", expected: %x, value: %x", expected, (byte)value));
|
| 235 |
|
|
|
| 236 |
|
|
//System.out.printf("expected: %x == value: %x, size: %d\n", expected, (byte)value, code_stream.size());
|
| 237 |
|
|
|
| 238 |
|
|
value >>= 8;
|
| 239 |
|
|
}
|
| 240 |
|
|
}
|
| 241 |
|
|
@Override
|
| 242 |
|
|
public void prefetch_page_fault(int cycle, long cr2, long error_code) throws Exception {
|
| 243 |
|
|
throw new Exception("prefetch pf.");
|
| 244 |
|
|
}
|
| 245 |
|
|
@Override
|
| 246 |
|
|
public void prefetch_gp_fault(int cycle) throws Exception {
|
| 247 |
|
|
if(code_stream.size() > 0) throw new Exception("prefetch gp fault before limit reached.");
|
| 248 |
|
|
|
| 249 |
|
|
counter++;
|
| 250 |
|
|
|
| 251 |
|
|
if(counter >= 100) {
|
| 252 |
|
|
finish_cycle = cycle+1;
|
| 253 |
|
|
return;
|
| 254 |
|
|
}
|
| 255 |
|
|
|
| 256 |
|
|
do {
|
| 257 |
|
|
long cs_base = random.nextLong() & 0x000FFFFFL;
|
| 258 |
|
|
long eip = random.nextLong() & 0x000FFFFFL;
|
| 259 |
|
|
long cpl = 0;
|
| 260 |
|
|
long cs_limit= eip + random.nextInt(512);
|
| 261 |
|
|
|
| 262 |
|
|
//can cross pages
|
| 263 |
|
|
if(((cs_base+eip) & 0xFFFFF000L) != ((cs_base + cs_limit) & 0xFFFFF000L)) continue;
|
| 264 |
|
|
|
| 265 |
|
|
if(cs_base + eip >= 0x100000000L) continue;
|
| 266 |
|
|
|
| 267 |
|
|
if(state.linear_set.contains((cs_base + eip) & 0xFFFFF000L)) {
|
| 268 |
|
|
//System.out.printf("can not accept: %x\n", (cs_base + eip) & 0xFFFFF000L);
|
| 269 |
|
|
continue;
|
| 270 |
|
|
}
|
| 271 |
|
|
|
| 272 |
|
|
state.prepare(avalon, cs_base + eip);
|
| 273 |
|
|
|
| 274 |
|
|
int minus_i = 0;
|
| 275 |
|
|
for(int i=0; i<cs_limit - eip + 1; i++) {
|
| 276 |
|
|
|
| 277 |
|
|
long b = avalon.get_memory(state.physical + i - minus_i);
|
| 278 |
|
|
code_stream.add((byte)b);
|
| 279 |
|
|
|
| 280 |
|
|
//System.out.printf("[%x] = %x\n", state.physical + i, b);
|
| 281 |
|
|
}
|
| 282 |
|
|
|
| 283 |
|
|
prefetch.prefetch(cpl, eip, cs_base, cs_limit, this);
|
| 284 |
|
|
break;
|
| 285 |
|
|
}while(true);
|
| 286 |
|
|
|
| 287 |
|
|
global.set_reset(true, false, false, false);
|
| 288 |
|
|
reset_cycle = cycle+1;
|
| 289 |
|
|
}
|
| 290 |
|
|
|
| 291 |
|
|
@Override
|
| 292 |
|
|
public void set_input(int cycle, Input input) throws Exception {
|
| 293 |
|
|
if(cycle >= finish_cycle && finish_cycle > 0) {
|
| 294 |
|
|
input.finished = true;
|
| 295 |
|
|
}
|
| 296 |
|
|
|
| 297 |
|
|
if(cycle >= reset_cycle && reset_cycle > 0) {
|
| 298 |
|
|
global.set_reset(false, false, false, false);
|
| 299 |
|
|
reset_cycle = 0;
|
| 300 |
|
|
}
|
| 301 |
|
|
}
|
| 302 |
|
|
|
| 303 |
|
|
@Override
|
| 304 |
|
|
public void get_output(int cycle, Output output) throws Exception {
|
| 305 |
|
|
}
|
| 306 |
|
|
|
| 307 |
|
|
@Override
|
| 308 |
|
|
public void avalon_read(int cycle, long read_address) throws Exception {
|
| 309 |
|
|
last_read_address = read_address;
|
| 310 |
|
|
}
|
| 311 |
|
|
|
| 312 |
|
|
int counter;
|
| 313 |
|
|
Random random;
|
| 314 |
|
|
|
| 315 |
|
|
LinkedList<Byte> code_stream;
|
| 316 |
|
|
|
| 317 |
|
|
int finish_cycle;
|
| 318 |
|
|
int reset_cycle;
|
| 319 |
|
|
|
| 320 |
|
|
long last_read_address;
|
| 321 |
|
|
|
| 322 |
|
|
TestTLBCodeState state;
|
| 323 |
|
|
|
| 324 |
|
|
AvalonListener avalon;
|
| 325 |
|
|
PrefetchListener prefetch;
|
| 326 |
|
|
GlobalListener global;
|
| 327 |
|
|
|
| 328 |
|
|
//-------------
|
| 329 |
|
|
|
| 330 |
|
|
public static void main(String args[]) throws Exception {
|
| 331 |
|
|
TestTLBCode test1 = new TestTLBCode();
|
| 332 |
|
|
|
| 333 |
|
|
for(int i=0; i<1; i++) {
|
| 334 |
|
|
test1.go(i);
|
| 335 |
|
|
|
| 336 |
|
|
System.out.println("Run " + i + " complete.");
|
| 337 |
|
|
}
|
| 338 |
|
|
|
| 339 |
|
|
System.out.println("[Main] thread end.");
|
| 340 |
|
|
}
|
| 341 |
|
|
}
|