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