Mercurial > sos > sos
annotate src/os/shell.c @ 150:e72f984619c7
Consolidated a lot of the defines which relate to the memory layout
There is a new file (include/os/memLayout.h) which now includes just
about all the #defines
author  Jonathan Pevarnek <pevarnj@gmail.com> 

date  Wed, 02 Nov 2011 09:55:09 0400 
parents  eb69d1caa83b 
children  a6482018de66 
rev  line source 

112
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset

1 #include <std.h> 
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset

2 #include <string.h> 
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset

3 #include <stdio.h> 
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset

4 #include <error.h> 
116
967a56b96d13
first attempt at redoing malloc, seems to work. The program level is not currently functional.
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
115
diff
changeset

5 #include <die.h> 
112
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset

6 #include <os/fs.h> 
115
4473e746fe5a
Restructured the include directory somewhat
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
112
diff
changeset

7 #include <os/elf.h> 
4473e746fe5a
Restructured the include directory somewhat
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
112
diff
changeset

8 #include <os/psw.h> 
4473e746fe5a
Restructured the include directory somewhat
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
112
diff
changeset

9 #include <os/svc.h> 
112
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset

10 #include <os/scall.h> 
123
2a35ea7e123b
Moved the psw and registers into a program control block structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
116
diff
changeset

11 #include <os/pcb.h> 
149
eb69d1caa83b
First try with storage keys
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
129
diff
changeset

12 #include <os/storageKeys.h> 
150
e72f984619c7
Consolidated a lot of the defines which relate to the memory layout
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
149
diff
changeset

13 #include <os/memLayout.h> 
129
86ec817aa4f4
Build the argument stuff in program space
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
128
diff
changeset

14 
86ec817aa4f4
Build the argument stuff in program space
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
128
diff
changeset

15 #define MAX_INPUT_LENGTH 128 //128 characters maximum input 
86ec817aa4f4
Build the argument stuff in program space
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
128
diff
changeset

16 #define MAX_ARGS 10 //no more than ten arguments, including the program name 
112
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset

17 
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset

18 void start(u64 __memsize) 
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset

19 { 
129
86ec817aa4f4
Build the argument stuff in program space
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
128
diff
changeset

20 char buffer[MAX_INPUT_LENGTH]; 
86ec817aa4f4
Build the argument stuff in program space
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
128
diff
changeset

21 char *c, *track; 
86ec817aa4f4
Build the argument stuff in program space
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
128
diff
changeset

22 char *argStartLoc = (void*)PROGRAM_STACK_START  MAX_INPUT_LENGTH; 
86ec817aa4f4
Build the argument stuff in program space
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
128
diff
changeset

23 
112
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset

24 ErrCode err; 
116
967a56b96d13
first attempt at redoing malloc, seems to work. The program level is not currently functional.
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
115
diff
changeset

25 init_all(__memsize); 
967a56b96d13
first attempt at redoing malloc, seems to work. The program level is not currently functional.
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
115
diff
changeset

26 if(isError(init_fs(0x100, __memsize))) die(); 
129
86ec817aa4f4
Build the argument stuff in program space
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
128
diff
changeset

27 set_svc_handler(svc_handler, (void*)(KERNEL_STACK_START  STACK_SPACING)); 
112
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset

28 
116
967a56b96d13
first attempt at redoing malloc, seems to work. The program level is not currently functional.
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
115
diff
changeset

29 sPrint("SOS online!\n"); 
112
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset

30 while(1) { 
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset

31 sPrint("$ "); 
129
86ec817aa4f4
Build the argument stuff in program space
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
128
diff
changeset

32 sGet(buffer, MAX_INPUT_LENGTH); 
86ec817aa4f4
Build the argument stuff in program space
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
128
diff
changeset

33 memcpy(argStartLoc, buffer, MAX_INPUT_LENGTH); //copy the command+arguments over 
128
ca9f01e6e2bf
First attempt at argv and argc, probably screwed up a bit it has been so long...
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
126
diff
changeset

34 c = strtok_r(buffer, " ", &track); 
125
e2396f625db8
Started working on argc, argv
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
123
diff
changeset

35 
112
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset

36 u32 fid; 
125
e2396f625db8
Started working on argc, argv
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
123
diff
changeset

37 if(isError(err = lookupFile(c, &fid))) { 
112
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset

38 if(errCode(err) == NOTFILE) sPrint("ERROR: command not found\n"); 
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset

39 continue; 
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset

40 } else { 
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset

41 u32 fileSize; 
116
967a56b96d13
first attempt at redoing malloc, seems to work. The program level is not currently functional.
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
115
diff
changeset

42 if(isError(getFileSize(fid, &fileSize))) { //get the length of the file 
967a56b96d13
first attempt at redoing malloc, seems to work. The program level is not currently functional.
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
115
diff
changeset

43 sPrint("ERROR: could not read file size\n"); 
967a56b96d13
first attempt at redoing malloc, seems to work. The program level is not currently functional.
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
115
diff
changeset

44 continue; 
967a56b96d13
first attempt at redoing malloc, seems to work. The program level is not currently functional.
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
115
diff
changeset

45 } 
112
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset

46 void *data = malloc(fileSize); 
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset

47 if(!data) continue; 
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset

48 if(isError(getFileData(fid, data))) goto loopCont; 
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset

49 Elf64_Ehdr *eHdr = data; 
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset

50 if(eHdr>e_ident[EI_MAG0] != ELFMAG0  eHdr>e_ident[EI_MAG1] != ELFMAG1  
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset

51 eHdr>e_ident[EI_MAG2] != ELFMAG2  eHdr>e_ident[EI_MAG3] != ELFMAG3) 
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset

52 goto loopCont; 
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset

53 if(eHdr>e_ident[EI_CLASS] != ELFCLASS64) goto loopCont; 
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset

54 int i; 
116
967a56b96d13
first attempt at redoing malloc, seems to work. The program level is not currently functional.
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
115
diff
changeset

55 for(i = 0; i < eHdr>e_phnum; i++) { //go through all the headers 
112
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset

56 Elf64_Phdr *pHdr = ((Elf64_Phdr*)(data + eHdr>e_phoff) + i); 
116
967a56b96d13
first attempt at redoing malloc, seems to work. The program level is not currently functional.
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
115
diff
changeset

57 if(pHdr>p_type == PT_LOAD) { //these segments get loaded into the memory 
112
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset

58 void *fromLoc = data + pHdr>p_offset; 
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset

59 void *toLoc = (void*)pHdr>p_vaddr; 
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset

60 memcpy(toLoc, fromLoc, pHdr>p_filesz); 
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset

61 size_t diff = pHdr>p_memsz  pHdr>p_filesz; 
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset

62 if(diff) memset(toLoc + pHdr>p_filesz, 0, diff); 
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset

63 } 
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset

64 } 
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset

65 
123
2a35ea7e123b
Moved the psw and registers into a program control block structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
116
diff
changeset

66 PCB pcb; 
2a35ea7e123b
Moved the psw and registers into a program control block structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
116
diff
changeset

67 memset(&pcb, 0, sizeof(pcb)); 
129
86ec817aa4f4
Build the argument stuff in program space
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
128
diff
changeset

68 
86ec817aa4f4
Build the argument stuff in program space
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
128
diff
changeset

69 /* TODO FUTURE PLAN FOR COMMAND LINE PARAMETERS 
86ec817aa4f4
Build the argument stuff in program space
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
128
diff
changeset

70 * Instead of the current way of copying things, I would like to eventually 
86ec817aa4f4
Build the argument stuff in program space
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
128
diff
changeset

71 * change it so that each argument is copied over and its location added to 
86ec817aa4f4
Build the argument stuff in program space
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
128
diff
changeset

72 * a stack. Later, the items could be removed from the stack and placed in 
86ec817aa4f4
Build the argument stuff in program space
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
128
diff
changeset

73 * the program stack in the correct order 
86ec817aa4f4
Build the argument stuff in program space
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
128
diff
changeset

74 */ 
86ec817aa4f4
Build the argument stuff in program space
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
128
diff
changeset

75 
86ec817aa4f4
Build the argument stuff in program space
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
128
diff
changeset

76 char **argv = (void*)PROGRAM_STACK_START  MAX_INPUT_LENGTH  MAX_ARGS*(sizeof(char*)); 
86ec817aa4f4
Build the argument stuff in program space
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
128
diff
changeset

77 char **curArg = argv; 
86ec817aa4f4
Build the argument stuff in program space
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
128
diff
changeset

78 char *b = argStartLoc; 
86ec817aa4f4
Build the argument stuff in program space
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
128
diff
changeset

79 int argc = 0; 
86ec817aa4f4
Build the argument stuff in program space
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
128
diff
changeset

80 
86ec817aa4f4
Build the argument stuff in program space
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
128
diff
changeset

81 while((*curArg++ = strtok_r(b, " ", &track))) { 
86ec817aa4f4
Build the argument stuff in program space
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
128
diff
changeset

82 if(b) b = NULL; 
86ec817aa4f4
Build the argument stuff in program space
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
128
diff
changeset

83 if(++argc == MAX_ARGS) break; //TODO find a better way to deal with this 
86ec817aa4f4
Build the argument stuff in program space
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
128
diff
changeset

84 } 
86ec817aa4f4
Build the argument stuff in program space
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
128
diff
changeset

85 
86ec817aa4f4
Build the argument stuff in program space
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
128
diff
changeset

86 size_t argOffset = MAX_INPUT_LENGTH + MAX_ARGS*(sizeof(char*)); 
86ec817aa4f4
Build the argument stuff in program space
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
128
diff
changeset

87 //I know the preceding line is a duplicate from the above, in the future 
86ec817aa4f4
Build the argument stuff in program space
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
128
diff
changeset

88 //I want to get rid of MAX_ARGS so this will be actually calculated... 
86ec817aa4f4
Build the argument stuff in program space
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
128
diff
changeset

89 
86ec817aa4f4
Build the argument stuff in program space
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
128
diff
changeset

90 pcb.registers[2] = argc; 
86ec817aa4f4
Build the argument stuff in program space
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
128
diff
changeset

91 pcb.registers[3] = (u64)argv; 
86ec817aa4f4
Build the argument stuff in program space
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
128
diff
changeset

92 pcb.registers[15] = PROGRAM_STACK_START  argOffset  STACK_SPACING; 
123
2a35ea7e123b
Moved the psw and registers into a program control block structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
116
diff
changeset

93 pcb.psw.p = 1; 
2a35ea7e123b
Moved the psw and registers into a program control block structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
116
diff
changeset

94 pcb.psw.ea = 1; 
2a35ea7e123b
Moved the psw and registers into a program control block structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
116
diff
changeset

95 pcb.psw.ba = 1; 
149
eb69d1caa83b
First try with storage keys
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
129
diff
changeset

96 pcb.psw.key = PROGSK; 
123
2a35ea7e123b
Moved the psw and registers into a program control block structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
116
diff
changeset

97 pcb.psw.ptr = eHdr>e_entry; 
128
ca9f01e6e2bf
First attempt at argv and argc, probably screwed up a bit it has been so long...
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
126
diff
changeset

98 
123
2a35ea7e123b
Moved the psw and registers into a program control block structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
116
diff
changeset

99 swapcontext_pcb(&shellPCB, &pcb); 
112
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset

100 loopCont: 
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset

101 free(data); 
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset

102 } 
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset

103 } 
aec919038b60
Started working on redoing the directory structure
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff
changeset

104 } 