]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/kern_kse.c
This commit was generated by cvs2svn to compensate for changes in r170222,
[FreeBSD/FreeBSD.git] / sys / kern / kern_kse.c
1 /*-
2  * Copyright (C) 2001 Julian Elischer <julian@freebsd.org>.
3  *  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice(s), this list of conditions and the following disclaimer as
10  *    the first lines of this file unmodified other than the possible
11  *    addition of one or more copyright notices.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice(s), this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
26  * DAMAGE.
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/imgact.h>
36 #include <sys/lock.h>
37 #include <sys/mutex.h>
38 #include <sys/proc.h>
39 #include <sys/ptrace.h>
40 #include <sys/smp.h>
41 #include <sys/syscallsubr.h>
42 #include <sys/sysproto.h>
43 #include <sys/sched.h>
44 #include <sys/signalvar.h>
45 #include <sys/sleepqueue.h>
46 #include <sys/kse.h>
47 #include <sys/ktr.h>
48 #include <vm/uma.h>
49
50 #ifdef KSE
51 static uma_zone_t upcall_zone;
52
53 /* DEBUG ONLY */
54 extern int virtual_cpu;
55 extern int thread_debug;
56
57 extern int max_threads_per_proc;
58 extern int max_groups_per_proc;
59 extern int max_threads_hits;
60 extern struct mtx kse_zombie_lock;
61
62
63 TAILQ_HEAD(, kse_upcall) zombie_upcalls =
64         TAILQ_HEAD_INITIALIZER(zombie_upcalls);
65
66 static int thread_update_usr_ticks(struct thread *td);
67 static void thread_alloc_spare(struct thread *td);
68
69 struct kse_upcall *
70 upcall_alloc(void)
71 {
72         struct kse_upcall *ku;
73
74         ku = uma_zalloc(upcall_zone, M_WAITOK | M_ZERO);
75         return (ku);
76 }
77
78 void
79 upcall_free(struct kse_upcall *ku)
80 {
81
82         uma_zfree(upcall_zone, ku);
83 }
84
85 void
86 upcall_link(struct kse_upcall *ku, struct proc *p)
87 {
88
89         mtx_assert(&sched_lock, MA_OWNED);
90         TAILQ_INSERT_TAIL(&p->p_upcalls, ku, ku_link);
91         ku->ku_proc = p;
92 }
93
94 void
95 upcall_unlink(struct kse_upcall *ku)
96 {
97         struct proc *p = ku->ku_proc;
98
99         mtx_assert(&sched_lock, MA_OWNED);
100         KASSERT(ku->ku_owner == NULL, ("%s: have owner", __func__));
101         TAILQ_REMOVE(&p->p_upcalls, ku, ku_link);
102         upcall_stash(ku);
103 }
104
105 void
106 upcall_remove(struct thread *td)
107 {
108
109         mtx_assert(&sched_lock, MA_OWNED);
110         if (td->td_upcall != NULL) {
111                 /*
112                 * If we are not a bound thread then decrement the count of
113                 * possible upcall sources
114                 */
115                 if (td->td_pflags & TDP_SA) 
116                         td->td_proc->p_numupcalls--;
117                 td->td_upcall->ku_owner = NULL;
118                 upcall_unlink(td->td_upcall);
119                 td->td_upcall = NULL;
120         }
121 }
122 #endif
123
124 #ifndef _SYS_SYSPROTO_H_
125 struct kse_switchin_args {
126         struct kse_thr_mailbox *tmbx;
127         int flags;
128 };
129 #endif
130
131 int
132 kse_switchin(struct thread *td, struct kse_switchin_args *uap)
133 {
134 #ifdef KSE
135         struct kse_thr_mailbox tmbx;
136         struct kse_upcall *ku;
137         int error;
138
139         if ((ku = td->td_upcall) == NULL || TD_CAN_UNBIND(td))
140                 return (EINVAL);
141         error = (uap->tmbx == NULL) ? EINVAL : 0;
142         if (!error)
143                 error = copyin(uap->tmbx, &tmbx, sizeof(tmbx));
144         if (!error && (uap->flags & KSE_SWITCHIN_SETTMBX))
145                 error = (suword(&ku->ku_mailbox->km_curthread,
146                          (long)uap->tmbx) != 0 ? EINVAL : 0);
147         if (!error)
148                 error = set_mcontext(td, &tmbx.tm_context.uc_mcontext);
149         if (!error) {
150                 suword32(&uap->tmbx->tm_lwp, td->td_tid);
151                 if (uap->flags & KSE_SWITCHIN_SETTMBX) {
152                         td->td_mailbox = uap->tmbx;
153                         td->td_pflags |= TDP_CAN_UNBIND;
154                 }
155                 PROC_LOCK(td->td_proc);
156                 if (td->td_proc->p_flag & P_TRACED) {
157                         _PHOLD(td->td_proc);
158                         if (tmbx.tm_dflags & TMDF_SSTEP)
159                                 ptrace_single_step(td);
160                         else
161                                 ptrace_clear_single_step(td);
162                         if (tmbx.tm_dflags & TMDF_SUSPEND) {
163                                 mtx_lock_spin(&sched_lock);
164                                 /* fuword can block, check again */
165                                 if (td->td_upcall)
166                                         ku->ku_flags |= KUF_DOUPCALL;
167                                 mtx_unlock_spin(&sched_lock);
168                         }
169                         _PRELE(td->td_proc);
170                 }
171                 PROC_UNLOCK(td->td_proc);
172         }
173         return ((error == 0) ? EJUSTRETURN : error);
174 #else /* !KSE */
175         return (EOPNOTSUPP);
176 #endif
177 }
178
179 /*
180 struct kse_thr_interrupt_args {
181         struct kse_thr_mailbox * tmbx;
182         int cmd;
183         long data;
184 };
185 */
186 int
187 kse_thr_interrupt(struct thread *td, struct kse_thr_interrupt_args *uap)
188 {
189 #ifdef KSE
190         struct kse_execve_args args;
191         struct image_args iargs;
192         struct proc *p;
193         struct thread *td2;
194         struct kse_upcall *ku;
195         struct kse_thr_mailbox *tmbx;
196         uint32_t flags;
197         int error;
198
199         p = td->td_proc;
200
201         if (!(p->p_flag & P_SA))
202                 return (EINVAL);
203
204         switch (uap->cmd) {
205         case KSE_INTR_SENDSIG:
206                 if (uap->data < 0 || uap->data > _SIG_MAXSIG)
207                         return (EINVAL);
208         case KSE_INTR_INTERRUPT:
209         case KSE_INTR_RESTART:
210                 PROC_LOCK(p);
211                 mtx_lock_spin(&sched_lock);
212                 FOREACH_THREAD_IN_PROC(p, td2) {
213                         if (td2->td_mailbox == uap->tmbx)
214                                 break;
215                 }
216                 if (td2 == NULL) {
217                         mtx_unlock_spin(&sched_lock);
218                         PROC_UNLOCK(p);
219                         return (ESRCH);
220                 }
221                 if (uap->cmd == KSE_INTR_SENDSIG) {
222                         if (uap->data > 0) {
223                                 td2->td_flags &= ~TDF_INTERRUPT;
224                                 mtx_unlock_spin(&sched_lock);
225                                 tdsignal(p, td2, (int)uap->data, NULL);
226                         } else {
227                                 mtx_unlock_spin(&sched_lock);
228                         }
229                 } else {
230                         td2->td_flags |= TDF_INTERRUPT | TDF_ASTPENDING;
231                         if (TD_CAN_UNBIND(td2))
232                                 td2->td_upcall->ku_flags |= KUF_DOUPCALL;
233                         if (uap->cmd == KSE_INTR_INTERRUPT)
234                                 td2->td_intrval = EINTR;
235                         else
236                                 td2->td_intrval = ERESTART;
237                         if (TD_ON_SLEEPQ(td2) && (td2->td_flags & TDF_SINTR))
238                                 sleepq_abort(td2, td2->td_intrval);
239                         mtx_unlock_spin(&sched_lock);
240                 }
241                 PROC_UNLOCK(p);
242                 break;
243         case KSE_INTR_SIGEXIT:
244                 if (uap->data < 1 || uap->data > _SIG_MAXSIG)
245                         return (EINVAL);
246                 PROC_LOCK(p);
247                 sigexit(td, (int)uap->data);
248                 break;
249
250         case KSE_INTR_DBSUSPEND:
251                 /* this sub-function is only for bound thread */
252                 if (td->td_pflags & TDP_SA)
253                         return (EINVAL);
254                 ku = td->td_upcall;
255                 tmbx = (void *)fuword((void *)&ku->ku_mailbox->km_curthread);
256                 if (tmbx == NULL || tmbx == (void *)-1)
257                         return (EINVAL);
258                 flags = 0;
259                 while ((p->p_flag & P_TRACED) && !(p->p_flag & P_SINGLE_EXIT)) {
260                         flags = fuword32(&tmbx->tm_dflags);
261                         if (!(flags & TMDF_SUSPEND))
262                                 break;
263                         PROC_LOCK(p);
264                         mtx_lock_spin(&sched_lock);
265                         thread_stopped(p);
266                         thread_suspend_one(td);
267                         PROC_UNLOCK(p);
268                         mi_switch(SW_VOL, NULL);
269                         mtx_unlock_spin(&sched_lock);
270                 }
271                 return (0);
272
273         case KSE_INTR_EXECVE:
274                 error = copyin((void *)uap->data, &args, sizeof(args));
275                 if (error)
276                         return (error);
277                 error = exec_copyin_args(&iargs, args.path, UIO_USERSPACE,
278                     args.argv, args.envp);
279                 if (error == 0)
280                         error = kern_execve(td, &iargs, NULL);
281                 if (error == 0) {
282                         PROC_LOCK(p);
283                         SIGSETOR(td->td_siglist, args.sigpend);
284                         PROC_UNLOCK(p);
285                         kern_sigprocmask(td, SIG_SETMASK, &args.sigmask, NULL,
286                             0);
287                 }
288                 return (error);
289
290         default:
291                 return (EINVAL);
292         }
293         return (0);
294 #else /* !KSE */
295         return (EOPNOTSUPP);
296 #endif
297 }
298
299 /*
300 struct kse_exit_args {
301         register_t dummy;
302 };
303 */
304 int
305 kse_exit(struct thread *td, struct kse_exit_args *uap)
306 {
307 #ifdef KSE
308         struct proc *p;
309         struct kse_upcall *ku, *ku2;
310         int    error, count;
311
312         p = td->td_proc;
313         /* 
314          * Ensure that this is only called from the UTS
315          */
316         if ((ku = td->td_upcall) == NULL || TD_CAN_UNBIND(td))
317                 return (EINVAL);
318
319
320         /*
321          * Calculate the existing non-exiting upcalls in this process.
322          * If we are the last upcall but there are still other threads,
323          * then do not exit. We need the other threads to be able to 
324          * complete whatever they are doing.
325          * XXX This relies on the userland knowing what to do if we return.
326          * It may be a better choice to convert ourselves into a kse_release
327          * ( or similar) and wait in the kernel to be needed.
328          * XXX Where are those other threads? I suppose they are waiting in
329          * the kernel. We should wait for them all at the user boundary after
330          * turning into an exit.
331          */
332         count = 0;
333         PROC_LOCK(p);
334         mtx_lock_spin(&sched_lock);
335         FOREACH_UPCALL_IN_PROC(p, ku2) {
336                 if ((ku2->ku_flags & KUF_EXITING) == 0)
337                         count++;
338         }
339         if (count == 1 && (p->p_numthreads > 1)) {
340                 mtx_unlock_spin(&sched_lock);
341                 PROC_UNLOCK(p);
342                 return (EDEADLK);
343         }
344         ku->ku_flags |= KUF_EXITING;
345         mtx_unlock_spin(&sched_lock);
346         PROC_UNLOCK(p);
347
348         /* 
349          * Mark the UTS mailbox as having been finished with.
350          * If that fails then just go for a segfault.
351          * XXX need to check it that can be deliverred without a mailbox.
352          */
353         error = suword32(&ku->ku_mailbox->km_flags, ku->ku_mflags|KMF_DONE);
354         if (!(td->td_pflags & TDP_SA))
355                 if (suword32(&td->td_mailbox->tm_lwp, 0))
356                         error = EFAULT;
357         PROC_LOCK(p);
358         if (error)
359                 psignal(p, SIGSEGV);
360         sigqueue_flush(&td->td_sigqueue);
361         mtx_lock_spin(&sched_lock);
362         upcall_remove(td);
363         if (p->p_numthreads != 1) {
364                 thread_stopped(p);
365                 thread_exit();
366                 /* NOTREACHED */
367         }
368         /*
369          * This is the last thread. Just return to the user.
370          * Effectively we have left threading mode..
371          * The only real thing left to do is ensure that the
372          * scheduler sets out concurrency back to 1 as that may be a
373          * resource leak otherwise.
374          * This is an A[PB]I issue.. what SHOULD we do?
375          * One possibility is to return to the user. It may not cope well.
376          * The other possibility would be to let the process exit.
377          */
378         thread_unthread(td);
379         mtx_unlock_spin(&sched_lock);
380         PROC_UNLOCK(p);
381 #if 0
382         return (0);
383 #else
384         printf("kse_exit: called on last thread. Calling exit1()");
385         exit1(td, 0);
386 #endif
387 #else /* !KSE */
388         return (EOPNOTSUPP);
389 #endif
390 }
391
392 /*
393  * Either becomes an upcall or waits for an awakening event and
394  * then becomes an upcall. Only error cases return.
395  */
396 /*
397 struct kse_release_args {
398         struct timespec *timeout;
399 };
400 */
401 int
402 kse_release(struct thread *td, struct kse_release_args *uap)
403 {
404 #ifdef KSE
405         struct proc *p;
406         struct kse_upcall *ku;
407         struct timespec timeout;
408         struct timeval tv;
409         sigset_t sigset;
410         int error;
411
412         p = td->td_proc;
413         if ((ku = td->td_upcall) == NULL || TD_CAN_UNBIND(td)) {
414                 printf("kse_release: called outside of threading. exiting");
415                 exit1(td, 0);
416         }
417         if (uap->timeout != NULL) {
418                 if ((error = copyin(uap->timeout, &timeout, sizeof(timeout))))
419                         return (error);
420                 TIMESPEC_TO_TIMEVAL(&tv, &timeout);
421         }
422         if (td->td_pflags & TDP_SA)
423                 td->td_pflags |= TDP_UPCALLING;
424         else {
425                 ku->ku_mflags = fuword32(&ku->ku_mailbox->km_flags);
426                 if (ku->ku_mflags == -1) {
427                         PROC_LOCK(p);
428                         sigexit(td, SIGSEGV);
429                 }
430         }
431         PROC_LOCK(p);
432         if (ku->ku_mflags & KMF_WAITSIGEVENT) {
433                 /* UTS wants to wait for signal event */
434                 if (!(p->p_flag & P_SIGEVENT) &&
435                     !(ku->ku_flags & KUF_DOUPCALL)) {
436                         td->td_kflags |= TDK_KSERELSIG;
437                         error = msleep(&p->p_siglist, &p->p_mtx, PPAUSE|PCATCH,
438                             "ksesigwait", (uap->timeout ? tvtohz(&tv) : 0));
439                         td->td_kflags &= ~(TDK_KSERELSIG | TDK_WAKEUP);
440                 }
441                 p->p_flag &= ~P_SIGEVENT;
442                 sigset = p->p_siglist;
443                 PROC_UNLOCK(p);
444                 error = copyout(&sigset, &ku->ku_mailbox->km_sigscaught,
445                     sizeof(sigset));
446         } else {
447                 if ((ku->ku_flags & KUF_DOUPCALL) == 0 &&
448                     ((ku->ku_mflags & KMF_NOCOMPLETED) ||
449                      (p->p_completed == NULL))) {
450                         p->p_upsleeps++;
451                         td->td_kflags |= TDK_KSEREL;
452                         error = msleep(&p->p_completed, &p->p_mtx,
453                                 PPAUSE|PCATCH, "kserel",
454                                 (uap->timeout ? tvtohz(&tv) : 0));
455                         td->td_kflags &= ~(TDK_KSEREL | TDK_WAKEUP);
456                         p->p_upsleeps--;
457                 }
458                 PROC_UNLOCK(p);
459         }
460         if (ku->ku_flags & KUF_DOUPCALL) {
461                 mtx_lock_spin(&sched_lock);
462                 ku->ku_flags &= ~KUF_DOUPCALL;
463                 mtx_unlock_spin(&sched_lock);
464         }
465         return (0);
466 #else /* !KSE */
467         return (EOPNOTSUPP);
468 #endif
469 }
470
471 /* struct kse_wakeup_args {
472         struct kse_mailbox *mbx;
473 }; */
474 int
475 kse_wakeup(struct thread *td, struct kse_wakeup_args *uap)
476 {
477 #ifdef KSE
478         struct proc *p;
479         struct kse_upcall *ku;
480         struct thread *td2;
481
482         p = td->td_proc;
483         td2 = NULL;
484         ku = NULL;
485         /* KSE-enabled processes only, please. */
486         if (!(p->p_flag & P_SA))
487                 return (EINVAL);
488         PROC_LOCK(p);
489         mtx_lock_spin(&sched_lock);
490         if (uap->mbx) {
491                 FOREACH_UPCALL_IN_PROC(p, ku) {
492                         if (ku->ku_mailbox == uap->mbx)
493                                 break;
494                 }
495         } else {
496                 if (p->p_upsleeps) {
497                         mtx_unlock_spin(&sched_lock);
498                         wakeup(&p->p_completed);
499                         PROC_UNLOCK(p);
500                         return (0);
501                 }
502                 ku = TAILQ_FIRST(&p->p_upcalls);
503         }
504         if (ku == NULL) {
505                 mtx_unlock_spin(&sched_lock);
506                 PROC_UNLOCK(p);
507                 return (ESRCH);
508         }
509         if ((td2 = ku->ku_owner) == NULL) {
510                 mtx_unlock_spin(&sched_lock);
511                 panic("%s: no owner", __func__);
512         } else if (td2->td_kflags & (TDK_KSEREL | TDK_KSERELSIG)) {
513                 mtx_unlock_spin(&sched_lock);
514                 if (!(td2->td_kflags & TDK_WAKEUP)) {
515                         td2->td_kflags |= TDK_WAKEUP;
516                         if (td2->td_kflags & TDK_KSEREL)
517                                 sleepq_remove(td2, &p->p_completed);
518                         else
519                                 sleepq_remove(td2, &p->p_siglist);
520                 }
521         } else {
522                 ku->ku_flags |= KUF_DOUPCALL;
523                 mtx_unlock_spin(&sched_lock);
524         }
525         PROC_UNLOCK(p);
526         return (0);
527 #else /* !KSE */
528         return (EOPNOTSUPP);
529 #endif
530 }
531
532 /*
533  * newgroup == 0: first call: use current KSE, don't schedule an upcall
534  * All other situations, do allocate max new KSEs and schedule an upcall.
535  *
536  * XXX should be changed so that 'first' behaviour lasts for as long
537  * as you have not made a thread in this proc. i.e. as long as we do not have
538  * a mailbox..
539  */
540 /* struct kse_create_args {
541         struct kse_mailbox *mbx;
542         int newgroup;
543 }; */
544 int
545 kse_create(struct thread *td, struct kse_create_args *uap)
546 {
547 #ifdef KSE
548         struct proc *p;
549         struct kse_mailbox mbx;
550         struct kse_upcall *newku;
551         int err, ncpus, sa = 0, first = 0;
552         struct thread *newtd;
553
554         p = td->td_proc;
555
556         /*
557          * Processes using the other threading model can't
558          * suddenly start calling this one
559          * XXX  maybe...
560          */
561         if ((p->p_flag & (P_SA|P_HADTHREADS)) == P_HADTHREADS) {
562                 PROC_UNLOCK(p);
563                 return (EINVAL);
564         }
565         if (!(p->p_flag & P_SA)) {
566                 first = 1;
567                 p->p_flag |= P_SA|P_HADTHREADS;
568         }
569
570         if ((err = copyin(uap->mbx, &mbx, sizeof(mbx))))
571                 return (err);
572
573         ncpus = mp_ncpus;
574         if (virtual_cpu != 0)
575                 ncpus = virtual_cpu;
576         /*
577          * If the new UTS mailbox says that this
578          * will be a BOUND lwp, then it had better
579          * have its thread mailbox already there.
580          */
581         if ((mbx.km_flags & KMF_BOUND) || uap->newgroup) {
582                 /* It's a bound thread (1:1) */
583                 if (mbx.km_curthread == NULL) 
584                         return (EINVAL);
585                 ncpus = 1;
586                 if (!(uap->newgroup || first))
587                         return (EINVAL);
588         } else {
589                 /* It's an upcall capable thread */
590                 sa = TDP_SA;
591                 PROC_LOCK(p);
592                 /*
593                  * Limit it to NCPU upcall contexts per proc in any case.
594                  * numupcalls will soon be numkse or something
595                  * as it will represent the number of 
596                  * non-bound upcalls available.  (i.e. ones that can 
597                  * actually call up).
598                  */
599                 if (p->p_numupcalls >= ncpus) {
600                         PROC_UNLOCK(p);
601                         return (EPROCLIM);
602                 }
603                 p->p_numupcalls++;
604                 PROC_UNLOCK(p);
605         }
606
607         /* 
608          * Even bound LWPs get a mailbox and an upcall to hold it.
609          * XXX This should change.
610          */
611         newku = upcall_alloc();
612         newku->ku_mailbox = uap->mbx;
613         newku->ku_func = mbx.km_func;
614         bcopy(&mbx.km_stack, &newku->ku_stack, sizeof(stack_t));
615
616         /*
617          * For the first call this may not have been set.
618          * Of course nor may it actually be needed.
619          * thread_schedule_upcall() will look for it.
620          */
621         if (td->td_standin == NULL)
622                 thread_alloc_spare(td);
623         PROC_LOCK(p);
624         mtx_lock_spin(&sched_lock);
625         /*
626          * If we are the first time, and a normal thread,
627          * then transfer all the signals back to the 'process'.
628          * SA threading will make a special thread to handle them.
629          */
630         if (first) {
631                 sigqueue_move_set(&td->td_sigqueue, &p->p_sigqueue, 
632                         &td->td_sigqueue.sq_signals);
633                 SIGFILLSET(td->td_sigmask);
634                 SIG_CANTMASK(td->td_sigmask);
635         }
636
637         /*
638          * Make the new upcall available to the process.
639          * It may or may not use it, but it's available.
640          */
641         upcall_link(newku, p);
642         PROC_UNLOCK(p);
643         if (mbx.km_quantum)
644 /* XXX should this be in the thread? */
645                 p->p_upquantum = max(1, mbx.km_quantum / tick);
646
647         /*
648          * Each upcall structure has an owner thread, find which
649          * one owns it.
650          */
651         if (uap->newgroup) {
652                 /*
653                  * The newgroup parameter now means
654                  * "bound, non SA, system scope"
655                  * It is only used for the interrupt thread at the
656                  * moment I think.. (or system scope threads dopey).
657                  * We'll rename it later.
658                  */
659                 newtd = thread_schedule_upcall(td, newku);
660         } else {
661                 /*
662                  * If the current thread hasn't an upcall structure,
663                  * just assign the upcall to it.
664                  * It'll just return.
665                  */
666                 if (td->td_upcall == NULL) {
667                         newku->ku_owner = td;
668                         td->td_upcall = newku;
669                         newtd = td;
670                 } else {
671                         /*
672                          * Create a new upcall thread to own it.
673                          */
674                         newtd = thread_schedule_upcall(td, newku);
675                 }
676         }
677         mtx_unlock_spin(&sched_lock);
678
679         /*
680          * Let the UTS instance know its LWPID.
681          * It doesn't really care. But the debugger will.
682          * XXX warning.. remember that this moves.
683          */
684         suword32(&newku->ku_mailbox->km_lwp, newtd->td_tid);
685
686         /*
687          * In the same manner, if the UTS has a current user thread, 
688          * then it is also running on this LWP so set it as well.
689          * The library could do that of course.. but why not..
690          * XXX I'm not sure this can ever happen but ...
691          * XXX does the UTS ever set this in the mailbox before calling this?
692          */
693         if (mbx.km_curthread)
694                 suword32(&mbx.km_curthread->tm_lwp, newtd->td_tid);
695         
696         if (sa) {
697                 newtd->td_pflags |= TDP_SA;
698                 /* 
699                  * If we are starting a new thread, kick it off.
700                  */
701                 if (newtd != td) {
702                         mtx_lock_spin(&sched_lock);
703                         sched_add(newtd, SRQ_BORING);
704                         mtx_unlock_spin(&sched_lock);
705                 }
706         } else {
707                 newtd->td_pflags &= ~TDP_SA;
708
709                 /*
710                  * Since a library will use the mailbox pointer to 
711                  * identify even a bound thread, and the mailbox pointer
712                  * will never be allowed to change after this syscall
713                  * for a bound thread, set it here so the library can
714                  * find the thread after the syscall returns.
715                  */
716                 newtd->td_mailbox = mbx.km_curthread;
717
718                 if (newtd != td) {
719                         /*
720                          * If we did create a new thread then
721                          * make sure it goes to the right place
722                          * when it starts up, and make sure that it runs 
723                          * at full speed when it gets there. 
724                          * thread_schedule_upcall() copies all cpu state
725                          * to the new thread, so we should clear single step
726                          * flag here.
727                          */
728                         cpu_set_upcall_kse(newtd, newku->ku_func,
729                                 newku->ku_mailbox, &newku->ku_stack);
730                         PROC_LOCK(p);
731                         if (p->p_flag & P_TRACED) {
732                                 _PHOLD(p);
733                                 ptrace_clear_single_step(newtd);
734                                 _PRELE(p);
735                         }
736                         PROC_UNLOCK(p);
737                         mtx_lock_spin(&sched_lock);
738                         sched_add(newtd, SRQ_BORING);
739                         mtx_unlock_spin(&sched_lock);
740                 }
741         }
742         return (0);
743 #else /* !KSE */
744         return (EOPNOTSUPP);
745 #endif
746 }
747
748 #ifdef KSE
749 /*
750  * Initialize global thread allocation resources.
751  */
752 void
753 kseinit(void)
754 {
755
756         upcall_zone = uma_zcreate("UPCALL", sizeof(struct kse_upcall),
757             NULL, NULL, NULL, NULL, UMA_ALIGN_CACHE, 0);
758 }
759
760 /*
761  * Stash an embarasingly extra upcall into the zombie upcall queue.
762  */
763
764 void
765 upcall_stash(struct kse_upcall *ku)
766 {
767         mtx_lock_spin(&kse_zombie_lock);
768         TAILQ_INSERT_HEAD(&zombie_upcalls, ku, ku_link);
769         mtx_unlock_spin(&kse_zombie_lock);
770 }
771
772 /*
773  * Reap zombie kse resource.
774  */
775 void
776 kse_GC(void)
777 {
778         struct kse_upcall *ku_first, *ku_next;
779
780         /*
781          * Don't even bother to lock if none at this instant,
782          * we really don't care about the next instant..
783          */
784         if (!TAILQ_EMPTY(&zombie_upcalls)) {
785                 mtx_lock_spin(&kse_zombie_lock);
786                 ku_first = TAILQ_FIRST(&zombie_upcalls);
787                 if (ku_first)
788                         TAILQ_INIT(&zombie_upcalls);
789                 mtx_unlock_spin(&kse_zombie_lock);
790                 while (ku_first) {
791                         ku_next = TAILQ_NEXT(ku_first, ku_link);
792                         upcall_free(ku_first);
793                         ku_first = ku_next;
794                 }
795         }
796 }
797
798 /*
799  * Store the thread context in the UTS's mailbox.
800  * then add the mailbox at the head of a list we are building in user space.
801  * The list is anchored in the proc structure.
802  */
803 int
804 thread_export_context(struct thread *td, int willexit)
805 {
806         struct proc *p;
807         uintptr_t mbx;
808         void *addr;
809         int error = 0, sig;
810         mcontext_t mc;
811
812         p = td->td_proc;
813
814         /*
815          * Post sync signal, or process SIGKILL and SIGSTOP.
816          * For sync signal, it is only possible when the signal is not
817          * caught by userland or process is being debugged.
818          */
819         PROC_LOCK(p);
820         if (td->td_flags & TDF_NEEDSIGCHK) {
821                 mtx_lock_spin(&sched_lock);
822                 td->td_flags &= ~TDF_NEEDSIGCHK;
823                 mtx_unlock_spin(&sched_lock);
824                 mtx_lock(&p->p_sigacts->ps_mtx);
825                 while ((sig = cursig(td)) != 0)
826                         postsig(sig);
827                 mtx_unlock(&p->p_sigacts->ps_mtx);
828         }
829         if (willexit)
830                 SIGFILLSET(td->td_sigmask);
831         PROC_UNLOCK(p);
832
833         /* Export the user/machine context. */
834         get_mcontext(td, &mc, 0);
835         addr = (void *)(&td->td_mailbox->tm_context.uc_mcontext);
836         error = copyout(&mc, addr, sizeof(mcontext_t));
837         if (error)
838                 goto bad;
839
840         addr = (caddr_t)(&td->td_mailbox->tm_lwp);
841         if (suword32(addr, 0)) {
842                 error = EFAULT;
843                 goto bad;
844         }
845
846         /* Get address in latest mbox of list pointer */
847         addr = (void *)(&td->td_mailbox->tm_next);
848         /*
849          * Put the saved address of the previous first
850          * entry into this one
851          */
852         for (;;) {
853                 mbx = (uintptr_t)p->p_completed;
854                 if (suword(addr, mbx)) {
855                         error = EFAULT;
856                         goto bad;
857                 }
858                 PROC_LOCK(p);
859                 if (mbx == (uintptr_t)p->p_completed) {
860                         p->p_completed = td->td_mailbox;
861                         /*
862                          * The thread context may be taken away by
863                          * other upcall threads when we unlock
864                          * process lock. it's no longer valid to
865                          * use it again in any other places.
866                          */
867                         td->td_mailbox = NULL;
868                         PROC_UNLOCK(p);
869                         break;
870                 }
871                 PROC_UNLOCK(p);
872         }
873         td->td_usticks = 0;
874         return (0);
875
876 bad:
877         PROC_LOCK(p);
878         sigexit(td, SIGILL);
879         return (error);
880 }
881
882 /*
883  * Take the list of completed mailboxes for this Process and put them on this
884  * upcall's mailbox as it's the next one going up.
885  */
886 static int
887 thread_link_mboxes(struct proc *p, struct kse_upcall *ku)
888 {
889         void *addr;
890         uintptr_t mbx;
891
892         addr = (void *)(&ku->ku_mailbox->km_completed);
893         for (;;) {
894                 mbx = (uintptr_t)p->p_completed;
895                 if (suword(addr, mbx)) {
896                         PROC_LOCK(p);
897                         psignal(p, SIGSEGV);
898                         PROC_UNLOCK(p);
899                         return (EFAULT);
900                 }
901                 PROC_LOCK(p);
902                 if (mbx == (uintptr_t)p->p_completed) {
903                         p->p_completed = NULL;
904                         PROC_UNLOCK(p);
905                         break;
906                 }
907                 PROC_UNLOCK(p);
908         }
909         return (0);
910 }
911
912 /*
913  * This function should be called at statclock interrupt time
914  */
915 int
916 thread_statclock(int user)
917 {
918         struct thread *td = curthread;
919
920         if (!(td->td_pflags & TDP_SA))
921                 return (0);
922         if (user) {
923                 /* Current always do via ast() */
924                 mtx_lock_spin(&sched_lock);
925                 td->td_flags |= TDF_ASTPENDING;
926                 mtx_unlock_spin(&sched_lock);
927                 td->td_uuticks++;
928         } else if (td->td_mailbox != NULL)
929                 td->td_usticks++;
930         return (0);
931 }
932
933 /*
934  * Export state clock ticks for userland
935  */
936 static int
937 thread_update_usr_ticks(struct thread *td)
938 {
939         struct proc *p = td->td_proc;
940         caddr_t addr;
941         u_int uticks;
942
943         if (td->td_mailbox == NULL)
944                 return (-1);
945
946         if ((uticks = td->td_uuticks) != 0) {
947                 td->td_uuticks = 0;
948                 addr = (caddr_t)&td->td_mailbox->tm_uticks;
949                 if (suword32(addr, uticks+fuword32(addr)))
950                         goto error;
951         }
952         if ((uticks = td->td_usticks) != 0) {
953                 td->td_usticks = 0;
954                 addr = (caddr_t)&td->td_mailbox->tm_sticks;
955                 if (suword32(addr, uticks+fuword32(addr)))
956                         goto error;
957         }
958         return (0);
959
960 error:
961         PROC_LOCK(p);
962         psignal(p, SIGSEGV);
963         PROC_UNLOCK(p);
964         return (-2);
965 }
966
967 /*
968  * This function is intended to be used to initialize a spare thread
969  * for upcall. Initialize thread's large data area outside sched_lock
970  * for thread_schedule_upcall(). The crhold is also here to get it out
971  * from the schedlock as it has a mutex op itself.
972  * XXX BUG.. we need to get the cr ref after the thread has 
973  * checked and chenged its own, not 6 months before...  
974  */
975 void
976 thread_alloc_spare(struct thread *td)
977 {
978         struct thread *spare;
979
980         if (td->td_standin)
981                 return;
982         spare = thread_alloc();
983         td->td_standin = spare;
984         bzero(&spare->td_startzero,
985             __rangeof(struct thread, td_startzero, td_endzero));
986         spare->td_proc = td->td_proc;
987         spare->td_ucred = crhold(td->td_ucred);
988 }
989
990 /*
991  * Create a thread and schedule it for upcall on the KSE given.
992  * Use our thread's standin so that we don't have to allocate one.
993  */
994 struct thread *
995 thread_schedule_upcall(struct thread *td, struct kse_upcall *ku)
996 {
997         struct thread *td2;
998
999         mtx_assert(&sched_lock, MA_OWNED);
1000
1001         /*
1002          * Schedule an upcall thread on specified kse_upcall,
1003          * the kse_upcall must be free.
1004          * td must have a spare thread.
1005          */
1006         KASSERT(ku->ku_owner == NULL, ("%s: upcall has owner", __func__));
1007         if ((td2 = td->td_standin) != NULL) {
1008                 td->td_standin = NULL;
1009         } else {
1010                 panic("no reserve thread when scheduling an upcall");
1011                 return (NULL);
1012         }
1013         CTR3(KTR_PROC, "thread_schedule_upcall: thread %p (pid %d, %s)",
1014              td2, td->td_proc->p_pid, td->td_proc->p_comm);
1015         /*
1016          * Bzero already done in thread_alloc_spare() because we can't
1017          * do the crhold here because we are in schedlock already.
1018          */
1019         bcopy(&td->td_startcopy, &td2->td_startcopy,
1020             __rangeof(struct thread, td_startcopy, td_endcopy));
1021         thread_link(td2, ku->ku_proc);
1022         /* inherit parts of blocked thread's context as a good template */
1023         cpu_set_upcall(td2, td);
1024         /* Let the new thread become owner of the upcall */
1025         ku->ku_owner   = td2;
1026         td2->td_upcall = ku;
1027         td2->td_flags  = 0;
1028         td2->td_pflags = TDP_SA|TDP_UPCALLING;
1029         td2->td_state  = TDS_CAN_RUN;
1030         td2->td_inhibitors = 0;
1031         SIGFILLSET(td2->td_sigmask);
1032         SIG_CANTMASK(td2->td_sigmask);
1033         sched_fork_thread(td, td2);
1034         return (td2);   /* bogus.. should be a void function */
1035 }
1036
1037 /*
1038  * It is only used when thread generated a trap and process is being
1039  * debugged.
1040  */
1041 void
1042 thread_signal_add(struct thread *td, ksiginfo_t *ksi)
1043 {
1044         struct proc *p;
1045         struct sigacts *ps;
1046         int error;
1047
1048         p = td->td_proc;
1049         PROC_LOCK_ASSERT(p, MA_OWNED);
1050         ps = p->p_sigacts;
1051         mtx_assert(&ps->ps_mtx, MA_OWNED);
1052
1053         mtx_unlock(&ps->ps_mtx);
1054         SIGADDSET(td->td_sigmask, ksi->ksi_signo);
1055         PROC_UNLOCK(p);
1056         error = copyout(&ksi->ksi_info, &td->td_mailbox->tm_syncsig,
1057                         sizeof(siginfo_t));
1058         if (error) {
1059                 PROC_LOCK(p);
1060                 sigexit(td, SIGSEGV);
1061         }
1062         PROC_LOCK(p);
1063         mtx_lock(&ps->ps_mtx);
1064 }
1065 #include "opt_sched.h"
1066 struct thread *
1067 thread_switchout(struct thread *td, int flags, struct thread *nextthread)
1068 {
1069         struct kse_upcall *ku;
1070         struct thread *td2;
1071
1072         mtx_assert(&sched_lock, MA_OWNED);
1073
1074         /*
1075          * If the outgoing thread is in threaded group and has never
1076          * scheduled an upcall, decide whether this is a short
1077          * or long term event and thus whether or not to schedule
1078          * an upcall.
1079          * If it is a short term event, just suspend it in
1080          * a way that takes its KSE with it.
1081          * Select the events for which we want to schedule upcalls.
1082          * For now it's just sleep or if thread is suspended but
1083          * process wide suspending flag is not set (debugger
1084          * suspends thread).
1085          * XXXKSE eventually almost any inhibition could do.
1086          */
1087         if (TD_CAN_UNBIND(td) && (td->td_standin) &&
1088             (TD_ON_SLEEPQ(td) || (TD_IS_SUSPENDED(td) &&
1089              !P_SHOULDSTOP(td->td_proc)))) {
1090                 /*
1091                  * Release ownership of upcall, and schedule an upcall
1092                  * thread, this new upcall thread becomes the owner of
1093                  * the upcall structure. It will be ahead of us in the
1094                  * run queue, so as we are stopping, it should either
1095                  * start up immediatly, or at least before us if
1096                  * we release our slot.
1097                  */
1098                 ku = td->td_upcall;
1099                 ku->ku_owner = NULL;
1100                 td->td_upcall = NULL;
1101                 td->td_pflags &= ~TDP_CAN_UNBIND;
1102                 td2 = thread_schedule_upcall(td, ku);
1103                 if (flags & SW_INVOL || nextthread) {
1104                         sched_add(td2, SRQ_YIELDING);
1105                 } else {
1106                         /* Keep up with reality.. we have one extra thread 
1107                          * in the picture.. and it's 'running'.
1108                          */
1109                         return td2;
1110                 }
1111         }
1112         return (nextthread);
1113 }
1114
1115 /*
1116  * Setup done on the thread when it enters the kernel.
1117  */
1118 void
1119 thread_user_enter(struct thread *td)
1120 {
1121         struct proc *p = td->td_proc;
1122         struct kse_upcall *ku;
1123         struct kse_thr_mailbox *tmbx;
1124         uint32_t flags;
1125
1126         /*
1127          * First check that we shouldn't just abort. we
1128          * can suspend it here or just exit.
1129          */
1130         if (__predict_false(P_SHOULDSTOP(p))) {
1131                 PROC_LOCK(p);
1132                 thread_suspend_check(0);
1133                 PROC_UNLOCK(p);
1134         }
1135
1136         if (!(td->td_pflags & TDP_SA))
1137                 return;
1138
1139         /*
1140          * If we are doing a syscall in a KSE environment,
1141          * note where our mailbox is.
1142          */
1143
1144         ku = td->td_upcall;
1145
1146         KASSERT(ku != NULL, ("no upcall owned"));
1147         KASSERT(ku->ku_owner == td, ("wrong owner"));
1148         KASSERT(!TD_CAN_UNBIND(td), ("can unbind"));
1149
1150         if (td->td_standin == NULL)
1151                 thread_alloc_spare(td);
1152         ku->ku_mflags = fuword32((void *)&ku->ku_mailbox->km_flags);
1153         tmbx = (void *)fuword((void *)&ku->ku_mailbox->km_curthread);
1154         if ((tmbx == NULL) || (tmbx == (void *)-1L) ||
1155             (ku->ku_mflags & KMF_NOUPCALL)) {
1156                 td->td_mailbox = NULL;
1157         } else {
1158                 flags = fuword32(&tmbx->tm_flags);
1159                 /*
1160                  * On some architectures, TP register points to thread
1161                  * mailbox but not points to kse mailbox, and userland
1162                  * can not atomically clear km_curthread, but can
1163                  * use TP register, and set TMF_NOUPCALL in thread
1164                  * flag to indicate a critical region.
1165                  */
1166                 if (flags & TMF_NOUPCALL) {
1167                         td->td_mailbox = NULL;
1168                 } else {
1169                         td->td_mailbox = tmbx;
1170                         td->td_pflags |= TDP_CAN_UNBIND;
1171                         if (__predict_false(p->p_flag & P_TRACED)) {
1172                                 flags = fuword32(&tmbx->tm_dflags);
1173                                 if (flags & TMDF_SUSPEND) {
1174                                         mtx_lock_spin(&sched_lock);
1175                                         /* fuword can block, check again */
1176                                         if (td->td_upcall)
1177                                                 ku->ku_flags |= KUF_DOUPCALL;
1178                                         mtx_unlock_spin(&sched_lock);
1179                                 }
1180                         }
1181                 }
1182         }
1183 }
1184
1185 /*
1186  * The extra work we go through if we are a threaded process when we
1187  * return to userland.
1188  *
1189  * If we are a KSE process and returning to user mode, check for
1190  * extra work to do before we return (e.g. for more syscalls
1191  * to complete first).  If we were in a critical section, we should
1192  * just return to let it finish. Same if we were in the UTS (in
1193  * which case the mailbox's context's busy indicator will be set).
1194  * The only traps we suport will have set the mailbox.
1195  * We will clear it here.
1196  */
1197 int
1198 thread_userret(struct thread *td, struct trapframe *frame)
1199 {
1200         struct kse_upcall *ku;
1201         struct proc *p;
1202         struct timespec ts;
1203         int error = 0, uts_crit;
1204
1205         /* Nothing to do with bound thread */
1206         if (!(td->td_pflags & TDP_SA))
1207                 return (0);
1208
1209         /*
1210          * Update stat clock count for userland
1211          */
1212         if (td->td_mailbox != NULL) {
1213                 thread_update_usr_ticks(td);
1214                 uts_crit = 0;
1215         } else {
1216                 uts_crit = 1;
1217         }
1218
1219         p = td->td_proc;
1220         ku = td->td_upcall;
1221
1222         /*
1223          * Optimisation:
1224          * This thread has not started any upcall.
1225          * If there is no work to report other than ourself,
1226          * then it can return direct to userland.
1227          */
1228         if (TD_CAN_UNBIND(td)) {
1229                 td->td_pflags &= ~TDP_CAN_UNBIND;
1230                 if ((td->td_flags & TDF_NEEDSIGCHK) == 0 &&
1231                     (p->p_completed == NULL) &&
1232                     (ku->ku_flags & KUF_DOUPCALL) == 0 &&
1233                     (p->p_upquantum && ticks < p->p_nextupcall)) {
1234                         nanotime(&ts);
1235                         error = copyout(&ts,
1236                                 (caddr_t)&ku->ku_mailbox->km_timeofday,
1237                                 sizeof(ts));
1238                         td->td_mailbox = 0;
1239                         ku->ku_mflags = 0;
1240                         if (error)
1241                                 goto out;
1242                         return (0);
1243                 }
1244                 thread_export_context(td, 0);
1245                 /*
1246                  * There is something to report, and we own an upcall
1247                  * structure, we can go to userland.
1248                  * Turn ourself into an upcall thread.
1249                  */
1250                 td->td_pflags |= TDP_UPCALLING;
1251         } else if (td->td_mailbox && (ku == NULL)) {
1252                 thread_export_context(td, 1);
1253                 PROC_LOCK(p);
1254                 if (p->p_upsleeps)
1255                         wakeup(&p->p_completed);
1256                 WITNESS_WARN(WARN_PANIC, &p->p_mtx.lock_object,
1257                     "thread exiting in userret");
1258                 sigqueue_flush(&td->td_sigqueue);
1259                 mtx_lock_spin(&sched_lock);
1260                 thread_stopped(p);
1261                 thread_exit();
1262                 /* NOTREACHED */
1263         }
1264
1265         KASSERT(ku != NULL, ("upcall is NULL"));
1266         KASSERT(TD_CAN_UNBIND(td) == 0, ("can unbind"));
1267
1268         if (p->p_numthreads > max_threads_per_proc) {
1269                 max_threads_hits++;
1270                 PROC_LOCK(p);
1271                 mtx_lock_spin(&sched_lock);
1272                 p->p_maxthrwaits++;
1273                 while (p->p_numthreads > max_threads_per_proc) {
1274                         if (p->p_numupcalls >= max_threads_per_proc)
1275                                 break;
1276                         mtx_unlock_spin(&sched_lock);
1277                         if (msleep(&p->p_numthreads, &p->p_mtx, PPAUSE|PCATCH,
1278                             "maxthreads", hz/10) != EWOULDBLOCK) {
1279                                 mtx_lock_spin(&sched_lock);
1280                                 break;
1281                         } else {
1282                                 mtx_lock_spin(&sched_lock);
1283                         }
1284                 }
1285                 p->p_maxthrwaits--;
1286                 mtx_unlock_spin(&sched_lock);
1287                 PROC_UNLOCK(p);
1288         }
1289
1290         if (td->td_pflags & TDP_UPCALLING) {
1291                 uts_crit = 0;
1292                 p->p_nextupcall = ticks + p->p_upquantum;
1293                 /*
1294                  * There is no more work to do and we are going to ride
1295                  * this thread up to userland as an upcall.
1296                  * Do the last parts of the setup needed for the upcall.
1297                  */
1298                 CTR3(KTR_PROC, "userret: upcall thread %p (pid %d, %s)",
1299                     td, td->td_proc->p_pid, td->td_proc->p_comm);
1300
1301                 td->td_pflags &= ~TDP_UPCALLING;
1302                 if (ku->ku_flags & KUF_DOUPCALL) {
1303                         mtx_lock_spin(&sched_lock);
1304                         ku->ku_flags &= ~KUF_DOUPCALL;
1305                         mtx_unlock_spin(&sched_lock);
1306                 }
1307                 /*
1308                  * Set user context to the UTS
1309                  */
1310                 if (!(ku->ku_mflags & KMF_NOUPCALL)) {
1311                         cpu_set_upcall_kse(td, ku->ku_func, ku->ku_mailbox,
1312                                 &ku->ku_stack);
1313                         PROC_LOCK(p);
1314                         if (p->p_flag & P_TRACED) {
1315                                 _PHOLD(p);
1316                                 ptrace_clear_single_step(td);
1317                                 _PRELE(p);
1318                         }
1319                         PROC_UNLOCK(p);
1320                         error = suword32(&ku->ku_mailbox->km_lwp,
1321                                         td->td_tid);
1322                         if (error)
1323                                 goto out;
1324                         error = suword(&ku->ku_mailbox->km_curthread, 0);
1325                         if (error)
1326                                 goto out;
1327                 }
1328
1329                 /*
1330                  * Unhook the list of completed threads.
1331                  * anything that completes after this gets to
1332                  * come in next time.
1333                  * Put the list of completed thread mailboxes on
1334                  * this KSE's mailbox.
1335                  */
1336                 if (!(ku->ku_mflags & KMF_NOCOMPLETED) &&
1337                     (error = thread_link_mboxes(p, ku)) != 0)
1338                         goto out;
1339         }
1340         if (!uts_crit) {
1341                 nanotime(&ts);
1342                 error = copyout(&ts, &ku->ku_mailbox->km_timeofday, sizeof(ts));
1343         }
1344
1345 out:
1346         if (error) {
1347                 /*
1348                  * Things are going to be so screwed we should just kill
1349                  * the process.
1350                  * how do we do that?
1351                  */
1352                 PROC_LOCK(p);
1353                 psignal(p, SIGSEGV);
1354                 PROC_UNLOCK(p);
1355         } else {
1356                 /*
1357                  * Optimisation:
1358                  * Ensure that we have a spare thread available,
1359                  * for when we re-enter the kernel.
1360                  */
1361                 if (td->td_standin == NULL)
1362                         thread_alloc_spare(td);
1363         }
1364
1365         ku->ku_mflags = 0;
1366         td->td_mailbox = NULL;
1367         td->td_usticks = 0;
1368         return (error); /* go sync */
1369 }
1370
1371 /*
1372  * called after ptrace resumed a process, force all
1373  * virtual CPUs to schedule upcall for SA process,
1374  * because debugger may have changed something in userland,
1375  * we should notice UTS as soon as possible.
1376  */
1377 void
1378 thread_continued(struct proc *p)
1379 {
1380         struct kse_upcall *ku;
1381         struct thread *td;
1382
1383         PROC_LOCK_ASSERT(p, MA_OWNED);
1384         KASSERT(P_SHOULDSTOP(p), ("process not stopped"));
1385
1386         if (!(p->p_flag & P_SA))
1387                 return;
1388
1389         if (p->p_flag & P_TRACED) {
1390                 td = TAILQ_FIRST(&p->p_threads);
1391                 if (td && (td->td_pflags & TDP_SA)) {
1392                         FOREACH_UPCALL_IN_PROC(p, ku) {
1393                                 mtx_lock_spin(&sched_lock);
1394                                 ku->ku_flags |= KUF_DOUPCALL;
1395                                 mtx_unlock_spin(&sched_lock);
1396                                 wakeup(&p->p_completed);
1397                         }
1398                 }
1399         }
1400 }
1401 #endif