Add further optimizations and fix bugs in exception handling

Add a few more optimizations to the memory functions patch, and
fix bugs in the fix-up code for exceptions. The number of
registers saved on the stack is significantly reduced compared
to original implementation.

Also add a version that applies cleanly to newer kernels that
already have the memset fix applied.