]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/mips/mips/freebsd32_machdep.c
dts: Update our copy for arm, arm64 and riscv dts to Linux 5.5
[FreeBSD/FreeBSD.git] / sys / mips / mips / freebsd32_machdep.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2012 Juli Mallett <jmallett@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30
31 /*
32  * Based on nwhitehorn's COMPAT_FREEBSD32 support code for PowerPC64.
33  */
34
35 #define __ELF_WORD_SIZE 32
36
37 #include <sys/types.h>
38 #include <sys/param.h>
39 #include <sys/kernel.h>
40 #include <sys/systm.h>
41 #include <sys/sysent.h>
42 #include <sys/exec.h>
43 #include <sys/imgact.h>
44 #include <sys/ktr.h>
45 #include <sys/malloc.h>
46 #include <sys/proc.h>
47 #include <sys/namei.h>
48 #include <sys/fcntl.h>
49 #include <sys/sysent.h>
50 #include <sys/imgact_elf.h>
51 #include <sys/syscall.h>
52 #include <sys/syscallsubr.h>
53 #include <sys/sysproto.h>
54 #include <sys/signalvar.h>
55 #include <sys/vnode.h>
56 #include <sys/linker.h>
57
58 #include <vm/vm.h>
59 #include <vm/vm_param.h>
60
61 #include <machine/md_var.h>
62 #include <machine/reg.h>
63 #include <machine/sigframe.h>
64 #include <machine/sysarch.h>
65 #include <machine/tls.h>
66
67 #include <compat/freebsd32/freebsd32_signal.h>
68 #include <compat/freebsd32/freebsd32_util.h>
69 #include <compat/freebsd32/freebsd32_proto.h>
70
71 static void freebsd32_exec_setregs(struct thread *, struct image_params *,
72     uintptr_t);
73 static int get_mcontext32(struct thread *, mcontext32_t *, int);
74 static int set_mcontext32(struct thread *, mcontext32_t *);
75 static void freebsd32_sendsig(sig_t, ksiginfo_t *, sigset_t *);
76
77 extern const char *freebsd32_syscallnames[];
78
79 struct sysentvec elf32_freebsd_sysvec = {
80         .sv_size        = SYS_MAXSYSCALL,
81         .sv_table       = freebsd32_sysent,
82         .sv_errsize     = 0,
83         .sv_errtbl      = NULL,
84         .sv_transtrap   = NULL,
85         .sv_fixup       = __elfN(freebsd_fixup),
86         .sv_sendsig     = freebsd32_sendsig,
87         .sv_sigcode     = sigcode32,
88         .sv_szsigcode   = &szsigcode32,
89         .sv_name        = "FreeBSD ELF32",
90         .sv_coredump    = __elfN(coredump),
91         .sv_imgact_try  = NULL,
92         .sv_minsigstksz = MINSIGSTKSZ,
93         .sv_minuser     = VM_MIN_ADDRESS,
94         .sv_maxuser     = ((vm_offset_t)0x80000000),
95         .sv_usrstack    = FREEBSD32_USRSTACK,
96         .sv_psstrings   = FREEBSD32_PS_STRINGS,
97         .sv_stackprot   = VM_PROT_ALL,
98         .sv_copyout_auxargs = __elfN(freebsd_copyout_auxargs),
99         .sv_copyout_strings = freebsd32_copyout_strings,
100         .sv_setregs     = freebsd32_exec_setregs,
101         .sv_fixlimit    = NULL,
102         .sv_maxssiz     = NULL,
103         .sv_flags       = SV_ABI_FREEBSD | SV_ILP32,
104         .sv_set_syscall_retval = cpu_set_syscall_retval,
105         .sv_fetch_syscall_args = cpu_fetch_syscall_args,
106         .sv_syscallnames = freebsd32_syscallnames,
107         .sv_schedtail   = NULL,
108         .sv_thread_detach = NULL,
109         .sv_trap        = NULL,
110 };
111 INIT_SYSENTVEC(elf32_sysvec, &elf32_freebsd_sysvec);
112
113 static Elf32_Brandinfo freebsd_brand_info = {
114         .brand          = ELFOSABI_FREEBSD,
115         .machine        = EM_MIPS,
116         .compat_3_brand = "FreeBSD",
117         .emul_path      = NULL,
118         .interp_path    = "/libexec/ld-elf.so.1",
119         .sysvec         = &elf32_freebsd_sysvec,
120         .interp_newpath = "/libexec/ld-elf32.so.1",
121         .brand_note     = &elf32_freebsd_brandnote,
122         .flags          = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
123 };
124
125 SYSINIT(elf32, SI_SUB_EXEC, SI_ORDER_FIRST,
126     (sysinit_cfunc_t) elf32_insert_brand_entry,
127     &freebsd_brand_info);
128
129 static void
130 freebsd32_exec_setregs(struct thread *td, struct image_params *imgp,
131     uintptr_t stack)
132 {
133         exec_setregs(td, imgp, stack);
134
135         /*
136          * See comment in exec_setregs about running 32-bit binaries with 64-bit
137          * registers.
138          */
139         td->td_frame->sp -= 65536;
140
141         /*
142          * Clear extended address space bit for userland.
143          */
144         td->td_frame->sr &= ~MIPS_SR_UX;
145
146         td->td_md.md_tls_tcb_offset = TLS_TP_OFFSET + TLS_TCB_SIZE32;
147 }
148
149 int
150 set_regs32(struct thread *td, struct reg32 *regs)
151 {
152         struct reg r;
153         unsigned i;
154
155         for (i = 0; i < NUMSAVEREGS; i++)
156                 r.r_regs[i] = regs->r_regs[i];
157
158         return (set_regs(td, &r));
159 }
160
161 int
162 fill_regs32(struct thread *td, struct reg32 *regs)
163 {
164         struct reg r;
165         unsigned i;
166         int error;
167
168         error = fill_regs(td, &r);
169         if (error != 0)
170                 return (error);
171
172         for (i = 0; i < NUMSAVEREGS; i++)
173                 regs->r_regs[i] = r.r_regs[i];
174
175         return (0);
176 }
177
178 int
179 set_fpregs32(struct thread *td, struct fpreg32 *fpregs)
180 {
181         struct fpreg fp;
182         unsigned i;
183
184         for (i = 0; i < NUMFPREGS; i++)
185                 fp.r_regs[i] = fpregs->r_regs[i];
186
187         return (set_fpregs(td, &fp));
188 }
189
190 int
191 fill_fpregs32(struct thread *td, struct fpreg32 *fpregs)
192 {
193         struct fpreg fp;
194         unsigned i;
195         int error;
196
197         error = fill_fpregs(td, &fp);
198         if (error != 0)
199                 return (error);
200
201         for (i = 0; i < NUMFPREGS; i++)
202                 fpregs->r_regs[i] = fp.r_regs[i];
203
204         return (0);
205 }
206
207 static int
208 get_mcontext32(struct thread *td, mcontext32_t *mcp, int flags)
209 {
210         mcontext_t mcp64;
211         unsigned i;
212         int error;
213
214         error = get_mcontext(td, &mcp64, flags);
215         if (error != 0)
216                 return (error);
217
218         mcp->mc_onstack = mcp64.mc_onstack;
219         mcp->mc_pc = mcp64.mc_pc;
220         for (i = 0; i < 32; i++)
221                 mcp->mc_regs[i] = mcp64.mc_regs[i];
222         mcp->sr = mcp64.sr;
223         mcp->mullo = mcp64.mullo;
224         mcp->mulhi = mcp64.mulhi;
225         mcp->mc_fpused = mcp64.mc_fpused;
226         for (i = 0; i < 33; i++)
227                 mcp->mc_fpregs[i] = mcp64.mc_fpregs[i];
228         mcp->mc_fpc_eir = mcp64.mc_fpc_eir;
229         mcp->mc_tls = (int32_t)(intptr_t)mcp64.mc_tls;
230
231         return (0);
232 }
233
234 static int
235 set_mcontext32(struct thread *td, mcontext32_t *mcp)
236 {
237         mcontext_t mcp64;
238         unsigned i;
239
240         mcp64.mc_onstack = mcp->mc_onstack;
241         mcp64.mc_pc = mcp->mc_pc;
242         for (i = 0; i < 32; i++)
243                 mcp64.mc_regs[i] = mcp->mc_regs[i];
244         mcp64.sr = mcp->sr;
245         mcp64.mullo = mcp->mullo;
246         mcp64.mulhi = mcp->mulhi;
247         mcp64.mc_fpused = mcp->mc_fpused;
248         for (i = 0; i < 33; i++)
249                 mcp64.mc_fpregs[i] = mcp->mc_fpregs[i];
250         mcp64.mc_fpc_eir = mcp->mc_fpc_eir;
251         mcp64.mc_tls = (void *)(intptr_t)mcp->mc_tls;
252
253         return (set_mcontext(td, &mcp64));
254 }
255
256 int
257 freebsd32_sigreturn(struct thread *td, struct freebsd32_sigreturn_args *uap)
258 {
259         ucontext32_t uc;
260         int error;
261
262         CTR2(KTR_SIG, "sigreturn: td=%p ucp=%p", td, uap->sigcntxp);
263
264         if (copyin(uap->sigcntxp, &uc, sizeof(uc)) != 0) {
265                 CTR1(KTR_SIG, "sigreturn: efault td=%p", td);
266                 return (EFAULT);
267         }
268
269         error = set_mcontext32(td, &uc.uc_mcontext);
270         if (error != 0)
271                 return (error);
272
273         kern_sigprocmask(td, SIG_SETMASK, &uc.uc_sigmask, NULL, 0);
274
275 #if 0
276         CTR3(KTR_SIG, "sigreturn: return td=%p pc=%#x sp=%#x",
277              td, uc.uc_mcontext.mc_srr0, uc.uc_mcontext.mc_gpr[1]);
278 #endif
279
280         return (EJUSTRETURN);
281 }
282
283 /*
284  * The first two fields of a ucontext_t are the signal mask and the machine
285  * context.  The next field is uc_link; we want to avoid destroying the link
286  * when copying out contexts.
287  */
288 #define UC32_COPY_SIZE  offsetof(ucontext32_t, uc_link)
289
290 int
291 freebsd32_getcontext(struct thread *td, struct freebsd32_getcontext_args *uap)
292 {
293         ucontext32_t uc;
294         int ret;
295
296         if (uap->ucp == NULL)
297                 ret = EINVAL;
298         else {
299                 bzero(&uc, sizeof(uc));
300                 get_mcontext32(td, &uc.uc_mcontext, GET_MC_CLEAR_RET);
301                 PROC_LOCK(td->td_proc);
302                 uc.uc_sigmask = td->td_sigmask;
303                 PROC_UNLOCK(td->td_proc);
304                 ret = copyout(&uc, uap->ucp, UC32_COPY_SIZE);
305         }
306         return (ret);
307 }
308
309 int
310 freebsd32_setcontext(struct thread *td, struct freebsd32_setcontext_args *uap)
311 {
312         ucontext32_t uc;
313         int ret;        
314
315         if (uap->ucp == NULL)
316                 ret = EINVAL;
317         else {
318                 ret = copyin(uap->ucp, &uc, UC32_COPY_SIZE);
319                 if (ret == 0) {
320                         ret = set_mcontext32(td, &uc.uc_mcontext);
321                         if (ret == 0) {
322                                 kern_sigprocmask(td, SIG_SETMASK,
323                                     &uc.uc_sigmask, NULL, 0);
324                         }
325                 }
326         }
327         return (ret == 0 ? EJUSTRETURN : ret);
328 }
329
330 int
331 freebsd32_swapcontext(struct thread *td, struct freebsd32_swapcontext_args *uap)
332 {
333         ucontext32_t uc;
334         int ret;
335
336         if (uap->oucp == NULL || uap->ucp == NULL)
337                 ret = EINVAL;
338         else {
339                 bzero(&uc, sizeof(uc));
340                 get_mcontext32(td, &uc.uc_mcontext, GET_MC_CLEAR_RET);
341                 PROC_LOCK(td->td_proc);
342                 uc.uc_sigmask = td->td_sigmask;
343                 PROC_UNLOCK(td->td_proc);
344                 ret = copyout(&uc, uap->oucp, UC32_COPY_SIZE);
345                 if (ret == 0) {
346                         ret = copyin(uap->ucp, &uc, UC32_COPY_SIZE);
347                         if (ret == 0) {
348                                 ret = set_mcontext32(td, &uc.uc_mcontext);
349                                 if (ret == 0) {
350                                         kern_sigprocmask(td, SIG_SETMASK,
351                                             &uc.uc_sigmask, NULL, 0);
352                                 }
353                         }
354                 }
355         }
356         return (ret == 0 ? EJUSTRETURN : ret);
357 }
358
359 #define UCONTEXT_MAGIC  0xACEDBADE
360
361 /*
362  * Send an interrupt to process.
363  *
364  * Stack is set up to allow sigcode stored
365  * at top to call routine, followed by kcall
366  * to sigreturn routine below.  After sigreturn
367  * resets the signal mask, the stack, and the
368  * frame pointer, it returns to the user
369  * specified pc, psl.
370  */
371 static void
372 freebsd32_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
373 {
374         struct proc *p;
375         struct thread *td;
376         struct fpreg32 fpregs;
377         struct reg32 regs;
378         struct sigacts *psp;
379         struct sigframe32 sf, *sfp;
380         int sig;
381         int oonstack;
382         unsigned i;
383
384         td = curthread;
385         p = td->td_proc;
386         PROC_LOCK_ASSERT(p, MA_OWNED);
387         sig = ksi->ksi_signo;
388         psp = p->p_sigacts;
389         mtx_assert(&psp->ps_mtx, MA_OWNED);
390
391         fill_regs32(td, &regs);
392         oonstack = sigonstack(td->td_frame->sp);
393
394         /* save user context */
395         bzero(&sf, sizeof sf);
396         sf.sf_uc.uc_sigmask = *mask;
397         sf.sf_uc.uc_stack.ss_sp = (int32_t)(intptr_t)td->td_sigstk.ss_sp;
398         sf.sf_uc.uc_stack.ss_size = td->td_sigstk.ss_size;
399         sf.sf_uc.uc_stack.ss_flags = td->td_sigstk.ss_flags;
400         sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
401         sf.sf_uc.uc_mcontext.mc_pc = regs.r_regs[PC];
402         sf.sf_uc.uc_mcontext.mullo = regs.r_regs[MULLO];
403         sf.sf_uc.uc_mcontext.mulhi = regs.r_regs[MULHI];
404         sf.sf_uc.uc_mcontext.mc_tls = (int32_t)(intptr_t)td->td_md.md_tls;
405         sf.sf_uc.uc_mcontext.mc_regs[0] = UCONTEXT_MAGIC;  /* magic number */
406         for (i = 1; i < 32; i++)
407                 sf.sf_uc.uc_mcontext.mc_regs[i] = regs.r_regs[i];
408         sf.sf_uc.uc_mcontext.mc_fpused = td->td_md.md_flags & MDTD_FPUSED;
409         if (sf.sf_uc.uc_mcontext.mc_fpused) {
410                 /* if FPU has current state, save it first */
411                 if (td == PCPU_GET(fpcurthread))
412                         MipsSaveCurFPState(td);
413                 fill_fpregs32(td, &fpregs);
414                 for (i = 0; i < 33; i++)
415                         sf.sf_uc.uc_mcontext.mc_fpregs[i] = fpregs.r_regs[i];
416         }
417
418         /* Allocate and validate space for the signal handler context. */
419         if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
420             SIGISMEMBER(psp->ps_sigonstack, sig)) {
421                 sfp = (struct sigframe32 *)(((uintptr_t)td->td_sigstk.ss_sp +
422                     td->td_sigstk.ss_size - sizeof(struct sigframe32))
423                     & ~(sizeof(__int64_t) - 1));
424         } else
425                 sfp = (struct sigframe32 *)((vm_offset_t)(td->td_frame->sp - 
426                     sizeof(struct sigframe32)) & ~(sizeof(__int64_t) - 1));
427
428         /* Build the argument list for the signal handler. */
429         td->td_frame->a0 = sig;
430         td->td_frame->a2 = (register_t)(intptr_t)&sfp->sf_uc;
431         if (SIGISMEMBER(psp->ps_siginfo, sig)) {
432                 /* Signal handler installed with SA_SIGINFO. */
433                 td->td_frame->a1 = (register_t)(intptr_t)&sfp->sf_si;
434                 /* sf.sf_ahu.sf_action = (__siginfohandler_t *)catcher; */
435
436                 /* fill siginfo structure */
437                 sf.sf_si.si_signo = sig;
438                 sf.sf_si.si_code = ksi->ksi_code;
439                 sf.sf_si.si_addr = td->td_frame->badvaddr;
440         } else {
441                 /* Old FreeBSD-style arguments. */
442                 td->td_frame->a1 = ksi->ksi_code;
443                 td->td_frame->a3 = td->td_frame->badvaddr;
444                 /* sf.sf_ahu.sf_handler = catcher; */
445         }
446
447         mtx_unlock(&psp->ps_mtx);
448         PROC_UNLOCK(p);
449
450         /*
451          * Copy the sigframe out to the user's stack.
452          */
453         if (copyout(&sf, sfp, sizeof(struct sigframe32)) != 0) {
454                 /*
455                  * Something is wrong with the stack pointer.
456                  * ...Kill the process.
457                  */
458                 PROC_LOCK(p);
459                 sigexit(td, SIGILL);
460         }
461
462         td->td_frame->pc = (register_t)(intptr_t)catcher;
463         td->td_frame->t9 = (register_t)(intptr_t)catcher;
464         td->td_frame->sp = (register_t)(intptr_t)sfp;
465         /*
466          * Signal trampoline code is at base of user stack.
467          */
468         td->td_frame->ra = (register_t)(intptr_t)FREEBSD32_PS_STRINGS - *(p->p_sysent->sv_szsigcode);
469         PROC_LOCK(p);
470         mtx_lock(&psp->ps_mtx);
471 }
472
473 int
474 freebsd32_sysarch(struct thread *td, struct freebsd32_sysarch_args *uap)
475 {
476         int error;
477         int32_t tlsbase;
478
479         switch (uap->op) {
480         case MIPS_SET_TLS:
481                 td->td_md.md_tls = (void *)(intptr_t)uap->parms;
482                 return (0);
483         case MIPS_GET_TLS: 
484                 tlsbase = (int32_t)(intptr_t)td->td_md.md_tls;
485                 error = copyout(&tlsbase, uap->parms, sizeof(tlsbase));
486                 return (error);
487         default:
488                 break;
489         }
490         return (EINVAL);
491 }
492
493 void
494 elf32_dump_thread(struct thread *td __unused, void *dst __unused,
495     size_t *off __unused)
496 {
497 }