blob: 241ee7a2906e413c6d367cf4e4d7356e68419106 [file] [log] [blame]
Ralf Baechle26009902006-04-05 09:45:45 +01001/*
2 * Copyright (C) 2005 MIPS Technologies, Inc. All rights reserved.
3 *
4 * This program is free software; you can distribute it and/or modify it
5 * under the terms of the GNU General Public License (Version 2) as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
11 * for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
16 *
17 */
18#include <linux/kernel.h>
19#include <linux/module.h>
20#include <linux/unistd.h>
21#include <linux/file.h>
22#include <linux/fs.h>
23#include <linux/syscalls.h>
24#include <linux/workqueue.h>
25#include <linux/errno.h>
26#include <linux/list.h>
27
28#include <asm/vpe.h>
29#include <asm/rtlx.h>
30#include <asm/kspd.h>
31
32static struct workqueue_struct *workqueue = NULL;
33static struct work_struct work;
34
35extern unsigned long cpu_khz;
36
37struct mtsp_syscall {
38 int cmd;
39 unsigned char abi;
40 unsigned char size;
41};
42
43struct mtsp_syscall_ret {
44 int retval;
45 int errno;
46};
47
48struct mtsp_syscall_generic {
49 int arg0;
50 int arg1;
51 int arg2;
52 int arg3;
53 int arg4;
54 int arg5;
55 int arg6;
56};
57
58static struct list_head kspd_notifylist;
59static int sp_stopping = 0;
60
61/* these should match with those in the SDE kit */
62#define MTSP_SYSCALL_BASE 0
63#define MTSP_SYSCALL_EXIT (MTSP_SYSCALL_BASE + 0)
64#define MTSP_SYSCALL_OPEN (MTSP_SYSCALL_BASE + 1)
65#define MTSP_SYSCALL_READ (MTSP_SYSCALL_BASE + 2)
66#define MTSP_SYSCALL_WRITE (MTSP_SYSCALL_BASE + 3)
67#define MTSP_SYSCALL_CLOSE (MTSP_SYSCALL_BASE + 4)
68#define MTSP_SYSCALL_LSEEK32 (MTSP_SYSCALL_BASE + 5)
69#define MTSP_SYSCALL_ISATTY (MTSP_SYSCALL_BASE + 6)
70#define MTSP_SYSCALL_GETTIME (MTSP_SYSCALL_BASE + 7)
71#define MTSP_SYSCALL_PIPEFREQ (MTSP_SYSCALL_BASE + 8)
72#define MTSP_SYSCALL_GETTOD (MTSP_SYSCALL_BASE + 9)
Ralf Baechle0e6ee852007-03-13 15:10:50 +000073#define MTSP_SYSCALL_IOCTL (MTSP_SYSCALL_BASE + 10)
Ralf Baechle26009902006-04-05 09:45:45 +010074
75#define MTSP_O_RDONLY 0x0000
76#define MTSP_O_WRONLY 0x0001
77#define MTSP_O_RDWR 0x0002
78#define MTSP_O_NONBLOCK 0x0004
79#define MTSP_O_APPEND 0x0008
80#define MTSP_O_SHLOCK 0x0010
81#define MTSP_O_EXLOCK 0x0020
82#define MTSP_O_ASYNC 0x0040
83#define MTSP_O_FSYNC O_SYNC
84#define MTSP_O_NOFOLLOW 0x0100
85#define MTSP_O_SYNC 0x0080
86#define MTSP_O_CREAT 0x0200
87#define MTSP_O_TRUNC 0x0400
88#define MTSP_O_EXCL 0x0800
89#define MTSP_O_BINARY 0x8000
90
91#define SP_VPE 1
92
93struct apsp_table {
94 int sp;
95 int ap;
96};
97
98/* we might want to do the mode flags too */
99struct apsp_table open_flags_table[] = {
100 { MTSP_O_RDWR, O_RDWR },
101 { MTSP_O_WRONLY, O_WRONLY },
102 { MTSP_O_CREAT, O_CREAT },
103 { MTSP_O_TRUNC, O_TRUNC },
104 { MTSP_O_NONBLOCK, O_NONBLOCK },
105 { MTSP_O_APPEND, O_APPEND },
106 { MTSP_O_NOFOLLOW, O_NOFOLLOW }
107};
108
109struct apsp_table syscall_command_table[] = {
110 { MTSP_SYSCALL_OPEN, __NR_open },
111 { MTSP_SYSCALL_CLOSE, __NR_close },
112 { MTSP_SYSCALL_READ, __NR_read },
113 { MTSP_SYSCALL_WRITE, __NR_write },
Ralf Baechle0e6ee852007-03-13 15:10:50 +0000114 { MTSP_SYSCALL_LSEEK32, __NR_lseek },
115 { MTSP_SYSCALL_IOCTL, __NR_ioctl }
Ralf Baechle26009902006-04-05 09:45:45 +0100116};
117
118static int sp_syscall(int num, int arg0, int arg1, int arg2, int arg3)
119{
120 register long int _num __asm__ ("$2") = num;
121 register long int _arg0 __asm__ ("$4") = arg0;
122 register long int _arg1 __asm__ ("$5") = arg1;
123 register long int _arg2 __asm__ ("$6") = arg2;
124 register long int _arg3 __asm__ ("$7") = arg3;
125
126 mm_segment_t old_fs;
127
128 old_fs = get_fs();
129 set_fs(KERNEL_DS);
130
131 __asm__ __volatile__ (
132 " syscall \n"
133 : "=r" (_num), "=r" (_arg3)
134 : "r" (_num), "r" (_arg0), "r" (_arg1), "r" (_arg2), "r" (_arg3));
135
136 set_fs(old_fs);
137
138 /* $a3 is error flag */
139 if (_arg3)
140 return -_num;
141
142 return _num;
143}
144
145static int translate_syscall_command(int cmd)
146{
147 int i;
148 int ret = -1;
149
150 for (i = 0; i < ARRAY_SIZE(syscall_command_table); i++) {
151 if ((cmd == syscall_command_table[i].sp))
152 return syscall_command_table[i].ap;
153 }
154
155 return ret;
156}
157
158static unsigned int translate_open_flags(int flags)
159{
160 int i;
161 unsigned int ret = 0;
162
163 for (i = 0; i < (sizeof(open_flags_table) / sizeof(struct apsp_table));
164 i++) {
165 if( (flags & open_flags_table[i].sp) ) {
166 ret |= open_flags_table[i].ap;
167 }
168 }
169
170 return ret;
171}
172
173
174static void sp_setfsuidgid( uid_t uid, gid_t gid)
175{
176 current->fsuid = uid;
177 current->fsgid = gid;
178
179 key_fsuid_changed(current);
180 key_fsgid_changed(current);
181}
182
183/*
184 * Expects a request to be on the sysio channel. Reads it. Decides whether
185 * its a linux syscall and runs it, or whatever. Puts the return code back
186 * into the request and sends the whole thing back.
187 */
188void sp_work_handle_request(void)
189{
190 struct mtsp_syscall sc;
191 struct mtsp_syscall_generic generic;
192 struct mtsp_syscall_ret ret;
193 struct kspd_notifications *n;
194 struct timeval tv;
195 struct timezone tz;
196 int cmd;
197
198 char *vcwd;
199 mm_segment_t old_fs;
200 int size;
201
202 ret.retval = -1;
203
204 if (!rtlx_read(RTLX_CHANNEL_SYSIO, &sc, sizeof(struct mtsp_syscall), 0)) {
205 printk(KERN_ERR "Expected request but nothing to read\n");
206 return;
207 }
208
209 size = sc.size;
210
211 if (size) {
212 if (!rtlx_read(RTLX_CHANNEL_SYSIO, &generic, size, 0)) {
213 printk(KERN_ERR "Expected request but nothing to read\n");
214 return;
215 }
216 }
217
218 /* Run the syscall at the priviledge of the user who loaded the
219 SP program */
220
221 if (vpe_getuid(SP_VPE))
222 sp_setfsuidgid( vpe_getuid(SP_VPE), vpe_getgid(SP_VPE));
223
224 switch (sc.cmd) {
225 /* needs the flags argument translating from SDE kit to
226 linux */
227 case MTSP_SYSCALL_PIPEFREQ:
228 ret.retval = cpu_khz * 1000;
229 ret.errno = 0;
230 break;
231
232 case MTSP_SYSCALL_GETTOD:
233 memset(&tz, 0, sizeof(tz));
234 if ((ret.retval = sp_syscall(__NR_gettimeofday, (int)&tv,
235 (int)&tz, 0,0)) == 0)
236 ret.retval = tv.tv_sec;
237
238 ret.errno = errno;
239 break;
240
241 case MTSP_SYSCALL_EXIT:
242 list_for_each_entry(n, &kspd_notifylist, list)
243 n->kspd_sp_exit(SP_VPE);
244 sp_stopping = 1;
245
246 printk(KERN_DEBUG "KSPD got exit syscall from SP exitcode %d\n",
247 generic.arg0);
248 break;
249
250 case MTSP_SYSCALL_OPEN:
251 generic.arg1 = translate_open_flags(generic.arg1);
252
253 vcwd = vpe_getcwd(SP_VPE);
254
255 /* change to the cwd of the process that loaded the SP program */
256 old_fs = get_fs();
257 set_fs(KERNEL_DS);
258 sys_chdir(vcwd);
259 set_fs(old_fs);
260
261 sc.cmd = __NR_open;
262
263 /* fall through */
264
265 default:
266 if ((sc.cmd >= __NR_Linux) &&
267 (sc.cmd <= (__NR_Linux + __NR_Linux_syscalls)) )
268 cmd = sc.cmd;
269 else
270 cmd = translate_syscall_command(sc.cmd);
271
272 if (cmd >= 0) {
273 ret.retval = sp_syscall(cmd, generic.arg0, generic.arg1,
274 generic.arg2, generic.arg3);
275 ret.errno = errno;
276 } else
277 printk(KERN_WARNING
278 "KSPD: Unknown SP syscall number %d\n", sc.cmd);
279 break;
280 } /* switch */
281
282 if (vpe_getuid(SP_VPE))
283 sp_setfsuidgid( 0, 0);
284
285 if ((rtlx_write(RTLX_CHANNEL_SYSIO, &ret, sizeof(struct mtsp_syscall_ret), 0))
286 < sizeof(struct mtsp_syscall_ret))
287 printk("KSPD: sp_work_handle_request failed to send to SP\n");
288}
289
290static void sp_cleanup(void)
291{
292 struct files_struct *files = current->files;
293 int i, j;
294 struct fdtable *fdt;
295
296 j = 0;
297
298 /*
299 * It is safe to dereference the fd table without RCU or
300 * ->file_lock
301 */
302 fdt = files_fdtable(files);
303 for (;;) {
304 unsigned long set;
305 i = j * __NFDBITS;
Vadim Lobanovbbea9f62006-12-10 02:21:12 -0800306 if (i >= fdt->max_fds)
Ralf Baechle26009902006-04-05 09:45:45 +0100307 break;
308 set = fdt->open_fds->fds_bits[j++];
309 while (set) {
310 if (set & 1) {
311 struct file * file = xchg(&fdt->fd[i], NULL);
312 if (file)
313 filp_close(file, files);
314 }
315 i++;
316 set >>= 1;
317 }
318 }
319}
320
321static int channel_open = 0;
322
323/* the work handler */
David Howells6d5aefb2006-12-05 19:36:26 +0000324static void sp_work(struct work_struct *unused)
Ralf Baechle26009902006-04-05 09:45:45 +0100325{
326 if (!channel_open) {
327 if( rtlx_open(RTLX_CHANNEL_SYSIO, 1) != 0) {
328 printk("KSPD: unable to open sp channel\n");
329 sp_stopping = 1;
330 } else {
331 channel_open++;
332 printk(KERN_DEBUG "KSPD: SP channel opened\n");
333 }
334 } else {
335 /* wait for some data, allow it to sleep */
336 rtlx_read_poll(RTLX_CHANNEL_SYSIO, 1);
337
338 /* Check we haven't been woken because we are stopping */
339 if (!sp_stopping)
340 sp_work_handle_request();
341 }
342
343 if (!sp_stopping)
344 queue_work(workqueue, &work);
345 else
346 sp_cleanup();
347}
348
349static void startwork(int vpe)
350{
351 sp_stopping = channel_open = 0;
352
353 if (workqueue == NULL) {
354 if ((workqueue = create_singlethread_workqueue("kspd")) == NULL) {
355 printk(KERN_ERR "unable to start kspd\n");
356 return;
357 }
358
David Howells6d5aefb2006-12-05 19:36:26 +0000359 INIT_WORK(&work, sp_work);
Ralf Baechle26009902006-04-05 09:45:45 +0100360 queue_work(workqueue, &work);
361 } else
362 queue_work(workqueue, &work);
363
364}
365
366static void stopwork(int vpe)
367{
368 sp_stopping = 1;
369
370 printk(KERN_DEBUG "KSPD: SP stopping\n");
371}
372
373void kspd_notify(struct kspd_notifications *notify)
374{
375 list_add(&notify->list, &kspd_notifylist);
376}
377
378static struct vpe_notifications notify;
379static int kspd_module_init(void)
380{
381 INIT_LIST_HEAD(&kspd_notifylist);
382
383 notify.start = startwork;
384 notify.stop = stopwork;
385 vpe_notify(SP_VPE, &notify);
386
387 return 0;
388}
389
390static void kspd_module_exit(void)
391{
392
393}
394
395module_init(kspd_module_init);
396module_exit(kspd_module_exit);
397
398MODULE_DESCRIPTION("MIPS KSPD");
399MODULE_AUTHOR("Elizabeth Oldham, MIPS Technologies, Inc.");
400MODULE_LICENSE("GPL");