?? os_cpu_c.c
字號:
/*
*********************************************************************************************************
* uC/OS-II
* The Real-Time Kernel
*
* (c) Copyright 1992-2001, Jean J. Labrosse, Weston, FL
* All Rights Reserved
*
*
* 80x86/80x88 Specific code
* PROTECTED MEMORY MODEL
*
* vc++6.0
*
* File : OS_CPU_C.C
* By : Jean J. Labrosse
changed : 文佳 Email: ganganwen@163.com
*********************************************************************************************************
*/
#define OS_CPU_GLOBALS
#include "includes.h"
/*
*********************************************************************************************************
* INITIALIZE A TASK'S STACK
*
* Description: This function is called by either OSTaskCreate() or OSTaskCreateExt() to initialize the
* stack frame of the task being created. This function is highly processor specific.
* 在console下沒有中斷調用,應此也沒有中斷返回地址,vc編譯后的保護模式下的段地址沒有變的
* 應此也不用壓棧,簡單起見,我也沒有把浮點寄存器壓棧
*
* Arguments : task is a pointer to the task code
*
* pdata is a pointer to a user supplied data area that will be passed to the task
* when the task first executes.
*
* ptos is a pointer to the top of stack. It is assumed that 'ptos' points to
* a 'free' entry on the task stack. If OS_STK_GROWTH is set to 1 then
* 'ptos' will contain the HIGHEST valid address of the stack. Similarly, if
* OS_STK_GROWTH is set to 0, the 'ptos' will contains the LOWEST valid address
* of the stack.
*
* opt specifies options that can be used to alter the behavior of OSTaskStkInit().
* (see uCOS_II.H for OS_TASK_OPT_???).
*
* Returns : Always returns the location of the new top-of-stack' once the processor registers have
* been placed on the stack in the proper order.
*
* Note(s) : Interrupts are enabled when your task starts executing. You can change this by setting the
* PSW to 0x0002 instead. In this case, interrupts would be disabled upon task startup. The
* application code would be responsible for enabling interrupts at the beginning of the task
* code. You will need to modify OSTaskIdle() and OSTaskStat() so that they enable
* interrupts. Failure to do this will make your system crash!
*********************************************************************************************************
*/
OS_STK *OSTaskStkInit (void (*task)(void *pd), void *pdata, OS_STK *ptos, INT16U opt)
{
INT32U *stk; //console 下寄存器為32位寬
opt = opt; /* 'opt' is not used, prevent warning */
stk = (INT32U *)ptos; /* Load stack pointer */
*--stk = (INT32U)pdata; /* Simulate call to function with argument */
*--stk = (INT32U)0X00000000; //子程序是從當前esp+4處取得傳入的參數,所以此處要空出4個字節
*--stk = (INT32U)task; /* Put pointer to task on top of stack */
*--stk = (INT32U)0x00000202; /* EFL = 0X00000202 */
*--stk = (INT32U)0xAAAAAAAA; /* EAX = 0xAAAAAAAA */
*--stk = (INT32U)0xCCCCCCCC; /* ECX = 0xCCCCCCCC */
*--stk = (INT32U)0xDDDDDDDD; /* EDX = 0xDDDDDDDD */
*--stk = (INT32U)0xBBBBBBBB; /* EBX = 0xBBBBBBBB */
*--stk = (INT32U)0x00000000; /* ESP = 0x00000000 esp可以任意,因為 */
*--stk = (INT32U)0x11111111; /* EBP = 0x11111111 */
*--stk = (INT32U)0x22222222; /* ESI = 0x22222222 */
*--stk = (INT32U)0x33333333; /* EDI = 0x33333333 */
return ((OS_STK *)stk);
}
/**********************************************************************************************************
; START MULTITASKING
; void OSStartHighRdy(void)
;
; The stack frame is assumed to look as follows:
;
; OSTCBHighRdy->OSTCBStkPtr --> EDI (Low memory)
; ESI
; EBP
; ESP
; EBX
; EDX
; ECX
; EAX
; Flags to load in PSW
; OFFSET of task code address
; 4 empty byte
; OFFSET of 'pdata' (High memory)
;
;
; Note : OSStartHighRdy() MUST:
; a) Call OSTaskSwHook() then,
; b) Set OSRunning to TRUE,
; c) Switch to the highest priority task.
;*********************************************************************************************************/
void OSStartHighRdy(void)
{
OSTaskSwHook();
OSRunning = TRUE;
_asm{
mov ebx, [OSTCBCur] ;OSTCBCur結構的第一個參數就是esp
mov esp, [ebx] ;恢復堆棧
popad ;恢復所有通用寄存器,共8個
popfd ;恢復標志寄存器
ret ;ret 指令相當于pop eip 但保護模式下不容許使用eip
;永遠都不返回
}
}
/*$PAGE*/
/*********************************************************************************************************
; PERFORM A CONTEXT SWITCH (From task level)
; void OSCtxSw(void)
;
; Note(s): 1) 此函數為手動任務切換函數
;
; 2) The stack frame of the task to suspend looks as follows:
;
; SP -> OFFSET of task to suspend (Low memory)
; PSW of task to suspend (High memory)
;
; 3) The stack frame of the task to resume looks as follows:
;
; OSTCBHighRdy->OSTCBStkPtr --> EDI (Low memory)
; ESI
; EBP
; ESP
; EBX
; EDX
; ECX
; EAX
; Flags to load in PSW
; OFFSET of task code address (High memory)
;
;*********************************************************************************************************/
void OSCtxSw(void)
{
_asm{
lea eax, nextstart ;任務切換回來后從nextstart開始
push eax
pushfd ;標志寄存器的值
pushad ;保存EAX -- EDI
mov ebx, [OSTCBCur]
mov [ebx], esp ;把堆棧入口的地址保存到當前TCB結構中
}
OSTaskSwHook();
OSTCBCur = OSTCBHighRdy;
OSPrioCur = OSPrioHighRdy;
_asm{
mov ebx, [OSTCBCur]
mov esp, [ebx] ;得到OSTCBHighRdy的esp
popad ;恢復所有通用寄存器,共8個
popfd ;恢復標志寄存器
ret ;跳轉到指定任務運行
}
nextstart: //任務切換回來的運行地址
return;
}
/*********************************************************************************************************
; PERFORM A CONTEXT SWITCH (From an ISR)
; void OSIntCtxSw(void)
;
; Note(s): 1) Context保存了主線程所以的上下文,因此切換起來比較簡單,先保存相應寄存器
然后把要運行的任務的上下文填入Context結構并保存,完成切換,堆棧內容如下 :
;
; OSTCBHighRdy->OSTCBStkPtr --> EDI (Low memory)
; ESI
; EBP
; ESP
; EBX
; EDX
; ECX
; EAX
; Flags to load in PSW
; OFFSET of task code address (High memory)
;*********************************************************************************************************/
void OSIntCtxSw(void)
{
OS_STK *sp;
OSTaskSwHook();
sp = (OS_STK *)Context.Esp; //得到主線程當前堆棧指針
//在堆棧中保存相應寄存器。
*--sp = Context.Eip; //先保存eip
*--sp = Context.EFlags; //保存efl
*--sp = Context.Eax;
*--sp = Context.Ecx;
*--sp = Context.Edx;
*--sp = Context.Ebx;
*--sp = Context.Esp; //此時保存的esp是錯誤的,但OSTCBCur保存了正確的
*--sp = Context.Ebp;
*--sp = Context.Esi;
*--sp = Context.Edi;
OSTCBCur->OSTCBStkPtr = (OS_STK *)sp; //保存當前esp
OSTCBCur = OSTCBHighRdy; //得到當前就緒最高優先級任務的tcb
OSPrioCur = OSPrioHighRdy; //得到當前就緒任務最高優先級數
sp = OSTCBHighRdy->OSTCBStkPtr; //得到重新執行的任務的堆棧指針
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -