ia32_sysvec.c (102808) | ia32_sysvec.c (114987) |
---|---|
1/*- 2 * Copyright (c) 2002 Doug Rabson | 1/*- 2 * Copyright (c) 2002 Doug Rabson |
3 * Copyright (c) 2003 Peter Wemm |
|
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 --- 7 unchanged lines hidden (view full) --- 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 * | 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright --- 7 unchanged lines hidden (view full) --- 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 * |
26 * $FreeBSD: head/sys/compat/ia32/ia32_sysvec.c 102808 2002-09-01 21:41:24Z jake $ | 27 * $FreeBSD: head/sys/compat/ia32/ia32_sysvec.c 114987 2003-05-14 04:10:49Z peter $ |
27 */ 28 | 28 */ 29 |
30#include "opt_compat.h" 31 |
|
29#define __ELF_WORD_SIZE 32 30 31#include <sys/param.h> 32#include <sys/exec.h> 33#include <sys/fcntl.h> 34#include <sys/imgact.h> 35#include <sys/kernel.h> 36#include <sys/lock.h> --- 18 unchanged lines hidden (view full) --- 55#include <vm/vm.h> 56#include <vm/vm_kern.h> 57#include <vm/vm_param.h> 58#include <vm/pmap.h> 59#include <vm/vm_map.h> 60#include <vm/vm_object.h> 61#include <vm/vm_extern.h> 62 | 32#define __ELF_WORD_SIZE 32 33 34#include <sys/param.h> 35#include <sys/exec.h> 36#include <sys/fcntl.h> 37#include <sys/imgact.h> 38#include <sys/kernel.h> 39#include <sys/lock.h> --- 18 unchanged lines hidden (view full) --- 58#include <vm/vm.h> 59#include <vm/vm_kern.h> 60#include <vm/vm_param.h> 61#include <vm/pmap.h> 62#include <vm/vm_map.h> 63#include <vm/vm_object.h> 64#include <vm/vm_extern.h> 65 |
63#include <ia64/ia32/ia32_util.h> 64#include <i386/include/psl.h> 65#include <i386/include/segments.h> 66#include <i386/include/specialreg.h> | 66#include <amd64/ia32/ia32_util.h> 67#include <amd64/ia32/ia32_proto.h> 68#include <amd64/ia32/ia32_signal.h> 69#include <machine/psl.h> 70#include <machine/segments.h> 71#include <machine/specialreg.h> |
67#include <machine/frame.h> 68#include <machine/md_var.h> | 72#include <machine/frame.h> 73#include <machine/md_var.h> |
74#include <machine/pcb.h> 75#include <machine/cpufunc.h> |
|
69 70static register_t *ia32_copyout_strings(struct image_params *imgp); 71static void ia32_setregs(struct thread *td, u_long entry, u_long stack, 72 u_long ps_strings); 73 74extern struct sysent ia32_sysent[]; 75 | 76 77static register_t *ia32_copyout_strings(struct image_params *imgp); 78static void ia32_setregs(struct thread *td, u_long entry, u_long stack, 79 u_long ps_strings); 80 81extern struct sysent ia32_sysent[]; 82 |
76static char ia32_sigcode[] = { 77 0xff, 0x54, 0x24, 0x10, /* call *SIGF_HANDLER(%esp) */ 78 0x8d, 0x44, 0x24, 0x14, /* lea SIGF_UC(%esp),%eax */ 79 0x50, /* pushl %eax */ 80 0xf7, 0x40, 0x54, 0x00, 0x00, 0x02, 0x02, /* testl $PSL_VM,UC_EFLAGS(%eax) */ 81 0x75, 0x03, /* jne 9f */ 82 0x8e, 0x68, 0x14, /* movl UC_GS(%eax),%gs */ 83 0xb8, 0x57, 0x01, 0x00, 0x00, /* 9: movl $SYS_sigreturn,%eax */ 84 0x50, /* pushl %eax */ 85 0xcd, 0x80, /* int $0x80 */ 86 0xeb, 0xfe, /* 0: jmp 0b */ 87 0, 0, 0, 0 88}; 89static int ia32_szsigcode = sizeof(ia32_sigcode) & ~3; 90 | |
91struct sysentvec ia32_freebsd_sysvec = { 92 SYS_MAXSYSCALL, 93 ia32_sysent, 94 0, 95 0, 96 NULL, 97 0, 98 NULL, 99 NULL, 100 elf32_freebsd_fixup, | 83struct sysentvec ia32_freebsd_sysvec = { 84 SYS_MAXSYSCALL, 85 ia32_sysent, 86 0, 87 0, 88 NULL, 89 0, 90 NULL, 91 NULL, 92 elf32_freebsd_fixup, |
101 sendsig, | 93 ia32_sendsig, |
102 ia32_sigcode, | 94 ia32_sigcode, |
103 &ia32_szsigcode, | 95 &sz_ia32_sigcode, |
104 NULL, | 96 NULL, |
105 "FreeBSD ELF", | 97 "FreeBSD ELF32", |
106 elf32_coredump, 107 NULL, 108 MINSIGSTKSZ, | 98 elf32_coredump, 99 NULL, 100 MINSIGSTKSZ, |
109 4096, | 101 PAGE_SIZE, |
110 0, | 102 0, |
111 IA32_USRSTACK, 112 IA32_USRSTACK, 113 IA32_PS_STRINGS, | 103 USRSTACK, 104 USRSTACK, 105 PS_STRINGS, |
114 VM_PROT_ALL, 115 ia32_copyout_strings, 116 ia32_setregs 117}; 118 | 106 VM_PROT_ALL, 107 ia32_copyout_strings, 108 ia32_setregs 109}; 110 |
111 112 |
|
119static Elf32_Brandinfo ia32_brand_info = { 120 ELFOSABI_FREEBSD, 121 EM_386, 122 "FreeBSD", 123 "/compat/ia32", 124 "/usr/libexec/ld-elf.so.1", 125 &ia32_freebsd_sysvec 126 }; 127 128SYSINIT(ia32, SI_SUB_EXEC, SI_ORDER_ANY, 129 (sysinit_cfunc_t) elf32_insert_brand_entry, 130 &ia32_brand_info); 131 | 113static Elf32_Brandinfo ia32_brand_info = { 114 ELFOSABI_FREEBSD, 115 EM_386, 116 "FreeBSD", 117 "/compat/ia32", 118 "/usr/libexec/ld-elf.so.1", 119 &ia32_freebsd_sysvec 120 }; 121 122SYSINIT(ia32, SI_SUB_EXEC, SI_ORDER_ANY, 123 (sysinit_cfunc_t) elf32_insert_brand_entry, 124 &ia32_brand_info); 125 |
126extern int _ucode32sel, _udatasel; 127 |
|
132static register_t * 133ia32_copyout_strings(struct image_params *imgp) 134{ 135 int argc, envc; 136 u_int32_t *vectp; 137 char *stringp, *destp; 138 u_int32_t *stack_base; 139 struct ia32_ps_strings *arginfo; 140 int szsigcode; 141 142 /* 143 * Calculate string base and vector table pointers. 144 * Also deal with signal trampoline code for this exec type. 145 */ | 128static register_t * 129ia32_copyout_strings(struct image_params *imgp) 130{ 131 int argc, envc; 132 u_int32_t *vectp; 133 char *stringp, *destp; 134 u_int32_t *stack_base; 135 struct ia32_ps_strings *arginfo; 136 int szsigcode; 137 138 /* 139 * Calculate string base and vector table pointers. 140 * Also deal with signal trampoline code for this exec type. 141 */ |
146 arginfo = (struct ia32_ps_strings *)IA32_PS_STRINGS; | 142 arginfo = (struct ia32_ps_strings *)PS_STRINGS; |
147 szsigcode = *(imgp->proc->p_sysent->sv_szsigcode); 148 destp = (caddr_t)arginfo - szsigcode - SPARE_USRSPACE - 149 roundup((ARG_MAX - imgp->stringspace), sizeof(char *)); 150 151 /* 152 * install sigcode 153 */ 154 if (szsigcode) --- 30 unchanged lines hidden (view full) --- 185 /* 186 * vectp also becomes our initial stack base 187 */ 188 stack_base = vectp; 189 190 stringp = imgp->stringbase; 191 argc = imgp->argc; 192 envc = imgp->envc; | 143 szsigcode = *(imgp->proc->p_sysent->sv_szsigcode); 144 destp = (caddr_t)arginfo - szsigcode - SPARE_USRSPACE - 145 roundup((ARG_MAX - imgp->stringspace), sizeof(char *)); 146 147 /* 148 * install sigcode 149 */ 150 if (szsigcode) --- 30 unchanged lines hidden (view full) --- 181 /* 182 * vectp also becomes our initial stack base 183 */ 184 stack_base = vectp; 185 186 stringp = imgp->stringbase; 187 argc = imgp->argc; 188 envc = imgp->envc; |
193 | |
194 /* 195 * Copy out strings - arguments and environment. 196 */ 197 copyout(stringp, destp, ARG_MAX - imgp->stringspace); 198 199 /* 200 * Fill in "ps_strings" struct for ps, w, etc. 201 */ --- 27 unchanged lines hidden (view full) --- 229 } 230 231 /* end of vector table is a null pointer */ 232 suword32(vectp, 0); 233 234 return ((register_t *)stack_base); 235} 236 | 189 /* 190 * Copy out strings - arguments and environment. 191 */ 192 copyout(stringp, destp, ARG_MAX - imgp->stringspace); 193 194 /* 195 * Fill in "ps_strings" struct for ps, w, etc. 196 */ --- 27 unchanged lines hidden (view full) --- 224 } 225 226 /* end of vector table is a null pointer */ 227 suword32(vectp, 0); 228 229 return ((register_t *)stack_base); 230} 231 |
237static void 238ia32_setregs(struct thread *td, u_long entry, u_long stack, u_long ps_strings) | 232/* 233 * Clear registers on exec 234 */ 235void 236ia32_setregs(td, entry, stack, ps_strings) 237 struct thread *td; 238 u_long entry; 239 u_long stack; 240 u_long ps_strings; |
239{ | 241{ |
240 struct trapframe *frame = td->td_frame; 241 vm_offset_t gdt, ldt; 242 u_int64_t codesel, datasel, ldtsel; 243 u_int64_t codeseg, dataseg, gdtseg, ldtseg; 244 struct segment_descriptor desc; 245 struct vmspace *vmspace = td->td_proc->p_vmspace; | 242 struct trapframe *regs = td->td_frame; 243 struct pcb *pcb = td->td_pcb; 244 u_int64_t pc; 245 register_t s; 246 247 wrmsr(MSR_FSBASE, 0); 248 wrmsr(MSR_KGSBASE, 0); /* User value while we're in the kernel */ 249 pcb->pcb_fsbase = 0; 250 pcb->pcb_gsbase = 0; 251 pcb->pcb_kgsbase = rdmsr(MSR_GSBASE); 252 load_ds(_udatasel); 253 load_es(_udatasel); 254 load_fs(_udatasel); 255 s = intr_disable(); 256 pc = rdmsr(MSR_GSBASE); 257 load_gs(_udatasel); /* Clobbers kernel %GS.base */ 258 wrmsr(MSR_GSBASE, pc); 259 intr_restore(s); 260 pcb->pcb_ds = _udatasel; 261 pcb->pcb_es = _udatasel; 262 pcb->pcb_fs = _udatasel; 263 pcb->pcb_gs = _udatasel; |
246 | 264 |
247 /* 248 * Make sure that we restore the entire trapframe after an 249 * execve. 250 */ 251 frame->tf_flags &= ~FRAME_SYSCALL; | 265 bzero((char *)regs, sizeof(struct trapframe)); 266 regs->tf_rip = entry; 267 regs->tf_rsp = stack; 268 regs->tf_rflags = PSL_USER | (regs->tf_rflags & PSL_T); 269 regs->tf_ss = _udatasel; 270 regs->tf_cs = _ucode32sel; 271 regs->tf_rbx = ps_strings; |
252 | 272 |
253 bzero(frame->tf_r, sizeof(frame->tf_r)); 254 bzero(frame->tf_f, sizeof(frame->tf_f)); 255 256 frame->tf_cr_iip = entry; 257 frame->tf_cr_ipsr = (IA64_PSR_IC 258 | IA64_PSR_I 259 | IA64_PSR_IT 260 | IA64_PSR_DT 261 | IA64_PSR_RT 262 | IA64_PSR_DFH 263 | IA64_PSR_IS 264 | IA64_PSR_BN 265 | IA64_PSR_CPL_USER); 266 frame->tf_r[FRAME_R12] = stack; 267 268 codesel = LSEL(LUCODE_SEL, SEL_UPL); 269 datasel = LSEL(LUDATA_SEL, SEL_UPL); 270 ldtsel = GSEL(GLDT_SEL, SEL_UPL); 271 272#if 1 273 frame->tf_r[FRAME_R16] = (datasel << 48) | (datasel << 32) 274 | (datasel << 16) | datasel; 275 frame->tf_r[FRAME_R17] = (ldtsel << 32) | (datasel << 16) | codesel; 276#else 277 frame->tf_r[FRAME_R16] = datasel; 278 frame->tf_r[FRAME_R17] = codesel; 279 frame->tf_r[FRAME_R18] = datasel; 280 frame->tf_r[FRAME_R19] = datasel; 281 frame->tf_r[FRAME_R20] = datasel; 282 frame->tf_r[FRAME_R21] = datasel; 283 frame->tf_r[FRAME_R22] = ldtsel; 284#endif 285 | |
286 /* | 273 /* |
287 * Build the GDT and LDT. | 274 * Arrange to trap the next npx or `fwait' instruction (see npx.c 275 * for why fwait must be trapped at least if there is an npx or an 276 * emulator). This is mainly to handle the case where npx0 is not 277 * configured, since the npx routines normally set up the trap 278 * otherwise. It should be done only at boot time, but doing it 279 * here allows modifying `npx_exists' for testing the emulator on 280 * systems with an npx. |
288 */ | 281 */ |
289 gdt = IA32_USRSTACK; 290 vm_map_find(&vmspace->vm_map, 0, 0, 291 &gdt, PAGE_SIZE, 0, 292 VM_PROT_ALL, VM_PROT_ALL, 0); 293 ldt = gdt + 4096; | 282 load_cr0(rcr0() | CR0_MP | CR0_TS); |
294 | 283 |
295 desc.sd_lolimit = 8*NLDT-1; 296 desc.sd_lobase = ldt & 0xffffff; 297 desc.sd_type = SDT_SYSLDT; 298 desc.sd_dpl = SEL_UPL; 299 desc.sd_p = 1; 300 desc.sd_hilimit = 0; 301 desc.sd_def32 = 0; 302 desc.sd_gran = 0; 303 desc.sd_hibase = ldt >> 24; 304 copyout(&desc, (caddr_t) gdt + 8*GLDT_SEL, sizeof(desc)); | 284 fpstate_drop(td); |
305 | 285 |
306 desc.sd_lolimit = ((IA32_USRSTACK >> 12) - 1) & 0xffff; 307 desc.sd_lobase = 0; 308 desc.sd_type = SDT_MEMERA; 309 desc.sd_dpl = SEL_UPL; 310 desc.sd_p = 1; 311 desc.sd_hilimit = ((IA32_USRSTACK >> 12) - 1) >> 16; 312 desc.sd_def32 = 1; 313 desc.sd_gran = 1; 314 desc.sd_hibase = 0; 315 copyout(&desc, (caddr_t) ldt + 8*LUCODE_SEL, sizeof(desc)); 316 desc.sd_type = SDT_MEMRWA; 317 copyout(&desc, (caddr_t) ldt + 8*LUDATA_SEL, sizeof(desc)); 318 319 codeseg = 0 /* base */ 320 + (((IA32_USRSTACK >> 12) - 1) << 32) /* limit */ 321 + ((long)SDT_MEMERA << 52) 322 + ((long)SEL_UPL << 57) 323 + (1L << 59) /* present */ 324 + (1L << 62) /* 32 bits */ 325 + (1L << 63); /* page granularity */ 326 dataseg = 0 /* base */ 327 + (((IA32_USRSTACK >> 12) - 1) << 32) /* limit */ 328 + ((long)SDT_MEMRWA << 52) 329 + ((long)SEL_UPL << 57) 330 + (1L << 59) /* present */ 331 + (1L << 62) /* 32 bits */ 332 + (1L << 63); /* page granularity */ 333 ia64_set_csd(codeseg); 334 ia64_set_ssd(dataseg); 335 frame->tf_r[FRAME_R24] = dataseg; /* ESD */ 336 frame->tf_r[FRAME_R27] = dataseg; /* DSD */ 337 frame->tf_r[FRAME_R28] = dataseg; /* FSD */ 338 frame->tf_r[FRAME_R29] = dataseg; /* GSD */ 339 340 gdtseg = gdt /* base */ 341 + ((8L*NGDT - 1) << 32) /* limit */ 342 + ((long)SDT_SYSNULL << 52) 343 + ((long)SEL_UPL << 57) 344 + (1L << 59) /* present */ 345 + (0L << 62) /* 16 bits */ 346 + (0L << 63); /* byte granularity */ 347 ldtseg = ldt /* base */ 348 + ((8L*NLDT - 1) << 32) /* limit */ 349 + ((long)SDT_SYSLDT << 52) 350 + ((long)SEL_UPL << 57) 351 + (1L << 59) /* present */ 352 + (0L << 62) /* 16 bits */ 353 + (0L << 63); /* byte granularity */ 354 frame->tf_r[FRAME_R30] = ldtseg; /* LDTD */ 355 frame->tf_r[FRAME_R31] = gdtseg; /* GDTD */ 356 357 ia64_set_eflag(PSL_USER); 358 359 /* PS_STRINGS value for BSD/OS binaries. It is 0 for non-BSD/OS. */ 360 frame->tf_r[FRAME_R11] = IA32_PS_STRINGS; 361 362 /* 363 * XXX - Linux emulator 364 * Make sure sure edx is 0x0 on entry. Linux binaries depend 365 * on it. 366 */ | 286 /* Return via doreti so that we can change to a different %cs */ 287 pcb->pcb_flags |= PCB_FULLCTX; |
367 td->td_retval[1] = 0; 368} | 288 td->td_retval[1] = 0; 289} |