linux-auxv.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* contrib/arm-neon/linux-auxv.c
  2. *
  3. * Copyright (c) 2014 Glenn Randers-Pehrson
  4. * Written by Mans Rullgard, 2011.
  5. * Last changed in libpng 1.6.10 [March 6, 2014]
  6. *
  7. * This code is released under the libpng license.
  8. * For conditions of distribution and use, see the disclaimer
  9. * and license in png.h
  10. *
  11. * SEE contrib/arm-neon/README before reporting bugs
  12. *
  13. * STATUS: COMPILED, TESTED
  14. * BUG REPORTS: [email protected]
  15. *
  16. * png_have_neon implemented for Linux versions which allow access to
  17. * /proc/self/auxv. This is probably faster, cleaner and safer than the code to
  18. * read /proc/cpuinfo in contrib/arm-neon/linux, however it is yet another piece
  19. * of potentially untested code and has more complex dependencies than the code
  20. * to read cpuinfo.
  21. *
  22. * This generic __linux__ implementation requires reading /proc/self/auxv and
  23. * looking at each element for one that records NEON capabilities.
  24. */
  25. #include <unistd.h> /* for POSIX 1003.1 */
  26. #include <errno.h> /* for EINTR */
  27. #include <sys/types.h>
  28. #include <sys/stat.h>
  29. #include <fcntl.h>
  30. #include <elf.h>
  31. #include <asm/hwcap.h>
  32. /* A read call may be interrupted, in which case it returns -1 and sets errno to
  33. * EINTR if nothing was done, otherwise (if something was done) a partial read
  34. * may result.
  35. */
  36. static size_t
  37. safe_read(png_structp png_ptr, int fd, void *buffer_in, size_t nbytes)
  38. {
  39. size_t ntotal = 0;
  40. char *buffer = png_voidcast(char*, buffer_in);
  41. while (nbytes > 0)
  42. {
  43. unsigned int nread;
  44. int iread;
  45. /* Passing nread > INT_MAX to read is implementation defined in POSIX
  46. * 1003.1, therefore despite the unsigned argument portable code must
  47. * limit the value to INT_MAX!
  48. */
  49. if (nbytes > INT_MAX)
  50. nread = INT_MAX;
  51. else
  52. nread = (unsigned int)/*SAFE*/nbytes;
  53. iread = read(fd, buffer, nread);
  54. if (iread == -1)
  55. {
  56. /* This is the devil in the details, a read can terminate early with 0
  57. * bytes read because of EINTR, yet it still returns -1 otherwise end
  58. * of file cannot be distinguished.
  59. */
  60. if (errno != EINTR)
  61. {
  62. png_warning(png_ptr, "/proc read failed");
  63. return 0; /* I.e., a permanent failure */
  64. }
  65. }
  66. else if (iread < 0)
  67. {
  68. /* Not a valid 'read' result: */
  69. png_warning(png_ptr, "OS /proc read bug");
  70. return 0;
  71. }
  72. else if (iread > 0)
  73. {
  74. /* Continue reading until a permanent failure, or EOF */
  75. buffer += iread;
  76. nbytes -= (unsigned int)/*SAFE*/iread;
  77. ntotal += (unsigned int)/*SAFE*/iread;
  78. }
  79. else
  80. return ntotal;
  81. }
  82. return ntotal; /* nbytes == 0 */
  83. }
  84. static int
  85. png_have_neon(png_structp png_ptr)
  86. {
  87. int fd = open("/proc/self/auxv", O_RDONLY);
  88. Elf32_auxv_t aux;
  89. /* Failsafe: failure to open means no NEON */
  90. if (fd == -1)
  91. {
  92. png_warning(png_ptr, "/proc/self/auxv open failed");
  93. return 0;
  94. }
  95. while (safe_read(png_ptr, fd, &aux, sizeof aux) == sizeof aux)
  96. {
  97. if (aux.a_type == AT_HWCAP && (aux.a_un.a_val & HWCAP_NEON) != 0)
  98. {
  99. close(fd);
  100. return 1;
  101. }
  102. }
  103. close(fd);
  104. return 0;
  105. }