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 java.util.HashMap;
|
30 |
|
|
import java.util.LinkedList;
|
31 |
|
|
import java.util.Random;
|
32 |
|
|
|
33 |
|
|
public class TSSCurrentLayer extends Layer {
|
34 |
|
|
public TSSCurrentLayer(Random random, Type type, int limit, int selector, LinkedList<Pair<Long, Long>> prohibited_list) {
|
35 |
|
|
this.random = random;
|
36 |
|
|
this.type = type;
|
37 |
|
|
|
38 |
|
|
for(Pair<Long, Long> pair : prohibited_list) {
|
39 |
|
|
System.out.printf("proh: %08x : %08x\n", pair.a, pair.b);
|
40 |
|
|
}
|
41 |
|
|
|
42 |
|
|
while(true) {
|
43 |
|
|
tr_base = norm(random.nextInt());
|
44 |
|
|
if(tr_base + tr_limit >= 0x100000000L) continue;
|
45 |
|
|
if(collides(prohibited_list, tr_base, tr_base + limit) == false) break;
|
46 |
|
|
}
|
47 |
|
|
prohibited_list.add(new Pair<>(tr_base, tr_base + limit));
|
48 |
|
|
|
49 |
|
|
tr_limit = limit;
|
50 |
|
|
|
51 |
|
|
System.out.printf("TSSCurrentLayer: base: %08x, base+limit: %08x\n", tr_base, tr_base+tr_limit);
|
52 |
|
|
|
53 |
|
|
tr_map.put(tr_base+0, (byte)(selector & 0xFF));
|
54 |
|
|
tr_map.put(tr_base+1, (byte)((selector >> 8) & 0xFF));
|
55 |
|
|
}
|
56 |
|
|
|
57 |
|
|
public void add_ss_esp(int pl, long esp, int ss) {
|
58 |
|
|
|
59 |
|
|
int offset = (type == Type.ACTIVE_286 || type == Type.BUSY_286)? 2 + pl*4 : 4 + pl*8;
|
60 |
|
|
|
61 |
|
|
if(type == Type.ACTIVE_286 || type == Type.BUSY_286) {
|
62 |
|
|
tr_map.put(tr_base+offset+0, (byte)(esp & 0xFF));
|
63 |
|
|
tr_map.put(tr_base+offset+1, (byte)((esp >> 8) & 0xFF));
|
64 |
|
|
|
65 |
|
|
tr_map.put(tr_base+offset+2, (byte)(ss & 0xFF));
|
66 |
|
|
tr_map.put(tr_base+offset+3, (byte)((ss >> 8) & 0xFF));
|
67 |
|
|
}
|
68 |
|
|
else {
|
69 |
|
|
tr_map.put(tr_base+offset+0, (byte)(esp & 0xFF));
|
70 |
|
|
tr_map.put(tr_base+offset+1, (byte)((esp >> 8) & 0xFF));
|
71 |
|
|
tr_map.put(tr_base+offset+2, (byte)((esp >> 16) & 0xFF));
|
72 |
|
|
tr_map.put(tr_base+offset+3, (byte)((esp >> 24) & 0xFF));
|
73 |
|
|
|
74 |
|
|
tr_map.put(tr_base+offset+4, (byte)(ss & 0xFF));
|
75 |
|
|
tr_map.put(tr_base+offset+5, (byte)((ss >> 8) & 0xFF));
|
76 |
|
|
tr_map.put(tr_base+offset+6, (byte)random.nextInt());
|
77 |
|
|
tr_map.put(tr_base+offset+7, (byte)random.nextInt());
|
78 |
|
|
}
|
79 |
|
|
}
|
80 |
|
|
|
81 |
|
|
public enum Type {
|
82 |
|
|
BUSY_386,
|
83 |
|
|
BUSY_286,
|
84 |
|
|
ACTIVE_386,
|
85 |
|
|
ACTIVE_286
|
86 |
|
|
}
|
87 |
|
|
|
88 |
|
|
long tr_limit;
|
89 |
|
|
long tr_base;
|
90 |
|
|
Type type;
|
91 |
|
|
|
92 |
|
|
//-----------
|
93 |
|
|
|
94 |
|
|
public long tr_base() {
|
95 |
|
|
return tr_base;
|
96 |
|
|
}
|
97 |
|
|
public long tr_limit() {
|
98 |
|
|
return tr_limit;
|
99 |
|
|
}
|
100 |
|
|
public long tr_type() throws Exception {
|
101 |
|
|
//was: return 0xB;
|
102 |
|
|
if(type == Type.BUSY_286) return 0x3;
|
103 |
|
|
if(type == Type.ACTIVE_286) return 0x1;
|
104 |
|
|
|
105 |
|
|
if(type == Type.BUSY_386) return 0xB;
|
106 |
|
|
if(type == Type.ACTIVE_386) return 0x9;
|
107 |
|
|
|
108 |
|
|
throw new Exception("Invalid TSS type: " + type);
|
109 |
|
|
}
|
110 |
|
|
public boolean is_memory_not_random(long address) { return tr_map.containsKey(address); }
|
111 |
|
|
|
112 |
|
|
public Byte get_memory(long address) {
|
113 |
|
|
if(address < tr_base || address > tr_base + tr_limit) return null;
|
114 |
|
|
|
115 |
|
|
if(tr_map.containsKey(address)) return tr_map.get(address);
|
116 |
|
|
|
117 |
|
|
return (byte)random.nextInt();
|
118 |
|
|
}
|
119 |
|
|
|
120 |
|
|
HashMap<Long, Byte> tr_map = new HashMap<>();
|
121 |
|
|
}
|