cache-utils.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #include "cache-utils.h"
  2. #if defined(_ARCH_PPC)
  3. struct qemu_cache_conf qemu_cache_conf = {
  4. .dcache_bsize = 16,
  5. .icache_bsize = 16
  6. };
  7. #if defined _AIX
  8. #include <sys/systemcfg.h>
  9. static void ppc_init_cacheline_sizes(void)
  10. {
  11. qemu_cache_conf.icache_bsize = _system_configuration.icache_line;
  12. qemu_cache_conf.dcache_bsize = _system_configuration.dcache_line;
  13. }
  14. #elif defined __linux__
  15. #define QEMU_AT_NULL 0
  16. #define QEMU_AT_DCACHEBSIZE 19
  17. #define QEMU_AT_ICACHEBSIZE 20
  18. static void ppc_init_cacheline_sizes(char **envp)
  19. {
  20. unsigned long *auxv;
  21. while (*envp++);
  22. for (auxv = (unsigned long *) envp; *auxv != QEMU_AT_NULL; auxv += 2) {
  23. switch (*auxv) {
  24. case QEMU_AT_DCACHEBSIZE: qemu_cache_conf.dcache_bsize = auxv[1]; break;
  25. case QEMU_AT_ICACHEBSIZE: qemu_cache_conf.icache_bsize = auxv[1]; break;
  26. default: break;
  27. }
  28. }
  29. }
  30. #elif defined __APPLE__
  31. #include <stdio.h>
  32. #include <sys/types.h>
  33. #include <sys/sysctl.h>
  34. static void ppc_init_cacheline_sizes(void)
  35. {
  36. size_t len;
  37. unsigned cacheline;
  38. int name[2] = { CTL_HW, HW_CACHELINE };
  39. len = sizeof(cacheline);
  40. if (sysctl(name, 2, &cacheline, &len, NULL, 0)) {
  41. perror("sysctl CTL_HW HW_CACHELINE failed");
  42. } else {
  43. qemu_cache_conf.dcache_bsize = cacheline;
  44. qemu_cache_conf.icache_bsize = cacheline;
  45. }
  46. }
  47. #endif
  48. #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
  49. #include <errno.h>
  50. #include <stdio.h>
  51. #include <stdlib.h>
  52. #include <string.h>
  53. #include <sys/types.h>
  54. #include <sys/sysctl.h>
  55. static void ppc_init_cacheline_sizes(void)
  56. {
  57. size_t len = 4;
  58. unsigned cacheline;
  59. if (sysctlbyname ("machdep.cacheline_size", &cacheline, &len, NULL, 0)) {
  60. fprintf(stderr, "sysctlbyname machdep.cacheline_size failed: %s\n",
  61. strerror(errno));
  62. exit(1);
  63. }
  64. qemu_cache_conf.dcache_bsize = cacheline;
  65. qemu_cache_conf.icache_bsize = cacheline;
  66. }
  67. #endif
  68. #ifdef __linux__
  69. void qemu_cache_utils_init(char **envp)
  70. {
  71. ppc_init_cacheline_sizes(envp);
  72. }
  73. #else
  74. void qemu_cache_utils_init(char **envp)
  75. {
  76. (void) envp;
  77. ppc_init_cacheline_sizes();
  78. }
  79. #endif
  80. #endif /* _ARCH_PPC */