Skip to content

Commit

Permalink
Fixed Issue 13: #13.
Browse files Browse the repository at this point in the history
Modified sva_release_stack to use ghostFree() to deallocate a
thread's remaining Ghost Memory.

Modified ghostFree() so that it can free Ghost Memory belonging
to threads other than the one currently running on the CPU.
This is needed by sva_release_stack().

Modified ghostFree() so that it can free Ghost Memory that is
not yet backed by physical memory.

Fixed Issue 13: #13.
  • Loading branch information
jtcriswell committed Aug 2, 2017
1 parent fc2c980 commit 2d81cae
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
36 changes: 30 additions & 6 deletions SVA/lib/secmem.c
Original file line number Diff line number Diff line change
Expand Up @@ -207,18 +207,32 @@ allocSecureMemory (void) {
*/
void
ghostFree (struct SVAThread * threadp, unsigned char * p, intptr_t size) {
/* Per-CPU data structure maintained by SVA */
struct CPUState * cpup;

/* Pointer to thread currently executing on the CPU */
struct SVAThread * currentThread;

/*
* If the amount of memory to free is zero, do nothing.
*/
if (size == 0) {
return;
}

/*
* Get a pointer to the thread currently running on the CPU.
*/
cpup = getCPUState();
currentThread = cpup->currentThread;

/*
* Verify that the memory is within the secure memory portion of the
* address space.
*/
uintptr_t pint = (uintptr_t) p;
if ((SECMEMSTART <= pint) && (pint < SECMEMEND) &&
(SECMEMSTART <= (pint + size)) && ((pint + size) < SECMEMEND)) {
/*
* Zero out the contents of the ghost memory.
*/
memset (p, 0, size);

/*
* Loop through each page of the ghost memory until all of the frames
* have been returned to the operating system kernel.
Expand All @@ -231,6 +245,14 @@ ghostFree (struct SVAThread * threadp, unsigned char * p, intptr_t size) {
*/
uintptr_t paddr = getPhysicalAddr (ptr);

/*
* Zero out the contents of the ghost memory if it has been mapped
* in the current address space.
*/
if ((paddr) && (threadp == currentThread)) {
memset (ptr, 0, X86_PAGE_SIZE);
}

/*
* Unmap the memory from the secure memory virtual address space.
*/
Expand All @@ -246,7 +268,9 @@ ghostFree (struct SVAThread * threadp, unsigned char * p, intptr_t size) {
* implementation in which it only releases one page at a time to the
* OS.
*/
releaseSVAMemory (paddr, X86_PAGE_SIZE);
if (paddr) {
releaseSVAMemory (paddr, X86_PAGE_SIZE);
}
}
}

Expand Down
12 changes: 6 additions & 6 deletions SVA/lib/state.c
Original file line number Diff line number Diff line change
Expand Up @@ -1036,13 +1036,13 @@ sva_release_stack (uintptr_t id) {
return;

/*
* Release ghost memory. Be sure to use the value of CR3 belonging to the
* thread that is being released.
* Release ghost memory belonging to the thread that we are deallocating.
*/
for (uintptr_t size=0; size < newThread->secmemSize; size += X86_PAGE_SIZE) {
if (vg) {
unmapSecurePage (newThread, (unsigned char *)(SECMEMSTART + size));
}
if (vg) {
extern void
ghostFree (struct SVAThread * tp, unsigned char * p, intptr_t size);
unsigned char * secmemStart = (unsigned char *)(SECMEMSTART);
ghostFree (newThread, secmemStart, newThread->secmemSize);
}

/*
Expand Down

0 comments on commit 2d81cae

Please sign in to comment.