s10_brand.c revision 12199:2dbcb597eb37
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/*
23 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
24 */
25
26#include <sys/errno.h>
27#include <sys/exec.h>
28#include <sys/file.h>
29#include <sys/kmem.h>
30#include <sys/modctl.h>
31#include <sys/model.h>
32#include <sys/proc.h>
33#include <sys/syscall.h>
34#include <sys/systm.h>
35#include <sys/thread.h>
36#include <sys/cmn_err.h>
37#include <sys/archsystm.h>
38#include <sys/pathname.h>
39#include <sys/sunddi.h>
40
41#include <sys/machbrand.h>
42#include <sys/brand.h>
43#include "s10_brand.h"
44
45char *s10_emulation_table = NULL;
46
47void	s10_init_brand_data(zone_t *);
48void	s10_free_brand_data(zone_t *);
49void	s10_setbrand(proc_t *);
50int	s10_getattr(zone_t *, int, void *, size_t *);
51int	s10_setattr(zone_t *, int, void *, size_t);
52int	s10_brandsys(int, int64_t *, uintptr_t, uintptr_t, uintptr_t,
53		uintptr_t, uintptr_t, uintptr_t);
54void	s10_copy_procdata(proc_t *, proc_t *);
55void	s10_proc_exit(struct proc *, klwp_t *);
56void	s10_exec();
57int	s10_initlwp(klwp_t *);
58void	s10_forklwp(klwp_t *, klwp_t *);
59void	s10_freelwp(klwp_t *);
60void	s10_lwpexit(klwp_t *);
61int	s10_elfexec(vnode_t *, execa_t *, uarg_t *, intpdata_t *, int,
62	long *, int, caddr_t, cred_t *, int);
63void	s10_sigset_native_to_s10(sigset_t *);
64void	s10_sigset_s10_to_native(sigset_t *);
65
66/* s10 brand */
67struct brand_ops s10_brops = {
68	s10_init_brand_data,
69	s10_free_brand_data,
70	s10_brandsys,
71	s10_setbrand,
72	s10_getattr,
73	s10_setattr,
74	s10_copy_procdata,
75	s10_proc_exit,
76	s10_exec,
77	lwp_setrval,
78	s10_initlwp,
79	s10_forklwp,
80	s10_freelwp,
81	s10_lwpexit,
82	s10_elfexec,
83	s10_sigset_native_to_s10,
84	s10_sigset_s10_to_native,
85	S10_NSIG,
86};
87
88#ifdef	sparc
89
90struct brand_mach_ops s10_mops = {
91	s10_brand_syscall_callback,
92	s10_brand_syscall32_callback
93};
94
95#else	/* sparc */
96
97#ifdef	__amd64
98
99struct brand_mach_ops s10_mops = {
100	s10_brand_sysenter_callback,
101	NULL,
102	s10_brand_int91_callback,
103	s10_brand_syscall_callback,
104	s10_brand_syscall32_callback,
105	NULL
106};
107
108#else	/* ! __amd64 */
109
110struct brand_mach_ops s10_mops = {
111	s10_brand_sysenter_callback,
112	NULL,
113	NULL,
114	s10_brand_syscall_callback,
115	NULL,
116	NULL
117};
118#endif	/* __amd64 */
119
120#endif	/* _sparc */
121
122struct brand	s10_brand = {
123	BRAND_VER_1,
124	"solaris10",
125	&s10_brops,
126	&s10_mops
127};
128
129static struct modlbrand modlbrand = {
130	&mod_brandops,		/* type of module */
131	"Solaris 10 Brand",	/* description of module */
132	&s10_brand		/* driver ops */
133};
134
135static struct modlinkage modlinkage = {
136	MODREV_1, (void *)&modlbrand, NULL
137};
138
139void
140s10_setbrand(proc_t *p)
141{
142	brand_solaris_setbrand(p, &s10_brand);
143}
144
145/*ARGSUSED*/
146int
147s10_getattr(zone_t *zone, int attr, void *buf, size_t *bufsize)
148{
149	ASSERT(zone->zone_brand == &s10_brand);
150	if (attr == S10_EMUL_BITMAP) {
151		if (buf == NULL || *bufsize != sizeof (s10_emul_bitmap_t))
152			return (EINVAL);
153		if (copyout(((s10_zone_data_t *)zone->zone_brand_data)->
154		    emul_bitmap, buf, sizeof (s10_emul_bitmap_t)) != 0)
155			return (EFAULT);
156		return (0);
157	}
158
159	return (EINVAL);
160}
161
162int
163s10_setattr(zone_t *zone, int attr, void *buf, size_t bufsize)
164{
165	ASSERT(zone->zone_brand == &s10_brand);
166	if (attr == S10_EMUL_BITMAP) {
167		if (buf == NULL || bufsize != sizeof (s10_emul_bitmap_t))
168			return (EINVAL);
169		if (copyin(buf, ((s10_zone_data_t *)zone->zone_brand_data)->
170		    emul_bitmap, sizeof (s10_emul_bitmap_t)) != 0)
171			return (EFAULT);
172		return (0);
173	}
174
175	return (EINVAL);
176}
177
178#ifdef	__amd64
179/*
180 * The Nevada kernel clears %fs for threads in 64-bit x86 processes but S10's
181 * libc expects %fs to be nonzero.  This causes some committed
182 * libc/libthread interfaces (e.g., thr_main()) to fail, which impacts several
183 * libraries, including libdoor.  This function sets the specified LWP's %fs
184 * register to the legacy S10 selector value (LWPFS_SEL).
185 *
186 * The best solution to the aforementioned problem is backporting CRs
187 * 6467491 to Solaris 10 so that 64-bit x86 Solaris 10 processes
188 * would accept zero for %fs.  Backporting the CRs is a requirement for running
189 * S10 Containers in PV domUs because 64-bit Xen clears %fsbase when %fs is
190 * nonzero.  Such behavior breaks 64-bit processes because Xen has to fetch the
191 * FS segments' base addresses from the LWPs' GDTs, which are only capable of
192 * 32-bit addressing.
193 */
194/*ARGSUSED*/
195static void
196s10_amd64_correct_fsreg(klwp_t *l)
197{
198	if (lwp_getdatamodel(l) == DATAMODEL_NATIVE) {
199		kpreempt_disable();
200		l->lwp_pcb.pcb_fs = LWPFS_SEL;
201		l->lwp_pcb.pcb_rupdate = 1;
202		lwptot(l)->t_post_sys = 1;	/* Guarantee update_sregs() */
203		kpreempt_enable();
204	}
205}
206#endif	/* __amd64 */
207
208int
209s10_native()
210{
211	struct user	*up = PTOU(curproc);
212	char		*args_new, *comm_new, *p;
213	int		len;
214
215	len = sizeof (BRAND_NATIVE_LINKER32 " ") - 1;
216
217	/*
218	 * Make sure that the process' interpreter is the native dynamic linker.
219	 * Convention dictates that native processes executing within solaris10-
220	 * branded zones are interpreted by the native dynamic linker (the
221	 * process and its arguments are specified as arguments to the dynamic
222	 * linker).  If this convention is violated (i.e.,
223	 * brandsys(B_S10_NATIVE, ...) is invoked by a process that shouldn't be
224	 * native), then do nothing and silently indicate success.
225	 */
226	if (strcmp(up->u_comm, S10_LINKER_NAME) != 0)
227		return (0);
228	if (strncmp(up->u_psargs, BRAND_NATIVE_LINKER64 " /", len + 4) == 0)
229		len += 3;		/* to account for "/64" in the path */
230	else if (strncmp(up->u_psargs, BRAND_NATIVE_LINKER32 " /", len + 1)
231	    != 0)
232		return (0);
233
234	args_new = strdup(&up->u_psargs[len]);
235	if ((p = strchr(args_new, ' ')) != NULL)
236		*p = '\0';
237	if ((comm_new = strrchr(args_new, '/')) != NULL)
238		comm_new = strdup(comm_new + 1);
239	else
240		comm_new = strdup(args_new);
241	if (p != NULL)
242		*p = ' ';
243
244	if ((strlen(args_new) != 0) && (strlen(comm_new) != 0)) {
245		mutex_enter(&curproc->p_lock);
246		(void) strlcpy(up->u_comm, comm_new, MAXCOMLEN+1);
247		(void) strlcpy(up->u_psargs, args_new, PSARGSZ);
248		mutex_exit(&curproc->p_lock);
249	}
250
251	strfree(args_new);
252	strfree(comm_new);
253	return (0);
254}
255
256/*ARGSUSED*/
257int
258s10_brandsys(int cmd, int64_t *rval, uintptr_t arg1, uintptr_t arg2,
259    uintptr_t arg3, uintptr_t arg4, uintptr_t arg5, uintptr_t arg6)
260{
261	proc_t	*p = curproc;
262	int	res;
263
264	*rval = 0;
265
266	if (cmd == B_S10_NATIVE)
267		return (s10_native());
268
269	res = brand_solaris_cmd(cmd, arg1, arg2, arg3, &s10_brand, S10_VERSION);
270	if (res >= 0)
271		return (res);
272
273	switch ((cmd)) {
274	case B_S10_PIDINFO:
275		/*
276		 * The s10 brand needs to be able to get the pid of the
277		 * current process and the pid of the zone's init, and it
278		 * needs to do this on every process startup.  Early in
279		 * brand startup, we can't call getpid() because calls to
280		 * getpid() represent a magical signal to some old-skool
281		 * debuggers.  By merging all of this into one call, we
282		 * make this quite a bit cheaper and easier to handle in
283		 * the brand module.
284		 */
285		if (copyout(&p->p_pid, (void *)arg1, sizeof (pid_t)) != 0)
286			return (EFAULT);
287		if (copyout(&p->p_zone->zone_proc_initpid, (void *)arg2,
288		    sizeof (pid_t)) != 0)
289			return (EFAULT);
290		return (0);
291
292	case B_S10_ISFDXATTRDIR: {
293		/*
294		 * This subcommand enables the userland brand emulation library
295		 * to determine whether a file descriptor refers to an extended
296		 * file attributes directory.  There is no standard syscall or
297		 * libc function that can make such a determination.
298		 */
299		file_t *dir_filep;
300
301		dir_filep = getf((int)arg1);
302		if (dir_filep == NULL)
303			return (EBADF);
304		ASSERT(dir_filep->f_vnode != NULL);
305		*rval = IS_XATTRDIR(dir_filep->f_vnode);
306		releasef((int)arg1);
307		return (0);
308	}
309
310#ifdef	__amd64
311	case B_S10_FSREGCORRECTION:
312		/*
313		 * This subcommand exists so that the SYS_lwp_private and
314		 * SYS_lwp_create syscalls can manually set the current thread's
315		 * %fs register to the legacy S10 selector value for 64-bit x86
316		 * processes.
317		 */
318		s10_amd64_correct_fsreg(ttolwp(curthread));
319		return (0);
320#endif	/* __amd64 */
321	}
322
323	return (EINVAL);
324}
325
326void
327s10_copy_procdata(proc_t *child, proc_t *parent)
328{
329	brand_solaris_copy_procdata(child, parent, &s10_brand);
330}
331
332void
333s10_proc_exit(struct proc *p, klwp_t *l)
334{
335	brand_solaris_proc_exit(p, l, &s10_brand);
336}
337
338void
339s10_exec()
340{
341	brand_solaris_exec(&s10_brand);
342}
343
344int
345s10_initlwp(klwp_t *l)
346{
347	return (brand_solaris_initlwp(l, &s10_brand));
348}
349
350void
351s10_forklwp(klwp_t *p, klwp_t *c)
352{
353	brand_solaris_forklwp(p, c, &s10_brand);
354
355#ifdef	__amd64
356	/*
357	 * Only correct the child's %fs register if the parent's %fs register
358	 * is LWPFS_SEL.  If the parent's %fs register is zero, then the Solaris
359	 * 10 environment that we're emulating uses a version of libc that
360	 * works when %fs is zero (i.e., it contains backports of CRs 6467491
361	 * and 6501650).
362	 */
363	if (p->lwp_pcb.pcb_fs == LWPFS_SEL)
364		s10_amd64_correct_fsreg(c);
365#endif	/* __amd64 */
366}
367
368void
369s10_freelwp(klwp_t *l)
370{
371	brand_solaris_freelwp(l, &s10_brand);
372}
373
374void
375s10_lwpexit(klwp_t *l)
376{
377	brand_solaris_lwpexit(l, &s10_brand);
378}
379
380void
381s10_free_brand_data(zone_t *zone)
382{
383	kmem_free(zone->zone_brand_data, sizeof (s10_zone_data_t));
384}
385
386void
387s10_init_brand_data(zone_t *zone)
388{
389	ASSERT(zone->zone_brand == &s10_brand);
390	ASSERT(zone->zone_brand_data == NULL);
391	zone->zone_brand_data = kmem_zalloc(sizeof (s10_zone_data_t), KM_SLEEP);
392}
393
394int
395s10_elfexec(vnode_t *vp, execa_t *uap, uarg_t *args, intpdata_t *idatap,
396	int level, long *execsz, int setid, caddr_t exec_file, cred_t *cred,
397	int brand_action)
398{
399	return (brand_solaris_elfexec(vp, uap, args, idatap, level, execsz,
400	    setid, exec_file, cred, brand_action, &s10_brand, S10_BRANDNAME,
401	    S10_LIB, S10_LIB32, S10_LINKER, S10_LINKER32));
402}
403
404void
405s10_sigset_native_to_s10(sigset_t *set)
406{
407	int nativesig;
408	int s10sig;
409	sigset_t s10set;
410
411	/*
412	 * Shortcut: we know the first 32 signals are the same in both
413	 * s10 and native Solaris.  Just assign the first word.
414	 */
415	s10set.__sigbits[0] = set->__sigbits[0];
416	s10set.__sigbits[1] = 0;
417	s10set.__sigbits[2] = 0;
418	s10set.__sigbits[3] = 0;
419
420	/*
421	 * Copy the remainder of the initial set of common signals.
422	 */
423	for (nativesig = 33; nativesig < S10_SIGRTMIN; nativesig++)
424		if (sigismember(set, nativesig))
425			sigaddset(&s10set, nativesig);
426
427	/*
428	 * Convert any native RT signals to their S10 values.
429	 */
430	for (nativesig = _SIGRTMIN, s10sig = S10_SIGRTMIN;
431	    nativesig <= _SIGRTMAX && s10sig <= S10_SIGRTMAX;
432	    nativesig++, s10sig++) {
433		if (sigismember(set, nativesig))
434			sigaddset(&s10set, s10sig);
435	}
436
437	*set = s10set;
438}
439
440void
441s10_sigset_s10_to_native(sigset_t *set)
442{
443	int s10sig;
444	int nativesig;
445	sigset_t nativeset;
446
447	/*
448	 * Shortcut: we know the first 32 signals are the same in both
449	 * s10 and native Solaris.  Just assign the first word.
450	 */
451	nativeset.__sigbits[0] = set->__sigbits[0];
452	nativeset.__sigbits[1] = 0;
453	nativeset.__sigbits[2] = 0;
454	nativeset.__sigbits[3] = 0;
455
456	/*
457	 * Copy the remainder of the initial set of common signals.
458	 */
459	for (s10sig = 33; s10sig < S10_SIGRTMIN; s10sig++)
460		if (sigismember(set, s10sig))
461			sigaddset(&nativeset, s10sig);
462
463	/*
464	 * Convert any S10 RT signals to their native values.
465	 */
466	for (s10sig = S10_SIGRTMIN, nativesig = _SIGRTMIN;
467	    s10sig <= S10_SIGRTMAX && nativesig <= _SIGRTMAX;
468	    s10sig++, nativesig++) {
469		if (sigismember(set, s10sig))
470			sigaddset(&nativeset, nativesig);
471	}
472
473	*set = nativeset;
474}
475
476int
477_init(void)
478{
479	int err;
480
481	/*
482	 * Set up the table indicating which system calls we want to
483	 * interpose on.  We should probably build this automatically from
484	 * a list of system calls that is shared with the user-space
485	 * library.
486	 */
487	s10_emulation_table = kmem_zalloc(NSYSCALL, KM_SLEEP);
488	s10_emulation_table[S10_SYS_forkall] = 1;		/*   2 */
489	s10_emulation_table[S10_SYS_open] = 1;			/*   5 */
490	s10_emulation_table[S10_SYS_wait] = 1;			/*   7 */
491	s10_emulation_table[S10_SYS_creat] = 1;			/*   8 */
492	s10_emulation_table[S10_SYS_unlink] = 1;		/*  10 */
493	s10_emulation_table[S10_SYS_exec] = 1;			/*  11 */
494	s10_emulation_table[S10_SYS_chown] = 1;			/*  16 */
495	s10_emulation_table[S10_SYS_stat] = 1;			/*  18 */
496	s10_emulation_table[S10_SYS_umount] = 1;		/*  22 */
497	s10_emulation_table[S10_SYS_fstat] = 1;			/*  28 */
498	s10_emulation_table[S10_SYS_utime] = 1;			/*  30 */
499	s10_emulation_table[S10_SYS_access] = 1;		/*  33 */
500	s10_emulation_table[SYS_kill] = 1;			/*  37 */
501	s10_emulation_table[S10_SYS_dup] = 1;			/*  41 */
502	s10_emulation_table[SYS_ioctl] = 1;			/*  54 */
503	s10_emulation_table[SYS_execve] = 1;			/*  59 */
504	s10_emulation_table[SYS_acctctl] = 1;			/*  71 */
505	s10_emulation_table[S10_SYS_issetugid] = 1;		/*  75 */
506	s10_emulation_table[S10_SYS_fsat] = 1;			/*  76 */
507	s10_emulation_table[S10_SYS_rmdir] = 1;			/*  79 */
508	s10_emulation_table[SYS_getdents] = 1;			/*  81 */
509	s10_emulation_table[S10_SYS_poll] = 1;			/*  87 */
510	s10_emulation_table[S10_SYS_lstat] = 1;			/*  88 */
511	s10_emulation_table[S10_SYS_fchown] = 1;		/*  94 */
512	s10_emulation_table[SYS_sigprocmask] = 1;		/*  95 */
513	s10_emulation_table[SYS_sigsuspend] = 1;		/*  96 */
514	s10_emulation_table[SYS_sigaction] = 1;			/*  98 */
515	s10_emulation_table[SYS_sigpending] = 1;		/*  99 */
516	s10_emulation_table[SYS_waitid] = 1;			/* 107 */
517	s10_emulation_table[SYS_sigsendsys] = 1;		/* 108 */
518#if defined(__x86)
519	s10_emulation_table[S10_SYS_xstat] = 1;			/* 123 */
520	s10_emulation_table[S10_SYS_lxstat] = 1;		/* 124 */
521	s10_emulation_table[S10_SYS_fxstat] = 1;		/* 125 */
522	s10_emulation_table[S10_SYS_xmknod] = 1;		/* 126 */
523#endif
524	s10_emulation_table[S10_SYS_lchown] = 1;		/* 130 */
525	s10_emulation_table[S10_SYS_rename] = 1;		/* 134 */
526	s10_emulation_table[SYS_uname] = 1;			/* 135 */
527	s10_emulation_table[SYS_sysconfig] = 1;			/* 137 */
528	s10_emulation_table[SYS_systeminfo] = 1;		/* 139 */
529	s10_emulation_table[S10_SYS_fork1] = 1;			/* 143 */
530	s10_emulation_table[SYS_sigtimedwait] = 1;		/* 144 */
531	s10_emulation_table[S10_SYS_lwp_sema_wait] = 1;		/* 147 */
532	s10_emulation_table[S10_SYS_utimes] = 1;		/* 154 */
533	s10_emulation_table[SYS_lwp_create] = 1;		/* 159 */
534	s10_emulation_table[SYS_lwp_kill] = 1;			/* 163 */
535	s10_emulation_table[SYS_lwp_sigmask] = 1;		/* 165 */
536#if defined(__amd64)
537	s10_emulation_table[SYS_lwp_private] = 1;		/* 166 */
538#endif	/* __amd64 */
539	s10_emulation_table[S10_SYS_lwp_mutex_lock] = 1;	/* 169 */
540	s10_emulation_table[SYS_pwrite] = 1;			/* 174 */
541	s10_emulation_table[SYS_auditsys] = 1;			/* 186 */
542	s10_emulation_table[SYS_sigqueue] = 1;			/* 190 */
543	s10_emulation_table[SYS_signotify] = 1;			/* 205 */
544	s10_emulation_table[SYS_lwp_mutex_timedlock] = 1;	/* 210 */
545	s10_emulation_table[SYS_getdents64] = 1;		/* 213 */
546	s10_emulation_table[S10_SYS_stat64] = 1;		/* 215 */
547	s10_emulation_table[S10_SYS_lstat64] = 1;		/* 216 */
548	s10_emulation_table[S10_SYS_fstat64] = 1;		/* 217 */
549	s10_emulation_table[SYS_pwrite64] = 1;			/* 223 */
550	s10_emulation_table[S10_SYS_creat64] = 1;		/* 224 */
551	s10_emulation_table[S10_SYS_open64] = 1;		/* 225 */
552	s10_emulation_table[SYS_zone] = 1;			/* 227 */
553	s10_emulation_table[SYS_lwp_mutex_trylock] = 1;		/* 251 */
554
555	err = mod_install(&modlinkage);
556	if (err) {
557		cmn_err(CE_WARN, "Couldn't install brand module");
558		kmem_free(s10_emulation_table, NSYSCALL);
559	}
560
561	return (err);
562}
563
564int
565_info(struct modinfo *modinfop)
566{
567	return (mod_info(&modlinkage, modinfop));
568}
569
570int
571_fini(void)
572{
573	return (brand_solaris_fini(&s10_emulation_table, &modlinkage,
574	    &s10_brand));
575}
576