1*22ce4affSfengbojiang /*-
2*22ce4affSfengbojiang  * Copyright (c) 2015-2016 Nuxi, https://nuxi.nl/
3*22ce4affSfengbojiang  *
4*22ce4affSfengbojiang  * Redistribution and use in source and binary forms, with or without
5*22ce4affSfengbojiang  * modification, are permitted provided that the following conditions
6*22ce4affSfengbojiang  * are met:
7*22ce4affSfengbojiang  * 1. Redistributions of source code must retain the above copyright
8*22ce4affSfengbojiang  *    notice, this list of conditions and the following disclaimer.
9*22ce4affSfengbojiang  * 2. Redistributions in binary form must reproduce the above copyright
10*22ce4affSfengbojiang  *    notice, this list of conditions and the following disclaimer in the
11*22ce4affSfengbojiang  *    documentation and/or other materials provided with the distribution.
12*22ce4affSfengbojiang  *
13*22ce4affSfengbojiang  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14*22ce4affSfengbojiang  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15*22ce4affSfengbojiang  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16*22ce4affSfengbojiang  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17*22ce4affSfengbojiang  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18*22ce4affSfengbojiang  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19*22ce4affSfengbojiang  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20*22ce4affSfengbojiang  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21*22ce4affSfengbojiang  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22*22ce4affSfengbojiang  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23*22ce4affSfengbojiang  * SUCH DAMAGE.
24*22ce4affSfengbojiang  */
25*22ce4affSfengbojiang 
26*22ce4affSfengbojiang #include <sys/cdefs.h>
27*22ce4affSfengbojiang __FBSDID("$FreeBSD$");
28*22ce4affSfengbojiang 
29*22ce4affSfengbojiang #include <sys/param.h>
30*22ce4affSfengbojiang #include <sys/imgact.h>
31*22ce4affSfengbojiang #include <sys/kernel.h>
32*22ce4affSfengbojiang #include <sys/proc.h>
33*22ce4affSfengbojiang #include <sys/sysent.h>
34*22ce4affSfengbojiang 
35*22ce4affSfengbojiang #include <vm/vm.h>
36*22ce4affSfengbojiang #include <vm/pmap.h>
37*22ce4affSfengbojiang 
38*22ce4affSfengbojiang #include <machine/frame.h>
39*22ce4affSfengbojiang #include <machine/pcb.h>
40*22ce4affSfengbojiang #include <machine/vmparam.h>
41*22ce4affSfengbojiang 
42*22ce4affSfengbojiang #include <compat/cloudabi/cloudabi_util.h>
43*22ce4affSfengbojiang 
44*22ce4affSfengbojiang #include <compat/cloudabi32/cloudabi32_syscall.h>
45*22ce4affSfengbojiang #include <compat/cloudabi32/cloudabi32_util.h>
46*22ce4affSfengbojiang 
47*22ce4affSfengbojiang #include <compat/ia32/ia32_signal.h>
48*22ce4affSfengbojiang #include <compat/ia32/ia32_util.h>
49*22ce4affSfengbojiang 
50*22ce4affSfengbojiang extern const char *cloudabi32_syscallnames[];
51*22ce4affSfengbojiang extern struct sysent cloudabi32_sysent[];
52*22ce4affSfengbojiang 
53*22ce4affSfengbojiang extern unsigned long ia32_maxssiz;
54*22ce4affSfengbojiang 
55*22ce4affSfengbojiang static int
cloudabi32_fixup_tcb(uintptr_t * stack_base,struct image_params * imgp)56*22ce4affSfengbojiang cloudabi32_fixup_tcb(uintptr_t *stack_base, struct image_params *imgp)
57*22ce4affSfengbojiang {
58*22ce4affSfengbojiang 	int error;
59*22ce4affSfengbojiang 	uint32_t args[2];
60*22ce4affSfengbojiang 
61*22ce4affSfengbojiang 	/* Place auxiliary vector and TCB on the stack. */
62*22ce4affSfengbojiang 	error = cloudabi32_fixup(stack_base, imgp);
63*22ce4affSfengbojiang 	if (error != 0)
64*22ce4affSfengbojiang 		return (error);
65*22ce4affSfengbojiang 
66*22ce4affSfengbojiang 	/*
67*22ce4affSfengbojiang 	 * On i386, the TCB is referred to by %gs:0. Reuse the empty
68*22ce4affSfengbojiang 	 * space normally used by the return address (args[0]) to store
69*22ce4affSfengbojiang 	 * a single element array, containing a pointer to the TCB. %gs
70*22ce4affSfengbojiang 	 * base will point to this.
71*22ce4affSfengbojiang 	 *
72*22ce4affSfengbojiang 	 * Also let the first argument of the entry point (args[1])
73*22ce4affSfengbojiang 	 * refer to the auxiliary vector, which is stored right after
74*22ce4affSfengbojiang 	 * the TCB.
75*22ce4affSfengbojiang 	 */
76*22ce4affSfengbojiang 	args[0] = *stack_base;
77*22ce4affSfengbojiang 	args[1] = *stack_base +
78*22ce4affSfengbojiang 	    roundup(sizeof(cloudabi32_tcb_t), sizeof(register_t));
79*22ce4affSfengbojiang 	*stack_base -= roundup2(sizeof(args), sizeof(register_t));
80*22ce4affSfengbojiang 	return (copyout(args, (void *)*stack_base, sizeof(args)));
81*22ce4affSfengbojiang }
82*22ce4affSfengbojiang 
83*22ce4affSfengbojiang static void
cloudabi32_proc_setregs(struct thread * td,struct image_params * imgp,uintptr_t stack)84*22ce4affSfengbojiang cloudabi32_proc_setregs(struct thread *td, struct image_params *imgp,
85*22ce4affSfengbojiang     uintptr_t stack)
86*22ce4affSfengbojiang {
87*22ce4affSfengbojiang 
88*22ce4affSfengbojiang 	ia32_setregs(td, imgp, stack);
89*22ce4affSfengbojiang 	(void)cpu_set_user_tls(td, TO_PTR(stack));
90*22ce4affSfengbojiang }
91*22ce4affSfengbojiang 
92*22ce4affSfengbojiang static int
cloudabi32_fetch_syscall_args(struct thread * td)93*22ce4affSfengbojiang cloudabi32_fetch_syscall_args(struct thread *td)
94*22ce4affSfengbojiang {
95*22ce4affSfengbojiang 	struct trapframe *frame;
96*22ce4affSfengbojiang 	struct syscall_args *sa;
97*22ce4affSfengbojiang 	int error;
98*22ce4affSfengbojiang 
99*22ce4affSfengbojiang 	frame = td->td_frame;
100*22ce4affSfengbojiang 	sa = &td->td_sa;
101*22ce4affSfengbojiang 
102*22ce4affSfengbojiang 	/* Obtain system call number. */
103*22ce4affSfengbojiang 	sa->code = frame->tf_rax;
104*22ce4affSfengbojiang 	if (sa->code >= CLOUDABI32_SYS_MAXSYSCALL)
105*22ce4affSfengbojiang 		return (ENOSYS);
106*22ce4affSfengbojiang 	sa->callp = &cloudabi32_sysent[sa->code];
107*22ce4affSfengbojiang 
108*22ce4affSfengbojiang 	/*
109*22ce4affSfengbojiang 	 * Fetch system call arguments.
110*22ce4affSfengbojiang 	 *
111*22ce4affSfengbojiang 	 * The vDSO has already made sure that the arguments are
112*22ce4affSfengbojiang 	 * eight-byte aligned. Pointers and size_t parameters are
113*22ce4affSfengbojiang 	 * zero-extended. This makes it possible to copy in the
114*22ce4affSfengbojiang 	 * arguments directly. As long as the call doesn't use 32-bit
115*22ce4affSfengbojiang 	 * data structures, we can just invoke the same system call
116*22ce4affSfengbojiang 	 * implementation used by 64-bit processes.
117*22ce4affSfengbojiang 	 */
118*22ce4affSfengbojiang 	error = copyin((void *)frame->tf_rcx, sa->args,
119*22ce4affSfengbojiang 	    sa->callp->sy_narg * sizeof(sa->args[0]));
120*22ce4affSfengbojiang 	if (error != 0)
121*22ce4affSfengbojiang 		return (error);
122*22ce4affSfengbojiang 
123*22ce4affSfengbojiang 	/* Default system call return values. */
124*22ce4affSfengbojiang 	td->td_retval[0] = 0;
125*22ce4affSfengbojiang 	td->td_retval[1] = 0;
126*22ce4affSfengbojiang 	return (0);
127*22ce4affSfengbojiang }
128*22ce4affSfengbojiang 
129*22ce4affSfengbojiang static void
cloudabi32_set_syscall_retval(struct thread * td,int error)130*22ce4affSfengbojiang cloudabi32_set_syscall_retval(struct thread *td, int error)
131*22ce4affSfengbojiang {
132*22ce4affSfengbojiang 	struct trapframe *frame = td->td_frame;
133*22ce4affSfengbojiang 
134*22ce4affSfengbojiang 	switch (error) {
135*22ce4affSfengbojiang 	case 0:
136*22ce4affSfengbojiang 		/*
137*22ce4affSfengbojiang 		 * System call succeeded.
138*22ce4affSfengbojiang 		 *
139*22ce4affSfengbojiang 		 * Simply copy out the 64-bit return values into the
140*22ce4affSfengbojiang 		 * same buffer provided for system call arguments. The
141*22ce4affSfengbojiang 		 * vDSO will copy them to the right spot, truncating
142*22ce4affSfengbojiang 		 * pointers and size_t values to 32 bits.
143*22ce4affSfengbojiang 		 */
144*22ce4affSfengbojiang 		frame->tf_rax = copyout(td->td_retval, (void *)frame->tf_rcx,
145*22ce4affSfengbojiang 		    sizeof(td->td_retval)) == 0 ? 0 : CLOUDABI_EFAULT;
146*22ce4affSfengbojiang 		break;
147*22ce4affSfengbojiang 	case ERESTART:
148*22ce4affSfengbojiang 		/* Restart system call. */
149*22ce4affSfengbojiang 		frame->tf_rip -= frame->tf_err;
150*22ce4affSfengbojiang 		frame->tf_r10 = frame->tf_rcx;
151*22ce4affSfengbojiang 		set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
152*22ce4affSfengbojiang 		break;
153*22ce4affSfengbojiang 	case EJUSTRETURN:
154*22ce4affSfengbojiang 		break;
155*22ce4affSfengbojiang 	default:
156*22ce4affSfengbojiang 		/* System call returned an error. */
157*22ce4affSfengbojiang 		frame->tf_rax = cloudabi_convert_errno(error);
158*22ce4affSfengbojiang 		break;
159*22ce4affSfengbojiang 	}
160*22ce4affSfengbojiang }
161*22ce4affSfengbojiang 
162*22ce4affSfengbojiang static void
cloudabi32_schedtail(struct thread * td)163*22ce4affSfengbojiang cloudabi32_schedtail(struct thread *td)
164*22ce4affSfengbojiang {
165*22ce4affSfengbojiang 	struct trapframe *frame = td->td_frame;
166*22ce4affSfengbojiang 	register_t retval[2];
167*22ce4affSfengbojiang 
168*22ce4affSfengbojiang 	/* Return values for processes returning from fork. */
169*22ce4affSfengbojiang 	if ((td->td_pflags & TDP_FORKING) != 0) {
170*22ce4affSfengbojiang 		retval[0] = CLOUDABI_PROCESS_CHILD;
171*22ce4affSfengbojiang 		retval[1] = td->td_tid;
172*22ce4affSfengbojiang 		copyout(retval, (void *)frame->tf_rcx, sizeof(retval));
173*22ce4affSfengbojiang 	}
174*22ce4affSfengbojiang }
175*22ce4affSfengbojiang 
176*22ce4affSfengbojiang int
cloudabi32_thread_setregs(struct thread * td,const cloudabi32_threadattr_t * attr,uint32_t tcb)177*22ce4affSfengbojiang cloudabi32_thread_setregs(struct thread *td,
178*22ce4affSfengbojiang     const cloudabi32_threadattr_t *attr, uint32_t tcb)
179*22ce4affSfengbojiang {
180*22ce4affSfengbojiang 	stack_t stack;
181*22ce4affSfengbojiang 	uint32_t args[3];
182*22ce4affSfengbojiang 	void *frameptr;
183*22ce4affSfengbojiang 	int error;
184*22ce4affSfengbojiang 
185*22ce4affSfengbojiang 	/* Perform standard register initialization. */
186*22ce4affSfengbojiang 	stack.ss_sp = TO_PTR(attr->stack);
187*22ce4affSfengbojiang 	stack.ss_size = attr->stack_len - sizeof(args);
188*22ce4affSfengbojiang 	cpu_set_upcall(td, TO_PTR(attr->entry_point), NULL, &stack);
189*22ce4affSfengbojiang 
190*22ce4affSfengbojiang 	/*
191*22ce4affSfengbojiang 	 * Copy the arguments for the thread entry point onto the stack
192*22ce4affSfengbojiang 	 * (args[1] and args[2]). Similar to process startup, use the
193*22ce4affSfengbojiang 	 * otherwise unused return address (args[0]) for TLS.
194*22ce4affSfengbojiang 	 */
195*22ce4affSfengbojiang 	args[0] = tcb;
196*22ce4affSfengbojiang 	args[1] = td->td_tid;
197*22ce4affSfengbojiang 	args[2] = attr->argument;
198*22ce4affSfengbojiang 	frameptr = (void *)td->td_frame->tf_rsp;
199*22ce4affSfengbojiang 	error = copyout(args, frameptr, sizeof(args));
200*22ce4affSfengbojiang 	if (error != 0)
201*22ce4affSfengbojiang 		return (error);
202*22ce4affSfengbojiang 
203*22ce4affSfengbojiang 	return (cpu_set_user_tls(td, frameptr));
204*22ce4affSfengbojiang }
205*22ce4affSfengbojiang 
206*22ce4affSfengbojiang static struct sysentvec cloudabi32_elf_sysvec = {
207*22ce4affSfengbojiang 	.sv_size		= CLOUDABI32_SYS_MAXSYSCALL,
208*22ce4affSfengbojiang 	.sv_table		= cloudabi32_sysent,
209*22ce4affSfengbojiang 	.sv_fixup		= cloudabi32_fixup_tcb,
210*22ce4affSfengbojiang 	.sv_name		= "CloudABI ELF32",
211*22ce4affSfengbojiang 	.sv_coredump		= elf32_coredump,
212*22ce4affSfengbojiang 	.sv_minuser		= FREEBSD32_MINUSER,
213*22ce4affSfengbojiang 	.sv_maxuser		= FREEBSD32_MAXUSER,
214*22ce4affSfengbojiang 	.sv_stackprot		= VM_PROT_READ | VM_PROT_WRITE,
215*22ce4affSfengbojiang 	.sv_copyout_strings	= cloudabi32_copyout_strings,
216*22ce4affSfengbojiang 	.sv_setregs		= cloudabi32_proc_setregs,
217*22ce4affSfengbojiang 	.sv_fixlimit		= ia32_fixlimit,
218*22ce4affSfengbojiang 	.sv_maxssiz		= &ia32_maxssiz,
219*22ce4affSfengbojiang 	.sv_flags		= SV_ABI_CLOUDABI | SV_CAPSICUM | SV_IA32 | SV_ILP32,
220*22ce4affSfengbojiang 	.sv_set_syscall_retval	= cloudabi32_set_syscall_retval,
221*22ce4affSfengbojiang 	.sv_fetch_syscall_args	= cloudabi32_fetch_syscall_args,
222*22ce4affSfengbojiang 	.sv_syscallnames	= cloudabi32_syscallnames,
223*22ce4affSfengbojiang 	.sv_schedtail		= cloudabi32_schedtail,
224*22ce4affSfengbojiang };
225*22ce4affSfengbojiang 
226*22ce4affSfengbojiang INIT_SYSENTVEC(elf_sysvec, &cloudabi32_elf_sysvec);
227*22ce4affSfengbojiang 
228*22ce4affSfengbojiang Elf32_Brandinfo cloudabi32_brand = {
229*22ce4affSfengbojiang 	.brand		= ELFOSABI_CLOUDABI,
230*22ce4affSfengbojiang 	.machine	= EM_386,
231*22ce4affSfengbojiang 	.sysvec		= &cloudabi32_elf_sysvec,
232*22ce4affSfengbojiang 	.flags		= BI_BRAND_ONLY_STATIC,
233*22ce4affSfengbojiang };
234