def generate(self, **kwargs): with ThreadSplitterContextManager(self): pc = PcConfig.get_base_boot_pc() (skip_boot, skip_boot_valid) = self.getOption("SkipBootCode") if skip_boot_valid and skip_boot == 1: pc = PcConfig.get_base_initial_pc() ( boot_pc_reg_index, thread_id_reg_index, pc_offset_reg_index, ) = self.getRandomGPRs(3, exclude="0") assembly_helper = AssemblyHelperRISCV(self) assembly_helper.genReadSystemRegister( thread_id_reg_index, "mhartid" ) # Get the thread ID load_gpr64_seq = LoadGPR64(self.genThread) load_gpr64_seq.load( pc_offset_reg_index, PcConfig.get_boot_pc_offset() ) self.genInstruction( "MUL##RISCV", { "rd": pc_offset_reg_index, "rs1": thread_id_reg_index, "rs2": pc_offset_reg_index, }, ) # Multiply the base PC offset by the thread ID load_gpr64_seq.load(boot_pc_reg_index, PcConfig.get_base_boot_pc()) assembly_helper.genAddRegister( boot_pc_reg_index, pc_offset_reg_index ) # Add the thread PC offset to the base initial PC self.genInstruction( "JALR##RISCV", { "rd": 0, "rs1": boot_pc_reg_index, "simm12": 0, "NoBnt": 1, "NoRestriction": 1, }, ) # Branch to calculated address
def generate(self, **kwargs): with ThreadSplitterContextManager(self): pc = PcConfig.get_base_boot_pc() (skip_boot, skip_boot_valid) = self.getOption("SkipBootCode") #TODO allow for granular control of skip boot code/skip thread splitter code if skip_boot_valid and skip_boot == 1: pc = PcConfig.get_base_initial_pc() (boot_pc_reg_index, thread_id_reg_index, pc_offset_reg_index) = self.getRandomGPRs(3, exclude='0') assembly_helper = AssemblyHelperRISCV(self) assembly_helper.genReadSystemRegister(thread_id_reg_index, 'mhartid') # Get the thread ID load_gpr64_seq = LoadGPR64(self.genThread) load_gpr64_seq.load(pc_offset_reg_index, PcConfig.get_boot_pc_offset()) self.genInstruction('MUL##RISCV', {'rd': pc_offset_reg_index, 'rs1': thread_id_reg_index, 'rs2': pc_offset_reg_index}) # Multiply the base PC offset by the thread ID load_gpr64_seq.load(boot_pc_reg_index, PcConfig.get_base_boot_pc()) assembly_helper.genAddRegister(boot_pc_reg_index, pc_offset_reg_index) # Add the thread PC offset to the base initial PC self.genInstruction('JALR##RISCV', {'rd': 0, 'rs1': boot_pc_reg_index, 'simm12': 0, 'NoBnt': 1, 'NoRestriction': 1}) # Branch to calculated address
def generate(self, **kargs): assembly_helper = AssemblyHelperRISCV(self) appRegSize = self.getGlobalState("AppRegisterWidth") if appRegSize == 32: self.notice("FORCE/RISCV configured for 32-bits...") else: self.notice("FORCE/RISCV configured for 64-bits...") (dest_reg_index, src_reg_index, src_reg_index_2) = self.getRandomGPRs(3, exclude="0") load_gpr64_seq = LoadGPR64(self.genThread) src_reg_val = RandomUtils.random32( ) if appRegSize == 32 else RandomUtils.random64() load_gpr64_seq.load(src_reg_index, src_reg_val) src_reg_val_2 = RandomUtils.random32( ) if appRegSize == 32 else RandomUtils.random64() load_gpr64_seq.load(src_reg_index_2, src_reg_val_2) MAX_GPR_VAL = 0xFFFFFFFF if appRegSize == 32 else 0xFFFFFFFFFFFFFFFF imm_val = RandomUtils.random32(0, 2047) assembly_helper.genMoveImmediate(dest_reg_index, imm_val) dest_reg_val = imm_val exception_handlers_test_utils.assert_gpr_has_value( self, dest_reg_index, dest_reg_val) max_shift_val = 31 if appRegSize == 32 else 63 shift_amount = RandomUtils.random32(0, max_shift_val) assembly_helper.genShiftLeftImmediate(dest_reg_index, shift_amount) dest_reg_val = (dest_reg_val << shift_amount) & MAX_GPR_VAL exception_handlers_test_utils.assert_gpr_has_value( self, dest_reg_index, dest_reg_val) shift_amount = RandomUtils.random32(0, max_shift_val) assembly_helper.genShiftRightImmediate(dest_reg_index, shift_amount) dest_reg_val = (dest_reg_val >> shift_amount) & MAX_GPR_VAL exception_handlers_test_utils.assert_gpr_has_value( self, dest_reg_index, dest_reg_val) shift_amount = RandomUtils.random32(0, max_shift_val) assembly_helper.genShiftLeftImmediate(dest_reg_index, shift_amount, aSrcRegIndex=src_reg_index) dest_reg_val = (src_reg_val << shift_amount) & MAX_GPR_VAL exception_handlers_test_utils.assert_gpr_has_value( self, dest_reg_index, dest_reg_val) shift_amount = RandomUtils.random32(0, max_shift_val) assembly_helper.genShiftRightImmediate(dest_reg_index, shift_amount, aSrcRegIndex=src_reg_index) dest_reg_val = (src_reg_val >> shift_amount) & MAX_GPR_VAL exception_handlers_test_utils.assert_gpr_has_value( self, dest_reg_index, dest_reg_val) imm_val = RandomUtils.random32(0, 2047) assembly_helper.genAndImmediate(dest_reg_index, imm_val) dest_reg_val &= imm_val exception_handlers_test_utils.assert_gpr_has_value( self, dest_reg_index, dest_reg_val) imm_val = RandomUtils.random32(0, 2047) assembly_helper.genAndImmediate(dest_reg_index, imm_val, aSrcRegIndex=src_reg_index) dest_reg_val = src_reg_val & imm_val exception_handlers_test_utils.assert_gpr_has_value( self, dest_reg_index, dest_reg_val) imm_val = RandomUtils.random32(0, 2047) assembly_helper.genOrImmediate(dest_reg_index, imm_val) dest_reg_val |= imm_val exception_handlers_test_utils.assert_gpr_has_value( self, dest_reg_index, dest_reg_val) imm_val = RandomUtils.random32(0, 2047) assembly_helper.genOrImmediate(dest_reg_index, imm_val, aSrcRegIndex=src_reg_index) dest_reg_val = src_reg_val | imm_val exception_handlers_test_utils.assert_gpr_has_value( self, dest_reg_index, dest_reg_val) imm_val = RandomUtils.random32(0, 2047) assembly_helper.genXorImmediate(dest_reg_index, imm_val) dest_reg_val ^= imm_val exception_handlers_test_utils.assert_gpr_has_value( self, dest_reg_index, dest_reg_val) imm_val = RandomUtils.random32(0, 2047) assembly_helper.genXorImmediate(dest_reg_index, imm_val, aSrcRegIndex=src_reg_index) dest_reg_val = src_reg_val ^ imm_val exception_handlers_test_utils.assert_gpr_has_value( self, dest_reg_index, dest_reg_val) imm_val = RandomUtils.random32(0, 2047) assembly_helper.genAddImmediate(dest_reg_index, imm_val) dest_reg_val = (dest_reg_val + imm_val) & MAX_GPR_VAL exception_handlers_test_utils.assert_gpr_has_value( self, dest_reg_index, dest_reg_val) imm_val = RandomUtils.random32(0, 2047) assembly_helper.genAddImmediate(dest_reg_index, imm_val, aSrcRegIndex=src_reg_index) dest_reg_val = (src_reg_val + imm_val) & MAX_GPR_VAL exception_handlers_test_utils.assert_gpr_has_value( self, dest_reg_index, dest_reg_val) assembly_helper.genMoveRegister(dest_reg_index, src_reg_index) dest_reg_val = src_reg_val exception_handlers_test_utils.assert_gpr_has_value( self, dest_reg_index, dest_reg_val) # Reset destination register value after copying directly from source; # otherwise, the tests that follow will be less interesting dest_reg_val = RandomUtils.random32( ) if appRegSize == 32 else RandomUtils.random64() load_gpr64_seq.load(dest_reg_index, dest_reg_val) assembly_helper.genAndRegister(dest_reg_index, src_reg_index) dest_reg_val &= src_reg_val exception_handlers_test_utils.assert_gpr_has_value( self, dest_reg_index, dest_reg_val) assembly_helper.genAndRegister(dest_reg_index, src_reg_index, aSrcRegIndex2=src_reg_index_2) dest_reg_val = src_reg_val_2 & src_reg_val exception_handlers_test_utils.assert_gpr_has_value( self, dest_reg_index, dest_reg_val) assembly_helper.genOrRegister(dest_reg_index, src_reg_index) dest_reg_val |= src_reg_val exception_handlers_test_utils.assert_gpr_has_value( self, dest_reg_index, dest_reg_val) assembly_helper.genOrRegister(dest_reg_index, src_reg_index, aSrcRegIndex2=src_reg_index_2) dest_reg_val = src_reg_val_2 | src_reg_val exception_handlers_test_utils.assert_gpr_has_value( self, dest_reg_index, dest_reg_val) assembly_helper.genNotRegister(dest_reg_index) dest_reg_val = ~dest_reg_val & MAX_GPR_VAL exception_handlers_test_utils.assert_gpr_has_value( self, dest_reg_index, dest_reg_val) assembly_helper.genNotRegister(dest_reg_index, aSrcRegIndex=src_reg_index) dest_reg_val = ~src_reg_val & MAX_GPR_VAL exception_handlers_test_utils.assert_gpr_has_value( self, dest_reg_index, dest_reg_val) assembly_helper.genAddRegister(dest_reg_index, src_reg_index) dest_reg_val = (dest_reg_val + src_reg_val) & MAX_GPR_VAL exception_handlers_test_utils.assert_gpr_has_value( self, dest_reg_index, dest_reg_val) assembly_helper.genAddRegister(dest_reg_index, src_reg_index, aSrcRegIndex2=src_reg_index_2) dest_reg_val = (src_reg_val_2 + src_reg_val) & MAX_GPR_VAL exception_handlers_test_utils.assert_gpr_has_value( self, dest_reg_index, dest_reg_val) assembly_helper.genSubRegister(dest_reg_index, src_reg_index) dest_reg_val = (dest_reg_val - src_reg_val) & MAX_GPR_VAL exception_handlers_test_utils.assert_gpr_has_value( self, dest_reg_index, dest_reg_val) assembly_helper.genSubRegister(dest_reg_index, src_reg_index, aMinuendRegIndex=src_reg_index_2) dest_reg_val = (src_reg_val_2 - src_reg_val) & MAX_GPR_VAL exception_handlers_test_utils.assert_gpr_has_value( self, dest_reg_index, dest_reg_val) assembly_helper.genReadSystemRegister(dest_reg_index, "mscratch") (dest_reg_val, valid) = self.readRegister("mscratch") if not valid: self.error("Value for register mscratch is invalid") exception_handlers_test_utils.assert_gpr_has_value( self, dest_reg_index, dest_reg_val) assembly_helper.genWriteSystemRegister("mscratch", src_reg_index) (mscratch_val, valid) = self.readRegister("mscratch") if not valid: self.error("Value for register mscratch is invalid") if mscratch_val != src_reg_val: self.error("Value of register mscratch did not match the expected " "value. Expected=0x%x, Actual=0x%x" % (src_reg_val, mscratch_val))
def generate(self, **kargs): assembly_helper = AssemblyHelperRISCV(self) # TODO(Noah): Test branch instruction generation methods when there is time to do so. (dest_reg_index, src_reg_index, src_reg_index_2) = self.getRandomGPRs(3, exclude='0') load_gpr64_seq = LoadGPR64(self.genThread) src_reg_val = RandomUtils.random64() load_gpr64_seq.load(src_reg_index, src_reg_val) src_reg_val_2 = RandomUtils.random64() load_gpr64_seq.load(src_reg_index_2, src_reg_val_2) MAX_GPR_VAL = 0xFFFFFFFFFFFFFFFF imm_val = RandomUtils.random32(0, 2047) assembly_helper.genMoveImmediate(dest_reg_index, imm_val) dest_reg_val = imm_val exception_handlers_test_utils.assertGprHasValue( self, dest_reg_index, dest_reg_val) shift_amount = RandomUtils.random32(0, 63) assembly_helper.genShiftLeftImmediate(dest_reg_index, shift_amount) dest_reg_val = (dest_reg_val << shift_amount) & MAX_GPR_VAL exception_handlers_test_utils.assertGprHasValue( self, dest_reg_index, dest_reg_val) shift_amount = RandomUtils.random32(0, 63) assembly_helper.genShiftRightImmediate(dest_reg_index, shift_amount) dest_reg_val = (dest_reg_val >> shift_amount) & MAX_GPR_VAL exception_handlers_test_utils.assertGprHasValue( self, dest_reg_index, dest_reg_val) shift_amount = RandomUtils.random32(0, 63) assembly_helper.genShiftLeftImmediate(dest_reg_index, shift_amount, aSrcRegIndex=src_reg_index) dest_reg_val = (src_reg_val << shift_amount) & MAX_GPR_VAL exception_handlers_test_utils.assertGprHasValue( self, dest_reg_index, dest_reg_val) shift_amount = RandomUtils.random32(0, 63) assembly_helper.genShiftRightImmediate(dest_reg_index, shift_amount, aSrcRegIndex=src_reg_index) dest_reg_val = (src_reg_val >> shift_amount) & MAX_GPR_VAL exception_handlers_test_utils.assertGprHasValue( self, dest_reg_index, dest_reg_val) imm_val = RandomUtils.random32(0, 2047) assembly_helper.genAndImmediate(dest_reg_index, imm_val) dest_reg_val &= imm_val exception_handlers_test_utils.assertGprHasValue( self, dest_reg_index, dest_reg_val) imm_val = RandomUtils.random32(0, 2047) assembly_helper.genAndImmediate(dest_reg_index, imm_val, aSrcRegIndex=src_reg_index) dest_reg_val = src_reg_val & imm_val exception_handlers_test_utils.assertGprHasValue( self, dest_reg_index, dest_reg_val) imm_val = RandomUtils.random32(0, 2047) assembly_helper.genOrImmediate(dest_reg_index, imm_val) dest_reg_val |= imm_val exception_handlers_test_utils.assertGprHasValue( self, dest_reg_index, dest_reg_val) imm_val = RandomUtils.random32(0, 2047) assembly_helper.genOrImmediate(dest_reg_index, imm_val, aSrcRegIndex=src_reg_index) dest_reg_val = src_reg_val | imm_val exception_handlers_test_utils.assertGprHasValue( self, dest_reg_index, dest_reg_val) imm_val = RandomUtils.random32(0, 2047) assembly_helper.genXorImmediate(dest_reg_index, imm_val) dest_reg_val ^= imm_val exception_handlers_test_utils.assertGprHasValue( self, dest_reg_index, dest_reg_val) imm_val = RandomUtils.random32(0, 2047) assembly_helper.genXorImmediate(dest_reg_index, imm_val, aSrcRegIndex=src_reg_index) dest_reg_val = src_reg_val ^ imm_val exception_handlers_test_utils.assertGprHasValue( self, dest_reg_index, dest_reg_val) imm_val = RandomUtils.random32(0, 2047) assembly_helper.genAddImmediate(dest_reg_index, imm_val) dest_reg_val = (dest_reg_val + imm_val) & MAX_GPR_VAL exception_handlers_test_utils.assertGprHasValue( self, dest_reg_index, dest_reg_val) imm_val = RandomUtils.random32(0, 2047) assembly_helper.genAddImmediate(dest_reg_index, imm_val, aSrcRegIndex=src_reg_index) dest_reg_val = (src_reg_val + imm_val) & MAX_GPR_VAL exception_handlers_test_utils.assertGprHasValue( self, dest_reg_index, dest_reg_val) assembly_helper.genMoveRegister(dest_reg_index, src_reg_index) dest_reg_val = src_reg_val exception_handlers_test_utils.assertGprHasValue( self, dest_reg_index, dest_reg_val) # Reset destination register value after copying directly from source; otherwise, the tests # that follow will be less interesting dest_reg_val = RandomUtils.random64() load_gpr64_seq.load(dest_reg_index, dest_reg_val) assembly_helper.genAndRegister(dest_reg_index, src_reg_index) dest_reg_val &= src_reg_val exception_handlers_test_utils.assertGprHasValue( self, dest_reg_index, dest_reg_val) assembly_helper.genAndRegister(dest_reg_index, src_reg_index, aSrcRegIndex2=src_reg_index_2) dest_reg_val = src_reg_val_2 & src_reg_val exception_handlers_test_utils.assertGprHasValue( self, dest_reg_index, dest_reg_val) assembly_helper.genOrRegister(dest_reg_index, src_reg_index) dest_reg_val |= src_reg_val exception_handlers_test_utils.assertGprHasValue( self, dest_reg_index, dest_reg_val) assembly_helper.genOrRegister(dest_reg_index, src_reg_index, aSrcRegIndex2=src_reg_index_2) dest_reg_val = src_reg_val_2 | src_reg_val exception_handlers_test_utils.assertGprHasValue( self, dest_reg_index, dest_reg_val) assembly_helper.genNotRegister(dest_reg_index) dest_reg_val = ~dest_reg_val & MAX_GPR_VAL exception_handlers_test_utils.assertGprHasValue( self, dest_reg_index, dest_reg_val) assembly_helper.genNotRegister(dest_reg_index, aSrcRegIndex=src_reg_index) dest_reg_val = ~src_reg_val & MAX_GPR_VAL exception_handlers_test_utils.assertGprHasValue( self, dest_reg_index, dest_reg_val) assembly_helper.genAddRegister(dest_reg_index, src_reg_index) dest_reg_val = (dest_reg_val + src_reg_val) & MAX_GPR_VAL exception_handlers_test_utils.assertGprHasValue( self, dest_reg_index, dest_reg_val) assembly_helper.genAddRegister(dest_reg_index, src_reg_index, aSrcRegIndex2=src_reg_index_2) dest_reg_val = (src_reg_val_2 + src_reg_val) & MAX_GPR_VAL exception_handlers_test_utils.assertGprHasValue( self, dest_reg_index, dest_reg_val) assembly_helper.genSubRegister(dest_reg_index, src_reg_index) dest_reg_val = (dest_reg_val - src_reg_val) & MAX_GPR_VAL exception_handlers_test_utils.assertGprHasValue( self, dest_reg_index, dest_reg_val) assembly_helper.genSubRegister(dest_reg_index, src_reg_index, aMinuendRegIndex=src_reg_index_2) dest_reg_val = (src_reg_val_2 - src_reg_val) & MAX_GPR_VAL exception_handlers_test_utils.assertGprHasValue( self, dest_reg_index, dest_reg_val) assembly_helper.genReadSystemRegister(dest_reg_index, 'mscratch') (dest_reg_val, valid) = self.readRegister('mscratch') if not valid: self.error('Value for register mscratch is invalid') exception_handlers_test_utils.assertGprHasValue( self, dest_reg_index, dest_reg_val) assembly_helper.genWriteSystemRegister('mscratch', src_reg_index) (mscratch_val, valid) = self.readRegister('mscratch') if not valid: self.error('Value for register mscratch is invalid') if mscratch_val != src_reg_val: self.error( 'Value of register mscratch did not match the expected value. Expected=0x%x, Actual=0x%x' % (src_reg_val, mscratch_val))