r2 release
[warm.git] / warm.c
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
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <unistd.h>
36 #include <fcntl.h>
37 #include <sys/ioctl.h>
38 #include <sys/utsname.h>
39 #include <sys/syscall.h>
40 #include <errno.h>
41
42 #define WARM_CODE
43 #include "warm.h"
44
45 /* provided by glibc */
46 extern long init_module(void *, unsigned long, const char *);
47 extern long delete_module(const char *, unsigned int);
48
49 static int warm_fd = -1;
50 static int kernel_version;
51
52 static void sys_cacheflush(void *start, void *end)
53 {
54 #ifdef __ARM_EABI__
55         /* EABI version */
56         int num = __ARM_NR_cacheflush;
57         __asm__("mov  r0, %0 ;"
58                 "mov  r1, %1 ;"
59                 "mov  r2, #0 ;"
60                 "mov  r7, %2 ;"
61                 "swi  0" : : "r" (start), "r" (end), "r" (num)
62                         : "r0", "r1", "r2", "r3", "r7");
63 #else
64         /* OABI */
65         __asm__("mov  r0, %0 ;"
66                 "mov  r1, %1 ;"
67                 "mov  r2, #0 ;"
68                 "swi  %2" : : "r" (start), "r" (end), "i" __ARM_NR_cacheflush
69                         : "r0", "r1", "r2", "r3");
70 #endif
71 }
72
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)
76 {
77         unsigned long len, read_len;
78         int ret = -1;
79         void *buff;
80         FILE *f;
81
82         f = fopen(fname, "rb");
83         if (f == NULL)
84                 return -1;
85
86         fseek(f, 0, SEEK_END);
87         len = ftell(f);
88         fseek(f, 0, SEEK_SET);
89
90         buff = malloc(len);
91         if (buff == NULL)
92                 goto fail0;
93
94         read_len = fread(buff, 1, len, f);
95         if (read_len != len) {
96                 fprintf(stderr, "failed to read module\n");
97                 goto fail1;
98         }
99
100         ret = init_module(buff, len, opts);
101
102 fail1:
103         free(buff);
104 fail0:
105         fclose(f);
106         return ret;
107 }
108
109 static int manual_rmmod(const char *name)
110 {
111         return delete_module(name, O_NONBLOCK|O_EXCL);
112 }
113
114 int warm_init(void)
115 {
116         struct utsname unm;
117         char buff1[32], buff2[128];
118         int ret;
119
120         memset(&unm, 0, sizeof(unm));
121         uname(&unm);
122
123         if (strlen(unm.release) < 3 || unm.release[1] != '.') {
124                 fprintf(stderr, "unexpected version string: %s\n", unm.release);
125                 goto fail;
126         }
127         kernel_version = ((unm.release[0] - '0') << 4) | (unm.release[2] - '0');
128
129         warm_fd = open("/proc/warm", O_RDWR);
130         if (warm_fd >= 0)
131                 return 0;
132
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);
135
136         /* try to insmod */
137         ret = system(buff2);
138         if (ret != 0) {
139                 fprintf(stderr, "system/insmod failed: %d %d\n", ret, errno);
140                 if (kernel_version >= 0x26) {
141                         ret = manual_insmod_26(buff1, "verbose=1");
142                         if (ret != 0)
143                                 fprintf(stderr, "manual insmod also failed: %d\n", ret);
144                 }
145         }
146
147         warm_fd = open("/proc/warm", O_RDWR);
148         if (warm_fd >= 0)
149                 return 0;
150
151 fail:
152         fprintf(stderr, "wARM: can't init, acting as sys_cacheflush wrapper\n");
153         return -1;
154 }
155
156 void warm_finish(void)
157 {
158         char name[32], cmd[64];
159         int ret;
160
161         if (warm_fd < 0)
162                 return;
163
164         close(warm_fd);
165         warm_fd = -1;
166
167         if (kernel_version < 0x26) {
168                 struct utsname unm;
169                 memset(&unm, 0, sizeof(unm));
170                 uname(&unm);
171                 snprintf(name, sizeof(name), "warm_%s", unm.release);
172         }
173         else
174                 strcpy(name, "warm");
175
176         snprintf(cmd, sizeof(cmd), "/sbin/rmmod %s", name);
177         ret = system(cmd);
178         if (ret != 0) {
179                 fprintf(stderr, "system/rmmod failed: %d %d\n", ret, errno);
180                 manual_rmmod(name);
181         }
182 }
183
184 int warm_cache_op_range(int op, void *addr, unsigned long size)
185 {
186         struct warm_cache_op wop;
187         int ret;
188
189         if (warm_fd < 0) {
190                 /* note that this won't work for warm_cache_op_all */
191                 sys_cacheflush(addr, (char *)addr + size);
192                 return -1;
193         }
194
195         wop.ops = op;
196         wop.addr = (unsigned long)addr;
197         wop.size = size;
198
199         ret = ioctl(warm_fd, WARMC_CACHE_OP, &wop);
200         if (ret != 0) {
201                 perror("WARMC_CACHE_OP failed");
202                 return -1;
203         }
204
205         return 0;
206 }
207
208 int warm_cache_op_all(int op)
209 {
210         return warm_cache_op_range(op, NULL, (unsigned long)-1);
211 }
212
213 int warm_change_cb_range(int cb, int is_set, void *addr, unsigned long size)
214 {
215         struct warm_change_cb ccb;
216         int ret;
217
218         if (warm_fd < 0)
219                 return -1;
220         
221         ccb.addr = (unsigned long)addr;
222         ccb.size = size;
223         ccb.cb = cb;
224         ccb.is_set = is_set;
225
226         ret = ioctl(warm_fd, WARMC_CHANGE_CB, &ccb);
227         if (ret != 0) {
228                 perror("WARMC_CHANGE_CB failed");
229                 return -1;
230         }
231
232         return 0;
233 }
234
235 int warm_change_cb_upper(int cb, int is_set)
236 {
237         return warm_change_cb_range(cb, is_set, 0, 0);
238 }
239
240 unsigned long warm_virt2phys(const void *ptr)
241 {
242         unsigned long ptrio;
243         int ret;
244
245         ptrio = (unsigned long)ptr;
246         ret = ioctl(warm_fd, WARMC_VIRT2PHYS, &ptrio);
247         if (ret != 0) {
248                 perror("WARMC_VIRT2PHYS failed");
249                 return (unsigned long)-1;
250         }
251
252         return ptrio;
253 }
254