1 /*
2 * Copyright (c) 2021 Bestechnic (Shanghai) Co., Ltd. All rights reserved.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "cmsis.h"
16 #include "securec.h"
17 #include "cmsis_os.h"
18 #include "cmsis_nvic.h"
19 #include "hal_timer.h"
20 #include "hal_timer_fast_irq.h"
21 #include "hal_trace.h"
22 #include "hal_gpio.h"
23 #include "hal_cache.h"
24 #include "hal_uart.h"
25 #include "los_config.h"
26 #include "string.h"
27 #include "los_memory.h"
28 #include "los_context.h"
29 #include "los_arch_interrupt.h"
30 #include "los_debug.h"
31 #include "los_exchook.h"
32 #include "los_backtrace.h"
33 #include "los_tick.h"
34 #include "stdio.h"
35
36 #ifndef CP_BUILD
37 #undef OS_SYS_CLOCK
38 /* =============================================================================
39 System clock module configuration
40 ============================================================================= */
41 /**
42 * @ingroup los_config
43 * System clock (unit: HZ)
44 */
45 #if defined(OSTICK_USE_FAST_TIMER)
46 #define OS_SYS_CLOCK (1000UL)
47 #else
48 #if defined(CHIP_BEST2002) || defined(CHIP_BEST2003)
49 #define OS_SYS_CLOCK (6000000UL)
50 #else
51 #define OS_SYS_CLOCK CONFIG_SYSTICK_HZ
52 #endif
53 #endif
54 #endif
55
56 #define SYSTICK_EXTERNAL_CLOCK 1
57 #ifndef OS_TASK_FLAG_SYSTEM_TASK
58 #define OS_TASK_FLAG_SYSTEM_TASK 0x1000U
59 #endif
60 extern VOID *LOS_MemAlloc(VOID *pool, UINT32 size);
61 extern UINT32 LOS_MemFree(VOID *pool, VOID *mem);
62 extern VOID *LOS_MemRealloc(VOID *pool, VOID *ptr, UINT32 size);
63 extern int platform_init_step1(int user_init);
64 extern int platform_init_step0(int release_version);
65 extern void platform_init_step2(int step1_init);
66
67 extern void OsTickHandler(void);
68 extern void SysTick_Handler(void);
69 extern UINT32 HalExcContextDump(UINT32 index, UINT32 *excContent);
70
71 #ifdef CP_BUILD
72 #define OS_VECTOR_SET NVIC_SetIrqHandler_cp
73 #else
74 #define OS_VECTOR_SET NVIC_SetIrqHandler
75 #endif
76
77 static uint8_t os_exc_occur = 0;
HalConsoleOutput(LogModuleType type,INT32 level,const CHAR * fmt,...)78 VOID HalConsoleOutput(LogModuleType type, INT32 level, const CHAR *fmt, ...)
79 {
80 static UINT8 buffer[320];
81 va_list list;
82 if (level > PRINT_LEVEL) {
83 return;
84 }
85 va_start(list, fmt);
86 vsnprintf((char*)buffer, sizeof(buffer), fmt, list);
87 va_end(list);
88 if (os_exc_occur)
89 hal_uart_output(buffer,strlen((const char*)buffer));
90 else
91 hal_trace_output(buffer,strlen((const char*)buffer));
92 }
93
94 #if !defined(CP_BUILD) && (LOSCFG_BACKTRACE_TYPE > 0)
95 #define OS_EXC_HOOK
96 #endif
97
98 #ifdef OS_EXC_HOOK
99 extern uint32_t __flashx_text_start__[];
100 extern uint32_t __flashx_text_end__[];
101 extern uint32_t __boot_sram_start__[];
102 extern uint32_t __boot_text_sram_end__[];
103 extern uint32_t __sram_text_start__[];
104 extern uint32_t __sram_text_end__[];
105 extern uint32_t __text_start__[];
106 extern uint32_t __text_end__[];
107 extern uint32_t __fast_sram_text_exec_start__[];
108 extern uint32_t __fast_sram_text_exec_end__[];
109
110 #define OS_IS_THUMB_INSTRUCTION(pc) ((pc & 0x1) == 1)
111
112 /* BL or BLX instruction flag. */
113 #define OS_BL_INS_MASK 0xF800
114 #define OS_BL_INS_HIGH 0xF800
115 #define OS_BL_INS_LOW 0xF000
116 #define OS_BLX_INX_MASK 0xFF00
117 #define OS_BLX_INX 0x4700
OsInsIsBlOrBlx(UINTPTR addr)118 STATIC INLINE BOOL OsInsIsBlOrBlx(UINTPTR addr)
119 {
120 UINT16 ins1 = *((UINT16 *)addr);
121 UINT16 ins2 = *((UINT16 *)(addr + 2)); /* 2: Thumb instruction is two bytes. */
122
123 if (((ins2 & OS_BL_INS_MASK) == OS_BL_INS_HIGH) &&
124 ((ins1 & OS_BL_INS_MASK) == OS_BL_INS_LOW)) {
125 return TRUE;
126 } else if ((ins2 & OS_BLX_INX_MASK) == OS_BLX_INX) {
127 return TRUE;
128 } else {
129 return FALSE;
130 }
131 }
132
OsAddrIsValid(UINTPTR sp)133 STATIC INLINE UINTPTR OsAddrIsValid(UINTPTR sp)
134 {
135 UINTPTR pc;
136 BOOL ret;
137
138 /* The stack space pointed to by the current SP may store the LR,
139 so need decrease a word to PC. */
140 pc = *((UINTPTR *)sp) - sizeof(UINTPTR);
141
142 if (!OS_IS_THUMB_INSTRUCTION(pc)) {
143 return 0;
144 }
145
146 /* PC in thumb mode is an odd number, fix the PC address by decreasing one byte. */
147 pc = *((UINTPTR *)sp) - 1;
148 uint32_t * ptr = (uint32_t *)pc;
149 if (!(((ptr >= __flashx_text_start__) && (ptr < __flashx_text_end__)) ||
150 ((ptr >= __boot_sram_start__) && (ptr < __boot_text_sram_end__)) ||
151 ((ptr >= __sram_text_start__) && (ptr < __sram_text_end__)) ||
152 ((ptr >= __text_start__) && (ptr < __text_end__)) ||
153 ((ptr >= __fast_sram_text_exec_start__) && (ptr < __fast_sram_text_exec_end__))))
154 {
155 return 0;
156 }
157
158 ret = OsInsIsBlOrBlx(pc - sizeof(UINTPTR));
159 if (ret == FALSE) {
160 return 0;
161 }
162
163 return pc;
164 }
165
HalSpGet(VOID)166 STATIC INLINE UINTPTR HalSpGet(VOID)
167 {
168 UINTPTR sp;
169 __asm volatile("mov %0, sp" : "=r" (sp));
170 return sp;
171 }
172
HalPspGet(VOID)173 STATIC INLINE UINTPTR HalPspGet(VOID)
174 {
175 UINTPTR psp;
176 __asm volatile("mrs %0, psp" : "=r" (psp));
177 return psp;
178 }
179
HalMspGet(VOID)180 STATIC INLINE UINTPTR HalMspGet(VOID)
181 {
182 UINTPTR msp;
183 __asm volatile("mrs %0, msp" : "=r" (msp));
184 return msp;
185 }
186
OsStackAddrGet(UINTPTR * stackStart,UINTPTR * stackEnd,UINTPTR SP)187 STATIC INLINE UINT32 OsStackAddrGet(UINTPTR *stackStart, UINTPTR *stackEnd, UINTPTR SP)
188 {
189 if (SP != 0) {
190 *stackStart = SP;
191 if ((SP >= CODE_START_ADDR) && (SP < CSTACK_END_ADDR)) {
192 *stackEnd = CSTACK_END_ADDR;
193 } else {
194 UINT32 taskID = LOS_CurTaskIDGet();
195 LosTaskCB *taskCB = OS_TCB_FROM_TID(taskID);
196 *stackEnd = (UINTPTR)taskCB->topOfStack + taskCB->stackSize;
197 if ((SP < (UINTPTR)taskCB->topOfStack) || (SP >= *stackEnd)) {
198 PRINT_ERR("msp statck [0x%x, 0x%x], cur task stack [0x%x, 0x%x], cur sp(0x%x) is overflow!\n",
199 CODE_START_ADDR, CSTACK_END_ADDR, (UINTPTR)taskCB->topOfStack, *stackEnd, SP);
200 return LOS_NOK;
201 }
202 }
203 } else {
204 if (HalSpGet() != HalPspGet()) {
205 *stackStart = HalMspGet();
206 *stackEnd = CSTACK_END_ADDR;
207 if ((*stackStart < CODE_START_ADDR) || (*stackStart >= CSTACK_END_ADDR)) {
208 PRINT_ERR("msp stack [0x%x, 0x%x], cur sp(0x%x) is overflow!\n",
209 CODE_START_ADDR, CSTACK_END_ADDR, *stackStart);
210 return LOS_NOK;
211 }
212 PRINTK("msp, start = %x, end = %x\n", *stackStart, *stackEnd);
213 } else {
214 *stackStart = HalPspGet();
215 UINT32 taskID = LOS_CurTaskIDGet();
216 LosTaskCB *taskCB = OS_TCB_FROM_TID(taskID);
217 *stackEnd = (UINTPTR)taskCB->topOfStack + taskCB->stackSize;
218 if ((*stackStart < (UINTPTR)taskCB->topOfStack) || (*stackStart >= *stackEnd)) {
219 PRINT_ERR("psp stack [0x%x, 0x%x], cur sp(0x%x) is overflow, cur task id is %d!\n",
220 taskCB->topOfStack, *stackEnd, *stackStart, taskID);
221 return LOS_NOK;
222 }
223 PRINTK("psp, start = %x, end = %x\n", *stackStart, *stackEnd);
224 }
225 }
226
227 return LOS_OK;
228 }
229
os_backtrace_hook(UINTPTR * LR,UINT32 LRSize,UINT32 jumpCount,UINTPTR SP)230 VOID os_backtrace_hook(UINTPTR *LR, UINT32 LRSize, UINT32 jumpCount, UINTPTR SP)
231 {
232 UINTPTR stackStart;
233 UINTPTR stackEnd;
234 UINT32 count = 0;
235 UINT32 index = 0;
236 UINTPTR sp;
237 UINTPTR pc;
238 UINT32 ret;
239
240 if (LR == NULL) {
241 return;
242 }
243
244 ret = OsStackAddrGet(&stackStart, &stackEnd, SP);
245 if (ret != LOS_OK) {
246 return;
247 }
248
249 /* Traverse the stack space and find the LR address. */
250 for (sp = stackStart; sp < stackEnd; sp += sizeof(UINTPTR)) {
251 pc = OsAddrIsValid(sp);
252 if ((pc != 0) && (count < LRSize)) {
253 if (index++ < jumpCount) {
254 continue;
255 }
256 LR[count] = pc;
257 count++;
258 if (count == LRSize) {
259 break;
260 }
261 }
262 }
263 if (count < LRSize) {
264 LR[count] = 0;
265 }
266 }
267
os_exc_hook(EXC_TYPE excType)268 VOID os_exc_hook(EXC_TYPE excType)
269 {
270 os_exc_occur = 1;
271 hal_uart_printf_init();
272 }
273 #endif
274
275 #ifndef SYSTICK_IRQ_PRIORITY
276 #define SYSTICK_IRQ_PRIORITY 0xFFU
277 #endif
278 static uint8_t PendST;
279
280 // Get OS Tick IRQ number.
OS_Tick_GetIRQn(void)281 __WEAK int32_t OS_Tick_GetIRQn (void) {
282 #ifdef OSTICK_USE_FAST_TIMER
283 return ((int32_t)TIMER11_IRQn);
284 #else
285 return ((int32_t)SysTick_IRQn);
286 #endif
287 }
288
289 // Setup OS Tick.
OS_Tick_Setup(uint32_t freq)290 static int32_t OS_Tick_Setup (uint32_t freq) {
291 uint32_t load;
292
293 if (freq == 0U) {
294 //lint -e{904} "Return statement before end of function"
295 return (-1);
296 }
297
298 load = OS_SYS_CLOCK/freq - 1U;
299 if (load > SysTick_LOAD_RELOAD_Msk) {
300 //lint -e{904} "Return statement before end of function"
301 return (-1);
302 }
303
304 SysTick->CTRL = 0;
305 // Set SysTick Interrupt Priority
306 #if ((defined(__ARM_ARCH_8M_MAIN__) && (__ARM_ARCH_8M_MAIN__ != 0)) || \
307 (defined(__CORTEX_M) && (__CORTEX_M == 7U)))
308 SCB->SHPR[11] = SYSTICK_IRQ_PRIORITY;
309 #elif (defined(__ARM_ARCH_8M_BASE__) && (__ARM_ARCH_8M_BASE__ != 0))
310 SCB->SHPR[1] |= ((uint32_t)SYSTICK_IRQ_PRIORITY << 24);
311 #elif ((defined(__ARM_ARCH_7M__) && (__ARM_ARCH_7M__ != 0)) || \
312 (defined(__ARM_ARCH_7EM__) && (__ARM_ARCH_7EM__ != 0)))
313 SCB->SHP[11] = SYSTICK_IRQ_PRIORITY;
314 #elif (defined(__ARM_ARCH_6M__) && (__ARM_ARCH_6M__ != 0))
315 SCB->SHP[1] |= ((uint32_t)SYSTICK_IRQ_PRIORITY << 24);
316 #else
317 #error "Unknown ARM Core!"
318 #endif
319
320 #ifdef OSTICK_USE_FAST_TIMER
321 hal_fast_timer_setup(HAL_TIMER_TYPE_PERIODIC, (HAL_TIMER_IRQ_HANDLER_T)OsTickHandler);
322 NVIC_SetPriority(OS_Tick_GetIRQn(), (1UL << __NVIC_PRIO_BITS) - 1UL);
323 hal_fast_timer_start(US_TO_FAST_TICKS(1000000/freq));
324 hal_fast_timer_pause();
325 #else /*OSTICK_USE_FAST_TIMER*/
326 #if (SYSTICK_EXTERNAL_CLOCK)
327 SysTick->CTRL = SysTick_CTRL_TICKINT_Msk;
328 #else
329 SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_TICKINT_Msk;
330 #endif
331 SysTick->LOAD = load;
332 SysTick->VAL = 0U;
333 #endif /*OSTICK_USE_FAST_TIMER*/
334 PendST = 0U;
335
336 return (0);
337 }
338
339 #ifdef OSTICK_USE_FAST_TIMER
340 /// Enable OS Tick.
os_tick_enable(void)341 static void os_tick_enable (void)
342 {
343 hal_fast_timer_continue();
344 }
345
346 /// Disable OS Tick.
os_tick_disable(void)347 static void os_tick_disable (void)
348 {
349 hal_fast_timer_pause();
350 }
351 #endif
352
os_tick_init(HWI_PROC_FUNC tickHandler)353 static uint32_t os_tick_init(HWI_PROC_FUNC tickHandler)
354 {
355 OS_VECTOR_SET(15, tickHandler);
356 OS_Tick_Setup(LOSCFG_BASE_CORE_TICK_PER_SECOND);
357 return 0;
358 }
359
os_pre_init_hook(void)360 void os_pre_init_hook(void)
361 {
362 #ifdef OS_EXC_HOOK
363 OS_VECTOR_SET(2, HalExcNMI);
364 OS_VECTOR_SET(3, HalExcHardFault);
365 OS_VECTOR_SET(4, HalExcMemFault);
366 OS_VECTOR_SET(5, HalExcBusFault);
367 OS_VECTOR_SET(6, HalExcUsageFault);
368 OsBackTraceHookSet(os_backtrace_hook);
369 #endif
370 OS_VECTOR_SET(11, HalSVCHandler);
371 OS_VECTOR_SET(14, HalPendSV);
372 hal_cache_sync_all(HAL_CACHE_ID_D_CACHE);
373
374 ArchTickTimer tickTimer;
375 (VOID)memcpy_s(&tickTimer, sizeof(ArchTickTimer), LOS_SysTickTimerGet(), sizeof(ArchTickTimer));
376 tickTimer.freq = OS_SYS_CLOCK;
377 tickTimer.irqNum = 15;
378 tickTimer.init = os_tick_init;
379 LOS_TickTimerRegister(&tickTimer, NULL);
380 }
381
382 extern UINT32 g_swtmrTaskID;
os_post_init_hook(void)383 void os_post_init_hook(void)
384 {
385 OS_TCB_FROM_TID(g_swtmrTaskID)->taskStatus &= ~OS_TASK_FLAG_SYSTEM_TASK;
386 }
387
388 #if !defined(MODULE_KERNEL_STUB)
OHOS_SystemInit(void)389 __WEAK void OHOS_SystemInit(void)
390 {
391 printf("%s stub function!\r\n",__FUNCTION__);
392 }
393 #ifndef CP_BUILD
OhosSystemAdapterHooks(void)394 __WEAK int OhosSystemAdapterHooks(void)
395 {
396 printf("%s stub function!\r\n",__FUNCTION__);
397 return 0;
398 }
399 #endif
400 #else
401 #ifndef CP_BUILD
402 extern void OhosSystemAdapterHooks(void);
403 #endif
404 #endif
405
406 int main(void);
407 extern void OHOS_SystemInit(void);
408 extern int platform_io_cfg(uint32_t fem_idx, uint32_t pa_en_idx);
409 #ifndef __NO_APP__
board_main(const void * arg)410 void board_main(const void * arg)
411 {
412 #if defined(RF_TX_CONTROL_IO) || defined(PA_ENABLE_IO)
413 uint32_t rf_tx_io = 0;
414 uint32_t pa_enable_io = -1;
415 #endif
416 UINTPTR intSave;
417 intSave = LOS_IntLock();
418 #ifdef OS_EXC_HOOK
419 LOS_RegExcHook(EXC_INTERRUPT, os_exc_hook);
420 //LOS_BackTraceInit();
421 #endif
422 LOS_IntRestore(intSave);
423 platform_init_step0(0);
424 #ifdef RF_TX_CONTROL_IO
425 rf_tx_io = RF_TX_CONTROL_IO;
426 #endif
427 #ifdef PA_ENABLE_IO
428 pa_enable_io = PA_ENABLE_IO;
429 #endif
430 #if defined(RF_TX_CONTROL_IO) || defined(PA_ENABLE_IO)
431 platform_io_cfg(rf_tx_io, pa_enable_io);
432 #endif
433 int ret = platform_init_step1(0);
434
435 if(!ret) {
436 #ifndef CP_BUILD
437 OhosSystemAdapterHooks();
438 #endif
439 OHOS_SystemInit();
440 }
441 hal_trace_crash_dump_register(HAL_TRACE_CRASH_DUMP_MODULE_SYS,
442 (HAL_TRACE_CRASH_DUMP_CB_T)LOS_TaskInfoMonitor);
443 platform_init_step2(ret);
444 while (1) {
445 osDelay(1000);
446 TRACE(0, "main idle");
447 }
448 }
449 #else
board_main(const void * arg)450 void board_main(const void * arg)
451 {
452 UINTPTR intSave;
453 intSave = LOS_IntLock();
454 #ifdef OS_EXC_HOOK
455 LOS_RegExcHook(EXC_INTERRUPT, os_exc_hook);
456 OsBackTraceHookSet(os_backtrace_hook);
457 //LOS_BackTraceInit();
458 #endif
459 LOS_IntRestore(intSave);
460 main();
461 }
462 #endif
463
464 osThreadDef_t os_thread_def_main = {(board_main),{"main",osThreadDetached,NULL,0U,0,0x4000,(osPriorityHigh),0U,0U}};
465 #ifdef __NO_STARTFILES__
_start(void)466 void _start(void)
467 {
468 os_pre_init_hook();
469 osKernelInitialize();
470 osThreadCreate(&os_thread_def_main, 0);
471 os_post_init_hook();
472 osKernelStart();
473 }
474 #else
475 #ifdef LIBC_INIT_AFTER_OS_INIT
software_init_hook(void)476 __attribute__((naked)) void software_init_hook (void) {
477 __asm (
478 ".syntax unified\n"
479 ".thumb\n"
480 "bl os_pre_init_hook\n"
481 "bl osKernelInitialize\n"
482 "movs r0,#0\n"
483 "movs r1,#0\n"
484 "mov r4,r0\n"
485 "mov r5,r1\n"
486 "ldr r0,= __libc_fini_array\n"
487 "bl atexit\n"
488 "bl __libc_init_array\n"
489 "mov r0,r4\n"
490 "mov r1,r5\n"
491 "ldr r0,=os_thread_def_main\n"
492 "movs r1,#0\n"
493 "bl osThreadCreate\n"
494 "bl os_post_init_hook\n"
495 "bl osKernelStart\n"
496 "bl exit\n"
497 );
498 }
499 #else
software_init_hook(void)500 __attribute__((naked)) void software_init_hook (void) {
501 __asm (
502 ".syntax unified\n"
503 ".thumb\n"
504 "movs r0,#0\n"
505 "movs r1,#0\n"
506 "mov r4,r0\n"
507 "mov r5,r1\n"
508 "ldr r0,= __libc_fini_array\n"
509 "bl atexit\n"
510 "bl __libc_init_array\n"
511 "mov r0,r4\n"
512 "mov r1,r5\n"
513 "bl os_pre_init_hook\n"
514 "bl osKernelInitialize\n"
515 "ldr r0,=os_thread_def_main\n"
516 "movs r1,#0\n"
517 "bl osThreadCreate\n"
518 "bl os_post_init_hook\n"
519 "bl osKernelStart\n"
520 "bl exit\n"
521 );
522 }
523 #endif
524 #endif
525
526
527 #ifndef OHOS_WRAP_MALLOC
__wrap_malloc(size_t size)528 void *__wrap_malloc(size_t size)
529 {
530 return LOS_MemAlloc(OS_SYS_MEM_ADDR, size);
531 }
532
__wrap__malloc_r(void * reent,size_t nbytes)533 void *__wrap__malloc_r(void *reent, size_t nbytes)
534 {
535 return LOS_MemAlloc(OS_SYS_MEM_ADDR, nbytes);
536 }
537
__wrap__realloc_r(void * reent,void * aptr,size_t nbytes)538 void *__wrap__realloc_r(void *reent,
539 void *aptr, size_t nbytes)
540 {
541 ///TODO: Fix bug of LOS_MemRealloc
542 //return LOS_MemRealloc(OS_SYS_MEM_ADDR, rmem, newsize);
543 void * ptr = NULL;
544 if (nbytes) {
545 ptr = LOS_MemAlloc(OS_SYS_MEM_ADDR, nbytes);
546 if (ptr && aptr) {
547 memcpy(ptr, aptr, nbytes);
548 LOS_MemFree(OS_SYS_MEM_ADDR, aptr);
549 }
550 } else
551 LOS_MemFree(OS_SYS_MEM_ADDR, aptr);
552 return ptr;
553 }
554
555
__wrap__calloc_r(struct _reent * reent,size_t nitems,size_t size)556 void *__wrap__calloc_r(struct _reent *reent, size_t nitems, size_t size)
557 {
558 size_t real_size;
559 void *ptr = NULL;
560
561 if (nitems == 0 || size == 0) {
562 return NULL;
563 }
564
565 real_size = (size_t)(nitems * size);
566 ptr = LOS_MemAlloc(OS_SYS_MEM_ADDR, real_size);
567 if (ptr != NULL) {
568 (void)memset(ptr, 0, real_size);
569 }
570 return ptr;
571 }
572
__wrap_calloc(size_t count,size_t size)573 void *__wrap_calloc(size_t count, size_t size)
574 {
575 void * ptr = NULL;
576 ptr = LOS_MemAlloc(OS_SYS_MEM_ADDR, count * size);
577 if (ptr)
578 memset(ptr, 0, count * size);
579 return ptr;
580 }
581
__wrap_realloc(void * rmem,size_t newsize)582 void *__wrap_realloc(void * rmem, size_t newsize)
583 {
584 ///TODO: Fix bug of LOS_MemRealloc
585 //return LOS_MemRealloc(OS_SYS_MEM_ADDR, rmem, newsize);
586 void * ptr = NULL;
587 if (newsize) {
588 ptr = LOS_MemAlloc(OS_SYS_MEM_ADDR, newsize);
589 if (ptr && rmem) {
590 memcpy(ptr, rmem, newsize);
591 LOS_MemFree(OS_SYS_MEM_ADDR, rmem);
592 }
593 } else
594 LOS_MemFree(OS_SYS_MEM_ADDR, rmem);
595 return ptr;
596 }
597
__wrap__free_r(struct _reent * reent,void * aptr)598 void __wrap__free_r(struct _reent *reent, void *aptr)
599 {
600 if (aptr == NULL) {
601 return;
602 }
603
604 LOS_MemFree(OS_SYS_MEM_ADDR, aptr);
605 }
606
__wrap_free(void * rmem)607 void __wrap_free(void *rmem)
608 {
609 LOS_MemFree(OS_SYS_MEM_ADDR, rmem);
610 return;
611 }
612 #endif
613
os_heap_stat_print(void)614 void os_heap_stat_print(void)
615 {
616 uint8_t fragment = 0;
617 uint8_t usage = 0;
618 LOS_MEM_POOL_STATUS poolStatus = {0};
619 (VOID) LOS_MemInfoGet(LOSCFG_SYS_HEAP_ADDR, &poolStatus);
620 if (poolStatus.totalFreeSize != 0) {
621 fragment = 100 - poolStatus.maxFreeNodeSize * 100 / poolStatus.totalFreeSize; /* 100: percent denominator. */
622 }
623 if (LOS_MemPoolSizeGet(LOSCFG_SYS_HEAP_ADDR) != 0) {
624 usage = LOS_MemTotalUsedGet(LOSCFG_SYS_HEAP_ADDR) * 100 / LOS_MemPoolSizeGet(LOSCFG_SYS_HEAP_ADDR); /* 100: percent denominator. */
625 }
626 TRACE(0, "os heap usage = %d, fragment = %d, maxFreeSize = %d, totalFreeSize = %d\n", usage, fragment,
627 poolStatus.maxFreeNodeSize, poolStatus.totalFreeSize);
628 }
629