This says to me that the error code presence is almost completely undefined from the point of view of the handler itself.Error codes are not pushed on the stack for exceptions that are generated externally (with the INTR or LINT[1:0] pins) or the INT n instruction, even if an error code is normally produced for those exceptions.
I assume no interrupts that CAN generate error codes can be triggered externally, which I believe is true but it is very misleading that the Intel doc says this a special case when the error code is not pushed?
There is no way you can push the error code if you call "int N" because the error code is pushed after the return RIP.
You can't assume the return RIP is anywhere, so if you try to manually check the opcode of the instruction just before the one pointed to by the return RIP and you were wrong (meaning you used the error code as the return RIP) you will probably triple fault.
The maximum value of the pushed error code is 0xFFFF since that covers the only defined non-zero fields for either normal codes or page fault codes. I suppose you could just never map the virtual memory pages that correspond to 0x0-0xFFFF and then check if [RSP + 8] <= 0xFFFF to guarantee error code detection. But stepping back, that is a terrible solution.
And finally, you can just say: never explicitly call an interrupt with "int N" that normally auto-generates an error code. Although I cannot find a statement like this in the docs, it seems to be almost reasonable.
Unless I don't understand part of the documentation available, there is no good solution to this problem? How do you handle it?