Re: use of setjmp/longjmp in x86 emulator.

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
From: Zachary Amsden
Date: Monday, March 1, 2010 - 9:13 am

On 02/28/2010 11:18 PM, Gleb Natapov wrote:

I'm all for radical ideas, but from a pragmatic point of view, you 
shouldn't use longjmp in the kernel.  Seriously bad things are happening 
with it; it leaves local variables undefined, doesn't undo global state 
changes.

So if you:

spin_lock(&s->lock);
if (!s->active)
     longjmp(buf, -1);

... you are broken.  This case can be made very much more complex and 
hard to reason about by using local variables which are reset by the 
longjmp.

Further, it requires use of the volatile keyword to interact properly 
with logic involving more than one variable, and thus, by definition is 
impossible to use in the kernel, which does not implement the volatile 
keyword.  :)


Instead, for this case, use the fact that there is an architecturally 
designed finite number of exceptions that can be processed 
simultaneously.  This means if you queue exceptions to a pending list of 
control-flow interrupting events to be processed, as long as the queue 
is appropriately sized, you will never overflow this queue and never 
require dynamic allocation.  Further, you can then naturally follow the 
exception priority rules at the top-level of the emulator and never need 
to pass back complex exception structures, merely a simple return value 
which indicates whether to return to top-level control logic or continue 
with instruction emulation.  I believe using this style of programming 
will make your need for setjmp/longjmp go away.

Zach
--
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
use of setjmp/longjmp in x86 emulator., Gleb Natapov, (Mon Mar 1, 2:18 am)
Re: use of setjmp/longjmp in x86 emulator., Takuya Yoshikawa, (Mon Mar 1, 5:45 am)
Re: use of setjmp/longjmp in x86 emulator., Gleb Natapov, (Mon Mar 1, 5:52 am)
Re: use of setjmp/longjmp in x86 emulator., Takuya Yoshikawa, (Mon Mar 1, 6:17 am)
Re: use of setjmp/longjmp in x86 emulator., Gleb Natapov, (Mon Mar 1, 6:26 am)
Re: use of setjmp/longjmp in x86 emulator., Zachary Amsden, (Mon Mar 1, 9:13 am)
Re: use of setjmp/longjmp in x86 emulator., Gleb Natapov, (Mon Mar 1, 10:47 am)
Re: use of setjmp/longjmp in x86 emulator., Zachary Amsden, (Mon Mar 1, 11:39 am)
Re: use of setjmp/longjmp in x86 emulator., Luca Barbieri, (Mon Mar 1, 11:47 am)
Re: use of setjmp/longjmp in x86 emulator., Gleb Natapov, (Mon Mar 1, 12:03 pm)
Re: use of setjmp/longjmp in x86 emulator., john cooper, (Mon Mar 1, 12:13 pm)
Re: use of setjmp/longjmp in x86 emulator., Zachary Amsden, (Mon Mar 1, 12:18 pm)
Re: use of setjmp/longjmp in x86 emulator., H. Peter Anvin, (Mon Mar 1, 3:31 pm)
Re: use of setjmp/longjmp in x86 emulator., H. Peter Anvin, (Mon Mar 1, 3:56 pm)
Re: use of setjmp/longjmp in x86 emulator., Zachary Amsden, (Mon Mar 1, 4:34 pm)
Re: use of setjmp/longjmp in x86 emulator., H. Peter Anvin, (Mon Mar 1, 4:43 pm)
Re: use of setjmp/longjmp in x86 emulator., Gleb Natapov, (Tue Mar 2, 12:28 am)
Re: use of setjmp/longjmp in x86 emulator., Gleb Natapov, (Tue Mar 2, 1:05 am)
Re: use of setjmp/longjmp in x86 emulator., Gleb Natapov, (Tue Mar 2, 1:49 am)
Re: use of setjmp/longjmp in x86 emulator., Avi Kivity, (Sun Mar 7, 2:00 am)
Re: use of setjmp/longjmp in x86 emulator., Avi Kivity, (Sun Mar 7, 2:04 am)
Re: use of setjmp/longjmp in x86 emulator., H. Peter Anvin, (Sun Mar 7, 5:08 pm)
Re: use of setjmp/longjmp in x86 emulator., Eric W. Biederman, (Mon Mar 8, 4:11 pm)
Re: use of setjmp/longjmp in x86 emulator., Gleb Natapov, (Mon Mar 8, 11:28 pm)