kspd.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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/sched.h>
  21. #include <linux/unistd.h>
  22. #include <linux/file.h>
  23. #include <linux/fdtable.h>
  24. #include <linux/fs.h>
  25. #include <linux/syscalls.h>
  26. #include <linux/workqueue.h>
  27. #include <linux/errno.h>
  28. #include <linux/list.h>
  29. #include <asm/vpe.h>
  30. #include <asm/rtlx.h>
  31. #include <asm/kspd.h>
  32. static struct workqueue_struct *workqueue;
  33. static struct work_struct work;
  34. extern unsigned long cpu_khz;
  35. struct mtsp_syscall {
  36. int cmd;
  37. unsigned char abi;
  38. unsigned char size;
  39. };
  40. struct mtsp_syscall_ret {
  41. int retval;
  42. int errno;
  43. };
  44. struct mtsp_syscall_generic {
  45. int arg0;
  46. int arg1;
  47. int arg2;
  48. int arg3;
  49. int arg4;
  50. int arg5;
  51. int arg6;
  52. };
  53. static struct list_head kspd_notifylist;
  54. static int sp_stopping;
  55. /* these should match with those in the SDE kit */
  56. #define MTSP_SYSCALL_BASE 0
  57. #define MTSP_SYSCALL_EXIT (MTSP_SYSCALL_BASE + 0)
  58. #define MTSP_SYSCALL_OPEN (MTSP_SYSCALL_BASE + 1)
  59. #define MTSP_SYSCALL_READ (MTSP_SYSCALL_BASE + 2)
  60. #define MTSP_SYSCALL_WRITE (MTSP_SYSCALL_BASE + 3)
  61. #define MTSP_SYSCALL_CLOSE (MTSP_SYSCALL_BASE + 4)
  62. #define MTSP_SYSCALL_LSEEK32 (MTSP_SYSCALL_BASE + 5)
  63. #define MTSP_SYSCALL_ISATTY (MTSP_SYSCALL_BASE + 6)
  64. #define MTSP_SYSCALL_GETTIME (MTSP_SYSCALL_BASE + 7)
  65. #define MTSP_SYSCALL_PIPEFREQ (MTSP_SYSCALL_BASE + 8)
  66. #define MTSP_SYSCALL_GETTOD (MTSP_SYSCALL_BASE + 9)
  67. #define MTSP_SYSCALL_IOCTL (MTSP_SYSCALL_BASE + 10)
  68. #define MTSP_O_RDONLY 0x0000
  69. #define MTSP_O_WRONLY 0x0001
  70. #define MTSP_O_RDWR 0x0002
  71. #define MTSP_O_NONBLOCK 0x0004
  72. #define MTSP_O_APPEND 0x0008
  73. #define MTSP_O_SHLOCK 0x0010
  74. #define MTSP_O_EXLOCK 0x0020
  75. #define MTSP_O_ASYNC 0x0040
  76. /* XXX: check which of these is actually O_SYNC vs O_DSYNC */
  77. #define MTSP_O_FSYNC O_SYNC
  78. #define MTSP_O_NOFOLLOW 0x0100
  79. #define MTSP_O_SYNC 0x0080
  80. #define MTSP_O_CREAT 0x0200
  81. #define MTSP_O_TRUNC 0x0400
  82. #define MTSP_O_EXCL 0x0800
  83. #define MTSP_O_BINARY 0x8000
  84. extern int tclimit;
  85. struct apsp_table {
  86. int sp;
  87. int ap;
  88. };
  89. /* we might want to do the mode flags too */
  90. struct apsp_table open_flags_table[] = {
  91. { MTSP_O_RDWR, O_RDWR },
  92. { MTSP_O_WRONLY, O_WRONLY },
  93. { MTSP_O_CREAT, O_CREAT },
  94. { MTSP_O_TRUNC, O_TRUNC },
  95. { MTSP_O_NONBLOCK, O_NONBLOCK },
  96. { MTSP_O_APPEND, O_APPEND },
  97. { MTSP_O_NOFOLLOW, O_NOFOLLOW }
  98. };
  99. struct apsp_table syscall_command_table[] = {
  100. { MTSP_SYSCALL_OPEN, __NR_open },
  101. { MTSP_SYSCALL_CLOSE, __NR_close },
  102. { MTSP_SYSCALL_READ, __NR_read },
  103. { MTSP_SYSCALL_WRITE, __NR_write },
  104. { MTSP_SYSCALL_LSEEK32, __NR_lseek },
  105. { MTSP_SYSCALL_IOCTL, __NR_ioctl }
  106. };
  107. static int sp_syscall(int num, int arg0, int arg1, int arg2, int arg3)
  108. {
  109. register long int _num __asm__("$2") = num;
  110. register long int _arg0 __asm__("$4") = arg0;
  111. register long int _arg1 __asm__("$5") = arg1;
  112. register long int _arg2 __asm__("$6") = arg2;
  113. register long int _arg3 __asm__("$7") = arg3;
  114. mm_segment_t old_fs;
  115. old_fs = get_fs();
  116. set_fs(KERNEL_DS);
  117. __asm__ __volatile__ (
  118. " syscall \n"
  119. : "=r" (_num), "=r" (_arg3)
  120. : "r" (_num), "r" (_arg0), "r" (_arg1), "r" (_arg2), "r" (_arg3));
  121. set_fs(old_fs);
  122. /* $a3 is error flag */
  123. if (_arg3)
  124. return -_num;
  125. return _num;
  126. }
  127. static int translate_syscall_command(int cmd)
  128. {
  129. int i;
  130. int ret = -1;
  131. for (i = 0; i < ARRAY_SIZE(syscall_command_table); i++) {
  132. if ((cmd == syscall_command_table[i].sp))
  133. return syscall_command_table[i].ap;
  134. }
  135. return ret;
  136. }
  137. static unsigned int translate_open_flags(int flags)
  138. {
  139. int i;
  140. unsigned int ret = 0;
  141. for (i = 0; i < ARRAY_SIZE(open_flags_table); i++) {
  142. if( (flags & open_flags_table[i].sp) ) {
  143. ret |= open_flags_table[i].ap;
  144. }
  145. }
  146. return ret;
  147. }
  148. static int sp_setfsuidgid(uid_t uid, gid_t gid)
  149. {
  150. struct cred *new;
  151. new = prepare_creds();
  152. if (!new)
  153. return -ENOMEM;
  154. new->fsuid = uid;
  155. new->fsgid = gid;
  156. commit_creds(new);
  157. return 0;
  158. }
  159. /*
  160. * Expects a request to be on the sysio channel. Reads it. Decides whether
  161. * its a linux syscall and runs it, or whatever. Puts the return code back
  162. * into the request and sends the whole thing back.
  163. */
  164. void sp_work_handle_request(void)
  165. {
  166. struct mtsp_syscall sc;
  167. struct mtsp_syscall_generic generic;
  168. struct mtsp_syscall_ret ret;
  169. struct kspd_notifications *n;
  170. unsigned long written;
  171. mm_segment_t old_fs;
  172. struct timeval tv;
  173. struct timezone tz;
  174. int err, cmd;
  175. char *vcwd;
  176. int size;
  177. ret.retval = -1;
  178. old_fs = get_fs();
  179. set_fs(KERNEL_DS);
  180. if (!rtlx_read(RTLX_CHANNEL_SYSIO, &sc, sizeof(struct mtsp_syscall))) {
  181. set_fs(old_fs);
  182. printk(KERN_ERR "Expected request but nothing to read\n");
  183. return;
  184. }
  185. size = sc.size;
  186. if (size) {
  187. if (!rtlx_read(RTLX_CHANNEL_SYSIO, &generic, size)) {
  188. set_fs(old_fs);
  189. printk(KERN_ERR "Expected request but nothing to read\n");
  190. return;
  191. }
  192. }
  193. /* Run the syscall at the privilege of the user who loaded the
  194. SP program */
  195. if (vpe_getuid(tclimit)) {
  196. err = sp_setfsuidgid(vpe_getuid(tclimit), vpe_getgid(tclimit));
  197. if (!err)
  198. pr_err("Change of creds failed\n");
  199. }
  200. switch (sc.cmd) {
  201. /* needs the flags argument translating from SDE kit to
  202. linux */
  203. case MTSP_SYSCALL_PIPEFREQ:
  204. ret.retval = cpu_khz * 1000;
  205. ret.errno = 0;
  206. break;
  207. case MTSP_SYSCALL_GETTOD:
  208. memset(&tz, 0, sizeof(tz));
  209. if ((ret.retval = sp_syscall(__NR_gettimeofday, (int)&tv,
  210. (int)&tz, 0, 0)) == 0)
  211. ret.retval = tv.tv_sec;
  212. break;
  213. case MTSP_SYSCALL_EXIT:
  214. list_for_each_entry(n, &kspd_notifylist, list)
  215. n->kspd_sp_exit(tclimit);
  216. sp_stopping = 1;
  217. printk(KERN_DEBUG "KSPD got exit syscall from SP exitcode %d\n",
  218. generic.arg0);
  219. break;
  220. case MTSP_SYSCALL_OPEN:
  221. generic.arg1 = translate_open_flags(generic.arg1);
  222. vcwd = vpe_getcwd(tclimit);
  223. /* change to cwd of the process that loaded the SP program */
  224. old_fs = get_fs();
  225. set_fs(KERNEL_DS);
  226. sys_chdir(vcwd);
  227. set_fs(old_fs);
  228. sc.cmd = __NR_open;
  229. /* fall through */
  230. default:
  231. if ((sc.cmd >= __NR_Linux) &&
  232. (sc.cmd <= (__NR_Linux + __NR_Linux_syscalls)) )
  233. cmd = sc.cmd;
  234. else
  235. cmd = translate_syscall_command(sc.cmd);
  236. if (cmd >= 0) {
  237. ret.retval = sp_syscall(cmd, generic.arg0, generic.arg1,
  238. generic.arg2, generic.arg3);
  239. } else
  240. printk(KERN_WARNING
  241. "KSPD: Unknown SP syscall number %d\n", sc.cmd);
  242. break;
  243. } /* switch */
  244. if (vpe_getuid(tclimit)) {
  245. err = sp_setfsuidgid(0, 0);
  246. if (!err)
  247. pr_err("restoring old creds failed\n");
  248. }
  249. old_fs = get_fs();
  250. set_fs(KERNEL_DS);
  251. written = rtlx_write(RTLX_CHANNEL_SYSIO, &ret, sizeof(ret));
  252. set_fs(old_fs);
  253. if (written < sizeof(ret))
  254. printk("KSPD: sp_work_handle_request failed to send to SP\n");
  255. }
  256. static void sp_cleanup(void)
  257. {
  258. struct files_struct *files = current->files;
  259. int i, j;
  260. struct fdtable *fdt;
  261. j = 0;
  262. /*
  263. * It is safe to dereference the fd table without RCU or
  264. * ->file_lock
  265. */
  266. fdt = files_fdtable(files);
  267. for (;;) {
  268. unsigned long set;
  269. i = j * __NFDBITS;
  270. if (i >= fdt->max_fds)
  271. break;
  272. set = fdt->open_fds->fds_bits[j++];
  273. while (set) {
  274. if (set & 1) {
  275. struct file * file = xchg(&fdt->fd[i], NULL);
  276. if (file)
  277. filp_close(file, files);
  278. }
  279. i++;
  280. set >>= 1;
  281. }
  282. }
  283. /* Put daemon cwd back to root to avoid umount problems */
  284. sys_chdir("/");
  285. }
  286. static int channel_open;
  287. /* the work handler */
  288. static void sp_work(struct work_struct *unused)
  289. {
  290. if (!channel_open) {
  291. if( rtlx_open(RTLX_CHANNEL_SYSIO, 1) != 0) {
  292. printk("KSPD: unable to open sp channel\n");
  293. sp_stopping = 1;
  294. } else {
  295. channel_open++;
  296. printk(KERN_DEBUG "KSPD: SP channel opened\n");
  297. }
  298. } else {
  299. /* wait for some data, allow it to sleep */
  300. rtlx_read_poll(RTLX_CHANNEL_SYSIO, 1);
  301. /* Check we haven't been woken because we are stopping */
  302. if (!sp_stopping)
  303. sp_work_handle_request();
  304. }
  305. if (!sp_stopping)
  306. queue_work(workqueue, &work);
  307. else
  308. sp_cleanup();
  309. }
  310. static void startwork(int vpe)
  311. {
  312. sp_stopping = channel_open = 0;
  313. if (workqueue == NULL) {
  314. if ((workqueue = create_singlethread_workqueue("kspd")) == NULL) {
  315. printk(KERN_ERR "unable to start kspd\n");
  316. return;
  317. }
  318. INIT_WORK(&work, sp_work);
  319. }
  320. queue_work(workqueue, &work);
  321. }
  322. static void stopwork(int vpe)
  323. {
  324. sp_stopping = 1;
  325. printk(KERN_DEBUG "KSPD: SP stopping\n");
  326. }
  327. void kspd_notify(struct kspd_notifications *notify)
  328. {
  329. list_add(&notify->list, &kspd_notifylist);
  330. }
  331. static struct vpe_notifications notify;
  332. static int kspd_module_init(void)
  333. {
  334. INIT_LIST_HEAD(&kspd_notifylist);
  335. notify.start = startwork;
  336. notify.stop = stopwork;
  337. vpe_notify(tclimit, &notify);
  338. return 0;
  339. }
  340. static void kspd_module_exit(void)
  341. {
  342. }
  343. module_init(kspd_module_init);
  344. module_exit(kspd_module_exit);
  345. MODULE_DESCRIPTION("MIPS KSPD");
  346. MODULE_AUTHOR("Elizabeth Oldham, MIPS Technologies, Inc.");
  347. MODULE_LICENSE("GPL");