2 * wARM - exporting ARM processor specific privileged services to userspace
5 * Copyright (c) GraÅžvydas "notaz" Ignotas, 2009
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.
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.
33 #include <sys/types.h>
37 #include <sys/ioctl.h>
38 #include <sys/utsname.h>
39 #include <sys/syscall.h>
45 extern long init_module(void *, unsigned long, const char *); /* provided by glibc */
47 static int warm_fd = -1;
48 static int kernel_version;
50 static void sys_cacheflush(void *start, void *end)
54 int num = __ARM_NR_cacheflush;
55 __asm__("mov r0, %0 ;"
59 "swi 0" : : "r" (start), "r" (end), "r" (num)
60 : "r0", "r1", "r2", "r3", "r7");
63 __asm__("mov r0, %0 ;"
66 "swi %2" : : "r" (start), "r" (end), "i" __ARM_NR_cacheflush
67 : "r0", "r1", "r2", "r3");
71 static int manual_insmod_26(const char *fname, const char *opts)
73 unsigned long len, read_len;
78 f = fopen(fname, "rb");
82 fseek(f, 0, SEEK_END);
84 fseek(f, 0, SEEK_SET);
90 read_len = fread(buff, 1, len, f);
91 if (read_len != len) {
92 fprintf(stderr, "failed to read module\n");
96 ret = init_module(buff, len, opts);
108 char buff1[32], buff2[128];
111 warm_fd = open("/proc/warm", O_RDWR);
115 memset(&unm, 0, sizeof(unm));
118 if (strlen(unm.release) < 3 || unm.release[1] != '.') {
119 fprintf(stderr, "unexpected version string: %s\n", unm.release);
122 kernel_version = ((unm.release[0] - '0') << 4) | (unm.release[2] - '0');
124 snprintf(buff1, sizeof(buff1), "warm_%s.%s", unm.release, kernel_version >= 0x26 ? "ko" : "o");
125 snprintf(buff2, sizeof(buff2), "/sbin/insmod %s verbose=1", buff1);
130 fprintf(stderr, "system/insmod failed: %d %d\n", ret, errno);
131 if (kernel_version >= 0x26) {
132 ret = manual_insmod_26(buff1, "verbose=1");
134 fprintf(stderr, "manual insmod also failed: %d\n", ret);
138 warm_fd = open("/proc/warm", O_RDWR);
143 fprintf(stderr, "wARM: can't init, acting as sys_cacheflush wrapper\n");
147 void warm_finish(void)
157 if (kernel_version < 0x26) {
159 memset(&unm, 0, sizeof(unm));
161 snprintf(cmd, sizeof(cmd), "/sbin/rmmod warm_%s", unm.release);
164 strcpy(cmd, "/sbin/rmmod warm");
169 int warm_cache_op_range(int op, void *addr, unsigned long size)
171 struct warm_cache_op wop;
175 /* note that this won't work for warm_cache_op_all */
176 sys_cacheflush(addr, (char *)addr + size);
181 wop.addr = (unsigned long)addr;
184 ret = ioctl(warm_fd, WARMC_CACHE_OP, &wop);
186 perror("WARMC_CACHE_OP failed");
193 int warm_cache_op_all(int op)
195 return warm_cache_op_range(op, NULL, (unsigned long)-1);
198 int warm_change_cb_range(int cb, int is_set, void *addr, unsigned long size)
200 struct warm_change_cb ccb;
206 ccb.addr = (unsigned long)addr;
211 ret = ioctl(warm_fd, WARMC_CHANGE_CB, &ccb);
213 perror("WARMC_CHANGE_CB failed");
220 int warm_change_cb_upper(int cb, int is_set)
222 return warm_change_cb_range(cb, is_set, 0, 0);
225 unsigned long warm_virt2phys(const void *ptr)
230 ptrio = (unsigned long)ptr;
231 ret = ioctl(warm_fd, WARMC_VIRT2PHYS, &ptrio);
233 perror("WARMC_VIRT2PHYS failed");
234 return (unsigned long)-1;