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

Subversion Repositories ao486

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

powered by: WebSVN 2.1.0

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