xref: /freebsd-13-stable/sys/arm/cloudabi32/cloudabi32_sysvec.c (revision 3bc80996974a61a4223eae4c1ccd47b6ee32a48a)
1 /*-
2  * Copyright (c) 2015-2016 Nuxi, https://nuxi.nl/
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  */
25 
26 #include <sys/cdefs.h>
27 #include <sys/param.h>
28 #include <sys/imgact.h>
29 #include <sys/kernel.h>
30 #include <sys/proc.h>
31 #include <sys/sysent.h>
32 
33 #include <vm/vm.h>
34 #include <vm/pmap.h>
35 
36 #include <machine/frame.h>
37 #include <machine/pcb.h>
38 #include <machine/vmparam.h>
39 
40 #include <compat/cloudabi/cloudabi_util.h>
41 
42 #include <compat/cloudabi32/cloudabi32_syscall.h>
43 #include <compat/cloudabi32/cloudabi32_util.h>
44 
45 extern const char *cloudabi32_syscallnames[];
46 extern struct sysent cloudabi32_sysent[];
47 
48 static void
cloudabi32_proc_setregs(struct thread * td,struct image_params * imgp,uintptr_t stack)49 cloudabi32_proc_setregs(struct thread *td, struct image_params *imgp,
50     uintptr_t stack)
51 {
52 	struct trapframe *regs;
53 
54 	exec_setregs(td, imgp, stack);
55 
56 	/*
57 	 * The stack now contains a pointer to the TCB and the auxiliary
58 	 * vector. Let r0 point to the auxiliary vector, and set
59 	 * tpidrurw to the TCB.
60 	 */
61 	regs = td->td_frame;
62 	regs->tf_r0 =
63 	    stack + roundup(sizeof(cloudabi32_tcb_t), sizeof(register_t));
64 	(void)cpu_set_user_tls(td, TO_PTR(stack));
65 }
66 
67 static int
cloudabi32_fetch_syscall_args(struct thread * td)68 cloudabi32_fetch_syscall_args(struct thread *td)
69 {
70 	struct trapframe *frame;
71 	struct syscall_args *sa;
72 	int error;
73 
74 	frame = td->td_frame;
75 	sa = &td->td_sa;
76 
77 	/* Obtain system call number. */
78 	sa->code = frame->tf_r12;
79 	if (sa->code >= CLOUDABI32_SYS_MAXSYSCALL)
80 		return (ENOSYS);
81 	sa->callp = &cloudabi32_sysent[sa->code];
82 
83 	/* Fetch system call arguments from registers and the stack. */
84 	sa->args[0] = frame->tf_r0;
85 	sa->args[1] = frame->tf_r1;
86 	sa->args[2] = frame->tf_r2;
87 	sa->args[3] = frame->tf_r3;
88 	if (sa->callp->sy_narg > 4) {
89 		error = copyin((void *)td->td_frame->tf_usr_sp, &sa->args[4],
90 		    (sa->callp->sy_narg - 4) * sizeof(register_t));
91 		if (error != 0)
92 			return (error);
93 	}
94 
95 	/* Default system call return values. */
96 	td->td_retval[0] = 0;
97 	td->td_retval[1] = frame->tf_r1;
98 	return (0);
99 }
100 
101 static void
cloudabi32_set_syscall_retval(struct thread * td,int error)102 cloudabi32_set_syscall_retval(struct thread *td, int error)
103 {
104 	struct trapframe *frame = td->td_frame;
105 
106 	switch (error) {
107 	case 0:
108 		/* System call succeeded. */
109 		frame->tf_r0 = td->td_retval[0];
110 		frame->tf_r1 = td->td_retval[1];
111 		frame->tf_spsr &= ~PSR_C;
112 		break;
113 	case ERESTART:
114 		/* Restart system call. */
115 		frame->tf_pc -= 4;
116 		break;
117 	case EJUSTRETURN:
118 		break;
119 	default:
120 		/* System call returned an error. */
121 		frame->tf_r0 = cloudabi_convert_errno(error);
122 		frame->tf_spsr |= PSR_C;
123 		break;
124 	}
125 }
126 
127 static void
cloudabi32_schedtail(struct thread * td)128 cloudabi32_schedtail(struct thread *td)
129 {
130 	struct trapframe *frame = td->td_frame;
131 
132 	/*
133 	 * Initial register values for processes returning from fork.
134 	 * Make sure that we only set these values when forking, not
135 	 * when creating a new thread.
136 	 */
137 	if ((td->td_pflags & TDP_FORKING) != 0) {
138 		frame->tf_r0 = CLOUDABI_PROCESS_CHILD;
139 		frame->tf_r1 = td->td_tid;
140 	}
141 }
142 
143 int
cloudabi32_thread_setregs(struct thread * td,const cloudabi32_threadattr_t * attr,uint32_t tcb)144 cloudabi32_thread_setregs(struct thread *td,
145     const cloudabi32_threadattr_t *attr, uint32_t tcb)
146 {
147 	struct trapframe *frame;
148 	stack_t stack;
149 
150 	/* Perform standard register initialization. */
151 	stack.ss_sp = TO_PTR(attr->stack);
152 	stack.ss_size = attr->stack_len;
153 	cpu_set_upcall(td, TO_PTR(attr->entry_point), NULL, &stack);
154 
155 	/*
156 	 * Pass in the thread ID of the new thread and the argument
157 	 * pointer provided by the parent thread in as arguments to the
158 	 * entry point.
159 	 */
160 	frame = td->td_frame;
161 	frame->tf_r0 = td->td_tid;
162 	frame->tf_r1 = attr->argument;
163 
164 	/* Set up TLS. */
165 	return (cpu_set_user_tls(td, TO_PTR(tcb)));
166 }
167 
168 static struct sysentvec cloudabi32_elf_sysvec = {
169 	.sv_size		= CLOUDABI32_SYS_MAXSYSCALL,
170 	.sv_table		= cloudabi32_sysent,
171 	.sv_fixup		= cloudabi32_fixup,
172 	.sv_name		= "CloudABI ELF32",
173 	.sv_coredump		= elf32_coredump,
174 	.sv_elf_core_osabi	= ELFOSABI_FREEBSD,
175 	.sv_elf_core_abi_vendor	= FREEBSD_ABI_VENDOR,
176 	.sv_elf_core_prepare_notes = elf32_prepare_notes,
177 	.sv_minuser		= VM_MIN_ADDRESS,
178 	.sv_maxuser		= VM_MAXUSER_ADDRESS,
179 	.sv_stackprot		= VM_PROT_READ | VM_PROT_WRITE,
180 	.sv_copyout_strings	= cloudabi32_copyout_strings,
181 	.sv_setregs		= cloudabi32_proc_setregs,
182 	.sv_flags		= SV_ABI_CLOUDABI | SV_CAPSICUM | SV_ILP32,
183 	.sv_set_syscall_retval	= cloudabi32_set_syscall_retval,
184 	.sv_fetch_syscall_args	= cloudabi32_fetch_syscall_args,
185 	.sv_syscallnames	= cloudabi32_syscallnames,
186 	.sv_schedtail		= cloudabi32_schedtail,
187 };
188 
189 INIT_SYSENTVEC(elf_sysvec, &cloudabi32_elf_sysvec);
190 
191 Elf32_Brandinfo cloudabi32_brand = {
192 	.brand		= ELFOSABI_CLOUDABI,
193 	.machine	= EM_ARM,
194 	.sysvec		= &cloudabi32_elf_sysvec,
195 	.flags		= BI_BRAND_ONLY_STATIC,
196 };
197