Page 1 of 1

Implementing setjmp.h

Posted: Thu Jul 14, 2011 4:05 pm
by xvedejas
Has anyone implemented setjmp() and longjmp() in their kernel? I thought these might've been builtin functions in C (like sizeof() and offsetof()) but it doesn't seem to be that simple. How would I go about implementing setjmp?

Re: Implementing setjmp.h

Posted: Thu Jul 14, 2011 4:29 pm
by Owen
GCC provides __builtin_setjmp and __builtin_longjmp. The alternative is to implement it using (inline) assembly.

Ignoring SSE and the FPU, one i386 option would be

Code: Select all

setjmp:	mov ecx, 4[esp]
	mov 0[ecx], ebp
	mov 4[ecx], esi
	mov 8[ecx], edi
	mov 12[ecx], ebx
	mov eax, 0[esp]
	mov 16[ecx], eax
	xor eax, eax
	ret
longjmp:	mov ecx, 4[esp]
	mov ebp, 0[ecx]
	mov esi, 4[ecx]
	mov edi, 8[ecx]
	mov ebx, 12[ecx]
	mov 8[esp], eax
	jmp 16[ecx]16
However, the builtin option will probably perform better. If my memory serves me right, the builtins use a jmp_buf type of "typedef void* env[4];"
Above assembly code untested. MASM dialect