2 * Copyright (C) 2001 Julian Elischer <julian@freebsd.org>.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/imgact.h>
37 #include <sys/mutex.h>
39 #include <sys/ptrace.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/syslog.h>
52 static uma_zone_t upcall_zone;
55 extern int virtual_cpu;
56 extern int thread_debug;
58 extern int max_threads_per_proc;
59 extern int max_groups_per_proc;
60 extern int max_threads_hits;
61 extern struct mtx kse_lock;
64 TAILQ_HEAD(, kse_upcall) zombie_upcalls =
65 TAILQ_HEAD_INITIALIZER(zombie_upcalls);
67 static int thread_update_usr_ticks(struct thread *td);
68 static int thread_alloc_spare(struct thread *td);
69 static struct thread *thread_schedule_upcall(struct thread *td, struct kse_upcall *ku);
70 static struct kse_upcall *upcall_alloc(void);
74 MTX_SYSINIT(kse_lock, &kse_lock, "kse lock", MTX_SPIN);
79 struct kse_upcall *ku;
81 ku = uma_zalloc(upcall_zone, M_WAITOK | M_ZERO);
88 TAILQ_HEAD(, kse_upcall) zupcalls;
89 struct kse_upcall *ku_item, *ku_tmp;
91 TAILQ_INIT(&zupcalls);
92 mtx_lock_spin(&kse_lock);
93 if (!TAILQ_EMPTY(&zombie_upcalls)) {
94 TAILQ_CONCAT(&zupcalls, &zombie_upcalls, ku_link);
95 TAILQ_INIT(&zombie_upcalls);
97 mtx_unlock_spin(&kse_lock);
98 TAILQ_FOREACH_SAFE(ku_item, &zupcalls, ku_link, ku_tmp)
99 uma_zfree(upcall_zone, ku_item);
103 upcall_remove(struct thread *td)
106 PROC_SLOCK_ASSERT(td->td_proc, MA_OWNED);
107 THREAD_LOCK_ASSERT(td, MA_OWNED);
108 if (td->td_upcall != NULL) {
110 * If we are not a bound thread then decrement the count of
111 * possible upcall sources
113 if (td->td_pflags & TDP_SA)
114 td->td_proc->p_numupcalls--;
115 mtx_lock_spin(&kse_lock);
116 td->td_upcall->ku_owner = NULL;
117 TAILQ_REMOVE(&td->td_upcall->ku_proc->p_upcalls, td->td_upcall,
119 TAILQ_INSERT_HEAD(&zombie_upcalls, td->td_upcall, ku_link);
120 mtx_unlock_spin(&kse_lock);
121 td->td_upcall = NULL;
126 #ifndef _SYS_SYSPROTO_H_
127 struct kse_switchin_args {
128 struct kse_thr_mailbox *tmbx;
135 kse_unlink(struct thread *td)
137 mtx_lock_spin(&kse_lock);
139 mtx_unlock_spin(&kse_lock);
145 kse_switchin(struct thread *td, struct kse_switchin_args *uap)
148 struct kse_thr_mailbox tmbx, *tmbxp;
149 struct kse_upcall *ku;
153 * Put the arguments in local variables, to allow uap to
154 * point into the trapframe. We clobber the trapframe as
155 * part of setting a new context.
161 if ((ku = td->td_upcall) == NULL || TD_CAN_UNBIND(td)) {
166 error = (tmbxp == NULL) ? EINVAL : 0;
168 error = copyin(tmbxp, &tmbx, sizeof(tmbx));
169 if (!error && (flags & KSE_SWITCHIN_SETTMBX))
170 error = (suword(&ku->ku_mailbox->km_curthread,
171 (long)tmbxp) != 0 ? EINVAL : 0);
173 error = set_mcontext(td, &tmbx.tm_context.uc_mcontext);
175 suword32(&tmbxp->tm_lwp, td->td_tid);
176 if (flags & KSE_SWITCHIN_SETTMBX) {
177 td->td_mailbox = tmbxp;
178 td->td_pflags |= TDP_CAN_UNBIND;
180 PROC_LOCK(td->td_proc);
181 if (td->td_proc->p_flag & P_TRACED) {
183 if (tmbx.tm_dflags & TMDF_SSTEP)
184 ptrace_single_step(td);
186 ptrace_clear_single_step(td);
187 if (tmbx.tm_dflags & TMDF_SUSPEND) {
189 /* fuword can block, check again */
191 ku->ku_flags |= KUF_DOUPCALL;
196 PROC_UNLOCK(td->td_proc);
198 return ((error == 0) ? EJUSTRETURN : error);
205 struct kse_thr_interrupt_args {
206 struct kse_thr_mailbox * tmbx;
212 kse_thr_interrupt(struct thread *td, struct kse_thr_interrupt_args *uap)
215 struct kse_execve_args args;
216 struct image_args iargs;
219 struct kse_upcall *ku;
220 struct kse_thr_mailbox *tmbx;
227 if (!(p->p_flag & P_SA)) {
234 case KSE_INTR_SENDSIG:
235 if (uap->data < 0 || uap->data > _SIG_MAXSIG)
237 case KSE_INTR_INTERRUPT:
238 case KSE_INTR_RESTART:
241 FOREACH_THREAD_IN_PROC(p, td2) {
242 if (td2->td_mailbox == uap->tmbx)
252 if (uap->cmd == KSE_INTR_SENDSIG) {
254 td2->td_flags &= ~TDF_INTERRUPT;
256 tdsignal(p, td2, (int)uap->data, NULL);
261 td2->td_flags |= TDF_INTERRUPT | TDF_ASTPENDING;
262 if (TD_CAN_UNBIND(td2))
263 td2->td_upcall->ku_flags |= KUF_DOUPCALL;
264 if (uap->cmd == KSE_INTR_INTERRUPT)
265 td2->td_intrval = EINTR;
267 td2->td_intrval = ERESTART;
268 if (TD_ON_SLEEPQ(td2) && (td2->td_flags & TDF_SINTR))
269 sleepq_abort(td2, td2->td_intrval);
274 case KSE_INTR_SIGEXIT:
275 if (uap->data < 1 || uap->data > _SIG_MAXSIG)
278 sigexit(td, (int)uap->data);
281 case KSE_INTR_DBSUSPEND:
282 /* this sub-function is only for bound thread */
283 if (td->td_pflags & TDP_SA)
288 tmbx = (void *)fuword((void *)&ku->ku_mailbox->km_curthread);
289 if (tmbx == NULL || tmbx == (void *)-1)
293 while ((p->p_flag & P_TRACED) && !(p->p_flag & P_SINGLE_EXIT)) {
294 flags = fuword32(&tmbx->tm_dflags);
295 if (!(flags & TMDF_SUSPEND))
301 thread_suspend_one(td);
303 mi_switch(SW_VOL, NULL);
310 case KSE_INTR_EXECVE:
311 error = copyin((void *)uap->data, &args, sizeof(args));
314 error = exec_copyin_args(&iargs, args.path, UIO_USERSPACE,
315 args.argv, args.envp);
317 error = kern_execve(td, &iargs, NULL);
320 SIGSETOR(td->td_siglist, args.sigpend);
322 kern_sigprocmask(td, SIG_SETMASK, &args.sigmask, NULL,
337 struct kse_exit_args {
342 kse_exit(struct thread *td, struct kse_exit_args *uap)
346 struct kse_upcall *ku, *ku2;
351 * Ensure that this is only called from the UTS
354 if ((ku = td->td_upcall) == NULL || TD_CAN_UNBIND(td)) {
361 * Calculate the existing non-exiting upcalls in this process.
362 * If we are the last upcall but there are still other threads,
363 * then do not exit. We need the other threads to be able to
364 * complete whatever they are doing.
365 * XXX This relies on the userland knowing what to do if we return.
366 * It may be a better choice to convert ourselves into a kse_release
367 * ( or similar) and wait in the kernel to be needed.
368 * XXX Where are those other threads? I suppose they are waiting in
369 * the kernel. We should wait for them all at the user boundary after
370 * turning into an exit.
375 FOREACH_UPCALL_IN_PROC(p, ku2) {
376 if ((ku2->ku_flags & KUF_EXITING) == 0)
379 if (count == 1 && (p->p_numthreads > 1)) {
384 ku->ku_flags |= KUF_EXITING;
389 * Mark the UTS mailbox as having been finished with.
390 * If that fails then just go for a segfault.
391 * XXX need to check it that can be deliverred without a mailbox.
393 error = suword32(&ku->ku_mailbox->km_flags, ku->ku_mflags|KMF_DONE);
394 if (!(td->td_pflags & TDP_SA))
395 if (suword32(&td->td_mailbox->tm_lwp, 0))
400 sigqueue_flush(&td->td_sigqueue);
405 if (p->p_numthreads != 1) {
411 * This is the last thread. Just return to the user.
412 * Effectively we have left threading mode..
413 * The only real thing left to do is ensure that the
414 * scheduler sets out concurrency back to 1 as that may be a
415 * resource leak otherwise.
416 * This is an A[PB]I issue.. what SHOULD we do?
417 * One possibility is to return to the user. It may not cope well.
418 * The other possibility would be to let the process exit.
426 printf("kse_exit: called on last thread. Calling exit1()");
435 * Either becomes an upcall or waits for an awakening event and
436 * then becomes an upcall. Only error cases return.
439 struct kse_release_args {
440 struct timespec *timeout;
444 kse_release(struct thread *td, struct kse_release_args *uap)
448 struct kse_upcall *ku;
449 struct timespec timeout;
456 if ((ku = td->td_upcall) == NULL || TD_CAN_UNBIND(td)) {
458 printf("kse_release: called outside of threading. exiting");
462 if (uap->timeout != NULL) {
463 if ((error = copyin(uap->timeout, &timeout, sizeof(timeout))))
465 TIMESPEC_TO_TIMEVAL(&tv, &timeout);
467 if (td->td_pflags & TDP_SA)
468 td->td_pflags |= TDP_UPCALLING;
470 ku->ku_mflags = fuword32(&ku->ku_mailbox->km_flags);
471 if (ku->ku_mflags == -1) {
473 sigexit(td, SIGSEGV);
477 if (ku->ku_mflags & KMF_WAITSIGEVENT) {
478 /* UTS wants to wait for signal event */
479 if (!(p->p_flag & P_SIGEVENT) &&
480 !(ku->ku_flags & KUF_DOUPCALL)) {
481 td->td_kflags |= TDK_KSERELSIG;
482 error = msleep(&p->p_siglist, &p->p_mtx, PPAUSE|PCATCH,
483 "ksesigwait", (uap->timeout ? tvtohz(&tv) : 0));
484 td->td_kflags &= ~(TDK_KSERELSIG | TDK_WAKEUP);
486 p->p_flag &= ~P_SIGEVENT;
487 sigset = p->p_siglist;
489 error = copyout(&sigset, &ku->ku_mailbox->km_sigscaught,
492 if ((ku->ku_flags & KUF_DOUPCALL) == 0 &&
493 ((ku->ku_mflags & KMF_NOCOMPLETED) ||
494 (p->p_completed == NULL))) {
496 td->td_kflags |= TDK_KSEREL;
497 error = msleep(&p->p_completed, &p->p_mtx,
498 PPAUSE|PCATCH, "kserel",
499 (uap->timeout ? tvtohz(&tv) : 0));
500 td->td_kflags &= ~(TDK_KSEREL | TDK_WAKEUP);
505 if (ku->ku_flags & KUF_DOUPCALL) {
507 ku->ku_flags &= ~KUF_DOUPCALL;
516 /* struct kse_wakeup_args {
517 struct kse_mailbox *mbx;
520 kse_wakeup(struct thread *td, struct kse_wakeup_args *uap)
524 struct kse_upcall *ku;
530 /* KSE-enabled processes only, please. */
532 if (!(p->p_flag & P_SA)) {
538 FOREACH_UPCALL_IN_PROC(p, ku) {
539 if (ku->ku_mailbox == uap->mbx)
545 wakeup(&p->p_completed);
549 ku = TAILQ_FIRST(&p->p_upcalls);
556 mtx_lock_spin(&kse_lock);
557 if ((td2 = ku->ku_owner) == NULL) {
558 mtx_unlock_spin(&kse_lock);
561 panic("%s: no owner", __func__);
562 } else if (td2->td_kflags & (TDK_KSEREL | TDK_KSERELSIG)) {
563 mtx_unlock_spin(&kse_lock);
564 if (!(td2->td_kflags & TDK_WAKEUP)) {
565 td2->td_kflags |= TDK_WAKEUP;
566 if (td2->td_kflags & TDK_KSEREL)
567 sleepq_remove(td2, &p->p_completed);
569 sleepq_remove(td2, &p->p_siglist);
572 ku->ku_flags |= KUF_DOUPCALL;
573 mtx_unlock_spin(&kse_lock);
584 * newgroup == 0: first call: use current KSE, don't schedule an upcall
585 * All other situations, do allocate max new KSEs and schedule an upcall.
587 * XXX should be changed so that 'first' behaviour lasts for as long
588 * as you have not made a thread in this proc. i.e. as long as we do not have
591 /* struct kse_create_args {
592 struct kse_mailbox *mbx;
596 kse_create(struct thread *td, struct kse_create_args *uap)
600 struct kse_mailbox mbx;
601 struct kse_upcall *newku;
602 int err, ncpus, sa = 0, first = 0;
603 struct thread *newtd;
608 * Processes using the other threading model can't
609 * suddenly start calling this one
613 if ((p->p_flag & (P_SA|P_HADTHREADS)) == P_HADTHREADS) {
617 if (!(p->p_flag & P_SA)) {
619 p->p_flag |= P_SA|P_HADTHREADS;
623 if ((err = copyin(uap->mbx, &mbx, sizeof(mbx))))
627 if (virtual_cpu != 0)
630 * If the new UTS mailbox says that this
631 * will be a BOUND lwp, then it had better
632 * have its thread mailbox already there.
634 if ((mbx.km_flags & KMF_BOUND) || uap->newgroup) {
635 /* It's a bound thread (1:1) */
636 if (mbx.km_curthread == NULL)
639 if (!(uap->newgroup || first))
642 /* It's an upcall capable thread */
646 * Limit it to NCPU upcall contexts per proc in any case.
647 * numupcalls will soon be numkse or something
648 * as it will represent the number of
649 * non-bound upcalls available. (i.e. ones that can
652 if (p->p_numupcalls >= ncpus) {
661 * For the first call this may not have been set.
662 * Of course nor may it actually be needed.
663 * thread_schedule_upcall() will look for it.
665 if (td->td_standin == NULL) {
666 if (!thread_alloc_spare(td))
671 * Even bound LWPs get a mailbox and an upcall to hold it.
672 * XXX This should change.
674 newku = upcall_alloc();
675 newku->ku_mailbox = uap->mbx;
676 newku->ku_func = mbx.km_func;
677 bcopy(&mbx.km_stack, &newku->ku_stack, sizeof(stack_t));
682 * If we are the first time, and a normal thread,
683 * then transfer all the signals back to the 'process'.
684 * SA threading will make a special thread to handle them.
687 sigqueue_move_set(&td->td_sigqueue, &p->p_sigqueue,
688 &td->td_sigqueue.sq_signals);
689 SIGFILLSET(td->td_sigmask);
690 SIG_CANTMASK(td->td_sigmask);
694 * Make the new upcall available to the process.
695 * It may or may not use it, but it's available.
697 TAILQ_INSERT_TAIL(&p->p_upcalls, newku, ku_link);
701 /* XXX should this be in the thread? */
702 p->p_upquantum = max(1, mbx.km_quantum / tick);
705 * Each upcall structure has an owner thread, find which
709 mtx_lock_spin(&kse_lock);
712 * The newgroup parameter now means
713 * "bound, non SA, system scope"
714 * It is only used for the interrupt thread at the
715 * moment I think.. (or system scope threads dopey).
716 * We'll rename it later.
718 newtd = thread_schedule_upcall(td, newku);
721 * If the current thread hasn't an upcall structure,
722 * just assign the upcall to it.
725 if (td->td_upcall == NULL) {
726 newku->ku_owner = td;
727 td->td_upcall = newku;
731 * Create a new upcall thread to own it.
733 newtd = thread_schedule_upcall(td, newku);
736 mtx_unlock_spin(&kse_lock);
741 * Let the UTS instance know its LWPID.
742 * It doesn't really care. But the debugger will.
743 * XXX warning.. remember that this moves.
745 suword32(&newku->ku_mailbox->km_lwp, newtd->td_tid);
748 * In the same manner, if the UTS has a current user thread,
749 * then it is also running on this LWP so set it as well.
750 * The library could do that of course.. but why not..
751 * XXX I'm not sure this can ever happen but ...
752 * XXX does the UTS ever set this in the mailbox before calling this?
754 if (mbx.km_curthread)
755 suword32(&mbx.km_curthread->tm_lwp, newtd->td_tid);
758 newtd->td_pflags |= TDP_SA;
760 * If we are starting a new thread, kick it off.
764 sched_add(newtd, SRQ_BORING);
765 thread_unlock(newtd);
768 newtd->td_pflags &= ~TDP_SA;
771 * Since a library will use the mailbox pointer to
772 * identify even a bound thread, and the mailbox pointer
773 * will never be allowed to change after this syscall
774 * for a bound thread, set it here so the library can
775 * find the thread after the syscall returns.
777 newtd->td_mailbox = mbx.km_curthread;
781 * If we did create a new thread then
782 * make sure it goes to the right place
783 * when it starts up, and make sure that it runs
784 * at full speed when it gets there.
785 * thread_schedule_upcall() copies all cpu state
786 * to the new thread, so we should clear single step
789 cpu_set_upcall_kse(newtd, newku->ku_func,
790 newku->ku_mailbox, &newku->ku_stack);
792 if (p->p_flag & P_TRACED) {
794 ptrace_clear_single_step(newtd);
799 sched_add(newtd, SRQ_BORING);
800 thread_unlock(newtd);
811 * Initialize global thread allocation resources.
817 upcall_zone = uma_zcreate("UPCALL", sizeof(struct kse_upcall),
818 NULL, NULL, NULL, NULL, UMA_ALIGN_CACHE, 0);
822 * Store the thread context in the UTS's mailbox.
823 * then add the mailbox at the head of a list we are building in user space.
824 * The list is anchored in the proc structure.
827 thread_export_context(struct thread *td, int willexit)
838 * Post sync signal, or process SIGKILL and SIGSTOP.
839 * For sync signal, it is only possible when the signal is not
840 * caught by userland or process is being debugged.
843 if (td->td_flags & TDF_NEEDSIGCHK) {
845 td->td_flags &= ~TDF_NEEDSIGCHK;
847 mtx_lock(&p->p_sigacts->ps_mtx);
848 while ((sig = cursig(td)) != 0)
850 mtx_unlock(&p->p_sigacts->ps_mtx);
853 SIGFILLSET(td->td_sigmask);
856 /* Export the user/machine context. */
857 get_mcontext(td, &mc, 0);
858 addr = (void *)(&td->td_mailbox->tm_context.uc_mcontext);
859 error = copyout(&mc, addr, sizeof(mcontext_t));
863 addr = (caddr_t)(&td->td_mailbox->tm_lwp);
864 if (suword32(addr, 0)) {
869 /* Get address in latest mbox of list pointer */
870 addr = (void *)(&td->td_mailbox->tm_next);
872 * Put the saved address of the previous first
873 * entry into this one
876 mbx = (uintptr_t)p->p_completed;
877 if (suword(addr, mbx)) {
882 if (mbx == (uintptr_t)p->p_completed) {
884 p->p_completed = td->td_mailbox;
886 * The thread context may be taken away by
887 * other upcall threads when we unlock
888 * process lock. it's no longer valid to
889 * use it again in any other places.
891 td->td_mailbox = NULL;
908 * Take the list of completed mailboxes for this Process and put them on this
909 * upcall's mailbox as it's the next one going up.
912 thread_link_mboxes(struct proc *p, struct kse_upcall *ku)
917 addr = (void *)(&ku->ku_mailbox->km_completed);
919 mbx = (uintptr_t)p->p_completed;
920 if (suword(addr, mbx)) {
927 if (mbx == (uintptr_t)p->p_completed) {
928 p->p_completed = NULL;
938 * This function should be called at statclock interrupt time
941 thread_statclock(int user)
943 struct thread *td = curthread;
945 if (!(td->td_pflags & TDP_SA))
948 /* Current always do via ast() */
950 td->td_flags |= TDF_ASTPENDING;
953 } else if (td->td_mailbox != NULL)
959 * Export state clock ticks for userland
962 thread_update_usr_ticks(struct thread *td)
964 struct proc *p = td->td_proc;
969 if (td->td_mailbox == NULL) {
975 if ((uticks = td->td_uuticks) != 0) {
977 addr = (caddr_t)&td->td_mailbox->tm_uticks;
978 if (suword32(addr, uticks+fuword32(addr)))
981 if ((uticks = td->td_usticks) != 0) {
983 addr = (caddr_t)&td->td_mailbox->tm_sticks;
984 if (suword32(addr, uticks+fuword32(addr)))
997 * This function is intended to be used to initialize a spare thread
998 * for upcall. Initialize thread's large data area outside the thread lock
999 * for thread_schedule_upcall(). The crhold is also here to get it out
1000 * from the schedlock as it has a mutex op itself.
1001 * XXX BUG.. we need to get the cr ref after the thread has
1002 * checked and chenged its own, not 6 months before...
1005 thread_alloc_spare(struct thread *td)
1007 struct thread *spare;
1011 spare = thread_alloc();
1014 td->td_standin = spare;
1015 bzero(&spare->td_startzero,
1016 __rangeof(struct thread, td_startzero, td_endzero));
1017 spare->td_proc = td->td_proc;
1018 spare->td_ucred = crhold(td->td_ucred);
1019 spare->td_flags = TDF_INMEM;
1024 * Create a thread and schedule it for upcall on the KSE given.
1025 * Use our thread's standin so that we don't have to allocate one.
1028 thread_schedule_upcall(struct thread *td, struct kse_upcall *ku)
1032 THREAD_LOCK_ASSERT(td, MA_OWNED);
1033 mtx_assert(&kse_lock, MA_OWNED);
1035 * Schedule an upcall thread on specified kse_upcall,
1036 * the kse_upcall must be free.
1037 * td must have a spare thread.
1039 KASSERT(ku->ku_owner == NULL, ("%s: upcall has owner", __func__));
1040 if ((td2 = td->td_standin) != NULL) {
1041 td->td_standin = NULL;
1043 panic("no reserve thread when scheduling an upcall");
1046 CTR3(KTR_PROC, "thread_schedule_upcall: thread %p (pid %d, %s)",
1047 td2, td->td_proc->p_pid, td->td_proc->p_comm);
1049 * Bzero already done in thread_alloc_spare() because we can't
1050 * do the crhold here because we are in schedlock already.
1052 bcopy(&td->td_startcopy, &td2->td_startcopy,
1053 __rangeof(struct thread, td_startcopy, td_endcopy));
1054 sched_fork_thread(td, td2);
1055 thread_link(td2, ku->ku_proc);
1056 /* inherit parts of blocked thread's context as a good template */
1057 cpu_set_upcall(td2, td);
1058 /* Let the new thread become owner of the upcall */
1060 td2->td_upcall = ku;
1061 td2->td_pflags = TDP_SA|TDP_UPCALLING;
1062 td2->td_state = TDS_CAN_RUN;
1063 td2->td_inhibitors = 0;
1064 SIGFILLSET(td2->td_sigmask);
1065 SIG_CANTMASK(td2->td_sigmask);
1066 return (td2); /* bogus.. should be a void function */
1070 * It is only used when thread generated a trap and process is being
1074 thread_signal_add(struct thread *td, ksiginfo_t *ksi)
1081 PROC_LOCK_ASSERT(p, MA_OWNED);
1083 mtx_assert(&ps->ps_mtx, MA_OWNED);
1085 mtx_unlock(&ps->ps_mtx);
1086 SIGADDSET(td->td_sigmask, ksi->ksi_signo);
1088 error = copyout(&ksi->ksi_info, &td->td_mailbox->tm_syncsig,
1092 sigexit(td, SIGSEGV);
1095 mtx_lock(&ps->ps_mtx);
1097 #include "opt_sched.h"
1099 thread_switchout(struct thread *td, int flags, struct thread *nextthread)
1101 struct kse_upcall *ku;
1104 THREAD_LOCK_ASSERT(td, MA_OWNED);
1107 * If the outgoing thread is in threaded group and has never
1108 * scheduled an upcall, decide whether this is a short
1109 * or long term event and thus whether or not to schedule
1111 * If it is a short term event, just suspend it in
1112 * a way that takes its KSE with it.
1113 * Select the events for which we want to schedule upcalls.
1114 * For now it's just sleep or if thread is suspended but
1115 * process wide suspending flag is not set (debugger
1117 * XXXKSE eventually almost any inhibition could do.
1119 if (TD_CAN_UNBIND(td) && (td->td_standin) &&
1120 (TD_ON_SLEEPQ(td) || (TD_IS_SUSPENDED(td) &&
1121 !P_SHOULDSTOP(td->td_proc)))) {
1123 * Release ownership of upcall, and schedule an upcall
1124 * thread, this new upcall thread becomes the owner of
1125 * the upcall structure. It will be ahead of us in the
1126 * run queue, so as we are stopping, it should either
1127 * start up immediatly, or at least before us if
1128 * we release our slot.
1130 mtx_lock_spin(&kse_lock);
1132 ku->ku_owner = NULL;
1133 td->td_upcall = NULL;
1134 td->td_pflags &= ~TDP_CAN_UNBIND;
1135 td2 = thread_schedule_upcall(td, ku);
1136 mtx_unlock_spin(&kse_lock);
1137 if (flags & SW_INVOL || nextthread) {
1139 sched_add(td2, SRQ_YIELDING);
1142 /* Keep up with reality.. we have one extra thread
1143 * in the picture.. and it's 'running'.
1148 return (nextthread);
1152 * Setup done on the thread when it enters the kernel.
1155 thread_user_enter(struct thread *td)
1157 struct proc *p = td->td_proc;
1158 struct kse_upcall *ku;
1159 struct kse_thr_mailbox *tmbx;
1163 * First check that we shouldn't just abort. we
1164 * can suspend it here or just exit.
1166 if (__predict_false(P_SHOULDSTOP(p))) {
1168 thread_suspend_check(0);
1172 if (!(td->td_pflags & TDP_SA))
1176 * If we are doing a syscall in a KSE environment,
1177 * note where our mailbox is.
1184 KASSERT(ku != NULL, ("no upcall owned"));
1185 KASSERT(ku->ku_owner == td, ("wrong owner"));
1186 KASSERT(!TD_CAN_UNBIND(td), ("can unbind"));
1188 if (td->td_standin == NULL) {
1189 if (!thread_alloc_spare(td)) {
1191 if (kern_logsigexit)
1193 "pid %d (%s), uid %d: thread_alloc_spare failed\n",
1194 p->p_pid, p->p_comm,
1195 td->td_ucred ? td->td_ucred->cr_uid : -1);
1196 sigexit(td, SIGSEGV); /* XXX ? */
1197 /* panic("thread_user_enter: thread_alloc_spare failed"); */
1200 ku->ku_mflags = fuword32((void *)&ku->ku_mailbox->km_flags);
1201 tmbx = (void *)fuword((void *)&ku->ku_mailbox->km_curthread);
1202 if ((tmbx == NULL) || (tmbx == (void *)-1L) ||
1203 (ku->ku_mflags & KMF_NOUPCALL)) {
1204 td->td_mailbox = NULL;
1206 flags = fuword32(&tmbx->tm_flags);
1208 * On some architectures, TP register points to thread
1209 * mailbox but not points to kse mailbox, and userland
1210 * can not atomically clear km_curthread, but can
1211 * use TP register, and set TMF_NOUPCALL in thread
1212 * flag to indicate a critical region.
1214 if (flags & TMF_NOUPCALL) {
1215 td->td_mailbox = NULL;
1217 td->td_mailbox = tmbx;
1218 td->td_pflags |= TDP_CAN_UNBIND;
1220 if (__predict_false(p->p_flag & P_TRACED)) {
1221 flags = fuword32(&tmbx->tm_dflags);
1222 if (flags & TMDF_SUSPEND) {
1224 /* fuword can block, check again */
1226 ku->ku_flags |= KUF_DOUPCALL;
1236 * The extra work we go through if we are a threaded process when we
1237 * return to userland.
1239 * If we are a KSE process and returning to user mode, check for
1240 * extra work to do before we return (e.g. for more syscalls
1241 * to complete first). If we were in a critical section, we should
1242 * just return to let it finish. Same if we were in the UTS (in
1243 * which case the mailbox's context's busy indicator will be set).
1244 * The only traps we suport will have set the mailbox.
1245 * We will clear it here.
1248 thread_userret(struct thread *td, struct trapframe *frame)
1250 struct kse_upcall *ku;
1253 int error = 0, uts_crit;
1255 /* Nothing to do with bound thread */
1256 if (!(td->td_pflags & TDP_SA))
1260 * Update stat clock count for userland
1262 if (td->td_mailbox != NULL) {
1263 thread_update_usr_ticks(td);
1275 * This thread has not started any upcall.
1276 * If there is no work to report other than ourself,
1277 * then it can return direct to userland.
1279 if (TD_CAN_UNBIND(td)) {
1281 td->td_pflags &= ~TDP_CAN_UNBIND;
1282 if ((td->td_flags & TDF_NEEDSIGCHK) == 0 &&
1283 (p->p_completed == NULL) &&
1284 (ku->ku_flags & KUF_DOUPCALL) == 0 &&
1285 (p->p_upquantum && ticks < p->p_nextupcall)) {
1287 error = copyout(&ts,
1288 (caddr_t)&ku->ku_mailbox->km_timeofday,
1296 thread_export_context(td, 0);
1298 * There is something to report, and we own an upcall
1299 * structure, we can go to userland.
1300 * Turn ourself into an upcall thread.
1302 td->td_pflags |= TDP_UPCALLING;
1303 } else if (td->td_mailbox && (ku == NULL)) {
1305 thread_export_context(td, 1);
1308 wakeup(&p->p_completed);
1309 WITNESS_WARN(WARN_PANIC, &p->p_mtx.lock_object,
1310 "thread exiting in userret");
1311 sigqueue_flush(&td->td_sigqueue);
1319 KASSERT(ku != NULL, ("upcall is NULL"));
1320 KASSERT(TD_CAN_UNBIND(td) == 0, ("can unbind"));
1324 if (p->p_numthreads > max_threads_per_proc) {
1326 while (p->p_numthreads > max_threads_per_proc) {
1327 if (p->p_numupcalls >= max_threads_per_proc)
1330 if (msleep(&p->p_numthreads, &p->p_mtx, PPAUSE|PCATCH,
1331 "maxthreads", hz/10) != EWOULDBLOCK) {
1341 if (td->td_pflags & TDP_UPCALLING) {
1343 p->p_nextupcall = ticks + p->p_upquantum;
1345 * There is no more work to do and we are going to ride
1346 * this thread up to userland as an upcall.
1347 * Do the last parts of the setup needed for the upcall.
1349 CTR3(KTR_PROC, "userret: upcall thread %p (pid %d, %s)",
1350 td, td->td_proc->p_pid, td->td_proc->p_comm);
1352 td->td_pflags &= ~TDP_UPCALLING;
1353 if (ku->ku_flags & KUF_DOUPCALL) {
1355 ku->ku_flags &= ~KUF_DOUPCALL;
1359 * Set user context to the UTS
1361 if (!(ku->ku_mflags & KMF_NOUPCALL)) {
1362 cpu_set_upcall_kse(td, ku->ku_func, ku->ku_mailbox,
1365 if (p->p_flag & P_TRACED) {
1367 ptrace_clear_single_step(td);
1371 error = suword32(&ku->ku_mailbox->km_lwp,
1375 error = suword(&ku->ku_mailbox->km_curthread, 0);
1381 * Unhook the list of completed threads.
1382 * anything that completes after this gets to
1383 * come in next time.
1384 * Put the list of completed thread mailboxes on
1385 * this KSE's mailbox.
1387 if (!(ku->ku_mflags & KMF_NOCOMPLETED) &&
1388 (error = thread_link_mboxes(p, ku)) != 0)
1393 error = copyout(&ts, &ku->ku_mailbox->km_timeofday, sizeof(ts));
1399 * Things are going to be so screwed we should just kill
1401 * how do we do that?
1404 psignal(p, SIGSEGV);
1409 * Ensure that we have a spare thread available,
1410 * for when we re-enter the kernel.
1412 if (td->td_standin == NULL)
1413 thread_alloc_spare(td); /* XXX care of failure ? */
1417 td->td_mailbox = NULL;
1419 return (error); /* go sync */
1423 * called after ptrace resumed a process, force all
1424 * virtual CPUs to schedule upcall for SA process,
1425 * because debugger may have changed something in userland,
1426 * we should notice UTS as soon as possible.
1429 thread_continued(struct proc *p)
1431 struct kse_upcall *ku;
1434 PROC_LOCK_ASSERT(p, MA_OWNED);
1435 KASSERT(P_SHOULDSTOP(p), ("process not stopped"));
1437 if (!(p->p_flag & P_SA))
1440 if (p->p_flag & P_TRACED) {
1441 td = TAILQ_FIRST(&p->p_threads);
1442 if (td && (td->td_pflags & TDP_SA)) {
1443 FOREACH_UPCALL_IN_PROC(p, ku) {
1445 ku->ku_flags |= KUF_DOUPCALL;
1447 wakeup(&p->p_completed);