fault_injection.cc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #include "fault_injection.h"
  2. #include "fault_injector_random.h"
  3. #include "simulator.h"
  4. #include "config.hpp"
  5. #include "log.h"
  6. FaultinjectionManager *
  7. FaultinjectionManager::create()
  8. {
  9. String s_type = Sim()->getCfg()->getString("fault_injection/type");
  10. String s_injector = Sim()->getCfg()->getString("fault_injection/injector");
  11. fault_type_t type;
  12. if (s_type == "toggle")
  13. type = FAULT_TYPE_TOGGLE;
  14. else if (s_type == "set0")
  15. type = FAULT_TYPE_SET0;
  16. else if (s_type == "set1")
  17. type = FAULT_TYPE_SET1;
  18. else if (s_type == "none")
  19. return NULL;
  20. else
  21. LOG_PRINT_ERROR("Unknown fault-injection scheme %s", s_type.c_str());
  22. fault_injector_t injector;
  23. if (s_injector == "none")
  24. injector = FAULT_INJECTOR_NONE;
  25. else if (s_injector == "random")
  26. injector = FAULT_INJECTOR_RANDOM;
  27. else
  28. LOG_PRINT_ERROR("Unknown fault injector %s", s_injector.c_str());
  29. return new FaultinjectionManager(type, injector);
  30. }
  31. FaultinjectionManager::FaultinjectionManager(fault_type_t type, fault_injector_t injector)
  32. : m_type(type)
  33. , m_injector(injector)
  34. {
  35. }
  36. FaultInjector *
  37. FaultinjectionManager::getFaultInjector(UInt32 core_id, MemComponent::component_t mem_component)
  38. {
  39. switch(m_injector)
  40. {
  41. case FAULT_INJECTOR_NONE:
  42. return new FaultInjector(core_id, mem_component);
  43. case FAULT_INJECTOR_RANDOM:
  44. return new FaultInjectorRandom(core_id, mem_component);
  45. }
  46. return NULL;
  47. }
  48. void
  49. FaultinjectionManager::applyFault(Core *core, IntPtr read_address, UInt32 data_size, MemoryResult &memres, Byte *data, const Byte *fault)
  50. {
  51. switch(m_type)
  52. {
  53. case FAULT_TYPE_TOGGLE:
  54. for(UInt32 i = 0; i < data_size; ++i)
  55. data[i] ^= fault[i];
  56. break;
  57. case FAULT_TYPE_SET0:
  58. for(UInt32 i = 0; i < data_size; ++i)
  59. data[i] &= ~fault[i];
  60. break;
  61. case FAULT_TYPE_SET1:
  62. for(UInt32 i = 0; i < data_size; ++i)
  63. data[i] |= fault[i];
  64. break;
  65. }
  66. }
  67. FaultInjector::FaultInjector(UInt32 core_id, MemComponent::component_t mem_component)
  68. : m_core_id(core_id)
  69. , m_mem_component(mem_component)
  70. {
  71. }
  72. void
  73. FaultInjector::preRead(IntPtr addr, IntPtr location, UInt32 data_size, Byte *fault, SubsecondTime time)
  74. {
  75. // Data at virtual address <addr> is about to be read with size <data_size>.
  76. // <location> corresponds to the physical location (cache line) where the data lives.
  77. // Update <fault> here according to errors that have accumulated in this memory location.
  78. }
  79. void
  80. FaultInjector::postWrite(IntPtr addr, IntPtr location, UInt32 data_size, Byte *fault, SubsecondTime time)
  81. {
  82. // Data at virtual address <addr> has just been written to.
  83. // Update <fault> here according to errors that occured during the writing of this memory location.
  84. }