From a638ff20189b8c057e4fbd8add41c321f2e8049d Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Wed, 10 Aug 2022 01:16:40 +0530 Subject: [PATCH 01/21] Add multi-core support and bug fixes - Added irq save locks to support multi-core processing and multi-threading - Fixed free call and added boundary case checks - Added alignment check for few arch that need data memory alignment --- src/lib/libc/include/stdlib.h | 1 + src/lib/libc/malloc_lite.c | 77 +++++++++++++++++-- .../mega_avr/atmega328p/include/plat_mem.h | 1 + .../sifive/fe310g002/include/plat_mem.h | 1 + 4 files changed, 73 insertions(+), 7 deletions(-) diff --git a/src/lib/libc/include/stdlib.h b/src/lib/libc/include/stdlib.h index f6f9368e..bbf0e28e 100644 --- a/src/lib/libc/include/stdlib.h +++ b/src/lib/libc/include/stdlib.h @@ -23,3 +23,4 @@ int atexit(void (*func)(int)); void exit(int status) _NORETURN; void *malloc(size_t); void free(void *); +void heap_dump(void); diff --git a/src/lib/libc/malloc_lite.c b/src/lib/libc/malloc_lite.c index cae2aeca..9eb23a2c 100644 --- a/src/lib/libc/malloc_lite.c +++ b/src/lib/libc/malloc_lite.c @@ -13,17 +13,41 @@ #include #include #include +#include +#include +#include +#include +#include + +extern uint8_t _heap_start, _heap_end; +static istate_t state; +static lock_t mlock; typedef struct chunk { size_t size; - int free; +#if HEAP_ALIGN > 1 + size_t free; +#else + uint8_t free; +#endif struct chunk *next; } chunk_t; -extern uint8_t _heap_start, _heap_end; static chunk_t *freeList; +static void heap_lock(void) +{ + lock_acquire(&mlock); + arch_di_save_state(&state); +} + +static void heap_unlock(void) +{ + lock_release(&mlock); + arch_ei_restore_state(&state); +} + static void split(chunk_t *fit_slot, size_t size) { chunk_t *new = (void *)((void *) fit_slot + size + sizeof(chunk_t)); @@ -53,20 +77,31 @@ static void merge() status_t platform_init_heap() { + heap_lock(); memset(&_heap_start, 0, (size_t)(&_heap_end - &_heap_start)); freeList = (void *)&_heap_start; freeList->size = (size_t)(&_heap_end - &_heap_start) - sizeof(chunk_t); freeList->free = 1; freeList->next = NULL; + heap_unlock(); return success; } void *malloc(size_t n_bytes) { chunk_t *cur; + void *p = NULL; - if(!freeList->size) + if(!n_bytes) return NULL; + +#if HEAP_ALIGN > 1 + n_bytes += ALIGN_BOUND - (n_bytes % ALIGN_BOUND); +#endif + + heap_lock(); + if(!freeList->size) + goto exit; cur = freeList; while(cur->size < n_bytes || (!cur->free && cur->next)) cur = cur->next; @@ -75,17 +110,45 @@ void *malloc(size_t n_bytes) else if(cur->size > (n_bytes + sizeof(chunk_t))) split(cur, n_bytes); else - return NULL; - return (void *)(++cur); + goto exit; + p = (void *)(++cur); +exit: + heap_unlock(); + return p; } void free(void *ptr) { - if((void *)&_heap_start <= ptr && ptr <= (void *)&_heap_end) + if(ptr == NULL) + return; + + chunk_t *cur = (chunk_t *)ptr - 1; + heap_lock(); + if((void *)&_heap_start <= (void *)cur && + (void *)cur <= (void *)&_heap_end) { - chunk_t *cur = ptr; cur->free = 1; merge(); + merge(); } + heap_unlock(); return; } + +void heap_dump(void) +{ + size_t i; + unsigned int cntr; + printf("Heap Dump: %p - %p\n", &_heap_start, &_heap_end); + for(i = (size_t)&_heap_start; i < (size_t)&_heap_end; i+=32) + { + printf("["); + for(cntr = 0; cntr < 32; cntr++) + { + printf("%02x", MMIO8(i+cntr)); + if(cntr && !((cntr + 1) % 4) && cntr != 31) + printf(" "); + } + printf("]\n"); + } +} diff --git a/src/platform/mega_avr/atmega328p/include/plat_mem.h b/src/platform/mega_avr/atmega328p/include/plat_mem.h index 4d9fb218..250d7358 100644 --- a/src/platform/mega_avr/atmega328p/include/plat_mem.h +++ b/src/platform/mega_avr/atmega328p/include/plat_mem.h @@ -26,3 +26,4 @@ #define STACK_SIZE 0x0200 #define HEAP_SIZE 128 +#define HEAP_ALIGN 1 diff --git a/src/platform/sifive/fe310g002/include/plat_mem.h b/src/platform/sifive/fe310g002/include/plat_mem.h index 31719ae7..34f408c6 100644 --- a/src/platform/sifive/fe310g002/include/plat_mem.h +++ b/src/platform/sifive/fe310g002/include/plat_mem.h @@ -30,3 +30,4 @@ #define STACK_SIZE_PCPU 0xc00 #define HEAP_SIZE 512 +#define HEAP_ALIGN 4 From 255d02afebb9843e965459b8007f65fdb0a0f866 Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Wed, 10 Aug 2022 17:56:20 +0530 Subject: [PATCH 02/21] Added more function for heap management - Added calloc and realloc function - Added heap_usage and improvised heap_dump - Fixed printf "%%" format specifier parser --- src/include/status.h | 1 + src/include/utils.h | 13 ++++++++ src/lib/libc/include/stdlib.h | 3 ++ src/lib/libc/malloc_lite.c | 58 +++++++++++++++++++++++++++++++++-- src/lib/libc/printf.c | 1 - 5 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 src/include/utils.h diff --git a/src/include/status.h b/src/include/status.h index f4f23cc3..634be50f 100644 --- a/src/include/status.h +++ b/src/include/status.h @@ -12,6 +12,7 @@ #define _STATUS_H_ #include +#include typedef enum status { diff --git a/src/include/utils.h b/src/include/utils.h new file mode 100644 index 00000000..115dc2ea --- /dev/null +++ b/src/include/utils.h @@ -0,0 +1,13 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2022, Cyancore Team + * + * File Name : utils.h + * Description : This file defines the utilities macros + * Primary Author : Akash Kollipara [akashkollipara@gmail.com] + * Organisation : Cyancore Core-Team + */ + +#pragma once + +#define ROUNDUP_ALIGN(x, align) x += align - (x % align) diff --git a/src/lib/libc/include/stdlib.h b/src/lib/libc/include/stdlib.h index bbf0e28e..698838cc 100644 --- a/src/lib/libc/include/stdlib.h +++ b/src/lib/libc/include/stdlib.h @@ -23,4 +23,7 @@ int atexit(void (*func)(int)); void exit(int status) _NORETURN; void *malloc(size_t); void free(void *); +void *calloc(size_t, size_t); +void *realloc(void *, size_t); void heap_dump(void); +size_t heap_usage(void); diff --git a/src/lib/libc/malloc_lite.c b/src/lib/libc/malloc_lite.c index 9eb23a2c..6c8dffcf 100644 --- a/src/lib/libc/malloc_lite.c +++ b/src/lib/libc/malloc_lite.c @@ -19,7 +19,7 @@ #include #include -extern uint8_t _heap_start, _heap_end; +extern uint8_t _heap_start, _heap_end, _heap_size; static istate_t state; static lock_t mlock; @@ -75,6 +75,11 @@ static void merge() } } +static chunk_t *get_header(void *p) +{ + return (chunk_t *)p - 1; +} + status_t platform_init_heap() { heap_lock(); @@ -96,7 +101,7 @@ void *malloc(size_t n_bytes) return NULL; #if HEAP_ALIGN > 1 - n_bytes += ALIGN_BOUND - (n_bytes % ALIGN_BOUND); + ROUNDUP_ALIGN(n_bytes, ALIGN_BOUND); #endif heap_lock(); @@ -122,7 +127,7 @@ void free(void *ptr) if(ptr == NULL) return; - chunk_t *cur = (chunk_t *)ptr - 1; + chunk_t *cur = get_header(ptr); heap_lock(); if((void *)&_heap_start <= (void *)cur && (void *)cur <= (void *)&_heap_end) @@ -135,11 +140,57 @@ void free(void *ptr) return; } +void *calloc(size_t n_blocks, size_t n_bytes) +{ + n_bytes *= n_blocks; + void *p = malloc(n_bytes); + if(p) + memset(p, 0, n_bytes); + return p; +} + +void *realloc(void *p, size_t n_bytes) +{ + if(!p) + return malloc(n_bytes); + if(!n_bytes) + { + free(p); + return NULL; + } + + chunk_t *header = get_header(p); + void *new_p = malloc(n_bytes); + if(!new_p) + return NULL; + memcpy(new_p, p, header->size); + free(p); + return new_p; +} + +size_t heap_usage(void) +{ + unsigned int usage = 0; + chunk_t *head = (chunk_t *)&_heap_start; + while(head->next != NULL) + { + if(!head->free) + usage += head->size + sizeof(chunk_t); + head = head->next; + } + return usage; +} + void heap_dump(void) { size_t i; unsigned int cntr; + unsigned int h_used, h_perc; + h_used = heap_usage(); + h_perc = (h_used * 100)/(unsigned int)&_heap_size; printf("Heap Dump: %p - %p\n", &_heap_start, &_heap_end); + printf("Heap Used: %u/%u - %u%%\n", h_used, + (unsigned int)&_heap_size, h_perc); for(i = (size_t)&_heap_start; i < (size_t)&_heap_end; i+=32) { printf("["); @@ -152,3 +203,4 @@ void heap_dump(void) printf("]\n"); } } + diff --git a/src/lib/libc/printf.c b/src/lib/libc/printf.c index bb0cc587..7a7c9708 100644 --- a/src/lib/libc/printf.c +++ b/src/lib/libc/printf.c @@ -176,7 +176,6 @@ int vprintf(const FILE *dev, bool en_stdout, const char *fmt, va_list args) } case '%': ret += __fputc(dev, en_stdout, *fmt); - fmt++; break; default: return -1; From a75fe6c426c846fae8f01ce266948e5213a7dadc Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Thu, 11 Aug 2022 17:23:11 +0530 Subject: [PATCH 03/21] Updated all drivers to use heap memory - Updated all driver programs to use heap memory instead of global memory space - Removed redundant variables from avr arch code - Updated driver engine to print error info - Increased heap sized for mega_avr and sifive platforms --- src/arch/avr/8/common_5x_6/terravisor/arch.c | 3 + .../terravisor/interrupt_handler.c | 5 +- .../con_serial_mega_avr/console_serial.c | 61 +++++++++-------- .../con_serial_mega_avr/earlycon_serial.c | 42 +++++++----- .../con_serial_sifive/console_serial.c | 68 +++++++++++-------- src/driver/driver.c | 2 +- src/driver/interrupt/plic/plic.c | 59 ++++++++-------- src/driver/sysclk/sysclk_prci/sysclk_prci.c | 25 ++++--- .../mega_avr/atmega2560/include/plat_mem.h | 2 +- .../mega_avr/atmega328p/include/plat_mem.h | 2 +- .../mega_avr/common/platform/platform_timer.c | 32 +++++---- .../mega_avr/common/platform/platform_wdt.c | 59 +++++++++------- src/platform/mega_avr/common/sections.ld.sx | 2 +- .../sifive/common_fe310/platform/plat_timer.c | 20 +++--- .../sifive/common_fe310/sections.ld.sx | 2 +- .../sifive/fe310g002-bl/include/plat_mem.h | 1 + .../sifive/fe310g002/include/plat_mem.h | 2 +- 17 files changed, 218 insertions(+), 169 deletions(-) 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 6a0145af..1530bf8c 100644 --- a/src/arch/avr/8/common_5x_6/terravisor/arch.c +++ b/src/arch/avr/8/common_5x_6/terravisor/arch.c @@ -96,6 +96,8 @@ void _NORETURN arch_panic_handler_callback() { context_frame_t *frame; frame = get_context_frame(); + if(!frame) + goto panic; syslog_stdout_enable(); sysdbg("r0=%p\tr1=%p\tr2=%p\tr3=%p\tr4=%p\tr5=%p\n", frame->r0, frame->r1, frame->r2, frame->r3, frame->r4, frame->r5); @@ -112,5 +114,6 @@ void _NORETURN arch_panic_handler_callback() #if DEBUG==0 syslog(info, "SP=%p\tSREG = %p\n", frame, frame->sreg); #endif +panic: while(1) arch_wfi(); } diff --git a/src/arch/avr/8/common_5x_6/terravisor/interrupt_handler.c b/src/arch/avr/8/common_5x_6/terravisor/interrupt_handler.c index 1a94c88f..a47cb59c 100644 --- a/src/arch/avr/8/common_5x_6/terravisor/interrupt_handler.c +++ b/src/arch/avr/8/common_5x_6/terravisor/interrupt_handler.c @@ -106,12 +106,9 @@ void interrupt_handler(unsigned char id, context_frame_t *frame) } -/* Fixed memory block of 33 bytes on bss */ -context_frame_t snapshot_frame; - context_frame_t *get_context_frame() { if(local_frame) return local_frame; - return &snapshot_frame; + return NULL; } diff --git a/src/driver/console/con_serial_mega_avr/console_serial.c b/src/driver/console/con_serial_mega_avr/console_serial.c index 3dcb36b5..b65badfe 100644 --- a/src/driver/console/con_serial_mega_avr/console_serial.c +++ b/src/driver/console/con_serial_mega_avr/console_serial.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -22,7 +23,7 @@ #include #include -static uart_port_t console_port; +static uart_port_t *console_port; static void console_serial_write_irq_handler(void); static void console_serial_read_irq_handler(void); @@ -48,24 +49,27 @@ static status_t console_serial_setup() return mres.status; } dp = (module_t *)mres.p; - console_port.port_id = dp->id; - console_port.clk_id = dp->clk_id; - console_port.baddr = dp->baddr; - console_port.stride = dp->stride; - console_port.baud = dp->clk; - console_port.tx_irq = &dp->interrupt[1]; - console_port.tx_handler = console_serial_write_irq_handler; - console_port.rx_irq = &dp->interrupt[0]; - console_port.rx_handler = console_serial_read_irq_handler; - - sysdbg2("UART engine @ %p\n", console_port.baddr); - sysdbg2("UART baud @ %lubps\n", console_port.baud); + console_port = (uart_port_t *)malloc(sizeof(uart_port_t)); + if(!console_port) + return error_memory_low; + console_port->port_id = dp->id; + console_port->clk_id = dp->clk_id; + console_port->baddr = dp->baddr; + console_port->stride = dp->stride; + console_port->baud = dp->clk; + console_port->tx_irq = &dp->interrupt[1]; + console_port->tx_handler = console_serial_write_irq_handler; + console_port->rx_irq = &dp->interrupt[0]; + console_port->rx_handler = console_serial_read_irq_handler; + + sysdbg2("UART engine @ %p\n", console_port->baddr); + sysdbg2("UART baud @ %lubps\n", console_port->baud); sysdbg2("UART irqs - %u & %u\n", dp->interrupt[1].id, dp->interrupt[0].id); /* * If memory mapping is applicable, * put it in mmu supported guide. */ - return uart_setup(&console_port, trx, no_parity); // + return uart_setup(console_port, trx, no_parity); // } static int_wait_t con_write_wait; @@ -79,7 +83,7 @@ status_t console_serial_write(const char c) { status_t ret; ret = wait_lock(&con_write_wait); - ret |= uart_tx(&console_port, c); + ret |= uart_tx(console_port, c); ret |= wait_till_irq(&con_write_wait); return ret; } @@ -90,7 +94,7 @@ static char con_char; static void console_serial_read_irq_handler() { wait_release_on_irq(&con_read_wait); - uart_rx(&console_port, &con_char); + uart_rx(console_port, &con_char); } static status_t console_serial_read(char *c) @@ -102,24 +106,19 @@ static status_t console_serial_read(char *c) return ret; } -static status_t console_serial_flush() -{ - return success; -} - -static console_t console_serial_driver = -{ - .write = &console_serial_write, - .read = &console_serial_read, - .flush = &console_serial_flush -}; +static console_t *console_serial_driver; status_t console_serial_driver_setup() { status_t ret; driver_exit("earlycon"); + console_serial_driver = (console_t *)malloc(sizeof(console_t)); + if(!console_serial_driver) + return error_memory_low; + console_serial_driver->write = &console_serial_write; + console_serial_driver->read = &console_serial_read; ret = console_serial_setup(); - ret |= console_attach_device(ret, &console_serial_driver); + ret |= console_attach_device(ret, console_serial_driver); return ret; } @@ -127,8 +126,10 @@ status_t console_serial_driver_exit() { status_t ret; ret = console_release_device(); - ret |= uart_shutdown(&console_port); - driver_setup("earlycon"); + ret |= uart_shutdown(console_port); + free(console_serial_driver); + free(console_port); + ret |= driver_setup("earlycon"); return ret; } diff --git a/src/driver/console/con_serial_mega_avr/earlycon_serial.c b/src/driver/console/con_serial_mega_avr/earlycon_serial.c index 5eef37e0..b31793f3 100644 --- a/src/driver/console/con_serial_mega_avr/earlycon_serial.c +++ b/src/driver/console/con_serial_mega_avr/earlycon_serial.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -22,7 +23,7 @@ #include #include -static uart_port_t earlycon_port; +static uart_port_t *earlycon_port; static status_t earlycon_serial_setup() { @@ -45,39 +46,44 @@ static status_t earlycon_serial_setup() return mres.status; } dp = (module_t *)mres.p; - earlycon_port.port_id = dp->id; - earlycon_port.clk_id = dp->clk_id; - earlycon_port.baddr = dp->baddr; - earlycon_port.stride = dp->stride; - earlycon_port.baud = dp->clk; + earlycon_port = (uart_port_t *)malloc(sizeof(uart_port_t)); + if(!earlycon_port) + return error_memory_low; - sysdbg2("UART engine @ %p\n", earlycon_port.baddr); - sysdbg2("UART baud @ %lubps\n", earlycon_port.baud); + earlycon_port->port_id = dp->id; + earlycon_port->clk_id = dp->clk_id; + earlycon_port->baddr = dp->baddr; + earlycon_port->stride = dp->stride; + earlycon_port->baud = dp->clk; + + sysdbg2("UART engine @ %p\n", earlycon_port->baddr); + sysdbg2("UART baud @ %lubps\n", earlycon_port->baud); /* * If memory mapping is applicable, * put it in mmu supported guide. */ - return uart_setup(&earlycon_port, tx, no_parity); + return uart_setup(earlycon_port, tx, no_parity); } static status_t earlycon_serial_write(const char c) { status_t ret; - ret = uart_tx(&earlycon_port, c); - uart_tx_wait_till_done(&earlycon_port); + ret = uart_tx(earlycon_port, c); + uart_tx_wait_till_done(earlycon_port); return ret; } -static console_t earlycon_serial_driver = -{ - .write = &earlycon_serial_write, -}; +static console_t *earlycon_serial_driver; status_t earlycon_serial_driver_setup() { status_t ret; + earlycon_serial_driver = (console_t *)malloc(sizeof(console_t)); + if(!earlycon_serial_driver) + return error_memory_low; + earlycon_serial_driver->write = &earlycon_serial_write; ret = earlycon_serial_setup(); - ret |= console_attach_device(ret, &earlycon_serial_driver); + ret |= console_attach_device(ret, earlycon_serial_driver); return ret; } @@ -85,7 +91,9 @@ status_t earlycon_serial_driver_exit() { status_t ret; ret = console_release_device(); - ret |= uart_shutdown(&earlycon_port); + ret |= uart_shutdown(earlycon_port); + free(earlycon_port); + free(earlycon_serial_driver); return ret; } diff --git a/src/driver/console/con_serial_sifive/console_serial.c b/src/driver/console/con_serial_sifive/console_serial.c index add2fdb5..83e995da 100644 --- a/src/driver/console/con_serial_sifive/console_serial.c +++ b/src/driver/console/con_serial_sifive/console_serial.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -23,31 +24,34 @@ #include #include -static uart_port_t console_port; +static uart_port_t *console_port; static void console_serial_irq_handler(void); static status_t console_serial_setup(void) { - uart_get_properties(&console_port, console_uart); + console_port = (uart_port_t *)malloc(sizeof(uart_port_t)); + if(!console_port) + return error_memory_low; + uart_get_properties(console_port, console_uart); - console_port.irq_handler = console_serial_irq_handler; + console_port->irq_handler = console_serial_irq_handler; - sysdbg2("UART engine @ %p\n", console_port.baddr); - sysdbg2("UART baud @ %lubps\n", console_port.baud); - sysdbg2("UART irqs - %u\n", console_port.irq); + sysdbg2("UART engine @ %p\n", console_port->baddr); + sysdbg2("UART baud @ %lubps\n", console_port->baud); + sysdbg2("UART irqs - %u\n", console_port->irq); /* * If memory mapping is applicable, * put it in mmu supported guide. */ - return uart_setup(&console_port, trx, no_parity); + return uart_setup(console_port, trx, no_parity); } static status_t console_serial_write(const char c) { status_t ret; - while(!uart_buffer_available(&console_port)); - ret = uart_tx(&console_port, c); + while(!uart_buffer_available(console_port)); + ret = uart_tx(console_port, c); return ret; } @@ -73,24 +77,19 @@ static status_t console_serial_read(char *c) static void console_serial_irq_handler(void) { - if(uart_rx_pending(&console_port)) + if(uart_rx_pending(console_port)) { wait_release_on_irq(&con_read_wait); - while(uart_rx_pending(&console_port)) + while(uart_rx_pending(console_port)) { - uart_rx(&console_port, &con_buff[rp++]); + uart_rx(console_port, &con_buff[rp++]); rp = rp % 32; occ++; } } } -static console_t console_serial_driver = -{ - .write = &console_serial_write, - .read = &console_serial_read, - .payload_size = (unsigned int *)&occ, -}; +static console_t *console_serial_driver; static status_t console_serial_pre_clk_config(void) { @@ -99,23 +98,31 @@ static status_t console_serial_pre_clk_config(void) static status_t console_serial_post_clk_config(void) { - uart_update_baud(&console_port); + uart_update_baud(console_port); return success; } -static sysclk_config_clk_callback_t console_handle = -{ - .pre_config = &console_serial_pre_clk_config, - .post_config = &console_serial_post_clk_config -}; +static sysclk_config_clk_callback_t *console_handle; status_t console_serial_driver_setup(void) { status_t ret; driver_exit("earlycon"); + console_serial_driver = (console_t *)malloc(sizeof(console_t)); + if(!console_serial_driver) + return error_memory_low; + console_serial_driver->write = &console_serial_write; + console_serial_driver->read = &console_serial_read; + console_serial_driver->payload_size = (unsigned int *)&occ; + + console_handle = (sysclk_config_clk_callback_t*)malloc(sizeof(sysclk_config_clk_callback_t)); + if(!console_handle) + return error_memory_low; + console_handle->pre_config = &console_serial_pre_clk_config; + console_handle->post_config = &console_serial_post_clk_config; ret = console_serial_setup(); - ret |= sysclk_register_config_clk_callback(&console_handle); - ret |= console_attach_device(ret, &console_serial_driver); + ret |= sysclk_register_config_clk_callback(console_handle); + ret |= console_attach_device(ret, console_serial_driver); return ret; } @@ -123,9 +130,12 @@ status_t console_serial_driver_exit(void) { status_t ret; ret = console_release_device(); - ret |= sysclk_deregister_config_clk_callback(&console_handle); - ret |= uart_shutdown(&console_port); - driver_setup("earlycon"); + ret |= sysclk_deregister_config_clk_callback(console_handle); + ret |= uart_shutdown(console_port); + free(console_port); + free(console_serial_driver); + free(console_handle); + ret |= driver_setup("earlycon"); return ret; } diff --git a/src/driver/driver.c b/src/driver/driver.c index fd16c16a..2f72c21f 100644 --- a/src/driver/driver.c +++ b/src/driver/driver.c @@ -202,7 +202,7 @@ status_t driver_register(device_t *dev _UNUSED) ret = dev->driver_setup(); (ret == success) ? syslog(pass, "Started %s\n", dev->name) : - syslog(fail, "Couldn't start %s\n", dev->name); + syslog(fail, "Couldn't start %s (Err: -%p)\n", dev->name, -ret); exit: return ret; } diff --git a/src/driver/interrupt/plic/plic.c b/src/driver/interrupt/plic/plic.c index d9696aad..49d6ff17 100644 --- a/src/driver/interrupt/plic/plic.c +++ b/src/driver/interrupt/plic/plic.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -28,7 +29,7 @@ #include "plic_private.h" -static plic_port_t port; +static plic_port_t *port; static status_t plic_setup() { @@ -40,38 +41,41 @@ static status_t plic_setup() if(mres.status != success) return mres.status; dp = (module_t *)mres.p; - port.baddr = dp->baddr; - port.stride = dp->stride; - port.port_id = plic; - port.irq = &dp->interrupt[0]; - syslog(info, "PLIC @ 0x%x found.\n", (unsigned int)port.baddr); + port = (plic_port_t *)malloc(sizeof(plic_port_t)); + if(!port) + return error_memory_low; + port->baddr = dp->baddr; + port->stride = dp->stride; + port->port_id = plic; + port->irq = &dp->interrupt[0]; + syslog(info, "PLIC @ 0x%x found.\n", (unsigned int)port->baddr); return success; } static uint32_t plic_get_priority(uint32_t irq_id) { - assert(port.baddr); - return MMIO32(port.baddr + PLIC_IPRIORITY_OFFSET(irq_id)); + assert(port->baddr); + return MMIO32(port->baddr + PLIC_IPRIORITY_OFFSET(irq_id)); } static status_t plic_set_priority(uint32_t irq_id, uint32_t priority) { - assert(port.baddr); - MMIO32(port.baddr + PLIC_IPRIORITY_OFFSET(irq_id)) = priority; + assert(port->baddr); + MMIO32(port->baddr + PLIC_IPRIORITY_OFFSET(irq_id)) = priority; arch_dmb(); return success; } static uint32_t plic_get_threshold(uint32_t core_id) { - assert(port.baddr); - return MMIO32(port.baddr + PLIC_ITHRESHOLD_OFFSET(core_id)); + assert(port->baddr); + return MMIO32(port->baddr + PLIC_ITHRESHOLD_OFFSET(core_id)); } static status_t plic_set_threshold(uint32_t core_id, uint32_t threshold) { - assert(port.baddr); - MMIO32(port.baddr + PLIC_ITHRESHOLD_OFFSET(core_id)) = threshold; + assert(port->baddr); + MMIO32(port->baddr + PLIC_ITHRESHOLD_OFFSET(core_id)) = threshold; arch_dmb(); return success; } @@ -80,9 +84,9 @@ static uint32_t plic_get_interrupt() { unsigned int irq; uint32_t core_id; - assert(port.baddr); + assert(port->baddr); core_id = arch_core_index(); - irq = MMIO32(port.baddr + PLIC_ICLAIM_OFFSET(core_id)); + irq = MMIO32(port->baddr + PLIC_ICLAIM_OFFSET(core_id)); arch_dmb(); return irq; } @@ -91,10 +95,10 @@ static uint32_t plic_get_interrupt() static void plic_clr_interrupt(uint32_t irq_id) { uint32_t core_id; - assert(port.baddr); + assert(port->baddr); core_id = arch_core_index(); sysdbg3("Clearing IRQ#%u on Core-%u\n", irq_id, core_id); - MMIO32(port.baddr + PLIC_ICLAIM_OFFSET(core_id)) = irq_id; + MMIO32(port->baddr + PLIC_ICLAIM_OFFSET(core_id)) = irq_id; arch_dmb(); return; } @@ -102,11 +106,11 @@ static void plic_clr_interrupt(uint32_t irq_id) static status_t plic_int_en(uint32_t irq_id) { uint32_t core_id, irq_shift; - assert(port.baddr); + assert(port->baddr); core_id = arch_core_index(); sysdbg3("Enabling IRQ#%u on Core-%u\n", irq_id, core_id); irq_shift = irq_id % 32; - MMIO32(port.baddr + PLIC_IENABLE_OFFSET(core_id, irq_id)) |= (1 << irq_shift); + MMIO32(port->baddr + PLIC_IENABLE_OFFSET(core_id, irq_id)) |= (1 << irq_shift); arch_dmb(); return success; } @@ -114,11 +118,11 @@ static status_t plic_int_en(uint32_t irq_id) static status_t plic_int_dis(uint32_t irq_id) { uint32_t core_id, irq_shift; - assert(port.baddr); + assert(port->baddr); core_id = arch_core_index(); sysdbg3("Disabling IRQ#%u on Core-%u\n", irq_id, core_id); irq_shift = irq_id % 32; - MMIO32(port.baddr + PLIC_IENABLE_OFFSET(core_id, irq_id)) &= ~(1 << irq_shift); + MMIO32(port->baddr + PLIC_IENABLE_OFFSET(core_id, irq_id)) &= ~(1 << irq_shift); arch_dmb(); return success; } @@ -134,8 +138,8 @@ static void plic_irqhandler() static bool plic_get_pending(uint32_t irq_id) { uint32_t pending; - assert(port.baddr); - pending = MMIO32(port.baddr + PLIC_IPENDING_OFFSET(irq_id)); + assert(port->baddr); + pending = MMIO32(port->baddr + PLIC_IPENDING_OFFSET(irq_id)); return (pending & (1 << (irq_id % 32))) ? true : false; } @@ -178,9 +182,9 @@ static status_t plic_driver_setup_pcpu() { status_t ret; unsigned int irq; - sysdbg3("Linking local IRQ#%u on Core-%u\n", port.irq->id, arch_core_index()); + sysdbg3("Linking local IRQ#%u on Core-%u\n", port->irq->id, arch_core_index()); plic_set_threshold(arch_core_index(), 0); - ret = link_interrupt(port.irq->module, port.irq->id, &plic_irqhandler); + ret = link_interrupt(port->irq->module, port->irq->id, &plic_irqhandler); do { irq = plic_get_interrupt(); @@ -193,13 +197,14 @@ static status_t plic_driver_setup_pcpu() static status_t plic_driver_exit() { + free(port); return ic_release_device(); } static status_t plic_driver_exit_pcpu() { arch_di_mei(); - return unlink_interrupt(port.irq->module, port.irq->id); + return unlink_interrupt(port->irq->module, port->irq->id); } INCLUDE_DRIVER(riscv_plic, plic_driver_setup, plic_driver_exit, 0, 0, 0); diff --git a/src/driver/sysclk/sysclk_prci/sysclk_prci.c b/src/driver/sysclk/sysclk_prci/sysclk_prci.c index b570d34e..151ac2f6 100644 --- a/src/driver/sysclk/sysclk_prci/sysclk_prci.c +++ b/src/driver/sysclk/sysclk_prci/sysclk_prci.c @@ -11,6 +11,7 @@ #include #include +#include #include #include #include @@ -23,7 +24,7 @@ #include #include -static sysclk_port_t sysclk; +static sysclk_port_t *sysclk; static clock_type_t clk_type; static lock_t sysclk_key; @@ -31,9 +32,14 @@ static status_t sysclk_setup() { mret_t mres; module_t *dp; - sysclk_port_t *port = &sysclk; + sysclk_port_t *port; istate_t ist; + sysclk = (sysclk_port_t *)malloc(sizeof(sysclk_port_t)); + if(!sysclk) + return error_memory_low; + port = sysclk; + arch_machine_call(fetch_dp, clock, 0, 0, &mres); if(mres.status != success) @@ -70,7 +76,7 @@ static status_t sysclk_disable() { status_t ret; istate_t ist; - sysclk_port_t *port = &sysclk; + sysclk_port_t *port = sysclk; assert(port->baddr && port->base_clk); @@ -82,6 +88,7 @@ static status_t sysclk_disable() prci_pll_deselect_pll(port); prci_pll_inline(port); memset(port, 0, sizeof(sysclk_port_t)); + free(sysclk); arch_ei_restore_state(&ist); lock_release(&sysclk_key); @@ -93,7 +100,7 @@ INCLUDE_DRIVER(sysclk_prci, sysclk_setup, sysclk_disable, 0, 1, 1); status_t sysclk_reset() { status_t ret; - sysclk_port_t *port = &sysclk; + sysclk_port_t *port = sysclk; istate_t ist; assert(arch_core_index() == BOOT_CORE_ID); @@ -118,7 +125,7 @@ status_t sysclk_reset() static inline void sysclk_set_internal(unsigned int clk _UNUSED) { status_t ret; - sysclk_port_t *port = &sysclk; + sysclk_port_t *port = sysclk; ret = prci_hfxocs_enable(port); prci_pll_bypass(port); ret |= prci_hfosc_enable(port); @@ -135,7 +142,7 @@ static inline void sysclk_set_internal(unsigned int clk _UNUSED) static inline void sysclk_set_external(void) { status_t ret; - sysclk_port_t *port = &sysclk; + sysclk_port_t *port = sysclk; ret = prci_hfosc_enable(port); ret |= prci_hfxocs_enable(port); prci_pll_select_xosc(port); @@ -150,7 +157,7 @@ static inline void sysclk_set_external(void) static inline void sysclk_set_pll(unsigned int clk) { status_t ret; - sysclk_port_t *port = &sysclk; + sysclk_port_t *port = sysclk; ret = prci_hfosc_enable(port); ret |= prci_hfxocs_enable(port); prci_pll_bypass(port); @@ -166,7 +173,7 @@ static inline void sysclk_set_pll(unsigned int clk) static void sysclk_configure_clk(call_arg_t a0, call_arg_t a1, call_arg_t a2 _UNUSED, mret_t *ret) { - sysclk_port_t *port = &sysclk; + sysclk_port_t *port = sysclk; istate_t ist; clock_type_t type = (clock_type_t) a0; unsigned int clk = (unsigned int) a1; @@ -212,7 +219,7 @@ INCLUDE_MCALL(sysclk_set, config_clk, sysclk_configure_clk); static void sysclk_get_freq(call_arg_t a0 _UNUSED, call_arg_t a1 _UNUSED, call_arg_t a2 _UNUSED, mret_t *ret) { - sysclk_port_t *port = &sysclk; + sysclk_port_t *port = sysclk; istate_t ist; unsigned int getclk; diff --git a/src/platform/mega_avr/atmega2560/include/plat_mem.h b/src/platform/mega_avr/atmega2560/include/plat_mem.h index 9f04c458..b0037ce8 100644 --- a/src/platform/mega_avr/atmega2560/include/plat_mem.h +++ b/src/platform/mega_avr/atmega2560/include/plat_mem.h @@ -25,4 +25,4 @@ #define STACK_SIZE 1024 -#define HEAP_SIZE 128 +#define HEAP_SIZE 1024 diff --git a/src/platform/mega_avr/atmega328p/include/plat_mem.h b/src/platform/mega_avr/atmega328p/include/plat_mem.h index 250d7358..c70b52a5 100644 --- a/src/platform/mega_avr/atmega328p/include/plat_mem.h +++ b/src/platform/mega_avr/atmega328p/include/plat_mem.h @@ -25,5 +25,5 @@ #define STACK_SIZE 0x0200 -#define HEAP_SIZE 128 +#define HEAP_SIZE 256 #define HEAP_ALIGN 1 diff --git a/src/platform/mega_avr/common/platform/platform_timer.c b/src/platform/mega_avr/common/platform/platform_timer.c index e3264ec4..df52135d 100644 --- a/src/platform/mega_avr/common/platform/platform_timer.c +++ b/src/platform/mega_avr/common/platform/platform_timer.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -30,7 +31,7 @@ static void (*tmr_cb)(void); /** * tport - Timer HAL device instance */ -static timer_port_t tport; +static timer_port_t *tport; /** * ticks - Driver variable for keeping track of timer ticks @@ -120,8 +121,8 @@ static void plat_timer_set_period(unsigned int p) { uint64_t nt = plat_get_timer_ticks_msec(tm->clk); ticks = (nt * p) - 1; - tport.value = (size_t) ticks; - timer_setup(&tport, 2, PS); + tport->value = (size_t) ticks; + timer_setup(tport, 2, PS); } /** @@ -144,7 +145,7 @@ static uint64_t plat_read_time(void) { uint64_t stamp = cntr; stamp <<= 10U; - stamp /= ((tport.value + 1) << 1); + stamp /= ((tport->value + 1) << 1); return stamp; } @@ -168,14 +169,18 @@ static status_t plat_timer_setup() status_t ret = success; ret |= plat_get_timer_prop(); - tport.port_id = tm->id; - tport.clk_id = tm->clk_id; - tport.baddr = tm->baddr; - tport.stride = tm->stride; - tport.tmr_irq = (size_t) tm->interrupt[0].id; - tport.tmr_handler = plat_tmr_isr; + tport = (timer_port_t *)malloc(sizeof(timer_port_t)); + if(!tport) + return error_memory_low; - ret |= timer_setup(&tport, 2, PS); + tport->port_id = tm->id; + tport->clk_id = tm->clk_id; + tport->baddr = tm->baddr; + tport->stride = tm->stride; + tport->tmr_irq = (size_t) tm->interrupt[0].id; + tport->tmr_handler = plat_tmr_isr; + + ret |= timer_setup(tport, 2, PS); plat_timer_set_period(1); ret |= timer_attach_device(ret, &plat_timer_port); @@ -188,11 +193,12 @@ static status_t plat_timer_setup() */ static status_t plat_timer_exit(void) { - status_t ret = timer_shutdown(&tport); + status_t ret = timer_shutdown(tport); ticks = 0; tmr_cb = (void *) 0; - memset(&tport, 0, sizeof(timer_port_t)); + memset(tport, 0, sizeof(timer_port_t)); ret |= timer_release_device(); + free(tport); return ret; } diff --git a/src/platform/mega_avr/common/platform/platform_wdt.c b/src/platform/mega_avr/common/platform/platform_wdt.c index 9074f4ec..288b66c4 100644 --- a/src/platform/mega_avr/common/platform/platform_wdt.c +++ b/src/platform/mega_avr/common/platform/platform_wdt.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -24,7 +25,7 @@ /** * plat_wdt - platform driver instance for wdt */ -static wdt_port_t plat_wdt; +static wdt_port_t *plat_wdt; /** * *callback_on_bark - Callback function pointer which needs @@ -54,17 +55,20 @@ static status_t platform_wdt_setup() return mres.status; } dp = (module_t *) mres.p; - plat_wdt.port_id = dp->id; - plat_wdt.clk_id = dp->clk_id; - plat_wdt.baddr = dp->baddr; - plat_wdt.stride = dp->stride; - plat_wdt.timeout = (size_t) dp->clk; - plat_wdt.wdt_irq = (unsigned int) dp->interrupt[0].id; - plat_wdt.wdt_handler = &platform_wdt_handler; - - sysdbg2("WDT engine @ %p\n", plat_wdt.baddr); - - return wdt_setup(&plat_wdt); + plat_wdt = (wdt_port_t *)malloc(sizeof(wdt_port_t)); + if(!plat_wdt) + return error_memory_low; + plat_wdt->port_id = dp->id; + plat_wdt->clk_id = dp->clk_id; + plat_wdt->baddr = dp->baddr; + plat_wdt->stride = dp->stride; + plat_wdt->timeout = (size_t) dp->clk; + plat_wdt->wdt_irq = (unsigned int) dp->interrupt[0].id; + plat_wdt->wdt_handler = &platform_wdt_handler; + + sysdbg2("WDT engine @ %p\n", plat_wdt->baddr); + + return wdt_setup(plat_wdt); } /** @@ -84,11 +88,11 @@ static status_t platform_wdt_guard(size_t timeout, bool bite, void *cb_bark) { status_t ret; sysdbg3("Configuring WDT timeout to gear %d\n", timeout); - plat_wdt.timeout = timeout; + plat_wdt->timeout = timeout; callback_on_bark = cb_bark; - ret = wdt_set_timeout(&plat_wdt); + ret = wdt_set_timeout(plat_wdt); sysdbg3("WDT Bite %s\n", bite ? "enabled" : "disabled"); - ret |= bite ? wdt_sre(&plat_wdt) : wdt_srd(&plat_wdt); + ret |= bite ? wdt_sre(plat_wdt) : wdt_srd(plat_wdt); return ret; } @@ -102,11 +106,11 @@ static status_t platform_wdt_guard(size_t timeout, bool bite, void *cb_bark) static status_t platform_wdt_hush() { status_t ret; - wdt_hush(&plat_wdt); + wdt_hush(plat_wdt); sysdbg3("WDT Hush!\n"); - plat_wdt.timeout = 0; - ret = wdt_set_timeout(&plat_wdt); - ret |= wdt_srd(&plat_wdt); + plat_wdt->timeout = 0; + ret = wdt_set_timeout(plat_wdt); + ret |= wdt_srd(plat_wdt); return ret; } @@ -123,11 +127,7 @@ void platform_wdt_handler() /** * @brief This struct links top level and low level driver apis */ -static wdog_t plat_wdt_driver = -{ - .guard = &platform_wdt_guard, - .hush = &platform_wdt_hush -}; +static wdog_t *plat_wdt_driver; /** * plat_wdt_driver_setup @@ -138,8 +138,13 @@ static status_t plat_wdt_driver_setup() { status_t ret; sysdbg3("In %s\n", __func__); + plat_wdt_driver = (wdog_t *)malloc(sizeof(wdog_t)); + if(!plat_wdt_driver) + return error_memory_low; + plat_wdt_driver->guard = &platform_wdt_guard; + plat_wdt_driver->hush = &platform_wdt_hush; ret = platform_wdt_setup(); - ret |= wdog_attach_device(ret, &plat_wdt_driver); + ret |= wdog_attach_device(ret, plat_wdt_driver); return ret; } @@ -152,7 +157,9 @@ static status_t plat_wdt_driver_exit() { status_t ret; ret = wdog_release_device(); - ret |= wdt_shutdown(&plat_wdt); + ret |= wdt_shutdown(plat_wdt); + free(plat_wdt_driver); + free(plat_wdt); return ret; } diff --git a/src/platform/mega_avr/common/sections.ld.sx b/src/platform/mega_avr/common/sections.ld.sx index d05543db..e2e9d5a0 100644 --- a/src/platform/mega_avr/common/sections.ld.sx +++ b/src/platform/mega_avr/common/sections.ld.sx @@ -54,7 +54,7 @@ SECTIONS .heap : { - . = ALIGN(8); + . = ALIGN(HEAP_ALIGN); *(.heap) KEEP(*(.heap)) . = . + HEAP_SIZE; diff --git a/src/platform/sifive/common_fe310/platform/plat_timer.c b/src/platform/sifive/common_fe310/platform/plat_timer.c index 7d0b4f4d..20afc101 100644 --- a/src/platform/sifive/common_fe310/platform/plat_timer.c +++ b/src/platform/sifive/common_fe310/platform/plat_timer.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -140,13 +141,7 @@ static uint64_t plat_read_time(void) /** * Driver ops for linking timer */ -static tvisor_timer_t plat_timer_port = -{ - .read_ticks = &clint_read_time, - .read_time = &plat_read_time, - .set_period = &plat_timer_set_period, - .reg_cb = &plat_timer_reg_cb, -}; +static tvisor_timer_t *plat_timer_port; /** * plat_timer_setup - Timer driver setup function @@ -158,6 +153,14 @@ static status_t plat_timer_setup(void) irqs_t *irq; arch_di_mtime(); + plat_timer_port = (tvisor_timer_t *)malloc(sizeof(tvisor_timer_t)); + if(!plat_timer_port) + return error_memory_low; + plat_timer_port->read_ticks = &clint_read_time; + plat_timer_port->read_time = &plat_read_time; + plat_timer_port->set_period = &plat_timer_set_period; + plat_timer_port->reg_cb = &plat_timer_reg_cb; + /* This funcition fetches device properties */ ret |= plat_get_timer_prop(); @@ -165,7 +168,7 @@ static status_t plat_timer_setup(void) /* Link timer isr handle */ ret |= link_interrupt(irq->module, irq->id, &plat_tmr_isr); - ret |= timer_attach_device(ret, &plat_timer_port); + ret |= timer_attach_device(ret, plat_timer_port); plat_timer_set_period(1); return ret; } @@ -179,6 +182,7 @@ static status_t plat_timer_exit(void) arch_di_mtime(); tmr_cb = (void *) 0; ticks = 0; + free(plat_timer_port); return timer_release_device(); } diff --git a/src/platform/sifive/common_fe310/sections.ld.sx b/src/platform/sifive/common_fe310/sections.ld.sx index da4f11b2..3c2d28bd 100644 --- a/src/platform/sifive/common_fe310/sections.ld.sx +++ b/src/platform/sifive/common_fe310/sections.ld.sx @@ -115,7 +115,7 @@ SECTIONS . = . + STACK_SIZE; } > vma_dmem - .heap : ALIGN(8) + .heap : ALIGN(HEAP_ALIGN) { *(.heap) KEEP(*(.heap)) diff --git a/src/platform/sifive/fe310g002-bl/include/plat_mem.h b/src/platform/sifive/fe310g002-bl/include/plat_mem.h index 9e500c07..b993fa3f 100644 --- a/src/platform/sifive/fe310g002-bl/include/plat_mem.h +++ b/src/platform/sifive/fe310g002-bl/include/plat_mem.h @@ -30,3 +30,4 @@ #define STACK_SIZE_PCPU 0xc00 #define HEAP_SIZE 512 +#define HEAP_ALIGN 4 diff --git a/src/platform/sifive/fe310g002/include/plat_mem.h b/src/platform/sifive/fe310g002/include/plat_mem.h index 34f408c6..a9a779d9 100644 --- a/src/platform/sifive/fe310g002/include/plat_mem.h +++ b/src/platform/sifive/fe310g002/include/plat_mem.h @@ -29,5 +29,5 @@ #define STACK_SIZE 0xc00 #define STACK_SIZE_PCPU 0xc00 -#define HEAP_SIZE 512 +#define HEAP_SIZE 4K #define HEAP_ALIGN 4 From 44545b774efb9499be8abd09249e93af63c8a821 Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Sun, 14 Aug 2022 00:08:19 +0530 Subject: [PATCH 04/21] Add ability to completely disable logs - This patch lets developer to completely disable logs and remove printf from compiled code without actually removing printf. - This optimises the build and generates bin for specific use case. Issue: #149 --- src/engine/build.mk | 9 +++++++++ src/lib/libc/include/stdio.h | 7 +++++-- src/lib/libc/printf.c | 4 ++-- src/lib/libsyslog/include/syslog.h | 10 +++++++++- src/lib/libsyslog/syslog.c | 6 +++++- 5 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/engine/build.mk b/src/engine/build.mk index 63bb3913..6121ebf2 100644 --- a/src/engine/build.mk +++ b/src/engine/build.mk @@ -13,5 +13,14 @@ DIR := $(GET_PATH) BOOTMSGS ?= 1 $(eval $(call add_define,BOOTMSGS)) +ifneq ($(filter $(BOOTMSGS) $(STDLOG_MEMBUF)\ + $(EARLYCON_SERIAL) $(CONSOLE_SERIAL),1),1) +NOLOGS := 1 +else +NOLOGS := 0 +endif + +$(eval $(call add_define,NOLOGS)) + include $(DIR)/banner.mk include mk/elf.mk diff --git a/src/lib/libc/include/stdio.h b/src/lib/libc/include/stdio.h index f1bfeb49..f26cb025 100644 --- a/src/lib/libc/include/stdio.h +++ b/src/lib/libc/include/stdio.h @@ -28,8 +28,8 @@ extern FILE stddev[]; #define stderr &stddev[2] #define stdlog &stddev[3] -int printf(const char *fmt, ...); -int eprintf(const char *fmt, ...); +int __printf(const char *fmt, ...); +int __eprintf(const char *fmt, ...); int fputs(const FILE *, const char *); int fputc(const FILE *, const char); int fgetc(const FILE *, char *); @@ -42,3 +42,6 @@ int fprintf(const FILE *, bool, const char *fmt, ...); int vprintf(const FILE *, bool, const char *fmt, va_list args); #endif #endif + +#define printf(fmt, ...) if(!NOLOGS) __printf(fmt, ##__VA_ARGS__) +#define eprintf(fmt, ...) if(!NOLOGS) __eprintf(fmt, ##__VA_ARGS__)) diff --git a/src/lib/libc/printf.c b/src/lib/libc/printf.c index 7a7c9708..65698219 100644 --- a/src/lib/libc/printf.c +++ b/src/lib/libc/printf.c @@ -191,7 +191,7 @@ int vprintf(const FILE *dev, bool en_stdout, const char *fmt, va_list args) return ret; } -int printf(const char *fmt, ...) +int __printf(const char *fmt, ...) { int ret; va_list va; @@ -201,7 +201,7 @@ int printf(const char *fmt, ...) return ret; } -int eprintf(const char *fmt, ...) +int __eprintf(const char *fmt, ...) { int ret; va_list va; diff --git a/src/lib/libsyslog/include/syslog.h b/src/lib/libsyslog/include/syslog.h index 72b8d19e..f6931a4f 100644 --- a/src/lib/libsyslog/include/syslog.h +++ b/src/lib/libsyslog/include/syslog.h @@ -20,11 +20,19 @@ typedef enum logtype dbug = 4 } logtype_t; -int syslog(logtype_t, const char *, ...); +int __syslog(logtype_t, const char *, ...); status_t syslog_print(); void syslog_stdout_enable(); void syslog_stdout_disable(); + +#if !NOLOGS +#define syslog(lt, fmt, ...) __syslog(lt, fmt, ##__VA_ARGS__) +#else +static void _UNUSED __dummylog(logtype_t lt _UNUSED, const char *a _UNUSED, ...){} +#define syslog(lt, fmt, ...) __dummylog(lt, fmt, ##__VA_ARGS__) +#endif + /* General Debug */ #define sysdbg(fmt, ...) if(DEBUG) syslog(dbug, fmt, ##__VA_ARGS__) /* Level 1 Debug: Application level logging */ diff --git a/src/lib/libsyslog/syslog.c b/src/lib/libsyslog/syslog.c index e04dbc03..b80c096f 100644 --- a/src/lib/libsyslog/syslog.c +++ b/src/lib/libsyslog/syslog.c @@ -23,7 +23,7 @@ static lock_t syslog_lock; static bool flag_enable_stdout; -int syslog(logtype_t t, const char *c, ...) +int __syslog(logtype_t t, const char *c, ...) { int ret; va_list va; @@ -45,6 +45,8 @@ status_t syslog_print() void syslog_stdout_enable() { + if(NOLOGS) + return; lock_acquire(&syslog_lock); flag_enable_stdout = true; lock_release(&syslog_lock); @@ -52,6 +54,8 @@ void syslog_stdout_enable() void syslog_stdout_disable() { + if(NOLOGS) + return; lock_acquire(&syslog_lock); flag_enable_stdout = false; lock_release(&syslog_lock); From b38cee6ca515d9fc5837a5f2c44502f37ad3faf5 Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Sun, 14 Aug 2022 00:16:55 +0530 Subject: [PATCH 05/21] Fix for return code of set clk func - sysclk_set_clk now returns correct code Issue: #150 --- src/driver/sysclk/sysclk_prci/sysclk_prci.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/driver/sysclk/sysclk_prci/sysclk_prci.c b/src/driver/sysclk/sysclk_prci/sysclk_prci.c index 151ac2f6..55110349 100644 --- a/src/driver/sysclk/sysclk_prci/sysclk_prci.c +++ b/src/driver/sysclk/sysclk_prci/sysclk_prci.c @@ -23,6 +23,7 @@ #include #include #include +#include static sysclk_port_t *sysclk; static clock_type_t clk_type; @@ -180,14 +181,12 @@ static void sysclk_configure_clk(call_arg_t a0, call_arg_t a1, call_arg_t a2 _UN assert(port->baddr && port->base_clk); - ret->status = error_func_inval_arg; ret->p = 0; ret->size = 0; - ret->status |= sysclk_execute_pre_config_clk_callback(); + ret->status = sysclk_execute_pre_config_clk_callback(); lock_acquire(&sysclk_key); arch_di_save_state(&ist); - switch(type) { case internal: @@ -204,6 +203,7 @@ static void sysclk_configure_clk(call_arg_t a0, call_arg_t a1, call_arg_t a2 _UN clk_type = pll; break; default: + ret->status = error_func_inval_arg; break; } From 81160181a59599a3a6c9f7ac30bb7ee27ed07f90 Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Sun, 14 Aug 2022 00:20:58 +0530 Subject: [PATCH 06/21] Updated platform config files - Updated device/software properties - Updated memory info to support heap - Minor change in atmega328p mem map Issue: #151 --- .../mega_avr/atmega2560/include/plat_mem.h | 7 ++--- .../mega_avr/atmega2560/resources/dp.c | 3 ++- .../mega_avr/atmega2560/resources/sp.c | 26 +++++++++++++------ .../mega_avr/atmega328p/include/plat_mem.h | 2 +- 4 files changed, 25 insertions(+), 13 deletions(-) diff --git a/src/platform/mega_avr/atmega2560/include/plat_mem.h b/src/platform/mega_avr/atmega2560/include/plat_mem.h index b0037ce8..8f58df5f 100644 --- a/src/platform/mega_avr/atmega2560/include/plat_mem.h +++ b/src/platform/mega_avr/atmega2560/include/plat_mem.h @@ -10,7 +10,7 @@ #pragma once -#define FLASH_SIZE 128K +#define FLASH_SIZE 256K #define RAM_SIZE 8K #define V_IMEM_START 0x00000000 @@ -23,6 +23,7 @@ #define ALIGN_BOUND 2 -#define STACK_SIZE 1024 +#define STACK_SIZE 2048 -#define HEAP_SIZE 1024 +#define HEAP_SIZE 2048 +#define HEAP_ALIGN 1 diff --git a/src/platform/mega_avr/atmega2560/resources/dp.c b/src/platform/mega_avr/atmega2560/resources/dp.c index 7900e791..3d672815 100644 --- a/src/platform/mega_avr/atmega2560/resources/dp.c +++ b/src/platform/mega_avr/atmega2560/resources/dp.c @@ -37,7 +37,7 @@ module_t uart0 = .baddr = 0xc0, .clk_id = 0x01, .stride = 0x06, - .clk = 19200, + .clk = 115200, .interrupt[0] = {int_arch, 25, int_level}, .interrupt[1] = {int_arch, 27, int_level}, }; @@ -136,6 +136,7 @@ module_t timer0 = .interrupt[0] = {int_arch, 21, int_level}, .interrupt[1] = {int_arch, 22, int_level}, .clk_id = 5, + .clk = 16000000, }; module_t timer1 = diff --git a/src/platform/mega_avr/atmega2560/resources/sp.c b/src/platform/mega_avr/atmega2560/resources/sp.c index 5d2fb277..2f5b6374 100644 --- a/src/platform/mega_avr/atmega2560/resources/sp.c +++ b/src/platform/mega_avr/atmega2560/resources/sp.c @@ -5,8 +5,8 @@ * File Name : platform_sp.c * Description : This file contains sources for platform * software properties - * Primary Author : Rahul Goyal [rhgoyal01@gmail.com] - * Organisation : Cyancore Contributor + * Primary Author : Akash Kollipara [akashkollipara@gmail.com] + * Organisation : Cyancore Core-Team */ #include @@ -18,20 +18,30 @@ swdev_t consoleUart = .hwdev_id = uart }; -sw_devid_t terra_devs[] = +swdev_t schedTimer = +{ + .swdev_id = sched_timer, + .hwdev_id = timer | 0x00, +}; + +static uint8_t led0pins[] = {7}; +static pinmux_t obled0 = addpins(1, led0pins, 0); +swdev_t onBoardLED0 = { - console_uart, + .swdev_id = onboard_led | 0, + .pmux = &obled0 }; -visor_t terravisor = +sw_devid_t terra_devs[] = { - .devids = terra_devs, - .n_dev = sizeof(terra_devs)/sizeof(sw_devid_t), + console_uart, sched_timer, (onboard_led | 0), }; +visor_t terravisor = add_visor_devs(terra_devs); + swdev_t *sw_devs[] = { - &consoleUart, + &consoleUart, &schedTimer, &onBoardLED0, }; sp_t software_prop = diff --git a/src/platform/mega_avr/atmega328p/include/plat_mem.h b/src/platform/mega_avr/atmega328p/include/plat_mem.h index c70b52a5..028cec0d 100644 --- a/src/platform/mega_avr/atmega328p/include/plat_mem.h +++ b/src/platform/mega_avr/atmega328p/include/plat_mem.h @@ -23,7 +23,7 @@ #define ALIGN_BOUND 2 -#define STACK_SIZE 0x0200 +#define STACK_SIZE 256 #define HEAP_SIZE 256 #define HEAP_ALIGN 1 From 9ea229730fbd0bc22989ee27233d466bfeef0349 Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Sun, 14 Aug 2022 00:22:29 +0530 Subject: [PATCH 07/21] Updated memory alignment Issue: #152 --- .../sifive/common_fe310/sections.ld.sx | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/platform/sifive/common_fe310/sections.ld.sx b/src/platform/sifive/common_fe310/sections.ld.sx index 3c2d28bd..ed4eaedc 100644 --- a/src/platform/sifive/common_fe310/sections.ld.sx +++ b/src/platform/sifive/common_fe310/sections.ld.sx @@ -59,18 +59,18 @@ SECTIONS *(.itim.*) } > vma_itmem AT > lma_mem - .bss : ALIGN(8) + .bss : ALIGN(4) { - . = ALIGN(8); + . = ALIGN(4); *(.bss) *(.bss.*) KEEP(*(.bss)) *(COMMON) } > vma_dmem - .static_bss_global : ALIGN(8) + .static_bss_global : ALIGN(4) { - . = ALIGN(8); + . = ALIGN(4); *(.sbss) *(.sbss.*) KEEP(*(.sbss)) @@ -80,32 +80,32 @@ SECTIONS { } > vma_dmem - .static_data_global : ALIGN(8) + .static_data_global : ALIGN(4) { - . = ALIGN(8); + . = ALIGN(4); *(.sdata) *(.sdata.*) KEEP(*(.sdata)) } > vma_dmem AT > lma_mem - .data : ALIGN(8) + .data : ALIGN(4) { - . = ALIGN(8); + . = ALIGN(4); *(.data) *(.data.*) KEEP(*(.data)) } > vma_dmem AT > lma_mem - .tdata : ALIGN(8) + .tdata : ALIGN(4) { - . = ALIGN(8); + . = ALIGN(4); *(.tdata) *(.tdata.*) KEEP(*(.tdata)) } > vma_dmem AT > lma_mem - .driver_table : ALIGN(8) {} > vma_dmem AT > lma_mem - .mcall_table : ALIGN(8) {} > vma_dmem AT > lma_mem + .driver_table : ALIGN(4) {} > vma_dmem AT > lma_mem + .mcall_table : ALIGN(4) {} > vma_dmem AT > lma_mem .stack : ALIGN(16) { From a575adc8ed683fe45edaaf450596a849d1b01200 Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Sun, 14 Aug 2022 00:25:00 +0530 Subject: [PATCH 08/21] Remove debug info with new rv toolchain --- src/sources.mk | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/sources.mk b/src/sources.mk index 93570111..f8b09135 100644 --- a/src/sources.mk +++ b/src/sources.mk @@ -34,6 +34,8 @@ CFLAGS += $(foreach i,$(INCLUDE),-I$(i)) \ ifneq ($(DEBUG),0) CFLAGS += -g +else +LD_FLAGS += -S endif $(eval $(call add_define,DEBUG)) @@ -45,7 +47,8 @@ ASFLAGS += $(foreach i,$(INCLUDE),-I$(i)) \ LD_FLAGS += --error-unresolved-symbols \ --warn-common --fatal-warnings \ --check-sections -nostdlib \ - --gc-sections -b $(OUTPUT_FORMAT) + --gc-sections -b $(OUTPUT_FORMAT) \ + --strip-discarded LINT_FLAGS += $(foreach i,$(INCLUDE),-I $(i)) \ $(filter -D%,$(CFLAGS)) From 5f88fae4691a21c5b877e01abb2a39f633d43321 Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Sun, 14 Aug 2022 00:43:33 +0530 Subject: [PATCH 09/21] Updated README --- src/README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/src/README.md b/src/README.md index 08c7bdde..9894a9f5 100644 --- a/src/README.md +++ b/src/README.md @@ -9,7 +9,6 @@ This is the root directory of all the sources of this framework. | [include](include) | Constitues of all the include/header files except for arch | | [lib](lib) | Constitues of all the necessary libraries | | [platform](platform) | Constitues of platform/development board specific sources | -| [projects](projects) | Constitues of project sources made using this framework | | [visor](visor) | Constitues of terra/hyper/super - visors' sources | --- From dfbeaefad0958e64b03111aa4499f1ce1c1bc86f Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Sun, 14 Aug 2022 13:59:47 +0530 Subject: [PATCH 10/21] Fixed missing clk failed init in timer - When clk in not provided in timer dp, timer should fail to start. With this patch it return correct status code - Code clean up - Fixed status code in status.h --- src/driver/sysclk/sysclk_prci/sysclk_prci.c | 2 -- src/include/status.h | 2 +- src/platform/mega_avr/common/platform/platform_timer.c | 2 ++ 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/driver/sysclk/sysclk_prci/sysclk_prci.c b/src/driver/sysclk/sysclk_prci/sysclk_prci.c index 55110349..e52ba7dc 100644 --- a/src/driver/sysclk/sysclk_prci/sysclk_prci.c +++ b/src/driver/sysclk/sysclk_prci/sysclk_prci.c @@ -19,11 +19,9 @@ #include #include #include -#include #include #include #include -#include static sysclk_port_t *sysclk; static clock_type_t clk_type; diff --git a/src/include/status.h b/src/include/status.h index 634be50f..0a4dbe38 100644 --- a/src/include/status.h +++ b/src/include/status.h @@ -64,7 +64,7 @@ typedef enum status error_system = -0x0b00, error_system_irq_link_fail = -0x0b01, error_system_irq_unlink_fail = -0x0b02, - error_system_clk_caliberation = -0xb003, + error_system_clk_caliberation = -0x0b03, /* Network related error */ error_net = -0x0c00, error_net_con_timeout = -0x0c01, diff --git a/src/platform/mega_avr/common/platform/platform_timer.c b/src/platform/mega_avr/common/platform/platform_timer.c index df52135d..624fc63d 100644 --- a/src/platform/mega_avr/common/platform/platform_timer.c +++ b/src/platform/mega_avr/common/platform/platform_timer.c @@ -172,6 +172,8 @@ static status_t plat_timer_setup() tport = (timer_port_t *)malloc(sizeof(timer_port_t)); if(!tport) return error_memory_low; + if(!tm->clk) + return error_system_clk_caliberation; tport->port_id = tm->id; tport->clk_id = tm->clk_id; From 94c3b4bfe15ec715a843594dff2c73ae1e765824 Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Sun, 14 Aug 2022 15:05:36 +0530 Subject: [PATCH 11/21] Updated clks in dp --- src/platform/mega_avr/atmega2560/resources/dp.c | 7 ++++++- src/platform/mega_avr/atmega328p/resources/dp.c | 4 +++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/platform/mega_avr/atmega2560/resources/dp.c b/src/platform/mega_avr/atmega2560/resources/dp.c index 3d672815..8a133f8e 100644 --- a/src/platform/mega_avr/atmega2560/resources/dp.c +++ b/src/platform/mega_avr/atmega2560/resources/dp.c @@ -133,10 +133,10 @@ module_t timer0 = .id = timer | 0x00, .baddr = 0x44, .stride = 5, + .clk = FCLK, .interrupt[0] = {int_arch, 21, int_level}, .interrupt[1] = {int_arch, 22, int_level}, .clk_id = 5, - .clk = 16000000, }; module_t timer1 = @@ -144,6 +144,7 @@ module_t timer1 = .id = timer | 0x10, .baddr = 0x80, .stride = 12, + .clk = FCLK, .interrupt[0] = {int_arch, 17, int_level}, .interrupt[1] = {int_arch, 18, int_level}, .clk_id = 3, @@ -154,6 +155,7 @@ module_t timer2 = .id = timer | 0x20, .baddr = 0xb0, .stride = 5, + .clk = FCLK, .interrupt[0] = {int_arch, 13, int_level}, .interrupt[1] = {int_arch, 24, int_level}, .clk_id = 6, @@ -164,6 +166,7 @@ module_t timer3 = .id = timer | 0x30, .baddr = 0x90, .stride = 12, + .clk = FCLK, .interrupt[0] = {int_arch, 32, int_level}, .interrupt[1] = {int_arch, 33, int_level}, .clk_id = 11, @@ -174,6 +177,7 @@ module_t timer4 = .id = timer | 0x40, .baddr = 0xa0, .stride = 12, + .clk = FCLK, .interrupt[0] = {int_arch, 42, int_level}, .interrupt[1] = {int_arch, 43, int_level}, .clk_id = 12, @@ -184,6 +188,7 @@ module_t timer5 = .id = timer | 0x50, .baddr = 0x120, .stride = 12, + .clk = FCLK, .interrupt[0] = {int_arch, 47, int_level}, .interrupt[1] = {int_arch, 48, int_level}, .clk_id = 13, diff --git a/src/platform/mega_avr/atmega328p/resources/dp.c b/src/platform/mega_avr/atmega328p/resources/dp.c index 485dee9d..a47682ee 100644 --- a/src/platform/mega_avr/atmega328p/resources/dp.c +++ b/src/platform/mega_avr/atmega328p/resources/dp.c @@ -77,7 +77,7 @@ module_t timer0 = .id = timer | 0x00, .baddr = 0x44, .stride = 5, - .clk = 16000000, + .clk = FCLK, .interrupt[0] = {int_arch, 14, int_level}, .interrupt[1] = {int_arch, 15, int_level}, .clk_id = 5, @@ -88,6 +88,7 @@ module_t timer1 = .id = timer | 0x10, .baddr = 0x80, .stride = 12, + .clk = FCLK, .interrupt[0] = {int_arch, 11, int_level}, .interrupt[1] = {int_arch, 12, int_level}, .clk_id = 3, @@ -98,6 +99,7 @@ module_t timer2 = .id = timer | 0x20, .baddr = 0xb0, .stride = 5, + .clk = FCLK, .interrupt[0] = {int_arch, 7, int_level}, .interrupt[1] = {int_arch, 8, int_level}, .clk_id = 6, From 46f874f13c1c2c6c14f855572e0c4c89499e1eda Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Tue, 16 Aug 2022 01:26:50 +0530 Subject: [PATCH 12/21] Optimised data space usage with correct qualifiers - Updated DP/SP to use correct qualifiers and types - Updated libresource to use correct qualifiers - Updated ealycon driver, plic driver, plat_timer to use malloc Issue: #157 --- .../con_serial_sifive/earlycon_serial.c | 53 +++++++++++-------- src/driver/interrupt/plic/plic.c | 28 +++++----- src/lib/libresource/dp/dp_system.c | 2 +- src/lib/libresource/include/dp.h | 8 +-- src/lib/libresource/include/dp/dp_gpio.h | 6 +-- src/lib/libresource/include/dp/dp_module.h | 2 +- src/lib/libresource/include/dp/dp_system.h | 8 +-- src/lib/libresource/include/sp.h | 8 +-- src/lib/libresource/include/sp/sp_visor.h | 6 +-- .../sifive/common_fe310/platform/plat_timer.c | 2 +- .../sifive/fe310g002-bl/resources/dp.c | 4 +- .../sifive/fe310g002-bl/resources/sp.c | 10 ++-- src/platform/sifive/fe310g002/resources/dp.c | 4 +- src/platform/sifive/fe310g002/resources/sp.c | 10 ++-- 14 files changed, 82 insertions(+), 69 deletions(-) diff --git a/src/driver/console/con_serial_sifive/earlycon_serial.c b/src/driver/console/con_serial_sifive/earlycon_serial.c index 937d6db2..35b0b4b1 100644 --- a/src/driver/console/con_serial_sifive/earlycon_serial.c +++ b/src/driver/console/con_serial_sifive/earlycon_serial.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -23,26 +24,29 @@ #include #include -static uart_port_t earlycon_port; +static uart_port_t *earlycon_port; static status_t earlycon_serial_setup() { - uart_get_properties(&earlycon_port, console_uart); + earlycon_port = (uart_port_t *)malloc(sizeof(uart_port_t)); + if(!earlycon_port) + return error_memory_low; + uart_get_properties(earlycon_port, console_uart); - sysdbg2("UART engine @ %p\n", earlycon_port.baddr); - sysdbg2("UART baud @ %lubps\n", earlycon_port.baud); + sysdbg2("UART engine @ %p\n", earlycon_port->baddr); + sysdbg2("UART baud @ %lubps\n", earlycon_port->baud); /* * If memory mapping is applicable, * put it in mmu supported guide. */ - return uart_setup(&earlycon_port, tx, no_parity); + return uart_setup(earlycon_port, tx, no_parity); } static status_t earlycon_serial_write(const char c) { status_t ret; - ret = uart_tx(&earlycon_port, c); - uart_tx_wait_till_done(&earlycon_port); + ret = uart_tx(earlycon_port, c); + uart_tx_wait_till_done(earlycon_port); return ret; } @@ -53,27 +57,32 @@ static status_t earlycon_serial_pre_clk_config() static status_t earlycon_serial_post_clk_config() { - uart_update_baud(&earlycon_port); + uart_update_baud(earlycon_port); return success; } -static sysclk_config_clk_callback_t earlycon_handle = -{ - .pre_config = &earlycon_serial_pre_clk_config, - .post_config = &earlycon_serial_post_clk_config, -}; +static sysclk_config_clk_callback_t *earlycon_handle; -static console_t earlycon_serial_driver = -{ - .write = &earlycon_serial_write, -}; +static console_t *earlycon_serial_driver; status_t earlycon_serial_driver_setup() { status_t ret; + + earlycon_serial_driver = (console_t *)malloc(sizeof(console_t)); + if(!earlycon_serial_driver) + return error_memory_low; + earlycon_serial_driver->write = &earlycon_serial_write; + + earlycon_handle = (sysclk_config_clk_callback_t *)malloc(sizeof(sysclk_config_clk_callback_t)); + if(!earlycon_handle) + return error_memory_low; + earlycon_handle->pre_config = &earlycon_serial_pre_clk_config; + earlycon_handle->post_config = &earlycon_serial_post_clk_config; + ret = earlycon_serial_setup(); - ret |= sysclk_register_config_clk_callback(&earlycon_handle); - ret |= console_attach_device(ret, &earlycon_serial_driver); + ret |= sysclk_register_config_clk_callback(earlycon_handle); + ret |= console_attach_device(ret, earlycon_serial_driver); return ret; } @@ -81,8 +90,10 @@ status_t earlycon_serial_driver_exit() { status_t ret; ret = console_release_device(); - ret |= sysclk_deregister_config_clk_callback(&earlycon_handle); - ret |= uart_shutdown(&earlycon_port); + ret |= sysclk_deregister_config_clk_callback(earlycon_handle); + ret |= uart_shutdown(earlycon_port); + free(earlycon_serial_driver); + free(earlycon_handle); return ret; } diff --git a/src/driver/interrupt/plic/plic.c b/src/driver/interrupt/plic/plic.c index 49d6ff17..08b8264d 100644 --- a/src/driver/interrupt/plic/plic.c +++ b/src/driver/interrupt/plic/plic.c @@ -151,25 +151,26 @@ static void plic_register_irq_handler(uint32_t id, void (* handler)(void)) arch_dsb(); } -static ic_t plic_port = -{ - .get_priority = &plic_get_priority, - .set_priority = &plic_set_priority, - .get_affinity = &plic_get_threshold, - .set_affinity = &plic_set_threshold, - .get_irq = &plic_get_interrupt, - .en_irq = &plic_int_en, - .dis_irq = &plic_int_dis, - .pending = &plic_get_pending, - .register_handler = &plic_register_irq_handler, -}; +static ic_t *plic_port; static status_t plic_driver_setup() { status_t ret; arch_di_mei(); ret = plic_setup(); - ret |= ic_attach_device(ret, &plic_port); + plic_port = (ic_t *)malloc(sizeof(ic_t)); + if(!plic_port) + return error_memory_low; + plic_port->get_priority = &plic_get_priority; + plic_port-> set_priority = &plic_set_priority; + plic_port->get_affinity = &plic_get_threshold; + plic_port->set_affinity = &plic_set_threshold; + plic_port->get_irq = &plic_get_interrupt; + plic_port->en_irq = &plic_int_en; + plic_port->dis_irq = &plic_int_dis; + plic_port->pending = &plic_get_pending; + plic_port->register_handler = &plic_register_irq_handler; + ret |= ic_attach_device(ret, plic_port); for(uint32_t i = 1; i <= N_PLAT_IRQS; i++) { plic_int_dis(i); @@ -197,6 +198,7 @@ static status_t plic_driver_setup_pcpu() static status_t plic_driver_exit() { + free(plic_port); free(port); return ic_release_device(); } diff --git a/src/lib/libresource/dp/dp_system.c b/src/lib/libresource/dp/dp_system.c index 32035818..64061e3d 100644 --- a/src/lib/libresource/dp/dp_system.c +++ b/src/lib/libresource/dp/dp_system.c @@ -19,7 +19,7 @@ cpu_t *dp_get_cpu_info(uint8_t id) return dev_prop->core[id]; } -unsigned long *dp_get_base_clock() +const unsigned long *dp_get_base_clock() { return &dev_prop->base_clock; } diff --git a/src/lib/libresource/include/dp.h b/src/lib/libresource/include/dp.h index 607709ae..df5aa996 100644 --- a/src/lib/libresource/include/dp.h +++ b/src/lib/libresource/include/dp.h @@ -15,16 +15,16 @@ #include #include -typedef struct device_properties +typedef const struct device_properties { - cpu_t *core[N_CORES]; + cpu_t * const core[N_CORES]; unsigned long base_clock; memory_t *memory; - gpio_module_t **ports; + gpio_module_t * const *ports; size_t n_ports; - module_t **modules; + module_t * const *modules; size_t n_mods; } dp_t; diff --git a/src/lib/libresource/include/dp/dp_gpio.h b/src/lib/libresource/include/dp/dp_gpio.h index b3c81a59..0b749000 100644 --- a/src/lib/libresource/include/dp/dp_gpio.h +++ b/src/lib/libresource/include/dp/dp_gpio.h @@ -13,15 +13,15 @@ #include -typedef struct pinmux +typedef const struct pinmux { uint8_t port; uint8_t function; uint8_t npins; - uint8_t *pins; + const uint8_t *pins; } pinmux_t; -typedef struct gpio_module +typedef const struct gpio_module { hw_devid_t id; uintptr_t baddr; diff --git a/src/lib/libresource/include/dp/dp_module.h b/src/lib/libresource/include/dp/dp_module.h index bec5a498..d2866449 100644 --- a/src/lib/libresource/include/dp/dp_module.h +++ b/src/lib/libresource/include/dp/dp_module.h @@ -13,7 +13,7 @@ #include -typedef struct module +typedef const struct module { hw_devid_t id; uintptr_t baddr; diff --git a/src/lib/libresource/include/dp/dp_system.h b/src/lib/libresource/include/dp/dp_system.h index e96e229e..fee61f31 100644 --- a/src/lib/libresource/include/dp/dp_system.h +++ b/src/lib/libresource/include/dp/dp_system.h @@ -40,14 +40,14 @@ typedef enum int_module int_plat = 3 } int_module_t; -typedef struct irqs +typedef const struct irqs { uint32_t module : 4; uint32_t id : 24; uint32_t trigger: 4; } irqs_t; -typedef struct cpu +typedef const struct cpu { char name[10]; uint16_t id; @@ -56,7 +56,7 @@ typedef struct cpu } cpu_t; -typedef struct memory_info +typedef const struct memory_info { uintptr_t start; uintptr_t size; @@ -70,5 +70,5 @@ typedef enum clock_type } clock_type_t; cpu_t *dp_get_cpu_info(uint8_t); -unsigned long *dp_get_base_clock(); +const unsigned long *dp_get_base_clock(); memory_t *dp_get_memory_info(); diff --git a/src/lib/libresource/include/sp.h b/src/lib/libresource/include/sp.h index ade3ee6c..3c6decdd 100644 --- a/src/lib/libresource/include/sp.h +++ b/src/lib/libresource/include/sp.h @@ -17,7 +17,7 @@ * | \- ... * \- n_swdev <- n(swdev) */ -typedef struct sp +typedef const struct sp { visor_t *terravisor; @@ -30,16 +30,16 @@ typedef struct sp #endif #if NSEC_SUP >= 1 - visor_t **nsec_supervisor; + visor_t * const *nsec_supervisor; size_t n_nsec_supervisor; #endif #if SEC_SUP >= 1 - visor_t **sec_supervisor; + visor_t * const *sec_supervisor; size_t n_sec_supervisor; #endif - swdev_t **swdev; + swdev_t * const *swdev; size_t n_swdev; } sp_t; diff --git a/src/lib/libresource/include/sp/sp_visor.h b/src/lib/libresource/include/sp/sp_visor.h index bd86e5e7..6da07093 100644 --- a/src/lib/libresource/include/sp/sp_visor.h +++ b/src/lib/libresource/include/sp/sp_visor.h @@ -16,16 +16,16 @@ #include #include -typedef struct swdev +typedef const struct swdev { sw_devid_t swdev_id; hw_devid_t hwdev_id; pinmux_t *pmux; } swdev_t; -typedef struct visor +typedef const struct visor { - sw_devid_t *devids; + sw_devid_t const *devids; size_t n_dev; } visor_t; diff --git a/src/platform/sifive/common_fe310/platform/plat_timer.c b/src/platform/sifive/common_fe310/platform/plat_timer.c index 20afc101..62e72e9c 100644 --- a/src/platform/sifive/common_fe310/platform/plat_timer.c +++ b/src/platform/sifive/common_fe310/platform/plat_timer.c @@ -150,7 +150,7 @@ static tvisor_timer_t *plat_timer_port; static status_t plat_timer_setup(void) { status_t ret = success; - irqs_t *irq; + const irqs_t *irq; arch_di_mtime(); plat_timer_port = (tvisor_timer_t *)malloc(sizeof(tvisor_timer_t)); diff --git a/src/platform/sifive/fe310g002-bl/resources/dp.c b/src/platform/sifive/fe310g002-bl/resources/dp.c index 0e918148..5cb7c709 100644 --- a/src/platform/sifive/fe310g002-bl/resources/dp.c +++ b/src/platform/sifive/fe310g002-bl/resources/dp.c @@ -92,12 +92,12 @@ module_t timer_core0 = .interrupt[0] = {int_local, 7, int_level}, }; -gpio_module_t *port_list[] = +gpio_module_t * const port_list[] = { &port0, }; -module_t *mod_list[] = +module_t * const mod_list[] = { &plic0, &uart0, &prci0, &clint0, &aon0, &uart1, &timer_core0, diff --git a/src/platform/sifive/fe310g002-bl/resources/sp.c b/src/platform/sifive/fe310g002-bl/resources/sp.c index 23e2be42..4e668118 100644 --- a/src/platform/sifive/fe310g002-bl/resources/sp.c +++ b/src/platform/sifive/fe310g002-bl/resources/sp.c @@ -13,7 +13,7 @@ #include #include -static uint8_t uart0pins[] = {16, 17}; +static const uint8_t uart0pins[] = {16, 17}; static pinmux_t uart0 = addpins(0, uart0pins, serial); swdev_t consoleUart = { @@ -28,7 +28,7 @@ swdev_t schedTimer = .hwdev_id = timer | 0, }; -static uint8_t led0pins[] = {19, 21}; +static const uint8_t led0pins[] = {19, 21}; static pinmux_t obled0 = addpins(0, led0pins, 0); swdev_t onBoardLED0 = { @@ -36,7 +36,7 @@ swdev_t onBoardLED0 = .pmux = &obled0 }; -static uint8_t led1pins[] = {20}; +static const uint8_t led1pins[] = {20}; static pinmux_t obled1 = addpins(0, led1pins, 0); swdev_t onBoardLED1 = { @@ -44,7 +44,7 @@ swdev_t onBoardLED1 = .pmux = &obled1 }; -sw_devid_t terra_devs[] = +const sw_devid_t terra_devs[] = { console_uart, sched_timer, (onboard_led | 0), (onboard_led | 1), @@ -52,7 +52,7 @@ sw_devid_t terra_devs[] = visor_t terravisor = add_visor_devs(terra_devs); -swdev_t *sw_devs[] = +swdev_t * const sw_devs[] = { &consoleUart, &schedTimer, &onBoardLED0, &onBoardLED1, diff --git a/src/platform/sifive/fe310g002/resources/dp.c b/src/platform/sifive/fe310g002/resources/dp.c index 0e918148..5cb7c709 100644 --- a/src/platform/sifive/fe310g002/resources/dp.c +++ b/src/platform/sifive/fe310g002/resources/dp.c @@ -92,12 +92,12 @@ module_t timer_core0 = .interrupt[0] = {int_local, 7, int_level}, }; -gpio_module_t *port_list[] = +gpio_module_t * const port_list[] = { &port0, }; -module_t *mod_list[] = +module_t * const mod_list[] = { &plic0, &uart0, &prci0, &clint0, &aon0, &uart1, &timer_core0, diff --git a/src/platform/sifive/fe310g002/resources/sp.c b/src/platform/sifive/fe310g002/resources/sp.c index 23e2be42..4e668118 100644 --- a/src/platform/sifive/fe310g002/resources/sp.c +++ b/src/platform/sifive/fe310g002/resources/sp.c @@ -13,7 +13,7 @@ #include #include -static uint8_t uart0pins[] = {16, 17}; +static const uint8_t uart0pins[] = {16, 17}; static pinmux_t uart0 = addpins(0, uart0pins, serial); swdev_t consoleUart = { @@ -28,7 +28,7 @@ swdev_t schedTimer = .hwdev_id = timer | 0, }; -static uint8_t led0pins[] = {19, 21}; +static const uint8_t led0pins[] = {19, 21}; static pinmux_t obled0 = addpins(0, led0pins, 0); swdev_t onBoardLED0 = { @@ -36,7 +36,7 @@ swdev_t onBoardLED0 = .pmux = &obled0 }; -static uint8_t led1pins[] = {20}; +static const uint8_t led1pins[] = {20}; static pinmux_t obled1 = addpins(0, led1pins, 0); swdev_t onBoardLED1 = { @@ -44,7 +44,7 @@ swdev_t onBoardLED1 = .pmux = &obled1 }; -sw_devid_t terra_devs[] = +const sw_devid_t terra_devs[] = { console_uart, sched_timer, (onboard_led | 0), (onboard_led | 1), @@ -52,7 +52,7 @@ sw_devid_t terra_devs[] = visor_t terravisor = add_visor_devs(terra_devs); -swdev_t *sw_devs[] = +swdev_t * const sw_devs[] = { &consoleUart, &schedTimer, &onBoardLED0, &onBoardLED1, From 84c874beff2ab5e3c37390edb7bc775a69563fbf Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Tue, 16 Aug 2022 13:14:33 +0530 Subject: [PATCH 13/21] Path to optimize memory usage - Updated linker scipt to keep necessary sections - Updated heap call to show and dump heap --- src/lib/libc/include/stdlib.h | 13 +++++++- src/lib/libc/malloc_lite.c | 6 +++- .../sifive/common_fe310/hal/clint/clint.c | 30 +++++++++++-------- .../sifive/common_fe310/sections.ld.sx | 1 - 4 files changed, 34 insertions(+), 16 deletions(-) diff --git a/src/lib/libc/include/stdlib.h b/src/lib/libc/include/stdlib.h index 698838cc..dbe198a7 100644 --- a/src/lib/libc/include/stdlib.h +++ b/src/lib/libc/include/stdlib.h @@ -12,6 +12,7 @@ #pragma once #define _STDLIB_H_ #include +#include #include #define EXIT_FAILURE 1 @@ -25,5 +26,15 @@ void *malloc(size_t); void free(void *); void *calloc(size_t, size_t); void *realloc(void *, size_t); -void heap_dump(void); +void __heap_status(bool); size_t heap_usage(void); + +static inline void heap_status(void) +{ + __heap_status(false); +} + +static inline void heap_dump(void) +{ + __heap_status(true); +} diff --git a/src/lib/libc/malloc_lite.c b/src/lib/libc/malloc_lite.c index 6c8dffcf..44040ce9 100644 --- a/src/lib/libc/malloc_lite.c +++ b/src/lib/libc/malloc_lite.c @@ -181,7 +181,7 @@ size_t heap_usage(void) return usage; } -void heap_dump(void) +void __heap_status(bool dump) { size_t i; unsigned int cntr; @@ -191,6 +191,10 @@ void heap_dump(void) printf("Heap Dump: %p - %p\n", &_heap_start, &_heap_end); printf("Heap Used: %u/%u - %u%%\n", h_used, (unsigned int)&_heap_size, h_perc); + + if(!dump) + return; + for(i = (size_t)&_heap_start; i < (size_t)&_heap_end; i+=32) { printf("["); diff --git a/src/platform/sifive/common_fe310/hal/clint/clint.c b/src/platform/sifive/common_fe310/hal/clint/clint.c index ffaf2bdc..86beec2d 100644 --- a/src/platform/sifive/common_fe310/hal/clint/clint.c +++ b/src/platform/sifive/common_fe310/hal/clint/clint.c @@ -10,6 +10,7 @@ #include #include +#include #include #include #include @@ -25,7 +26,7 @@ #include "clint_private.h" -static clint_port_t port; +static clint_port_t *port; static status_t clint_setup() { @@ -36,46 +37,49 @@ static status_t clint_setup() if(mres.status != success) return mres.status; + + port = (clint_port_t *)malloc(sizeof(clint_port_t)); + if(!port) + return error_memory_low; + dp = (module_t *)mres.p; - port.baddr = dp->baddr; - port.stride = dp->stride; - port.port_id = clint; + port->baddr = dp->baddr; + port->stride = dp->stride; + port->port_id = clint; return success; } static status_t clint_exit() { - port.baddr = 0; - port.stride = 0; - port.port_id = 0; + free(port); return success; } status_t clint_send_softirq(size_t core_id) { - assert(port.baddr); + assert(port->baddr); if(core_id >= N_CORES) return error_func_inval_arg; - MMIO32(port.baddr + MSIP_OFFSET(core_id)) = 1; + MMIO32(port->baddr + MSIP_OFFSET(core_id)) = 1; arch_dsb(); return success; } status_t clint_config_tcmp(size_t core_id, uint64_t value) { - assert(port.baddr); + assert(port->baddr); if(core_id >= N_CORES) return error_func_inval_arg; - MMIO64(port.baddr + MTCMP_OFFSET(core_id)) = value; + MMIO64(port->baddr + MTCMP_OFFSET(core_id)) = value; arch_dsb(); return success; } uint64_t clint_read_time() { - assert(port.baddr); + assert(port->baddr); uint64_t time_stamp; - time_stamp = MMIO64(port.baddr + MTIME_OFFSET); + time_stamp = MMIO64(port->baddr + MTIME_OFFSET); return time_stamp; } diff --git a/src/platform/sifive/common_fe310/sections.ld.sx b/src/platform/sifive/common_fe310/sections.ld.sx index ed4eaedc..150f0b4c 100644 --- a/src/platform/sifive/common_fe310/sections.ld.sx +++ b/src/platform/sifive/common_fe310/sections.ld.sx @@ -35,7 +35,6 @@ SECTIONS KEEP(*(.text.entry)) *(.text) *(.text.*) - KEEP(*(.text)) . = ALIGN(4); } > vma_imem AT > lma_mem From abd7a9206f298d0eabb8c0c0b02ed20a091af6bd Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Wed, 17 Aug 2022 01:05:35 +0530 Subject: [PATCH 14/21] Updated visuals --- src/include/insignia.h | 2 +- .../sifive/common_fe310/platform/platform.c | 19 +++++++++---------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/include/insignia.h b/src/include/insignia.h index fbea2be7..cae313e9 100644 --- a/src/include/insignia.h +++ b/src/include/insignia.h @@ -19,7 +19,7 @@ static inline void cyancore_insignia() { extern char cyancore_ascii_insignia[]; extern ccver_t FWVersion; - printf("%s\nVersion %u.%u.%u\n", cyancore_ascii_insignia, + printf("%sVersion %u.%u.%u\n", cyancore_ascii_insignia, (uint8_t) FWVersion.v.arch, (uint16_t) FWVersion.v.major, (uint8_t) FWVersion.v.minor); } diff --git a/src/platform/sifive/common_fe310/platform/platform.c b/src/platform/sifive/common_fe310/platform/platform.c index a5e0b6d4..05177574 100644 --- a/src/platform/sifive/common_fe310/platform/platform.c +++ b/src/platform/sifive/common_fe310/platform/platform.c @@ -70,24 +70,23 @@ void platform_memory_layout() _itim_vstart, _itim_size, _itim_vend, _flash_size, _ram_size; - syslog(info, "Memory Info >\n"); - syslog(info, "Flash Size : %u\n", &_flash_size); - syslog(info, "RAM Size : %u\n", &_ram_size); syslog(info, "\n"); syslog(info, "Program Memory Layout >\n"); - syslog(info, "text Region\t: %08p - %08p \t: Size: %u\n", + syslog(info, "Flash Size\t: %u\n", &_flash_size); + syslog(info, "RAM Size\t: %u\n", &_ram_size); + syslog(info, "text Region\t: %010p - %010p : Size: %u\n", &_text_start, &_text_end, &_text_size); - syslog(info, "rodata Region\t: %08p - %08p \t: Size: %u\n", + syslog(info, "rodata Region\t: %010p - %010p : Size: %u\n", &_rodata_start, &_rodata_end, &_rodata_size); - syslog(info, "ITIM Region\t: %08p - %08p \t: Size: %u\n", + syslog(info, "ITIM Region\t: %010p - %010p : Size: %u\n", &_itim_vstart, &_itim_vend, &_itim_size); - syslog(info, "bss Region\t: %08p - %08p \t: Size: %u\n", + syslog(info, "bss Region\t: %010p - %010p : Size: %u\n", &_bss_start, &_bss_end, &_bss_size); - syslog(info, "data Region\t: %08p - %08p \t: Size: %u\n", + syslog(info, "data Region\t: %010p - %010p : Size: %u\n", &_data_vstart, &_data_vend, &_data_size); - syslog(info, "stack Region\t: %08p - %08p \t: Size: %u\n", + syslog(info, "stack Region\t: %010p - %010p : Size: %u\n", &_stack_end, &_stack_start, &_stack_size); - syslog(info, "heap Region\t: %08p - %08p \t: Size: %u\n", + syslog(info, "heap Region\t: %010p - %010p : Size: %u\n", &_heap_start, &_heap_end, &_heap_size); syslog(info, "\n"); } From f0ea9f4188a6c0cc29638e34c4e6d5924e00528d Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Wed, 17 Aug 2022 01:05:56 +0530 Subject: [PATCH 15/21] Improved error handling and update err codes - Updated system and platform drivers to handle errors better and prevent memory leaks - Updated status code for lists --- .../con_serial_mega_avr/console_serial.c | 33 ++++---- .../con_serial_mega_avr/earlycon_serial.c | 29 ++++--- .../con_serial_sifive/console_serial.c | 77 +++++++++++++------ .../con_serial_sifive/earlycon_serial.c | 57 +++++++++----- src/driver/interrupt/plic/plic.c | 49 ++++++++---- src/driver/onboardled/onboardled.c | 6 ++ src/driver/sysclk/sysclk.c | 19 ++++- src/driver/sysclk/sysclk_prci/sysclk_prci.c | 31 ++++++-- src/include/status.h | 4 + .../mega_avr/common/platform/platform_timer.c | 22 +++++- .../mega_avr/common/platform/platform_wdt.c | 13 +++- .../sifive/common_fe310/platform/plat_timer.c | 3 + 12 files changed, 251 insertions(+), 92 deletions(-) diff --git a/src/driver/console/con_serial_mega_avr/console_serial.c b/src/driver/console/con_serial_mega_avr/console_serial.c index b65badfe..5a839077 100644 --- a/src/driver/console/con_serial_mega_avr/console_serial.c +++ b/src/driver/console/con_serial_mega_avr/console_serial.c @@ -69,7 +69,7 @@ static status_t console_serial_setup() * If memory mapping is applicable, * put it in mmu supported guide. */ - return uart_setup(console_port, trx, no_parity); // + return uart_setup(console_port, trx, no_parity); } static int_wait_t con_write_wait; @@ -108,6 +108,17 @@ static status_t console_serial_read(char *c) static console_t *console_serial_driver; +status_t console_serial_driver_exit() +{ + status_t ret; + ret = console_release_device(); + ret |= uart_shutdown(console_port); + free(console_serial_driver); + free(console_port); + ret |= driver_setup("earlycon"); + return ret; +} + status_t console_serial_driver_setup() { status_t ret; @@ -118,18 +129,14 @@ status_t console_serial_driver_setup() console_serial_driver->write = &console_serial_write; console_serial_driver->read = &console_serial_read; ret = console_serial_setup(); - ret |= console_attach_device(ret, console_serial_driver); - return ret; -} - -status_t console_serial_driver_exit() -{ - status_t ret; - ret = console_release_device(); - ret |= uart_shutdown(console_port); - free(console_serial_driver); - free(console_port); - ret |= driver_setup("earlycon"); + if(ret) + goto cleanup_exit; + ret = console_attach_device(ret, console_serial_driver); + if(!ret) + goto exit; +cleanup_exit: + console_serial_driver_exit(); +exit: return ret; } diff --git a/src/driver/console/con_serial_mega_avr/earlycon_serial.c b/src/driver/console/con_serial_mega_avr/earlycon_serial.c index b31793f3..05d8a527 100644 --- a/src/driver/console/con_serial_mega_avr/earlycon_serial.c +++ b/src/driver/console/con_serial_mega_avr/earlycon_serial.c @@ -75,6 +75,16 @@ static status_t earlycon_serial_write(const char c) static console_t *earlycon_serial_driver; +status_t earlycon_serial_driver_exit() +{ + status_t ret; + ret = console_release_device(); + ret |= uart_shutdown(earlycon_port); + free(earlycon_port); + free(earlycon_serial_driver); + return ret; +} + status_t earlycon_serial_driver_setup() { status_t ret; @@ -83,17 +93,14 @@ status_t earlycon_serial_driver_setup() return error_memory_low; earlycon_serial_driver->write = &earlycon_serial_write; ret = earlycon_serial_setup(); - ret |= console_attach_device(ret, earlycon_serial_driver); - return ret; -} - -status_t earlycon_serial_driver_exit() -{ - status_t ret; - ret = console_release_device(); - ret |= uart_shutdown(earlycon_port); - free(earlycon_port); - free(earlycon_serial_driver); + if(ret) + goto cleanup_exit; + ret = console_attach_device(ret, earlycon_serial_driver); + if(!ret) + goto exit; +cleanup_exit: + earlycon_serial_driver_exit(); +exit: return ret; } diff --git a/src/driver/console/con_serial_sifive/console_serial.c b/src/driver/console/con_serial_sifive/console_serial.c index 83e995da..4bd9c194 100644 --- a/src/driver/console/con_serial_sifive/console_serial.c +++ b/src/driver/console/con_serial_sifive/console_serial.c @@ -30,10 +30,11 @@ static void console_serial_irq_handler(void); static status_t console_serial_setup(void) { - console_port = (uart_port_t *)malloc(sizeof(uart_port_t)); - if(!console_port) - return error_memory_low; - uart_get_properties(console_port, console_uart); + status_t ret; + + ret = uart_get_properties(console_port, console_uart); + if(ret) + goto exit; console_port->irq_handler = console_serial_irq_handler; @@ -44,7 +45,9 @@ static status_t console_serial_setup(void) * If memory mapping is applicable, * put it in mmu supported guide. */ - return uart_setup(console_port, trx, no_parity); + ret = uart_setup(console_port, trx, no_parity); +exit: + return ret; } static status_t console_serial_write(const char c) @@ -104,38 +107,68 @@ static status_t console_serial_post_clk_config(void) static sysclk_config_clk_callback_t *console_handle; +status_t console_serial_driver_exit(void) +{ + status_t ret; + ret = console_release_device(); + if(console_handle) + ret |= sysclk_deregister_config_clk_callback(console_handle); + if(console_port) + ret |= uart_shutdown(console_port); + free(console_port); + free(console_serial_driver); + free(console_handle); + ret |= driver_setup("earlycon"); + return ret; +} + status_t console_serial_driver_setup(void) { status_t ret; driver_exit("earlycon"); + + console_port = (uart_port_t *)malloc(sizeof(uart_port_t)); + if(!console_port) + { + ret = error_memory_low; + goto cleanup_1; + } + console_serial_driver = (console_t *)malloc(sizeof(console_t)); if(!console_serial_driver) - return error_memory_low; + { + ret = error_memory_low; + goto cleanup_1; + } + console_serial_driver->write = &console_serial_write; console_serial_driver->read = &console_serial_read; console_serial_driver->payload_size = (unsigned int *)&occ; console_handle = (sysclk_config_clk_callback_t*)malloc(sizeof(sysclk_config_clk_callback_t)); if(!console_handle) - return error_memory_low; + { + ret = error_memory_low; + goto cleanup_1; + } + console_handle->pre_config = &console_serial_pre_clk_config; console_handle->post_config = &console_serial_post_clk_config; - ret = console_serial_setup(); - ret |= sysclk_register_config_clk_callback(console_handle); - ret |= console_attach_device(ret, console_serial_driver); - return ret; -} -status_t console_serial_driver_exit(void) -{ - status_t ret; - ret = console_release_device(); - ret |= sysclk_deregister_config_clk_callback(console_handle); - ret |= uart_shutdown(console_port); - free(console_port); - free(console_serial_driver); - free(console_handle); - ret |= driver_setup("earlycon"); + ret = console_serial_setup(); + if(ret) + goto cleanup_1; + + ret = sysclk_register_config_clk_callback(console_handle); + if(ret && ret != error_list_node_exists) + goto cleanup_1; + ret = console_attach_device(ret, console_serial_driver); + if(!ret) + goto exit; + +cleanup_1: + console_serial_driver_exit(); +exit: return ret; } diff --git a/src/driver/console/con_serial_sifive/earlycon_serial.c b/src/driver/console/con_serial_sifive/earlycon_serial.c index 35b0b4b1..bb71d828 100644 --- a/src/driver/console/con_serial_sifive/earlycon_serial.c +++ b/src/driver/console/con_serial_sifive/earlycon_serial.c @@ -28,9 +28,6 @@ static uart_port_t *earlycon_port; static status_t earlycon_serial_setup() { - earlycon_port = (uart_port_t *)malloc(sizeof(uart_port_t)); - if(!earlycon_port) - return error_memory_low; uart_get_properties(earlycon_port, console_uart); sysdbg2("UART engine @ %p\n", earlycon_port->baddr); @@ -65,35 +62,59 @@ static sysclk_config_clk_callback_t *earlycon_handle; static console_t *earlycon_serial_driver; +status_t earlycon_serial_driver_exit() +{ + status_t ret; + ret = console_release_device(); + if(earlycon_handle) + ret |= sysclk_deregister_config_clk_callback(earlycon_handle); + if(earlycon_port) + ret |= uart_shutdown(earlycon_port); + free(earlycon_serial_driver); + free(earlycon_handle); + return ret; +} + status_t earlycon_serial_driver_setup() { status_t ret; + earlycon_port = (uart_port_t *)malloc(sizeof(uart_port_t)); + if(!earlycon_port) + { + ret = error_memory_low; + goto cleanup_1; + } + earlycon_serial_driver = (console_t *)malloc(sizeof(console_t)); if(!earlycon_serial_driver) - return error_memory_low; + { + ret = error_memory_low; + goto cleanup_1; + } earlycon_serial_driver->write = &earlycon_serial_write; earlycon_handle = (sysclk_config_clk_callback_t *)malloc(sizeof(sysclk_config_clk_callback_t)); if(!earlycon_handle) - return error_memory_low; + { + ret = error_memory_low; + goto cleanup_1; + } earlycon_handle->pre_config = &earlycon_serial_pre_clk_config; earlycon_handle->post_config = &earlycon_serial_post_clk_config; ret = earlycon_serial_setup(); - ret |= sysclk_register_config_clk_callback(earlycon_handle); - ret |= console_attach_device(ret, earlycon_serial_driver); - return ret; -} - -status_t earlycon_serial_driver_exit() -{ - status_t ret; - ret = console_release_device(); - ret |= sysclk_deregister_config_clk_callback(earlycon_handle); - ret |= uart_shutdown(earlycon_port); - free(earlycon_serial_driver); - free(earlycon_handle); + if(ret) + goto cleanup_1; + ret = sysclk_register_config_clk_callback(earlycon_handle); + if(ret && ret != error_list_node_exists) + goto cleanup_1; + ret = console_attach_device(ret, earlycon_serial_driver); + if(!ret) + goto exit; +cleanup_1: + earlycon_serial_driver_exit(); +exit: return ret; } diff --git a/src/driver/interrupt/plic/plic.c b/src/driver/interrupt/plic/plic.c index 08b8264d..96df39bc 100644 --- a/src/driver/interrupt/plic/plic.c +++ b/src/driver/interrupt/plic/plic.c @@ -153,14 +153,32 @@ static void plic_register_irq_handler(uint32_t id, void (* handler)(void)) static ic_t *plic_port; +static status_t plic_driver_exit() +{ + free(plic_port); + free(port); + return ic_release_device(); +} + +static status_t plic_driver_exit_pcpu() +{ + arch_di_mei(); + return unlink_interrupt(port->irq->module, port->irq->id); +} + static status_t plic_driver_setup() { status_t ret; arch_di_mei(); ret = plic_setup(); + if(ret) + goto cleanup_exit; plic_port = (ic_t *)malloc(sizeof(ic_t)); if(!plic_port) - return error_memory_low; + { + ret = error_memory_low; + goto cleanup_exit; + } plic_port->get_priority = &plic_get_priority; plic_port-> set_priority = &plic_set_priority; plic_port->get_affinity = &plic_get_threshold; @@ -170,12 +188,18 @@ static status_t plic_driver_setup() plic_port->dis_irq = &plic_int_dis; plic_port->pending = &plic_get_pending; plic_port->register_handler = &plic_register_irq_handler; - ret |= ic_attach_device(ret, plic_port); + ret = ic_attach_device(ret, plic_port); + if(ret) + goto cleanup_exit; for(uint32_t i = 1; i <= N_PLAT_IRQS; i++) { plic_int_dis(i); plic_set_priority(i, 1); } + goto exit; +cleanup_exit: + plic_driver_exit(); +exit: return ret; } @@ -183,9 +207,13 @@ static status_t plic_driver_setup_pcpu() { status_t ret; unsigned int irq; + if(!port) + return error_driver_init_failed; sysdbg3("Linking local IRQ#%u on Core-%u\n", port->irq->id, arch_core_index()); plic_set_threshold(arch_core_index(), 0); ret = link_interrupt(port->irq->module, port->irq->id, &plic_irqhandler); + if(ret) + goto cleanup_exit; do { irq = plic_get_interrupt(); @@ -193,21 +221,12 @@ static status_t plic_driver_setup_pcpu() plic_clr_interrupt(irq); } while(irq); arch_ei_mei(); + goto exit; +cleanup_exit: + plic_driver_exit_pcpu(); +exit: return ret; } -static status_t plic_driver_exit() -{ - free(plic_port); - free(port); - return ic_release_device(); -} - -static status_t plic_driver_exit_pcpu() -{ - arch_di_mei(); - return unlink_interrupt(port->irq->module, port->irq->id); -} - INCLUDE_DRIVER(riscv_plic, plic_driver_setup, plic_driver_exit, 0, 0, 0); INCLUDE_DRIVER(riscv_plic_pcpu, plic_driver_setup_pcpu, plic_driver_exit_pcpu, 1, 1, 1); diff --git a/src/driver/onboardled/onboardled.c b/src/driver/onboardled/onboardled.c index e0520a2d..0f619b3c 100644 --- a/src/driver/onboardled/onboardled.c +++ b/src/driver/onboardled/onboardled.c @@ -90,6 +90,12 @@ static status_t onboardled_setup(void) obledPort = (gpio_port_t *)malloc(sizeof(gpio_port_t) * obled_sp->pmux->npins); + if(!obledPort) + { + ret = error_memory_low; + goto exit; + } + for(uint8_t i = 0; i < obled_sp->pmux->npins; i++) { ret |= gpio_pin_alloc(&obledPort[i], obled_sp->pmux->port, diff --git a/src/driver/sysclk/sysclk.c b/src/driver/sysclk/sysclk.c index b5bf73f0..37a29a90 100644 --- a/src/driver/sysclk/sysclk.c +++ b/src/driver/sysclk/sysclk.c @@ -39,12 +39,21 @@ status_t sysclk_register_config_clk_callback(sysclk_config_clk_callback_t *node) { temp = root; while(temp->next) + { temp = temp->next; + if(temp == node) + { + sysdbg3("sysclk: Found duplicate node %p, failed to register!\n", temp); + ret = error_list_node_exists; + goto exit; + } + } sysdbg3("sysclk: Adding node to list %p\n", node); temp->next = node; } } +exit: lock_release(&sysclk_cb_key); return ret; } @@ -66,18 +75,24 @@ status_t sysclk_deregister_config_clk_callback(sysclk_config_clk_callback_t *nod { sysdbg3("sysclk: Removing root node %p\n", node); root = temp->next; - goto end; + goto exit; } while(temp != node) { prev = temp; temp = temp->next; + if(!temp) + { + sysdbg3("sysclk: Node %p not found!. Failed to deregister!", node); + ret = error_list_node_not_found; + goto exit; + } } sysdbg3("sysclk: Removing node from list %p\n", node); prev->next = temp->next; } -end: +exit: lock_release(&sysclk_cb_key); return ret; } diff --git a/src/driver/sysclk/sysclk_prci/sysclk_prci.c b/src/driver/sysclk/sysclk_prci/sysclk_prci.c index e52ba7dc..4d3ab45d 100644 --- a/src/driver/sysclk/sysclk_prci/sysclk_prci.c +++ b/src/driver/sysclk/sysclk_prci/sysclk_prci.c @@ -26,9 +26,11 @@ static sysclk_port_t *sysclk; static clock_type_t clk_type; static lock_t sysclk_key; +static status_t sysclk_disable(); static status_t sysclk_setup() { + status_t ret = success; mret_t mres; module_t *dp; sysclk_port_t *port; @@ -45,7 +47,8 @@ static status_t sysclk_setup() { sysdbg2("Clock not found in DP!\n"); port->base_clk = 0; - return mres.status; + ret = mres.status; + goto cleanup_exit; } port->base_clk = *((unsigned int *)mres.p); @@ -54,7 +57,8 @@ static status_t sysclk_setup() if(mres.status != success) { sysdbg2("PRCI not found in DP!\n"); - return mres.status; + ret = mres.status; + goto cleanup_exit; } lock_acquire(&sysclk_key); @@ -65,10 +69,13 @@ static status_t sysclk_setup() port->baddr = dp->baddr; port->stride = dp->stride; port->type = dp->clk_id; - + goto exit; +cleanup_exit: + free(sysclk); +exit: arch_ei_restore_state(&ist); lock_release(&sysclk_key); - return success; + return ret; } static status_t sysclk_disable() @@ -77,6 +84,9 @@ static status_t sysclk_disable() istate_t ist; sysclk_port_t *port = sysclk; + if(!port) + return error_driver_init_failed; + assert(port->baddr && port->base_clk); lock_acquire(&sysclk_key); @@ -101,8 +111,9 @@ status_t sysclk_reset() status_t ret; sysclk_port_t *port = sysclk; istate_t ist; + if(!port) + return error_driver_init_failed; - assert(arch_core_index() == BOOT_CORE_ID); assert(port->baddr && port->base_clk); lock_acquire(&sysclk_key); @@ -125,6 +136,8 @@ static inline void sysclk_set_internal(unsigned int clk _UNUSED) { status_t ret; sysclk_port_t *port = sysclk; + if(!port) + return; ret = prci_hfxocs_enable(port); prci_pll_bypass(port); ret |= prci_hfosc_enable(port); @@ -142,6 +155,8 @@ static inline void sysclk_set_external(void) { status_t ret; sysclk_port_t *port = sysclk; + if(!port) + return; ret = prci_hfosc_enable(port); ret |= prci_hfxocs_enable(port); prci_pll_select_xosc(port); @@ -157,6 +172,8 @@ static inline void sysclk_set_pll(unsigned int clk) { status_t ret; sysclk_port_t *port = sysclk; + if(!port) + return; ret = prci_hfosc_enable(port); ret |= prci_hfxocs_enable(port); prci_pll_bypass(port); @@ -176,6 +193,8 @@ static void sysclk_configure_clk(call_arg_t a0, call_arg_t a1, call_arg_t a2 _UN istate_t ist; clock_type_t type = (clock_type_t) a0; unsigned int clk = (unsigned int) a1; + if(!port) + return; assert(port->baddr && port->base_clk); @@ -220,6 +239,8 @@ static void sysclk_get_freq(call_arg_t a0 _UNUSED, call_arg_t a1 _UNUSED, sysclk_port_t *port = sysclk; istate_t ist; unsigned int getclk; + if(!port) + return; assert(port->baddr && port->base_clk); diff --git a/src/include/status.h b/src/include/status.h index 0a4dbe38..62e06982 100644 --- a/src/include/status.h +++ b/src/include/status.h @@ -79,4 +79,8 @@ typedef enum status /* Mesg related error */ error_mesg = -0x1000, error_mesg_long = -0x1001, +/* List related error */ + error_list = -0x1100, + error_list_node_exists = -0x1101, + error_list_node_not_found = -0x1102, } status_t; diff --git a/src/platform/mega_avr/common/platform/platform_timer.c b/src/platform/mega_avr/common/platform/platform_timer.c index 624fc63d..aa93e92c 100644 --- a/src/platform/mega_avr/common/platform/platform_timer.c +++ b/src/platform/mega_avr/common/platform/platform_timer.c @@ -160,6 +160,8 @@ static tvisor_timer_t plat_timer_port = .reg_cb = &plat_timer_reg_cb, }; +static status_t plat_timer_exit(void); + /** * plat_timer_setup - Timer driver setup function * To be exported to driver table. @@ -173,7 +175,10 @@ static status_t plat_timer_setup() if(!tport) return error_memory_low; if(!tm->clk) - return error_system_clk_caliberation; + { + ret = error_system_clk_caliberation; + goto cleanup_exit1; + } tport->port_id = tm->id; tport->clk_id = tm->clk_id; @@ -182,10 +187,19 @@ static status_t plat_timer_setup() tport->tmr_irq = (size_t) tm->interrupt[0].id; tport->tmr_handler = plat_tmr_isr; - ret |= timer_setup(tport, 2, PS); + ret = timer_setup(tport, 2, PS); + if(ret) + goto cleanup_exit2; plat_timer_set_period(1); - ret |= timer_attach_device(ret, &plat_timer_port); - + ret = timer_attach_device(ret, &plat_timer_port); + if(!ret) + goto exit; +cleanup_exit2: + plat_timer_exit(); +cleanup_exit1: + if(tport) + free(tport); +exit: return ret; } diff --git a/src/platform/mega_avr/common/platform/platform_wdt.c b/src/platform/mega_avr/common/platform/platform_wdt.c index 288b66c4..c2211611 100644 --- a/src/platform/mega_avr/common/platform/platform_wdt.c +++ b/src/platform/mega_avr/common/platform/platform_wdt.c @@ -128,6 +128,7 @@ void platform_wdt_handler() * @brief This struct links top level and low level driver apis */ static wdog_t *plat_wdt_driver; +static status_t plat_wdt_driver_exit(); /** * plat_wdt_driver_setup @@ -144,7 +145,14 @@ static status_t plat_wdt_driver_setup() plat_wdt_driver->guard = &platform_wdt_guard; plat_wdt_driver->hush = &platform_wdt_hush; ret = platform_wdt_setup(); - ret |= wdog_attach_device(ret, plat_wdt_driver); + if(ret) + goto cleanup_exit; + ret = wdog_attach_device(ret, plat_wdt_driver); + if(!ret) + goto exit; +cleanup_exit: + plat_wdt_driver_exit(); +exit: return ret; } @@ -157,7 +165,8 @@ static status_t plat_wdt_driver_exit() { status_t ret; ret = wdog_release_device(); - ret |= wdt_shutdown(plat_wdt); + if(plat_wdt) + ret |= wdt_shutdown(plat_wdt); free(plat_wdt_driver); free(plat_wdt); return ret; diff --git a/src/platform/sifive/common_fe310/platform/plat_timer.c b/src/platform/sifive/common_fe310/platform/plat_timer.c index 62e72e9c..12e380e7 100644 --- a/src/platform/sifive/common_fe310/platform/plat_timer.c +++ b/src/platform/sifive/common_fe310/platform/plat_timer.c @@ -179,9 +179,12 @@ static status_t plat_timer_setup(void) */ static status_t plat_timer_exit(void) { + const irqs_t *irq; arch_di_mtime(); tmr_cb = (void *) 0; ticks = 0; + irq = &tm->interrupt[0]; + unlink_interrupt(irq->module, irq->id); free(plat_timer_port); return timer_release_device(); } From 199ead7f1c8e01405bb0fdf09961421cdb570957 Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Thu, 18 Aug 2022 20:13:35 +0530 Subject: [PATCH 16/21] Feature updated to print size of sections --- mk/elf.mk | 6 ++---- mk/tc.mk | 1 - mk/tc_get.mk | 8 ++++++++ 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/mk/elf.mk b/mk/elf.mk index 974faac9..b607c62d 100644 --- a/mk/elf.mk +++ b/mk/elf.mk @@ -22,18 +22,16 @@ LD_SUPPLEMENT := $(addprefix $(OUT)/,$(LD_SUPPLEMENT:.ld.sx=.ld)) elf: $(ELF) -$(ELF): $(DEP_LIBS) $(DEP_OBJS) $(LD_SCRIPT) $(LD_SUPPLEMENT) +.SECONDEXPANSION: +$(ELF): $(SIZE) $(DEP_LIBS) $(DEP_OBJS) $(LD_SCRIPT) $(LD_SUPPLEMENT) | $$(SIZE) @echo "Elf: Generating $(@F) ..." $(LD) -dT $(LD_SCRIPT) $(addprefix -T , $(LD_SUPPLEMENT)) $(LD_FLAGS) -Map=$(@:.elf=.map) -o $@ $(filter %.o, $^) $(DEP_LIB_PATH) $(DEP_LIBS_ARG) -L $(TL) -lgcc $(OD) -Dx -h --wide $@ > $(@:.elf=.lst) $(OC) -O binary $@ $(@:.elf=.bin) $(OC) -O ihex $@ $(@:.elf=.hex) - @echo "==================================================" - @echo "Size of Executable:" @cd $(@D); $(SIZE) $(@F) @echo "" -.SECONDEXPANSION: $(OUT)/%.ld: %.ld.sx mkdir -p $(@D) @echo "Elf: Preprocessing $(@F) ..." diff --git a/mk/tc.mk b/mk/tc.mk index ace7b061..7abd3e42 100644 --- a/mk/tc.mk +++ b/mk/tc.mk @@ -39,7 +39,6 @@ OD := $(TC)-objdump OC := $(TC)-objcopy STRIP := $(TC)-strip A2L := $(TC)-addr2line -SIZE := $(TC)-size endif ifeq ($(realpath $(TI)),) diff --git a/mk/tc_get.mk b/mk/tc_get.mk index 285bc78b..2393c01d 100644 --- a/mk/tc_get.mk +++ b/mk/tc_get.mk @@ -14,6 +14,7 @@ T_ALLOWLIST += get_all_tc get_avr_tc get_arm_tc get_riscv_tc # GIT REPO RECOMMENDED # Provide git repo path for toolchains for better experience +ESIZE_REPO := https://github.com/VisorFolks/cc_elf_size.git AVR_TC_REPO ?= RISC_V_TC_REPO ?= ARM_TC_REPO ?= @@ -23,6 +24,13 @@ get_all_tc: --tc_clear get_avr_tc get_arm_tc get_riscv_tc --tc_clear: rm -rf $(TOOLS_ROOT) +SIZE := $(TOOLS_ROOT)/cc_elf_size/size +$(SIZE): + $(info < ! > Fetching ELF-Size utility ...) + cd $(TOOLS_ROOT); git clone $(ESIZE_REPO); + $(MAKE) -C $(@D) + echo "< / > Done !" + get_arm_tc: $(TOOLS_ROOT)/arm-toolchain/bin/arm-none-eabi-gcc $(TOOLS_ROOT)/arm-toolchain/bin/arm-none-eabi-gcc: ifeq ($(ARM_TC_REPO),) From d68977acd4903a081e2f8a5ee924fddb588827e5 Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Mon, 15 Aug 2022 18:47:24 +0530 Subject: [PATCH 17/21] Updated machine call structure - Moved machine call to header files to accomodate library creation for apps (future plans) Issue: #155 --- src/arch/avr/8/common_5x_6/terravisor/arch.c | 24 +----------- .../8/common_5x_6/terravisor/include/arch.h | 31 +++++++++++----- src/arch/riscv/32/i/terravisor/arch.c | 27 -------------- src/arch/riscv/32/i/terravisor/include/arch.h | 37 +++++++++++++++---- 4 files changed, 54 insertions(+), 65 deletions(-) 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 1530bf8c..f11ee6ae 100644 --- a/src/arch/avr/8/common_5x_6/terravisor/arch.c +++ b/src/arch/avr/8/common_5x_6/terravisor/arch.c @@ -12,11 +12,10 @@ #include #include #include -#include -#include #include #include #include +#include /** * arch_early_setup - This function is called in the early stages of boot @@ -24,6 +23,7 @@ * @brief This function is responsible to clean reset cpu status/control registers. * */ +void (* const p_mcall)(unsigned int, unsigned int, unsigned int, unsigned int, mret_t *) = &machine_call; void arch_early_setup() { arch_di(); @@ -72,26 +72,6 @@ void arch_wfi() asm volatile("sleep"); } -/** - * arch_machine_call - perform machine call - * - * @brief 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 value - */ -void arch_machine_call(unsigned int code, unsigned int a0, unsigned int a1, unsigned int a2, mret_t *ret) -{ - if(ret == NULL) - return; - machine_call(code, a0, a1, a2, ret); - return; -} - void _NORETURN arch_panic_handler_callback() { context_frame_t *frame; 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 56bb5275..8a6b9704 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 @@ -15,6 +15,7 @@ #include #include #include +#include /** * arch_early_setup - This needs to be called in early stages of boot @@ -38,15 +39,6 @@ void arch_ei_restore_state(istate_t *); */ void arch_panic_handler(); -#ifdef _MACHINE_CALL_H_ -/** - * arch_machine_call - Performs machine call - * - * Refer arch.c for more details. - */ -void arch_machine_call(call_arg_t, call_arg_t, call_arg_t, call_arg_t, mret_t *); -#endif - /** * arch_register_interrupt_handler - Registers interrtup handler * for arch specific interrupt vectors @@ -62,6 +54,27 @@ static inline unsigned int arch_core_index() return 0; } +/** + * arch_machine_call - perform machine call + * + * @brief 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 value + */ +static inline void arch_machine_call(unsigned int code, unsigned int a0, unsigned int a1, unsigned int a2, mret_t *ret) +{ + extern void (*const p_mcall)(unsigned int, unsigned int, unsigned int, unsigned int, mret_t*); + if(ret == NULL) + return; + p_mcall(code, a0, a1, a2, ret); + return; +} + /** * arch_ei - arch enable global interrupts */ diff --git a/src/arch/riscv/32/i/terravisor/arch.c b/src/arch/riscv/32/i/terravisor/arch.c index 0221f4f3..068e43cc 100644 --- a/src/arch/riscv/32/i/terravisor/arch.c +++ b/src/arch/riscv/32/i/terravisor/arch.c @@ -95,33 +95,6 @@ void arch_ei_restore_state(istate_t *istate) asm volatile("csrs mie, %0" : : "r" (*istate)); } -/** - * 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() { const context_frame_t *frame = get_context_frame(); diff --git a/src/arch/riscv/32/i/terravisor/include/arch.h b/src/arch/riscv/32/i/terravisor/include/arch.h index bc926aab..cae3b34c 100644 --- a/src/arch/riscv/32/i/terravisor/include/arch.h +++ b/src/arch/riscv/32/i/terravisor/include/arch.h @@ -38,13 +38,6 @@ void arch_early_signal_boot_start(); void arch_wait_till_boot_done(); void arch_signal_boot_done(); -/** - * 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 @@ -67,6 +60,36 @@ static inline unsigned int arch_core_index() return id; } +/** + * 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 + */ +static inline void arch_machine_call(unsigned int code, unsigned int arg0, unsigned int arg1, unsigned int arg2, mret_t *ret) +{ + if(ret == NULL) + return; + register uint32_t a0 asm("a0") = code; + register uint32_t a1 asm("a1") = arg0; + register uint32_t a2 asm("a2") = arg1; + register uint32_t a3 asm("a3") = arg2; + asm volatile("ecall" + : "+r" (a0), "+r" (a1), "+r"(a2) + : "r" (a0), "r" (a1), "r" (a2), "r" (a3) + : "memory"); + ret->p = a0; + ret->size = a1; + ret->status = a2; + return; +} + /** * arch_ei - arch enable global interrupts */ From f844671503306d309ed27f97dde949926c5dcded Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Fri, 19 Aug 2022 14:24:07 +0530 Subject: [PATCH 18/21] Driver fix to start from 0 Issue: #159 --- projects/demo_avr/config.mk | 1 - src/platform/mega_avr/common/hal/timer/timer16.h | 1 + src/platform/mega_avr/common/hal/timer/timer8.h | 1 + src/platform/sifive/common_fe310/hal/clint/clint.c | 1 + 4 files changed, 3 insertions(+), 1 deletion(-) diff --git a/projects/demo_avr/config.mk b/projects/demo_avr/config.mk index d6468d1d..8ca18de0 100644 --- a/projects/demo_avr/config.mk +++ b/projects/demo_avr/config.mk @@ -16,5 +16,4 @@ STDLOG_MEMBUF := 0 BOOTMSGS := 0 EARLYCON_SERIAL := 1 CONSOLE_SERIAL := 1 -TERRAKERN := 0 OBRDLED_ENABLE := 1 diff --git a/src/platform/mega_avr/common/hal/timer/timer16.h b/src/platform/mega_avr/common/hal/timer/timer16.h index b39383fd..bb7ef8c5 100644 --- a/src/platform/mega_avr/common/hal/timer/timer16.h +++ b/src/platform/mega_avr/common/hal/timer/timer16.h @@ -72,6 +72,7 @@ static void timer16_config_op_mode(const timer_port_t *port, bool en, bool inv) reg &= ~(3 << ((3 - id) << 1)); MMIO8(port->baddr + TCCRA_16B_OFFSET) = reg; + MMIO16(port->baddr + TCNTL_16B_OFFSET) = 0; } static void timer16_set(const timer_port_t *port, uint16_t value) diff --git a/src/platform/mega_avr/common/hal/timer/timer8.h b/src/platform/mega_avr/common/hal/timer/timer8.h index a92b3731..eb791373 100644 --- a/src/platform/mega_avr/common/hal/timer/timer8.h +++ b/src/platform/mega_avr/common/hal/timer/timer8.h @@ -54,6 +54,7 @@ static void timer8_config_ps(const timer_port_t *port, unsigned int ps) } MMIO8(port->baddr + TCCRB_8B_OFFSET) = reg; + MMIO8(port->baddr + TCNT_8B_OFFSET) = 0; } static void timer8_config_op_mode(const timer_port_t *port, bool en, bool inv) diff --git a/src/platform/sifive/common_fe310/hal/clint/clint.c b/src/platform/sifive/common_fe310/hal/clint/clint.c index 86beec2d..6ca26ecd 100644 --- a/src/platform/sifive/common_fe310/hal/clint/clint.c +++ b/src/platform/sifive/common_fe310/hal/clint/clint.c @@ -46,6 +46,7 @@ static status_t clint_setup() port->baddr = dp->baddr; port->stride = dp->stride; port->port_id = clint; + MMIO64(port->baddr + MTIME_OFFSET) = 0; return success; } From e1d2d65397d3f116e22954be0d9748fb5f5f0ca5 Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Sat, 20 Aug 2022 00:15:55 +0530 Subject: [PATCH 19/21] Improved memory organization and placements - Cleaned up and simplified linker scripts for platforms - Reorganised ordered memory sections - Simplified memory utilization calculation --- src/arch/avr/8/common_5x_6/terravisor/entry.S | 4 +- src/arch/avr/8/common_5x_6/terravisor/stack.S | 27 ------------- src/driver/build.mk | 2 - src/driver/driver.ld.sx | 22 ----------- src/include/linker_macros.h | 21 ++++++++++ src/include/machine_call.h | 2 +- .../mega_avr/common/platform/platform.c | 2 +- src/platform/mega_avr/common/sections.ld.sx | 39 ++++++++++--------- .../sifive/common_fe310/sections.ld.sx | 38 +++++++++--------- src/visor/terravisor/workers/mcall/build.mk | 2 - .../terravisor/workers/mcall/machine_call.c | 6 +-- .../terravisor/workers/mcall/mcall.ld.sx | 22 ----------- 12 files changed, 66 insertions(+), 121 deletions(-) delete mode 100644 src/arch/avr/8/common_5x_6/terravisor/stack.S delete mode 100644 src/driver/driver.ld.sx create mode 100644 src/include/linker_macros.h delete mode 100644 src/visor/terravisor/workers/mcall/mcall.ld.sx diff --git a/src/arch/avr/8/common_5x_6/terravisor/entry.S b/src/arch/avr/8/common_5x_6/terravisor/entry.S index ada2fe81..7663a4de 100644 --- a/src/arch/avr/8/common_5x_6/terravisor/entry.S +++ b/src/arch/avr/8/common_5x_6/terravisor/entry.S @@ -30,10 +30,10 @@ */ function entry cli - ldi r18, lo8(stack_start) + ldi r18, lo8(_stack_end) ldi r30, SPL st Z, r18 - ldi r18, hi8(stack_start) + ldi r18, hi8(_stack_end) ldi r30, SPH st Z, r18 eor r18, r18 diff --git a/src/arch/avr/8/common_5x_6/terravisor/stack.S b/src/arch/avr/8/common_5x_6/terravisor/stack.S deleted file mode 100644 index 3f3c7065..00000000 --- a/src/arch/avr/8/common_5x_6/terravisor/stack.S +++ /dev/null @@ -1,27 +0,0 @@ -/* - * CYANCORE LICENSE - * Copyrights (C) 2019, Cyancore Team - * - * File Name : stack.S - * Description : This file defines the stack space for the AVR core - * Primary Author : Akash Kollipara [akashkollipara@gmail.com] - * Organisation : Cyancore Core-Team - */ - -#include -#include - -/** - * stack - Stack object for AVR cores - * - * @brief This defines stack space for AVR core. It is ideal to declare a - * stack space apriory to avoid shortage of stack after the data in - * ro-section and heap-section are defined by linker. - */ - -.global stack_end -.global stack_start -object stack -stack_end: - .space STACK_SIZE, 0 -stack_start: diff --git a/src/driver/build.mk b/src/driver/build.mk index d0f47761..572b7f87 100644 --- a/src/driver/build.mk +++ b/src/driver/build.mk @@ -11,8 +11,6 @@ DRIVER_PATH := $(GET_PATH) -LD_SUPPLEMENT += $(DRIVER_PATH)/driver.ld.sx - #==========< Essentials Drivers >==========# include $(DRIVER_PATH)/interrupt/build.mk include $(DRIVER_PATH)/watchdog/build.mk diff --git a/src/driver/driver.ld.sx b/src/driver/driver.ld.sx deleted file mode 100644 index ec56258b..00000000 --- a/src/driver/driver.ld.sx +++ /dev/null @@ -1,22 +0,0 @@ -/* - * CYANCORE LICENSE - * Copyrights (C) 2019, Cyancore Team - * - * File Name : driver.ld.sx - * Description : This file contains memory layout for the - * cyancore driver framework - * Primary Author : Akash Kollipara [akashkollipara@gmail.com] - * Organisation : Cyancore Core-Team - */ - -SECTIONS -{ - .driver_table : - { - PROVIDE(_driver_table_start = .); - KEEP(*(.driver)) - PROVIDE(_driver_table_end = .); - } -} - -INSERT BEFORE .stack; diff --git a/src/include/linker_macros.h b/src/include/linker_macros.h new file mode 100644 index 00000000..a14f50b1 --- /dev/null +++ b/src/include/linker_macros.h @@ -0,0 +1,21 @@ +/* + * CYANCORE LICENSE + * Copyrights (C) 2022, Cyancore Team + * + * File Name : linker_macro.h + * Description : This file defines the linker macro utilities + * Primary Author : Akash Kollipara [akashkollipara@gmail.com] + * Organisation : Cyancore Core-Team + */ + +#pragma once + +#define DRIVER_TABLE \ + PROVIDE(_driver_table_start = .); \ + KEEP(*(.driver)) \ + PROVIDE(_driver_table_end = .); + +#define ECALL_TABLE \ + PROVIDE(_ecall_table_start = .); \ + KEEP(*(.ecall)) \ + PROVIDE(_ecall_table_end = .); diff --git a/src/include/machine_call.h b/src/include/machine_call.h index 5be20c13..e39c3fb0 100644 --- a/src/include/machine_call.h +++ b/src/include/machine_call.h @@ -40,7 +40,7 @@ typedef struct mcall } mcall_t; #define INCLUDE_MCALL(_name, _id , _callback) \ - const mcall_t _name _SECTION(".mcall") = \ + const mcall_t _name _SECTION(".ecall") = \ { \ .id = _id, \ .callback = _callback \ diff --git a/src/platform/mega_avr/common/platform/platform.c b/src/platform/mega_avr/common/platform/platform.c index a701e606..3f1ed8d5 100644 --- a/src/platform/mega_avr/common/platform/platform.c +++ b/src/platform/mega_avr/common/platform/platform.c @@ -123,7 +123,7 @@ void platform_memory_layout() syslog(info, "data Region\t: %06p - %06p : Size: %u\n", &_data_vstart, &_data_vend, &_data_size); syslog(info, "stack Region\t: %06p - %06p : Size: %u\n", - &_stack_end, &_stack_start, &_stack_size); + &_stack_start, &_stack_end, &_stack_size); syslog(info, "heap Region\t: %06p - %06p : Size: %u\n", &_heap_start, &_heap_end, &_heap_size); syslog(info, "\n"); diff --git a/src/platform/mega_avr/common/sections.ld.sx b/src/platform/mega_avr/common/sections.ld.sx index e2e9d5a0..b8177686 100644 --- a/src/platform/mega_avr/common/sections.ld.sx +++ b/src/platform/mega_avr/common/sections.ld.sx @@ -10,6 +10,7 @@ */ #include +#include #ifndef L_MEM_START #define L_MEM_START 0 @@ -18,8 +19,8 @@ MEMORY { /* VM Addresses */ - vma_imem : ORIGIN = V_IMEM_START, LENGTH = IMEM_LENGTH - vma_dmem : ORIGIN = V_DMEM_START, LENGTH = DMEM_LENGTH + vma_imem (airwx) : 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 = IMEM_LENGTH @@ -52,14 +53,6 @@ SECTIONS *(COMMON) } > vma_dmem - .heap : - { - . = ALIGN(HEAP_ALIGN); - *(.heap) - KEEP(*(.heap)) - . = . + HEAP_SIZE; - } > vma_dmem - .data : { . = ALIGN(ALIGN_BOUND); @@ -71,38 +64,46 @@ SECTIONS *(.data) *(.data.*) KEEP(*(.data)) + DRIVER_TABLE + ECALL_TABLE } > vma_dmem AT > lma_mem - .driver_table : {} > vma_dmem AT > lma_mem - .mcall_table : {} > vma_dmem AT > lma_mem + .heap : + { + . = ALIGN(HEAP_ALIGN); + *(.heap) + KEEP(*(.heap)) + . = . + HEAP_SIZE; + } > vma_dmem .stack : { . = ALIGN(ALIGN_BOUND); *(.stack) KEEP(*(.stack)) - } > vma_dmem AT > lma_mem + . = . + STACK_SIZE; + } > vma_dmem PROVIDE(_text_start = ADDR(.text)); PROVIDE(_text_size = SIZEOF(.text)); - PROVIDE(_text_end = _text_start + _text_size); + PROVIDE(_text_end = _text_start + _text_size - 1); PROVIDE(_data_start = LOADADDR(.data)); - PROVIDE(_data_size = SIZEOF(.data) + SIZEOF(.driver_table) + SIZEOF(.mcall_table)); + PROVIDE(_data_size = SIZEOF(.data)); PROVIDE(_data_vstart = ADDR(.data)); - PROVIDE(_data_vend = _data_vstart + _data_size); + PROVIDE(_data_vend = _data_vstart + _data_size - 1); PROVIDE(_bss_start = ADDR(.bss)); PROVIDE(_bss_size = SIZEOF(.bss)); - PROVIDE(_bss_end = _bss_start + _bss_size); + PROVIDE(_bss_end = _bss_start + _bss_size - 1); PROVIDE(_stack_start = ADDR(.stack)); PROVIDE(_stack_size = SIZEOF(.stack)); - PROVIDE(_stack_end = _stack_start - _stack_size); + PROVIDE(_stack_end = _stack_start + _stack_size - 1); PROVIDE(_heap_start = ADDR(.heap)); PROVIDE(_heap_size = SIZEOF(.heap)); - PROVIDE(_heap_end = _heap_start + SIZEOF(.heap)); + PROVIDE(_heap_end = _heap_start + _heap_size - 1); PROVIDE(_flash_size = _data_size + SIZEOF(.text)); PROVIDE(_ram_size = _bss_size + _data_size + SIZEOF(.stack) + SIZEOF(.heap)); diff --git a/src/platform/sifive/common_fe310/sections.ld.sx b/src/platform/sifive/common_fe310/sections.ld.sx index 150f0b4c..b3ba505e 100644 --- a/src/platform/sifive/common_fe310/sections.ld.sx +++ b/src/platform/sifive/common_fe310/sections.ld.sx @@ -10,6 +10,7 @@ */ #include +#include #ifndef L_MEM_START #define L_MEM_START 0 @@ -93,6 +94,8 @@ SECTIONS *(.data) *(.data.*) KEEP(*(.data)) + DRIVER_TABLE + ECALL_TABLE } > vma_dmem AT > lma_mem .tdata : ALIGN(4) @@ -103,56 +106,51 @@ SECTIONS 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 + .heap : ALIGN(HEAP_ALIGN) + { + *(.heap) + KEEP(*(.heap)) + . = . + HEAP_SIZE; + } > vma_dmem .stack : ALIGN(16) { - . = ALIGN(16); *(.stack) KEEP(*(.stack)) . = . + STACK_SIZE; } > vma_dmem - .heap : ALIGN(HEAP_ALIGN) - { - *(.heap) - KEEP(*(.heap)) - . = . + HEAP_SIZE; - } > vma_dmem - PROVIDE(_text_start = ADDR(.text)); PROVIDE(_text_size = SIZEOF(.text)); - PROVIDE(_text_end = _text_start + _text_size); + PROVIDE(_text_end = _text_start + _text_size - 1); PROVIDE(_rodata_start = ADDR(.rodata)); PROVIDE(_rodata_size = SIZEOF(.rodata)); - PROVIDE(_rodata_end = _rodata_start + _rodata_size); + PROVIDE(_rodata_end = _rodata_start + _rodata_size - 1); 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_size = SIZEOF(.data) + SIZEOF(.tdata) + SIZEOF(.static_data_global)); PROVIDE(_data_vstart = ADDR(.static_data_global)); - PROVIDE(_data_vend = _data_vstart + _data_size); + PROVIDE(_data_vend = _data_vstart + _data_size - 1); PROVIDE(_tls_start = ADDR(.tdata)); PROVIDE(_global_start = ADDR(.static_global_var) + 0x800); - PROVIDE(_stack_start = ADDR(.stack) + STACK_SIZE); PROVIDE(_stack_size = SIZEOF(.stack)); PROVIDE(_stack_end = ADDR(.stack)); + PROVIDE(_stack_start = _stack_end + _stack_size); PROVIDE(_bss_start = ADDR(.bss)); - PROVIDE(_bss_size = SIZEOF(.bss) + SIZEOF(.static_bss_global) + 4); - PROVIDE(_bss_end = _bss_start + _bss_size); + PROVIDE(_bss_size = SIZEOF(.bss) + SIZEOF(.static_bss_global)); + PROVIDE(_bss_end = _bss_start + _bss_size - 1); PROVIDE(_heap_start = ADDR(.heap)); PROVIDE(_heap_size = SIZEOF(.heap)); - PROVIDE(_heap_end = _heap_start + _heap_size); + PROVIDE(_heap_end = _heap_start + _heap_size - 1); PROVIDE(_itim_start = LOADADDR(.itim)); PROVIDE(_itim_size = SIZEOF(.itim)); PROVIDE(_itim_vstart = ADDR(.itim)); - PROVIDE(_itim_vend = _itim_vstart + _itim_size); + PROVIDE(_itim_vend = _itim_vstart + _itim_size - (_itim_size ? 1 : 0)); PROVIDE(_flash_size = _data_size + SIZEOF(.text) + _itim_size + SIZEOF(.rodata)); PROVIDE(_ram_size = _bss_size + _data_size + SIZEOF(.stack) + SIZEOF(.heap)); diff --git a/src/visor/terravisor/workers/mcall/build.mk b/src/visor/terravisor/workers/mcall/build.mk index b8c6f9b2..9e20c5e8 100644 --- a/src/visor/terravisor/workers/mcall/build.mk +++ b/src/visor/terravisor/workers/mcall/build.mk @@ -10,8 +10,6 @@ MCALL_DIR := $(GET_PATH) -LD_SUPPLEMENT += $(MCALL_DIR)/mcall.ld.sx - DIR := $(MCALL_DIR) include mk/obj.mk diff --git a/src/visor/terravisor/workers/mcall/machine_call.c b/src/visor/terravisor/workers/mcall/machine_call.c index c0d91856..1bcb928a 100644 --- a/src/visor/terravisor/workers/mcall/machine_call.c +++ b/src/visor/terravisor/workers/mcall/machine_call.c @@ -16,8 +16,8 @@ #include /* mcall Table start and end defined in linker script mcall.ld.sx */ -extern mcall_t _mcall_table_start; -extern mcall_t _mcall_table_end; +extern mcall_t _ecall_table_start; +extern mcall_t _ecall_table_end; typedef void (*mcb_t)(call_arg_t, call_arg_t, call_arg_t, mret_t *); static mcb_t get_callback_from_table(mcall_t *start, mcall_t *end, mcall_id_t id) @@ -45,7 +45,7 @@ void machine_call(mcall_id_t id, call_arg_t a0, call_arg_t a1, call_arg_t a2, mr ret->size = 0; ret->status = error_mcall_code_inval; - cb = get_callback_from_table(&_mcall_table_start, &_mcall_table_end, id); + cb = get_callback_from_table(&_ecall_table_start, &_ecall_table_end, id); if(cb != 0) cb(a0, a1, a2, ret); return; diff --git a/src/visor/terravisor/workers/mcall/mcall.ld.sx b/src/visor/terravisor/workers/mcall/mcall.ld.sx deleted file mode 100644 index 9cd88913..00000000 --- a/src/visor/terravisor/workers/mcall/mcall.ld.sx +++ /dev/null @@ -1,22 +0,0 @@ -/* - * CYANCORE LICENSE - * Copyrights (C) 2019, Cyancore Team - * - * File Name : mcall.ld.sx - * Description : This file contains memory layout for the - * cyancore mcall framework - * Primary Author : Akash Kollipara [akashkollipara@gmail.com] - * Organisation : Cyancore Core-Team - */ - -SECTIONS -{ - .mcall_table : - { - PROVIDE(_mcall_table_start = .); - KEEP(*(.mcall)) - PROVIDE(_mcall_table_end = .); - } -} - -INSERT BEFORE .stack; From 99ddf8335af1daca91dd4b974416391f3260330e Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Mon, 22 Aug 2022 20:44:32 +0530 Subject: [PATCH 20/21] Patch for new size tool --- mk/elf.mk | 3 ++- src/platform/mega_avr/atmega2560/config.mk | 5 +++++ src/platform/mega_avr/atmega2560/include/plat_mem.h | 4 ++-- src/platform/mega_avr/atmega328p/config.mk | 5 +++++ src/platform/sifive/common_fe310/build.mk | 2 +- src/platform/sifive/fe310g002-bl/config.mk | 7 +++++++ src/platform/sifive/fe310g002/config.mk | 7 +++++++ 7 files changed, 29 insertions(+), 4 deletions(-) diff --git a/mk/elf.mk b/mk/elf.mk index 2283aff0..765c1d08 100644 --- a/mk/elf.mk +++ b/mk/elf.mk @@ -29,7 +29,8 @@ $(ELF): $(DEP_LIBS) $(DEP_OBJS) $(LD_SCRIPT) $(LD_SUPPLEMENT) | $$(SIZE) $(OD) -Dx -h --wide $@ > $(@:.elf=.lst) $(OC) -O binary $@ $(@:.elf=.bin) $(OC) -O ihex $@ $(@:.elf=.hex) - @cd $(@D); $(SIZE) $(@F) + @cd $(@D); $(SIZE) -f $(@F) -m Flash $(FLASH_START) $(FLASH_SIZE) \ + -m RAM $(RAM_START) $(RAM_SIZE) $(MEMSIZE_ARGS) @echo "" $(OUT)/%.ld: %.ld.sx diff --git a/src/platform/mega_avr/atmega2560/config.mk b/src/platform/mega_avr/atmega2560/config.mk index 0b82cd13..597d571f 100644 --- a/src/platform/mega_avr/atmega2560/config.mk +++ b/src/platform/mega_avr/atmega2560/config.mk @@ -24,6 +24,11 @@ $(eval $(call add_define,BIT)) BOOT_CORE_ID:= 0 $(eval $(call add_define,BOOT_CORE_ID)) +FLASH_START := 0x0000 +FLASH_SIZE := 0x40000 # 256K +RAM_START := 0x00800200 +RAM_SIZE := 0x2000 # 2K + # Call this FLAG from Project config file if needed FCLK ?= 16000000 $(eval $(call add_define,FCLK)) diff --git a/src/platform/mega_avr/atmega2560/include/plat_mem.h b/src/platform/mega_avr/atmega2560/include/plat_mem.h index 8f58df5f..22fa45ec 100644 --- a/src/platform/mega_avr/atmega2560/include/plat_mem.h +++ b/src/platform/mega_avr/atmega2560/include/plat_mem.h @@ -18,8 +18,8 @@ #define L_MEM_START 0x00000000 -#define IMEM_LENGTH 0x20000 -#define DMEM_LENGTH 0x1fff +#define IMEM_LENGTH 0x40000 +#define DMEM_LENGTH 0x2000 #define ALIGN_BOUND 2 diff --git a/src/platform/mega_avr/atmega328p/config.mk b/src/platform/mega_avr/atmega328p/config.mk index 3d873f23..95c37b99 100644 --- a/src/platform/mega_avr/atmega328p/config.mk +++ b/src/platform/mega_avr/atmega328p/config.mk @@ -24,6 +24,11 @@ $(eval $(call add_define,BIT)) BOOT_CORE_ID:= 0 $(eval $(call add_define,BOOT_CORE_ID)) +FLASH_START := 0x0000 +FLASH_SIZE := 0x8000 # 32K +RAM_START := 0x00800100 +RAM_SIZE := 0x800 # 2K + # Call this FLAG from Project config file if needed FCLK ?= 16000000 $(eval $(call add_define,FCLK)) diff --git a/src/platform/sifive/common_fe310/build.mk b/src/platform/sifive/common_fe310/build.mk index 077d4b41..3704a641 100644 --- a/src/platform/sifive/common_fe310/build.mk +++ b/src/platform/sifive/common_fe310/build.mk @@ -17,7 +17,7 @@ PLAT_INCLUDE += $(FE310_COMMON_DIR)/include LINT_FLAGS += --platform=unix32 -#LD_FLAGS += --no-relax +MEMSIZE_ARGS += -m ITIM $(ITIM_START) $(ITIM_SIZE) USE_DEFAULT_RESOURCES ?= 1 LOCAL_INTERRUPT_DEVICE ?= 1 diff --git a/src/platform/sifive/fe310g002-bl/config.mk b/src/platform/sifive/fe310g002-bl/config.mk index 0f5ebabf..00790b1a 100644 --- a/src/platform/sifive/fe310g002-bl/config.mk +++ b/src/platform/sifive/fe310g002-bl/config.mk @@ -27,6 +27,13 @@ $(eval $(call add_define,BIT)) BOOT_CORE_ID:= 0 $(eval $(call add_define,BOOT_CORE_ID)) +FLASH_START := 0x20000000 +FLASH_SIZE := 0x10000 # 64K +RAM_START := 0x80000000 +RAM_SIZE := 0x4000 # 16K +ITIM_START := 0x08000000 +ITIM_SIZE := 0x2000 # 8K + # Call this FLAG from Project config file if needed XCLK ?= 16000000 ICLK ?= 72000000 diff --git a/src/platform/sifive/fe310g002/config.mk b/src/platform/sifive/fe310g002/config.mk index 6992053b..70bc7681 100644 --- a/src/platform/sifive/fe310g002/config.mk +++ b/src/platform/sifive/fe310g002/config.mk @@ -27,6 +27,13 @@ $(eval $(call add_define,BIT)) BOOT_CORE_ID:= 0 $(eval $(call add_define,BOOT_CORE_ID)) +FLASH_START := 0x20010000 +FLASH_SIZE := 0x3f0000 # 4M-64K +RAM_START := 0x80000000 +RAM_SIZE := 0x4000 # 16K +ITIM_START := 0x08000000 +ITIM_SIZE := 0x2000 # 8K + # Call this FLAG from Project config file if needed XCLK ?= 16000000 ICLK ?= 72000000 From 98c9363f7087e1661b13f30fe3665f00357f633c Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Tue, 23 Aug 2022 14:20:37 +0530 Subject: [PATCH 21/21] Updated release info --- README.md | 2 +- mk/elf.mk | 3 ++- src/engine/banner.mk | 6 +++--- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index cf5a038b..3349c8a6 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ -> **Version (arch:2 | major:4 | minor:2): 1.2.2** +> **Version (arch:2 | major:4 | minor:2): 1.3.0** [![GitHub CI](https://github.com/VisorFolks/cyancore/actions/workflows/github_ci.yml/badge.svg)](https://github.com/VisorFolks/cyancore/actions/workflows/github_ci.yml) [![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=VisorFolks_cyancore&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=VisorFolks_cyancore) diff --git a/mk/elf.mk b/mk/elf.mk index 765c1d08..303c35ad 100644 --- a/mk/elf.mk +++ b/mk/elf.mk @@ -25,7 +25,8 @@ elf: $(ELF) .SECONDEXPANSION: $(ELF): $(DEP_LIBS) $(DEP_OBJS) $(LD_SCRIPT) $(LD_SUPPLEMENT) | $$(SIZE) @echo "Elf: Generating $(@F) ..." - $(LD) -dT $(LD_SCRIPT) $(addprefix -T , $(LD_SUPPLEMENT)) $(LD_FLAGS) -Map=$(@:.elf=.map) -o $@ $(filter %.o, $^) $(DEP_LIB_PATH) $(DEP_LIBS_ARG) -L $(TL) -lgcc + $(LD) -dT $(LD_SCRIPT) $(addprefix -T , $(LD_SUPPLEMENT)) $(LD_FLAGS) \ + -Map=$(@:.elf=.map) -o $@ $(filter %.o, $^) $(DEP_LIB_PATH) $(DEP_LIBS_ARG) -L $(TL) -lgcc $(OD) -Dx -h --wide $@ > $(@:.elf=.lst) $(OC) -O binary $@ $(@:.elf=.bin) $(OC) -O ihex $@ $(@:.elf=.hex) diff --git a/src/engine/banner.mk b/src/engine/banner.mk index 8a6456fb..f4df49e2 100644 --- a/src/engine/banner.mk +++ b/src/engine/banner.mk @@ -1,6 +1,6 @@ # # CYANCORE LICENSE -# Copyrights (C) 2019, Cyancore Team +# Copyrights (C) 2019-2022, Cyancore Team # # File Name : banner.mk # Description : This script prints banner while starting build @@ -9,8 +9,8 @@ # .PHONY: version -NAME = Helium -VERSION = 0x01000202 +NAME = Lithium +VERSION = 0x01000300 $(eval $(call add_define,VERSION))