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