warm.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * wARM - exporting ARM processor specific privileged services to userspace
  3. * userspace part
  4. *
  5. * Copyright (c) Gražvydas "notaz" Ignotas, 2009
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * * Neither the name of the organization nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  24. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  25. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <sys/types.h>
  33. #include <sys/stat.h>
  34. #include <unistd.h>
  35. #include <fcntl.h>
  36. #include <sys/ioctl.h>
  37. #include <sys/utsname.h>
  38. #include <sys/syscall.h>
  39. #include <errno.h>
  40. #define WARM_CODE
  41. #include "warm.h"
  42. /* provided by glibc */
  43. extern long init_module(void *, unsigned long, const char *);
  44. extern long delete_module(const char *, unsigned int);
  45. static int warm_fd = -1;
  46. static int kernel_version;
  47. static void sys_cacheflush(void *start, void *end)
  48. {
  49. #ifdef __ARM_EABI__
  50. /* EABI version */
  51. int num = __ARM_NR_cacheflush;
  52. __asm__("mov r0, %0 ;"
  53. "mov r1, %1 ;"
  54. "mov r2, #0 ;"
  55. "mov r7, %2 ;"
  56. "swi 0" : : "r" (start), "r" (end), "r" (num)
  57. : "r0", "r1", "r2", "r3", "r7");
  58. #else
  59. /* OABI */
  60. __asm__("mov r0, %0 ;"
  61. "mov r1, %1 ;"
  62. "mov r2, #0 ;"
  63. "swi %2" : : "r" (start), "r" (end), "i" __ARM_NR_cacheflush
  64. : "r0", "r1", "r2", "r3");
  65. #endif
  66. }
  67. /* Those are here because system() occasionaly fails on Wiz
  68. * with errno 12 for some unknown reason */
  69. static int manual_insmod_26(const char *fname, const char *opts)
  70. {
  71. unsigned long len, read_len;
  72. int ret = -1;
  73. void *buff;
  74. FILE *f;
  75. f = fopen(fname, "rb");
  76. if (f == NULL)
  77. return -1;
  78. fseek(f, 0, SEEK_END);
  79. len = ftell(f);
  80. fseek(f, 0, SEEK_SET);
  81. buff = malloc(len);
  82. if (buff == NULL)
  83. goto fail0;
  84. read_len = fread(buff, 1, len, f);
  85. if (read_len != len) {
  86. fprintf(stderr, "failed to read module\n");
  87. goto fail1;
  88. }
  89. ret = init_module(buff, len, opts);
  90. fail1:
  91. free(buff);
  92. fail0:
  93. fclose(f);
  94. return ret;
  95. }
  96. static int manual_rmmod(const char *name)
  97. {
  98. return delete_module(name, O_NONBLOCK|O_EXCL);
  99. }
  100. int warm_init(void)
  101. {
  102. struct utsname unm;
  103. char buff1[32], buff2[128];
  104. int ret;
  105. memset(&unm, 0, sizeof(unm));
  106. uname(&unm);
  107. if (strlen(unm.release) < 3 || unm.release[1] != '.') {
  108. fprintf(stderr, "unexpected version string: %s\n", unm.release);
  109. goto fail;
  110. }
  111. kernel_version = ((unm.release[0] - '0') << 4) | (unm.release[2] - '0');
  112. warm_fd = open("/proc/warm", O_RDWR);
  113. if (warm_fd >= 0)
  114. return 0;
  115. snprintf(buff1, sizeof(buff1), "warm_%s.%s", unm.release, kernel_version >= 0x26 ? "ko" : "o");
  116. snprintf(buff2, sizeof(buff2), "/sbin/insmod %s verbose=1", buff1);
  117. /* try to insmod */
  118. ret = system(buff2);
  119. if (ret != 0) {
  120. fprintf(stderr, "system/insmod failed: %d %d\n", ret, errno);
  121. if (kernel_version >= 0x26) {
  122. ret = manual_insmod_26(buff1, "verbose=1");
  123. if (ret != 0)
  124. fprintf(stderr, "manual insmod also failed: %d\n", ret);
  125. }
  126. }
  127. warm_fd = open("/proc/warm", O_RDWR);
  128. if (warm_fd >= 0)
  129. return 0;
  130. fail:
  131. fprintf(stderr, "wARM: can't init, acting as sys_cacheflush wrapper\n");
  132. return -1;
  133. }
  134. void warm_finish(void)
  135. {
  136. char name[32], cmd[64];
  137. int ret;
  138. if (warm_fd < 0)
  139. return;
  140. close(warm_fd);
  141. warm_fd = -1;
  142. if (kernel_version < 0x26) {
  143. struct utsname unm;
  144. memset(&unm, 0, sizeof(unm));
  145. uname(&unm);
  146. snprintf(name, sizeof(name), "warm_%s", unm.release);
  147. }
  148. else
  149. strcpy(name, "warm");
  150. snprintf(cmd, sizeof(cmd), "/sbin/rmmod %s", name);
  151. ret = system(cmd);
  152. if (ret != 0) {
  153. fprintf(stderr, "system/rmmod failed: %d %d\n", ret, errno);
  154. manual_rmmod(name);
  155. }
  156. }
  157. int warm_cache_op_range(int op, void *addr, unsigned long size)
  158. {
  159. struct warm_cache_op wop;
  160. int ret;
  161. if (warm_fd < 0) {
  162. /* note that this won't work for warm_cache_op_all */
  163. sys_cacheflush(addr, (char *)addr + size);
  164. return -1;
  165. }
  166. wop.ops = op;
  167. wop.addr = (unsigned long)addr;
  168. wop.size = size;
  169. ret = ioctl(warm_fd, WARMC_CACHE_OP, &wop);
  170. if (ret != 0) {
  171. perror("WARMC_CACHE_OP failed");
  172. return -1;
  173. }
  174. return 0;
  175. }
  176. int warm_cache_op_all(int op)
  177. {
  178. return warm_cache_op_range(op, NULL, (unsigned long)-1);
  179. }
  180. int warm_change_cb_range(int cb, int is_set, void *addr, unsigned long size)
  181. {
  182. struct warm_change_cb ccb;
  183. int ret;
  184. if (warm_fd < 0)
  185. return -1;
  186. ccb.addr = (unsigned long)addr;
  187. ccb.size = size;
  188. ccb.cb = cb;
  189. ccb.is_set = is_set;
  190. ret = ioctl(warm_fd, WARMC_CHANGE_CB, &ccb);
  191. if (ret != 0) {
  192. perror("WARMC_CHANGE_CB failed");
  193. return -1;
  194. }
  195. return 0;
  196. }
  197. int warm_change_cb_upper(int cb, int is_set)
  198. {
  199. return warm_change_cb_range(cb, is_set, 0, 0);
  200. }
  201. unsigned long warm_virt2phys(const void *ptr)
  202. {
  203. unsigned long ptrio;
  204. int ret;
  205. ptrio = (unsigned long)ptr;
  206. ret = ioctl(warm_fd, WARMC_VIRT2PHYS, &ptrio);
  207. if (ret != 0) {
  208. perror("WARMC_VIRT2PHYS failed");
  209. return (unsigned long)-1;
  210. }
  211. return ptrio;
  212. }