The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/kern_thr.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 2003, Jeffrey Roberson <jeff@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 unmodified, this list of conditions, and the following
   10  *    disclaimer.
   11  * 2. Redistributions in binary form must reproduce the above copyright
   12  *    notice, this list of conditions and the following disclaimer in the
   13  *    documentation and/or other materials provided with the distribution.
   14  *
   15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   25  */
   26 
   27 #include <sys/cdefs.h>
   28 __FBSDID("$FreeBSD$");
   29 
   30 #include "opt_compat.h"
   31 #include "opt_posix.h"
   32 #include <sys/param.h>
   33 #include <sys/kernel.h>
   34 #include <sys/lock.h>
   35 #include <sys/mutex.h>
   36 #include <sys/priv.h>
   37 #include <sys/proc.h>
   38 #include <sys/posix4.h>
   39 #include <sys/resourcevar.h>
   40 #include <sys/sched.h>
   41 #include <sys/sysctl.h>
   42 #include <sys/smp.h>
   43 #include <sys/syscallsubr.h>
   44 #include <sys/sysent.h>
   45 #include <sys/systm.h>
   46 #include <sys/sysproto.h>
   47 #include <sys/signalvar.h>
   48 #include <sys/sysctl.h>
   49 #include <sys/ucontext.h>
   50 #include <sys/thr.h>
   51 #include <sys/rtprio.h>
   52 #include <sys/umtx.h>
   53 #include <sys/limits.h>
   54 
   55 #include <machine/frame.h>
   56 
   57 #include <security/audit/audit.h>
   58 
   59 SYSCTL_NODE(_kern, OID_AUTO, threads, CTLFLAG_RW, 0, "thread allocation");
   60 
   61 static int max_threads_per_proc = 1500;
   62 SYSCTL_INT(_kern_threads, OID_AUTO, max_threads_per_proc, CTLFLAG_RW,
   63         &max_threads_per_proc, 0, "Limit on threads per proc");
   64 
   65 static int max_threads_hits;
   66 SYSCTL_INT(_kern_threads, OID_AUTO, max_threads_hits, CTLFLAG_RD,
   67         &max_threads_hits, 0, "");
   68 
   69 #ifdef COMPAT_FREEBSD32
   70 
   71 static inline int
   72 suword_lwpid(void *addr, lwpid_t lwpid)
   73 {
   74         int error;
   75 
   76         if (SV_CURPROC_FLAG(SV_LP64))
   77                 error = suword(addr, lwpid);
   78         else
   79                 error = suword32(addr, lwpid);
   80         return (error);
   81 }
   82 
   83 #else
   84 #define suword_lwpid    suword
   85 #endif
   86 
   87 static int create_thread(struct thread *td, mcontext_t *ctx,
   88                          void (*start_func)(void *), void *arg,
   89                          char *stack_base, size_t stack_size,
   90                          char *tls_base,
   91                          long *child_tid, long *parent_tid,
   92                          int flags, struct rtprio *rtp);
   93 
   94 /*
   95  * System call interface.
   96  */
   97 int
   98 thr_create(struct thread *td, struct thr_create_args *uap)
   99     /* ucontext_t *ctx, long *id, int flags */
  100 {
  101         ucontext_t ctx;
  102         int error;
  103 
  104         if ((error = copyin(uap->ctx, &ctx, sizeof(ctx))))
  105                 return (error);
  106 
  107         error = create_thread(td, &ctx.uc_mcontext, NULL, NULL,
  108                 NULL, 0, NULL, uap->id, NULL, uap->flags, NULL);
  109         return (error);
  110 }
  111 
  112 int
  113 thr_new(struct thread *td, struct thr_new_args *uap)
  114     /* struct thr_param * */
  115 {
  116         struct thr_param param;
  117         int error;
  118 
  119         if (uap->param_size < 0 || uap->param_size > sizeof(param))
  120                 return (EINVAL);
  121         bzero(&param, sizeof(param));
  122         if ((error = copyin(uap->param, &param, uap->param_size)))
  123                 return (error);
  124         return (kern_thr_new(td, &param));
  125 }
  126 
  127 int
  128 kern_thr_new(struct thread *td, struct thr_param *param)
  129 {
  130         struct rtprio rtp, *rtpp;
  131         int error;
  132 
  133         rtpp = NULL;
  134         if (param->rtp != 0) {
  135                 error = copyin(param->rtp, &rtp, sizeof(struct rtprio));
  136                 if (error)
  137                         return (error);
  138                 rtpp = &rtp;
  139         }
  140         error = create_thread(td, NULL, param->start_func, param->arg,
  141                 param->stack_base, param->stack_size, param->tls_base,
  142                 param->child_tid, param->parent_tid, param->flags,
  143                 rtpp);
  144         return (error);
  145 }
  146 
  147 static int
  148 create_thread(struct thread *td, mcontext_t *ctx,
  149             void (*start_func)(void *), void *arg,
  150             char *stack_base, size_t stack_size,
  151             char *tls_base,
  152             long *child_tid, long *parent_tid,
  153             int flags, struct rtprio *rtp)
  154 {
  155         stack_t stack;
  156         struct thread *newtd;
  157         struct proc *p;
  158         int error;
  159 
  160         p = td->td_proc;
  161 
  162         /* Have race condition but it is cheap. */
  163         if (p->p_numthreads >= max_threads_per_proc) {
  164                 ++max_threads_hits;
  165                 return (EPROCLIM);
  166         }
  167 
  168         if (rtp != NULL) {
  169                 switch(rtp->type) {
  170                 case RTP_PRIO_REALTIME:
  171                 case RTP_PRIO_FIFO:
  172                         /* Only root can set scheduler policy */
  173                         if (priv_check(td, PRIV_SCHED_SETPOLICY) != 0)
  174                                 return (EPERM);
  175                         if (rtp->prio > RTP_PRIO_MAX)
  176                                 return (EINVAL);
  177                         break;
  178                 case RTP_PRIO_NORMAL:
  179                         rtp->prio = 0;
  180                         break;
  181                 default:
  182                         return (EINVAL);
  183                 }
  184         }
  185 
  186         /* Initialize our td */
  187         newtd = thread_alloc(0);
  188         if (newtd == NULL)
  189                 return (ENOMEM);
  190 
  191         cpu_set_upcall(newtd, td);
  192 
  193         /*
  194          * Try the copyout as soon as we allocate the td so we don't
  195          * have to tear things down in a failure case below.
  196          * Here we copy out tid to two places, one for child and one
  197          * for parent, because pthread can create a detached thread,
  198          * if parent wants to safely access child tid, it has to provide 
  199          * its storage, because child thread may exit quickly and
  200          * memory is freed before parent thread can access it.
  201          */
  202         if ((child_tid != NULL &&
  203             suword_lwpid(child_tid, newtd->td_tid)) ||
  204             (parent_tid != NULL &&
  205             suword_lwpid(parent_tid, newtd->td_tid))) {
  206                 thread_free(newtd);
  207                 return (EFAULT);
  208         }
  209 
  210         bzero(&newtd->td_startzero,
  211             __rangeof(struct thread, td_startzero, td_endzero));
  212         bzero(&newtd->td_rux, sizeof(newtd->td_rux));
  213         newtd->td_map_def_user = NULL;
  214         newtd->td_dbg_forked = 0;
  215         bcopy(&td->td_startcopy, &newtd->td_startcopy,
  216             __rangeof(struct thread, td_startcopy, td_endcopy));
  217         newtd->td_proc = td->td_proc;
  218         newtd->td_ucred = crhold(td->td_ucred);
  219 
  220         if (ctx != NULL) { /* old way to set user context */
  221                 error = set_mcontext(newtd, ctx);
  222                 if (error != 0) {
  223                         thread_free(newtd);
  224                         crfree(td->td_ucred);
  225                         return (error);
  226                 }
  227         } else {
  228                 /* Set up our machine context. */
  229                 stack.ss_sp = stack_base;
  230                 stack.ss_size = stack_size;
  231                 /* Set upcall address to user thread entry function. */
  232                 cpu_set_upcall_kse(newtd, start_func, arg, &stack);
  233                 /* Setup user TLS address and TLS pointer register. */
  234                 error = cpu_set_user_tls(newtd, tls_base);
  235                 if (error != 0) {
  236                         thread_free(newtd);
  237                         crfree(td->td_ucred);
  238                         return (error);
  239                 }
  240         }
  241 
  242         PROC_LOCK(td->td_proc);
  243         td->td_proc->p_flag |= P_HADTHREADS;
  244         newtd->td_sigmask = td->td_sigmask;
  245         thread_link(newtd, p); 
  246         bcopy(p->p_comm, newtd->td_name, sizeof(newtd->td_name));
  247         thread_lock(td);
  248         /* let the scheduler know about these things. */
  249         sched_fork_thread(td, newtd);
  250         thread_unlock(td);
  251         if (P_SHOULDSTOP(p))
  252                 newtd->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK;
  253         PROC_UNLOCK(p);
  254         thread_lock(newtd);
  255         if (rtp != NULL) {
  256                 if (!(td->td_pri_class == PRI_TIMESHARE &&
  257                       rtp->type == RTP_PRIO_NORMAL)) {
  258                         rtp_to_pri(rtp, newtd);
  259                         sched_prio(newtd, newtd->td_user_pri);
  260                 } /* ignore timesharing class */
  261         }
  262         TD_SET_CAN_RUN(newtd);
  263         sched_add(newtd, SRQ_BORING);
  264         thread_unlock(newtd);
  265 
  266         return (0);
  267 }
  268 
  269 int
  270 thr_self(struct thread *td, struct thr_self_args *uap)
  271     /* long *id */
  272 {
  273         int error;
  274 
  275         error = suword_lwpid(uap->id, (unsigned)td->td_tid);
  276         if (error == -1)
  277                 return (EFAULT);
  278         return (0);
  279 }
  280 
  281 int
  282 thr_exit(struct thread *td, struct thr_exit_args *uap)
  283     /* long *state */
  284 {
  285         struct proc *p;
  286 
  287         p = td->td_proc;
  288 
  289         /* Signal userland that it can free the stack. */
  290         if ((void *)uap->state != NULL) {
  291                 suword_lwpid(uap->state, 1);
  292                 kern_umtx_wake(td, uap->state, INT_MAX, 0);
  293         }
  294 
  295         PROC_LOCK(p);
  296         tdsigcleanup(td);
  297         PROC_SLOCK(p);
  298 
  299         /*
  300          * Shutting down last thread in the proc.  This will actually
  301          * call exit() in the trampoline when it returns.
  302          */
  303         if (p->p_numthreads != 1) {
  304                 thread_stopped(p);
  305                 thread_exit();
  306                 /* NOTREACHED */
  307         }
  308         PROC_SUNLOCK(p);
  309         PROC_UNLOCK(p);
  310         return (0);
  311 }
  312 
  313 int
  314 thr_kill(struct thread *td, struct thr_kill_args *uap)
  315     /* long id, int sig */
  316 {
  317         ksiginfo_t ksi;
  318         struct thread *ttd;
  319         struct proc *p;
  320         int error;
  321 
  322         p = td->td_proc;
  323         error = 0;
  324         ksiginfo_init(&ksi);
  325         ksi.ksi_signo = uap->sig;
  326         ksi.ksi_code = SI_USER;
  327         ksi.ksi_pid = p->p_pid;
  328         ksi.ksi_uid = td->td_ucred->cr_ruid;
  329         PROC_LOCK(p);
  330         if (uap->id == -1) {
  331                 if (uap->sig != 0 && !_SIG_VALID(uap->sig)) {
  332                         error = EINVAL;
  333                 } else {
  334                         error = ESRCH;
  335                         FOREACH_THREAD_IN_PROC(p, ttd) {
  336                                 if (ttd != td) {
  337                                         error = 0;
  338                                         if (uap->sig == 0)
  339                                                 break;
  340                                         tdsignal(p, ttd, uap->sig, &ksi);
  341                                 }
  342                         }
  343                 }
  344         } else {
  345                 if (uap->id != td->td_tid)
  346                         ttd = thread_find(p, uap->id);
  347                 else
  348                         ttd = td;
  349                 if (ttd == NULL)
  350                         error = ESRCH;
  351                 else if (uap->sig == 0)
  352                         ;
  353                 else if (!_SIG_VALID(uap->sig))
  354                         error = EINVAL;
  355                 else
  356                         tdsignal(p, ttd, uap->sig, &ksi);
  357         }
  358         PROC_UNLOCK(p);
  359         return (error);
  360 }
  361 
  362 int
  363 thr_kill2(struct thread *td, struct thr_kill2_args *uap)
  364     /* pid_t pid, long id, int sig */
  365 {
  366         ksiginfo_t ksi;
  367         struct thread *ttd;
  368         struct proc *p;
  369         int error;
  370 
  371         AUDIT_ARG_SIGNUM(uap->sig);
  372 
  373         if (uap->pid == td->td_proc->p_pid) {
  374                 p = td->td_proc;
  375                 PROC_LOCK(p);
  376         } else if ((p = pfind(uap->pid)) == NULL) {
  377                 return (ESRCH);
  378         }
  379         AUDIT_ARG_PROCESS(p);
  380 
  381         error = p_cansignal(td, p, uap->sig);
  382         if (error == 0) {
  383                 ksiginfo_init(&ksi);
  384                 ksi.ksi_signo = uap->sig;
  385                 ksi.ksi_code = SI_USER;
  386                 ksi.ksi_pid = td->td_proc->p_pid;
  387                 ksi.ksi_uid = td->td_ucred->cr_ruid;
  388                 if (uap->id == -1) {
  389                         if (uap->sig != 0 && !_SIG_VALID(uap->sig)) {
  390                                 error = EINVAL;
  391                         } else {
  392                                 error = ESRCH;
  393                                 FOREACH_THREAD_IN_PROC(p, ttd) {
  394                                         if (ttd != td) {
  395                                                 error = 0;
  396                                                 if (uap->sig == 0)
  397                                                         break;
  398                                                 tdsignal(p, ttd, uap->sig,
  399                                                     &ksi);
  400                                         }
  401                                 }
  402                         }
  403                 } else {
  404                         if (uap->id != td->td_tid)
  405                                 ttd = thread_find(p, uap->id);
  406                         else
  407                                 ttd = td;
  408                         if (ttd == NULL)
  409                                 error = ESRCH;
  410                         else if (uap->sig == 0)
  411                                 ;
  412                         else if (!_SIG_VALID(uap->sig))
  413                                 error = EINVAL;
  414                         else
  415                                 tdsignal(p, ttd, uap->sig, &ksi);
  416                 }
  417         }
  418         PROC_UNLOCK(p);
  419         return (error);
  420 }
  421 
  422 int
  423 thr_suspend(struct thread *td, struct thr_suspend_args *uap)
  424         /* const struct timespec *timeout */
  425 {
  426         struct timespec ts, *tsp;
  427         int error;
  428 
  429         tsp = NULL;
  430         if (uap->timeout != NULL) {
  431                 error = umtx_copyin_timeout(uap->timeout, &ts);
  432                 if (error != 0)
  433                         return (error);
  434                 tsp = &ts;
  435         }
  436 
  437         return (kern_thr_suspend(td, tsp));
  438 }
  439 
  440 int
  441 kern_thr_suspend(struct thread *td, struct timespec *tsp)
  442 {
  443         struct timeval tv;
  444         int error = 0, hz = 0;
  445 
  446         if (tsp != NULL) {
  447                 if (tsp->tv_sec == 0 && tsp->tv_nsec == 0)
  448                         return (ETIMEDOUT);
  449                 TIMESPEC_TO_TIMEVAL(&tv, tsp);
  450                 hz = tvtohz(&tv);
  451         }
  452 
  453         if (td->td_pflags & TDP_WAKEUP) {
  454                 td->td_pflags &= ~TDP_WAKEUP;
  455                 return (0);
  456         }
  457 
  458         PROC_LOCK(td->td_proc);
  459         if ((td->td_flags & TDF_THRWAKEUP) == 0)
  460                 error = msleep((void *)td, &td->td_proc->p_mtx, PCATCH, "lthr",
  461                     hz);
  462         if (td->td_flags & TDF_THRWAKEUP) {
  463                 thread_lock(td);
  464                 td->td_flags &= ~TDF_THRWAKEUP;
  465                 thread_unlock(td);
  466                 PROC_UNLOCK(td->td_proc);
  467                 return (0);
  468         }
  469         PROC_UNLOCK(td->td_proc);
  470         if (error == EWOULDBLOCK)
  471                 error = ETIMEDOUT;
  472         else if (error == ERESTART) {
  473                 if (hz != 0)
  474                         error = EINTR;
  475         }
  476         return (error);
  477 }
  478 
  479 int
  480 thr_wake(struct thread *td, struct thr_wake_args *uap)
  481         /* long id */
  482 {
  483         struct proc *p;
  484         struct thread *ttd;
  485 
  486         if (uap->id == td->td_tid) {
  487                 td->td_pflags |= TDP_WAKEUP;
  488                 return (0);
  489         } 
  490 
  491         p = td->td_proc;
  492         PROC_LOCK(p);
  493         ttd = thread_find(p, uap->id);
  494         if (ttd == NULL) {
  495                 PROC_UNLOCK(p);
  496                 return (ESRCH);
  497         }
  498         thread_lock(ttd);
  499         ttd->td_flags |= TDF_THRWAKEUP;
  500         thread_unlock(ttd);
  501         wakeup((void *)ttd);
  502         PROC_UNLOCK(p);
  503         return (0);
  504 }
  505 
  506 int
  507 thr_set_name(struct thread *td, struct thr_set_name_args *uap)
  508 {
  509         struct proc *p = td->td_proc;
  510         char name[MAXCOMLEN + 1];
  511         struct thread *ttd;
  512         int error;
  513 
  514         error = 0;
  515         name[0] = '\0';
  516         if (uap->name != NULL) {
  517                 error = copyinstr(uap->name, name, sizeof(name),
  518                         NULL);
  519                 if (error)
  520                         return (error);
  521         }
  522         PROC_LOCK(p);
  523         if (uap->id == td->td_tid)
  524                 ttd = td;
  525         else
  526                 ttd = thread_find(p, uap->id);
  527         if (ttd != NULL)
  528                 strcpy(ttd->td_name, name);
  529         else 
  530                 error = ESRCH;
  531 #ifdef KTR
  532         sched_clear_tdname(ttd);
  533 #endif
  534         PROC_UNLOCK(p);
  535         return (error);
  536 }

Cache object: 1057ee5ae1b398b9787ccd68faa61a95


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.