kspd.c 8.9 KB

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