2 * Copyright (c) 2002 Doug Rabson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include "opt_compat.h"
32 #define __ELF_WORD_SIZE 32
34 #include <sys/param.h>
36 #include <sys/fcntl.h>
37 #include <sys/imgact.h>
38 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 #include <sys/mutex.h>
43 #include <sys/namei.h>
44 #include <sys/pioctl.h>
46 #include <sys/procfs.h>
47 #include <sys/resourcevar.h>
48 #include <sys/systm.h>
49 #include <sys/signalvar.h>
52 #include <sys/syscall.h>
53 #include <sys/sysctl.h>
54 #include <sys/sysent.h>
55 #include <sys/vnode.h>
56 #include <sys/imgact_elf.h>
57 #include <sys/sysproto.h>
59 #include <machine/frame.h>
60 #include <machine/md_var.h>
61 #include <machine/pcb.h>
64 #include <vm/vm_kern.h>
65 #include <vm/vm_param.h>
67 #include <vm/vm_map.h>
68 #include <vm/vm_object.h>
69 #include <vm/vm_extern.h>
71 #include <compat/freebsd32/freebsd32_signal.h>
72 #include <compat/freebsd32/freebsd32_util.h>
73 #include <compat/freebsd32/freebsd32_proto.h>
74 #include <compat/ia32/ia32_signal.h>
75 #include <i386/include/psl.h>
76 #include <i386/include/segments.h>
77 #include <i386/include/specialreg.h>
79 char ia32_sigcode[] = {
80 0xff, 0x54, 0x24, 0x10, /* call *SIGF_HANDLER(%esp) */
81 0x8d, 0x44, 0x24, 0x14, /* lea SIGF_UC(%esp),%eax */
82 0x50, /* pushl %eax */
83 0xf7, 0x40, 0x54, 0x00, 0x00, 0x02, 0x02, /* testl $PSL_VM,UC_EFLAGS(%ea
85 0x75, 0x03, /* jne 9f */
86 0x8e, 0x68, 0x14, /* movl UC_GS(%eax),%gs */
87 0xb8, 0x57, 0x01, 0x00, 0x00, /* 9: movl $SYS_sigreturn,%eax */
88 0x50, /* pushl %eax */
89 0xcd, 0x80, /* int $0x80 */
90 0xeb, 0xfe, /* 0: jmp 0b */
93 int sz_ia32_sigcode = sizeof(ia32_sigcode);
96 * Signal sending has not been implemented on ia64. This causes
97 * the sigtramp code to not understand the arguments and the application
98 * will generally crash if it tries to handle a signal. Calling
99 * sendsig() means that at least untrapped signals will work.
102 ia32_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
104 sendsig(catcher, ksi, mask);
107 #ifdef COMPAT_FREEBSD4
109 freebsd4_freebsd32_sigreturn(struct thread *td, struct freebsd4_freebsd32_sigreturn_args *uap)
111 return (sigreturn(td, (struct sigreturn_args *)uap));
116 freebsd32_sigreturn(struct thread *td, struct freebsd32_sigreturn_args *uap)
118 return (sigreturn(td, (struct sigreturn_args *)uap));
123 ia32_setregs(struct thread *td, u_long entry, u_long stack, u_long ps_strings)
125 struct trapframe *tf = td->td_frame;
126 vm_offset_t gdt, ldt;
127 u_int64_t codesel, datasel, ldtsel;
128 u_int64_t codeseg, dataseg, gdtseg, ldtseg;
129 struct segment_descriptor desc;
130 struct vmspace *vmspace = td->td_proc->p_vmspace;
132 exec_setregs(td, entry, stack, ps_strings);
134 /* Non-syscall frames are cleared by exec_setregs() */
135 if (tf->tf_flags & FRAME_SYSCALL) {
136 bzero(&tf->tf_scratch, sizeof(tf->tf_scratch));
137 bzero(&tf->tf_scratch_fp, sizeof(tf->tf_scratch_fp));
139 tf->tf_special.ndirty = 0;
141 tf->tf_special.psr |= IA64_PSR_IS;
142 tf->tf_special.sp = stack;
144 /* Point the RSE backstore to something harmless. */
145 tf->tf_special.bspstore = (FREEBSD32_PS_STRINGS - sz_ia32_sigcode -
146 SPARE_USRSPACE + 15) & ~15;
148 codesel = LSEL(LUCODE_SEL, SEL_UPL);
149 datasel = LSEL(LUDATA_SEL, SEL_UPL);
150 ldtsel = GSEL(GLDT_SEL, SEL_UPL);
152 /* Setup ia32 segment registers. */
153 tf->tf_scratch.gr16 = (datasel << 48) | (datasel << 32) |
154 (datasel << 16) | datasel;
155 tf->tf_scratch.gr17 = (ldtsel << 32) | (datasel << 16) | codesel;
158 * Build the GDT and LDT.
160 gdt = FREEBSD32_USRSTACK;
161 vm_map_find(&vmspace->vm_map, 0, 0, &gdt, IA32_PAGE_SIZE << 1, 0,
162 VM_PROT_ALL, VM_PROT_ALL, 0);
163 ldt = gdt + IA32_PAGE_SIZE;
165 desc.sd_lolimit = 8*NLDT-1;
166 desc.sd_lobase = ldt & 0xffffff;
167 desc.sd_type = SDT_SYSLDT;
168 desc.sd_dpl = SEL_UPL;
173 desc.sd_hibase = ldt >> 24;
174 copyout(&desc, (caddr_t) gdt + 8*GLDT_SEL, sizeof(desc));
176 desc.sd_lolimit = ((FREEBSD32_USRSTACK >> 12) - 1) & 0xffff;
178 desc.sd_type = SDT_MEMERA;
179 desc.sd_dpl = SEL_UPL;
181 desc.sd_hilimit = ((FREEBSD32_USRSTACK >> 12) - 1) >> 16;
185 copyout(&desc, (caddr_t) ldt + 8*LUCODE_SEL, sizeof(desc));
186 desc.sd_type = SDT_MEMRWA;
187 copyout(&desc, (caddr_t) ldt + 8*LUDATA_SEL, sizeof(desc));
189 codeseg = 0 /* base */
190 + (((FREEBSD32_USRSTACK >> 12) - 1) << 32) /* limit */
191 + ((long)SDT_MEMERA << 52)
192 + ((long)SEL_UPL << 57)
193 + (1L << 59) /* present */
194 + (1L << 62) /* 32 bits */
195 + (1L << 63); /* page granularity */
196 dataseg = 0 /* base */
197 + (((FREEBSD32_USRSTACK >> 12) - 1) << 32) /* limit */
198 + ((long)SDT_MEMRWA << 52)
199 + ((long)SEL_UPL << 57)
200 + (1L << 59) /* present */
201 + (1L << 62) /* 32 bits */
202 + (1L << 63); /* page granularity */
204 tf->tf_scratch.csd = codeseg;
205 tf->tf_scratch.ssd = dataseg;
206 tf->tf_scratch.gr24 = dataseg; /* ESD */
207 tf->tf_scratch.gr27 = dataseg; /* DSD */
208 tf->tf_scratch.gr28 = dataseg; /* FSD */
209 tf->tf_scratch.gr29 = dataseg; /* GSD */
211 gdtseg = gdt /* base */
212 + ((8L*NGDT - 1) << 32) /* limit */
213 + ((long)SDT_SYSNULL << 52)
214 + ((long)SEL_UPL << 57)
215 + (1L << 59) /* present */
216 + (0L << 62) /* 16 bits */
217 + (0L << 63); /* byte granularity */
218 ldtseg = ldt /* base */
219 + ((8L*NLDT - 1) << 32) /* limit */
220 + ((long)SDT_SYSLDT << 52)
221 + ((long)SEL_UPL << 57)
222 + (1L << 59) /* present */
223 + (0L << 62) /* 16 bits */
224 + (0L << 63); /* byte granularity */
226 tf->tf_scratch.gr30 = ldtseg; /* LDTD */
227 tf->tf_scratch.gr31 = gdtseg; /* GDTD */
229 /* Set ia32 control registers on this processor. */
230 ia64_set_cflg(CR0_PE | CR0_PG | ((long)(CR4_XMM | CR4_FXSR) << 32));
231 ia64_set_eflag(PSL_USER);
233 /* PS_STRINGS value for BSD/OS binaries. It is 0 for non-BSD/OS. */
234 tf->tf_scratch.gr11 = FREEBSD32_PS_STRINGS;
237 * XXX - Linux emulator
238 * Make sure sure edx is 0x0 on entry. Linux binaries depend
241 td->td_retval[1] = 0;
245 ia32_restorectx(struct pcb *pcb)
248 ia64_set_cflg(pcb->pcb_ia32_cflg);
249 ia64_set_eflag(pcb->pcb_ia32_eflag);
250 ia64_set_fcr(pcb->pcb_ia32_fcr);
251 ia64_set_fdr(pcb->pcb_ia32_fdr);
252 ia64_set_fir(pcb->pcb_ia32_fir);
253 ia64_set_fsr(pcb->pcb_ia32_fsr);
257 ia32_savectx(struct pcb *pcb)
260 pcb->pcb_ia32_cflg = ia64_get_cflg();
261 pcb->pcb_ia32_eflag = ia64_get_eflag();
262 pcb->pcb_ia32_fcr = ia64_get_fcr();
263 pcb->pcb_ia32_fdr = ia64_get_fdr();
264 pcb->pcb_ia32_fir = ia64_get_fir();
265 pcb->pcb_ia32_fsr = ia64_get_fsr();
269 freebsd32_getcontext(struct thread *td, struct freebsd32_getcontext_args *uap)
272 return (nosys(td, NULL));
276 freebsd32_setcontext(struct thread *td, struct freebsd32_setcontext_args *uap)
279 return (nosys(td, NULL));
283 freebsd32_swapcontext(struct thread *td, struct freebsd32_swapcontext_args *uap)
286 return (nosys(td, NULL));