arch/sim: Move setjmp/longjmp to libc/machine/sim

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
This commit is contained in:
Xiang Xiao 2021-04-05 13:01:15 +08:00 committed by Alan Carvalho de Assis
parent acc3596adc
commit dd1d980c3a
25 changed files with 225 additions and 161 deletions

View File

@ -76,6 +76,8 @@ config ARCH_SIM
select ARCH_HAVE_POWEROFF
select ARCH_HAVE_TESTSET
select ARCH_HAVE_VFORK
select ARCH_HAVE_SETJMP
select ARCH_SETJMP_H
select ALARM_ARCH
select ONESHOT
select SERIAL_CONSOLE

View File

@ -25,48 +25,30 @@
#ifndef __ARCH_SIM_INCLUDE_IRQ_H
#define __ARCH_SIM_INCLUDE_IRQ_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <arch/setjmp.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define NR_IRQS 64
/* Number of registers saved in context switch */
#if defined(CONFIG_HOST_X86_64) && !defined(CONFIG_SIM_M32)
/* Storage order: %rbx, %rsp, %rbp, %r12, %r13, %r14, %r15, %rip */
# define XCPTCONTEXT_REGS 8
# define XCPTCONTEXT_SIZE (8 * XCPTCONTEXT_REGS)
#elif defined(CONFIG_HOST_X86) || defined(CONFIG_SIM_M32)
/* Storage order: %ebx, %esi, %edi, %ebp, sp, and return PC */
# define XCPTCONTEXT_REGS 6
# define XCPTCONTEXT_SIZE (4 * XCPTCONTEXT_REGS)
#elif defined(CONFIG_HOST_ARM)
# define XCPTCONTEXT_REGS 16
# define XCPTCONTEXT_SIZE (4 * XCPTCONTEXT_REGS)
#endif
/****************************************************************************
* Public Types
****************************************************************************/
#ifndef __ASSEMBLY__
#if defined(CONFIG_HOST_X86_64) && !defined(CONFIG_SIM_M32)
typedef unsigned long xcpt_reg_t;
#else
typedef unsigned int xcpt_reg_t;
#endif
/* This struct defines the way the registers are stored */
struct xcptcontext
{
void *sigdeliver; /* Actual type is sig_deliver_t */
xcpt_reg_t regs[XCPTCONTEXT_REGS];
jmp_buf regs;
};
#endif

146
arch/sim/include/setjmp.h Normal file
View File

@ -0,0 +1,146 @@
/****************************************************************************
* arch/sim/include/setjmp.h
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
#ifndef __ARCH_SIM_INCLUDE_SETJUMP_H
#define __ARCH_SIM_INCLUDE_SETJUMP_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <nuttx/compiler.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* Number of registers saved in context switch */
#if defined(CONFIG_HOST_X86_64) && !defined(CONFIG_SIM_M32)
/* Storage order: %rbx, %rsp, %rbp, %r12, %r13, %r14, %r15, %rip */
# define XCPTCONTEXT_REGS 8
# define XCPTCONTEXT_SIZE (8 * XCPTCONTEXT_REGS)
# ifdef __ASSEMBLY__
# define JB_RBX (0*8)
# define JB_RSP (1*8)
# define JB_RBP (2*8)
# define JB_R12 (3*8)
# define JB_R13 (4*8)
# define JB_R14 (5*8)
# define JB_R15 (6*8)
# define JB_RSI (7*8)
# else
# define JB_RBX (0)
# define JB_RSP (1)
# define JB_RBP (2)
# define JB_R12 (3)
# define JB_R13 (4)
# define JB_R14 (5)
# define JB_R15 (6)
# define JB_RSI (7)
# endif /* __ASSEMBLY__ */
/* Compatibility definitions */
# define JB_FP JB_RBP
# define JB_SP JB_RSP
# define JB_PC JB_RSI
#elif defined(CONFIG_HOST_X86) || defined(CONFIG_SIM_M32)
/* Storage order: %ebx, %esi, %edi, %ebp, sp, and return PC */
# define XCPTCONTEXT_REGS 6
# define XCPTCONTEXT_SIZE (4 * XCPTCONTEXT_REGS)
# ifdef __ASSEMBLY__
# define JB_EBX (0*4)
# define JB_ESI (1*4)
# define JB_EDI (2*4)
# define JB_EBP (3*4)
# define JB_SP (4*4)
# define JB_PC (5*4)
# else
# define JB_EBX (0)
# define JB_ESI (1)
# define JB_EDI (2)
# define JB_EBP (3)
# define JB_SP (4)
# define JB_PC (5)
# endif /* __ASSEMBLY__ */
/* Compatibility definitions */
# define JB_FP JB_EBP
#elif defined(CONFIG_HOST_ARM)
# define XCPTCONTEXT_REGS 16
# define XCPTCONTEXT_SIZE (4 * XCPTCONTEXT_REGS)
# define JB_FP 7
# define JB_SP 8
# define JB_PC 9
#endif
/****************************************************************************
* Public Types
****************************************************************************/
#ifndef __ASSEMBLY__
#if defined(CONFIG_HOST_X86_64) && !defined(CONFIG_SIM_M32)
typedef unsigned long xcpt_reg_t;
#else
typedef unsigned int xcpt_reg_t;
#endif
typedef xcpt_reg_t jmp_buf[XCPTCONTEXT_REGS];
#endif
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
#ifndef __ASSEMBLY__
#ifdef __cplusplus
#define EXTERN extern "C"
extern "C"
{
#else
#define EXTERN extern
#endif
int setjmp(jmp_buf env);
void longjmp(jmp_buf env, int val) noreturn_function;
#undef EXTERN
#ifdef __cplusplus
}
#endif
#endif /* !__ASSEMBLY__ */
#endif /* __ARCH_SIM_INCLUDE_SETJUMP_H */

View File

@ -41,17 +41,13 @@ REQUIREDOBJS = $(LINKOBJS)
ifeq ($(CONFIG_HOST_X86_64),y)
ifeq ($(CONFIG_SIM_M32),y)
ASRCS += up_setjmp32.S
ASRCS += up_vfork32.S
else
ASRCS += up_setjmp64.S
ASRCS += up_vfork64.S
endif
else ifeq ($(CONFIG_HOST_X86),y)
ASRCS += up_setjmp32.S
ASRCS += up_vfork32.S
else ifeq ($(CONFIG_HOST_ARM),y)
ASRCS += up_setjmp_arm.S
ASRCS += up_vfork_arm.S
endif

View File

@ -47,6 +47,7 @@ NXSYMBOLS(fsync)
NXSYMBOLS(ftruncate)
NXSYMBOLS(if_nametoindex)
NXSYMBOLS(ioctl)
NXSYMBOLS(longjmp)
NXSYMBOLS(lseek)
NXSYMBOLS(malloc)
NXSYMBOLS(mallinfo)
@ -83,6 +84,7 @@ NXSYMBOLS(rmdir)
NXSYMBOLS(sched_yield)
NXSYMBOLS(select)
NXSYMBOLS(sendmsg)
NXSYMBOLS(setjmp)
NXSYMBOLS(shutdown)
NXSYMBOLS(sigaction)
NXSYMBOLS(sigaddset)

View File

@ -106,11 +106,11 @@ void up_block_task(struct tcb_s *tcb, tstate_t task_state)
}
/* Copy the exception context into the TCB at the (old) head of the
* ready-to-run Task list. if up_setjmp returns a non-zero
* ready-to-run Task list. if setjmp returns a non-zero
* value, then this is really the previously running task restarting!
*/
else if (!up_setjmp(rtcb->xcp.regs))
else if (!setjmp(rtcb->xcp.regs))
{
/* Restore the exception context of the rtcb at the (new) head
* of the ready-to-run task list.
@ -125,7 +125,7 @@ void up_block_task(struct tcb_s *tcb, tstate_t task_state)
/* Then switch contexts */
up_longjmp(rtcb->xcp.regs, 1);
longjmp(rtcb->xcp.regs, 1);
}
else
{

View File

@ -29,8 +29,6 @@
#include <nuttx/arch.h>
#include <nuttx/sched.h>
#include "up_internal.h"
/****************************************************************************
* Public Functions
****************************************************************************/

View File

@ -31,7 +31,6 @@
#include "task/task.h"
#include "sched/sched.h"
#include "up_internal.h"
/****************************************************************************
* Public Functions
@ -79,7 +78,7 @@ void up_exit(int status)
/* Then switch contexts */
up_longjmp(tcb->xcp.regs, 1);
longjmp(tcb->xcp.regs, 1);
/* The function does not return */

View File

@ -49,70 +49,6 @@
# endif
#endif
/* Context Switching Definitions ********************************************/
#if defined(CONFIG_HOST_X86_64) && !defined(CONFIG_SIM_M32)
/* Storage order: %rbx, %rsp, %rbp, %r12, %r13, %r14, %r15, %rip */
# ifdef __ASSEMBLY__
# define JB_RBX (0*8)
# define JB_RSP (1*8)
# define JB_RBP (2*8)
# define JB_R12 (3*8)
# define JB_R13 (4*8)
# define JB_R14 (5*8)
# define JB_R15 (6*8)
# define JB_RSI (7*8)
# else
# define JB_RBX (0)
# define JB_RSP (1)
# define JB_RBP (2)
# define JB_R12 (3)
# define JB_R13 (4)
# define JB_R14 (5)
# define JB_R15 (6)
# define JB_RSI (7)
# endif /* __ASSEMBLY__ */
/* Compatibility definitions */
# define JB_FP JB_RBP
# define JB_SP JB_RSP
# define JB_PC JB_RSI
#elif defined(CONFIG_HOST_X86) || defined(CONFIG_SIM_M32)
/* Storage order: %ebx, $esi, %edi, %ebp, sp, and return PC */
# ifdef __ASSEMBLY__
# define JB_EBX (0*4)
# define JB_ESI (1*4)
# define JB_EDI (2*4)
# define JB_EBP (3*4)
# define JB_SP (4*4)
# define JB_PC (5*4)
# else
# define JB_EBX (0)
# define JB_ESI (1)
# define JB_EDI (2)
# define JB_EBP (3)
# define JB_SP (4)
# define JB_PC (5)
# endif /* __ASSEMBLY__ */
/* Compatibility definitions */
# define JB_FP JB_EBP
#elif defined(CONFIG_HOST_ARM)
# define JB_FP 7
# define JB_SP 8
# define JB_PC 9
#endif
/* Simulated Heap Definitions ***********************************************/
/* Size of the simulated heap */
@ -202,11 +138,6 @@ void *up_doirq(int irq, void *regs);
void host_abort(int status);
/* up_setjmp32.S ************************************************************/
int up_setjmp(void *jb);
void up_longjmp(void *jb, int val);
/* up_hostmemory.c **********************************************************/
void *host_alloc_heap(size_t sz);

View File

@ -63,7 +63,7 @@ void *up_doirq(int irq, void *context)
*/
#ifdef CONFIG_SMP
if (up_setjmp(regs) == 0)
if (setjmp(regs) == 0)
{
#endif
@ -95,7 +95,7 @@ void *up_doirq(int irq, void *context)
/* Then switch contexts */
up_longjmp(regs, 1);
longjmp(regs, 1);
}
#endif

View File

@ -75,11 +75,11 @@ void up_release_pending(void)
}
/* Copy the exception context into the TCB of the task that was
* currently active. if up_setjmp returns a non-zero value, then
* currently active. if setjmp returns a non-zero value, then
* this is really the previously running task restarting!
*/
else if (!up_setjmp(rtcb->xcp.regs))
else if (!setjmp(rtcb->xcp.regs))
{
/* Restore the exception context of the rtcb at the (new) head
* of the ready-to-run task list.
@ -94,7 +94,7 @@ void up_release_pending(void)
/* Then switch contexts */
up_longjmp(rtcb->xcp.regs, 1);
longjmp(rtcb->xcp.regs, 1);
}
else
{

View File

@ -29,8 +29,6 @@
#include <nuttx/arch.h>
#include <nuttx/kmalloc.h>
#include "up_internal.h"
/****************************************************************************
* Public Functions
****************************************************************************/

View File

@ -129,12 +129,12 @@ void up_reprioritize_rtr(struct tcb_s *tcb, uint8_t priority)
}
/* Copy the exception context into the TCB at the (old) head of the
* ready-to-run Task list. if up_setjmp returns a non-zero
* ready-to-run Task list. if setjmp returns a non-zero
* value, then this is really the previously running task
* restarting!
*/
else if (!up_setjmp(rtcb->xcp.regs))
else if (!setjmp(rtcb->xcp.regs))
{
/* Restore the exception context of the rtcb at the (new) head
* of the ready-to-run task list.
@ -149,7 +149,7 @@ void up_reprioritize_rtr(struct tcb_s *tcb, uint8_t priority)
/* Then switch contexts */
up_longjmp(rtcb->xcp.regs, 1);
longjmp(rtcb->xcp.regs, 1);
}
else
{

View File

@ -30,7 +30,6 @@
#include <nuttx/arch.h>
#include "sched/sched.h"
#include "up_internal.h"
/****************************************************************************
* Public Functions

View File

@ -32,8 +32,6 @@
#include <nuttx/arch.h>
#include <arch/irq.h>
#include "up_internal.h"
/****************************************************************************
* Pre-processor Macros
****************************************************************************/

View File

@ -106,11 +106,11 @@ void up_unblock_task(FAR struct tcb_s *tcb)
}
/* Copy the exception context into the TCB of the task that was
* previously active. if up_setjmp returns a non-zero value, then
* previously active. if setjmp returns a non-zero value, then
* this is really the previously running task restarting!
*/
else if (!up_setjmp(rtcb->xcp.regs))
else if (!setjmp(rtcb->xcp.regs))
{
/* Restore the exception context of the new task that is ready to
* run (probably tcb). This is the new rtcb at the head of the
@ -126,7 +126,7 @@ void up_unblock_task(FAR struct tcb_s *tcb)
/* Then switch contexts */
up_longjmp(rtcb->xcp.regs, 1);
longjmp(rtcb->xcp.regs, 1);
}
else
{

View File

@ -35,7 +35,6 @@
#include <nuttx/arch.h>
#include <arch/irq.h>
#include "up_internal.h"
#include "sched/sched.h"
/****************************************************************************

View File

@ -23,7 +23,6 @@
************************************************************************************/
#include <nuttx/config.h>
#include "up_internal.h"
#include <arch/irq.h>
/************************************************************************************
@ -99,7 +98,7 @@
SYMBOL(vfork):
sub $XCPTCONTEXT_SIZE, %esp
push %esp
call SYMBOL(up_setjmp)
call SYMBOL(setjmp)
sub $1, %eax
jz child

View File

@ -23,7 +23,6 @@
************************************************************************************/
#include <nuttx/config.h>
#include "up_internal.h"
#include <arch/irq.h>
/************************************************************************************
@ -103,7 +102,7 @@ SYMBOL(vfork):
#else /* if defined(CONFIG_SIM_X8664_SYSTEMV) */
mov %rsp, %rdi
#endif
call SYMBOL(up_setjmp)
call SYMBOL(setjmp)
sub $1, %eax
jz child

View File

@ -23,7 +23,7 @@
************************************************************************************/
#include <nuttx/config.h>
#include "up_internal.h"
#include <arch/irq.h>
/************************************************************************************
* Pre-processor Definitions
@ -87,7 +87,7 @@
vfork:
sub sp, sp, #XCPTCONTEXT_SIZE
mov r0, sp
bl up_setjmp
bl setjmp
subs r0, #1
jz child

View File

@ -23,7 +23,7 @@ CSRCS += arch_elf.c
endif
ifeq ($(CONFIG_ARCH_SETJMP_H),y)
ASRCS += arch_setjmp.S
ASRCS += arch_setjmp.S
endif
DEPPATH += --dep-path machine/risc-v/common

View File

@ -21,18 +21,34 @@
# XXX ELF relocations are not actually sim-dependent.
# We should share the code with eg. ../x86/arch_elf.c.
ifeq ($(CONFIG_LIBC_ARCH_ELF),y)
ifeq ($(CONFIG_HOST_X86_64),y)
ifeq ($(CONFIG_SIM_M32),y)
ifeq ($(CONFIG_LIBC_ARCH_ELF),y)
CSRCS += arch_elf.c
else
CSRCS += arch_elf64.c
endif
ifeq ($(CONFIG_ARCH_SETJMP_H),y)
ASRCS += arch_setjmp.S
endif
else
ifeq ($(CONFIG_LIBC_ARCH_ELF),y)
CSRCS += arch_elf64.c
endif
ifeq ($(CONFIG_ARCH_SETJMP_H),y)
ASRCS += arch_setjmp64.S
endif
endif
else ifeq ($(CONFIG_HOST_X86),y)
ifeq ($(CONFIG_LIBC_ARCH_ELF),y)
CSRCS += arch_elf.c
endif
ifeq ($(CONFIG_ARCH_SETJMP_H),y)
ASRCS += arch_setjmp.S
endif
else ifeq ($(CONFIG_HOST_ARM),y)
ifeq ($(CONFIG_ARCH_SETJMP_H),y)
ASRCS += arch_setjmp_arm.S
endif
endif
DEPPATH += --dep-path machine/sim
VPATH += :machine/sim
endif

View File

@ -1,5 +1,5 @@
/**************************************************************************
* arch/sim/src/sim/up_setjmp32.S
* libs/libc/machine/sim/arch_setjmp.S
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
@ -22,7 +22,7 @@
* Included Files
**************************************************************************/
#include "up_internal.h"
#include <arch/setjmp.h>
/**************************************************************************
* Pre-processor Definitions
@ -41,11 +41,11 @@
**************************************************************************/
.text
.globl SYMBOL(up_setjmp)
.globl SYMBOL(setjmp)
#ifdef __ELF__
.type SYMBOL(up_setjmp), @function
.type SYMBOL(setjmp), @function
#endif
SYMBOL(up_setjmp):
SYMBOL(setjmp):
/* %ebx, %esi, %edi, and %ebp must be preserved.
* save %ebx, $esi, and %edi now... */
@ -74,13 +74,13 @@ SYMBOL(up_setjmp):
xorl %eax, %eax
ret
#ifdef __ELF__
.size SYMBOL(up_setjmp), . - SYMBOL(up_setjmp)
.size SYMBOL(setjmp), . - SYMBOL(setjmp)
#endif
.globl SYMBOL(up_longjmp)
.globl SYMBOL(longjmp)
#ifdef __ELF__
.type SYMBOL(up_longjmp), @function
.type SYMBOL(longjmp), @function
#endif
SYMBOL(up_longjmp):
SYMBOL(longjmp):
movl 4(%esp), %ecx /* jmpbuf in %ecx. */
movl 8(%esp), %eax /* Second argument is return value. */
@ -100,5 +100,5 @@ SYMBOL(up_longjmp):
jmp *%edx
#ifdef __ELF__
.size SYMBOL(up_longjmp), . - SYMBOL(up_longjmp)
.size SYMBOL(longjmp), . - SYMBOL(longjmp)
#endif

View File

@ -1,5 +1,5 @@
/**************************************************************************
* arch/sim/src/sim/up_setjmp64.S
* libs/libc/machine/sim/arch_setjmp64.S
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
@ -22,7 +22,7 @@
* Included Files
**************************************************************************/
#include "up_internal.h"
#include <arch/setjmp.h>
/**************************************************************************
* Pre-processor Definitions
@ -76,11 +76,11 @@
.text
.align 4
.globl SYMBOL(up_setjmp)
.globl SYMBOL(setjmp)
#ifdef __ELF__
.type SYMBOL(up_setjmp), @function
.type SYMBOL(setjmp), @function
#endif
SYMBOL(up_setjmp):
SYMBOL(setjmp):
/* Get the return address, adjusting the stack pointer */
@ -115,15 +115,15 @@ SYMBOL(up_setjmp):
ret
#ifdef __ELF__
.size SYMBOL(up_setjmp), . - SYMBOL(up_setjmp)
.size SYMBOL(setjmp), . - SYMBOL(setjmp)
#endif
.align 4
.globl SYMBOL(up_longjmp)
.globl SYMBOL(longjmp)
#ifdef __ELF__
.type SYMBOL(up_longjmp), @function
.type SYMBOL(longjmp), @function
#endif
SYMBOL(up_longjmp):
SYMBOL(longjmp):
/* Setup return value */
@ -144,5 +144,5 @@ SYMBOL(up_longjmp):
jmp *JB_RSI(REGS)
#ifdef __ELF__
.size SYMBOL(up_longjmp), . - SYMBOL(up_longjmp)
.size SYMBOL(longjmp), . - SYMBOL(longjmp)
#endif

View File

@ -1,5 +1,5 @@
/****************************************************************************
* arch/sim/src/sim/up_setjmp_arm.h
* libs/libc/machine/sim/arch_setjmp_arm.S
*
* Copyright (C) 2017 Gregory Nutt. All rights reserved.
* Author: Nickolay Semyonov (RPI) <snob@wolpike.com>
@ -37,9 +37,9 @@
****************************************************************************/
.syntax unified
.global up_setjmp
.type up_setjmp,%function
up_setjmp:
.global setjmp
.type setjmp,%function
setjmp:
mov ip,r0
stmia ip!,{v1,v2,v3,v4,v5,v6,sl,fp}
mov r2,sp
@ -75,9 +75,9 @@ up_setjmp:
3: bx lr
.syntax unified
.global up_longjmp
.type up_longjmp,%function
up_longjmp:
.global longjmp
.type longjmp,%function
longjmp:
mov ip,r0
movs r0,r1
moveq r0,#1