xref: /freebsd-13-stable/sys/amd64/cloudabi64/cloudabi64_sysvec.c (revision 3bc80996974a61a4223eae4c1ccd47b6ee32a48a)
1 /*-
2  * Copyright (c) 2015 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/md_var.h>
38 #include <machine/pcb.h>
39 #include <machine/vmparam.h>
40 
41 #include <compat/cloudabi/cloudabi_util.h>
42 
43 #include <compat/cloudabi64/cloudabi64_syscall.h>
44 #include <compat/cloudabi64/cloudabi64_util.h>
45 
46 extern const char *cloudabi64_syscallnames[];
47 extern struct sysent cloudabi64_sysent[];
48 
49 static int
cloudabi64_fixup_tcb(uintptr_t * stack_base,struct image_params * imgp)50 cloudabi64_fixup_tcb(uintptr_t *stack_base, struct image_params *imgp)
51 {
52 	int error;
53 	register_t tcbptr;
54 
55 	/* Place auxiliary vector and TCB on the stack. */
56 	error = cloudabi64_fixup(stack_base, imgp);
57 	if (error != 0)
58 		return (error);
59 
60 	/*
61 	 * On x86-64, the TCB is referred to by %fs:0. Take some space
62 	 * from the top of the stack to store a single element array,
63 	 * containing a pointer to the TCB. %fs base will point to this.
64 	 */
65 	tcbptr = (register_t)*stack_base;
66 	*stack_base -= sizeof(tcbptr);
67 	return (copyout(&tcbptr, (void *)*stack_base, sizeof(tcbptr)));
68 }
69 
70 static void
cloudabi64_proc_setregs(struct thread * td,struct image_params * imgp,uintptr_t stack)71 cloudabi64_proc_setregs(struct thread *td, struct image_params *imgp,
72     uintptr_t stack)
73 {
74 	struct trapframe *regs;
75 
76 	exec_setregs(td, imgp, stack);
77 
78 	/*
79 	 * The stack now contains a pointer to the TCB, the TCB itself,
80 	 * and the auxiliary vector. Let %rdx point to the auxiliary
81 	 * vector, and set %fs base to the address of the TCB.
82 	 */
83 	regs = td->td_frame;
84 	regs->tf_rdi = stack + sizeof(register_t) +
85 	    roundup(sizeof(cloudabi64_tcb_t), sizeof(register_t));
86 	(void)cpu_set_user_tls(td, TO_PTR(stack));
87 }
88 
89 static int
cloudabi64_fetch_syscall_args(struct thread * td)90 cloudabi64_fetch_syscall_args(struct thread *td)
91 {
92 	struct trapframe *frame;
93 	struct syscall_args *sa;
94 
95 	frame = td->td_frame;
96 	sa = &td->td_sa;
97 
98 	/* Obtain system call number. */
99 	sa->code = frame->tf_rax;
100 	if (sa->code >= CLOUDABI64_SYS_MAXSYSCALL)
101 		return (ENOSYS);
102 	sa->callp = &cloudabi64_sysent[sa->code];
103 
104 	/* Fetch system call arguments. */
105 	sa->args[0] = frame->tf_rdi;
106 	sa->args[1] = frame->tf_rsi;
107 	sa->args[2] = frame->tf_rdx;
108 	sa->args[3] = frame->tf_rcx; /* Actually %r10. */
109 	sa->args[4] = frame->tf_r8;
110 	sa->args[5] = frame->tf_r9;
111 
112 	/* Default system call return values. */
113 	td->td_retval[0] = 0;
114 	td->td_retval[1] = frame->tf_rdx;
115 	return (0);
116 }
117 
118 static void
cloudabi64_set_syscall_retval(struct thread * td,int error)119 cloudabi64_set_syscall_retval(struct thread *td, int error)
120 {
121 	struct trapframe *frame = td->td_frame;
122 
123 	switch (error) {
124 	case 0:
125 		/* System call succeeded. */
126 		frame->tf_rax = td->td_retval[0];
127 		frame->tf_rdx = td->td_retval[1];
128 		frame->tf_rflags &= ~PSL_C;
129 		break;
130 	case ERESTART:
131 		/* Restart system call. */
132 		frame->tf_rip -= frame->tf_err;
133 		frame->tf_r10 = frame->tf_rcx;
134 		set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
135 		break;
136 	case EJUSTRETURN:
137 		break;
138 	default:
139 		/* System call returned an error. */
140 		frame->tf_rax = cloudabi_convert_errno(error);
141 		frame->tf_rflags |= PSL_C;
142 		break;
143 	}
144 }
145 
146 static void
cloudabi64_schedtail(struct thread * td)147 cloudabi64_schedtail(struct thread *td)
148 {
149 	struct trapframe *frame = td->td_frame;
150 
151 	/* Initial register values for processes returning from fork. */
152 	frame->tf_rax = CLOUDABI_PROCESS_CHILD;
153 	frame->tf_rdx = td->td_tid;
154 }
155 
156 int
cloudabi64_thread_setregs(struct thread * td,const cloudabi64_threadattr_t * attr,uint64_t tcb)157 cloudabi64_thread_setregs(struct thread *td,
158     const cloudabi64_threadattr_t *attr, uint64_t tcb)
159 {
160 	struct trapframe *frame;
161 	stack_t stack;
162 	uint64_t tcbptr;
163 	int error;
164 
165 	/*
166 	 * On x86-64, the TCB is referred to by %fs:0. Take some space
167 	 * from the top of the stack to store a single element array,
168 	 * containing a pointer to the TCB. %fs base will point to this.
169 	 */
170 	tcbptr = rounddown(attr->stack + attr->stack_len - sizeof(tcbptr),
171 	    _Alignof(tcbptr));
172 	error = copyout(&tcb, (void *)tcbptr, sizeof(tcb));
173 	if (error != 0)
174 		return (error);
175 
176 	/* Perform standard register initialization. */
177 	stack.ss_sp = TO_PTR(attr->stack);
178 	stack.ss_size = tcbptr - attr->stack;
179 	cpu_set_upcall(td, TO_PTR(attr->entry_point), NULL, &stack);
180 
181 	/*
182 	 * Pass in the thread ID of the new thread and the argument
183 	 * pointer provided by the parent thread in as arguments to the
184 	 * entry point.
185 	 */
186 	frame = td->td_frame;
187 	frame->tf_rdi = td->td_tid;
188 	frame->tf_rsi = attr->argument;
189 
190 	return (cpu_set_user_tls(td, TO_PTR(tcbptr)));
191 }
192 
193 static struct sysentvec cloudabi64_elf_sysvec = {
194 	.sv_size		= CLOUDABI64_SYS_MAXSYSCALL,
195 	.sv_table		= cloudabi64_sysent,
196 	.sv_fixup		= cloudabi64_fixup_tcb,
197 	.sv_name		= "CloudABI ELF64",
198 	.sv_coredump		= elf64_coredump,
199 	.sv_elf_core_osabi	= ELFOSABI_FREEBSD,
200 	.sv_elf_core_abi_vendor	= FREEBSD_ABI_VENDOR,
201 	.sv_elf_core_prepare_notes = elf64_prepare_notes,
202 	.sv_minuser		= VM_MIN_ADDRESS,
203 	/* Keep top page reserved to work around AMD Ryzen stability issues. */
204 	.sv_maxuser		= VM_MAXUSER_ADDRESS - PAGE_SIZE,
205 	.sv_stackprot		= VM_PROT_READ | VM_PROT_WRITE,
206 	.sv_copyout_strings	= cloudabi64_copyout_strings,
207 	.sv_setregs		= cloudabi64_proc_setregs,
208 	.sv_flags		= SV_ABI_CLOUDABI | SV_CAPSICUM | SV_LP64,
209 	.sv_set_syscall_retval	= cloudabi64_set_syscall_retval,
210 	.sv_fetch_syscall_args	= cloudabi64_fetch_syscall_args,
211 	.sv_syscallnames	= cloudabi64_syscallnames,
212 	.sv_schedtail		= cloudabi64_schedtail,
213 	.sv_set_fork_retval	= x86_set_fork_retval,
214 };
215 
216 INIT_SYSENTVEC(elf_sysvec, &cloudabi64_elf_sysvec);
217 
218 Elf64_Brandinfo cloudabi64_brand = {
219 	.brand		= ELFOSABI_CLOUDABI,
220 	.machine	= EM_X86_64,
221 	.sysvec		= &cloudabi64_elf_sysvec,
222 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_ONLY_STATIC,
223 };
224