kspd.c 9.0 KB

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