summaryrefslogtreecommitdiff
path: root/sys/compat/linux/arch/mips/linux_machdep.c
blob: f11e15af2436a3703efb954190f6e282650a6e14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/*	$NetBSD: linux_machdep.c,v 1.20 2003/12/04 19:38:22 atatat Exp $ */

/*-
 * Copyright (c) 1995, 2000, 2001 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Frank van der Linden and Emmanuel Dreyfus.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the NetBSD
 *	Foundation, Inc. and its contributors.
 * 4. Neither the name of The NetBSD Foundation nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: linux_machdep.c,v 1.20 2003/12/04 19:38:22 atatat Exp $");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/signalvar.h>
#include <sys/kernel.h>
#include <sys/proc.h>
#include <sys/user.h>
#include <sys/buf.h>
#include <sys/reboot.h>
#include <sys/conf.h>
#include <sys/exec.h>
#include <sys/file.h>
#include <sys/callout.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/msgbuf.h>
#include <sys/mount.h>
#include <sys/vnode.h>
#include <sys/device.h>
#include <sys/sa.h>
#include <sys/syscallargs.h>
#include <sys/filedesc.h>
#include <sys/exec_elf.h>
#include <sys/disklabel.h>
#include <sys/ioctl.h>
#include <sys/sysctl.h>
#include <miscfs/specfs/specdev.h>

#include <compat/linux/common/linux_types.h>
#include <compat/linux/common/linux_signal.h>
#include <compat/linux/common/linux_util.h>
#include <compat/linux/common/linux_ioctl.h>
#include <compat/linux/common/linux_hdio.h>
#include <compat/linux/common/linux_exec.h>
#include <compat/linux/common/linux_machdep.h>

#include <compat/linux/linux_syscallargs.h>

#include <machine/cpu.h>
#include <machine/psl.h>
#include <machine/reg.h>
#include <machine/regnum.h>
#include <machine/vmparam.h>
#include <machine/locore.h>

#include <mips/cache.h>

/*
 * To see whether wscons is configured (for virtual console ioctl calls).
 */
#if defined(_KERNEL_OPT)
#include "wsdisplay.h"
#endif
#if (NWSDISPLAY > 0)
#include <dev/wscons/wsconsio.h>
#include <dev/wscons/wsdisplay_usl_io.h>
#endif

/* 
 * Set set up registers on exec.
 * XXX not used at the moment since in sys/kern/exec_conf, LINUX_COMPAT
 * entry uses NetBSD's native setregs instead of linux_setregs
 */
void
linux_setregs(l, pack, stack) 
	struct lwp *l;
	struct exec_package *pack;
	u_long stack;
{	
	setregs(l, pack, stack);
	return;
}

/*
 * Send an interrupt to process.
 *
 * Adapted from sys/arch/mips/mips/mips_machdep.c
 *
 * XXX Does not work well yet with RT signals
 *
 */

void
linux_sendsig(sig, mask, code)  /* XXX Check me */
	int sig;
	const sigset_t *mask;
	u_long code;
{
	struct lwp *l = curlwp;
	struct proc *p = l->l_proc;
	struct linux_sigframe *fp;
	struct frame *f;
	int i,onstack;
	sig_t catcher = SIGACTION(p, sig).sa_handler;
	struct linux_sigframe sf;

#ifdef DEBUG_LINUX
	printf("linux_sendsig()\n");
#endif /* DEBUG_LINUX */
	f = (struct frame *)l->l_md.md_regs;

	/* 
	 * Do we need to jump onto the signal stack? 
	 */
	onstack =
	    (p->p_sigctx.ps_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
	    (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;

	/*
	 * Signal stack is broken (see at the end of linux_sigreturn), so we do
	 * not use it yet. XXX fix this.
	 */
	onstack=0;

	/* 
	 * Allocate space for the signal handler context. 
	 */
	if (onstack)
		fp = (struct linux_sigframe *)
		    ((caddr_t)p->p_sigctx.ps_sigstk.ss_sp
		    + p->p_sigctx.ps_sigstk.ss_size);
	else
		/* cast for _MIPS_BSD_API == _MIPS_BSD_API_LP32_64CLEAN case */
		fp = (struct linux_sigframe *)(u_int32_t)f->f_regs[SP];

	/* 
	 * Build stack frame for signal trampoline. 
	 */
	memset(&sf, 0, sizeof sf);

	/*
	 * This is the signal trampoline used by Linux, we don't use it,
	 * but we set it up in case an application expects it to be there
	 */
	sf.lsf_code[0] = 0x24020000;	/* li	v0, __NR_sigreturn	*/
	sf.lsf_code[1] = 0x0000000c;	/* syscall			*/

	native_to_linux_sigset(&sf.lsf_mask, mask);
	for (i=0; i<32; i++) {
		sf.lsf_sc.lsc_regs[i] = f->f_regs[i];
	}
	sf.lsf_sc.lsc_mdhi = f->f_regs[MULHI];
	sf.lsf_sc.lsc_mdlo = f->f_regs[MULLO];
	sf.lsf_sc.lsc_pc = f->f_regs[PC];
	sf.lsf_sc.lsc_status = f->f_regs[SR];
	sf.lsf_sc.lsc_cause = f->f_regs[CAUSE];
	sf.lsf_sc.lsc_badvaddr = f->f_regs[BADVADDR];

	/* 
	 * Save signal stack.  XXX broken
	 */
	/* kregs.sc_onstack = p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK; */

	/*
	 * Install the sigframe onto the stack
	 */
	fp -= sizeof(struct linux_sigframe);
	if (copyout(&sf, fp, sizeof(sf)) != 0) {
		/*
		 * Process has trashed its stack; give it an illegal
		 * instruction to halt it in its tracks.
		 */
#ifdef DEBUG_LINUX
		printf("linux_sendsig: stack trashed\n");
#endif /* DEBUG_LINUX */
		sigexit(l, SIGILL);
		/* NOTREACHED */
	}

	/* Set up the registers to return to sigcode. */
	f->f_regs[A0] = native_to_linux_signo[sig];
	f->f_regs[A1] = 0;
	f->f_regs[A2] = (unsigned long)&fp->lsf_sc;

#ifdef DEBUG_LINUX
	printf("sigcontext is at %p\n", &fp->lsf_sc);
#endif /* DEBUG_LINUX */

	f->f_regs[SP] = (unsigned long)fp;
	/* Signal trampoline code is at base of user stack. */
	f->f_regs[RA] = (unsigned long)p->p_sigctx.ps_sigcode;
	f->f_regs[T9] = (unsigned long)catcher;
	f->f_regs[PC] = (unsigned long)catcher;

	/* Remember that we're now on the signal stack. */
	if (onstack)
		p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK;

	return;
}

/*
 * System call to cleanup state after a signal
 * has been taken.  Reset signal mask and
 * stack state from context left by sendsig (above).
 */
int
linux_sys_sigreturn(l, v, retval)
	struct lwp *l;
	void *v;
	register_t *retval;
{
	struct linux_sys_sigreturn_args /* {
		syscallarg(struct linux_sigframe *) sf;
	} */ *uap = v;
	struct proc *p = l->l_proc;
	struct linux_sigframe *sf, ksf;
	struct frame *f;
	sigset_t mask;
	int i, error;

#ifdef DEBUG_LINUX
	printf("linux_sys_sigreturn()\n");
#endif /* DEBUG_LINUX */

	/*
	 * The trampoline code hands us the context.
	 * It is unsafe to keep track of it ourselves, in the event that a
	 * program jumps out of a signal handler.
	 */
	sf = SCARG(uap, sf); 

	if ((error = copyin(sf, &ksf, sizeof(ksf))) != 0)
		return (error);

	/* Restore the register context. */
	f = (struct frame *)l->l_md.md_regs;
	for (i=0; i<32; i++)
		f->f_regs[i] = ksf.lsf_sc.lsc_regs[i];
	f->f_regs[MULLO] = ksf.lsf_sc.lsc_mdlo;
	f->f_regs[MULHI] = ksf.lsf_sc.lsc_mdhi;
	f->f_regs[PC] = ksf.lsf_sc.lsc_pc;
	f->f_regs[BADVADDR] = ksf.lsf_sc.lsc_badvaddr;
	f->f_regs[CAUSE] = ksf.lsf_sc.lsc_cause;

	/* Restore signal stack. */
	p->p_sigctx.ps_sigstk.ss_flags &= ~SS_ONSTACK;

	/* Restore signal mask. */
	linux_to_native_sigset(&mask, (linux_sigset_t *)&ksf.lsf_mask);
	(void)sigprocmask1(p, SIG_SETMASK, &mask, 0);

	return (EJUSTRETURN);
}


int
linux_sys_rt_sigreturn(l, v, retval)  
	struct lwp *l;
	void *v;
	register_t *retval;
{
	return (ENOSYS);
}


#if 0
int
linux_sys_modify_ldt(l, v, retval)
	struct lwp *l;
	void *v;
	register_t *retval;
{
	/* 
	 * This syscall is not implemented in Linux/Mips: we should not
	 * be here
	 */
#ifdef DEBUG_LINUX
	printf("linux_sys_modify_ldt: should not be here.\n");	 
#endif /* DEBUG_LINUX */
  return 0;
}
#endif

/* 
 * major device numbers remapping
 */
dev_t
linux_fakedev(dev, raw)
	dev_t dev;
	int raw;
{
	/* XXX write me */
	return dev;
}

/*
 * We come here in a last attempt to satisfy a Linux ioctl() call
 */
int
linux_machdepioctl(p, v, retval)
	struct proc *p;
	void *v;
	register_t *retval;
{ 
	return 0;
}

/*
 * See above. If a root process tries to set access to an I/O port,
 * just let it have the whole range.
 */
int
linux_sys_ioperm(l, v, retval)
	struct lwp *l;
	void *v;
	register_t *retval;
{
	/* 
	 * This syscall is not implemented in Linux/Mips: we should not be here
	 */
#ifdef DEBUG_LINUX
	printf("linux_sys_ioperm: should not be here.\n");	 
#endif /* DEBUG_LINUX */
	return 0;
}

/*
 * wrapper linux_sys_new_uname() -> linux_sys_uname() 
 */
int	
linux_sys_new_uname(l, v, retval) 
	struct lwp *l;
	void *v;
	register_t *retval;
{
/*
 * Use this if you want to try Linux emulation with a glibc-2.2 
 * or higher. Note that signals will not work
 */
#if 0
        struct linux_sys_uname_args /* {
                syscallarg(struct linux_utsname *) up;
        } */ *uap = v;
        struct linux_utsname luts;

        strncpy(luts.l_sysname, linux_sysname, sizeof(luts.l_sysname));
        strncpy(luts.l_nodename, hostname, sizeof(luts.l_nodename));
        strncpy(luts.l_release, "2.4.0", sizeof(luts.l_release));
        strncpy(luts.l_version, linux_version, sizeof(luts.l_version));
        strncpy(luts.l_machine, machine, sizeof(luts.l_machine));
        strncpy(luts.l_domainname, domainname, sizeof(luts.l_domainname));

        return copyout(&luts, SCARG(uap, up), sizeof(luts));
#else
	return linux_sys_uname(l, v, retval);
#endif
}

/*
 * In Linux, cacheflush is currently implemented
 * as a whole cache flush (arguments are ignored)
 * we emulate this broken beahior.
 */
int
linux_sys_cacheflush(l, v, retval)
	struct lwp *l;
	void *v;
	register_t *retval;
{
	mips_icache_sync_all();
	mips_dcache_wbinv_all();
	return 0;
}

/*
 * This system call is depecated in Linux, but 
 * some binaries and some libraries use it.
 */
int
linux_sys_sysmips(l, v, retval)
	struct lwp *l;
	void *v;
	register_t *retval;
{
	struct linux_sys_sysmips_args {
		syscallarg(int) cmd;
		syscallarg(int) arg1;
		syscallarg(int) arg2;
		syscallarg(int) arg3;
	} *uap = v;
	struct proc *p = l->l_proc;
	int error;
	
	switch (SCARG(uap, cmd)) {
	case LINUX_SETNAME: {
		char nodename [LINUX___NEW_UTS_LEN + 1];
		int name[2];
		size_t len;

		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
			return error;
		if ((error = copyinstr((char *)SCARG(uap, arg1), nodename, 
		    LINUX___NEW_UTS_LEN, &len)) != 0)
			return error;

		name[0] = CTL_KERN;
		name[1] = KERN_HOSTNAME;
		return (old_sysctl(&name[0], 2, 0, 0, nodename, len, NULL));
		
		break;
	}
	case LINUX_MIPS_ATOMIC_SET: {
		void *addr;
		int s;
		u_int8_t value = 0;

		addr = (void *)SCARG(uap, arg1);

		s = splhigh();
		/*
		 * No error testing here. This is bad, but Linux does
		 * it like this. The source aknowledge "This is broken"
		 * in a comment...
		 */
		(void) copyin(addr, &value, 1);
		*retval = value;
		value = (u_int8_t) SCARG(uap, arg2);
		error = copyout(&value, addr, 1);
		splx(s);

		return 0;
		break;
	}
	case LINUX_MIPS_FIXADE:		/* XXX not implemented */
		break;
	case LINUX_FLUSH_CACHE:
		mips_icache_sync_all();
		mips_dcache_wbinv_all();
		break;
	case LINUX_MIPS_RDNVRAM:
		return EIO;
		break;
	default:
		return EINVAL;
		break;
	}
#ifdef DEBUG_LINUX
	printf("linux_sys_sysmips(): unimplemented command %d\n", 
	    SCARG(uap,cmd));	
#endif /* DEBUG_LINUX */
	return 0;
}