Right way of handling various exceptions?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Ethin
Member
Member
Posts: 625
Joined: Sun Jun 23, 2019 5:36 pm
Location: North Dakota, United States

Right way of handling various exceptions?

Post by Ethin »

So I know how to handle some of the common exceptions (page fault, double fault, general protection, etc). What I'm confused about handling are the other exceptions -- overflow, bounds range exceeded, division by zero, any SIMD exception, that sort of thing.
What is the proper way of handling these? The intel manual (and even aMD manual) is silent on these and doesn't explain how to handle them. I thought that I might need to go in and "patch" the process that triggered the exception via looking at the RIP and changing it to something else, but that seems both ridiculously invasive and far too complicated. Not to mention the number of conditions that can generate these exceptions. So how do you guys handle these?
Octocontrabass
Member
Member
Posts: 5568
Joined: Mon Mar 25, 2013 7:01 pm

Re: Right way of handling various exceptions?

Post by Octocontrabass »

If something useful can be done in response to an exception, the program that causes the exception will know what needs to be done.

Give programs a way to register a callback. When the program causes an exception, call the callback. If no callback is registered, kill the program.

This also applies to page faults and general protection faults.
nullplan
Member
Member
Posts: 1790
Joined: Wed Aug 30, 2017 8:24 am

Re: Right way of handling various exceptions?

Post by nullplan »

Most of these are simple exceptions. This means, if they are generated in user space, you just deliver a fault to the user process, and if they are generated in kernel space, you generate a kernel panic. On POSIX-like operating systems you deliver a fault in form of a signal. FPU and SIMD exceptions get SIGFPE, divide error also gets SIGFPE with a different reason code, bounds check gets a SIGSEGV, etc.

In kernel mode, these indicate programming errors on your part, so they have to be treated as such. Print all debugging information that you can, then halt the system.
Carpe diem!
Ethin
Member
Member
Posts: 625
Joined: Sun Jun 23, 2019 5:36 pm
Location: North Dakota, United States

Re: Right way of handling various exceptions?

Post by Ethin »

Thanks, that makes sense.
Post Reply