diff --git a/components/finsh/cmd.c b/components/finsh/cmd.c index 0e04b794e975553d8a4357b171f14c1496da836c..7f12671429444839dada014949994d5ac2f472ad 100644 --- a/components/finsh/cmd.c +++ b/components/finsh/cmd.c @@ -102,9 +102,9 @@ static long _list_thread(struct rt_list_node *list) while (*ptr == '#')ptr ++; rt_kprintf(" 0x%08x 0x%08x %02d%% 0x%08x %03d\n", - thread->stack_size + ((rt_uint32_t)thread->stack_addr - (rt_uint32_t)thread->sp), + thread->stack_size + ((rt_ubase_t)thread->stack_addr - (rt_ubase_t)thread->sp), thread->stack_size, - (thread->stack_size - ((rt_uint32_t) ptr - (rt_uint32_t) thread->stack_addr)) * 100 + (thread->stack_size - ((rt_ubase_t) ptr - (rt_ubase_t) thread->stack_addr)) * 100 / thread->stack_size, thread->remaining_tick, thread->error); diff --git a/include/rtdef.h b/include/rtdef.h index 99caa1c7d558c1477c78a309c9dbf48dbbe4d8ae..fef08599c79b0199bfe01d639d086396f3c834ea 100644 --- a/include/rtdef.h +++ b/include/rtdef.h @@ -25,6 +25,7 @@ * 2018-09-04 Bernard change version number to v3.1.1 * 2018-09-14 Bernard apply Apache License v2.0 to RT-Thread Kernel * 2018-10-13 Bernard change version number to v4.0.0 + * 2018-10-02 Bernard add 64bit arch support */ #ifndef __RT_DEF_H__ @@ -55,13 +56,20 @@ extern "C" { /* RT-Thread basic data type definitions */ typedef signed char rt_int8_t; /**< 8bit integer type */ typedef signed short rt_int16_t; /**< 16bit integer type */ -typedef signed long rt_int32_t; /**< 32bit integer type */ +typedef signed int rt_int32_t; /**< 32bit integer type */ typedef unsigned char rt_uint8_t; /**< 8bit unsigned integer type */ typedef unsigned short rt_uint16_t; /**< 16bit unsigned integer type */ -typedef unsigned long rt_uint32_t; /**< 32bit unsigned integer type */ -typedef int rt_bool_t; /**< boolean type */ +typedef unsigned int rt_uint32_t; /**< 32bit unsigned integer type */ -/* 32bit CPU */ +#ifdef ARCH_CPU_64BIT +typedef signed long rt_int64_t; /**< 64bit integer type */ +typedef unsigned long rt_uint64_t; /**< 64bit unsigned integer type */ +#else +typedef signed long long rt_int64_t; /**< 64bit integer type */ +typedef unsigned long long rt_uint64_t; /**< 64bit unsigned integer type */ +#endif + +typedef int rt_bool_t; /**< boolean type */ typedef long rt_base_t; /**< Nbit CPU related date type */ typedef unsigned long rt_ubase_t; /**< Nbit unsigned CPU related data type */ @@ -648,7 +656,7 @@ struct rt_mailbox { struct rt_ipc_object parent; /**< inherit from ipc_object */ - rt_uint32_t *msg_pool; /**< start address of message buffer */ + rt_ubase_t *msg_pool; /**< start address of message buffer */ rt_uint16_t size; /**< size of message pool */ diff --git a/include/rthw.h b/include/rthw.h index c6af392e6284207fd16e7f00e21ef7124e7a5bd8..17151de99a8292c49a642d381c87828063d5b5ae 100644 --- a/include/rthw.h +++ b/include/rthw.h @@ -98,14 +98,14 @@ void rt_hw_interrupt_enable(rt_base_t level); /* * Context interfaces */ -void rt_hw_context_switch(rt_uint32_t from, rt_uint32_t to); -void rt_hw_context_switch_to(rt_uint32_t to); -void rt_hw_context_switch_interrupt(rt_uint32_t from, rt_uint32_t to); +void rt_hw_context_switch(rt_ubase_t from, rt_ubase_t to); +void rt_hw_context_switch_to(rt_ubase_t to); +void rt_hw_context_switch_interrupt(rt_ubase_t from, rt_ubase_t to); void rt_hw_console_output(const char *str); -void rt_hw_backtrace(rt_uint32_t *fp, rt_uint32_t thread_entry); -void rt_hw_show_memory(rt_uint32_t addr, rt_uint32_t size); +void rt_hw_backtrace(rt_uint32_t *fp, rt_ubase_t thread_entry); +void rt_hw_show_memory(rt_uint32_t addr, rt_size_t size); /* * Exception interfaces diff --git a/include/rtthread.h b/include/rtthread.h index fc5029a22003839cf9c96e9ec34396bf7ce276c1..814fa454d85d29648e928884cdd29563a4144a13 100644 --- a/include/rtthread.h +++ b/include/rtthread.h @@ -256,7 +256,7 @@ void rt_page_free(void *addr, rt_size_t npages); #endif #ifdef RT_USING_HOOK -void rt_malloc_sethook(void (*hook)(void *ptr, rt_uint32_t size)); +void rt_malloc_sethook(void (*hook)(void *ptr, rt_size_t size)); void rt_free_sethook(void (*hook)(void *ptr)); #endif @@ -269,9 +269,9 @@ void rt_free_sethook(void (*hook)(void *ptr)); rt_err_t rt_memheap_init(struct rt_memheap *memheap, const char *name, void *start_addr, - rt_uint32_t size); + rt_size_t size); rt_err_t rt_memheap_detach(struct rt_memheap *heap); -void *rt_memheap_alloc(struct rt_memheap *heap, rt_uint32_t size); +void *rt_memheap_alloc(struct rt_memheap *heap, rt_size_t size); void *rt_memheap_realloc(struct rt_memheap *heap, void *ptr, rt_size_t newsize); void rt_memheap_free(void *ptr); #endif @@ -347,11 +347,11 @@ rt_err_t rt_mb_detach(rt_mailbox_t mb); rt_mailbox_t rt_mb_create(const char *name, rt_size_t size, rt_uint8_t flag); rt_err_t rt_mb_delete(rt_mailbox_t mb); -rt_err_t rt_mb_send(rt_mailbox_t mb, rt_uint32_t value); +rt_err_t rt_mb_send(rt_mailbox_t mb, rt_ubase_t value); rt_err_t rt_mb_send_wait(rt_mailbox_t mb, - rt_uint32_t value, + rt_ubase_t value, rt_int32_t timeout); -rt_err_t rt_mb_recv(rt_mailbox_t mb, rt_uint32_t *value, rt_int32_t timeout); +rt_err_t rt_mb_recv(rt_mailbox_t mb, rt_ubase_t *value, rt_int32_t timeout); rt_err_t rt_mb_control(rt_mailbox_t mb, int cmd, void *arg); #endif diff --git a/libcpu/Kconfig b/libcpu/Kconfig index 0a280d4b5cb2be2ae4cdb372471ff8298ec9f26e..69d9220da11d510eba751b9cd68612f0540bd9d3 100644 --- a/libcpu/Kconfig +++ b/libcpu/Kconfig @@ -1,3 +1,6 @@ +config ARCH_CPU_64BIT + bool + config ARCH_ARM bool @@ -83,6 +86,18 @@ config ARCH_POWERPC config ARCH_RISCV bool +config ARCH_RISCV_FPU + bool + +config ARCH_RISCV32 + select ARCH_RISCV + bool + +config ARCH_RISCV64 + select ARCH_RISCV + select ARCH_CPU_64BIT + bool + config ARCH_IA32 bool diff --git a/src/ipc.c b/src/ipc.c index 56045911a8cb446268bf0471e5e35944ed5c2868..7f603cfc4dcd74ef29e640a4f35e53367264c89e 100644 --- a/src/ipc.c +++ b/src/ipc.c @@ -32,6 +32,7 @@ * 2010-11-10 Bernard add IPC reset command implementation. * 2011-12-18 Bernard add more parameter checking in message queue * 2013-09-14 Grissiom add an option check in rt_event_recv + * 2018-10-02 Bernard add 64bit support for mailbox */ #include @@ -486,10 +487,10 @@ rt_err_t rt_sem_control(rt_sem_t sem, int cmd, void *arg) if (cmd == RT_IPC_CMD_RESET) { - rt_uint32_t value; + rt_ubase_t value; /* get value */ - value = (rt_uint32_t)arg; + value = (rt_ubase_t)arg; /* disable interrupt */ level = rt_hw_interrupt_disable(); @@ -1373,7 +1374,7 @@ rt_mailbox_t rt_mb_create(const char *name, rt_size_t size, rt_uint8_t flag) /* init mailbox */ mb->size = size; - mb->msg_pool = RT_KERNEL_MALLOC(mb->size * sizeof(rt_uint32_t)); + mb->msg_pool = RT_KERNEL_MALLOC(mb->size * sizeof(rt_ubase_t)); if (mb->msg_pool == RT_NULL) { /* delete mailbox object */ @@ -1436,7 +1437,7 @@ RTM_EXPORT(rt_mb_delete); * @return the error code */ rt_err_t rt_mb_send_wait(rt_mailbox_t mb, - rt_uint32_t value, + rt_ubase_t value, rt_int32_t timeout) { struct rt_thread *thread; @@ -1567,7 +1568,7 @@ RTM_EXPORT(rt_mb_send_wait); * * @return the error code */ -rt_err_t rt_mb_send(rt_mailbox_t mb, rt_uint32_t value) +rt_err_t rt_mb_send(rt_mailbox_t mb, rt_ubase_t value) { return rt_mb_send_wait(mb, value, 0); } @@ -1583,7 +1584,7 @@ RTM_EXPORT(rt_mb_send); * * @return the error code */ -rt_err_t rt_mb_recv(rt_mailbox_t mb, rt_uint32_t *value, rt_int32_t timeout) +rt_err_t rt_mb_recv(rt_mailbox_t mb, rt_ubase_t *value, rt_int32_t timeout) { struct rt_thread *thread; register rt_ubase_t temp; diff --git a/src/kservice.c b/src/kservice.c index dbc4a2fb0e0bb383ec91fa730bb5c69317ea1118..95c9308182375128d4aa42615bd2794a60017ee8 100644 --- a/src/kservice.c +++ b/src/kservice.c @@ -1189,31 +1189,36 @@ RTM_EXPORT(rt_kprintf); */ void *rt_malloc_align(rt_size_t size, rt_size_t align) { - void *align_ptr; void *ptr; + void *align_ptr; + int uintptr_size; rt_size_t align_size; - /* align the alignment size to 4 byte */ - align = ((align + 0x03) & ~0x03); + /* sizeof pointer */ + uintptr_size = sizeof(void*); + uintptr_size -= 1; + + /* align the alignment size to uintptr size byte */ + align = ((align + uintptr_size) & ~uintptr_size); /* get total aligned size */ - align_size = ((size + 0x03) & ~0x03) + align; + align_size = ((size + uintptr_size) & ~uintptr_size) + align; /* allocate memory block from heap */ ptr = rt_malloc(align_size); if (ptr != RT_NULL) { /* the allocated memory block is aligned */ - if (((rt_uint32_t)ptr & (align - 1)) == 0) + if (((rt_ubase_t)ptr & (align - 1)) == 0) { - align_ptr = (void *)((rt_uint32_t)ptr + align); + align_ptr = (void *)((rt_ubase_t)ptr + align); } else { - align_ptr = (void *)(((rt_uint32_t)ptr + (align - 1)) & ~(align - 1)); + align_ptr = (void *)(((rt_ubase_t)ptr + (align - 1)) & ~(align - 1)); } /* set the pointer before alignment pointer to the real pointer */ - *((rt_uint32_t *)((rt_uint32_t)align_ptr - sizeof(void *))) = (rt_uint32_t)ptr; + *((rt_ubase_t *)((rt_ubase_t)align_ptr - sizeof(void *))) = (rt_ubase_t)ptr; ptr = align_ptr; } @@ -1232,7 +1237,7 @@ void rt_free_align(void *ptr) { void *real_ptr; - real_ptr = (void *) * (rt_uint32_t *)((rt_uint32_t)ptr - sizeof(void *)); + real_ptr = (void *) * (rt_ubase_t *)((rt_ubase_t)ptr - sizeof(void *)); rt_free(real_ptr); } RTM_EXPORT(rt_free_align); diff --git a/src/mem.c b/src/mem.c index 2fefb48b679117dfe6d716f8f9cade89bf29b670..ca5329404e57f7f4f81bb9610015a367a378d220 100644 --- a/src/mem.c +++ b/src/mem.c @@ -11,6 +11,7 @@ * 2010-07-13 Bernard fix RT_ALIGN issue found by kuronca * 2010-10-14 Bernard fix rt_realloc issue when realloc a NULL pointer. * 2017-07-14 armink fix rt_realloc issue when new size is 0 + * 2018-10-02 Bernard Add 64bit support */ /* @@ -97,12 +98,19 @@ struct heap_mem /* magic and used flag */ rt_uint16_t magic; rt_uint16_t used; +#ifdef ARCH_CPU_64BIT + rt_uint32_t resv; +#endif rt_size_t next, prev; #ifdef RT_USING_MEMTRACE +#ifdef ARCH_CPU_64BIT + rt_uint8_t thread[8]; +#else rt_uint8_t thread[4]; /* thread name */ #endif +#endif }; /** pointer to the heap: for alignment, heap_ptr is now a pointer instead of an array */ @@ -111,7 +119,12 @@ static rt_uint8_t *heap_ptr; /** the last entry, always unused! */ static struct heap_mem *heap_end; +#ifdef ARCH_CPU_64BIT +#define MIN_SIZE 24 +#else #define MIN_SIZE 12 +#endif + #define MIN_SIZE_ALIGNED RT_ALIGN(MIN_SIZE, RT_ALIGN_SIZE) #define SIZEOF_STRUCT_MEM RT_ALIGN(sizeof(struct heap_mem), RT_ALIGN_SIZE) @@ -191,8 +204,8 @@ static void plug_holes(struct heap_mem *mem) void rt_system_heap_init(void *begin_addr, void *end_addr) { struct heap_mem *mem; - rt_uint32_t begin_align = RT_ALIGN((rt_uint32_t)begin_addr, RT_ALIGN_SIZE); - rt_uint32_t end_align = RT_ALIGN_DOWN((rt_uint32_t)end_addr, RT_ALIGN_SIZE); + rt_ubase_t begin_align = RT_ALIGN((rt_ubase_t)begin_addr, RT_ALIGN_SIZE); + rt_ubase_t end_align = RT_ALIGN_DOWN((rt_ubase_t)end_addr, RT_ALIGN_SIZE); RT_DEBUG_NOT_IN_INTERRUPT; @@ -206,7 +219,7 @@ void rt_system_heap_init(void *begin_addr, void *end_addr) else { rt_kprintf("mem init, error begin address 0x%x, and end address 0x%x\n", - (rt_uint32_t)begin_addr, (rt_uint32_t)end_addr); + (rt_ubase_t)begin_addr, (rt_ubase_t)end_addr); return; } @@ -215,7 +228,7 @@ void rt_system_heap_init(void *begin_addr, void *end_addr) heap_ptr = (rt_uint8_t *)begin_align; RT_DEBUG_LOG(RT_DEBUG_MEM, ("mem init, heap begin address 0x%x, size %d\n", - (rt_uint32_t)heap_ptr, mem_size_aligned)); + (rt_ubase_t)heap_ptr, mem_size_aligned)); /* initialize the start of the heap */ mem = (struct heap_mem *)heap_ptr; @@ -374,14 +387,14 @@ void *rt_malloc(rt_size_t size) } rt_sem_release(&heap_sem); - RT_ASSERT((rt_uint32_t)mem + SIZEOF_STRUCT_MEM + size <= (rt_uint32_t)heap_end); - RT_ASSERT((rt_uint32_t)((rt_uint8_t *)mem + SIZEOF_STRUCT_MEM) % RT_ALIGN_SIZE == 0); - RT_ASSERT((((rt_uint32_t)mem) & (RT_ALIGN_SIZE - 1)) == 0); + RT_ASSERT((rt_ubase_t)mem + SIZEOF_STRUCT_MEM + size <= (rt_ubase_t)heap_end); + RT_ASSERT((rt_ubase_t)((rt_uint8_t *)mem + SIZEOF_STRUCT_MEM) % RT_ALIGN_SIZE == 0); + RT_ASSERT((((rt_ubase_t)mem) & (RT_ALIGN_SIZE - 1)) == 0); RT_DEBUG_LOG(RT_DEBUG_MEM, ("allocate memory at 0x%x, size: %d\n", - (rt_uint32_t)((rt_uint8_t *)mem + SIZEOF_STRUCT_MEM), - (rt_uint32_t)(mem->next - ((rt_uint8_t *)mem - heap_ptr)))); + (rt_ubase_t)((rt_uint8_t *)mem + SIZEOF_STRUCT_MEM), + (rt_ubase_t)(mem->next - ((rt_uint8_t *)mem - heap_ptr)))); RT_OBJECT_HOOK_CALL(rt_malloc_hook, (((void *)((rt_uint8_t *)mem + SIZEOF_STRUCT_MEM)), size)); @@ -539,7 +552,7 @@ void rt_free(void *rmem) RT_DEBUG_NOT_IN_INTERRUPT; - RT_ASSERT((((rt_uint32_t)rmem) & (RT_ALIGN_SIZE - 1)) == 0); + RT_ASSERT((((rt_ubase_t)rmem) & (RT_ALIGN_SIZE - 1)) == 0); RT_ASSERT((rt_uint8_t *)rmem >= (rt_uint8_t *)heap_ptr && (rt_uint8_t *)rmem < (rt_uint8_t *)heap_end); @@ -558,8 +571,8 @@ void rt_free(void *rmem) RT_DEBUG_LOG(RT_DEBUG_MEM, ("release memory 0x%x, size: %d\n", - (rt_uint32_t)rmem, - (rt_uint32_t)(mem->next - ((rt_uint8_t *)mem - heap_ptr)))); + (rt_ubase_t)rmem, + (rt_ubase_t)(mem->next - ((rt_uint8_t *)mem - heap_ptr)))); /* protect the heap from concurrent access */ @@ -624,12 +637,12 @@ FINSH_FUNCTION_EXPORT(list_mem, list memory usage information) int memcheck(void) { int position; - rt_uint32_t level; + rt_ubase_t level; struct heap_mem *mem; level = rt_hw_interrupt_disable(); for (mem = (struct heap_mem *)heap_ptr; mem != heap_end; mem = (struct heap_mem *)&heap_ptr[mem->next]) { - position = (rt_uint32_t)mem - (rt_uint32_t)heap_ptr; + position = (rt_ubase_t)mem - (rt_ubase_t)heap_ptr; if (position < 0) goto __exit; if (position > mem_size_aligned) goto __exit; if (mem->magic != HEAP_MAGIC) goto __exit; @@ -664,7 +677,7 @@ int memtrace(int argc, char **argv) rt_kprintf("\n--memory item information --\n"); for (mem = (struct heap_mem *)heap_ptr; mem != heap_end; mem = (struct heap_mem *)&heap_ptr[mem->next]) { - int position = (rt_uint32_t)mem - (rt_uint32_t)heap_ptr; + int position = (rt_ubase_t)mem - (rt_ubase_t)heap_ptr; int size; rt_kprintf("[0x%08x - ", mem); diff --git a/src/memheap.c b/src/memheap.c index 946f163b645b6c4d502cb574e0714214fcd8d6d0..eb5d710029af70160574ff879ccec125fe0daa84 100644 --- a/src/memheap.c +++ b/src/memheap.c @@ -50,7 +50,7 @@ rt_err_t rt_memheap_init(struct rt_memheap *memheap, const char *name, void *start_addr, - rt_uint32_t size) + rt_size_t size) { struct rt_memheap_item *item; @@ -135,7 +135,7 @@ rt_err_t rt_memheap_detach(struct rt_memheap *heap) } RTM_EXPORT(rt_memheap_detach); -void *rt_memheap_alloc(struct rt_memheap *heap, rt_uint32_t size) +void *rt_memheap_alloc(struct rt_memheap *heap, rt_size_t size) { rt_err_t result; rt_uint32_t free_size; diff --git a/src/scheduler.c b/src/scheduler.c index 95b5d487df3078db436234ea38647cf51ffb0f8b..446b153e1bfeea56571a3f74210260818bdbd9b1 100644 --- a/src/scheduler.c +++ b/src/scheduler.c @@ -75,11 +75,11 @@ static void _rt_scheduler_stack_check(struct rt_thread *thread) RT_ASSERT(thread != RT_NULL); if (*((rt_uint8_t *)thread->stack_addr) != '#' || - (rt_uint32_t)thread->sp <= (rt_uint32_t)thread->stack_addr || - (rt_uint32_t)thread->sp > - (rt_uint32_t)thread->stack_addr + (rt_uint32_t)thread->stack_size) + (rt_ubase_t)thread->sp <= (rt_ubase_t)thread->stack_addr || + (rt_ubase_t)thread->sp > + (rt_ubase_t)thread->stack_addr + (rt_ubase_t)thread->stack_size) { - rt_uint32_t level; + rt_ubase_t level; rt_kprintf("thread:%s stack overflow\n", thread->name); #ifdef RT_USING_FINSH @@ -91,7 +91,7 @@ static void _rt_scheduler_stack_check(struct rt_thread *thread) level = rt_hw_interrupt_disable(); while (level); } - else if ((rt_uint32_t)thread->sp <= ((rt_uint32_t)thread->stack_addr + 32)) + else if ((rt_ubase_t)thread->sp <= ((rt_ubase_t)thread->stack_addr + 32)) { rt_kprintf("warning: %s stack is close to end of stack address.\n", thread->name); @@ -159,7 +159,7 @@ void rt_system_scheduler_start(void) rt_current_thread = to_thread; /* switch to new thread */ - rt_hw_context_switch_to((rt_uint32_t)&to_thread->sp); + rt_hw_context_switch_to((rt_ubase_t)&to_thread->sp); /* never come back */ } @@ -214,8 +214,8 @@ void rt_schedule(void) /* switch to new thread */ RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("[%d]switch to priority#%d " - "thread:%.*s(sp:0x%p), " - "from thread:%.*s(sp: 0x%p)\n", + "thread:%.*s(sp:0x%08x), " + "from thread:%.*s(sp: 0x%08x)\n", rt_interrupt_nest, highest_ready_priority, RT_NAME_MAX, to_thread->name, to_thread->sp, RT_NAME_MAX, from_thread->name, from_thread->sp)); @@ -228,8 +228,8 @@ void rt_schedule(void) { extern void rt_thread_handle_sig(rt_bool_t clean_state); - rt_hw_context_switch((rt_uint32_t)&from_thread->sp, - (rt_uint32_t)&to_thread->sp); + rt_hw_context_switch((rt_ubase_t)&from_thread->sp, + (rt_ubase_t)&to_thread->sp); /* enable interrupt */ rt_hw_interrupt_enable(level); @@ -243,8 +243,8 @@ void rt_schedule(void) { RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("switch in interrupt\n")); - rt_hw_context_switch_interrupt((rt_uint32_t)&from_thread->sp, - (rt_uint32_t)&to_thread->sp); + rt_hw_context_switch_interrupt((rt_ubase_t)&from_thread->sp, + (rt_ubase_t)&to_thread->sp); /* enable interrupt */ rt_hw_interrupt_enable(level); } diff --git a/src/thread.c b/src/thread.c index 6d79ece54bfe09eda5f8053492dbe814952c4541..82eb18942c4e5dd310ab5aeec499143f1c88562d 100644 --- a/src/thread.c +++ b/src/thread.c @@ -137,7 +137,7 @@ static rt_err_t _rt_thread_init(struct rt_thread *thread, /* init thread stack */ rt_memset(thread->stack_addr, '#', thread->stack_size); thread->sp = (void *)rt_hw_stack_init(thread->entry, thread->parameter, - (void *)((char *)thread->stack_addr + thread->stack_size - 4), + (void *)((char *)thread->stack_addr + thread->stack_size - sizeof(rt_ubase_t)), (void *)rt_thread_exit); /* priority init */