]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/kern_kse.c
lock proc while calling psignal
[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  * $FreeBSD$
29  */
30
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/lock.h>
35 #include <sys/malloc.h>
36 #include <sys/mutex.h>
37 #include <sys/proc.h>
38 #include <sys/sysctl.h>
39 #include <sys/filedesc.h>
40 #include <sys/tty.h>
41 #include <sys/signalvar.h>
42 #include <sys/sx.h>
43 #include <sys/user.h>
44 #include <sys/jail.h>
45 #include <sys/kse.h>
46 #include <sys/ktr.h>
47 #include <sys/ucontext.h>
48
49 #include <vm/vm.h>
50 #include <vm/vm_object.h>
51 #include <vm/pmap.h>
52 #include <vm/uma.h>
53 #include <vm/vm_map.h>
54
55 #include <machine/frame.h>
56
57 /*
58  * KSEGRP related storage.
59  */
60 static uma_zone_t ksegrp_zone;
61 static uma_zone_t kse_zone;
62 static uma_zone_t thread_zone;
63
64 /* DEBUG ONLY */
65 SYSCTL_NODE(_kern, OID_AUTO, threads, CTLFLAG_RW, 0, "thread allocation");
66 static int oiks_debug = 1;      /* 0 disable, 1 printf, 2 enter debugger */
67 SYSCTL_INT(_kern_threads, OID_AUTO, oiks, CTLFLAG_RW,
68         &oiks_debug, 0, "OIKS thread debug");
69
70 static int max_threads_per_proc = 6;
71 SYSCTL_INT(_kern_threads, OID_AUTO, max_per_proc, CTLFLAG_RW,
72         &max_threads_per_proc, 0, "Limit on threads per proc");
73
74 #define RANGEOF(type, start, end) (offsetof(type, end) - offsetof(type, start))
75
76 struct threadqueue zombie_threads = TAILQ_HEAD_INITIALIZER(zombie_threads);
77 struct mtx zombie_thread_lock;
78 MTX_SYSINIT(zombie_thread_lock, &zombie_thread_lock,
79     "zombie_thread_lock", MTX_SPIN);
80
81 /*
82  * Pepare a thread for use.
83  */
84 static void
85 thread_ctor(void *mem, int size, void *arg)
86 {
87         struct thread   *td;
88
89         KASSERT((size == sizeof(struct thread)),
90             ("size mismatch: %d != %d\n", size, (int)sizeof(struct thread)));
91
92         td = (struct thread *)mem;
93         td->td_state = TDS_INACTIVE;
94         td->td_flags |= TDF_UNBOUND;
95 }
96
97 /*
98  * Reclaim a thread after use.
99  */
100 static void
101 thread_dtor(void *mem, int size, void *arg)
102 {
103         struct thread   *td;
104
105         KASSERT((size == sizeof(struct thread)),
106             ("size mismatch: %d != %d\n", size, (int)sizeof(struct thread)));
107
108         td = (struct thread *)mem;
109
110 #ifdef INVARIANTS
111         /* Verify that this thread is in a safe state to free. */
112         switch (td->td_state) {
113         case TDS_INHIBITED:
114         case TDS_RUNNING:
115         case TDS_CAN_RUN:
116         case TDS_RUNQ:
117                 /*
118                  * We must never unlink a thread that is in one of
119                  * these states, because it is currently active.
120                  */
121                 panic("bad state for thread unlinking");
122                 /* NOTREACHED */
123         case TDS_INACTIVE:
124                 break;
125         default:
126                 panic("bad thread state");
127                 /* NOTREACHED */
128         }
129 #endif
130 }
131
132 /*
133  * Initialize type-stable parts of a thread (when newly created).
134  */
135 static void
136 thread_init(void *mem, int size)
137 {
138         struct thread   *td;
139
140         KASSERT((size == sizeof(struct thread)),
141             ("size mismatch: %d != %d\n", size, (int)sizeof(struct thread)));
142
143         td = (struct thread *)mem;
144         mtx_lock(&Giant);
145         pmap_new_thread(td);
146         mtx_unlock(&Giant);
147         cpu_thread_setup(td);
148 }
149
150 /*
151  * Tear down type-stable parts of a thread (just before being discarded).
152  */
153 static void
154 thread_fini(void *mem, int size)
155 {
156         struct thread   *td;
157
158         KASSERT((size == sizeof(struct thread)),
159             ("size mismatch: %d != %d\n", size, (int)sizeof(struct thread)));
160
161         td = (struct thread *)mem;
162         pmap_dispose_thread(td);
163 }
164
165 /*
166  * Fill a ucontext_t with a thread's context information.
167  *
168  * This is an analogue to getcontext(3).
169  */
170 void
171 thread_getcontext(struct thread *td, ucontext_t *uc)
172 {
173
174 /*
175  * XXX this is declared in a MD include file, i386/include/ucontext.h but
176  * is used in MI code.
177  */
178 #ifdef __i386__
179         get_mcontext(td, &uc->uc_mcontext);
180 #endif
181         uc->uc_sigmask = td->td_proc->p_sigmask;
182 }
183
184 /*
185  * Set a thread's context from a ucontext_t.
186  *
187  * This is an analogue to setcontext(3).
188  */
189 int
190 thread_setcontext(struct thread *td, ucontext_t *uc)
191 {
192         int ret;
193
194 /*
195  * XXX this is declared in a MD include file, i386/include/ucontext.h but
196  * is used in MI code.
197  */
198 #ifdef __i386__
199         ret = set_mcontext(td, &uc->uc_mcontext);
200 #else
201         ret = ENOSYS;
202 #endif
203         if (ret == 0) {
204                 SIG_CANTMASK(uc->uc_sigmask);
205                 PROC_LOCK(td->td_proc);
206                 td->td_proc->p_sigmask = uc->uc_sigmask;
207                 PROC_UNLOCK(td->td_proc);
208         }
209         return (ret);
210 }
211
212 /*
213  * Initialize global thread allocation resources.
214  */
215 void
216 threadinit(void)
217 {
218
219         thread_zone = uma_zcreate("THREAD", sizeof (struct thread),
220             thread_ctor, thread_dtor, thread_init, thread_fini,
221             UMA_ALIGN_CACHE, 0);
222         ksegrp_zone = uma_zcreate("KSEGRP", sizeof (struct ksegrp),
223             NULL, NULL, NULL, NULL,
224             UMA_ALIGN_CACHE, 0);
225         kse_zone = uma_zcreate("KSE", sizeof (struct kse),
226             NULL, NULL, NULL, NULL,
227             UMA_ALIGN_CACHE, 0);
228 }
229
230 /*
231  * Stash an embarasingly extra thread into the zombie thread queue.
232  */
233 void
234 thread_stash(struct thread *td)
235 {
236         mtx_lock_spin(&zombie_thread_lock);
237         TAILQ_INSERT_HEAD(&zombie_threads, td, td_runq);
238         mtx_unlock_spin(&zombie_thread_lock);
239 }
240
241 /*
242  * Reap zombie threads.
243  */
244 void
245 thread_reap(void)
246 {
247         struct thread *td_reaped;
248
249         /*
250          * don't even bother to lock if none at this instant
251          * We really don't care about the next instant..
252          */
253         if (!TAILQ_EMPTY(&zombie_threads)) {
254                 mtx_lock_spin(&zombie_thread_lock);
255                 while (!TAILQ_EMPTY(&zombie_threads)) {
256                         td_reaped = TAILQ_FIRST(&zombie_threads);
257                         TAILQ_REMOVE(&zombie_threads, td_reaped, td_runq);
258                         mtx_unlock_spin(&zombie_thread_lock);
259                         thread_free(td_reaped);
260                         mtx_lock_spin(&zombie_thread_lock);
261                 }
262                 mtx_unlock_spin(&zombie_thread_lock);
263         }
264 }
265
266 /*
267  * Allocate a ksegrp.
268  */
269 struct ksegrp *
270 ksegrp_alloc(void)
271 {
272         return (uma_zalloc(ksegrp_zone, M_WAITOK));
273 }
274
275 /*
276  * Allocate a kse.
277  */
278 struct kse *
279 kse_alloc(void)
280 {
281         return (uma_zalloc(kse_zone, M_WAITOK));
282 }
283
284 /*
285  * Allocate a thread.
286  */
287 struct thread *
288 thread_alloc(void)
289 {
290         thread_reap(); /* check if any zombies to get */
291         return (uma_zalloc(thread_zone, M_WAITOK));
292 }
293
294 /*
295  * Deallocate a ksegrp.
296  */
297 void
298 ksegrp_free(struct ksegrp *td)
299 {
300         uma_zfree(ksegrp_zone, td);
301 }
302
303 /*
304  * Deallocate a kse.
305  */
306 void
307 kse_free(struct kse *td)
308 {
309         uma_zfree(kse_zone, td);
310 }
311
312 /*
313  * Deallocate a thread.
314  */
315 void
316 thread_free(struct thread *td)
317 {
318         uma_zfree(thread_zone, td);
319 }
320
321 /*
322  * Store the thread context in the UTS's mailbox.
323  * then add the mailbox at the head of a list we are building in user space.
324  * The list is anchored in the ksegrp structure.
325  */
326 int
327 thread_export_context(struct thread *td)
328 {
329         struct proc *p = td->td_proc;
330         struct ksegrp *kg;
331         uintptr_t mbx;
332         void *addr;
333         int error;
334         ucontext_t uc;
335
336         /* Export the user/machine context. */
337 #if 0
338         addr = (caddr_t)td->td_mailbox +
339             offsetof(struct kse_thr_mailbox, tm_context);
340 #else /* if user pointer arithmetic is valid in the kernel */
341                 addr = (void *)(&td->td_mailbox->tm_context);
342 #endif
343         error = copyin(addr, &uc, sizeof(ucontext_t));
344         if (error == 0) {
345                 thread_getcontext(td, &uc);
346                 error = copyout(&uc, addr, sizeof(ucontext_t));
347
348         }
349         if (error) {
350                 PROC_LOCK(p);
351                 psignal(p, SIGSEGV);
352                 PROC_UNLOCK(p);
353                 return (error);
354         }
355         /* get address in latest mbox of list pointer */
356 #if 0
357         addr = (caddr_t)td->td_mailbox
358             + offsetof(struct kse_thr_mailbox , tm_next);
359 #else /* if user pointer arithmetic is valid in the kernel */
360         addr = (void *)(&td->td_mailbox->tm_next);
361 #endif
362         /*
363          * Put the saved address of the previous first
364          * entry into this one
365          */
366         kg = td->td_ksegrp;
367         for (;;) {
368                 mbx = (uintptr_t)kg->kg_completed;
369                 if (suword(addr, mbx)) {
370                         PROC_LOCK(p);
371                         psignal(p, SIGSEGV);
372                         PROC_UNLOCK(p);
373                         return (EFAULT);
374                 }
375                 PROC_LOCK(p);
376                 if (mbx == (uintptr_t)kg->kg_completed) {
377                         kg->kg_completed = td->td_mailbox;
378                         PROC_UNLOCK(p);
379                         break;
380                 }
381                 PROC_UNLOCK(p);
382         }
383         return (0);
384 }
385
386 /*
387  * Take the list of completed mailboxes for this KSEGRP and put them on this
388  * KSE's mailbox as it's the next one going up.
389  */
390 static int
391 thread_link_mboxes(struct ksegrp *kg, struct kse *ke)
392 {
393         struct proc *p = kg->kg_proc;
394         void *addr;
395         uintptr_t mbx;
396
397 #if 0
398         addr = (caddr_t)ke->ke_mailbox
399             + offsetof(struct kse_mailbox, km_completed);
400 #else /* if user pointer arithmetic is valid in the kernel */
401                 addr = (void *)(&ke->ke_mailbox->km_completed);
402 #endif
403         for (;;) {
404                 mbx = (uintptr_t)kg->kg_completed;
405                 if (suword(addr, mbx)) {
406                         PROC_LOCK(p);
407                         psignal(p, SIGSEGV);
408                         PROC_UNLOCK(p);
409                         return (EFAULT);
410                 }
411                 /* XXXKSE could use atomic CMPXCH here */
412                 PROC_LOCK(p);
413                 if (mbx == (uintptr_t)kg->kg_completed) {
414                         kg->kg_completed = NULL;
415                         PROC_UNLOCK(p);
416                         break;
417                 }
418                 PROC_UNLOCK(p);
419         }
420         return (0);
421 }
422
423 /*
424  * Discard the current thread and exit from its context.
425  *
426  * Because we can't free a thread while we're operating under its context,
427  * push the current thread into our KSE's ke_tdspare slot, freeing the
428  * thread that might be there currently. Because we know that only this
429  * processor will run our KSE, we needn't worry about someone else grabbing
430  * our context before we do a cpu_throw.
431  */
432 void
433 thread_exit(void)
434 {
435         struct thread *td;
436         struct kse *ke;
437         struct proc *p;
438         struct ksegrp   *kg;
439
440         td = curthread;
441         kg = td->td_ksegrp;
442         p = td->td_proc;
443         ke = td->td_kse;
444
445         mtx_assert(&sched_lock, MA_OWNED);
446         KASSERT(p != NULL, ("thread exiting without a process"));
447         KASSERT(ke != NULL, ("thread exiting without a kse"));
448         KASSERT(kg != NULL, ("thread exiting without a kse group"));
449         PROC_LOCK_ASSERT(p, MA_OWNED);
450         CTR1(KTR_PROC, "thread_exit: thread %p", td);
451         KASSERT(!mtx_owned(&Giant), ("dying thread owns giant"));
452
453         if (ke->ke_tdspare != NULL) {
454                 thread_stash(ke->ke_tdspare);
455                 ke->ke_tdspare = NULL;
456         }
457         cpu_thread_exit(td);    /* XXXSMP */
458
459         /*
460          * The last thread is left attached to the process
461          * So that the whole bundle gets recycled. Skip
462          * all this stuff.
463          */
464         if (p->p_numthreads > 1) {
465                 /* Reassign this thread's KSE. */
466                 ke->ke_thread = NULL;
467                 td->td_kse = NULL;
468                 ke->ke_state = KES_UNQUEUED;
469                 kse_reassign(ke);
470
471                 /* Unlink this thread from its proc. and the kseg */
472                 TAILQ_REMOVE(&p->p_threads, td, td_plist);
473                 p->p_numthreads--;
474                 TAILQ_REMOVE(&kg->kg_threads, td, td_kglist);
475                 kg->kg_numthreads--;
476                 /*
477                  * The test below is NOT true if we are the
478                  * sole exiting thread. P_STOPPED_SNGL is unset
479                  * in exit1() after it is the only survivor.
480                  */
481                 if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
482                         if (p->p_numthreads == p->p_suspcount) {
483                                 thread_unsuspend_one(p->p_singlethread);
484                         }
485                 }
486                 PROC_UNLOCK(p);
487                 td->td_state    = TDS_INACTIVE;
488                 td->td_proc     = NULL;
489                 td->td_ksegrp   = NULL;
490                 td->td_last_kse = NULL;
491                 ke->ke_tdspare = td;
492         } else {
493                 PROC_UNLOCK(p);
494         }
495
496         cpu_throw();
497         /* NOTREACHED */
498 }
499
500 /*
501  * Link a thread to a process.
502  * set up anything that needs to be initialized for it to
503  * be used by the process.
504  *
505  * Note that we do not link to the proc's ucred here.
506  * The thread is linked as if running but no KSE assigned.
507  */
508 void
509 thread_link(struct thread *td, struct ksegrp *kg)
510 {
511         struct proc *p;
512
513         p = kg->kg_proc;
514         td->td_state = TDS_INACTIVE;
515         td->td_proc     = p;
516         td->td_ksegrp   = kg;
517         td->td_last_kse = NULL;
518
519         LIST_INIT(&td->td_contested);
520         callout_init(&td->td_slpcallout, 1);
521         TAILQ_INSERT_HEAD(&p->p_threads, td, td_plist);
522         TAILQ_INSERT_HEAD(&kg->kg_threads, td, td_kglist);
523         p->p_numthreads++;
524         kg->kg_numthreads++;
525         if (oiks_debug && p->p_numthreads > max_threads_per_proc) {
526                 printf("OIKS %d\n", p->p_numthreads);
527                 if (oiks_debug > 1)
528                         Debugger("OIKS");
529         }
530         td->td_kse      = NULL;
531 }
532
533 /*
534  * Create a thread and schedule it for upcall on the KSE given.
535  */
536 struct thread *
537 thread_schedule_upcall(struct thread *td, struct kse *ke)
538 {
539         struct thread *td2;
540
541         mtx_assert(&sched_lock, MA_OWNED);
542         if (ke->ke_tdspare != NULL) {
543                 td2 = ke->ke_tdspare;
544                 ke->ke_tdspare = NULL;
545         } else {
546                 mtx_unlock_spin(&sched_lock);
547                 td2 = thread_alloc();
548                 mtx_lock_spin(&sched_lock);
549         }
550         CTR3(KTR_PROC, "thread_schedule_upcall: thread %p (pid %d, %s)",
551              td, td->td_proc->p_pid, td->td_proc->p_comm);
552         bzero(&td2->td_startzero,
553             (unsigned)RANGEOF(struct thread, td_startzero, td_endzero));
554         bcopy(&td->td_startcopy, &td2->td_startcopy,
555             (unsigned) RANGEOF(struct thread, td_startcopy, td_endcopy));
556         thread_link(td2, ke->ke_ksegrp);
557         cpu_set_upcall(td2, td->td_pcb);
558         bcopy(td->td_frame, td2->td_frame, sizeof(struct trapframe));
559         /*
560          * The user context for this thread is selected when we choose
561          * a KSE and return to userland on it. All we need do here is
562          * note that the thread exists in order to perform an upcall.
563          *
564          * Since selecting a KSE to perform the upcall involves locking
565          * that KSE's context to our upcall, its best to wait until the
566          * last possible moment before grabbing a KSE. We do this in
567          * userret().
568          */
569         td2->td_ucred = crhold(td->td_ucred);
570         td2->td_flags = TDF_UNBOUND|TDF_UPCALLING;
571         TD_SET_CAN_RUN(td2);
572         setrunqueue(td2);
573         return (td2);
574 }
575
576 /*
577  * Schedule an upcall to notify a KSE process recieved signals.
578  *
579  * XXX - Modifying a sigset_t like this is totally bogus.
580  */
581 struct thread *
582 signal_upcall(struct proc *p, int sig)
583 {
584         struct thread *td, *td2;
585         struct kse *ke;
586         sigset_t ss;
587         int error;
588
589         PROC_LOCK_ASSERT(p, MA_OWNED);
590
591         td = FIRST_THREAD_IN_PROC(p);
592         ke = td->td_kse;
593         PROC_UNLOCK(p);
594         error = copyin(&ke->ke_mailbox->km_sigscaught, &ss, sizeof(sigset_t));
595         PROC_LOCK(p);
596         if (error)
597                 return (NULL);
598         SIGADDSET(ss, sig);
599         PROC_UNLOCK(p);
600         error = copyout(&ss, &ke->ke_mailbox->km_sigscaught, sizeof(sigset_t));
601         PROC_LOCK(p);
602         if (error)
603                 return (NULL);
604         mtx_lock_spin(&sched_lock);
605         td2 = thread_schedule_upcall(td, ke);
606         mtx_unlock_spin(&sched_lock);
607         return (td2);
608 }
609
610 /*
611  * Consider whether or not an upcall should be made, and update the
612  * TDF_UPCALLING flag appropriately.
613  *
614  * This function is called when the current thread had been bound to a user
615  * thread that performed a syscall that blocked, and is now returning.
616  * Got that? syscall -> msleep -> wakeup -> syscall_return -> us.
617  *
618  * This thread will be returned to the UTS in its mailbox as a completed
619  * thread.  We need to decide whether or not to perform an upcall now,
620  * or simply queue the thread for later.
621  *
622  * XXXKSE Future enhancement: We could also return back to
623  * the thread if we haven't had to do an upcall since then.
624  * If the KSE's copy is == the thread's copy, and there are
625  * no other completed threads.
626  */
627 static int
628 thread_consider_upcalling(struct thread *td)
629 {
630         struct proc *p;
631         struct ksegrp *kg;
632         int error;
633
634         /*
635          * Save the thread's context, and link it
636          * into the KSEGRP's list of completed threads.
637          */
638         error = thread_export_context(td);
639         td->td_flags &= ~TDF_UNBOUND;
640         td->td_mailbox = NULL;
641         if (error)
642                 /*
643                  * Failing to do the KSE operation just defaults
644                  * back to synchonous operation, so just return from
645                  * the syscall.
646                  */
647                 return (error);
648
649         /*
650          * Decide whether to perform an upcall now.
651          */
652         /* Make sure there are no other threads waiting to run. */
653         p = td->td_proc;
654         kg = td->td_ksegrp;
655         PROC_LOCK(p);
656         mtx_lock_spin(&sched_lock);
657         /* bogus test, ok for testing though */
658         if (TAILQ_FIRST(&kg->kg_runq) && 
659             (TAILQ_LAST(&kg->kg_runq, threadqueue) 
660                 != kg->kg_last_assigned)) {
661                 /*
662                  * Another thread in this KSEG needs to run.
663                  * Switch to it instead of performing an upcall,
664                  * abondoning this thread.  Perform the upcall
665                  * later; discard this thread for now.
666                  *
667                  * XXXKSE - As for the other threads to run;
668                  * we COULD rush through all the threads
669                  * in this KSEG at this priority, or we
670                  * could throw the ball back into the court
671                  * and just run the highest prio kse available.
672                  * What is OUR priority?  The priority of the highest
673                  * sycall waiting to be returned?
674                  * For now, just let another KSE run (easiest).
675                  */
676                 thread_exit(); /* Abandon current thread. */
677                 /* NOTREACHED */
678         } 
679         /*
680          * Perform an upcall now.
681          *
682          * XXXKSE - Assumes we are going to userland, and not
683          * nested in the kernel.
684          */
685         td->td_flags |= TDF_UPCALLING;
686         mtx_unlock_spin(&sched_lock);
687         PROC_UNLOCK(p);
688         return (0);
689 }
690
691 /*
692  * The extra work we go through if we are a threaded process when we
693  * return to userland.
694  *
695  * If we are a KSE process and returning to user mode, check for
696  * extra work to do before we return (e.g. for more syscalls
697  * to complete first).  If we were in a critical section, we should
698  * just return to let it finish. Same if we were in the UTS (in
699  * which case the mailbox's context's busy indicator will be set).
700  * The only traps we suport will have set the mailbox.
701  * We will clear it here.
702  */
703 int
704 thread_userret(struct thread *td, struct trapframe *frame)
705 {
706         int error;
707         int unbound;
708         struct kse *ke;
709
710         /* Make the thread bound from now on, but remember what it was. */
711         unbound = td->td_flags & TDF_UNBOUND;
712         td->td_flags &= ~TDF_UNBOUND;
713         /*
714          * Ensure that we have a spare thread available.
715          */
716         ke = td->td_kse;
717         if (ke->ke_tdspare == NULL) {
718                 mtx_lock(&Giant);
719                 ke->ke_tdspare = thread_alloc();
720                 mtx_unlock(&Giant);
721         }
722         /*
723          * Originally bound threads need no additional work.
724          */
725         if (unbound == 0)
726                 return (0);
727         error = 0;
728         /*
729          * Decide whether or not we should perform an upcall now.
730          */
731         if (((td->td_flags & TDF_UPCALLING) == 0) && unbound) {
732                 /* if we have other threads to run we will not return */
733                 if ((error = thread_consider_upcalling(td)))
734                         return (error); /* coundn't go async , just go sync. */
735         }
736         if (td->td_flags & TDF_UPCALLING) {
737                 /*
738                  * There is no more work to do and we are going to ride
739                  * this thead/KSE up to userland as an upcall.
740                  */
741                 CTR3(KTR_PROC, "userret: upcall thread %p (pid %d, %s)",
742                     td, td->td_proc->p_pid, td->td_proc->p_comm);
743
744                 /*
745                  * Set user context to the UTS.
746                  */
747                 cpu_set_upcall_kse(td, ke);
748
749                 /*
750                  * Put any completed mailboxes on this KSE's list.
751                  */
752                 error = thread_link_mboxes(td->td_ksegrp, ke);
753                 if (error)
754                         goto bad;
755
756                 /*
757                  * Set state and mailbox.
758                  */
759                 td->td_flags &= ~TDF_UPCALLING;
760 #if 0
761                 error = suword((caddr_t)ke->ke_mailbox +
762                     offsetof(struct kse_mailbox, km_curthread),
763                     0);
764 #else   /* if user pointer arithmetic is ok in the kernel */
765                 error = suword((caddr_t)&ke->ke_mailbox->km_curthread, 0);
766 #endif
767                 if (error)
768                         goto bad;
769         }
770         /*
771          * Stop any chance that we may be separated from
772          * the KSE we are currently on. This is "biting the bullet",
773          * we are committing to go to user space as as this KSE here.
774          */
775         return (error);
776 bad:
777         /*
778          * Things are going to be so screwed we should just kill the process.
779          * how do we do that?
780          */
781          panic ("thread_userret.. need to kill proc..... how?");
782 }
783
784 /*
785  * Enforce single-threading.
786  *
787  * Returns 1 if the caller must abort (another thread is waiting to
788  * exit the process or similar). Process is locked!
789  * Returns 0 when you are successfully the only thread running.
790  * A process has successfully single threaded in the suspend mode when
791  * There are no threads in user mode. Threads in the kernel must be
792  * allowed to continue until they get to the user boundary. They may even
793  * copy out their return values and data before suspending. They may however be
794  * accellerated in reaching the user boundary as we will wake up
795  * any sleeping threads that are interruptable. (PCATCH).
796  */
797 int
798 thread_single(int force_exit)
799 {
800         struct thread *td;
801         struct thread *td2;
802         struct proc *p;
803
804         td = curthread;
805         p = td->td_proc;
806         PROC_LOCK_ASSERT(p, MA_OWNED);
807         KASSERT((td != NULL), ("curthread is NULL"));
808
809         if ((p->p_flag & P_KSES) == 0)
810                 return (0);
811
812         /* Is someone already single threading? */
813         if (p->p_singlethread) 
814                 return (1);
815
816         if (force_exit == SINGLE_EXIT)
817                 p->p_flag |= P_SINGLE_EXIT;
818         else
819                 p->p_flag &= ~P_SINGLE_EXIT;
820         p->p_flag |= P_STOPPED_SINGLE;
821         p->p_singlethread = td;
822         while ((p->p_numthreads - p->p_suspcount) != 1) {
823                 mtx_lock_spin(&sched_lock);
824                 FOREACH_THREAD_IN_PROC(p, td2) {
825                         if (td2 == td)
826                                 continue;
827                         if (TD_IS_INHIBITED(td2)) {
828                                 if (TD_IS_SUSPENDED(td2)) {
829                                         if (force_exit == SINGLE_EXIT) {
830                                                 thread_unsuspend_one(td2);
831                                         }
832                                 }
833                                 if ( TD_IS_SLEEPING(td2)) {
834                                         if (td2->td_flags & TDF_CVWAITQ)
835                                                 cv_waitq_remove(td2);
836                                         else
837                                                 unsleep(td2);
838                                         break;
839                                 }
840                                 if (TD_CAN_RUN(td2))
841                                         setrunqueue(td2);
842                         }
843                 }
844                 /*
845                  * Wake us up when everyone else has suspended.
846                  * In the mean time we suspend as well.
847                  */
848                 thread_suspend_one(td);
849                 mtx_unlock(&Giant);
850                 PROC_UNLOCK(p);
851                 mi_switch();
852                 mtx_unlock_spin(&sched_lock);
853                 mtx_lock(&Giant);
854                 PROC_LOCK(p);
855         }
856         return (0);
857 }
858
859 /*
860  * Called in from locations that can safely check to see
861  * whether we have to suspend or at least throttle for a
862  * single-thread event (e.g. fork).
863  *
864  * Such locations include userret().
865  * If the "return_instead" argument is non zero, the thread must be able to
866  * accept 0 (caller may continue), or 1 (caller must abort) as a result.
867  *
868  * The 'return_instead' argument tells the function if it may do a
869  * thread_exit() or suspend, or whether the caller must abort and back
870  * out instead.
871  *
872  * If the thread that set the single_threading request has set the
873  * P_SINGLE_EXIT bit in the process flags then this call will never return
874  * if 'return_instead' is false, but will exit.
875  *
876  * P_SINGLE_EXIT | return_instead == 0| return_instead != 0
877  *---------------+--------------------+---------------------
878  *       0       | returns 0          |   returns 0 or 1
879  *               | when ST ends       |   immediatly
880  *---------------+--------------------+---------------------
881  *       1       | thread exits       |   returns 1
882  *               |                    |  immediatly
883  * 0 = thread_exit() or suspension ok,
884  * other = return error instead of stopping the thread.
885  *
886  * While a full suspension is under effect, even a single threading
887  * thread would be suspended if it made this call (but it shouldn't).
888  * This call should only be made from places where
889  * thread_exit() would be safe as that may be the outcome unless 
890  * return_instead is set.
891  */
892 int
893 thread_suspend_check(int return_instead)
894 {
895         struct thread *td = curthread;
896         struct proc *p = td->td_proc;
897
898         td = curthread;
899         p = td->td_proc;
900         PROC_LOCK_ASSERT(p, MA_OWNED);
901         while (P_SHOULDSTOP(p)) {
902                 if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
903                         KASSERT(p->p_singlethread != NULL,
904                             ("singlethread not set"));
905                         /*
906                          * The only suspension in action is a
907                          * single-threading. Single threader need not stop.
908                          * XXX Should be safe to access unlocked 
909                          * as it can only be set to be true by us.
910                          */
911                         if (p->p_singlethread == td)
912                                 return (0);     /* Exempt from stopping. */
913                 } 
914                 if (return_instead)
915                         return (1);
916
917                 /*
918                  * If the process is waiting for us to exit,
919                  * this thread should just suicide.
920                  * Assumes that P_SINGLE_EXIT implies P_STOPPED_SINGLE.
921                  */
922                 if ((p->p_flag & P_SINGLE_EXIT) && (p->p_singlethread != td)) {
923                         mtx_lock_spin(&sched_lock);
924                         while (mtx_owned(&Giant))
925                                 mtx_unlock(&Giant);
926                         thread_exit();
927                 }
928
929                 /*
930                  * When a thread suspends, it just
931                  * moves to the processes's suspend queue
932                  * and stays there.
933                  *
934                  * XXXKSE if TDF_BOUND is true
935                  * it will not release it's KSE which might
936                  * lead to deadlock if there are not enough KSEs
937                  * to complete all waiting threads.
938                  * Maybe be able to 'lend' it out again.
939                  * (lent kse's can not go back to userland?)
940                  * and can only be lent in STOPPED state.
941                  */
942                 mtx_lock_spin(&sched_lock);
943                 if ((p->p_flag & P_STOPPED_SIG) &&
944                     (p->p_suspcount+1 == p->p_numthreads)) {
945                         mtx_unlock_spin(&sched_lock);
946                         PROC_LOCK(p->p_pptr);
947                         if ((p->p_pptr->p_procsig->ps_flag &
948                                 PS_NOCLDSTOP) == 0) {
949                                 psignal(p->p_pptr, SIGCHLD);
950                         }
951                         PROC_UNLOCK(p->p_pptr);
952                         mtx_lock_spin(&sched_lock);
953                 }
954                 mtx_assert(&Giant, MA_NOTOWNED);
955                 thread_suspend_one(td);
956                 PROC_UNLOCK(p);
957                 if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
958                         if (p->p_numthreads == p->p_suspcount) {
959                                 thread_unsuspend_one(p->p_singlethread);
960                         }
961                 }
962                 p->p_stats->p_ru.ru_nivcsw++;
963                 mi_switch();
964                 mtx_unlock_spin(&sched_lock);
965                 PROC_LOCK(p);
966         }
967         return (0);
968 }
969
970 void
971 thread_suspend_one(struct thread *td)
972 {
973         struct proc *p = td->td_proc;
974
975         mtx_assert(&sched_lock, MA_OWNED);
976         p->p_suspcount++;
977         TD_SET_SUSPENDED(td);
978         TAILQ_INSERT_TAIL(&p->p_suspended, td, td_runq);
979         /*
980          * Hack: If we are suspending but are on the sleep queue
981          * then we are in msleep or the cv equivalent. We
982          * want to look like we have two Inhibitors.
983          */
984         if (TD_ON_SLEEPQ(td))
985                 TD_SET_SLEEPING(td);
986 }
987
988 void
989 thread_unsuspend_one(struct thread *td)
990 {
991         struct proc *p = td->td_proc;
992
993         mtx_assert(&sched_lock, MA_OWNED);
994         TAILQ_REMOVE(&p->p_suspended, td, td_runq);
995         TD_CLR_SUSPENDED(td);
996         p->p_suspcount--;
997         setrunnable(td);
998 }
999
1000 /*
1001  * Allow all threads blocked by single threading to continue running.
1002  */
1003 void
1004 thread_unsuspend(struct proc *p)
1005 {
1006         struct thread *td;
1007
1008         mtx_assert(&sched_lock, MA_OWNED);
1009         PROC_LOCK_ASSERT(p, MA_OWNED);
1010         if (!P_SHOULDSTOP(p)) {
1011                 while (( td = TAILQ_FIRST(&p->p_suspended))) {
1012                         thread_unsuspend_one(td);
1013                 }
1014         } else if ((P_SHOULDSTOP(p) == P_STOPPED_SINGLE) &&
1015             (p->p_numthreads == p->p_suspcount)) {
1016                 /*
1017                  * Stopping everything also did the job for the single
1018                  * threading request. Now we've downgraded to single-threaded,
1019                  * let it continue.
1020                  */
1021                 thread_unsuspend_one(p->p_singlethread);
1022         }
1023 }
1024
1025 void
1026 thread_single_end(void)
1027 {
1028         struct thread *td;
1029         struct proc *p;
1030
1031         td = curthread;
1032         p = td->td_proc;
1033         PROC_LOCK_ASSERT(p, MA_OWNED);
1034         p->p_flag &= ~P_STOPPED_SINGLE;
1035         p->p_singlethread = NULL;
1036         /*
1037          * If there are other threads they mey now run,
1038          * unless of course there is a blanket 'stop order'
1039          * on the process. The single threader must be allowed
1040          * to continue however as this is a bad place to stop.
1041          */
1042         if ((p->p_numthreads != 1) && (!P_SHOULDSTOP(p))) {
1043                 mtx_lock_spin(&sched_lock);
1044                 while (( td = TAILQ_FIRST(&p->p_suspended))) {
1045                         thread_unsuspend_one(td);
1046                 }
1047                 mtx_unlock_spin(&sched_lock);
1048         }
1049 }
1050
1051