Index: share/man/man4/cloudabi.4 =================================================================== --- share/man/man4/cloudabi.4 +++ share/man/man4/cloudabi.4 @@ -22,7 +22,7 @@ .\" SUCH DAMAGE. .\" .\" $FreeBSD$ -.Dd July 31, 2015 +.Dd October 15, 2015 .Dt CLOUDABI 4 .Os .Sh NAME @@ -73,7 +73,7 @@ .Fx , the .Nm cloudabi64 -module is only available for amd64. +module is only available for amd64 and arm64. .Pp A full cross compilation toolchain for CloudABI is available in the .Pa devel/cloudabi-toolchain @@ -95,6 +95,9 @@ .Pp cloudlibc on GitHub: .Pa https://github.com/NuxiNL/cloudlibc . +.Pp +The CloudABI Ports Collection on GitHub: +.Pa https://github.com/NuxiNL/cloudabi-ports . .Sh HISTORY CloudABI support first appeared in .Fx 11.0 . Index: sys/amd64/cloudabi64/cloudabi64_sysvec.c =================================================================== --- sys/amd64/cloudabi64/cloudabi64_sysvec.c +++ sys/amd64/cloudabi64/cloudabi64_sysvec.c @@ -27,107 +27,26 @@ __FBSDID("$FreeBSD$"); #include -#include -#include -#include #include -#include #include -#include -#include #include -#include -#include #include +#include #include #include #include -#include #include #include #include -#include #include extern const char *cloudabi64_syscallnames[]; extern struct sysent cloudabi64_sysent[]; -static register_t * -cloudabi64_copyout_strings(struct image_params *imgp) -{ - uintptr_t begin; - size_t len; - - /* Copy out program arguments. */ - len = imgp->args->begin_envv - imgp->args->begin_argv; - begin = rounddown2(USRSTACK - len, sizeof(register_t)); - copyout(imgp->args->begin_argv, (void *)begin, len); - return ((register_t *)begin); -} - -static int -cloudabi64_fixup(register_t **stack_base, struct image_params *imgp) -{ - char canarybuf[64]; - Elf64_Auxargs *args; - struct thread *td; - void *argdata, *canary; - size_t argdatalen; - int error; - - /* - * CloudABI executables do not store the FreeBSD OS release - * number in their header. Set the OS release number to the - * latest version of FreeBSD, so that system calls behave as if - * called natively. - */ - td = curthread; - td->td_proc->p_osrel = __FreeBSD_version; - - /* Store canary for stack smashing protection. */ - argdata = *stack_base; - arc4rand(canarybuf, sizeof(canarybuf), 0); - *stack_base -= howmany(sizeof(canarybuf), sizeof(register_t)); - canary = *stack_base; - error = copyout(canarybuf, canary, sizeof(canarybuf)); - if (error != 0) - return (error); - - /* - * Compute length of program arguments. As the argument data is - * binary safe, we had to add a trailing null byte in - * exec_copyin_data_fds(). Undo this by reducing the length. - */ - args = (Elf64_Auxargs *)imgp->auxargs; - argdatalen = imgp->args->begin_envv - imgp->args->begin_argv; - if (argdatalen > 0) - --argdatalen; - - /* Write out an auxiliary vector. */ - cloudabi64_auxv_t auxv[] = { -#define VAL(type, val) { .a_type = (type), .a_val = (val) } -#define PTR(type, ptr) { .a_type = (type), .a_ptr = (uintptr_t)(ptr) } - PTR(CLOUDABI_AT_ARGDATA, argdata), - VAL(CLOUDABI_AT_ARGDATALEN, argdatalen), - PTR(CLOUDABI_AT_CANARY, canary), - VAL(CLOUDABI_AT_CANARYLEN, sizeof(canarybuf)), - VAL(CLOUDABI_AT_NCPUS, mp_ncpus), - VAL(CLOUDABI_AT_PAGESZ, args->pagesz), - PTR(CLOUDABI_AT_PHDR, args->phdr), - VAL(CLOUDABI_AT_PHNUM, args->phnum), - VAL(CLOUDABI_AT_TID, td->td_tid), -#undef VAL -#undef PTR - { .a_type = CLOUDABI_AT_NULL }, - }; - *stack_base -= howmany(sizeof(auxv), sizeof(register_t)); - return (copyout(auxv, *stack_base, sizeof(auxv))); -} - static int cloudabi64_fetch_syscall_args(struct thread *td, struct syscall_args *sa) { @@ -234,43 +153,9 @@ INIT_SYSENTVEC(elf_sysvec, &cloudabi64_elf_sysvec); -static Elf64_Brandinfo cloudabi64_brand = { +Elf64_Brandinfo cloudabi64_brand = { .brand = ELFOSABI_CLOUDABI, .machine = EM_X86_64, .sysvec = &cloudabi64_elf_sysvec, .compat_3_brand = "CloudABI", }; - -static int -cloudabi64_modevent(module_t mod, int type, void *data) -{ - - switch (type) { - case MOD_LOAD: - if (elf64_insert_brand_entry(&cloudabi64_brand) < 0) { - printf("Failed to add CloudABI ELF brand handler\n"); - return (EINVAL); - } - return (0); - case MOD_UNLOAD: - if (elf64_brand_inuse(&cloudabi64_brand)) - return (EBUSY); - if (elf64_remove_brand_entry(&cloudabi64_brand) < 0) { - printf("Failed to remove CloudABI ELF brand handler\n"); - return (EINVAL); - } - return (0); - default: - return (EOPNOTSUPP); - } -} - -static moduledata_t cloudabi64_module = { - "cloudabi64", - cloudabi64_modevent, - NULL -}; - -DECLARE_MODULE_TIED(cloudabi64, cloudabi64_module, SI_SUB_EXEC, SI_ORDER_ANY); -MODULE_DEPEND(cloudabi64, cloudabi, 1, 1, 1); -FEATURE(cloudabi64, "CloudABI 64bit support"); Index: sys/arm64/cloudabi64/cloudabi64_sysvec.c =================================================================== --- sys/arm64/cloudabi64/cloudabi64_sysvec.c +++ sys/arm64/cloudabi64/cloudabi64_sysvec.c @@ -24,132 +24,48 @@ */ #include -__FBSDID("$FreeBSD: head/sys/amd64/cloudabi64/cloudabi64_sysvec.c 286924 2015-08-19 15:18:32Z bapt $"); +__FBSDID("$FreeBSD: 286924 2015-08-19 15:18:32Z bapt $"); #include -#include -#include -#include #include -#include #include -#include -#include #include -#include -#include #include +#include #include #include #include -#include #include #include #include -#include #include extern const char *cloudabi64_syscallnames[]; extern struct sysent cloudabi64_sysent[]; -static register_t * -cloudabi64_copyout_strings(struct image_params *imgp) -{ - uintptr_t begin; - size_t len; - - /* Copy out program arguments. */ - len = imgp->args->begin_envv - imgp->args->begin_argv; - begin = rounddown2(USRSTACK - len, sizeof(register_t)); - copyout(imgp->args->begin_argv, (void *)begin, len); - return ((register_t *)begin); -} - -static int -cloudabi64_fixup(register_t **stack_base, struct image_params *imgp) -{ - char canarybuf[64]; - Elf64_Auxargs *args; - struct thread *td; - void *argdata, *canary; - size_t argdatalen; - int error; - - /* - * CloudABI executables do not store the FreeBSD OS release - * number in their header. Set the OS release number to the - * latest version of FreeBSD, so that system calls behave as if - * called natively. - */ - td = curthread; - td->td_proc->p_osrel = __FreeBSD_version; - - /* Store canary for stack smashing protection. */ - argdata = *stack_base; - arc4rand(canarybuf, sizeof(canarybuf), 0); - *stack_base -= howmany(sizeof(canarybuf), sizeof(register_t)); - canary = *stack_base; - error = copyout(canarybuf, canary, sizeof(canarybuf)); - if (error != 0) - return (error); - - /* - * Compute length of program arguments. As the argument data is - * binary safe, we had to add a trailing null byte in - * exec_copyin_data_fds(). Undo this by reducing the length. - */ - args = (Elf64_Auxargs *)imgp->auxargs; - argdatalen = imgp->args->begin_envv - imgp->args->begin_argv; - if (argdatalen > 0) - --argdatalen; - - /* Write out an auxiliary vector. */ - cloudabi64_auxv_t auxv[] = { -#define VAL(type, val) { .a_type = (type), .a_val = (val) } -#define PTR(type, ptr) { .a_type = (type), .a_ptr = (uintptr_t)(ptr) } - PTR(CLOUDABI_AT_ARGDATA, argdata), - VAL(CLOUDABI_AT_ARGDATALEN, argdatalen), - PTR(CLOUDABI_AT_CANARY, canary), - VAL(CLOUDABI_AT_CANARYLEN, sizeof(canarybuf)), - VAL(CLOUDABI_AT_NCPUS, mp_ncpus), - VAL(CLOUDABI_AT_PAGESZ, args->pagesz), - PTR(CLOUDABI_AT_PHDR, args->phdr), - VAL(CLOUDABI_AT_PHNUM, args->phnum), - VAL(CLOUDABI_AT_TID, td->td_tid), -#undef VAL -#undef PTR - { .a_type = CLOUDABI_AT_NULL }, - }; - *stack_base -= howmany(sizeof(auxv), sizeof(register_t)); - return (copyout(auxv, *stack_base, sizeof(auxv))); -} - static int cloudabi64_fetch_syscall_args(struct thread *td, struct syscall_args *sa) { struct trapframe *frame = td->td_frame; + int i; /* Obtain system call number. */ - sa->code = frame->tf_rax; + sa->code = frame->tf_x[8]; if (sa->code >= CLOUDABI64_SYS_MAXSYSCALL) return (ENOSYS); sa->callp = &cloudabi64_sysent[sa->code]; /* Fetch system call arguments. */ - sa->args[0] = frame->tf_rdi; - sa->args[1] = frame->tf_rsi; - sa->args[2] = frame->tf_rdx; - sa->args[3] = frame->tf_rcx; /* Actually %r10. */ - sa->args[4] = frame->tf_r8; - sa->args[5] = frame->tf_r9; + for (i = 0; i < MAXARGS; i++) + sa->args[i] = frame->tf_x[i]; /* Default system call return values. */ td->td_retval[0] = 0; - td->td_retval[1] = frame->tf_rdx; + td->td_retval[1] = 0; return (0); } @@ -161,22 +77,20 @@ switch (error) { case 0: /* System call succeeded. */ - frame->tf_rax = td->td_retval[0]; - frame->tf_rdx = td->td_retval[1]; - frame->tf_rflags &= ~PSL_C; + frame->tf_x[0] = td->td_retval[0]; + frame->tf_x[1] = td->td_retval[1]; + frame->tf_spsr &= ~PSR_C; break; case ERESTART: /* Restart system call. */ - frame->tf_rip -= frame->tf_err; - frame->tf_r10 = frame->tf_rcx; - set_pcb_flags(td->td_pcb, PCB_FULL_IRET); + frame->tf_elr -= 4; break; case EJUSTRETURN: break; default: /* System call returned an error. */ - frame->tf_rax = cloudabi_convert_errno(error); - frame->tf_rflags |= PSL_C; + frame->tf_x[0] = cloudabi_convert_errno(error); + frame->tf_spsr |= PSR_C; break; } } @@ -186,9 +100,15 @@ { struct trapframe *frame = td->td_frame; - /* Initial register values for processes returning from fork. */ - frame->tf_rax = CLOUDABI_PROCESS_CHILD; - frame->tf_rdx = td->td_tid; + /* + * Initial register values for processes returning from fork. + * Make sure that we only set these values when forking, not + * when creating a new thread. + */ + if (frame->tf_x[0] == 0) { + frame->tf_x[0] = CLOUDABI_PROCESS_CHILD; + frame->tf_x[1] = td->td_tid; + } } void @@ -209,8 +129,8 @@ * entry point. */ frame = td->td_frame; - frame->tf_rdi = td->td_tid; - frame->tf_rsi = attr->argument; + frame->tf_x[0] = td->td_tid; + frame->tf_x[1] = attr->argument; } static struct sysentvec cloudabi64_elf_sysvec = { @@ -234,43 +154,9 @@ INIT_SYSENTVEC(elf_sysvec, &cloudabi64_elf_sysvec); -static Elf64_Brandinfo cloudabi64_brand = { +Elf64_Brandinfo cloudabi64_brand = { .brand = ELFOSABI_CLOUDABI, - .machine = EM_X86_64, + .machine = EM_AARCH64, .sysvec = &cloudabi64_elf_sysvec, .compat_3_brand = "CloudABI", }; - -static int -cloudabi64_modevent(module_t mod, int type, void *data) -{ - - switch (type) { - case MOD_LOAD: - if (elf64_insert_brand_entry(&cloudabi64_brand) < 0) { - printf("Failed to add CloudABI ELF brand handler\n"); - return (EINVAL); - } - return (0); - case MOD_UNLOAD: - if (elf64_brand_inuse(&cloudabi64_brand)) - return (EBUSY); - if (elf64_remove_brand_entry(&cloudabi64_brand) < 0) { - printf("Failed to remove CloudABI ELF brand handler\n"); - return (EINVAL); - } - return (0); - default: - return (EOPNOTSUPP); - } -} - -static moduledata_t cloudabi64_module = { - "cloudabi64", - cloudabi64_modevent, - NULL -}; - -DECLARE_MODULE_TIED(cloudabi64, cloudabi64_module, SI_SUB_EXEC, SI_ORDER_ANY); -MODULE_DEPEND(cloudabi64, cloudabi, 1, 1, 1); -FEATURE(cloudabi64, "CloudABI 64bit support"); Index: sys/compat/cloudabi64/cloudabi64_util.h =================================================================== --- sys/compat/cloudabi64/cloudabi64_util.h +++ sys/compat/cloudabi64/cloudabi64_util.h @@ -28,10 +28,20 @@ #ifndef _CLOUDABI64_UTIL_H_ #define _CLOUDABI64_UTIL_H_ +#include +#include + #include +struct image_params; struct thread; +extern Elf64_Brandinfo cloudabi64_brand; + +/* Stack initialization during process execution. */ +register_t *cloudabi64_copyout_strings(struct image_params *); +int cloudabi64_fixup(register_t **, struct image_params *); + void cloudabi64_thread_setregs(struct thread *, const cloudabi64_threadattr_t *); Index: sys/conf/files =================================================================== --- sys/conf/files +++ sys/conf/files @@ -266,6 +266,7 @@ compat/cloudabi/cloudabi_sock.c optional compat_cloudabi64 compat/cloudabi/cloudabi_thread.c optional compat_cloudabi64 compat/cloudabi64/cloudabi64_fd.c optional compat_cloudabi64 +compat/cloudabi64/cloudabi64_module.c optional compat_cloudabi64 compat/cloudabi64/cloudabi64_poll.c optional compat_cloudabi64 compat/cloudabi64/cloudabi64_sock.c optional compat_cloudabi64 compat/cloudabi64/cloudabi64_syscalls.c optional compat_cloudabi64 Index: sys/conf/files.arm64 =================================================================== --- sys/conf/files.arm64 +++ sys/conf/files.arm64 @@ -53,6 +53,7 @@ arm64/cavium/thunder_pcie.c optional soc_cavm_thunderx pci fdt arm64/cavium/thunder_pcie_pem.c optional soc_cavm_thunderx pci arm64/cavium/thunder_pcie_common.c optional soc_cavm_thunderx pci +arm64/cloudabi64/cloudabi64_sysvec.c optional compat_cloudabi64 crypto/blowfish/bf_enc.c optional crypto | ipsec crypto/des/des_enc.c optional crypto | ipsec | netsmb dev/acpica/acpi_if.m optional acpi Index: sys/modules/Makefile =================================================================== --- sys/modules/Makefile +++ sys/modules/Makefile @@ -633,7 +633,6 @@ .endif .if ${MACHINE_CPUARCH} == "amd64" -_cloudabi64= cloudabi64 _ioat= ioat _ixl= ixl _ixlv= ixlv @@ -749,6 +748,10 @@ .endif .endif +.if ${MACHINE_CPUARCH} == "aarch64" || ${MACHINE_CPUARCH} == "amd64" +_cloudabi64= cloudabi64 +.endif + .endif SUBDIR+=${MODULES_EXTRA} Index: sys/modules/cloudabi64/Makefile =================================================================== --- sys/modules/cloudabi64/Makefile +++ sys/modules/cloudabi64/Makefile @@ -4,8 +4,8 @@ .PATH: ${.CURDIR}/../../${MACHINE}/cloudabi64 KMOD= cloudabi64 -SRCS= cloudabi64_fd.c cloudabi64_poll.c cloudabi64_sock.c \ - cloudabi64_syscalls.c cloudabi64_sysent.c cloudabi64_sysvec.c \ - cloudabi64_thread.c +SRCS= cloudabi64_fd.c cloudabi64_module.c cloudabi64_poll.c \ + cloudabi64_sock.c cloudabi64_syscalls.c cloudabi64_sysent.c \ + cloudabi64_sysvec.c cloudabi64_thread.c .include