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