Path: blob/main/lib/libc/aarch64/gen/getcontextx.c
48255 views
/*1* Copyright (c) 2011 Konstantin Belousov <[email protected]>2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7*8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR15* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES16* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.17* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,18* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT19* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,20* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY21* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT22* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF23* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.24*/2526#include <sys/types.h>27#include <sys/ucontext.h>28#include <errno.h>29#include <stdlib.h>3031int32__getcontextx_size(void)33{34size_t size;3536size = sizeof(ucontext_t);37size += sizeof(struct arm64_reg_context); /* Space for ARM64_CTX_END */3839return (size);40}4142int43__fillcontextx2(char *ctx)44{45struct arm64_reg_context *reg_ctx;46ucontext_t *ucp;4748ucp = (ucontext_t *)ctx;49ucp->uc_mcontext.mc_ptr = (uint64_t)(ucp + 1);5051reg_ctx = (struct arm64_reg_context *)ucp->uc_mcontext.mc_ptr;52reg_ctx->ctx_id = ARM64_CTX_END;53reg_ctx->ctx_size = sizeof(struct arm64_reg_context);5455return (0);56}5758int59__fillcontextx(char *ctx)60{61ucontext_t *ucp;6263ucp = (ucontext_t *)ctx;64if (getcontext(ucp) == -1)65return (-1);66__fillcontextx2(ctx);67return (0);68}6970__weak_reference(__getcontextx, getcontextx);7172ucontext_t *73__getcontextx(void)74{75char *ctx;76int error;7778ctx = malloc(__getcontextx_size());79if (ctx == NULL)80return (NULL);81if (__fillcontextx(ctx) == -1) {82error = errno;83free(ctx);84errno = error;85return (NULL);86}87return ((ucontext_t *)ctx);88}899091