cloudabi32_sysvec.c revision 316576
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__FBSDID("$FreeBSD: stable/11/sys/arm/cloudabi32/cloudabi32_sysvec.c 316576 2017-04-06 15:19:18Z kib $"); 28 29#include <sys/param.h> 30#include <sys/imgact.h> 31#include <sys/kernel.h> 32#include <sys/proc.h> 33#include <sys/sysent.h> 34 35#include <vm/vm.h> 36#include <vm/pmap.h> 37 38#include <machine/frame.h> 39#include <machine/pcb.h> 40#include <machine/vmparam.h> 41 42#include <compat/cloudabi/cloudabi_util.h> 43 44#include <compat/cloudabi32/cloudabi32_syscall.h> 45#include <compat/cloudabi32/cloudabi32_util.h> 46 47extern const char *cloudabi32_syscallnames[]; 48extern struct sysent cloudabi32_sysent[]; 49 50static void 51cloudabi32_proc_setregs(struct thread *td, struct image_params *imgp, 52 unsigned long stack) 53{ 54 struct trapframe *regs; 55 56 exec_setregs(td, imgp, stack); 57 58 /* 59 * The stack now contains a pointer to the TCB and the auxiliary 60 * vector. Let r0 point to the auxiliary vector, and set 61 * tpidrurw to the TCB. 62 */ 63 regs = td->td_frame; 64 regs->tf_r0 = td->td_retval[0] = 65 stack + roundup(sizeof(cloudabi32_tcb_t), sizeof(register_t)); 66 (void)cpu_set_user_tls(td, (void *)stack); 67} 68 69static int 70cloudabi32_fetch_syscall_args(struct thread *td, struct syscall_args *sa) 71{ 72 struct trapframe *frame = td->td_frame; 73 int error; 74 75 /* Obtain system call number. */ 76 sa->code = frame->tf_r12; 77 if (sa->code >= CLOUDABI32_SYS_MAXSYSCALL) 78 return (ENOSYS); 79 sa->callp = &cloudabi32_sysent[sa->code]; 80 sa->narg = sa->callp->sy_narg; 81 82 /* Fetch system call arguments from registers and the stack. */ 83 sa->args[0] = frame->tf_r0; 84 sa->args[1] = frame->tf_r1; 85 sa->args[2] = frame->tf_r2; 86 sa->args[3] = frame->tf_r3; 87 if (sa->narg > 4) { 88 error = copyin((void *)td->td_frame->tf_usr_sp, &sa->args[4], 89 (sa->narg - 4) * sizeof(register_t)); 90 if (error != 0) 91 return (error); 92 } 93 94 /* Default system call return values. */ 95 td->td_retval[0] = 0; 96 td->td_retval[1] = frame->tf_r1; 97 return (0); 98} 99 100static void 101cloudabi32_set_syscall_retval(struct thread *td, int error) 102{ 103 struct trapframe *frame = td->td_frame; 104 105 switch (error) { 106 case 0: 107 /* System call succeeded. */ 108 frame->tf_r0 = td->td_retval[0]; 109 frame->tf_r1 = td->td_retval[1]; 110 frame->tf_spsr &= ~PSR_C; 111 break; 112 case ERESTART: 113 /* Restart system call. */ 114 frame->tf_pc -= 4; 115 break; 116 case EJUSTRETURN: 117 break; 118 default: 119 /* System call returned an error. */ 120 frame->tf_r0 = cloudabi_convert_errno(error); 121 frame->tf_spsr |= PSR_C; 122 break; 123 } 124} 125 126static void 127cloudabi32_schedtail(struct thread *td) 128{ 129 struct trapframe *frame = td->td_frame; 130 131 /* 132 * Initial register values for processes returning from fork. 133 * Make sure that we only set these values when forking, not 134 * when creating a new thread. 135 */ 136 if ((td->td_pflags & TDP_FORKING) != 0) { 137 frame->tf_r0 = CLOUDABI_PROCESS_CHILD; 138 frame->tf_r1 = td->td_tid; 139 } 140} 141 142int 143cloudabi32_thread_setregs(struct thread *td, 144 const cloudabi32_threadattr_t *attr, uint32_t tcb) 145{ 146 struct trapframe *frame; 147 stack_t stack; 148 149 /* Perform standard register initialization. */ 150 stack.ss_sp = TO_PTR(attr->stack); 151 stack.ss_size = attr->stack_len; 152 cpu_set_upcall(td, TO_PTR(attr->entry_point), NULL, &stack); 153 154 /* 155 * Pass in the thread ID of the new thread and the argument 156 * pointer provided by the parent thread in as arguments to the 157 * entry point. 158 */ 159 frame = td->td_frame; 160 frame->tf_r0 = td->td_tid; 161 frame->tf_r1 = attr->argument; 162 163 /* Set up TLS. */ 164 return (cpu_set_user_tls(td, (void *)tcb)); 165} 166 167static struct sysentvec cloudabi32_elf_sysvec = { 168 .sv_size = CLOUDABI32_SYS_MAXSYSCALL, 169 .sv_table = cloudabi32_sysent, 170 .sv_fixup = cloudabi32_fixup, 171 .sv_name = "CloudABI ELF32", 172 .sv_coredump = elf32_coredump, 173 .sv_pagesize = PAGE_SIZE, 174 .sv_minuser = VM_MIN_ADDRESS, 175 .sv_maxuser = VM_MAXUSER_ADDRESS, 176 .sv_stackprot = VM_PROT_READ | VM_PROT_WRITE, 177 .sv_copyout_strings = cloudabi32_copyout_strings, 178 .sv_setregs = cloudabi32_proc_setregs, 179 .sv_flags = SV_ABI_CLOUDABI | SV_CAPSICUM | SV_ILP32, 180 .sv_set_syscall_retval = cloudabi32_set_syscall_retval, 181 .sv_fetch_syscall_args = cloudabi32_fetch_syscall_args, 182 .sv_syscallnames = cloudabi32_syscallnames, 183 .sv_schedtail = cloudabi32_schedtail, 184}; 185 186INIT_SYSENTVEC(elf_sysvec, &cloudabi32_elf_sysvec); 187 188Elf32_Brandinfo cloudabi32_brand = { 189 .brand = ELFOSABI_CLOUDABI, 190 .machine = EM_ARM, 191 .sysvec = &cloudabi32_elf_sysvec, 192 .flags = BI_BRAND_ONLY_STATIC, 193}; 194