Note: This is not a problem i'm experiencing, but it is something i'd like to understand (just because i want to be a better person, and to further the horizon of human understanding).
In the bonus chapter of Raymond Chen's book,
Raymond gives the example of a bug in a sound card driver:
The original function, called at hardware interrupt time, looks like this in the DDK:
void FAR PASCAL midiCallback(NPPORTALLOC pPortAlloc, WORD msg, DWORD dwParam1, DWORD dwParm2) { if (pPostAlloc->dwCallback) DriverCallBack(pPortalloc->dwCallback, HIWORD(pPortalloc->dwFlags), pPortalloc->hMidi, msg, dwParam1, dwParam2); }
Their version of the function looked like this:
void FAR PASCAL midiCallback(NPPORTALLOC pPortAlloc, WORD msg, DWORD dwParam1, DWORD dwParm2) { char szBuf[80]; if (pPostAlloc->dwCallback) { wsprintf(szBuf, " Dc(hMidi=%X,wMsg=%X)", pPortalloc->hMidi, msg); #ifdef DEBUG OutputDebugString(szBuf); #endif DriverCallBack(pPortalloc->dwCallback, HIWORD(pPortalloc->dwFlags), pPortalloc->hMidi, msg, dwParam1, dwParam2); } }
Not only is there leftover debug stuff in retail code, but it is calling a noninterrupt- safe function at hardware interrupt time. If the
wsprintf
function ever gets discarded, the system will take a segment-not-present fault inside a hardware interrupt, which leads to a pretty quick death.
Now if i'm looking at that code i wouldn't have guessed that a call to the library function wsprintf would be a problem. What happens if my driver code needs to make use of the Win32 API?
What is a segment fault? i understand the concept of a page-fault: the code i need is sitting on a page that has been swapped out to the hard-drive, and will need to get back from the hard drive before code execution can continue. What is a segment fault when we're inside a device-driver's interrupt?
Is page-faults the protected mode equivalent of a segment-fault? How does one avoid segment faults? Does Windows ever swap out device driver code? How would i stop "wsprintf from being discarded"? What would cause wsprintf to be "discarded"? What is "discarded"? What is the virtue of discarding? When it something *un*discarded
Why is calling an API call from inside a driver bad, and how would one work around it?