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

Subversion Repositories ao486

[/] [ao486/] [trunk/] [ao486_tool/] [src/] [ao486/] [test/] [segment/] [TestLTR.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.segment;
28
 
29
import ao486.test.TestUnit;
30
import static ao486.test.TestUnit.run_test;
31
import ao486.test.layers.DescriptorTableLayer;
32
import ao486.test.layers.EffectiveAddressLayerFactory;
33
import ao486.test.layers.FlagsLayer;
34
import ao486.test.layers.GeneralRegisterLayer;
35
import ao486.test.layers.HandleModeChangeLayer;
36
import ao486.test.layers.IOLayer;
37
import ao486.test.layers.InstructionLayer;
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 java.io.Serializable;
44
import java.util.LinkedList;
45
import java.util.Random;
46
 
47
public class TestLTR extends TestUnit implements Serializable {
48
    public static void main(String args[]) throws Exception {
49
        run_test(TestLTR.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(707 + index);
62
 
63
        /* 0. not protected mode
64
         * 1. protected mode and CPL != 0
65
         * 2. zero selector
66
         *      - null selector TI = 0
67
         *      - null selector TI = 1
68
         * 3. fetch descriptor failed:
69
         *      - index over limit GDT
70
         * 4. descriptor check failed:
71
         * 5. all ok
72
         */
73
 
74
        int type = 5; //random.nextInt(6);
75
 
76
        String instruction;
77
        while(true) {
78
            layers.clear();
79
 
80
            long    next_cs_rpl   = 0;
81
            boolean descr_seg     = false;
82
            int     descr_type    = 0;
83
            int     descr_dpl     = 0;
84
            boolean descr_present = false;
85
            long    selector_rpl  = 0;
86
 
87
            if(type == 0)      next_cs_rpl = random.nextInt(4);
88
            else if(type == 1) next_cs_rpl = 1 + random.nextInt(3);
89
            else               next_cs_rpl = 0;
90
 
91
 
92
            int cond = (type == 5)? 0 : 1 << (random.nextInt(100000) % 3);
93
 
94
            System.out.printf("cond: %d, index: %d\n", cond, index);
95
 
96
            boolean lldt_cond_1;
97
            boolean lldt_cond_2;
98
            boolean lldt_cond_3;
99
 
100
            do {
101
                descr_seg       = random.nextBoolean();
102
                descr_type      = random.nextInt(16);
103
                descr_dpl       = random.nextInt(4);
104
                descr_present   = (type == 5)? true : random.nextBoolean();
105
                selector_rpl    = random.nextInt(4);
106
 
107
 
108
                lldt_cond_1 = descr_seg;
109
                lldt_cond_2 = descr_type != 1 && descr_type != 9;
110
                lldt_cond_3 = descr_present == false;
111
            }
112
            while(!isAccepted(cond, lldt_cond_1,lldt_cond_2,lldt_cond_3));
113
 
114
 
115
            //0-real; 1-v8086; 2-protected
116
            int mode = (type == 0)? random.nextInt(2) : 2;
117
 
118
            LinkedList<Pair<Long, Long>> prohibited_list = new LinkedList<>();
119
 
120
            InstructionLayer instr = new InstructionLayer(random, prohibited_list);
121
            layers.add(instr);
122
            layers.add(new StackLayer(random, prohibited_list));
123
            layers.add(new OtherLayer((mode >= 1)? OtherLayer.Type.PROTECTED_OR_V8086 : OtherLayer.Type.REAL, random));
124
            layers.add(new FlagsLayer((mode == 1)? FlagsLayer.Type.V8086 : (mode == 2)? FlagsLayer.Type.NOT_V8086 : FlagsLayer.Type.RANDOM, random));
125
            layers.add(new GeneralRegisterLayer(random));
126
            layers.add(new SegmentLayer(random));
127
            layers.add(new MemoryLayer(random));
128
            layers.add(new IOLayer(random));
129
            layers.addFirst(new HandleModeChangeLayer(
130
                    getInput("cr0_pe"),
131
                    getInput("vmflag"),
132
                    next_cs_rpl, //getInput("cs_rpl"),
133
                    getInput("cs_p"),
134
                    getInput("cs_s"),
135
                    getInput("cs_type")
136
            ));
137
 
138
            // instruction size
139
            boolean cs_d_b = getInput("cs_d_b") == 1;
140
            long    cs_rpl = getInput("cs_rpl");
141
 
142
            boolean a32 = random.nextBoolean();
143
            boolean o32 = random.nextBoolean();
144
 
145
            long selector = 0;
146
 
147
            if(type == 2) {
148
                selector = random.nextInt(4);
149
 
150
                if(random.nextBoolean()) selector |= (random.nextInt(500) | 1) << 2;
151
            }
152
            else if(type == 3) {
153
                boolean ldtr_valid = random.nextInt(5) != 0;
154
 
155
                DescriptorTableLayer tables = new DescriptorTableLayer(random, prohibited_list, ldtr_valid);
156
 
157
                boolean is_ldt = false;
158
 
159
                int index = tables.getOutOfBoundsIndex(is_ldt);
160
                if(index == -1) continue;
161
 
162
                if(ldtr_valid == false && is_ldt) index = 0;
163
 
164
                index = index << 3;
165
                if(is_ldt) index |= 4;
166
 
167
                index |= random.nextInt(4);
168
 
169
                selector = index;
170
 
171
                layers.addFirst(tables);
172
            }
173
            else if(type >= 4) {
174
                TestUnit.Descriptor desc = new TestUnit.Descriptor(
175
                        random.nextInt(), //base
176
                        random.nextInt() & 0xFFFFF, //limit
177
                        descr_type,
178
                        descr_seg,
179
                        descr_present,
180
                        descr_dpl,
181
                        random.nextBoolean(), //d_b
182
                        random.nextBoolean(), //g
183
                        random.nextBoolean(), //l
184
                        random.nextBoolean()  //avl
185
                );
186
 
187
                DescriptorTableLayer tables = new DescriptorTableLayer(random, prohibited_list, true);
188
 
189
                boolean is_ldt = random.nextBoolean();
190
 
191
                int index = tables.addDescriptor(is_ldt, desc);
192
                if(index == -1) continue;
193
 
194
                index = index << 3;
195
                if(is_ldt) index |= 4;
196
 
197
                index |= selector_rpl;
198
 
199
                selector = index;
200
 
201
                layers.addFirst(tables);
202
            }
203
 
204
            byte extra_bytes[] = null;
205
 
206
            byte modregrm_bytes[] = EffectiveAddressLayerFactory.prepare(
207
                    selector,
208
                    3, EffectiveAddressLayerFactory.modregrm_reg_t.SET,
209
                    2, a32,
210
                    layers, random, this, false, false);
211
            extra_bytes = modregrm_bytes;
212
 
213
            // instruction
214
            instruction = prepare_instr(cs_d_b, a32, o32, extra_bytes);
215
 
216
            instruction += instruction;
217
            instruction += "0F0F";
218
 
219
            // add instruction
220
            instr.add_instruction(instruction);
221
 
222
            // end condition
223
            break;
224
        }
225
 
226
        System.out.println("Instruction: [" + instruction + "]");
227
    }
228
 
229
    String prepare_instr(boolean cs_d_b, boolean a32, boolean o32, byte modregrm_bytes[]) throws Exception {
230
 
231
        int opcodes[] = {
232
            0x00
233
        };
234
 
235
        String prefix = "";
236
        if(cs_d_b != o32) { prefix = "66" + prefix; }
237
        if(cs_d_b != a32) { prefix = "67" + prefix; }
238
 
239
        int opcode = opcodes[random.nextInt(opcodes.length)];
240
 
241
        byte instr[] = new byte[1 + modregrm_bytes.length];
242
        instr[0] = (byte)opcode;
243
        System.arraycopy(modregrm_bytes, 0, instr, 1, modregrm_bytes.length);
244
 
245
        return prefix + "0F" + bytesToHex(instr);
246
    }
247
}

powered by: WebSVN 2.1.0

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