2 * Copyright (C) 2005 MIPS Technologies, Inc. All rights reserved.
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.
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
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.
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/sched.h>
21 #include <linux/unistd.h>
22 #include <linux/file.h>
23 #include <linux/fdtable.h>
25 #include <linux/syscalls.h>
26 #include <linux/workqueue.h>
27 #include <linux/errno.h>
28 #include <linux/list.h>
34 static struct workqueue_struct
*workqueue
;
35 static struct work_struct work
;
37 extern unsigned long cpu_khz
;
45 struct mtsp_syscall_ret
{
50 struct mtsp_syscall_generic
{
60 static struct list_head kspd_notifylist
;
61 static int sp_stopping
;
63 /* these should match with those in the SDE kit */
64 #define MTSP_SYSCALL_BASE 0
65 #define MTSP_SYSCALL_EXIT (MTSP_SYSCALL_BASE + 0)
66 #define MTSP_SYSCALL_OPEN (MTSP_SYSCALL_BASE + 1)
67 #define MTSP_SYSCALL_READ (MTSP_SYSCALL_BASE + 2)
68 #define MTSP_SYSCALL_WRITE (MTSP_SYSCALL_BASE + 3)
69 #define MTSP_SYSCALL_CLOSE (MTSP_SYSCALL_BASE + 4)
70 #define MTSP_SYSCALL_LSEEK32 (MTSP_SYSCALL_BASE + 5)
71 #define MTSP_SYSCALL_ISATTY (MTSP_SYSCALL_BASE + 6)
72 #define MTSP_SYSCALL_GETTIME (MTSP_SYSCALL_BASE + 7)
73 #define MTSP_SYSCALL_PIPEFREQ (MTSP_SYSCALL_BASE + 8)
74 #define MTSP_SYSCALL_GETTOD (MTSP_SYSCALL_BASE + 9)
75 #define MTSP_SYSCALL_IOCTL (MTSP_SYSCALL_BASE + 10)
77 #define MTSP_O_RDONLY 0x0000
78 #define MTSP_O_WRONLY 0x0001
79 #define MTSP_O_RDWR 0x0002
80 #define MTSP_O_NONBLOCK 0x0004
81 #define MTSP_O_APPEND 0x0008
82 #define MTSP_O_SHLOCK 0x0010
83 #define MTSP_O_EXLOCK 0x0020
84 #define MTSP_O_ASYNC 0x0040
85 /* XXX: check which of these is actually O_SYNC vs O_DSYNC */
86 #define MTSP_O_FSYNC O_SYNC
87 #define MTSP_O_NOFOLLOW 0x0100
88 #define MTSP_O_SYNC 0x0080
89 #define MTSP_O_CREAT 0x0200
90 #define MTSP_O_TRUNC 0x0400
91 #define MTSP_O_EXCL 0x0800
92 #define MTSP_O_BINARY 0x8000
101 /* we might want to do the mode flags too */
102 struct apsp_table open_flags_table
[] = {
103 { MTSP_O_RDWR
, O_RDWR
},
104 { MTSP_O_WRONLY
, O_WRONLY
},
105 { MTSP_O_CREAT
, O_CREAT
},
106 { MTSP_O_TRUNC
, O_TRUNC
},
107 { MTSP_O_NONBLOCK
, O_NONBLOCK
},
108 { MTSP_O_APPEND
, O_APPEND
},
109 { MTSP_O_NOFOLLOW
, O_NOFOLLOW
}
112 struct apsp_table syscall_command_table
[] = {
113 { MTSP_SYSCALL_OPEN
, __NR_open
},
114 { MTSP_SYSCALL_CLOSE
, __NR_close
},
115 { MTSP_SYSCALL_READ
, __NR_read
},
116 { MTSP_SYSCALL_WRITE
, __NR_write
},
117 { MTSP_SYSCALL_LSEEK32
, __NR_lseek
},
118 { MTSP_SYSCALL_IOCTL
, __NR_ioctl
}
121 static int sp_syscall(int num
, int arg0
, int arg1
, int arg2
, int arg3
)
123 register long int _num
__asm__("$2") = num
;
124 register long int _arg0
__asm__("$4") = arg0
;
125 register long int _arg1
__asm__("$5") = arg1
;
126 register long int _arg2
__asm__("$6") = arg2
;
127 register long int _arg3
__asm__("$7") = arg3
;
134 __asm__
__volatile__ (
136 : "=r" (_num
), "=r" (_arg3
)
137 : "r" (_num
), "r" (_arg0
), "r" (_arg1
), "r" (_arg2
), "r" (_arg3
));
141 /* $a3 is error flag */
148 static int translate_syscall_command(int cmd
)
153 for (i
= 0; i
< ARRAY_SIZE(syscall_command_table
); i
++) {
154 if ((cmd
== syscall_command_table
[i
].sp
))
155 return syscall_command_table
[i
].ap
;
161 static unsigned int translate_open_flags(int flags
)
164 unsigned int ret
= 0;
166 for (i
= 0; i
< ARRAY_SIZE(open_flags_table
); i
++) {
167 if( (flags
& open_flags_table
[i
].sp
) ) {
168 ret
|= open_flags_table
[i
].ap
;
176 static int sp_setfsuidgid(uid_t uid
, gid_t gid
)
180 new = prepare_creds();
193 * Expects a request to be on the sysio channel. Reads it. Decides whether
194 * its a linux syscall and runs it, or whatever. Puts the return code back
195 * into the request and sends the whole thing back.
197 void sp_work_handle_request(void)
199 struct mtsp_syscall sc
;
200 struct mtsp_syscall_generic generic
;
201 struct mtsp_syscall_ret ret
;
202 struct kspd_notifications
*n
;
203 unsigned long written
;
217 if (!rtlx_read(RTLX_CHANNEL_SYSIO
, &sc
, sizeof(struct mtsp_syscall
))) {
219 printk(KERN_ERR
"Expected request but nothing to read\n");
226 if (!rtlx_read(RTLX_CHANNEL_SYSIO
, &generic
, size
)) {
228 printk(KERN_ERR
"Expected request but nothing to read\n");
233 /* Run the syscall at the privilege of the user who loaded the
236 if (vpe_getuid(tclimit
)) {
237 err
= sp_setfsuidgid(vpe_getuid(tclimit
), vpe_getgid(tclimit
));
239 pr_err("Change of creds failed\n");
243 /* needs the flags argument translating from SDE kit to
245 case MTSP_SYSCALL_PIPEFREQ
:
246 ret
.retval
= cpu_khz
* 1000;
250 case MTSP_SYSCALL_GETTOD
:
251 memset(&tz
, 0, sizeof(tz
));
252 if ((ret
.retval
= sp_syscall(__NR_gettimeofday
, (int)&tv
,
253 (int)&tz
, 0, 0)) == 0)
254 ret
.retval
= tv
.tv_sec
;
257 case MTSP_SYSCALL_EXIT
:
258 list_for_each_entry(n
, &kspd_notifylist
, list
)
259 n
->kspd_sp_exit(tclimit
);
262 printk(KERN_DEBUG
"KSPD got exit syscall from SP exitcode %d\n",
266 case MTSP_SYSCALL_OPEN
:
267 generic
.arg1
= translate_open_flags(generic
.arg1
);
269 vcwd
= vpe_getcwd(tclimit
);
271 /* change to cwd of the process that loaded the SP program */
282 if ((sc
.cmd
>= __NR_Linux
) &&
283 (sc
.cmd
<= (__NR_Linux
+ __NR_Linux_syscalls
)) )
286 cmd
= translate_syscall_command(sc
.cmd
);
289 ret
.retval
= sp_syscall(cmd
, generic
.arg0
, generic
.arg1
,
290 generic
.arg2
, generic
.arg3
);
293 "KSPD: Unknown SP syscall number %d\n", sc
.cmd
);
297 if (vpe_getuid(tclimit
)) {
298 err
= sp_setfsuidgid(0, 0);
300 pr_err("restoring old creds failed\n");
305 written
= rtlx_write(RTLX_CHANNEL_SYSIO
, &ret
, sizeof(ret
));
307 if (written
< sizeof(ret
))
308 printk("KSPD: sp_work_handle_request failed to send to SP\n");
311 static void sp_cleanup(void)
313 struct files_struct
*files
= current
->files
;
320 * It is safe to dereference the fd table without RCU or
323 fdt
= files_fdtable(files
);
326 i
= j
* BITS_PER_LONG
;
327 if (i
>= fdt
->max_fds
)
329 set
= fdt
->open_fds
[j
++];
332 struct file
* file
= xchg(&fdt
->fd
[i
], NULL
);
334 filp_close(file
, files
);
341 /* Put daemon cwd back to root to avoid umount problems */
345 static int channel_open
;
347 /* the work handler */
348 static void sp_work(struct work_struct
*unused
)
351 if( rtlx_open(RTLX_CHANNEL_SYSIO
, 1) != 0) {
352 printk("KSPD: unable to open sp channel\n");
356 printk(KERN_DEBUG
"KSPD: SP channel opened\n");
359 /* wait for some data, allow it to sleep */
360 rtlx_read_poll(RTLX_CHANNEL_SYSIO
, 1);
362 /* Check we haven't been woken because we are stopping */
364 sp_work_handle_request();
368 queue_work(workqueue
, &work
);
373 static void startwork(int vpe
)
375 sp_stopping
= channel_open
= 0;
377 if (workqueue
== NULL
) {
378 if ((workqueue
= create_singlethread_workqueue("kspd")) == NULL
) {
379 printk(KERN_ERR
"unable to start kspd\n");
383 INIT_WORK(&work
, sp_work
);
386 queue_work(workqueue
, &work
);
389 static void stopwork(int vpe
)
393 printk(KERN_DEBUG
"KSPD: SP stopping\n");
396 void kspd_notify(struct kspd_notifications
*notify
)
398 list_add(¬ify
->list
, &kspd_notifylist
);
401 static struct vpe_notifications notify
;
402 static int kspd_module_init(void)
404 INIT_LIST_HEAD(&kspd_notifylist
);
406 notify
.start
= startwork
;
407 notify
.stop
= stopwork
;
408 vpe_notify(tclimit
, ¬ify
);
413 static void kspd_module_exit(void)
418 module_init(kspd_module_init
);
419 module_exit(kspd_module_exit
);
421 MODULE_DESCRIPTION("MIPS KSPD");
422 MODULE_AUTHOR("Elizabeth Oldham, MIPS Technologies, Inc.");
423 MODULE_LICENSE("GPL");