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 2010 Sun Microsystems, Inc.  All rights reserved.
  23  * Use is subject to license terms.
  24  */
  25 
  26 #include <sys/errno.h>
  27 #include <sys/systm.h>
  28 #include <sys/archsystm.h>
  29 #include <sys/privregs.h>
  30 #include <sys/exec.h>
  31 #include <sys/lwp.h>
  32 #include <sys/sem.h>
  33 #include <sys/brand.h>
  34 #include <sys/lx_brand.h>
  35 #include <sys/lx_pid.h>
  36 #include <sys/lx_futex.h>
  37 
  38 /* Linux specific functions and definitions */
  39 void lx_setrval(klwp_t *, int, int);
  40 void lx_exec();
  41 int lx_initlwp(klwp_t *);
  42 void lx_forklwp(klwp_t *, klwp_t *);
  43 void lx_exitlwp(klwp_t *);
  44 void lx_freelwp(klwp_t *);
  45 static void lx_save(klwp_t *);
  46 static void lx_restore(klwp_t *);
  47 extern void lx_ptrace_free(proc_t *);
  48 
  49 /*
  50  * Set the return code for the forked child, always zero
  51  */
  52 /*ARGSUSED*/
  53 void
  54 lx_setrval(klwp_t *lwp, int v1, int v2)
  55 {
  56         lwptoregs(lwp)->r_r0 = 0;
  57 }
  58 
  59 /*
  60  * Reset process state on exec(2)
  61  */
  62 void
  63 lx_exec()
  64 {
  65         klwp_t *lwp = ttolwp(curthread);
  66         struct lx_lwp_data *lwpd = lwptolxlwp(lwp);
  67         int err;
  68 
  69         /*
  70          * There are two mutually exclusive special cases we need to
  71          * address.  First, if this was a native process prior to this
  72          * exec(), then this lwp won't have its brand-specific data
  73          * initialized and it won't be assigned a Linux PID yet.  Second,
  74          * if this was a multi-threaded Linux process and this lwp wasn't
  75          * the main lwp, then we need to make its Solaris and Linux PIDS
  76          * match.
  77          */
  78         if (lwpd == NULL) {
  79                 err = lx_initlwp(lwp);
  80                 /*
  81                  * Only possible failure from this routine should be an
  82                  * inability to allocate a new PID.  Since single-threaded
  83                  * processes don't need a new PID, we should never hit this
  84                  * error.
  85                  */
  86                 ASSERT(err == 0);
  87                 lwpd = lwptolxlwp(lwp);
  88         } else if (curthread->t_tid != 1) {
  89                 lx_pid_reassign(curthread);
  90         }
  91 
  92         installctx(lwptot(lwp), lwp, lx_save, lx_restore, NULL, NULL, lx_save,
  93             NULL);
  94 
  95         /*
  96          * clear out the tls array
  97          */
  98         bzero(lwpd->br_tls, sizeof (lwpd->br_tls));
  99 
 100         /*
 101          * reset the tls entries in the gdt
 102          */
 103         kpreempt_disable();
 104         lx_restore(lwp);
 105         kpreempt_enable();
 106 }
 107 
 108 void
 109 lx_exitlwp(klwp_t *lwp)
 110 {
 111         struct lx_lwp_data *lwpd = lwptolxlwp(lwp);
 112         proc_t *p;
 113         kthread_t *t;
 114         sigqueue_t *sqp = NULL;
 115         pid_t ppid;
 116         id_t ptid;
 117 
 118         if (lwpd == NULL)
 119                 return;         /* second time thru' */
 120 
 121         if (lwpd->br_clear_ctidp != NULL) {
 122                 (void) suword32(lwpd->br_clear_ctidp, 0);
 123                 (void) lx_futex((uintptr_t)lwpd->br_clear_ctidp, FUTEX_WAKE, 1,
 124                     NULL, NULL, 0);
 125         }
 126 
 127         if (lwpd->br_signal != 0) {
 128                 /*
 129                  * The first thread in a process doesn't cause a signal to
 130                  * be sent when it exits.  It was created by a fork(), not
 131                  * a clone(), so the parent should get signalled when the
 132                  * process exits.
 133                  */
 134                 if (lwpd->br_ptid == -1)
 135                         goto free;
 136 
 137                 sqp = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP);
 138                 /*
 139                  * If br_ppid is 0, it means this is a CLONE_PARENT thread,
 140                  * so the signal goes to the parent process - not to a
 141                  * specific thread in this process.
 142                  */
 143                 p = lwptoproc(lwp);
 144                 if (lwpd->br_ppid == 0) {
 145                         mutex_enter(&p->p_lock);
 146                         ppid = p->p_ppid;
 147                         t = NULL;
 148                 } else {
 149                         /*
 150                          * If we have been reparented to init or if our
 151                          * parent thread is gone, then nobody gets
 152                          * signaled.
 153                          */
 154                         if ((lx_lwp_ppid(lwp, &ppid, &ptid) == 1) ||
 155                             (ptid == -1))
 156                                 goto free;
 157 
 158                         mutex_enter(&pidlock);
 159                         if ((p = prfind(ppid)) == NULL || p->p_stat == SIDL) {
 160                                 mutex_exit(&pidlock);
 161                                 goto free;
 162                         }
 163                         mutex_enter(&p->p_lock);
 164                         mutex_exit(&pidlock);
 165 
 166                         if ((t = idtot(p, ptid)) == NULL) {
 167                                 mutex_exit(&p->p_lock);
 168                                 goto free;
 169                         }
 170                 }
 171 
 172                 sqp->sq_info.si_signo = lwpd->br_signal;
 173                 sqp->sq_info.si_code = lwpd->br_exitwhy;
 174                 sqp->sq_info.si_status = lwpd->br_exitwhat;
 175                 sqp->sq_info.si_pid = lwpd->br_pid;
 176                 sqp->sq_info.si_uid = crgetruid(CRED());
 177                 sigaddqa(p, t, sqp);
 178                 mutex_exit(&p->p_lock);
 179                 sqp = NULL;
 180         }
 181 
 182 free:
 183         if (sqp)
 184                 kmem_free(sqp, sizeof (sigqueue_t));
 185 
 186         lx_freelwp(lwp);
 187 }
 188 
 189 void
 190 lx_freelwp(klwp_t *lwp)
 191 {
 192         struct lx_lwp_data *lwpd = lwptolxlwp(lwp);
 193 
 194         if (lwpd != NULL) {
 195                 (void) removectx(lwptot(lwp), lwp, lx_save, lx_restore,
 196                     NULL, NULL, lx_save, NULL);
 197                 if (lwpd->br_pid != 0)
 198                         lx_pid_rele(lwptoproc(lwp)->p_pid,
 199                             lwptot(lwp)->t_tid);
 200 
 201                 lwp->lwp_brand = NULL;
 202                 kmem_free(lwpd, sizeof (struct lx_lwp_data));
 203         }
 204 }
 205 
 206 int
 207 lx_initlwp(klwp_t *lwp)
 208 {
 209         struct lx_lwp_data *lwpd;
 210         struct lx_lwp_data *plwpd;
 211         kthread_t *tp = lwptot(lwp);
 212 
 213         lwpd = kmem_zalloc(sizeof (struct lx_lwp_data), KM_SLEEP);
 214         lwpd->br_exitwhy = CLD_EXITED;
 215         lwpd->br_lwp = lwp;
 216         lwpd->br_clear_ctidp = NULL;
 217         lwpd->br_set_ctidp = NULL;
 218         lwpd->br_signal = 0;
 219         /*
 220          * lwpd->br_affinitymask was zeroed by kmem_zalloc().
 221          */
 222 
 223         /*
 224          * The first thread in a process has ppid set to the parent
 225          * process's pid, and ptid set to -1.  Subsequent threads in the
 226          * process have their ppid set to the pid of the thread that
 227          * created them, and their ptid to that thread's tid.
 228          */
 229         if (tp->t_next == tp) {
 230                 lwpd->br_ppid = tp->t_procp->p_ppid;
 231                 lwpd->br_ptid = -1;
 232         } else if (ttolxlwp(curthread) != NULL) {
 233                 plwpd = ttolxlwp(curthread);
 234                 bcopy(plwpd->br_tls, lwpd->br_tls, sizeof (lwpd->br_tls));
 235                 lwpd->br_ppid = plwpd->br_pid;
 236                 lwpd->br_ptid = curthread->t_tid;
 237         } else {
 238                 /*
 239                  * Oddball case: the parent thread isn't a Linux process.
 240                  */
 241                 lwpd->br_ppid = 0;
 242                 lwpd->br_ptid = -1;
 243         }
 244         lwp->lwp_brand = lwpd;
 245 
 246         if (lx_pid_assign(tp)) {
 247                 kmem_free(lwpd, sizeof (struct lx_lwp_data));
 248                 lwp->lwp_brand = NULL;
 249                 return (-1);
 250         }
 251         lwpd->br_tgid = lwpd->br_pid;
 252 
 253         installctx(lwptot(lwp), lwp, lx_save, lx_restore, NULL, NULL,
 254             lx_save, NULL);
 255 
 256         return (0);
 257 }
 258 
 259 /*
 260  * There is no need to have any locking for either the source or
 261  * destination struct lx_lwp_data structs.  This is always run in the
 262  * thread context of the source thread, and the destination thread is
 263  * always newly created and not referred to from anywhere else.
 264  */
 265 void
 266 lx_forklwp(klwp_t *srclwp, klwp_t *dstlwp)
 267 {
 268         struct lx_lwp_data *src = srclwp->lwp_brand;
 269         struct lx_lwp_data *dst = dstlwp->lwp_brand;
 270 
 271         dst->br_ppid = src->br_pid;
 272         dst->br_ptid = lwptot(srclwp)->t_tid;
 273         bcopy(src->br_tls, dst->br_tls, sizeof (dst->br_tls));
 274 
 275         /*
 276          * copy only these flags
 277          */
 278         dst->br_lwp_flags = src->br_lwp_flags & BR_CPU_BOUND;
 279         dst->br_clone_args = NULL;
 280 }
 281 
 282 /*
 283  * When switching a Linux process off the CPU, clear its GDT entries.
 284  */
 285 /* ARGSUSED */
 286 static void
 287 lx_save(klwp_t *t)
 288 {
 289         int i;
 290 
 291 #if defined(__amd64)
 292         reset_sregs();
 293 #endif
 294         for (i = 0; i < LX_TLSNUM; i++)
 295                 gdt_update_usegd(GDT_TLSMIN + i, &null_udesc);
 296 }
 297 
 298 /*
 299  * When switching a Linux process on the CPU, set its GDT entries.
 300  */
 301 static void
 302 lx_restore(klwp_t *t)
 303 {
 304         struct lx_lwp_data *lwpd = lwptolxlwp(t);
 305         user_desc_t *tls;
 306         int i;
 307 
 308         ASSERT(lwpd);
 309 
 310         tls = lwpd->br_tls;
 311         for (i = 0; i < LX_TLSNUM; i++)
 312                 gdt_update_usegd(GDT_TLSMIN + i, &tls[i]);
 313 }
 314 
 315 void
 316 lx_set_gdt(int entry, user_desc_t *descrp)
 317 {
 318 
 319         gdt_update_usegd(entry, descrp);
 320 }
 321 
 322 void
 323 lx_clear_gdt(int entry)
 324 {
 325         gdt_update_usegd(entry, &null_udesc);
 326 }
 327 
 328 longlong_t
 329 lx_nosys()
 330 {
 331         return (set_errno(ENOSYS));
 332 }
 333 
 334 longlong_t
 335 lx_opnotsupp()
 336 {
 337         return (set_errno(EOPNOTSUPP));
 338 }
 339 
 340 /*
 341  * Brand-specific routine to check if given non-Solaris standard segment
 342  * register values should be modified to other values.
 343  */
 344 /*ARGSUSED*/
 345 greg_t
 346 lx_fixsegreg(greg_t sr, model_t datamodel)
 347 {
 348         ASSERT(sr == (sr & 0xffff));
 349 
 350         /*
 351          * Force the SR into the LDT in ring 3 for 32-bit processes.
 352          *
 353          * 64-bit processes get the null GDT selector since they are not
 354          * allowed to have a private LDT.
 355          */
 356 #if defined(__amd64)
 357         return (datamodel == DATAMODEL_ILP32 ? (sr | SEL_TI_LDT | SEL_UPL) : 0);
 358 #elif defined(__i386)
 359         datamodel = datamodel;  /* datamodel currently unused for 32-bit */
 360         return (sr | SEL_TI_LDT | SEL_UPL);
 361 #endif  /* __amd64 */
 362 }