1f0089e39SRichard Lowe /*
2f0089e39SRichard Lowe * CDDL HEADER START
3f0089e39SRichard Lowe *
4f0089e39SRichard Lowe * The contents of this file are subject to the terms of the
5f0089e39SRichard Lowe * Common Development and Distribution License (the "License").
6f0089e39SRichard Lowe * You may not use this file except in compliance with the License.
7f0089e39SRichard Lowe *
8f0089e39SRichard Lowe * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9f0089e39SRichard Lowe * or http://www.opensolaris.org/os/licensing.
10f0089e39SRichard Lowe * See the License for the specific language governing permissions
11f0089e39SRichard Lowe * and limitations under the License.
12f0089e39SRichard Lowe *
13f0089e39SRichard Lowe * When distributing Covered Code, include this CDDL HEADER in each
14f0089e39SRichard Lowe * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15f0089e39SRichard Lowe * If applicable, add the following below this CDDL HEADER, with the
16f0089e39SRichard Lowe * fields enclosed by brackets "[]" replaced with your own identifying
17f0089e39SRichard Lowe * information: Portions Copyright [yyyy] [name of copyright owner]
18f0089e39SRichard Lowe *
19f0089e39SRichard Lowe * CDDL HEADER END
20f0089e39SRichard Lowe */
21f0089e39SRichard Lowe
22f0089e39SRichard Lowe /*
23f0089e39SRichard Lowe * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
24f0089e39SRichard Lowe * Use is subject to license terms.
25f0089e39SRichard Lowe */
26f0089e39SRichard Lowe
27f0089e39SRichard Lowe /* Copyright (c) 1990, 1991 UNIX System Laboratories, Inc. */
28f0089e39SRichard Lowe /* Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T */
29f0089e39SRichard Lowe /* All Rights Reserved */
30f0089e39SRichard Lowe
31*ed093b41SRobert Mustacchi /*
32*ed093b41SRobert Mustacchi * Copyright 2023 Oxide Computer Company
33*ed093b41SRobert Mustacchi */
34*ed093b41SRobert Mustacchi
35f0089e39SRichard Lowe #include <sys/types.h>
36f0089e39SRichard Lowe #include <sys/param.h>
37f0089e39SRichard Lowe #include <sys/sysmacros.h>
38f0089e39SRichard Lowe #include <sys/signal.h>
39f0089e39SRichard Lowe #include <sys/systm.h>
40f0089e39SRichard Lowe #include <sys/user.h>
41f0089e39SRichard Lowe #include <sys/mman.h>
42f0089e39SRichard Lowe #include <sys/class.h>
43f0089e39SRichard Lowe #include <sys/proc.h>
44f0089e39SRichard Lowe #include <sys/procfs.h>
45f0089e39SRichard Lowe #include <sys/buf.h>
46f0089e39SRichard Lowe #include <sys/kmem.h>
47f0089e39SRichard Lowe #include <sys/cred.h>
48f0089e39SRichard Lowe #include <sys/archsystm.h>
49f0089e39SRichard Lowe #include <sys/vmparam.h>
50f0089e39SRichard Lowe #include <sys/prsystm.h>
51f0089e39SRichard Lowe #include <sys/reboot.h>
52f0089e39SRichard Lowe #include <sys/uadmin.h>
53f0089e39SRichard Lowe #include <sys/vfs.h>
54f0089e39SRichard Lowe #include <sys/vnode.h>
55f0089e39SRichard Lowe #include <sys/file.h>
56f0089e39SRichard Lowe #include <sys/session.h>
57f0089e39SRichard Lowe #include <sys/ucontext.h>
58f0089e39SRichard Lowe #include <sys/dnlc.h>
59f0089e39SRichard Lowe #include <sys/var.h>
60f0089e39SRichard Lowe #include <sys/cmn_err.h>
61f0089e39SRichard Lowe #include <sys/debugreg.h>
62f0089e39SRichard Lowe #include <sys/thread.h>
63f0089e39SRichard Lowe #include <sys/vtrace.h>
64f0089e39SRichard Lowe #include <sys/consdev.h>
65f0089e39SRichard Lowe #include <sys/psw.h>
66f0089e39SRichard Lowe #include <sys/regset.h>
67f0089e39SRichard Lowe
68f0089e39SRichard Lowe #include <sys/privregs.h>
69f0089e39SRichard Lowe
70f0089e39SRichard Lowe #include <sys/stack.h>
71f0089e39SRichard Lowe #include <sys/swap.h>
72f0089e39SRichard Lowe #include <vm/hat.h>
73f0089e39SRichard Lowe #include <vm/anon.h>
74f0089e39SRichard Lowe #include <vm/as.h>
75f0089e39SRichard Lowe #include <vm/page.h>
76f0089e39SRichard Lowe #include <vm/seg.h>
77f0089e39SRichard Lowe #include <vm/seg_kmem.h>
78f0089e39SRichard Lowe #include <vm/seg_map.h>
79f0089e39SRichard Lowe #include <vm/seg_vn.h>
80f0089e39SRichard Lowe #include <sys/exec.h>
81f0089e39SRichard Lowe #include <sys/acct.h>
82f0089e39SRichard Lowe #include <sys/core.h>
83f0089e39SRichard Lowe #include <sys/corectl.h>
84f0089e39SRichard Lowe #include <sys/modctl.h>
85f0089e39SRichard Lowe #include <sys/tuneable.h>
86f0089e39SRichard Lowe #include <c2/audit.h>
87f0089e39SRichard Lowe #include <sys/bootconf.h>
88f0089e39SRichard Lowe #include <sys/dumphdr.h>
89f0089e39SRichard Lowe #include <sys/promif.h>
90f0089e39SRichard Lowe #include <sys/systeminfo.h>
91f0089e39SRichard Lowe #include <sys/kdi.h>
92f0089e39SRichard Lowe #include <sys/contract_impl.h>
93f0089e39SRichard Lowe #include <sys/x86_archext.h>
94f0089e39SRichard Lowe
95f0089e39SRichard Lowe /*
96f0089e39SRichard Lowe * Construct the execution environment for the user's signal
97f0089e39SRichard Lowe * handler and arrange for control to be given to it on return
98f0089e39SRichard Lowe * to userland. The library code now calls setcontext() to
99f0089e39SRichard Lowe * clean up after the signal handler, so sigret() is no longer
100f0089e39SRichard Lowe * needed.
101f0089e39SRichard Lowe *
102f0089e39SRichard Lowe * (The various 'volatile' declarations are need to ensure that values
103f0089e39SRichard Lowe * are correct on the error return from on_fault().)
104f0089e39SRichard Lowe */
105f0089e39SRichard Lowe
106f0089e39SRichard Lowe
107f0089e39SRichard Lowe /*
108f0089e39SRichard Lowe * An amd64 signal frame looks like this on the stack:
109f0089e39SRichard Lowe *
110f0089e39SRichard Lowe * old %rsp:
111f0089e39SRichard Lowe * <128 bytes of untouched stack space>
112f0089e39SRichard Lowe * <a siginfo_t [optional]>
113f0089e39SRichard Lowe * <a ucontext_t>
114*ed093b41SRobert Mustacchi * <a ucontext_t's xsave state>
115*ed093b41SRobert Mustacchi * <siginfo_t *> ---+
116*ed093b41SRobert Mustacchi * <signal number> | sigframe
117*ed093b41SRobert Mustacchi * new %rsp: <return address (deliberately invalid)> ---+
118f0089e39SRichard Lowe *
119f0089e39SRichard Lowe * The signal number and siginfo_t pointer are only pushed onto the stack in
120f0089e39SRichard Lowe * order to allow stack backtraces. The actual signal handling code expects the
121f0089e39SRichard Lowe * arguments in registers.
122f0089e39SRichard Lowe */
123f0089e39SRichard Lowe
124f0089e39SRichard Lowe struct sigframe {
125f0089e39SRichard Lowe caddr_t retaddr;
126f0089e39SRichard Lowe long signo;
127f0089e39SRichard Lowe siginfo_t *sip;
128f0089e39SRichard Lowe };
129f0089e39SRichard Lowe
130f0089e39SRichard Lowe int
sendsig(int sig,k_siginfo_t * sip,void (* hdlr)())131f0089e39SRichard Lowe sendsig(int sig, k_siginfo_t *sip, void (*hdlr)())
132f0089e39SRichard Lowe {
133*ed093b41SRobert Mustacchi volatile size_t minstacksz;
134*ed093b41SRobert Mustacchi boolean_t newstack;
135*ed093b41SRobert Mustacchi size_t xsave_size;
136*ed093b41SRobert Mustacchi int ret;
137f0089e39SRichard Lowe label_t ljb;
138f0089e39SRichard Lowe volatile caddr_t sp;
139f0089e39SRichard Lowe caddr_t fp;
140f0089e39SRichard Lowe volatile struct regs *rp;
141f0089e39SRichard Lowe volatile greg_t upc;
142f0089e39SRichard Lowe volatile proc_t *p = ttoproc(curthread);
143f0089e39SRichard Lowe struct as *as = p->p_as;
144f0089e39SRichard Lowe klwp_t *lwp = ttolwp(curthread);
145f0089e39SRichard Lowe ucontext_t *volatile tuc = NULL;
146f0089e39SRichard Lowe ucontext_t *uc;
147f0089e39SRichard Lowe siginfo_t *sip_addr;
148f0089e39SRichard Lowe volatile int watched;
149f0089e39SRichard Lowe
150f0089e39SRichard Lowe /*
151f0089e39SRichard Lowe * This routine is utterly dependent upon STACK_ALIGN being
152f0089e39SRichard Lowe * 16 and STACK_ENTRY_ALIGN being 8. Let's just acknowledge
153f0089e39SRichard Lowe * that and require it.
154f0089e39SRichard Lowe */
155f0089e39SRichard Lowe
156f0089e39SRichard Lowe #if STACK_ALIGN != 16 || STACK_ENTRY_ALIGN != 8
157f0089e39SRichard Lowe #error "sendsig() amd64 did not find the expected stack alignments"
158f0089e39SRichard Lowe #endif
159f0089e39SRichard Lowe
160f0089e39SRichard Lowe rp = lwptoregs(lwp);
161f0089e39SRichard Lowe upc = rp->r_pc;
162f0089e39SRichard Lowe
163f0089e39SRichard Lowe /*
164f0089e39SRichard Lowe * Since we're setting up to run the signal handler we have to
165f0089e39SRichard Lowe * arrange that the stack at entry to the handler is (only)
166f0089e39SRichard Lowe * STACK_ENTRY_ALIGN (i.e. 8) byte aligned so that when the handler
167f0089e39SRichard Lowe * executes its push of %rbp, the stack realigns to STACK_ALIGN
168f0089e39SRichard Lowe * (i.e. 16) correctly.
169f0089e39SRichard Lowe *
170f0089e39SRichard Lowe * The new sp will point to the sigframe and the ucontext_t. The
171f0089e39SRichard Lowe * above means that sp (and thus sigframe) will be 8-byte aligned,
172f0089e39SRichard Lowe * but not 16-byte aligned. ucontext_t, however, contains %xmm regs
173f0089e39SRichard Lowe * which must be 16-byte aligned. Because of this, for correct
174f0089e39SRichard Lowe * alignment, sigframe must be a multiple of 8-bytes in length, but
175f0089e39SRichard Lowe * not 16-bytes. This will place ucontext_t at a nice 16-byte boundary.
176*ed093b41SRobert Mustacchi *
177*ed093b41SRobert Mustacchi * When we move onto the xsave state, right now, we don't guarantee any
178*ed093b41SRobert Mustacchi * alignment of the resulting data, but we will ensure that the
179*ed093b41SRobert Mustacchi * resulting sp does have proper alignment. This will ensure that the
180*ed093b41SRobert Mustacchi * guarantee on the ucontex_t is not violated.
181f0089e39SRichard Lowe */
182f0089e39SRichard Lowe
183*ed093b41SRobert Mustacchi CTASSERT((sizeof (struct sigframe) % 16) == 8);
184f0089e39SRichard Lowe
185f0089e39SRichard Lowe minstacksz = sizeof (struct sigframe) + SA(sizeof (*uc));
186f0089e39SRichard Lowe if (sip != NULL)
187f0089e39SRichard Lowe minstacksz += SA(sizeof (siginfo_t));
188*ed093b41SRobert Mustacchi
189*ed093b41SRobert Mustacchi if (fpu_xsave_enabled()) {
190*ed093b41SRobert Mustacchi xsave_size = SA(fpu_signal_size(lwp));
191*ed093b41SRobert Mustacchi minstacksz += xsave_size;
192*ed093b41SRobert Mustacchi } else {
193*ed093b41SRobert Mustacchi xsave_size = 0;
194*ed093b41SRobert Mustacchi }
195*ed093b41SRobert Mustacchi
196f0089e39SRichard Lowe ASSERT((minstacksz & (STACK_ENTRY_ALIGN - 1ul)) == 0);
197f0089e39SRichard Lowe
198f0089e39SRichard Lowe /*
199f0089e39SRichard Lowe * Figure out whether we will be handling this signal on
200f0089e39SRichard Lowe * an alternate stack specified by the user. Then allocate
201f0089e39SRichard Lowe * and validate the stack requirements for the signal handler
202f0089e39SRichard Lowe * context. on_fault will catch any faults.
203f0089e39SRichard Lowe */
204f0089e39SRichard Lowe newstack = sigismember(&PTOU(curproc)->u_sigonstack, sig) &&
205f0089e39SRichard Lowe !(lwp->lwp_sigaltstack.ss_flags & (SS_ONSTACK|SS_DISABLE));
206f0089e39SRichard Lowe
207f0089e39SRichard Lowe if (newstack) {
208f0089e39SRichard Lowe fp = (caddr_t)(SA((uintptr_t)lwp->lwp_sigaltstack.ss_sp) +
209f0089e39SRichard Lowe SA(lwp->lwp_sigaltstack.ss_size) - STACK_ALIGN);
210f0089e39SRichard Lowe } else {
211f0089e39SRichard Lowe /*
212f0089e39SRichard Lowe * Drop below the 128-byte reserved region of the stack frame
213f0089e39SRichard Lowe * we're interrupting.
214f0089e39SRichard Lowe */
215f0089e39SRichard Lowe fp = (caddr_t)rp->r_sp - STACK_RESERVE;
216f0089e39SRichard Lowe }
217f0089e39SRichard Lowe
218f0089e39SRichard Lowe /*
219f0089e39SRichard Lowe * Force proper stack pointer alignment, even in the face of a
220f0089e39SRichard Lowe * misaligned stack pointer from user-level before the signal.
221f0089e39SRichard Lowe */
222f0089e39SRichard Lowe fp = (caddr_t)((uintptr_t)fp & ~(STACK_ENTRY_ALIGN - 1ul));
223f0089e39SRichard Lowe
224f0089e39SRichard Lowe /*
225f0089e39SRichard Lowe * Most of the time during normal execution, the stack pointer
226f0089e39SRichard Lowe * is aligned on a STACK_ALIGN (i.e. 16 byte) boundary. However,
227f0089e39SRichard Lowe * (for example) just after a call instruction (which pushes
228f0089e39SRichard Lowe * the return address), the callers stack misaligns until the
229f0089e39SRichard Lowe * 'push %rbp' happens in the callee prolog. So while we should
230f0089e39SRichard Lowe * expect the stack pointer to be always at least STACK_ENTRY_ALIGN
231f0089e39SRichard Lowe * aligned, we should -not- expect it to always be STACK_ALIGN aligned.
232f0089e39SRichard Lowe * We now adjust to ensure that the new sp is aligned to
233f0089e39SRichard Lowe * STACK_ENTRY_ALIGN but not to STACK_ALIGN.
234f0089e39SRichard Lowe */
235f0089e39SRichard Lowe sp = fp - minstacksz;
236f0089e39SRichard Lowe if (((uintptr_t)sp & (STACK_ALIGN - 1ul)) == 0) {
237f0089e39SRichard Lowe sp -= STACK_ENTRY_ALIGN;
238f0089e39SRichard Lowe minstacksz = fp - sp;
239f0089e39SRichard Lowe }
240f0089e39SRichard Lowe
241f0089e39SRichard Lowe /*
242f0089e39SRichard Lowe * Now, make sure the resulting signal frame address is sane
243f0089e39SRichard Lowe */
244f0089e39SRichard Lowe if (sp >= as->a_userlimit || fp >= as->a_userlimit) {
245f0089e39SRichard Lowe #ifdef DEBUG
246f0089e39SRichard Lowe printf("sendsig: bad signal stack cmd=%s, pid=%d, sig=%d\n",
247f0089e39SRichard Lowe PTOU(p)->u_comm, p->p_pid, sig);
248f0089e39SRichard Lowe printf("sigsp = 0x%p, action = 0x%p, upc = 0x%lx\n",
249f0089e39SRichard Lowe (void *)sp, (void *)hdlr, (uintptr_t)upc);
250f0089e39SRichard Lowe printf("sp above USERLIMIT\n");
251f0089e39SRichard Lowe #endif
252f0089e39SRichard Lowe return (0);
253f0089e39SRichard Lowe }
254f0089e39SRichard Lowe
255f0089e39SRichard Lowe watched = watch_disable_addr((caddr_t)sp, minstacksz, S_WRITE);
256f0089e39SRichard Lowe
257f0089e39SRichard Lowe if (on_fault(&ljb))
258f0089e39SRichard Lowe goto badstack;
259f0089e39SRichard Lowe
260f0089e39SRichard Lowe if (sip != NULL) {
261f0089e39SRichard Lowe zoneid_t zoneid;
262f0089e39SRichard Lowe
263f0089e39SRichard Lowe fp -= SA(sizeof (siginfo_t));
264f0089e39SRichard Lowe uzero(fp, sizeof (siginfo_t));
265f0089e39SRichard Lowe if (SI_FROMUSER(sip) &&
266f0089e39SRichard Lowe (zoneid = p->p_zone->zone_id) != GLOBAL_ZONEID &&
267f0089e39SRichard Lowe zoneid != sip->si_zoneid) {
268f0089e39SRichard Lowe k_siginfo_t sani_sip = *sip;
269f0089e39SRichard Lowe
270f0089e39SRichard Lowe sani_sip.si_pid = p->p_zone->zone_zsched->p_pid;
271f0089e39SRichard Lowe sani_sip.si_uid = 0;
272f0089e39SRichard Lowe sani_sip.si_ctid = -1;
273f0089e39SRichard Lowe sani_sip.si_zoneid = zoneid;
274f0089e39SRichard Lowe copyout_noerr(&sani_sip, fp, sizeof (sani_sip));
275f0089e39SRichard Lowe } else
276f0089e39SRichard Lowe copyout_noerr(sip, fp, sizeof (*sip));
277f0089e39SRichard Lowe sip_addr = (siginfo_t *)fp;
278f0089e39SRichard Lowe
279f0089e39SRichard Lowe if (sig == SIGPROF &&
280f0089e39SRichard Lowe curthread->t_rprof != NULL &&
281f0089e39SRichard Lowe curthread->t_rprof->rp_anystate) {
282f0089e39SRichard Lowe /*
283f0089e39SRichard Lowe * We stand on our head to deal with
284f0089e39SRichard Lowe * the real time profiling signal.
285f0089e39SRichard Lowe * Fill in the stuff that doesn't fit
286f0089e39SRichard Lowe * in a normal k_siginfo structure.
287f0089e39SRichard Lowe */
288f0089e39SRichard Lowe int i = sip->si_nsysarg;
289f0089e39SRichard Lowe
290f0089e39SRichard Lowe while (--i >= 0)
291f0089e39SRichard Lowe sulword_noerr(
292f0089e39SRichard Lowe (ulong_t *)&(sip_addr->si_sysarg[i]),
293f0089e39SRichard Lowe (ulong_t)lwp->lwp_arg[i]);
294f0089e39SRichard Lowe copyout_noerr(curthread->t_rprof->rp_state,
295f0089e39SRichard Lowe sip_addr->si_mstate,
296f0089e39SRichard Lowe sizeof (curthread->t_rprof->rp_state));
297f0089e39SRichard Lowe }
298f0089e39SRichard Lowe } else
299f0089e39SRichard Lowe sip_addr = NULL;
300f0089e39SRichard Lowe
301f0089e39SRichard Lowe no_fault();
302*ed093b41SRobert Mustacchi
303*ed093b41SRobert Mustacchi /*
304*ed093b41SRobert Mustacchi * Save the current context on the user stack directly after the
305*ed093b41SRobert Mustacchi * sigframe. Since sigframe is 8-byte-but-not-16-byte aligned, and since
306*ed093b41SRobert Mustacchi * sizeof (struct sigframe) is 24, this guarantees 16-byte alignment for
307*ed093b41SRobert Mustacchi * ucontext_t and its %xmm registers. The xsave state part of the
308*ed093b41SRobert Mustacchi * ucontext_t may be inbetween these two. However, we have ensured that
309*ed093b41SRobert Mustacchi * the size of the stack space is 16-byte aligned as the actual size may
310*ed093b41SRobert Mustacchi * vary.
311*ed093b41SRobert Mustacchi */
312*ed093b41SRobert Mustacchi tuc = kmem_alloc(sizeof (*tuc), KM_SLEEP);
313*ed093b41SRobert Mustacchi if (xsave_size != 0) {
314*ed093b41SRobert Mustacchi tuc->uc_xsave = (unsigned long)(sp + sizeof (struct sigframe));
315*ed093b41SRobert Mustacchi }
316*ed093b41SRobert Mustacchi uc = (ucontext_t *)(sp + sizeof (struct sigframe) + xsave_size);
317*ed093b41SRobert Mustacchi ret = savecontext(tuc, &lwp->lwp_sigoldmask, SAVECTXT_F_EXTD |
318*ed093b41SRobert Mustacchi SAVECTXT_F_ONFAULT);
319*ed093b41SRobert Mustacchi if (ret != 0)
320*ed093b41SRobert Mustacchi goto postfault;
321f0089e39SRichard Lowe if (on_fault(&ljb))
322f0089e39SRichard Lowe goto badstack;
323f0089e39SRichard Lowe copyout_noerr(tuc, uc, sizeof (*tuc));
324f0089e39SRichard Lowe kmem_free(tuc, sizeof (*tuc));
325f0089e39SRichard Lowe tuc = NULL;
326f0089e39SRichard Lowe
327f0089e39SRichard Lowe lwp->lwp_oldcontext = (uintptr_t)uc;
328f0089e39SRichard Lowe
329f0089e39SRichard Lowe if (newstack) {
330f0089e39SRichard Lowe lwp->lwp_sigaltstack.ss_flags |= SS_ONSTACK;
331f0089e39SRichard Lowe if (lwp->lwp_ustack)
332f0089e39SRichard Lowe copyout_noerr(&lwp->lwp_sigaltstack,
333f0089e39SRichard Lowe (stack_t *)lwp->lwp_ustack, sizeof (stack_t));
334f0089e39SRichard Lowe }
335f0089e39SRichard Lowe
336f0089e39SRichard Lowe /*
337f0089e39SRichard Lowe * Set up signal handler return and stack linkage
338f0089e39SRichard Lowe */
339f0089e39SRichard Lowe {
340f0089e39SRichard Lowe struct sigframe frame;
341f0089e39SRichard Lowe
342f0089e39SRichard Lowe /*
343f0089e39SRichard Lowe * ensure we never return "normally"
344f0089e39SRichard Lowe */
345f0089e39SRichard Lowe frame.retaddr = (caddr_t)(uintptr_t)-1L;
346f0089e39SRichard Lowe frame.signo = sig;
347f0089e39SRichard Lowe frame.sip = sip_addr;
348f0089e39SRichard Lowe copyout_noerr(&frame, sp, sizeof (frame));
349f0089e39SRichard Lowe }
350f0089e39SRichard Lowe
351f0089e39SRichard Lowe no_fault();
352f0089e39SRichard Lowe if (watched)
353f0089e39SRichard Lowe watch_enable_addr((caddr_t)sp, minstacksz, S_WRITE);
354f0089e39SRichard Lowe
355f0089e39SRichard Lowe /*
356f0089e39SRichard Lowe * Set up user registers for execution of signal handler.
357f0089e39SRichard Lowe */
358f0089e39SRichard Lowe rp->r_sp = (greg_t)sp;
359f0089e39SRichard Lowe rp->r_pc = (greg_t)hdlr;
360f0089e39SRichard Lowe rp->r_ps = PSL_USER | (rp->r_ps & PS_IOPL);
361f0089e39SRichard Lowe
362f0089e39SRichard Lowe rp->r_rdi = sig;
363f0089e39SRichard Lowe rp->r_rsi = (uintptr_t)sip_addr;
364f0089e39SRichard Lowe rp->r_rdx = (uintptr_t)uc;
365f0089e39SRichard Lowe
366f0089e39SRichard Lowe if ((rp->r_cs & 0xffff) != UCS_SEL ||
367f0089e39SRichard Lowe (rp->r_ss & 0xffff) != UDS_SEL) {
368f0089e39SRichard Lowe /*
369f0089e39SRichard Lowe * Try our best to deliver the signal.
370f0089e39SRichard Lowe */
371f0089e39SRichard Lowe rp->r_cs = UCS_SEL;
372f0089e39SRichard Lowe rp->r_ss = UDS_SEL;
373f0089e39SRichard Lowe }
374f0089e39SRichard Lowe
375f0089e39SRichard Lowe /*
376f0089e39SRichard Lowe * Don't set lwp_eosys here. sendsig() is called via psig() after
377f0089e39SRichard Lowe * lwp_eosys is handled, so setting it here would affect the next
378f0089e39SRichard Lowe * system call.
379f0089e39SRichard Lowe */
380f0089e39SRichard Lowe return (1);
381f0089e39SRichard Lowe
382f0089e39SRichard Lowe badstack:
383f0089e39SRichard Lowe no_fault();
384*ed093b41SRobert Mustacchi postfault:
385f0089e39SRichard Lowe if (watched)
386f0089e39SRichard Lowe watch_enable_addr((caddr_t)sp, minstacksz, S_WRITE);
387f0089e39SRichard Lowe if (tuc)
388f0089e39SRichard Lowe kmem_free(tuc, sizeof (*tuc));
389f0089e39SRichard Lowe #ifdef DEBUG
390f0089e39SRichard Lowe printf("sendsig: bad signal stack cmd=%s, pid=%d, sig=%d\n",
391f0089e39SRichard Lowe PTOU(p)->u_comm, p->p_pid, sig);
392f0089e39SRichard Lowe printf("on fault, sigsp = 0x%p, action = 0x%p, upc = 0x%lx\n",
393f0089e39SRichard Lowe (void *)sp, (void *)hdlr, (uintptr_t)upc);
394f0089e39SRichard Lowe #endif
395f0089e39SRichard Lowe return (0);
396f0089e39SRichard Lowe }
397f0089e39SRichard Lowe
398f0089e39SRichard Lowe #ifdef _SYSCALL32_IMPL
399f0089e39SRichard Lowe
400f0089e39SRichard Lowe /*
401f0089e39SRichard Lowe * An i386 SVR4/ABI signal frame looks like this on the stack:
402f0089e39SRichard Lowe *
403f0089e39SRichard Lowe * old %esp:
404f0089e39SRichard Lowe * <a siginfo32_t [optional]>
405f0089e39SRichard Lowe * <a ucontext32_t>
406*ed093b41SRobert Mustacchi * <a ucontext32_t's xsave state>
407f0089e39SRichard Lowe * <pointer to that ucontext32_t>
408f0089e39SRichard Lowe * <pointer to that siginfo32_t>
409f0089e39SRichard Lowe * <signo>
410f0089e39SRichard Lowe * new %esp: <return address (deliberately invalid)>
411f0089e39SRichard Lowe */
412f0089e39SRichard Lowe struct sigframe32 {
413f0089e39SRichard Lowe caddr32_t retaddr;
414f0089e39SRichard Lowe uint32_t signo;
415f0089e39SRichard Lowe caddr32_t sip;
416f0089e39SRichard Lowe caddr32_t ucp;
417f0089e39SRichard Lowe };
418f0089e39SRichard Lowe
419f0089e39SRichard Lowe int
sendsig32(int sig,k_siginfo_t * sip,void (* hdlr)())420f0089e39SRichard Lowe sendsig32(int sig, k_siginfo_t *sip, void (*hdlr)())
421f0089e39SRichard Lowe {
422*ed093b41SRobert Mustacchi volatile size_t minstacksz;
423*ed093b41SRobert Mustacchi boolean_t newstack;
424*ed093b41SRobert Mustacchi size_t xsave_size;
425*ed093b41SRobert Mustacchi int ret;
426f0089e39SRichard Lowe label_t ljb;
427f0089e39SRichard Lowe volatile caddr_t sp;
428f0089e39SRichard Lowe caddr_t fp;
429f0089e39SRichard Lowe volatile struct regs *rp;
430f0089e39SRichard Lowe volatile greg_t upc;
431f0089e39SRichard Lowe volatile proc_t *p = ttoproc(curthread);
432f0089e39SRichard Lowe klwp_t *lwp = ttolwp(curthread);
433f0089e39SRichard Lowe ucontext32_t *volatile tuc = NULL;
434f0089e39SRichard Lowe ucontext32_t *uc;
435f0089e39SRichard Lowe siginfo32_t *sip_addr;
436f0089e39SRichard Lowe volatile int watched;
437f0089e39SRichard Lowe
438f0089e39SRichard Lowe rp = lwptoregs(lwp);
439f0089e39SRichard Lowe upc = rp->r_pc;
440f0089e39SRichard Lowe
441f0089e39SRichard Lowe minstacksz = SA32(sizeof (struct sigframe32)) + SA32(sizeof (*uc));
442f0089e39SRichard Lowe if (sip != NULL)
443f0089e39SRichard Lowe minstacksz += SA32(sizeof (siginfo32_t));
444*ed093b41SRobert Mustacchi
445*ed093b41SRobert Mustacchi if (fpu_xsave_enabled()) {
446*ed093b41SRobert Mustacchi xsave_size = SA32(fpu_signal_size(lwp));
447*ed093b41SRobert Mustacchi minstacksz += xsave_size;
448*ed093b41SRobert Mustacchi } else {
449*ed093b41SRobert Mustacchi xsave_size = 0;
450*ed093b41SRobert Mustacchi }
451f0089e39SRichard Lowe ASSERT((minstacksz & (STACK_ALIGN32 - 1)) == 0);
452f0089e39SRichard Lowe
453f0089e39SRichard Lowe /*
454f0089e39SRichard Lowe * Figure out whether we will be handling this signal on
455f0089e39SRichard Lowe * an alternate stack specified by the user. Then allocate
456f0089e39SRichard Lowe * and validate the stack requirements for the signal handler
457f0089e39SRichard Lowe * context. on_fault will catch any faults.
458f0089e39SRichard Lowe */
459f0089e39SRichard Lowe newstack = sigismember(&PTOU(curproc)->u_sigonstack, sig) &&
460f0089e39SRichard Lowe !(lwp->lwp_sigaltstack.ss_flags & (SS_ONSTACK|SS_DISABLE));
461f0089e39SRichard Lowe
462f0089e39SRichard Lowe if (newstack) {
463f0089e39SRichard Lowe fp = (caddr_t)(SA32((uintptr_t)lwp->lwp_sigaltstack.ss_sp) +
464f0089e39SRichard Lowe SA32(lwp->lwp_sigaltstack.ss_size) - STACK_ALIGN32);
465f0089e39SRichard Lowe } else if ((rp->r_ss & 0xffff) != UDS_SEL) {
466f0089e39SRichard Lowe user_desc_t *ldt;
467f0089e39SRichard Lowe /*
468f0089e39SRichard Lowe * If the stack segment selector is -not- pointing at
469f0089e39SRichard Lowe * the UDS_SEL descriptor and we have an LDT entry for
470f0089e39SRichard Lowe * it instead, add the base address to find the effective va.
471f0089e39SRichard Lowe */
472f0089e39SRichard Lowe if ((ldt = p->p_ldt) != NULL)
473f0089e39SRichard Lowe fp = (caddr_t)rp->r_sp +
474f0089e39SRichard Lowe USEGD_GETBASE(&ldt[SELTOIDX(rp->r_ss)]);
475f0089e39SRichard Lowe else
476f0089e39SRichard Lowe fp = (caddr_t)rp->r_sp;
477f0089e39SRichard Lowe } else
478f0089e39SRichard Lowe fp = (caddr_t)rp->r_sp;
479f0089e39SRichard Lowe
480f0089e39SRichard Lowe /*
481f0089e39SRichard Lowe * Force proper stack pointer alignment, even in the face of a
482f0089e39SRichard Lowe * misaligned stack pointer from user-level before the signal.
483f0089e39SRichard Lowe * Don't use the SA32() macro because that rounds up, not down.
484f0089e39SRichard Lowe */
485f0089e39SRichard Lowe fp = (caddr_t)((uintptr_t)fp & ~(STACK_ALIGN32 - 1));
486f0089e39SRichard Lowe sp = fp - minstacksz;
487f0089e39SRichard Lowe
488f0089e39SRichard Lowe /*
489f0089e39SRichard Lowe * Make sure lwp hasn't trashed its stack
490f0089e39SRichard Lowe */
491f0089e39SRichard Lowe if (sp >= (caddr_t)(uintptr_t)USERLIMIT32 ||
492f0089e39SRichard Lowe fp >= (caddr_t)(uintptr_t)USERLIMIT32) {
493f0089e39SRichard Lowe #ifdef DEBUG
494f0089e39SRichard Lowe printf("sendsig32: bad signal stack cmd=%s, pid=%d, sig=%d\n",
495f0089e39SRichard Lowe PTOU(p)->u_comm, p->p_pid, sig);
496f0089e39SRichard Lowe printf("sigsp = 0x%p, action = 0x%p, upc = 0x%lx\n",
497f0089e39SRichard Lowe (void *)sp, (void *)hdlr, (uintptr_t)upc);
498f0089e39SRichard Lowe printf("sp above USERLIMIT\n");
499f0089e39SRichard Lowe #endif
500f0089e39SRichard Lowe return (0);
501f0089e39SRichard Lowe }
502f0089e39SRichard Lowe
503f0089e39SRichard Lowe watched = watch_disable_addr((caddr_t)sp, minstacksz, S_WRITE);
504f0089e39SRichard Lowe
505f0089e39SRichard Lowe if (on_fault(&ljb))
506f0089e39SRichard Lowe goto badstack;
507f0089e39SRichard Lowe
508f0089e39SRichard Lowe if (sip != NULL) {
509f0089e39SRichard Lowe siginfo32_t si32;
510f0089e39SRichard Lowe zoneid_t zoneid;
511f0089e39SRichard Lowe
512f0089e39SRichard Lowe siginfo_kto32(sip, &si32);
513f0089e39SRichard Lowe if (SI_FROMUSER(sip) &&
514f0089e39SRichard Lowe (zoneid = p->p_zone->zone_id) != GLOBAL_ZONEID &&
515f0089e39SRichard Lowe zoneid != sip->si_zoneid) {
516f0089e39SRichard Lowe si32.si_pid = p->p_zone->zone_zsched->p_pid;
517f0089e39SRichard Lowe si32.si_uid = 0;
518f0089e39SRichard Lowe si32.si_ctid = -1;
519f0089e39SRichard Lowe si32.si_zoneid = zoneid;
520f0089e39SRichard Lowe }
521f0089e39SRichard Lowe fp -= SA32(sizeof (si32));
522f0089e39SRichard Lowe uzero(fp, sizeof (si32));
523f0089e39SRichard Lowe copyout_noerr(&si32, fp, sizeof (si32));
524f0089e39SRichard Lowe sip_addr = (siginfo32_t *)fp;
525f0089e39SRichard Lowe
526f0089e39SRichard Lowe if (sig == SIGPROF &&
527f0089e39SRichard Lowe curthread->t_rprof != NULL &&
528f0089e39SRichard Lowe curthread->t_rprof->rp_anystate) {
529f0089e39SRichard Lowe /*
530f0089e39SRichard Lowe * We stand on our head to deal with
531f0089e39SRichard Lowe * the real-time profiling signal.
532f0089e39SRichard Lowe * Fill in the stuff that doesn't fit
533f0089e39SRichard Lowe * in a normal k_siginfo structure.
534f0089e39SRichard Lowe */
535f0089e39SRichard Lowe int i = sip->si_nsysarg;
536f0089e39SRichard Lowe
537f0089e39SRichard Lowe while (--i >= 0)
538f0089e39SRichard Lowe suword32_noerr(&(sip_addr->si_sysarg[i]),
539f0089e39SRichard Lowe (uint32_t)lwp->lwp_arg[i]);
540f0089e39SRichard Lowe copyout_noerr(curthread->t_rprof->rp_state,
541f0089e39SRichard Lowe sip_addr->si_mstate,
542f0089e39SRichard Lowe sizeof (curthread->t_rprof->rp_state));
543f0089e39SRichard Lowe }
544f0089e39SRichard Lowe } else
545f0089e39SRichard Lowe sip_addr = NULL;
546*ed093b41SRobert Mustacchi no_fault();
547f0089e39SRichard Lowe
548f0089e39SRichard Lowe /* save the current context on the user stack */
549*ed093b41SRobert Mustacchi tuc = kmem_alloc(sizeof (*tuc), KM_SLEEP);
550f0089e39SRichard Lowe fp -= SA32(sizeof (*tuc));
551f0089e39SRichard Lowe uc = (ucontext32_t *)fp;
552*ed093b41SRobert Mustacchi if (xsave_size != 0) {
553*ed093b41SRobert Mustacchi fp -= xsave_size;
554*ed093b41SRobert Mustacchi tuc->uc_xsave = (int32_t)(uintptr_t)fp;
555*ed093b41SRobert Mustacchi }
556*ed093b41SRobert Mustacchi ret = savecontext32(tuc, &lwp->lwp_sigoldmask, SAVECTXT_F_EXTD |
557*ed093b41SRobert Mustacchi SAVECTXT_F_ONFAULT);
558*ed093b41SRobert Mustacchi if (ret != 0)
559*ed093b41SRobert Mustacchi goto postfault;
560f0089e39SRichard Lowe if (on_fault(&ljb))
561f0089e39SRichard Lowe goto badstack;
562f0089e39SRichard Lowe copyout_noerr(tuc, uc, sizeof (*tuc));
563f0089e39SRichard Lowe kmem_free(tuc, sizeof (*tuc));
564f0089e39SRichard Lowe tuc = NULL;
565f0089e39SRichard Lowe
566f0089e39SRichard Lowe lwp->lwp_oldcontext = (uintptr_t)uc;
567f0089e39SRichard Lowe
568f0089e39SRichard Lowe if (newstack) {
569f0089e39SRichard Lowe lwp->lwp_sigaltstack.ss_flags |= SS_ONSTACK;
570f0089e39SRichard Lowe if (lwp->lwp_ustack) {
571f0089e39SRichard Lowe stack32_t stk32;
572f0089e39SRichard Lowe
573f0089e39SRichard Lowe stk32.ss_sp = (caddr32_t)(uintptr_t)
574f0089e39SRichard Lowe lwp->lwp_sigaltstack.ss_sp;
575f0089e39SRichard Lowe stk32.ss_size = (size32_t)
576f0089e39SRichard Lowe lwp->lwp_sigaltstack.ss_size;
577f0089e39SRichard Lowe stk32.ss_flags = (int32_t)
578f0089e39SRichard Lowe lwp->lwp_sigaltstack.ss_flags;
579f0089e39SRichard Lowe copyout_noerr(&stk32,
580f0089e39SRichard Lowe (stack32_t *)lwp->lwp_ustack, sizeof (stk32));
581f0089e39SRichard Lowe }
582f0089e39SRichard Lowe }
583f0089e39SRichard Lowe
584f0089e39SRichard Lowe /*
585f0089e39SRichard Lowe * Set up signal handler arguments
586f0089e39SRichard Lowe */
587f0089e39SRichard Lowe {
588f0089e39SRichard Lowe struct sigframe32 frame32;
589f0089e39SRichard Lowe
590f0089e39SRichard Lowe frame32.sip = (caddr32_t)(uintptr_t)sip_addr;
591f0089e39SRichard Lowe frame32.ucp = (caddr32_t)(uintptr_t)uc;
592f0089e39SRichard Lowe frame32.signo = sig;
593f0089e39SRichard Lowe frame32.retaddr = 0xffffffff; /* never return! */
594f0089e39SRichard Lowe copyout_noerr(&frame32, sp, sizeof (frame32));
595f0089e39SRichard Lowe }
596f0089e39SRichard Lowe
597f0089e39SRichard Lowe no_fault();
598f0089e39SRichard Lowe if (watched)
599f0089e39SRichard Lowe watch_enable_addr((caddr_t)sp, minstacksz, S_WRITE);
600f0089e39SRichard Lowe
601f0089e39SRichard Lowe rp->r_sp = (greg_t)(uintptr_t)sp;
602f0089e39SRichard Lowe rp->r_pc = (greg_t)(uintptr_t)hdlr;
603f0089e39SRichard Lowe rp->r_ps = PSL_USER | (rp->r_ps & PS_IOPL);
604f0089e39SRichard Lowe
605f0089e39SRichard Lowe if ((rp->r_cs & 0xffff) != U32CS_SEL ||
606f0089e39SRichard Lowe (rp->r_ss & 0xffff) != UDS_SEL) {
607f0089e39SRichard Lowe /*
608f0089e39SRichard Lowe * Try our best to deliver the signal.
609f0089e39SRichard Lowe */
610f0089e39SRichard Lowe rp->r_cs = U32CS_SEL;
611f0089e39SRichard Lowe rp->r_ss = UDS_SEL;
612f0089e39SRichard Lowe }
613f0089e39SRichard Lowe
614f0089e39SRichard Lowe /*
615f0089e39SRichard Lowe * Don't set lwp_eosys here. sendsig() is called via psig() after
616f0089e39SRichard Lowe * lwp_eosys is handled, so setting it here would affect the next
617f0089e39SRichard Lowe * system call.
618f0089e39SRichard Lowe */
619f0089e39SRichard Lowe return (1);
620f0089e39SRichard Lowe
621f0089e39SRichard Lowe badstack:
622f0089e39SRichard Lowe no_fault();
623*ed093b41SRobert Mustacchi postfault:
624f0089e39SRichard Lowe if (watched)
625f0089e39SRichard Lowe watch_enable_addr((caddr_t)sp, minstacksz, S_WRITE);
626f0089e39SRichard Lowe if (tuc)
627f0089e39SRichard Lowe kmem_free(tuc, sizeof (*tuc));
628f0089e39SRichard Lowe #ifdef DEBUG
629f0089e39SRichard Lowe printf("sendsig32: bad signal stack cmd=%s pid=%d, sig=%d\n",
630f0089e39SRichard Lowe PTOU(p)->u_comm, p->p_pid, sig);
631f0089e39SRichard Lowe printf("on fault, sigsp = 0x%p, action = 0x%p, upc = 0x%lx\n",
632f0089e39SRichard Lowe (void *)sp, (void *)hdlr, (uintptr_t)upc);
633f0089e39SRichard Lowe #endif
634f0089e39SRichard Lowe return (0);
635f0089e39SRichard Lowe }
636f0089e39SRichard Lowe
637f0089e39SRichard Lowe #endif /* _SYSCALL32_IMPL */
638