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>
47 /* provided by glibc */
48 extern long init_module(void *, unsigned long, const char *);
49 extern long delete_module(const char *, unsigned int);
51 static int warm_fd = -1;
52 static int kernel_version;
53 static int module_inserted;
55 static void sys_cacheflush(void *start, void *end)
59 int num = __ARM_NR_cacheflush;
60 __asm__("mov r0, %0 ;"
64 "swi 0" : : "r" (start), "r" (end), "r" (num)
65 : "r0", "r1", "r2", "r3", "r7");
68 __asm__("mov r0, %0 ;"
71 "swi %2" : : "r" (start), "r" (end), "i" __ARM_NR_cacheflush
72 : "r0", "r1", "r2", "r3");
76 /* Those are here because system() occasionaly fails on Wiz
77 * with errno 12 for some unknown reason */
78 static int manual_insmod_26(const char *fname, const char *opts)
80 unsigned long len, read_len;
85 f = fopen(fname, "rb");
89 fseek(f, 0, SEEK_END);
91 fseek(f, 0, SEEK_SET);
97 read_len = fread(buff, 1, len, f);
98 if (read_len != len) {
99 fprintf(stderr, PFX "failed to read module\n");
103 ret = init_module(buff, len, opts);
112 static int manual_rmmod(const char *name)
114 return delete_module(name, O_NONBLOCK|O_EXCL);
120 char buff1[32], buff2[128];
123 memset(&unm, 0, sizeof(unm));
126 if (strlen(unm.release) < 3 || unm.release[1] != '.') {
127 fprintf(stderr, PFX "unexpected version string: %s\n", unm.release);
130 kernel_version = ((unm.release[0] - '0') << 4) | (unm.release[2] - '0');
132 warm_fd = open("/proc/warm", O_RDWR);
136 snprintf(buff1, sizeof(buff1), "warm_%s.%s", unm.release, kernel_version >= 0x26 ? "ko" : "o");
137 snprintf(buff2, sizeof(buff2), "/sbin/insmod %s verbose=1", buff1);
142 fprintf(stderr, PFX "system/insmod failed: %d %d\n", ret, errno);
143 if (kernel_version >= 0x26) {
144 ret = manual_insmod_26(buff1, "verbose=1");
146 fprintf(stderr, PFX "manual insmod also failed: %d\n", ret);
152 warm_fd = open("/proc/warm", O_RDWR);
157 fprintf(stderr, PFX "can't init, acting as sys_cacheflush wrapper\n");
161 void warm_finish(void)
163 char name[32], cmd[64];
172 if (module_inserted) {
173 if (kernel_version < 0x26) {
175 memset(&unm, 0, sizeof(unm));
177 snprintf(name, sizeof(name), "warm_%s", unm.release);
180 strcpy(name, "warm");
182 snprintf(cmd, sizeof(cmd), "/sbin/rmmod %s", name);
185 fprintf(stderr, PFX "system/rmmod failed: %d %d\n", ret, errno);
191 int warm_cache_op_range(int op, void *addr, unsigned long size)
193 struct warm_cache_op wop;
197 /* note that this won't work for warm_cache_op_all */
198 sys_cacheflush(addr, (char *)addr + size);
203 wop.addr = (unsigned long)addr;
206 ret = ioctl(warm_fd, WARMC_CACHE_OP, &wop);
208 perror("WARMC_CACHE_OP failed");
215 int warm_cache_op_all(int op)
217 return warm_cache_op_range(op, NULL, (unsigned long)-1);
220 int warm_change_cb_range(int cb, int is_set, void *addr, unsigned long size)
222 struct warm_change_cb ccb;
228 ccb.addr = (unsigned long)addr;
233 ret = ioctl(warm_fd, WARMC_CHANGE_CB, &ccb);
235 perror("WARMC_CHANGE_CB failed");
242 int warm_change_cb_upper(int cb, int is_set)
244 return warm_change_cb_range(cb, is_set, 0, 0);
247 unsigned long warm_virt2phys(const void *ptr)
252 ptrio = (unsigned long)ptr;
253 ret = ioctl(warm_fd, WARMC_VIRT2PHYS, &ptrio);
255 perror("WARMC_VIRT2PHYS failed");
256 return (unsigned long)-1;