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

Subversion Repositories ao486

[/] [ao486/] [trunk/] [ao486_tool/] [src/] [ao486/] [test/] [branch/] [TestJMP_call_gate.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.branch;
28
 
29
import ao486.test.TestUnit;
30
import ao486.test.layers.DescriptorTableLayer;
31
import ao486.test.layers.EffectiveAddressLayerFactory;
32
import ao486.test.layers.FlagsLayer;
33
import ao486.test.layers.GeneralRegisterLayer;
34
import ao486.test.layers.HandleModeChangeLayer;
35
import ao486.test.layers.IOLayer;
36
import ao486.test.layers.InstructionLayer;
37
import ao486.test.layers.Layer;
38
import ao486.test.layers.MemoryLayer;
39
import ao486.test.layers.MemoryPatchLayer;
40
import ao486.test.layers.OtherLayer;
41
import ao486.test.layers.Pair;
42
import ao486.test.layers.SegmentLayer;
43
import ao486.test.layers.StackLayer;
44
import java.io.*;
45
import java.util.LinkedList;
46
import java.util.Random;
47
 
48
 
49
public class TestJMP_call_gate extends TestUnit implements Serializable {
50
    public static void main(String args[]) throws Exception {
51
        run_test(TestJMP_call_gate.class);
52
    }
53
 
54
    //--------------------------------------------------------------------------
55
    @Override
56
    public int get_test_count() throws Exception {
57
        return 100;
58
    }
59
 
60
    @Override
61
    public void init() throws Exception {
62
 
63
        random = new Random(15 + index);
64
 
65
        String instruction;
66
        while(true) {
67
            layers.clear();
68
 
69
            LinkedList<Pair<Long, Long>> prohibited_list = new LinkedList<>();
70
 
71
            InstructionLayer instr = new InstructionLayer(random, prohibited_list);
72
            layers.add(instr);
73
            StackLayer stack = new StackLayer(random, prohibited_list);
74
            layers.add(stack);
75
            layers.add(new OtherLayer(OtherLayer.Type.PROTECTED_OR_V8086, random));
76
            layers.add(new FlagsLayer(FlagsLayer.Type.NOT_V8086, random));
77
            layers.add(new GeneralRegisterLayer(random));
78
            layers.add(new SegmentLayer(random));
79
            layers.add(new MemoryLayer(random));
80
            layers.add(new IOLayer(random));
81
 
82
            layers.addFirst(new HandleModeChangeLayer(
83
                    getInput("cr0_pe"),
84
                    getInput("vmflag"),
85
                    getInput("cs_rpl"),
86
                    getInput("cs_p"),
87
                    getInput("cs_s"),
88
                    getInput("cs_type")
89
            ));
90
 
91
            // instruction size
92
            boolean cs_d_b = getInput("cs_d_b") == 1;
93
 
94
            boolean a32 = random.nextBoolean();
95
            boolean o32 = random.nextBoolean();
96
 
97
            /* null check, selector limit checked in: TestJMP_protected_seg
98
             *
99
             * 0 - pre-(call gate) valid check
100
             * 1 - cs_selector NULL
101
             * 2 - cs_selector out of bounds
102
             * 3 - cs_descriptor valid check
103
             * 4 - eip out of bounds
104
             *
105
             * 5 - all ok
106
             */
107
 
108
            int type = random.nextInt(6);
109
 
110
 
111
            DescriptorTableLayer tables = null;
112
 
113
            //------------------------------------------------------------------
114
            //------------------------------------------------------------------
115
 
116
            // prepare cs descriptor
117
            boolean is_cs_ldt = (type == 1)? false : random.nextBoolean();
118
 
119
            boolean conds[] = new boolean[5];
120
            int cond = 1 << random.nextInt(conds.length);
121
            if(type >= 4) cond = 0;
122
 
123
            int     new_cs_rpl  = 0;
124
            boolean new_cs_seg  = false;
125
            int     new_cs_type = 0;
126
            int     new_cs_dpl  = 0;
127
            boolean new_cs_p    = false;
128
            int     old_cs_rpl  = 0;
129
 
130
            do {
131
                new_cs_seg  = random.nextBoolean();
132
                new_cs_type = random.nextInt(16);
133
                new_cs_p    = random.nextBoolean();
134
 
135
                new_cs_rpl  = random.nextInt(4);
136
                new_cs_dpl  = random.nextInt(4);
137
 
138
                old_cs_rpl  = random.nextInt(4);
139
 
140
 
141
                conds[0] = new_cs_seg == false;
142
                conds[1] = ((new_cs_type >> 3) & 1) == 0; // data
143
                conds[2] = ((new_cs_type >> 2) & 1) == 0 && new_cs_dpl != old_cs_rpl;
144
                conds[3] = ((new_cs_type >> 2) & 1) == 1 && new_cs_dpl > old_cs_rpl;
145
                conds[4] = new_cs_p == false;
146
            }
147
            while(!isAccepted(cond, conds[0],conds[1],conds[2],conds[3],conds[4]));
148
 
149
System.out.printf("cond cs: %d\n", cond);
150
 
151
            long new_cs_base, new_cs_limit;
152
            boolean new_cs_g;
153
            while(true) {
154
                new_cs_base = Layer.norm(random.nextInt());
155
                new_cs_g    = random.nextBoolean();
156
 
157
                new_cs_limit = random.nextInt(new_cs_g? 0xF : 0xFFFF);
158
                if(new_cs_g) new_cs_limit = (new_cs_limit << 12) | 0xFFF;
159
 
160
                if( new_cs_base + new_cs_limit < 4294967296L &&
161
                    Layer.collides(prohibited_list, (int)new_cs_base, (int)(new_cs_base + new_cs_limit)) == false
162
                ) break;
163
            }
164
 
165
            boolean new_cs_d_b = random.nextBoolean();
166
            boolean new_cs_l   = random.nextBoolean();
167
            boolean new_cs_avl = random.nextBoolean();
168
            long new_cs_limit_final = new_cs_g? new_cs_limit >> 12 : new_cs_limit;
169
            Descriptor cs_desc = new Descriptor((int)new_cs_base, (int)new_cs_limit_final, new_cs_type, new_cs_seg, new_cs_p, new_cs_dpl, new_cs_d_b, new_cs_g, new_cs_l, new_cs_avl);
170
 
171
System.out.printf("cs_desc: ");
172
for(int i=0; i<8; i++) System.out.printf("%02x ", cs_desc.get_byte(i));
173
System.out.printf("\n");
174
 
175
            //-------
176
            tables = new DescriptorTableLayer(random, prohibited_list, true);
177
 
178
            int index = -1;
179
            if(type == 1) {
180
                index = random.nextInt(4);
181
            }
182
            else if(type == 2) {
183
                index = tables.getOutOfBoundsIndex(is_cs_ldt);
184
                if(index == -1) continue;
185
            }
186
            else {
187
                index = tables.addDescriptor(is_cs_ldt, cs_desc);
188
                if(index == -1) continue;
189
            }
190
 
191
            if(type != 1) {
192
                index <<= 3;
193
                if(is_cs_ldt) index |= 4;
194
                index |= new_cs_rpl;
195
            }
196
 
197
            //--------------------------------------------------------------
198
            // prepare call gate descriptor
199
 
200
            boolean is_cg_ldt = random.nextBoolean();
201
 
202
            conds = new boolean[2];
203
            cond = 1 << random.nextInt(conds.length);
204
            if(type >= 1) cond = 0;
205
 
206
            int     new_cg_rpl  = 0;
207
            boolean new_cg_seg  = false;
208
            int     new_cg_type = 0;
209
            int     new_cg_dpl  = 0;
210
            boolean new_cg_p    = false;
211
 
212
            do {
213
                new_cg_seg  = false;
214
                new_cg_type = random.nextBoolean()? 0x4 : 0xc; //CALL_GATE 286,386
215
 
216
                new_cg_rpl  = random.nextInt(4);
217
                new_cg_dpl  = random.nextInt(4);
218
                new_cg_p    = random.nextBoolean();
219
                is_cg_ldt   = random.nextBoolean();
220
 
221
                if((cond & 1) == 1 && old_cs_rpl == 0) {
222
                    cond &= 0xFE;
223
                    cond |= 2;
224
                }
225
 
226
                conds[0] = new_cg_dpl < old_cs_rpl || new_cg_dpl < new_cg_rpl;
227
                conds[1] = new_cg_p == false;
228
            }
229
            while(!isAccepted(cond, conds[0],conds[1]));
230
 
231
            long new_cg_base  = index;
232
            long new_cg_limit = Layer.norm(random.nextInt(0xFFFFF+1));
233
            boolean new_cg_g  = random.nextBoolean();
234
 
235
            boolean new_cg_d_b = random.nextBoolean();
236
            boolean new_cg_l   = random.nextBoolean();
237
            boolean new_cg_avl = random.nextBoolean();
238
            long new_cg_limit_final = new_cg_g? new_cg_limit >> 12 : new_cg_limit;
239
            Descriptor cg_desc = new Descriptor((int)new_cg_base, (int)new_cg_limit_final, new_cg_type, new_cg_seg, new_cg_p, new_cg_dpl, new_cg_d_b, new_cg_g, new_cg_l, new_cg_avl);
240
 
241
//------------
242
            final int old_cs_rpl_final = old_cs_rpl;
243
            Layer cs_rpl_layer = new Layer() {
244
                long cs_rpl() { return old_cs_rpl_final; }
245
            };
246
            layers.addFirst(cs_rpl_layer);
247
 
248
            // eip limit
249
            long eip = 0;
250
            if(type == 4) {
251
                while(true) {
252
                    eip = new_cs_limit + 1 + random.nextInt(10);
253
 
254
                    if(new_cg_type == 0x4) eip &= 0xFFFF;
255
 
256
                    if(eip > new_cs_limit) break;
257
                }
258
                if(new_cg_type == 0x4) eip |= (random.nextInt() & 0xFFFF0000);
259
            }
260
            else {
261
                while(true) {
262
                    eip = Layer.norm(random.nextInt((int)new_cs_limit+1));
263
 
264
                    if(new_cg_type == 0x4) eip &= 0xFFFF;
265
 
266
                    if(eip <= new_cs_limit) break;
267
                }
268
                long dest = new_cs_base + eip;
269
                // adding always possible
270
                MemoryPatchLayer patch = new MemoryPatchLayer(random, prohibited_list, (int)dest, 0x0F,0x0F);
271
                layers.addFirst(patch);
272
 
273
                if(new_cg_type == 0x4) eip |= (random.nextInt() & 0xFFFF0000);
274
            }
275
            cg_desc.set_dest_offset(eip);
276
 
277
System.out.printf("cg_desc: ");
278
for(int i=0; i<8; i++) System.out.printf("%02x ", cg_desc.get_byte(i));
279
System.out.printf("\n");
280
 
281
            //----------
282
            index = tables.addDescriptor(is_cg_ldt, cg_desc);
283
            if(index == -1) continue;
284
 
285
            index = index << 3;
286
            if(is_cg_ldt) index |= 4;
287
            index |= new_cg_rpl;
288
 
289
System.out.printf("cond cg: %d\n", cond);
290
 
291
            layers.addFirst(tables);
292
 
293
            //------------------------------------------------------------------
294
            //------------------------------------------------------------------
295
 
296
            long new_eip = 0;
297
            long new_cs  = index;
298
 
299
            // instruction
300
            byte extra_bytes[] = null;
301
 
302
            boolean is_Ep = random.nextBoolean();
303
 
304
            if(is_Ep) {
305
                byte modregrm_bytes[] = EffectiveAddressLayerFactory.prepare(
306
                        o32? (((new_cs & 0xFFFF) << 32) | (new_eip & 0xFFFFFFFF)) : (((new_cs & 0xFFFF) << 16) | (new_eip & 0xFFFF)),
307
                        5, EffectiveAddressLayerFactory.modregrm_reg_t.SET,
308
                        o32? 6 : 4, a32,
309
                        layers, random, this, true, false);
310
                extra_bytes = modregrm_bytes;
311
            }
312
            else {
313
                long immediate = o32? (((new_cs & 0xFFFF) << 32) | (new_eip & 0xFFFFFFFF)) : (((new_cs & 0xFFFF) << 16) | (new_eip & 0xFFFF));
314
 
315
                byte imm_bytes[] = new byte[o32? 6 : 4];
316
                for(int i=0; i<imm_bytes.length; i++) {
317
                    imm_bytes[i] = (byte)(immediate & 0xFF);
318
                    immediate >>= 8;
319
                }
320
                extra_bytes = imm_bytes;
321
            }
322
 
323
            instruction = prepare_instr(cs_d_b, a32, o32, extra_bytes, is_Ep);
324
            instr.add_instruction(instruction);
325
 
326
            // end condition
327
            break;
328
        }
329
 
330
        System.out.println("Instruction: [" + instruction + "]");
331
    }
332
 
333
    String prepare_instr(boolean cs_d_b, boolean a32, boolean o32, byte extra_bytes[], boolean is_Ep) throws Exception {
334
        int opcodes[] = {
335
            0xFF, 0xEA
336
        };
337
 
338
        String prefix = "";
339
        if(cs_d_b != o32) { prefix = "66" + prefix; }
340
        if(cs_d_b != a32) { prefix = "67" + prefix; }
341
 
342
        int opcode = opcodes[is_Ep? 0 : 1];
343
 
344
        byte instr[] = new byte[1 + extra_bytes.length];
345
        instr[0] = (byte)opcode;
346
        System.arraycopy(extra_bytes, 0, instr, 1, extra_bytes.length);
347
 
348
        return prefix + bytesToHex(instr);
349
    }
350
 
351
}

powered by: WebSVN 2.1.0

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