Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 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> |
Ralf Baechle | 7f5a771 | 2007-04-25 15:08:57 +0100 | [diff] [blame] | 20 | #include <linux/sched.h> |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 21 | #include <linux/unistd.h> |
| 22 | #include <linux/file.h> |
| 23 | #include <linux/fs.h> |
| 24 | #include <linux/syscalls.h> |
| 25 | #include <linux/workqueue.h> |
| 26 | #include <linux/errno.h> |
| 27 | #include <linux/list.h> |
| 28 | |
| 29 | #include <asm/vpe.h> |
| 30 | #include <asm/rtlx.h> |
| 31 | #include <asm/kspd.h> |
| 32 | |
| 33 | static struct workqueue_struct *workqueue = NULL; |
| 34 | static struct work_struct work; |
| 35 | |
| 36 | extern unsigned long cpu_khz; |
| 37 | |
| 38 | struct mtsp_syscall { |
| 39 | int cmd; |
| 40 | unsigned char abi; |
| 41 | unsigned char size; |
| 42 | }; |
| 43 | |
| 44 | struct mtsp_syscall_ret { |
| 45 | int retval; |
| 46 | int errno; |
| 47 | }; |
| 48 | |
| 49 | struct mtsp_syscall_generic { |
| 50 | int arg0; |
| 51 | int arg1; |
| 52 | int arg2; |
| 53 | int arg3; |
| 54 | int arg4; |
| 55 | int arg5; |
| 56 | int arg6; |
| 57 | }; |
| 58 | |
| 59 | static struct list_head kspd_notifylist; |
| 60 | static int sp_stopping = 0; |
| 61 | |
| 62 | /* these should match with those in the SDE kit */ |
| 63 | #define MTSP_SYSCALL_BASE 0 |
| 64 | #define MTSP_SYSCALL_EXIT (MTSP_SYSCALL_BASE + 0) |
| 65 | #define MTSP_SYSCALL_OPEN (MTSP_SYSCALL_BASE + 1) |
| 66 | #define MTSP_SYSCALL_READ (MTSP_SYSCALL_BASE + 2) |
| 67 | #define MTSP_SYSCALL_WRITE (MTSP_SYSCALL_BASE + 3) |
| 68 | #define MTSP_SYSCALL_CLOSE (MTSP_SYSCALL_BASE + 4) |
| 69 | #define MTSP_SYSCALL_LSEEK32 (MTSP_SYSCALL_BASE + 5) |
| 70 | #define MTSP_SYSCALL_ISATTY (MTSP_SYSCALL_BASE + 6) |
| 71 | #define MTSP_SYSCALL_GETTIME (MTSP_SYSCALL_BASE + 7) |
| 72 | #define MTSP_SYSCALL_PIPEFREQ (MTSP_SYSCALL_BASE + 8) |
| 73 | #define MTSP_SYSCALL_GETTOD (MTSP_SYSCALL_BASE + 9) |
Ralf Baechle | 0e6ee85 | 2007-03-13 15:10:50 +0000 | [diff] [blame] | 74 | #define MTSP_SYSCALL_IOCTL (MTSP_SYSCALL_BASE + 10) |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 75 | |
| 76 | #define MTSP_O_RDONLY 0x0000 |
| 77 | #define MTSP_O_WRONLY 0x0001 |
| 78 | #define MTSP_O_RDWR 0x0002 |
| 79 | #define MTSP_O_NONBLOCK 0x0004 |
| 80 | #define MTSP_O_APPEND 0x0008 |
| 81 | #define MTSP_O_SHLOCK 0x0010 |
| 82 | #define MTSP_O_EXLOCK 0x0020 |
| 83 | #define MTSP_O_ASYNC 0x0040 |
| 84 | #define MTSP_O_FSYNC O_SYNC |
| 85 | #define MTSP_O_NOFOLLOW 0x0100 |
| 86 | #define MTSP_O_SYNC 0x0080 |
| 87 | #define MTSP_O_CREAT 0x0200 |
| 88 | #define MTSP_O_TRUNC 0x0400 |
| 89 | #define MTSP_O_EXCL 0x0800 |
| 90 | #define MTSP_O_BINARY 0x8000 |
| 91 | |
Ralf Baechle | 07cc0c9 | 2007-07-27 19:31:10 +0100 | [diff] [blame] | 92 | extern int tclimit; |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 93 | |
| 94 | struct apsp_table { |
| 95 | int sp; |
| 96 | int ap; |
| 97 | }; |
| 98 | |
| 99 | /* we might want to do the mode flags too */ |
| 100 | struct apsp_table open_flags_table[] = { |
| 101 | { MTSP_O_RDWR, O_RDWR }, |
| 102 | { MTSP_O_WRONLY, O_WRONLY }, |
| 103 | { MTSP_O_CREAT, O_CREAT }, |
| 104 | { MTSP_O_TRUNC, O_TRUNC }, |
| 105 | { MTSP_O_NONBLOCK, O_NONBLOCK }, |
| 106 | { MTSP_O_APPEND, O_APPEND }, |
| 107 | { MTSP_O_NOFOLLOW, O_NOFOLLOW } |
| 108 | }; |
| 109 | |
| 110 | struct apsp_table syscall_command_table[] = { |
| 111 | { MTSP_SYSCALL_OPEN, __NR_open }, |
| 112 | { MTSP_SYSCALL_CLOSE, __NR_close }, |
| 113 | { MTSP_SYSCALL_READ, __NR_read }, |
| 114 | { MTSP_SYSCALL_WRITE, __NR_write }, |
Ralf Baechle | 0e6ee85 | 2007-03-13 15:10:50 +0000 | [diff] [blame] | 115 | { MTSP_SYSCALL_LSEEK32, __NR_lseek }, |
| 116 | { MTSP_SYSCALL_IOCTL, __NR_ioctl } |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | static int sp_syscall(int num, int arg0, int arg1, int arg2, int arg3) |
| 120 | { |
Ralf Baechle | 49a89ef | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 121 | register long int _num __asm__("$2") = num; |
| 122 | register long int _arg0 __asm__("$4") = arg0; |
| 123 | register long int _arg1 __asm__("$5") = arg1; |
| 124 | register long int _arg2 __asm__("$6") = arg2; |
| 125 | register long int _arg3 __asm__("$7") = arg3; |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 126 | |
| 127 | mm_segment_t old_fs; |
| 128 | |
| 129 | old_fs = get_fs(); |
| 130 | set_fs(KERNEL_DS); |
| 131 | |
| 132 | __asm__ __volatile__ ( |
| 133 | " syscall \n" |
| 134 | : "=r" (_num), "=r" (_arg3) |
| 135 | : "r" (_num), "r" (_arg0), "r" (_arg1), "r" (_arg2), "r" (_arg3)); |
| 136 | |
| 137 | set_fs(old_fs); |
| 138 | |
| 139 | /* $a3 is error flag */ |
| 140 | if (_arg3) |
| 141 | return -_num; |
| 142 | |
| 143 | return _num; |
| 144 | } |
| 145 | |
| 146 | static int translate_syscall_command(int cmd) |
| 147 | { |
| 148 | int i; |
| 149 | int ret = -1; |
| 150 | |
| 151 | for (i = 0; i < ARRAY_SIZE(syscall_command_table); i++) { |
| 152 | if ((cmd == syscall_command_table[i].sp)) |
| 153 | return syscall_command_table[i].ap; |
| 154 | } |
| 155 | |
| 156 | return ret; |
| 157 | } |
| 158 | |
| 159 | static unsigned int translate_open_flags(int flags) |
| 160 | { |
| 161 | int i; |
| 162 | unsigned int ret = 0; |
| 163 | |
Alejandro Martinez Ruiz | 2b22c03 | 2007-10-22 21:36:44 +0200 | [diff] [blame] | 164 | for (i = 0; i < ARRAY_SIZE(open_flags_table); i++) { |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 165 | if( (flags & open_flags_table[i].sp) ) { |
| 166 | ret |= open_flags_table[i].ap; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | return ret; |
| 171 | } |
| 172 | |
| 173 | |
| 174 | static 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 | */ |
| 188 | void 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; |
Ralf Baechle | 46230aa | 2007-03-16 12:16:27 +0000 | [diff] [blame] | 194 | unsigned long written; |
| 195 | mm_segment_t old_fs; |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 196 | struct timeval tv; |
| 197 | struct timezone tz; |
| 198 | int cmd; |
| 199 | |
| 200 | char *vcwd; |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 201 | int size; |
| 202 | |
| 203 | ret.retval = -1; |
| 204 | |
Ralf Baechle | 46230aa | 2007-03-16 12:16:27 +0000 | [diff] [blame] | 205 | old_fs = get_fs(); |
| 206 | set_fs(KERNEL_DS); |
| 207 | |
| 208 | if (!rtlx_read(RTLX_CHANNEL_SYSIO, &sc, sizeof(struct mtsp_syscall))) { |
| 209 | set_fs(old_fs); |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 210 | printk(KERN_ERR "Expected request but nothing to read\n"); |
| 211 | return; |
| 212 | } |
| 213 | |
| 214 | size = sc.size; |
| 215 | |
| 216 | if (size) { |
Ralf Baechle | 46230aa | 2007-03-16 12:16:27 +0000 | [diff] [blame] | 217 | if (!rtlx_read(RTLX_CHANNEL_SYSIO, &generic, size)) { |
| 218 | set_fs(old_fs); |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 219 | printk(KERN_ERR "Expected request but nothing to read\n"); |
| 220 | return; |
| 221 | } |
| 222 | } |
| 223 | |
Joe Perches | 603e82e | 2008-02-03 16:54:53 +0200 | [diff] [blame] | 224 | /* Run the syscall at the privilege of the user who loaded the |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 225 | SP program */ |
| 226 | |
Ralf Baechle | 07cc0c9 | 2007-07-27 19:31:10 +0100 | [diff] [blame] | 227 | if (vpe_getuid(tclimit)) |
| 228 | sp_setfsuidgid(vpe_getuid(tclimit), vpe_getgid(tclimit)); |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 229 | |
| 230 | switch (sc.cmd) { |
| 231 | /* needs the flags argument translating from SDE kit to |
| 232 | linux */ |
| 233 | case MTSP_SYSCALL_PIPEFREQ: |
| 234 | ret.retval = cpu_khz * 1000; |
| 235 | ret.errno = 0; |
| 236 | break; |
| 237 | |
| 238 | case MTSP_SYSCALL_GETTOD: |
| 239 | memset(&tz, 0, sizeof(tz)); |
| 240 | if ((ret.retval = sp_syscall(__NR_gettimeofday, (int)&tv, |
Ralf Baechle | 21a151d | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 241 | (int)&tz, 0, 0)) == 0) |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 242 | ret.retval = tv.tv_sec; |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 243 | break; |
| 244 | |
| 245 | case MTSP_SYSCALL_EXIT: |
| 246 | list_for_each_entry(n, &kspd_notifylist, list) |
Ralf Baechle | 07cc0c9 | 2007-07-27 19:31:10 +0100 | [diff] [blame] | 247 | n->kspd_sp_exit(tclimit); |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 248 | sp_stopping = 1; |
| 249 | |
| 250 | printk(KERN_DEBUG "KSPD got exit syscall from SP exitcode %d\n", |
| 251 | generic.arg0); |
| 252 | break; |
| 253 | |
| 254 | case MTSP_SYSCALL_OPEN: |
| 255 | generic.arg1 = translate_open_flags(generic.arg1); |
| 256 | |
Ralf Baechle | 07cc0c9 | 2007-07-27 19:31:10 +0100 | [diff] [blame] | 257 | vcwd = vpe_getcwd(tclimit); |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 258 | |
| 259 | /* change to the cwd of the process that loaded the SP program */ |
| 260 | old_fs = get_fs(); |
| 261 | set_fs(KERNEL_DS); |
| 262 | sys_chdir(vcwd); |
| 263 | set_fs(old_fs); |
| 264 | |
| 265 | sc.cmd = __NR_open; |
| 266 | |
| 267 | /* fall through */ |
| 268 | |
| 269 | default: |
| 270 | if ((sc.cmd >= __NR_Linux) && |
| 271 | (sc.cmd <= (__NR_Linux + __NR_Linux_syscalls)) ) |
| 272 | cmd = sc.cmd; |
| 273 | else |
| 274 | cmd = translate_syscall_command(sc.cmd); |
| 275 | |
| 276 | if (cmd >= 0) { |
| 277 | ret.retval = sp_syscall(cmd, generic.arg0, generic.arg1, |
| 278 | generic.arg2, generic.arg3); |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 279 | } else |
| 280 | printk(KERN_WARNING |
| 281 | "KSPD: Unknown SP syscall number %d\n", sc.cmd); |
| 282 | break; |
| 283 | } /* switch */ |
| 284 | |
Ralf Baechle | 07cc0c9 | 2007-07-27 19:31:10 +0100 | [diff] [blame] | 285 | if (vpe_getuid(tclimit)) |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 286 | sp_setfsuidgid( 0, 0); |
| 287 | |
Ralf Baechle | 46230aa | 2007-03-16 12:16:27 +0000 | [diff] [blame] | 288 | old_fs = get_fs(); |
| 289 | set_fs(KERNEL_DS); |
| 290 | written = rtlx_write(RTLX_CHANNEL_SYSIO, &ret, sizeof(ret)); |
| 291 | set_fs(old_fs); |
| 292 | if (written < sizeof(ret)) |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 293 | printk("KSPD: sp_work_handle_request failed to send to SP\n"); |
| 294 | } |
| 295 | |
| 296 | static void sp_cleanup(void) |
| 297 | { |
| 298 | struct files_struct *files = current->files; |
| 299 | int i, j; |
| 300 | struct fdtable *fdt; |
| 301 | |
| 302 | j = 0; |
| 303 | |
| 304 | /* |
| 305 | * It is safe to dereference the fd table without RCU or |
| 306 | * ->file_lock |
| 307 | */ |
| 308 | fdt = files_fdtable(files); |
| 309 | for (;;) { |
| 310 | unsigned long set; |
| 311 | i = j * __NFDBITS; |
Vadim Lobanov | bbea9f6 | 2006-12-10 02:21:12 -0800 | [diff] [blame] | 312 | if (i >= fdt->max_fds) |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 313 | break; |
| 314 | set = fdt->open_fds->fds_bits[j++]; |
| 315 | while (set) { |
| 316 | if (set & 1) { |
| 317 | struct file * file = xchg(&fdt->fd[i], NULL); |
| 318 | if (file) |
| 319 | filp_close(file, files); |
| 320 | } |
| 321 | i++; |
| 322 | set >>= 1; |
| 323 | } |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | static int channel_open = 0; |
| 328 | |
| 329 | /* the work handler */ |
David Howells | 6d5aefb | 2006-12-05 19:36:26 +0000 | [diff] [blame] | 330 | static void sp_work(struct work_struct *unused) |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 331 | { |
| 332 | if (!channel_open) { |
| 333 | if( rtlx_open(RTLX_CHANNEL_SYSIO, 1) != 0) { |
| 334 | printk("KSPD: unable to open sp channel\n"); |
| 335 | sp_stopping = 1; |
| 336 | } else { |
| 337 | channel_open++; |
| 338 | printk(KERN_DEBUG "KSPD: SP channel opened\n"); |
| 339 | } |
| 340 | } else { |
| 341 | /* wait for some data, allow it to sleep */ |
| 342 | rtlx_read_poll(RTLX_CHANNEL_SYSIO, 1); |
| 343 | |
| 344 | /* Check we haven't been woken because we are stopping */ |
| 345 | if (!sp_stopping) |
| 346 | sp_work_handle_request(); |
| 347 | } |
| 348 | |
| 349 | if (!sp_stopping) |
| 350 | queue_work(workqueue, &work); |
| 351 | else |
| 352 | sp_cleanup(); |
| 353 | } |
| 354 | |
| 355 | static void startwork(int vpe) |
| 356 | { |
| 357 | sp_stopping = channel_open = 0; |
| 358 | |
| 359 | if (workqueue == NULL) { |
| 360 | if ((workqueue = create_singlethread_workqueue("kspd")) == NULL) { |
| 361 | printk(KERN_ERR "unable to start kspd\n"); |
| 362 | return; |
| 363 | } |
| 364 | |
David Howells | 6d5aefb | 2006-12-05 19:36:26 +0000 | [diff] [blame] | 365 | INIT_WORK(&work, sp_work); |
Ralf Baechle | 07cc0c9 | 2007-07-27 19:31:10 +0100 | [diff] [blame] | 366 | } |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 367 | |
Ralf Baechle | 07cc0c9 | 2007-07-27 19:31:10 +0100 | [diff] [blame] | 368 | queue_work(workqueue, &work); |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | static void stopwork(int vpe) |
| 372 | { |
| 373 | sp_stopping = 1; |
| 374 | |
| 375 | printk(KERN_DEBUG "KSPD: SP stopping\n"); |
| 376 | } |
| 377 | |
| 378 | void kspd_notify(struct kspd_notifications *notify) |
| 379 | { |
| 380 | list_add(¬ify->list, &kspd_notifylist); |
| 381 | } |
| 382 | |
| 383 | static struct vpe_notifications notify; |
| 384 | static int kspd_module_init(void) |
| 385 | { |
| 386 | INIT_LIST_HEAD(&kspd_notifylist); |
| 387 | |
| 388 | notify.start = startwork; |
| 389 | notify.stop = stopwork; |
Ralf Baechle | 07cc0c9 | 2007-07-27 19:31:10 +0100 | [diff] [blame] | 390 | vpe_notify(tclimit, ¬ify); |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 391 | |
| 392 | return 0; |
| 393 | } |
| 394 | |
| 395 | static void kspd_module_exit(void) |
| 396 | { |
| 397 | |
| 398 | } |
| 399 | |
| 400 | module_init(kspd_module_init); |
| 401 | module_exit(kspd_module_exit); |
| 402 | |
| 403 | MODULE_DESCRIPTION("MIPS KSPD"); |
| 404 | MODULE_AUTHOR("Elizabeth Oldham, MIPS Technologies, Inc."); |
| 405 | MODULE_LICENSE("GPL"); |