OSDev.org

The Place to Start for Operating System Developers
It is currently Sun Sep 19, 2021 10:31 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: C++-like exception handling in kernel
PostPosted: Mon Apr 05, 2021 10:22 am 
Offline
Member
Member
User avatar

Joined: Wed Jan 13, 2021 8:49 am
Posts: 25
Hi!

Is it possible to implement a C++-like exception handling mechanism in kernel code?
I mean throw means something like calling a sw exception with a specific value in eax/rax. And locally define handlers for identifying (by eax/rax value) and handling the exception. (So with a primitive alternative to RTTI).


Top
 Profile  
 
 Post subject: Re: C++-like exception handling in kernel
PostPosted: Mon Apr 05, 2021 10:48 am 
Offline
Member
Member

Joined: Mon Feb 02, 2015 7:11 pm
Posts: 712
You can probably make something like setjmp() and longjump() work. But that won't call C++ destructors. If you want the latter, it's a lot of work, complicated and likely not worth it.

_________________
https://github.com/kiznit/rainbow-os


Top
 Profile  
 
 Post subject: Re: C++-like exception handling in kernel
PostPosted: Mon Apr 05, 2021 12:15 pm 
Offline
Member
Member

Joined: Thu May 17, 2007 1:27 pm
Posts: 972
You can make C++ exceptions work in the kernel (by linking against libgcc and taking the appropriate headers from libsupc++). There is also -fnon-call-exceptions to make GCC emit exception tables for each and every memory access.

But you probably should not do this. Even as a proponent of C++, I would suggest not to use exceptions or RTTI in the kernel.

_________________
managarm: Microkernel-based OS capable of running a Wayland desktop (Discord: https://discord.gg/7WB6Ur3). My OS-dev projects: [mlibc: Portable C library for managarm, qword, Linux, Sigma, ...] [LAI: AML interpreter] [xbstrap: Build system for OS distributions].


Top
 Profile  
 
 Post subject: Re: C++-like exception handling in kernel
PostPosted: Mon Apr 05, 2021 12:21 pm 
Offline
Member
Member

Joined: Mon Feb 02, 2015 7:11 pm
Posts: 712
There is actually a lot more work that that in making C++ exceptions work in the kernel.

Assuming x86_64, you need to configure your compiler for multi-lib support so that you can build a version of libgcc that has mcmodel=kernel and mno-red-zone.

You also need a pthread implementation (unless you decide to customize libgcc with your own threading model, which is even more work).

You also need malloc/free as libgcc will rely on that at initialisation and when throwing or handling exceptions (I can't remember the specifics).

Assuming x86_64, using the GCC provided C++ headers is also problematic: they use floating point numbers in a number of places and you can't have that without enabling SSE in your kernel (x86_64 ABI says float params / return values go in XMM registers), which opens a whole can of worms. You could define your own C++ headers and compile libgcc with them, I didn't try it.

I am sure there was more when I did it, I just can't recall everything right now.

And after all this work, you have something that only works with GCC (and not clang or other compilers). I concluded that it wasn't worth it to have C++ exceptions in the kernel.

_________________
https://github.com/kiznit/rainbow-os


Top
 Profile  
 
 Post subject: Re: C++-like exception handling in kernel
PostPosted: Mon Apr 05, 2021 12:49 pm 
Offline
Member
Member

Joined: Thu May 17, 2007 1:27 pm
Posts: 972
Oh I forgot about pthreads. Yes, that's annoying indeed.

(However, why won't that strategy work with Clang? Clang's RTTI and exception ABI is compatible with GCC's, and Clang can also use libgcc if you pass --gcc-toolchain=<path> to Clang.)

_________________
managarm: Microkernel-based OS capable of running a Wayland desktop (Discord: https://discord.gg/7WB6Ur3). My OS-dev projects: [mlibc: Portable C library for managarm, qword, Linux, Sigma, ...] [LAI: AML interpreter] [xbstrap: Build system for OS distributions].


Top
 Profile  
 
 Post subject: Re: C++-like exception handling in kernel
PostPosted: Mon Apr 05, 2021 12:50 pm 
Offline
Member
Member

Joined: Mon Feb 02, 2015 7:11 pm
Posts: 712
My bad, I assumed clang worked differently because it doesn't have libgcc (another assumption).

It is possible that the clang implementation has different requirements than libgcc, but this is unlikely to matter in practice if you are willing to add all the pieces required to support C++ exceptions.

Another alternative I looked into was to not use libgcc at all and write my own implementation to support C++ exceptions... But that's a lot of work as well.

_________________
https://github.com/kiznit/rainbow-os


Top
 Profile  
 
 Post subject: Re: C++-like exception handling in kernel
PostPosted: Mon Apr 05, 2021 1:32 pm 
Offline
Member
Member

Joined: Thu May 17, 2007 1:27 pm
Posts: 972
Yes, rolling your own exception library requires writing a DWARF parser; that is not really a nice exercise either.

_________________
managarm: Microkernel-based OS capable of running a Wayland desktop (Discord: https://discord.gg/7WB6Ur3). My OS-dev projects: [mlibc: Portable C library for managarm, qword, Linux, Sigma, ...] [LAI: AML interpreter] [xbstrap: Build system for OS distributions].


Top
 Profile  
 
 Post subject: Re: C++-like exception handling in kernel
PostPosted: Thu May 06, 2021 10:44 am 
Offline

Joined: Thu May 06, 2021 10:41 am
Posts: 1
You can probably combine long/setjmp with the gcc destructor attribute. You can create try/catch using macros too.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group