kservice.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  1. /*
  2. * Copyright (c) 2006-2024, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2006-03-16 Bernard the first version
  9. * 2006-05-25 Bernard rewrite vsprintf
  10. * 2006-08-10 Bernard add rt_show_version
  11. * 2010-03-17 Bernard remove rt_strlcpy function
  12. * fix gcc compiling issue.
  13. * 2010-04-15 Bernard remove weak definition on ICCM16C compiler
  14. * 2012-07-18 Arda add the alignment display for signed integer
  15. * 2012-11-23 Bernard fix IAR compiler error.
  16. * 2012-12-22 Bernard fix rt_kprintf issue, which found by Grissiom.
  17. * 2013-06-24 Bernard remove rt_kprintf if RT_USING_CONSOLE is not defined.
  18. * 2013-09-24 aozima make sure the device is in STREAM mode when used by rt_kprintf.
  19. * 2015-07-06 Bernard Add rt_assert_handler routine.
  20. * 2021-02-28 Meco Man add RT_KSERVICE_USING_STDLIB
  21. * 2021-12-20 Meco Man implement rt_strcpy()
  22. * 2022-01-07 Gabriel add __on_rt_assert_hook
  23. * 2022-06-04 Meco Man remove strnlen
  24. * 2022-08-24 Yunjie make rt_memset word-independent to adapt to ti c28x (16bit word)
  25. * 2022-08-30 Yunjie make rt_vsnprintf adapt to ti c28x (16bit int)
  26. * 2023-02-02 Bernard add Smart ID for logo version show
  27. * 2023-10-16 Shell Add hook point for rt_malloc services
  28. * 2023-12-10 xqyjlj perf rt_hw_interrupt_disable/enable, fix memheap lock
  29. * 2024-03-10 Meco Man move std libc related functions to rtklibc
  30. */
  31. #include <rtthread.h>
  32. #include <rthw.h>
  33. #define DBG_TAG "kernel.service"
  34. #ifdef RT_DEBUG_DEVICE
  35. #define DBG_LVL DBG_LOG
  36. #else
  37. #define DBG_LVL DBG_WARNING
  38. #endif /* defined (RT_DEBUG_DEVICE) */
  39. #include <rtdbg.h>
  40. #ifdef RT_USING_MODULE
  41. #include <dlmodule.h>
  42. #endif /* RT_USING_MODULE */
  43. #ifdef RT_USING_SMART
  44. #include <lwp.h>
  45. #include <lwp_user_mm.h>
  46. #endif
  47. /**
  48. * @addtogroup KernelService
  49. * @{
  50. */
  51. #if defined(RT_USING_DEVICE) && defined(RT_USING_CONSOLE)
  52. static rt_device_t _console_device = RT_NULL;
  53. #endif
  54. rt_weak void rt_hw_us_delay(rt_uint32_t us)
  55. {
  56. (void) us;
  57. LOG_W("rt_hw_us_delay() doesn't support for this board."
  58. "Please consider implementing rt_hw_us_delay() in another file.");
  59. }
  60. rt_weak void rt_hw_cpu_reset(void)
  61. {
  62. LOG_W("rt_hw_cpu_reset() doesn't support for this board."
  63. "Please consider implementing rt_hw_cpu_reset() in another file.");
  64. return;
  65. }
  66. rt_weak void rt_hw_cpu_shutdown(void)
  67. {
  68. rt_base_t level;
  69. LOG_I("CPU shutdown...");
  70. LOG_W("Using default rt_hw_cpu_shutdown()."
  71. "Please consider implementing rt_hw_cpu_reset() in another file.");
  72. level = rt_hw_interrupt_disable();
  73. while (level)
  74. {
  75. RT_ASSERT(RT_NULL);
  76. }
  77. return;
  78. }
  79. rt_weak rt_err_t rt_hw_backtrace_frame_get(rt_thread_t thread, struct rt_hw_backtrace_frame *frame)
  80. {
  81. RT_UNUSED(thread);
  82. RT_UNUSED(frame);
  83. LOG_W("%s is not implemented", __func__);
  84. return -RT_ENOSYS;
  85. }
  86. rt_weak rt_err_t rt_hw_backtrace_frame_unwind(rt_thread_t thread, struct rt_hw_backtrace_frame *frame)
  87. {
  88. RT_UNUSED(thread);
  89. RT_UNUSED(frame);
  90. LOG_W("%s is not implemented", __func__);
  91. return -RT_ENOSYS;
  92. }
  93. rt_weak const char *rt_hw_cpu_arch(void)
  94. {
  95. return "unknown";
  96. }
  97. /**
  98. * @brief This function will show the version of rt-thread rtos
  99. */
  100. void rt_show_version(void)
  101. {
  102. rt_kprintf("\n \\ | /\n");
  103. #if defined(RT_USING_SMART)
  104. rt_kprintf("- RT - Thread Smart Operating System\n");
  105. #elif defined(RT_USING_NANO)
  106. rt_kprintf("- RT - Thread Nano Operating System\n");
  107. #else
  108. rt_kprintf("- RT - Thread Operating System\n");
  109. #endif
  110. rt_kprintf(" / | \\ %d.%d.%d build %s %s\n",
  111. (rt_int32_t)RT_VERSION_MAJOR, (rt_int32_t)RT_VERSION_MINOR, (rt_int32_t)RT_VERSION_PATCH, __DATE__, __TIME__);
  112. rt_kprintf(" 2006 - 2024 Copyright by RT-Thread team\n");
  113. }
  114. RTM_EXPORT(rt_show_version);
  115. #ifdef RT_USING_CONSOLE
  116. #ifdef RT_USING_DEVICE
  117. /**
  118. * @brief This function returns the device using in console.
  119. *
  120. * @return Returns the console device pointer or RT_NULL.
  121. */
  122. rt_device_t rt_console_get_device(void)
  123. {
  124. return _console_device;
  125. }
  126. RTM_EXPORT(rt_console_get_device);
  127. /**
  128. * @brief This function will set a device as console device.
  129. * After set a device to console, all output of rt_kprintf will be
  130. * redirected to this new device.
  131. *
  132. * @param name is the name of new console device.
  133. *
  134. * @return the old console device handler on successful, or RT_NULL on failure.
  135. */
  136. rt_device_t rt_console_set_device(const char *name)
  137. {
  138. rt_device_t new_device, old_device;
  139. /* save old device */
  140. old_device = _console_device;
  141. /* find new console device */
  142. new_device = rt_device_find(name);
  143. /* check whether it's a same device */
  144. if (new_device == old_device) return RT_NULL;
  145. if (new_device != RT_NULL)
  146. {
  147. if (_console_device != RT_NULL)
  148. {
  149. /* close old console device */
  150. rt_device_close(_console_device);
  151. }
  152. /* set new console device */
  153. rt_device_open(new_device, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_STREAM);
  154. _console_device = new_device;
  155. }
  156. return old_device;
  157. }
  158. RTM_EXPORT(rt_console_set_device);
  159. #endif /* RT_USING_DEVICE */
  160. rt_weak void rt_hw_console_output(const char *str)
  161. {
  162. /* empty console output */
  163. RT_UNUSED(str);
  164. }
  165. RTM_EXPORT(rt_hw_console_output);
  166. #ifdef RT_USING_THREADSAFE_PRINTF
  167. /* system console lock */
  168. static struct rt_spinlock _syscon_lock = RT_SPINLOCK_INIT;
  169. /* lock of kprintf buffer */
  170. static struct rt_spinlock _prbuf_lock = RT_SPINLOCK_INIT;
  171. /* current user of system console */
  172. static rt_thread_t _pr_curr_user;
  173. #ifdef RT_USING_DEBUG
  174. static rt_base_t _pr_critical_level;
  175. #endif /* RT_USING_DEBUG */
  176. /* nested level of current user */
  177. static volatile int _pr_curr_user_nested;
  178. rt_thread_t rt_console_current_user(void)
  179. {
  180. return _pr_curr_user;
  181. }
  182. static void _console_take(void)
  183. {
  184. rt_ubase_t level = rt_spin_lock_irqsave(&_syscon_lock);
  185. rt_thread_t self_thread = rt_thread_self();
  186. rt_base_t critical_level;
  187. RT_UNUSED(critical_level);
  188. while (_pr_curr_user != self_thread)
  189. {
  190. if (_pr_curr_user == RT_NULL)
  191. {
  192. /* no preemption is allowed to avoid dead lock */
  193. critical_level = rt_enter_critical();
  194. #ifdef RT_USING_DEBUG
  195. _pr_critical_level = _syscon_lock.critical_level;
  196. _syscon_lock.critical_level = critical_level;
  197. #endif
  198. _pr_curr_user = self_thread;
  199. break;
  200. }
  201. else
  202. {
  203. rt_spin_unlock_irqrestore(&_syscon_lock, level);
  204. rt_thread_yield();
  205. level = rt_spin_lock_irqsave(&_syscon_lock);
  206. }
  207. }
  208. _pr_curr_user_nested++;
  209. rt_spin_unlock_irqrestore(&_syscon_lock, level);
  210. }
  211. static void _console_release(void)
  212. {
  213. rt_ubase_t level = rt_spin_lock_irqsave(&_syscon_lock);
  214. rt_thread_t self_thread = rt_thread_self();
  215. RT_UNUSED(self_thread);
  216. RT_ASSERT(_pr_curr_user == self_thread);
  217. _pr_curr_user_nested--;
  218. if (!_pr_curr_user_nested)
  219. {
  220. _pr_curr_user = RT_NULL;
  221. #ifdef RT_USING_DEBUG
  222. rt_exit_critical_safe(_syscon_lock.critical_level);
  223. _syscon_lock.critical_level = _pr_critical_level;
  224. #else
  225. rt_exit_critical();
  226. #endif
  227. }
  228. rt_spin_unlock_irqrestore(&_syscon_lock, level);
  229. }
  230. #define CONSOLE_TAKE _console_take()
  231. #define CONSOLE_RELEASE _console_release()
  232. #define PRINTF_BUFFER_TAKE rt_ubase_t level = rt_spin_lock_irqsave(&_prbuf_lock)
  233. #define PRINTF_BUFFER_RELEASE rt_spin_unlock_irqrestore(&_prbuf_lock, level)
  234. #else
  235. #define CONSOLE_TAKE
  236. #define CONSOLE_RELEASE
  237. #define PRINTF_BUFFER_TAKE
  238. #define PRINTF_BUFFER_RELEASE
  239. #endif /* RT_USING_THREADSAFE_PRINTF */
  240. /**
  241. * @brief This function will put string to the console.
  242. *
  243. * @param str is the string output to the console.
  244. */
  245. static void _kputs(const char *str, long len)
  246. {
  247. RT_UNUSED(len);
  248. CONSOLE_TAKE;
  249. #ifdef RT_USING_DEVICE
  250. if (_console_device == RT_NULL)
  251. {
  252. rt_hw_console_output(str);
  253. }
  254. else
  255. {
  256. rt_device_write(_console_device, 0, str, len);
  257. }
  258. #else
  259. rt_hw_console_output(str);
  260. #endif /* RT_USING_DEVICE */
  261. CONSOLE_RELEASE;
  262. }
  263. /**
  264. * @brief This function will put string to the console.
  265. *
  266. * @param str is the string output to the console.
  267. */
  268. void rt_kputs(const char *str)
  269. {
  270. if (!str)
  271. {
  272. return;
  273. }
  274. _kputs(str, rt_strlen(str));
  275. }
  276. /**
  277. * @brief This function will print a formatted string on system console.
  278. *
  279. * @param fmt is the format parameters.
  280. *
  281. * @return The number of characters actually written to buffer.
  282. */
  283. rt_weak int rt_kprintf(const char *fmt, ...)
  284. {
  285. va_list args;
  286. rt_size_t length = 0;
  287. static char rt_log_buf[RT_CONSOLEBUF_SIZE];
  288. va_start(args, fmt);
  289. PRINTF_BUFFER_TAKE;
  290. /* the return value of vsnprintf is the number of bytes that would be
  291. * written to buffer had if the size of the buffer been sufficiently
  292. * large excluding the terminating null byte. If the output string
  293. * would be larger than the rt_log_buf, we have to adjust the output
  294. * length. */
  295. length = rt_vsnprintf(rt_log_buf, sizeof(rt_log_buf) - 1, fmt, args);
  296. if (length > RT_CONSOLEBUF_SIZE - 1)
  297. {
  298. length = RT_CONSOLEBUF_SIZE - 1;
  299. }
  300. _kputs(rt_log_buf, length);
  301. PRINTF_BUFFER_RELEASE;
  302. va_end(args);
  303. return length;
  304. }
  305. RTM_EXPORT(rt_kprintf);
  306. #endif /* RT_USING_CONSOLE */
  307. #ifdef __GNUC__
  308. rt_weak rt_err_t rt_backtrace(void)
  309. {
  310. struct rt_hw_backtrace_frame frame = {
  311. .fp = (rt_base_t)__builtin_frame_address(0U),
  312. .pc = ({__label__ pc; pc: (rt_base_t)&&pc;})
  313. };
  314. rt_hw_backtrace_frame_unwind(rt_thread_self(), &frame);
  315. return rt_backtrace_frame(&frame);
  316. }
  317. #else /* otherwise not implemented */
  318. rt_weak rt_err_t rt_backtrace(void)
  319. {
  320. /* LOG_W cannot work under this environment */
  321. rt_kprintf("%s is not implemented\n", __func__);
  322. return -RT_ENOSYS;
  323. }
  324. #endif
  325. rt_err_t rt_backtrace_frame(struct rt_hw_backtrace_frame *frame)
  326. {
  327. long nesting = 0;
  328. rt_kprintf("please use: addr2line -e rtthread.elf -a -f");
  329. while (nesting < RT_BACKTRACE_LEVEL_MAX_NR)
  330. {
  331. rt_kprintf(" 0x%lx", (rt_ubase_t)frame->pc);
  332. if (rt_hw_backtrace_frame_unwind(rt_thread_self(), frame))
  333. {
  334. break;
  335. }
  336. nesting++;
  337. }
  338. rt_kprintf("\n");
  339. return RT_EOK;
  340. }
  341. rt_err_t rt_backtrace_thread(rt_thread_t thread)
  342. {
  343. rt_err_t rc;
  344. struct rt_hw_backtrace_frame frame;
  345. if (thread)
  346. {
  347. rc = rt_hw_backtrace_frame_get(thread, &frame);
  348. if (rc == RT_EOK)
  349. {
  350. rc = rt_backtrace_frame(&frame);
  351. }
  352. }
  353. else
  354. {
  355. rc = -RT_EINVAL;
  356. }
  357. return rc;
  358. }
  359. #if defined(RT_USING_LIBC) && defined(RT_USING_FINSH)
  360. #include <stdlib.h> /* for string service */
  361. static void cmd_backtrace(int argc, char** argv)
  362. {
  363. rt_ubase_t pid;
  364. char *end_ptr;
  365. if (argc != 2)
  366. {
  367. if (argc == 1)
  368. {
  369. rt_kprintf("[INFO] No thread specified\n"
  370. "[HELP] You can use commands like: backtrace %p\n"
  371. "Printing backtrace of calling stack...\n",
  372. rt_thread_self());
  373. rt_backtrace();
  374. return ;
  375. }
  376. else
  377. {
  378. rt_kprintf("please use: backtrace [thread_address]\n");
  379. return;
  380. }
  381. }
  382. pid = strtoul(argv[1], &end_ptr, 0);
  383. if (end_ptr == argv[1])
  384. {
  385. rt_kprintf("Invalid input: %s\n", argv[1]);
  386. return ;
  387. }
  388. if (pid && rt_object_get_type((void *)pid) == RT_Object_Class_Thread)
  389. {
  390. rt_thread_t target = (rt_thread_t)pid;
  391. rt_kprintf("backtrace %s(0x%lx), from %s\n", target->parent.name, pid, argv[1]);
  392. rt_backtrace_thread(target);
  393. }
  394. else
  395. rt_kprintf("Invalid pid: %ld\n", pid);
  396. }
  397. MSH_CMD_EXPORT_ALIAS(cmd_backtrace, backtrace, print backtrace of a thread);
  398. #endif /* RT_USING_LIBC */
  399. #if defined(RT_USING_HEAP) && !defined(RT_USING_USERHEAP)
  400. #ifdef RT_USING_HOOK
  401. static void (*rt_malloc_hook)(void **ptr, rt_size_t size);
  402. static void (*rt_realloc_entry_hook)(void **ptr, rt_size_t size);
  403. static void (*rt_realloc_exit_hook)(void **ptr, rt_size_t size);
  404. static void (*rt_free_hook)(void **ptr);
  405. /**
  406. * @addtogroup Hook
  407. * @{
  408. */
  409. /**
  410. * @brief This function will set a hook function, which will be invoked when a memory
  411. * block is allocated from heap memory.
  412. *
  413. * @param hook the hook function.
  414. */
  415. void rt_malloc_sethook(void (*hook)(void **ptr, rt_size_t size))
  416. {
  417. rt_malloc_hook = hook;
  418. }
  419. /**
  420. * @brief This function will set a hook function, which will be invoked when a memory
  421. * block is allocated from heap memory.
  422. *
  423. * @param hook the hook function.
  424. */
  425. void rt_realloc_set_entry_hook(void (*hook)(void **ptr, rt_size_t size))
  426. {
  427. rt_realloc_entry_hook = hook;
  428. }
  429. /**
  430. * @brief This function will set a hook function, which will be invoked when a memory
  431. * block is allocated from heap memory.
  432. *
  433. * @param hook the hook function.
  434. */
  435. void rt_realloc_set_exit_hook(void (*hook)(void **ptr, rt_size_t size))
  436. {
  437. rt_realloc_exit_hook = hook;
  438. }
  439. /**
  440. * @brief This function will set a hook function, which will be invoked when a memory
  441. * block is released to heap memory.
  442. *
  443. * @param hook the hook function
  444. */
  445. void rt_free_sethook(void (*hook)(void **ptr))
  446. {
  447. rt_free_hook = hook;
  448. }
  449. /**@}*/
  450. #endif /* RT_USING_HOOK */
  451. #if defined(RT_USING_HEAP_ISR)
  452. static struct rt_spinlock _heap_spinlock;
  453. #elif defined(RT_USING_MUTEX)
  454. static struct rt_mutex _lock;
  455. #endif
  456. rt_inline void _heap_lock_init(void)
  457. {
  458. #if defined(RT_USING_HEAP_ISR)
  459. rt_spin_lock_init(&_heap_spinlock);
  460. #elif defined(RT_USING_MUTEX)
  461. rt_mutex_init(&_lock, "heap", RT_IPC_FLAG_PRIO);
  462. #endif
  463. }
  464. rt_inline rt_base_t _heap_lock(void)
  465. {
  466. #if defined(RT_USING_HEAP_ISR)
  467. return rt_spin_lock_irqsave(&_heap_spinlock);
  468. #elif defined(RT_USING_MUTEX)
  469. if (rt_thread_self())
  470. return rt_mutex_take(&_lock, RT_WAITING_FOREVER);
  471. else
  472. return RT_EOK;
  473. #else
  474. rt_enter_critical();
  475. return RT_EOK;
  476. #endif
  477. }
  478. rt_inline void _heap_unlock(rt_base_t level)
  479. {
  480. #if defined(RT_USING_HEAP_ISR)
  481. rt_spin_unlock_irqrestore(&_heap_spinlock, level);
  482. #elif defined(RT_USING_MUTEX)
  483. RT_ASSERT(level == RT_EOK);
  484. if (rt_thread_self())
  485. rt_mutex_release(&_lock);
  486. #else
  487. rt_exit_critical();
  488. #endif
  489. }
  490. #ifdef RT_USING_UTESTCASES
  491. /* export to utest to observe the inner statements */
  492. #ifdef _MSC_VER
  493. #define rt_heap_lock() _heap_lock()
  494. #define rt_heap_unlock() _heap_unlock()
  495. #else
  496. rt_base_t rt_heap_lock(void) __attribute__((alias("_heap_lock")));
  497. void rt_heap_unlock(rt_base_t level) __attribute__((alias("_heap_unlock")));
  498. #endif /* _MSC_VER */
  499. #endif
  500. #if defined(RT_USING_SMALL_MEM_AS_HEAP)
  501. static rt_smem_t system_heap;
  502. rt_inline void _smem_info(rt_size_t *total,
  503. rt_size_t *used, rt_size_t *max_used)
  504. {
  505. if (total)
  506. *total = system_heap->total;
  507. if (used)
  508. *used = system_heap->used;
  509. if (max_used)
  510. *max_used = system_heap->max;
  511. }
  512. #define _MEM_INIT(_name, _start, _size) \
  513. system_heap = rt_smem_init(_name, _start, _size)
  514. #define _MEM_MALLOC(_size) \
  515. rt_smem_alloc(system_heap, _size)
  516. #define _MEM_REALLOC(_ptr, _newsize)\
  517. rt_smem_realloc(system_heap, _ptr, _newsize)
  518. #define _MEM_FREE(_ptr) \
  519. rt_smem_free(_ptr)
  520. #define _MEM_INFO(_total, _used, _max) \
  521. _smem_info(_total, _used, _max)
  522. #elif defined(RT_USING_MEMHEAP_AS_HEAP)
  523. static struct rt_memheap system_heap;
  524. void *_memheap_alloc(struct rt_memheap *heap, rt_size_t size);
  525. void _memheap_free(void *rmem);
  526. void *_memheap_realloc(struct rt_memheap *heap, void *rmem, rt_size_t newsize);
  527. #define _MEM_INIT(_name, _start, _size) \
  528. do {\
  529. rt_memheap_init(&system_heap, _name, _start, _size); \
  530. system_heap.locked = RT_TRUE; \
  531. } while(0)
  532. #define _MEM_MALLOC(_size) \
  533. _memheap_alloc(&system_heap, _size)
  534. #define _MEM_REALLOC(_ptr, _newsize) \
  535. _memheap_realloc(&system_heap, _ptr, _newsize)
  536. #define _MEM_FREE(_ptr) \
  537. _memheap_free(_ptr)
  538. #define _MEM_INFO(_total, _used, _max) \
  539. rt_memheap_info(&system_heap, _total, _used, _max)
  540. #elif defined(RT_USING_SLAB_AS_HEAP)
  541. static rt_slab_t system_heap;
  542. rt_inline void _slab_info(rt_size_t *total,
  543. rt_size_t *used, rt_size_t *max_used)
  544. {
  545. if (total)
  546. *total = system_heap->total;
  547. if (used)
  548. *used = system_heap->used;
  549. if (max_used)
  550. *max_used = system_heap->max;
  551. }
  552. #define _MEM_INIT(_name, _start, _size) \
  553. system_heap = rt_slab_init(_name, _start, _size)
  554. #define _MEM_MALLOC(_size) \
  555. rt_slab_alloc(system_heap, _size)
  556. #define _MEM_REALLOC(_ptr, _newsize) \
  557. rt_slab_realloc(system_heap, _ptr, _newsize)
  558. #define _MEM_FREE(_ptr) \
  559. rt_slab_free(system_heap, _ptr)
  560. #define _MEM_INFO _slab_info
  561. #else
  562. #define _MEM_INIT(...)
  563. #define _MEM_MALLOC(...) RT_NULL
  564. #define _MEM_REALLOC(...) RT_NULL
  565. #define _MEM_FREE(...)
  566. #define _MEM_INFO(...)
  567. #endif
  568. static void _rt_system_heap_init(void *begin_addr, void *end_addr)
  569. {
  570. rt_ubase_t begin_align = RT_ALIGN((rt_ubase_t)begin_addr, RT_ALIGN_SIZE);
  571. rt_ubase_t end_align = RT_ALIGN_DOWN((rt_ubase_t)end_addr, RT_ALIGN_SIZE);
  572. RT_ASSERT(end_align > begin_align);
  573. /* Initialize system memory heap */
  574. _MEM_INIT("heap", (void *)begin_align, end_align - begin_align);
  575. /* Initialize multi thread contention lock */
  576. _heap_lock_init();
  577. }
  578. /**
  579. * @brief This function will init system heap.
  580. *
  581. * @param begin_addr the beginning address of system page.
  582. *
  583. * @param end_addr the end address of system page.
  584. */
  585. rt_weak void rt_system_heap_init(void *begin_addr, void *end_addr)
  586. {
  587. _rt_system_heap_init(begin_addr, end_addr);
  588. }
  589. /**
  590. * @brief Allocate a block of memory with a minimum of 'size' bytes.
  591. *
  592. * @param size is the minimum size of the requested block in bytes.
  593. *
  594. * @return the pointer to allocated memory or NULL if no free memory was found.
  595. */
  596. rt_weak void *rt_malloc(rt_size_t size)
  597. {
  598. rt_base_t level;
  599. void *ptr;
  600. /* Enter critical zone */
  601. level = _heap_lock();
  602. /* allocate memory block from system heap */
  603. ptr = _MEM_MALLOC(size);
  604. /* Exit critical zone */
  605. _heap_unlock(level);
  606. /* call 'rt_malloc' hook */
  607. RT_OBJECT_HOOK_CALL(rt_malloc_hook, (&ptr, size));
  608. return ptr;
  609. }
  610. RTM_EXPORT(rt_malloc);
  611. /**
  612. * @brief This function will change the size of previously allocated memory block.
  613. *
  614. * @param ptr is the pointer to memory allocated by rt_malloc.
  615. *
  616. * @param newsize is the required new size.
  617. *
  618. * @return the changed memory block address.
  619. */
  620. rt_weak void *rt_realloc(void *ptr, rt_size_t newsize)
  621. {
  622. rt_base_t level;
  623. void *nptr;
  624. /* Entry hook */
  625. RT_OBJECT_HOOK_CALL(rt_realloc_entry_hook, (&ptr, newsize));
  626. /* Enter critical zone */
  627. level = _heap_lock();
  628. /* Change the size of previously allocated memory block */
  629. nptr = _MEM_REALLOC(ptr, newsize);
  630. /* Exit critical zone */
  631. _heap_unlock(level);
  632. /* Exit hook */
  633. RT_OBJECT_HOOK_CALL(rt_realloc_exit_hook, (&nptr, newsize));
  634. return nptr;
  635. }
  636. RTM_EXPORT(rt_realloc);
  637. /**
  638. * @brief This function will contiguously allocate enough space for count objects
  639. * that are size bytes of memory each and returns a pointer to the allocated
  640. * memory.
  641. *
  642. * @note The allocated memory is filled with bytes of value zero.
  643. *
  644. * @param count is the number of objects to allocate.
  645. *
  646. * @param size is the size of one object to allocate.
  647. *
  648. * @return pointer to allocated memory / NULL pointer if there is an error.
  649. */
  650. rt_weak void *rt_calloc(rt_size_t count, rt_size_t size)
  651. {
  652. void *p;
  653. /* allocate 'count' objects of size 'size' */
  654. p = rt_malloc(count * size);
  655. /* zero the memory */
  656. if (p)
  657. {
  658. rt_memset(p, 0, count * size);
  659. }
  660. return p;
  661. }
  662. RTM_EXPORT(rt_calloc);
  663. /**
  664. * @brief This function will release the previously allocated memory block by
  665. * rt_malloc. The released memory block is taken back to system heap.
  666. *
  667. * @param ptr the address of memory which will be released.
  668. */
  669. rt_weak void rt_free(void *ptr)
  670. {
  671. rt_base_t level;
  672. /* call 'rt_free' hook */
  673. RT_OBJECT_HOOK_CALL(rt_free_hook, (&ptr));
  674. /* NULL check */
  675. if (ptr == RT_NULL) return;
  676. /* Enter critical zone */
  677. level = _heap_lock();
  678. _MEM_FREE(ptr);
  679. /* Exit critical zone */
  680. _heap_unlock(level);
  681. }
  682. RTM_EXPORT(rt_free);
  683. /**
  684. * @brief This function will caculate the total memory, the used memory, and
  685. * the max used memory.
  686. *
  687. * @param total is a pointer to get the total size of the memory.
  688. *
  689. * @param used is a pointer to get the size of memory used.
  690. *
  691. * @param max_used is a pointer to get the maximum memory used.
  692. */
  693. rt_weak void rt_memory_info(rt_size_t *total,
  694. rt_size_t *used,
  695. rt_size_t *max_used)
  696. {
  697. rt_base_t level;
  698. /* Enter critical zone */
  699. level = _heap_lock();
  700. _MEM_INFO(total, used, max_used);
  701. /* Exit critical zone */
  702. _heap_unlock(level);
  703. }
  704. RTM_EXPORT(rt_memory_info);
  705. #if defined(RT_USING_SLAB) && defined(RT_USING_SLAB_AS_HEAP)
  706. void *rt_page_alloc(rt_size_t npages)
  707. {
  708. rt_base_t level;
  709. void *ptr;
  710. /* Enter critical zone */
  711. level = _heap_lock();
  712. /* alloc page */
  713. ptr = rt_slab_page_alloc(system_heap, npages);
  714. /* Exit critical zone */
  715. _heap_unlock(level);
  716. return ptr;
  717. }
  718. void rt_page_free(void *addr, rt_size_t npages)
  719. {
  720. rt_base_t level;
  721. /* Enter critical zone */
  722. level = _heap_lock();
  723. /* free page */
  724. rt_slab_page_free(system_heap, addr, npages);
  725. /* Exit critical zone */
  726. _heap_unlock(level);
  727. }
  728. #endif
  729. /**
  730. * @brief This function allocates a memory block, which address is aligned to the
  731. * specified alignment size.
  732. *
  733. * @param size is the allocated memory block size.
  734. *
  735. * @param align is the alignment size.
  736. *
  737. * @return The memory block address was returned successfully, otherwise it was
  738. * returned empty RT_NULL.
  739. */
  740. rt_weak void *rt_malloc_align(rt_size_t size, rt_size_t align)
  741. {
  742. void *ptr = RT_NULL;
  743. void *align_ptr = RT_NULL;
  744. int uintptr_size = 0;
  745. rt_size_t align_size = 0;
  746. /* sizeof pointer */
  747. uintptr_size = sizeof(void*);
  748. uintptr_size -= 1;
  749. /* align the alignment size to uintptr size byte */
  750. align = ((align + uintptr_size) & ~uintptr_size);
  751. /* get total aligned size */
  752. align_size = ((size + uintptr_size) & ~uintptr_size) + align;
  753. /* allocate memory block from heap */
  754. ptr = rt_malloc(align_size);
  755. if (ptr != RT_NULL)
  756. {
  757. /* the allocated memory block is aligned */
  758. if (((rt_ubase_t)ptr & (align - 1)) == 0)
  759. {
  760. align_ptr = (void *)((rt_ubase_t)ptr + align);
  761. }
  762. else
  763. {
  764. align_ptr = (void *)(((rt_ubase_t)ptr + (align - 1)) & ~(align - 1));
  765. }
  766. /* set the pointer before alignment pointer to the real pointer */
  767. *((rt_ubase_t *)((rt_ubase_t)align_ptr - sizeof(void *))) = (rt_ubase_t)ptr;
  768. ptr = align_ptr;
  769. }
  770. return ptr;
  771. }
  772. RTM_EXPORT(rt_malloc_align);
  773. /**
  774. * @brief This function release the memory block, which is allocated by
  775. * rt_malloc_align function and address is aligned.
  776. *
  777. * @param ptr is the memory block pointer.
  778. */
  779. rt_weak void rt_free_align(void *ptr)
  780. {
  781. void *real_ptr = RT_NULL;
  782. /* NULL check */
  783. if (ptr == RT_NULL) return;
  784. real_ptr = (void *) * (rt_ubase_t *)((rt_ubase_t)ptr - sizeof(void *));
  785. rt_free(real_ptr);
  786. }
  787. RTM_EXPORT(rt_free_align);
  788. #endif /* RT_USING_HEAP */
  789. #ifndef RT_USING_CPU_FFS
  790. #ifdef RT_USING_TINY_FFS
  791. const rt_uint8_t __lowest_bit_bitmap[] =
  792. {
  793. /* 0 - 7 */ 0, 1, 2, 27, 3, 24, 28, 32,
  794. /* 8 - 15 */ 4, 17, 25, 31, 29, 12, 32, 14,
  795. /* 16 - 23 */ 5, 8, 18, 32, 26, 23, 32, 16,
  796. /* 24 - 31 */ 30, 11, 13, 7, 32, 22, 15, 10,
  797. /* 32 - 36 */ 6, 21, 9, 20, 19
  798. };
  799. /**
  800. * @brief This function finds the first bit set (beginning with the least significant bit)
  801. * in value and return the index of that bit.
  802. *
  803. * Bits are numbered starting at 1 (the least significant bit). A return value of
  804. * zero from any of these functions means that the argument was zero.
  805. *
  806. * @param value is the value to find the first bit set in.
  807. *
  808. * @return return the index of the first bit set. If value is 0, then this function
  809. * shall return 0.
  810. */
  811. int __rt_ffs(int value)
  812. {
  813. return __lowest_bit_bitmap[(rt_uint32_t)(value & (value - 1) ^ value) % 37];
  814. }
  815. #else
  816. const rt_uint8_t __lowest_bit_bitmap[] =
  817. {
  818. /* 00 */ 0, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  819. /* 10 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  820. /* 20 */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  821. /* 30 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  822. /* 40 */ 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  823. /* 50 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  824. /* 60 */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  825. /* 70 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  826. /* 80 */ 7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  827. /* 90 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  828. /* A0 */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  829. /* B0 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  830. /* C0 */ 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  831. /* D0 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  832. /* E0 */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  833. /* F0 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0
  834. };
  835. /**
  836. * @brief This function finds the first bit set (beginning with the least significant bit)
  837. * in value and return the index of that bit.
  838. *
  839. * Bits are numbered starting at 1 (the least significant bit). A return value of
  840. * zero from any of these functions means that the argument was zero.
  841. *
  842. * @param value is the value to find the first bit set in.
  843. *
  844. * @return Return the index of the first bit set. If value is 0, then this function
  845. * shall return 0.
  846. */
  847. int __rt_ffs(int value)
  848. {
  849. if (value == 0)
  850. {
  851. return 0;
  852. }
  853. if (value & 0xff)
  854. {
  855. return __lowest_bit_bitmap[value & 0xff] + 1;
  856. }
  857. if (value & 0xff00)
  858. {
  859. return __lowest_bit_bitmap[(value & 0xff00) >> 8] + 9;
  860. }
  861. if (value & 0xff0000)
  862. {
  863. return __lowest_bit_bitmap[(value & 0xff0000) >> 16] + 17;
  864. }
  865. return __lowest_bit_bitmap[(value & 0xff000000) >> 24] + 25;
  866. }
  867. #endif /* RT_USING_TINY_FFS */
  868. #endif /* RT_USING_CPU_FFS */
  869. #ifdef RT_DEBUGING_ASSERT
  870. /* RT_ASSERT(EX)'s hook */
  871. void (*rt_assert_hook)(const char *ex, const char *func, rt_size_t line);
  872. /**
  873. * This function will set a hook function to RT_ASSERT(EX). It will run when the expression is false.
  874. *
  875. * @param hook is the hook function.
  876. */
  877. void rt_assert_set_hook(void (*hook)(const char *ex, const char *func, rt_size_t line))
  878. {
  879. rt_assert_hook = hook;
  880. }
  881. /**
  882. * The RT_ASSERT function.
  883. *
  884. * @param ex_string is the assertion condition string.
  885. *
  886. * @param func is the function name when assertion.
  887. *
  888. * @param line is the file line number when assertion.
  889. */
  890. void rt_assert_handler(const char *ex_string, const char *func, rt_size_t line)
  891. {
  892. volatile char dummy = 0;
  893. if (rt_assert_hook == RT_NULL)
  894. {
  895. #ifdef RT_USING_MODULE
  896. if (dlmodule_self())
  897. {
  898. /* close assertion module */
  899. dlmodule_exit(-1);
  900. }
  901. else
  902. #endif /*RT_USING_MODULE*/
  903. {
  904. rt_kprintf("(%s) assertion failed at function:%s, line number:%d \n", ex_string, func, line);
  905. rt_backtrace();
  906. while (dummy == 0);
  907. }
  908. }
  909. else
  910. {
  911. rt_assert_hook(ex_string, func, line);
  912. }
  913. }
  914. RTM_EXPORT(rt_assert_handler);
  915. #endif /* RT_DEBUGING_ASSERT */
  916. /**@}*/