From 4d0df2442b9cc64ce477cddb3d51c7a84d1bc3a8 Mon Sep 17 00:00:00 2001 From: Giovanni Di Sirio Date: Sat, 10 Sep 2022 12:13:40 +0000 Subject: [PATCH] Optimizations, stable point, not finished. git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@15779 27425a3e-05d8-49a3-a47f-9c15f0e5edd8 --- os/common/ports/ARMv7-M-ALT/chcore.c | 8 +++-- .../ARMv7-M-ALT/compilers/GCC/chcoreasm.S | 18 +++++----- os/sb/host/compilers/GCC/sbexc.S | 13 +++++++- os/sb/host/sbapi.c | 12 +++++-- os/sb/host/sbvrq.c | 33 +++++++++++++++++-- 5 files changed, 69 insertions(+), 15 deletions(-) diff --git a/os/common/ports/ARMv7-M-ALT/chcore.c b/os/common/ports/ARMv7-M-ALT/chcore.c index e4ed1fe27..02ac56a00 100644 --- a/os/common/ports/ARMv7-M-ALT/chcore.c +++ b/os/common/ports/ARMv7-M-ALT/chcore.c @@ -68,15 +68,19 @@ CC_NO_INLINE uint32_t port_get_s_psp(void) { return (uint32_t)__port_syscall_get_s_psp(__sch_get_currthread()); } -CC_WEAK void __port_do_fastcall_entry(uint32_t n) { +CC_WEAK void __port_do_fastcall_entry(struct port_extctx *ectxp, + uint32_t n) { + (void)ectxp; (void)n; chSysHalt("unimplemented fastcall"); } -CC_WEAK void __port_do_syscall_entry(uint32_t n) { +CC_WEAK void __port_do_syscall_entry(struct port_extctx *ectxp, + uint32_t n) { + (void)ectxp; (void)n; chSysHalt("unimplemented syscall"); diff --git a/os/common/ports/ARMv7-M-ALT/compilers/GCC/chcoreasm.S b/os/common/ports/ARMv7-M-ALT/compilers/GCC/chcoreasm.S index c0171b361..6fa215500 100644 --- a/os/common/ports/ARMv7-M-ALT/compilers/GCC/chcoreasm.S +++ b/os/common/ports/ARMv7-M-ALT/compilers/GCC/chcoreasm.S @@ -218,26 +218,26 @@ .thumb_func .globl SVC_Handler SVC_Handler: - mrs r2, PSP #if PORT_USE_SYSCALL mrs r12, CONTROL tst r12, #1 beq frompriv /* SVC called from non-privileged mode for a syscall.*/ /* Note, LR already contains the return address.*/ - ldr r3, [r2, #24] /* PC position. */ - ldrh r0, [r3, #-2] /* SVC opcode. */ - and r0, #255 - cmp r0, #0x80 + mrs r0, PSP /* Position of the exc.frame.*/ + ldr r3, [r0, #24] /* PC position.*/ + ldrb r1, [r3, #-2] /* SVC parameter.*/ + cmp r1, #0x80 bge __port_do_fastcall_entry bic r12, #1 - msr CONTROL, r12 /* Switching to privileged. */ + msr CONTROL, r12 /* Switching to privileged.*/ b __port_do_syscall_entry frompriv: /* SVC called from privileged mode for unprivileged return.*/ - ldr r3, [r2, #24] /* PC position. */ - ldrh r3, [r3, #-2] /* SVC opcode. */ + mrs r2, PSP /* Position of the exc.frame.*/ + ldr r3, [r2, #24] /* PC position.*/ + ldrh r3, [r3, #-2] /* SVC opcode.*/ ands r3, #255 beq ctxswitch /* Called for non-privileged mode change.*/ @@ -247,6 +247,8 @@ frompriv: ctxswitch: /* SVC called from privilege mode for context switch.*/ +#else + mrs r2, PSP /* Position of the exc.frame.*/ #endif /* Context store for old thread through R1.*/ adds r1, #CONTEXT_OFFSET diff --git a/os/sb/host/compilers/GCC/sbexc.S b/os/sb/host/compilers/GCC/sbexc.S index 4abc55cda..13eb84ae4 100644 --- a/os/sb/host/compilers/GCC/sbexc.S +++ b/os/sb/host/compilers/GCC/sbexc.S @@ -51,12 +51,23 @@ .thumb_func .global __sb_dispatch_syscall __sb_dispatch_syscall: - ldr r3, =sb_syscalls + movw r3, #:lower16:sb_syscalls + movt r3, #:upper16:sb_syscalls ldr.w r3, [r3, r1, lsl #2] blx r3 svc 1 .zombies1: b .zombies1 + .align 2 + .thumb_func + .global __port_do_fastcall_entry +__port_do_fastcall_entry: + movw r3, #:lower16:sb_fastcalls + movt r3, #:upper16:sb_fastcalls + subs r1, #128 + ldr.w r3, [r3, r1, lsl #2] + bx r3 + .align 2 .thumb_func .global HardFault_Handler diff --git a/os/sb/host/sbapi.c b/os/sb/host/sbapi.c index 87211f113..2e0b97ded 100644 --- a/os/sb/host/sbapi.c +++ b/os/sb/host/sbapi.c @@ -1216,12 +1216,15 @@ void __sb_abort(msg_t msg) { chSysHalt("zombies"); } -void __port_do_syscall_entry(uint32_t n) { +void __port_do_syscall_entry(struct port_extctx *ectxpxx, + uint32_t n) { extern void __sb_dispatch_syscall(struct port_extctx *ctxp, uint32_t n); thread_t *tp = __sch_get_currthread(); struct port_extctx *ectxp, *newctxp; uint32_t u_psp; +(void)ectxpxx; + /* Caller context in unprivileged memory.*/ u_psp = __get_PSP(); ectxp = (struct port_extctx *)u_psp; @@ -1260,11 +1263,16 @@ void __port_do_syscall_return(void) { #endif } -void __port_do_fastcall_entry(uint32_t n) { +#if 0 /* Moved in asm module.*/ +void __port_do_fastcall_entry(struct port_extctx *ectxpxx, + uint32_t n) { struct port_extctx *ectxp; + (void)ectxpxx; + ectxp = (struct port_extctx *)__get_PSP(); sb_fastcalls[n - 128](ectxp); } +#endif /** @} */ diff --git a/os/sb/host/sbvrq.c b/os/sb/host/sbvrq.c index 50fc0cfe2..11da74c27 100644 --- a/os/sb/host/sbvrq.c +++ b/os/sb/host/sbvrq.c @@ -99,6 +99,27 @@ static struct port_extctx * vrq_pushctx(sb_class_t *sbp, return ectxp; } +CC_NO_INLINE +static void vrq_pushctx2(struct port_extctx *ectxp, + sb_class_t *sbp, + sb_vrqmask_t active_mask) { + + /* Checking if the new frame is within the sandbox else failure.*/ + if (!sb_is_valid_write_range(sbp, + (void *)(ectxp - 1), + sizeof (struct port_extctx))) { + /* Making the sandbox return on a privileged address, this + will cause a fault and sandbox termination.*/ + ectxp->pc = (uint32_t)vrq_privileged_code; + } + else { + /* Creating a new context for return the VRQ handler.*/ + ectxp--; + vrq_makectx(sbp, ectxp, active_mask); + __set_PSP((uint32_t)ectxp); + } +} + static void delay_cb(virtual_timer_t *vtp, void *arg) { sb_class_t *sbp = (sb_class_t *)arg; @@ -326,10 +347,18 @@ void sb_api_vrq_disable(struct port_extctx *ectxp) { void sb_api_vrq_enable(struct port_extctx *ectxp) { sb_class_t *sbp = (sb_class_t *)chThdGetSelfX()->ctx.syscall.p; + sb_vrqmask_t active_mask; - sbp->vrq_isr = 0U; + active_mask = sbp->vrq_wtmask & sbp->vrq_enmask; + if (active_mask != 0U) { + sbp->vrq_isr = 0U; - __sb_vrq_check_pending(sbp, ectxp); + /* Creating a return context.*/ + vrq_pushctx2(ectxp, sbp, active_mask); + } + else { + sbp->vrq_isr = 0U; + } } void sb_api_vrq_getisr(struct port_extctx *ectxp) {