OSDev.org

The Place to Start for Operating System Developers
It is currently Tue Apr 16, 2024 3:11 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 16 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: idt and exceptions
PostPosted: Tue Jul 07, 2020 3:35 pm 
Offline

Joined: Tue Jul 07, 2020 3:18 pm
Posts: 17
A little question. I made a little kernel wit idt realization.
Even keyboard are working.

But this c code not generate exceptions:
Code:
int x = 5/0;

int f[4];
f[5] = 8;   


And with asm code any exception generated right:
Code:
asm("int $0");


Help me understand why this can happen.


Top
 Profile  
 
 Post subject: Re: idt and exceptions
PostPosted: Fri Jul 10, 2020 1:34 am 
Offline
Member
Member

Joined: Sun Apr 05, 2020 1:01 pm
Posts: 183
Code:
int x = 5/0;


This either gets optimized out, or would end up generating a UD2 instruction.

Code:
int f[4];
f[5] = 8;


What exception would you expect this to generate? You're just corrupting your own stack here.

Code:
asm("int $0");


Yes, this makes sense


Top
 Profile  
 
 Post subject: Re: idt and exceptions
PostPosted: Fri Jul 10, 2020 1:49 am 
Offline
Member
Member

Joined: Tue Mar 04, 2014 5:27 am
Posts: 1108
This should generate a division by 0 exception:
Code:
volatile int x = 0;
x = 1 / x;


Top
 Profile  
 
 Post subject: Re: idt and exceptions
PostPosted: Fri Jul 10, 2020 12:06 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5134
This is guaranteed to generate a division by zero exception:
Code:
asm("div %ah");


Top
 Profile  
 
 Post subject: Re: idt and exceptions
PostPosted: Mon Feb 01, 2021 4:48 am 
Offline

Joined: Tue Jul 07, 2020 3:18 pm
Posts: 17
It's been something with gcc optimization flags. I remove -O2 and all works. Sorry for late answer :roll:


Top
 Profile  
 
 Post subject: Re: idt and exceptions
PostPosted: Mon Feb 01, 2021 7:58 am 
Offline
Member
Member
User avatar

Joined: Sun Oct 11, 2020 9:46 pm
Posts: 370
Location: United States
JustVic wrote:
It's been something with gcc optimization flags. I remove -O2 and all works. Sorry for late answer :roll:

Your exception handlers work. BUT DON'T REMOVE OPTIMIZATION. Instead, use the volatile keyword on lines you don't want optimized out.

_________________
Skylight: https://github.com/austanss/skylight

I make stupid mistakes and my vision is terrible. Not a good combination.

NOTE: Never respond to my posts with "it's too hard".


Top
 Profile  
 
 Post subject: Re: idt and exceptions
PostPosted: Mon Feb 01, 2021 9:20 am 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 4594
Location: Chichester, UK
It's actually a very good idea to remove optimization whilst developing an OS. It makes it far easier to debug. Obviously, once you have your code running you will want to enable optimization. This may break things in which case it should be relatively easy to find the cause by comparing the optimized and non-optimized versions of the code.


Top
 Profile  
 
 Post subject: Re: idt and exceptions
PostPosted: Mon Feb 01, 2021 9:59 am 
Offline
Member
Member
User avatar

Joined: Sun Oct 11, 2020 9:46 pm
Posts: 370
Location: United States
I personally use -Ofast.

_________________
Skylight: https://github.com/austanss/skylight

I make stupid mistakes and my vision is terrible. Not a good combination.

NOTE: Never respond to my posts with "it's too hard".


Top
 Profile  
 
 Post subject: Re: idt and exceptions
PostPosted: Mon Feb 01, 2021 10:26 am 
Offline
Member
Member

Joined: Tue Feb 18, 2020 3:29 pm
Posts: 1071
I agree with @iansjack. You should seperate Debug and Release builds. A debug build is built with -g and -O0. I've heard that GCC offers -Og for debug friendly optimizations, but I've also heard that it has its issues. A release build is built with -O3 and if you use assert -DNDEBUG. But using optimizations with debug build can cause problems with GDB. Also, compiling with -O3 on always may cover up some hidden bugs. This happened in my last OS. I compiled with -O3, and a bug went away, but with -O0, it occured every so often. Optimizations don't make sense :roll: .

_________________
"How did you do this?"
"It's very simple — you read the protocol and write the code." - Bill Joy
Projects: NexNix | libnex | nnpkg


Top
 Profile  
 
 Post subject: Re: idt and exceptions
PostPosted: Mon Feb 01, 2021 11:14 am 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 4594
Location: Chichester, UK
rizxt wrote:
I personally use -Ofast.

That probably explains the number of problems you seem to have, particularly with regard to debugging. I wouldn't use such aggressive optimization until I was fairly sure that my code is correct.

Interestingly, a lot of people find that -Os produces not only the most compact code but also the fastest (but not until you've finished debugging).


Top
 Profile  
 
 Post subject: Re: idt and exceptions
PostPosted: Mon Feb 01, 2021 11:23 am 
Offline
Member
Member
User avatar

Joined: Sun Oct 11, 2020 9:46 pm
Posts: 370
Location: United States
iansjack wrote:
rizxt wrote:
I personally use -Ofast.

That probably explains the number of problems you seem to have, particularly with regard to debugging. I wouldn't use such aggressive optimization until I was fairly sure that my code is correct.

Interestingly, a lot of people find that -Os produces not only the most compact code but also the fastest (but not until you've finished debugging).

I dunno, optimizations have never been an issue for me. If I am really stumped on an issue I will try removing optimizations, but even then it still doesn't work.

_________________
Skylight: https://github.com/austanss/skylight

I make stupid mistakes and my vision is terrible. Not a good combination.

NOTE: Never respond to my posts with "it's too hard".


Top
 Profile  
 
 Post subject: Re: idt and exceptions
PostPosted: Mon Feb 01, 2021 11:29 am 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 4594
Location: Chichester, UK
You just don't realize that they have been an issue. Your comments on gdb tell a different story to me.

But, hey - go with what works for you.


Top
 Profile  
 
 Post subject: Re: idt and exceptions
PostPosted: Mon Feb 01, 2021 3:36 pm 
Offline
Member
Member

Joined: Tue Feb 18, 2020 3:29 pm
Posts: 1071
@iansjack is right. When I was working on an older kernel, GDB would jump around a lot when compiling with -O3. But not -O0.

_________________
"How did you do this?"
"It's very simple — you read the protocol and write the code." - Bill Joy
Projects: NexNix | libnex | nnpkg


Top
 Profile  
 
 Post subject: Re: idt and exceptions
PostPosted: Mon Feb 01, 2021 11:50 pm 
Offline
Member
Member

Joined: Wed Aug 30, 2017 8:24 am
Posts: 1604
We are getting kind of off-topic, but: I always compile with full optimizations and debug using log messages instead of GDB. The GDB model is abhorrent to me, as it requires me to constantly test a version of the program I will not ship, and then ship a version I have not tested thoroughly. There is some observer effect to the log messages, yes, but there is a hell of a lot more to the mere use of GDB. If I do use debugging capabilities, I use those that show me what is actually going on, not what would be going on if this was a C64 and I was writing in BASIC.

_________________
Carpe diem!


Top
 Profile  
 
 Post subject: Re: idt and exceptions
PostPosted: Tue Feb 02, 2021 1:45 am 
Offline
Member
Member

Joined: Mon Dec 07, 2020 8:09 am
Posts: 212
nullplan wrote:
We are getting kind of off-topic, but: I always compile with full optimizations and debug using log messages instead of GDB. The GDB model is abhorrent to me, as it requires me to constantly test a version of the program I will not ship, and then ship a version I have not tested thoroughly. There is some observer effect to the log messages, yes, but there is a hell of a lot more to the mere use of GDB. If I do use debugging capabilities, I use those that show me what is actually going on, not what would be going on if this was a C64 and I was writing in BASIC.


Isn't the old fashioned standard practice thoroughly testing both debug and release builds and not releasing if there's issue in either?

If an issue do happen in both versions, I've seen many choose to fix the debug build first as it is more human friendly to not having to worry about the crazy optimizations and the debug build will almost always fail faster in a more debug-able state (if it didn't then more asserts are needed).

Nowadays though, the standard practice is probably "Testing is such a waste, if it seems to be almost working let's release it. The users will be our testers and we can push an update to fix any issue that the users complain about. We can even laugh at the users that dare to file bugs and close them as 'by design' 'no repro' or 'user error'"

Printf vs debugger does sound off topic, like some vi users refuse (or don't know the key combinations?) to use emacs kind of off topic? :wink:


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 16 posts ]  Go to page 1, 2  Next

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot], Majestic-12 [Bot] and 594 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