OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 4:36 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Make pointer volatile in Bare Bones?
PostPosted: Tue Jan 09, 2018 2:41 am 
Offline

Joined: Tue Jan 09, 2018 2:35 am
Posts: 2
Hi all,

So recently I have been starting to develop my own kernel. I am quite new into the topic, but like it so far and I am ready to struggle. To set up my build environment, I followed the (probably) well-known Bare bones tutorial on the wiki:

http://wiki.osdev.org/Bare_bones

After some rewrite of the terminal stuff, qemu kept rebooting my kernel without notice (I guess a triple fault). This was because the variable terminal_buffer is not declared as being volatile (and was being optimized away). Isn't it best practice to do that and explicitly let your compiler know that this pointer points to a memory-mapped location? Or is it best practice to not compile with any optimization? I am interested to hear your thoughts on this.


Top
 Profile  
 
 Post subject: Re: Make pointer volatile in Bare Bones?
PostPosted: Tue Jan 09, 2018 5:48 am 
Offline
Member
Member
User avatar

Joined: Thu Nov 16, 2006 12:01 pm
Posts: 7612
Location: Germany
LvdB wrote:
This was because the variable terminal_buffer is not declared as being volatile (and was being optimized away).


I doubt that this is what was happening. Usually the volatile keyword is used to tell the compiler that the object might change by effects outside of the program, and thus not to cache the variable. If it was indeed optimized away, I would find that.... questionable.

But adding volatile at that point certainly wouldn't hurt, and make things a bit clearer.

_________________
Every good solution is obvious once you've found it.


Top
 Profile  
 
 Post subject: Re: Make pointer volatile in Bare Bones?
PostPosted: Tue Jan 09, 2018 9:31 am 
Offline

Joined: Tue Jan 09, 2018 2:35 am
Posts: 2
After looking a bit more into this, I see you are right. I did not set up my cross compiler correctly, so I should go back to the drawing table.

The issue is that my compiler is producing vectorized instructions. I do not have much experience with these type of instructions, but explicitly turning these optimizations off (which should not be necessary if I did set up my cross-compiler correct in the first place, but hey) "fixes" the problem.

Thanks for your fast answer! I should again look into the definition of a volatile pointer.


Top
 Profile  
 
 Post subject: Re: Make pointer volatile in Bare Bones?
PostPosted: Tue Jan 09, 2018 10:36 am 
Offline
Member
Member

Joined: Thu May 17, 2007 1:27 pm
Posts: 999
Missing volatile cannot lead to crashes here, but it could totally (if you enable whole-program optimization or mark the variable as static) lead to nothing being displayed as the writes get eliminated.

So yes, the missing volatile is a bug in the tutorial.

Speaking of it, the mixed C/C++ style of the tutorial is very bad in my eyes. Nobody is every going to write source-files (as opposed to header files) in way that ensures that they can be compiled both as C and as C++. Does anyone mind if I remove the C++ parts from the example code and move them to the "Writing a kernel in C++" section?

_________________
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: Make pointer volatile in Bare Bones?
PostPosted: Wed Jan 10, 2018 7:09 am 
Offline
Member
Member
User avatar

Joined: Thu Nov 16, 2006 12:01 pm
Posts: 7612
Location: Germany
Absolutely, go ahead. The <header.h> style is deprecated for C++ anyway... and I'd fully expect anyone setting out to write a kernel in C++ to be capable to interface with C code without missing a step.

_________________
Every good solution is obvious once you've found it.


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 21 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