1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 /*
  22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
  23  * Use is subject to license terms.
  24  */
  25 
  26 /*
  27  * Copyright 2016 Joyent, Inc.
  28  */
  29 
  30 #ifndef _SYS_KLWP_H
  31 #define _SYS_KLWP_H
  32 
  33 #include <sys/types.h>
  34 #include <sys/condvar.h>
  35 #include <sys/thread.h>
  36 #include <sys/signal.h>
  37 #include <sys/siginfo.h>
  38 #include <sys/pcb.h>
  39 #include <sys/time.h>
  40 #include <sys/msacct.h>
  41 #include <sys/ucontext.h>
  42 #include <sys/lwp.h>
  43 #include <sys/contract.h>
  44 
  45 #if (defined(_KERNEL) || defined(_KMEMUSER)) && defined(_MACHDEP)
  46 #include <sys/machparam.h>
  47 #endif
  48 
  49 #ifdef  __cplusplus
  50 extern "C" {
  51 #endif
  52 
  53 /*
  54  * The light-weight process object and the methods by which it
  55  * is accessed.
  56  */
  57 
  58 #define MAXSYSARGS      8       /* Maximum # of arguments passed to a syscall */
  59 
  60 /* lwp_eosys values */
  61 #define NORMALRETURN    0       /* normal return; adjusts PC, registers */
  62 #define JUSTRETURN      1       /* just return, leave registers alone */
  63 
  64 /*
  65  * Resource usage, per-lwp plus per-process (sum over defunct lwps).
  66  */
  67 struct lrusage {
  68         u_longlong_t    minflt;         /* minor page faults */
  69         u_longlong_t    majflt;         /* major page faults */
  70         u_longlong_t    nswap;          /* swaps */
  71         u_longlong_t    inblock;        /* input blocks */
  72         u_longlong_t    oublock;        /* output blocks */
  73         u_longlong_t    msgsnd;         /* messages sent */
  74         u_longlong_t    msgrcv;         /* messages received */
  75         u_longlong_t    nsignals;       /* signals received */
  76         u_longlong_t    nvcsw;          /* voluntary context switches */
  77         u_longlong_t    nivcsw;         /* involuntary context switches */
  78         u_longlong_t    sysc;           /* system calls */
  79         u_longlong_t    ioch;           /* chars read and written */
  80 };
  81 
  82 typedef struct _klwp    *klwp_id_t;
  83 
  84 typedef struct _klwp {
  85         /*
  86          * user-mode context
  87          */
  88         struct pcb      lwp_pcb;                /* user regs save pcb */
  89         uintptr_t       lwp_oldcontext;         /* previous user context */
  90 
  91         /*
  92          * system-call interface
  93          */
  94         long    *lwp_ap;        /* pointer to arglist */
  95         int     lwp_errno;      /* error for current syscall (private) */
  96         /*
  97          * support for I/O
  98          */
  99         char    lwp_error;      /* return error code */
 100         char    lwp_eosys;      /* special action on end of syscall */
 101         char    lwp_argsaved;   /* are all args in lwp_arg */
 102         char    lwp_watchtrap;  /* lwp undergoing watchpoint single-step */
 103         long    lwp_arg[MAXSYSARGS];    /* args to current syscall */
 104         void    *lwp_regs;      /* pointer to saved regs on stack */
 105         void    *lwp_fpu;       /* pointer to fpu regs */
 106         label_t lwp_qsav;       /* longjmp label for quits and interrupts */
 107 
 108         /*
 109          * signal handling and debugger (/proc) interface
 110          */
 111         uchar_t lwp_cursig;             /* current signal */
 112         uchar_t lwp_curflt;             /* current fault */
 113         uchar_t lwp_sysabort;           /* if set, abort syscall */
 114         uchar_t lwp_asleep;             /* lwp asleep in syscall */
 115         uchar_t lwp_extsig;             /* cursig sent from another contract */
 116         stack_t lwp_sigaltstack;        /* alternate signal stack */
 117         struct sigqueue *lwp_curinfo;   /* siginfo for current signal */
 118         k_siginfo_t     lwp_siginfo;    /* siginfo for stop-on-fault */
 119         k_sigset_t      lwp_sigoldmask; /* for sigsuspend */
 120         struct lwp_watch {              /* used in watchpoint single-stepping */
 121                 caddr_t wpaddr;
 122                 size_t  wpsize;
 123                 int     wpcode;
 124                 int     wpmapped;
 125                 greg_t  wppc;
 126         } lwp_watch[4];         /* one for each of exec/write/read/read */
 127 
 128         uint32_t lwp_oweupc;            /* profil(2) ticks owed to this lwp */
 129 
 130         /*
 131          * Microstate accounting.  Timestamps are made at the start and the
 132          * end of each microstate (see <sys/msacct.h> for state definitions)
 133          * and the corresponding accounting info is updated.  The current
 134          * microstate is kept in the thread struct, since there are cases
 135          * when one thread must update another thread's state (a no-no
 136          * for an lwp since it may be swapped/paged out).  The rest of the
 137          * microstate stuff is kept here to avoid wasting space on things
 138          * like kernel threads that don't have an associated lwp.
 139          */
 140         struct mstate {
 141                 int ms_prev;                    /* previous running mstate */
 142                 hrtime_t ms_start;              /* lwp creation time */
 143                 hrtime_t ms_term;               /* lwp termination time */
 144                 hrtime_t ms_state_start;        /* start time of this mstate */
 145                 hrtime_t ms_acct[NMSTATES];     /* per mstate accounting */
 146         } lwp_mstate;
 147 
 148         /*
 149          * Per-lwp resource usage.
 150          */
 151         struct lrusage lwp_ru;
 152 
 153         /*
 154          * Things to keep for real-time (SIGPROF) profiling.
 155          */
 156         int     lwp_lastfault;
 157         caddr_t lwp_lastfaddr;
 158 
 159         /*
 160          * timers. Protected by lwp->procp->p_lock
 161          */
 162         struct itimerval lwp_timer[3];
 163 
 164         /*
 165          * used to stop/alert lwps
 166          */
 167         char    lwp_unused;
 168         char    lwp_state;      /* Running in User/Kernel mode (no lock req) */
 169         ushort_t lwp_nostop;    /* Don't stop this lwp (no lock required) */
 170         ushort_t lwp_pad;       /* Reserved for future use */
 171 
 172         /*
 173          * Last failed privilege.
 174          */
 175         short   lwp_badpriv;
 176 
 177         /*
 178          * linkage
 179          */
 180         struct _kthread *lwp_thread;
 181         struct proc     *lwp_procp;
 182 
 183         size_t lwp_childstksz;  /* kernel stksize for this lwp's descendants */
 184 
 185         uintptr_t       lwp_ustack;             /* current stack bounds */
 186         size_t          lwp_old_stk_ctl;        /* old stack limit */
 187 
 188         /*
 189          * Contracts
 190          */
 191         struct ct_template *lwp_ct_active[CTT_MAXTYPE]; /* active templates */
 192         struct contract *lwp_ct_latest[CTT_MAXTYPE]; /* last created contract */
 193 
 194         /*
 195          * Branding:
 196          * lwp_brand                    - per-lwp brand data
 197          * lwp_brand_syscall            - brand syscall interposer
 198          */
 199         void    *lwp_brand;
 200         int     (*lwp_brand_syscall)(void);
 201 
 202         struct psinfo *lwp_spymaster;   /* if an agent LWP, our spymaster */
 203 } klwp_t;
 204 
 205 /* lwp states */
 206 #define LWP_USER        0x01            /* Running in user mode */
 207 #define LWP_SYS         0x02            /* Running in kernel mode */
 208 
 209 #if     defined(_KERNEL)
 210 extern  int     lwp_default_stksize;
 211 extern  int     lwp_reapcnt;
 212 
 213 extern  struct _kthread *lwp_deathrow;
 214 extern  kmutex_t        reaplock;
 215 extern  struct kmem_cache *lwp_cache;
 216 extern  void            *segkp_lwp;
 217 extern  klwp_t          lwp0;
 218 
 219 /* where newly-created lwps normally start */
 220 extern  void    lwp_rtt(void);
 221 
 222 #endif  /* _KERNEL */
 223 
 224 #ifdef  __cplusplus
 225 }
 226 #endif
 227 
 228 #endif  /* _SYS_KLWP_H */