| 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.other;
  | 
      
      
         | 28 | 
          | 
          | 
          
  | 
      
      
         | 29 | 
          | 
          | 
         import ao486.test.TestUnit;
  | 
      
      
         | 30 | 
          | 
          | 
         import ao486.test.layers.EffectiveAddressLayerFactory;
  | 
      
      
         | 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 java.io.*;
  | 
      
      
         | 43 | 
          | 
          | 
         import java.util.LinkedList;
  | 
      
      
         | 44 | 
          | 
          | 
         import java.util.Random;
  | 
      
      
         | 45 | 
          | 
          | 
          
  | 
      
      
         | 46 | 
          | 
          | 
          
  | 
      
      
         | 47 | 
          | 
          | 
         public class TestMOV_CRx_load extends TestUnit implements Serializable {
  | 
      
      
         | 48 | 
          | 
          | 
             public static void main(String args[]) throws Exception {
  | 
      
      
         | 49 | 
          | 
          | 
                 run_test(TestMOV_CRx_load.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(4 + index);
  | 
      
      
         | 62 | 
          | 
          | 
          
  | 
      
      
         | 63 | 
          | 
          | 
                 String instruction;
  | 
      
      
         | 64 | 
          | 
          | 
                 while(true) {
  | 
      
      
         | 65 | 
          | 
          | 
                     layers.clear();
  | 
      
      
         | 66 | 
          | 
          | 
          
  | 
      
      
         | 67 | 
          | 
          | 
                     /* 0 - CPL != 0
  | 
      
      
         | 68 | 
          | 
          | 
                      * 1 - all ok
  | 
      
      
         | 69 | 
          | 
          | 
                      */
  | 
      
      
         | 70 | 
          | 
          | 
                     int type = random.nextInt(2);
  | 
      
      
         | 71 | 
          | 
          | 
          
  | 
      
      
         | 72 | 
          | 
          | 
                     LinkedList<Pair<Long, Long>> prohibited_list = new LinkedList<>();
  | 
      
      
         | 73 | 
          | 
          | 
          
  | 
      
      
         | 74 | 
          | 
          | 
                     // if false: v8086 mode
  | 
      
      
         | 75 | 
          | 
          | 
                     boolean is_real = (type == 0)? false : random.nextBoolean();
  | 
      
      
         | 76 | 
          | 
          | 
          
  | 
      
      
         | 77 | 
          | 
          | 
                     InstructionLayer instr  = new InstructionLayer(random, prohibited_list);
  | 
      
      
         | 78 | 
          | 
          | 
                     layers.add(instr);
  | 
      
      
         | 79 | 
          | 
          | 
                     StackLayer stack        = new StackLayer(random, prohibited_list);
  | 
      
      
         | 80 | 
          | 
          | 
                     layers.add(stack);
  | 
      
      
         | 81 | 
          | 
          | 
                     layers.add(new OtherLayer(is_real ? OtherLayer.Type.REAL : OtherLayer.Type.PROTECTED_OR_V8086, random));
  | 
      
      
         | 82 | 
          | 
          | 
                     layers.add(new FlagsLayer(FlagsLayer.Type.RANDOM, random));
  | 
      
      
         | 83 | 
          | 
          | 
                     layers.add(new GeneralRegisterLayer(random));
  | 
      
      
         | 84 | 
          | 
          | 
                     layers.add(new SegmentLayer(random));
  | 
      
      
         | 85 | 
          | 
          | 
                     layers.add(new MemoryLayer(random));
  | 
      
      
         | 86 | 
          | 
          | 
                     layers.add(new IOLayer(random));
  | 
      
      
         | 87 | 
          | 
          | 
                     layers.addFirst(new HandleModeChangeLayer(
  | 
      
      
         | 88 | 
          | 
          | 
                             getInput("cr0_pe"),
  | 
      
      
         | 89 | 
          | 
          | 
                             getInput("vmflag"),
  | 
      
      
         | 90 | 
          | 
          | 
                             getInput("cs_rpl"),
  | 
      
      
         | 91 | 
          | 
          | 
                             getInput("cs_p"),
  | 
      
      
         | 92 | 
          | 
          | 
                             getInput("cs_s"),
  | 
      
      
         | 93 | 
          | 
          | 
                             getInput("cs_type")
  | 
      
      
         | 94 | 
          | 
          | 
                     ));
  | 
      
      
         | 95 | 
          | 
          | 
          
  | 
      
      
         | 96 | 
          | 
          | 
                     // instruction size
  | 
      
      
         | 97 | 
          | 
          | 
                     boolean cs_d_b = getInput("cs_d_b") == 1;
  | 
      
      
         | 98 | 
          | 
          | 
                     boolean vmflag = getInput("vmflag") == 1;
  | 
      
      
         | 99 | 
          | 
          | 
                     boolean cr0_pe = getInput("cr0_pe") == 1;
  | 
      
      
         | 100 | 
          | 
          | 
          
  | 
      
      
         | 101 | 
          | 
          | 
                     boolean a32 = random.nextBoolean();
  | 
      
      
         | 102 | 
          | 
          | 
                     boolean o32 = random.nextBoolean();
  | 
      
      
         | 103 | 
          | 
          | 
          
  | 
      
      
         | 104 | 
          | 
          | 
                     if(type == 0) {
  | 
      
      
         | 105 | 
          | 
          | 
                         final int cs_rpl = (vmflag)? 3 : 1 + random.nextInt(3);
  | 
      
      
         | 106 | 
          | 
          | 
                         Layer cs_rpl_layer = new Layer() {
  | 
      
      
         | 107 | 
          | 
          | 
                             long cs_rpl() { return cs_rpl; }
  | 
      
      
         | 108 | 
          | 
          | 
                         };
  | 
      
      
         | 109 | 
          | 
          | 
                         layers.addFirst(cs_rpl_layer);
  | 
      
      
         | 110 | 
          | 
          | 
                     }
  | 
      
      
         | 111 | 
          | 
          | 
          
  | 
      
      
         | 112 | 
          | 
          | 
                     // random CR0 bits
  | 
      
      
         | 113 | 
          | 
          | 
                     //cr0_pe set above
  | 
      
      
         | 114 | 
          | 
          | 
                     final boolean cr0_mp = random.nextBoolean();
  | 
      
      
         | 115 | 
          | 
          | 
                     final boolean cr0_em = random.nextBoolean();
  | 
      
      
         | 116 | 
          | 
          | 
                     final boolean cr0_ts = random.nextBoolean();
  | 
      
      
         | 117 | 
          | 
          | 
                     final boolean cr0_ne = random.nextBoolean();
  | 
      
      
         | 118 | 
          | 
          | 
                     final boolean cr0_wp = random.nextBoolean();
  | 
      
      
         | 119 | 
          | 
          | 
                     final boolean cr0_am = random.nextBoolean();
  | 
      
      
         | 120 | 
          | 
          | 
                     final boolean cr0_nw = random.nextBoolean();
  | 
      
      
         | 121 | 
          | 
          | 
                     final boolean cr0_cd = random.nextBoolean();
  | 
      
      
         | 122 | 
          | 
          | 
          
  | 
      
      
         | 123 | 
          | 
          | 
                     final int cr2 = random.nextInt();
  | 
      
      
         | 124 | 
          | 
          | 
                     final int cr3 = random.nextInt();
  | 
      
      
         | 125 | 
          | 
          | 
          
  | 
      
      
         | 126 | 
          | 
          | 
                     Layer cr0_2_3_layer = new Layer() {
  | 
      
      
         | 127 | 
          | 
          | 
                         long cr0_mp() { return cr0_mp? 1:0; }
  | 
      
      
         | 128 | 
          | 
          | 
                         long cr0_em() { return cr0_em? 1:0; }
  | 
      
      
         | 129 | 
          | 
          | 
                         long cr0_ts() { return cr0_ts? 1:0; }
  | 
      
      
         | 130 | 
          | 
          | 
                         long cr0_ne() { return cr0_ne? 1:0; }
  | 
      
      
         | 131 | 
          | 
          | 
                         long cr0_wp() { return cr0_wp? 1:0; }
  | 
      
      
         | 132 | 
          | 
          | 
                         long cr0_am() { return cr0_am? 1:0; }
  | 
      
      
         | 133 | 
          | 
          | 
                         long cr0_nw() { return cr0_nw? 1:0; }
  | 
      
      
         | 134 | 
          | 
          | 
                         long cr0_cd() { return cr0_cd? 1:0; }
  | 
      
      
         | 135 | 
          | 
          | 
                         long cr0_pg() { return 0; }
  | 
      
      
         | 136 | 
          | 
          | 
          
  | 
      
      
         | 137 | 
          | 
          | 
                         long cr2() { return cr2; }
  | 
      
      
         | 138 | 
          | 
          | 
                         long cr3() { return cr3; }
  | 
      
      
         | 139 | 
          | 
          | 
                     };
  | 
      
      
         | 140 | 
          | 
          | 
                     layers.addFirst(cr0_2_3_layer);
  | 
      
      
         | 141 | 
          | 
          | 
          
  | 
      
      
         | 142 | 
          | 
          | 
                     // destination
  | 
      
      
         | 143 | 
          | 
          | 
                     int idx = random.nextInt(3);
  | 
      
      
         | 144 | 
          | 
          | 
                     int cr_reg = (random.nextInt(10) == 0)? random.nextInt(8) : ( (idx == 0)? 0 : (idx == 1)? 2 : 3 );
  | 
      
      
         | 145 | 
          | 
          | 
          
  | 
      
      
         | 146 | 
          | 
          | 
                     int cr_mod = random.nextInt(4);
  | 
      
      
         | 147 | 
          | 
          | 
          
  | 
      
      
         | 148 | 
          | 
          | 
                     // source
  | 
      
      
         | 149 | 
          | 
          | 
                     long value = Layer.norm(random.nextInt());
  | 
      
      
         | 150 | 
          | 
          | 
          
  | 
      
      
         | 151 | 
          | 
          | 
                     value &= 0x7FFFFFFF; // do not set PG bit
  | 
      
      
         | 152 | 
          | 
          | 
          
  | 
      
      
         | 153 | 
          | 
          | 
                     byte modregrm_bytes[] = EffectiveAddressLayerFactory.prepare(
  | 
      
      
         | 154 | 
          | 
          | 
                             value,
  | 
      
      
         | 155 | 
          | 
          | 
                             cr_reg, EffectiveAddressLayerFactory.modregrm_reg_t.SET, // not used
  | 
      
      
         | 156 | 
          | 
          | 
                             4, a32,
  | 
      
      
         | 157 | 
          | 
          | 
                             layers, random, this, false, true);
  | 
      
      
         | 158 | 
          | 
          | 
          
  | 
      
      
         | 159 | 
          | 
          | 
                     int cr_rm = modregrm_bytes[0] & 7;
  | 
      
      
         | 160 | 
          | 
          | 
          
  | 
      
      
         | 161 | 
          | 
          | 
                     // check that vm flag is not set when entering protected mode
  | 
      
      
         | 162 | 
          | 
          | 
                     if(cr_reg == 0 && cr0_pe == false && (value & 1) == 1 && vmflag) continue;
  | 
      
      
         | 163 | 
          | 
          | 
          
  | 
      
      
         | 164 | 
          | 
          | 
                     // instruction
  | 
      
      
         | 165 | 
          | 
          | 
                     byte modregrm_byte = (byte)((cr_mod << 6) | (cr_reg << 3) | (cr_rm));
  | 
      
      
         | 166 | 
          | 
          | 
          
  | 
      
      
         | 167 | 
          | 
          | 
                     instruction = prepare_instr(cs_d_b, a32, o32, modregrm_byte);
  | 
      
      
         | 168 | 
          | 
          | 
          
  | 
      
      
         | 169 | 
          | 
          | 
                     instruction += instruction;
  | 
      
      
         | 170 | 
          | 
          | 
                     instruction += "0F0F";
  | 
      
      
         | 171 | 
          | 
          | 
          
  | 
      
      
         | 172 | 
          | 
          | 
                     // add instruction
  | 
      
      
         | 173 | 
          | 
          | 
                     instr.add_instruction(instruction);
  | 
      
      
         | 174 | 
          | 
          | 
          
  | 
      
      
         | 175 | 
          | 
          | 
                     // end condition
  | 
      
      
         | 176 | 
          | 
          | 
                     break;
  | 
      
      
         | 177 | 
          | 
          | 
                 }
  | 
      
      
         | 178 | 
          | 
          | 
          
  | 
      
      
         | 179 | 
          | 
          | 
                 System.out.println("Instruction: [" + instruction + "]");
  | 
      
      
         | 180 | 
          | 
          | 
             }
  | 
      
      
         | 181 | 
          | 
          | 
          
  | 
      
      
         | 182 | 
          | 
          | 
             String prepare_instr(boolean cs_d_b, boolean a32, boolean o32, byte modregrm_byte) throws Exception {
  | 
      
      
         | 183 | 
          | 
          | 
                 int opcodes[] = {
  | 
      
      
         | 184 | 
          | 
          | 
                     0x22
  | 
      
      
         | 185 | 
          | 
          | 
                 };
  | 
      
      
         | 186 | 
          | 
          | 
          
  | 
      
      
         | 187 | 
          | 
          | 
                 String prefix = "";
  | 
      
      
         | 188 | 
          | 
          | 
                 if(cs_d_b != o32) { prefix = "66" + prefix; }
  | 
      
      
         | 189 | 
          | 
          | 
                 if(cs_d_b != a32) { prefix = "67" + prefix; }
  | 
      
      
         | 190 | 
          | 
          | 
          
  | 
      
      
         | 191 | 
          | 
          | 
                 prefix += "0F";
  | 
      
      
         | 192 | 
          | 
          | 
                 int opcode = opcodes[random.nextInt(opcodes.length)];
  | 
      
      
         | 193 | 
          | 
          | 
          
  | 
      
      
         | 194 | 
          | 
          | 
                 byte instr[] = new byte[1 + 1];
  | 
      
      
         | 195 | 
          | 
          | 
                 instr[0] = (byte)opcode;
  | 
      
      
         | 196 | 
          | 
          | 
                 instr[1] = modregrm_byte;
  | 
      
      
         | 197 | 
          | 
          | 
          
  | 
      
      
         | 198 | 
          | 
          | 
                 return prefix + bytesToHex(instr);
  | 
      
      
         | 199 | 
          | 
          | 
             }
  | 
      
      
         | 200 | 
          | 
          | 
         }
  |