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.test.layers;
|
28 |
|
|
|
29 |
|
|
import ao486.test.TestUnit.Descriptor;
|
30 |
|
|
import java.util.HashMap;
|
31 |
|
|
import java.util.HashSet;
|
32 |
|
|
import java.util.LinkedList;
|
33 |
|
|
import java.util.Random;
|
34 |
|
|
|
35 |
|
|
public class DescriptorTableLayer extends Layer {
|
36 |
|
|
|
37 |
|
|
public DescriptorTableLayer(Random random, LinkedList<Pair<Long, Long>> prohibited_list, boolean ldtr_valid) {
|
38 |
|
|
this.random = random;
|
39 |
|
|
|
40 |
|
|
while(true) {
|
41 |
|
|
gdtr_base = norm(random.nextInt());
|
42 |
|
|
gdtr_limit = random.nextInt(0xFFFF + 1);
|
43 |
|
|
|
44 |
|
|
if( gdtr_base + gdtr_limit < 4294967296L &&
|
45 |
|
|
collides(prohibited_list, gdtr_base, gdtr_base + gdtr_limit) == false
|
46 |
|
|
) break;
|
47 |
|
|
}
|
48 |
|
|
prohibited_list.add(new Pair<>(gdtr_base, gdtr_base+gdtr_limit));
|
49 |
|
|
|
50 |
|
|
while(true) {
|
51 |
|
|
ldtr_base = norm(random.nextInt());
|
52 |
|
|
ldtr_limit = random.nextInt(0xFFFF + 1);
|
53 |
|
|
|
54 |
|
|
if( ldtr_base + ldtr_limit < 4294967296L &&
|
55 |
|
|
collides(prohibited_list, (int)ldtr_base, (int)(ldtr_base + ldtr_limit)) == false
|
56 |
|
|
) break;
|
57 |
|
|
}
|
58 |
|
|
prohibited_list.add(new Pair<>(ldtr_base, ldtr_base+ldtr_limit));
|
59 |
|
|
|
60 |
|
|
this.ldtr_valid = ldtr_valid;
|
61 |
|
|
}
|
62 |
|
|
|
63 |
|
|
/** @return -1 : no index that is out of bounds
|
64 |
|
|
*/
|
65 |
|
|
public int getOutOfBoundsIndex(boolean is_ldtr) {
|
66 |
|
|
if(is_ldtr) {
|
67 |
|
|
long limit = (new_ldtr_enabled)? new_ldtr_limit : ldtr_limit;
|
68 |
|
|
|
69 |
|
|
if(limit >= 65535) return -1;
|
70 |
|
|
|
71 |
|
|
long offset = random.nextInt(65535-(int)limit);
|
72 |
|
|
offset += limit + 1;
|
73 |
|
|
|
74 |
|
|
return (int)(offset >> 3);
|
75 |
|
|
}
|
76 |
|
|
else {
|
77 |
|
|
if(gdtr_limit >= 65535) return -1;
|
78 |
|
|
|
79 |
|
|
long offset = random.nextInt(65535-(int)gdtr_limit);
|
80 |
|
|
offset += gdtr_limit + 1;
|
81 |
|
|
|
82 |
|
|
return (int)(offset >> 3);
|
83 |
|
|
}
|
84 |
|
|
}
|
85 |
|
|
|
86 |
|
|
public int addDescriptor(boolean is_ldtr, Descriptor desc) {
|
87 |
|
|
long offset = 0;
|
88 |
|
|
long index = 0;
|
89 |
|
|
|
90 |
|
|
while(true) {
|
91 |
|
|
if(is_ldtr) {
|
92 |
|
|
long limit = (new_ldtr_enabled)? new_ldtr_limit : ldtr_limit;
|
93 |
|
|
long base = (new_ldtr_enabled)? new_ldtr_base : ldtr_base;
|
94 |
|
|
HashSet<Integer> set = (new_ldtr_enabled)? new_ldtr_set : ldtr_set;
|
95 |
|
|
|
96 |
|
|
if(limit <= 6) return -1;
|
97 |
|
|
|
98 |
|
|
long ldtr_limit_norm = (limit+1) & 0xFFF8;
|
99 |
|
|
int ldtr_max = (int)(ldtr_limit_norm/8);
|
100 |
|
|
if(set.size() >= ldtr_max) return -1;
|
101 |
|
|
|
102 |
|
|
offset = random.nextInt((int)((limit+1) & 0xFFF8));
|
103 |
|
|
offset &= 0xFFF8;
|
104 |
|
|
|
105 |
|
|
if(set.contains((int)offset)) continue;
|
106 |
|
|
set.add((int)offset);
|
107 |
|
|
|
108 |
|
|
index = offset >> 3;
|
109 |
|
|
offset += base;
|
110 |
|
|
}
|
111 |
|
|
else {
|
112 |
|
|
if(gdtr_limit <= 6) return -1;
|
113 |
|
|
|
114 |
|
|
long gdtr_limit_norm = (gdtr_limit+1) & 0xFFF8;
|
115 |
|
|
int gdtr_max = (int)(gdtr_limit_norm/8);
|
116 |
|
|
if(gdtr_set.size() >= gdtr_max) return -1;
|
117 |
|
|
|
118 |
|
|
offset = random.nextInt((int)((gdtr_limit+1) & 0xFFF8));
|
119 |
|
|
offset &= 0xFFF8;
|
120 |
|
|
|
121 |
|
|
if(gdtr_set.contains((int)offset)) continue;
|
122 |
|
|
gdtr_set.add((int)offset);
|
123 |
|
|
|
124 |
|
|
index = offset >> 3;
|
125 |
|
|
offset += gdtr_base;
|
126 |
|
|
}
|
127 |
|
|
break;
|
128 |
|
|
}
|
129 |
|
|
|
130 |
|
|
for(int i=0; i<8; i++) {
|
131 |
|
|
descr_map.put(offset+i, desc.get_byte(i));
|
132 |
|
|
}
|
133 |
|
|
|
134 |
|
|
return (int)index;
|
135 |
|
|
}
|
136 |
|
|
|
137 |
|
|
public void setup_new_ldt(int new_ldtr_base, int new_ldtr_limit) {
|
138 |
|
|
if(new_ldtr_enabled) return;
|
139 |
|
|
|
140 |
|
|
new_ldtr_enabled = true;
|
141 |
|
|
|
142 |
|
|
this.new_ldtr_base = norm(new_ldtr_base);
|
143 |
|
|
this.new_ldtr_limit = norm(new_ldtr_limit);
|
144 |
|
|
}
|
145 |
|
|
|
146 |
|
|
//----------
|
147 |
|
|
public long gdtr_base() { return gdtr_base; }
|
148 |
|
|
public long gdtr_limit() { return gdtr_limit; }
|
149 |
|
|
|
150 |
|
|
public long ldtr_base() { return ldtr_base; }
|
151 |
|
|
public long ldtr_limit() { return ldtr_limit; }
|
152 |
|
|
|
153 |
|
|
public long ldtr_valid() { return ldtr_valid? 1 : 0; }
|
154 |
|
|
|
155 |
|
|
public boolean is_memory_not_random(long address) { return descr_map.containsKey(address); }
|
156 |
|
|
|
157 |
|
|
public Byte get_memory(long address) {
|
158 |
|
|
return (descr_map.containsKey(address) == false)? null : descr_map.get(address);
|
159 |
|
|
}
|
160 |
|
|
|
161 |
|
|
//----------
|
162 |
|
|
long gdtr_base, gdtr_limit;
|
163 |
|
|
long ldtr_base, ldtr_limit;
|
164 |
|
|
boolean ldtr_valid;
|
165 |
|
|
|
166 |
|
|
HashMap<Long, Byte> descr_map = new HashMap<>();
|
167 |
|
|
HashSet<Integer> ldtr_set = new HashSet<>();
|
168 |
|
|
HashSet<Integer> gdtr_set = new HashSet<>();
|
169 |
|
|
|
170 |
|
|
//--- new ldt
|
171 |
|
|
boolean new_ldtr_enabled;
|
172 |
|
|
long new_ldtr_base, new_ldtr_limit;
|
173 |
|
|
HashSet<Integer> new_ldtr_set = new HashSet<>();
|
174 |
|
|
}
|