From 5dd0b3b106577354e098c9cac9f4b6c7877d7e83 Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Thu, 13 Jan 2022 01:19:55 +0530 Subject: [PATCH 01/31] Revert "Merge branch 'development' into cc-kernel-dev" This reverts commit 2e22bb3044d64b776b5c23aabc8eb4ebe158995e, reversing changes made to d68cfc76c1a536a7720b0c7dfdec5f05f47785e2. --- README.md | 7 +- jenkins.pipe | 3 - mk/copy_to_remote.mk | 16 -- mk/elf.mk | 2 - mk/project.mk | 17 ++- src/arch/avr/8/common_5x_6/terravisor/arch.c | 2 - .../8/common_5x_6/terravisor/include/arch.h | 25 ++++ src/arch/riscv/32/a/build.mk | 14 -- src/arch/riscv/32/a/spinlock.c | 35 ----- src/arch/riscv/32/i/terravisor/arch.c | 91 ----------- src/arch/riscv/32/i/terravisor/asm.S | 77 ---------- src/arch/riscv/32/i/terravisor/build.mk | 16 -- src/arch/riscv/32/i/terravisor/entry.S | 72 --------- .../riscv/32/i/terravisor/exception_handler.c | 65 -------- src/arch/riscv/32/i/terravisor/include/arch.h | 94 ------------ .../riscv/32/i/terravisor/include/riscv.h | 28 ---- src/arch/riscv/32/i/terravisor/init.c | 32 ---- src/arch/riscv/32/i/terravisor/spinlock.c | 36 ----- src/arch/riscv/32/imac/terravisor/build.mk | 17 --- src/driver/interrupt/interrupt.c | 3 + .../mega_avr/common/platform/platform.c | 1 + .../sifive/common_fe310/arch/build.mk | 14 -- src/platform/sifive/common_fe310/arch/panic.S | 15 -- .../common_fe310/arch/platform_copy_mem.c | 25 ---- .../sifive/common_fe310/arch/platform_mcall.c | 36 ----- src/platform/sifive/common_fe310/build.mk | 23 --- .../sifive/common_fe310/include/platform.h | 21 --- .../sifive/common_fe310/platform/build.mk | 14 -- .../sifive/common_fe310/platform/platform.c | 49 ------ .../common_fe310/platform/platform_reset.c | 54 ------- .../common_fe310/platform/platform_resource.c | 97 ------------ .../sifive/common_fe310/sections.ld.sx | 141 ------------------ src/platform/sifive/fe310g002/build.mk | 26 ---- src/platform/sifive/fe310g002/config.mk | 62 -------- .../sifive/fe310g002/include/plat_mem.h | 30 ---- src/projects/demo_riscv/build.mk | 21 --- src/projects/demo_riscv/config.mk | 16 -- src/projects/demo_riscv/project.c | 27 ---- .../terravisor/workers/mcall/machine_call.c | 2 +- 39 files changed, 43 insertions(+), 1283 deletions(-) delete mode 100644 mk/copy_to_remote.mk delete mode 100644 src/arch/riscv/32/a/build.mk delete mode 100644 src/arch/riscv/32/a/spinlock.c delete mode 100644 src/arch/riscv/32/i/terravisor/arch.c delete mode 100644 src/arch/riscv/32/i/terravisor/asm.S delete mode 100644 src/arch/riscv/32/i/terravisor/build.mk delete mode 100644 src/arch/riscv/32/i/terravisor/entry.S delete mode 100644 src/arch/riscv/32/i/terravisor/exception_handler.c delete mode 100644 src/arch/riscv/32/i/terravisor/include/arch.h delete mode 100644 src/arch/riscv/32/i/terravisor/include/riscv.h delete mode 100644 src/arch/riscv/32/i/terravisor/init.c delete mode 100644 src/arch/riscv/32/i/terravisor/spinlock.c delete mode 100644 src/arch/riscv/32/imac/terravisor/build.mk delete mode 100644 src/platform/sifive/common_fe310/arch/build.mk delete mode 100644 src/platform/sifive/common_fe310/arch/panic.S delete mode 100644 src/platform/sifive/common_fe310/arch/platform_copy_mem.c delete mode 100644 src/platform/sifive/common_fe310/arch/platform_mcall.c delete mode 100644 src/platform/sifive/common_fe310/build.mk delete mode 100644 src/platform/sifive/common_fe310/include/platform.h delete mode 100644 src/platform/sifive/common_fe310/platform/build.mk delete mode 100644 src/platform/sifive/common_fe310/platform/platform.c delete mode 100644 src/platform/sifive/common_fe310/platform/platform_reset.c delete mode 100644 src/platform/sifive/common_fe310/platform/platform_resource.c delete mode 100644 src/platform/sifive/common_fe310/sections.ld.sx delete mode 100644 src/platform/sifive/fe310g002/build.mk delete mode 100644 src/platform/sifive/fe310g002/config.mk delete mode 100644 src/platform/sifive/fe310g002/include/plat_mem.h delete mode 100644 src/projects/demo_riscv/build.mk delete mode 100644 src/projects/demo_riscv/config.mk delete mode 100644 src/projects/demo_riscv/project.c diff --git a/README.md b/README.md index eb507e72..454e3381 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ Click the above icon to connect | Atmel | SiFive | STMicro | TI | Raspberry Pi | | ---------- | ----------- | ----------- | ----------- | -------------| -| ATMega328P | FE310-G002 | Coming soon | Coming soon | Coming soon | +| ATMega328P | Coming soon | Coming soon | Coming soon | Coming soon | | ATMega2560 | | | | | ### Projects @@ -38,7 +38,8 @@ $ make list ### Programming Languages * asm/assembly -* C/C++ +* C +* Cpp/C++ * makefile * linker script @@ -63,4 +64,4 @@ Host system requirements: Toolchain Requirements: * Please push your toolchains to your git repo * Update the links in mk/tc_get.mk file -* run ```$ make help``` for more details +* run ```make help``` for more details diff --git a/jenkins.pipe b/jenkins.pipe index 1346583b..1970d0bf 100644 --- a/jenkins.pipe +++ b/jenkins.pipe @@ -5,21 +5,18 @@ pipeline { stage('tc check') { steps { sh 'make get_avr_tc AVR_TC_REPO=git@github.com:VisorFolks/avr-toolchain.git' - sh 'make get_riscv_tc RISC_V_TC_REPO=git@github.com:VisorFolks/risc-v-toolchain.git' } } stage('check') { steps { sh 'make demo_avr check' sh 'make demo_avr_cpp check' - sh 'make demo_riscv check' } } stage('build') { steps { sh 'make demo_avr clean default' sh 'make demo_avr_cpp clean default' - sh 'make demo_riscv clean default' } } stage('clean') { diff --git a/mk/copy_to_remote.mk b/mk/copy_to_remote.mk deleted file mode 100644 index 24165ca3..00000000 --- a/mk/copy_to_remote.mk +++ /dev/null @@ -1,16 +0,0 @@ -# -# CYANCORE LICENSE -# Copyrights (C) 2019, Cyancore Team -# -# File Name : copy_to_target.mk -# Description : This file has build target to copy to -# remote machine if any -# Primary Author : Akash Kollipara [akashkollipara@gmail.com] -# Organisation : Cyancore Core-Team -# - ---cpremote: $(ELF) - @echo "< ! > Please add your commands here..." - ---rmremote: - @echo "< ! > Please add your commands here..." diff --git a/mk/elf.mk b/mk/elf.mk index b3606491..0df08068 100644 --- a/mk/elf.mk +++ b/mk/elf.mk @@ -33,9 +33,7 @@ $(ELF): $(DEP_LIBS) $(DEP_OBJS) $(LD_SCRIPT) $(LD_SUPPLEMENT) @cd $(@D); $(SIZE) $(@F) @echo "" -.SECONDEXPANSION: $(OUT)/%.ld: %.ld.sx - mkdir -p $(@D) @echo "Elf: Preprocessing $(@F) ..." $(CC) $(CFLAGS) -E -P -o $@ $< diff --git a/mk/project.mk b/mk/project.mk index ac599d9b..7df54aae 100644 --- a/mk/project.mk +++ b/mk/project.mk @@ -45,12 +45,11 @@ list: check: --lint -copy_to_remote: --cpremote -clean_remote: --rmremote - - ifeq ($(findstring $(MAKECMDGOALS),$(T_ALLOWLIST)),) -ifeq ($(findstring $(firstword $(MAKECMDGOALS)),$(P_TARGETS)),) +ifneq ($(firstword $(MAKECMDGOALS)),clean) +ifneq ($(firstword $(MAKECMDGOALS)),default) +ifneq ($(firstword $(MAKECMDGOALS)),cyancore) +ifneq ($(firstword $(MAKECMDGOALS)),check) PROJECT ?= $(firstword $(MAKECMDGOALS)) CMD := $(word 2,$(MAKECMDGOALS)) ifeq ($(CMD),) @@ -60,6 +59,9 @@ endif .PHONY: $(PROJECT) $(PROJECT): $(CMD) +endif +endif +endif endif ifeq ($(PROJECT),) @@ -71,15 +73,14 @@ $(info < x > Invalid project name...) $(info < ! > Run `make list` to get list of projects) $(error < x > Build Failed!) endif +endif ifeq ($(findstring $(MAKECMDGOALS),$(T_ALLOWLIST)),) include $(SRC)/sources.mk include mk/tc.mk -include mk/copy_to_remote.mk -endif endif -ifeq ($(findstring $(CMD),$(T_ALLOWLIST) $(P_TARGETS)),) +ifeq ($(findstring $(CMD),$(T_ALLOWLIST) default clean cyancore),) $(CMD): $(filter %/$(CMD),$(DEP_LIBS) $(DEP_OBJS)) if [ "$<" = "" ]; then \ echo "No such target: $@"; \ diff --git a/src/arch/avr/8/common_5x_6/terravisor/arch.c b/src/arch/avr/8/common_5x_6/terravisor/arch.c index 5a653506..e763d2a4 100644 --- a/src/arch/avr/8/common_5x_6/terravisor/arch.c +++ b/src/arch/avr/8/common_5x_6/terravisor/arch.c @@ -102,8 +102,6 @@ void (*mcall)(unsigned int, unsigned int, unsigned int, unsigned int, mret_t *); */ void arch_machine_call(unsigned int code, unsigned int a0, unsigned int a1, unsigned int a2, mret_t *ret) { - if(ret == NULL) - return; mcall(code, a0, a1, a2, ret); return; } diff --git a/src/arch/avr/8/common_5x_6/terravisor/include/arch.h b/src/arch/avr/8/common_5x_6/terravisor/include/arch.h index 17384a27..57b89c22 100644 --- a/src/arch/avr/8/common_5x_6/terravisor/include/arch.h +++ b/src/arch/avr/8/common_5x_6/terravisor/include/arch.h @@ -89,3 +89,28 @@ static inline void arch_nop() { asm volatile("nop"); } + +static inline void arch_sync() +{ + arch_nop(); +} + +static inline void arch_msb() +{ + arch_nop(); +} + +static inline void arch_isb() +{ + arch_nop(); +} + +static inline void arch_dsb() +{ + arch_nop(); +} + +static inline void arch_wfe() +{ + arch_nop(); +} diff --git a/src/arch/riscv/32/a/build.mk b/src/arch/riscv/32/a/build.mk deleted file mode 100644 index 2311cf00..00000000 --- a/src/arch/riscv/32/a/build.mk +++ /dev/null @@ -1,14 +0,0 @@ -# -# CYANCORE LICENSE -# Copyrights (C) 2019, Cyancore Team -# -# File Name : build.mk -# Description : Build script for this directory. -# Primary Authod : Akash Kollipara [akashkollipara@gmail.com] -# Organisation : Cyancore Core-Team -# - -RV32IA_ARCH_DIR := $(GET_PATH) - -DIR := $(RV32IA_ARCH_DIR) -include mk/obj.mk diff --git a/src/arch/riscv/32/a/spinlock.c b/src/arch/riscv/32/a/spinlock.c deleted file mode 100644 index 7ecfa808..00000000 --- a/src/arch/riscv/32/a/spinlock.c +++ /dev/null @@ -1,35 +0,0 @@ -/* - * CYANCORE LICENSE - * Copyrights (C) 2019, Cyancore Team - * - * File Name : spinlock.c - * Description : This file consists of sources of spinlock - * Primary Author : Akash Kollipara [akashkollipara@gmail.com] - * Organisation : Cyancore Core-Team - */ - -#include -#include -#include -#include - -void spinlock_acquire(spinlock_t *key) -{ - unsigned int old, new; - new = 1; - while(1) - { - if(*key) - continue; - asm volatile("amoswap.w %0, %2, %1" : "=r" (old), "+A" (*key) : "r" (new) : "memory"); - fence(r, rw); - if(!old) - break; - } -} - -void spinlock_release(spinlock_t *key) -{ - fence(r, rw); - *key = 0; -} diff --git a/src/arch/riscv/32/i/terravisor/arch.c b/src/arch/riscv/32/i/terravisor/arch.c deleted file mode 100644 index 3bab6546..00000000 --- a/src/arch/riscv/32/i/terravisor/arch.c +++ /dev/null @@ -1,91 +0,0 @@ -/* - * CYANCORE LICENSE - * Copyrights (C) 2019, Cyancore Team - * - * File Name : arch.c - * Description : This file consists of architecture specific function that - * cannot be inlined. Hence, present in c file. - * Primary Author : Akash Kollipara [akashkollipara@gmail.com] - * Organisation : Cyancore Core-Team - */ - -#include -#include -#include -#include - -/** - * arch_early_setup - This function is called in the early stages of boot - * - * @brief This function is responsible to clean reset cpu status/control registers. - * - */ -void arch_early_setup() -{ - arch_di(); - riscv_update_vector(); - return; -} - -/** - * arch_setup - This function is called after initial setup is done - * - * @brief This function is called after initial setup is done. - */ -void arch_setup() -{ - return; -} - -void arch_di_save_state() -{ -} - -void arch_ei_restore_state() -{ -} - -/** - * arch_machine_call - perform machine call - * - * @brief This function performs environment call - * in m mode - * - * @param[in] code: machine call code - * @param[in] a0: first argument - * @param[in] a1: second argument - * @param[in] a2: third argument - * @param[in] *ret: return struct - */ -void arch_machine_call(unsigned int code, unsigned int a0, unsigned int a1, unsigned int a2, mret_t *ret) -{ - if(ret == NULL) - return; - asm volatile("mv a0, %0" : : "r" (code)); - asm volatile("mv a1, %0" : : "r" (a0)); - asm volatile("mv a2, %0" : : "r" (a1)); - asm volatile("mv a3, %0" : : "r" (a2)); - asm volatile("ecall"); - asm volatile("mv %0, a0" : "=r" (ret->p)); - asm volatile("mv %0, a1" : "=r" (ret->size)); - asm volatile("mv %0, a2" : "=r" (ret->status)); - return; -} - -_WEAK void arch_panic_handler() -{ - context_frame_t *frame = get_context_frame(); - printf("< x > Arch Panic!\n"); - printf("Info:\nCause\t: 0x%x\t Address\t: 0x%x\n", frame->mcause, frame->mepc); - while(1) - arch_wfi(); -} - -_WEAK void arch_unhandled_irq() -{ - context_frame_t *frame = get_context_frame(); - printf("< x > Arch Unhandled IRQ!\n"); - printf("Info:\nIRQ ID\t: 0x%x\n", frame->mcause & ~(1U << 31)); - while(1) - arch_wfi(); -} diff --git a/src/arch/riscv/32/i/terravisor/asm.S b/src/arch/riscv/32/i/terravisor/asm.S deleted file mode 100644 index 4684e6f9..00000000 --- a/src/arch/riscv/32/i/terravisor/asm.S +++ /dev/null @@ -1,77 +0,0 @@ -/* - * CYANCORE LICENSE - * Copyrights (C) 2019, Cyancore Team - * - * File Name : asm.S - * Description : This file consists of all the function written in asm - * like ISR, context management, zero_reg and panic handler - * Primary Author : Akash Kollipara [akashkollipara@gmail.com] - * Organisation : Cyancore Core-Team - */ - -#include - -.macro PROLOGUE - addi sp, sp, -96 /* - size(context_frame) * 4 */ - sw t6, 68(sp) - sw t5, 64(sp) - sw t4, 60(sp) - sw t3, 56(sp) - sw a7, 52(sp) - sw a6, 48(sp) - sw a5, 44(sp) - sw a4, 40(sp) - sw a3, 36(sp) - sw a2, 32(sp) - sw a1, 28(sp) - sw a0, 24(sp) - sw t2, 20(sp) - sw t1, 16(sp) - sw t0, 12(sp) - sw ra, 8(sp) - csrr t0, mstatus - sw t0, 4(sp) - csrr t0, mepc - sw t0, 0(sp) -.endm - -.macro EPILOGUE - lw t0, 0(sp) - csrw mepc, t0 - lw t0, 4(sp) - csrw mstatus, t0 - lw ra, 8(sp) - lw t0, 12(sp) - lw t1, 16(sp) - lw t2, 20(sp) - lw a0, 24(sp) - lw a1, 28(sp) - lw a2, 32(sp) - lw a3, 36(sp) - lw a4, 40(sp) - lw a5, 44(sp) - lw a6, 48(sp) - lw a7, 52(sp) - lw t3, 56(sp) - lw t4, 60(sp) - lw t5, 64(sp) - lw t6, 68(sp) - addi sp, sp, 96 /* size(context_frame) * 4 */ -.endm - -function exception_handler_base -.p2align 2 /* Needs 4Byte Alignment, refer mtvec reg */ - PROLOGUE - csrr a0, mcause - sw a0, 72(sp) - mv a1, sp - call exception_handler - EPILOGUE - mret - -function riscv_update_vector - la a0, exception_handler_base - csrw mtvec, a0 - mv a0, zero - fence.i - ret diff --git a/src/arch/riscv/32/i/terravisor/build.mk b/src/arch/riscv/32/i/terravisor/build.mk deleted file mode 100644 index 0a8e902f..00000000 --- a/src/arch/riscv/32/i/terravisor/build.mk +++ /dev/null @@ -1,16 +0,0 @@ -# -# CYANCORE LICENSE -# Copyrights (C) 2019, Cyancore Team -# -# File Name : build.mk -# Description : Build script for this directory. -# Primary Authod : Akash Kollipara [akashkollipara@gmail.com] -# Organisation : Cyancore Core-Team -# - -RV32I_T_ARCH_DIR := $(GET_PATH) - -$(eval $(call add_include,$(RV32I_T_ARCH_DIR)/include/)) - -DIR := $(RV32I_T_ARCH_DIR) -include mk/obj.mk diff --git a/src/arch/riscv/32/i/terravisor/entry.S b/src/arch/riscv/32/i/terravisor/entry.S deleted file mode 100644 index b76cb04c..00000000 --- a/src/arch/riscv/32/i/terravisor/entry.S +++ /dev/null @@ -1,72 +0,0 @@ -/* - * CYANCORE LICENSE - * Copyrights (C) 2019, Cyancore Team - * - * File Name : entry.S - * Description : This file consists of entry point of the framework. - * After the reset of system, program in this file gets - * executed. - * Primary Author : Akash Kollipara [akashkollipara@gmail.com] - * Organisation : Cyancore Core-Team - */ - -#include -#include - -/** - * stack_start - Variable pointing to stack start assigned while linking - * - * @brief This variable is substituted by the linker with actual value of - * stack start. Please refer linker script for this variable assignment. - */ - -/** - * global_start - Variable pointing to global start assigned while linking - * - * @brief This variable is substituted by the linker with actual value of - * global start. Please refer linker script for this variable assignment. - * RISC-V CPU has global var symbol access range of gp +/- 2KB, total of 4KB. - * gp = + 0x800 - */ - -/** - * tls_start - Variable pointing to thread data start assigned while linking - * - * @brief This variable is substituted by the linker with actual value of - * tls start. Please refer linker script for this variable assignment. - */ - -/** - * Entry - Entry point of the framework - * - * @brief This is the entry point for the framework. After the reset interrupt - * is triggered, PC should be updated with the address of this function. - * This function initialises stack pointer and later it jumps to 'init', - * from here the framework boots. - */ -function entry -.option norvc -.option push -.option norelax - la gp, _global_start -.option pop - -stack_setup: - la a0, _stack_start - csrr a1, mhartid - mv t0, zero -1: /* Start of for loop, assume mul is not supported */ - beq t0, a1, 1f - li t1, STACK_SIZE_PCPU - add a0, a0, t1 - addi t0, t0, 1 - j 1b -1: - mv sp, a0 - mv a0, zero - mv a1, zero - mv t0, zero - mv t1, zero - -jump_to_init: - j init diff --git a/src/arch/riscv/32/i/terravisor/exception_handler.c b/src/arch/riscv/32/i/terravisor/exception_handler.c deleted file mode 100644 index f9c14c63..00000000 --- a/src/arch/riscv/32/i/terravisor/exception_handler.c +++ /dev/null @@ -1,65 +0,0 @@ -/* - * CYANCORE LICENSE - * Copyrights (C) 2019, Cyancore Team - * - * File Name : exception_handler.c - * Description : This file consists of sources for exception - * handling. - * Primary Author : Akash Kollipara [akashkollipara@gmail.com] - * Organisation : Cyancore Core-Team - */ - -#include -#include -#include -#include -#include - -static void (* exhandler[N_CORES][N_EXCEP])(void) = {{[0 ... N_EXCEP-1] = arch_panic_handler}}; -static void (* irqhandler[N_CORES][N_IRQ])(void) = {{[0 ... N_IRQ-1] = arch_unhandled_irq}}; -static context_frame_t *local_frame[N_CORES]; - -static void set_context_frame(context_frame_t *frame) -{ - unsigned int cpuid = arch_core_index(); - local_frame[cpuid] = frame; - fence(w, w); -} - -context_frame_t *get_context_frame(void) -{ - unsigned int cpuid = arch_core_index(); - return local_frame[cpuid]; -} - -void arch_register_interrupt_handler(unsigned int id, void (*handler)(void)) -{ - unsigned int cpuid = arch_core_index(); - assert(id < N_EXCEP); - exhandler[cpuid][id] = handler; - fence(ow, ow); -} - -void local_register_interrupt_handler(unsigned int id, void (*handler)(void)) -{ - unsigned int cpuid = arch_core_index(); - assert(id < N_IRQ); - irqhandler[cpuid][id] = handler; - fence(ow, ow); -} - -void exception_handler(uint32_t mcause, context_frame_t *frame) -{ - unsigned int cause = mcause & ~(1U << 31); - unsigned int cpuid = arch_core_index(); - - set_context_frame(frame); - - if(mcause & (1U << 31)) - irqhandler[cpuid][cause](); - else - exhandler[cpuid][cause](); - - frame->mepc += (MMIO8(frame->mepc) & 0x3) ? 4 : 2; - fence(ow, ow); -} diff --git a/src/arch/riscv/32/i/terravisor/include/arch.h b/src/arch/riscv/32/i/terravisor/include/arch.h deleted file mode 100644 index 75234a64..00000000 --- a/src/arch/riscv/32/i/terravisor/include/arch.h +++ /dev/null @@ -1,94 +0,0 @@ -/* - * CYANCORE LICENSE - * Copyrights (C) 2019, Cyancore Team - * - * File Name : arch.h - * Description : This file prototypes arch related functions and - * defines inline-able arch functions. - * Primary Author : Akash Kollipara [akashkollipara@gmail.com] - * Organisation : Cyancore Core-Team - */ - -#pragma once -#define _ARCH_H_ -#include -#include - -/** - * arch_early_setup - This needs to be called in early stages of boot - */ -void arch_early_setup(); - -/** - * arch_setup - This needs to be called after inital setup is completed - */ -void arch_setup(); - -void arch_di_save_state(); -void arch_ei_restore_state(); - -/** - * arch_panic_handler - Executes when arch error occurs - */ -void arch_panic_handler(); -void arch_unhandled_irq(); - -/** - * arch_machine_call - Performs machine call - * - * Refer arch.c for more details. - */ -void arch_machine_call(unsigned int, unsigned int, unsigned int, unsigned int, mret_t *); - -/** - * arch_register_interrupt_handler - Registers interrtup handler - * for arch specific exception vectors - */ -void arch_register_interrupt_handler(unsigned int, void (*)(void)); - -/** - * local_register_interrupt_handler - Registers interrupt handler - * for cpu specific interrupt vectors - */ -void local_register_interrupt_handler(unsigned int, void (*)(void)); - -/** - * arch_core_index - Returns code index - */ -static inline unsigned int arch_core_index() -{ - unsigned int id; - asm volatile("csrr %0, mhartid" : "=r" (id)); - return id; -} - -/** - * arch_ei - arch enable global interrupts - */ -static inline void arch_ei() -{ - unsigned int bits = (1 << 3) | (1 << 7); - asm volatile("csrs mstatus, %0" : : "r" (bits)); -} - -/** - * arch_di - arch disable global interrupts - */ -static inline void arch_di() -{ - unsigned int bits = (1 << 3) | (1 << 7); - asm volatile("csrc mstatus, %0" : : "r" (bits)); -} - -static inline void arch_nop() -{ - asm volatile("nop"); -} - -static inline void arch_wfi() -{ - asm volatile("wfi"); -#if ERRATA_CIP578 - arch_nop(); -#endif -} diff --git a/src/arch/riscv/32/i/terravisor/include/riscv.h b/src/arch/riscv/32/i/terravisor/include/riscv.h deleted file mode 100644 index 20bf61bc..00000000 --- a/src/arch/riscv/32/i/terravisor/include/riscv.h +++ /dev/null @@ -1,28 +0,0 @@ -/* - * CYANCORE LICENSE - * Copyrights (C) 2019, Cyancore Team - * - * File Name : riscv.h - * Description : This file prototypes riscv arch related functions and - * structures. - * Primary Author : Akash Kollipara [akashkollipara@gmail.com] - * Organisation : Cyancore Core-Team - */ - -#pragma once -#define _RISC_V_ -#include - -#define fence(predecessor, successor) asm volatile("fence " #predecessor ", " #successor ::: "memory") - -typedef struct context_frame -{ - uint32_t mepc, mstatus, ra, t0, - t1, t2, a0, a1, a2, a3, - a4, a5, a6, a7, t3, t4, - t5, t6, mcause, pad[5]; - /* Padding is necessary for alignment */ -} context_frame_t; - -void riscv_update_vector(); -context_frame_t *get_context_frame(); diff --git a/src/arch/riscv/32/i/terravisor/init.c b/src/arch/riscv/32/i/terravisor/init.c deleted file mode 100644 index 86bcd954..00000000 --- a/src/arch/riscv/32/i/terravisor/init.c +++ /dev/null @@ -1,32 +0,0 @@ -/* - * CYANCORE LICENSE - * Copyrights (C) 2019, Cyancore Team - * - * File Name : init.c - * Description : This file consists of init routine of the framework. - * Primary Author : Akash Kollipara [akashkollipara@gmail.com] - * Organisation : Cyancore Core-Team - */ - -#include -#include -#include -#include -#include - -/** - * init - Entry point for the framework - * - * @brief This function runs to reset the cpu reg bank to architectrually - * reset state and jump to 'engine'. - */ -void init() -{ - arch_di(); - asm volatile("la tp, _tls_start"); - /* Boot framework */ - engine(); - - /* Accidental trap, if control returns from framework */ - exit(EXIT_FAILURE); -} diff --git a/src/arch/riscv/32/i/terravisor/spinlock.c b/src/arch/riscv/32/i/terravisor/spinlock.c deleted file mode 100644 index 099b95f2..00000000 --- a/src/arch/riscv/32/i/terravisor/spinlock.c +++ /dev/null @@ -1,36 +0,0 @@ -/* - * CYANCORE LICENSE - * Copyrights (C) 2019, Cyancore Team - * - * File Name : spinlock.c - * Description : This file consists of sources of spinlock - * Primary Author : Akash Kollipara [akashkollipara@gmail.com] - * Organisation : Cyancore Core-Team - */ - -#include -#include -#include -#include - -_WEAK void spinlock_acquire(spinlock_t *key) -{ - while(1) - { - arch_di(); - if(!*key) - { - *key = 1; - fence(r, rw); - break; - } - arch_ei(); - } - arch_ei(); -} - -_WEAK void spinlock_release(spinlock_t *key) -{ - fence(rw, r); - *key = 0; -} diff --git a/src/arch/riscv/32/imac/terravisor/build.mk b/src/arch/riscv/32/imac/terravisor/build.mk deleted file mode 100644 index b69123ca..00000000 --- a/src/arch/riscv/32/imac/terravisor/build.mk +++ /dev/null @@ -1,17 +0,0 @@ -# -# CYANCORE LICENSE -# Copyrights (C) 2019, Cyancore Team -# -# File Name : build.mk -# Description : Build script for this directory. -# Primary Authod : Akash Kollipara [akashkollipara@gmail.com] -# Organisation : Cyancore Core-Team -# - -RV32IMAC_T_ARCH_DIR := $(GET_PATH) - -include $(RV32IMAC_T_ARCH_DIR)/../../i/terravisor/build.mk -include $(RV32IMAC_T_ARCH_DIR)/../../a/build.mk - -DIR := $(RV32IMAC_T_ARCH_DIR) -include mk/obj.mk diff --git a/src/driver/interrupt/interrupt.c b/src/driver/interrupt/interrupt.c index 80d7bc14..f799e8bf 100644 --- a/src/driver/interrupt/interrupt.c +++ b/src/driver/interrupt/interrupt.c @@ -13,6 +13,9 @@ #include #include #include +#if LOCAL_INTERRUPT_DEVICE==1 +#include +#endif #if PLAT_INTERRUPT_DEVICE==1 #include #endif diff --git a/src/platform/mega_avr/common/platform/platform.c b/src/platform/mega_avr/common/platform/platform.c index 42ed265e..7c3c047b 100644 --- a/src/platform/mega_avr/common/platform/platform.c +++ b/src/platform/mega_avr/common/platform/platform.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include diff --git a/src/platform/sifive/common_fe310/arch/build.mk b/src/platform/sifive/common_fe310/arch/build.mk deleted file mode 100644 index ef63bb16..00000000 --- a/src/platform/sifive/common_fe310/arch/build.mk +++ /dev/null @@ -1,14 +0,0 @@ -# -# CYANCORE LICENSE -# Copyrights (C) 2019, Cyancore Team -# -# File Name : build.mk -# Description : This file accumulates the build fe310 series -# platform arch sources -# Primary Author : Akash Kollipara [akashkollipara@gmail.com] -# Organisation : Cyancore Core-Team -# - -DIR := $(GET_PATH) - -include mk/obj.mk diff --git a/src/platform/sifive/common_fe310/arch/panic.S b/src/platform/sifive/common_fe310/arch/panic.S deleted file mode 100644 index 4ede533b..00000000 --- a/src/platform/sifive/common_fe310/arch/panic.S +++ /dev/null @@ -1,15 +0,0 @@ -/* - * CYANCORE LICENSE - * Copyrights (C) 2019, Cyancore Team - * - * File Name : panic.S - * Description : This file contains platform panic handler - * Primary Author : Akash Kollipara [akashkollipara@gmail.com] - * Organisation : Cyancore Core-Team - */ - -#include - -function plat_panic_handler - wfi - j plat_panic_handler diff --git a/src/platform/sifive/common_fe310/arch/platform_copy_mem.c b/src/platform/sifive/common_fe310/arch/platform_copy_mem.c deleted file mode 100644 index 269bcc0e..00000000 --- a/src/platform/sifive/common_fe310/arch/platform_copy_mem.c +++ /dev/null @@ -1,25 +0,0 @@ -#include -#include -#include -#include - -extern uintptr_t _bss_start, _bss_size, _data_start, _data_size, - _data_vstart, _itim_start, _itim_size, _itim_vstart; - -status_t platform_bss_clear() -{ - memset(&_bss_start, 0, (size_t)&_bss_size); - return success; -} - -status_t platform_copy_itim() -{ - memcpy(&_itim_vstart, &_itim_start, (size_t)&_itim_size); - return success; -} - -status_t platform_copy_data() -{ - memcpy(&_data_vstart, &_data_start, (size_t)&_data_size); - return success; -} diff --git a/src/platform/sifive/common_fe310/arch/platform_mcall.c b/src/platform/sifive/common_fe310/arch/platform_mcall.c deleted file mode 100644 index 1bfa2325..00000000 --- a/src/platform/sifive/common_fe310/arch/platform_mcall.c +++ /dev/null @@ -1,36 +0,0 @@ -/* - * CYANCORE LICENSE - * Copyrights (C) 2019, Cyancore Team - * - * File Name : platform_mcall.c - * Description : This file contains platform mcall registration - * api - * Primary Author : Akash Kollipara [akashkollipara@gmail.com] - * Organisation : Cyancore Core-Team - */ - -#include -#include -#include -#include -#include -#include -#include -#include - -static void platform_mcall_handler() -{ - context_frame_t *frame = get_context_frame(); - mret_t mres; - machine_call(frame->a0, frame->a1, frame->a2, frame->a3, &mres); - fence(w, w); - frame->a0 = mres.p; - frame->a1 = mres.size; - frame->a2 = mres.status; - return; -} - -status_t platform_mcall_update() -{ - return link_interrupt(arch, 11, &platform_mcall_handler); -} diff --git a/src/platform/sifive/common_fe310/build.mk b/src/platform/sifive/common_fe310/build.mk deleted file mode 100644 index 423a0649..00000000 --- a/src/platform/sifive/common_fe310/build.mk +++ /dev/null @@ -1,23 +0,0 @@ -# -# CYANCORE LICENSE -# Copyrights (C) 2019, Cyancore Team -# -# File Name : build.mk -# Description : This file accumulates sources from FE310 -# common directory -# Primary Author : Akash Kollipara [akashkollipara@gmail.com] -# Organisation : Cyancore Core-Team -# - -FE310_COMMON_DIR := $(GET_PATH) - -LD_SCRIPT := $(FE310_COMMON_DIR)/sections.ld.sx - -PLAT_INCLUDE += $(FE310_COMMON_DIR)/include - -LOCAL_INTERRUPT_DEVICE := 1 -PLAT_INTERRUPT_DEVICE := 0 - -include $(FE310_COMMON_DIR)/arch/build.mk -#include $(FE310_COMMON_DIR)/hal/build.mk -include $(FE310_COMMON_DIR)/platform/build.mk diff --git a/src/platform/sifive/common_fe310/include/platform.h b/src/platform/sifive/common_fe310/include/platform.h deleted file mode 100644 index fc60c7ad..00000000 --- a/src/platform/sifive/common_fe310/include/platform.h +++ /dev/null @@ -1,21 +0,0 @@ -/* - * CYANCORE LICENSE - * Copyrights (C) 2019, Cyancore Team - * - * File Name : platform.h - * Description : This file contains prototypes of platform apis - * Primary Author : Akash Kollipara [akashkollipara@gmail.com] - * Organisation : Cyancore Core-Team - */ - -#pragma once -#define _SIFIVE_PLATFORM_H_ - -#include - -void plat_panic_handler(); -status_t platform_copy_itim(); -status_t platform_copy_data(); -status_t platform_bss_clear(); -status_t platform_resources_setup(); -status_t platform_mcall_update(); diff --git a/src/platform/sifive/common_fe310/platform/build.mk b/src/platform/sifive/common_fe310/platform/build.mk deleted file mode 100644 index 6fd659de..00000000 --- a/src/platform/sifive/common_fe310/platform/build.mk +++ /dev/null @@ -1,14 +0,0 @@ -# -# CYANCORE LICENSE -# Copyrights (C) 2019, Cyancore Team -# -# File Name : build.mk -# Description : This file builds sources from fe310 -# common platform directory -# Primary Author : Akash Kollipara [akashkollipara@gmail.com] -# Organisation : Cyancore Core-Team -# - -DIR := $(GET_PATH) - -include mk/obj.mk diff --git a/src/platform/sifive/common_fe310/platform/platform.c b/src/platform/sifive/common_fe310/platform/platform.c deleted file mode 100644 index f73d217d..00000000 --- a/src/platform/sifive/common_fe310/platform/platform.c +++ /dev/null @@ -1,49 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include - -unsigned int reset_syndrome; - -void platform_early_setup() -{ - status_t ret = success; - - ret |= platform_copy_data(); - ret |= platform_copy_itim(); - ret |= platform_bss_clear(); - - reset_syndrome = 1; - - if(ret != success) - exit(EXIT_FAILURE); - return; -} - -void platform_setup() -{ - status_t ret = success; - -// ret |= platform_resources_setup(); - - if(ret != success) - exit(EXIT_FAILURE); - return; -} - -void platform_cpu_setup() -{ - status_t ret = success; - - ret |= platform_mcall_update(); - - arch_ei(); - - if(ret != success) - exit(EXIT_FAILURE); - return; -} diff --git a/src/platform/sifive/common_fe310/platform/platform_reset.c b/src/platform/sifive/common_fe310/platform/platform_reset.c deleted file mode 100644 index eb9bf824..00000000 --- a/src/platform/sifive/common_fe310/platform/platform_reset.c +++ /dev/null @@ -1,54 +0,0 @@ -/* - * CYANCORE LICENSE - * Copyrights (C) 2019, Cyancore Team - * - * File Name : platform_reset.c - * Description : This file contains sources for platform - * reset apis - * Primary Author : Akash Kollipara [akashkollipara@gmail.com] - * Organisation : Cyancore Core-Team - */ - -#include -#include -#include -#include -#include - -extern unsigned int reset_syndrome; - -/** - * platform_get_reset_syndrome - returns the cause of reset - * - * @brief This function returns the information related to - * the reset sources. - * - * @return reset_cause: This function returs the reset cause - */ -reset_t platform_get_reset_syndrome() -{ - if(reset_syndrome & 1) /* Power on Reset */ - return power_on_reset; - else - return inval_reset; -} - - -/** - * platform_reset_handler - handles the reset conditions - * - * @brief This function is responsible to handle the reset - * sources like watchdog, brownout, external reset, etc. - * - * @param[in] rsyn: Reset syndrome - * - * @return void - */ -void platform_reset_handler(reset_t rsyn) -{ - if(rsyn == power_on_reset) - return; - else - plat_panic_handler(); -} - diff --git a/src/platform/sifive/common_fe310/platform/platform_resource.c b/src/platform/sifive/common_fe310/platform/platform_resource.c deleted file mode 100644 index 7e73a032..00000000 --- a/src/platform/sifive/common_fe310/platform/platform_resource.c +++ /dev/null @@ -1,97 +0,0 @@ -/* - * CYANCORE LICENSE - * Copyrights (C) 2019, Cyancore Team - * - * File Name : platform_resource.c - * Description : This file contains sources for platform - * resource apis - * Primary Author : Akash Kollipara [akashkollipara@gmail.com] - * Organisation : Cyancore Core-Team - */ - -#include -#include -#include -#include -#include -#include - -/** - * platform_resources_setup - Updates platform DP and SP - * - * @brief This function is responsible to update platform DP - * and make it accessible to the system. This function needs - * to be called during platform setup. - * - * @return status: return the function execution status - */ -status_t platform_resources_setup() -{ - status_t ret = success; -// extern dp_t device_prop; -// extern sp_t software_prop; -// ret = dp_init(&device_prop); -// ret |= sp_init(&software_prop); - return ret; -} - -/** - * platform_fetch_sp - mcall handler for fetch_sp - * - * @brief This function is a machine call hander for fetch_sp - * access code. It is responsible to respond with hardware ID - * to corresponding software properties. - * - * @param[in] a0: arg0 - * @param[in] a1: arg1 - * @param[in] a2: arg2 - * - * @return status: return the function execution status - */ -mret_t platform_fetch_sp(unsigned int a0, unsigned int a1 _UNUSED, unsigned int a2 _UNUSED) -{ - mret_t ret; - ret.p = (uintptr_t) sp_terravisor_dev_info(a0); - ret.size = (ret.p) ? sizeof(hw_devid_t) : 0; - ret.status = (ret.p) ? success : error_device_id_inval; - return ret; -} - -INCLUDE_MCALL(sifive_fe310_fetch_sp, fetch_sp, platform_fetch_sp); - -/** - * platform_fetch_dp - mcall handler for fetch_dp - * - * @brief This function is a machine call hander for fetch_dp - * access code. It is responsible to respond with pointer to - * corresponding device properties. - * - * @param[in] a0: arg0 - * @param[in] a1: arg1 - * @param[in] a2: arg2 - * - * @return status: return the function execution status - */ -mret_t platform_fetch_dp(unsigned int a0, unsigned int a1 _UNUSED, unsigned int a2 _UNUSED) -{ - mret_t ret; - switch(a0) - { - case clock: - ret.p = (uintptr_t)dp_get_base_clock(); - ret.size = (ret.p) ? sizeof(unsigned long) : 0; - break; - case gpio: - ret.p = (uintptr_t)dp_get_port_info(a0 | a1); - ret.size = (ret.p) ? sizeof(gpio_module_t) : 0; - break; - default: - ret.p = (uintptr_t) dp_get_module_info(a0 | a1); - ret.size = (ret.p) ? sizeof(module_t) : 0; - break; - } - ret.status = (ret.p) ? success : error_device_id_inval; - return ret; -} - -INCLUDE_MCALL(sifive_fe310_fetch_dp, fetch_dp, platform_fetch_dp); diff --git a/src/platform/sifive/common_fe310/sections.ld.sx b/src/platform/sifive/common_fe310/sections.ld.sx deleted file mode 100644 index beaa49af..00000000 --- a/src/platform/sifive/common_fe310/sections.ld.sx +++ /dev/null @@ -1,141 +0,0 @@ -/* - * CYANCORE LICENSE - * Copyrights (C) 2019, Cyancore Team - * - * File Name : sections.ld.sx - * Description : This file contains memory layout for the - * cyancore framework - * Primary Author : Akash Kollipara [akashkollipara@gmail.com] - * Organisation : Cyancore Core-Team - */ - -#include - -#ifndef L_MEM_START -#define L_MEM_START 0 -#endif - -MEMORY -{ - /* VM Addresses */ - vma_itmem (airwx) : ORIGIN = V_ITMEM_START, LENGTH = ITMEM_LENGTH - vma_imem (irx!aw) : ORIGIN = V_IMEM_START, LENGTH = IMEM_LENGTH - vma_dmem (arw!xi) : ORIGIN = V_DMEM_START, LENGTH = DMEM_LENGTH - - /* LM Addresses */ - lma_mem : ORIGIN = L_MEM_START, LENGTH = L_MEM_LENGTH -} - -ENTRY(entry) - -SECTIONS -{ - .text : - { - KEEP(*(.text.entry)) - *(.text) - *(.text.*) - KEEP(*(.text)) - . = ALIGN(4); - } > vma_imem AT > lma_mem - - .rodata : - { - *(.version) - KEEP(*(.version)) - KEEP(*(.rdata)) - KEEP(*(.rodata)) - KEEP(*(.rodata.*)) - *(.srodata) - *(.srodata.*) - KEEP(*(.srodata)) - } > vma_imem AT > lma_mem - - .itim : - { - . = ALIGN(4); - KEEP(*(.itim)) - KEEP(*(.itim.*)) - } > vma_itmem AT > lma_mem - - .bss : - { - . = ALIGN(1); - *(.bss) - *(.bss.*) - KEEP(*(.bss)) - *(COMMON) - } > vma_dmem - - .static_bss_global : - { - . = ALIGN(1); - *(.sbss) - *(.sbss.*) - KEEP(*(.sbss)) - } > vma_dmem - - .static_global_var : - { - } > vma_dmem - - .static_data_global : - { - . = ALIGN(1); - *(.sdata) - *(.sdata.*) - KEEP(*(.sdata)) - } > vma_dmem AT > lma_mem - - .data : - { - . = ALIGN(1); - *(.data) - *(.data.*) - KEEP(*(.data)) - } > vma_dmem AT > lma_mem - - .tdata : - { - . = ALIGN(1); - *(.tdata) - *(.tdata.*) - KEEP(*(.tdata)) - } > vma_dmem AT > lma_mem - - .driver_table : ALIGN(4) {} > vma_dmem AT > lma_mem - .mcall_table : ALIGN(4) {} > vma_dmem AT > lma_mem - - .stack : - { - *(.stack) - KEEP(*(.stack)) - } > vma_dmem - - PROVIDE(_data_start = LOADADDR(.static_data_global)); - PROVIDE(_data_size = SIZEOF(.data) + SIZEOF(.tdata) + SIZEOF(.static_data_global) +\ - SIZEOF(.driver_table) + SIZEOF(.mcall_table)); - PROVIDE(_data_vstart = ADDR(.static_data_global)); - PROVIDE(_data_vend = _data_vstart + _data_size); - - PROVIDE(_tls_start = ADDR(.tdata)); - PROVIDE(_global_start = ADDR(.static_global_var) + 0x800); - PROVIDE(_stack_start = ADDR(.stack) + STACK_SIZE); - - PROVIDE(_bss_start = ADDR(.bss)); - PROVIDE(_bss_size = SIZEOF(.bss) + SIZEOF(.static_bss_global)); - - PROVIDE(_itim_start = LOADADDR(.itim)); - PROVIDE(_itim_size = SIZEOF(.itim)); - PROVIDE(_itim_vstart = ADDR(.itim)); - PROVIDE(_itim_vsize = _itim_vstart + _itim_size); - - PROVIDE(_flash_size = _data_size + SIZEOF(.text) + _itim_size + SIZEOF(.rodata)); - PROVIDE(_ram_size = _bss_size + _data_size + SIZEOF(.stack)); - - - ASSERT((_flash_size < FLASH_SIZE), "< x > Flash size exceeded ...") - ASSERT((_ram_size < RAM_SIZE), "< x > RAM size exceeded ...") - - /DISCARD/ : { *(.comment .trampolines) } -} diff --git a/src/platform/sifive/fe310g002/build.mk b/src/platform/sifive/fe310g002/build.mk deleted file mode 100644 index 53313dcb..00000000 --- a/src/platform/sifive/fe310g002/build.mk +++ /dev/null @@ -1,26 +0,0 @@ -# -# CYANCORE LICENSE -# Copyrights (C) 2019, Cyancore Team -# -# File Name : build.mk -# Description : This file accumulates the build scripts from -# all other directories that have HiFive 1 Rev B -# board support sources -# Primary Author : Akash Kollipara [akashkollipara@gmail.com] -# Organisation : Cyancore Core-Team -# - -FE310G002_DIR := $(GET_PATH) - -ARCH := riscv -BIT := 32 -ARCH_VARIANT := imac -TARGET_FLAGS += -march=rv32imac -mabi=ilp32 -PLAT_INCLUDE += $(FE310G002_DIR)/include -OUTPUT_FORMAT := elf32-littleriscv - -include $(FE310G002_DIR)/config.mk -#include $(FE310G002_DIR)/arch/build.mk -#include $(FE310G002_DIR)/platform/build.mk -#include $(FE310G002_DIR)/resources/build.mk -include $(FE310G002_DIR)/../common_fe310/build.mk diff --git a/src/platform/sifive/fe310g002/config.mk b/src/platform/sifive/fe310g002/config.mk deleted file mode 100644 index fe4da77d..00000000 --- a/src/platform/sifive/fe310g002/config.mk +++ /dev/null @@ -1,62 +0,0 @@ -# -# CYANCORE LICENSE -# Copyrights (C) 2019, Cyancore Team -# -# File Name : config.mk -# Description : This file defines configuration for HiFive 1B -# Primary Author : Akash Kollipara [akashkollipara@gmail.com] -# Organisation : Cyancore Core-Team -# - -#====================================================================== -# Configuration file for Platforms -#====================================================================== - -#====================================================================== -# Platform Configuration -# Do not alter below FLAGS unless explicitly mentioned -#====================================================================== -N_CORES := 1 -$(eval $(call add_define,N_CORES)) - -$(eval $(call add_define,BIT)) - -BOOT_CORE_ID:= 0 -$(eval $(call add_define,BOOT_CORE_ID)) - -# Call this FLAG from Project config file if needed -FCLK ?= 16000000 -$(eval $(call add_define,FCLK)) - -N_EXCEP := 12 -$(eval $(call add_define,N_EXCEP)) - -N_IRQ := 12 -$(eval $(call add_define,N_IRQ)) - -MAX_INTERRUPTS_PER_DEVICE := 2 -$(eval $(call add_define,MAX_INTERRUPTS_PER_DEVICE)) - -USE_SPINLOCK ?= 1 -$(eval $(call add_define,USE_SPINLOCK)) - -#====================================================================== -# MEMBUF Configuration -#====================================================================== -MEMBUF_SIZE ?= 1024 -$(eval $(call add_define,MEMBUF_SIZE)) -#====================================================================== - -#====================================================================== -# GPIO Configuration -#====================================================================== -N_PORT := 3 -$(eval $(call add_define,N_PORT)) -#====================================================================== - -#====================================================================== -# Errata CIP-578 -#====================================================================== -ERRATA_CIP578 := 1 -$(eval $(call add_define,ERRATA_CIP578)) -#====================================================================== diff --git a/src/platform/sifive/fe310g002/include/plat_mem.h b/src/platform/sifive/fe310g002/include/plat_mem.h deleted file mode 100644 index 2403e73c..00000000 --- a/src/platform/sifive/fe310g002/include/plat_mem.h +++ /dev/null @@ -1,30 +0,0 @@ -/* - * CYANCORE LICENSE - * Copyrights (C) 2019, Cyancore Team - * - * File Name : plat_mem.h - * Description : This file contains memory config of fe310-g002 - * Primary Author : Akash Kollipara [akashkollipara@gmail.com] - * Organisation : Cyancore Core-Team - */ - -#pragma once - -#define FLASH_SIZE (4M-64K) -#define RAM_SIZE 16K - -#define V_ITMEM_START 0x08000000 -#define V_IMEM_START 0x20010000 -#define V_DMEM_START 0x80000000 - -#define L_MEM_START 0x20010000 -#define L_MEM_LENGTH 0x003f0000 - -#define ITMEM_LENGTH 0x00002000 -#define IMEM_LENGTH 0x20000000 -#define DMEM_LENGTH 0x00004000 - -#define ALIGN_BOUND 4 - -#define STACK_SIZE 0xc00 -#define STACK_SIZE_PCPU 0xc00 diff --git a/src/projects/demo_riscv/build.mk b/src/projects/demo_riscv/build.mk deleted file mode 100644 index 7e0fe7b4..00000000 --- a/src/projects/demo_riscv/build.mk +++ /dev/null @@ -1,21 +0,0 @@ -# -# CYANCORE LICENSE -# Copyrights (C) 2019, Cyancore Team -# -# File Name : build.mk -# Description : This file build project sources and specifies -# project properties -# Primary Author : Akash Kollipara [akashkollipara@gmail.com] -# Organisation : Cyancore Core-Team -# - -PROJECT_DIR := $(GET_PATH) - -OPTIMIZATION := s - -EXE_MODE := terravisor - -include $(PROJECT_DIR)/config.mk - -DIR := $(PROJECT_DIR) -include mk/obj.mk diff --git a/src/projects/demo_riscv/config.mk b/src/projects/demo_riscv/config.mk deleted file mode 100644 index 88a5f8b7..00000000 --- a/src/projects/demo_riscv/config.mk +++ /dev/null @@ -1,16 +0,0 @@ -# -# CYANCORE LICENSE -# Copyrights (C) 2019, Cyancore Team -# -# File Name : config.mk -# Description : This file consists of project config -# Primary Author : Akash Kollipara [akashkollipara@gmail.com] -# Organisation : Cyancore Core-Team -# - -COMPILER := gcc -TC_VER := 8.2.0 -FAMILY := sifive -PLATFORM := fe310g002 -EARLYCON_MEMBUF := 1 -CONSOLE_MEMBUF := 1 diff --git a/src/projects/demo_riscv/project.c b/src/projects/demo_riscv/project.c deleted file mode 100644 index 48fcdf7e..00000000 --- a/src/projects/demo_riscv/project.c +++ /dev/null @@ -1,27 +0,0 @@ -/* - * CYANCORE LICENSE - * Copyrights (C) 2019, Cyancore Team - * - * File Name : project.c - * Description : This file consists of project srouces - * Primary Author : Akash Kollipara [akashkollipara@gmail.com] - * Organisation : Cyancore Core-Team - */ - -#include -#include -#include -#include - -void plug() -{ - mret_t mres; - bootstrap(); - driver_setup_all(); - arch_machine_call(0, 100, 200, 300, &mres); -} - -void play() -{ - arch_wfi(); -} diff --git a/src/visor/terravisor/workers/mcall/machine_call.c b/src/visor/terravisor/workers/mcall/machine_call.c index 46e7f6bf..1b54ed14 100644 --- a/src/visor/terravisor/workers/mcall/machine_call.c +++ b/src/visor/terravisor/workers/mcall/machine_call.c @@ -35,7 +35,7 @@ void machine_call(mcall_id_t id, unsigned int a0, unsigned int a1, unsigned int ptr = &_mcall_table_start; /* Run through the table till the end */ - while(ptr < & _mcall_table_end) + while(ptr <= & _mcall_table_end) { /* Check if the mcall ID matches */ if(ptr->id == id) From 0e4ca3ed49fed840a1729c89e53dfe0275f59b01 Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Fri, 14 Jan 2022 00:21:09 +0530 Subject: [PATCH 02/31] Revert "Merge branch 'development' into cc-kernel-dev" This reverts commit 41fb4034ec70dad2d4ec9078fc864f594ca3cbf9, reversing changes made to 71f092e37e1757fb44fac2225d533a923e74119c. --- .github/workflows/github_ci.yml | 74 --------------------------------- 1 file changed, 74 deletions(-) delete mode 100644 .github/workflows/github_ci.yml diff --git a/.github/workflows/github_ci.yml b/.github/workflows/github_ci.yml deleted file mode 100644 index bf84d636..00000000 --- a/.github/workflows/github_ci.yml +++ /dev/null @@ -1,74 +0,0 @@ -name: GitHub CI - -on: - push: - branches: - - stable - - development - pull_request: - branches: - - stable - - development - -jobs: - build: - - runs-on: ubuntu-latest - env: - SONAR_SCANNER_VERSION: 4.4.0.2170 - SONAR_SERVER_URL: "https://sonarcloud.io" - BUILD_WRAPPER_OUT_DIR: bw-output # Directory where build-wrapper output will be placed - steps: - - uses: actions/checkout@v2 - with: - fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis - - - name: Install Dependencies - run: sudo apt install cppcheck -y - - - name: Configure Git - env: - TOKEN: ${{ secrets.CI_PAT }} - run: git config --global url."https://${TOKEN}:x-oauth-basic@github.com/".insteadOf "https://github.com/" - - - name: Get Toolchains - run: | - make get_avr_tc AVR_TC_REPO=https://github.com/VisorFolks/avr-toolchain.git - make get_riscv_tc RISC_V_TC_REPO=https://github.com/VisorFolks/risc-v-toolchain.git - - name: Set up JDK 11 - uses: actions/setup-java@v1 - with: - java-version: 11 - - name: Download and set up sonar-scanner - env: - SONAR_SCANNER_DOWNLOAD_URL: https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-${{ env.SONAR_SCANNER_VERSION }}-linux.zip - run: | - mkdir -p $HOME/.sonar - curl -sSLo $HOME/.sonar/sonar-scanner.zip ${{ env.SONAR_SCANNER_DOWNLOAD_URL }} - unzip -o $HOME/.sonar/sonar-scanner.zip -d $HOME/.sonar/ - echo "$HOME/.sonar/sonar-scanner-${{ env.SONAR_SCANNER_VERSION }}-linux/bin" >> $GITHUB_PATH - - - name: Download and set up build-wrapper - env: - BUILD_WRAPPER_DOWNLOAD_URL: ${{ env.SONAR_SERVER_URL }}/static/cpp/build-wrapper-linux-x86.zip - run: | - curl -sSLo $HOME/.sonar/build-wrapper-linux-x86.zip ${{ env.BUILD_WRAPPER_DOWNLOAD_URL }} - unzip -o $HOME/.sonar/build-wrapper-linux-x86.zip -d $HOME/.sonar/ - echo "$HOME/.sonar/build-wrapper-linux-x86" >> $GITHUB_PATH - - - name: Run build-wrapper - run: | - build-wrapper-linux-x86-64 --out-dir ${{ env.BUILD_WRAPPER_OUT_DIR }} make all_projects - - - name: Run sonar-scanner - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} - run: | - sonar-scanner \ - --define sonar.host.url="${{ env.SONAR_SERVER_URL }}" \ - --define sonar.cfamily.build-wrapper-output="${{ env.BUILD_WRAPPER_OUT_DIR }}" \ - --define sonar.organization=cyancore \ - --define sonar.projectKey=VisorFolks_cyancore \ - --define sonar.sources=src \ - --define sonar.verbose=true From f3f0e1a3f4d48b9901345c2e370383205a18d975 Mon Sep 17 00:00:00 2001 From: Pranjal Chanda Date: Tue, 4 Jan 2022 23:39:46 +0530 Subject: [PATCH 03/31] Add POSIX prototype as defined by libc. --- src/lib/posix/cc_posix_internal.h | 100 +++++++ src/lib/posix/cc_posix_types.h | 58 ++++ src/lib/posix/include/errno.h | 68 +++++ src/lib/posix/include/fcntl.h | 54 ++++ src/lib/posix/include/mqueue.h | 225 ++++++++++++++ src/lib/posix/include/pthread.h | 476 ++++++++++++++++++++++++++++++ src/lib/posix/include/sched.h | 59 ++++ src/lib/posix/include/semaphore.h | 118 ++++++++ src/lib/posix/include/signal.h | 45 +++ src/lib/posix/include/sys/types.h | 166 +++++++++++ src/lib/posix/include/time.h | 233 +++++++++++++++ src/lib/posix/include/unistd.h | 36 +++ src/lib/posix/include/utils.h | 130 ++++++++ src/lib/posix/src/posix_time.c | 12 + 14 files changed, 1780 insertions(+) create mode 100644 src/lib/posix/cc_posix_internal.h create mode 100644 src/lib/posix/cc_posix_types.h create mode 100644 src/lib/posix/include/errno.h create mode 100644 src/lib/posix/include/fcntl.h create mode 100644 src/lib/posix/include/mqueue.h create mode 100644 src/lib/posix/include/pthread.h create mode 100644 src/lib/posix/include/sched.h create mode 100644 src/lib/posix/include/semaphore.h create mode 100644 src/lib/posix/include/signal.h create mode 100644 src/lib/posix/include/sys/types.h create mode 100644 src/lib/posix/include/time.h create mode 100644 src/lib/posix/include/unistd.h create mode 100644 src/lib/posix/include/utils.h create mode 100644 src/lib/posix/src/posix_time.c diff --git a/src/lib/posix/cc_posix_internal.h b/src/lib/posix/cc_posix_internal.h new file mode 100644 index 00000000..071e86d8 --- /dev/null +++ b/src/lib/posix/cc_posix_internal.h @@ -0,0 +1,100 @@ +#ifndef _CC_POSIX_INTERNAL_H_ +#define _CC_POSIX_INTERNAL_H_ +/** + * @file cc_posix_internal.h + * @brief Internal structs and initializers for CC+POSIX. + */ + +/** + * @brief Mutex attribute object. + */ +#if posixconfigENABLE_PTHREAD_MUTEXATTR_T == 1 + typedef struct pthread_mutexattr_internal + { + int iType; /**< Mutex type. */ + } pthread_mutexattr_internal_t; +#endif + +#if posixconfigENABLE_PTHREAD_MUTEX_T == 1 + +/** + * @brief Mutex. + */ + typedef struct pthread_mutex_internal + { + BaseType_t xIsInitialized; /**< Set to pdTRUE if this mutex is initialized, pdFALSE otherwise. */ + StaticSemaphore_t xMutex; /**< Kernel mutex. */ + TaskHandle_t xTaskOwner; /**< Owner; used for deadlock detection and permission checks. */ + pthread_mutexattr_internal_t xAttr; /**< Mutex attributes. */ + } pthread_mutex_internal_t; + +/** + * @brief Compile-time initializer of pthread_mutex_internal_t. + */ + #define FREERTOS_POSIX_MUTEX_INITIALIZER \ + ( ( ( pthread_mutex_internal_t ) \ + { \ + .xIsInitialized = pdFALSE, \ + .xMutex = { { 0 } }, \ + .xTaskOwner = NULL, \ + .xAttr = { .iType = 0 } \ + } \ + ) \ + ) +#endif /* if posixconfigENABLE_PTHREAD_MUTEX_T == 1 */ + +#if posixconfigENABLE_PTHREAD_COND_T == 1 + +/** + * @brief Condition variable. + */ + typedef struct pthread_cond_internal + { + BaseType_t xIsInitialized; /**< Set to pdTRUE if this condition variable is initialized, pdFALSE otherwise. */ + StaticSemaphore_t xCondWaitSemaphore; /**< Threads block on this semaphore in pthread_cond_wait. */ + unsigned iWaitingThreads; /**< The number of threads currently waiting on this condition variable. */ + } pthread_cond_internal_t; + +/** + * @brief Compile-time initializer of pthread_cond_internal_t. + */ + + #define FREERTOS_POSIX_COND_INITIALIZER \ + ( ( ( pthread_cond_internal_t ) \ + { \ + .xIsInitialized = pdFALSE, \ + .xCondWaitSemaphore = { { 0 } }, \ + .iWaitingThreads = 0 \ + } \ + ) \ + ) + +#endif /* if posixconfigENABLE_PTHREAD_COND_T == 1 */ + +#if posixconfigENABLE_SEM_T == 1 + +/** + * @brief Semaphore type. + */ + typedef struct + { + StaticSemaphore_t xSemaphore; /**< Kernel semaphore. */ + int value; /**< POSIX semaphore count. */ + } sem_internal_t; +#endif /* if posixconfigENABLE_SEM_T == 1 */ + +#if posixconfigENABLE_PTHREAD_BARRIER_T == 1 + +/** + * @brief Barrier object. + */ + typedef struct pthread_barrier_internal + { + unsigned uThreadCount; /**< Current number of threads that have entered barrier. */ + unsigned uThreshold; /**< The count argument of pthread_barrier_init. */ + StaticSemaphore_t xThreadCountSemaphore; /**< Prevents more than uThreshold threads from exiting pthread_barrier_wait at once. */ + StaticEventGroup_t xBarrierEventGroup; /**< Kernel event group that blocks to wait on threads entering barrier. */ + } pthread_barrier_internal_t; +#endif /* if posixconfigENABLE_PTHREAD_BARRIER_T == 1 */ + +#endif /* _CC_POSIX_INTERNAL_H_ */ diff --git a/src/lib/posix/cc_posix_types.h b/src/lib/posix/cc_posix_types.h new file mode 100644 index 00000000..636fcece --- /dev/null +++ b/src/lib/posix/cc_posix_types.h @@ -0,0 +1,58 @@ + + +#ifndef _CC_POSIX_INTERNAL_TYPES_H_ +#define _CC_POSIX_INTERNAL_TYPES_H_ + +#include + +/* + * sys/types.h defines a POSIX type when posixconfigENABLE_PTHREAD__T + * is not defined AND when posixconfigENABLE_PTHREAD__T is set to 1. + * cc_posix_internal.h defines internal type ONLY when + * posixconfigENABLE_PTHREAD__T is set to 1. + * #else part below is to have a type defined, so the code compiles, when + * posixconfigENABLE_PTHREAD__T is not defined. + */ +#if posixconfigENABLE_PTHREAD_MUTEX_T == 1 + typedef pthread_mutex_internal_t PthreadMutexType_t; +#else + typedef void * PthreadMutexType_t; +#endif + +#if posixconfigENABLE_PTHREAD_COND_T == 1 + typedef pthread_cond_internal_t PthreadCondType_t; +#else + typedef void * PthreadCondType_t; +#endif + +#if posixconfigENABLE_SEM_T == 1 + typedef sem_internal_t PosixSemType_t; +#else + typedef void * PosixSemType_t; +#endif + +#if posixconfigENABLE_PTHREAD_MUTEXATTR_T == 1 + typedef struct pthread_mutexattr + { + uint32_t ulpthreadMutexAttrStorage; + } PthreadMutexAttrType_t; +#else + typedef void * PthreadMutexAttrType_t; +#endif + +#if posixconfigENABLE_PTHREAD_ATTR_T == 1 + typedef struct pthread_attr + { + uint32_t ulpthreadAttrStorage; + } PthreadAttrType_t; +#else + typedef void * PthreadAttrType_t; +#endif + +#if posixconfigENABLE_PTHREAD_BARRIER_T == 1 + typedef pthread_barrier_internal_t PthreadBarrierType_t; +#else + typedef void * PthreadBarrierType_t; +#endif + +#endif /* _CC_POSIX_INTERNAL_TYPES_H_ */ diff --git a/src/lib/posix/include/errno.h b/src/lib/posix/include/errno.h new file mode 100644 index 00000000..5806db52 --- /dev/null +++ b/src/lib/posix/include/errno.h @@ -0,0 +1,68 @@ +/** + * @file errno.h + * @brief System error numbers. + * + * http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/errno.h.html + * + * The values defined in this file may not be compatible with the strerror + * function provided by this system. + */ + +#ifndef _CC_POSIX_ERRNO_H_ +#define _CC_POSIX_ERRNO_H_ + +/* Undefine all errnos to avoid redefinition errors with system errnos. */ +#undef EPERM +#undef ENOENT +#undef EBADF +#undef EAGAIN +#undef ENOMEM +#undef EEXIST +#undef EBUSY +#undef EINVAL +#undef ENOSPC +#undef ERANGE +#undef ENAMETOOLONG +#undef EDEADLK +#undef EOVERFLOW +#undef ENOSYS +#undef EMSGSIZE +#undef ENOTSUP +#undef ETIMEDOUT + +/** + * @name Definition of POSIX errnos. + */ +/**@{ */ +#define EPERM 1 /**< Operation not permitted. */ +#define ENOENT 2 /**< No such file or directory. */ +#define EBADF 9 /**< Bad file descriptor. */ +#define EAGAIN 11 /**< Resource unavailable, try again. */ +#define ENOMEM 12 /**< Not enough space. */ +#define EEXIST 17 /**< File exists. */ +#define EBUSY 16 /**< Device or resource busy. */ +#define EINVAL 22 /**< Invalid argument. */ +#define ENOSPC 28 /**< No space left on device. */ +#define ERANGE 34 /**< Result too large. */ +#define ENAMETOOLONG 36 /**< File name too long. */ +#define EDEADLK 45 /**< Resource deadlock would occur. */ +#define EOVERFLOW 75 /**< Value too large to be stored in data type. */ +#define ENOSYS 88 /**< Function not supported. */ +#define EMSGSIZE 90 /**< Message too long. */ +#define ENOTSUP 95 /**< Operation not supported. */ +#define ETIMEDOUT 116 /**< Connection timed out. */ +/**@} */ + +/** + * @name System Variable + * + * @brief Define CC+POSIX errno, if enabled. + * Set configUSE_POSIX_ERRNO to enable, and clear to disable. + * + * @{ + */ +#if ( configUSE_POSIX_ERRNO == 1 ) +#endif +/**@} */ + +#endif /* ifndef _CC_POSIX_ERRNO_H_ */ diff --git a/src/lib/posix/include/fcntl.h b/src/lib/posix/include/fcntl.h new file mode 100644 index 00000000..1f712f48 --- /dev/null +++ b/src/lib/posix/include/fcntl.h @@ -0,0 +1,54 @@ +/** + * @file fcntl.h + * @brief File control options. + * + * http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/fcntl.h.html + */ + +#ifndef _CC_POSIX_FCNTL_H_ +#define _CC_POSIX_FCNTL_H_ + +/** + * @name File creation flags for use in the oflag value to open() and openat(). + */ +/**@{ */ +#define O_CLOEXEC 0x0001 /**< Close the file descriptor upon exec(). */ +#define O_CREAT 0x0002 /**< Create file if it does not exist. */ +#define O_DIRECTORY 0x0004 /**< Fail if file is a non-directory file. */ +#define O_EXCL 0x0008 /**< Exclusive use flag. */ +#define O_NOCTTY 0x0010 /**< Do not assign controlling terminal. */ +#define O_NOFOLLOW 0x0020 /**< Do not follow symbolic links. */ +#define O_TRUNC 0x0040 /**< Truncate flag. */ +#define O_TTY_INIT 0x0080 /**< termios structure provides conforming behavior. */ +/**@} */ + +/** + * @name File status flags for open(), openat(), and fcntl(). + */ +/**@{ */ +#define O_APPEND 0x0100 /**< Set append mode. */ +#define O_DSYNC 0x0200 /**< Write according to synchronized I/O data integrity completion. */ +#define O_NONBLOCK 0x0400 /**< Non-blocking mode. */ +#define O_RSYNC 0x0800 /**< Synchronized read I/O operations. */ +#define O_SYNC 0x0200 /**< Write according to synchronized I/O file integrity completion. */ +/**@} */ + +/** + * @name Mask for file access modes. + */ +/**@{ */ +#define O_ACCMODE 0xF000 +/**@} */ + +/** + * @name File access modes for open(), openat(), and fcntl(). + */ +/**@{ */ +#define O_EXEC 0x1000 /**< Open for execute only (non-directory files). */ +#define O_RDONLY 0x2000 /**< Open for reading only. */ +#define O_RDWR 0xA000 /**< Open for reading and writing. */ +#define O_SEARCH 0x4000 /**< Open directory for search only. */ +#define O_WRONLY 0x8000 /**< Open for writing only. */ +/**@} */ + +#endif /* ifndef _CC_POSIX_FCNTL_H_ */ diff --git a/src/lib/posix/include/mqueue.h b/src/lib/posix/include/mqueue.h new file mode 100644 index 00000000..3bad2463 --- /dev/null +++ b/src/lib/posix/include/mqueue.h @@ -0,0 +1,225 @@ +/** + * @file mqueue.h + * @brief Message queues. + * + * http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/mqueue.h.html + */ + +#ifndef _CC_POSIX_MQUEUE_H_ +#define _CC_POSIX_MQUEUE_H_ + +/* CC+POSIX includes. */ +#include "lib/posix/include/time.h" + +/** + * @brief Message queue descriptor. + */ +typedef void * mqd_t; + +/** + * @brief Message queue attributes. + */ +struct mq_attr +{ + long mq_flags; /**< Message queue flags. */ + long mq_maxmsg; /**< Maximum number of messages. */ + long mq_msgsize; /**< Maximum message size. */ + long mq_curmsgs; /**< Number of messages currently queued. */ +}; + +/** + * @brief Close a message queue. + * + * http://pubs.opengroup.org/onlinepubs/9699919799/functions/mq_close.html + * + * @retval 0 - Upon successful completion + * @retval -1 - A error occurred. errno is also set. + * + * @sideeffect Possible errno values + *
+ * EBADF - The mqdes argument is not a valid message queue descriptor. + */ +int mq_close( mqd_t mqdes ); + +/** + * @brief Get message queue attributes. + * + * http://pubs.opengroup.org/onlinepubs/9699919799/functions/mq_getattr.html + * + * @retval 0 - Upon successful completion + * @retval -1 - A error occurred. errno is also set. + * + * @sideeffect Possible errno values + *
+ * DBADF - The mqdes argument is not a valid message queue descriptor. + */ +int mq_getattr( mqd_t mqdes, + struct mq_attr * mqstat ); + +/** + * @brief Open a message queue. + * + * http://pubs.opengroup.org/onlinepubs/9699919799/functions/mq_open.html + * + * @note Supported name pattern: leading <slash> character in name is always required; + * the maximum length (excluding null-terminator) of the name argument can be NAME_MAX. + * The default value of NAME_MAX in FreeRTOS_POSIX_portable_default.h is 64, which can be + * overwritten by user. + * @note mode argument is not supported. + * @note Supported oflags: O_RDWR, O_CREAT, O_EXCL, and O_NONBLOCK. + * + * @retval Message queue descriptor -- Upon successful completion + * @retval (mqd_t) - 1 -- An error occurred. errno is also set. + * + * @sideeffect Possible errno values + *
+ * EINVAL - name argument is invalid (not following name pattern), + * OR if O_CREAT is specified in oflag with attr argument not NULL and either mq_maxmsg or mq_msgsize is equal to or less than zero, + * OR either O_CREAT or O_EXCL is not set and a queue with the same name is unlinked but pending to be removed. + *
+ * EEXIST - O_CREAT and O_EXCL are set and the named message queue already exists. + *
+ * ENOSPC - There is insufficient space for the creation of the new message queue. + *
+ * ENOENT - O_CREAT is not set and the named message queue does not exist. + */ +mqd_t mq_open( const char * name, + int oflag, + mode_t mode, + struct mq_attr * attr ); + +/** + * @brief Receive a message from a message queue. + * + * http://pubs.opengroup.org/onlinepubs/9699919799/functions/mq_receive.html + * + * @note msg_prio argument is not supported. Messages are not checked for corruption. + * + * @retval The length of the selected message in bytes - Upon successful completion. + * The message is removed from the queue + * @retval -1 - An error occurred. errno is also set. + * + * @sideeffect Possible errno values + *
+ * EBADF - The mqdes argument is not a valid message queue descriptor open for reading. + *
+ * EMSGSIZE - The specified message buffer size, msg_len, is less than the message size attribute of the message queue. + *
+ * ETIMEDOUT - The O_NONBLOCK flag was not set when the message queue was opened, + * but no message arrived on the queue before the specified timeout expired. + *
+ * EAGAIN - O_NONBLOCK was set in the message description associated with mqdes, and the specified message queue is empty. + */ +ssize_t mq_receive( mqd_t mqdes, + char * msg_ptr, + size_t msg_len, + unsigned int * msg_prio ); + +/** + * @brief Send a message to a message queue. + * + * http://pubs.opengroup.org/onlinepubs/9699919799/functions/mq_send.html + * + * @note msg_prio argument is not supported. + * + * @retval 0 - Upon successful completion. + * @retval -1 - An error occurred. errno is also set. + * + * @sideeffect Possible errno values + *
+ * EBADF - The mqdes argument is not a valid message queue descriptor open for writing. + *
+ * EMSGSIZE - The specified message length, msg_len, exceeds the message size attribute of the message queue, + * OR insufficient memory for the message to be sent. + *
+ * ETIMEDOUT - The O_NONBLOCK flag was not set when the message queue was opened, + * but the timeout expired before the message could be added to the queue. + *
+ * EAGAIN - The O_NONBLOCK flag is set in the message queue description associated with mqdes, + * and the specified message queue is full. + */ +int mq_send( mqd_t mqdes, + const char * msg_ptr, + size_t msg_len, + unsigned msg_prio ); + +/** + * @brief Receive a message from a message queue with timeout. + * + * http://pubs.opengroup.org/onlinepubs/9699919799/functions/mq_timedreceive.html + * + * @note msg_prio argument is not supported. Messages are not checked for corruption. + * + * @retval The length of the selected message in bytes - Upon successful completion. + * The message is removed from the queue + * @retval -1 - An error occurred. errno is also set. + * + * @sideeffect Possible errno values + *
+ * EBADF - The mqdes argument is not a valid message queue descriptor open for reading. + *
+ * EMSGSIZE - The specified message buffer size, msg_len, is less than the message size attribute of the message queue. + *
+ * EINVAL - The process or thread would have blocked, and the abstime parameter specified a nanoseconds field value + * less than zero or greater than or equal to 1000 million. + *
+ * ETIMEDOUT - The O_NONBLOCK flag was not set when the message queue was opened, + * but no message arrived on the queue before the specified timeout expired. + *
+ * EAGAIN - O_NONBLOCK was set in the message description associated with mqdes, and the specified message queue is empty. + */ +ssize_t mq_timedreceive( mqd_t mqdes, + char * msg_ptr, + size_t msg_len, + unsigned * msg_prio, + const struct timespec * abstime ); + +/** + * @brief Send a message to a message queue with timeout. + * + * http://pubs.opengroup.org/onlinepubs/9699919799/functions/mq_timedsend.html + * + * @note msg_prio argument is not supported. + * + * @retval 0 - Upon successful completion. + * @retval -1 - An error occurred. errno is also set. + * + * @sideeffect Possible errno values + *
+ * EBADF - The mqdes argument is not a valid message queue descriptor open for writing. + *
+ * EMSGSIZE - The specified message length, msg_len, exceeds the message size attribute of the message queue, + * OR insufficient memory for the message to be sent. + *
+ * EINVAL - The process or thread would have blocked, and the abstime parameter specified a nanoseconds field + * value less than zero or greater than or equal to 1000 million. + *
+ * ETIMEDOUT - The O_NONBLOCK flag was not set when the message queue was opened, + * but the timeout expired before the message could be added to the queue. + *
+ * EAGAIN - The O_NONBLOCK flag is set in the message queue description associated with mqdes, + * and the specified message queue is full. + */ +int mq_timedsend( mqd_t mqdes, + const char * msg_ptr, + size_t msg_len, + unsigned msg_prio, + const struct timespec * abstime ); + +/** + * @brief Remove a message queue. + * + * http://pubs.opengroup.org/onlinepubs/9699919799/functions/mq_unlink.html + * + * @retval 0 - Upon successful completion. + * @retval -1 - An error occurred. errno is also set. + * + * @sideeffect Possible errno values + *
+ * EINVAL - name argument is invalid. Refer to requirements on name argument in mq_open(). + *
+ * ENOENT - The named message queue does not exist. + */ +int mq_unlink( const char * name ); + +#endif /* ifndef _CC_POSIX_MQUEUE_H_ */ diff --git a/src/lib/posix/include/pthread.h b/src/lib/posix/include/pthread.h new file mode 100644 index 00000000..4fabe011 --- /dev/null +++ b/src/lib/posix/include/pthread.h @@ -0,0 +1,476 @@ +/** + * @file pthread.h + * @brief Threads. + * + * http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/pthread.h.html + */ + +#ifndef _CC_POSIX_PTHREAD_H_ +#define _CC_POSIX_PTHREAD_H_ + +/* CC+POSIX includes. POSIX states that this header shall make symbols + * defined in sched.h and time.h visible. */ +#include "lib/posix/include/sched.h" +#include "lib/posix/include/time.h" + +/** + * @name pthread detach state. + */ +/**@{ */ +#define PTHREAD_CREATE_DETACHED 0 /**< Detached. */ +#define PTHREAD_CREATE_JOINABLE 1 /**< Joinable (default). */ +/**@} */ + +/** + * @name Returned to a single thread after a successful pthread_barrier_wait. + * + * @brief POSIX specifies that "The constant PTHREAD_BARRIER_SERIAL_THREAD is defined in + * and its value shall be distinct from any other value returned by pthread_barrier_wait()." + * So it's defined as negative to distinguish it from the errnos, which are positive. + */ +#define PTHREAD_BARRIER_SERIAL_THREAD ( -2 ) + +/** + * @name Mutex types. + */ +/**@{ */ +#ifndef PTHREAD_MUTEX_NORMAL + #define PTHREAD_MUTEX_NORMAL 0 /**< Non-robust, deadlock on relock, does not remember owner. */ +#endif +#ifndef PTHREAD_MUTEX_ERRORCHECK + #define PTHREAD_MUTEX_ERRORCHECK 1 /**< Non-robust, error on relock, remembers owner. */ +#endif +#ifndef PTHREAD_MUTEX_RECURSIVE + #define PTHREAD_MUTEX_RECURSIVE 2 /**< Non-robust, recursive relock, remembers owner. */ +#endif +#ifndef PTHREAD_MUTEX_DEFAULT + #define PTHREAD_MUTEX_DEFAULT PTHREAD_MUTEX_NORMAL /**< PTHREAD_MUTEX_NORMAL (default). */ +#endif +/**@} */ + +/** + * @name Compile-time initializers. + * + * @brief To use PTHREAD_COND_INITIALIZER, posixconfigENABLE_PTHREAD_COND_T needs to be set to 1 + * in port specific POSIX config file. + * + * To use PTHREAD_MUTEX_INITIALIZER, posixconfigENABLE_PTHREAD_MUTEX_T needs to be set to 1 in + * port specific POSIX config file. + */ +/**@{ */ +#if posixconfigENABLE_PTHREAD_COND_T == 1 + #define PTHREAD_COND_INITIALIZER CC_POSIX_COND_INITIALIZER /**< pthread_cond_t. */ +#endif + +#if posixconfigENABLE_PTHREAD_MUTEX_T == 1 + #define PTHREAD_MUTEX_INITIALIZER CC_POSIX_MUTEX_INITIALIZER /**< pthread_mutex_t. */ +#endif + +/**@} */ + +/** + * @brief Destroy the thread attributes object. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_attr_destroy.html + * + * @retval 0 - Upon successful completion. + */ +int pthread_attr_destroy( pthread_attr_t * attr ); + +/** + * @brief Get detachstate attribute. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_attr_getdetachstate.html + * + * @retval 0 - Upon successful completion. + */ +int pthread_attr_getdetachstate( const pthread_attr_t * attr, + int * detachstate ); + +/** + * @brief Get schedparam attribute. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_attr_getschedparam.html + * + * @retval 0 - Upon successful completion. + */ +int pthread_attr_getschedparam( const pthread_attr_t * attr, + struct sched_param * param ); + +/** + * @brief Get stacksize attribute. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_attr_getstacksize.html + * + * @retval 0 - Upon successful completion. + */ +int pthread_attr_getstacksize( const pthread_attr_t * attr, + size_t * stacksize ); + +/** + * @brief Initialize the thread attributes object. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_attr_init.html + * + * @retval 0 - Upon successful completion. + * + * @note Currently, only stack size, sched_param, and detach state attributes + * are supported. Also see pthread_attr_get*() and pthread_attr_set*(). + */ +int pthread_attr_init( pthread_attr_t * attr ); + +/** + * @brief Set detachstate attribute. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_attr_setdetachstate.html + * + * @retval 0 - Upon successful completion + * @retval EINVAL - The value of detachstate is not valid. Currently, supported detach states are -- + * PTHREAD_CREATE_DETACHED and PTHREAD_CREATE_JOINABLE. + */ +int pthread_attr_setdetachstate( pthread_attr_t * attr, + int detachstate ); + +/** + * @brief Set schedparam attribute. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_attr_setschedparam.html + * + * @retval 0 - Upon successful completion. + * @retval EINVAL - The value of param is not valid. + * @retval ENOTSUP - An attempt was made to set the attribute to an unsupported value. + */ +int pthread_attr_setschedparam( pthread_attr_t * attr, + const struct sched_param * param ); + +/** + * @brief Set the schedpolicy attribute. + * + * http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_attr_setschedpolicy.html + * + * @retval 0 - Upon successful completion. + * + * @warning This function is a stub and always returns 0. + */ +int pthread_attr_setschedpolicy( pthread_attr_t * attr, + int policy ); + +/** + * @brief Set stacksize attribute. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_attr_setstacksize.html + * + * @retval 0 - Upon successful completion. + * @retval EINVAL - The value of stacksize is less than {PTHREAD_STACK_MIN}. + */ +int pthread_attr_setstacksize( pthread_attr_t * attr, + size_t stacksize ); + +/** + * @brief Destroy a barrier object. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_barrier_destroy.html + * + * @retval 0 - Upon successful completion. + * + * @note This function does not validate whether there is any thread blocking on the barrier before destroying. + */ +int pthread_barrier_destroy( pthread_barrier_t * barrier ); + +/** + * @brief Initialize a barrier object. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_barrier_init.html + * + * @retval 0 - Upon successful completion. + * @retval EINVAL - The value specified by count is equal to zero. + * @retval ENOMEM - count cannot fit into Kernel event group type OR insufficient memory exists to initialize the barrier. + * + * @note attr is ignored. + * + * @note pthread_barrier_init() is implemented with Kernel event group. + * To ensure count fits in event group, count may be at most 8 when configUSE_16_BIT_TICKS is 1; + * it may be at most 24 otherwise. configUSE_16_BIT_TICKS is configured in application FreeRTOSConfig.h + * file, which defines how many bits tick count type has. See further details and limitation about event + * group and configUSE_16_BIT_TICKS in Kernel site. + */ +int pthread_barrier_init( pthread_barrier_t * barrier, + const pthread_barrierattr_t * attr, + unsigned count ); + +/** + * @brief Synchronize at a barrier. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_barrier_wait.html + * + * @retval PTHREAD_BARRIER_SERIAL_THREAD - Upon successful completion, the first thread. + * @retval 0 - Upon successful completion, other thread(s). + */ +int pthread_barrier_wait( pthread_barrier_t * barrier ); + +/** + * @brief Thread creation. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_create.html + * + * @retval 0 - Upon successful completion. + * @retval EAGAIN - Insufficient memory for either thread structure or task creation. + */ +int pthread_create( pthread_t * thread, + const pthread_attr_t * attr, + void *( *startroutine )( void * ), + void * arg ); + +/** + * @brief Broadcast a condition. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_cond_broadcast.html + * + * @retval 0 - Upon successful completion. + */ +int pthread_cond_broadcast( pthread_cond_t * cond ); + +/** + * @brief Destroy condition variables. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_cond_destroy.html + * + * @retval 0 - Upon successful completion. + */ +int pthread_cond_destroy( pthread_cond_t * cond ); + +/** + * @brief Initialize condition variables. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_cond_init.html + * + * @retval 0 - Upon successful completion. + * @retval ENOMEM - Insufficient memory exists to initialize the condition variable. + * + * @note attr is ignored and treated as NULL. Default setting is always used. + */ +int pthread_cond_init( pthread_cond_t * cond, + const pthread_condattr_t * attr ); + +/** + * @brief Signal a condition. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_cond_signal.html + * + * @retval 0 - Upon successful completion. + */ +int pthread_cond_signal( pthread_cond_t * cond ); + +/** + * @brief Wait on a condition with a timeout. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_cond_timedwait.html + * + * @retval 0 - Upon successful completion. + * @retval EINVAL - The abstime argument passed in does not refer to an initialized structure OR + * the abstime parameter specified a nanoseconds field value less than zero or + * greater than or equal to 1000 million. + * @retval ETIMEDOUT - The time specified by abstime to pthread_cond_timedwait() has passed. + */ +int pthread_cond_timedwait( pthread_cond_t * cond, + pthread_mutex_t * mutex, + const struct timespec * abstime ); + +/** + * @brief Wait on a condition. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_cond_wait.html + * + * @retval 0 - Upon successful completion. + */ +int pthread_cond_wait( pthread_cond_t * cond, + pthread_mutex_t * mutex ); + +/** + * @brief Compare thread IDs. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_equal.html + * + * @retval 0 - t1 and t2 are both not NULL && equal. + * @retval non-zero - otherwise. + */ +int pthread_equal( pthread_t t1, + pthread_t t2 ); + +/** + * @brief Thread termination. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_exit.html + * + * @retval void - this function cannot return to its caller. + */ +void pthread_exit( void * value_ptr ); + +/** + * @brief Dynamic thread scheduling parameters access. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_getschedparam.html + * + * @retval 0 - Upon successful completion. + * + * @note policy is always set to SCHED_OTHER by this function. + */ +int pthread_getschedparam( pthread_t thread, + int * policy, + struct sched_param * param ); + +/** + * @brief Wait for thread termination. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_join.html + * + * @retval 0 - Upon successful completion. + * @retval EDEADLK - The value specified by the thread argument to pthread_join() does not refer + * to a joinable thread OR multiple simultaneous calls to pthread_join() + * specifying the same target thread OR the value specified by the thread argument + * to pthread_join() refers to the calling thread. + */ +int pthread_join( pthread_t thread, + void ** retval ); + +/** + * @brief Destroy a mutex. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_mutex_destroy.html + * + * @retval 0 - Upon successful completion. + * + * @note If there exists a thread holding this mutex, this function returns 0 with mutex not being destroyed. + */ +int pthread_mutex_destroy( pthread_mutex_t * mutex ); + +/** + * @brief Initialize a mutex. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_mutex_init.html + * + * @retval 0 - Upon successful completion. + * @retval ENOMEM - Insufficient memory exists to initialize the mutex structure. + * @retval EAGAIN - Unable to initialize the mutex structure member(s). + */ +int pthread_mutex_init( pthread_mutex_t * mutex, + const pthread_mutexattr_t * attr ); + +/** + * @brief Lock a mutex. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_mutex_lock.html + * + * @retval 0 - Upon successful completion. + * @retval EINVAL - the abstime parameter specified a nanoseconds field value less than zero + * or greater than or equal to 1000 million. + * @retval EDEADLK - The mutex type is PTHREAD_MUTEX_ERRORCHECK and the current thread already + * owns the mutex. + */ +int pthread_mutex_lock( pthread_mutex_t * mutex ); + +/** + * @brief Lock a mutex with timeout. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_mutex_timedlock.html + * + * @retval 0 - Upon successful completion. + * @retval EINVAL - The abstime argument passed in does not refer to an initialized structure OR + * the abstime parameter specified a nanoseconds field value less than zero or + * greater than or equal to 1000 million. + * @retval EDEADLK - The mutex type is PTHREAD_MUTEX_ERRORCHECK and the current thread already owns the mutex. + * @retval ETIMEDOUT - The mutex could not be locked before the specified timeout expired. + */ +int pthread_mutex_timedlock( pthread_mutex_t * mutex, + const struct timespec * abstime ); + +/** + * @brief Attempt to lock a mutex. Fail immediately if mutex is already locked. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_mutex_trylock.html + * + * @retval 0 - Upon successful completion. + * @retval EINVAL - the abstime parameter specified a nanoseconds field value less than zero + * or greater than or equal to 1000 million. + * @retval EDEADLK - The mutex type is PTHREAD_MUTEX_ERRORCHECK and the current thread already + * owns the mutex. + * @retval EBUSY - The mutex could not be acquired because it was already locked. + */ +int pthread_mutex_trylock( pthread_mutex_t * mutex ); + +/** + * @brief Unlock a mutex. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_mutex_unlock.html + * + * @retval 0 - Upon successful completion. + * @retval EPERM - The mutex type is PTHREAD_MUTEX_ERRORCHECK or PTHREAD_MUTEX_RECURSIVE, and + * the current thread does not own the mutex. + */ +int pthread_mutex_unlock( pthread_mutex_t * mutex ); + +/** + * @brief Destroy the mutex attributes object. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_mutexattr_destroy.html + * + * @retval 0 - Upon successful completion. + */ +int pthread_mutexattr_destroy( pthread_mutexattr_t * attr ); + +/** + * @brief Get the mutex type attribute. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_mutexattr_gettype.html + * + * @retval 0 - Upon successful completion. + */ +int pthread_mutexattr_gettype( const pthread_mutexattr_t * attr, + int * type ); + +/** + * @brief Initialize the mutex attributes object. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_mutexattr_init.html + * + * @retval 0 - Upon successful completion. + * + * @note Currently, only the type attribute is supported. Also see pthread_mutexattr_settype() + * and pthread_mutexattr_gettype(). + */ +int pthread_mutexattr_init( pthread_mutexattr_t * attr ); + +/** + * @brief Set the mutex type attribute. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_mutexattr_settype.html + * + * @retval 0 - Upon successful completion. + * @retval EINVAL - The value type is invalid. + */ +int pthread_mutexattr_settype( pthread_mutexattr_t * attr, + int type ); + +/** + * @brief Get the calling thread ID. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_self.html + * + * @retval the thread ID of the calling thread. + */ +pthread_t pthread_self( void ); + +/** + * @brief Dynamic thread scheduling parameters access. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_setschedparam.html + * + * @note policy is ignored; only priority (param.sched_priority) may be changed. + * + * @retval 0 - Upon successful completion. + */ +int pthread_setschedparam( pthread_t thread, + int policy, + const struct sched_param * param ); + +#endif /* _CC_POSIX_PTHREAD_H_ */ diff --git a/src/lib/posix/include/sched.h b/src/lib/posix/include/sched.h new file mode 100644 index 00000000..14532d1b --- /dev/null +++ b/src/lib/posix/include/sched.h @@ -0,0 +1,59 @@ +/** + * @file sched.h + * @brief Execution scheduling. + * + * http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sched.h.html + */ + +#ifndef _CC_POSIX_SCHED_H_ +#define _CC_POSIX_SCHED_H_ + +/** + * @name Scheduling Policies + */ +/**@{ */ +#define SCHED_OTHER 0 /**< Another scheduling policy. */ +/**@} */ + +/** + * @brief Scheduling parameters required for implementation of each supported + * scheduling policy. + */ +struct sched_param +{ + int sched_priority; /**< Process or thread execution scheduling priority. */ +}; + +/** + * @brief Get priority limit (max). + * + * http://pubs.opengroup.org/onlinepubs/9699919799/functions/sched_get_priority_max.html + * + * @note policy is ignored. + * + * @return the maximum priority value (0-based) system configuration allows. + *
+ * e.g. if configMAX_PRIORITIES == 7, this function returns (configMAX_PRIORITIES - 1). + * configMAX_PRIORITIES is configured in application FreeRTOSConfig.h file. + */ +int sched_get_priority_max( int policy ); + +/** + * @brief Get priority limit (min). + * + * http://pubs.opengroup.org/onlinepubs/9699919799/functions/sched_get_priority_min.html + * + * @note policy is ignored. + */ +int sched_get_priority_min( int policy ); + +/** + * @brief Yield the processor. + * + * http://pubs.opengroup.org/onlinepubs/9699919799/functions/sched_yield.html + * + * @retval 0 - Upon successful completion + */ +int sched_yield( void ); + +#endif /* ifndef _CC_POSIX_SCHED_H_ */ diff --git a/src/lib/posix/include/semaphore.h b/src/lib/posix/include/semaphore.h new file mode 100644 index 00000000..d82c514e --- /dev/null +++ b/src/lib/posix/include/semaphore.h @@ -0,0 +1,118 @@ +/** + * @file semaphore.h + * @brief Semaphores. + * + * http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/semaphore.h.html + */ + +#ifndef _CC_POSIX_SEMAPHORE_H_ +#define _CC_POSIX_SEMAPHORE_H_ + +/* CC+POSIX includes. */ +#include "lib/posix/include/time.h" +#include "lib/posix/cc_posix_types.h" + +/** + * @brief Semaphore type. + */ +typedef PosixSemType_t sem_t; + +/** + * @brief Destroy an unnamed semaphore. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/sem_destroy.html + * + * @retval 0 - upon successful completion + * + * @note Semaphore is destroyed regardless of whether there is any thread currently blocked on this semaphore. + */ +int sem_destroy( sem_t * sem ); + +/** + * @brief Get the value of a semaphore. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/sem_getvalue.html + * + * @retval 0 - Upon successful completion + * + * @note If sem is locked, then the object to which sval points is set to zero. + */ +int sem_getvalue( sem_t * sem, + int * sval ); + +/** + * @brief Initialize an unnamed semaphore. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/sem_init.html + * + * @note pshared is ignored. Semaphores will always be considered "shared". + * + * @retval 0 - upon successful completion + * @retval -1 - otherwise. System error variable errno is also set in this case. + * + * @sideeffect Possible errno values + *
+ * EINVAL - The value argument exceeds {SEM_VALUE_MAX}. + *
+ * ENOSPC - A resource required to initialize the semaphore has been exhausted. + */ +int sem_init( sem_t * sem, + int pshared, + unsigned value ); + +/** + * @brief Unlock a semaphore. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/sem_post.html + * + * @retval 0 - upon successful completion + */ +int sem_post( sem_t * sem ); + +/** + * @brief Lock a semaphore with timeout. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/sem_timedwait.html + * + * @retval 0 - upon successful completion + * @retval -1 - otherwise. System error variable errno is also set in this case. + * + * @sideeffect Possible errno values + *
+ * EINVAL - parameter specified a nanoseconds field value less than zero or greater + * than or equal to 1000 million + *
+ * ETIMEDOUT - The semaphore could not be locked before the specified timeout expired. + * + * @note Deadlock detection is not implemented. + */ +int sem_timedwait( sem_t * sem, + const struct timespec * abstime ); + +/** + * @brief Lock a semaphore if available. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/sem_trywait.html + * + * @retval 0 - upon successful completion + * @retval -1 - otherwise. System error variable errno is also set in this case. + * + * @sideeffect Possible errno values + *
+ * EAGAIN - The semaphore was already locked, so it cannot be immediately locked by the sem_trywait() operation. + */ +int sem_trywait( sem_t * sem ); + +/** + * @brief Lock a semaphore. + * + * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/sem_wait.html + * + * @retval 0 - upon successful completion + * @retval -1 - otherwise. System error variable errno is also set in this case. + * + * @note Deadlock detection is not implemented. + */ +int sem_wait( sem_t * sem ); + +#endif /* ifndef _CC_POSIX_SEMAPHORE_H_ */ diff --git a/src/lib/posix/include/signal.h b/src/lib/posix/include/signal.h new file mode 100644 index 00000000..7beb8cd6 --- /dev/null +++ b/src/lib/posix/include/signal.h @@ -0,0 +1,45 @@ +/** + * @file signal.h + * @brief Signals. + * + * Signals are currently not implemented in CC+POSIX. This header only + * defines the signal data structures used elsewhere. + * + * http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/signal.h.html + */ + + +#ifndef _CC_POSIX_SIGNAL_H_ +#define _CC_POSIX_SIGNAL_H_ + +/** + * @name Values of sigev_notify. + */ +/**@{ */ +#define SIGEV_NONE 0 /**< No asynchronous notification is delivered when the event of interest occurs. */ +#define SIGEV_SIGNAL 1 /**< A queued signal, with an application-defined value, is generated when the event of interest occurs. Not supported. */ +#define SIGEV_THREAD 2 /**< A notification function is called to perform notification. */ +/**@} */ + +/** + * @brief Signal value. + */ +union sigval +{ + int sival_int; /**< Integer signal value. */ + void * sival_ptr; /**< Pointer signal value. */ +}; + +/** + * @brief Signal event structure. + */ +struct sigevent +{ + int sigev_notify; /**< Notification type. A value of SIGEV_SIGNAL is not supported. */ + int sigev_signo; /**< Signal number. This member is ignored. */ + union sigval sigev_value; /**< Signal value. Only the sival_ptr member is used. */ + void ( * sigev_notify_function )( union sigval ); /**< Notification function. */ + pthread_attr_t * sigev_notify_attributes; /**< Notification attributes. */ +}; + +#endif /* ifndef _CC_POSIX_SIGNAL_H_ */ diff --git a/src/lib/posix/include/sys/types.h b/src/lib/posix/include/sys/types.h new file mode 100644 index 00000000..a370432b --- /dev/null +++ b/src/lib/posix/include/sys/types.h @@ -0,0 +1,166 @@ +/** + * @file sys/types.h + * @brief Data types. + * + * http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_types.h.html + */ + +#ifndef _CC_POSIX_TYPES_H_ +#define _CC_POSIX_TYPES_H_ + +/* C standard library includes. */ +#include + +/* Kernel types include */ +#include + +/** + * @brief Used for system times in clock ticks or CLOCKS_PER_SEC. + * + * Enabled/disabled by posixconfigENABLE_CLOCK_T. + */ +#if !defined( posixconfigENABLE_CLOCK_T ) || ( posixconfigENABLE_CLOCK_T == 1 ) + typedef uint32_t clock_t; +#endif + +/** + * @brief Used for clock ID type in the clock and timer functions. + * + * Enabled/disabled by posixconfigENABLE_CLOCKID_T. + */ +#if !defined( posixconfigENABLE_CLOCKID_T ) || ( posixconfigENABLE_CLOCKID_T == 1 ) + typedef int clockid_t; +#endif + +/** + * @brief Used for some file attributes. + * + * Enabled/disabled by posixconfigENABLE_MODE_T. + */ +#if !defined( posixconfigENABLE_MODE_T ) || ( posixconfigENABLE_MODE_T == 1 ) + typedef int mode_t; +#endif + +/** + * @brief Used for process IDs and process group IDs. + * + * Enabled/disabled by posixconfigENABLE_PID_T. + */ +#if !defined( posixconfigENABLE_PID_T ) || ( posixconfigENABLE_PID_T == 1 ) + typedef int pid_t; +#endif + +/** + * @brief Used to identify a thread attribute object. + * + * Enabled/disabled by posixconfigENABLE_PTHREAD_ATTR_T. + */ +#if !defined( posixconfigENABLE_PTHREAD_ATTR_T ) || ( posixconfigENABLE_PTHREAD_ATTR_T == 1 ) + typedef PthreadAttrType_t pthread_attr_t; +#endif + +/** + * @brief Used to identify a barrier. + * + * Enabled/disabled by posixconfigENABLE_PTHREAD_BARRIER_T. + */ +#if !defined( posixconfigENABLE_PTHREAD_BARRIER_T ) || ( posixconfigENABLE_PTHREAD_BARRIER_T == 1 ) + typedef PthreadBarrierType_t pthread_barrier_t; +#endif + +/** + * @brief Used to define a barrier attributes object. + */ +typedef void * pthread_barrierattr_t; + +/** + * @brief Used for condition variables. + * + * Enabled/disabled by posixconfigENABLE_PTHREAD_COND_T. + */ +#if !defined( posixconfigENABLE_PTHREAD_COND_T ) || ( posixconfigENABLE_PTHREAD_COND_T == 1 ) + typedef PthreadCondType_t pthread_cond_t; +#endif + +/** + * @brief Used to identify a condition attribute object. + * + * Enabled/disabled by posixconfigENABLE_PTHREAD_CONDATTR_T. + */ +#if !defined( posixconfigENABLE_PTHREAD_CONDATTR_T ) || ( posixconfigENABLE_PTHREAD_CONDATTR_T == 1 ) + typedef void * pthread_condattr_t; +#endif + +/** + * @brief Used for mutexes. + * + * Enabled/disabled by posixconfigENABLE_PTHREAD_MUTEX_T. + */ +#if !defined( posixconfigENABLE_PTHREAD_MUTEX_T ) || ( posixconfigENABLE_PTHREAD_MUTEX_T == 1 ) + typedef PthreadMutexType_t pthread_mutex_t; +#endif + +/** + * @brief Used to identify a mutex attribute object. + * + * Enabled/disabled by posixconfigENABLE_PTHREAD_MUTEXATTR_T. + */ +#if !defined( posixconfigENABLE_PTHREAD_MUTEXATTR_T ) || ( posixconfigENABLE_PTHREAD_MUTEXATTR_T == 1 ) + typedef PthreadMutexAttrType_t pthread_mutexattr_t; +#endif + +/** + * @brief Used to identify a thread. + * + * Enabled/disabled by posixconfigENABLE_PTHREAD_T. + */ +#if !defined( posixconfigENABLE_PTHREAD_T ) || ( posixconfigENABLE_PTHREAD_T == 1 ) + typedef void * pthread_t; +#endif + +/** + * @brief Used for a count of bytes or an error indication. + * + * Enabled/disabled by posixconfigENABLE_SSIZE_T. + */ +#if !defined( posixconfigENABLE_SSIZE_T ) || ( posixconfigENABLE_SSIZE_T == 1 ) + typedef int ssize_t; +#endif + +/** + * @brief Used for time in seconds. + * + * Enabled/disabled by posixconfigENABLE_TIME_T. + */ +#if !defined( posixconfigENABLE_TIME_T ) || ( posixconfigENABLE_TIME_T == 1 ) + typedef int64_t time_t; +#endif + +/** + * @brief Used for timer ID returned by timer_create(). + * + * Enabled/disabled by posixconfigENABLE_TIMER_T. + */ +#if !defined( posixconfigENABLE_TIMER_T ) || ( posixconfigENABLE_TIMER_T == 1 ) + typedef void * timer_t; +#endif + +/** + * @brief Used for time in microseconds. + * + * Enabled/disabled by posixconfigENABLE_USECONDS_T. + */ +#if !defined( posixconfigENABLE_USECONDS_T ) || ( posixconfigENABLE_USECONDS_T == 1 ) + typedef unsigned long useconds_t; +#endif + +/** + * @brief Used for file sizes. + * + * Enabled/disabled by posixconfigENABLE_OFF_T. + */ +#if !defined( posixconfigENABLE_OFF_T ) || ( posixconfigENABLE_OFF_T == 1 ) + typedef long int off_t; +#endif + +#endif /* ifndef _CC_POSIX_TYPES_H_ */ diff --git a/src/lib/posix/include/time.h b/src/lib/posix/include/time.h new file mode 100644 index 00000000..02b51a5a --- /dev/null +++ b/src/lib/posix/include/time.h @@ -0,0 +1,233 @@ +/** + * @file time.h + * @brief Time types. + * + * http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/time.h.html + */ + +#ifndef _CC_POSIX_TIME_H_ +#define _CC_POSIX_TIME_H_ + +/* CC+POSIX includes. */ +#include +#include + +/** + * @name Unit conversion constants. + */ +/**@{ */ +#define MICROSECONDS_PER_SECOND ( 1000000LL ) /**< Microseconds per second. */ +#define NANOSECONDS_PER_SECOND ( 1000000000LL ) /**< Nanoseconds per second. */ +#define NANOSECONDS_PER_TICK ( NANOSECONDS_PER_SECOND / configTICK_RATE_HZ ) /**< Nanoseconds per Kernel tick. */ +/**@} */ + +/** + * @name Clock identifiers. + */ +/**@{ */ +#define CLOCK_REALTIME 0 /**< The identifier of the system-wide clock measuring real time. */ +#define CLOCK_MONOTONIC 1 /**< The identifier for the system-wide monotonic clock.*/ +/**@} */ + +/** + * @name A number used to convert the value returned by the clock() function into seconds. + */ +/**@{ */ +#define CLOCKS_PER_SEC ( ( clock_t ) configTICK_RATE_HZ ) +/**@} */ + +/** + * @name Flag indicating time is absolute. + * + * For functions taking timer objects, this refers to the clock associated with the timer. + */ +/**@{ */ +#define TIMER_ABSTIME 0x01 +/**@} */ + +#if !defined( posixconfigENABLE_TIMESPEC ) || ( posixconfigENABLE_TIMESPEC == 1 ) + +/** + * @brief represents an elapsed time + */ + struct timespec + { + time_t tv_sec; /**< Seconds. */ + long tv_nsec; /**< Nanoseconds. */ + }; +#endif + +#if !defined( posixconfigENABLE_ITIMERSPEC ) || ( posixconfigENABLE_ITIMERSPEC == 1 ) + +/** + * @brief timer + */ + struct itimerspec + { + struct timespec it_interval; /**< Timer period. */ + struct timespec it_value; /**< Timer expiration. */ + }; +#endif + +/** + * @brief Report CPU time used. + * + * http://pubs.opengroup.org/onlinepubs/9699919799/functions/clock.html + * + * @return The number of Kernel ticks since the scheduler + * was started minus the ticks spent in the idle task. + * + * @note This function does NOT report the number of ticks spent by the calling thread. + */ +clock_t clock( void ); + +/** + * @brief Access a process CPU-time clock. + * + * http://pubs.opengroup.org/onlinepubs/9699919799/functions/clock_getcpuclockid.html + * + * @retval EPERM + * + * @note This function is currently unsupported. + * + */ +int clock_getcpuclockid( pid_t pid, + clockid_t * clock_id ); + +/** + * @brief Returns the resolution of a clock. + * + * http://pubs.opengroup.org/onlinepubs/9699919799/functions/clock_getres.html + * + * @note clock_id is ignored + * @note This function stores the resolution of the Kernel tick count in the object res points to. + * + * @retval 0 - Upon successful execution + */ +int clock_getres( clockid_t clock_id, + struct timespec * res ); + +/** + * @brief Returns the current value for the specified clock, clock_id. + * + * http://pubs.opengroup.org/onlinepubs/9699919799/functions/clock_gettime.html + * + * @note clock_id is ignored + * @note this function does not check for overflows of time_t. + * + * @retval 0 - Upon successful completion. + */ +int clock_gettime( clockid_t clock_id, + struct timespec * tp ); + +/** + * @brief High resolution sleep with specifiable clock. + * + * http://pubs.opengroup.org/onlinepubs/9699919799/functions/clock_nanosleep.html + * + * @note clock_id is ignored, as this function uses the Kernel tick count as its clock. + * @note flags is ignored, if INCLUDE_vTaskDelayUntil is 0. i.e. the Kernel function vTaskDelayUntil isn't available. + * @note rmtp is also ignored, as signals are not implemented. + * + * @retval 0 - Upon successful completion. + * @retval EINVAL - The rqtp argument specified a nanosecond value less than zero or greater than or equal to 1000 million. + */ +int clock_nanosleep( clockid_t clock_id, + int flags, + const struct timespec * rqtp, + struct timespec * rmtp ); + +/** + * @brief Sets the time for the specified clock. + * + * http://pubs.opengroup.org/onlinepubs/9699919799/functions/clock_settime.html + * + * @retval -1 with errno set to EPERM. + * + * @note This function is currently unsupported, as Kernel does not provide a function to modify the tick count. + */ +int clock_settime( clockid_t clock_id, + const struct timespec * tp ); + +/** + * @brief High resolution sleep. + * + * http://pubs.opengroup.org/onlinepubs/9699919799/functions/nanosleep.html + * + * @note rmtp is ignored, as signals are not implemented. + * + * @retval 0 - Upon successful completion. + * @retval -1 - The rqtp argument is invalid OR the rqtp argument specified a nanosecond value less than zero or greater than or equal to 1000 million. + * + */ +int nanosleep( const struct timespec * rqtp, + struct timespec * rmtp ); + +/** + * @brief Create a per-process timer. + * + * http://pubs.opengroup.org/onlinepubs/9699919799/functions/timer_create.html + * + * @note clock_id is ignored, as this function used the Kernel tick count as its clock. + * @note evp.sigev_notify must be set to SIGEV_THREAD, since signals are currently not supported. + * + * @retval 0 - Upon successful completion, with location referenced by timerid updated. + * @retval -1 - If an error occurs. errno is also set. + * + * @sideeffect Possible errno values + *
+ * ENOTSUP - If evp is NULL OR evp->sigen_notify == SIGEV_SIGNAL. + *
+ * EAGAIN - The system lacks sufficient signal queuing resources to honor the request. + */ +int timer_create( clockid_t clockid, + struct sigevent * evp, + timer_t * timerid ); + +/** + * @brief Delete a per-process timer. + * + * http://pubs.opengroup.org/onlinepubs/9699919799/functions/timer_delete.html + * + * @retval 0 - Upon successful completion. + */ +int timer_delete( timer_t timerid ); + +/** + * @brief Get the timer overrun count. + * + * http://pubs.opengroup.org/onlinepubs/9699919799/functions/timer_getoverrun.html + * + * @retval 0 - Always return 0, since signals are not supported. + */ +int timer_getoverrun( timer_t timerid ); + +/** + * @brief Get the amount of time until the timer expires. + * + * http://pubs.opengroup.org/onlinepubs/9699919799/functions/timer_gettime.html + * + * @retval 0 - Upon successful completion. + */ +int timer_gettime( timer_t timerid, + struct itimerspec * value ); + +/** + * @brief Set the time until the next expiration of the timer. + * + * http://pubs.opengroup.org/onlinepubs/9699919799/functions/timer_settime.html + * + * @retval 0 - Upon successful completion. + * @retval -1 - An error occurred, errno is also set. + * + * @sideeffect Possible errno values + *
+ * EINVAL - A value structure specified a nanosecond value less than zero or greater than or equal to 1000 million, + * AND the it_value member of that structure did not specify zero seconds and nanoseconds. + */ +int timer_settime( timer_t timerid, + int flags, + const struct itimerspec * value, + struct itimerspec * ovalue ); + +#endif /* ifndef _CC_POSIX_TIME_H_ */ diff --git a/src/lib/posix/include/unistd.h b/src/lib/posix/include/unistd.h new file mode 100644 index 00000000..c387347d --- /dev/null +++ b/src/lib/posix/include/unistd.h @@ -0,0 +1,36 @@ +/** + * @file unistd.h + * @brief Standard symbolic constants and types + * + * http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/unistd.h.html + */ + +#ifndef _CC_POSIX_UNISTD_H_ +#define _CC_POSIX_UNISTD_H_ + +#include "lib/posix/include/sys/types.h" + +/** + * @brief Suspend execution for an interval of time. + * + * http://pubs.opengroup.org/onlinepubs/9699919799/functions/sleep.html + * + * @param[in] seconds The number of seconds to suspend execution. + * + * @retval 0 - Upon successful completion. + * + * @note Return value of a positive number is not yet supported. + */ +unsigned sleep( unsigned seconds ); + +/** + * @brief Suspend execution for microsecond intervals. + * + * This is a useful, non-POSIX function. + * @param[in] usec The number of microseconds to suspend execution. + * + * @retval 0 - Upon successful completion. + */ +int usleep( useconds_t usec ); + +#endif /* ifndef _CC_POSIX_UNISTD_H_ */ diff --git a/src/lib/posix/include/utils.h b/src/lib/posix/include/utils.h new file mode 100644 index 00000000..1788a1fe --- /dev/null +++ b/src/lib/posix/include/utils.h @@ -0,0 +1,130 @@ +/** + * @file utils.h + * @brief Utility functions used by CC+POSIX. + */ + +#ifndef _CC_POSIX_UTILS_ +#define _CC_POSIX_UTILS_ + +/* C standard library includes. */ +#include +#include + +/* CC+POSIX includes. */ +#include + +/** + * @brief Calculates the length of pcString, up to xMaxLength. + * + * @param[in] pcString The string to find the length of. + * @param[in] xMaxLength The limit when searching for the end of pcString. + * + * @return 0 if pcString is NULL; otherwise, the length of pcString or xMaxLength, + * whichever is smaller. + */ +size_t UTILS_strnlen( const char * const pcString, + size_t xMaxLength ); + +/** + * @brief Calculates the number of ticks between now and a given timespec. + * + * @param[in] pxAbsoluteTime A time in the future, specified as seconds and + * nanoseconds since CLOCK_REALTIME's 0. + * @param[in] pxCurrentTime current time, specified as seconds and + * nanoseconds. + * @param[out] pxResult Where the result of the conversion is stored. The result + * is rounded up for fractional ticks. + * + * @return 0 on success. Otherwise, ETIMEDOUT if pxAbsoluteTime is in the past, + * or EINVAL for invalid parameters. + */ +int UTILS_AbsoluteTimespecToDeltaTicks( const struct timespec * const pxAbsoluteTime, + const struct timespec * const pxCurrentTime, + TickType_t * const pxResult ); + +/** + * @brief Converts a struct timespec to Kernel ticks. + * + * @param[in] pxTimespec The timespec to convert. + * @param[out] Where the result of the conversion is stored. The result is rounded + * up for fractional ticks. + * + * @return 0 on success. Otherwise, EINVAL for invalid parameters. + */ +int UTILS_TimespecToTicks( const struct timespec * const pxTimespec, + TickType_t * const pxResult ); + +/** + * @brief Converts an integer value to a timespec. + * + * @param[in] llSource The value to convert. + * @param[out] pxDestination Where to store the converted value. + * + * @return No return value. + */ +void UTILS_NanosecondsToTimespec( int64_t llSource, + struct timespec * const pxDestination ); + +/** + * @brief Calculates pxResult = x + y. + * + * @param[in] x The first argument for addition. + * @param[in] y The second argument for addition. + * @param[out] pxResult Where the result of the calculation is stored. + * + * @return -1 if any argument was NULL; 1 if result is negative (overflow); otherwise, 0. + */ +int UTILS_TimespecAdd( const struct timespec * const x, + const struct timespec * const y, + struct timespec * const pxResult ); + +/** + * @brief Calculates pxResult = x + ( struct timespec ) nanosec. + * + * @param[in] x The first argument for addition. + * @param[in] llNanoseconds The second argument for addition. + * @param[out] pxResult Where the result of the calculation is stored. + * + * @return -1 if pxResult or x was NULL; 1 if result is negative; otherwise, 0. + */ +int UTILS_TimespecAddNanoseconds( const struct timespec * const x, + int64_t llNanoseconds, + struct timespec * const pxResult ); + +/** + * @brief Calculates pxResult = x - y. If the result is negative contents of + * pResult are undefined + * + * @param[in] x The first argument for subtraction. + * @param[in] y The second argument for subtraction. + * @param[out] pxResult Where the result of the calculation is stored. + * + * @return -1 if any argument was NULL; 1 if result is negative; otherwise, 0. + */ +int UTILS_TimespecSubtract( const struct timespec * const x, + const struct timespec * const y, + struct timespec * const pxResult ); + +/** + * @brief Compare x == y. + * + * @param[in] x The first argument for comparison. + * @param[in] y The second argument for comparison. + * + * @return 0 if x == y; 1 if x > y; -1 if x < y or any argument was NULL + */ +int UTILS_TimespecCompare( const struct timespec * const x, + const struct timespec * const y ); + +/** + * @brief Checks that a timespec conforms to POSIX. + * + * A valid timespec must have 0 <= tv_nsec < 1000000000. + * + * @param[in] pxTimespec The timespec to validate. + * + * @return true if the pxTimespec is valid, false otherwise. + */ +bool UTILS_ValidateTimespec( const struct timespec * const pxTimespec ); + +#endif /* ifndef _CC_POSIX_UTILS_ */ diff --git a/src/lib/posix/src/posix_time.c b/src/lib/posix/src/posix_time.c new file mode 100644 index 00000000..758e8cfd --- /dev/null +++ b/src/lib/posix/src/posix_time.c @@ -0,0 +1,12 @@ +#include +#include + +unsigned sleep( unsigned seconds ) +{ + return (unsigned) usleep(MICROSECONDS_PER_SECOND * seconds); +} + +int usleep( useconds_t usec ) +{ + +} From 8f35a598af44f7ad69280d85b75c0d515b00e9e5 Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Sat, 25 Dec 2021 17:49:41 +0530 Subject: [PATCH 04/31] Add support for arch, initial commit - Added RISC-V 32I code support - Added initial platform support for Sifive FE310G002 (Hifive 1B) --- README.md | 7 +- mk/elf.mk | 2 +- src/arch/riscv/32/i/terravisor/arch.c | 70 +++++++++++ src/arch/riscv/32/i/terravisor/asm.S | 0 src/arch/riscv/32/i/terravisor/build.mk | 16 +++ src/arch/riscv/32/i/terravisor/entry.S | 73 +++++++++++ src/arch/riscv/32/i/terravisor/include/arch.h | 86 +++++++++++++ src/arch/riscv/32/i/terravisor/init.c | 31 +++++ .../riscv/32/i/terravisor/interrupt_handler.c | 0 src/arch/riscv/32/i/terravisor/spinlock.S | 0 src/arch/riscv/32/i/terravisor/stack.S | 0 src/platform/sifive/common_fe310/build.mk | 23 ++++ .../sifive/common_fe310/include/platform.h | 23 ++++ .../sifive/common_fe310/sections.ld.sx | 115 ++++++++++++++++++ src/platform/sifive/fe310g002/build.mk | 26 ++++ src/platform/sifive/fe310g002/config.mk | 59 +++++++++ .../sifive/fe310g002/include/plat_mem.h | 30 +++++ src/projects/demo_riscv/build.mk | 21 ++++ src/projects/demo_riscv/config.mk | 16 +++ src/projects/demo_riscv/project.c | 18 +++ 20 files changed, 611 insertions(+), 5 deletions(-) create mode 100644 src/arch/riscv/32/i/terravisor/arch.c create mode 100644 src/arch/riscv/32/i/terravisor/asm.S create mode 100644 src/arch/riscv/32/i/terravisor/build.mk create mode 100644 src/arch/riscv/32/i/terravisor/entry.S create mode 100644 src/arch/riscv/32/i/terravisor/include/arch.h create mode 100644 src/arch/riscv/32/i/terravisor/init.c create mode 100644 src/arch/riscv/32/i/terravisor/interrupt_handler.c create mode 100644 src/arch/riscv/32/i/terravisor/spinlock.S create mode 100644 src/arch/riscv/32/i/terravisor/stack.S create mode 100644 src/platform/sifive/common_fe310/build.mk create mode 100644 src/platform/sifive/common_fe310/include/platform.h create mode 100644 src/platform/sifive/common_fe310/sections.ld.sx create mode 100644 src/platform/sifive/fe310g002/build.mk create mode 100644 src/platform/sifive/fe310g002/config.mk create mode 100644 src/platform/sifive/fe310g002/include/plat_mem.h create mode 100644 src/projects/demo_riscv/build.mk create mode 100644 src/projects/demo_riscv/config.mk create mode 100644 src/projects/demo_riscv/project.c diff --git a/README.md b/README.md index 454e3381..eb507e72 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ Click the above icon to connect | Atmel | SiFive | STMicro | TI | Raspberry Pi | | ---------- | ----------- | ----------- | ----------- | -------------| -| ATMega328P | Coming soon | Coming soon | Coming soon | Coming soon | +| ATMega328P | FE310-G002 | Coming soon | Coming soon | Coming soon | | ATMega2560 | | | | | ### Projects @@ -38,8 +38,7 @@ $ make list ### Programming Languages * asm/assembly -* C -* Cpp/C++ +* C/C++ * makefile * linker script @@ -64,4 +63,4 @@ Host system requirements: Toolchain Requirements: * Please push your toolchains to your git repo * Update the links in mk/tc_get.mk file -* run ```make help``` for more details +* run ```$ make help``` for more details diff --git a/mk/elf.mk b/mk/elf.mk index 0df08068..b43ae239 100644 --- a/mk/elf.mk +++ b/mk/elf.mk @@ -33,7 +33,7 @@ $(ELF): $(DEP_LIBS) $(DEP_OBJS) $(LD_SCRIPT) $(LD_SUPPLEMENT) @cd $(@D); $(SIZE) $(@F) @echo "" -$(OUT)/%.ld: %.ld.sx +$(OUT)/%.ld: %.ld.sx | $$(@D)/ @echo "Elf: Preprocessing $(@F) ..." $(CC) $(CFLAGS) -E -P -o $@ $< diff --git a/src/arch/riscv/32/i/terravisor/arch.c b/src/arch/riscv/32/i/terravisor/arch.c new file mode 100644 index 00000000..9171222e --- /dev/null +++ b/src/arch/riscv/32/i/terravisor/arch.c @@ -0,0 +1,70 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2019, Cyancore Team + * + * File Name : arch.c + * Description : This file consists of architecture specific function that + * cannot be inlined. Hence, present in c file. + * Primary Author : Akash Kollipara [akashkollipara@gmail.com] + * Organisation : Cyancore Core-Team + */ + +#include +#include +#include + +/** + * arch_early_setup - This function is called in the early stages of boot + * + * @brief This function is responsible to clean reset cpu status/control registers. + * + */ +void arch_early_setup() +{ + arch_di(); + return; +} + +/** + * arch_setup - This function is called after initial setup is done + * + * @brief This function is called after initial setup is done. + */ +void arch_setup() +{ + return; +} + +void arch_di_save_state() +{ +} + +void arch_ei_restore_state() +{ +} + +/** + * arch_machine_call - perform machine call + * + * @brief This function executes function pointed by mcall. As the + * AVR core doesn't support multiple execution levels, this + * function emulates the machine call to maintain consistency. + * + * @param[in] code: machine call code + * @param[in] a0: first argument + * @param[in] a1: second argument + * @param[in] a2: third argument + * @param[in] *ret: return struct + */ +void arch_machine_call(unsigned int code, unsigned int a0, unsigned int a1, unsigned int a2, mret_t *ret) +{ + asm volatile("mv a0, %0" : : "r" (code)); + asm volatile("mv a1, %0" : : "r" (a0)); + asm volatile("mv a2, %0" : : "r" (a1)); + asm volatile("mv a3, %0" : : "r" (a2)); + asm volatile("ecall"); + asm volatile("mv %0, a0" : "=r" (ret->p)); + asm volatile("mv %0, a1" : "=r" (ret->size)); + asm volatile("mv %0, a2" : "=r" (ret->status)); + return; +} diff --git a/src/arch/riscv/32/i/terravisor/asm.S b/src/arch/riscv/32/i/terravisor/asm.S new file mode 100644 index 00000000..e69de29b diff --git a/src/arch/riscv/32/i/terravisor/build.mk b/src/arch/riscv/32/i/terravisor/build.mk new file mode 100644 index 00000000..0a8e902f --- /dev/null +++ b/src/arch/riscv/32/i/terravisor/build.mk @@ -0,0 +1,16 @@ +# +# CYANCORE LICENSE +# Copyrights (C) 2019, Cyancore Team +# +# File Name : build.mk +# Description : Build script for this directory. +# Primary Authod : Akash Kollipara [akashkollipara@gmail.com] +# Organisation : Cyancore Core-Team +# + +RV32I_T_ARCH_DIR := $(GET_PATH) + +$(eval $(call add_include,$(RV32I_T_ARCH_DIR)/include/)) + +DIR := $(RV32I_T_ARCH_DIR) +include mk/obj.mk diff --git a/src/arch/riscv/32/i/terravisor/entry.S b/src/arch/riscv/32/i/terravisor/entry.S new file mode 100644 index 00000000..a54d6972 --- /dev/null +++ b/src/arch/riscv/32/i/terravisor/entry.S @@ -0,0 +1,73 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2019, Cyancore Team + * + * File Name : entry.S + * Description : This file consists of entry point of the framework. + * After the reset of system, program in this file gets + * executed. + * Primary Author : Akash Kollipara [akashkollipara@gmail.com] + * Organisation : Cyancore Core-Team + */ + +#include +#include + +/** + * stack_start - Variable pointing to stack start assigned while linking + * + * @brief This variable is substituted by the linker with actual value of + * stack start. Please refer linker script for this variable assignment. + */ + +/** + * global_start - Variable pointing to global start assigned while linking + * + * @brief This variable is substituted by the linker with actual value of + * global start. Please refer linker script for this variable assignment. + * RISC-V CPU has global var symbol access range of gp +/- 2KB, total of 4KB. + * gp = + 0x800 + */ + +/** + * tls_start - Variable pointing to thread data start assigned while linking + * + * @brief This variable is substituted by the linker with actual value of + * tls start. Please refer linker script for this variable assignment. + */ + +/** + * Entry - Entry point of the framework + * + * @brief This is the entry point for the framework. After the reset interrupt + * is triggered, PC should be updated with the address of this function. + * This function initialises stack pointer and later it jumps to 'init', + * from here the framework boots. + */ +function entry +.option norvc +.option push +.option norelax + la gp, _global_start +.option pop + +stack_setup: + la a0, _stack_start + csrr a1, mhartid + mv t0, zero +1: /* Start of for loop, assume mul is not supported */ + beq t0, a1, 1f + li t1, STACK_SIZE_PCPU + add a0, a0, t1 + addi t0, t0, 1 + j 1b +1: + mv sp, a0 + la tp, _tls_start + mv a0, zero + mv a1, zero + mv t0, zero + mv t1, zero + +jump_to_init: + call init diff --git a/src/arch/riscv/32/i/terravisor/include/arch.h b/src/arch/riscv/32/i/terravisor/include/arch.h new file mode 100644 index 00000000..359dd26f --- /dev/null +++ b/src/arch/riscv/32/i/terravisor/include/arch.h @@ -0,0 +1,86 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2019, Cyancore Team + * + * File Name : arch.h + * Description : This file prototypes arch related functions and + * defines inline-able arch functions. + * Primary Author : Akash Kollipara [akashkollipara@gmail.com] + * Organisation : Cyancore Core-Team + */ + +#pragma once +#define _ARCH_H_ +#include + +/** + * arch_early_setup - This needs to be called in early stages of boot + */ +void arch_early_setup(); + +/** + * arch_setup - This needs to be called after inital setup is completed + */ +void arch_setup(); + +void arch_di_save_state(); +void arch_ei_restore_state(); + +/** + * arch_panic_handler - Executes when arch error occurs + */ +void arch_panic_handler(); + +/** + * arch_machine_call - Performs machine call + * + * Refer arch.c for more details. + */ +void arch_machine_call(unsigned int, unsigned int, unsigned int, unsigned int, mret_t *); + +/** + * arch_register_interrupt_handler - Registers interrtup handler + * for arch specific interrupt vectors + */ +void arch_register_interrupt_handler(unsigned int, void (*)(void)); + +/** + * arch_core_index - Returns code index (0, in case of AVR) + */ +static inline unsigned int arch_core_index() +{ + /* AVR only support uni core architecture */ + unsigned int id; + asm volatile("csrr %0, mhartid" : "=r" (id)); + return id; +} + +/** + * arch_ei - arch enable global interrupts + */ +static inline void arch_ei() +{ + asm volatile("csrsi mstatus, 3"); + asm volatile("csrsi mstatus, 7"); +} + +/** + * arch_di - arch disable global interrupts + */ +static inline void arch_di() +{ + asm volatile("csrw mstatus, zero"); +} + +static inline void arch_nop() +{ + asm volatile("nop"); +} + +static inline void arch_wfi() +{ + asm volatile("wfi"); +#if ERRATA_CIP578 + arch_nop(); +#endif +} diff --git a/src/arch/riscv/32/i/terravisor/init.c b/src/arch/riscv/32/i/terravisor/init.c new file mode 100644 index 00000000..06adf7cc --- /dev/null +++ b/src/arch/riscv/32/i/terravisor/init.c @@ -0,0 +1,31 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2019, Cyancore Team + * + * File Name : init.c + * Description : This file consists of init routine of the framework. + * Primary Author : Akash Kollipara [akashkollipara@gmail.com] + * Organisation : Cyancore Core-Team + */ + +#include +#include +#include +#include + +/** + * init - Entry point for the framework + * + * @brief This function runs to reset the cpu reg bank to architectrually + * reset state and jump to 'engine'. + */ +void init() +{ + arch_di(); + arch_ei(); + /* Boot framework */ + engine(); + + /* Accidental trap, if control returns from framework */ + exit(EXIT_FAILURE); +} diff --git a/src/arch/riscv/32/i/terravisor/interrupt_handler.c b/src/arch/riscv/32/i/terravisor/interrupt_handler.c new file mode 100644 index 00000000..e69de29b diff --git a/src/arch/riscv/32/i/terravisor/spinlock.S b/src/arch/riscv/32/i/terravisor/spinlock.S new file mode 100644 index 00000000..e69de29b diff --git a/src/arch/riscv/32/i/terravisor/stack.S b/src/arch/riscv/32/i/terravisor/stack.S new file mode 100644 index 00000000..e69de29b diff --git a/src/platform/sifive/common_fe310/build.mk b/src/platform/sifive/common_fe310/build.mk new file mode 100644 index 00000000..31dd43f2 --- /dev/null +++ b/src/platform/sifive/common_fe310/build.mk @@ -0,0 +1,23 @@ +# +# CYANCORE LICENSE +# Copyrights (C) 2019, Cyancore Team +# +# File Name : build.mk +# Description : This file accumulates sources from mega-avr +# common directory +# Primary Author : Akash Kollipara [akashkollipara@gmail.com] +# Organisation : Cyancore Core-Team +# + +FE310_COMMON_DIR := $(GET_PATH) + +LD_SCRIPT := $(FE310_COMMON_DIR)/sections.ld.sx + +PLAT_INCLUDE += $(FE310_COMMON_DIR)/include + +LOCAL_INTERRUPT_DEVICE := 0 +PLAT_INTERRUPT_DEVICE := 0 + +#include $(SIFIVE_COMMON_DIR)/arch/build.mk +#include $(SIFIVE_COMMON_DIR)/hal/build.mk +#include $(SIFIVE_COMMON_DIR)/platform/build.mk diff --git a/src/platform/sifive/common_fe310/include/platform.h b/src/platform/sifive/common_fe310/include/platform.h new file mode 100644 index 00000000..ac936f3e --- /dev/null +++ b/src/platform/sifive/common_fe310/include/platform.h @@ -0,0 +1,23 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2019, Cyancore Team + * + * File Name : platform.h + * Description : This file contains prototypes of platform apis + * Primary Author : Akash Kollipara [akashkollipara@gmail.com] + * Organisation : Cyancore Core-Team + */ + +#pragma once +#define _SIFIVE_PLATFORM_H_ + +#include + +void plat_panic_handler(); +status_t platform_copy_data(); +status_t platform_bss_clear(); +status_t platform_resources_setup(); +status_t platform_clk_reset(); +status_t platform_clk_en(unsigned int); +status_t platform_clk_dis(unsigned int); +status_t platform_wdt_reset(); diff --git a/src/platform/sifive/common_fe310/sections.ld.sx b/src/platform/sifive/common_fe310/sections.ld.sx new file mode 100644 index 00000000..6db4f16d --- /dev/null +++ b/src/platform/sifive/common_fe310/sections.ld.sx @@ -0,0 +1,115 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2019, Cyancore Team + * + * File Name : sections.ld.sx + * Description : This file contains memory layout for the + * cyancore framework + * Primary Author : Akash Kollipara [akashkollipara@gmail.com] + * Organisation : Cyancore Core-Team + */ + +#include + +#ifndef L_MEM_START +#define L_MEM_START 0 +#endif + +MEMORY +{ + /* VM Addresses */ + vma_itmem (airwx) : ORIGIN = V_ITMEM_START, LENGTH = ITMEM_LENGTH + vma_imem (arw!xi) : ORIGIN = V_IMEM_START, LENGTH = IMEM_LENGTH + vma_dmem (irx!aw) : ORIGIN = V_DMEM_START, LENGTH = DMEM_LENGTH + + /* LM Addresses */ + lma_mem : ORIGIN = L_MEM_START, LENGTH = L_MEM_LENGTH +} + +SECTIONS +{ + .text : + { + KEEP(*(.text.entry)) + *(.text) + *(.text.*) + KEEP(*(.text)) + } > vma_imem AT > lma_mem + + .rodata : + { + *(.version) + KEEP(*(.version)) + *(.rodata) + *(.rodata.*) + KEEP(*(.rodata)) + } > vma_imem AT > lma_mem + + .driver_table : {} > vma_imem AT > lma_mem + .mcall_table : {} > vma_imem AT > lma_mem + + .data : + { + *(.data) + *(.data.*) + KEEP(*(.data)) + } > vma_dmem AT > lma_mem + + .tdata : + { + *(.tdata) + *(.tdata.*) + KEEP(*(.tdata)) + } > vma_dmem AT > lma_mem + + .static_global_var : + { + } > vma_dmem + + .static_data_global : + { + *(.sdata) + *(.sdata.*) + KEEP(*(.sdata)) + } > vma_dmem AT > lma_mem + + .static_bss_global : + { + *(.sbss) + *(.sbss.*) + KEEP(*(.sbss)) + } > vma_dmem + + .bss : + { + *(.bss) + *(.bss.*) + KEEP(*(.bss)) + *(COMMON) + } > vma_dmem + + .stack : + { + *(.stack) + KEEP(*(.stack)) + } > vma_dmem + + PROVIDE(_data_start = LOADADDR(.data)); + PROVIDE(_data_size = SIZEOF(.data) + SIZEOF(.driver_table) + SIZEOF(.mcall_table)); + PROVIDE(_data_vstart = ADDR(.data)); + PROVIDE(_data_vend = _data_vstart + _data_size); + + PROVIDE(_tls_start = ADDR(.tdata)); + PROVIDE(_global_start = ADDR(.static_global_var) + 0x800); + PROVIDE(_stack_start = ADDR(.stack) + STACK_SIZE); + + PROVIDE(_bss_start = ADDR(.bss)); + PROVIDE(_bss_size = SIZEOF(.bss)); + PROVIDE(_flash_size = _data_size + SIZEOF(.text)); + PROVIDE(_ram_size = _bss_size + _data_size + SIZEOF(.stack)); + + ASSERT((_flash_size < FLASH_SIZE), "< x > Flash size exceeded ...") + ASSERT((_ram_size < RAM_SIZE), "< x > RAM size exceeded ...") + + /DISCARD/ : { *(.comment .trampolines) } +} diff --git a/src/platform/sifive/fe310g002/build.mk b/src/platform/sifive/fe310g002/build.mk new file mode 100644 index 00000000..32545e69 --- /dev/null +++ b/src/platform/sifive/fe310g002/build.mk @@ -0,0 +1,26 @@ +# +# CYANCORE LICENSE +# Copyrights (C) 2019, Cyancore Team +# +# File Name : build.mk +# Description : This file accumulates the build scripts from +# all other directories that have HiFive 1 Rev B +# board support sources +# Primary Author : Akash Kollipara [akashkollipara@gmail.com] +# Organisation : Cyancore Core-Team +# + +FE310G002_DIR := $(GET_PATH) + +ARCH := riscv +BIT := 32 +ARCH_VARIANT := i +TARGET_FLAGS += -march=rv32imac -mabi=ilp32 +PLAT_INCLUDE += $(FE310G002_DIR)/include +OUTPUT_FORMAT := elf32-littleriscv + +include $(FE310G002_DIR)/config.mk +#include $(FE310G002_DIR)/arch/build.mk +#include $(FE310G002_DIR)/platform/build.mk +#include $(FE310G002_DIR)/resources/build.mk +include $(FE310G002_DIR)/../common_fe310/build.mk diff --git a/src/platform/sifive/fe310g002/config.mk b/src/platform/sifive/fe310g002/config.mk new file mode 100644 index 00000000..3dfb30da --- /dev/null +++ b/src/platform/sifive/fe310g002/config.mk @@ -0,0 +1,59 @@ +# +# CYANCORE LICENSE +# Copyrights (C) 2019, Cyancore Team +# +# File Name : config.mk +# Description : This file defines configuration for HiFive 1B +# Primary Author : Akash Kollipara [akashkollipara@gmail.com] +# Organisation : Cyancore Core-Team +# + +#====================================================================== +# Configuration file for Platforms +#====================================================================== + +#====================================================================== +# Platform Configuration +# Do not alter below FLAGS unless explicitly mentioned +#====================================================================== +N_CORES := 1 +$(eval $(call add_define,N_CORES)) + +$(eval $(call add_define,BIT)) + +BOOT_CORE_ID:= 0 +$(eval $(call add_define,BOOT_CORE_ID)) + +# Call this FLAG from Project config file if needed +FCLK ?= 16000000 +$(eval $(call add_define,FCLK)) + +N_IRQ := 25 +$(eval $(call add_define,N_IRQ)) + +MAX_INTERRUPTS_PER_DEVICE := 2 +$(eval $(call add_define,MAX_INTERRUPTS_PER_DEVICE)) + +USE_SPINLOCK ?= 1 +$(eval $(call add_define,USE_SPINLOCK)) + +#====================================================================== +# MEMBUF Configuration +#====================================================================== +MEMBUF_SIZE ?= 1024 +$(eval $(call add_define,MEMBUF_SIZE)) +#====================================================================== + +#====================================================================== +# GPIO Configuration +#====================================================================== +N_PORT := 3 +$(eval $(call add_define,N_PORT)) +#====================================================================== + +#====================================================================== +# Errata CIP-578 +#====================================================================== +ERRATA_CIP578 := 1 +$(eval $(call add_define,ERRATA_CIP578)) +#====================================================================== diff --git a/src/platform/sifive/fe310g002/include/plat_mem.h b/src/platform/sifive/fe310g002/include/plat_mem.h new file mode 100644 index 00000000..2403e73c --- /dev/null +++ b/src/platform/sifive/fe310g002/include/plat_mem.h @@ -0,0 +1,30 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2019, Cyancore Team + * + * File Name : plat_mem.h + * Description : This file contains memory config of fe310-g002 + * Primary Author : Akash Kollipara [akashkollipara@gmail.com] + * Organisation : Cyancore Core-Team + */ + +#pragma once + +#define FLASH_SIZE (4M-64K) +#define RAM_SIZE 16K + +#define V_ITMEM_START 0x08000000 +#define V_IMEM_START 0x20010000 +#define V_DMEM_START 0x80000000 + +#define L_MEM_START 0x20010000 +#define L_MEM_LENGTH 0x003f0000 + +#define ITMEM_LENGTH 0x00002000 +#define IMEM_LENGTH 0x20000000 +#define DMEM_LENGTH 0x00004000 + +#define ALIGN_BOUND 4 + +#define STACK_SIZE 0xc00 +#define STACK_SIZE_PCPU 0xc00 diff --git a/src/projects/demo_riscv/build.mk b/src/projects/demo_riscv/build.mk new file mode 100644 index 00000000..7e0fe7b4 --- /dev/null +++ b/src/projects/demo_riscv/build.mk @@ -0,0 +1,21 @@ +# +# CYANCORE LICENSE +# Copyrights (C) 2019, Cyancore Team +# +# File Name : build.mk +# Description : This file build project sources and specifies +# project properties +# Primary Author : Akash Kollipara [akashkollipara@gmail.com] +# Organisation : Cyancore Core-Team +# + +PROJECT_DIR := $(GET_PATH) + +OPTIMIZATION := s + +EXE_MODE := terravisor + +include $(PROJECT_DIR)/config.mk + +DIR := $(PROJECT_DIR) +include mk/obj.mk diff --git a/src/projects/demo_riscv/config.mk b/src/projects/demo_riscv/config.mk new file mode 100644 index 00000000..1d2ce83e --- /dev/null +++ b/src/projects/demo_riscv/config.mk @@ -0,0 +1,16 @@ +# +# CYANCORE LICENSE +# Copyrights (C) 2019, Cyancore Team +# +# File Name : config.mk +# Description : This file consists of project config +# Primary Author : Akash Kollipara [akashkollipara@gmail.com] +# Organisation : Cyancore Core-Team +# + +COMPILER := gcc +TC_VER := 8.2.0 +FAMILY := sifive +PLATFORM := fe310g002 +EARLYCON_SERIAL := 0 +CONSOLE_SERIAL := 0 diff --git a/src/projects/demo_riscv/project.c b/src/projects/demo_riscv/project.c new file mode 100644 index 00000000..596c6d1f --- /dev/null +++ b/src/projects/demo_riscv/project.c @@ -0,0 +1,18 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2019, Cyancore Team + * + * File Name : project.c + * Description : This file consists of project srouces + * Primary Author : Akash Kollipara [akashkollipara@gmail.com] + * Organisation : Cyancore Core-Team + */ + + +void plug() +{ +} + +void play() +{ +} From 7a953c6e219751384fe0e849d3f4c4369c2668a3 Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Mon, 27 Dec 2021 13:22:19 +0530 Subject: [PATCH 05/31] Added exception handler - Added exception handler support - Updated arch file and minor fixes - Updated project and platform files --- src/arch/riscv/32/i/terravisor/arch.c | 1 + src/arch/riscv/32/i/terravisor/asm.S | 75 +++++++++++++++++++ src/arch/riscv/32/i/terravisor/entry.S | 3 +- .../riscv/32/i/terravisor/exception_handler.c | 16 ++++ src/arch/riscv/32/i/terravisor/include/arch.h | 8 +- .../riscv/32/i/terravisor/include/riscv.h | 25 +++++++ src/arch/riscv/32/i/terravisor/init.c | 3 +- .../riscv/32/i/terravisor/interrupt_handler.c | 0 src/arch/riscv/32/i/terravisor/stack.S | 0 .../sifive/common_fe310/sections.ld.sx | 3 + src/projects/demo_riscv/config.mk | 4 +- src/projects/demo_riscv/project.c | 1 - 12 files changed, 130 insertions(+), 9 deletions(-) create mode 100644 src/arch/riscv/32/i/terravisor/exception_handler.c create mode 100644 src/arch/riscv/32/i/terravisor/include/riscv.h delete mode 100644 src/arch/riscv/32/i/terravisor/interrupt_handler.c delete mode 100644 src/arch/riscv/32/i/terravisor/stack.S diff --git a/src/arch/riscv/32/i/terravisor/arch.c b/src/arch/riscv/32/i/terravisor/arch.c index 9171222e..420f6f83 100644 --- a/src/arch/riscv/32/i/terravisor/arch.c +++ b/src/arch/riscv/32/i/terravisor/arch.c @@ -22,6 +22,7 @@ void arch_early_setup() { arch_di(); + riscv_update_vector(); return; } diff --git a/src/arch/riscv/32/i/terravisor/asm.S b/src/arch/riscv/32/i/terravisor/asm.S index e69de29b..c3fa0b69 100644 --- a/src/arch/riscv/32/i/terravisor/asm.S +++ b/src/arch/riscv/32/i/terravisor/asm.S @@ -0,0 +1,75 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2019, Cyancore Team + * + * File Name : asm.S + * Description : This file consists of all the function written in asm + * like ISR, context management, zero_reg and panic handler + * Primary Author : Akash Kollipara [akashkollipara@gmail.com] + * Organisation : Cyancore Core-Team + */ + + #include + +.macro PROLOGUE + addi sp, sp, -96 // - size(context_frame) * 4 + sw t6, 68(sp) + sw t5, 64(sp) + sw t4, 60(sp) + sw t3, 56(sp) + sw a7, 52(sp) + sw a6, 48(sp) + sw a5, 44(sp) + sw a4, 40(sp) + sw a3, 36(sp) + sw a2, 32(sp) + sw a1, 28(sp) + sw a0, 24(sp) + sw t2, 20(sp) + sw t1, 16(sp) + sw t0, 12(sp) + sw ra, 8(sp) + csrr t0, mstatus + sw t0, 4(sp) + csrr t0, mepc + sw t0, 0(sp) +.endm + +.macro EPILOGUE + lw t0, 0(sp) + csrw mepc, t0 + lw t0, 4(sp) + csrw mstatus, t0 + lw ra, 8(sp) + lw t0, 12(sp) + lw t1, 16(sp) + lw t2, 20(sp) + lw a0, 24(sp) + lw a1, 28(sp) + lw a2, 32(sp) + lw a3, 36(sp) + lw a4, 40(sp) + lw a5, 44(sp) + lw a6, 48(sp) + lw a7, 52(sp) + lw t3, 56(sp) + lw t4, 60(sp) + lw t5, 64(sp) + lw t6, 68(sp) + addi sp, sp, 96 // size(context_frame) * 4 +.endm + +function exception_handler_base +.p2align 2 + PROLOGUE + csrr a0, mcause + mv a1, sp + call exception_handler + EPILOGUE + mret + +function riscv_update_vector + la a0, exception_handler_base + csrw mtvec, a0 + mv a0, zero + ret diff --git a/src/arch/riscv/32/i/terravisor/entry.S b/src/arch/riscv/32/i/terravisor/entry.S index a54d6972..b76cb04c 100644 --- a/src/arch/riscv/32/i/terravisor/entry.S +++ b/src/arch/riscv/32/i/terravisor/entry.S @@ -63,11 +63,10 @@ stack_setup: j 1b 1: mv sp, a0 - la tp, _tls_start mv a0, zero mv a1, zero mv t0, zero mv t1, zero jump_to_init: - call init + j init diff --git a/src/arch/riscv/32/i/terravisor/exception_handler.c b/src/arch/riscv/32/i/terravisor/exception_handler.c new file mode 100644 index 00000000..b8bbf4bc --- /dev/null +++ b/src/arch/riscv/32/i/terravisor/exception_handler.c @@ -0,0 +1,16 @@ + +#include +#include +#include +#include + +void exception_handler(uint32_t mcause, context_frame_t *frame) +{ + printf("Cause: %u -- EPC: %u", mcause, frame->mepc); + frame->a0 = 1; + frame->a1 = 2; + frame->a2 = 3; + frame->a3 = 0; + asm("nop"); + frame->mepc += (MMIO8(frame->mepc) & 0x3) ? 4 : 2; +} diff --git a/src/arch/riscv/32/i/terravisor/include/arch.h b/src/arch/riscv/32/i/terravisor/include/arch.h index 359dd26f..5943b8fe 100644 --- a/src/arch/riscv/32/i/terravisor/include/arch.h +++ b/src/arch/riscv/32/i/terravisor/include/arch.h @@ -12,6 +12,7 @@ #pragma once #define _ARCH_H_ #include +#include /** * arch_early_setup - This needs to be called in early stages of boot @@ -60,8 +61,8 @@ static inline unsigned int arch_core_index() */ static inline void arch_ei() { - asm volatile("csrsi mstatus, 3"); - asm volatile("csrsi mstatus, 7"); + unsigned int bits = (1 << 3) | (1 << 7); + asm volatile("csrs mstatus, %0" : : "r" (bits)); } /** @@ -69,7 +70,8 @@ static inline void arch_ei() */ static inline void arch_di() { - asm volatile("csrw mstatus, zero"); + unsigned int bits = (1 << 3) | (1 << 7); + asm volatile("csrc mstatus, %0" : : "r" (bits)); } static inline void arch_nop() diff --git a/src/arch/riscv/32/i/terravisor/include/riscv.h b/src/arch/riscv/32/i/terravisor/include/riscv.h new file mode 100644 index 00000000..4f25837e --- /dev/null +++ b/src/arch/riscv/32/i/terravisor/include/riscv.h @@ -0,0 +1,25 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2019, Cyancore Team + * + * File Name : riscv.h + * Description : This file prototypes riscv arch related functions and + * structures. + * Primary Author : Akash Kollipara [akashkollipara@gmail.com] + * Organisation : Cyancore Core-Team + */ + +#pragma once +#define _RISC_V_ +#include + +typedef struct context_frame +{ + uint32_t mepc, mstatus, ra, t0, + t1, t2, a0, a1, a2, a3, + a4, a5, a6, a7, t3, t4, + t5, t6, pad[6]; + /* Padding is necessary for alignment */ +} context_frame_t; + +void riscv_update_vector(); diff --git a/src/arch/riscv/32/i/terravisor/init.c b/src/arch/riscv/32/i/terravisor/init.c index 06adf7cc..86bcd954 100644 --- a/src/arch/riscv/32/i/terravisor/init.c +++ b/src/arch/riscv/32/i/terravisor/init.c @@ -9,6 +9,7 @@ */ #include +#include #include #include #include @@ -22,7 +23,7 @@ void init() { arch_di(); - arch_ei(); + asm volatile("la tp, _tls_start"); /* Boot framework */ engine(); diff --git a/src/arch/riscv/32/i/terravisor/interrupt_handler.c b/src/arch/riscv/32/i/terravisor/interrupt_handler.c deleted file mode 100644 index e69de29b..00000000 diff --git a/src/arch/riscv/32/i/terravisor/stack.S b/src/arch/riscv/32/i/terravisor/stack.S deleted file mode 100644 index e69de29b..00000000 diff --git a/src/platform/sifive/common_fe310/sections.ld.sx b/src/platform/sifive/common_fe310/sections.ld.sx index 6db4f16d..445b2b6a 100644 --- a/src/platform/sifive/common_fe310/sections.ld.sx +++ b/src/platform/sifive/common_fe310/sections.ld.sx @@ -26,6 +26,8 @@ MEMORY lma_mem : ORIGIN = L_MEM_START, LENGTH = L_MEM_LENGTH } +ENTRY(entry) + SECTIONS { .text : @@ -38,6 +40,7 @@ SECTIONS .rodata : { + . = ALIGN(8); *(.version) KEEP(*(.version)) *(.rodata) diff --git a/src/projects/demo_riscv/config.mk b/src/projects/demo_riscv/config.mk index 1d2ce83e..67c0a2bd 100644 --- a/src/projects/demo_riscv/config.mk +++ b/src/projects/demo_riscv/config.mk @@ -12,5 +12,5 @@ COMPILER := gcc TC_VER := 8.2.0 FAMILY := sifive PLATFORM := fe310g002 -EARLYCON_SERIAL := 0 -CONSOLE_SERIAL := 0 +EARLYCON_MEMBUF := 1 +CONSOLE_MEMBUF := 0 diff --git a/src/projects/demo_riscv/project.c b/src/projects/demo_riscv/project.c index 596c6d1f..6d58ba75 100644 --- a/src/projects/demo_riscv/project.c +++ b/src/projects/demo_riscv/project.c @@ -8,7 +8,6 @@ * Organisation : Cyancore Core-Team */ - void plug() { } From 00e3dd721a1b8723745c0c19b613a6b352ae7366 Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Mon, 27 Dec 2021 13:24:17 +0530 Subject: [PATCH 06/31] Updates to support riscv - Updated jenkins build flow - bug fix in elf.mk --- jenkins.pipe | 3 +++ mk/elf.mk | 4 +++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/jenkins.pipe b/jenkins.pipe index 1970d0bf..1346583b 100644 --- a/jenkins.pipe +++ b/jenkins.pipe @@ -5,18 +5,21 @@ pipeline { stage('tc check') { steps { sh 'make get_avr_tc AVR_TC_REPO=git@github.com:VisorFolks/avr-toolchain.git' + sh 'make get_riscv_tc RISC_V_TC_REPO=git@github.com:VisorFolks/risc-v-toolchain.git' } } stage('check') { steps { sh 'make demo_avr check' sh 'make demo_avr_cpp check' + sh 'make demo_riscv check' } } stage('build') { steps { sh 'make demo_avr clean default' sh 'make demo_avr_cpp clean default' + sh 'make demo_riscv clean default' } } stage('clean') { diff --git a/mk/elf.mk b/mk/elf.mk index b43ae239..b3606491 100644 --- a/mk/elf.mk +++ b/mk/elf.mk @@ -33,7 +33,9 @@ $(ELF): $(DEP_LIBS) $(DEP_OBJS) $(LD_SCRIPT) $(LD_SUPPLEMENT) @cd $(@D); $(SIZE) $(@F) @echo "" -$(OUT)/%.ld: %.ld.sx | $$(@D)/ +.SECONDEXPANSION: +$(OUT)/%.ld: %.ld.sx + mkdir -p $(@D) @echo "Elf: Preprocessing $(@F) ..." $(CC) $(CFLAGS) -E -P -o $@ $< From 8e87cb5dacb56779b560033b17731f106e0c8af1 Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Mon, 27 Dec 2021 20:08:54 +0530 Subject: [PATCH 07/31] Removed unused functions --- .../8/common_5x_6/terravisor/include/arch.h | 25 ------------------- 1 file changed, 25 deletions(-) diff --git a/src/arch/avr/8/common_5x_6/terravisor/include/arch.h b/src/arch/avr/8/common_5x_6/terravisor/include/arch.h index 57b89c22..17384a27 100644 --- a/src/arch/avr/8/common_5x_6/terravisor/include/arch.h +++ b/src/arch/avr/8/common_5x_6/terravisor/include/arch.h @@ -89,28 +89,3 @@ static inline void arch_nop() { asm volatile("nop"); } - -static inline void arch_sync() -{ - arch_nop(); -} - -static inline void arch_msb() -{ - arch_nop(); -} - -static inline void arch_isb() -{ - arch_nop(); -} - -static inline void arch_dsb() -{ - arch_nop(); -} - -static inline void arch_wfe() -{ - arch_nop(); -} From bc2cd468a7e12f18d3b096c96318f0a3fd61f4c5 Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Wed, 29 Dec 2021 00:09:43 +0530 Subject: [PATCH 08/31] Added exception handler support - Updated Sifive E310 platform support - Added RISC-V I exception handling support --- src/arch/riscv/32/i/terravisor/arch.c | 12 +++++ src/arch/riscv/32/i/terravisor/asm.S | 9 ++-- .../riscv/32/i/terravisor/exception_handler.c | 47 ++++++++++++++++--- src/arch/riscv/32/i/terravisor/include/arch.h | 9 +++- .../riscv/32/i/terravisor/include/riscv.h | 2 + .../sifive/common_fe310/arch/build.mk | 14 ++++++ src/platform/sifive/common_fe310/arch/panic.S | 15 ++++++ src/platform/sifive/common_fe310/build.mk | 6 +-- src/platform/sifive/fe310g002/config.mk | 5 +- src/projects/demo_riscv/config.mk | 2 +- 10 files changed, 104 insertions(+), 17 deletions(-) create mode 100644 src/platform/sifive/common_fe310/arch/build.mk create mode 100644 src/platform/sifive/common_fe310/arch/panic.S diff --git a/src/arch/riscv/32/i/terravisor/arch.c b/src/arch/riscv/32/i/terravisor/arch.c index 420f6f83..6141c572 100644 --- a/src/arch/riscv/32/i/terravisor/arch.c +++ b/src/arch/riscv/32/i/terravisor/arch.c @@ -69,3 +69,15 @@ void arch_machine_call(unsigned int code, unsigned int a0, unsigned int a1, unsi asm volatile("mv %0, a2" : "=r" (ret->status)); return; } + +_WEAK void arch_panic_handler(context_frame_t *frame _UNUSED) +{ + while(1) + arch_wfi(); +} + +_WEAK void arch_unhandled_irq() +{ + while(1) + arch_wfi(); +} diff --git a/src/arch/riscv/32/i/terravisor/asm.S b/src/arch/riscv/32/i/terravisor/asm.S index c3fa0b69..bcb89de3 100644 --- a/src/arch/riscv/32/i/terravisor/asm.S +++ b/src/arch/riscv/32/i/terravisor/asm.S @@ -9,10 +9,10 @@ * Organisation : Cyancore Core-Team */ - #include +#include .macro PROLOGUE - addi sp, sp, -96 // - size(context_frame) * 4 + addi sp, sp, -96 /* - size(context_frame) * 4 */ sw t6, 68(sp) sw t5, 64(sp) sw t4, 60(sp) @@ -56,11 +56,11 @@ lw t4, 60(sp) lw t5, 64(sp) lw t6, 68(sp) - addi sp, sp, 96 // size(context_frame) * 4 + addi sp, sp, 96 /* size(context_frame) * 4 */ .endm function exception_handler_base -.p2align 2 +.p2align 2 /* Needs 4Byte Alignment, refer mtvec reg */ PROLOGUE csrr a0, mcause mv a1, sp @@ -72,4 +72,5 @@ function riscv_update_vector la a0, exception_handler_base csrw mtvec, a0 mv a0, zero + fence.i ret diff --git a/src/arch/riscv/32/i/terravisor/exception_handler.c b/src/arch/riscv/32/i/terravisor/exception_handler.c index b8bbf4bc..e224b45e 100644 --- a/src/arch/riscv/32/i/terravisor/exception_handler.c +++ b/src/arch/riscv/32/i/terravisor/exception_handler.c @@ -1,16 +1,49 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2019, Cyancore Team + * + * File Name : exception_handler.c + * Description : This file consists of sources for exception + * handling. + * Primary Author : Akash Kollipara [akashkollipara@gmail.com] + * Organisation : Cyancore Core-Team + */ #include -#include +#include +#include #include #include +static void (* exhandler[N_CORES][N_EXCEP])(context_frame_t *) = {{[0 ... N_EXCEP-1] = arch_panic_handler}}; +static void (* irqhandler[N_CORES][N_IRQ])(void) = {{[0 ... N_IRQ-1] = arch_unhandled_irq}}; + +void arch_register_exception_handler(unsigned int id, void (*handler)(context_frame_t *)) +{ + unsigned int cpuid = arch_core_index(); + assert(id < N_EXCEP); + exhandler[cpuid][id] = handler; + fence(ow, ow); +} + +void arch_register_interrupt_handler(unsigned int id, void (*handler)(void)) +{ + unsigned int cpuid = arch_core_index(); + assert(id < N_IRQ); + irqhandler[cpuid][id] = handler; + fence(ow, ow); +} + void exception_handler(uint32_t mcause, context_frame_t *frame) { - printf("Cause: %u -- EPC: %u", mcause, frame->mepc); - frame->a0 = 1; - frame->a1 = 2; - frame->a2 = 3; - frame->a3 = 0; - asm("nop"); + unsigned int cause = mcause & ~(1U << 31); + unsigned int cpuid = arch_core_index(); + + if(mcause & (1U << 31)) + irqhandler[cpuid][cause](); + else + exhandler[cpuid][cause](frame); + frame->mepc += (MMIO8(frame->mepc) & 0x3) ? 4 : 2; + fence(ow, ow); } diff --git a/src/arch/riscv/32/i/terravisor/include/arch.h b/src/arch/riscv/32/i/terravisor/include/arch.h index 5943b8fe..69c2a528 100644 --- a/src/arch/riscv/32/i/terravisor/include/arch.h +++ b/src/arch/riscv/32/i/terravisor/include/arch.h @@ -30,7 +30,8 @@ void arch_ei_restore_state(); /** * arch_panic_handler - Executes when arch error occurs */ -void arch_panic_handler(); +void arch_panic_handler(context_frame_t *); +void arch_unhandled_irq(); /** * arch_machine_call - Performs machine call @@ -45,6 +46,12 @@ void arch_machine_call(unsigned int, unsigned int, unsigned int, unsigned int */ void arch_register_interrupt_handler(unsigned int, void (*)(void)); +/** + * arch_register_exception_handler - Registers exception handler + * for arch specific exception vectors + */ +void arch_register_exception_handler(unsigned int, void (*)(context_frame_t *)); + /** * arch_core_index - Returns code index (0, in case of AVR) */ diff --git a/src/arch/riscv/32/i/terravisor/include/riscv.h b/src/arch/riscv/32/i/terravisor/include/riscv.h index 4f25837e..67dc4002 100644 --- a/src/arch/riscv/32/i/terravisor/include/riscv.h +++ b/src/arch/riscv/32/i/terravisor/include/riscv.h @@ -13,6 +13,8 @@ #define _RISC_V_ #include +#define fence(predecessor, successor) asm volatile("fence " #predecessor ", " #successor ::: "memory") + typedef struct context_frame { uint32_t mepc, mstatus, ra, t0, diff --git a/src/platform/sifive/common_fe310/arch/build.mk b/src/platform/sifive/common_fe310/arch/build.mk new file mode 100644 index 00000000..ef63bb16 --- /dev/null +++ b/src/platform/sifive/common_fe310/arch/build.mk @@ -0,0 +1,14 @@ +# +# CYANCORE LICENSE +# Copyrights (C) 2019, Cyancore Team +# +# File Name : build.mk +# Description : This file accumulates the build fe310 series +# platform arch sources +# Primary Author : Akash Kollipara [akashkollipara@gmail.com] +# Organisation : Cyancore Core-Team +# + +DIR := $(GET_PATH) + +include mk/obj.mk diff --git a/src/platform/sifive/common_fe310/arch/panic.S b/src/platform/sifive/common_fe310/arch/panic.S new file mode 100644 index 00000000..4ede533b --- /dev/null +++ b/src/platform/sifive/common_fe310/arch/panic.S @@ -0,0 +1,15 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2019, Cyancore Team + * + * File Name : panic.S + * Description : This file contains platform panic handler + * Primary Author : Akash Kollipara [akashkollipara@gmail.com] + * Organisation : Cyancore Core-Team + */ + +#include + +function plat_panic_handler + wfi + j plat_panic_handler diff --git a/src/platform/sifive/common_fe310/build.mk b/src/platform/sifive/common_fe310/build.mk index 31dd43f2..8ad59cec 100644 --- a/src/platform/sifive/common_fe310/build.mk +++ b/src/platform/sifive/common_fe310/build.mk @@ -18,6 +18,6 @@ PLAT_INCLUDE += $(FE310_COMMON_DIR)/include LOCAL_INTERRUPT_DEVICE := 0 PLAT_INTERRUPT_DEVICE := 0 -#include $(SIFIVE_COMMON_DIR)/arch/build.mk -#include $(SIFIVE_COMMON_DIR)/hal/build.mk -#include $(SIFIVE_COMMON_DIR)/platform/build.mk +include $(FE310_COMMON_DIR)/arch/build.mk +#include $(FE310_COMMON_DIR)/hal/build.mk +#include $(FE310_COMMON_DIR)/platform/build.mk diff --git a/src/platform/sifive/fe310g002/config.mk b/src/platform/sifive/fe310g002/config.mk index 3dfb30da..fe4da77d 100644 --- a/src/platform/sifive/fe310g002/config.mk +++ b/src/platform/sifive/fe310g002/config.mk @@ -28,7 +28,10 @@ $(eval $(call add_define,BOOT_CORE_ID)) FCLK ?= 16000000 $(eval $(call add_define,FCLK)) -N_IRQ := 25 +N_EXCEP := 12 +$(eval $(call add_define,N_EXCEP)) + +N_IRQ := 12 $(eval $(call add_define,N_IRQ)) MAX_INTERRUPTS_PER_DEVICE := 2 diff --git a/src/projects/demo_riscv/config.mk b/src/projects/demo_riscv/config.mk index 67c0a2bd..09b31fed 100644 --- a/src/projects/demo_riscv/config.mk +++ b/src/projects/demo_riscv/config.mk @@ -12,5 +12,5 @@ COMPILER := gcc TC_VER := 8.2.0 FAMILY := sifive PLATFORM := fe310g002 -EARLYCON_MEMBUF := 1 +EARLYCON_MEMBUF := 0 CONSOLE_MEMBUF := 0 From c054b5cb15d3da81e4213a445b796cb4d66aca5d Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Wed, 29 Dec 2021 00:53:25 +0530 Subject: [PATCH 09/31] Add arch suppot --- src/arch/riscv/32/imac/terravisor/build.mk | 16 ++++++++++++++++ src/platform/sifive/fe310g002/build.mk | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 src/arch/riscv/32/imac/terravisor/build.mk diff --git a/src/arch/riscv/32/imac/terravisor/build.mk b/src/arch/riscv/32/imac/terravisor/build.mk new file mode 100644 index 00000000..711b24ef --- /dev/null +++ b/src/arch/riscv/32/imac/terravisor/build.mk @@ -0,0 +1,16 @@ +# +# CYANCORE LICENSE +# Copyrights (C) 2019, Cyancore Team +# +# File Name : build.mk +# Description : Build script for this directory. +# Primary Authod : Akash Kollipara [akashkollipara@gmail.com] +# Organisation : Cyancore Core-Team +# + +RV32IMAC_T_ARCH_DIR := $(GET_PATH) + +include $(RV32IMAC_T_ARCH_DIR)/../../i/terravisor/build.mk + +DIR := $(RV32IMAC_T_ARCH_DIR) +include mk/obj.mk diff --git a/src/platform/sifive/fe310g002/build.mk b/src/platform/sifive/fe310g002/build.mk index 32545e69..53313dcb 100644 --- a/src/platform/sifive/fe310g002/build.mk +++ b/src/platform/sifive/fe310g002/build.mk @@ -14,7 +14,7 @@ FE310G002_DIR := $(GET_PATH) ARCH := riscv BIT := 32 -ARCH_VARIANT := i +ARCH_VARIANT := imac TARGET_FLAGS += -march=rv32imac -mabi=ilp32 PLAT_INCLUDE += $(FE310G002_DIR)/include OUTPUT_FORMAT := elf32-littleriscv From f6cb61898b9b45419f82c474795273610c42ee67 Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Wed, 29 Dec 2021 14:46:27 +0530 Subject: [PATCH 10/31] Updated interrupts and CLINT architecture - Updated arch exception and interrupt handlers - Added support for CLINT IRQs only - Updated platform files --- src/arch/riscv/32/i/terravisor/arch.c | 3 ++- .../riscv/32/i/terravisor/exception_handler.c | 24 +++++++++++++++---- src/arch/riscv/32/i/terravisor/include/arch.h | 10 ++++---- .../riscv/32/i/terravisor/include/riscv.h | 1 + src/driver/interrupt/interrupt.c | 3 --- src/platform/sifive/common_fe310/build.mk | 2 +- 6 files changed, 29 insertions(+), 14 deletions(-) diff --git a/src/arch/riscv/32/i/terravisor/arch.c b/src/arch/riscv/32/i/terravisor/arch.c index 6141c572..72054857 100644 --- a/src/arch/riscv/32/i/terravisor/arch.c +++ b/src/arch/riscv/32/i/terravisor/arch.c @@ -70,8 +70,9 @@ void arch_machine_call(unsigned int code, unsigned int a0, unsigned int a1, unsi return; } -_WEAK void arch_panic_handler(context_frame_t *frame _UNUSED) +_WEAK void arch_panic_handler() { + context_frame_t *frame _UNUSED = get_context_frame(); while(1) arch_wfi(); } diff --git a/src/arch/riscv/32/i/terravisor/exception_handler.c b/src/arch/riscv/32/i/terravisor/exception_handler.c index e224b45e..f9c14c63 100644 --- a/src/arch/riscv/32/i/terravisor/exception_handler.c +++ b/src/arch/riscv/32/i/terravisor/exception_handler.c @@ -15,10 +15,24 @@ #include #include -static void (* exhandler[N_CORES][N_EXCEP])(context_frame_t *) = {{[0 ... N_EXCEP-1] = arch_panic_handler}}; +static void (* exhandler[N_CORES][N_EXCEP])(void) = {{[0 ... N_EXCEP-1] = arch_panic_handler}}; static void (* irqhandler[N_CORES][N_IRQ])(void) = {{[0 ... N_IRQ-1] = arch_unhandled_irq}}; +static context_frame_t *local_frame[N_CORES]; -void arch_register_exception_handler(unsigned int id, void (*handler)(context_frame_t *)) +static void set_context_frame(context_frame_t *frame) +{ + unsigned int cpuid = arch_core_index(); + local_frame[cpuid] = frame; + fence(w, w); +} + +context_frame_t *get_context_frame(void) +{ + unsigned int cpuid = arch_core_index(); + return local_frame[cpuid]; +} + +void arch_register_interrupt_handler(unsigned int id, void (*handler)(void)) { unsigned int cpuid = arch_core_index(); assert(id < N_EXCEP); @@ -26,7 +40,7 @@ void arch_register_exception_handler(unsigned int id, void (*handler)(context_fr fence(ow, ow); } -void arch_register_interrupt_handler(unsigned int id, void (*handler)(void)) +void local_register_interrupt_handler(unsigned int id, void (*handler)(void)) { unsigned int cpuid = arch_core_index(); assert(id < N_IRQ); @@ -39,10 +53,12 @@ void exception_handler(uint32_t mcause, context_frame_t *frame) unsigned int cause = mcause & ~(1U << 31); unsigned int cpuid = arch_core_index(); + set_context_frame(frame); + if(mcause & (1U << 31)) irqhandler[cpuid][cause](); else - exhandler[cpuid][cause](frame); + exhandler[cpuid][cause](); frame->mepc += (MMIO8(frame->mepc) & 0x3) ? 4 : 2; fence(ow, ow); diff --git a/src/arch/riscv/32/i/terravisor/include/arch.h b/src/arch/riscv/32/i/terravisor/include/arch.h index 69c2a528..33e39b3e 100644 --- a/src/arch/riscv/32/i/terravisor/include/arch.h +++ b/src/arch/riscv/32/i/terravisor/include/arch.h @@ -30,7 +30,7 @@ void arch_ei_restore_state(); /** * arch_panic_handler - Executes when arch error occurs */ -void arch_panic_handler(context_frame_t *); +void arch_panic_handler(); void arch_unhandled_irq(); /** @@ -42,15 +42,15 @@ void arch_machine_call(unsigned int, unsigned int, unsigned int, unsigned int /** * arch_register_interrupt_handler - Registers interrtup handler - * for arch specific interrupt vectors + * for arch specific exception vectors */ void arch_register_interrupt_handler(unsigned int, void (*)(void)); /** - * arch_register_exception_handler - Registers exception handler - * for arch specific exception vectors + * local_register_interrupt_handler - Registers interrupt handler + * for cpu specific interrupt vectors */ -void arch_register_exception_handler(unsigned int, void (*)(context_frame_t *)); +void local_register_interrupt_handler(unsigned int, void (*)(void)); /** * arch_core_index - Returns code index (0, in case of AVR) diff --git a/src/arch/riscv/32/i/terravisor/include/riscv.h b/src/arch/riscv/32/i/terravisor/include/riscv.h index 67dc4002..8a11b0d8 100644 --- a/src/arch/riscv/32/i/terravisor/include/riscv.h +++ b/src/arch/riscv/32/i/terravisor/include/riscv.h @@ -25,3 +25,4 @@ typedef struct context_frame } context_frame_t; void riscv_update_vector(); +context_frame_t *get_context_frame(); diff --git a/src/driver/interrupt/interrupt.c b/src/driver/interrupt/interrupt.c index f799e8bf..80d7bc14 100644 --- a/src/driver/interrupt/interrupt.c +++ b/src/driver/interrupt/interrupt.c @@ -13,9 +13,6 @@ #include #include #include -#if LOCAL_INTERRUPT_DEVICE==1 -#include -#endif #if PLAT_INTERRUPT_DEVICE==1 #include #endif diff --git a/src/platform/sifive/common_fe310/build.mk b/src/platform/sifive/common_fe310/build.mk index 8ad59cec..f02c4320 100644 --- a/src/platform/sifive/common_fe310/build.mk +++ b/src/platform/sifive/common_fe310/build.mk @@ -15,7 +15,7 @@ LD_SCRIPT := $(FE310_COMMON_DIR)/sections.ld.sx PLAT_INCLUDE += $(FE310_COMMON_DIR)/include -LOCAL_INTERRUPT_DEVICE := 0 +LOCAL_INTERRUPT_DEVICE := 1 PLAT_INTERRUPT_DEVICE := 0 include $(FE310_COMMON_DIR)/arch/build.mk From de04cb59b4af3d704b81cc674d3132e1f8a69609 Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Wed, 29 Dec 2021 15:09:54 +0530 Subject: [PATCH 11/31] Adding additional element to context frame --- src/arch/riscv/32/i/terravisor/asm.S | 1 + src/arch/riscv/32/i/terravisor/include/riscv.h | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/arch/riscv/32/i/terravisor/asm.S b/src/arch/riscv/32/i/terravisor/asm.S index bcb89de3..e8c74a6a 100644 --- a/src/arch/riscv/32/i/terravisor/asm.S +++ b/src/arch/riscv/32/i/terravisor/asm.S @@ -63,6 +63,7 @@ function exception_handler_base .p2align 2 /* Needs 4Byte Alignment, refer mtvec reg */ PROLOGUE csrr a0, mcause + lw a0, 72(sp) mv a1, sp call exception_handler EPILOGUE diff --git a/src/arch/riscv/32/i/terravisor/include/riscv.h b/src/arch/riscv/32/i/terravisor/include/riscv.h index 8a11b0d8..20bf61bc 100644 --- a/src/arch/riscv/32/i/terravisor/include/riscv.h +++ b/src/arch/riscv/32/i/terravisor/include/riscv.h @@ -20,7 +20,7 @@ typedef struct context_frame uint32_t mepc, mstatus, ra, t0, t1, t2, a0, a1, a2, a3, a4, a5, a6, a7, t3, t4, - t5, t6, pad[6]; + t5, t6, mcause, pad[5]; /* Padding is necessary for alignment */ } context_frame_t; From 951f962f5798a3e2acba780273c7badf71fee6f6 Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Wed, 29 Dec 2021 17:27:02 +0530 Subject: [PATCH 12/31] Adding spinlock --- src/arch/riscv/32/a/build.mk | 14 ++++++++ src/arch/riscv/32/a/spinlock.c | 35 ++++++++++++++++++ src/arch/riscv/32/i/terravisor/arch.c | 5 ++- src/arch/riscv/32/i/terravisor/include/arch.h | 3 +- src/arch/riscv/32/i/terravisor/spinlock.S | 0 src/arch/riscv/32/i/terravisor/spinlock.c | 36 +++++++++++++++++++ src/arch/riscv/32/imac/terravisor/build.mk | 1 + 7 files changed, 89 insertions(+), 5 deletions(-) create mode 100644 src/arch/riscv/32/a/build.mk create mode 100644 src/arch/riscv/32/a/spinlock.c delete mode 100644 src/arch/riscv/32/i/terravisor/spinlock.S create mode 100644 src/arch/riscv/32/i/terravisor/spinlock.c diff --git a/src/arch/riscv/32/a/build.mk b/src/arch/riscv/32/a/build.mk new file mode 100644 index 00000000..2311cf00 --- /dev/null +++ b/src/arch/riscv/32/a/build.mk @@ -0,0 +1,14 @@ +# +# CYANCORE LICENSE +# Copyrights (C) 2019, Cyancore Team +# +# File Name : build.mk +# Description : Build script for this directory. +# Primary Authod : Akash Kollipara [akashkollipara@gmail.com] +# Organisation : Cyancore Core-Team +# + +RV32IA_ARCH_DIR := $(GET_PATH) + +DIR := $(RV32IA_ARCH_DIR) +include mk/obj.mk diff --git a/src/arch/riscv/32/a/spinlock.c b/src/arch/riscv/32/a/spinlock.c new file mode 100644 index 00000000..7ecfa808 --- /dev/null +++ b/src/arch/riscv/32/a/spinlock.c @@ -0,0 +1,35 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2019, Cyancore Team + * + * File Name : spinlock.c + * Description : This file consists of sources of spinlock + * Primary Author : Akash Kollipara [akashkollipara@gmail.com] + * Organisation : Cyancore Core-Team + */ + +#include +#include +#include +#include + +void spinlock_acquire(spinlock_t *key) +{ + unsigned int old, new; + new = 1; + while(1) + { + if(*key) + continue; + asm volatile("amoswap.w %0, %2, %1" : "=r" (old), "+A" (*key) : "r" (new) : "memory"); + fence(r, rw); + if(!old) + break; + } +} + +void spinlock_release(spinlock_t *key) +{ + fence(r, rw); + *key = 0; +} diff --git a/src/arch/riscv/32/i/terravisor/arch.c b/src/arch/riscv/32/i/terravisor/arch.c index 72054857..dc1eb201 100644 --- a/src/arch/riscv/32/i/terravisor/arch.c +++ b/src/arch/riscv/32/i/terravisor/arch.c @@ -47,9 +47,8 @@ void arch_ei_restore_state() /** * arch_machine_call - perform machine call * - * @brief This function executes function pointed by mcall. As the - * AVR core doesn't support multiple execution levels, this - * function emulates the machine call to maintain consistency. + * @brief This function performs environment call + * in m mode * * @param[in] code: machine call code * @param[in] a0: first argument diff --git a/src/arch/riscv/32/i/terravisor/include/arch.h b/src/arch/riscv/32/i/terravisor/include/arch.h index 33e39b3e..75234a64 100644 --- a/src/arch/riscv/32/i/terravisor/include/arch.h +++ b/src/arch/riscv/32/i/terravisor/include/arch.h @@ -53,11 +53,10 @@ void arch_register_interrupt_handler(unsigned int, void (*)(void)); void local_register_interrupt_handler(unsigned int, void (*)(void)); /** - * arch_core_index - Returns code index (0, in case of AVR) + * arch_core_index - Returns code index */ static inline unsigned int arch_core_index() { - /* AVR only support uni core architecture */ unsigned int id; asm volatile("csrr %0, mhartid" : "=r" (id)); return id; diff --git a/src/arch/riscv/32/i/terravisor/spinlock.S b/src/arch/riscv/32/i/terravisor/spinlock.S deleted file mode 100644 index e69de29b..00000000 diff --git a/src/arch/riscv/32/i/terravisor/spinlock.c b/src/arch/riscv/32/i/terravisor/spinlock.c new file mode 100644 index 00000000..099b95f2 --- /dev/null +++ b/src/arch/riscv/32/i/terravisor/spinlock.c @@ -0,0 +1,36 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2019, Cyancore Team + * + * File Name : spinlock.c + * Description : This file consists of sources of spinlock + * Primary Author : Akash Kollipara [akashkollipara@gmail.com] + * Organisation : Cyancore Core-Team + */ + +#include +#include +#include +#include + +_WEAK void spinlock_acquire(spinlock_t *key) +{ + while(1) + { + arch_di(); + if(!*key) + { + *key = 1; + fence(r, rw); + break; + } + arch_ei(); + } + arch_ei(); +} + +_WEAK void spinlock_release(spinlock_t *key) +{ + fence(rw, r); + *key = 0; +} diff --git a/src/arch/riscv/32/imac/terravisor/build.mk b/src/arch/riscv/32/imac/terravisor/build.mk index 711b24ef..b69123ca 100644 --- a/src/arch/riscv/32/imac/terravisor/build.mk +++ b/src/arch/riscv/32/imac/terravisor/build.mk @@ -11,6 +11,7 @@ RV32IMAC_T_ARCH_DIR := $(GET_PATH) include $(RV32IMAC_T_ARCH_DIR)/../../i/terravisor/build.mk +include $(RV32IMAC_T_ARCH_DIR)/../../a/build.mk DIR := $(RV32IMAC_T_ARCH_DIR) include mk/obj.mk From 9903687ec93d0f96c451453524e7448d36e744f6 Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Thu, 30 Dec 2021 12:28:01 +0530 Subject: [PATCH 13/31] Added platform boot and driver init - Added platform bootsrap support - Updated linker script to support driver init - Fixed memory alignment in linker script and section arrangement --- .../mega_avr/common/platform/platform.c | 1 - .../common_fe310/arch/platform_copy_mem.c | 25 +++++ src/platform/sifive/common_fe310/build.mk | 2 +- .../sifive/common_fe310/include/platform.h | 1 + .../sifive/common_fe310/platform/build.mk | 14 +++ .../sifive/common_fe310/platform/platform.c | 43 ++++++++ .../common_fe310/platform/platform_reset.c | 70 +++++++++++++ .../common_fe310/platform/platform_resource.c | 97 +++++++++++++++++++ .../sifive/common_fe310/sections.ld.sx | 89 ++++++++++------- src/projects/demo_riscv/config.mk | 2 +- src/projects/demo_riscv/project.c | 4 + 11 files changed, 312 insertions(+), 36 deletions(-) create mode 100644 src/platform/sifive/common_fe310/arch/platform_copy_mem.c create mode 100644 src/platform/sifive/common_fe310/platform/build.mk create mode 100644 src/platform/sifive/common_fe310/platform/platform.c create mode 100644 src/platform/sifive/common_fe310/platform/platform_reset.c create mode 100644 src/platform/sifive/common_fe310/platform/platform_resource.c diff --git a/src/platform/mega_avr/common/platform/platform.c b/src/platform/mega_avr/common/platform/platform.c index 7c3c047b..42ed265e 100644 --- a/src/platform/mega_avr/common/platform/platform.c +++ b/src/platform/mega_avr/common/platform/platform.c @@ -12,7 +12,6 @@ #include #include #include -#include #include #include #include diff --git a/src/platform/sifive/common_fe310/arch/platform_copy_mem.c b/src/platform/sifive/common_fe310/arch/platform_copy_mem.c new file mode 100644 index 00000000..269bcc0e --- /dev/null +++ b/src/platform/sifive/common_fe310/arch/platform_copy_mem.c @@ -0,0 +1,25 @@ +#include +#include +#include +#include + +extern uintptr_t _bss_start, _bss_size, _data_start, _data_size, + _data_vstart, _itim_start, _itim_size, _itim_vstart; + +status_t platform_bss_clear() +{ + memset(&_bss_start, 0, (size_t)&_bss_size); + return success; +} + +status_t platform_copy_itim() +{ + memcpy(&_itim_vstart, &_itim_start, (size_t)&_itim_size); + return success; +} + +status_t platform_copy_data() +{ + memcpy(&_data_vstart, &_data_start, (size_t)&_data_size); + return success; +} diff --git a/src/platform/sifive/common_fe310/build.mk b/src/platform/sifive/common_fe310/build.mk index f02c4320..ec6a85c3 100644 --- a/src/platform/sifive/common_fe310/build.mk +++ b/src/platform/sifive/common_fe310/build.mk @@ -20,4 +20,4 @@ PLAT_INTERRUPT_DEVICE := 0 include $(FE310_COMMON_DIR)/arch/build.mk #include $(FE310_COMMON_DIR)/hal/build.mk -#include $(FE310_COMMON_DIR)/platform/build.mk +include $(FE310_COMMON_DIR)/platform/build.mk diff --git a/src/platform/sifive/common_fe310/include/platform.h b/src/platform/sifive/common_fe310/include/platform.h index ac936f3e..e8898fd9 100644 --- a/src/platform/sifive/common_fe310/include/platform.h +++ b/src/platform/sifive/common_fe310/include/platform.h @@ -14,6 +14,7 @@ #include void plat_panic_handler(); +status_t platform_copy_itim(); status_t platform_copy_data(); status_t platform_bss_clear(); status_t platform_resources_setup(); diff --git a/src/platform/sifive/common_fe310/platform/build.mk b/src/platform/sifive/common_fe310/platform/build.mk new file mode 100644 index 00000000..6fd659de --- /dev/null +++ b/src/platform/sifive/common_fe310/platform/build.mk @@ -0,0 +1,14 @@ +# +# CYANCORE LICENSE +# Copyrights (C) 2019, Cyancore Team +# +# File Name : build.mk +# Description : This file builds sources from fe310 +# common platform directory +# Primary Author : Akash Kollipara [akashkollipara@gmail.com] +# Organisation : Cyancore Core-Team +# + +DIR := $(GET_PATH) + +include mk/obj.mk diff --git a/src/platform/sifive/common_fe310/platform/platform.c b/src/platform/sifive/common_fe310/platform/platform.c new file mode 100644 index 00000000..278ccdda --- /dev/null +++ b/src/platform/sifive/common_fe310/platform/platform.c @@ -0,0 +1,43 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +void platform_early_setup() +{ + status_t ret = success; + + ret |= platform_copy_data(); + ret |= platform_copy_itim(); + ret |= platform_bss_clear(); + + if(ret != success) + exit(EXIT_FAILURE); + return; +} + +void platform_setup() +{ + status_t ret = success; + +// ret |= platform_resources_setup(); + + if(ret != success) + exit(EXIT_FAILURE); + return; +} + +void platform_cpu_setup() +{ + status_t ret = success; + + arch_ei(); + + if(ret != success) + exit(EXIT_FAILURE); + return; +} diff --git a/src/platform/sifive/common_fe310/platform/platform_reset.c b/src/platform/sifive/common_fe310/platform/platform_reset.c new file mode 100644 index 00000000..94c2dc10 --- /dev/null +++ b/src/platform/sifive/common_fe310/platform/platform_reset.c @@ -0,0 +1,70 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2019, Cyancore Team + * + * File Name : platform_reset.c + * Description : This file contains sources for platform + * reset apis + * Primary Author : Akash Kollipara [akashkollipara@gmail.com] + * Organisation : Cyancore Core-Team + */ + +#include +#include +#include +#include +#include + +uint8_t reset_syndrome; + +/** + * platform_get_reset_syndrome - returns the cause of reset + * + * @brief This function returns the information related to + * the reset sources. + * + * @return reset_cause: This function returs the reset cause + */ +reset_t platform_get_reset_syndrome() +{ + if(reset_syndrome & 1) /* Power on Reset */ + return power_on_reset; + else if(reset_syndrome & 2) /* External Reset */ + return external_reset; + else if(reset_syndrome & 4) /* Brown out Reset */ + return brownout_reset; + else if(reset_syndrome & 8) /* WDog Reset */ + return wdog_reset; + else + return inval_reset; +} + + +TODO(This Function needs to be replaced with brownout detection driver api) +_WEAK void brownout_reset_handler() +{ + plat_panic_handler(); +} + +/** + * platform_reset_handler - handles the reset conditions + * + * @brief This function is responsible to handle the reset + * sources like watchdog, brownout, external reset, etc. + * + * @param[in] rsyn: Reset syndrome + * + * @return void + */ +void platform_reset_handler(reset_t rsyn) +{ + if((rsyn == power_on_reset) || (rsyn == external_reset)) + return; + else if(rsyn == brownout_reset) + brownout_reset_handler(); + else if(rsyn == wdog_reset) + wdog_reset_handler(); + else + plat_panic_handler(); +} + diff --git a/src/platform/sifive/common_fe310/platform/platform_resource.c b/src/platform/sifive/common_fe310/platform/platform_resource.c new file mode 100644 index 00000000..1300a441 --- /dev/null +++ b/src/platform/sifive/common_fe310/platform/platform_resource.c @@ -0,0 +1,97 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2019, Cyancore Team + * + * File Name : platform_resource.c + * Description : This file contains sources for platform + * resource apis + * Primary Author : Akash Kollipara [akashkollipara@gmail.com] + * Organisation : Cyancore Core-Team + */ + +#include +#include +#include +#include +#include +#include + +/** + * platform_resources_setup - Updates platform DP and SP + * + * @brief This function is responsible to update platform DP + * and make it accessible to the system. This function needs + * to be called during platform setup. + * + * @return status: return the function execution status + */ +status_t platform_resources_setup() +{ + status_t ret = success; +// extern dp_t device_prop; +// extern sp_t software_prop; +// ret = dp_init(&device_prop); +// ret |= sp_init(&software_prop); + return ret; +} + +/** + * platform_fetch_sp - mcall handler for fetch_sp + * + * @brief This function is a machine call hander for fetch_sp + * access code. It is responsible to respond with hardware ID + * to corresponding software properties. + * + * @param[in] a0: arg0 + * @param[in] a1: arg1 + * @param[in] a2: arg2 + * + * @return status: return the function execution status + */ +mret_t platform_fetch_sp(unsigned int a0, unsigned int a1 _UNUSED, unsigned int a2 _UNUSED) +{ + mret_t ret; + ret.p = (uintptr_t) sp_terravisor_dev_info(a0); + ret.size = (ret.p) ? sizeof(hw_devid_t) : 0; + ret.status = (ret.p) ? success : error_device_id_inval; + return ret; +} + +INCLUDE_MCALL(atmega328p_fetch_sp, fetch_sp, platform_fetch_sp); + +/** + * platform_fetch_dp - mcall handler for fetch_dp + * + * @brief This function is a machine call hander for fetch_dp + * access code. It is responsible to respond with pointer to + * corresponding device properties. + * + * @param[in] a0: arg0 + * @param[in] a1: arg1 + * @param[in] a2: arg2 + * + * @return status: return the function execution status + */ +mret_t platform_fetch_dp(unsigned int a0, unsigned int a1 _UNUSED, unsigned int a2 _UNUSED) +{ + mret_t ret; + switch(a0) + { + case clock: + ret.p = (uintptr_t)dp_get_base_clock(); + ret.size = (ret.p) ? sizeof(unsigned long) : 0; + break; + case gpio: + ret.p = (uintptr_t)dp_get_port_info(a0 | a1); + ret.size = (ret.p) ? sizeof(gpio_module_t) : 0; + break; + default: + ret.p = (uintptr_t) dp_get_module_info(a0 | a1); + ret.size = (ret.p) ? sizeof(module_t) : 0; + break; + } + ret.status = (ret.p) ? success : error_device_id_inval; + return ret; +} + +INCLUDE_MCALL(atmega328p_fetch_dp, fetch_dp, platform_fetch_dp); diff --git a/src/platform/sifive/common_fe310/sections.ld.sx b/src/platform/sifive/common_fe310/sections.ld.sx index 445b2b6a..beaa49af 100644 --- a/src/platform/sifive/common_fe310/sections.ld.sx +++ b/src/platform/sifive/common_fe310/sections.ld.sx @@ -19,8 +19,8 @@ MEMORY { /* VM Addresses */ vma_itmem (airwx) : ORIGIN = V_ITMEM_START, LENGTH = ITMEM_LENGTH - vma_imem (arw!xi) : ORIGIN = V_IMEM_START, LENGTH = IMEM_LENGTH - vma_dmem (irx!aw) : ORIGIN = V_DMEM_START, LENGTH = DMEM_LENGTH + vma_imem (irx!aw) : ORIGIN = V_IMEM_START, LENGTH = IMEM_LENGTH + vma_dmem (arw!xi) : ORIGIN = V_DMEM_START, LENGTH = DMEM_LENGTH /* LM Addresses */ lma_mem : ORIGIN = L_MEM_START, LENGTH = L_MEM_LENGTH @@ -36,34 +36,44 @@ SECTIONS *(.text) *(.text.*) KEEP(*(.text)) + . = ALIGN(4); } > vma_imem AT > lma_mem .rodata : { - . = ALIGN(8); *(.version) KEEP(*(.version)) - *(.rodata) - *(.rodata.*) + KEEP(*(.rdata)) KEEP(*(.rodata)) + KEEP(*(.rodata.*)) + *(.srodata) + *(.srodata.*) + KEEP(*(.srodata)) } > vma_imem AT > lma_mem - .driver_table : {} > vma_imem AT > lma_mem - .mcall_table : {} > vma_imem AT > lma_mem + .itim : + { + . = ALIGN(4); + KEEP(*(.itim)) + KEEP(*(.itim.*)) + } > vma_itmem AT > lma_mem - .data : + .bss : { - *(.data) - *(.data.*) - KEEP(*(.data)) - } > vma_dmem AT > lma_mem + . = ALIGN(1); + *(.bss) + *(.bss.*) + KEEP(*(.bss)) + *(COMMON) + } > vma_dmem - .tdata : + .static_bss_global : { - *(.tdata) - *(.tdata.*) - KEEP(*(.tdata)) - } > vma_dmem AT > lma_mem + . = ALIGN(1); + *(.sbss) + *(.sbss.*) + KEEP(*(.sbss)) + } > vma_dmem .static_global_var : { @@ -71,25 +81,30 @@ SECTIONS .static_data_global : { + . = ALIGN(1); *(.sdata) *(.sdata.*) KEEP(*(.sdata)) } > vma_dmem AT > lma_mem - .static_bss_global : + .data : { - *(.sbss) - *(.sbss.*) - KEEP(*(.sbss)) - } > vma_dmem + . = ALIGN(1); + *(.data) + *(.data.*) + KEEP(*(.data)) + } > vma_dmem AT > lma_mem - .bss : + .tdata : { - *(.bss) - *(.bss.*) - KEEP(*(.bss)) - *(COMMON) - } > vma_dmem + . = ALIGN(1); + *(.tdata) + *(.tdata.*) + KEEP(*(.tdata)) + } > vma_dmem AT > lma_mem + + .driver_table : ALIGN(4) {} > vma_dmem AT > lma_mem + .mcall_table : ALIGN(4) {} > vma_dmem AT > lma_mem .stack : { @@ -97,9 +112,10 @@ SECTIONS KEEP(*(.stack)) } > vma_dmem - PROVIDE(_data_start = LOADADDR(.data)); - PROVIDE(_data_size = SIZEOF(.data) + SIZEOF(.driver_table) + SIZEOF(.mcall_table)); - PROVIDE(_data_vstart = ADDR(.data)); + PROVIDE(_data_start = LOADADDR(.static_data_global)); + PROVIDE(_data_size = SIZEOF(.data) + SIZEOF(.tdata) + SIZEOF(.static_data_global) +\ + SIZEOF(.driver_table) + SIZEOF(.mcall_table)); + PROVIDE(_data_vstart = ADDR(.static_data_global)); PROVIDE(_data_vend = _data_vstart + _data_size); PROVIDE(_tls_start = ADDR(.tdata)); @@ -107,10 +123,17 @@ SECTIONS PROVIDE(_stack_start = ADDR(.stack) + STACK_SIZE); PROVIDE(_bss_start = ADDR(.bss)); - PROVIDE(_bss_size = SIZEOF(.bss)); - PROVIDE(_flash_size = _data_size + SIZEOF(.text)); + PROVIDE(_bss_size = SIZEOF(.bss) + SIZEOF(.static_bss_global)); + + PROVIDE(_itim_start = LOADADDR(.itim)); + PROVIDE(_itim_size = SIZEOF(.itim)); + PROVIDE(_itim_vstart = ADDR(.itim)); + PROVIDE(_itim_vsize = _itim_vstart + _itim_size); + + PROVIDE(_flash_size = _data_size + SIZEOF(.text) + _itim_size + SIZEOF(.rodata)); PROVIDE(_ram_size = _bss_size + _data_size + SIZEOF(.stack)); + ASSERT((_flash_size < FLASH_SIZE), "< x > Flash size exceeded ...") ASSERT((_ram_size < RAM_SIZE), "< x > RAM size exceeded ...") diff --git a/src/projects/demo_riscv/config.mk b/src/projects/demo_riscv/config.mk index 09b31fed..67c0a2bd 100644 --- a/src/projects/demo_riscv/config.mk +++ b/src/projects/demo_riscv/config.mk @@ -12,5 +12,5 @@ COMPILER := gcc TC_VER := 8.2.0 FAMILY := sifive PLATFORM := fe310g002 -EARLYCON_MEMBUF := 0 +EARLYCON_MEMBUF := 1 CONSOLE_MEMBUF := 0 diff --git a/src/projects/demo_riscv/project.c b/src/projects/demo_riscv/project.c index 6d58ba75..88d31fbd 100644 --- a/src/projects/demo_riscv/project.c +++ b/src/projects/demo_riscv/project.c @@ -8,8 +8,12 @@ * Organisation : Cyancore Core-Team */ +#include +#include + void plug() { + bootstrap(); } void play() From 08bdb7a5c394edd64277b1b3ba1080c1c3e368c3 Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Thu, 30 Dec 2021 13:03:46 +0530 Subject: [PATCH 14/31] Hardcoding reset syndrome to 1 - As the platform currently doesnt support reset source identification, the reset syndrome is by default pogrammed to "power on reset" --- src/platform/sifive/common_fe310/platform/platform.c | 4 ++++ src/platform/sifive/common_fe310/platform/platform_reset.c | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/platform/sifive/common_fe310/platform/platform.c b/src/platform/sifive/common_fe310/platform/platform.c index 278ccdda..fd09f800 100644 --- a/src/platform/sifive/common_fe310/platform/platform.c +++ b/src/platform/sifive/common_fe310/platform/platform.c @@ -7,6 +7,8 @@ #include #include +unsigned int reset_syndrome; + void platform_early_setup() { status_t ret = success; @@ -14,6 +16,8 @@ void platform_early_setup() ret |= platform_copy_data(); ret |= platform_copy_itim(); ret |= platform_bss_clear(); + + reset_syndrome = 1; if(ret != success) exit(EXIT_FAILURE); diff --git a/src/platform/sifive/common_fe310/platform/platform_reset.c b/src/platform/sifive/common_fe310/platform/platform_reset.c index 94c2dc10..da00224e 100644 --- a/src/platform/sifive/common_fe310/platform/platform_reset.c +++ b/src/platform/sifive/common_fe310/platform/platform_reset.c @@ -15,7 +15,7 @@ #include #include -uint8_t reset_syndrome; +extern unsigned int reset_syndrome; /** * platform_get_reset_syndrome - returns the cause of reset From 262b6396520889b1ec5f3b794878d9879053fd7f Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Thu, 30 Dec 2021 14:50:49 +0530 Subject: [PATCH 15/31] fixed machine call bug - Fixed machine call bug while parsing the handler table - Added check for return pointers for arch_machine_call - Updated FE310 platform support - Updated projects to test riscv mcall --- src/arch/avr/8/common_5x_6/terravisor/arch.c | 2 ++ src/arch/riscv/32/i/terravisor/arch.c | 2 ++ src/arch/riscv/32/i/terravisor/asm.S | 2 +- .../sifive/common_fe310/arch/platform_mcall.c | 36 +++++++++++++++++++ .../sifive/common_fe310/include/platform.h | 5 +-- .../sifive/common_fe310/platform/platform.c | 2 ++ .../common_fe310/platform/platform_reset.c | 18 +--------- src/projects/demo_riscv/project.c | 3 ++ .../terravisor/workers/mcall/machine_call.c | 2 +- 9 files changed, 49 insertions(+), 23 deletions(-) create mode 100644 src/platform/sifive/common_fe310/arch/platform_mcall.c diff --git a/src/arch/avr/8/common_5x_6/terravisor/arch.c b/src/arch/avr/8/common_5x_6/terravisor/arch.c index e763d2a4..5a653506 100644 --- a/src/arch/avr/8/common_5x_6/terravisor/arch.c +++ b/src/arch/avr/8/common_5x_6/terravisor/arch.c @@ -102,6 +102,8 @@ void (*mcall)(unsigned int, unsigned int, unsigned int, unsigned int, mret_t *); */ void arch_machine_call(unsigned int code, unsigned int a0, unsigned int a1, unsigned int a2, mret_t *ret) { + if(ret == NULL) + return; mcall(code, a0, a1, a2, ret); return; } diff --git a/src/arch/riscv/32/i/terravisor/arch.c b/src/arch/riscv/32/i/terravisor/arch.c index dc1eb201..92de39c8 100644 --- a/src/arch/riscv/32/i/terravisor/arch.c +++ b/src/arch/riscv/32/i/terravisor/arch.c @@ -58,6 +58,8 @@ void arch_ei_restore_state() */ void arch_machine_call(unsigned int code, unsigned int a0, unsigned int a1, unsigned int a2, mret_t *ret) { + if(ret == NULL) + return; asm volatile("mv a0, %0" : : "r" (code)); asm volatile("mv a1, %0" : : "r" (a0)); asm volatile("mv a2, %0" : : "r" (a1)); diff --git a/src/arch/riscv/32/i/terravisor/asm.S b/src/arch/riscv/32/i/terravisor/asm.S index e8c74a6a..4684e6f9 100644 --- a/src/arch/riscv/32/i/terravisor/asm.S +++ b/src/arch/riscv/32/i/terravisor/asm.S @@ -63,7 +63,7 @@ function exception_handler_base .p2align 2 /* Needs 4Byte Alignment, refer mtvec reg */ PROLOGUE csrr a0, mcause - lw a0, 72(sp) + sw a0, 72(sp) mv a1, sp call exception_handler EPILOGUE diff --git a/src/platform/sifive/common_fe310/arch/platform_mcall.c b/src/platform/sifive/common_fe310/arch/platform_mcall.c new file mode 100644 index 00000000..1bfa2325 --- /dev/null +++ b/src/platform/sifive/common_fe310/arch/platform_mcall.c @@ -0,0 +1,36 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2019, Cyancore Team + * + * File Name : platform_mcall.c + * Description : This file contains platform mcall registration + * api + * Primary Author : Akash Kollipara [akashkollipara@gmail.com] + * Organisation : Cyancore Core-Team + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +static void platform_mcall_handler() +{ + context_frame_t *frame = get_context_frame(); + mret_t mres; + machine_call(frame->a0, frame->a1, frame->a2, frame->a3, &mres); + fence(w, w); + frame->a0 = mres.p; + frame->a1 = mres.size; + frame->a2 = mres.status; + return; +} + +status_t platform_mcall_update() +{ + return link_interrupt(arch, 11, &platform_mcall_handler); +} diff --git a/src/platform/sifive/common_fe310/include/platform.h b/src/platform/sifive/common_fe310/include/platform.h index e8898fd9..fc60c7ad 100644 --- a/src/platform/sifive/common_fe310/include/platform.h +++ b/src/platform/sifive/common_fe310/include/platform.h @@ -18,7 +18,4 @@ status_t platform_copy_itim(); status_t platform_copy_data(); status_t platform_bss_clear(); status_t platform_resources_setup(); -status_t platform_clk_reset(); -status_t platform_clk_en(unsigned int); -status_t platform_clk_dis(unsigned int); -status_t platform_wdt_reset(); +status_t platform_mcall_update(); diff --git a/src/platform/sifive/common_fe310/platform/platform.c b/src/platform/sifive/common_fe310/platform/platform.c index fd09f800..f73d217d 100644 --- a/src/platform/sifive/common_fe310/platform/platform.c +++ b/src/platform/sifive/common_fe310/platform/platform.c @@ -39,6 +39,8 @@ void platform_cpu_setup() { status_t ret = success; + ret |= platform_mcall_update(); + arch_ei(); if(ret != success) diff --git a/src/platform/sifive/common_fe310/platform/platform_reset.c b/src/platform/sifive/common_fe310/platform/platform_reset.c index da00224e..eb9bf824 100644 --- a/src/platform/sifive/common_fe310/platform/platform_reset.c +++ b/src/platform/sifive/common_fe310/platform/platform_reset.c @@ -29,23 +29,11 @@ reset_t platform_get_reset_syndrome() { if(reset_syndrome & 1) /* Power on Reset */ return power_on_reset; - else if(reset_syndrome & 2) /* External Reset */ - return external_reset; - else if(reset_syndrome & 4) /* Brown out Reset */ - return brownout_reset; - else if(reset_syndrome & 8) /* WDog Reset */ - return wdog_reset; else return inval_reset; } -TODO(This Function needs to be replaced with brownout detection driver api) -_WEAK void brownout_reset_handler() -{ - plat_panic_handler(); -} - /** * platform_reset_handler - handles the reset conditions * @@ -58,12 +46,8 @@ _WEAK void brownout_reset_handler() */ void platform_reset_handler(reset_t rsyn) { - if((rsyn == power_on_reset) || (rsyn == external_reset)) + if(rsyn == power_on_reset) return; - else if(rsyn == brownout_reset) - brownout_reset_handler(); - else if(rsyn == wdog_reset) - wdog_reset_handler(); else plat_panic_handler(); } diff --git a/src/projects/demo_riscv/project.c b/src/projects/demo_riscv/project.c index 88d31fbd..a405207b 100644 --- a/src/projects/demo_riscv/project.c +++ b/src/projects/demo_riscv/project.c @@ -10,10 +10,13 @@ #include #include +#include void plug() { + mret_t mres; bootstrap(); + arch_machine_call(0, 100, 200, 300, &mres); } void play() diff --git a/src/visor/terravisor/workers/mcall/machine_call.c b/src/visor/terravisor/workers/mcall/machine_call.c index 1b54ed14..46e7f6bf 100644 --- a/src/visor/terravisor/workers/mcall/machine_call.c +++ b/src/visor/terravisor/workers/mcall/machine_call.c @@ -35,7 +35,7 @@ void machine_call(mcall_id_t id, unsigned int a0, unsigned int a1, unsigned int ptr = &_mcall_table_start; /* Run through the table till the end */ - while(ptr <= & _mcall_table_end) + while(ptr < & _mcall_table_end) { /* Check if the mcall ID matches */ if(ptr->id == id) From ee720fac2812b2889e0eda6311291d83c0a3f755 Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Thu, 30 Dec 2021 15:43:05 +0530 Subject: [PATCH 16/31] fixed platform name --- src/platform/sifive/common_fe310/build.mk | 2 +- src/platform/sifive/common_fe310/platform/platform_resource.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/platform/sifive/common_fe310/build.mk b/src/platform/sifive/common_fe310/build.mk index ec6a85c3..423a0649 100644 --- a/src/platform/sifive/common_fe310/build.mk +++ b/src/platform/sifive/common_fe310/build.mk @@ -3,7 +3,7 @@ # Copyrights (C) 2019, Cyancore Team # # File Name : build.mk -# Description : This file accumulates sources from mega-avr +# Description : This file accumulates sources from FE310 # common directory # Primary Author : Akash Kollipara [akashkollipara@gmail.com] # Organisation : Cyancore Core-Team diff --git a/src/platform/sifive/common_fe310/platform/platform_resource.c b/src/platform/sifive/common_fe310/platform/platform_resource.c index 1300a441..7e73a032 100644 --- a/src/platform/sifive/common_fe310/platform/platform_resource.c +++ b/src/platform/sifive/common_fe310/platform/platform_resource.c @@ -57,7 +57,7 @@ mret_t platform_fetch_sp(unsigned int a0, unsigned int a1 _UNUSED, unsigned int return ret; } -INCLUDE_MCALL(atmega328p_fetch_sp, fetch_sp, platform_fetch_sp); +INCLUDE_MCALL(sifive_fe310_fetch_sp, fetch_sp, platform_fetch_sp); /** * platform_fetch_dp - mcall handler for fetch_dp @@ -94,4 +94,4 @@ mret_t platform_fetch_dp(unsigned int a0, unsigned int a1 _UNUSED, unsigned int return ret; } -INCLUDE_MCALL(atmega328p_fetch_dp, fetch_dp, platform_fetch_dp); +INCLUDE_MCALL(sifive_fe310_fetch_dp, fetch_dp, platform_fetch_dp); From 38e924d87dc7c9dc2065f1ea11ef7dd7b993c478 Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Thu, 30 Dec 2021 21:36:02 +0530 Subject: [PATCH 17/31] Add prints in panic handlers --- src/arch/riscv/32/i/terravisor/arch.c | 8 +++++++- src/projects/demo_riscv/config.mk | 2 +- src/projects/demo_riscv/project.c | 2 ++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/arch/riscv/32/i/terravisor/arch.c b/src/arch/riscv/32/i/terravisor/arch.c index 92de39c8..3bab6546 100644 --- a/src/arch/riscv/32/i/terravisor/arch.c +++ b/src/arch/riscv/32/i/terravisor/arch.c @@ -11,6 +11,7 @@ #include #include +#include #include /** @@ -73,13 +74,18 @@ void arch_machine_call(unsigned int code, unsigned int a0, unsigned int a1, unsi _WEAK void arch_panic_handler() { - context_frame_t *frame _UNUSED = get_context_frame(); + context_frame_t *frame = get_context_frame(); + printf("< x > Arch Panic!\n"); + printf("Info:\nCause\t: 0x%x\t Address\t: 0x%x\n", frame->mcause, frame->mepc); while(1) arch_wfi(); } _WEAK void arch_unhandled_irq() { + context_frame_t *frame = get_context_frame(); + printf("< x > Arch Unhandled IRQ!\n"); + printf("Info:\nIRQ ID\t: 0x%x\n", frame->mcause & ~(1U << 31)); while(1) arch_wfi(); } diff --git a/src/projects/demo_riscv/config.mk b/src/projects/demo_riscv/config.mk index 67c0a2bd..88a5f8b7 100644 --- a/src/projects/demo_riscv/config.mk +++ b/src/projects/demo_riscv/config.mk @@ -13,4 +13,4 @@ TC_VER := 8.2.0 FAMILY := sifive PLATFORM := fe310g002 EARLYCON_MEMBUF := 1 -CONSOLE_MEMBUF := 0 +CONSOLE_MEMBUF := 1 diff --git a/src/projects/demo_riscv/project.c b/src/projects/demo_riscv/project.c index a405207b..cafd369f 100644 --- a/src/projects/demo_riscv/project.c +++ b/src/projects/demo_riscv/project.c @@ -11,11 +11,13 @@ #include #include #include +#include void plug() { mret_t mres; bootstrap(); + driver_setup_all(); arch_machine_call(0, 100, 200, 300, &mres); } From 5297343a6b49ac3366e8fffbadc47622f2598d17 Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Thu, 30 Dec 2021 21:44:16 +0530 Subject: [PATCH 18/31] Added remote copy to build engine eg: copy_to_remote: $(ELF) scp @^ @:/ --- mk/copy_to_remote.mk | 12 ++++++++++++ mk/project.mk | 6 +++++- 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 mk/copy_to_remote.mk diff --git a/mk/copy_to_remote.mk b/mk/copy_to_remote.mk new file mode 100644 index 00000000..164117a6 --- /dev/null +++ b/mk/copy_to_remote.mk @@ -0,0 +1,12 @@ +# +# CYANCORE LICENSE +# Copyrights (C) 2019, Cyancore Team +# +# File Name : copy_to_target.mk +# Description : This file has target to copy to remote machine if any +# Primary Author : Akash Kollipara [akashkollipara@gmail.com] +# Organisation : Cyancore Core-Team +# + +copy_to_target: $(ELF) + @echo "< ! > Please add your commands here..." diff --git a/mk/project.mk b/mk/project.mk index 7df54aae..91588ff2 100644 --- a/mk/project.mk +++ b/mk/project.mk @@ -45,11 +45,13 @@ list: check: --lint + ifeq ($(findstring $(MAKECMDGOALS),$(T_ALLOWLIST)),) ifneq ($(firstword $(MAKECMDGOALS)),clean) ifneq ($(firstword $(MAKECMDGOALS)),default) ifneq ($(firstword $(MAKECMDGOALS)),cyancore) ifneq ($(firstword $(MAKECMDGOALS)),check) +ifneq ($(firstword $(MAKECMDGOALS)),copy_to_target) PROJECT ?= $(firstword $(MAKECMDGOALS)) CMD := $(word 2,$(MAKECMDGOALS)) ifeq ($(CMD),) @@ -63,6 +65,7 @@ endif endif endif endif +endif ifeq ($(PROJECT),) $(info < x > Project name was not specified...) @@ -80,10 +83,11 @@ include $(SRC)/sources.mk include mk/tc.mk endif -ifeq ($(findstring $(CMD),$(T_ALLOWLIST) default clean cyancore),) +ifeq ($(findstring $(CMD),$(T_ALLOWLIST) default clean cyancore copy_to_target),) $(CMD): $(filter %/$(CMD),$(DEP_LIBS) $(DEP_OBJS)) if [ "$<" = "" ]; then \ echo "No such target: $@"; \ exit 2; \ fi endif +include mk/copy_to_remote.mk From fefd3c584354005a1476dd99127220893d4bd999 Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Fri, 31 Dec 2021 15:05:08 +0530 Subject: [PATCH 19/31] Updated build with remote copy feature - Cleaned up project.mk and simplified targets assessment - Updated remote copy feature --- mk/copy_to_remote.mk | 8 ++++++-- mk/project.mk | 19 +++++++------------ 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/mk/copy_to_remote.mk b/mk/copy_to_remote.mk index 164117a6..24165ca3 100644 --- a/mk/copy_to_remote.mk +++ b/mk/copy_to_remote.mk @@ -3,10 +3,14 @@ # Copyrights (C) 2019, Cyancore Team # # File Name : copy_to_target.mk -# Description : This file has target to copy to remote machine if any +# Description : This file has build target to copy to +# remote machine if any # Primary Author : Akash Kollipara [akashkollipara@gmail.com] # Organisation : Cyancore Core-Team # -copy_to_target: $(ELF) +--cpremote: $(ELF) + @echo "< ! > Please add your commands here..." + +--rmremote: @echo "< ! > Please add your commands here..." diff --git a/mk/project.mk b/mk/project.mk index 91588ff2..ac599d9b 100644 --- a/mk/project.mk +++ b/mk/project.mk @@ -45,13 +45,12 @@ list: check: --lint +copy_to_remote: --cpremote +clean_remote: --rmremote + ifeq ($(findstring $(MAKECMDGOALS),$(T_ALLOWLIST)),) -ifneq ($(firstword $(MAKECMDGOALS)),clean) -ifneq ($(firstword $(MAKECMDGOALS)),default) -ifneq ($(firstword $(MAKECMDGOALS)),cyancore) -ifneq ($(firstword $(MAKECMDGOALS)),check) -ifneq ($(firstword $(MAKECMDGOALS)),copy_to_target) +ifeq ($(findstring $(firstword $(MAKECMDGOALS)),$(P_TARGETS)),) PROJECT ?= $(firstword $(MAKECMDGOALS)) CMD := $(word 2,$(MAKECMDGOALS)) ifeq ($(CMD),) @@ -61,10 +60,6 @@ endif .PHONY: $(PROJECT) $(PROJECT): $(CMD) -endif -endif -endif -endif endif ifeq ($(PROJECT),) @@ -76,18 +71,18 @@ $(info < x > Invalid project name...) $(info < ! > Run `make list` to get list of projects) $(error < x > Build Failed!) endif -endif ifeq ($(findstring $(MAKECMDGOALS),$(T_ALLOWLIST)),) include $(SRC)/sources.mk include mk/tc.mk +include mk/copy_to_remote.mk +endif endif -ifeq ($(findstring $(CMD),$(T_ALLOWLIST) default clean cyancore copy_to_target),) +ifeq ($(findstring $(CMD),$(T_ALLOWLIST) $(P_TARGETS)),) $(CMD): $(filter %/$(CMD),$(DEP_LIBS) $(DEP_OBJS)) if [ "$<" = "" ]; then \ echo "No such target: $@"; \ exit 2; \ fi endif -include mk/copy_to_remote.mk From 9e2b8a8200567240c9beceed3de1810a0b75c970 Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Fri, 31 Dec 2021 15:24:28 +0530 Subject: [PATCH 20/31] Updated help --- src/projects/demo_riscv/project.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/projects/demo_riscv/project.c b/src/projects/demo_riscv/project.c index cafd369f..48fcdf7e 100644 --- a/src/projects/demo_riscv/project.c +++ b/src/projects/demo_riscv/project.c @@ -23,4 +23,5 @@ void plug() void play() { + arch_wfi(); } From 9864ec2484de95cd6c10e1eb499b9f65e9a99aea Mon Sep 17 00:00:00 2001 From: Pranjal Chanda Date: Wed, 5 Jan 2022 20:51:04 +0530 Subject: [PATCH 21/31] scall add, Bugfix mcall (callback NULL check) --- src/include/status.h | 34 ++++++++- src/include/supervisor_call.h | 41 +++++++++++ src/include/visor/supervisor/workers.h | 14 ++++ src/lib/posix/include/errno.h | 50 ++++++------- src/lib/posix/include/sys/types.h | 13 ++-- src/lib/posix/src/posix_time.c | 71 +++++++++++++++++++ src/visor/supervisor/workers/scall/build.mk | 17 +++++ .../supervisor/workers/scall/scall.ld.sx | 22 ++++++ .../workers/scall/supervisory_call.c | 55 ++++++++++++++ src/visor/terravisor/workers/mcall/build.mk | 2 +- .../terravisor/workers/mcall/machine_call.c | 5 +- 11 files changed, 291 insertions(+), 33 deletions(-) create mode 100644 src/include/supervisor_call.h create mode 100644 src/include/visor/supervisor/workers.h create mode 100644 src/visor/supervisor/workers/scall/build.mk create mode 100644 src/visor/supervisor/workers/scall/scall.ld.sx create mode 100644 src/visor/supervisor/workers/scall/supervisory_call.c diff --git a/src/include/status.h b/src/include/status.h index e84e2f4c..a5cb36ef 100644 --- a/src/include/status.h +++ b/src/include/status.h @@ -15,34 +15,64 @@ typedef enum status { - success = 0x0000, + success = 0x0000, +/* Generic error */ error_generic = -0x0001, error_func_inval = -0x0002, error_func_inval_arg = -0x0003, + error_overflow = -0x0004, +/* Driver related error */ error_driver = -0x0100, error_driver_init_done = -0x0101, error_driver_init_failed = -0x0102, error_driver_busy = -0x0103, error_driver_data = -0x0104, +/* Device related error */ error_device = -0x0200, error_device_id_inval = -0x0201, error_device_inval = -0x0202, + error_device_busy = -0x0203, +/* Machine Call related error */ error_mcall = -0x0300, error_mcall_code_inval = -0x0301, +/* hcall related error */ error_hcall = -0x0400, +/* Syscall related error */ error_scall = -0x0500, +/* Memory related error */ error_memory = -0x0600, + error_memory_low = -0x0601, +/* File related error */ error_file = -0x0700, + error_file_desc = -0x0701, + error_file_exist = -0x0702, + error_file_not_found = -0x0703, + error_file_no_space = -0x0704, + error_file_long_name = -0x0705, +/* String related error */ error_string = -0x0800, +/* Math related error */ error_math = -0x0900, error_math_inval_arg = -0x0901, + error_math_large_val = -0x0902, +/* Access related error */ error_access = -0x0a00, +/* System related error */ error_system = -0x0b00, error_system_irq_link_fail = -0x0b01, error_system_irq_unlink_fail = -0x0b02, - error_network = -0x0c00, +/* Network related error */ + error_net = -0x0c00, + error_net_con_timeout = -0x0c001, +/* User space related error */ error_user = -0x0d00, +/* IO related error */ error_io = -0x0e00, +/* OS related error */ error_os = -0x0f00, error_os_task_overfow = -0x0f01, + error_os_deadlock = -0x0f02, +/* Mesg related error */ + error_mesg = -0x1000, + error_mesg_long = -0x1001, } status_t; diff --git a/src/include/supervisor_call.h b/src/include/supervisor_call.h new file mode 100644 index 00000000..8210e9f7 --- /dev/null +++ b/src/include/supervisor_call.h @@ -0,0 +1,41 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2019, Cyancore Team + * + * File Name : supervisor_call.h + * Description : This file consists of supervisor call IDs and prototypes + * Primary Author : Pranjal Chanda [pranjalchanda08@gmail.com] + * Organisation : Cyancore Core-Team + */ + +#pragma once +#define _SUPER_CALL_H_ + +#include +#include + +// Supervisor call IDs +typedef enum scall_id +{ + scall_def_placeholder +} scall_id_t; + +typedef struct sret +{ + uintptr_t p; + size_t size; + status_t status; +} sret_t; + +typedef struct scall +{ + scall_id_t id; + sret_t (*callback)(unsigned int a0, unsigned int a1, unsigned int a2); +} scall_t; + +#define INCLUDE_SCALL(_name, _id , _callback) \ + const scall_t _name _SECTION(".scall") = \ + { \ + .id = _id, \ + .callback = _callback \ + } diff --git a/src/include/visor/supervisor/workers.h b/src/include/visor/supervisor/workers.h new file mode 100644 index 00000000..7e6b090c --- /dev/null +++ b/src/include/visor/supervisor/workers.h @@ -0,0 +1,14 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2019, Cyancore Team + * + * File Name : worker.h + * Description : This file consists of supervisor-workers + * and related prototypes + * Primary Author : Pranjal Chanda [pranjalchanda08@gmail.com] + * Organisation : Cyancore Core-Team + */ + +#pragma once + +void super_call(scall_id, unsigned int, unsigned int, unsigned int, sret_t *); diff --git a/src/lib/posix/include/errno.h b/src/lib/posix/include/errno.h index 5806db52..8238859e 100644 --- a/src/lib/posix/include/errno.h +++ b/src/lib/posix/include/errno.h @@ -1,16 +1,18 @@ -/** - * @file errno.h - * @brief System error numbers. - * - * http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/errno.h.html +/* + * CYANCORE LICENSE + * Copyrights (C) 2019, Cyancore Team * - * The values defined in this file may not be compatible with the strerror - * function provided by this system. + * File Name : errno.h + * Description : System error numbers. + * Primary Author : Pranjal Chanda [pranjalchanda08@gmail.com] + * Organisation : Cyancore Core-Team */ #ifndef _CC_POSIX_ERRNO_H_ #define _CC_POSIX_ERRNO_H_ +#include + /* Undefine all errnos to avoid redefinition errors with system errnos. */ #undef EPERM #undef ENOENT @@ -34,23 +36,23 @@ * @name Definition of POSIX errnos. */ /**@{ */ -#define EPERM 1 /**< Operation not permitted. */ -#define ENOENT 2 /**< No such file or directory. */ -#define EBADF 9 /**< Bad file descriptor. */ -#define EAGAIN 11 /**< Resource unavailable, try again. */ -#define ENOMEM 12 /**< Not enough space. */ -#define EEXIST 17 /**< File exists. */ -#define EBUSY 16 /**< Device or resource busy. */ -#define EINVAL 22 /**< Invalid argument. */ -#define ENOSPC 28 /**< No space left on device. */ -#define ERANGE 34 /**< Result too large. */ -#define ENAMETOOLONG 36 /**< File name too long. */ -#define EDEADLK 45 /**< Resource deadlock would occur. */ -#define EOVERFLOW 75 /**< Value too large to be stored in data type. */ -#define ENOSYS 88 /**< Function not supported. */ -#define EMSGSIZE 90 /**< Message too long. */ -#define ENOTSUP 95 /**< Operation not supported. */ -#define ETIMEDOUT 116 /**< Connection timed out. */ +#define EPERM -error_access /**< Operation not permitted. */ +#define ENOENT -error_file_not_found /**< No such file or directory. */ +#define EBADF -error_file_desc /**< Bad file descriptor. */ +#define EAGAIN -error_device_inval /**< Resource unavailable, try again. */ +#define ENOMEM -error_memory_low /**< Not enough space. */ +#define EEXIST -error_file_exist /**< File exists. */ +#define EBUSY -error_device_busy /**< Device or resource busy. */ +#define EINVAL -error_func_inval_arg /**< Invalid argument. */ +#define ENOSPC -error_file_no_space /**< No space left on device. */ +#define ERANGE -error_math_large_val /**< Result too large. */ +#define ENAMETOOLONG -error_file_long_name /**< File name too long. */ +#define EDEADLK -error_os_deadlock /**< Resource deadlock would occur. */ +#define EOVERFLOW -error_overflow /**< Value too large to be stored in data type. */ +#define ENOSYS -error_func_inval /**< Function not supported. */ +#define EMSGSIZE -error_mesg_long /**< Message too long. */ +#define ENOTSUP -error_func_inval /**< Operation not supported. */ +#define ETIMEDOUT -error_net_con_timeout /**< Connection timed out. */ /**@} */ /** diff --git a/src/lib/posix/include/sys/types.h b/src/lib/posix/include/sys/types.h index a370432b..bd925f6e 100644 --- a/src/lib/posix/include/sys/types.h +++ b/src/lib/posix/include/sys/types.h @@ -1,8 +1,11 @@ -/** - * @file sys/types.h - * @brief Data types. - * - * http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_types.h.html +/* + * CYANCORE LICENSE + * Copyrights (C) 2019, Cyancore Team + * + * File Name : sys/types.h + * Description : Data types. https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_types.h.html + * Primary Author : Pranjal Chanda [pranjalchanda08@gmail.com] + * Organisation : Cyancore Core-Team */ #ifndef _CC_POSIX_TYPES_H_ diff --git a/src/lib/posix/src/posix_time.c b/src/lib/posix/src/posix_time.c index 758e8cfd..11c00e74 100644 --- a/src/lib/posix/src/posix_time.c +++ b/src/lib/posix/src/posix_time.c @@ -10,3 +10,74 @@ int usleep( useconds_t usec ) { } + +clock_t clock( void ) +{ + +} + +int clock_getcpuclockid( pid_t pid, clockid_t * clock_id ) +{ + +} + +int clock_getres( clockid_t clock_id, struct timespec * res ) +{ + +} + +int clock_gettime( clockid_t clock_id, struct timespec * tp ) +{ + +} + +int clock_nanosleep( clockid_t clock_id, + int flags, + const struct timespec * rqtp, + struct timespec * rmtp ) +{ + +} + +int clock_settime( clockid_t clock_id, + const struct timespec * tp ) +{ + +} + +int nanosleep( const struct timespec * rqtp, + struct timespec * rmtp ) +{ + +} + +int timer_create( clockid_t clockid, + struct sigevent * evp, + timer_t * timerid ) +{ + +} + +int timer_delete( timer_t timerid ) +{ + +} + +int timer_getoverrun( timer_t timerid ) +{ + +} + +int timer_gettime( timer_t timerid, + struct itimerspec * value ) +{ + +} + +int timer_settime( timer_t timerid, + int flags, + const struct itimerspec * value, + struct itimerspec * ovalue ) +{ + +} diff --git a/src/visor/supervisor/workers/scall/build.mk b/src/visor/supervisor/workers/scall/build.mk new file mode 100644 index 00000000..c07c5394 --- /dev/null +++ b/src/visor/supervisor/workers/scall/build.mk @@ -0,0 +1,17 @@ +# +# CYANCORE LICENSE +# Copyrights (C) 2019, Cyancore Team +# +# File Name : build.mk +# Description : This file builds worker sources +# Primary Author : Akash Kollipara [akashkollipara@gmail.com] +# Organisation : Cyancore Core-Team +# + +MCALL_DIR := $(GET_PATH) + +LD_SUPPLEMENT += $(MCALL_DIR)/scall.ld.sx + +DIR := $(MCALL_DIR) + +include mk/obj.mk diff --git a/src/visor/supervisor/workers/scall/scall.ld.sx b/src/visor/supervisor/workers/scall/scall.ld.sx new file mode 100644 index 00000000..8771727f --- /dev/null +++ b/src/visor/supervisor/workers/scall/scall.ld.sx @@ -0,0 +1,22 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2019, Cyancore Team + * + * File Name : scall.ld.sx + * Description : This file contains memory layout for the + * cyancore scall framework + * Primary Author : Pranjal Chanda [pranjalchanda08@gmail.com] + * Organisation : Cyancore Core-Team + */ + +SECTIONS +{ + .scall_table : + { + PROVIDE(_scall_table_start = .); + KEEP(*(.scall)) + PROVIDE(_scall_table_end = .); + } +} + +INSERT BEFORE .stack; diff --git a/src/visor/supervisor/workers/scall/supervisory_call.c b/src/visor/supervisor/workers/scall/supervisory_call.c new file mode 100644 index 00000000..fb1c8f93 --- /dev/null +++ b/src/visor/supervisor/workers/scall/supervisory_call.c @@ -0,0 +1,55 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2019, Cyancore Team + * + * File Name : supervisory_call.c + * Description : This file contains sources of supervisory call handler + * Primary Author : Pranjal Chanda [pranjalchanda08@gmail.com] + * Organisation : Cyancore Core-Team + */ + +#include +#include +#include +#include +#include +#include + +/* mcall Table start and end defined in linker script mcall.ld.sx */ +extern scall_t _scall_table_start; +extern scall_t _scall_table_end; + +void super_call(scall_id_t id, unsigned int a0, unsigned int a1, unsigned int a2, sret_t *ret) +{ + /* mcall Table pointer */ + scall_t *ptr; + + /* Assign "ret" parameters to default error case, + * on execution of the call, the ret will updated. + */ + ret->p = (uintptr_t) NULL; + ret->size = 0; + ret->status = error_mcall_code_inval; + + /* Assign the pointer to start of table */ + ptr = &_scall_table_start; + + /* Run through the table till the end */ + while(ptr <= & _scall_table_end) + { + /* Check if the mcall ID matches */ + if(ptr->id == id) + { + /* Execute the callback function and update the "ret" */ + if (ptr->callback != NULL) + { + *ret = ptr->callback(a0, a1, a2); + } + /* Stop parsing the table and return */ + break; + } + /* Increament location pointer */ + ptr++; + } + return; +} diff --git a/src/visor/terravisor/workers/mcall/build.mk b/src/visor/terravisor/workers/mcall/build.mk index b8c6f9b2..c07c5394 100644 --- a/src/visor/terravisor/workers/mcall/build.mk +++ b/src/visor/terravisor/workers/mcall/build.mk @@ -10,7 +10,7 @@ MCALL_DIR := $(GET_PATH) -LD_SUPPLEMENT += $(MCALL_DIR)/mcall.ld.sx +LD_SUPPLEMENT += $(MCALL_DIR)/scall.ld.sx DIR := $(MCALL_DIR) diff --git a/src/visor/terravisor/workers/mcall/machine_call.c b/src/visor/terravisor/workers/mcall/machine_call.c index 46e7f6bf..a374a10f 100644 --- a/src/visor/terravisor/workers/mcall/machine_call.c +++ b/src/visor/terravisor/workers/mcall/machine_call.c @@ -41,7 +41,10 @@ void machine_call(mcall_id_t id, unsigned int a0, unsigned int a1, unsigned int if(ptr->id == id) { /* Execute the callback function and update the "ret" */ - *ret = ptr->callback(a0, a1, a2); + if (ptr->callback != NULL) + { + *ret = ptr->callback(a0, a1, a2); + } /* Stop parsing the table and return */ break; } From 2310ea8d2e164df0e3c11a543104079cee5dbf8d Mon Sep 17 00:00:00 2001 From: Pranjal Chanda Date: Wed, 5 Jan 2022 21:28:15 +0530 Subject: [PATCH 22/31] build.mk bugfix --- src/visor/terravisor/workers/mcall/build.mk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/visor/terravisor/workers/mcall/build.mk b/src/visor/terravisor/workers/mcall/build.mk index c07c5394..b8c6f9b2 100644 --- a/src/visor/terravisor/workers/mcall/build.mk +++ b/src/visor/terravisor/workers/mcall/build.mk @@ -10,7 +10,7 @@ MCALL_DIR := $(GET_PATH) -LD_SUPPLEMENT += $(MCALL_DIR)/scall.ld.sx +LD_SUPPLEMENT += $(MCALL_DIR)/mcall.ld.sx DIR := $(MCALL_DIR) From 7378ad945935523e87eb9e0198c12aeecc95370f Mon Sep 17 00:00:00 2001 From: Pranjal Chanda Date: Wed, 5 Jan 2022 22:49:20 +0530 Subject: [PATCH 23/31] Add scall_id into scall_id_t --- src/include/status.h | 1 + src/include/supervisor_call.h | 58 ++- src/lib/posix/src/posix_time.c | 10 + src/lib/posix/src/posix_utils.c | 337 ++++++++++++++++++ .../workers/scall/supervisory_call.c | 6 +- 5 files changed, 407 insertions(+), 5 deletions(-) create mode 100644 src/lib/posix/src/posix_utils.c diff --git a/src/include/status.h b/src/include/status.h index a5cb36ef..856a190e 100644 --- a/src/include/status.h +++ b/src/include/status.h @@ -39,6 +39,7 @@ typedef enum status error_hcall = -0x0400, /* Syscall related error */ error_scall = -0x0500, + error_scall_code_inval = -0x0501, /* Memory related error */ error_memory = -0x0600, error_memory_low = -0x0601, diff --git a/src/include/supervisor_call.h b/src/include/supervisor_call.h index 8210e9f7..4dbbee6e 100644 --- a/src/include/supervisor_call.h +++ b/src/include/supervisor_call.h @@ -17,7 +17,59 @@ // Supervisor call IDs typedef enum scall_id { - scall_def_placeholder + scall_id_generic = 0x0000, +/* pthread related */ + scall_id_pthread_attr_destroy = 0x1000, + scall_id_pthread_attr_getdetachstate, + scall_id_pthread_attr_getschedparam, + scall_id_pthread_attr_getstacksize, + scall_id_pthread_attr_init, + scall_id_pthread_attr_setdetachstate, + scall_id_pthread_attr_setschedparam, + scall_id_pthread_attr_setschedpolicy, + scall_id_pthread_attr_setstacksize, + scall_id_pthread_barrier_destroy, + scall_id_pthread_barrier_init, + scall_id_pthread_barrier_wait, + scall_id_pthread_create, + scall_id_pthread_cond_broadcast, + scall_id_pthread_cond_destroy, + scall_id_pthread_cond_init, + scall_id_pthread_cond_signal, + scall_id_pthread_cond_timedwait, + scall_id_pthread_equal, + scall_id_pthread_exit, + scall_id_pthread_getschedparam, + scall_id_pthread_join, + scall_id_pthread_mutex_destroy, + scall_id_pthread_mutex_init, + scall_id_pthread_mutex_timedlock, + scall_id_pthread_mutex_unlock, + scall_id_pthread_mutexattr_destroy, + scall_id_pthread_mutexattr_gettype, + scall_id_pthread_mutexattr_init, + scall_id_pthread_mutexattr_settype, + scall_id_pthread_self, + scall_id_pthread_setschedparam, +/* mqueue related */ + scall_id_mq_close, + scall_id_mq_getattr, + scall_id_mq_open, + scall_id_mq_timedreceive, + scall_id_mq_timedsend, + scall_id_mq_unlink, +/* semaphore related */ + scall_id_sem_init, + scall_id_sem_destroy, + scall_id_sem_getvalue, + scall_id_sem_post, + scall_id_sem_timedwait, + scall_id_sem_trywait, +/* scheduler related */ + scall_id_sched_get_max_priority, + scall_id_sched_get_min_priority, + scall_id_sched_yield, + } scall_id_t; typedef struct sret @@ -27,10 +79,12 @@ typedef struct sret status_t status; } sret_t; +typedef void * sargs; + typedef struct scall { scall_id_t id; - sret_t (*callback)(unsigned int a0, unsigned int a1, unsigned int a2); + sret_t (*callback)(sargs args); } scall_t; #define INCLUDE_SCALL(_name, _id , _callback) \ diff --git a/src/lib/posix/src/posix_time.c b/src/lib/posix/src/posix_time.c index 11c00e74..3ef75fe2 100644 --- a/src/lib/posix/src/posix_time.c +++ b/src/lib/posix/src/posix_time.c @@ -1,3 +1,13 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2019, Cyancore Team + * + * File Name : posix_time.c + * Description : This file consists of posix time related call handles + * Primary Author : Pranjal Chanda [pranjalchanda08@gmail.com] + * Organisation : Cyancore Core-Team + */ + #include #include diff --git a/src/lib/posix/src/posix_utils.c b/src/lib/posix/src/posix_utils.c new file mode 100644 index 00000000..18a7c70b --- /dev/null +++ b/src/lib/posix/src/posix_utils.c @@ -0,0 +1,337 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2019, Cyancore Team + * + * File Name : posix_utils.c + * Description : This file consists of posix utils call handles + * Primary Author : Pranjal Chanda [pranjalchanda08@gmail.com] + * Organisation : Cyancore Core-Team + */ + +#include +#include + +size_t UTILS_strnlen( const char * const pcString, size_t xMaxLength ) +{ + size_t len = 0; + if (pcString != NULL) + { + while((pcString[len] != '\0') && (len < xMaxLength)) + { + len++; + } + } + + return len; +} +int UTILS_AbsoluteTimespecToDeltaTicks( const struct timespec * const pxAbsoluteTime, + const struct timespec * const pxCurrentTime, + TickType_t * const pxResult ) +{ + int err_status = 0; + struct timespec time_diff = { 0 }; + + /* Check parameters. */ + if( ( pxAbsoluteTime == NULL ) || ( pxCurrentTime == NULL ) || ( pxResult == NULL ) ) + { + err_status = EINVAL; + } + + /* Calculate the difference between the current time and absolute time. */ + if( err_status == 0 ) + { + err_status = UTILS_TimespecSubtract( pxAbsoluteTime, pxCurrentTime, &time_diff ); + + if( err_status == 1 ) + { + /* pxAbsoluteTime was in the past. */ + err_status = ETIMEDOUT; + } + else if( err_status == -1 ) + { + /* error */ + err_status = EINVAL; + } + } + + /* Convert the time difference to ticks. */ + if( err_status == 0 ) + { + err_status = UTILS_TimespecToTicks( &time_diff, pxResult ); + } + + return err_status; +} + +int UTILS_TimespecToTicks( const struct timespec * const pxTimespec, + TickType_t * const pxResult ) +{ + int err_staus = 0; + int64_t total_ticks = 0; + long nano_secs = 0; + + /* Check parameters. */ + if( ( pxTimespec == NULL ) || ( pxResult == NULL ) ) + { + err_staus = EINVAL; + } + else if( ( err_staus == 0 ) && ( UTILS_ValidateTimespec( pxTimespec ) == false ) ) + { + err_staus = EINVAL; + } + + if( err_staus == 0 ) + { + /* Convert timespec.tv_sec to ticks. */ + total_ticks = ( int64_t ) configTICK_RATE_HZ * ( pxTimespec->tv_sec ); + + /* Convert timespec.tv_nsec to ticks. This value does not have to be checked + * for overflow because a valid timespec has 0 <= tv_nsec < 1000000000 and + * NANOSECONDS_PER_TICK > 1. */ + nano_secs = pxTimespec->tv_nsec / ( long ) NANOSECONDS_PER_TICK + /* Whole nanoseconds. */ + ( long ) ( pxTimespec->tv_nsec % ( long ) NANOSECONDS_PER_TICK != 0 ); /* Add 1 to round up if needed. */ + + /* Add the nanoseconds to the total ticks. */ + total_ticks += ( int64_t ) nano_secs; + + /* Check for overflow */ + if( total_ticks < 0 ) + { + err_staus = EINVAL; + } + else + { + /* check if TickType_t is 32 bit or 64 bit */ + uint32_t ulTickTypeSize = sizeof( TickType_t ); + + /* check for downcast overflow */ + if( ulTickTypeSize == sizeof( uint32_t ) ) + { + if( total_ticks > UINT_MAX ) + { + err_staus = EINVAL; + } + } + } + + /* Write result. */ + *pxResult = ( TickType_t ) total_ticks; + } + + return err_staus; +} + +void UTILS_NanosecondsToTimespec( int64_t llSource, + struct timespec * const pxDestination ) +{ + long lCarrySec = 0; + + /* Convert to timespec. */ + pxDestination->tv_sec = ( time_t ) ( llSource / NANOSECONDS_PER_SECOND ); + pxDestination->tv_nsec = ( long ) ( llSource % NANOSECONDS_PER_SECOND ); + + /* Subtract from tv_sec if tv_nsec < 0. */ + if( pxDestination->tv_nsec < 0L ) + { + /* Compute the number of seconds to carry. */ + lCarrySec = ( pxDestination->tv_nsec / ( long ) NANOSECONDS_PER_SECOND ) + 1L; + + pxDestination->tv_sec -= ( time_t ) ( lCarrySec ); + pxDestination->tv_nsec += lCarrySec * ( long ) NANOSECONDS_PER_SECOND; + } +} + +int UTILS_TimespecAdd( const struct timespec * const x, + const struct timespec * const y, + struct timespec * const pxResult ) +{ + int64_t llPartialSec = 0; + int err_status = 0; + + /* Check parameters. */ + if( ( pxResult == NULL ) || ( x == NULL ) || ( y == NULL ) ) + { + err_status = -1; + } + + if( err_status == 0 ) + { + /* Perform addition. */ + pxResult->tv_nsec = x->tv_nsec + y->tv_nsec; + + /* check for overflow in case nsec value was invalid */ + if( pxResult->tv_nsec < 0 ) + { + err_status = 1; + } + else + { + llPartialSec = ( pxResult->tv_nsec ) / NANOSECONDS_PER_SECOND; + pxResult->tv_nsec = ( pxResult->tv_nsec ) % NANOSECONDS_PER_SECOND; + pxResult->tv_sec = x->tv_sec + y->tv_sec + llPartialSec; + + /* check for overflow */ + if( pxResult->tv_sec < 0 ) + { + err_status = 1; + } + } + } + + return err_status; +} + +int UTILS_TimespecAddNanoseconds( const struct timespec * const x, + int64_t lnano_secs, + struct timespec * const pxResult ) +{ + int64_t llTotalNSec = 0; + int err_status = 0; + + /* Check parameters. */ + if( ( pxResult == NULL ) || ( x == NULL ) ) + { + err_status = -1; + } + + if( err_status == 0 ) + { + /* add nano seconds */ + llTotalNSec = x->tv_nsec + lnano_secs; + + /* check for nano seconds overflow */ + if( llTotalNSec < 0 ) + { + err_status = 1; + } + else + { + pxResult->tv_nsec = llTotalNSec % NANOSECONDS_PER_SECOND; + pxResult->tv_sec = x->tv_sec + ( llTotalNSec / NANOSECONDS_PER_SECOND ); + + /* check for seconds overflow */ + if( pxResult->tv_sec < 0 ) + { + err_status = 1; + } + } + } + + return err_status; +} + +int UTILS_TimespecSubtract( const struct timespec * const x, + const struct timespec * const y, + struct timespec * const pxResult ) +{ + int iCompareResult = 0; + int err_status = 0; + + /* Check parameters. */ + if( ( pxResult == NULL ) || ( x == NULL ) || ( y == NULL ) ) + { + err_status = -1; + } + + if( err_status == 0 ) + { + iCompareResult = UTILS_TimespecCompare( x, y ); + + /* if x < y then result would be negative, return 1 */ + if( iCompareResult == -1 ) + { + err_status = 1; + } + else if( iCompareResult == 0 ) + { + /* if times are the same return zero */ + pxResult->tv_sec = 0; + pxResult->tv_nsec = 0; + } + else + { + /* If x > y Perform subtraction. */ + pxResult->tv_sec = x->tv_sec - y->tv_sec; + pxResult->tv_nsec = x->tv_nsec - y->tv_nsec; + + /* check if nano seconds value needs to borrow */ + if( pxResult->tv_nsec < 0 ) + { + /* Based on comparison, tv_sec > 0 */ + pxResult->tv_sec--; + pxResult->tv_nsec += ( long ) NANOSECONDS_PER_SECOND; + } + + /* if nano second is negative after borrow, it is an overflow error */ + if( pxResult->tv_nsec < 0 ) + { + err_status = -1; + } + } + } + + return err_status; +} + +int UTILS_TimespecCompare( const struct timespec * const x, + const struct timespec * const y ) +{ + int err_status = 0; + + /* Check parameters */ + if( ( x == NULL ) && ( y == NULL ) ) + { + err_status = 0; + } + else if( y == NULL ) + { + err_status = 1; + } + else if( x == NULL ) + { + err_status = -1; + } + else if( x->tv_sec > y->tv_sec ) + { + err_status = 1; + } + else if( x->tv_sec < y->tv_sec ) + { + err_status = -1; + } + else + { + /* seconds are equal compare nano seconds */ + if( x->tv_nsec > y->tv_nsec ) + { + err_status = 1; + } + else if( x->tv_nsec < y->tv_nsec ) + { + err_status = -1; + } + else + { + err_status = 0; + } + } + + return err_status; +} + +bool UTILS_ValidateTimespec( const struct timespec * const pxTimespec ) +{ + bool xReturn = false; + + if( pxTimespec != NULL ) + { + /* Verify 0 <= tv_nsec < 1000000000. */ + if( ( pxTimespec->tv_nsec >= 0 ) && + ( pxTimespec->tv_nsec < NANOSECONDS_PER_SECOND ) ) + { + xReturn = true; + } + } + + return xReturn; +} diff --git a/src/visor/supervisor/workers/scall/supervisory_call.c b/src/visor/supervisor/workers/scall/supervisory_call.c index fb1c8f93..df7f4735 100644 --- a/src/visor/supervisor/workers/scall/supervisory_call.c +++ b/src/visor/supervisor/workers/scall/supervisory_call.c @@ -19,7 +19,7 @@ extern scall_t _scall_table_start; extern scall_t _scall_table_end; -void super_call(scall_id_t id, unsigned int a0, unsigned int a1, unsigned int a2, sret_t *ret) +void super_call(scall_id_t id, sargs arg, sret_t *ret) { /* mcall Table pointer */ scall_t *ptr; @@ -29,7 +29,7 @@ void super_call(scall_id_t id, unsigned int a0, unsigned int a1, unsigned int a2 */ ret->p = (uintptr_t) NULL; ret->size = 0; - ret->status = error_mcall_code_inval; + ret->status = error_scall_code_inval; /* Assign the pointer to start of table */ ptr = &_scall_table_start; @@ -43,7 +43,7 @@ void super_call(scall_id_t id, unsigned int a0, unsigned int a1, unsigned int a2 /* Execute the callback function and update the "ret" */ if (ptr->callback != NULL) { - *ret = ptr->callback(a0, a1, a2); + *ret = ptr->callback(arg); } /* Stop parsing the table and return */ break; From 7085e3977b0533691fdcaa0034f0f4ffc23c7716 Mon Sep 17 00:00:00 2001 From: Pranjal Chanda Date: Fri, 7 Jan 2022 23:05:56 +0530 Subject: [PATCH 24/31] MQueue implementation Initial commit --- src/include/supervisor_call.h | 11 +- src/include/visor/supervisor/workers.h | 4 +- src/lib/posix/cc_posix_config.h | 36 ++ src/lib/posix/cc_posix_internal.h | 3 + src/lib/posix/cc_posix_types.h | 2 + src/lib/posix/include/errno.h | 35 +- src/lib/posix/include/mqueue.h | 67 ++-- src/lib/posix/include/time.h | 32 +- src/lib/posix/include/utils.h | 5 + src/lib/posix/src/posix_mqueue.c | 315 ++++++++++++++++++ src/lib/posix/src/posix_utils.c | 20 +- .../workers/scall/supervisory_call.c | 4 +- 12 files changed, 454 insertions(+), 80 deletions(-) create mode 100644 src/lib/posix/cc_posix_config.h create mode 100644 src/lib/posix/src/posix_mqueue.c diff --git a/src/include/supervisor_call.h b/src/include/supervisor_call.h index 4dbbee6e..97638e95 100644 --- a/src/include/supervisor_call.h +++ b/src/include/supervisor_call.h @@ -18,6 +18,7 @@ typedef enum scall_id { scall_id_generic = 0x0000, + scall_id_is_irq, /* pthread related */ scall_id_pthread_attr_destroy = 0x1000, scall_id_pthread_attr_getdetachstate, @@ -51,12 +52,14 @@ typedef enum scall_id scall_id_pthread_mutexattr_settype, scall_id_pthread_self, scall_id_pthread_setschedparam, + scall_id_pthread_delay_ticks, /* mqueue related */ scall_id_mq_close, + scall_id_mq_setattr, scall_id_mq_getattr, scall_id_mq_open, - scall_id_mq_timedreceive, - scall_id_mq_timedsend, + scall_id_mq_receive, + scall_id_mq_send, scall_id_mq_unlink, /* semaphore related */ scall_id_sem_init, @@ -79,12 +82,10 @@ typedef struct sret status_t status; } sret_t; -typedef void * sargs; - typedef struct scall { scall_id_t id; - sret_t (*callback)(sargs args); + sret_t (*callback)(unsigned int a0, unsigned int a1, unsigned int a2); } scall_t; #define INCLUDE_SCALL(_name, _id , _callback) \ diff --git a/src/include/visor/supervisor/workers.h b/src/include/visor/supervisor/workers.h index 7e6b090c..e966d14d 100644 --- a/src/include/visor/supervisor/workers.h +++ b/src/include/visor/supervisor/workers.h @@ -11,4 +11,6 @@ #pragma once -void super_call(scall_id, unsigned int, unsigned int, unsigned int, sret_t *); +#include + +void super_call(scall_id_t id, unsigned int a0, unsigned int a1, unsigned int a2, sret_t *ret); diff --git a/src/lib/posix/cc_posix_config.h b/src/lib/posix/cc_posix_config.h new file mode 100644 index 00000000..f5d0ec48 --- /dev/null +++ b/src/lib/posix/cc_posix_config.h @@ -0,0 +1,36 @@ +#pragma once + + +#define posxconfigMAX_DELAY (size_t)(~0) + +/******************* + * MQUEUE configs + *******************/ + +/** + * @brief MQUEUE max name length in bytes + */ +#ifndef posixconfigMQ_NAME_LEN_MAX +#define posixconfigMQ_NAME_LEN_MAX (16) +#endif + +/** + * @brief MQUEUE max munber of queues allowed + */ +#ifndef posixconfigMQ_MAX_QUEUE +#define posixconfigMQ_MAX_QUEUE (5) +#endif + +/** + * @brief MQUEUE Maximum number of messages in an mq at one time + */ +#ifndef posixconfigMQ_MAX_MESSAGES +#define posixconfigMQ_MAX_MESSAGES (10) +#endif + +/** + * @brief MQUEUE Maximum size (in bytes) of each message + */ +#ifndef posixconfigMQ_MAX_SIZE +#define posixconfigMQ_MAX_SIZE (128) +#endif diff --git a/src/lib/posix/cc_posix_internal.h b/src/lib/posix/cc_posix_internal.h index 071e86d8..89558d9a 100644 --- a/src/lib/posix/cc_posix_internal.h +++ b/src/lib/posix/cc_posix_internal.h @@ -1,5 +1,8 @@ #ifndef _CC_POSIX_INTERNAL_H_ #define _CC_POSIX_INTERNAL_H_ + +#include + /** * @file cc_posix_internal.h * @brief Internal structs and initializers for CC+POSIX. diff --git a/src/lib/posix/cc_posix_types.h b/src/lib/posix/cc_posix_types.h index 636fcece..1b827432 100644 --- a/src/lib/posix/cc_posix_types.h +++ b/src/lib/posix/cc_posix_types.h @@ -55,4 +55,6 @@ typedef void * PthreadBarrierType_t; #endif +typedef unsigned int TickType_t; + #endif /* _CC_POSIX_INTERNAL_TYPES_H_ */ diff --git a/src/lib/posix/include/errno.h b/src/lib/posix/include/errno.h index 8238859e..7ca898d6 100644 --- a/src/lib/posix/include/errno.h +++ b/src/lib/posix/include/errno.h @@ -36,23 +36,24 @@ * @name Definition of POSIX errnos. */ /**@{ */ -#define EPERM -error_access /**< Operation not permitted. */ -#define ENOENT -error_file_not_found /**< No such file or directory. */ -#define EBADF -error_file_desc /**< Bad file descriptor. */ -#define EAGAIN -error_device_inval /**< Resource unavailable, try again. */ -#define ENOMEM -error_memory_low /**< Not enough space. */ -#define EEXIST -error_file_exist /**< File exists. */ -#define EBUSY -error_device_busy /**< Device or resource busy. */ -#define EINVAL -error_func_inval_arg /**< Invalid argument. */ -#define ENOSPC -error_file_no_space /**< No space left on device. */ -#define ERANGE -error_math_large_val /**< Result too large. */ -#define ENAMETOOLONG -error_file_long_name /**< File name too long. */ -#define EDEADLK -error_os_deadlock /**< Resource deadlock would occur. */ -#define EOVERFLOW -error_overflow /**< Value too large to be stored in data type. */ -#define ENOSYS -error_func_inval /**< Function not supported. */ -#define EMSGSIZE -error_mesg_long /**< Message too long. */ -#define ENOTSUP -error_func_inval /**< Operation not supported. */ -#define ETIMEDOUT -error_net_con_timeout /**< Connection timed out. */ +#define SUCCESS success /**< Operation success*/ +#define EPERM (-error_access) /**< Operation not permitted. */ +#define ENOENT (-error_file_not_found) /**< No such file or directory. */ +#define EBADF (-error_file_desc) /**< Bad file descriptor. */ +#define EAGAIN (-error_device_inval) /**< Resource unavailable, try again. */ +#define ENOMEM (-error_memory_low) /**< Not enough space. */ +#define EEXIST (-error_file_exist) /**< File exists. */ +#define EBUSY (-error_device_busy) /**< Device or resource busy. */ +#define EINVAL (-error_func_inval_arg) /**< Invalid argument. */ +#define ENOSPC (-error_file_no_space) /**< No space left on device. */ +#define ERANGE (-error_math_large_val) /**< Result too large. */ +#define ENAMETOOLONG (-error_file_long_name) /**< File name too long. */ +#define EDEADLK (-error_os_deadlock) /**< Resource deadlock would occur. */ +#define EOVERFLOW (-error_overflow) /**< Value too large to be stored in data type. */ +#define ENOSYS (-error_func_inval) /**< Function not supported. */ +#define EMSGSIZE (-error_mesg_long) /**< Message too long. */ +#define ENOTSUP (-error_func_inval) /**< Operation not supported. */ +#define ETIMEDOUT (-error_net_con_timeout)/**< Connection timed out. */ /**@} */ /** diff --git a/src/lib/posix/include/mqueue.h b/src/lib/posix/include/mqueue.h index 3bad2463..35e5ac86 100644 --- a/src/lib/posix/include/mqueue.h +++ b/src/lib/posix/include/mqueue.h @@ -9,23 +9,32 @@ #define _CC_POSIX_MQUEUE_H_ /* CC+POSIX includes. */ -#include "lib/posix/include/time.h" +#include +#include /** - * @brief Message queue descriptor. + * @brief Message queue attributes. */ -typedef void * mqd_t; +typedef struct mq_attr +{ + long mq_flags; /**< Message queue flags. */ + long mq_maxmsg; /**< Maximum number of messages. */ + long mq_msgsize; /**< Maximum message size. */ + long mq_curmsgs; /**< Number of messages currently queued. */ +}mq_attr_t; /** - * @brief Message queue attributes. + * @brief Message queue descriptor. */ -struct mq_attr +typedef struct mqd_section { - long mq_flags; /**< Message queue flags. */ - long mq_maxmsg; /**< Maximum number of messages. */ - long mq_msgsize; /**< Maximum message size. */ - long mq_curmsgs; /**< Number of messages currently queued. */ -}; + size_t id; + char mq_name[posixconfigMQ_NAME_LEN_MAX]; + mq_attr_t attr; + uintptr_t kernel_buff; +}mqd_section_t; + +typedef void * mqd_t; /** * @brief Close a message queue. @@ -54,7 +63,7 @@ int mq_close( mqd_t mqdes ); * DBADF - The mqdes argument is not a valid message queue descriptor. */ int mq_getattr( mqd_t mqdes, - struct mq_attr * mqstat ); + mq_attr_t * mqstat ); /** * @brief Open a message queue. @@ -83,10 +92,10 @@ int mq_getattr( mqd_t mqdes, *
* ENOENT - O_CREAT is not set and the named message queue does not exist. */ -mqd_t mq_open( const char * name, - int oflag, - mode_t mode, - struct mq_attr * attr ); +mqd_t mq_open( const char * name, + int oflag, + mode_t mode, + mq_attr_t * attr ); /** * @brief Receive a message from a message queue. @@ -111,9 +120,9 @@ mqd_t mq_open( const char * name, * EAGAIN - O_NONBLOCK was set in the message description associated with mqdes, and the specified message queue is empty. */ ssize_t mq_receive( mqd_t mqdes, - char * msg_ptr, - size_t msg_len, - unsigned int * msg_prio ); + char * msg_ptr, + size_t msg_len, + unsigned int * msg_prio ); /** * @brief Send a message to a message queue. @@ -139,9 +148,9 @@ ssize_t mq_receive( mqd_t mqdes, * and the specified message queue is full. */ int mq_send( mqd_t mqdes, - const char * msg_ptr, - size_t msg_len, - unsigned msg_prio ); + const char * msg_ptr, + size_t msg_len, + unsigned msg_prio ); /** * @brief Receive a message from a message queue with timeout. @@ -169,10 +178,10 @@ int mq_send( mqd_t mqdes, * EAGAIN - O_NONBLOCK was set in the message description associated with mqdes, and the specified message queue is empty. */ ssize_t mq_timedreceive( mqd_t mqdes, - char * msg_ptr, - size_t msg_len, - unsigned * msg_prio, - const struct timespec * abstime ); + char * msg_ptr, + size_t msg_len, + unsigned * msg_prio, + const struct timespec * abstime ); /** * @brief Send a message to a message queue with timeout. @@ -201,10 +210,10 @@ ssize_t mq_timedreceive( mqd_t mqdes, * and the specified message queue is full. */ int mq_timedsend( mqd_t mqdes, - const char * msg_ptr, - size_t msg_len, - unsigned msg_prio, - const struct timespec * abstime ); + const char * msg_ptr, + size_t msg_len, + unsigned msg_prio, + const struct timespec * abstime ); /** * @brief Remove a message queue. diff --git a/src/lib/posix/include/time.h b/src/lib/posix/include/time.h index 02b51a5a..9bc93ec9 100644 --- a/src/lib/posix/include/time.h +++ b/src/lib/posix/include/time.h @@ -50,11 +50,11 @@ /** * @brief represents an elapsed time */ - struct timespec + typedef struct timespec { time_t tv_sec; /**< Seconds. */ long tv_nsec; /**< Nanoseconds. */ - }; + }timespec_t; #endif #if !defined( posixconfigENABLE_ITIMERSPEC ) || ( posixconfigENABLE_ITIMERSPEC == 1 ) @@ -62,11 +62,11 @@ /** * @brief timer */ - struct itimerspec + typedef struct itimerspec { - struct timespec it_interval; /**< Timer period. */ - struct timespec it_value; /**< Timer expiration. */ - }; + timespec_t it_interval; /**< Timer period. */ + timespec_t it_value; /**< Timer expiration. */ + }itimerspec_t; #endif /** @@ -105,7 +105,7 @@ int clock_getcpuclockid( pid_t pid, * @retval 0 - Upon successful execution */ int clock_getres( clockid_t clock_id, - struct timespec * res ); + timespec_t * res ); /** * @brief Returns the current value for the specified clock, clock_id. @@ -118,7 +118,7 @@ int clock_getres( clockid_t clock_id, * @retval 0 - Upon successful completion. */ int clock_gettime( clockid_t clock_id, - struct timespec * tp ); + timespec_t * tp ); /** * @brief High resolution sleep with specifiable clock. @@ -134,8 +134,8 @@ int clock_gettime( clockid_t clock_id, */ int clock_nanosleep( clockid_t clock_id, int flags, - const struct timespec * rqtp, - struct timespec * rmtp ); + const timespec_t * rqtp, + timespec_t * rmtp ); /** * @brief Sets the time for the specified clock. @@ -147,7 +147,7 @@ int clock_nanosleep( clockid_t clock_id, * @note This function is currently unsupported, as Kernel does not provide a function to modify the tick count. */ int clock_settime( clockid_t clock_id, - const struct timespec * tp ); + const timespec_t * tp ); /** * @brief High resolution sleep. @@ -160,8 +160,8 @@ int clock_settime( clockid_t clock_id, * @retval -1 - The rqtp argument is invalid OR the rqtp argument specified a nanosecond value less than zero or greater than or equal to 1000 million. * */ -int nanosleep( const struct timespec * rqtp, - struct timespec * rmtp ); +int nanosleep( const timespec_t * rqtp, + timespec_t * rmtp ); /** * @brief Create a per-process timer. @@ -210,7 +210,7 @@ int timer_getoverrun( timer_t timerid ); * @retval 0 - Upon successful completion. */ int timer_gettime( timer_t timerid, - struct itimerspec * value ); + itimerspec_t * value ); /** * @brief Set the time until the next expiration of the timer. @@ -227,7 +227,7 @@ int timer_gettime( timer_t timerid, */ int timer_settime( timer_t timerid, int flags, - const struct itimerspec * value, - struct itimerspec * ovalue ); + const itimerspec_t * value, + itimerspec_t * ovalue ); #endif /* ifndef _CC_POSIX_TIME_H_ */ diff --git a/src/lib/posix/include/utils.h b/src/lib/posix/include/utils.h index 1788a1fe..92b2a373 100644 --- a/src/lib/posix/include/utils.h +++ b/src/lib/posix/include/utils.h @@ -13,6 +13,11 @@ /* CC+POSIX includes. */ #include +#define DO_NOTHING +#define RST_VAL (0) +#define ASSERT_IF_FALSE(con, type) RET_ERR_IF_FALSE(con, -EINVAL, type) +#define RET_ERR_IF_FALSE(con, ret, type) if(!(con)){return (type) ret;} + /** * @brief Calculates the length of pcString, up to xMaxLength. * diff --git a/src/lib/posix/src/posix_mqueue.c b/src/lib/posix/src/posix_mqueue.c new file mode 100644 index 00000000..d44b69a6 --- /dev/null +++ b/src/lib/posix/src/posix_mqueue.c @@ -0,0 +1,315 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2019, Cyancore Team + * + * File Name : posix_mqueue.c + * Description : This file consists of posix message queue related call handles + * Primary Author : Pranjal Chanda [pranjalchanda08@gmail.com] + * Organisation : Cyancore Core-Team + */ + +#include +#include +#include +#include +#include +#include + +#define DELAY_MIN_TICK (1U) + +sret_t g_mq_sys_ret; +size_t g_mq_id_ctr = 0; + +static int s_find_queue_in_desc_list(char * name, mqd_section_t * p_mqd_section); +static int s_queue_desc_allocator(size_t index, mqd_section_t ** p_mqd_section); +static int s_queue_desc_deallocator(mqd_section_t ** p_mqd_section); +static int s_mqueue_delay(TickType_t ticks); +static int s_queue_write(mqd_t mqdes, const void * buff, size_t size); +static int s_queue_read (mqd_t mqdes, const void * buff, size_t size); +static int s_acquire_resource(void); +static int s_release_resource(void); + +mqd_t mq_open( const char * name, + int oflag, + mode_t mode, + mq_attr_t * attr ) +{ + mqd_section_t * p_mqd_section = NULL; + size_t index; + + /* Check argument assertions */ + ASSERT_IF_FALSE(name != NULL, mqd_t); + ASSERT_IF_FALSE(UTILS_strnlen(name, posixconfigMQ_NAME_LEN_MAX) < posixconfigMQ_NAME_LEN_MAX, mqd_t); + + /* Return ENOENT if element already exist */ + RET_ERR_IF_FALSE(s_find_queue_in_desc_list(name, NULL) == SUCCESS, -ENOENT, mqd_t); + + /* Return ENOENT if oflag is not equal to O_CREAT */ + RET_ERR_IF_FALSE( oflag & O_CREAT, -ENOENT, mqd_t); + + /* Grab resource access else return EBUSY */ + RET_ERR_IF_FALSE( s_acquire_resource() == SUCCESS, -EBUSY, mqd_t); + + /* Find available slot in descriptor table else return ENOSPC */ + if(s_queue_desc_allocator(g_mq_id_ctr++, &p_mqd_section ) == SUCCESS) + { + if (attr != NULL) + { + /* Fill user attributes */ + memcpy(&(p_mqd_section->attr), attr, sizeof(mq_attr_t)); + } + else + { /* Fill default attribute vals */ + p_mqd_section->attr.mq_maxmsg = posixconfigMQ_MAX_MESSAGES; + p_mqd_section->attr.mq_msgsize = posixconfigMQ_MAX_SIZE; + } + + p_mqd_section->attr.mq_flags = oflag; + + /* Perform super_call */ + super_call(scall_id_mq_open, p_mqd_section->attr.mq_maxmsg, p_mqd_section->attr.mq_msgsize, 0, &g_mq_sys_ret); + if (g_mq_sys_ret.status != SUCCESS) + { + p_mqd_section = (mqd_section_t *) -ENOTSUP; + } + else + { + /* Set Kernel buffer ptr */ + p_mqd_section->kernel_buff = g_mq_sys_ret.p; + + /* Set queue name as specified */ + strcpy(&(p_mqd_section->mq_name), name ); + } + } + else + { + DO_NOTHING; + } + + RET_ERR_IF_FALSE( s_release_resource() == SUCCESS, -EBUSY, mqd_t); + + return (mqd_t) p_mqd_section; +} + +int mq_close( mqd_t mqdes ) +{ + int err = SUCCESS; + + ASSERT_IF_FALSE(mqdes != NULL, ssize_t); + + /* Grab resource access else return EBUSY */ + RET_ERR_IF_FALSE( s_acquire_resource() == SUCCESS, -EBUSY, int); + + /* Return ENOENT if element already exist */ + if (s_find_queue_in_desc_list(NULL, (mqd_section_t *) mqdes) == SUCCESS) + { + /* Reset attr values */ + memset(&((mqd_section_t *) mqdes)->attr, RST_VAL, sizeof(mq_attr_t)); + + /* Perform Super Call */ + super_call(scall_id_mq_close, ((mqd_section_t *) mqdes)->kernel_buff, 0, 0, &g_mq_sys_ret); + if (g_mq_sys_ret.status != SUCCESS) + { + err = -ENOTSUP; + } + else + { + /* Deallocate the descriptor section from user space */ + err = s_queue_desc_deallocator((mqd_section_t **) &mqdes); + } + } + else + { + err = -EBADF; + } + + RET_ERR_IF_FALSE( s_release_resource() == SUCCESS, -EBUSY, ssize_t); + + return err; +} + +int mq_getattr( mqd_t mqdes, + mq_attr_t * attr ) +{ + ASSERT_IF_FALSE(mqdes != NULL, ssize_t); + + memcpy(attr, &(((mqd_section_t *) mqdes)->attr), sizeof(mq_attr_t)); + + return SUCCESS; +} + +ssize_t mq_receive( mqd_t mqdes, + char * msg_ptr, + size_t msg_len, + unsigned int * msg_prio ) +{ + return mq_timedreceive( mqdes, msg_ptr, msg_len, msg_prio, NULL ); +} + +int mq_send( mqd_t mqdes, + const char * msg_ptr, + size_t msg_len, + unsigned msg_prio ) +{ + return mq_timedsend( mqdes, msg_ptr, msg_len, msg_prio, NULL ); +} + +ssize_t mq_timedreceive( mqd_t mqdes, + char * msg_ptr, + size_t msg_len, + unsigned * msg_prio _UNUSED, + const timespec_t * abstime ) +{ + ASSERT_IF_FALSE(mqdes != NULL, ssize_t); + ASSERT_IF_FALSE(msg_ptr != NULL, ssize_t); + ASSERT_IF_FALSE(msg_len > 0, ssize_t); + + ssize_t err = SUCCESS; + TickType_t abs_ticks; + + if (abstime == NULL) + { + abs_ticks = s_is_irq() ? RST_VAL : posxconfigMAX_DELAY; + } + + else + { + ASSERT_IF_FALSE (UTILS_TimespecToTicks(abstime, &abs_ticks) == SUCCESS, ssize_t); + } + + /* Grab resource access else return EBUSY */ + RET_ERR_IF_FALSE( s_acquire_resource() == SUCCESS, -EBUSY, ssize_t); + + if (s_find_queue_in_desc_list(NULL, (mqd_section_t *) mqdes) == SUCCESS) + { + do + { + /* Try to read queue from kernel */ + if (s_queue_read(mqdes, msg_ptr, msg_len) == SUCCESS) + { + break; + } + else + { + if (((mqd_section_t *) mqdes)->attr.mq_flags & O_NONBLOCK) + { + err = -ENOSPC; + break; + } + else + { + s_mqueue_delay(DELAY_MIN_TICK); + abs_ticks--; + + if (abs_ticks == 0) + { + err = -ETIMEDOUT; + } + else + { + DO_NOTHING; + } + } + } + }while(abs_ticks > 0); + } + else + { + err = -EBADF; + } + + RET_ERR_IF_FALSE( s_release_resource() == SUCCESS, -EBUSY, ssize_t); + + return err; +} + +int mq_timedsend( mqd_t mqdes, + const char * msg_ptr, + size_t msg_len, + unsigned msg_prio, + const struct timespec * abstime ) +{ + ASSERT_IF_FALSE(mqdes != NULL, int); + ASSERT_IF_FALSE(msg_ptr != NULL, int); + ASSERT_IF_FALSE(msg_len > 0, int); + + TODO(mq_timedsend); + + return SUCCESS; +} + +int mq_unlink( const char * name ) +{ + TODO(mq_unlink); + return SUCCESS; +} + +/************************************************************/ + +static int s_find_queue_in_desc_list(char * name, mqd_section_t * p_mqd_section) +{ + TODO(s_find_queue_in_desc_list); + return SUCCESS; +} + +static int s_queue_desc_allocator(size_t index, mqd_section_t ** p_mqd_section) +{ + TODO(s_queue_desc_allocator); + return SUCCESS; +} + +static int s_queue_desc_deallocator(mqd_section_t ** p_mqd_section) +{ + TODO(s_queue_desc_deallocator); + return SUCCESS; +} + +static int s_queue_write(mqd_t mqdes, const void * buff, size_t size) +{ + mqd_section_t * p_mqd_section = (mqd_section_t *) mqdes; + + RET_ERR_IF_FALSE((p_mqd_section->attr.mq_flags & O_RDONLY), -ENOTSUP, int); + + memcpy(p_mqd_section->kernel_buff, buff, size); + return SUCCESS; +} + +static int s_queue_read(mqd_t mqdes, const void * buff, size_t size) +{ + mqd_section_t * p_mqd_section = (mqd_section_t *) mqdes; + + RET_ERR_IF_FALSE((p_mqd_section->attr.mq_flags & O_WRONLY), -ENOTSUP, int); + + super_call(scall_id_mq_receive, p_mqd_section->kernel_buff, size, 0, &g_mq_sys_ret); + RET_ERR_IF_FALSE(g_mq_sys_ret.status == SUCCESS, -EBADF, int); + + memcpy(buff, (void *) (g_mq_sys_ret.p), size); + + return SUCCESS; +} + +static int s_mqueue_delay(TickType_t ticks) +{ + super_call(scall_id_pthread_delay_ticks, ticks, 0, 0, &g_mq_sys_ret); + RET_ERR_IF_FALSE(g_mq_sys_ret.status == SUCCESS, EAGAIN, int); + + return SUCCESS; +} + +static int s_acquire_resource(void) +{ + TODO(s_acquire_resource); + return SUCCESS; +} + +static int s_release_resource(void) +{ + TODO(s_release_resource); + return SUCCESS; +} + +static bool s_is_irq(void) +{ + super_call(scall_id_is_irq, 0, 0, 0, &g_mq_sys_ret); + return g_mq_sys_ret.status == SUCCESS; +} diff --git a/src/lib/posix/src/posix_utils.c b/src/lib/posix/src/posix_utils.c index 18a7c70b..7bcf7fa0 100644 --- a/src/lib/posix/src/posix_utils.c +++ b/src/lib/posix/src/posix_utils.c @@ -34,7 +34,7 @@ int UTILS_AbsoluteTimespecToDeltaTicks( const struct timespec * const pxAbsolute /* Check parameters. */ if( ( pxAbsoluteTime == NULL ) || ( pxCurrentTime == NULL ) || ( pxResult == NULL ) ) { - err_status = EINVAL; + err_status = -EINVAL; } /* Calculate the difference between the current time and absolute time. */ @@ -45,12 +45,12 @@ int UTILS_AbsoluteTimespecToDeltaTicks( const struct timespec * const pxAbsolute if( err_status == 1 ) { /* pxAbsoluteTime was in the past. */ - err_status = ETIMEDOUT; + err_status = -ETIMEDOUT; } else if( err_status == -1 ) { /* error */ - err_status = EINVAL; + err_status = -EINVAL; } } @@ -73,11 +73,11 @@ int UTILS_TimespecToTicks( const struct timespec * const pxTimespec, /* Check parameters. */ if( ( pxTimespec == NULL ) || ( pxResult == NULL ) ) { - err_staus = EINVAL; + err_staus = -EINVAL; } else if( ( err_staus == 0 ) && ( UTILS_ValidateTimespec( pxTimespec ) == false ) ) { - err_staus = EINVAL; + err_staus = -EINVAL; } if( err_staus == 0 ) @@ -97,7 +97,7 @@ int UTILS_TimespecToTicks( const struct timespec * const pxTimespec, /* Check for overflow */ if( total_ticks < 0 ) { - err_staus = EINVAL; + err_staus = -EINVAL; } else { @@ -109,7 +109,7 @@ int UTILS_TimespecToTicks( const struct timespec * const pxTimespec, { if( total_ticks > UINT_MAX ) { - err_staus = EINVAL; + err_staus = -EINVAL; } } } @@ -321,7 +321,7 @@ int UTILS_TimespecCompare( const struct timespec * const x, bool UTILS_ValidateTimespec( const struct timespec * const pxTimespec ) { - bool xReturn = false; + bool ret = false; if( pxTimespec != NULL ) { @@ -329,9 +329,9 @@ bool UTILS_ValidateTimespec( const struct timespec * const pxTimespec ) if( ( pxTimespec->tv_nsec >= 0 ) && ( pxTimespec->tv_nsec < NANOSECONDS_PER_SECOND ) ) { - xReturn = true; + ret = true; } } - return xReturn; + return ret; } diff --git a/src/visor/supervisor/workers/scall/supervisory_call.c b/src/visor/supervisor/workers/scall/supervisory_call.c index df7f4735..c53bfa2d 100644 --- a/src/visor/supervisor/workers/scall/supervisory_call.c +++ b/src/visor/supervisor/workers/scall/supervisory_call.c @@ -19,7 +19,7 @@ extern scall_t _scall_table_start; extern scall_t _scall_table_end; -void super_call(scall_id_t id, sargs arg, sret_t *ret) +void super_call(scall_id_t id, unsigned int a0, unsigned int a1, unsigned int a2, sret_t *ret) { /* mcall Table pointer */ scall_t *ptr; @@ -43,7 +43,7 @@ void super_call(scall_id_t id, sargs arg, sret_t *ret) /* Execute the callback function and update the "ret" */ if (ptr->callback != NULL) { - *ret = ptr->callback(arg); + *ret = ptr->callback(a0, a1, a2); } /* Stop parsing the table and return */ break; From 2b93bc73a5b7b41ed4b9c388870e260f51b59f14 Mon Sep 17 00:00:00 2001 From: Pranjal Chanda Date: Sun, 9 Jan 2022 21:52:32 +0530 Subject: [PATCH 25/31] Add placeholders for undefined functions. --- src/lib/posix/cc_posix_config.h | 3 - src/lib/posix/cc_posix_types.h | 7 +- src/lib/posix/include/mqueue.h | 3 +- src/lib/posix/src/posix_mqueue.c | 271 ++++++++++++++++++++----------- src/lib/posix/src/posix_time.c | 52 ++++-- 5 files changed, 219 insertions(+), 117 deletions(-) diff --git a/src/lib/posix/cc_posix_config.h b/src/lib/posix/cc_posix_config.h index f5d0ec48..0922104e 100644 --- a/src/lib/posix/cc_posix_config.h +++ b/src/lib/posix/cc_posix_config.h @@ -1,8 +1,5 @@ #pragma once - -#define posxconfigMAX_DELAY (size_t)(~0) - /******************* * MQUEUE configs *******************/ diff --git a/src/lib/posix/cc_posix_types.h b/src/lib/posix/cc_posix_types.h index 1b827432..8dfddcbe 100644 --- a/src/lib/posix/cc_posix_types.h +++ b/src/lib/posix/cc_posix_types.h @@ -55,6 +55,11 @@ typedef void * PthreadBarrierType_t; #endif -typedef unsigned int TickType_t; +#if( posixconfigUSE_16_BIT_TICKS == 1 ) + typedef uint16_t TickType_t; +#else + typedef size_t TickType_t; +#endif +#define posixconfigMAX_DELAY (TickType_t)(~0) #endif /* _CC_POSIX_INTERNAL_TYPES_H_ */ diff --git a/src/lib/posix/include/mqueue.h b/src/lib/posix/include/mqueue.h index 35e5ac86..67563a40 100644 --- a/src/lib/posix/include/mqueue.h +++ b/src/lib/posix/include/mqueue.h @@ -31,7 +31,8 @@ typedef struct mqd_section size_t id; char mq_name[posixconfigMQ_NAME_LEN_MAX]; mq_attr_t attr; - uintptr_t kernel_buff; + uintptr_t kernel_buff_send; + uintptr_t kernel_buff_recv; }mqd_section_t; typedef void * mqd_t; diff --git a/src/lib/posix/src/posix_mqueue.c b/src/lib/posix/src/posix_mqueue.c index d44b69a6..4654a952 100644 --- a/src/lib/posix/src/posix_mqueue.c +++ b/src/lib/posix/src/posix_mqueue.c @@ -10,24 +10,106 @@ #include #include +#include #include #include #include #include #define DELAY_MIN_TICK (1U) +#define s_is_irq() true sret_t g_mq_sys_ret; -size_t g_mq_id_ctr = 0; +size_t g_mq_id_ctr = RST_VAL; -static int s_find_queue_in_desc_list(char * name, mqd_section_t * p_mqd_section); -static int s_queue_desc_allocator(size_t index, mqd_section_t ** p_mqd_section); -static int s_queue_desc_deallocator(mqd_section_t ** p_mqd_section); -static int s_mqueue_delay(TickType_t ticks); -static int s_queue_write(mqd_t mqdes, const void * buff, size_t size); -static int s_queue_read (mqd_t mqdes, const void * buff, size_t size); -static int s_acquire_resource(void); -static int s_release_resource(void); +pthread_mutex_t g_mq_mutex; +pthread_mutexattr_t g_mq_mutex_attr = NULL; + +/********************* + * Static Functions + ********************/ + +static int s_find_queue_in_desc_list(char * name, mqd_section_t * p_mqd_section) +{ + TODO(name); + TODO(p_mqd_section); + return SUCCESS; +} + +static int s_queue_desc_allocator(size_t index, mqd_section_t ** p_mqd_section) +{ + TODO(index); + TODO(p_mqd_section); + return SUCCESS; +} + +static int s_queue_desc_deallocator(mqd_section_t ** p_mqd_section) +{ + TODO(p_mqd_section); + return SUCCESS; +} + +static int s_queue_write(mqd_t mqdes, const void * buff, size_t size) +{ + mqd_section_t * p_mqd_section = (mqd_section_t *) mqdes; + + RET_ERR_IF_FALSE((p_mqd_section->attr.mq_flags & O_RDONLY), -ENOTSUP, int); + + memcpy(p_mqd_section->kernel_buff_send, buff, size); + + super_call(scall_id_mq_send, p_mqd_section->kernel_buff_send, size, RST_VAL, &g_mq_sys_ret); + RET_ERR_IF_FALSE(g_mq_sys_ret.status == SUCCESS, -EBADF, int); + + return SUCCESS; +} + +static int s_queue_read(mqd_t mqdes, const void * buff, size_t size) +{ + mqd_section_t * p_mqd_section = (mqd_section_t *) mqdes; + + RET_ERR_IF_FALSE((p_mqd_section->attr.mq_flags & O_WRONLY), -ENOTSUP, int); + + super_call(scall_id_mq_receive, p_mqd_section->kernel_buff_recv, size, RST_VAL, &g_mq_sys_ret); + RET_ERR_IF_FALSE(g_mq_sys_ret.status == SUCCESS, -EBADF, int); + + memcpy(buff, (void *) (g_mq_sys_ret.p), size); + + return SUCCESS; +} + +static int s_mqueue_delay(TickType_t ticks) +{ + super_call(scall_id_pthread_delay_ticks, ticks, RST_VAL, RST_VAL, &g_mq_sys_ret); + RET_ERR_IF_FALSE(g_mq_sys_ret.status == SUCCESS, EAGAIN, int); + + return SUCCESS; +} + +static int s_mq_lock_init(void) +{ + pthread_mutexattr_init(&g_mq_mutex_attr); + return pthread_mutex_init(&g_mq_mutex, &g_mq_mutex_attr); +} + +static int s_mq_lock_deinit(void) +{ + pthread_mutexattr_destroy(&g_mq_mutex_attr); + return pthread_mutex_destroy(&g_mq_mutex); +} + +static int s_mq_acquire_lock(void) +{ + return pthread_mutex_lock(&g_mq_mutex); +} + +static int s_mq_release_lock(void) +{ + return pthread_mutex_unlock(&g_mq_mutex); +} + +/********************* + * POSIX Functions + ********************/ mqd_t mq_open( const char * name, int oflag, @@ -47,8 +129,12 @@ mqd_t mq_open( const char * name, /* Return ENOENT if oflag is not equal to O_CREAT */ RET_ERR_IF_FALSE( oflag & O_CREAT, -ENOENT, mqd_t); + /* Initialise lock */ + p_mqd_section = (mqd_section_t *) s_mq_lock_init(); + RET_ERR_IF_FALSE(p_mqd_section == SUCCESS , p_mqd_section, mqd_t); + /* Grab resource access else return EBUSY */ - RET_ERR_IF_FALSE( s_acquire_resource() == SUCCESS, -EBUSY, mqd_t); + RET_ERR_IF_FALSE( s_mq_acquire_lock() == SUCCESS, -EBUSY, mqd_t); /* Find available slot in descriptor table else return ENOSPC */ if(s_queue_desc_allocator(g_mq_id_ctr++, &p_mqd_section ) == SUCCESS) @@ -67,16 +153,18 @@ mqd_t mq_open( const char * name, p_mqd_section->attr.mq_flags = oflag; /* Perform super_call */ - super_call(scall_id_mq_open, p_mqd_section->attr.mq_maxmsg, p_mqd_section->attr.mq_msgsize, 0, &g_mq_sys_ret); + super_call(scall_id_mq_open, + (p_mqd_section->attr.mq_maxmsg * p_mqd_section->attr.mq_msgsize), + &(p_mqd_section->kernel_buff_send), + &(p_mqd_section->kernel_buff_recv), + &g_mq_sys_ret + ); if (g_mq_sys_ret.status != SUCCESS) { p_mqd_section = (mqd_section_t *) -ENOTSUP; } else { - /* Set Kernel buffer ptr */ - p_mqd_section->kernel_buff = g_mq_sys_ret.p; - /* Set queue name as specified */ strcpy(&(p_mqd_section->mq_name), name ); } @@ -86,7 +174,7 @@ mqd_t mq_open( const char * name, DO_NOTHING; } - RET_ERR_IF_FALSE( s_release_resource() == SUCCESS, -EBUSY, mqd_t); + RET_ERR_IF_FALSE( s_mq_release_lock() == SUCCESS, -EBUSY, mqd_t); return (mqd_t) p_mqd_section; } @@ -98,7 +186,7 @@ int mq_close( mqd_t mqdes ) ASSERT_IF_FALSE(mqdes != NULL, ssize_t); /* Grab resource access else return EBUSY */ - RET_ERR_IF_FALSE( s_acquire_resource() == SUCCESS, -EBUSY, int); + RET_ERR_IF_FALSE( s_mq_acquire_lock() == SUCCESS, -EBUSY, int); /* Return ENOENT if element already exist */ if (s_find_queue_in_desc_list(NULL, (mqd_section_t *) mqdes) == SUCCESS) @@ -107,7 +195,7 @@ int mq_close( mqd_t mqdes ) memset(&((mqd_section_t *) mqdes)->attr, RST_VAL, sizeof(mq_attr_t)); /* Perform Super Call */ - super_call(scall_id_mq_close, ((mqd_section_t *) mqdes)->kernel_buff, 0, 0, &g_mq_sys_ret); + super_call(scall_id_mq_close, ((mqd_section_t *) mqdes)->kernel_buff_send, RST_VAL, RST_VAL, &g_mq_sys_ret); if (g_mq_sys_ret.status != SUCCESS) { err = -ENOTSUP; @@ -123,7 +211,10 @@ int mq_close( mqd_t mqdes ) err = -EBADF; } - RET_ERR_IF_FALSE( s_release_resource() == SUCCESS, -EBUSY, ssize_t); + RET_ERR_IF_FALSE( s_mq_release_lock() == SUCCESS, -EBUSY, ssize_t); + + err = s_mq_lock_deinit(); + RET_ERR_IF_FALSE( err == SUCCESS, err, ssize_t ); return err; } @@ -162,24 +253,25 @@ ssize_t mq_timedreceive( mqd_t mqdes, { ASSERT_IF_FALSE(mqdes != NULL, ssize_t); ASSERT_IF_FALSE(msg_ptr != NULL, ssize_t); - ASSERT_IF_FALSE(msg_len > 0, ssize_t); + ASSERT_IF_FALSE(msg_len > RST_VAL, ssize_t); ssize_t err = SUCCESS; TickType_t abs_ticks; if (abstime == NULL) { - abs_ticks = s_is_irq() ? RST_VAL : posxconfigMAX_DELAY; + abs_ticks = s_is_irq() ? RST_VAL : posixconfigMAX_DELAY; } else { - ASSERT_IF_FALSE (UTILS_TimespecToTicks(abstime, &abs_ticks) == SUCCESS, ssize_t); + ASSERT_IF_FALSE ( UTILS_TimespecToTicks(abstime, &abs_ticks) == SUCCESS, ssize_t ); } /* Grab resource access else return EBUSY */ - RET_ERR_IF_FALSE( s_acquire_resource() == SUCCESS, -EBUSY, ssize_t); + RET_ERR_IF_FALSE( s_mq_acquire_lock() == SUCCESS, -EBUSY, ssize_t); + /* Check the availability of the Queue entry */ if (s_find_queue_in_desc_list(NULL, (mqd_section_t *) mqdes) == SUCCESS) { do @@ -198,27 +290,25 @@ ssize_t mq_timedreceive( mqd_t mqdes, } else { - s_mqueue_delay(DELAY_MIN_TICK); - abs_ticks--; - - if (abs_ticks == 0) + if (abs_ticks == RST_VAL) { err = -ETIMEDOUT; } else { - DO_NOTHING; + s_mqueue_delay(DELAY_MIN_TICK); + abs_ticks--;; } } } - }while(abs_ticks > 0); + }while(abs_ticks > RST_VAL); } else { err = -EBADF; } - RET_ERR_IF_FALSE( s_release_resource() == SUCCESS, -EBUSY, ssize_t); + RET_ERR_IF_FALSE( s_mq_release_lock() == SUCCESS, -EBUSY, ssize_t ); return err; } @@ -231,85 +321,68 @@ int mq_timedsend( mqd_t mqdes, { ASSERT_IF_FALSE(mqdes != NULL, int); ASSERT_IF_FALSE(msg_ptr != NULL, int); - ASSERT_IF_FALSE(msg_len > 0, int); - - TODO(mq_timedsend); - - return SUCCESS; -} - -int mq_unlink( const char * name ) -{ - TODO(mq_unlink); - return SUCCESS; -} - -/************************************************************/ - -static int s_find_queue_in_desc_list(char * name, mqd_section_t * p_mqd_section) -{ - TODO(s_find_queue_in_desc_list); - return SUCCESS; -} - -static int s_queue_desc_allocator(size_t index, mqd_section_t ** p_mqd_section) -{ - TODO(s_queue_desc_allocator); - return SUCCESS; -} - -static int s_queue_desc_deallocator(mqd_section_t ** p_mqd_section) -{ - TODO(s_queue_desc_deallocator); - return SUCCESS; -} + ASSERT_IF_FALSE(msg_len > RST_VAL, int); -static int s_queue_write(mqd_t mqdes, const void * buff, size_t size) -{ - mqd_section_t * p_mqd_section = (mqd_section_t *) mqdes; - - RET_ERR_IF_FALSE((p_mqd_section->attr.mq_flags & O_RDONLY), -ENOTSUP, int); - - memcpy(p_mqd_section->kernel_buff, buff, size); - return SUCCESS; -} - -static int s_queue_read(mqd_t mqdes, const void * buff, size_t size) -{ - mqd_section_t * p_mqd_section = (mqd_section_t *) mqdes; - - RET_ERR_IF_FALSE((p_mqd_section->attr.mq_flags & O_WRONLY), -ENOTSUP, int); + ssize_t err = SUCCESS; + TickType_t abs_ticks; - super_call(scall_id_mq_receive, p_mqd_section->kernel_buff, size, 0, &g_mq_sys_ret); - RET_ERR_IF_FALSE(g_mq_sys_ret.status == SUCCESS, -EBADF, int); + if (abstime == NULL) + { + abs_ticks = s_is_irq() ? RST_VAL : posixconfigMAX_DELAY; + } - memcpy(buff, (void *) (g_mq_sys_ret.p), size); + else + { + ASSERT_IF_FALSE (UTILS_TimespecToTicks(abstime, &abs_ticks) == SUCCESS, ssize_t); + } - return SUCCESS; -} + /* Grab resource access else return EBUSY */ + RET_ERR_IF_FALSE( s_mq_acquire_lock() == SUCCESS, -EBUSY, ssize_t); -static int s_mqueue_delay(TickType_t ticks) -{ - super_call(scall_id_pthread_delay_ticks, ticks, 0, 0, &g_mq_sys_ret); - RET_ERR_IF_FALSE(g_mq_sys_ret.status == SUCCESS, EAGAIN, int); + /* Check the availability of the Queue entry */ + if (s_find_queue_in_desc_list(NULL, (mqd_section_t *) mqdes) == SUCCESS) + { + do + { + /* Try to send to kernel queue buffer */ + if (s_queue_write(mqdes, msg_ptr, msg_len) == SUCCESS) + { + break; + } + else + { + if (((mqd_section_t *) mqdes)->attr.mq_flags & O_NONBLOCK) + { + err = -ENOSPC; + break; + } + else + { + if (abs_ticks == RST_VAL) + { + err = -ETIMEDOUT; + } + else + { + s_mqueue_delay(DELAY_MIN_TICK); + abs_ticks--;; + } + } + } + }while(abs_ticks > RST_VAL); + } + else + { + err = -EBADF; + } - return SUCCESS; -} + RET_ERR_IF_FALSE( s_mq_release_lock() == SUCCESS, -EBUSY, ssize_t ); -static int s_acquire_resource(void) -{ - TODO(s_acquire_resource); - return SUCCESS; + return err; } -static int s_release_resource(void) +int mq_unlink( const char * name ) { - TODO(s_release_resource); + TODO(mq_unlink); return SUCCESS; } - -static bool s_is_irq(void) -{ - super_call(scall_id_is_irq, 0, 0, 0, &g_mq_sys_ret); - return g_mq_sys_ret.status == SUCCESS; -} diff --git a/src/lib/posix/src/posix_time.c b/src/lib/posix/src/posix_time.c index 3ef75fe2..168a3949 100644 --- a/src/lib/posix/src/posix_time.c +++ b/src/lib/posix/src/posix_time.c @@ -10,6 +10,7 @@ #include #include +#include unsigned sleep( unsigned seconds ) { @@ -18,7 +19,7 @@ unsigned sleep( unsigned seconds ) int usleep( useconds_t usec ) { - + TODO(usec); } clock_t clock( void ) @@ -28,17 +29,23 @@ clock_t clock( void ) int clock_getcpuclockid( pid_t pid, clockid_t * clock_id ) { - + TODO(pid); + TODO(clock_id); + return SUCCESS; } int clock_getres( clockid_t clock_id, struct timespec * res ) { - + TODO(clock_id); + TODO(res); + return SUCCESS; } int clock_gettime( clockid_t clock_id, struct timespec * tp ) { - + TODO(clock_id); + TODO(tp); + return SUCCESS; } int clock_nanosleep( clockid_t clock_id, @@ -46,42 +53,57 @@ int clock_nanosleep( clockid_t clock_id, const struct timespec * rqtp, struct timespec * rmtp ) { - + TODO(clock_id); + TODO(flags); + TODO(rqtp); + TODO(rmtp); + return SUCCESS; } int clock_settime( clockid_t clock_id, const struct timespec * tp ) { - + TODO(clock_id); + TODO(tp); + return SUCCESS; } int nanosleep( const struct timespec * rqtp, struct timespec * rmtp ) { - + TODO(rqtp); + TODO(rmtp); + return SUCCESS; } -int timer_create( clockid_t clockid, +int timer_create( clockid_t clock_id, struct sigevent * evp, timer_t * timerid ) { - + TODO(clock_id); + TODO(evp); + TODO(timerid); + return SUCCESS; } int timer_delete( timer_t timerid ) { - + TODO(timerid); + return SUCCESS; } int timer_getoverrun( timer_t timerid ) { - + TODO(timerid); + return SUCCESS; } int timer_gettime( timer_t timerid, struct itimerspec * value ) { - + TODO(timerid); + TODO(value); + return SUCCESS; } int timer_settime( timer_t timerid, @@ -89,5 +111,9 @@ int timer_settime( timer_t timerid, const struct itimerspec * value, struct itimerspec * ovalue ) { - + TODO(timerid); + TODO(flags); + TODO(value); + TODO(ovalue); + return SUCCESS; } From 0dbc8def29713e7c9b32dbe7ddd19dee6e10f5f3 Mon Sep 17 00:00:00 2001 From: Pranjal Chanda Date: Sun, 9 Jan 2022 23:42:00 +0530 Subject: [PATCH 26/31] Add support for posix sem --- src/include/supervisor_call.h | 3 +- src/lib/posix/cc_posix_config.h | 4 + src/lib/posix/cc_posix_internal.h | 1 + src/lib/posix/cc_posix_types.h | 2 +- src/lib/posix/include/mqueue.h | 18 ++-- src/lib/posix/include/pthread.h | 4 +- src/lib/posix/include/semaphore.h | 18 ++-- src/lib/posix/include/sys/types.h | 2 +- src/lib/posix/include/time.h | 8 +- src/lib/posix/include/unistd.h | 2 +- src/lib/posix/include/utils.h | 12 ++- src/lib/posix/src/posix_mqueue.c | 53 +++++------ src/lib/posix/src/posix_semaphore.c | 135 ++++++++++++++++++++++++++++ src/lib/posix/src/posix_time.c | 6 +- src/lib/posix/src/posix_utils.c | 19 +++- 15 files changed, 226 insertions(+), 61 deletions(-) create mode 100644 src/lib/posix/src/posix_semaphore.c diff --git a/src/include/supervisor_call.h b/src/include/supervisor_call.h index 97638e95..7bbb54b1 100644 --- a/src/include/supervisor_call.h +++ b/src/include/supervisor_call.h @@ -66,8 +66,7 @@ typedef enum scall_id scall_id_sem_destroy, scall_id_sem_getvalue, scall_id_sem_post, - scall_id_sem_timedwait, - scall_id_sem_trywait, + scall_id_sem_wait, /* scheduler related */ scall_id_sched_get_max_priority, scall_id_sched_get_min_priority, diff --git a/src/lib/posix/cc_posix_config.h b/src/lib/posix/cc_posix_config.h index 0922104e..aedc6bf7 100644 --- a/src/lib/posix/cc_posix_config.h +++ b/src/lib/posix/cc_posix_config.h @@ -1,5 +1,9 @@ #pragma once +#ifndef posixconfigTICK_RATE_HZ +#define posixconfigTICK_RATE_HZ (1000) +#endif + /******************* * MQUEUE configs *******************/ diff --git a/src/lib/posix/cc_posix_internal.h b/src/lib/posix/cc_posix_internal.h index 89558d9a..2318abed 100644 --- a/src/lib/posix/cc_posix_internal.h +++ b/src/lib/posix/cc_posix_internal.h @@ -1,6 +1,7 @@ #ifndef _CC_POSIX_INTERNAL_H_ #define _CC_POSIX_INTERNAL_H_ +#include #include /** diff --git a/src/lib/posix/cc_posix_types.h b/src/lib/posix/cc_posix_types.h index 8dfddcbe..971e1b75 100644 --- a/src/lib/posix/cc_posix_types.h +++ b/src/lib/posix/cc_posix_types.h @@ -3,7 +3,7 @@ #ifndef _CC_POSIX_INTERNAL_TYPES_H_ #define _CC_POSIX_INTERNAL_TYPES_H_ -#include +#include /* * sys/types.h defines a POSIX type when posixconfigENABLE_PTHREAD__T diff --git a/src/lib/posix/include/mqueue.h b/src/lib/posix/include/mqueue.h index 67563a40..4aeb944d 100644 --- a/src/lib/posix/include/mqueue.h +++ b/src/lib/posix/include/mqueue.h @@ -1,16 +1,20 @@ -/** - * @file mqueue.h - * @brief Message queues. - * - * http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/mqueue.h.html +/* + * CYANCORE LICENSE + * Copyrights (C) 2019, Cyancore Team + * + * File Name : mqueue.h + * Description : This file consists of posix message queue related declarations + * : http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/mqueue.h.html + * Primary Author : Pranjal Chanda [pranjalchanda08@gmail.com] + * Organisation : Cyancore Core-Team */ #ifndef _CC_POSIX_MQUEUE_H_ #define _CC_POSIX_MQUEUE_H_ /* CC+POSIX includes. */ -#include -#include +#include +#include /** * @brief Message queue attributes. diff --git a/src/lib/posix/include/pthread.h b/src/lib/posix/include/pthread.h index 4fabe011..b0a1430c 100644 --- a/src/lib/posix/include/pthread.h +++ b/src/lib/posix/include/pthread.h @@ -10,8 +10,8 @@ /* CC+POSIX includes. POSIX states that this header shall make symbols * defined in sched.h and time.h visible. */ -#include "lib/posix/include/sched.h" -#include "lib/posix/include/time.h" +#include "posix/include/sched.h" +#include "posix/include/time.h" /** * @name pthread detach state. diff --git a/src/lib/posix/include/semaphore.h b/src/lib/posix/include/semaphore.h index d82c514e..96ce14ac 100644 --- a/src/lib/posix/include/semaphore.h +++ b/src/lib/posix/include/semaphore.h @@ -1,16 +1,20 @@ -/** - * @file semaphore.h - * @brief Semaphores. - * - * http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/semaphore.h.html +/* + * CYANCORE LICENSE + * Copyrights (C) 2019, Cyancore Team + * + * File Name : semaphore.h + * Description : This file consists of posix semaphore related declarations + * : http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/semaphore.h.html + * Primary Author : Pranjal Chanda [pranjalchanda08@gmail.com] + * Organisation : Cyancore Core-Team */ #ifndef _CC_POSIX_SEMAPHORE_H_ #define _CC_POSIX_SEMAPHORE_H_ /* CC+POSIX includes. */ -#include "lib/posix/include/time.h" -#include "lib/posix/cc_posix_types.h" +#include +#include /** * @brief Semaphore type. diff --git a/src/lib/posix/include/sys/types.h b/src/lib/posix/include/sys/types.h index bd925f6e..20c6f214 100644 --- a/src/lib/posix/include/sys/types.h +++ b/src/lib/posix/include/sys/types.h @@ -15,7 +15,7 @@ #include /* Kernel types include */ -#include +#include /** * @brief Used for system times in clock ticks or CLOCKS_PER_SEC. diff --git a/src/lib/posix/include/time.h b/src/lib/posix/include/time.h index 9bc93ec9..b3578f28 100644 --- a/src/lib/posix/include/time.h +++ b/src/lib/posix/include/time.h @@ -9,8 +9,8 @@ #define _CC_POSIX_TIME_H_ /* CC+POSIX includes. */ -#include -#include +#include +#include /** * @name Unit conversion constants. @@ -18,7 +18,7 @@ /**@{ */ #define MICROSECONDS_PER_SECOND ( 1000000LL ) /**< Microseconds per second. */ #define NANOSECONDS_PER_SECOND ( 1000000000LL ) /**< Nanoseconds per second. */ -#define NANOSECONDS_PER_TICK ( NANOSECONDS_PER_SECOND / configTICK_RATE_HZ ) /**< Nanoseconds per Kernel tick. */ +#define NANOSECONDS_PER_TICK ( NANOSECONDS_PER_SECOND / posixconfigTICK_RATE_HZ ) /**< Nanoseconds per Kernel tick. */ /**@} */ /** @@ -33,7 +33,7 @@ * @name A number used to convert the value returned by the clock() function into seconds. */ /**@{ */ -#define CLOCKS_PER_SEC ( ( clock_t ) configTICK_RATE_HZ ) +#define CLOCKS_PER_SEC ( ( clock_t ) posixconfigTICK_RATE_HZ ) /**@} */ /** diff --git a/src/lib/posix/include/unistd.h b/src/lib/posix/include/unistd.h index c387347d..b90cc650 100644 --- a/src/lib/posix/include/unistd.h +++ b/src/lib/posix/include/unistd.h @@ -8,7 +8,7 @@ #ifndef _CC_POSIX_UNISTD_H_ #define _CC_POSIX_UNISTD_H_ -#include "lib/posix/include/sys/types.h" +#include "posix/include/sys/types.h" /** * @brief Suspend execution for an interval of time. diff --git a/src/lib/posix/include/utils.h b/src/lib/posix/include/utils.h index 92b2a373..cade1af3 100644 --- a/src/lib/posix/include/utils.h +++ b/src/lib/posix/include/utils.h @@ -11,12 +11,14 @@ #include /* CC+POSIX includes. */ -#include +#include +#define DELAY_MIN_TICK (1U) #define DO_NOTHING #define RST_VAL (0) #define ASSERT_IF_FALSE(con, type) RET_ERR_IF_FALSE(con, -EINVAL, type) #define RET_ERR_IF_FALSE(con, ret, type) if(!(con)){return (type) ret;} +#define IS_ISR() true /** * @brief Calculates the length of pcString, up to xMaxLength. @@ -131,5 +133,13 @@ int UTILS_TimespecCompare( const struct timespec * const x, * @return true if the pxTimespec is valid, false otherwise. */ bool UTILS_ValidateTimespec( const struct timespec * const pxTimespec ); +/** + * @brief Creates OS delay according to number of ticks provided + * + * @param[in] ticks Number of OS ticks to delay + * + * @return None + */ +void UTILS_OS_Delay( const TickType_t ticks ); #endif /* ifndef _CC_POSIX_UTILS_ */ diff --git a/src/lib/posix/src/posix_mqueue.c b/src/lib/posix/src/posix_mqueue.c index 4654a952..d9338dd2 100644 --- a/src/lib/posix/src/posix_mqueue.c +++ b/src/lib/posix/src/posix_mqueue.c @@ -10,16 +10,15 @@ #include #include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include #define DELAY_MIN_TICK (1U) -#define s_is_irq() true +#define IS_ISR() true -sret_t g_mq_sys_ret; size_t g_mq_id_ctr = RST_VAL; pthread_mutex_t g_mq_mutex; @@ -52,13 +51,14 @@ static int s_queue_desc_deallocator(mqd_section_t ** p_mqd_section) static int s_queue_write(mqd_t mqdes, const void * buff, size_t size) { mqd_section_t * p_mqd_section = (mqd_section_t *) mqdes; + sret_t mq_sys_ret; RET_ERR_IF_FALSE((p_mqd_section->attr.mq_flags & O_RDONLY), -ENOTSUP, int); memcpy(p_mqd_section->kernel_buff_send, buff, size); - super_call(scall_id_mq_send, p_mqd_section->kernel_buff_send, size, RST_VAL, &g_mq_sys_ret); - RET_ERR_IF_FALSE(g_mq_sys_ret.status == SUCCESS, -EBADF, int); + super_call(scall_id_mq_send, p_mqd_section->kernel_buff_send, size, RST_VAL, &mq_sys_ret); + RET_ERR_IF_FALSE(mq_sys_ret.status == SUCCESS, -EBADF, int); return SUCCESS; } @@ -66,21 +66,14 @@ static int s_queue_write(mqd_t mqdes, const void * buff, size_t size) static int s_queue_read(mqd_t mqdes, const void * buff, size_t size) { mqd_section_t * p_mqd_section = (mqd_section_t *) mqdes; + sret_t mq_sys_ret; RET_ERR_IF_FALSE((p_mqd_section->attr.mq_flags & O_WRONLY), -ENOTSUP, int); - super_call(scall_id_mq_receive, p_mqd_section->kernel_buff_recv, size, RST_VAL, &g_mq_sys_ret); - RET_ERR_IF_FALSE(g_mq_sys_ret.status == SUCCESS, -EBADF, int); + super_call(scall_id_mq_receive, p_mqd_section->kernel_buff_recv, size, RST_VAL, &mq_sys_ret); + RET_ERR_IF_FALSE(mq_sys_ret.status == SUCCESS, -EBADF, int); - memcpy(buff, (void *) (g_mq_sys_ret.p), size); - - return SUCCESS; -} - -static int s_mqueue_delay(TickType_t ticks) -{ - super_call(scall_id_pthread_delay_ticks, ticks, RST_VAL, RST_VAL, &g_mq_sys_ret); - RET_ERR_IF_FALSE(g_mq_sys_ret.status == SUCCESS, EAGAIN, int); + memcpy(buff, (void *) (mq_sys_ret.p), size); return SUCCESS; } @@ -117,6 +110,7 @@ mqd_t mq_open( const char * name, mq_attr_t * attr ) { mqd_section_t * p_mqd_section = NULL; + sret_t mq_sys_ret; size_t index; /* Check argument assertions */ @@ -157,9 +151,9 @@ mqd_t mq_open( const char * name, (p_mqd_section->attr.mq_maxmsg * p_mqd_section->attr.mq_msgsize), &(p_mqd_section->kernel_buff_send), &(p_mqd_section->kernel_buff_recv), - &g_mq_sys_ret + &mq_sys_ret ); - if (g_mq_sys_ret.status != SUCCESS) + if (mq_sys_ret.status != SUCCESS) { p_mqd_section = (mqd_section_t *) -ENOTSUP; } @@ -182,6 +176,7 @@ mqd_t mq_open( const char * name, int mq_close( mqd_t mqdes ) { int err = SUCCESS; + sret_t mq_sys_ret; ASSERT_IF_FALSE(mqdes != NULL, ssize_t); @@ -195,8 +190,8 @@ int mq_close( mqd_t mqdes ) memset(&((mqd_section_t *) mqdes)->attr, RST_VAL, sizeof(mq_attr_t)); /* Perform Super Call */ - super_call(scall_id_mq_close, ((mqd_section_t *) mqdes)->kernel_buff_send, RST_VAL, RST_VAL, &g_mq_sys_ret); - if (g_mq_sys_ret.status != SUCCESS) + super_call(scall_id_mq_close, ((mqd_section_t *) mqdes)->kernel_buff_send, RST_VAL, RST_VAL, &mq_sys_ret); + if (mq_sys_ret.status != SUCCESS) { err = -ENOTSUP; } @@ -260,7 +255,7 @@ ssize_t mq_timedreceive( mqd_t mqdes, if (abstime == NULL) { - abs_ticks = s_is_irq() ? RST_VAL : posixconfigMAX_DELAY; + abs_ticks = IS_ISR() ? RST_VAL : posixconfigMAX_DELAY; } else @@ -296,8 +291,8 @@ ssize_t mq_timedreceive( mqd_t mqdes, } else { - s_mqueue_delay(DELAY_MIN_TICK); - abs_ticks--;; + UTILS_OS_Delay((const TickType_t)DELAY_MIN_TICK); + abs_ticks--; } } } @@ -328,7 +323,7 @@ int mq_timedsend( mqd_t mqdes, if (abstime == NULL) { - abs_ticks = s_is_irq() ? RST_VAL : posixconfigMAX_DELAY; + abs_ticks = IS_ISR() ? RST_VAL : posixconfigMAX_DELAY; } else @@ -364,7 +359,7 @@ int mq_timedsend( mqd_t mqdes, } else { - s_mqueue_delay(DELAY_MIN_TICK); + UTILS_OS_Delay((const TickType_t)DELAY_MIN_TICK); abs_ticks--;; } } diff --git a/src/lib/posix/src/posix_semaphore.c b/src/lib/posix/src/posix_semaphore.c new file mode 100644 index 00000000..7b980eb8 --- /dev/null +++ b/src/lib/posix/src/posix_semaphore.c @@ -0,0 +1,135 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2019, Cyancore Team + * + * File Name : posix_semaphore.c + * Description : This file consists of posix semaphore related call handles + * Primary Author : Pranjal Chanda [pranjalchanda08@gmail.com] + * Organisation : Cyancore Core-Team + */ + +#include +#include +#include +#include +#include + +/********************* + * Static Functions + ********************/ +static int s_sem_wait( sem_t * sem ) +{ + sret_t sem_sys_ret; + ASSERT_IF_FALSE(sem == NULL, int); + + super_call(scall_id_sem_wait, *sem, RST_VAL, RST_VAL, &sem_sys_ret); + + return sem_sys_ret.status; +} + +/********************* + * POSIX Functions + ********************/ +int sem_destroy( sem_t * sem ) +{ + sret_t sem_sys_ret; + ASSERT_IF_FALSE(sem == NULL, int); + + super_call(scall_id_sem_destroy, *sem, RST_VAL, RST_VAL, &sem_sys_ret); + RET_ERR_IF_FALSE(sem_sys_ret.status == SUCCESS, sem_sys_ret.status, int); + + *sem = (sem_t) NULL; + + return SUCCESS; +} + +int sem_getvalue( sem_t * sem, + int * sval ) +{ + sret_t sem_sys_ret; + ASSERT_IF_FALSE(sem == NULL, int); + + super_call(scall_id_sem_getvalue, *sem, RST_VAL, RST_VAL, &sem_sys_ret); + RET_ERR_IF_FALSE(sem_sys_ret.status == SUCCESS, sem_sys_ret.status, int); + + *sem = (sem_t) sem_sys_ret.p; + + return SUCCESS; +} + +int sem_init( sem_t * sem, + int pshared _UNUSED, + unsigned value ) +{ + sret_t sem_sys_ret; + ASSERT_IF_FALSE(sem == NULL, int); + + super_call(scall_id_sem_init, value, RST_VAL, RST_VAL, &sem_sys_ret); + RET_ERR_IF_FALSE(sem_sys_ret.status == SUCCESS, sem_sys_ret.status, int); + + *sem = (sem_t) sem_sys_ret.p; + + return SUCCESS; +} + +int sem_post( sem_t * sem ) +{ + sret_t sem_sys_ret; + ASSERT_IF_FALSE(sem == NULL, int); + + super_call(scall_id_sem_post, *sem, RST_VAL, RST_VAL, &sem_sys_ret); + + return sem_sys_ret.status; +} + +int sem_timedwait( sem_t * sem, + const struct timespec * abstime ) +{ + ASSERT_IF_FALSE(sem == NULL, int); + + int err = SUCCESS; + TickType_t abs_ticks; + + if (abstime == NULL) + { + abs_ticks = IS_ISR() ? RST_VAL : posixconfigMAX_DELAY; + } + + else + { + ASSERT_IF_FALSE ( UTILS_TimespecToTicks(abstime, &abs_ticks) == SUCCESS, ssize_t ); + } + + do + { + if (s_sem_wait(sem) == SUCCESS) + { + break; + } + else + { + if (abs_ticks == RST_VAL) + { + err = -ETIMEDOUT; + } + else + { + UTILS_OS_Delay((const TickType_t)DELAY_MIN_TICK); + abs_ticks--; + } + } + + }while(abs_ticks > RST_VAL); + + return err; +} + +int sem_trywait( sem_t * sem ) +{ + return sem_timedwait(sem, RST_VAL); +} + +int sem_wait( sem_t * sem ) +{ + return sem_timedwait(sem, NULL); +} diff --git a/src/lib/posix/src/posix_time.c b/src/lib/posix/src/posix_time.c index 168a3949..c909dae2 100644 --- a/src/lib/posix/src/posix_time.c +++ b/src/lib/posix/src/posix_time.c @@ -8,9 +8,9 @@ * Organisation : Cyancore Core-Team */ -#include -#include -#include +#include +#include +#include unsigned sleep( unsigned seconds ) { diff --git a/src/lib/posix/src/posix_utils.c b/src/lib/posix/src/posix_utils.c index 7bcf7fa0..5c583e58 100644 --- a/src/lib/posix/src/posix_utils.c +++ b/src/lib/posix/src/posix_utils.c @@ -8,8 +8,11 @@ * Organisation : Cyancore Core-Team */ -#include -#include +#include +#include +#include +#include +#include size_t UTILS_strnlen( const char * const pcString, size_t xMaxLength ) { @@ -83,7 +86,7 @@ int UTILS_TimespecToTicks( const struct timespec * const pxTimespec, if( err_staus == 0 ) { /* Convert timespec.tv_sec to ticks. */ - total_ticks = ( int64_t ) configTICK_RATE_HZ * ( pxTimespec->tv_sec ); + total_ticks = ( int64_t ) posixconfigTICK_RATE_HZ * ( pxTimespec->tv_sec ); /* Convert timespec.tv_nsec to ticks. This value does not have to be checked * for overflow because a valid timespec has 0 <= tv_nsec < 1000000000 and @@ -335,3 +338,13 @@ bool UTILS_ValidateTimespec( const struct timespec * const pxTimespec ) return ret; } + +void UTILS_OS_Delay( const TickType_t ticks ) +{ + sret_t sys_ret; + + super_call(scall_id_pthread_delay_ticks, ticks, RST_VAL, RST_VAL, &sys_ret); + RET_ERR_IF_FALSE(sys_ret.status == SUCCESS, EAGAIN, int); + + return SUCCESS; +} From 304cebf3d43a8bcb1b660add64237998a2df3b32 Mon Sep 17 00:00:00 2001 From: Pranjal Chanda Date: Wed, 12 Jan 2022 18:45:35 +0530 Subject: [PATCH 27/31] Build Success: posix_time.c, posix_semaphore.c, posix_utils.c, posix_mqueue.c --- src/lib/build.mk | 1 + src/lib/posix/build.mk | 22 +++++++ src/lib/posix/{ => include}/cc_posix_config.h | 10 +++ .../posix/{ => include}/cc_posix_internal.h | 15 +++-- src/lib/posix/{ => include}/cc_posix_types.h | 17 ++++-- src/lib/posix/include/{ => posix}/errno.h | 5 +- src/lib/posix/include/{ => posix}/fcntl.h | 18 +++--- src/lib/posix/include/{ => posix}/mqueue.h | 10 +-- src/lib/posix/include/{ => posix}/pthread.h | 22 ++++--- src/lib/posix/include/{ => posix}/sched.h | 18 +++--- src/lib/posix/include/{ => posix}/semaphore.h | 10 +-- src/lib/posix/include/{ => posix}/signal.h | 18 +++--- src/lib/posix/include/{ => posix}/sys/types.h | 11 ++-- src/lib/posix/include/{ => posix}/time.h | 22 ++++--- src/lib/posix/include/{ => posix}/unistd.h | 22 ++++--- src/lib/posix/include/{ => posix}/utils.h | 20 +++--- src/lib/posix/src/build.mk | 14 +++++ src/lib/posix/src/posix_mqueue.c | 51 ++++++++-------- src/lib/posix/src/posix_semaphore.c | 19 +++--- src/lib/posix/src/posix_time.c | 61 ++++++++++--------- src/lib/posix/src/posix_utils.c | 14 ++--- 21 files changed, 242 insertions(+), 158 deletions(-) create mode 100644 src/lib/posix/build.mk rename src/lib/posix/{ => include}/cc_posix_config.h (75%) rename src/lib/posix/{ => include}/cc_posix_internal.h (90%) rename src/lib/posix/{ => include}/cc_posix_types.h (82%) rename src/lib/posix/include/{ => posix}/errno.h (93%) rename src/lib/posix/include/{ => posix}/fcntl.h (81%) rename src/lib/posix/include/{ => posix}/mqueue.h (95%) rename src/lib/posix/include/{ => posix}/pthread.h (95%) rename src/lib/posix/include/{ => posix}/sched.h (75%) rename src/lib/posix/include/{ => posix}/semaphore.h (92%) rename src/lib/posix/include/{ => posix}/signal.h (73%) rename src/lib/posix/include/{ => posix}/sys/types.h (92%) rename src/lib/posix/include/{ => posix}/time.h (91%) rename src/lib/posix/include/{ => posix}/unistd.h (61%) rename src/lib/posix/include/{ => posix}/utils.h (90%) create mode 100644 src/lib/posix/src/build.mk diff --git a/src/lib/build.mk b/src/lib/build.mk index 131b6846..311f487d 100644 --- a/src/lib/build.mk +++ b/src/lib/build.mk @@ -14,6 +14,7 @@ LIB_DIR := $(GET_PATH) # Mandatory libraries include $(LIB_DIR)/libresource/build.mk include $(LIB_DIR)/libc/build.mk +include $(LIB_DIR)/posix/build.mk #================================================== include $(LIB_DIR)/libnmath/build.mk diff --git a/src/lib/posix/build.mk b/src/lib/posix/build.mk new file mode 100644 index 00000000..a77936fd --- /dev/null +++ b/src/lib/posix/build.mk @@ -0,0 +1,22 @@ +# +# CYANCORE LICENSE +# Copyrights (C) 2019, Cyancore Team +# +# File Name : build.mk +# Descrption : This script accumulates sources and builds +# library +# Primary Author : Pranjal Chanda [pranjalchanda08@gmail.com] +# Organisation : Cyancore Core-Team +# + +POSIX_PATH := $(GET_PATH) +LIB_OBJS := + +LIB := libposix.a +LIB_INCLUDE += $(POSIX_PATH)/include/ +DEP_LIBS_ARG += -lposix + +include $(POSIX_PATH)/src/build.mk + +DIR := $(POSIX_PATH) +include mk/lib.mk diff --git a/src/lib/posix/cc_posix_config.h b/src/lib/posix/include/cc_posix_config.h similarity index 75% rename from src/lib/posix/cc_posix_config.h rename to src/lib/posix/include/cc_posix_config.h index aedc6bf7..c5d721d2 100644 --- a/src/lib/posix/cc_posix_config.h +++ b/src/lib/posix/include/cc_posix_config.h @@ -1,3 +1,13 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2019, Cyancore Team + * + * File Name : cc_posix_config.h + * Description : POSIX user config + * Primary Author : Pranjal Chanda [pranjalchanda08@gmail.com] + * Organisation : Cyancore Core-Team + */ + #pragma once #ifndef posixconfigTICK_RATE_HZ diff --git a/src/lib/posix/cc_posix_internal.h b/src/lib/posix/include/cc_posix_internal.h similarity index 90% rename from src/lib/posix/cc_posix_internal.h rename to src/lib/posix/include/cc_posix_internal.h index 2318abed..6e291224 100644 --- a/src/lib/posix/cc_posix_internal.h +++ b/src/lib/posix/include/cc_posix_internal.h @@ -1,5 +1,14 @@ -#ifndef _CC_POSIX_INTERNAL_H_ -#define _CC_POSIX_INTERNAL_H_ +/* + * CYANCORE LICENSE + * Copyrights (C) 2019, Cyancore Team + * + * File Name : cc_posix_internal.h + * Description : Internal typedefs + * Primary Author : Pranjal Chanda [pranjalchanda08@gmail.com] + * Organisation : Cyancore Core-Team + */ + +#pragma once #include #include @@ -100,5 +109,3 @@ StaticEventGroup_t xBarrierEventGroup; /**< Kernel event group that blocks to wait on threads entering barrier. */ } pthread_barrier_internal_t; #endif /* if posixconfigENABLE_PTHREAD_BARRIER_T == 1 */ - -#endif /* _CC_POSIX_INTERNAL_H_ */ diff --git a/src/lib/posix/cc_posix_types.h b/src/lib/posix/include/cc_posix_types.h similarity index 82% rename from src/lib/posix/cc_posix_types.h rename to src/lib/posix/include/cc_posix_types.h index 971e1b75..501273b5 100644 --- a/src/lib/posix/cc_posix_types.h +++ b/src/lib/posix/include/cc_posix_types.h @@ -1,9 +1,16 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2019, Cyancore Team + * + * File Name : cc_posix_types.h + * Description : POSIX typedefs + * Primary Author : Pranjal Chanda [pranjalchanda08@gmail.com] + * Organisation : Cyancore Core-Team + */ +#pragma once -#ifndef _CC_POSIX_INTERNAL_TYPES_H_ -#define _CC_POSIX_INTERNAL_TYPES_H_ - -#include +#include /* * sys/types.h defines a POSIX type when posixconfigENABLE_PTHREAD__T @@ -61,5 +68,3 @@ typedef size_t TickType_t; #endif #define posixconfigMAX_DELAY (TickType_t)(~0) - -#endif /* _CC_POSIX_INTERNAL_TYPES_H_ */ diff --git a/src/lib/posix/include/errno.h b/src/lib/posix/include/posix/errno.h similarity index 93% rename from src/lib/posix/include/errno.h rename to src/lib/posix/include/posix/errno.h index 7ca898d6..c20a3b75 100644 --- a/src/lib/posix/include/errno.h +++ b/src/lib/posix/include/posix/errno.h @@ -8,7 +8,8 @@ * Organisation : Cyancore Core-Team */ -#ifndef _CC_POSIX_ERRNO_H_ +#pragma once + #define _CC_POSIX_ERRNO_H_ #include @@ -67,5 +68,3 @@ #if ( configUSE_POSIX_ERRNO == 1 ) #endif /**@} */ - -#endif /* ifndef _CC_POSIX_ERRNO_H_ */ diff --git a/src/lib/posix/include/fcntl.h b/src/lib/posix/include/posix/fcntl.h similarity index 81% rename from src/lib/posix/include/fcntl.h rename to src/lib/posix/include/posix/fcntl.h index 1f712f48..ceae4781 100644 --- a/src/lib/posix/include/fcntl.h +++ b/src/lib/posix/include/posix/fcntl.h @@ -1,11 +1,17 @@ -/** - * @file fcntl.h - * @brief File control options. +/* + * CYANCORE LICENSE + * Copyrights (C) 2019, Cyancore Team + * + * File Name : fcntl.h + * Description : File control options. + * : http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/fcntl.h.html * - * http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/fcntl.h.html + * Primary Author : Pranjal Chanda [pranjalchanda08@gmail.com] + * Organisation : Cyancore Core-Team */ -#ifndef _CC_POSIX_FCNTL_H_ +#pragma once + #define _CC_POSIX_FCNTL_H_ /** @@ -50,5 +56,3 @@ #define O_SEARCH 0x4000 /**< Open directory for search only. */ #define O_WRONLY 0x8000 /**< Open for writing only. */ /**@} */ - -#endif /* ifndef _CC_POSIX_FCNTL_H_ */ diff --git a/src/lib/posix/include/mqueue.h b/src/lib/posix/include/posix/mqueue.h similarity index 95% rename from src/lib/posix/include/mqueue.h rename to src/lib/posix/include/posix/mqueue.h index 4aeb944d..936f2774 100644 --- a/src/lib/posix/include/mqueue.h +++ b/src/lib/posix/include/posix/mqueue.h @@ -5,16 +5,18 @@ * File Name : mqueue.h * Description : This file consists of posix message queue related declarations * : http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/mqueue.h.html + * * Primary Author : Pranjal Chanda [pranjalchanda08@gmail.com] * Organisation : Cyancore Core-Team */ -#ifndef _CC_POSIX_MQUEUE_H_ +#pragma once + #define _CC_POSIX_MQUEUE_H_ /* CC+POSIX includes. */ -#include -#include +#include +#include /** * @brief Message queue attributes. @@ -235,5 +237,3 @@ int mq_timedsend( mqd_t mqdes, * ENOENT - The named message queue does not exist. */ int mq_unlink( const char * name ); - -#endif /* ifndef _CC_POSIX_MQUEUE_H_ */ diff --git a/src/lib/posix/include/pthread.h b/src/lib/posix/include/posix/pthread.h similarity index 95% rename from src/lib/posix/include/pthread.h rename to src/lib/posix/include/posix/pthread.h index b0a1430c..ba4ad394 100644 --- a/src/lib/posix/include/pthread.h +++ b/src/lib/posix/include/posix/pthread.h @@ -1,17 +1,23 @@ -/** - * @file pthread.h - * @brief Threads. +/* + * CYANCORE LICENSE + * Copyrights (C) 2019, Cyancore Team + * + * File Name : pthread.h + * Description : Threads. + * : http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/pthread.h.html * - * http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/pthread.h.html + * Primary Author : Pranjal Chanda [pranjalchanda08@gmail.com] + * Organisation : Cyancore Core-Team */ -#ifndef _CC_POSIX_PTHREAD_H_ +#pragma once + #define _CC_POSIX_PTHREAD_H_ /* CC+POSIX includes. POSIX states that this header shall make symbols * defined in sched.h and time.h visible. */ -#include "posix/include/sched.h" -#include "posix/include/time.h" +#include "posix/sched.h" +#include "posix/time.h" /** * @name pthread detach state. @@ -472,5 +478,3 @@ pthread_t pthread_self( void ); int pthread_setschedparam( pthread_t thread, int policy, const struct sched_param * param ); - -#endif /* _CC_POSIX_PTHREAD_H_ */ diff --git a/src/lib/posix/include/sched.h b/src/lib/posix/include/posix/sched.h similarity index 75% rename from src/lib/posix/include/sched.h rename to src/lib/posix/include/posix/sched.h index 14532d1b..230f65f1 100644 --- a/src/lib/posix/include/sched.h +++ b/src/lib/posix/include/posix/sched.h @@ -1,11 +1,17 @@ -/** - * @file sched.h - * @brief Execution scheduling. +/* + * CYANCORE LICENSE + * Copyrights (C) 2019, Cyancore Team + * + * File Name : sched.h + * Description : Execution scheduling. + * : http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sched.h.html * - * http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sched.h.html + * Primary Author : Pranjal Chanda [pranjalchanda08@gmail.com] + * Organisation : Cyancore Core-Team */ -#ifndef _CC_POSIX_SCHED_H_ +#pragma once + #define _CC_POSIX_SCHED_H_ /** @@ -55,5 +61,3 @@ int sched_get_priority_min( int policy ); * @retval 0 - Upon successful completion */ int sched_yield( void ); - -#endif /* ifndef _CC_POSIX_SCHED_H_ */ diff --git a/src/lib/posix/include/semaphore.h b/src/lib/posix/include/posix/semaphore.h similarity index 92% rename from src/lib/posix/include/semaphore.h rename to src/lib/posix/include/posix/semaphore.h index 96ce14ac..5ad9f4a1 100644 --- a/src/lib/posix/include/semaphore.h +++ b/src/lib/posix/include/posix/semaphore.h @@ -5,16 +5,18 @@ * File Name : semaphore.h * Description : This file consists of posix semaphore related declarations * : http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/semaphore.h.html + * * Primary Author : Pranjal Chanda [pranjalchanda08@gmail.com] * Organisation : Cyancore Core-Team */ -#ifndef _CC_POSIX_SEMAPHORE_H_ +#pragma once + #define _CC_POSIX_SEMAPHORE_H_ /* CC+POSIX includes. */ -#include -#include +#include +#include /** * @brief Semaphore type. @@ -118,5 +120,3 @@ int sem_trywait( sem_t * sem ); * @note Deadlock detection is not implemented. */ int sem_wait( sem_t * sem ); - -#endif /* ifndef _CC_POSIX_SEMAPHORE_H_ */ diff --git a/src/lib/posix/include/signal.h b/src/lib/posix/include/posix/signal.h similarity index 73% rename from src/lib/posix/include/signal.h rename to src/lib/posix/include/posix/signal.h index 7beb8cd6..e800559f 100644 --- a/src/lib/posix/include/signal.h +++ b/src/lib/posix/include/posix/signal.h @@ -1,15 +1,17 @@ -/** - * @file signal.h - * @brief Signals. +/* + * CYANCORE LICENSE + * Copyrights (C) 2019, Cyancore Team * - * Signals are currently not implemented in CC+POSIX. This header only - * defines the signal data structures used elsewhere. + * File Name : signal.h + * Description : Signals (Signals are currently not implemented in CC+POSIX) + * : http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/signal.h.html * - * http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/signal.h.html + * Primary Author : Pranjal Chanda [pranjalchanda08@gmail.com] + * Organisation : Cyancore Core-Team */ +#pragma once -#ifndef _CC_POSIX_SIGNAL_H_ #define _CC_POSIX_SIGNAL_H_ /** @@ -41,5 +43,3 @@ struct sigevent void ( * sigev_notify_function )( union sigval ); /**< Notification function. */ pthread_attr_t * sigev_notify_attributes; /**< Notification attributes. */ }; - -#endif /* ifndef _CC_POSIX_SIGNAL_H_ */ diff --git a/src/lib/posix/include/sys/types.h b/src/lib/posix/include/posix/sys/types.h similarity index 92% rename from src/lib/posix/include/sys/types.h rename to src/lib/posix/include/posix/sys/types.h index 20c6f214..8a0c3348 100644 --- a/src/lib/posix/include/sys/types.h +++ b/src/lib/posix/include/posix/sys/types.h @@ -3,19 +3,22 @@ * Copyrights (C) 2019, Cyancore Team * * File Name : sys/types.h - * Description : Data types. https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_types.h.html + * Description : Data types. + * : https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_types.h.html + * * Primary Author : Pranjal Chanda [pranjalchanda08@gmail.com] * Organisation : Cyancore Core-Team */ -#ifndef _CC_POSIX_TYPES_H_ +#pragma once + #define _CC_POSIX_TYPES_H_ /* C standard library includes. */ #include /* Kernel types include */ -#include +#include /** * @brief Used for system times in clock ticks or CLOCKS_PER_SEC. @@ -165,5 +168,3 @@ typedef void * pthread_barrierattr_t; #if !defined( posixconfigENABLE_OFF_T ) || ( posixconfigENABLE_OFF_T == 1 ) typedef long int off_t; #endif - -#endif /* ifndef _CC_POSIX_TYPES_H_ */ diff --git a/src/lib/posix/include/time.h b/src/lib/posix/include/posix/time.h similarity index 91% rename from src/lib/posix/include/time.h rename to src/lib/posix/include/posix/time.h index b3578f28..9ecd8376 100644 --- a/src/lib/posix/include/time.h +++ b/src/lib/posix/include/posix/time.h @@ -1,16 +1,22 @@ -/** - * @file time.h - * @brief Time types. +/* + * CYANCORE LICENSE + * Copyrights (C) 2019, Cyancore Team + * + * File Name : time.h + * Description : Time types + * : http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/time.h.html * - * http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/time.h.html + * Primary Author : Pranjal Chanda [pranjalchanda08@gmail.com] + * Organisation : Cyancore Core-Team */ -#ifndef _CC_POSIX_TIME_H_ +#pragma once + #define _CC_POSIX_TIME_H_ /* CC+POSIX includes. */ -#include -#include +#include +#include /** * @name Unit conversion constants. @@ -229,5 +235,3 @@ int timer_settime( timer_t timerid, int flags, const itimerspec_t * value, itimerspec_t * ovalue ); - -#endif /* ifndef _CC_POSIX_TIME_H_ */ diff --git a/src/lib/posix/include/unistd.h b/src/lib/posix/include/posix/unistd.h similarity index 61% rename from src/lib/posix/include/unistd.h rename to src/lib/posix/include/posix/unistd.h index b90cc650..c37db52e 100644 --- a/src/lib/posix/include/unistd.h +++ b/src/lib/posix/include/posix/unistd.h @@ -1,14 +1,20 @@ -/** - * @file unistd.h - * @brief Standard symbolic constants and types +/* + * CYANCORE LICENSE + * Copyrights (C) 2019, Cyancore Team + * + * File Name : unistd.h + * Description : Standard symbolic constants and types + * : http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/unistd.h.html * - * http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/unistd.h.html + * Primary Author : Pranjal Chanda [pranjalchanda08@gmail.com] + * Organisation : Cyancore Core-Team */ -#ifndef _CC_POSIX_UNISTD_H_ -#define _CC_POSIX_UNISTD_H_ +#pragma once -#include "posix/include/sys/types.h" +#include "posix/sys/types.h" + +#define _CC_POSIX_UNISTD_H_ /** * @brief Suspend execution for an interval of time. @@ -32,5 +38,3 @@ unsigned sleep( unsigned seconds ); * @retval 0 - Upon successful completion. */ int usleep( useconds_t usec ); - -#endif /* ifndef _CC_POSIX_UNISTD_H_ */ diff --git a/src/lib/posix/include/utils.h b/src/lib/posix/include/posix/utils.h similarity index 90% rename from src/lib/posix/include/utils.h rename to src/lib/posix/include/posix/utils.h index cade1af3..4e08b5fc 100644 --- a/src/lib/posix/include/utils.h +++ b/src/lib/posix/include/posix/utils.h @@ -1,9 +1,15 @@ -/** - * @file utils.h - * @brief Utility functions used by CC+POSIX. +/* + * CYANCORE LICENSE + * Copyrights (C) 2019, Cyancore Team + * + * File Name : utils.h + * Description : Utility functions used by CC_POSIX + * Primary Author : Pranjal Chanda [pranjalchanda08@gmail.com] + * Organisation : Cyancore Core-Team */ -#ifndef _CC_POSIX_UTILS_ +#pragma once + #define _CC_POSIX_UTILS_ /* C standard library includes. */ @@ -11,7 +17,7 @@ #include /* CC+POSIX includes. */ -#include +#include #define DELAY_MIN_TICK (1U) #define DO_NOTHING @@ -140,6 +146,4 @@ bool UTILS_ValidateTimespec( const struct timespec * const pxTimespec ); * * @return None */ -void UTILS_OS_Delay( const TickType_t ticks ); - -#endif /* ifndef _CC_POSIX_UTILS_ */ +void os_delay_ticks( const TickType_t ticks ); diff --git a/src/lib/posix/src/build.mk b/src/lib/posix/src/build.mk new file mode 100644 index 00000000..79a75548 --- /dev/null +++ b/src/lib/posix/src/build.mk @@ -0,0 +1,14 @@ +# +# CYANCORE LICENSE +# Copyrights (C) 2019, Cyancore Team +# +# File Name : build.mk +# Descrption : This script accumulates all dp sources and +# generated dp library objects +# Primary Author : Pranjal Chanda [pranjalchanda08@gmail.com] +# Organisation : Cyancore Core-Team +# + +DIR := $(GET_PATH) + +include mk/lobj.mk diff --git a/src/lib/posix/src/posix_mqueue.c b/src/lib/posix/src/posix_mqueue.c index d9338dd2..d5b1287d 100644 --- a/src/lib/posix/src/posix_mqueue.c +++ b/src/lib/posix/src/posix_mqueue.c @@ -8,13 +8,14 @@ * Organisation : Cyancore Core-Team */ +#include #include #include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include #define DELAY_MIN_TICK (1U) #define IS_ISR() true @@ -30,34 +31,34 @@ pthread_mutexattr_t g_mq_mutex_attr = NULL; static int s_find_queue_in_desc_list(char * name, mqd_section_t * p_mqd_section) { - TODO(name); - TODO(p_mqd_section); + (void)(name); + (void)(p_mqd_section); return SUCCESS; } static int s_queue_desc_allocator(size_t index, mqd_section_t ** p_mqd_section) { - TODO(index); - TODO(p_mqd_section); + (void)(index); + (void)(p_mqd_section); return SUCCESS; } static int s_queue_desc_deallocator(mqd_section_t ** p_mqd_section) { - TODO(p_mqd_section); + (void)(p_mqd_section); return SUCCESS; } -static int s_queue_write(mqd_t mqdes, const void * buff, size_t size) +static int s_queue_write(mqd_t mqdes, const void * buff, size_t size, unsigned msg_prio) { mqd_section_t * p_mqd_section = (mqd_section_t *) mqdes; sret_t mq_sys_ret; RET_ERR_IF_FALSE((p_mqd_section->attr.mq_flags & O_RDONLY), -ENOTSUP, int); - memcpy(p_mqd_section->kernel_buff_send, buff, size); + memcpy((void *)p_mqd_section->kernel_buff_send, buff, size); - super_call(scall_id_mq_send, p_mqd_section->kernel_buff_send, size, RST_VAL, &mq_sys_ret); + super_call(scall_id_mq_send, p_mqd_section->kernel_buff_send, size, msg_prio, &mq_sys_ret); RET_ERR_IF_FALSE(mq_sys_ret.status == SUCCESS, -EBADF, int); return SUCCESS; @@ -73,7 +74,7 @@ static int s_queue_read(mqd_t mqdes, const void * buff, size_t size) super_call(scall_id_mq_receive, p_mqd_section->kernel_buff_recv, size, RST_VAL, &mq_sys_ret); RET_ERR_IF_FALSE(mq_sys_ret.status == SUCCESS, -EBADF, int); - memcpy(buff, (void *) (mq_sys_ret.p), size); + memcpy((void *)buff, (void *) (mq_sys_ret.p), size); return SUCCESS; } @@ -106,19 +107,19 @@ static int s_mq_release_lock(void) mqd_t mq_open( const char * name, int oflag, - mode_t mode, + mode_t mode _UNUSED, mq_attr_t * attr ) { mqd_section_t * p_mqd_section = NULL; sret_t mq_sys_ret; - size_t index; + size_t name_len = UTILS_strnlen(name, posixconfigMQ_NAME_LEN_MAX); /* Check argument assertions */ ASSERT_IF_FALSE(name != NULL, mqd_t); - ASSERT_IF_FALSE(UTILS_strnlen(name, posixconfigMQ_NAME_LEN_MAX) < posixconfigMQ_NAME_LEN_MAX, mqd_t); + ASSERT_IF_FALSE( name_len < posixconfigMQ_NAME_LEN_MAX, mqd_t); /* Return ENOENT if element already exist */ - RET_ERR_IF_FALSE(s_find_queue_in_desc_list(name, NULL) == SUCCESS, -ENOENT, mqd_t); + RET_ERR_IF_FALSE(s_find_queue_in_desc_list((char *) name, NULL) == SUCCESS, -ENOENT, mqd_t); /* Return ENOENT if oflag is not equal to O_CREAT */ RET_ERR_IF_FALSE( oflag & O_CREAT, -ENOENT, mqd_t); @@ -149,8 +150,8 @@ mqd_t mq_open( const char * name, /* Perform super_call */ super_call(scall_id_mq_open, (p_mqd_section->attr.mq_maxmsg * p_mqd_section->attr.mq_msgsize), - &(p_mqd_section->kernel_buff_send), - &(p_mqd_section->kernel_buff_recv), + (unsigned int) &(p_mqd_section->kernel_buff_send), + (unsigned int) &(p_mqd_section->kernel_buff_recv), &mq_sys_ret ); if (mq_sys_ret.status != SUCCESS) @@ -160,7 +161,7 @@ mqd_t mq_open( const char * name, else { /* Set queue name as specified */ - strcpy(&(p_mqd_section->mq_name), name ); + memcpy(&(p_mqd_section->mq_name), name, name_len); } } else @@ -291,7 +292,7 @@ ssize_t mq_timedreceive( mqd_t mqdes, } else { - UTILS_OS_Delay((const TickType_t)DELAY_MIN_TICK); + os_delay_ticks((const TickType_t)DELAY_MIN_TICK); abs_ticks--; } } @@ -340,7 +341,7 @@ int mq_timedsend( mqd_t mqdes, do { /* Try to send to kernel queue buffer */ - if (s_queue_write(mqdes, msg_ptr, msg_len) == SUCCESS) + if (s_queue_write(mqdes, msg_ptr, msg_len, msg_prio) == SUCCESS) { break; } @@ -359,7 +360,7 @@ int mq_timedsend( mqd_t mqdes, } else { - UTILS_OS_Delay((const TickType_t)DELAY_MIN_TICK); + os_delay_ticks((const TickType_t)DELAY_MIN_TICK); abs_ticks--;; } } @@ -378,6 +379,6 @@ int mq_timedsend( mqd_t mqdes, int mq_unlink( const char * name ) { - TODO(mq_unlink); + (void)(name); return SUCCESS; } diff --git a/src/lib/posix/src/posix_semaphore.c b/src/lib/posix/src/posix_semaphore.c index 7b980eb8..ed0a09f7 100644 --- a/src/lib/posix/src/posix_semaphore.c +++ b/src/lib/posix/src/posix_semaphore.c @@ -8,11 +8,12 @@ * Organisation : Cyancore Core-Team */ +#include #include #include -#include -#include -#include +#include +#include +#include /********************* * Static Functions @@ -22,7 +23,7 @@ static int s_sem_wait( sem_t * sem ) sret_t sem_sys_ret; ASSERT_IF_FALSE(sem == NULL, int); - super_call(scall_id_sem_wait, *sem, RST_VAL, RST_VAL, &sem_sys_ret); + super_call(scall_id_sem_wait, (unsigned int) *sem, RST_VAL, RST_VAL, &sem_sys_ret); return sem_sys_ret.status; } @@ -35,7 +36,7 @@ int sem_destroy( sem_t * sem ) sret_t sem_sys_ret; ASSERT_IF_FALSE(sem == NULL, int); - super_call(scall_id_sem_destroy, *sem, RST_VAL, RST_VAL, &sem_sys_ret); + super_call(scall_id_sem_destroy, (unsigned int) *sem, RST_VAL, RST_VAL, &sem_sys_ret); RET_ERR_IF_FALSE(sem_sys_ret.status == SUCCESS, sem_sys_ret.status, int); *sem = (sem_t) NULL; @@ -49,10 +50,10 @@ int sem_getvalue( sem_t * sem, sret_t sem_sys_ret; ASSERT_IF_FALSE(sem == NULL, int); - super_call(scall_id_sem_getvalue, *sem, RST_VAL, RST_VAL, &sem_sys_ret); + super_call(scall_id_sem_getvalue, (unsigned int) *sem, RST_VAL, RST_VAL, &sem_sys_ret); RET_ERR_IF_FALSE(sem_sys_ret.status == SUCCESS, sem_sys_ret.status, int); - *sem = (sem_t) sem_sys_ret.p; + *sval = (int) sem_sys_ret.p; return SUCCESS; } @@ -77,7 +78,7 @@ int sem_post( sem_t * sem ) sret_t sem_sys_ret; ASSERT_IF_FALSE(sem == NULL, int); - super_call(scall_id_sem_post, *sem, RST_VAL, RST_VAL, &sem_sys_ret); + super_call(scall_id_sem_post, (unsigned int) *sem, RST_VAL, RST_VAL, &sem_sys_ret); return sem_sys_ret.status; } @@ -114,7 +115,7 @@ int sem_timedwait( sem_t * sem, } else { - UTILS_OS_Delay((const TickType_t)DELAY_MIN_TICK); + os_delay_ticks((const TickType_t)DELAY_MIN_TICK); abs_ticks--; } } diff --git a/src/lib/posix/src/posix_time.c b/src/lib/posix/src/posix_time.c index c909dae2..0584e054 100644 --- a/src/lib/posix/src/posix_time.c +++ b/src/lib/posix/src/posix_time.c @@ -8,9 +8,9 @@ * Organisation : Cyancore Core-Team */ -#include -#include -#include +#include +#include +#include unsigned sleep( unsigned seconds ) { @@ -19,32 +19,33 @@ unsigned sleep( unsigned seconds ) int usleep( useconds_t usec ) { - TODO(usec); + (void) usec; + return SUCCESS; } clock_t clock( void ) { - + return (clock_t) 0; } int clock_getcpuclockid( pid_t pid, clockid_t * clock_id ) { - TODO(pid); - TODO(clock_id); + (void)(pid); + (void)(clock_id); return SUCCESS; } int clock_getres( clockid_t clock_id, struct timespec * res ) { - TODO(clock_id); - TODO(res); + (void)(clock_id); + (void)(res); return SUCCESS; } int clock_gettime( clockid_t clock_id, struct timespec * tp ) { - TODO(clock_id); - TODO(tp); + (void)(clock_id); + (void)(tp); return SUCCESS; } @@ -53,26 +54,26 @@ int clock_nanosleep( clockid_t clock_id, const struct timespec * rqtp, struct timespec * rmtp ) { - TODO(clock_id); - TODO(flags); - TODO(rqtp); - TODO(rmtp); + (void)(clock_id); + (void)(flags); + (void)(rqtp); + (void)(rmtp); return SUCCESS; } int clock_settime( clockid_t clock_id, const struct timespec * tp ) { - TODO(clock_id); - TODO(tp); + (void)(clock_id); + (void)(tp); return SUCCESS; } int nanosleep( const struct timespec * rqtp, struct timespec * rmtp ) { - TODO(rqtp); - TODO(rmtp); + (void)(rqtp); + (void)(rmtp); return SUCCESS; } @@ -80,29 +81,29 @@ int timer_create( clockid_t clock_id, struct sigevent * evp, timer_t * timerid ) { - TODO(clock_id); - TODO(evp); - TODO(timerid); + (void)(clock_id); + (void)(evp); + (void)(timerid); return SUCCESS; } int timer_delete( timer_t timerid ) { - TODO(timerid); + (void)(timerid); return SUCCESS; } int timer_getoverrun( timer_t timerid ) { - TODO(timerid); + (void)(timerid); return SUCCESS; } int timer_gettime( timer_t timerid, struct itimerspec * value ) { - TODO(timerid); - TODO(value); + (void)(timerid); + (void)(value); return SUCCESS; } @@ -111,9 +112,9 @@ int timer_settime( timer_t timerid, const struct itimerspec * value, struct itimerspec * ovalue ) { - TODO(timerid); - TODO(flags); - TODO(value); - TODO(ovalue); + (void)(timerid); + (void)(flags); + (void)(value); + (void)(ovalue); return SUCCESS; } diff --git a/src/lib/posix/src/posix_utils.c b/src/lib/posix/src/posix_utils.c index 5c583e58..7d38f30f 100644 --- a/src/lib/posix/src/posix_utils.c +++ b/src/lib/posix/src/posix_utils.c @@ -8,11 +8,11 @@ * Organisation : Cyancore Core-Team */ -#include +#include #include -#include -#include -#include +#include +#include +#include size_t UTILS_strnlen( const char * const pcString, size_t xMaxLength ) { @@ -339,12 +339,10 @@ bool UTILS_ValidateTimespec( const struct timespec * const pxTimespec ) return ret; } -void UTILS_OS_Delay( const TickType_t ticks ) +void os_delay_ticks( const TickType_t ticks ) { sret_t sys_ret; super_call(scall_id_pthread_delay_ticks, ticks, RST_VAL, RST_VAL, &sys_ret); - RET_ERR_IF_FALSE(sys_ret.status == SUCCESS, EAGAIN, int); - - return SUCCESS; + RET_ERR_IF_FALSE(sys_ret.status == SUCCESS, EAGAIN, void); } From e4457e082f21d7a5d914941fb50ee850cf896f6f Mon Sep 17 00:00:00 2001 From: Pranjal Chanda Date: Wed, 12 Jan 2022 19:51:23 +0530 Subject: [PATCH 28/31] Linting issue fixed --- src/lib/posix/src/posix_mqueue.c | 3 +-- src/lib/posix/src/posix_utils.c | 38 ++++++++++++++++---------------- 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/src/lib/posix/src/posix_mqueue.c b/src/lib/posix/src/posix_mqueue.c index d5b1287d..0c3bfb90 100644 --- a/src/lib/posix/src/posix_mqueue.c +++ b/src/lib/posix/src/posix_mqueue.c @@ -209,8 +209,7 @@ int mq_close( mqd_t mqdes ) RET_ERR_IF_FALSE( s_mq_release_lock() == SUCCESS, -EBUSY, ssize_t); - err = s_mq_lock_deinit(); - RET_ERR_IF_FALSE( err == SUCCESS, err, ssize_t ); + RET_ERR_IF_FALSE( s_mq_lock_deinit() == SUCCESS, err, ssize_t ); return err; } diff --git a/src/lib/posix/src/posix_utils.c b/src/lib/posix/src/posix_utils.c index 7d38f30f..fca2a824 100644 --- a/src/lib/posix/src/posix_utils.c +++ b/src/lib/posix/src/posix_utils.c @@ -19,7 +19,7 @@ size_t UTILS_strnlen( const char * const pcString, size_t xMaxLength ) size_t len = 0; if (pcString != NULL) { - while((pcString[len] != '\0') && (len < xMaxLength)) + while((len < xMaxLength) && (pcString[len] != '\0')) { len++; } @@ -70,8 +70,8 @@ int UTILS_TimespecToTicks( const struct timespec * const pxTimespec, TickType_t * const pxResult ) { int err_staus = 0; - int64_t total_ticks = 0; - long nano_secs = 0; + int64_t total_ticks; + long nano_secs; /* Check parameters. */ if( ( pxTimespec == NULL ) || ( pxResult == NULL ) ) @@ -127,7 +127,7 @@ int UTILS_TimespecToTicks( const struct timespec * const pxTimespec, void UTILS_NanosecondsToTimespec( int64_t llSource, struct timespec * const pxDestination ) { - long lCarrySec = 0; + long carry_sec; /* Convert to timespec. */ pxDestination->tv_sec = ( time_t ) ( llSource / NANOSECONDS_PER_SECOND ); @@ -137,10 +137,10 @@ void UTILS_NanosecondsToTimespec( int64_t llSource, if( pxDestination->tv_nsec < 0L ) { /* Compute the number of seconds to carry. */ - lCarrySec = ( pxDestination->tv_nsec / ( long ) NANOSECONDS_PER_SECOND ) + 1L; + carry_sec = ( pxDestination->tv_nsec / ( long ) NANOSECONDS_PER_SECOND ) + 1L; - pxDestination->tv_sec -= ( time_t ) ( lCarrySec ); - pxDestination->tv_nsec += lCarrySec * ( long ) NANOSECONDS_PER_SECOND; + pxDestination->tv_sec -= ( time_t ) ( carry_sec ); + pxDestination->tv_nsec += carry_sec * ( long ) NANOSECONDS_PER_SECOND; } } @@ -148,7 +148,7 @@ int UTILS_TimespecAdd( const struct timespec * const x, const struct timespec * const y, struct timespec * const pxResult ) { - int64_t llPartialSec = 0; + int64_t partial_sec; int err_status = 0; /* Check parameters. */ @@ -169,9 +169,9 @@ int UTILS_TimespecAdd( const struct timespec * const x, } else { - llPartialSec = ( pxResult->tv_nsec ) / NANOSECONDS_PER_SECOND; + partial_sec = ( pxResult->tv_nsec ) / NANOSECONDS_PER_SECOND; pxResult->tv_nsec = ( pxResult->tv_nsec ) % NANOSECONDS_PER_SECOND; - pxResult->tv_sec = x->tv_sec + y->tv_sec + llPartialSec; + pxResult->tv_sec = x->tv_sec + y->tv_sec + partial_sec; /* check for overflow */ if( pxResult->tv_sec < 0 ) @@ -188,7 +188,7 @@ int UTILS_TimespecAddNanoseconds( const struct timespec * const x, int64_t lnano_secs, struct timespec * const pxResult ) { - int64_t llTotalNSec = 0; + int64_t total_sec; int err_status = 0; /* Check parameters. */ @@ -200,17 +200,17 @@ int UTILS_TimespecAddNanoseconds( const struct timespec * const x, if( err_status == 0 ) { /* add nano seconds */ - llTotalNSec = x->tv_nsec + lnano_secs; + total_sec = x->tv_nsec + lnano_secs; /* check for nano seconds overflow */ - if( llTotalNSec < 0 ) + if( total_sec < 0 ) { err_status = 1; } else { - pxResult->tv_nsec = llTotalNSec % NANOSECONDS_PER_SECOND; - pxResult->tv_sec = x->tv_sec + ( llTotalNSec / NANOSECONDS_PER_SECOND ); + pxResult->tv_nsec = total_sec % NANOSECONDS_PER_SECOND; + pxResult->tv_sec = x->tv_sec + ( total_sec / NANOSECONDS_PER_SECOND ); /* check for seconds overflow */ if( pxResult->tv_sec < 0 ) @@ -227,7 +227,7 @@ int UTILS_TimespecSubtract( const struct timespec * const x, const struct timespec * const y, struct timespec * const pxResult ) { - int iCompareResult = 0; + int comp; int err_status = 0; /* Check parameters. */ @@ -238,14 +238,14 @@ int UTILS_TimespecSubtract( const struct timespec * const x, if( err_status == 0 ) { - iCompareResult = UTILS_TimespecCompare( x, y ); + comp = UTILS_TimespecCompare( x, y ); /* if x < y then result would be negative, return 1 */ - if( iCompareResult == -1 ) + if( comp == -1 ) { err_status = 1; } - else if( iCompareResult == 0 ) + else if( comp == 0 ) { /* if times are the same return zero */ pxResult->tv_sec = 0; From c6693fe1f012d4a543e7b09204dbe996e13fbccf Mon Sep 17 00:00:00 2001 From: Pranjal Chanda Date: Wed, 12 Jan 2022 19:51:23 +0530 Subject: [PATCH 29/31] Linting issue fixed --- src/lib/build.mk | 2 +- src/lib/{posix => libposix}/build.mk | 0 src/lib/{posix => libposix}/include/cc_posix_config.h | 0 src/lib/{posix => libposix}/include/cc_posix_internal.h | 0 src/lib/{posix => libposix}/include/cc_posix_types.h | 0 src/lib/{posix => libposix}/include/posix/errno.h | 0 src/lib/{posix => libposix}/include/posix/fcntl.h | 0 src/lib/{posix => libposix}/include/posix/mqueue.h | 0 src/lib/{posix => libposix}/include/posix/pthread.h | 0 src/lib/{posix => libposix}/include/posix/sched.h | 0 src/lib/{posix => libposix}/include/posix/semaphore.h | 0 src/lib/{posix => libposix}/include/posix/signal.h | 0 src/lib/{posix => libposix}/include/posix/sys/types.h | 0 src/lib/{posix => libposix}/include/posix/time.h | 0 src/lib/{posix => libposix}/include/posix/unistd.h | 0 src/lib/{posix => libposix}/include/posix/utils.h | 0 src/lib/{posix => libposix}/src/build.mk | 0 src/lib/{posix => libposix}/src/posix_mqueue.c | 0 src/lib/{posix => libposix}/src/posix_semaphore.c | 0 src/lib/{posix => libposix}/src/posix_time.c | 0 src/lib/{posix => libposix}/src/posix_utils.c | 0 21 files changed, 1 insertion(+), 1 deletion(-) rename src/lib/{posix => libposix}/build.mk (100%) rename src/lib/{posix => libposix}/include/cc_posix_config.h (100%) rename src/lib/{posix => libposix}/include/cc_posix_internal.h (100%) rename src/lib/{posix => libposix}/include/cc_posix_types.h (100%) rename src/lib/{posix => libposix}/include/posix/errno.h (100%) rename src/lib/{posix => libposix}/include/posix/fcntl.h (100%) rename src/lib/{posix => libposix}/include/posix/mqueue.h (100%) rename src/lib/{posix => libposix}/include/posix/pthread.h (100%) rename src/lib/{posix => libposix}/include/posix/sched.h (100%) rename src/lib/{posix => libposix}/include/posix/semaphore.h (100%) rename src/lib/{posix => libposix}/include/posix/signal.h (100%) rename src/lib/{posix => libposix}/include/posix/sys/types.h (100%) rename src/lib/{posix => libposix}/include/posix/time.h (100%) rename src/lib/{posix => libposix}/include/posix/unistd.h (100%) rename src/lib/{posix => libposix}/include/posix/utils.h (100%) rename src/lib/{posix => libposix}/src/build.mk (100%) rename src/lib/{posix => libposix}/src/posix_mqueue.c (100%) rename src/lib/{posix => libposix}/src/posix_semaphore.c (100%) rename src/lib/{posix => libposix}/src/posix_time.c (100%) rename src/lib/{posix => libposix}/src/posix_utils.c (100%) diff --git a/src/lib/build.mk b/src/lib/build.mk index 311f487d..1ab699cc 100644 --- a/src/lib/build.mk +++ b/src/lib/build.mk @@ -14,7 +14,7 @@ LIB_DIR := $(GET_PATH) # Mandatory libraries include $(LIB_DIR)/libresource/build.mk include $(LIB_DIR)/libc/build.mk -include $(LIB_DIR)/posix/build.mk +include $(LIB_DIR)/libposix/build.mk #================================================== include $(LIB_DIR)/libnmath/build.mk diff --git a/src/lib/posix/build.mk b/src/lib/libposix/build.mk similarity index 100% rename from src/lib/posix/build.mk rename to src/lib/libposix/build.mk diff --git a/src/lib/posix/include/cc_posix_config.h b/src/lib/libposix/include/cc_posix_config.h similarity index 100% rename from src/lib/posix/include/cc_posix_config.h rename to src/lib/libposix/include/cc_posix_config.h diff --git a/src/lib/posix/include/cc_posix_internal.h b/src/lib/libposix/include/cc_posix_internal.h similarity index 100% rename from src/lib/posix/include/cc_posix_internal.h rename to src/lib/libposix/include/cc_posix_internal.h diff --git a/src/lib/posix/include/cc_posix_types.h b/src/lib/libposix/include/cc_posix_types.h similarity index 100% rename from src/lib/posix/include/cc_posix_types.h rename to src/lib/libposix/include/cc_posix_types.h diff --git a/src/lib/posix/include/posix/errno.h b/src/lib/libposix/include/posix/errno.h similarity index 100% rename from src/lib/posix/include/posix/errno.h rename to src/lib/libposix/include/posix/errno.h diff --git a/src/lib/posix/include/posix/fcntl.h b/src/lib/libposix/include/posix/fcntl.h similarity index 100% rename from src/lib/posix/include/posix/fcntl.h rename to src/lib/libposix/include/posix/fcntl.h diff --git a/src/lib/posix/include/posix/mqueue.h b/src/lib/libposix/include/posix/mqueue.h similarity index 100% rename from src/lib/posix/include/posix/mqueue.h rename to src/lib/libposix/include/posix/mqueue.h diff --git a/src/lib/posix/include/posix/pthread.h b/src/lib/libposix/include/posix/pthread.h similarity index 100% rename from src/lib/posix/include/posix/pthread.h rename to src/lib/libposix/include/posix/pthread.h diff --git a/src/lib/posix/include/posix/sched.h b/src/lib/libposix/include/posix/sched.h similarity index 100% rename from src/lib/posix/include/posix/sched.h rename to src/lib/libposix/include/posix/sched.h diff --git a/src/lib/posix/include/posix/semaphore.h b/src/lib/libposix/include/posix/semaphore.h similarity index 100% rename from src/lib/posix/include/posix/semaphore.h rename to src/lib/libposix/include/posix/semaphore.h diff --git a/src/lib/posix/include/posix/signal.h b/src/lib/libposix/include/posix/signal.h similarity index 100% rename from src/lib/posix/include/posix/signal.h rename to src/lib/libposix/include/posix/signal.h diff --git a/src/lib/posix/include/posix/sys/types.h b/src/lib/libposix/include/posix/sys/types.h similarity index 100% rename from src/lib/posix/include/posix/sys/types.h rename to src/lib/libposix/include/posix/sys/types.h diff --git a/src/lib/posix/include/posix/time.h b/src/lib/libposix/include/posix/time.h similarity index 100% rename from src/lib/posix/include/posix/time.h rename to src/lib/libposix/include/posix/time.h diff --git a/src/lib/posix/include/posix/unistd.h b/src/lib/libposix/include/posix/unistd.h similarity index 100% rename from src/lib/posix/include/posix/unistd.h rename to src/lib/libposix/include/posix/unistd.h diff --git a/src/lib/posix/include/posix/utils.h b/src/lib/libposix/include/posix/utils.h similarity index 100% rename from src/lib/posix/include/posix/utils.h rename to src/lib/libposix/include/posix/utils.h diff --git a/src/lib/posix/src/build.mk b/src/lib/libposix/src/build.mk similarity index 100% rename from src/lib/posix/src/build.mk rename to src/lib/libposix/src/build.mk diff --git a/src/lib/posix/src/posix_mqueue.c b/src/lib/libposix/src/posix_mqueue.c similarity index 100% rename from src/lib/posix/src/posix_mqueue.c rename to src/lib/libposix/src/posix_mqueue.c diff --git a/src/lib/posix/src/posix_semaphore.c b/src/lib/libposix/src/posix_semaphore.c similarity index 100% rename from src/lib/posix/src/posix_semaphore.c rename to src/lib/libposix/src/posix_semaphore.c diff --git a/src/lib/posix/src/posix_time.c b/src/lib/libposix/src/posix_time.c similarity index 100% rename from src/lib/posix/src/posix_time.c rename to src/lib/libposix/src/posix_time.c diff --git a/src/lib/posix/src/posix_utils.c b/src/lib/libposix/src/posix_utils.c similarity index 100% rename from src/lib/posix/src/posix_utils.c rename to src/lib/libposix/src/posix_utils.c From 6f8bc372e46b28d1af0c7b94b5a3f9c1b5243e86 Mon Sep 17 00:00:00 2001 From: Pranjal Chanda Date: Thu, 13 Jan 2022 13:28:43 +0530 Subject: [PATCH 30/31] Local integration and Test complete --- sonar-project.properties | 11 ++++++ src/lib/libc/printf.c | 2 +- src/lib/libposix/include/posix/errno.h | 19 ----------- src/lib/libposix/src/posix_semaphore.c | 46 +++++++++++++++++--------- 4 files changed, 43 insertions(+), 35 deletions(-) create mode 100644 sonar-project.properties diff --git a/sonar-project.properties b/sonar-project.properties new file mode 100644 index 00000000..643645d7 --- /dev/null +++ b/sonar-project.properties @@ -0,0 +1,11 @@ +sonar.login=a15620523b79247d23371f05082ab54e16fbb805 +sonar.organization=cyancore +sonar.projectKey=VisorFolks_cyancore +sonar.sources=src +sonar.cfamily.build-wrapper-output=toolchain/bw-output +sonar.verbose=false +sonar.host.url=https://sonarcloud.io +sonar.verbose=true +sonar.cfamily.threads=8 +sonar.cfamily.cache.enabled=true +sonar.cfamily.cache.path=toolchain/bw-output/cfamily_cache diff --git a/src/lib/libc/printf.c b/src/lib/libc/printf.c index 570f72b7..efb48528 100644 --- a/src/lib/libc/printf.c +++ b/src/lib/libc/printf.c @@ -107,7 +107,7 @@ int vprintf(const char *fmt, va_list args) break; case 'c': str = va_arg(args, char *); - ret += console_putc((int)str); + ret += console_putc((int)str[0]); break; case 's': str = va_arg(args, char *); diff --git a/src/lib/libposix/include/posix/errno.h b/src/lib/libposix/include/posix/errno.h index c20a3b75..01501b40 100644 --- a/src/lib/libposix/include/posix/errno.h +++ b/src/lib/libposix/include/posix/errno.h @@ -14,25 +14,6 @@ #include -/* Undefine all errnos to avoid redefinition errors with system errnos. */ -#undef EPERM -#undef ENOENT -#undef EBADF -#undef EAGAIN -#undef ENOMEM -#undef EEXIST -#undef EBUSY -#undef EINVAL -#undef ENOSPC -#undef ERANGE -#undef ENAMETOOLONG -#undef EDEADLK -#undef EOVERFLOW -#undef ENOSYS -#undef EMSGSIZE -#undef ENOTSUP -#undef ETIMEDOUT - /** * @name Definition of POSIX errnos. */ diff --git a/src/lib/libposix/src/posix_semaphore.c b/src/lib/libposix/src/posix_semaphore.c index ed0a09f7..d1d943ff 100644 --- a/src/lib/libposix/src/posix_semaphore.c +++ b/src/lib/libposix/src/posix_semaphore.c @@ -20,10 +20,13 @@ ********************/ static int s_sem_wait( sem_t * sem ) { - sret_t sem_sys_ret; - ASSERT_IF_FALSE(sem == NULL, int); + sret_t sem_sys_ret= + { + .status = SUCCESS + }; + ASSERT_IF_FALSE(sem != NULL, int); - super_call(scall_id_sem_wait, (unsigned int) *sem, RST_VAL, RST_VAL, &sem_sys_ret); + super_call(scall_id_sem_wait, (uintptr_t) *sem, RST_VAL, RST_VAL, &sem_sys_ret); return sem_sys_ret.status; } @@ -33,10 +36,13 @@ static int s_sem_wait( sem_t * sem ) ********************/ int sem_destroy( sem_t * sem ) { - sret_t sem_sys_ret; - ASSERT_IF_FALSE(sem == NULL, int); + sret_t sem_sys_ret= + { + .status = SUCCESS + }; + ASSERT_IF_FALSE(sem != NULL, int); - super_call(scall_id_sem_destroy, (unsigned int) *sem, RST_VAL, RST_VAL, &sem_sys_ret); + super_call(scall_id_sem_destroy, (uintptr_t) *sem, RST_VAL, RST_VAL, &sem_sys_ret); RET_ERR_IF_FALSE(sem_sys_ret.status == SUCCESS, sem_sys_ret.status, int); *sem = (sem_t) NULL; @@ -47,10 +53,13 @@ int sem_destroy( sem_t * sem ) int sem_getvalue( sem_t * sem, int * sval ) { - sret_t sem_sys_ret; - ASSERT_IF_FALSE(sem == NULL, int); + sret_t sem_sys_ret= + { + .status = SUCCESS + }; + ASSERT_IF_FALSE(sem != NULL, int); - super_call(scall_id_sem_getvalue, (unsigned int) *sem, RST_VAL, RST_VAL, &sem_sys_ret); + super_call(scall_id_sem_getvalue, (uintptr_t) *sem, RST_VAL, RST_VAL, &sem_sys_ret); RET_ERR_IF_FALSE(sem_sys_ret.status == SUCCESS, sem_sys_ret.status, int); *sval = (int) sem_sys_ret.p; @@ -62,8 +71,12 @@ int sem_init( sem_t * sem, int pshared _UNUSED, unsigned value ) { - sret_t sem_sys_ret; - ASSERT_IF_FALSE(sem == NULL, int); + sret_t sem_sys_ret= + { + .status = SUCCESS + }; + + ASSERT_IF_FALSE(sem != NULL, int); super_call(scall_id_sem_init, value, RST_VAL, RST_VAL, &sem_sys_ret); RET_ERR_IF_FALSE(sem_sys_ret.status == SUCCESS, sem_sys_ret.status, int); @@ -75,10 +88,13 @@ int sem_init( sem_t * sem, int sem_post( sem_t * sem ) { - sret_t sem_sys_ret; - ASSERT_IF_FALSE(sem == NULL, int); + sret_t sem_sys_ret= + { + .status = SUCCESS + }; + ASSERT_IF_FALSE(sem != NULL, int); - super_call(scall_id_sem_post, (unsigned int) *sem, RST_VAL, RST_VAL, &sem_sys_ret); + super_call(scall_id_sem_post, (uintptr_t) *sem, RST_VAL, RST_VAL, &sem_sys_ret); return sem_sys_ret.status; } @@ -86,7 +102,7 @@ int sem_post( sem_t * sem ) int sem_timedwait( sem_t * sem, const struct timespec * abstime ) { - ASSERT_IF_FALSE(sem == NULL, int); + ASSERT_IF_FALSE(sem != NULL, int); int err = SUCCESS; TickType_t abs_ticks; From 6ea756627694b4dc1f93092f145b28adf2f7634e Mon Sep 17 00:00:00 2001 From: Pranjal Chanda Date: Thu, 13 Jan 2022 13:58:49 +0530 Subject: [PATCH 31/31] README.md update --- README.md | 2 +- sonar-project.properties | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index eb507e72..df72933a 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,7 @@ For details about the framework, follow the README.md in each folder. Host system requirements: * Linux Machine (WSL/Dedicated machine) -* make utility and cppcheck installed; run +* make utility and cppcheck installed; run ```sh $ sudo apt install build-essential cppcheck -y ``` diff --git a/sonar-project.properties b/sonar-project.properties index 643645d7..e127fc2b 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -1,4 +1,3 @@ -sonar.login=a15620523b79247d23371f05082ab54e16fbb805 sonar.organization=cyancore sonar.projectKey=VisorFolks_cyancore sonar.sources=src