LW_API VOID API_KernelPrimaryStart (PKERNEL_START_ROUTINE pfuncStartHook) #endif /* LW_CFG_MEMORY_HEAP_... */ { INT iError; _BugHandle(LW_SYS_STATUS_IS_RUNNING(), LW_TRUE, "kernel is already start.\r\n"); _BugHandle(LW_NCPUS > LW_CFG_MAX_PROCESSORS, LW_TRUE, "LW_NCPUS > LW_CFG_MAX_PROCESSORS.\r\n"); _DebugHandle(__LOGMESSAGE_LEVEL, "longwing(TM) kernel initialize...\r\n"); #if LW_CFG_MEMORY_HEAP_CONFIG_TYPE > 0 _BugHandle(!pvKernelHeapMem, LW_TRUE, "heap memory invalidate.\r\n"); _BugHandle(!_Addresses_Is_Aligned(pvKernelHeapMem), LW_TRUE, "heap memory is not align.\r\n"); _BugHandle(stKernelHeapSize < LW_CFG_KB_SIZE, LW_TRUE, "heap memory is too little.\r\n"); if (pvSystemHeapMem && stSystemHeapSize) { _BugHandle(!pvSystemHeapMem, LW_TRUE, "heap memory invalidate.\r\n"); _BugHandle(!_Addresses_Is_Aligned(pvSystemHeapMem), LW_TRUE, "heap memory is not align.\r\n"); _BugHandle(stSystemHeapSize < LW_CFG_KB_SIZE, LW_TRUE, "heap memory is too little.\r\n"); } _KernelPrimaryLowLevelInit(pvKernelHeapMem, stKernelHeapSize, pvSystemHeapMem, stSystemHeapSize); /* 内核底层初始化 */ #else _KernelPrimaryLowLevelInit(); /* 内核底层初始化 */ #endif /* LW_CFG_MEMORY_HEAP_... */ _DebugHandle(__LOGMESSAGE_LEVEL, "kernel interrupt vector initialize...\r\n"); bspIntInit(); /* 初始化中断系统 */ _KernelHighLevelInit(); /* 内核高级初始化 */ iError = _cppRtInit(); /* CPP 运行时库初始化 */ if (iError != ERROR_NONE) { _DebugHandle(__ERRORMESSAGE_LEVEL, "c++ run time lib initialized error!\r\n"); } else { _DebugHandle(__LOGMESSAGE_LEVEL, "c++ run time lib initialized.\r\n"); } _DebugHandle(__LOGMESSAGE_LEVEL, "kernel primary cpu usrStartup...\r\n"); if (pfuncStartHook) { /* 用户是否要求需要初始化 */ pfuncStartHook(); /* 用户系统初始化 */ } #if LW_CFG_MODULELOADER_EN > 0 _resInit(); /* 从现在开始记录资源情况 */ #endif /* LW_CFG_MODULELOADER_EN > 0 */ #if LW_CFG_SMP_EN > 0 _KernelBootSecondary(); /* 从核可以进行初始化操作 */ #endif /* LW_CFG_SMP_EN */ _KernelPrimaryEntry(LW_CPU_GET_CUR()); /* 启动内核 */ /* 多核将在第一次调度被启用 */ }
/********************************************************************************************************* ** 函数名称: __vmmLibPageMap ** 功能描述: 将物理页面重新映射 ** 输 入 : ulPhysicalAddr 物理页面地址 ** ulVirtualAddr 需要映射的虚拟地址 ** ulPageNum 需要映射的页面个数 ** ulFlag 页面标志 ** 输 出 : ERROR CODE ** 全局变量: ** 调用模块: *********************************************************************************************************/ ULONG __vmmLibPageMap (addr_t ulPhysicalAddr, addr_t ulVirtualAddr, ULONG ulPageNum, ULONG ulFlag) { INTREG iregInterLevel; ULONG i; PLW_MMU_CONTEXT pmmuctx = __vmmGetCurCtx(); LW_PGD_TRANSENTRY *p_pgdentry; LW_PMD_TRANSENTRY *p_pmdentry; LW_PTE_TRANSENTRY *p_pteentry; for (i = 0; i < ulPageNum; i++) { p_pgdentry = __vmm_pgd_alloc(pmmuctx, ulVirtualAddr); if (p_pgdentry == LW_NULL) { return (ERROR_VMM_LOW_LEVEL); } p_pmdentry = __vmm_pmd_alloc(pmmuctx, p_pgdentry, ulVirtualAddr); if (p_pmdentry == LW_NULL) { return (ERROR_VMM_LOW_LEVEL); } p_pteentry = __vmm_pte_alloc(pmmuctx, p_pmdentry, ulVirtualAddr); if (p_pteentry == LW_NULL) { return (ERROR_VMM_LOW_LEVEL); } iregInterLevel = KN_INT_DISABLE(); /* 关闭中断 */ __VMM_MMU_MAKE_TRANS(pmmuctx, p_pteentry, ulPhysicalAddr, ulFlag); /* 创建映射关系 */ KN_INT_ENABLE(iregInterLevel); /* 打开中断 */ ulPhysicalAddr += LW_CFG_VMM_PAGE_SIZE; ulVirtualAddr += LW_CFG_VMM_PAGE_SIZE; } iregInterLevel = KN_INT_DISABLE(); /* 关闭中断 */ #if LW_CFG_SMP_EN > 0 if (LW_SYS_STATUS_IS_RUNNING() && (__VMM_MMU_OPTION() & LW_VMM_MMU_FLUSH_TLB_MP)) { _SmpSendIpiAllOther(LW_IPI_FLUSH_TLB, 1); /* 同步刷新所有 CPU TLB */ } #endif /* LW_CFG_SMP_EN */ __VMM_MMU_INV_TLB(pmmuctx); /* 无效快表 */ KN_INT_ENABLE(iregInterLevel); /* 打开中断 */ return (ERROR_NONE); }
/********************************************************************************************************* ** 函数名称: API_CoroutineDelete ** 功能描述: 删除一个指定的协程. ** 输 入 : pvCrcb 协程句柄 ** 输 出 : ERROR ** 全局变量: ** 调用模块: API 函数 *********************************************************************************************************/ LW_API ULONG API_CoroutineDelete (PVOID pvCrcb) { INTREG iregInterLevel; REGISTER PLW_CLASS_COROUTINE pcrcbDel = (PLW_CLASS_COROUTINE)pvCrcb; REGISTER PLW_CLASS_COROUTINE pcrcbNow; PLW_CLASS_TCB ptcbCur; if (!LW_SYS_STATUS_IS_RUNNING()) { /* 系统必须已经启动 */ _ErrorHandle(ERROR_KERNEL_NOT_RUNNING); return (ERROR_KERNEL_NOT_RUNNING); } if (LW_CPU_GET_CUR_NESTING()) { /* 不能在中断中调用 */ _DebugHandle(__ERRORMESSAGE_LEVEL, "called from ISR.\r\n"); _ErrorHandle(ERROR_KERNEL_IN_ISR); return (ERROR_KERNEL_IN_ISR); } if (!pcrcbDel) { _DebugHandle(__ERRORMESSAGE_LEVEL, "coroutine handle invalidate.\r\n"); _ErrorHandle(EINVAL); return (EINVAL); } LW_TCB_GET_CUR_SAFE(ptcbCur); /* 当前任务控制块 */ pcrcbNow = _LIST_ENTRY(ptcbCur->TCB_pringCoroutineHeader, LW_CLASS_COROUTINE, COROUTINE_ringRoutine); /* 获得当前协程 */ if (pcrcbNow == pcrcbDel) { /* 删除当前协程 */ return (API_CoroutineExit()); } LW_SPIN_LOCK_QUICK(&ptcbCur->TCB_slLock, &iregInterLevel); _List_Ring_Del(&pcrcbDel->COROUTINE_ringRoutine, &ptcbCur->TCB_pringCoroutineHeader); /* 从协程表中删除 */ LW_SPIN_UNLOCK_QUICK(&ptcbCur->TCB_slLock, iregInterLevel); MONITOR_EVT_LONG2(MONITOR_EVENT_ID_COROUTINE, MONITOR_EVENT_COROUTINE_DELETE, ptcbCur->TCB_ulId, pcrcbDel, LW_NULL); if (pcrcbDel->COROUTINE_bIsNeedFree) { _StackFree(ptcbCur, pcrcbDel->COROUTINE_pstkStackLowAddr, LW_TRUE); /* 释放内存 */ } return (ERROR_NONE); }
/********************************************************************************************************* ** 函数名称: _KernelPrimaryEntry ** 功能描述: 主核系统内核内部入口 ** 输 入 : pcpuCur 当前 CPU ** 输 出 : ** 全局变量: ** 调用模块: *********************************************************************************************************/ static VOID _KernelPrimaryEntry (PLW_CLASS_CPU pcpuCur) { if (LW_SYS_STATUS_IS_RUNNING()) { /* 系统必须没有启动 */ _DebugHandle(__ERRORMESSAGE_LEVEL, "kernel is already run! can not re-enter\r\n"); _ErrorHandle(ERROR_KERNEL_RUNNING); return; } _DebugHandle(__LOGMESSAGE_LEVEL, "kernel ticks initialize...\r\n"); bspTickInit(); /* 初始化操作系统时钟 */ LW_SYS_STATUS_SET(LW_SYS_STATUS_RUNNING); /* 启动多任务处理 */ _KernelPrimaryCoreStartup(pcpuCur); /* 主核(当前核)运行 */ }
/********************************************************************************************************* ** 函数名称: API_CoroutineExit ** 功能描述: 在当前线程正在执行的协程删除 ** 输 入 : NONE ** 输 出 : ERROR ** 全局变量: ** 调用模块: API 函数 *********************************************************************************************************/ LW_API ULONG API_CoroutineExit (VOID) { INTREG iregInterLevel; PLW_CLASS_CPU pcpuCur; PLW_CLASS_TCB ptcbCur; REGISTER PLW_CLASS_COROUTINE pcrcbExit; REGISTER PLW_CLASS_COROUTINE pcrcbNext; REGISTER PLW_LIST_RING pringNext; if (!LW_SYS_STATUS_IS_RUNNING()) { /* 系统必须已经启动 */ _ErrorHandle(ERROR_KERNEL_NOT_RUNNING); return (ERROR_KERNEL_NOT_RUNNING); } if (LW_CPU_GET_CUR_NESTING()) { /* 不能在中断中调用 */ _DebugHandle(__ERRORMESSAGE_LEVEL, "called from ISR.\r\n"); _ErrorHandle(ERROR_KERNEL_IN_ISR); return (ERROR_KERNEL_IN_ISR); } LW_TCB_GET_CUR_SAFE(ptcbCur); pcrcbExit = _LIST_ENTRY(ptcbCur->TCB_pringCoroutineHeader, LW_CLASS_COROUTINE, COROUTINE_ringRoutine); /* 获得当前协程 */ if (&pcrcbExit->COROUTINE_ringRoutine == _list_ring_get_next(&pcrcbExit->COROUTINE_ringRoutine)) { /* 仅有这一个协程 */ #if LW_CFG_THREAD_DEL_EN > 0 API_ThreadExit(LW_NULL); #endif /* LW_CFG_THREAD_DEL_EN > 0 */ return (ERROR_NONE); } pringNext = _list_ring_get_next(&pcrcbExit->COROUTINE_ringRoutine); pcrcbNext = _LIST_ENTRY(pringNext, LW_CLASS_COROUTINE, COROUTINE_ringRoutine); /* 获得下一个协程 */ LW_SPIN_LOCK_QUICK(&ptcbCur->TCB_slLock, &iregInterLevel); _List_Ring_Del(&pcrcbExit->COROUTINE_ringRoutine, &ptcbCur->TCB_pringCoroutineHeader); /* 从协程表中删除 */ LW_SPIN_UNLOCK_QUICK(&ptcbCur->TCB_slLock, iregInterLevel); ptcbCur->TCB_pringCoroutineHeader = pringNext; /* 转动到下一个协程 */ MONITOR_EVT_LONG2(MONITOR_EVENT_ID_COROUTINE, MONITOR_EVENT_COROUTINE_DELETE, ptcbCur->TCB_ulId, pcrcbExit, LW_NULL); if (pcrcbExit->COROUTINE_bIsNeedFree) { _StackFree(ptcbCur, pcrcbExit->COROUTINE_pstkStackLowAddr, LW_TRUE); /* 释放内存 */ } iregInterLevel = KN_INT_DISABLE(); /* 关闭中断 */ INIT_DUMMY_STACK(); pcpuCur = LW_CPU_GET_CUR(); pcpuCur->CPU_pcrcbCur = &_K_pcrcbDummy; pcpuCur->CPU_pcrcbNext = pcrcbNext; archCrtCtxSwitch(LW_CPU_GET_CUR()); /* 协程切换 */ KN_INT_ENABLE(iregInterLevel); /* 打开中断 */ return (ERROR_NONE); /* 理论上是无法运行到这里的 */ }
LW_API ULONG API_SemaphoreMPost (LW_OBJECT_HANDLE ulId) { INTREG iregInterLevel; PLW_CLASS_TCB ptcbCur; REGISTER UINT16 usIndex; REGISTER PLW_CLASS_EVENT pevent; REGISTER PLW_CLASS_TCB ptcb; REGISTER PLW_LIST_RING *ppringList; /* 等待队列地址 */ usIndex = _ObjectGetIndex(ulId); if (LW_CPU_GET_CUR_NESTING()) { /* 不能在中断中调用 */ _DebugHandle(__ERRORMESSAGE_LEVEL, "called from ISR.\r\n"); _ErrorHandle(ERROR_KERNEL_IN_ISR); return (ERROR_KERNEL_IN_ISR); } LW_TCB_GET_CUR_SAFE(ptcbCur); /* 当前任务控制块 */ #if LW_CFG_ARG_CHK_EN > 0 if (!_ObjectClassOK(ulId, _OBJECT_SEM_M)) { /* 类型是否正确 */ _DebugHandle(__ERRORMESSAGE_LEVEL, "semaphore handle invalidate.\r\n"); _ErrorHandle(ERROR_KERNEL_HANDLE_NULL); return (ERROR_KERNEL_HANDLE_NULL); } if (_Event_Index_Invalid(usIndex)) { /* 下标是否正正确 */ _DebugHandle(__ERRORMESSAGE_LEVEL, "semaphore handle invalidate.\r\n"); _ErrorHandle(ERROR_KERNEL_HANDLE_NULL); return (ERROR_KERNEL_HANDLE_NULL); } #endif pevent = &_K_eventBuffer[usIndex]; if (!LW_SYS_STATUS_IS_RUNNING()) { /* 系统还没有启动 */ return (ERROR_NONE); } __KERNEL_ENTER(); /* 进入内核 */ if (_Event_Type_Invalid(usIndex, LW_TYPE_EVENT_MUTEX)) { __KERNEL_EXIT(); /* 退出内核 */ _DebugHandle(__ERRORMESSAGE_LEVEL, "semaphore handle invalidate.\r\n"); _ErrorHandle(ERROR_EVENT_TYPE); return (ERROR_EVENT_TYPE); } if (ptcbCur != (PLW_CLASS_TCB)pevent->EVENT_pvTcbOwn) { /* 是否是拥有者 */ __KERNEL_EXIT(); /* 退出内核 */ _ErrorHandle(ERROR_EVENT_NOT_OWN); /* 没有事件所有权 */ return (ERROR_EVENT_NOT_OWN); } if (pevent->EVENT_pvPtr) { /* 检测是否进行了连续调用 */ pevent->EVENT_pvPtr = (PVOID)((ULONG)pevent->EVENT_pvPtr - 1); /* 临时计数器-- */ __KERNEL_EXIT(); /* 退出内核 */ return (ERROR_NONE); } iregInterLevel = KN_INT_DISABLE(); if (_EventWaitNum(pevent)) { if (pevent->EVENT_ulOption & LW_OPTION_WAIT_PRIORITY) { /* 优先级等待队列 */ _EVENT_DEL_Q_PRIORITY(ppringList); /* 检查需要激活的队列 */ /* 激活优先级等待线程 */ ptcb = _EventReadyPriorityLowLevel(pevent, LW_NULL, ppringList); } else { _EVENT_DEL_Q_FIFO(ppringList); /* 检查需要激活的FIFO队列 */ /* 激活FIFO等待线程 */ ptcb = _EventReadyFifoLowLevel(pevent, LW_NULL, ppringList); } KN_INT_ENABLE(iregInterLevel); _EventPrioTryResume(pevent, ptcbCur); /* 尝试返回之前的优先级 */ pevent->EVENT_ulMaxCounter = (ULONG)ptcb->TCB_ucPriority; pevent->EVENT_pvTcbOwn = (PVOID)ptcb; /* 保存线程信息 */ _EventReadyHighLevel(ptcb, LW_THREAD_STATUS_SEM); /* 处理 TCB */ MONITOR_EVT_LONG2(MONITOR_EVENT_ID_SEMM, MONITOR_EVENT_SEM_POST, ulId, ptcb->TCB_ulId, LW_NULL); __KERNEL_EXIT(); /* 退出内核 */ if (pevent->EVENT_ulOption & LW_OPTION_DELETE_SAFE) { /* 退出安全模式 */ _ThreadUnsafeInternal(); } return (ERROR_NONE); } else { /* 没有线程等待 */ KN_INT_ENABLE(iregInterLevel); if (pevent->EVENT_ulCounter == LW_FALSE) { /* 检查是否还有空间加 */ pevent->EVENT_ulCounter = (ULONG)LW_TRUE; pevent->EVENT_ulMaxCounter = LW_PRIO_LOWEST; /* 清空保存信息 */ pevent->EVENT_pvTcbOwn = LW_NULL; __KERNEL_EXIT(); /* 退出内核 */ if (pevent->EVENT_ulOption & LW_OPTION_DELETE_SAFE) { /* 退出安全模式 */ _ThreadUnsafeInternal(); } return (ERROR_NONE); } else { /* 已经满了 */ __KERNEL_EXIT(); /* 退出内核 */ _ErrorHandle(ERROR_EVENT_FULL); return (ERROR_EVENT_FULL); } } }