fxsupport.cc 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #include <stdlib.h>
  2. #include "fxsupport.h"
  3. #include "core_manager.h"
  4. #include "simulator.h"
  5. Fxsupport *Fxsupport::m_singleton;
  6. Fxsupport::Fxsupport(core_id_t core_count)
  7. {
  8. m_core_count = core_count;
  9. m_fx_buf = (char**) malloc (m_core_count * sizeof (char*));
  10. for (int i = 0; i < m_core_count; i++)
  11. {
  12. __attribute__((unused)) int status = posix_memalign ((void**) &m_fx_buf[i], 16, 512);
  13. assert (status == 0);
  14. }
  15. m_ref_count = (uint64_t**) malloc (m_core_count * sizeof(uint64_t*));
  16. for (int i = 0; i < m_core_count; i++)
  17. {
  18. m_ref_count[i] = 0;
  19. }
  20. }
  21. Fxsupport::~Fxsupport()
  22. {
  23. for (int i = 0; i < m_core_count; i++)
  24. free ((void*) m_fx_buf[i]);
  25. free (m_fx_buf);
  26. free (m_ref_count);
  27. }
  28. void Fxsupport::init()
  29. {
  30. assert (m_singleton == NULL);
  31. core_id_t core_count = Sim()->getConfig()->getTotalCores();
  32. m_singleton = new Fxsupport (core_count);
  33. }
  34. void Fxsupport::fini()
  35. {
  36. assert (m_singleton);
  37. delete m_singleton;
  38. }
  39. Fxsupport *Fxsupport::getSingleton()
  40. {
  41. assert (m_singleton);
  42. return m_singleton;
  43. }
  44. void Fxsupport::fxsave()
  45. {
  46. if (Sim()->getCoreManager()->amiUserThread()) {
  47. LOG_PRINT("fxsave() start");
  48. core_id_t core_index = Sim()->getCoreManager()->getCurrentCoreID();
  49. if (m_ref_count[core_index] == 0)
  50. {
  51. char *buf = m_fx_buf[core_index];
  52. asm volatile ("fxsave %0\n\t"
  53. "emms"
  54. :"=m"(*buf));
  55. }
  56. m_ref_count[core_index]++;
  57. LOG_PRINT("fxsave() end");
  58. }
  59. }
  60. void Fxsupport::fxrstor()
  61. {
  62. if (Sim()->getCoreManager()->amiUserThread()) {
  63. LOG_PRINT("fxrstor() start");
  64. core_id_t core_index = Sim()->getCoreManager()->getCurrentCoreID();
  65. m_ref_count[core_index]--;
  66. if (m_ref_count[core_index] == 0)
  67. {
  68. char *buf = m_fx_buf[core_index];
  69. asm volatile ("fxrstor %0"::"m"(*buf));
  70. }
  71. LOG_PRINT("fxrstor() end");
  72. }
  73. }