Blame view

arch/mips/kernel/kspd.c 9.28 KB
2600990e6   Ralf Baechle   [MIPS] kpsd and o...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  /*
   * Copyright (C) 2005 MIPS Technologies, Inc.  All rights reserved.
   *
   *  This program is free software; you can distribute it and/or modify it
   *  under the terms of the GNU General Public License (Version 2) as
   *  published by the Free Software Foundation.
   *
   *  This program is distributed in the hope it will be useful, but WITHOUT
   *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   *  for more details.
   *
   *  You should have received a copy of the GNU General Public License along
   *  with this program; if not, write to the Free Software Foundation, Inc.,
   *  59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
   *
   */
  #include <linux/kernel.h>
  #include <linux/module.h>
7f5a7716d   Ralf Baechle   [MIPS] Fix AP/SP ...
20
  #include <linux/sched.h>
2600990e6   Ralf Baechle   [MIPS] kpsd and o...
21
22
  #include <linux/unistd.h>
  #include <linux/file.h>
9f3acc314   Al Viro   [PATCH] split lin...
23
  #include <linux/fdtable.h>
2600990e6   Ralf Baechle   [MIPS] kpsd and o...
24
25
26
27
28
29
30
31
32
  #include <linux/fs.h>
  #include <linux/syscalls.h>
  #include <linux/workqueue.h>
  #include <linux/errno.h>
  #include <linux/list.h>
  
  #include <asm/vpe.h>
  #include <asm/rtlx.h>
  #include <asm/kspd.h>
982f6ffee   Ralf Baechle   MIPS: Remove usel...
33
  static struct workqueue_struct *workqueue;
2600990e6   Ralf Baechle   [MIPS] kpsd and o...
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
  static struct work_struct work;
  
  extern unsigned long cpu_khz;
  
  struct mtsp_syscall {
  	int cmd;
  	unsigned char abi;
  	unsigned char size;
  };
  
  struct mtsp_syscall_ret {
  	int retval;
  	int errno;
  };
  
  struct mtsp_syscall_generic {
  	int arg0;
  	int arg1;
  	int arg2;
  	int arg3;
  	int arg4;
  	int arg5;
  	int arg6;
  };
  
  static struct list_head kspd_notifylist;
982f6ffee   Ralf Baechle   MIPS: Remove usel...
60
  static int sp_stopping;
2600990e6   Ralf Baechle   [MIPS] kpsd and o...
61
62
63
64
65
66
67
68
69
70
71
72
73
  
  /* these should match with those in the SDE kit */
  #define MTSP_SYSCALL_BASE	0
  #define MTSP_SYSCALL_EXIT	(MTSP_SYSCALL_BASE + 0)
  #define MTSP_SYSCALL_OPEN	(MTSP_SYSCALL_BASE + 1)
  #define MTSP_SYSCALL_READ	(MTSP_SYSCALL_BASE + 2)
  #define MTSP_SYSCALL_WRITE	(MTSP_SYSCALL_BASE + 3)
  #define MTSP_SYSCALL_CLOSE	(MTSP_SYSCALL_BASE + 4)
  #define MTSP_SYSCALL_LSEEK32	(MTSP_SYSCALL_BASE + 5)
  #define MTSP_SYSCALL_ISATTY	(MTSP_SYSCALL_BASE + 6)
  #define MTSP_SYSCALL_GETTIME	(MTSP_SYSCALL_BASE + 7)
  #define MTSP_SYSCALL_PIPEFREQ	(MTSP_SYSCALL_BASE + 8)
  #define MTSP_SYSCALL_GETTOD	(MTSP_SYSCALL_BASE + 9)
0e6ee854e   Ralf Baechle   [MIPS] kspd: ioct...
74
  #define MTSP_SYSCALL_IOCTL     (MTSP_SYSCALL_BASE + 10)
2600990e6   Ralf Baechle   [MIPS] kpsd and o...
75
76
77
78
79
80
81
82
83
  
  #define MTSP_O_RDONLY		0x0000
  #define MTSP_O_WRONLY		0x0001
  #define MTSP_O_RDWR		0x0002
  #define MTSP_O_NONBLOCK		0x0004
  #define MTSP_O_APPEND		0x0008
  #define MTSP_O_SHLOCK		0x0010
  #define MTSP_O_EXLOCK		0x0020
  #define MTSP_O_ASYNC		0x0040
6b2f3d1f7   Christoph Hellwig   vfs: Implement pr...
84
  /* XXX: check which of these is actually O_SYNC vs O_DSYNC */
2600990e6   Ralf Baechle   [MIPS] kpsd and o...
85
86
87
88
89
90
91
  #define MTSP_O_FSYNC		O_SYNC
  #define MTSP_O_NOFOLLOW		0x0100
  #define MTSP_O_SYNC		0x0080
  #define MTSP_O_CREAT		0x0200
  #define MTSP_O_TRUNC		0x0400
  #define MTSP_O_EXCL		0x0800
  #define MTSP_O_BINARY		0x8000
07cc0c9e6   Ralf Baechle   [MIPS] MT: Enable...
92
  extern int tclimit;
2600990e6   Ralf Baechle   [MIPS] kpsd and o...
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
  
  struct apsp_table  {
  	int sp;
  	int ap;
  };
  
  /* we might want to do the mode flags too */
  struct apsp_table open_flags_table[] = {
  	{ MTSP_O_RDWR, O_RDWR },
  	{ MTSP_O_WRONLY, O_WRONLY },
  	{ MTSP_O_CREAT, O_CREAT },
  	{ MTSP_O_TRUNC, O_TRUNC },
  	{ MTSP_O_NONBLOCK, O_NONBLOCK },
  	{ MTSP_O_APPEND, O_APPEND },
  	{ MTSP_O_NOFOLLOW, O_NOFOLLOW }
  };
  
  struct apsp_table syscall_command_table[] = {
  	{ MTSP_SYSCALL_OPEN, __NR_open },
  	{ MTSP_SYSCALL_CLOSE, __NR_close },
  	{ MTSP_SYSCALL_READ, __NR_read },
  	{ MTSP_SYSCALL_WRITE, __NR_write },
0e6ee854e   Ralf Baechle   [MIPS] kspd: ioct...
115
116
  	{ MTSP_SYSCALL_LSEEK32, __NR_lseek },
  	{ MTSP_SYSCALL_IOCTL, __NR_ioctl }
2600990e6   Ralf Baechle   [MIPS] kpsd and o...
117
118
119
120
  };
  
  static int sp_syscall(int num, int arg0, int arg1, int arg2, int arg3)
  {
49a89efbb   Ralf Baechle   [MIPS] Fix "no sp...
121
122
123
124
125
  	register long int _num  __asm__("$2") = num;
  	register long int _arg0  __asm__("$4") = arg0;
  	register long int _arg1  __asm__("$5") = arg1;
  	register long int _arg2  __asm__("$6") = arg2;
  	register long int _arg3  __asm__("$7") = arg3;
2600990e6   Ralf Baechle   [MIPS] kpsd and o...
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
  
  	mm_segment_t old_fs;
  
  	old_fs = get_fs();
   	set_fs(KERNEL_DS);
  
    	__asm__ __volatile__ (
   	"	syscall					
  "
   	: "=r" (_num), "=r" (_arg3)
   	: "r" (_num), "r" (_arg0), "r" (_arg1), "r" (_arg2), "r" (_arg3));
  
  	set_fs(old_fs);
  
  	/* $a3 is error flag */
  	if (_arg3)
  		return -_num;
  
  	return _num;
  }
  
  static int translate_syscall_command(int cmd)
  {
  	int i;
  	int ret = -1;
  
  	for (i = 0; i < ARRAY_SIZE(syscall_command_table); i++) {
  		if ((cmd == syscall_command_table[i].sp))
  			return syscall_command_table[i].ap;
  	}
  
  	return ret;
  }
  
  static unsigned int translate_open_flags(int flags)
  {
  	int i;
  	unsigned int ret = 0;
2b22c034d   Alejandro Martinez Ruiz   [MIPS] Converting...
164
  	for (i = 0; i < ARRAY_SIZE(open_flags_table); i++) {
2600990e6   Ralf Baechle   [MIPS] kpsd and o...
165
166
167
168
169
170
171
  		if( (flags & open_flags_table[i].sp) ) {
  			ret |= open_flags_table[i].ap;
  		}
  	}
  
  	return ret;
  }
c0648e02d   Ralf Baechle   MIPS: VPE: Fix bu...
172
  static int sp_setfsuidgid(uid_t uid, gid_t gid)
2600990e6   Ralf Baechle   [MIPS] kpsd and o...
173
  {
c0648e02d   Ralf Baechle   MIPS: VPE: Fix bu...
174
  	struct cred *new;
2600990e6   Ralf Baechle   [MIPS] kpsd and o...
175

c0648e02d   Ralf Baechle   MIPS: VPE: Fix bu...
176
177
178
179
180
181
182
183
184
185
  	new = prepare_creds();
  	if (!new)
  		return -ENOMEM;
  
  	new->fsuid = uid;
  	new->fsgid = gid;
  
  	commit_creds(new);
  
  	return 0;
2600990e6   Ralf Baechle   [MIPS] kpsd and o...
186
187
188
189
190
191
192
193
194
195
196
197
198
  }
  
  /*
   * Expects a request to be on the sysio channel. Reads it.  Decides whether
   * its a linux syscall and runs it, or whatever.  Puts the return code back
   * into the request and sends the whole thing back.
   */
  void sp_work_handle_request(void)
  {
  	struct mtsp_syscall sc;
  	struct mtsp_syscall_generic generic;
  	struct mtsp_syscall_ret ret;
  	struct kspd_notifications *n;
46230aa6e   Ralf Baechle   [MIPS] RTLX: Hand...
199
200
  	unsigned long written;
  	mm_segment_t old_fs;
2600990e6   Ralf Baechle   [MIPS] kpsd and o...
201
202
  	struct timeval tv;
  	struct timezone tz;
c0648e02d   Ralf Baechle   MIPS: VPE: Fix bu...
203
  	int err, cmd;
2600990e6   Ralf Baechle   [MIPS] kpsd and o...
204
205
  
  	char *vcwd;
2600990e6   Ralf Baechle   [MIPS] kpsd and o...
206
207
208
  	int size;
  
  	ret.retval = -1;
46230aa6e   Ralf Baechle   [MIPS] RTLX: Hand...
209
210
211
212
213
  	old_fs = get_fs();
  	set_fs(KERNEL_DS);
  
  	if (!rtlx_read(RTLX_CHANNEL_SYSIO, &sc, sizeof(struct mtsp_syscall))) {
  		set_fs(old_fs);
2600990e6   Ralf Baechle   [MIPS] kpsd and o...
214
215
216
217
218
219
220
221
  		printk(KERN_ERR "Expected request but nothing to read
  ");
  		return;
  	}
  
  	size = sc.size;
  
  	if (size) {
46230aa6e   Ralf Baechle   [MIPS] RTLX: Hand...
222
223
  		if (!rtlx_read(RTLX_CHANNEL_SYSIO, &generic, size)) {
  			set_fs(old_fs);
2600990e6   Ralf Baechle   [MIPS] kpsd and o...
224
225
226
227
228
  			printk(KERN_ERR "Expected request but nothing to read
  ");
  			return;
  		}
  	}
603e82edf   Joe Perches   arch/mips/: Spell...
229
  	/* Run the syscall at the privilege of the user who loaded the
2600990e6   Ralf Baechle   [MIPS] kpsd and o...
230
  	   SP program */
c0648e02d   Ralf Baechle   MIPS: VPE: Fix bu...
231
232
233
234
235
236
  	if (vpe_getuid(tclimit)) {
  		err = sp_setfsuidgid(vpe_getuid(tclimit), vpe_getgid(tclimit));
  		if (!err)
  			pr_err("Change of creds failed
  ");
  	}
2600990e6   Ralf Baechle   [MIPS] kpsd and o...
237
238
239
240
241
242
243
244
245
246
247
248
  
  	switch (sc.cmd) {
  	/* needs the flags argument translating from SDE kit to
  	   linux */
   	case MTSP_SYSCALL_PIPEFREQ:
   		ret.retval = cpu_khz * 1000;
   		ret.errno = 0;
   		break;
  
   	case MTSP_SYSCALL_GETTOD:
   		memset(&tz, 0, sizeof(tz));
   		if ((ret.retval = sp_syscall(__NR_gettimeofday, (int)&tv,
21a151d8c   Ralf Baechle   [MIPS] checkfiles...
249
  					     (int)&tz, 0, 0)) == 0)
26deda5ce   Julia Lawall   MIPS: kspd: Adjus...
250
  			ret.retval = tv.tv_sec;
2600990e6   Ralf Baechle   [MIPS] kpsd and o...
251
252
253
254
  		break;
  
   	case MTSP_SYSCALL_EXIT:
  		list_for_each_entry(n, &kspd_notifylist, list)
07cc0c9e6   Ralf Baechle   [MIPS] MT: Enable...
255
  			n->kspd_sp_exit(tclimit);
2600990e6   Ralf Baechle   [MIPS] kpsd and o...
256
257
258
259
260
261
262
263
264
  		sp_stopping = 1;
  
  		printk(KERN_DEBUG "KSPD got exit syscall from SP exitcode %d
  ",
  		       generic.arg0);
   		break;
  
   	case MTSP_SYSCALL_OPEN:
   		generic.arg1 = translate_open_flags(generic.arg1);
07cc0c9e6   Ralf Baechle   [MIPS] MT: Enable...
265
  		vcwd = vpe_getcwd(tclimit);
2600990e6   Ralf Baechle   [MIPS] kpsd and o...
266

1928cc84a   Kevin D. Kissell   [MIPS] MT: Functi...
267
  		/* change to cwd of the process that loaded the SP program */
2600990e6   Ralf Baechle   [MIPS] kpsd and o...
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
  		old_fs = get_fs();
  		set_fs(KERNEL_DS);
  		sys_chdir(vcwd);
  		set_fs(old_fs);
  
   		sc.cmd = __NR_open;
  
  		/* fall through */
  
    	default:
   		if ((sc.cmd >= __NR_Linux) &&
  		    (sc.cmd <= (__NR_Linux +  __NR_Linux_syscalls)) )
  			cmd = sc.cmd;
  		else
  			cmd = translate_syscall_command(sc.cmd);
  
  		if (cmd >= 0) {
  			ret.retval = sp_syscall(cmd, generic.arg0, generic.arg1,
  			                        generic.arg2, generic.arg3);
2600990e6   Ralf Baechle   [MIPS] kpsd and o...
287
288
289
290
291
292
  		} else
   			printk(KERN_WARNING
  			       "KSPD: Unknown SP syscall number %d
  ", sc.cmd);
  		break;
   	} /* switch */
c0648e02d   Ralf Baechle   MIPS: VPE: Fix bu...
293
294
295
296
297
298
  	if (vpe_getuid(tclimit)) {
  		err = sp_setfsuidgid(0, 0);
  		if (!err)
  			pr_err("restoring old creds failed
  ");
  	}
2600990e6   Ralf Baechle   [MIPS] kpsd and o...
299

46230aa6e   Ralf Baechle   [MIPS] RTLX: Hand...
300
301
302
303
304
  	old_fs = get_fs();
  	set_fs(KERNEL_DS);
  	written = rtlx_write(RTLX_CHANNEL_SYSIO, &ret, sizeof(ret));
  	set_fs(old_fs);
  	if (written < sizeof(ret))
2600990e6   Ralf Baechle   [MIPS] kpsd and o...
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
  		printk("KSPD: sp_work_handle_request failed to send to SP
  ");
  }
  
  static void sp_cleanup(void)
  {
  	struct files_struct *files = current->files;
  	int i, j;
  	struct fdtable *fdt;
  
  	j = 0;
  
  	/*
  	 * It is safe to dereference the fd table without RCU or
  	 * ->file_lock
  	 */
  	fdt = files_fdtable(files);
  	for (;;) {
  		unsigned long set;
  		i = j * __NFDBITS;
bbea9f696   Vadim Lobanov   [PATCH] fdtable: ...
325
  		if (i >= fdt->max_fds)
2600990e6   Ralf Baechle   [MIPS] kpsd and o...
326
327
328
329
330
331
332
333
334
335
336
337
  			break;
  		set = fdt->open_fds->fds_bits[j++];
  		while (set) {
  			if (set & 1) {
  				struct file * file = xchg(&fdt->fd[i], NULL);
  				if (file)
  					filp_close(file, files);
  			}
  			i++;
  			set >>= 1;
  		}
  	}
1928cc84a   Kevin D. Kissell   [MIPS] MT: Functi...
338
339
340
  
  	/* Put daemon cwd back to root to avoid umount problems */
  	sys_chdir("/");
2600990e6   Ralf Baechle   [MIPS] kpsd and o...
341
  }
982f6ffee   Ralf Baechle   MIPS: Remove usel...
342
  static int channel_open;
2600990e6   Ralf Baechle   [MIPS] kpsd and o...
343
344
  
  /* the work handler */
6d5aefb8e   David Howells   WorkQueue: Fix up...
345
  static void sp_work(struct work_struct *unused)
2600990e6   Ralf Baechle   [MIPS] kpsd and o...
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
  {
  	if (!channel_open) {
  		if( rtlx_open(RTLX_CHANNEL_SYSIO, 1) != 0) {
  			printk("KSPD: unable to open sp channel
  ");
  			sp_stopping = 1;
  		} else {
  			channel_open++;
  			printk(KERN_DEBUG "KSPD: SP channel opened
  ");
  		}
  	} else {
  		/* wait for some data, allow it to sleep */
  		rtlx_read_poll(RTLX_CHANNEL_SYSIO, 1);
  
  		/* Check we haven't been woken because we are stopping */
  		if (!sp_stopping)
  			sp_work_handle_request();
  	}
  
  	if (!sp_stopping)
  		queue_work(workqueue, &work);
  	else
  		sp_cleanup();
  }
  
  static void startwork(int vpe)
  {
  	sp_stopping = channel_open = 0;
  
  	if (workqueue == NULL) {
  		if ((workqueue = create_singlethread_workqueue("kspd")) == NULL) {
  			printk(KERN_ERR "unable to start kspd
  ");
  			return;
  		}
6d5aefb8e   David Howells   WorkQueue: Fix up...
382
  		INIT_WORK(&work, sp_work);
07cc0c9e6   Ralf Baechle   [MIPS] MT: Enable...
383
  	}
2600990e6   Ralf Baechle   [MIPS] kpsd and o...
384

07cc0c9e6   Ralf Baechle   [MIPS] MT: Enable...
385
  	queue_work(workqueue, &work);
2600990e6   Ralf Baechle   [MIPS] kpsd and o...
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
  }
  
  static void stopwork(int vpe)
  {
  	sp_stopping = 1;
  
  	printk(KERN_DEBUG "KSPD: SP stopping
  ");
  }
  
  void kspd_notify(struct kspd_notifications *notify)
  {
  	list_add(&notify->list, &kspd_notifylist);
  }
  
  static struct vpe_notifications notify;
  static int kspd_module_init(void)
  {
  	INIT_LIST_HEAD(&kspd_notifylist);
  
  	notify.start = startwork;
  	notify.stop = stopwork;
07cc0c9e6   Ralf Baechle   [MIPS] MT: Enable...
408
  	vpe_notify(tclimit, &notify);
2600990e6   Ralf Baechle   [MIPS] kpsd and o...
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
  
  	return 0;
  }
  
  static void kspd_module_exit(void)
  {
  
  }
  
  module_init(kspd_module_init);
  module_exit(kspd_module_exit);
  
  MODULE_DESCRIPTION("MIPS KSPD");
  MODULE_AUTHOR("Elizabeth Oldham, MIPS Technologies, Inc.");
  MODULE_LICENSE("GPL");