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