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

Subversion Repositories ao486

[/] [ao486/] [trunk/] [ao486_tool/] [src/] [ao486/] [test/] [io/] [TestOUTS_real_rep_interrupt.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.io;
28
 
29
import ao486.test.TestUnit;
30
import ao486.test.layers.FlagsLayer;
31
import ao486.test.layers.GeneralRegisterLayer;
32
import ao486.test.layers.HandleModeChangeLayer;
33
import ao486.test.layers.IOLayer;
34
import ao486.test.layers.InstructionLayer;
35
import ao486.test.layers.Layer;
36
import ao486.test.layers.MemoryLayer;
37
import ao486.test.layers.MemoryPatchLayer;
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 java.io.*;
43
import java.util.LinkedList;
44
import java.util.Random;
45
 
46
 
47
public class TestOUTS_real_rep_interrupt extends TestUnit implements Serializable {
48
    public static void main(String args[]) throws Exception {
49
        run_test(TestOUTS_real_rep_interrupt.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(86 + index);
62
 
63
        String instruction;
64
        while(true) {
65
            layers.clear();
66
 
67
            LinkedList<Pair<Long, Long>> prohibited_list = new LinkedList<>();
68
 
69
            // if false: v8086 mode
70
            boolean is_real = true;
71
 
72
            InstructionLayer instr  = new InstructionLayer(random, prohibited_list, true);
73
            layers.add(instr);
74
            StackLayer stack        = new StackLayer(random, prohibited_list);
75
            layers.add(stack);
76
            layers.add(new OtherLayer(is_real ? OtherLayer.Type.REAL : OtherLayer.Type.PROTECTED_OR_V8086, random));
77
            layers.add(new FlagsLayer(is_real ? FlagsLayer.Type.RANDOM : FlagsLayer.Type.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
            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
            long cs_limit = getInput("cs_limit");
98
 
99
            //----------------
100
 
101
            final Random final_random = random;
102
 
103
            Layer layer = new Layer() {
104
                long ecx() { return final_random.nextInt(5); }
105
 
106
                long ds_limit() { return 0xFFFF; }
107
                long es_limit() { return 0xFFFF; }
108
 
109
                long esi() {
110
                    return final_random.nextInt(0x1000) + 0x1000;
111
                    /*
112
                    int val = final_random.nextInt();
113
                    return ((val % 18) == 0)? 0 :
114
                           ((val % 18) == 1)? 1 :
115
                           ((val % 18) == 2)? 2 :
116
                           ((val % 18) == 3)? 3 :
117
                           ((val % 18) == 4)? 4 :
118
                           ((val % 18) == 5)? 0xFFFFFFFF :
119
                           ((val % 18) == 6)? 0x0000FFFF :
120
                                             final_random.nextInt() & ((final_random.nextInt(3) == 0)? 0xFFFFFFFF : 0x00000FFF);
121
                    */
122
                }
123
 
124
                long edi() {
125
                    return final_random.nextInt(0x1000) + 0x1000;
126
                    /*
127
                    int val = final_random.nextInt();
128
                    return ((val % 18) == 0)? 0 :
129
                           ((val % 18) == 1)? 1 :
130
                           ((val % 18) == 2)? 2 :
131
                           ((val % 18) == 3)? 3 :
132
                           ((val % 18) == 4)? 4 :
133
                           ((val % 18) == 5)? 0xFFFFFFFF :
134
                           ((val % 18) == 6)? 0x0000FFFF :
135
                                             final_random.nextInt() & ((final_random.nextInt(3) == 0)? 0xFFFFFFFF : 0x00000FFF);
136
                    */
137
                }
138
            };
139
            layers.addFirst(layer);
140
 
141
            //-----------------
142
 
143
            /* 0 - no rep; no interrupt
144
             * 1 - no rep; interrupt on 0
145
             * 2 - no rep; no interrupt; ecx = 0
146
             * 3 - rep;    no interrupt; ecx = 0
147
             * 4 - rep;    interrupt on 0
148
             * 5 - rep;    interrupt on 8
149
             * 6 - rep;    interrupt on 9
150
             */
151
            int type = random.nextInt(7);
152
 
153
            int interrupt_position = -1;
154
 
155
            if(type == 0) {
156
                interrupt_position = -1;
157
            }
158
            else if(type == 1) {
159
                interrupt_position = 0;
160
            }
161
            else if(type == 2) {
162
                interrupt_position = -1;
163
            }
164
            else if(type == 3) {
165
                interrupt_position = -1;
166
            }
167
            else if(type == 4) {
168
                interrupt_position = 0;
169
            }
170
            else if(type == 5) {
171
                interrupt_position = 8;
172
            }
173
            else if(type == 6) {
174
                interrupt_position = 9;
175
            }
176
            final int interrupt_position_final = interrupt_position;
177
 
178
 
179
            final int test_type = (type == 0)? 3 : (type == 1 || type == 2 || type == 3)? 4 : 12;
180
            final int ecx       = (type == 2 || type == 3)? 0 : 10;
181
            Layer esp_layer = new Layer() {
182
                long esp()          { return 0xF0; }
183
                long ss_base()      { return 0; }
184
                long iflag()        { return 1; }
185
                long tflag()        { return 0; }
186
 
187
                long cs_d_b()       { return 0; }
188
 
189
                long ecx()          { return ecx; }
190
 
191
                long check_interrupt(long counter) { return counter == interrupt_position_final? 0xAB : 0x100; }
192
 
193
                long get_test_type(){ return test_type; }
194
            };
195
            layers.addFirst(esp_layer);
196
 
197
            long handler = cs_limit;
198
 
199
            //0xAB -- interrupt vector = 0xAB*4 = 0x2AC linear
200
            MemoryPatchLayer int_patch = new MemoryPatchLayer(random, prohibited_list, (int)(0xAB*4), (byte)(handler&0xFF),(byte)((handler>>8)&0xFF), 0x00,0x00);
201
            layers.addFirst(int_patch);
202
 
203
            //interrupt handler: simply IRET
204
            MemoryPatchLayer handler_patch = new MemoryPatchLayer(random, prohibited_list, (int)handler, 0xCF);
205
            layers.addFirst(handler_patch);
206
 
207
            // byte or word/double word
208
            String instr_string = prepare_instr(cs_d_b, a32, o32);
209
            String rep_string   = (type == 0 || type == 1 || type == 2)? "" : random.nextBoolean()? "F2" : "F3";
210
 
211
            // add instruction
212
            instruction = rep_string + instr_string + "90909090909090909090909090909090";
213
 
214
            instr.add_instruction(instruction);
215
 
216
            //-----------------
217
 
218
            String instruction_string = prepare_instr(cs_d_b, a32, o32);
219
 
220
            // add instruction
221
            instruction = instruction_string + instruction_string + "9090900F0F";
222
 
223
            instr.add_instruction(instruction);
224
 
225
            // end condition
226
            break;
227
        }
228
 
229
        System.out.println("Instruction: [" + instruction + "]");
230
    }
231
    String prepare_instr(boolean cs_d_b, boolean a32, boolean o32) throws Exception {
232
        int opcodes[] = {
233
            0x6E,0x6F
234
        };
235
 
236
        String prefix = "";
237
        if(cs_d_b != o32) { prefix = "66" + prefix; }
238
        if(cs_d_b != a32) { prefix = "67" + prefix; }
239
 
240
        int opcode = opcodes[random.nextInt(opcodes.length)];
241
 
242
        int len = 1;
243
 
244
        byte instr[] = new byte[len];
245
        instr[0] = (byte)opcode;
246
        if(len >= 2) instr[1] = (byte)random.nextInt();
247
 
248
        return prefix + bytesToHex(instr);
249
    }
250
}

powered by: WebSVN 2.1.0

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