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