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

Subversion Repositories ao486

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

powered by: WebSVN 2.1.0

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