usrWdbTask.c
上传用户:luoyougen
上传日期:2008-05-12
资源大小:23136k
文件大小:11k
源码类别:

VxWorks

开发平台:

C/C++

  1. /* usrWdbTask.c - configuration file for the WDB task agent */
  2. /* Copyright 1997-2001 Wind River Systems, Inc. */
  3. /*
  4. modification history
  5. --------------------
  6. 01j,26nov01,jhw  Moved wdbInfo back into WIND_TCB.
  7. 01i,20oct01,jhw  Moved wdbExitHook out of WIND_TCB.
  8. 01h,29may01,pch  Fix compiler warning
  9. 01g,18apr01,kab  Added Altivec support
  10. 01f,16nov00,zl   added DSP support.
  11. 01e,07apr99,cym  changing vxTaskRegsGet to match with src/config.
  12.  (SPR #26387)
  13. 01d,15feb99,dbt  remove task creation hook only if it has been previously
  14.                  installed (SPR #24195)
  15. 01c,18may98,dbt  integrated latest WDB stuff
  16. 01b,09oct97,ms   use _func_ioTaskStdSet to break binding to iosLib
  17. 01a,21jul97,ms   taken from 01y of usrWdb.c. moved some macros to configAll.h
  18. */
  19. /*
  20. DESCRIPTION
  21. This library configures and initializes the WDB task agent.
  22. */
  23. /* defines */
  24. #define MILLION 1000000
  25. /* globals */
  26. FUNCPTR wdbTaskFpRegsGet;
  27. FUNCPTR wdbTaskFpRegsSet;
  28. FUNCPTR wdbTaskDspRegsGet;
  29. FUNCPTR wdbTaskDspRegsSet;
  30. FUNCPTR wdbTaskAvRegsGet;
  31. FUNCPTR wdbTaskAvRegsSet;
  32. /* externals */
  33. extern WDB_RT_IF  wdbRtIf;
  34. extern VOIDFUNCPTR wdbTaskRestartHook;
  35. extern BOOL wdbIsInitialized;
  36. /* locals */
  37. static VOIDFUNCPTR      wdbCreateHook = NULL;   /* WDB task create hook */
  38. /* forward declarations */
  39. static void wdbTaskRtIfInit ();
  40. /******************************************************************************
  41. *
  42. * wdbTaskRestart - restart the WDB task
  43. */
  44. static void wdbTaskRestart (void)
  45.     {
  46.     static int  restartCnt;
  47.     extern int  wdbTaskId;
  48.     /*
  49.      * if the exception is in the agent task, restart the agent
  50.      * after a delay.
  51.      */
  52.     if (((int)taskIdCurrent == wdbTaskId) && (restartCnt < WDB_MAX_RESTARTS))
  53.         {
  54.         restartCnt++;
  55.         if (_func_logMsg != NULL)
  56.             _func_logMsg ("WDB exception. restarting agent in %d seconds...n",
  57.                 WDB_RESTART_TIME, 0,0,0,0,0);
  58.         taskDelay (sysClkRateGet() * WDB_RESTART_TIME);
  59.         taskRestart (0);
  60.         }
  61.     }
  62. /******************************************************************************
  63. *
  64. * wdbTaskModeInit - configure and initialize the WDB task mode agent.
  65. *
  66. */
  67. STATUS wdbTaskModeInit (void)
  68.     {
  69.     if (!wdbIsInitialized) /* test if WDB is initialized */
  70. return (ERROR);
  71.     wdbTaskRtIfInit ();
  72.     if (wdbTaskInit (WDB_TASK_PRIORITY,
  73. WDB_TASK_OPTIONS, NULL, WDB_STACK_SIZE) == ERROR)
  74. return (ERROR);
  75.     wdbModeSet (WDB_MODE_TASK);
  76.     wdbTaskRestartHook = wdbTaskRestart;
  77.     return (OK);
  78.     }
  79. /******************************************************************************
  80. *
  81. * vxTaskCreate - WDB callout to create a task (and leave suspended).
  82. */
  83. static int vxTaskCreate
  84.     (
  85.     char *   name,       /* name of new task (stored at pStackBase)   */
  86.     int      priority,   /* priority of new task                      */
  87.     int      options,    /* task option word                          */
  88.     caddr_t  stackBase,  /* base of stack. ignored by VxWorks       */
  89.     int      stackSize,  /* size (bytes) of stack needed plus name    */
  90.     caddr_t  entryPt,    /* entry point of new task                   */
  91.     int      arg[10],  /* 1st of 10 req'd task args to pass to func */
  92.     int      fdIn,  /* fd for input redirection       */
  93.     int      fdOut,  /* fd for output redirection       */
  94.     int      fdErr  /* fd for error output redirection       */
  95.     )
  96.     {
  97.     int tid;
  98.     if (stackSize == 0)
  99. stackSize = WDB_SPAWN_STACK_SIZE;
  100.     tid = taskCreat (name, priority, options, stackSize, (int (*)())entryPt,
  101. arg[0], arg[1], arg[2], arg[3], arg[4], arg[5], arg[6],
  102. arg[7], arg[8], arg[9]);
  103.     if (tid == 0) /* create failed */
  104. return (ERROR);
  105.     if (_func_ioTaskStdSet != NULL)
  106. {
  107. if (fdIn != 0)
  108.     _func_ioTaskStdSet (tid, 0, fdIn);
  109. if (fdOut != 0)
  110.     _func_ioTaskStdSet (tid, 1, fdOut);
  111. if (fdErr != 0)
  112.     _func_ioTaskStdSet (tid, 2, fdErr);
  113. }
  114.     return (tid);
  115.     }
  116. /******************************************************************************
  117. *
  118. * vxTaskResume - WDB callout to resume a suspended task.
  119. */
  120. static STATUS vxTaskResume
  121.     (
  122.     WDB_CTX * pContext
  123.     )
  124.     {
  125.     if (pContext->contextType != WDB_CTX_TASK)
  126. return (ERROR);
  127.     return (taskResume (pContext->contextId));
  128.     }
  129. /******************************************************************************
  130. *
  131. * vxTaskSuspend - WDB callout to suspend a task.
  132. */
  133. static STATUS vxTaskSuspend
  134.     (
  135.     WDB_CTX * pContext
  136.     )
  137.     {
  138.     if (pContext->contextType != WDB_CTX_TASK)
  139. return (ERROR);
  140.     return (taskSuspend (pContext->contextId));
  141.     }
  142. /******************************************************************************
  143. *
  144. * vxTaskDelete - WDB callout to delete a task.
  145. */
  146. static STATUS vxTaskDelete
  147.     (
  148.     WDB_CTX * pContext
  149.     )
  150.     {
  151.     if (pContext->contextType != WDB_CTX_TASK)
  152. return (ERROR);
  153.     return (taskDelete (pContext->contextId));
  154.     }
  155. /******************************************************************************
  156. *
  157. * vxTaskRegsSet - WDB callout to get a task register set.
  158. */
  159. static STATUS vxTaskRegsSet
  160.     (
  161.     WDB_CTX *  pContext,
  162.     WDB_REG_SET_TYPE regSetType,
  163.     char *  pRegSet
  164.     )
  165.     {
  166.     if (pContext->contextType != WDB_CTX_TASK)
  167. return (ERROR);
  168.     switch (regSetType)
  169. {
  170. case WDB_REG_SET_IU:
  171.     return (taskRegsSet (pContext->contextId, (REG_SET *)pRegSet));
  172. case WDB_REG_SET_FPU:
  173.     if (wdbTaskFpRegsSet == NULL)
  174. return (ERROR);
  175.     return (wdbTaskFpRegsSet (pContext, pRegSet));
  176. case WDB_REG_SET_DSP:
  177.     if (wdbTaskDspRegsSet == NULL)
  178. return (ERROR);
  179.     return (wdbTaskDspRegsSet (pContext, pRegSet));
  180. case WDB_REG_SET_AV:
  181.     if (wdbTaskAvRegsSet == NULL)
  182. return (ERROR);
  183.     return (wdbTaskAvRegsSet (pContext, pRegSet));
  184. default:
  185.     return (ERROR);
  186. }
  187.     }
  188. /******************************************************************************
  189. *
  190. * vxTaskRegsGet - WDB callout to get a tasks register set.
  191. *
  192. * This routine is not reentrant, but it it only called by one thread (the
  193. * WDB agent).
  194. */
  195. static STATUS vxTaskRegsGet
  196.     (
  197.     WDB_CTX * pContext,
  198.     WDB_REG_SET_TYPE  regSetType,
  199.     char ** ppRegSet
  200.     )
  201.     {
  202.     WIND_TCB * pTcb;
  203.     if (pContext->contextType != WDB_CTX_TASK)
  204. return (ERROR);
  205.     pTcb = taskTcb (pContext->contextId);
  206.     if (pTcb == NULL)
  207. return (ERROR);
  208.     switch (regSetType)
  209. {
  210. case WDB_REG_SET_IU:
  211.     {
  212.     REG_SET dummy;
  213.     /* DO NOT REMOVE THIS CALL TO taskRegsGet (SPR #26387) */
  214.     taskRegsGet (pContext->contextId,&dummy);
  215.     *ppRegSet = (char *) &pTcb->regs;
  216.     return (OK);
  217.     }
  218. case WDB_REG_SET_FPU:
  219.     if (wdbTaskFpRegsGet == NULL)
  220. return (ERROR);
  221.     return (wdbTaskFpRegsGet (pContext, ppRegSet));
  222. case WDB_REG_SET_DSP:
  223.     if (wdbTaskDspRegsGet == NULL)
  224. return (ERROR);
  225.     return (wdbTaskDspRegsGet (pContext, ppRegSet));
  226. case WDB_REG_SET_AV:
  227.     if (wdbTaskAvRegsGet == NULL)
  228. return (ERROR);
  229.     return (wdbTaskAvRegsGet (pContext, ppRegSet));
  230. default:
  231.     return (ERROR);
  232. }
  233.     }
  234. /******************************************************************************
  235. *
  236. * vxSemCreate - create a SEMAPHORE
  237. */
  238. static void * vxSemCreate (void)
  239.     {
  240.     return ((void *)semBCreate (0, 0));
  241.     }
  242. /******************************************************************************
  243. *
  244. * vxSemGive - give a semaphore
  245. */
  246. static STATUS vxSemGive
  247.     (
  248.     void * semId
  249.     )
  250.     {
  251.     return (semGive ((SEM_ID)semId));
  252.     }
  253. /******************************************************************************
  254. *
  255. * vxSemTake - take a semaphore
  256. */
  257. static STATUS vxSemTake
  258.     (
  259.     void * semId,
  260.     struct timeval * tv
  261.     )
  262.     {
  263.     return (semTake ((SEM_ID) semId, 
  264. (tv == NULL ? WAIT_FOREVER :
  265. tv->tv_sec * sysClkRateGet() +
  266. (tv->tv_usec * sysClkRateGet()) / MILLION)));
  267.     }
  268. /******************************************************************************
  269. *
  270. * __wdbTaskDeleteHook -
  271. */
  272. static int __wdbTaskDeleteHook
  273.     (
  274.     WIND_TCB *pTcb
  275.     )
  276.     {
  277.     WDB_CTX     ctx;
  278.     void        (*hook)();
  279.     hook = (pTcb->wdbInfo).wdbExitHook;
  280.     if (hook != NULL)
  281.         {
  282.         ctx.contextType = WDB_CTX_TASK;
  283.         ctx.contextId   = (UINT32)pTcb;
  284.         (*hook) (ctx, pTcb->exitCode, pTcb->errorStatus);
  285.         }
  286.     return (OK);
  287.     }
  288. /******************************************************************************
  289. *
  290. * vxTaskDeleteHookAdd - task-specific delete hook (one per task).
  291. *
  292. * currently only one hook per task.
  293. */
  294. static STATUS vxTaskDeleteHookAdd
  295.     (
  296.     UINT32      tid,
  297.     void        (*hook)()
  298.     )
  299.     {
  300.     static BOOL initialized = FALSE;
  301.     if (taskIdVerify ((int)tid) == ERROR)
  302.         return (ERROR);
  303.     (taskTcb (tid)->wdbInfo).wdbExitHook = hook;
  304.     if (!initialized)
  305.         {
  306.         taskDeleteHookAdd (__wdbTaskDeleteHook);
  307.         initialized = TRUE;
  308.         }
  309.     return (OK);
  310.     }
  311. /******************************************************************************
  312. *
  313. * __wdbTaskCreateHook - task create hook
  314. *
  315. * This hook is called each time a task is created.
  316. *
  317. * RETURNS : OK always
  318. */ 
  319. static int __wdbTaskCreateHook
  320.     (
  321.     WIND_TCB * pTcb
  322.     )
  323.     {
  324.     WDB_CTX createdCtx;
  325.     WDB_CTX creationCtx;
  326.     if (wdbCreateHook != NULL)
  327. {
  328. /* fill createdCtx structure */
  329. createdCtx.contextType = WDB_CTX_TASK;
  330. createdCtx.contextId = (UINT32)pTcb;
  331. /* fill creationCtx structure */
  332. creationCtx.contextType = WDB_CTX_TASK;
  333. creationCtx.contextId = (UINT32)taskIdCurrent;
  334. (*wdbCreateHook) (&createdCtx, &creationCtx);
  335. }
  336.     return (OK);
  337.     }
  338. /******************************************************************************
  339. *
  340. * vxTaskCreateHookAdd - install WDB task create hook.
  341. *
  342. * This routine installs or remove the WDB task create hook. 
  343. *
  344. * RETURNS : OK always.
  345. */ 
  346. static STATUS vxTaskCreateHookAdd
  347.     (
  348.     void (*hook)()
  349.     )
  350.     {
  351.     static BOOL initialized = FALSE;
  352.     wdbCreateHook = hook;
  353.     if ((hook == NULL) && initialized) /* remove task creation hook */
  354. {
  355. taskCreateHookDelete (__wdbTaskCreateHook);
  356. initialized = FALSE;
  357. }
  358.     else if (!initialized) /* install task creation hook */
  359. {
  360. taskCreateHookAdd (__wdbTaskCreateHook);
  361. initialized = TRUE;
  362. }
  363.     return (OK);
  364.     }
  365. /******************************************************************************
  366. *
  367. * wdbTaskRtIfInit - Initialize pointers to the VxWorks routines.
  368. */
  369. static void wdbTaskRtIfInit ()
  370.     {
  371.     wdbRtIf.taskCreate = vxTaskCreate;
  372.     wdbRtIf.taskResume = vxTaskResume;
  373.     wdbRtIf.taskSuspend = vxTaskSuspend;
  374.     wdbRtIf.taskDelete = vxTaskDelete;
  375.     wdbRtIf.taskLock = (VOIDFUNCPTR) taskLock;
  376.     wdbRtIf.taskUnlock = (VOIDFUNCPTR) taskUnlock;
  377.     wdbRtIf.taskRegsSet = vxTaskRegsSet;
  378.     wdbRtIf.taskRegsGet = vxTaskRegsGet;
  379.     wdbRtIf.malloc = malloc;
  380.     wdbRtIf.free = free;
  381.     wdbRtIf.semCreate = vxSemCreate;
  382.     wdbRtIf.semGive = vxSemGive;
  383.     wdbRtIf.semTake = vxSemTake;
  384.     wdbRtIf.taskDeleteHookAdd = vxTaskDeleteHookAdd;
  385.     wdbRtIf.taskSwitchHookAdd = (STATUS (*)())taskSwitchHookAdd;
  386.     wdbRtIf.taskCreateHookAdd = (STATUS (*)())vxTaskCreateHookAdd;
  387.     }