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 /* provided by glibc */
46 extern long init_module(void *, unsigned long, const char *);
47 extern long delete_module(const char *, unsigned int);
49 static int warm_fd = -1;
50 static int kernel_version;
52 static void sys_cacheflush(void *start, void *end)
56 int num = __ARM_NR_cacheflush;
57 __asm__("mov r0, %0 ;"
61 "swi 0" : : "r" (start), "r" (end), "r" (num)
62 : "r0", "r1", "r2", "r3", "r7");
65 __asm__("mov r0, %0 ;"
68 "swi %2" : : "r" (start), "r" (end), "i" __ARM_NR_cacheflush
69 : "r0", "r1", "r2", "r3");
73 /* Those are here because system() occasionaly fails on Wiz
74 * with errno 12 for some unknown reason */
75 static int manual_insmod_26(const char *fname, const char *opts)
77 unsigned long len, read_len;
82 f = fopen(fname, "rb");
86 fseek(f, 0, SEEK_END);
88 fseek(f, 0, SEEK_SET);
94 read_len = fread(buff, 1, len, f);
95 if (read_len != len) {
96 fprintf(stderr, "failed to read module\n");
100 ret = init_module(buff, len, opts);
109 static int manual_rmmod(const char *name)
111 return delete_module(name, O_NONBLOCK|O_EXCL);
117 char buff1[32], buff2[128];
120 memset(&unm, 0, sizeof(unm));
123 if (strlen(unm.release) < 3 || unm.release[1] != '.') {
124 fprintf(stderr, "unexpected version string: %s\n", unm.release);
127 kernel_version = ((unm.release[0] - '0') << 4) | (unm.release[2] - '0');
129 warm_fd = open("/proc/warm", O_RDWR);
133 snprintf(buff1, sizeof(buff1), "warm_%s.%s", unm.release, kernel_version >= 0x26 ? "ko" : "o");
134 snprintf(buff2, sizeof(buff2), "/sbin/insmod %s verbose=1", buff1);
139 fprintf(stderr, "system/insmod failed: %d %d\n", ret, errno);
140 if (kernel_version >= 0x26) {
141 ret = manual_insmod_26(buff1, "verbose=1");
143 fprintf(stderr, "manual insmod also failed: %d\n", ret);
147 warm_fd = open("/proc/warm", O_RDWR);
152 fprintf(stderr, "wARM: can't init, acting as sys_cacheflush wrapper\n");
156 void warm_finish(void)
158 char name[32], cmd[64];
167 if (kernel_version < 0x26) {
169 memset(&unm, 0, sizeof(unm));
171 snprintf(name, sizeof(name), "warm_%s", unm.release);
174 strcpy(name, "warm");
176 snprintf(cmd, sizeof(cmd), "/sbin/rmmod %s", name);
179 fprintf(stderr, "system/rmmod failed: %d %d\n", ret, errno);
184 int warm_cache_op_range(int op, void *addr, unsigned long size)
186 struct warm_cache_op wop;
190 /* note that this won't work for warm_cache_op_all */
191 sys_cacheflush(addr, (char *)addr + size);
196 wop.addr = (unsigned long)addr;
199 ret = ioctl(warm_fd, WARMC_CACHE_OP, &wop);
201 perror("WARMC_CACHE_OP failed");
208 int warm_cache_op_all(int op)
210 return warm_cache_op_range(op, NULL, (unsigned long)-1);
213 int warm_change_cb_range(int cb, int is_set, void *addr, unsigned long size)
215 struct warm_change_cb ccb;
221 ccb.addr = (unsigned long)addr;
226 ret = ioctl(warm_fd, WARMC_CHANGE_CB, &ccb);
228 perror("WARMC_CHANGE_CB failed");
235 int warm_change_cb_upper(int cb, int is_set)
237 return warm_change_cb_range(cb, is_set, 0, 0);
240 unsigned long warm_virt2phys(const void *ptr)
245 ptrio = (unsigned long)ptr;
246 ret = ioctl(warm_fd, WARMC_VIRT2PHYS, &ptrio);
248 perror("WARMC_VIRT2PHYS failed");
249 return (unsigned long)-1;