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

Subversion Repositories ao486

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

powered by: WebSVN 2.1.0

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