36#define RT_HW_INCLUDE_CPUPORT
39#define DBG_TAG "kernel.service"
41#define DBG_LVL DBG_LOG
43#define DBG_LVL DBG_WARNING
53#include <lwp_user_mm.h>
61#if defined(RT_USING_DEVICE) && defined(RT_USING_CONSOLE)
68 LOG_W(
"rt_hw_us_delay() doesn't support for this board."
69 "Please consider implementing rt_hw_us_delay() in another file.");
74 LOG_W(
"rt_hw_cpu_reset() doesn't support for this board."
75 "Please consider implementing rt_hw_cpu_reset() in another file.");
81 LOG_I(
"CPU shutdown...");
82 LOG_W(
"Using default rt_hw_cpu_shutdown()."
83 "Please consider implementing rt_hw_cpu_shutdown() in another file.");
92#ifndef RT_HW_BACKTRACE_FRAME_GET_SELF
95 #define RT_HW_BACKTRACE_FRAME_GET_SELF(frame) do { \
96 (frame)->fp = (rt_uintptr_t)__builtin_frame_address(0U); \
97 (frame)->pc = ({__label__ pc; pc: (rt_uintptr_t)&&pc;}); \
101 #define RT_HW_BACKTRACE_FRAME_GET_SELF(frame) do { \
122 LOG_W(
"%s is not implemented", __func__);
138 LOG_W(
"%s is not implemented", __func__);
153#if defined(RT_USING_SMART)
154 rt_kprintf(
"- RT - Thread Smart Operating System\n");
155#elif defined(RT_USING_NANO)
156 rt_kprintf(
"- RT - Thread Nano Operating System\n");
158 rt_kprintf(
"- RT - Thread Operating System\n");
162 rt_kprintf(
" 2006 - 2024 Copyright by RT-Thread team\n");
166#ifdef RT_USING_CONSOLE
167#ifdef RT_USING_DEVICE
175 return _console_device;
193 if (new_device !=
RT_NULL && new_device != old_device)
203 _console_device = new_device;
218#ifdef RT_USING_THREADSAFE_PRINTF
232static volatile int _pr_curr_user_nested;
236 return _pr_curr_user;
239static void _console_take(
void)
246 while (_pr_curr_user != self_thread)
253 _pr_critical_level = _syscon_lock.critical_level;
254 _syscon_lock.critical_level = critical_level;
256 _pr_curr_user = self_thread;
267 _pr_curr_user_nested++;
272static void _console_release(
void)
280 _pr_curr_user_nested--;
281 if (!_pr_curr_user_nested)
287 _syscon_lock.critical_level = _pr_critical_level;
295#define CONSOLE_TAKE _console_take()
296#define CONSOLE_RELEASE _console_release()
297#define PRINTF_BUFFER_TAKE rt_ubase_t level = rt_spin_lock_irqsave(&_prbuf_lock)
298#define PRINTF_BUFFER_RELEASE rt_spin_unlock_irqrestore(&_prbuf_lock, level)
302#define CONSOLE_RELEASE
303#define PRINTF_BUFFER_TAKE
304#define PRINTF_BUFFER_RELEASE
312static void _kputs(
const char *str,
long len)
314#ifdef RT_USING_DEVICE
315 rt_device_t console_device = rt_console_get_device();
320#ifdef RT_USING_DEVICE
349 _kputs(str, rt_strlen(str));
363 static char rt_log_buf[RT_CONSOLEBUF_SIZE];
373 length = rt_vsnprintf(rt_log_buf,
sizeof(rt_log_buf) - 1, fmt, args);
374 if (length > RT_CONSOLEBUF_SIZE - 1)
376 length = RT_CONSOLEBUF_SIZE - 1;
379 _kputs(rt_log_buf, length);
381 PRINTF_BUFFER_RELEASE;
420 rt_kprintf(
"please use: addr2line -e rtthread.elf -a -f\n");
422 while (nesting < RT_BACKTRACE_LEVEL_MAX_NR)
444 rt_kprintf(
"please use: addr2line -e rtthread.elf -a -f\n");
446 for (
rt_size_t i = 0; i < buflen && buffer[i] != 0; i++)
495 }
while (skip-- > 0);
497 while (nesting < buflen)
507 if (nesting < buflen)
538#if defined(RT_USING_LIBC) && defined(RT_USING_FINSH)
541static void cmd_backtrace(
int argc,
char** argv)
551 "[HELP] You can use commands like: backtrace %p\n"
552 "Printing backtrace of calling stack...\n",
559 rt_kprintf(
"please use: backtrace [thread_address]\n");
564 pid = strtoul(argv[1], &end_ptr, 0);
565 if (end_ptr == argv[1])
584#if defined(RT_USING_HEAP) && !defined(RT_USING_USERHEAP)
586static void (*rt_malloc_hook)(
void **ptr,
rt_size_t size);
587static void (*rt_realloc_entry_hook)(
void **ptr,
rt_size_t size);
588static void (*rt_realloc_exit_hook)(
void **ptr,
rt_size_t size);
589static void (*rt_free_hook)(
void **ptr);
604 rt_malloc_hook = hook;
615 rt_realloc_entry_hook = hook;
626 rt_realloc_exit_hook = hook;
644#if defined(RT_USING_HEAP_ISR)
646#elif defined(RT_USING_MUTEX)
652#if defined(RT_USING_HEAP_ISR)
654#elif defined(RT_USING_MUTEX)
661#if defined(RT_USING_HEAP_ISR)
663#elif defined(RT_USING_MUTEX)
676#if defined(RT_USING_HEAP_ISR)
678#elif defined(RT_USING_MUTEX)
687#ifdef RT_USING_UTESTCASES
690#define rt_heap_lock() _heap_lock()
691#define rt_heap_unlock() _heap_unlock()
698#if defined(RT_USING_SMALL_MEM_AS_HEAP)
700rt_inline
void _smem_info(
rt_size_t *total,
704 *total = system_heap->
total;
706 *used = system_heap->
used;
708 *max_used = system_heap->
max;
710#define _MEM_INIT(_name, _start, _size) \
711 system_heap = rt_smem_init(_name, _start, _size)
712#define _MEM_MALLOC(_size) \
713 rt_smem_alloc(system_heap, _size)
714#define _MEM_REALLOC(_ptr, _newsize)\
715 rt_smem_realloc(system_heap, _ptr, _newsize)
716#define _MEM_FREE(_ptr) \
718#define _MEM_INFO(_total, _used, _max) \
719 _smem_info(_total, _used, _max)
720#elif defined(RT_USING_MEMHEAP_AS_HEAP)
721static struct rt_memheap system_heap;
722void *_memheap_alloc(
struct rt_memheap *heap,
rt_size_t size);
723void _memheap_free(
void *rmem);
724void *_memheap_realloc(
struct rt_memheap *heap,
void *rmem,
rt_size_t newsize);
725#define _MEM_INIT(_name, _start, _size) \
727 rt_memheap_init(&system_heap, _name, _start, _size); \
728 system_heap.locked = RT_TRUE; \
730#define _MEM_MALLOC(_size) \
731 _memheap_alloc(&system_heap, _size)
732#define _MEM_REALLOC(_ptr, _newsize) \
733 _memheap_realloc(&system_heap, _ptr, _newsize)
734#define _MEM_FREE(_ptr) \
736#define _MEM_INFO(_total, _used, _max) \
737 rt_memheap_info(&system_heap, _total, _used, _max)
738#elif defined(RT_USING_SLAB_AS_HEAP)
739static rt_slab_t system_heap;
740rt_inline
void _slab_info(
rt_size_t *total,
744 *total = system_heap->
total;
746 *used = system_heap->used;
748 *max_used = system_heap->max;
750#define _MEM_INIT(_name, _start, _size) \
751 system_heap = rt_slab_init(_name, _start, _size)
752#define _MEM_MALLOC(_size) \
753 rt_slab_alloc(system_heap, _size)
754#define _MEM_REALLOC(_ptr, _newsize) \
755 rt_slab_realloc(system_heap, _ptr, _newsize)
756#define _MEM_FREE(_ptr) \
757 rt_slab_free(system_heap, _ptr)
758#define _MEM_INFO _slab_info
760#define _MEM_INIT(...)
761#define _MEM_MALLOC(...) RT_NULL
762#define _MEM_REALLOC(...) RT_NULL
763#define _MEM_FREE(...)
764#define _MEM_INFO(...)
782 _MEM_INIT(
"heap", (
void *)begin_align, end_align - begin_align);
874 rt_memset(p, 0, count * size);
926#if defined(RT_USING_SLAB) && defined(RT_USING_SLAB_AS_HEAP)
935 ptr = rt_slab_page_alloc(system_heap, npages);
941void rt_page_free(
void *addr,
rt_size_t npages)
948 rt_slab_page_free(system_heap, addr, npages);
969 int uintptr_size = 0;
973 uintptr_size =
sizeof(
void*);
977 align = ((align + uintptr_size) & ~uintptr_size);
980 align_size = ((size + uintptr_size) & ~uintptr_size) + align;
992 align_ptr = (
void *)(((
rt_uintptr_t)ptr + (align - 1)) & ~(align - 1));
1023#ifndef RT_USING_CPU_FFS
1024#ifdef RT_USING_TINY_FFS
1027 0, 1, 2, 27, 3, 24, 28, 32,
1028 4, 17, 25, 31, 29, 12, 32, 14,
1029 5, 8, 18, 32, 26, 23, 32, 16,
1030 30, 11, 13, 7, 32, 22, 15, 10,
1053 0, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
1054 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
1055 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
1056 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
1057 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
1058 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
1059 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
1060 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
1061 7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
1062 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
1063 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
1064 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
1065 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
1066 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
1067 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
1068 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0
1100 if (value & 0xff0000)
1110#ifdef RT_DEBUGING_ASSERT
1113void (*rt_assert_hook)(
const char *ex,
const char *func,
rt_size_t line);
1120void rt_assert_set_hook(
void (*hook)(
const char *ex,
const char *func,
rt_size_t line))
1122 rt_assert_hook = hook;
1134void rt_assert_handler(
const char *ex_string,
const char *func,
rt_size_t line)
1136 volatile char dummy = 0;
1138 if (rt_assert_hook ==
RT_NULL)
1140#ifdef RT_USING_MODULE
1141 if (dlmodule_self())
1149 rt_kprintf(
"(%s) assertion failed at function:%s, line number:%d \n", ex_string, func, line);
1156 rt_assert_hook(ex_string, func, line);
#define RT_ALIGN(size, align)
#define RT_ALIGN_DOWN(size, align)
#define RT_DEVICE_FLAG_STREAM
rt_ssize_t rt_device_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
rt_device_t rt_device_find(const char *name)
struct rt_device * rt_device_t
rt_err_t rt_device_close(rt_device_t dev)
rt_err_t rt_device_open(rt_device_t dev, rt_uint16_t oflag)
#define RT_DEVICE_OFLAG_RDWR
void rt_realloc_set_exit_hook(void(*hook)(void **ptr, rt_size_t size))
This function will set a hook function, which will be invoked when a memory block is allocated from h...
void rt_free_sethook(void(*hook)(void **ptr))
This function will set a hook function, which will be invoked when a memory block is released to heap...
void rt_realloc_set_entry_hook(void(*hook)(void **ptr, rt_size_t size))
This function will set a hook function, which will be invoked when a memory block is allocated from h...
void rt_malloc_sethook(void(*hook)(void **ptr, rt_size_t size))
This function will set a hook function, which will be invoked when a memory block is allocated from h...
rt_base_t rt_spin_lock_irqsave(struct rt_spinlock *lock)
This function will disable the local interrupt and then lock the spinlock, will lock the thread sched...
#define RT_WAITING_FOREVER
void rt_spin_lock_init(struct rt_spinlock *lock)
Initialize a static spinlock object.
void rt_spin_unlock_irqrestore(struct rt_spinlock *lock, rt_base_t level)
This function will unlock the spinlock and then restore current cpu interrupt status,...
#define RT_OBJECT_HOOK_CALL(func, argv)
rt_uint8_t rt_object_get_type(rt_object_t object)
This function will return the type of object without RT_Object_Class_Static flag.
rt_weak void * rt_calloc(rt_size_t count, rt_size_t size)
This function will contiguously allocate enough space for count objects that are size bytes of memory...
rt_inline rt_base_t _heap_lock(void)
rt_weak void rt_hw_cpu_shutdown(void)
void rt_show_version(void)
This function will show the version of rt-thread rtos
#define RT_HW_BACKTRACE_FRAME_GET_SELF(frame)
rt_weak rt_err_t rt_hw_backtrace_frame_unwind(rt_thread_t thread, struct rt_hw_backtrace_frame *frame)
Unwind the target frame
rt_weak const char * rt_hw_cpu_arch(void)
rt_weak void rt_free_align(void *ptr)
This function release the memory block, which is allocated by rt_malloc_align function and address is...
rt_weak void rt_system_heap_init(void *begin_addr, void *end_addr)
This function will init system heap. User can override this API to complete other works,...
rt_weak void * rt_malloc_align(rt_size_t size, rt_size_t align)
This function allocates a memory block, which address is aligned to the specified alignment size.
rt_weak rt_err_t rt_backtrace_to_buffer(rt_thread_t thread, struct rt_hw_backtrace_frame *frame, long skip, rt_ubase_t *buffer, long buflen)
Print backtrace from frame to the given buffer
rt_weak rt_err_t rt_hw_backtrace_frame_get(rt_thread_t thread, struct rt_hw_backtrace_frame *frame)
Get the inner most frame of target thread
const rt_uint8_t __lowest_bit_bitmap[]
rt_weak void * rt_realloc(void *ptr, rt_size_t newsize)
This function will change the size of previously allocated memory block.
rt_weak rt_err_t rt_backtrace_formatted_print(rt_ubase_t *buffer, long buflen)
Print backtrace from buffer to system console
rt_weak rt_err_t rt_backtrace_frame(rt_thread_t thread, struct rt_hw_backtrace_frame *frame)
Print backtrace from frame to system console device
rt_inline void _heap_unlock(rt_base_t level)
rt_weak void rt_hw_us_delay(rt_uint32_t us)
rt_weak void rt_free(void *ptr)
This function will release the previously allocated memory block by rt_malloc. The released memory bl...
rt_weak void rt_memory_info(rt_size_t *total, rt_size_t *used, rt_size_t *max_used)
This function will caculate the total memory, the used memory, and the max used memory.
rt_weak void rt_hw_cpu_reset(void)
rt_weak void * rt_malloc(rt_size_t size)
Allocate a block of memory with a minimum of 'size' bytes.
rt_inline void _heap_lock_init(void)
int __rt_ffs(int value)
This function finds the first bit set (beginning with the least significant bit) in value and return ...
void rt_system_heap_init_generic(void *begin_addr, void *end_addr)
This function will do the generic system heap initialization.
rt_weak rt_err_t rt_backtrace(void)
Print backtrace of current thread to system console device
rt_err_t rt_backtrace_thread(rt_thread_t thread)
Print backtrace of a thread to system console device
#define _MEM_REALLOC(...)
void rt_exit_critical_safe(rt_base_t critical_level)
void rt_exit_critical(void)
This function will unlock the thread scheduler.
rt_err_t rt_thread_yield(void)
This function will let current thread yield processor, and scheduler will choose the highest thread t...
rt_thread_t rt_thread_self(void)
This function will return self thread object.
rt_base_t rt_enter_critical(void)
This function will lock the thread scheduler.
struct rt_thread * rt_thread_t
#define MSH_CMD_EXPORT_ALIAS(...)
rt_err_t rt_mutex_take(rt_mutex_t mutex, rt_int32_t time)
rt_err_t rt_mutex_init(rt_mutex_t mutex, const char *name, rt_uint8_t flag)
Initialize a static mutex object.
rt_err_t rt_mutex_release(rt_mutex_t mutex)
This function will release a mutex. If there is thread suspended on the mutex, the thread will be res...
void rt_hw_console_output(const char *str)
rt_base_t rt_hw_interrupt_disable(void)
#define RTM_EXPORT(symbol)
struct usb_os_header_comp_id_descriptor __attribute__