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

Subversion Repositories ao486

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

powered by: WebSVN 2.1.0

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