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

Subversion Repositories ao486

[/] [ao486/] [trunk/] [ao486_tool/] [src/] [ao486/] [test/] [branch/] [TestIRET_task_switch.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.FlagsLayer;
32
import ao486.test.layers.GeneralRegisterLayer;
33
import ao486.test.layers.HandleModeChangeLayer;
34
import ao486.test.layers.IOLayer;
35
import ao486.test.layers.InstructionLayer;
36
import ao486.test.layers.Layer;
37
import ao486.test.layers.MemoryLayer;
38
import ao486.test.layers.OtherLayer;
39
import ao486.test.layers.Pair;
40
import ao486.test.layers.SegmentLayer;
41
import ao486.test.layers.StackLayer;
42
import ao486.test.layers.TSSCurrentLayer;
43
import java.io.*;
44
import java.util.LinkedList;
45
import java.util.Random;
46
 
47
 
48
public class TestIRET_task_switch extends TestUnit implements Serializable {
49
    public static void main(String args[]) throws Exception {
50
        run_test(TestIRET_task_switch.class);
51
    }
52
 
53
    //--------------------------------------------------------------------------
54
    @Override
55
    public int get_test_count() throws Exception {
56
        return 100;
57
    }
58
 
59
    @Override
60
    public void init() throws Exception {
61
 
62
        random = new Random(3 + index);
63
 
64
        String instruction;
65
        while(true) {
66
            layers.clear();
67
 
68
            LinkedList<Pair<Long, Long>> prohibited_list = new LinkedList<>();
69
 
70
            InstructionLayer instr  = new InstructionLayer(random, prohibited_list);
71
            layers.add(instr);
72
            StackLayer stack        = new StackLayer(random, prohibited_list);
73
            layers.add(stack);
74
            layers.add(new OtherLayer(OtherLayer.Type.PROTECTED_OR_V8086, random));
75
            layers.add(new FlagsLayer(FlagsLayer.Type.NOT_V8086_NT, random));
76
            layers.add(new GeneralRegisterLayer(random));
77
            layers.add(new SegmentLayer(random));
78
            layers.add(new MemoryLayer(random));
79
            layers.add(new IOLayer(random));
80
 
81
            layers.addFirst(new HandleModeChangeLayer(
82
                    getInput("cr0_pe"),
83
                    getInput("vmflag"),
84
                    getInput("cs_rpl"),
85
                    getInput("cs_p"),
86
                    getInput("cs_s"),
87
                    getInput("cs_type")
88
            ));
89
 
90
            // instruction size
91
            boolean cs_d_b = getInput("cs_d_b") == 1;
92
 
93
            boolean a32 = random.nextBoolean();
94
            boolean o32 = random.nextBoolean();
95
 
96
            /* 0 - link tss selector with TI set
97
             * 1 - TSS descriptor out of bounds
98
             * 2 - invalid TSS descriptor
99
             *
100
             * >=3 - task switch tests
101
             */
102
 
103
            int type = random.nextInt(4);
104
            int task_switch_type = -1;
105
 
106
            DescriptorTableLayer tables = null;
107
            int new_tss_selector = -1;
108
            int old_tss_limit = 0xFFFF;
109
 
110
            TSSCurrentLayer.Type old_tss_type = random.nextBoolean()? TSSCurrentLayer.Type.BUSY_286 : TSSCurrentLayer.Type.BUSY_386;
111
 
112
            //------------------------------------------------------------------
113
            //------------------------------------------------------------------
114
 
115
 
116
 
117
 
118
            //------------------------------------------------------------------
119
            //------------------------------------------------------------------
120
 
121
            if(type == 0) {
122
                new_tss_selector = random.nextInt(65536);
123
                new_tss_selector |= 4;
124
 
125
                TSSCurrentLayer current_tss = new TSSCurrentLayer(random, old_tss_type, 0xFFFF, new_tss_selector, prohibited_list);
126
                layers.addFirst(current_tss);
127
            }
128
            else if(type == 1) {
129
                tables = new DescriptorTableLayer(random, prohibited_list, true);
130
 
131
                new_tss_selector = tables.getOutOfBoundsIndex(false);
132
                if(new_tss_selector == -1) continue;
133
 
134
                new_tss_selector <<= 3;
135
                new_tss_selector |= random.nextInt(0x8);
136
            }
137
            else if(type == 2) {
138
 
139
                boolean conds[] = new boolean[3];
140
                int cond = 1 << random.nextInt(conds.length);
141
 
142
                boolean new_tss_seg  = false;
143
                int     new_tss_type = 0;
144
                boolean new_tss_p    = false;
145
 
146
                int     new_tss_dpl  = random.nextInt(4);
147
                boolean new_tss_d_b  = random.nextBoolean();
148
                boolean new_tss_l    = random.nextBoolean();
149
                boolean new_tss_avl  = random.nextBoolean();
150
                int     new_tss_rpl  = random.nextInt(4);
151
 
152
                do {
153
                    new_tss_seg = random.nextBoolean();
154
                    new_tss_type= random.nextInt(16);
155
                    new_tss_p   = random.nextBoolean();
156
 
157
                    conds[0] = new_tss_seg;
158
                    conds[1] = new_tss_type != 0x3 && new_tss_type != 0xb;
159
                    conds[2] = new_tss_p == false;
160
                }
161
                while(!isAccepted(cond, conds[0],conds[1],conds[2]));
162
 
163
                //---------
164
                long new_tss_base, new_tss_limit;
165
                boolean new_tss_g;
166
                while(true) {
167
                    new_tss_base = Layer.norm(random.nextInt());
168
                    new_tss_g    = random.nextBoolean();
169
 
170
                    new_tss_limit = random.nextInt(new_tss_g? 0xF+1 : 0xFFFF + 1);
171
                    if(new_tss_g) new_tss_limit = (new_tss_limit << 12) | 0xFFF;
172
 
173
                    if( new_tss_base + new_tss_limit < 4294967296L &&
174
                        Layer.collides(prohibited_list, (int)new_tss_base, (int)(new_tss_base + new_tss_limit)) == false )
175
                    {
176
                        prohibited_list.add(new Pair<>(new_tss_base, new_tss_base + new_tss_limit));
177
                        break;
178
                    }
179
                }
180
 
181
                long new_tss_limit_final = new_tss_g? new_tss_limit >> 12 : new_tss_limit;
182
 
183
                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);
184
 
185
                tables = new DescriptorTableLayer(random, prohibited_list, true);
186
                new_tss_selector = tables.addDescriptor(false, tss_desc);
187
                if(new_tss_selector == -1) continue;
188
 
189
                //copy
190
                new_tss_selector <<= 3;
191
                new_tss_selector |= new_tss_rpl;
192
            }
193
            else if(type >= 3) {
194
                boolean conds[] = new boolean[3];
195
                int cond = 0;
196
 
197
                boolean new_tss_seg  = false;
198
                int     new_tss_type = 0;
199
                boolean new_tss_p    = false;
200
 
201
                int     new_tss_dpl  = random.nextInt(4);
202
                boolean new_tss_d_b  = random.nextBoolean();
203
                boolean new_tss_l    = random.nextBoolean();
204
                boolean new_tss_avl  = random.nextBoolean();
205
                int     new_tss_rpl  = random.nextInt(4);
206
 
207
                do {
208
                    new_tss_seg = random.nextBoolean();
209
                    new_tss_type= (type == 8 || type == 9)? 0xB : random.nextInt(16);
210
                    new_tss_p   = random.nextBoolean();
211
 
212
                    conds[0] = new_tss_seg;
213
                    conds[1] = new_tss_type != 0x3 && new_tss_type != 0xb;
214
                    conds[2] = new_tss_p == false;
215
                }
216
                while(!isAccepted(cond, conds[0],conds[1],conds[2]));
217
 
218
                //---------
219
                long new_tss_base, new_tss_limit;
220
                boolean new_tss_g;
221
                while(true) {
222
                    new_tss_base = Layer.norm(random.nextInt());
223
                    new_tss_g    = random.nextBoolean();
224
 
225
                    new_tss_limit = random.nextInt(new_tss_g? 0xF+1 : 0xFFFF + 1);
226
                    if(new_tss_g) new_tss_limit = (new_tss_limit << 12) | 0xFFF;
227
 
228
                    if( new_tss_base + new_tss_limit < 4294967296L &&
229
                        Layer.collides(prohibited_list, (int)new_tss_base, (int)(new_tss_base + new_tss_limit)) == false)
230
                    {
231
                        prohibited_list.add(new Pair<>(new_tss_base, new_tss_base + new_tss_limit));
232
                        break;
233
                    }
234
                }
235
 
236
                long new_tss_limit_final = new_tss_g? new_tss_limit >> 12 : new_tss_limit;
237
 
238
                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);
239
 
240
                //--------------------------------------------------------------
241
                //--------------------------------------------------------------
242
                //--------------------------------------------------------------
243
 
244
                boolean is_ok = TestTaskSwitch.test(random, this, prohibited_list, TestTaskSwitch.Source.FROM_IRET, tss_desc, new_tss_rpl, null, task_switch_type);
245
                if(is_ok == false) continue;
246
 
247
                tables              = TestTaskSwitch.tables;
248
                new_tss_selector    = TestTaskSwitch.new_tss_selector;
249
                old_tss_limit       = TestTaskSwitch.old_tss_limit;
250
            }
251
 
252
            //------------------------------------------------------------------
253
            //------------------------------------------------------------------
254
 
255
            if(type != 0) {
256
                TSSCurrentLayer old_tss = new TSSCurrentLayer(random, old_tss_type, old_tss_limit, new_tss_selector, prohibited_list);
257
                layers.addFirst(old_tss);
258
 
259
                layers.addFirst(tables);
260
            }
261
 
262
 
263
            // add instruction
264
            instruction = prepare_instr(cs_d_b, a32, o32);
265
            instr.add_instruction(instruction);
266
 
267
            // end condition
268
            break;
269
        }
270
 
271
        System.out.println("Instruction: [" + instruction + "]");
272
    }
273
 
274
    int imm_len(boolean a32, boolean o32, int opcode) {
275
        return 0;
276
    }
277
    String prepare_instr(boolean cs_d_b, boolean a32, boolean o32) throws Exception {
278
        int opcodes[] = {
279
            0xCF
280
        };
281
 
282
        String prefix = "";
283
        if(cs_d_b != o32) { prefix = "66" + prefix; }
284
        if(cs_d_b != a32) { prefix = "67" + prefix; }
285
 
286
        int     opcode      = opcodes[random.nextInt(opcodes.length)];
287
        boolean is_modregrm = false;
288
 
289
        byte possible_modregrm = (byte)random.nextInt();
290
        byte possible_sib      = (byte)random.nextInt();
291
 
292
        int len = (is_modregrm == false)? 1 : 1 + modregrm_len(!cs_d_b, unsigned(possible_modregrm), unsigned(possible_sib));
293
        len += imm_len(a32, o32, opcode);
294
 
295
 
296
        byte instr[] = new byte[len];
297
        instr[0] = (byte)opcode;
298
        for(int i=1; i<len; i++) {
299
            if(i==1)        instr[1] = possible_modregrm;
300
            else if(i==2)   instr[2] = possible_sib;
301
            else            instr[i] = (byte)random.nextInt();
302
        }
303
 
304
        return prefix + bytesToHex(instr);
305
    }
306
 
307
}

powered by: WebSVN 2.1.0

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