summaryrefslogtreecommitdiffstats
path: root/sys/sparc64/sparc64/trap.c
blob: f1fa5446c1e63377c23e40b541ae1d35257e5447 (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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
/*-
 * Copyright (c) 2001, Jake Burkholder
 * Copyright (C) 1994, David Greenman
 * Copyright (c) 1990, 1993
 *      The Regents of the University of California.  All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * the University of Utah, and William Jolitz.
 *
 * 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 University of
 *      California, Berkeley and its contributors.
 * 4. Neither the name of the University 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 AUTHOR 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 AUTHOR 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.
 *
 *      from: @(#)trap.c        7.4 (Berkeley) 5/13/91
 * 	from: FreeBSD: src/sys/i386/i386/trap.c,v 1.197 2001/07/19
 * $FreeBSD$
 */

#include "opt_ddb.h"
#include "opt_ktr.h"

#include <sys/param.h>
#include <sys/ktr.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#include <sys/systm.h>
#include <sys/pioctl.h>
#include <sys/proc.h>
#include <sys/syscall.h>
#include <sys/sysent.h>
#include <sys/user.h>
#include <sys/vmmeter.h>

#include <vm/vm.h>
#include <vm/pmap.h>
#include <vm/vm_extern.h>
#include <vm/vm_param.h>
#include <vm/vm_kern.h>
#include <vm/vm_map.h>
#include <vm/vm_page.h>

#include <machine/clock.h>
#include <machine/frame.h>
#include <machine/intr_machdep.h>
#include <machine/pcb.h>
#include <machine/pv.h>
#include <machine/trap.h>
#include <machine/tstate.h>
#include <machine/tte.h>
#include <machine/tlb.h>
#include <machine/tsb.h>
#include <machine/watch.h>

void trap(struct trapframe *tf);
int trap_mmu_fault(struct thread *td, struct trapframe *tf);
void syscall(struct thread *td, struct trapframe *tf, u_int sticks);

u_long trap_mask = 0xffffffffffffffffL & ~(1 << T_INTR);

extern char *syscallnames[];

const char *trap_msg[] = {
	"reserved",
	"power on reset",
	"watchdog reset",
	"externally initiated reset",
	"software initiated reset",
	"red state exception",
	"instruction access exception",
	"instruction access error",
	"illegal instruction",
	"privileged opcode",
	"floating point disabled",
	"floating point exception ieee 754",
	"floating point exception other",
	"tag overflow",
	"division by zero",
	"data access exception",
	"data access error",
	"memory address not aligned",
	"lddf memory address not aligned",
	"stdf memory address not aligned",
	"privileged action",
	"interrupt vector",
	"physical address watchpoint",
	"virtual address watchpoint",
	"corrected ecc error",
	"fast instruction access mmu miss",
	"fast data access mmu miss",
	"fast data access protection",
	"spill",
	"fill",
	"fill",
	"breakpoint",
	"syscall",
	"trap instruction",
};

void
trap(struct trapframe *tf)
{
	u_int sticks;
	struct thread *td;
	struct proc *p;
	int error;
	int ucode;
	int type;
	int sig;
	int mask;

	KASSERT(PCPU_GET(curthread) != NULL, ("trap: curthread NULL"));
	KASSERT(PCPU_GET(curpcb) != NULL, ("trap: curpcb NULL"));

	error = 0;
	td = PCPU_GET(curthread);
	p = td->td_proc;
	type = tf->tf_type;
	ucode = type;	/* XXX */
	sticks = 0;

#if KTR_COMPILE & KTR_TRAP
	if (trap_mask & (1 << (type & ~T_KERNEL))) {
		CTR5(KTR_TRAP, "trap: %s type=%s (%s) ws=%#lx ow=%#lx",
		    p->p_comm, trap_msg[type & ~T_KERNEL],
		    ((type & T_KERNEL) ? "kernel" : "user"),
		    rdpr(wstate), rdpr(otherwin));
	}
#endif

	if (type == T_SYSCALL)
		cnt.v_syscall++;
	else if ((type & ~T_KERNEL) == T_INTR)
		cnt.v_intr++;
	else
		cnt.v_trap++;

	if ((type & T_KERNEL) == 0) {
		sticks = td->td_kse->ke_sticks;
		td->td_frame = tf;
	}

	switch (type) {
	case T_ALIGN:
	case T_ALIGN_LDDF:
	case T_ALIGN_STDF:
		sig = SIGBUS;
		goto trapsig;
	case T_BREAKPOINT:
		sig = SIGTRAP;
		goto trapsig;
	case T_DIVIDE:
		sig = SIGFPE;
		goto trapsig;
	case T_FP_DISABLED:
		if (fp_enable_thread(td))
			goto user;
		/* Fallthrough. */
	case T_FP_IEEE:
	case T_FP_OTHER:
		sig = SIGFPE;
		goto trapsig;
	case T_DATA_ERROR:
	case T_DATA_EXCPTN:
	case T_INSN_ERROR:
	case T_INSN_EXCPTN:
		sig = SIGILL;	/* XXX */
		goto trapsig;
	case T_DMMU_MISS:
	case T_DMMU_PROT:
	case T_IMMU_MISS:
		error = trap_mmu_fault(td, tf);
		if (error == 0)
			goto user;
		sig = error;
		goto trapsig;
	case T_FILL:
		if (rwindow_load(td, tf, 2))
			sigexit(td, SIGILL);
		goto out;
	case T_FILL_RET:
		if (rwindow_load(td, tf, 1))
			sigexit(td, SIGILL);
		goto out;
	case T_INSN_ILLEGAL:
		sig = SIGILL;
		goto trapsig;
	case T_INTR:
		intr_dispatch(tf->tf_arg, tf);
		goto out;
	case T_PRIV_ACTION:
	case T_PRIV_OPCODE:
		sig = SIGBUS;
		goto trapsig;
	case T_SOFT:
		sig = SIGILL;
		goto trapsig;
	case T_SPILL:
		if (rwindow_save(td))
			sigexit(td, SIGILL);
		goto out;
	case T_SYSCALL:
		/* syscall() calls userret(), so we need goto out; */
		syscall(td, tf, sticks);
		goto out;
	case T_TAG_OVFLW:
		sig = SIGEMT;
		goto trapsig;
#ifdef DDB
	case T_BREAKPOINT | T_KERNEL:
		if (kdb_trap(tf) != 0)
			goto out;
		break;
#endif
	case T_DMMU_MISS | T_KERNEL:
	case T_DMMU_PROT | T_KERNEL:
		error = trap_mmu_fault(td, tf);
		if (error == 0)
			goto out;
		break;
	case T_INTR | T_KERNEL:
		intr_dispatch(tf->tf_arg, tf);
		goto out;
	case T_WATCH_VIRT | T_KERNEL:
		/*
		 * At the moment, just print the information from the trap,
		 * remove the watchpoint, use evil magic to execute the
		 * instruction (we temporarily save the instruction at
		 * %tnpc, write a trap instruction, resume, and reset the
		 * watch point when the trap arrives).
		 * To make sure that no interrupt gets in between and creates
		 * a potentially large window where the watchpoint is inactive,
		 * disable interrupts temporarily.
		 * This is obviously fragile and evilish.
		 */
		printf("Virtual watchpoint triggered, tpc=0x%lx, tnpc=0x%lx\n",
		    tf->tf_tpc, tf->tf_tnpc);
		PCPU_SET(wp_pstate, (tf->tf_tstate & TSTATE_PSTATE_MASK) >>
		    TSTATE_PSTATE_SHIFT);
		tf->tf_tstate &= ~TSTATE_IE;
		wrpr(pstate, rdpr(pstate), PSTATE_IE);
		PCPU_SET(wp_insn, *((u_int *)tf->tf_tnpc));
		*((u_int *)tf->tf_tnpc) = 0x91d03002;	/* ta %xcc, 2 */
		flush(tf->tf_tnpc);
		PCPU_SET(wp_va, watch_virt_get(&mask));
		PCPU_SET(wp_mask, mask);
		watch_virt_clear();
		goto out;
	case T_RESTOREWP | T_KERNEL:
		/*
		 * Undo the tweaks tone for T_WATCH, reset the watch point and
		 * contunue execution.
		 * Note that here, we run with interrupts enabled, so there
		 * is a small chance that we will be interrupted before we
		 * could reset the watch point.
		 */
		tf->tf_tstate = (tf->tf_tstate & ~TSTATE_PSTATE_MASK) |
		    PCPU_GET(wp_pstate) << TSTATE_PSTATE_SHIFT;
		watch_virt_set_mask(PCPU_GET(wp_va), PCPU_GET(wp_mask));
		*(u_int *)tf->tf_tpc = PCPU_GET(wp_insn);
		flush(tf->tf_tpc);
		goto out;
	default:
		break;
	}
	panic("trap: %s", trap_msg[type & ~T_KERNEL]);

trapsig:
	/* Translate fault for emulators. */
	if (p->p_sysent->sv_transtrap != NULL)
		sig = (p->p_sysent->sv_transtrap)(sig, type);
	trapsignal(p, sig, ucode);
user:
	userret(td, tf, sticks);
out:
#if KTR_COMPILE & KTR_TRAP
	if (trap_mask & (1 << (type & ~T_KERNEL))) {
		CTR1(KTR_TRAP, "trap: p=%p return", p);
	}
#endif
	return;
}

int
trap_mmu_fault(struct thread *td, struct trapframe *tf)
{
	struct mmuframe *mf;
	struct vmspace *vm;
	struct stte *stp;
	struct pcb *pcb;
	struct tte tte;
	struct proc *p;
	vm_offset_t va;
	vm_prot_t prot;
	u_long ctx;
	pmap_t pm;
	int flags;
	int type;
	int rv;

	p = td->td_proc;
	KASSERT(p->p_vmspace != NULL, ("trap_dmmu_miss: vmspace NULL"));

	rv = KERN_SUCCESS;
	mf = (struct mmuframe *)tf->tf_arg;
	ctx = TLB_TAR_CTX(mf->mf_tar);
	pcb = PCPU_GET(curpcb);
	type = tf->tf_type & ~T_KERNEL;
	va = TLB_TAR_VA(mf->mf_tar);

	CTR4(KTR_TRAP, "trap_mmu_fault: td=%p pm_ctx=%#lx va=%#lx ctx=%#lx",
	    td, p->p_vmspace->vm_pmap.pm_context, va, ctx);

	if (type == T_DMMU_PROT) {
		prot = VM_PROT_WRITE;
		flags = VM_FAULT_DIRTY;
	} else {
		if (type == T_DMMU_MISS)
			prot = VM_PROT_READ;
		else
			prot = VM_PROT_READ | VM_PROT_EXECUTE;
		flags = VM_FAULT_NORMAL;
	}

	if (ctx == TLB_CTX_KERNEL) {
		mtx_lock(&Giant);
		rv = vm_fault(kernel_map, va, prot, VM_FAULT_NORMAL);
		mtx_unlock(&Giant);
		if (rv == KERN_SUCCESS) {
			stp = tsb_kvtostte(va);
			tte = stp->st_tte;
			if (type == T_IMMU_MISS)
				tlb_store(TLB_DTLB | TLB_ITLB, va, ctx, tte);
			else
				tlb_store(TLB_DTLB, va, ctx, tte);
		}
	} else if (tf->tf_type & T_KERNEL &&
	    (td->td_intr_nesting_level != 0 || pcb->pcb_onfault == NULL)) {
		rv = KERN_FAILURE;
	} else {
		mtx_lock(&Giant);
		vm = p->p_vmspace;
		pm = &vm->vm_pmap;
		stp = tsb_stte_lookup(pm, va);
		if (stp == NULL || type == T_DMMU_PROT) {
			/*
			 * Keep the process from being swapped out at this
			 * critical time.
			 */
			PROC_LOCK(p);
			++p->p_lock;
			PROC_UNLOCK(p);
		
			/*
			 * Grow the stack if necessary.  vm_map_growstack only
			 * fails if the va falls into a growable stack region
			 * and the stack growth fails.  If it succeeds, or the
			 * va was not within a growable stack region, fault in
			 * the user page.
			 */
			if (vm_map_growstack(p, va) != KERN_SUCCESS)
				rv = KERN_FAILURE;
			else
				rv = vm_fault(&vm->vm_map, va, prot, flags);
		
			/*
			 * Now the process can be swapped again.
			 */
			PROC_LOCK(p);
			--p->p_lock;
			PROC_UNLOCK(p);
		} else if (type == T_IMMU_MISS) {
			if ((stp->st_tte.tte_data & TD_EXEC) == 0)
				rv = KERN_FAILURE;
			else
				tlb_store(TLB_DTLB | TLB_ITLB, va, ctx,
				    stp->st_tte);
		} else if (type == T_DMMU_PROT &&
			   (stp->st_tte.tte_data & TD_SW) == 0) {
			rv = KERN_FAILURE;
		} else {
			tlb_store(TLB_DTLB, va, ctx, stp->st_tte);
		}
		mtx_unlock(&Giant);
	}
	CTR3(KTR_TRAP, "trap_mmu_fault: return p=%p va=%#lx rv=%d", p, va, rv);
	if (rv == KERN_SUCCESS)
		return (0);
	if (tf->tf_type & T_KERNEL) {
		if (pcb->pcb_onfault != NULL && ctx != TLB_CTX_KERNEL) {
			tf->tf_tpc = (u_long)pcb->pcb_onfault;
			tf->tf_tnpc = tf->tf_tpc + 4;
			return (0);
		}
	}
	return (rv == KERN_PROTECTION_FAILURE ? SIGBUS : SIGSEGV);
}

/* Maximum number of arguments that can be passed via the out registers. */
#define	REG_MAXARGS	6

/*
 * Syscall handler. The arguments to the syscall are passed in the o registers
 * by the caller, and are saved in the trap frame. The syscall number is passed
 * in %g1 (and also saved in the trap frame).
 */
void
syscall(struct thread *td, struct trapframe *tf, u_int sticks)
{
	struct sysent *callp;
	struct proc *p;
	u_long code;
	u_long tpc;
	int reg;
	int regcnt;
	int narg;
	int error;
	register_t args[8];
	register_t *argp;

	narg = 0;
	error = 0;
	reg = 0;
	regcnt = REG_MAXARGS;
	code = tf->tf_global[1];
	p = td->td_proc;
	/*
	 * For syscalls, we don't want to retry the faulting instruction
	 * (usually), instead we need to advance one instruction.
	 */
	tpc = tf->tf_tpc;
	tf->tf_tpc = tf->tf_tnpc;
	tf->tf_tnpc += 4;

	if (p->p_sysent->sv_prepsyscall) {
		/*
		 * The prep code is MP aware.
		 */
#if 0
		(*p->p_sysent->sv_prepsyscall)(tf, args, &code, &params);
#endif	
	} else 	if (code == SYS_syscall || code == SYS___syscall) {
		code = tf->tf_out[reg++];
		regcnt--;
	}

 	if (p->p_sysent->sv_mask)
 		code &= p->p_sysent->sv_mask;

 	if (code >= p->p_sysent->sv_size)
 		callp = &p->p_sysent->sv_table[0];
  	else
 		callp = &p->p_sysent->sv_table[code];

	narg = callp->sy_narg & SYF_ARGMASK;

	if (narg <= regcnt)
		argp = &tf->tf_out[reg];
	else {
		KASSERT(narg <= sizeof(args) / sizeof(args[0]),
		    ("Too many syscall arguments!"));
		argp = args;
		bcopy(&tf->tf_out[reg], args, sizeof(args[0]) * regcnt);
		error = copyin((void *)(tf->tf_out[6] + SPOFF +
		    offsetof(struct frame, f_pad[6])),
		    &args[reg + regcnt], (narg - regcnt) * sizeof(args[0]));
		if (error != 0)
			goto bad;
	}

	CTR5(KTR_SYSC, "syscall: td=%p %s(%#lx, %#lx, %#lx)", td,
	    syscallnames[code], argp[0], argp[1], argp[2]);

	/*
	 * Try to run the syscall without the MP lock if the syscall
	 * is MP safe.
	 */
	if ((callp->sy_narg & SYF_MPSAFE) == 0)
		mtx_lock(&Giant);

#ifdef KTRACE
	/*
	 * We have to obtain the MP lock no matter what if 
	 * we are ktracing
	 */
	if (KTRPOINT(p, KTR_SYSCALL)) {
		ktrsyscall(p->p_tracep, code, narg, args);
	}
#endif
	td->td_retval[0] = 0;
	td->td_retval[1] = tf->tf_out[1];

	STOPEVENT(p, S_SCE, narg);	/* MP aware */

	error = (*callp->sy_call)(td, argp);

	CTR5(KTR_SYSC, "syscall: p=%p error=%d %s return %#lx %#lx ", p,
	    error, syscallnames[code], td->td_retval[0], td->td_retval[1]);
	
	/*
	 * MP SAFE (we may or may not have the MP lock at this point)
	 */
	switch (error) {
	case 0:
		tf->tf_out[0] = td->td_retval[0];
		tf->tf_out[1] = td->td_retval[1];
		tf->tf_tstate &= ~TSTATE_XCC_C;
		break;

	case ERESTART:
		/*
		 * Undo the tpc advancement we have done above, we want to
		 * reexecute the system call.
		 */
		tf->tf_tpc = tpc;
		tf->tf_tnpc -= 4;
		break;

	case EJUSTRETURN:
		break;

	default:
bad:
 		if (p->p_sysent->sv_errsize) {
 			if (error >= p->p_sysent->sv_errsize)
  				error = -1;	/* XXX */
   			else
  				error = p->p_sysent->sv_errtbl[error];
		}
		tf->tf_out[0] = error;
		tf->tf_tstate |= TSTATE_XCC_C;
		break;
	}

	/*
	 * Handle reschedule and other end-of-syscall issues
	 */
	userret(td, tf, sticks);

#ifdef KTRACE
	if (KTRPOINT(p, KTR_SYSRET)) {
		ktrsysret(p->p_tracep, code, error, td->td_retval[0]);
	}
#endif

	/*
	 * Release Giant if we had to get it.  Don't use mtx_owned(),
	 * we want to catch broken syscalls.
	 */
	if ((callp->sy_narg & SYF_MPSAFE) == 0)
		mtx_unlock(&Giant);

	/*
	 * This works because errno is findable through the
	 * register set.  If we ever support an emulation where this
	 * is not the case, this code will need to be revisited.
	 */
	STOPEVENT(p, S_SCX, code);

#ifdef WITNESS
	if (witness_list(td)) {
		panic("system call %s returning with mutex(s) held\n",
		    syscallnames[code]);
	}
#endif
	mtx_assert(&sched_lock, MA_NOTOWNED);
	mtx_assert(&Giant, MA_NOTOWNED);
	
}
OpenPOWER on IntegriCloud