OpenCores
URL https://opencores.org/ocsvn/ao486/ao486/trunk

Subversion Repositories ao486

[/] [ao486/] [trunk/] [ao486_tool/] [src/] [ao486/] [test/] [io/] [TestINS_protected_rep.java] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
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.io;
28
 
29
import ao486.test.TestUnit;
30
import ao486.test.layers.FlagsLayer;
31
import ao486.test.layers.GeneralRegisterLayer;
32
import ao486.test.layers.HandleModeChangeLayer;
33
import ao486.test.layers.IOLayer;
34
import ao486.test.layers.InstructionLayer;
35
import ao486.test.layers.Layer;
36
import ao486.test.layers.MemoryLayer;
37
import ao486.test.layers.MemoryPatchLayer;
38
import ao486.test.layers.OtherLayer;
39
import ao486.test.layers.Pair;
40
import ao486.test.layers.SegmentLayer;
41
import ao486.test.layers.StackLayer;
42
import java.io.*;
43
import java.util.LinkedList;
44
import java.util.Random;
45
 
46
 
47
public class TestINS_protected_rep extends TestUnit implements Serializable {
48
    public static void main(String args[]) throws Exception {
49
        run_test(TestINS_protected_rep.class);
50
    }
51
 
52
    //--------------------------------------------------------------------------
53
    @Override
54
    public int get_test_count() throws Exception {
55
        return 100;
56
    }
57
 
58
    @Override
59
    public void init() throws Exception {
60
 
61
        random = new Random(7 + index);
62
 
63
        String instruction;
64
        while(true) {
65
            layers.clear();
66
 
67
            LinkedList<Pair<Long, Long>> prohibited_list = new LinkedList<>();
68
 
69
            //-----
70
 
71
            /* type:
72
             * 0 - no io_allow
73
             * 1 - no valid 32-bit TSS
74
             * 2 - TR.limit < 103
75
             * 3 - port permission bits >= limit
76
             * 4 - port permission bits invalid
77
             * 5 - all ok
78
             */
79
 
80
            int type = 5; //random.nextInt(5+1);
81
 
82
            int     next_port = 0;
83
            int     next_rpl = 0;
84
            boolean next_cr0_pe = true;
85
            boolean next_tr_valid = true;
86
            int     next_tr_base = 0;
87
            int     next_tr_limit = 0;
88
            int     next_tr_type = 0;
89
            boolean next_vm = false;
90
            int     next_iopl = 0;
91
 
92
            if(type == 0) {
93
                do {
94
                    next_rpl        = random.nextInt(4);
95
                    next_cr0_pe     = random.nextBoolean();
96
                    next_tr_valid   = random.nextBoolean();
97
                    next_tr_base    = random.nextInt();
98
                    next_tr_limit   = random.nextInt(1048576);
99
                    next_tr_type    = random.nextInt(16);
100
                    next_vm         = random.nextBoolean();
101
                    next_iopl       = random.nextInt(4);
102
                }while(next_cr0_pe && (next_vm || next_rpl > next_iopl));
103
 
104
                next_port       = random.nextInt(65536);
105
            }
106
 
107
            if(type == 1) {
108
                do {
109
                    next_rpl        = random.nextInt(4);
110
                    next_cr0_pe     = random.nextBoolean();
111
                    next_vm         = random.nextBoolean();
112
                    next_iopl       = random.nextInt(4);
113
                }while( !(next_cr0_pe && (next_vm || next_rpl > next_iopl)));
114
 
115
                do {
116
                    next_tr_valid   = random.nextBoolean();
117
                    next_tr_type    = random.nextInt(16);
118
                }while( !(next_tr_valid == false || (next_tr_type != 9 && next_tr_type != 11)));
119
 
120
                next_tr_base    = random.nextInt();
121
                next_tr_limit   = random.nextInt(1048576);
122
                next_port       = random.nextInt();
123
            }
124
 
125
            if(type == 2) {
126
                do {
127
                    next_rpl        = random.nextInt(4);
128
                    next_cr0_pe     = random.nextBoolean();
129
                    next_vm         = random.nextBoolean();
130
                    next_iopl       = random.nextInt(4);
131
                }while( !(next_cr0_pe && (next_vm || next_rpl > next_iopl)));
132
 
133
                do {
134
                    next_tr_valid   = random.nextBoolean();
135
                    next_tr_type    = random.nextInt(16);
136
                }while(next_tr_valid == false || (next_tr_type != 9 && next_tr_type != 11));
137
 
138
                do {
139
                    next_tr_limit   = random.nextInt(103);
140
                }while( !(next_tr_limit < 103));
141
 
142
                next_tr_base    = random.nextInt();
143
                next_port       = random.nextInt();
144
            }
145
 
146
            if(type == 3) {
147
                do {
148
                    next_rpl        = random.nextInt(4);
149
                    next_cr0_pe     = random.nextBoolean();
150
                    next_vm         = random.nextBoolean();
151
                    next_iopl       = random.nextInt(4);
152
                }while( !(next_cr0_pe && (next_vm || next_rpl > next_iopl)));
153
 
154
                do {
155
                    next_tr_valid   = random.nextBoolean();
156
                    next_tr_type    = random.nextInt(16);
157
                }while(next_tr_valid == false || (next_tr_type != 9 && next_tr_type != 11));
158
 
159
                do {
160
                    next_tr_limit   = random.nextInt(1048576);
161
                }while(next_tr_limit < 103);
162
 
163
                int permission_base = 0;
164
                do {
165
                    permission_base = random.nextInt(65536);
166
                    next_port       = random.nextInt(65536);
167
                    next_tr_limit   = random.nextInt(1048576-103); next_tr_limit += 103;
168
                }while( !(permission_base + (next_port/8) >= next_tr_limit));
169
 
170
                next_tr_base    = random.nextInt();
171
 
172
                MemoryPatchLayer tss_patch = new MemoryPatchLayer(random, prohibited_list, (int)(next_tr_base + 102),
173
                         (byte)(permission_base & 0xFF), ((permission_base >> 8) & 0xFF));
174
                layers.addFirst(tss_patch);
175
            }
176
 
177
            if(type == 4) {
178
                do {
179
                    next_rpl        = random.nextInt(4);
180
                    next_cr0_pe     = random.nextBoolean();
181
                    next_vm         = random.nextBoolean();
182
                    next_iopl       = random.nextInt(4);
183
                }while( !(next_cr0_pe && (next_vm || next_rpl > next_iopl)));
184
 
185
                do {
186
                    next_tr_valid   = random.nextBoolean();
187
                    next_tr_type    = random.nextInt(16);
188
                }while(next_tr_valid == false || (next_tr_type != 9 && next_tr_type != 11));
189
 
190
                do {
191
                    next_tr_limit   = random.nextInt(1048576);
192
                }while(next_tr_limit < 103);
193
 
194
                int permission_base = 0;
195
                do {
196
                    permission_base = random.nextInt(65536);
197
                    next_port       = random.nextInt(65536);
198
                    next_tr_limit   = random.nextInt(1048576-103); next_tr_limit += 103;
199
                }while(permission_base + (next_port/8) >= next_tr_limit);
200
 
201
                next_tr_base    = random.nextInt();
202
 
203
                MemoryPatchLayer tss_patch = new MemoryPatchLayer(random, prohibited_list, (int)(next_tr_base + 102),
204
                         (byte)(permission_base & 0xFF), ((permission_base >> 8) & 0xFF));
205
                layers.addFirst(tss_patch);
206
 
207
                int permission_bits = 0;
208
                if(random.nextInt(4) == 0) permission_bits = random.nextInt();
209
 
210
                 MemoryPatchLayer perm_patch = new MemoryPatchLayer(random, prohibited_list, (int)(next_tr_base + permission_base + next_port/8 + 0),
211
                         (byte)(permission_bits & 0xFF), (permission_bits >> 8) & 0xFF);
212
                 layers.addFirst(perm_patch);
213
            }
214
 
215
            if(type == 5) {
216
                do {
217
                    next_rpl        = random.nextInt(4);
218
                    next_cr0_pe     = random.nextBoolean();
219
                    next_vm         = random.nextBoolean();
220
                    next_iopl       = random.nextInt(4);
221
                }while( !(next_cr0_pe && (next_vm || next_rpl > next_iopl)));
222
 
223
                do {
224
                    next_tr_valid   = random.nextBoolean();
225
                    next_tr_type    = random.nextInt(16);
226
                }while(next_tr_valid == false || (next_tr_type != 9 && next_tr_type != 11));
227
 
228
                do {
229
                    next_tr_limit   = random.nextInt(1048576);
230
                }while(next_tr_limit < 103);
231
 
232
                int permission_base = 0;
233
                do {
234
                    permission_base = random.nextInt(65536);
235
                    next_port       = random.nextInt(65536);
236
                    next_tr_limit   = random.nextInt(1048576-103); next_tr_limit += 103;
237
                }while(permission_base + (next_port/8) >= next_tr_limit);
238
 
239
                next_tr_base    = random.nextInt();
240
 
241
                MemoryPatchLayer tss_patch = new MemoryPatchLayer(random, prohibited_list, (int)(next_tr_base + 102),
242
                         (byte)(permission_base & 0xFF), ((permission_base >> 8) & 0xFF));
243
                 layers.addFirst(tss_patch);
244
 
245
                int permission_bits = 0;
246
 
247
                 MemoryPatchLayer perm_patch = new MemoryPatchLayer(random, prohibited_list, (int)(next_tr_base + permission_base + next_port/8 + 0),
248
                         (byte)(permission_bits & 0xFF), (permission_bits >> 8) & 0xFF);
249
                 layers.addFirst(perm_patch);
250
            }
251
 
252
            InstructionLayer instr  = new InstructionLayer(random, prohibited_list, true);
253
            layers.add(instr);
254
            StackLayer stack        = new StackLayer(random, prohibited_list);
255
            layers.add(stack);
256
            layers.add(new OtherLayer(next_cr0_pe? OtherLayer.Type.PROTECTED_OR_V8086 : OtherLayer.Type.REAL, random));
257
            layers.add(new FlagsLayer(next_vm? FlagsLayer.Type.V8086 : FlagsLayer.Type.NOT_V8086, random));
258
            layers.add(new GeneralRegisterLayer(random));
259
            layers.add(new SegmentLayer(random));
260
            layers.add(new MemoryLayer(random));
261
            layers.add(new IOLayer(random));
262
            layers.addFirst(new HandleModeChangeLayer(
263
                    getInput("cr0_pe"),
264
                    getInput("vmflag"),
265
                    next_rpl, //getInput("cs_rpl"),
266
                    getInput("cs_p"),
267
                    getInput("cs_s"),
268
                    getInput("cs_type")
269
            ));
270
 
271
            // instruction size
272
            boolean cs_d_b = getInput("cs_d_b") == 1;
273
 
274
            boolean a32 = random.nextBoolean();
275
            boolean o32 = random.nextBoolean();
276
 
277
            long cs_limit = getInput("cs_limit");
278
            long cs_rpl   = getInput("cs_rpl");
279
 
280
 
281
            final int     final_port        = (random.nextInt() & 0xFFFF0000) | (next_port & 0xFFFF);
282
            final long    final_rpl         = cs_rpl;
283
            final boolean final_tr_valid    = next_tr_valid;
284
            final int     final_tr_base     = next_tr_base;
285
            final int     final_tr_limit    = next_tr_limit;
286
            final int     final_tr_type     = next_tr_type;
287
            final int     final_iopl        = next_iopl;
288
 
289
            Layer layer = new Layer() {
290
                long edx()          { return final_port; }
291
 
292
                long iopl()         { return final_iopl; }
293
 
294
                long cs_rpl()       { return final_rpl; }
295
 
296
                long tr_valid()     { return final_tr_valid? 1 : 0; }
297
                long tr_base()      { return final_tr_base; }
298
 
299
                long tr_limit()     { return final_tr_limit; }
300
 
301
                long dr7()          { return 0x22220400L; } // disable debug register
302
 
303
                long tr_type()      { return final_tr_type; }
304
            };
305
            layers.addFirst(layer);
306
 
307
            //-----------
308
            final Random final_random = random;
309
            Layer layer_rep = new Layer() {
310
                long ecx() { return final_random.nextInt(5); }
311
 
312
                long esi() {
313
                    int val = final_random.nextInt();
314
                    return ((val % 18) == 0)? 0 :
315
                           ((val % 18) == 1)? 1 :
316
                           ((val % 18) == 2)? 2 :
317
                           ((val % 18) == 3)? 3 :
318
                           ((val % 18) == 4)? 4 :
319
                           ((val % 18) == 5)? 0xFFFFFFFF :
320
                           ((val % 18) == 6)? 0x0000FFFF :
321
                                             final_random.nextInt() & ((final_random.nextInt(3) == 0)? 0xFFFFFFFF : 0x00000FFF);
322
                }
323
 
324
                long edi() {
325
                    int val = final_random.nextInt();
326
                    return ((val % 18) == 0)? 0 :
327
                           ((val % 18) == 1)? 1 :
328
                           ((val % 18) == 2)? 2 :
329
                           ((val % 18) == 3)? 3 :
330
                           ((val % 18) == 4)? 4 :
331
                           ((val % 18) == 5)? 0xFFFFFFFF :
332
                           ((val % 18) == 6)? 0x0000FFFF :
333
                                             final_random.nextInt() & ((final_random.nextInt(3) == 0)? 0xFFFFFFFF : 0x00000FFF);
334
                }
335
            };
336
            layers.addFirst(layer_rep);
337
            //-----------
338
 
339
            String instruction_string = prepare_instr(cs_d_b, a32, o32);
340
 
341
            // add instruction
342
            instruction = instruction_string + instruction_string + "9090900F0F";
343
 
344
            instr.add_instruction(instruction);
345
 
346
            // end condition
347
            break;
348
 
349
        }
350
 
351
        System.out.println("Instruction: [" + instruction + "]");
352
    }
353
 
354
    String prepare_instr(boolean cs_d_b, boolean a32, boolean o32) throws Exception {
355
        int opcodes[] = {
356
            0x6C,0x6D
357
        };
358
 
359
        String prefix = "";
360
        if(cs_d_b != o32) { prefix = "66" + prefix; }
361
        if(cs_d_b != a32) { prefix = "67" + prefix; }
362
        if(random.nextBoolean()) { prefix = "F2" + prefix; }
363
        if(random.nextBoolean()) { prefix = "F3" + prefix; }
364
 
365
        int opcode = opcodes[random.nextInt(opcodes.length)];
366
 
367
        int len = 1;
368
 
369
        byte instr[] = new byte[len];
370
        instr[0] = (byte)opcode;
371
        if(len >= 2) instr[1] = (byte)random.nextInt();
372
 
373
        return prefix + bytesToHex(instr);
374
    }
375
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.