Kernel/x86 - Disabled task tracing
[tpg/acess2.git] / Kernel / system.c
1 /*
2  * Acess 2
3  * Architecture Independent System Init
4  * system.c
5  */
6 #define DEBUG   0
7 #include <acess.h>
8
9 #define N_VARIABLES     16
10 #define N_MAX_ARGS      BITS
11
12 // === TYPES ===
13 typedef struct
14 {
15          int    TrueLine;
16          int    nParts;
17         char    **Parts;
18 }       tConfigLine;
19 typedef struct
20 {
21          int    nLines;
22         tConfigLine     Lines[];
23 }       tConfigFile;
24 typedef struct
25 {
26         const char      *Name;          // Name
27          int    MinArgs;        // Minimum number of arguments
28          int    MaxArgs;        // Maximum number of arguments
29         Uint    IntArgs;        // Bitmap of arguments that should be treated as integers
30          int    Index;          // 
31         const char      *OptDefaults[N_MAX_ARGS];       // Default values for optional arguments
32 }       tConfigCommand;
33
34 // === IMPORTS ===
35 extern void     Arch_LoadBootModules(void);
36 extern int      Modules_LoadBuiltins(void);
37 extern void     Modules_SetBuiltinParams(char *Name, char *ArgString);
38 extern void     Debug_SetKTerminal(const char *File);
39
40 // === PROTOTYPES ===
41 void    System_Init(char *Commandline);
42 void    System_ParseCommandLine(char *ArgString);
43 void    System_ExecuteCommandLine(void);
44 void    System_ParseVFS(char *Arg);
45 void    System_ParseModuleArgs(char *Arg);
46 void    System_ParseSetting(char *Arg);
47 void    System_ExecuteScript(void);
48 tConfigFile     *System_Int_ParseFile(char *File);
49
50 // === CONSTANTS ===
51 enum eConfigCommands {
52         CC_LOADMODULE,
53         CC_SPAWN,
54         CC_MOUNT,
55         CC_SYMLINK,
56         CC_MKDIR,
57         CC_OPEN,
58         CC_CLOSE,
59         CC_IOCTL
60 };
61 const tConfigCommand    caConfigCommands[] = {
62         {"module",  1,2, 00, CC_LOADMODULE, {"",NULL}}, // Load a module from a file
63         {"spawn",   1,1, 00, CC_SPAWN, {NULL}},         // Spawn a process
64         // --- VFS ---
65         {"mount",   3,4, 00, CC_MOUNT, {"",0}},         // Mount a device
66         {"symlink", 2,2, 00, CC_SYMLINK, {0}},  // Create a Symbolic Link
67         {"mkdir",   1,1, 00, CC_MKDIR, {0}},            // Create a Directory
68         {"open",    1,2, 00, CC_OPEN,  {(void*)VFS_OPENFLAG_READ,0}},   // Open a file
69         {"close",   1,1, 01, CC_CLOSE, {0}},    // Close an open file
70         {"ioctl",   3,3, 03, CC_IOCTL, {0}},    // Call an IOCtl
71         
72         {"", 0,0, 0, 0, {0}}
73 };
74 #define NUM_CONFIG_COMMANDS     (sizeof(caConfigCommands)/sizeof(caConfigCommands[0]))
75
76 // === GLOBALS ===
77 const char      *gsConfigScript = "/Acess/Conf/BootConf.cfg";
78 char    *argv[32];
79  int    argc;
80
81 // === CODE ===
82 void System_Init(char *CommandLine)
83 {
84         // Parse Kernel's Command Line
85         System_ParseCommandLine(CommandLine);
86         
87         // Initialise modules
88         Log_Log("Config", "Initialising builtin modules...");
89         Modules_LoadBuiltins();
90         Arch_LoadBootModules();
91         
92         System_ExecuteCommandLine();
93         
94         // - Execute the Config Script
95         Log_Log("Config", "Executing config script '%s'", gsConfigScript);
96         System_ExecuteScript();
97         
98         // Set the debug to be echoed to the terminal
99         Log_Log("Config", "Kernel now echoes to VT7 (Ctrl-Alt-F8)");
100         Debug_SetKTerminal("/Devices/VTerm/7");
101 }
102
103 /**
104  * \fn void System_ParseCommandLine(char *ArgString)
105  * \brief Parses the kernel's command line and sets the environment
106  */
107 void System_ParseCommandLine(char *ArgString)
108 {
109          int    i;
110         char    *str;
111         
112         Log_Log("Config", "Kernel Invocation (%p) \"%s\"", ArgString, ArgString);
113         
114         // --- Get Arguments ---
115         str = ArgString;
116         for( argc = 0; argc < 32; argc++ )
117         {
118                 // Eat Whitespace
119                 while(*str == ' ')      str++;
120                 // Check for the end of the string
121                 if(*str == '\0') {      argc--; break;} 
122                 argv[argc] = str;
123                 if(*str == '"') {
124                         while(*str && !(*str == '"' && str[-1] != '\\'))
125                                 str ++;
126                 }
127                 else {
128                         while(*str && *str != ' ')
129                                 str++;
130                 }
131                 if(*str == '\0')        break;  // Check for EOS
132                 *str = '\0';    // Cap off argument string
133                 str ++; // and increment the string pointer
134         }
135         if(argc < 32)
136                 argc ++;        // Count last argument
137         
138         // --- Parse Arguments (Pass 1) ---
139         for( i = 0; i < argc; i++ )
140         {
141                 switch(argv[i][0])
142                 {
143                 // --- VFS ---
144                 // Ignored on this pass
145                 case '/':
146                         break;
147                 
148                 // --- Module Paramaters ---
149                 // -VTerm:Width=640,Height=480,Scrollback=2
150                 case '-':
151                         System_ParseModuleArgs( argv[i] );
152                         break;
153                 // --- Config Options ---
154                 // SCRIPT=/Acess/Conf/BootConf.cfg
155                 default:
156                         System_ParseSetting( argv[i] );
157                         break;
158                 }
159         }
160 }
161
162 void System_ExecuteCommandLine(void)
163 {
164          int    i;
165         for( i = 0; i < argc; i++ )
166         {
167                 Log("argv[%i] = '%s'", i, argv[i]);
168                 switch(argv[i][0])
169                 {
170                 // --- VFS ---
171                 // Mount    /System=ext2:/Devices/ATA/A1
172                 // Symlink  /Acess=/System/Acess2
173                 case '/':
174                         System_ParseVFS( argv[i] );
175                         break;
176                 }
177         }
178 }
179
180 /**
181  * \fn void System_ParseVFS(char *Arg)
182  */
183 void System_ParseVFS(char *Arg)
184 {
185         char    *value;
186          int    fd;
187         
188         value = Arg;
189         // Search for the '=' token
190         while( *value && *value != '=' )
191                 value++;
192         
193         // Check if the equals was found
194         if( *value == '\0' ) {
195                 Log_Warning("Config", "Expected '=' in the string '%s'", Arg);
196                 return ;
197         }
198         
199         // Edit string
200         *value = '\0';  value ++;
201         
202         // Check assignment type
203         // - Symbolic Link <link>=<destination>
204         if(value[0] == '/')
205         {
206                 Log_Log("Config", "Symbolic link '%s' pointing to '%s'", Arg, value);
207                 VFS_Symlink(Arg, value);
208         }
209         // - Mount <mountpoint>=<fs>:<device>
210         else
211         {
212                 char    *dev = value;
213                 // Find colon
214                 while(*dev && *dev != ':')      dev++;
215                 if(*dev) {
216                         *dev = '\0';
217                         dev++;  // Eat ':'
218                 }
219                 // Create Mountpoint
220                 if( (fd = VFS_Open(Arg, 0)) == -1 ) {
221                         Log_Log("Config", "Creating directory '%s'", Arg, value);
222                         VFS_MkDir( Arg );
223                 } else {
224                         VFS_Close(fd);
225                 }
226                 // Mount
227                 Log_Log("Config", "Mounting '%s' to '%s' ('%s')", dev, Arg, value);
228                 VFS_Mount(dev, Arg, value, "");
229         }
230 }
231
232 /**
233  * \brief Parse a module argument string
234  * \param Arg   Argument string
235  */
236 void System_ParseModuleArgs(char *Arg)
237 {
238         char    *name, *args;
239          int    i;
240         
241         // Remove '-'   
242         name = Arg + 1;
243         
244         // Find the start of the args
245         i = strpos(name, ':');
246         if( i == -1 ) {
247                 Log_Warning("Config", "Module spec with no arguments");
248                 #if 1
249                 return ;
250                 #else
251                 i = strlen(name);
252                 args = name + i;
253                 #endif
254         }
255         else {
256                 name[i] = '\0';
257                 args = name + i + 1;
258         }
259         
260         Log_Log("Config", "Setting boot parameters for '%s' to '%s'", name, args);
261         Modules_SetBuiltinParams(name, args);
262 }
263
264 /**
265  * \fn void System_ParseSetting(char *Arg)
266  */
267 void System_ParseSetting(char *Arg)
268 {
269         char    *value;
270         value = Arg;
271
272         // Search for the '=' token
273         while( *value && *value != '=' )
274                 value++;
275         
276         // Check for boolean/flag (no '=')
277         if(*value == '\0')
278         {
279                 //if(strcmp(Arg, "") == 0) {
280                 //} else {
281                         Log_Warning("Config", "Kernel flag '%s' is not recognised", Arg);
282                 //}
283         }
284         else
285         {
286                 *value = '\0';  // Remove '='
287                 value ++;       // and eat it's position
288                 
289                 if(strcmp(Arg, "SCRIPT") == 0) {
290                         Log_Log("Config", "Config Script: '%s'", value);
291                         if(strlen(value) == 0)
292                                 gsConfigScript = NULL;
293                         else
294                                 gsConfigScript = value;
295                 } else {
296                         Log_Warning("Config", "Kernel config setting '%s' is not recognised", Arg);
297                 }
298                 
299         }
300 }
301
302 /**
303  * \fn void System_ExecuteScript()
304  * \brief Reads and parses the boot configuration script
305  */
306 void System_ExecuteScript(void)
307 {
308          int    fp;
309          int    fLen = 0;
310          int    i, j, k;
311          int    val;
312          int    result = 0;
313          int    variables[N_VARIABLES];
314          int    bReplaced[N_MAX_ARGS];
315         char    *fData;
316         char    *jmpTarget;
317         tConfigFile     *file;
318         tConfigLine     *line;
319         
320         // Open Script
321         fp = VFS_Open(gsConfigScript, VFS_OPENFLAG_READ);
322         if(fp == -1) {
323                 Log_Warning("Config", "Passed script '%s' does not exist", gsConfigScript);
324                 return;
325         }
326         
327         // Get length
328         VFS_Seek(fp, 0, SEEK_END);
329         fLen = VFS_Tell(fp);
330         Log_Debug("System", "VFS_Tell(%i) = %i", fp, fLen);
331         VFS_Seek(fp, 0, SEEK_SET);
332         // Read into memory buffer
333         fData = malloc(fLen+1);
334         VFS_Read(fp, fLen, fData);
335         fData[fLen] = '\0';
336         VFS_Close(fp);
337         
338         
339         // Parse File
340         file = System_Int_ParseFile(fData);
341         
342         // Parse each line
343         for( i = 0; i < file->nLines; i++ )
344         {
345                 line = &file->Lines[i];
346                 if( line->nParts == 0 ) continue;       // Skip blank
347                 
348                 if(line->Parts[0][0] == ':')    continue;       // Ignore labels
349                 
350                 // Prescan and eliminate variables
351                 for( j = 1; j < line->nParts; j++ )
352                 {
353                         Log_Debug("Config", "Arg #%i is '%s'", j, line->Parts[j]);
354                         bReplaced[j] = 0;
355                         if( line->Parts[j][0] != '$' )  continue;
356                         if( line->Parts[j][1] == '?' ) {
357                                 val = result;
358                         }
359                         else {
360                                 val = atoi( &line->Parts[j][1] );
361                                 if( val < 0 || val > N_VARIABLES )      continue;
362                                 val = variables[ val ];
363                         }
364                         Log_Debug("Config", "Replaced arg %i ('%s') with 0x%x", j, line->Parts[j], val);
365                         line->Parts[j] = malloc( BITS/8+2+1 );
366                         sprintf(line->Parts[j], "0x%x", val);
367                         bReplaced[j] = 1;
368                 }
369                 
370                 // Find the command name
371                 for( j = 0; j < NUM_CONFIG_COMMANDS; j++ )
372                 {
373                         const char      *args[N_MAX_ARGS];
374                         
375                         if(strcmp(line->Parts[0], caConfigCommands[j].Name) != 0)       continue;
376                         
377                         Log_Debug("Config", "Command '%s', %i args passed", line->Parts[0], line->nParts-1);
378                         
379                         // Check against minimum argument count
380                         if( line->nParts - 1 < caConfigCommands[j].MinArgs ) {
381                                 Log_Warning("Config",
382                                         "Configuration command '%s' requires at least %i arguments, %i given",
383                                         caConfigCommands[j].Name, caConfigCommands[j].MinArgs, line->nParts-1
384                                         );
385                                 break;
386                         }
387                         
388                         // Check for extra arguments
389                         if( line->nParts - 1 > caConfigCommands[j].MaxArgs ) {
390                                 Log_Warning("Config",
391                                         "Configuration command '%s' takes at most %i arguments, %i given",
392                                         caConfigCommands[j].Name, caConfigCommands[j].MaxArgs, line->nParts-1
393                                         );
394                                 break;
395                         }
396                         
397                         // Fill in defaults
398                         for( k = caConfigCommands[j].MaxArgs-1; k > line->nParts - 1; k-- ) {
399                                 args[k] = caConfigCommands[j].OptDefaults[k];
400                         }
401                         
402                         // Convert arguments to integers
403                         for( k = line->nParts-1; k--; )
404                         {
405                                 if( k < 32 && (caConfigCommands[j].IntArgs & (1 << k)) ) {
406                                         args[k] = (const char *)(Uint)atoi(line->Parts[k+1]);
407                                 }
408                                 else {
409                                         args[k] = (char *)line->Parts[k+1];
410                                 }
411                                 Log_Debug("Config", "args[%i] = %p", k, args[k]);
412                         }
413                         switch( (enum eConfigCommands) caConfigCommands[j].Index )
414                         {
415                         case CC_LOADMODULE:
416                                 result = Module_LoadFile( args[0], args[1] );
417                                 break;
418                         case CC_SPAWN:
419                                 result = Proc_Spawn( args[0] );
420                                 break;
421                         case CC_MOUNT:
422                                 result = VFS_Mount( args[0], args[1], args[2], args[3] );
423                                 break;
424                         case CC_SYMLINK:
425                                 result = VFS_Symlink( args[0], args[1] );
426                                 break;
427                         case CC_OPEN:
428                                 result = VFS_Open( args[0], (Uint)args[1] );
429                                 break;
430                         case CC_CLOSE:
431                                 VFS_Close( (Uint)args[0] );
432                                 result = 0;
433                                 break;
434                         case CC_MKDIR:
435                                 result = VFS_MkDir( args[0] );
436                                 break;
437                         case CC_IOCTL:
438                                 result = VFS_IOCtl( (Uint)args[0], (Uint)args[1], (void *)args[2] );
439                                 break;
440                         }
441                         Log_Debug("Config", "result = %i", result);
442                         break;
443                 }
444                 if( j < NUM_CONFIG_COMMANDS )   continue;
445                         
446                 // --- State and Variables ---
447                 if(strcmp(line->Parts[0], "set") == 0)
448                 {
449                          int    to, value;
450                         if( line->nParts-1 != 2 ) {
451                                 Log_Warning("Config", "Configuration command 'set' requires 2 arguments, %i given",
452                                         line->nParts-1);
453                                 continue;
454                         }
455                         
456                         to = atoi(line->Parts[1]);
457                         value = atoi(line->Parts[2]);
458                         
459                         variables[to] = value;
460                         result = value;
461                 }
462                 // if <val1> <op> <val2> <dest>
463                 else if(strcmp(line->Parts[0], "if") == 0)
464                 {
465                         if( line->nParts-1 != 4 ) {
466                                 Log_Warning("Config", "Configuration command 'if' requires 4 arguments, %i given",
467                                         line->nParts-1);
468                         }
469                         
470                         result = atoi(line->Parts[1]);
471                         val = atoi(line->Parts[3]);
472                         
473                         jmpTarget = line->Parts[4];
474                         
475                         Log_Log("Config", "IF 0x%x %s 0x%x THEN GOTO %s",
476                                 result, line->Parts[2], val, jmpTarget);
477                         
478                         if( strcmp(line->Parts[2], "<" ) == 0 ) {
479                                 if( result < val )      goto jumpToLabel;
480                         }
481                         else if( strcmp(line->Parts[2], "<=") == 0 ) {
482                                 if( result <= val )     goto jumpToLabel;
483                         }
484                         else if( strcmp(line->Parts[2], ">" ) == 0 ) {
485                                 if (result > val )      goto jumpToLabel;
486                         }
487                         else if( strcmp(line->Parts[2], ">=") == 0 ) {
488                                 if( result >= val )     goto jumpToLabel;
489                         }
490                         else if( strcmp(line->Parts[2],  "=") == 0 ) {
491                                 if( result == val )     goto jumpToLabel;
492                         }
493                         else if( strcmp(line->Parts[2], "!=") == 0 ) {
494                                 if( result != val )     goto jumpToLabel;
495                         }
496                         else {
497                                 Log_Warning("Config", "Unknown comparision '%s' in `if`", line->Parts[2]);
498                         }
499                         
500                 }
501                 else if(strcmp(line->Parts[0], "goto") == 0) {
502                         if( line->nParts-1 != 1 ) {
503                                 Log_Warning("Config", "Configuration command 'goto' requires 1 arguments, %i given",
504                                         line->nParts-1);
505                         }
506                         jmpTarget = line->Parts[1];
507                 
508                 jumpToLabel:
509                         for( j = 0; j < file->nLines; j ++ )
510                         {
511                                 if(file->Lines[j].nParts == 0)
512                                         continue;
513                                 if(file->Lines[j].Parts[0][0] != ':')
514                                         continue;
515                                 if( strcmp(file->Lines[j].Parts[0]+1, jmpTarget) == 0)
516                                         break;
517                         }
518                         if( j == file->nLines )
519                                 Log_Warning("Config", "Unable to find label '%s'", jmpTarget);
520                         else
521                                 i = j;
522                 }
523                 else {
524                         Log_Warning("Config", "Unknown configuration command '%s' on line %i",
525                                 line->Parts[0],
526                                 line->TrueLine
527                                 );
528                 }
529         }
530         
531         // Clean up after ourselves
532         for( i = 0; i < file->nLines; i++ ) {
533                 if( file->Lines[i].nParts == 0 )        continue;       // Skip blank
534                 for( j = 0; j < file->Lines[i].nParts; j++ ) {
535                         if(IsHeap(file->Lines[i].Parts[j]))
536                                 free(file->Lines[i].Parts[j]);
537                 }
538                 free( file->Lines[i].Parts );
539         }
540         
541         // Free data
542         free( file );
543         free( fData );
544 }
545
546 /**
547  * \brief Parses a config file
548  * \param FileData      Read/Write buffer containing the config file data
549  *                  (will be modified)
550  * \return ::tConfigFile structure that represents the original contents
551  *         of \a FileData
552  */
553 tConfigFile     *System_Int_ParseFile(char *FileData)
554 {
555         char    *ptr;
556         char    *start;
557          int    nLines = 1;
558          int    i, j;
559         tConfigFile     *ret;
560         
561         ENTER("pFileData", FileData);
562         
563         // Prescan and count the number of lines
564         for(ptr = FileData; *ptr; ptr++)
565         {               
566                 if(*ptr != '\n')        continue;
567                 
568                 if(ptr == FileData) {
569                         nLines ++;
570                         continue;
571                 }
572                 
573                 // Escaped EOL
574                 if(ptr[-1] == '\\')     continue;
575                 
576                 nLines ++;
577         }
578         
579         LOG("nLines = %i", nLines);
580         
581         // Ok so we have `nLines` lines, now to allocate our return
582         ret = malloc( sizeof(tConfigFile) + sizeof(tConfigLine)*nLines );
583         ret->nLines = nLines;
584         
585         // Read the file for real
586         for(
587                 ptr = FileData, i = 0;
588                 *ptr;
589                 i++
590                 )
591         {
592                 start = ptr;
593                 
594                 ret->Lines[i].nParts = 0;
595                 ret->Lines[i].Parts = NULL;
596                 
597                 // Count parts
598                 for(;;)
599                 {
600                         // Read leading whitespace
601                         while( *ptr == '\t' || *ptr == ' ' )    ptr++;
602                         
603                         // End of line/file
604                         if( *ptr == '\0' || *ptr == '\n' ) {
605                                 if(*ptr == '\n')        ptr ++;
606                                 break;
607                         }
608                         // Comment
609                         if( *ptr == '#' || *ptr == ';' ) {
610                                 while( *ptr && *ptr != '\n' )   ptr ++;
611                                 if(*ptr == '\n')        ptr ++;
612                                 break;
613                         }
614                         
615                         ret->Lines[i].nParts ++;
616                         // Quoted
617                         if( *ptr == '"' ) {
618                                 ptr ++;
619                                 while( *ptr && !(*ptr == '"' && ptr[-1] == '\\') && *ptr != '\n' )
620                                         ptr++;
621                                 continue;
622                         }
623                         // Unquoted
624                         while( *ptr && !(*ptr == '\t' || *ptr == ' ') && *ptr != '\n' )
625                                 ptr++;
626                 }
627                 
628                 LOG("ret->Lines[%i].nParts = %i", i, ret->Lines[i].nParts);
629                 
630                 if( ret->Lines[i].nParts == 0 ) {
631                         ret->Lines[i].Parts = NULL;
632                         continue;
633                 }
634                 
635                 // Allocate part list
636                 ret->Lines[i].Parts = malloc( sizeof(char*) * ret->Lines[i].nParts );
637                 
638                 // Fill list
639                 for( ptr = start, j = 0; ; j++ )
640                 {
641                         // Read leading whitespace
642                         while( *ptr == '\t' || *ptr == ' ' )    ptr++;
643                         
644                         // End of line/file
645                         if( *ptr == '\0' || *ptr == '\n' ) {
646                                 if(*ptr == '\n')        ptr ++;
647                                 break;
648                         }
649                         // Comment
650                         if( *ptr == '#' || *ptr == ';' ) {
651                                 while( *ptr && *ptr != '\n' )   ptr ++;
652                                 if(*ptr == '\n')        ptr ++;
653                                 break;
654                         }
655                         
656                         ret->Lines[i].Parts[j] = ptr;
657                         
658                         // Quoted
659                         if( *ptr == '"' ) {
660                                 ptr ++;
661                                 ret->Lines[i].Parts[j] = ptr;
662                                 while( *ptr && !(*ptr == '"' && ptr[-1] == '\\') && *ptr != '\n' )
663                                         ptr++;
664                         }
665                         // Unquoted
666                         else {
667                                 while( *ptr != '\t' && *ptr != ' ' && *ptr != '\n' )
668                                         ptr++;
669                         }
670                         
671                         // Break if we have reached NULL
672                         if( *ptr == '\0' ) {
673                                 LOG("ret->Lines[%i].Parts[%i] = '%s'", i, j, ret->Lines[i].Parts[j]);
674                                 break;
675                         }
676                         if( *ptr == '\n' ) {
677                                 *ptr = '\0';
678                                 LOG("ret->Lines[%i].Parts[%i] = '%s'", i, j, ret->Lines[i].Parts[j]);
679                                 ptr ++;
680                                 break;
681                         }
682                         *ptr = '\0';    // Cap off string
683                         LOG("ret->Lines[%i].Parts[%i] = '%s'", i, j, ret->Lines[i].Parts[j]);
684                         ptr ++; // And increment for the next round
685                 }
686         }
687         
688         if( i < ret->nLines ) {
689                 ret->Lines[i].nParts = 0;
690                 ret->Lines[i].Parts = NULL;
691                 Log_Log("System", "Cleaning up final empty line");
692         }
693         
694         LEAVE('p', ret);
695         return ret;
696 }

UCC git Repository :: git.ucc.asn.au