1.å¦ä½å¦ä¹ nucleus os
å¦ä½å¦ä¹ nucleus os
å 容ï¼
ä¸ãnucleus plusç¹ç¹ï¼
1.å æ ¸éç¨å¾®å æ ¸ç设计ï¼æ¹ä¾¿ç§»æ¤ï¼èµæåçæ´reliableï¼ä½æ¯æä¸è¿ä¹è®¤ä¸ºï¼ä¸linuxç¸æ¯ï¼ä»¥ARMå¹³å°ä¸ºä¾ï¼NUåªç¨å°äºSVC modeï¼å æ ¸ä¸ç¨æ·ä»»å¡é½è¿è¡å¨åä¸ä¸ªç¶æä¸ï¼ä¹å°±æ¯è¯´ææçtaské½æ¥æ访é®ä»»ä½èµæºçæéï¼è¿æ ·å¾reliableä¹ï¼
2.real-time OSï¼NUæ¯ä¸ä¸ªè½¯å®æ¶æä½ç³»ç»ï¼VxWorksæ¯ç¡¬å®æ¶ï¼ï¼thread control componentæ¯æå¤ä»»å¡ä»¥åä»»å¡çæ¢å ï¼å¯¹äºä¸æçå¤çå®ä¹äºä¸¤ç§æå¡æ¹å¼ï¼LISRåHISRï¼è¿ä¸ªä¸linuxä¸çä¸ãä¸åé¨æºå¶ç±»ä¼¼ï¼linuxä¸çä¸åé¨æ¯éè¿è½¯ä¸ææ¥å®ç°çï¼NUçHISRåªæ¯ä½ä¸ºä¸ç§ä¼å 级æ»æ¯é«äºtaskçä»»å¡åºç°ã
3.NUæ¯ä»¥libraryçæ¹å¼åºç¨çï¼éè¿åèªå·±çapp taskä¸è£åªåçNUå æ ¸åç»ä»¶é¾æ¥èµ·æ¥ï¼NU并没æCLI
äºãç»ä»¶
1.IN component
åå§åç»ä»¶ç±ä¸ä¸ªé¨åç»æï¼ç¡¬ä»¶å¨resetåé¦å è¿å ¥INT_initialize()ï¼è¿è¡æ¿çº§çç¸å ³åå§åï¼é¦å 设置SVC modeï¼å ³ä¸æï¼ç¶åå°å æ ¸ä»romä¸æ·è´è³ramä¸ï¼å»ºç«bss段ï¼ä¾æ¬¡å»ºç«sys stack,源码微营销源码 irq stackåfiq stackï¼æååå§åtimerï¼å»ºç«timer HISRçæ 空é´ï¼çäºä¸ä¸å¹³å°ç代ç ï¼ä¸ä¸ªtick大æ¦æ¯.8msï¼å®ææ¿çº§çåå§ååå°±è¿å ¥äºINC_initializeï¼åå§åå个ç»ä»¶ï¼å ¶ä¸å æ¬Application initializeï¼create taskåHISRï¼æåå°æ§å¶æ交ç»scheduleï¼ä¸»è¦çäºä¸ä¸RAMä¸å°å空é´çå®æ
|timer HISR stack = |
|FIQ stack = |
|IRQ stack = |
|SVC stack = |
|.bss|
|.data|
|.text|
å ¶ä¸SVC stackç大å°ä¸ä¸ææºç个æ°ç¸å ³ï¼nested irqåçæ¶ï¼irq_contextä¿åå¨SVC stackä¸ï¼IRQçstackåªæ¯åäºä¸´æ¶æ çä½ç¨ã
2.thread control component
TCç»ä»¶æ¯NUå æ ¸çæéè¦ç»æé¨åï¼ä¸»è¦æ¶µçäºè°åº¦ãä¸æãä»»å¡çç¸å ³æä½ãéãæ¶éå 个æ¹é¢ï¼ä¸é¢åå«ä»ç»ã
è°åº¦ï¼scheduleï¼
NUä¸ç线ç¨ç±»åï¼å¨åä¸ä¸ªå°å空é´å ï¼æ两ç§ï¼HISRåtaskï¼HISRå¯ä»¥ç解为ä¸ç§ä¼å 级è¾é«çtaskï¼ä½åä¸æ¯taskï¼HISRä¼å 级é«äºtaskçå®ç°æ¹å¼å°±æ¯schduleæ¶ï¼å å»æ¥çå½åæ¯å¦æactiveçHISRï¼åå»æ¥çtaskãtaskæsuspendãreadyãfinishedåterminatedåç§ç¶æï¼èHISRåªæexecutingåno-activeè¿ä¸¤ç§ç¶æã
æ¯ä¸ä¸ªtaské½æä¸ä¸ªçº¿ç¨æ§å¶çæ°æ®ç»æï¼TCB thread control blockï¼,å ¶ä¸å æ¬äºtaskçä¼å 级ãç¶æãæ¶é´çãtaskæ ãprotectä¿¡æ¯ãsignalæä½çæ å¿ä½åsignal_handlerçï¼taskå¨å建æ¶åå§åè¿äºä¿¡æ¯ï¼å°taskæå°ä¸ä¸ªcreate_listä¸ï¼åå§è®¾å®task为pure_suspendï¼å¦æ设å®auto startï¼è°ç¨resume_taskï¼ï¼å¤étaskï¼è¿éæ个ç»èï¼å¦æå¨application initializeä¸create_task()ï¼åtaskä¸ä¼èªå¨è¿è¡ï¼å 为åå§åè¿æªå®æï¼æ§å¶æè¿æ²¡æ交ç»scheduleï¼æ æ³è°åº¦taskãtask被å¤éåç¶ææ¹å为readyï¼å¹¶æå¨ä¸ä¸ªTCD_Priority_List[]ä¸ï¼æ°ç»çæ¯ä¸ªå ç´ æ¯ä¸ä¸ªæåTCBç¯å½¢ååé¾è¡¨çæéï¼æ ¹æ®taskçtc_priorityæ¾å°å¯¹åºä¼å 级çTCB head pointerã
æ¯ä¸ä¸ªHISRé½æä¸ä¸ªHISRæ§å¶çæ°æ®ç»æï¼HCB HISR control blockï¼,å ¶ä¸åªæä¼å 级ï¼HISRæ åHISR entryä¿¡æ¯ï¼å æ¤HISRæ¯ä¸å¯ä»¥suspendï¼åæ¶ä¹æ²¡ætime slice以åsignalçç¸å ³æä½ï¼ä¸è¬æ åµä¸å½åçäºä¸æåï¼HISR被activateï¼scheduleå°±ä¼è°åº¦HISRè¿è¡ï¼æé´å¦æä¸åçä¸æï¼HISRçæ§è¡æ¯ä¸ä¼è¢«ææçï¼HISRçä¼å 级åªæ0ã1ã2ï¼timerçHISRä¼å 级为2ï¼ä¹å°±æ¯è¯´ç±å¤é¨ä¸ææ¿æ´»çHISRå¾é¾è¢«æ¢å çï¼åªææ´é«ä¼å 级çä¸æHISRæå¯ä»¥ãä¸taskä¸åï¼è¢«æ¿æ´»çHISR使ç¨head_liståtail_listå°HCBæå¨ä¸ä¸ªå项çé¾è¡¨ä¸ï¼å 为ç¸åä¼å 级çHISRä¸ä¼æ¢å 对æ¹ï¼å æ¤ä¸éè¦ååé¾è¡¨ï¼ä½¿ç¨ä¸¤ä¸ªæéç®çæ¯å å¿«HISRæ§è¡çé度ã
ä¸ä¸ªå®æ¶æä½ç³»ç»çæ ¸å¿å°±æ¯å¯¹äºä»»å¡çè°åº¦ï¼NUçè°åº¦çç¥æ¯time sliceåround robinçç®æ³ï¼
è°åº¦çé¨å主è¦æä¸ä¸ªå½æ°control_to_system()ç¨äºä¿åä¸ä¸æï¼å»ºç«solicited stackï¼å ³ä¸æï¼å ³system time sliceï¼å¹¶éç½®taskçtime slice为é¢è®¾å¼ï¼å°spæ´æ°ä¸ºsystem_stack_pointerï¼è°ç¨schedule()ï¼è°åº¦çè¿ç¨æ¯é常ç®åçæ¥è¯¢ï¼å°±æ¯æ¥çä¸¤ä¸ªå ¨å±çåéï¼TCD_Execute_HISRåTCD_Execute_Taskï¼scheduleé¨åçå ³é®æ¯æå¼äºä¸æï¼ä¸ç¶å¦æå½å没æreadyçtaskææ¯è¢«æ¿æ´»çHISRï¼åsheduleæ»å¾ªç¯ä¸å»ï¼æ¥è¯¢å°ä¸ä¸ä¸ªåºè¯¥æ§è¡ç线ç¨å跳转è³control_to_thread(),å¨è¿ééæ°å¼å¯system time sliceï¼ç¶åå°çº¿ç¨çtc_stack_ptrå å ¥å°spä¸ï¼åæ¢è³çº¿ç¨çæ ä¸ï¼ä¾æ¬¡popåºæ¥ï¼å³å®æäºä»»å¡è°åº¦ã
ä»»å¡çåæ¢ä¸»è¦æ¯ä¸ä¸æçåæ¢ï¼ä¹å°±æ¯taskæ çåæ¢ï¼å½æ°çè°ç¨ä¼ä¿åé¨åregsåè¿åå°åï¼è¿äºå¨ä½é½æ¯ç¼è¯å¨æ¥å®æçï¼èOSä¸çä»»å¡åæ¢æ¯è¿è¡æ¶ï¼runtimeï¼çä¸ç§ç¶æååï¼å æ¤ç¼è¯å¨ä¹æ è½ä¸ºåï¼æ以对äºä¸ä¸æçä¿åéè¦ä»£ç æ¥å®ç°ã
ä»»å¡çæ¢å æ¯å¼æ¥çå æ¤å¿ é¡»è¦éè¿ä¸ææ¥å®ç°ï¼ä¸è¬æ¯æ¬¡timerçä¸æå³å®å½åçtaskçslice timeæ¯å¦expiredï¼ç¶å设置TCT_Set_Execute_Task为ç¸åä¼å 级çå ¶ä»taskææ´é«ä¼å 级çtaskï¼é«ä¼å 级çtaskæ¢å ä½ä¼å 级çtaskï¼ä¸è¬æ¯å¤é¨ä¸æ触åï¼å¨HISRä¸resume_task()å¤éé«ä¼å 级çtaskï¼ç¶åscheduleå°é«ä¼å 级çtaskä¸ï¼å 为timerçHISRæ¯å¨ç³»ç»åå§å就已ç»æ³¨åçï¼åªæ¯æ§è¡timeoutåtime sliceè¶ æ¶åçæä½ï¼å¹¶æ²¡ææ§è¡resume_taskçå¨ä½ã
NUä¸çstackæ两ç§solicited stackåinterrupt stackï¼solicited stackæ¯ä¸ç§minmum stackï¼èinterrupt stackæ¯å¯¹å½åææå¯åå¨å ¨é¨ä¿åï¼TCBä¸çminimum stack size = ç³è¯·å¾å°stack size - solicited stackï¼å¨arm modeä¸å åèï¼thumb modeä¸å åèï¼ï¼thumbæ å¿ç¨æ¥è®°å½ä¸ä¸æä¿åæ¶çARMçå·¥ä½æ¨¡å¼ï¼c代ç ç¼è¯ä¸ºthumb模å¼ï¼è¿æ ·å¯ä»¥åå°code sizeï¼æé«ä»£ç å¯åº¦ï¼assembly代ç ç¼è¯ä¸ºarm模å¼æå代ç çæçï¼NUä¸å æ ¸ç代ç ä¸å¤ï¼ä¸»è¦æ¯assembly代ç ãstackçç±»åä¸å ¶ä¸PCæåçshellæ å ³ï¼interrupt stackä¿åçæ¯taskææ¯HISRå¨æ§è¡çè¿ç¨ä¸è¢«ä¸ææ¶çç°åºï¼solicited stack建ç«çå°æ¹å æ¬ control_to_system()ãschedule_protect()åsend_signals()åéç»å æprotectèµæºçtaskçæ åµï¼HISR_Shell()æ§è¡å®åä¼å»ºç«solicited stackï¼å跳转è³scheduleã
(Lower Address) Stack Top -> 1 (Interrupt stack type)
CPSR Saved CPSR
r0 Saved r0
r1 Saved r1
r2 Saved r2
r3 Saved r3
r4 Saved r4
r5 Saved r5
r6 Saved r6
r7 Saved r7
r8 Saved r8
r9 Saved r9
r Saved r
r Saved r
r Saved r
sp Saved sp
lr Saved lr
(Higher Address) Stack Bottom-> pc Saved pc
(Lower Address) Stack Top -> 0 (Solicited stack type)
!!FOR THUMB ONLY!! 0/0x Saved state mask
r4 Saved r4
r5 Saved r5
r6 Saved r6
r7 Saved r7
r8 Saved r8
r9 Saved r9
r Saved r
r Saved r
r Saved r
(Higher Address) Stack Bottom-> pc Saved pc
ä¸ä¸ªç®åçä¾å说æstackçæ åµï¼é¦å æ¯ä¸ä¸ªtaskå¨ready(executing)çç¶æä¸ï¼èä¸time sliceè¶ æ¶äºï¼timerä¸æåçåï¼ä¿åtaskä¸ä¸æinterrupt_contex_save()ï¼å¨taskçtc_stack_ptræåçå°æ¹å»ºç«ä¸ææ
taskA |interrupt stack|___tc_stack_ptr æ 顶端æ¯pc=lr-4
ARM对äºä¸æçå¤å®åçå¨å½åæ令å®æexecuteæ¶ï¼åæ¶pipelineçåå pc=pc+8ï¼å ¥æ æ¶å°±ælr-4é¦å æ¾å¨stackçæé«ç«¯ï¼highï¼ã
timerçLISRå®æåæ¿æ´»äºHISRï¼æ§è¡TCC_Time_slice()å°å½åtask移å°ç¸åä¼å 级ç尾端ï¼å¹¶ä¸è®¾ç½®ä¸ä¸ä¸ªè¦æ§è¡çtaskï¼HISRå¨æ 顶端ä¿åçæ¯è¿ä¸ªHISR_shellçå ¥å£å°åï¼å 为taskçæ§è¡å®å°±finishedï¼HISRæ¯å¯éå ¥ç
HISR |solicited stack| æ 顶端æ¯HISR_shell_entry
ä¸æï¼interruptï¼
åé¢å·²ç»æåäºä¸æçåºæ¬æä½ï¼è¿éå°±åä¸äºä»£ç è·¯å¾çç»èï¼ä¸æçæ§è¡ä¸»è¦æ¯ä¸¤ä¸ªé¨åLISRåHISRï¼åæ两个é¨åçç®çå°±æ¯å°å ³ä¸æçæ¶é´æå°åï¼å¹¶ä¸å¨LISRä¸å¼ä¸æå 许ä¸æçåµå¥ï¼ä»¥å建ç«ä¸æä¼å 级ï¼é½å¯ä»¥åå°ä¸æç延è¿ï¼ä¿è¯OSçå®æ¶æ§ã
NUçä¸æ模å¼æ¯å¯éå ¥çä¸æå¤çæ¹å¼ï¼ä¹å°±æ¯åºäºä¸æä¼å 级ååµå¥ç模å¼ï¼ä¸æçåµå¥å¨å¤ççè¿ç¨ä¸åºå¯¹lr_irq_modeå¯åå¨è¿è¡ä¿åï¼å 为é«ä¼å 级çä¸æåçæ¶ä¼è¦çæä½ä¼å 级ä¸æçråspsrï¼å æ¤è¦å©ç¨ç³»ç»çæ æ¥ä¿åä¸ææ ã
NU对äºä¸æä¸ä¸æçä¿åå ·ä½æä½å¦ä¸ï¼
ï¼1ï¼å¨ä¸æåçåæ§è¡çå ¥å£å½æ°INT_IRQ()ä¸ï¼å°r0-r4ä¿åè³irqçæ ä¸
ï¼2ï¼æ¥æ¾å°å¯¹åºçinterrupt_shell()ï¼clearä¸ææºï¼æ´æ°å ¨å±çä¸æ计æ°å¨ï¼ç¶åè¿è¡interrupt_contex_save
ï¼3ï¼é¦å å©ç¨r1,r2,r3ä¿åirq模å¼ä¸çsp,lr,spsrï¼è¿éspæ¯ç¨æ¥åæ¢è³ç³»ç»æ åæ·è´lråspsrç,è¿éä¿ålråspsræ¯ç®çæ¯task被æ¢å åï¼å½å次scheduleæ¶å¯ä»¥è¿åtaskä¹åçç¶æã
ï¼4ï¼åæ¢è³SVC模å¼ï¼å¦ææ¯éåµå¥çä¸æåä¿åä¸ä¸æè³task stackä¸ï¼å°irq模å¼ä¸çlrä½ä¸ºé¡¶ç«¯PCçè¿åå¼å ¥æ ï¼å°SVC模å¼ä¸çr6-rå ¥æ ï¼å°irq模å¼ä¸çspä¿åè³r4ä¸å ¥æ ï¼æåå°ä¿åå¨irq_stackä¸çr0-r4å ¥æ
ï¼5ï¼å¦ææ¯åµå¥ä¸æï¼ä¸æçåµå¥åçå¨LISRä¸ï¼å¨æ§è¡LISRæ¶å·²ç»åæ¢è³system stackï¼å æ¤åµå¥ä¸æè¦å°ä¸æçä¸ä¸æä¿åè³system stackä¸ï¼ä¸task stackä¸interrupt stackç¸æ¯åªæ¯å°äºæ 顶ç¨æ¥æ è®°åµå¥çæ å¿ï¼1 not nestedï¼
ï¼6ï¼æä¸ä¸ªåæ¯å¤æï¼å°±æ¯å¦æå½å线ç¨æ¯ç©ºï¼å³TCD_Current_Thread == NULLï¼è¡¨æå½åæ¯scheduleä¸ï¼å 为åå§å线ç¨æ¯å ³ä¸æçï¼è¿æ ·å°±ä¸ä¸ºschedule线ç¨å»ºç«æ 帧ï¼å 为scheduleä¸éè¦ä¿åä¸ä¸æï¼å¨restoreä¸æä¸ä¸ææ¶ç´æ¥è·³è½¬è³scheduleã
ä¸æä¸ä¸æçæ¢å¤
å ¨å±çä¸æ计æ°å¨INT_Countæ¯å¦ä¸º0æ¥å¤å®å½ååºæ çä¿¡æ¯ï¼å¦ææ¯åµå¥åè¿åLISRä¸ï¼å¦ååæ¢è³system stackæ§è¡schedule
timer
timerä¸ä¸æç´§å¯ç¸å ³ï¼å ¶å®timerä¹æ¯ä¸æçä¸ç§ï¼åªæ¯åçä¸æçé¢çè¾é«ï¼ä¸ä½ç¨é大ï¼ä¸ä¸ªå®æ¶æä½ç³»ç»ï¼æ¶é´æ¯é常éè¦çä¸é¨åï¼NUä¸çtimer主è¦æå个ä½ç¨ï¼
ï¼1ï¼ç»´æ¤ç³»ç»æ¶é TMD_system_clock
ï¼2ï¼taskçtime slice
ï¼3ï¼taskçsuspend timeout timer
ï¼4ï¼application timer
å ¶ä¸ï¼3ï¼ï¼4ï¼å ±ç¨ä¸ç§æºå¶ï¼ä¸ä¸ªå ¨å±çæ¶é´è½´TMD_timerï¼timeout timeråapp timeré½å»ºç«å¨ä¸ä¸ªTM_TCBçæ°æ®ç»æä¸ï¼éè¿tm_remaining_timeæ¥è¡¨å¾å½åtimerçå©ä½æ¶é´ï¼ä¾å¦å½åætimer_listä¸æä¸ä¸ªTM_TCBï¼ä¾æ¬¡æ¯Ta = 5ï¼Tb = 7, Tc = ,é£ä¹å»ºç«çé¾è¡¨ä¸å©ä½æ¶é´ä¾æ¬¡æ¯5ï¼2ï¼8ï¼å¦æç°å¨è¦å å ¥ä¸ä¸ªæ°çtimeræ ¹æ®timerå¼æå ¥è³åéçä½ç½®ï¼å¦ææå ¥çtimer为ï¼åå®æå¨Tbåé¢ï¼å©ä½æ¶é´ä¸º1ï¼åé¢ç8æ¹ä¸º7ï¼å½åçäºtimer expiredï¼å触åtimer_HISRï¼å¦ææ¯app timeråæ§è¡timer callbackï¼å¦ææ¯task timeout timerï¼åæ§è¡TCC_Task_Timeoutå¤étaskã
ï¼2ï¼çå®ç°ä¹æ¯ä¾èµäºå ¨å±çtime sliceæ¶é´è½´ï¼æ¯ä¸ä¸ªtaskå¨æ§è¡æ¶é½ä¼å°èªå·±çæ¶é´çä¿¡æ¯æ´æ°è³å ¨å±çæ¶é´è½´ä¸ï¼å½ä¸ä¸ªtaskçtime sliceæ§è¡å®å¨timer HISRä¸è°ç¨TCC_task_Timeoutå°å½åçtaskæ¾å¨ç¸åä¼å 级listçæ尾端ï¼å¹¶è®¾ç½®ä¸ä¸ä¸ªæé«ä¼å 级çä»»å¡ãtaskå¨æ§è¡çè¿ç¨ä¸åªæ被ä¸æåtime sliceä¼ä¿åä¸æ¥ï¼å ¶ä»è®©åºå¤çå¨çæ åµé½ä¼å°time sliceæ´æ°ä¸ºé¢è®¾å¼ã
protect
protectä¸linuxçéæºå¶ç±»ä¼¼ï¼äºæ¥è®¿é®ï¼å©ç¨å¼å ³ä¸ææ¥å®ç°ï¼å¹¶ä¸æ¥æprotectçtaskæ¯ä¸å¯ä»¥suspendçï¼å¿ é¡»è¦å°protectéæ¾åæå¯ä»¥æèµ·ï¼å½ä¸ä¸ªä¼å 级è¾ä½çtaskå æprotectèµæºï¼å¦æ被æ¢å ï¼ä¸ä¸ªé«ä¼å 级çtaskæHISRå¨è¯·æ±protectèµæºæ¶ä¼æ§è¡TCC_schedule_protect()让åºå¤çå¨ç»ä½ä¼å 级çtaskæ§è¡ï¼ç´å°ä½ä¼å 级çtaskæ§è¡unprotect()为æ¢ï¼æ¤æ¶taskæHISR建ç«çæ¯solicited stackï¼åæ¶å¨control_to_threadåå¼å ³ä¸æä¸æ¬¡ï¼è¿æ ·å¯ä»¥åå°ä¸æ¬¡ä¸ä¸æçåæ¢ãNUä¸å¸¸ç¨å°çæ¯system_protectï¼å®å°±æ¯ä¸æ大éï¼ä¿æ¤å æ ¸ä¸ææå ¨å±æ°æ®ç»æç顺åºè®¿é®ï¼ç²åº¦å¾å¤§ã
LISRä¸ä¸å¯ä»¥è¯·æ±protectèµæºï¼å 为LISRæ¯ä¸ætaskåæ§è¡ï¼å¦ætaskå æprotectèµæºï¼è¿æ¶LISRåå»è¯·æ±protectèµæºï¼ä¼åçæ»éï¼å 为LISR让åºå¤çå¨åï¼schedule没åæ³å次è°åº¦å°LISRæ§è¡ï¼ååçæ»éé误ï¼å æ¤å¨LISRä¸é¤äºactivate_HISRï¼ï¼ä»¥å¤ä¸å¯ä»¥ä½¿ç¨system callï¼ä¾å¦resume_taskççï¼è¿åç³»ç»è°ç¨é½ä¼è¯·æ±protectèµæºã
对äºprotectç请æ±æç §ä¸å®ç顺åºå¯ä»¥é²æ¢æ»éï¼NUçæºç ä¸ä¸è¬å°system_protectèµæºç请æ±æ¾å¨åé¢ï¼å ¶ä»å¦DM_protectå 请æ±ã