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