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

Subversion Repositories ao486

[/] [ao486/] [trunk/] [ao486_tool/] [src/] [ao486/] [test/] [branch/] [TestCALL_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.EffectiveAddressLayerFactory;
32
import ao486.test.layers.FlagsLayer;
33
import ao486.test.layers.GeneralRegisterLayer;
34
import ao486.test.layers.HandleModeChangeLayer;
35
import ao486.test.layers.IOLayer;
36
import ao486.test.layers.InstructionLayer;
37
import ao486.test.layers.Layer;
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 ao486.test.layers.TSSCurrentLayer;
44
import java.io.*;
45
import java.util.LinkedList;
46
import java.util.Random;
47
 
48
 
49
public class TestCALL_task_switch extends TestUnit implements Serializable {
50
    public static void main(String args[]) throws Exception {
51
        run_test(TestCALL_task_switch.class);
52
    }
53
 
54
    //--------------------------------------------------------------------------
55
    @Override
56
    public int get_test_count() throws Exception {
57
        return 100;
58
    }
59
 
60
    @Override
61
    public void init() throws Exception {
62
 
63
        random = new Random(index + 31);
64
 
65
        String instruction;
66
        while(true) {
67
            layers.clear();
68
 
69
            LinkedList<Pair<Long, Long>> prohibited_list = new LinkedList<>();
70
 
71
            InstructionLayer instr = new InstructionLayer(random, prohibited_list);
72
            layers.add(instr);
73
            StackLayer stack = new StackLayer(random, prohibited_list);
74
            layers.add(stack);
75
            layers.add(new OtherLayer(OtherLayer.Type.PROTECTED_OR_V8086, random));
76
            layers.add(new FlagsLayer(FlagsLayer.Type.NOT_V8086, random));
77
            layers.add(new GeneralRegisterLayer(random));
78
            layers.add(new SegmentLayer(random));
79
            layers.add(new MemoryLayer(random));
80
            layers.add(new IOLayer(random));
81
 
82
            layers.addFirst(new HandleModeChangeLayer(
83
                    getInput("cr0_pe"),
84
                    getInput("vmflag"),
85
                    getInput("cs_rpl"),
86
                    getInput("cs_p"),
87
                    getInput("cs_s"),
88
                    getInput("cs_type")
89
            ));
90
 
91
            // instruction size
92
            boolean cs_d_b = getInput("cs_d_b") == 1;
93
 
94
            boolean a32 = random.nextBoolean();
95
            boolean o32 = random.nextBoolean();
96
 
97
            /* null check, selector limit checked in: TestCALL_protected_seg
98
             *
99
             * 0 - pre-(task switch) valid check
100
             *
101
             * >=1 - task switch tests
102
             */
103
 
104
            int type = 1;
105
            int task_switch_type = -1;
106
 
107
            DescriptorTableLayer tables = null;
108
            int new_tss_selector = random.nextInt(4);
109
            int old_tss_limit = 0xFFFF;
110
 
111
            TSSCurrentLayer.Type old_tss_type = random.nextBoolean()? TSSCurrentLayer.Type.BUSY_286 : TSSCurrentLayer.Type.BUSY_386;
112
 
113
            //------------------------------------------------------------------
114
            //------------------------------------------------------------------
115
 
116
            if(type >= 0) {
117
                boolean is_ldt = false;
118
 
119
                boolean conds[] = new boolean[4];
120
                int cond = 1 << random.nextInt(conds.length);
121
                if(type >= 1) cond = 0;
122
 
123
                int     new_cs_rpl  = 0;
124
                int     old_cs_rpl  = 0;
125
                boolean new_cs_seg  = false;
126
                int     new_cs_type = 0;
127
                int     new_cs_dpl  = 0;
128
                boolean new_cs_p    = false;
129
 
130
                do {
131
                    new_cs_seg  = false;
132
                    new_cs_type = random.nextBoolean()? 0x1 : 0x9; //AVAIL_TSS_286,386
133
 
134
                    new_cs_rpl  = random.nextInt(4);
135
                    old_cs_rpl  = random.nextInt(4);
136
                    new_cs_dpl  = random.nextInt(4);
137
                    new_cs_p    = random.nextBoolean();
138
                    is_ldt      = random.nextBoolean();
139
 
140
                    conds[0] = new_cs_dpl < old_cs_rpl;
141
                    conds[1] = new_cs_dpl < new_cs_rpl;
142
                    conds[2] = is_ldt;
143
                    conds[3] = new_cs_p == false;
144
                }
145
                while(!isAccepted(cond, conds[0],conds[1],conds[2],conds[3]));
146
 
147
                long new_cs_base, new_cs_limit;
148
                boolean new_cs_g;
149
                while(true) {
150
                    new_cs_base = Layer.norm(random.nextInt());
151
                    new_cs_g    = random.nextBoolean();
152
 
153
                    new_cs_limit = random.nextInt(new_cs_g? 0xF : 0xFFFF);
154
                    if(new_cs_g) new_cs_limit = (new_cs_limit << 12) | 0xFFF;
155
 
156
                    if( new_cs_base + new_cs_limit < 4294967296L &&
157
                        Layer.collides(prohibited_list, (int)new_cs_base, (int)(new_cs_base + new_cs_limit)) == false
158
                    ) break;
159
                }
160
 
161
                boolean new_cs_d_b = random.nextBoolean();
162
                boolean new_cs_l   = random.nextBoolean();
163
                boolean new_cs_avl = random.nextBoolean();
164
                long new_cs_limit_final = new_cs_g? new_cs_limit >> 12 : new_cs_limit;
165
                Descriptor cs_desc = new Descriptor((int)new_cs_base, (int)new_cs_limit_final, new_cs_type, new_cs_seg, new_cs_p, new_cs_dpl, new_cs_d_b, new_cs_g, new_cs_l, new_cs_avl);
166
 
167
System.out.printf("cs_desc: ");
168
for(int i=0; i<8; i++) System.out.printf("%02x ", cs_desc.get_byte(i));
169
System.out.printf("\n");
170
 
171
                final int old_cs_rpl_final = old_cs_rpl;
172
                Layer cs_rpl_layer = new Layer() {
173
                    long cs_rpl() { return old_cs_rpl_final; }
174
                };
175
                layers.addFirst(cs_rpl_layer);
176
 
177
                if(type == 0) {
178
                    tables = new DescriptorTableLayer(random, prohibited_list, true);
179
                    int index = tables.addDescriptor(is_ldt, cs_desc);
180
                    if(index == -1) continue;
181
 
182
                    index = index << 3;
183
                    if(is_ldt) index |= 4;
184
 
185
                    index |= new_cs_rpl;
186
 
187
                    new_tss_selector = index;
188
 
189
                    layers.addFirst(tables);
190
                }
191
 
192
System.out.printf("cond: %d\n", cond);
193
 
194
                if(type >= 1) {
195
                    boolean is_ok = TestTaskSwitch.test(random, this, prohibited_list, TestTaskSwitch.Source.FROM_CALL, cs_desc, new_cs_rpl, null, task_switch_type);
196
                    if(is_ok == false) continue;
197
 
198
                    tables              = TestTaskSwitch.tables;
199
                    new_tss_selector    = TestTaskSwitch.new_tss_selector;
200
                    old_tss_limit       = TestTaskSwitch.old_tss_limit;
201
                }
202
            }
203
 
204
            //------------------------------------------------------------------
205
            //------------------------------------------------------------------
206
 
207
            long new_eip = 0;
208
            long new_cs  = new_tss_selector;
209
 
210
            if(type >= 1) {
211
                TSSCurrentLayer old_tss = new TSSCurrentLayer(random, old_tss_type, old_tss_limit, new_tss_selector, prohibited_list);
212
                layers.addFirst(old_tss);
213
 
214
                layers.addFirst(tables);
215
            }
216
 
217
            // instruction
218
            byte extra_bytes[] = null;
219
 
220
            boolean is_Ep = random.nextBoolean();
221
 
222
            if(is_Ep) {
223
                byte modregrm_bytes[] = EffectiveAddressLayerFactory.prepare(
224
                        o32? (((new_cs & 0xFFFF) << 32) | (new_eip & 0xFFFFFFFF)) : (((new_cs & 0xFFFF) << 16) | (new_eip & 0xFFFF)),
225
                        3, EffectiveAddressLayerFactory.modregrm_reg_t.SET,
226
                        o32? 6 : 4, a32,
227
                        layers, random, this, true, false);
228
                extra_bytes = modregrm_bytes;
229
            }
230
            else {
231
                long immediate = o32? (((new_cs & 0xFFFF) << 32) | (new_eip & 0xFFFFFFFF)) : (((new_cs & 0xFFFF) << 16) | (new_eip & 0xFFFF));
232
 
233
                byte imm_bytes[] = new byte[o32? 6 : 4];
234
                for(int i=0; i<imm_bytes.length; i++) {
235
                    imm_bytes[i] = (byte)(immediate & 0xFF);
236
                    immediate >>= 8;
237
                }
238
                extra_bytes = imm_bytes;
239
            }
240
 
241
            instruction = prepare_instr(cs_d_b, a32, o32, extra_bytes, is_Ep);
242
            instr.add_instruction(instruction);
243
 
244
            // end condition
245
            break;
246
        }
247
 
248
        System.out.println("Instruction: [" + instruction + "]");
249
    }
250
 
251
    String prepare_instr(boolean cs_d_b, boolean a32, boolean o32, byte extra_bytes[], boolean is_Ep) throws Exception {
252
        int opcodes[] = {
253
            0xFF, 0x9A
254
        };
255
 
256
        String prefix = "";
257
        if(cs_d_b != o32) { prefix = "66" + prefix; }
258
        if(cs_d_b != a32) { prefix = "67" + prefix; }
259
 
260
        int opcode = opcodes[is_Ep? 0 : 1];
261
 
262
        byte instr[] = new byte[1 + extra_bytes.length];
263
        instr[0] = (byte)opcode;
264
        System.arraycopy(extra_bytes, 0, instr, 1, extra_bytes.length);
265
 
266
        return prefix + bytesToHex(instr);
267
    }
268
 
269
}

powered by: WebSVN 2.1.0

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