trap.c revision 1.14
1/*	$NetBSD: trap.c,v 1.14 2003/07/15 02:54:44 lukem Exp $	*/
2
3/*
4 * Copyright 2001 Wasabi Systems, Inc.
5 * All rights reserved.
6 *
7 * Written by Eduardo Horvath and Simon Burge for Wasabi Systems, Inc.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 *    must display the following acknowledgement:
19 *      This product includes software developed for the NetBSD Project by
20 *      Wasabi Systems, Inc.
21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 *    or promote products derived from this software without specific prior
23 *    written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38/*
39 * Copyright (C) 1995, 1996 Wolfgang Solfrank.
40 * Copyright (C) 1995, 1996 TooLs GmbH.
41 * All rights reserved.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 *    notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 *    notice, this list of conditions and the following disclaimer in the
50 *    documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 *    must display the following acknowledgement:
53 *	This product includes software developed by TooLs GmbH.
54 * 4. The name of TooLs GmbH may not be used to endorse or promote products
55 *    derived from this software without specific prior written permission.
56 *
57 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
58 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
59 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
60 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
61 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
62 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
63 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
64 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
65 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
66 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
67 */
68
69#include <sys/cdefs.h>
70__KERNEL_RCSID(0, "$NetBSD: trap.c,v 1.14 2003/07/15 02:54:44 lukem Exp $");
71
72#include "opt_altivec.h"
73#include "opt_ddb.h"
74#include "opt_ktrace.h"
75#include "opt_systrace.h"
76#include "opt_syscall_debug.h"
77
78#include <sys/param.h>
79#include <sys/proc.h>
80#include <sys/reboot.h>
81#include <sys/syscall.h>
82#include <sys/systm.h>
83#include <sys/user.h>
84#ifdef KTRACE
85#include <sys/ktrace.h>
86#endif
87#include <sys/pool.h>
88#include <sys/sa.h>
89#include <sys/savar.h>
90#ifdef SYSTRACE
91#include <sys/systrace.h>
92#endif
93
94#include <uvm/uvm_extern.h>
95
96#include <dev/cons.h>
97
98#include <machine/cpu.h>
99#include <machine/db_machdep.h>
100#include <machine/fpu.h>
101#include <machine/frame.h>
102#include <machine/pcb.h>
103#include <machine/psl.h>
104#include <machine/trap.h>
105
106#include <powerpc/spr.h>
107#include <powerpc/ibm4xx/pmap.h>
108#include <powerpc/ibm4xx/tlb.h>
109#include <powerpc/fpu/fpu_extern.h>
110
111/* These definitions should probably be somewhere else			XXX */
112#define	FIRSTARG	3		/* first argument is in reg 3 */
113#define	NARGREG		8		/* 8 args are in registers */
114#define	MOREARGS(sp)	((caddr_t)((int)(sp) + 8)) /* more args go here */
115
116static int fix_unaligned __P((struct lwp *l, struct trapframe *frame));
117
118void trap __P((struct trapframe *));	/* Called from locore / trap_subr */
119/* Why are these not defined in a header? */
120int badaddr __P((void *, size_t));
121int badaddr_read __P((void *, size_t, int *));
122int ctx_setup __P((int, int));
123
124#ifdef DEBUG
125#define TDB_ALL	0x1
126int trapdebug = /* TDB_ALL */ 0;
127#define	DBPRINTF(x, y)	if (trapdebug & (x)) printf y
128#else
129#define DBPRINTF(x, y)
130#endif
131
132void
133trap(struct trapframe *frame)
134{
135	struct lwp *l = curlwp;
136	struct proc *p = l ? l->l_proc : NULL;
137	int type = frame->exc;
138	int ftype, rv;
139
140	KASSERT(l == 0 || (l->l_stat == LSONPROC));
141
142	if (frame->srr1 & PSL_PR)
143		type |= EXC_USER;
144
145	ftype = VM_PROT_READ;
146
147	DBPRINTF(TDB_ALL, ("trap(%x) at %lx from frame %p &frame %p\n",
148	    type, frame->srr0, frame, &frame));
149
150	switch (type) {
151	case EXC_DEBUG|EXC_USER:
152		{
153			int srr2, srr3;
154
155			__asm __volatile("mfspr %0,0x3f0" :
156			    "=r" (rv), "=r" (srr2), "=r" (srr3) :);
157			printf("debug reg is %x srr2 %x srr3 %x\n", rv, srr2,
158			    srr3);
159			/* XXX fall through or break here?! */
160		}
161		/*
162		 * DEBUG intr -- probably single-step.
163		 */
164	case EXC_TRC|EXC_USER:
165		KERNEL_PROC_LOCK(l);
166		frame->srr1 &= ~PSL_SE;
167		trapsignal(l, SIGTRAP, EXC_TRC);
168		KERNEL_PROC_UNLOCK(l);
169		break;
170
171	/*
172	 * If we could not find and install appropriate TLB entry, fall through.
173	 */
174
175	case EXC_DSI:
176		/* FALLTHROUGH */
177	case EXC_DTMISS:
178		{
179			struct vm_map *map;
180			vaddr_t va;
181			struct faultbuf *fb = NULL;
182
183			KERNEL_LOCK(LK_CANRECURSE|LK_EXCLUSIVE);
184			va = frame->dar;
185			if (frame->tf_xtra[TF_PID] == KERNEL_PID) {
186				map = kernel_map;
187			} else {
188				map = &p->p_vmspace->vm_map;
189			}
190
191			if (frame->tf_xtra[TF_ESR] & (ESR_DST|ESR_DIZ))
192				ftype = VM_PROT_WRITE;
193
194			DBPRINTF(TDB_ALL,
195			    ("trap(EXC_DSI) at %lx %s fault on %p esr %x\n",
196			    frame->srr0,
197			    (ftype & VM_PROT_WRITE) ? "write" : "read",
198			    (void *)va, frame->tf_xtra[TF_ESR]));
199			rv = uvm_fault(map, trunc_page(va), 0, ftype);
200			KERNEL_UNLOCK();
201			if (rv == 0)
202				goto done;
203			if ((fb = l->l_addr->u_pcb.pcb_onfault) != NULL) {
204				frame->tf_xtra[TF_PID] = KERNEL_PID;
205				frame->srr0 = fb->fb_pc;
206				frame->srr1 |= PSL_IR; /* Re-enable IMMU */
207				frame->fixreg[1] = fb->fb_sp;
208				frame->fixreg[2] = fb->fb_r2;
209				frame->fixreg[3] = 1; /* Return TRUE */
210				frame->cr = fb->fb_cr;
211				memcpy(&frame->fixreg[13], fb->fb_fixreg,
212				    sizeof(fb->fb_fixreg));
213				goto done;
214			}
215		}
216		goto brain_damage;
217
218	case EXC_DSI|EXC_USER:
219		/* FALLTHROUGH */
220	case EXC_DTMISS|EXC_USER:
221		KERNEL_PROC_LOCK(l);
222
223		if (frame->tf_xtra[TF_ESR] & (ESR_DST|ESR_DIZ))
224			ftype = VM_PROT_WRITE;
225
226		DBPRINTF(TDB_ALL,
227		    ("trap(EXC_DSI|EXC_USER) at %lx %s fault on %lx %x\n",
228		    frame->srr0, (ftype & VM_PROT_WRITE) ? "write" : "read",
229		    frame->dar, frame->tf_xtra[TF_ESR]));
230		KASSERT(l == curlwp && (l->l_stat == LSONPROC));
231		rv = uvm_fault(&p->p_vmspace->vm_map, trunc_page(frame->dar),
232		    0, ftype);
233		if (rv == 0) {
234			KERNEL_PROC_UNLOCK(l);
235			break;
236		}
237		if (rv == ENOMEM) {
238			printf("UVM: pid %d (%s) lid %d, uid %d killed: "
239			    "out of swap\n",
240			    p->p_pid, p->p_comm, l->l_lid,
241			    p->p_cred && p->p_ucred ?
242			    p->p_ucred->cr_uid : -1);
243			trapsignal(l, SIGKILL, EXC_DSI);
244		} else {
245			trapsignal(l, SIGSEGV, EXC_DSI);
246		}
247		KERNEL_PROC_UNLOCK(l);
248		break;
249	case EXC_ITMISS|EXC_USER:
250	case EXC_ISI|EXC_USER:
251		KERNEL_PROC_LOCK(l);
252		ftype = VM_PROT_READ | VM_PROT_EXECUTE;
253		DBPRINTF(TDB_ALL,
254		    ("trap(EXC_ISI|EXC_USER) at %lx %s fault on %lx tf %p\n",
255		    frame->srr0, (ftype & VM_PROT_WRITE) ? "write" : "read",
256		    frame->srr0, frame));
257		rv = uvm_fault(&p->p_vmspace->vm_map, trunc_page(frame->srr0),
258		    0, ftype);
259		if (rv == 0) {
260			KERNEL_PROC_UNLOCK(l);
261			break;
262		}
263		trapsignal(l, SIGSEGV, EXC_ISI);
264		KERNEL_PROC_UNLOCK(l);
265		break;
266
267	case EXC_AST|EXC_USER:
268		curcpu()->ci_astpending = 0;	/* we are about to do it */
269		KERNEL_PROC_LOCK(l);
270		uvmexp.softs++;
271		if (p->p_flag & P_OWEUPC) {
272			p->p_flag &= ~P_OWEUPC;
273			ADDUPROF(p);
274		}
275		/* Check whether we are being preempted. */
276		if (curcpu()->ci_want_resched)
277			preempt(0);
278		KERNEL_PROC_UNLOCK(l);
279		break;
280
281
282	case EXC_ALI|EXC_USER:
283		KERNEL_PROC_LOCK(l);
284		if (fix_unaligned(l, frame) != 0)
285			trapsignal(l, SIGBUS, EXC_ALI);
286		else
287			frame->srr0 += 4;
288		KERNEL_PROC_UNLOCK(l);
289		break;
290
291	case EXC_PGM|EXC_USER:
292		/*
293		 * Illegal insn:
294		 *
295		 * let's try to see if it's FPU and can be emulated.
296		 */
297		uvmexp.traps ++;
298		if (!(l->l_addr->u_pcb.pcb_flags & PCB_FPU)) {
299			memset(&l->l_addr->u_pcb.pcb_fpu, 0,
300				sizeof l->l_addr->u_pcb.pcb_fpu);
301			l->l_addr->u_pcb.pcb_flags |= PCB_FPU;
302		}
303
304		if ((rv = fpu_emulate(frame,
305			(struct fpreg *)&l->l_addr->u_pcb.pcb_fpu))) {
306			KERNEL_PROC_LOCK(l);
307			trapsignal(l, rv, EXC_PGM);
308			KERNEL_PROC_UNLOCK(l);
309		}
310		break;
311
312	case EXC_MCHK:
313		{
314			struct faultbuf *fb;
315
316			if ((fb = l->l_addr->u_pcb.pcb_onfault) != NULL) {
317				frame->tf_xtra[TF_PID] = KERNEL_PID;
318				frame->srr0 = fb->fb_pc;
319				frame->srr1 |= PSL_IR; /* Re-enable IMMU */
320				frame->fixreg[1] = fb->fb_sp;
321				frame->fixreg[2] = fb->fb_r2;
322				frame->fixreg[3] = 1; /* Return TRUE */
323				frame->cr = fb->fb_cr;
324				memcpy(&frame->fixreg[13], fb->fb_fixreg,
325				    sizeof(fb->fb_fixreg));
326				goto done;
327			}
328		}
329		goto brain_damage;
330	default:
331 brain_damage:
332		printf("trap type 0x%x at 0x%lx\n", type, frame->srr0);
333#ifdef DDB
334		if (kdb_trap(type, frame))
335			goto done;
336#endif
337#ifdef TRAP_PANICWAIT
338		printf("Press a key to panic.\n");
339		cngetc();
340#endif
341		panic("trap");
342	}
343
344	/* Take pending signals. */
345	{
346		int sig;
347
348		while ((sig = CURSIG(l)) != 0)
349			postsig(sig);
350	}
351
352	/* Invoke per-process kernel-exit handling, if any */
353	if (p->p_userret)
354		(p->p_userret)(l, p->p_userret_arg);
355
356	/* Invoke any pending upcalls */
357	while (l->l_flag & L_SA_UPCALL)
358		sa_upcall_userret(l);
359
360	curcpu()->ci_schedstate.spc_curpriority = l->l_priority = l->l_usrpri;
361 done:
362	return;
363}
364
365int
366ctx_setup(int ctx, int srr1)
367{
368	volatile struct pmap *pm;
369
370	/* Update PID if we're returning to user mode. */
371	if (srr1 & PSL_PR) {
372		pm = curproc->p_vmspace->vm_map.pmap;
373		if (!pm->pm_ctx) {
374			ctx_alloc((struct pmap *)pm);
375		}
376		ctx = pm->pm_ctx;
377		if (srr1 & PSL_SE) {
378			int dbreg, mask = 0x48000000;
379				/*
380				 * Set the Internal Debug and
381				 * Instruction Completion bits of
382				 * the DBCR0 register.
383				 *
384				 * XXX this is also used by jtag debuggers...
385				 */
386			__asm __volatile("mfspr %0,0x3f2;"
387			    "or %0,%0,%1;"
388			    "mtspr 0x3f2,%0;" :
389			    "=&r" (dbreg) : "r" (mask));
390		}
391	}
392	else if (!ctx) {
393		ctx = KERNEL_PID;
394	}
395	return (ctx);
396}
397
398/*
399 * Used by copyin()/copyout()
400 */
401extern vaddr_t vmaprange __P((struct proc *, vaddr_t, vsize_t, int));
402extern void vunmaprange __P((vaddr_t, vsize_t));
403static int bigcopyin __P((const void *, void *, size_t ));
404static int bigcopyout __P((const void *, void *, size_t ));
405
406int
407copyin(const void *udaddr, void *kaddr, size_t len)
408{
409	struct pmap *pm = curproc->p_vmspace->vm_map.pmap;
410	int msr, pid, tmp, ctx;
411	struct faultbuf env;
412
413	/* For bigger buffers use the faster copy */
414	if (len > 256) return (bigcopyin(udaddr, kaddr, len));
415
416	if (setfault(&env)) {
417		curpcb->pcb_onfault = 0;
418		return EFAULT;
419	}
420
421	if (!(ctx = pm->pm_ctx)) {
422		/* No context -- assign it one */
423		ctx_alloc(pm);
424		ctx = pm->pm_ctx;
425	}
426
427	asm volatile("addi %6,%6,1; mtctr %6;"	/* Set up counter */
428		"mfmsr %0;"			/* Save MSR */
429		"li %1,0x20; "
430		"andc %1,%0,%1; mtmsr %1;"	/* Disable IMMU */
431		"mfpid %1;"			/* Save old PID */
432		"sync; isync;"
433
434		"1: bdz 2f;"			/* while len */
435		"mtpid %3; sync;"		/* Load user ctx */
436		"lbz %2,0(%4); addi %4,%4,1;"	/* Load byte */
437		"sync; isync;"
438		"mtpid %1;sync;"
439		"stb %2,0(%5); dcbf 0,%5; addi %5,%5,1;" /* Store kernel byte */
440		"sync; isync;"
441		"b 1b;"				/* repeat */
442
443		"2: mtpid %1; mtmsr %0;"	/* Restore PID and MSR */
444		"sync; isync;"
445		: "=&r" (msr), "=&r" (pid), "=&r" (tmp)
446		: "r" (ctx), "r" (udaddr), "r" (kaddr), "r" (len));
447
448	curpcb->pcb_onfault = 0;
449	return 0;
450}
451
452static int
453bigcopyin(const void *udaddr, void *kaddr, size_t len)
454{
455	const char *up;
456	char *kp = kaddr;
457	struct lwp *l = curlwp;
458	struct proc *p;
459	int error;
460
461	if (!l) {
462		return EFAULT;
463	}
464
465	p = l->l_proc;
466
467	/*
468	 * Stolen from physio():
469	 */
470	PHOLD(l);
471	error = uvm_vslock(p, (caddr_t)udaddr, len, VM_PROT_READ);
472	if (error) {
473		PRELE(l);
474		return EFAULT;
475	}
476	up = (char *)vmaprange(p, (vaddr_t)udaddr, len, VM_PROT_READ);
477
478	memcpy(kp, up, len);
479	vunmaprange((vaddr_t)up, len);
480	uvm_vsunlock(p, (caddr_t)udaddr, len);
481	PRELE(l);
482
483	return 0;
484}
485
486int
487copyout(const void *kaddr, void *udaddr, size_t len)
488{
489	struct pmap *pm = curproc->p_vmspace->vm_map.pmap;
490	int msr, pid, tmp, ctx;
491	struct faultbuf env;
492
493	/* For big copies use more efficient routine */
494	if (len > 256) return (bigcopyout(kaddr, udaddr, len));
495
496	if (setfault(&env)) {
497		curpcb->pcb_onfault = 0;
498		return EFAULT;
499	}
500
501	if (!(ctx = pm->pm_ctx)) {
502		/* No context -- assign it one */
503		ctx_alloc(pm);
504		ctx = pm->pm_ctx;
505	}
506
507	asm volatile("addi %6,%6,1; mtctr %6;"	/* Set up counter */
508		"mfmsr %0;"			/* Save MSR */
509		"li %1,0x20; "
510		"andc %1,%0,%1; mtmsr %1;"	/* Disable IMMU */
511		"mfpid %1;"			/* Save old PID */
512		"sync; isync;"
513
514		"1: bdz 2f;"			/* while len */
515		"mtpid %1;sync;"
516		"lbz %2,0(%5); addi %5,%5,1;"	/* Load kernel byte */
517		"sync; isync;"
518		"mtpid %3; sync;"		/* Load user ctx */
519		"stb %2,0(%4);  dcbf 0,%4; addi %4,%4,1;" /* Store user byte */
520		"sync; isync;"
521		"b 1b;"				/* repeat */
522
523		"2: mtpid %1; mtmsr %0;"	/* Restore PID and MSR */
524		"sync; isync;"
525		: "=&r" (msr), "=&r" (pid), "=&r" (tmp)
526		: "r" (ctx), "r" (udaddr), "r" (kaddr), "r" (len));
527
528	curpcb->pcb_onfault = 0;
529	return 0;
530}
531
532static int
533bigcopyout(const void *kaddr, void *udaddr, size_t len)
534{
535	char *up;
536	const char *kp = (char *)kaddr;
537	struct lwp *l = curlwp;
538	struct proc *p;
539	int error;
540
541	if (!l) {
542		return EFAULT;
543	}
544
545	p = l->l_proc;
546
547	/*
548	 * Stolen from physio():
549	 */
550	PHOLD(l);
551	error = uvm_vslock(p, udaddr, len, VM_PROT_WRITE);
552	if (error) {
553		PRELE(l);
554		return EFAULT;
555	}
556	up = (char *)vmaprange(p, (vaddr_t)udaddr, len,
557	    VM_PROT_READ | VM_PROT_WRITE);
558
559	memcpy(up, kp, len);
560	vunmaprange((vaddr_t)up, len);
561	uvm_vsunlock(p, udaddr, len);
562	PRELE(l);
563
564	return 0;
565}
566
567/*
568 * kcopy(const void *src, void *dst, size_t len);
569 *
570 * Copy len bytes from src to dst, aborting if we encounter a fatal
571 * page fault.
572 *
573 * kcopy() _must_ save and restore the old fault handler since it is
574 * called by uiomove(), which may be in the path of servicing a non-fatal
575 * page fault.
576 */
577int
578kcopy(const void *src, void *dst, size_t len)
579{
580	struct faultbuf env, *oldfault;
581
582	oldfault = curpcb->pcb_onfault;
583	if (setfault(&env)) {
584		curpcb->pcb_onfault = oldfault;
585		return EFAULT;
586	}
587
588	memcpy(dst, src, len);
589
590	curpcb->pcb_onfault = oldfault;
591	return 0;
592}
593
594int
595badaddr(void *addr, size_t size)
596{
597
598	return badaddr_read(addr, size, NULL);
599}
600
601int
602badaddr_read(void *addr, size_t size, int *rptr)
603{
604	struct faultbuf env;
605	int x;
606
607	/* Get rid of any stale machine checks that have been waiting.  */
608	__asm __volatile ("sync; isync");
609
610	if (setfault(&env)) {
611		curpcb->pcb_onfault = 0;
612		__asm __volatile ("sync");
613		return 1;
614	}
615
616	__asm __volatile ("sync");
617
618	switch (size) {
619	case 1:
620		x = *(volatile int8_t *)addr;
621		break;
622	case 2:
623		x = *(volatile int16_t *)addr;
624		break;
625	case 4:
626		x = *(volatile int32_t *)addr;
627		break;
628	default:
629		panic("badaddr: invalid size (%d)", size);
630	}
631
632	/* Make sure we took the machine check, if we caused one. */
633	__asm __volatile ("sync; isync");
634
635	curpcb->pcb_onfault = 0;
636	__asm __volatile ("sync");	/* To be sure. */
637
638	/* Use the value to avoid reorder. */
639	if (rptr)
640		*rptr = x;
641
642	return 0;
643}
644
645/*
646 * For now, this only deals with the particular unaligned access case
647 * that gcc tends to generate.  Eventually it should handle all of the
648 * possibilities that can happen on a 32-bit PowerPC in big-endian mode.
649 */
650
651static int
652fix_unaligned(struct lwp *l, struct trapframe *frame)
653{
654
655	return -1;
656}
657
658/*
659 * Start a new LWP
660 */
661void
662startlwp(arg)
663	void *arg;
664{
665	int err;
666	ucontext_t *uc = arg;
667	struct lwp *l = curlwp;
668
669	err = cpu_setmcontext(l, &uc->uc_mcontext, uc->uc_flags);
670#if DIAGNOSTIC
671	if (err) {
672		printf("Error %d from cpu_setmcontext.", err);
673	}
674#endif
675	pool_put(&lwp_uc_pool, uc);
676
677	upcallret(l);
678}
679
680/*
681 * XXX This is a terrible name.
682 */
683void
684upcallret(l)
685	struct lwp *l;
686{
687	int sig;
688
689	/* Take pending signals. */
690	while ((sig = CURSIG(l)) != 0)
691		postsig(sig);
692
693	/* Invoke per-process kernel-exit handling, if any */
694	if (l->l_proc->p_userret)
695		(l->l_proc->p_userret)(l, l->l_proc->p_userret_arg);
696
697	/* Invoke any pending upcalls */
698	while (l->l_flag & L_SA_UPCALL)
699		sa_upcall_userret(l);
700
701	curcpu()->ci_schedstate.spc_curpriority = l->l_priority = l->l_usrpri;
702}
703