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

Subversion Repositories ao486

[/] [ao486/] [trunk/] [ao486_tool/] [src/] [ao486/] [test/] [branch/] [TestJMP_task_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.OtherLayer;
40
import ao486.test.layers.Pair;
41
import ao486.test.layers.SegmentLayer;
42
import ao486.test.layers.StackLayer;
43
import ao486.test.layers.TSSCurrentLayer;
44
import java.io.*;
45
import java.util.LinkedList;
46
import java.util.Random;
47
 
48
 
49
public class TestJMP_task_gate extends TestUnit implements Serializable {
50
    public static void main(String args[]) throws Exception {
51
        run_test(TestJMP_task_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(804 + 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: TestCALL_protected_seg
98
             *
99
             * 0 - pre-(task gate) valid check
100
             * 1 - tss_selector TI
101
             * 2 - tss_descriptor out of bounds
102
             * 3 - tss_descriptor valid check
103
             *
104
             * >=4 - task switch tests
105
             */
106
 
107
            int type = random.nextInt(5);
108
            int task_switch_test = -1;
109
 
110
            DescriptorTableLayer tables = null;
111
            int new_tss_selector = random.nextInt(4);
112
            int old_tss_limit = 0xFFFF;
113
 
114
            TSSCurrentLayer.Type old_tss_type = random.nextBoolean()? TSSCurrentLayer.Type.BUSY_286 : TSSCurrentLayer.Type.BUSY_386;
115
 
116
            //------------------------------------------------------------------
117
            //------------------------------------------------------------------
118
 
119
            if(type >= 0) {
120
                // prepare tss descriptor
121
                boolean is_tss_ldt = (type == 1)? true : false;
122
 
123
                boolean conds[] = new boolean[3];
124
                int cond = 1 << random.nextInt(conds.length);
125
                if(type >= 4) cond = 0;
126
 
127
                int     new_tss_rpl  = 0;
128
                boolean new_tss_seg  = false;
129
                int     new_tss_type = 0;
130
                int     new_tss_dpl  = 0;
131
                boolean new_tss_p    = false;
132
 
133
                do {
134
                    new_tss_seg  = random.nextBoolean();
135
                    new_tss_type = random.nextInt(16);
136
                    new_tss_p    = random.nextBoolean();
137
 
138
                    new_tss_rpl  = random.nextInt(4);
139
                    new_tss_dpl  = random.nextInt(4);
140
 
141
 
142
                    conds[0] = new_tss_seg;
143
                    conds[1] = new_tss_type != 0x1 && new_tss_type != 0x9; //AVAIL_TSS_286,386
144
                    conds[2] = new_tss_p == false;
145
                }
146
                while(!isAccepted(cond, conds[0],conds[1],conds[2]));
147
 
148
                long new_tss_base, new_tss_limit;
149
                boolean new_tss_g;
150
                while(true) {
151
                    new_tss_base = Layer.norm(random.nextInt());
152
                    new_tss_g    = random.nextBoolean();
153
 
154
                    new_tss_limit = random.nextInt(new_tss_g? 0xF : 0xFFFF);
155
                    if(new_tss_g) new_tss_limit = (new_tss_limit << 12) | 0xFFF;
156
 
157
                    if( new_tss_base + new_tss_limit < 4294967296L &&
158
                        Layer.collides(prohibited_list, (int)new_tss_base, (int)(new_tss_base + new_tss_limit)) == false
159
                    ) break;
160
                }
161
 
162
                boolean new_tss_d_b = random.nextBoolean();
163
                boolean new_tss_l   = random.nextBoolean();
164
                boolean new_tss_avl = random.nextBoolean();
165
                long new_tss_limit_final = new_tss_g? new_tss_limit >> 12 : new_tss_limit;
166
                Descriptor tss_desc = new Descriptor((int)new_tss_base, (int)new_tss_limit_final, new_tss_type, new_tss_seg, new_tss_p, new_tss_dpl, new_tss_d_b, new_tss_g, new_tss_l, new_tss_avl);
167
 
168
System.out.printf("tss_desc: ");
169
for(int i=0; i<8; i++) System.out.printf("%02x ", tss_desc.get_byte(i));
170
System.out.printf("\n");
171
 
172
                tables = new DescriptorTableLayer(random, prohibited_list, true);
173
 
174
                int index = -1;
175
                if(type != 2) {
176
                    index = tables.addDescriptor(is_tss_ldt, tss_desc);
177
                    if(index == -1) continue;
178
                }
179
                else {
180
                    index = tables.getOutOfBoundsIndex(is_tss_ldt);
181
                    if(index == -1) continue;
182
                }
183
 
184
                index <<= 3;
185
                if(is_tss_ldt) index |= 4;
186
                index |= new_tss_rpl;
187
 
188
 
189
                // prepare task gate descriptor
190
 
191
 
192
                boolean is_ldt = random.nextBoolean();
193
 
194
                conds = new boolean[3];
195
                cond = 1 << random.nextInt(conds.length);
196
                if(type >= 1) cond = 0;
197
 
198
                int     new_cs_rpl  = 0;
199
                int     old_cs_rpl  = 0;
200
                boolean new_cs_seg  = false;
201
                int     new_cs_type = 0;
202
                int     new_cs_dpl  = 0;
203
                boolean new_cs_p    = false;
204
 
205
                do {
206
                    new_cs_seg  = false;
207
                    new_cs_type = 0x5; //TASK_GATE
208
 
209
                    new_cs_rpl  = random.nextInt(4);
210
                    old_cs_rpl  = random.nextInt(4);
211
                    new_cs_dpl  = random.nextInt(4);
212
                    new_cs_p    = random.nextBoolean();
213
                    is_ldt      = random.nextBoolean();
214
 
215
                    conds[0] = new_cs_dpl < old_cs_rpl;
216
                    conds[1] = new_cs_dpl < new_cs_rpl;
217
                    conds[2] = new_cs_p == false;
218
                }
219
                while(!isAccepted(cond, conds[0],conds[1],conds[2]));
220
 
221
                long new_cs_base  = index;
222
                long new_cs_limit = Layer.norm(random.nextInt(0xFFFFF+1));
223
                boolean new_cs_g  = random.nextBoolean();
224
 
225
                boolean new_cs_d_b = random.nextBoolean();
226
                boolean new_cs_l   = random.nextBoolean();
227
                boolean new_cs_avl = random.nextBoolean();
228
                long new_cs_limit_final = new_cs_g? new_cs_limit >> 12 : new_cs_limit;
229
                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);
230
 
231
System.out.printf("cs_desc: ");
232
for(int i=0; i<8; i++) System.out.printf("%02x ", cs_desc.get_byte(i));
233
System.out.printf("\n");
234
 
235
                final int old_cs_rpl_final = old_cs_rpl;
236
                Layer cs_rpl_layer = new Layer() {
237
                    long cs_rpl() { return old_cs_rpl_final; }
238
                };
239
                layers.addFirst(cs_rpl_layer);
240
 
241
                //----------
242
                index = tables.addDescriptor(is_ldt, cs_desc);
243
                if(index == -1) continue;
244
 
245
                index = index << 3;
246
                if(is_ldt) index |= 4;
247
                index |= new_cs_rpl;
248
 
249
                new_tss_selector = index;
250
                TestTaskSwitch.new_tss_selector = new_tss_selector;
251
 
252
                if(type == 0) {
253
                    layers.addFirst(tables);
254
                }
255
 
256
System.out.printf("cond: %d\n", cond);
257
 
258
                if(type >= 4) {
259
                    boolean is_ok = TestTaskSwitch.test(random, this, prohibited_list, TestTaskSwitch.Source.FROM_JUMP, tss_desc, new_cs_rpl, tables, task_switch_test);
260
                    if(is_ok == false) continue;
261
 
262
                    tables              = TestTaskSwitch.tables;
263
                    new_tss_selector    = TestTaskSwitch.new_tss_selector;
264
                    old_tss_limit       = TestTaskSwitch.old_tss_limit;
265
                }
266
            }
267
 
268
            //------------------------------------------------------------------
269
            //------------------------------------------------------------------
270
 
271
            long new_eip = 0;
272
            long new_cs  = new_tss_selector;
273
 
274
            if(type >= 1) {
275
                TSSCurrentLayer old_tss = new TSSCurrentLayer(random, old_tss_type, old_tss_limit, new_tss_selector, prohibited_list);
276
                layers.addFirst(old_tss);
277
 
278
                layers.addFirst(tables);
279
            }
280
 
281
            // instruction
282
            byte extra_bytes[] = null;
283
 
284
            boolean is_Ep = random.nextBoolean();
285
 
286
            if(is_Ep) {
287
                byte modregrm_bytes[] = EffectiveAddressLayerFactory.prepare(
288
                        o32? (((new_cs & 0xFFFF) << 32) | (new_eip & 0xFFFFFFFF)) : (((new_cs & 0xFFFF) << 16) | (new_eip & 0xFFFF)),
289
                        5, EffectiveAddressLayerFactory.modregrm_reg_t.SET,
290
                        o32? 6 : 4, a32,
291
                        layers, random, this, true, false);
292
                extra_bytes = modregrm_bytes;
293
            }
294
            else {
295
                long immediate = o32? (((new_cs & 0xFFFF) << 32) | (new_eip & 0xFFFFFFFF)) : (((new_cs & 0xFFFF) << 16) | (new_eip & 0xFFFF));
296
 
297
                byte imm_bytes[] = new byte[o32? 6 : 4];
298
                for(int i=0; i<imm_bytes.length; i++) {
299
                    imm_bytes[i] = (byte)(immediate & 0xFF);
300
                    immediate >>= 8;
301
                }
302
                extra_bytes = imm_bytes;
303
            }
304
 
305
            instruction = prepare_instr(cs_d_b, a32, o32, extra_bytes, is_Ep);
306
            instr.add_instruction(instruction);
307
 
308
            // end condition
309
            break;
310
        }
311
 
312
        System.out.println("Instruction: [" + instruction + "]");
313
    }
314
 
315
    String prepare_instr(boolean cs_d_b, boolean a32, boolean o32, byte extra_bytes[], boolean is_Ep) throws Exception {
316
        int opcodes[] = {
317
            0xFF, 0xEA
318
        };
319
 
320
        String prefix = "";
321
        if(cs_d_b != o32) { prefix = "66" + prefix; }
322
        if(cs_d_b != a32) { prefix = "67" + prefix; }
323
 
324
        int opcode = opcodes[is_Ep? 0 : 1];
325
 
326
        byte instr[] = new byte[1 + extra_bytes.length];
327
        instr[0] = (byte)opcode;
328
        System.arraycopy(extra_bytes, 0, instr, 1, extra_bytes.length);
329
 
330
        return prefix + bytesToHex(instr);
331
    }
332
 
333
}

powered by: WebSVN 2.1.0

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