Merge branch 'for_paulus' of master.kernel.org:/pub/scm/linux/kernel/git/galak/powerpc
[pandora-kernel.git] / arch / mips / kernel / kspd.c
1 /*
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
32 static struct workqueue_struct *workqueue = NULL;
33 static struct work_struct work;
34
35 extern unsigned long cpu_khz;
36
37 struct mtsp_syscall {
38         int cmd;
39         unsigned char abi;
40         unsigned char size;
41 };
42
43 struct mtsp_syscall_ret {
44         int retval;
45         int errno;
46 };
47
48 struct 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
58 static struct list_head kspd_notifylist;
59 static 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)
73
74 #define MTSP_O_RDONLY           0x0000
75 #define MTSP_O_WRONLY           0x0001
76 #define MTSP_O_RDWR             0x0002
77 #define MTSP_O_NONBLOCK         0x0004
78 #define MTSP_O_APPEND           0x0008
79 #define MTSP_O_SHLOCK           0x0010
80 #define MTSP_O_EXLOCK           0x0020
81 #define MTSP_O_ASYNC            0x0040
82 #define MTSP_O_FSYNC            O_SYNC
83 #define MTSP_O_NOFOLLOW         0x0100
84 #define MTSP_O_SYNC             0x0080
85 #define MTSP_O_CREAT            0x0200
86 #define MTSP_O_TRUNC            0x0400
87 #define MTSP_O_EXCL             0x0800
88 #define MTSP_O_BINARY           0x8000
89
90 #define SP_VPE 1
91
92 struct apsp_table  {
93         int sp;
94         int ap;
95 };
96
97 /* we might want to do the mode flags too */
98 struct apsp_table open_flags_table[] = {
99         { MTSP_O_RDWR, O_RDWR },
100         { MTSP_O_WRONLY, O_WRONLY },
101         { MTSP_O_CREAT, O_CREAT },
102         { MTSP_O_TRUNC, O_TRUNC },
103         { MTSP_O_NONBLOCK, O_NONBLOCK },
104         { MTSP_O_APPEND, O_APPEND },
105         { MTSP_O_NOFOLLOW, O_NOFOLLOW }
106 };
107
108 struct apsp_table syscall_command_table[] = {
109         { MTSP_SYSCALL_OPEN, __NR_open },
110         { MTSP_SYSCALL_CLOSE, __NR_close },
111         { MTSP_SYSCALL_READ, __NR_read },
112         { MTSP_SYSCALL_WRITE, __NR_write },
113         { MTSP_SYSCALL_LSEEK32, __NR_lseek }
114 };
115
116 static int sp_syscall(int num, int arg0, int arg1, int arg2, int arg3)
117 {
118         register long int _num  __asm__ ("$2") = num;
119         register long int _arg0  __asm__ ("$4") = arg0;
120         register long int _arg1  __asm__ ("$5") = arg1;
121         register long int _arg2  __asm__ ("$6") = arg2;
122         register long int _arg3  __asm__ ("$7") = arg3;
123
124         mm_segment_t old_fs;
125
126         old_fs = get_fs();
127         set_fs(KERNEL_DS);
128
129         __asm__ __volatile__ (
130         "       syscall                                 \n"
131         : "=r" (_num), "=r" (_arg3)
132         : "r" (_num), "r" (_arg0), "r" (_arg1), "r" (_arg2), "r" (_arg3));
133
134         set_fs(old_fs);
135
136         /* $a3 is error flag */
137         if (_arg3)
138                 return -_num;
139
140         return _num;
141 }
142
143 static int translate_syscall_command(int cmd)
144 {
145         int i;
146         int ret = -1;
147
148         for (i = 0; i < ARRAY_SIZE(syscall_command_table); i++) {
149                 if ((cmd == syscall_command_table[i].sp))
150                         return syscall_command_table[i].ap;
151         }
152
153         return ret;
154 }
155
156 static unsigned int translate_open_flags(int flags)
157 {
158         int i;
159         unsigned int ret = 0;
160
161         for (i = 0; i < (sizeof(open_flags_table) / sizeof(struct apsp_table));
162              i++) {
163                 if( (flags & open_flags_table[i].sp) ) {
164                         ret |= open_flags_table[i].ap;
165                 }
166         }
167
168         return ret;
169 }
170
171
172 static void sp_setfsuidgid( uid_t uid, gid_t gid)
173 {
174         current->fsuid = uid;
175         current->fsgid = gid;
176
177         key_fsuid_changed(current);
178         key_fsgid_changed(current);
179 }
180
181 /*
182  * Expects a request to be on the sysio channel. Reads it.  Decides whether
183  * its a linux syscall and runs it, or whatever.  Puts the return code back
184  * into the request and sends the whole thing back.
185  */
186 void sp_work_handle_request(void)
187 {
188         struct mtsp_syscall sc;
189         struct mtsp_syscall_generic generic;
190         struct mtsp_syscall_ret ret;
191         struct kspd_notifications *n;
192         struct timeval tv;
193         struct timezone tz;
194         int cmd;
195
196         char *vcwd;
197         mm_segment_t old_fs;
198         int size;
199
200         ret.retval = -1;
201
202         if (!rtlx_read(RTLX_CHANNEL_SYSIO, &sc, sizeof(struct mtsp_syscall), 0)) {
203                 printk(KERN_ERR "Expected request but nothing to read\n");
204                 return;
205         }
206
207         size = sc.size;
208
209         if (size) {
210                 if (!rtlx_read(RTLX_CHANNEL_SYSIO, &generic, size, 0)) {
211                         printk(KERN_ERR "Expected request but nothing to read\n");
212                         return;
213                 }
214         }
215
216         /* Run the syscall at the priviledge of the user who loaded the
217            SP program */
218
219         if (vpe_getuid(SP_VPE))
220                 sp_setfsuidgid( vpe_getuid(SP_VPE), vpe_getgid(SP_VPE));
221
222         switch (sc.cmd) {
223         /* needs the flags argument translating from SDE kit to
224            linux */
225         case MTSP_SYSCALL_PIPEFREQ:
226                 ret.retval = cpu_khz * 1000;
227                 ret.errno = 0;
228                 break;
229
230         case MTSP_SYSCALL_GETTOD:
231                 memset(&tz, 0, sizeof(tz));
232                 if ((ret.retval = sp_syscall(__NR_gettimeofday, (int)&tv,
233                                              (int)&tz, 0,0)) == 0)
234                 ret.retval = tv.tv_sec;
235
236                 ret.errno = errno;
237                 break;
238
239         case MTSP_SYSCALL_EXIT:
240                 list_for_each_entry(n, &kspd_notifylist, list)
241                         n->kspd_sp_exit(SP_VPE);
242                 sp_stopping = 1;
243
244                 printk(KERN_DEBUG "KSPD got exit syscall from SP exitcode %d\n",
245                        generic.arg0);
246                 break;
247
248         case MTSP_SYSCALL_OPEN:
249                 generic.arg1 = translate_open_flags(generic.arg1);
250
251                 vcwd = vpe_getcwd(SP_VPE);
252
253                 /* change to the cwd of the process that loaded the SP program */
254                 old_fs = get_fs();
255                 set_fs(KERNEL_DS);
256                 sys_chdir(vcwd);
257                 set_fs(old_fs);
258
259                 sc.cmd = __NR_open;
260
261                 /* fall through */
262
263         default:
264                 if ((sc.cmd >= __NR_Linux) &&
265                     (sc.cmd <= (__NR_Linux +  __NR_Linux_syscalls)) )
266                         cmd = sc.cmd;
267                 else
268                         cmd = translate_syscall_command(sc.cmd);
269
270                 if (cmd >= 0) {
271                         ret.retval = sp_syscall(cmd, generic.arg0, generic.arg1,
272                                                 generic.arg2, generic.arg3);
273                         ret.errno = errno;
274                 } else
275                         printk(KERN_WARNING
276                                "KSPD: Unknown SP syscall number %d\n", sc.cmd);
277                 break;
278         } /* switch */
279
280         if (vpe_getuid(SP_VPE))
281                 sp_setfsuidgid( 0, 0);
282
283         if ((rtlx_write(RTLX_CHANNEL_SYSIO, &ret, sizeof(struct mtsp_syscall_ret), 0))
284             < sizeof(struct mtsp_syscall_ret))
285                 printk("KSPD: sp_work_handle_request failed to send to SP\n");
286 }
287
288 static void sp_cleanup(void)
289 {
290         struct files_struct *files = current->files;
291         int i, j;
292         struct fdtable *fdt;
293
294         j = 0;
295
296         /*
297          * It is safe to dereference the fd table without RCU or
298          * ->file_lock
299          */
300         fdt = files_fdtable(files);
301         for (;;) {
302                 unsigned long set;
303                 i = j * __NFDBITS;
304                 if (i >= fdt->max_fdset || i >= fdt->max_fds)
305                         break;
306                 set = fdt->open_fds->fds_bits[j++];
307                 while (set) {
308                         if (set & 1) {
309                                 struct file * file = xchg(&fdt->fd[i], NULL);
310                                 if (file)
311                                         filp_close(file, files);
312                         }
313                         i++;
314                         set >>= 1;
315                 }
316         }
317 }
318
319 static int channel_open = 0;
320
321 /* the work handler */
322 static void sp_work(void *data)
323 {
324         if (!channel_open) {
325                 if( rtlx_open(RTLX_CHANNEL_SYSIO, 1) != 0) {
326                         printk("KSPD: unable to open sp channel\n");
327                         sp_stopping = 1;
328                 } else {
329                         channel_open++;
330                         printk(KERN_DEBUG "KSPD: SP channel opened\n");
331                 }
332         } else {
333                 /* wait for some data, allow it to sleep */
334                 rtlx_read_poll(RTLX_CHANNEL_SYSIO, 1);
335
336                 /* Check we haven't been woken because we are stopping */
337                 if (!sp_stopping)
338                         sp_work_handle_request();
339         }
340
341         if (!sp_stopping)
342                 queue_work(workqueue, &work);
343         else
344                 sp_cleanup();
345 }
346
347 static void startwork(int vpe)
348 {
349         sp_stopping = channel_open = 0;
350
351         if (workqueue == NULL) {
352                 if ((workqueue = create_singlethread_workqueue("kspd")) == NULL) {
353                         printk(KERN_ERR "unable to start kspd\n");
354                         return;
355                 }
356
357                 INIT_WORK(&work, sp_work, NULL);
358                 queue_work(workqueue, &work);
359         } else
360                 queue_work(workqueue, &work);
361
362 }
363
364 static void stopwork(int vpe)
365 {
366         sp_stopping = 1;
367
368         printk(KERN_DEBUG "KSPD: SP stopping\n");
369 }
370
371 void kspd_notify(struct kspd_notifications *notify)
372 {
373         list_add(&notify->list, &kspd_notifylist);
374 }
375
376 static struct vpe_notifications notify;
377 static int kspd_module_init(void)
378 {
379         INIT_LIST_HEAD(&kspd_notifylist);
380
381         notify.start = startwork;
382         notify.stop = stopwork;
383         vpe_notify(SP_VPE, &notify);
384
385         return 0;
386 }
387
388 static void kspd_module_exit(void)
389 {
390
391 }
392
393 module_init(kspd_module_init);
394 module_exit(kspd_module_exit);
395
396 MODULE_DESCRIPTION("MIPS KSPD");
397 MODULE_AUTHOR("Elizabeth Oldham, MIPS Technologies, Inc.");
398 MODULE_LICENSE("GPL");