gonzui


Format: Advanced Search

tkernel_2/kernel/sysmgr/src/system.cbare sourcepermlink (0.02 seconds)

Search this content:

    1: /*
    2:  *----------------------------------------------------------------------
    3:  *    T-Kernel 2.0 Software Package
    4:  *
    5:  *    Copyright 2011 by Ken Sakamura.
    6:  *    This software is distributed under the latest version of T-License 2.x.
    7:  *----------------------------------------------------------------------
    8:  *
    9:  *    Released by T-Engine Forum(http://www.t-engine.org/) at 2011/05/17.
   10:  *    Modified by T-Engine Forum at 2014/09/10.
   11:  *    Modified by TRON Forum(http://www.tron.org/) at 2015/06/01.
   12:  *
   13:  *----------------------------------------------------------------------
   14:  */
   15: 
   16: /*
   17:  *      system.c (T-Kernel/SM)
   18:  *      System Information Management Function
   19:  *      System Memory Management Function
   20:  */
   21: 
   22: #include "sysmgr.h"
   23: #include "syslog.h"
   24: #include <sys/rominfo.h>
   25: #include <sys/imalloc.h>
   26: #include <sys/commarea.h>
   27: #include <sys/svc/ifsysmgr.h>
   28: #include <sys/sysinfo.h>
   29: 
   30: #define SYSCONF         ( SCInfo.sysconf )
   31: #define DEVCONF         ( SCInfo.devconf )
   32: 
   33: /* ------------------------------------------------------------------------ */
   34: /*
   35:  *      System information management function
   36:  */
   37: 
   38: /*
   39:  * Search 'name' information
   40:  *      If it is not found, return NULL.
   41:  */
   42: LOCAL CONST UB* search_conf( CONST UB *cp, CONST UB *name )
   43: {
   44:         size_t         len = STRLEN((char*)name);
   45:         CONST UB       *p;
   46: 
   47:         while ( *cp != '\0' ) {
   48:                 if ( *cp == name[0] ) {
   49:                         for ( p = cp; *p > ' ' && *p != '#'; ++p ) {
   50:                                 ;
   51:                         }
   52:                         if ( (size_t)(p - cp) == len && MEMCMP(cp, name, len) == 0 ) {
   53:                                 return cp; /* Found */
   54:                         }
   55:                 }
   56: 
   57:                 /* Next */
   58:                 while ( *cp != '\0' && *cp++ != '\n' ) {
   59:                         ;
   60:                 }
   61:         }
   62: 
   63:         return NULL;
   64: }
   65: 
   66: /*
   67:  * Skip to the next word
   68:  *      If it is the end, return NULL.
   69:  */
   70: LOCAL CONST UB* skip_next( CONST UB *p )
   71: {
   72:         UB     c;
   73: 
   74:         /* Skip reading current word */
   75:         while ( (c = *p) > ' ' && c != '#' ) {
   76:                 ++p;
   77:         }
   78: 
   79:         /* Skip reading word separators */
   80:         while ( (c = *p) != '\0' ) {
   81:                 if ( c == '\n' || c == '#' ) {
   82:                         break;
   83:                 }
   84:                 if ( c > ' ' ) {
   85:                         return p;
   86:                 }
   87:                 p++;
   88:         }
   89: 
   90:         return NULL;
   91: }
   92: 
   93: /*
   94:  * Get numeric value information
   95:  *      Called directly from sysinit.
   96:  */
   97: EXPORT INT getcfn( CONST UB *conf, CONST UB *name, INT *val, INT max )
   98: {
   99:         CONST UB       *p;
  100:         INT            v, n;
  101: 
  102:         p = search_conf(conf, name);
  103:         if ( p == NULL ) {
  104:                 return E_NOEXS; /* Not found */
  105:         }
  106: 
  107:         n = 0;
  108:         while ( (p = skip_next(p)) != NULL ) {
  109:                 v = STRTOUL((char*)p, (char**)&p, 0);
  110:                 if ( max-- > 0 ) {
  111:                         *val++ = v;
  112:                 }
  113:                 n++;
  114:         }
  115: 
  116:         return n;
  117: }
  118: 
  119: /*
  120:  * Get string information
  121:  *      Called directly from sysinit.
  122:  */
  123: EXPORT INT getcfs( CONST UB *conf, CONST UB *name, UB *bp, INT max )
  124: {
  125:         CONST UB       *p, *sp;
  126:         UB             c;
  127: 
  128:         p = search_conf(conf, name);
  129:         if ( p == NULL ) {
  130:                 return E_NOEXS; /* Not found */
  131:         }
  132: 
  133:         sp = skip_next(p);
  134:         if ( sp == NULL ) {
  135:                 return 0; /* Data is not defined */
  136:         }
  137: 
  138:         for ( p = sp; (c = *p) != '\0'; ++p ) {
  139:                 if ( c == '\n' || c == '#' ) {
  140:                         break;
  141:                 }
  142: 
  143:                 if ( max-- > 0 ) {
  144:                         *bp++ = c;
  145:                 }
  146:         }
  147:         if ( max > 0 ) {
  148:                 *bp = '\0';
  149:         }
  150: 
  151:         return p - sp;
  152: }
  153: 
  154: /*
  155:  * Get system information (numeric values)
  156:  *      Called directly from T-Kernel/OS.
  157:  *      Do not call system call and extension SVC inside because
  158:  *      this function is also called during system start-up.
  159:  */
  160: EXPORT INT _tk_get_cfn( CONST UB *name, INT *val, INT max )
  161: {
  162:         INT    n;
  163: 
  164:         n = getcfn(SYSCONF, name, val, max);
  165:         if ( n < 0 ) {
  166:                 n = getcfn(DEVCONF, name, val, max);
  167:         }
  168:         if ( n < 0 ) {
  169:                 goto err_ret;
  170:         }
  171: 
  172:         return n;
  173: 
  174: err_ret:
  175:         TM_DEBUG_PRINT(("_tk_get_cfn ercd = %d\n", n));
  176:         return n;
  177: }
  178: 
  179: /*
  180:  * Get system information (numeric values)
  181:  */
  182: LOCAL INT __tk_get_cfn( CONST UB *name, INT *val, INT max )
  183: {
  184:         ER     ercd;
  185: 
  186:         ercd = ChkSpaceBstrR(name, 0);
  187:         if ( ercd < E_OK ) {
  188:                 goto err_ret;
  189:         }
  190:         ercd = ChkSpaceRW(val, (INT)sizeof(INT) * max);
  191:         if ( ercd < E_OK ) {
  192:                 goto err_ret;
  193:         }
  194: 
  195:         ercd = _tk_get_cfn(name, val, max);
  196:         if ( ercd < E_OK ) {
  197:                 goto err_ret;
  198:         }
  199: 
  200:         return ercd;
  201: 
  202: err_ret:
  203:         DEBUG_PRINT(("__tk_get_cfn ercd = %d\n", ercd));
  204:         return ercd;
  205: }
  206: 
  207: /*
  208:  * Get system information (strings)
  209:  *      Called directly from T-Kernel/OS.
  210:  *      Do not call system call and extension SVC inside because
  211:  *      this function is also called during system start-up.
  212:  */
  213: EXPORT INT _tk_get_cfs( CONST UB *name, UB *buf, INT max )
  214: {
  215:         INT    n;
  216: 
  217:         n = getcfs(SYSCONF, name, buf, max);
  218:         if ( n < 0 ) {
  219:                 n = getcfs(DEVCONF, name, buf, max);
  220:         }
  221:         if ( n < 0 ) {
  222:                 goto err_ret;
  223:         }
  224: 
  225:         return n;
  226: 
  227: err_ret:
  228:         TM_DEBUG_PRINT(("_tk_get_cfs ercd = %d\n", n));
  229:         return n;
  230: }
  231: 
  232: /*
  233:  * Get system information (strings)
  234:  */
  235: LOCAL INT __tk_get_cfs( CONST UB *name, UB *buf, INT max )
  236: {
  237:         ER     ercd;
  238: 
  239:         ercd = ChkSpaceBstrR(name, 0);
  240:         if ( ercd < E_OK ) {
  241:                 goto err_ret;
  242:         }
  243:         ercd = ChkSpaceRW(buf, max);
  244:         if ( ercd < E_OK ) {
  245:                 goto err_ret;
  246:         }
  247: 
  248:         ercd = _tk_get_cfs(name, buf, max);
  249:         if ( ercd < E_OK ) {
  250:                 goto err_ret;
  251:         }
  252: 
  253:         return ercd;
  254: 
  255: err_ret:
  256:         DEBUG_PRINT(("__tk_get_cfs ercd = %d\n", ercd));
  257:         return ercd;
  258: }
  259: 
  260: /* ------------------------------------------------------------------------ */
  261: /*
  262:  *      System memory management function
  263:  *
  264:  *      Only the extension SVC entry part is processed here, while the
  265:  *      actual memory management functions are provided externally for
  266:  *      virtual memory system and real memory system, respectively.
  267:  *      Memory management functions are provided in the upper level for
  268:  *      virtual memory system, while it is provided in T-Kernel/SM for
  269:  *      real memory system.
  270:  */
  271: 
  272: #define VALID_MEMATR    (TA_RNG3 | TA_NORESIDENT | TA_NOCACHE)
  273: 
  274: /*
  275:  * Get system memory
  276:  */
  277: LOCAL ER _tk_get_smb( void **addr, INT nblk, UINT attr )
  278: {
  279:         ER     ercd;
  280: 
  281:         if ( (nblk <= 0) || ((attr & ~VALID_MEMATR) != 0) ) {
  282:                 ercd = E_PAR;
  283:                 goto err_ret;
  284:         }
  285:         ercd = ChkSpaceRW(addr, sizeof(void*));
  286:         if ( ercd < E_OK ) {
  287:                 goto err_ret;
  288:         }
  289: 
  290:         *addr = GetSysMemBlk(nblk, attr);
  291:         if ( *addr == NULL ) {
  292:                 ercd = E_NOMEM;
  293:                 goto err_ret;
  294:         }
  295: 
  296:         return E_OK;
  297: 
  298: err_ret:
  299:         DEBUG_PRINT(("_tk_get_smb ercd = %d\n", ercd));
  300:         return ercd;
  301: }
  302: 
  303: /*
  304:  * Free system memory
  305:  */
  306: LOCAL ER _tk_rel_smb( void *addr )
  307: {
  308:         ER     ercd;
  309: 
  310:         ercd = RelSysMemBlk(addr);
  311:         if ( ercd < E_OK ) {
  312:                 goto err_ret;
  313:         }
  314: 
  315:         return E_OK;
  316: 
  317: err_ret:
  318:         DEBUG_PRINT(("_tk_rel_smb ercd = %d\n", ercd));
  319:         return ercd;
  320: }
  321: 
  322: /*
  323:  * Get system memory information
  324:  */
  325: LOCAL ER _tk_ref_smb( T_RSMB *pk_rsmb )
  326: {
  327:         ER     ercd;
  328: 
  329:         ercd = ChkSpaceRW(pk_rsmb, sizeof(T_RSMB));
  330:         if ( ercd < E_OK ) {
  331:                 goto err_ret;
  332:         }
  333: 
  334:         ercd = RefSysMemInfo(pk_rsmb);
  335:         if ( ercd < E_OK ) {
  336:                 goto err_ret;
  337:         }
  338: 
  339:         return E_OK;
  340: 
  341: err_ret:
  342:         DEBUG_PRINT(("_tk_ref_smb ercd = %d\n", ercd));
  343:         return ercd;
  344: }
  345: 
  346: /* ------------------------------------------------------------------------ */
  347: /*
  348:  *      Kernel shared data
  349:  */
  350: 
  351: LOCAL   CommArea  _CommArea;       /* Kernel shared data area */
  352: IMPORT  CommArea *__CommArea;    /* libtk */
  353: 
  354: /*
  355:  * Get Kernel shared data
  356:  */
  357: LOCAL ER __GetKernelCommonArea( CommArea **area )
  358: {
  359:         *area = &_CommArea;
  360: 
  361:         return E_OK;
  362: }
  363: 
  364: /*
  365:  * Kernel shared data initial setting
  366:  */
  367: LOCAL void initKernelCommonArea( void )
  368: {
  369:         INT    n, val;
  370: 
  371:         __CommArea = &_CommArea;
  372: 
  373:         n = _tk_get_cfn(SCTAG_TEV_FFLOCK, &val, 1);
  374:         _CommArea.tev_fflock = ( n > 0 )? val: 0;
  375: }
  376: 
  377: /* ------------------------------------------------------------------------ */
  378: 
  379: /*
  380:  * Extension SVC entry
  381:  */
  382: LOCAL INT sysmgr_svcentry( void *pk_para, FN fncd )
  383: {
  384:         ER     ercd;
  385: 
  386:         switch ( fncd ) {
  387:                 /* Ignore protection level */
  388:           case SYSTEM_TK_GET_CFN_FN:
  389:           case SYSTEM_TK_GET_CFS_FN:
  390:           case SYSTEM__SYSLOG_SEND_FN:
  391:                 break;
  392: 
  393:           default:
  394:                 /* Test call protection level */
  395:                 ercd = ChkCallPLevel();
  396:                 if ( ercd < E_OK ) {
  397:                         goto err_ret;
  398:                 }
  399:         }
  400: 
  401:         switch ( fncd ) {
  402:           case SYSTEM_TK_GET_CFN_FN:
  403:                 { SYSTEM_TK_GET_CFN_PARA *p = pk_para;
  404:                 return __tk_get_cfn(p->name, p->val, p->max); }
  405:           case SYSTEM_TK_GET_CFS_FN:
  406:                 { SYSTEM_TK_GET_CFS_PARA *p = pk_para;
  407:                 return __tk_get_cfs(p->name, p->buf, p->max); }
  408:           case SYSTEM_TK_GET_SMB_FN:
  409:                 { SYSTEM_TK_GET_SMB_PARA *p = pk_para;
  410:                 return _tk_get_smb(p->addr, p->nblk, p->attr); }
  411:           case SYSTEM_TK_REL_SMB_FN:
  412:                 { SYSTEM_TK_REL_SMB_PARA *p = pk_para;
  413:                 return _tk_rel_smb(p->addr); }
  414:           case SYSTEM_TK_REF_SMB_FN:
  415:                 { SYSTEM_TK_REF_SMB_PARA *p = pk_para;
  416:                 return _tk_ref_smb(p->pk_rsmb); }
  417: 
  418:           case SYSTEM__GETKERNELCOMMONAREA_FN:
  419:                 { SYSTEM__GETKERNELCOMMONAREA_PARA *p = pk_para;
  420:                 return __GetKernelCommonArea(p->area); }
  421:           case SYSTEM__SYSLOG_SEND_FN:
  422:                 { SYSTEM__SYSLOG_SEND_PARA *p = pk_para;
  423:                 return __syslog_send(p->string, p->len); }
  424:           default:
  425:                 ercd = E_RSFN;
  426:         }
  427: 
  428: err_ret:
  429:         DEBUG_PRINT(("sysmgr_svcentry ercd = %d\n", ercd));
  430:         return ercd;
  431: }
  432: 
  433: /*
  434:  * Initialization of system management
  435:  */
  436: EXPORT ER initialize_sysmgr( void )
  437: {
  438:         T_DSSY dssy;
  439:         ER     ercd;
  440: 
  441:         /* Kernel shared data area setting */
  442:         initKernelCommonArea();
  443: 
  444:         /* syslog initialization sequence */
  445:         ercd = initialize_syslog();
  446:         if ( ercd < E_OK ) {
  447:                 goto err_ret;
  448:         }
  449: 
  450:         /* subsystem registration */
  451:         dssy.ssyatr    = TA_NULL;
  452:         dssy.ssypri    = SYSTEM_PRI;
  453:         dssy.svchdr    = (FP)&sysmgr_svcentry;
  454:         dssy.breakfn   = NULL;
  455:         dssy.startupfn = NULL;
  456:         dssy.cleanupfn = NULL;
  457:         dssy.eventfn   = NULL;
  458:         dssy.resblksz  = 0;
  459:         ercd = tk_def_ssy(SYSTEM_SVC, &dssy);
  460:         if ( ercd < E_OK ) {
  461:                 goto err_ret;
  462:         }
  463: 
  464:         return E_OK;
  465: 
  466: err_ret:
  467:         DEBUG_PRINT(("initialize_sysmgr ercd = %d\n", ercd));
  468:         return ercd;
  469: }
  470: 
  471: /*
  472:  * Finalization sequence of system management
  473:  */
  474: EXPORT ER finish_sysmgr( void )
  475: {
  476:         ER     ercd;
  477: 
  478:         /* Unregister subsystem */
  479:         ercd = tk_def_ssy(SYSTEM_SVC, NULL);
  480: #ifdef DEBUG
  481:         if ( ercd < E_OK ) {
  482:                 DEBUG_PRINT(("1. finish_sysmgr -> tk_def_ssy ercd = %d\n", ercd));
  483:         }
  484: #endif
  485: 
  486:         /* syslog finalization sequence */
  487:         ercd = finish_syslog();
  488: #ifdef DEBUG
  489:         if ( ercd < E_OK ) {
  490:                 DEBUG_PRINT(("2. finish_sysmgr -> finish_syslog ercd = %d\n", ercd));
  491:         }
  492: #endif
  493: 
  494:         return ercd;
  495: }