changeset 157:a6482018de66

Storage keys for program text are now set when running the program When a program is being run, the storage keys for its entire text segment are change to be the program storage key right before it is run. Additionally, that memory is marked to be read-only to the program. I fixed an issue with make depend (not that it will matter since I am about to switch to cmake) I fixed some of the comments in elf.h
author Jonathan Pevarnek <pevarnj@gmail.com>
date Thu, 24 Nov 2011 19:48:24 -0500
parents 2cf6fb157764
children 89be7c9881b5
files Makefile include/os/elf.h src/os/shell.c src/os/storageKeys.c
diffstat 4 files changed, 90 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile	Tue Nov 08 09:29:22 2011 -0500
+++ b/Makefile	Thu Nov 24 19:48:24 2011 -0500
@@ -121,9 +121,68 @@
 	$(AS) -m64 -o $@ $<
 
 depend:
-	makedepend -I include src/*.c
+	makedepend -I include src/*.c src/os/*.c src/prog/*.c
 
 # DO NOT DELETE
 
 src/testFS.o: include/std.h include/die.h include/memHead.h include/os/fs.h
 src/testFS.o: include/error.h include/os/fsStructs.h
+src/os/except.o: include/os/psw.h include/os/progint.h include/std.h
+src/os/except.o: include/die.h include/memHead.h include/string.h
+src/os/except.o: include/stdio.h include/os/scall.h include/os/pcb.h
+src/os/except.o: include/os/svc.h
+src/os/fs.o: include/os/fs.h include/std.h include/die.h include/memHead.h
+src/os/fs.o: include/error.h include/os/fsStructs.h include/string.h
+src/os/fs.o: include/tod.h
+src/os/heap.o: include/os/heap.h include/memHead.h include/os/storageKeys.h
+src/os/heap.o: include/os/memLayout.h
+src/os/memStack.o: include/memStack.h include/error.h include/std.h
+src/os/memStack.o: include/die.h include/memHead.h
+src/os/scall.o: include/os/scall.h include/os/psw.h include/os/pcb.h
+src/os/scall.o: include/os/svc.h include/os/svcNums.h include/os/fs.h
+src/os/scall.o: include/std.h include/die.h include/memHead.h include/error.h
+src/os/scall.o: include/os/fsStructs.h include/os/heap.h
+src/os/scall.o: include/os/storageKeys.h include/memStack.h
+src/os/shell.o: include/std.h include/die.h include/memHead.h
+src/os/shell.o: include/string.h include/stdio.h include/error.h
+src/os/shell.o: include/os/fs.h include/os/fsStructs.h include/os/elf.h
+src/os/shell.o: include/os/psw.h include/os/svc.h include/os/scall.h
+src/os/shell.o: include/os/pcb.h include/os/storageKeys.h
+src/os/shell.o: include/os/memLayout.h
+src/os/std.o: include/std.h include/die.h include/memHead.h include/string.h
+src/os/std.o: include/prog/svcCalls.h include/prog/svc_prog.h
+src/os/std.o: include/os/svcNums.h include/error.h
+src/os/stdio.o: include/stdio.h include/std.h include/die.h include/memHead.h
+src/os/stdio.o: include/string.h include/stdarg.h
+src/os/storageKeys.o: include/os/storageKeys.h include/os/memLayout.h
+src/os/storageKeys.o: include/string.h
+src/os/string.o: include/string.h
+src/prog/dynamic.o: include/prog/svcCalls.h include/prog/svc_prog.h
+src/prog/dynamic.o: include/os/svcNums.h include/error.h include/std.h
+src/prog/dynamic.o: include/die.h include/memHead.h include/string.h
+src/prog/dynamic.o: include/stack.h
+src/prog/echo.o: include/prog/svcCalls.h include/prog/svc_prog.h
+src/prog/echo.o: include/os/svcNums.h include/error.h include/std.h
+src/prog/echo.o: include/die.h include/memHead.h include/string.h
+src/prog/hello.o: include/prog/svcCalls.h include/prog/svc_prog.h
+src/prog/hello.o: include/os/svcNums.h include/error.h include/std.h
+src/prog/hello.o: include/die.h include/memHead.h
+src/prog/ls.o: include/os/fsStructs.h include/prog/svcCalls.h
+src/prog/ls.o: include/prog/svc_prog.h include/os/svcNums.h include/error.h
+src/prog/operations.o: include/prog/svcCalls.h include/prog/svc_prog.h
+src/prog/operations.o: include/os/svcNums.h include/error.h
+src/prog/operations.o: include/prog/operations.h include/std.h include/die.h
+src/prog/operations.o: include/memHead.h include/stack.h include/string.h
+src/prog/operations.o: include/math.h
+src/prog/rpn.o: include/prog/svcCalls.h include/prog/svc_prog.h
+src/prog/rpn.o: include/os/svcNums.h include/error.h include/std.h
+src/prog/rpn.o: include/die.h include/memHead.h include/string.h
+src/prog/rpn.o: include/prog/operations.h include/stack.h
+src/prog/stack.o: include/stack.h include/std.h include/die.h
+src/prog/stack.o: include/memHead.h
+src/prog/std.o: include/std.h include/die.h include/memHead.h
+src/prog/std.o: include/string.h include/prog/svcCalls.h
+src/prog/std.o: include/prog/svc_prog.h include/os/svcNums.h include/error.h
+src/prog/stdio.o: include/stdio.h include/std.h include/die.h
+src/prog/stdio.o: include/memHead.h include/string.h include/stdarg.h
+src/prog/string.o: include/string.h
--- a/include/os/elf.h	Tue Nov 08 09:29:22 2011 -0500
+++ b/include/os/elf.h	Thu Nov 24 19:48:24 2011 -0500
@@ -75,14 +75,14 @@
  * ELF program header
  */
 typedef struct {
-	Elf64_Word	p_type;			/* Segment type */
-	Elf64_Word	p_flags;		/* Segment file offset */
-	Elf64_Off	p_offset;		/* Segment virt. addr */
-	Elf64_Addr	p_vaddr;		/* <undefined> */
-	Elf64_Addr	p_paddr;		/* Segment size in file */
-	Elf64_Xword	p_filesz;		/* Segment size in mem */
-	Elf64_Xword	p_memsz;		/* Segment flags */
-	Elf64_Xword	p_align;		/* Segment alignment */
+	Elf64_Word	p_type;     /* Segment type */
+	Elf64_Word	p_flags;    /* Segment flags */
+	Elf64_Off	p_offset;     /* Segment file offset */
+	Elf64_Addr	p_vaddr;    /* Segment virt. addr */
+	Elf64_Addr	p_paddr;    /* <undefined> */
+	Elf64_Xword	p_filesz;   /* Segment size in file */
+	Elf64_Xword	p_memsz;    /* Segment size in mem */
+	Elf64_Xword	p_align;    /* Segment alignment */
 } Elf64_Phdr;
 
 #endif
--- a/src/os/shell.c	Tue Nov 08 09:29:22 2011 -0500
+++ b/src/os/shell.c	Thu Nov 24 19:48:24 2011 -0500
@@ -48,10 +48,12 @@
 			if(isError(getFileData(fid, data))) goto loopCont;
 			Elf64_Ehdr *eHdr = data;
 			if(eHdr->e_ident[EI_MAG0] != ELFMAG0 || eHdr->e_ident[EI_MAG1] != ELFMAG1 ||
-					eHdr->e_ident[EI_MAG2] != ELFMAG2 || eHdr->e_ident[EI_MAG3] != ELFMAG3)
-				goto loopCont;
+					eHdr->e_ident[EI_MAG2] != ELFMAG2 || eHdr->e_ident[EI_MAG3] != ELFMAG3) {
+				goto loopCont; //problem with program, ask for a new one
+			}
 			if(eHdr->e_ident[EI_CLASS] != ELFCLASS64) goto loopCont;
 			int i;
+			size_t textEnd = PROGRAM_TEXT_START; //assume that there is no text
 			for(i = 0; i < eHdr->e_phnum; i++) { //go through all the headers
 				Elf64_Phdr *pHdr = ((Elf64_Phdr*)(data + eHdr->e_phoff) + i);
 				if(pHdr->p_type == PT_LOAD) { //these segments get loaded into the memory
@@ -60,8 +62,18 @@
 					memcpy(toLoc, fromLoc, pHdr->p_filesz);
 					size_t diff = pHdr->p_memsz - pHdr->p_filesz;
 					if(diff) memset(toLoc + pHdr->p_filesz, 0, diff);
+					size_t endLoc = pHdr->p_vaddr + pHdr->p_memsz;
+					if(endLoc > textEnd) {
+						textEnd = endLoc; //ending location of the program text
+					}
 				}
 			}
+			
+			//allow the program to have read-only access to its text
+			size_t sKeyPtr;
+			for(sKeyPtr = PROGRAM_TEXT_START; sKeyPtr < textEnd; sKeyPtr += BLOCKSIZE) {
+				setStorageKey(sKeyPtr, PROGSK, 0);
+			}
 
 			PCB pcb;
 			memset(&pcb, 0, sizeof(pcb));
@@ -97,6 +109,11 @@
 			pcb.psw.ptr = eHdr->e_entry;
 
 			swapcontext_pcb(&shellPCB, &pcb);
+
+			//unset any storage keys that were set for the program text
+			for(sKeyPtr = PROGRAM_TEXT_START; sKeyPtr < textEnd; sKeyPtr += BLOCKSIZE) {
+				setStorageKey(sKeyPtr, 0, 1);
+			}
 		loopCont:
 			free(data);
 		}
--- a/src/os/storageKeys.c	Tue Nov 08 09:29:22 2011 -0500
+++ b/src/os/storageKeys.c	Thu Nov 24 19:48:24 2011 -0500
@@ -31,7 +31,9 @@
 	intptr_t ptr = 0x0;
 	u8 key;
 	for(ptr = 0x0; ptr < memsize; ptr += BLOCKSIZE) {
-		if(ptr >= PROGRAM_STACK_START - STACK_SIZE && ptr < PROGRAM_TEXT_START + 0x10000) {
+		//BIG MEMO FOR FUTURE REFERENCE SO I STOP DEBUGGING DUMB THINGS FOR HOURS:
+		//This is for the stack
+		if(ptr >= PROGRAM_STACK_START - STACK_SIZE && ptr < PROGRAM_STACK_START) {
 			//is in the range the program should be accessing
 			key = PROGSK;
 		} else {