prefix all messages
[warm.git] / warm.c
CommitLineData
198a1649 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>
cc951732 39#include <sys/syscall.h>
198a1649 40#include <errno.h>
41
42#define WARM_CODE
43#include "warm.h"
cc951732 44
bee2f7fb 45#define PFX "wARM: "
46
f43e9610 47/* provided by glibc */
48extern long init_module(void *, unsigned long, const char *);
49extern long delete_module(const char *, unsigned int);
198a1649 50
51static int warm_fd = -1;
cc951732 52static int kernel_version;
53
54static 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
f43e9610 75/* Those are here because system() occasionaly fails on Wiz
76 * with errno 12 for some unknown reason */
cc951732 77static 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) {
bee2f7fb 98 fprintf(stderr, PFX "failed to read module\n");
cc951732 99 goto fail1;
100 }
101
102 ret = init_module(buff, len, opts);
103
104fail1:
105 free(buff);
106fail0:
107 fclose(f);
108 return ret;
109}
198a1649 110
f43e9610 111static int manual_rmmod(const char *name)
112{
113 return delete_module(name, O_NONBLOCK|O_EXCL);
114}
115
198a1649 116int warm_init(void)
117{
118 struct utsname unm;
cc951732 119 char buff1[32], buff2[128];
120 int ret;
198a1649 121
198a1649 122 memset(&unm, 0, sizeof(unm));
123 uname(&unm);
cc951732 124
125 if (strlen(unm.release) < 3 || unm.release[1] != '.') {
bee2f7fb 126 fprintf(stderr, PFX "unexpected version string: %s\n", unm.release);
cc951732 127 goto fail;
128 }
129 kernel_version = ((unm.release[0] - '0') << 4) | (unm.release[2] - '0');
130
f43e9610 131 warm_fd = open("/proc/warm", O_RDWR);
132 if (warm_fd >= 0)
133 return 0;
134
cc951732 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);
198a1649 137
138 /* try to insmod */
cc951732 139 ret = system(buff2);
140 if (ret != 0) {
bee2f7fb 141 fprintf(stderr, PFX "system/insmod failed: %d %d\n", ret, errno);
cc951732 142 if (kernel_version >= 0x26) {
143 ret = manual_insmod_26(buff1, "verbose=1");
144 if (ret != 0)
bee2f7fb 145 fprintf(stderr, PFX "manual insmod also failed: %d\n", ret);
cc951732 146 }
147 }
148
198a1649 149 warm_fd = open("/proc/warm", O_RDWR);
150 if (warm_fd >= 0)
151 return 0;
152
cc951732 153fail:
bee2f7fb 154 fprintf(stderr, PFX "can't init, acting as sys_cacheflush wrapper\n");
198a1649 155 return -1;
156}
157
158void warm_finish(void)
159{
f43e9610 160 char name[32], cmd[64];
161 int ret;
cc951732 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);
f43e9610 173 snprintf(name, sizeof(name), "warm_%s", unm.release);
cc951732 174 }
175 else
f43e9610 176 strcpy(name, "warm");
cc951732 177
f43e9610 178 snprintf(cmd, sizeof(cmd), "/sbin/rmmod %s", name);
179 ret = system(cmd);
180 if (ret != 0) {
bee2f7fb 181 fprintf(stderr, PFX "system/rmmod failed: %d %d\n", ret, errno);
f43e9610 182 manual_rmmod(name);
183 }
198a1649 184}
185
186int 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) {
cc951732 192 /* note that this won't work for warm_cache_op_all */
198a1649 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
210int warm_cache_op_all(int op)
211{
cc951732 212 return warm_cache_op_range(op, NULL, (unsigned long)-1);
198a1649 213}
214
215int 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
237int warm_change_cb_upper(int cb, int is_set)
238{
239 return warm_change_cb_range(cb, is_set, 0, 0);
240}
241
242unsigned 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