-Matt
Sunday, March 3, 2013
Bird Trajectory Miscalculation
Tuesday, February 12, 2013
Picking up the Trash and Caller vs Callee Saved Registers
To me the difference between caller and callee saved registers matters. Why is that? Well, for one, I am writing a garbage collector and registers along with stack variables and global variables make up the root-set. The root-set is a set of variables that the garbage collector knows about and it is where the collector begins tracing. In short, for each variable in the root set, the collector follows any pointers that variable might have. For any variable that can be reached, starting from the root-set, that variable is said to be alive, and its memory cannot be reclaimed. Any variables not found through tracing can have their memory reclaimed. There it is, garbage collector 101, see you next semester.
As I mentioned, registers make up the root-set. Since the registers can contain pointers to data that was allocated, it is necessary to trace them at garbage collection time. If that allocated data can't be reached, the collector can recycle it and conserve memory resources. But an interesting thing occurs, and this is based on calling convention, how the compiler (or assembly-level programmer) constructs calls to functions. Some registers must be traced and some should be ignored. This, my friends, is the difference between caller and callee saved registers. The callee-saved registers must be traced. You will see why in just a jiffy!
These caller vs. callee registers are defined by the calling convention such as the Sys-V ABI for Intel Processors and/or another calling convention standard. For instance, the former is similar to the cdecl calling convention, and is the convention we will investigate here, as my work space is gcc and it constructs programs in a somewhat similar manner (similar enough to convey the message in this post). These conventions specify how certain registers are to be used. Since registers are global, all functions can see and access/modify them. So, it goes to say that specifying which registers a callee (subroutine) function can manipulate and which registers a caller must save is probably a good thing. As there are times where a caller wants to reclaim data it never stored on the stack, but left lying around in a register.
A caller-saved register is a register that a calling function must save before it calls any other function. As the other function, the callee, can use this fiddle with the register. These are "volatile" and if the data is not stored elsewhere before a function call, they could be lost. That is why these registers are typically stored on the stack.
A callee-saved register is a register that a callee must preserve if it wants to further use the register to store other information. A non-volatile register.
Given my environment (linux on a x86-64bit CPU and compile using gcc), and according to my references the caller and callee saved registers for such a configuration are as follows:
Caller-saved registers: rax, rcx, rdx
Callee-saved registers: rbp, rsp, rdi, rsi, rbx, r12, r13, r14, r15, rip
This is a pretty clear distinction, but a simple example goes a long way... here goes, in pseudo-assembly code. The function foo adds 123 to 456 and then calls bar. The result of calling bar is then added to 123 + 456:
# Comments:
foo: # Function foo entry
mov 123, rax # rax = 123
add rax, 456 # rax = 123 + 456
push rax # Caller saved
call bar # bar()
pop rbx # Restore the saved value (123 + 456 or 579)
add rax, rbx # rax = rax + rbx (whatever bar returned + 579)
The common calling convention here cdecl, which is similar to the SysV ABI for
Intel and what gcc uses, mentions that return values are stored in the rax
register. rax is a caller-saved register, it's volatile. If foo
doesn't save it, bar might fiddle with it and change its value. While I don't
show a body for bar, suppose it uses register r12 to do some of its math.
r12 is a callee-saved register, meaning that the data in r12 is preserved by
the callee. Suppose bar looks like:
bar:
push r12 # Saving whatever is in r12
mov 789, r12 # r12 = 789
add r12, 42 # r12 = r12 + 42
mov r12, rax # rax = r12
pop r12 # Restore r12
return # return rax
A keen eye will notice that r12 is being used in bar as a callee-saved
register, as what the calling convention requires. bar is free to use r12 but
it must restore it before it returns. This means that foo does not have to push
anything to the stack, in fact foo could be written as follows:
foo: # Function foo entry
mov 123, rax # rax = 123
add rax, 456 # rax = 123 + 456
mov rax, r12 # rax is caller saved
call bar # bar()
mov r12, rbx # Restore the saved value (123 + 456 or 579)
add rax, rbx # rax = rax + rbx (whatever bar returned + 579)
Instead of pushing to the stack, foo in this case knows that r12 is a callee-saved, non-volatile, register and makes use of that fact. Now, a keener eye might be saying, well... foo might be a callee of another function. Maybe main called foo well, yes, foo should follow convention and preserve r12.
This is the message for garbage collectors. The garbage collection function is called (probably from the memory allocator, when it notices that memory is low). When the collector function begins to execute, it knows of the callee-saved registers as the caller should have already stashed the data it cared about on its stack or in a callee-saved register.
A few ways to programmatically access the registers is via the setjmp, sigsetjmp, and/or ucontext glibc function calls. For instance, the sigsetjmp is one method that the Bohem collector uses to access registers for his garbage collector (libgc). If you were to look at the setjmp family you would learn that it returns only callee-saved registers, and not the full register set. Why is that? setjmp stores the environment into a structure. This includes just the callee-saved registers, since the caller should have preserved its caller-saved registers onto the onto the stack or in a callee-saved register. Consider the flow: main() --> functionA() --> functionB() --> setjmp(). setjmp would return the callee-saved registers and functionA can make use of these. As the registers values relevant for main, and functionA would be stored elsewhere (stack or callee-saved).
I am pretty sure this post is not free from flaws, but I hope it made sense, or that any flaws do not crush the integrity of the data in this post. Thanks for reading.
Resources:
- Defined: http://zhongshugu.wordpress.com/2011/02/23/caller-save-registers-and-callee-save-registers/
- Defined: http://msdn.microsoft.com/en-us/library/6t169e9c(v=vs.90).aspx
- Defined: http://stackoverflow.com/questions/9268586/whats-are-callee-and-caller-saved-registers
- Defined: http://pdos.csail.mit.edu/6.828/2004/lec/l2.html
- Calling Convention: http://en.wikipedia.org/wiki/X86_calling_conventions#cdecl
- Calling Convention: http://www.delorie.com/djgpp/doc/ug/asm/calling.html
- ABI: SystemV Application Binary Interface for Intel386 Architecture Processor Supplement, Fourth Edition
- Bohem Collector: http://www.hpl.hp.com/personal/Hans_Boehm/gc/#details
- glibc-2.14 Source Code
Monday, February 4, 2013
TBL in Melb
-Matt
Monday, January 14, 2013
History Lesson
-Matt
Monday, December 31, 2012
3...2..1... Collect the Trash!
Well, why do I want to dance with a garbage collector? Isn't that the evil-bastard-child/antithesis to region based memory management (RBMM), what I have been working on the past two years or so? Nope! Garbage collection can be a compliment. See, RBMM works by analyzing a program at compile-time (my system uses a gcc-plugin). From this analysis the compiler can insert memory allocation and reclaim calls (think malloc/free) at compile time. My system automatically free's memory, based on which objects point to which other objects. These groupings of objects can come from the same region of memory. A region cannot be reclaimed until all objects in it are no longer needed. Then, in one quick swoop, the memory of the region (the space that all the objects take-up) can be reclaimed. This fast memory reclaim is a key benefit of RBMM, as there is no need to visit each object and reclaim its memory individually.
Unfortunately, not all object lifetimes can be decided at compile time, such as global variables, or values pointed to/aliased by globals. This means that some objects cannot be reclaimed based on a compile-time static analysis. If these undecidable-lifetime objects are in a region, then that region lives for the entire lifetime of the program. Static analysis cannot force a region to die, unless it can be 100% sure that the region contents (objects) are never needed again. Global nastyness creates region bloat, as some objects might be reclaimable, and not others. As I previously mentioned, a region cannot be removed until ALL of its objects are no longer alive. If a global variable pops into the region, then that region now lives forever. Like a virus.
The above issue means that a garbage collector can actually benefit an RBMM system. That is what I have been working on: a combining of RBMM and garbage collection. This is not unique, and has been done before; however, the way we implement our collector is unique, but that is not discussed in this post.
Disclaimer: Grammar nuts please note that this is a blog post, so I am immediately relieved of any misuse of narrative modes. This is a design for my PhD and something that I have worked on with my advisers quite a bit (the "we"). The code is mine.
What I want to discuss in this post is how we locate stack variables. Stack variables form a subset of the root-set. The root-set being a collection of variables that are live at the time of garbage collection. If any data is accessible through the root-set then that object is also alive and must survive garbage collection. Else, the memory for data/objects that is not reachable can be collected. I will not go into details, or even the collector type I have chosen (e.g. mark-sweep, or copy, etc). Instead, I just want to talk about finding the stack variables; as I said a sub-set of the root set. Globals and registers also can be part of the root-set, but this post is only about stack variables </redundancy>.
When the garbage collector kicks-in the collector needs to start somewhere. It starts by scanning the root-set. Well, how does the collector get this root set? Specifically, what this post is concerned with is obtaining a sub-set of the roots, the stack variables. If you are familiar with stack-frames or activation records, the values there in the stack must be scanned. If any of those variables point to objects dynamically allocated (e.g. heap variables or variables from a region), then such objects must survive collection, and their memory cannot be recycled.
I have implemented a compiler modification (gcc-plugin) which provides hints to the garbage collector as to how to trace the stack of the program. This is how the collector gets the subset of the root-set, the stack variables. Actually, it is so cool and nifty I warranted this entire blog post to the notion.
My aim is to provide, at compile-time, a table that can be read by the garbage collector at runtime. During compile-time, as the source-code is being processed my plugin locates all function calls in the program. At the return of each function call a label is inserted (think assembly goto/label). This label is unique for each callsite, and makes up the key into a table that I am building. This key points to information about the function that the label is inside. For the example just below, the three labels are unique, but all point to the same function information. Information about my_function(), which specifies what local variables it has (x, y, z), if the variables are pointers, and how big the variables are.
void my_function(void)
{
int x;
float y;
double z;
foo();
__label0:
bar();
__label1:
baz();
__label2;
}
The function above has three callees (foo, bar, baz). At each call site,
my plugin inserts a label. Now, why do I insert labels? Well, at runtime we
need a way of associating the return point for each function call (foo, bar,
baz). At compile time I do not have any addresses to use, hell, addresses are
created by the OS when the executable is loaded; we haven't even finished
compiling the source code yet! The solution is to insert a
label which can be used at compile time to build a table of stack-variable
information. The loader will fix-up the addresses, and at runtime the labels
will have the correct address that the function call returns to. Recall that
each time a function is called, the return instruction pointer RIP or return
address will be stored on the stack. This value will be the same as the
respected label. For instance, consider that my_function() is being executed,
and the line which calls bar() is executed. When bar() gets called, the return
point, which now happens to also match __label1, is placed onto the stack. And
when bar() returns, the return point is now the same as __label1. The only
thing we have done so far is insert labels. No big deal. Note: I doubt anyone except for maybe one person has read this far, if you do, email me, you kick so much ass, that next time we meet, I will buy you coffee produced from the most lavish and rare of coffee seeds: unicorn digested coffee. I will then serenade you with a ear-piercing song and proclaim you Captain Badassery and dance around bear-chested wearing a Burger-King crown and throw pennies at your feet.
As mentioned, the labels are entries into a table that contains function information (variables, stack size, etc). When the garbage collector kicks-in, it looks at the stack and locates the return point. That value is a key into a function table my plugin generated, remember the RIP coincides with the added labels. The collector uses the RIP/label as a key to get function information, and then processes the variables (figuring out which heap values can be accessed though the local variables in that function). Once that function is scanned, its caller function is scanned, and so on... it's turtles all the way down... until the collector hits main(). Then this portion of garbage collection is complete.
TLDR; Anyways, the above diatribe is an expose into the use of labels for providing a means of accessing stack variables, and providing a compile-time generated way of performing a runtime stack trace.
Have an awesome New Year and may all of your garbage be collected properly!
-Matt
Monday, December 24, 2012
Have a Happy Astrophysical Season!
-Matt
Tuesday, December 4, 2012
What What?! More Graffiti!
-Matt