OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Dec 02, 2021 10:44 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: Reset on casting to float
PostPosted: Sat Oct 16, 2021 12:12 pm 
Offline

Joined: Sat Oct 16, 2021 11:57 am
Posts: 3
Hi all.
I'm just getting into OSdev with my first attempt, starting out following a youtube tutorial series for the assembly needed to start up. So far I can print text to VGA mode 3. I wanted to get a working system for printing numbers in place in order to be able to debug my IDT implementation but I encountered a perplexing issue: Whenever I attempt to cast from a float to an int in C++, QEMU appears to reset into BIOS.
I am assuming that the reset is due to an unhandled interrupt thanks to said IDT not working yet but I have no idea if that is right or why that would occur.
My toolchain is based off of https://hub.docker.com/r/randomdude/gcc ... x86_64-elf, which seems to have issues with c++ code in general. Does anyone know how to fix this or which other platform independent c++ compiler to use?
Thanks!


Top
 Profile  
 
 Post subject: Re: Reset on casting to float
PostPosted: Sat Oct 16, 2021 8:30 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 3492
skyesp wrote:
tutorial

Beware: tutorials are usually written by beginners, and therefore full of beginner mistakes.

skyesp wrote:
I am assuming that the reset is due to an unhandled interrupt thanks to said IDT not working yet but I have no idea if that is right or why that would occur.

Floating-point operations rely on extended CPU state (x87, SSE, or AVX depending on how you've configured the compiler). Trying to use the extended CPU state without proper initialization can cause exceptions, and exceptions without a working IDT can cause a triple fault and reboot.

OS kernels typically don't use floating-point: the kernel has to save and restore the CPU state on every interrupt and system call, but it can skip saving/restoring things it's not going to use (outside of context switches).

skyesp wrote:
My toolchain is based off of https://hub.docker.com/r/randomdude/gcc ... x86_64-elf, which seems to have issues with c++ code in general. Does anyone know how to fix this or which other platform independent c++ compiler to use?

You have to write additional support code before you can use certain C++ features.


Top
 Profile  
 
 Post subject: Re: Reset on casting to float
PostPosted: Sat Oct 16, 2021 8:46 pm 
Offline
Member
Member

Joined: Sun Jun 23, 2019 5:36 pm
Posts: 456
Location: North Dakota, United States
skyesp wrote:
Hi all.
I'm just getting into OSdev with my first attempt, starting out following a youtube tutorial series for the assembly needed to start up. So far I can print text to VGA mode 3. I wanted to get a working system for printing numbers in place in order to be able to debug my IDT implementation but I encountered a perplexing issue: Whenever I attempt to cast from a float to an int in C++, QEMU appears to reset into BIOS.
I am assuming that the reset is due to an unhandled interrupt thanks to said IDT not working yet but I have no idea if that is right or why that would occur.
My toolchain is based off of https://hub.docker.com/r/randomdude/gcc ... x86_64-elf, which seems to have issues with c++ code in general. Does anyone know how to fix this or which other platform independent c++ compiler to use?
Thanks!

The golden rule in OSDev -- or any low-level code like this (unless you really, really, really need it) is: avoid floating-point at all costs.
This is because floating-point is, by its nature, very complicated. If you absolutely need it (and you will once you get to running actual apps, but since your just starting out you shouldn't need that for a long, long time), you need to enable the floating-point unit and SSE. Then, on every interrupt, you need to save and restore both the FPU and SSE registers and state information. Only then will floating-point work.
Edit: I note to save both FPU and SSE state and to enable both because with floating point (from what I know) its not easy to predict exactly what instruction will correspond to a given FP operation in C/C++, and so you really can only either limit your compiler to emit certain instruction sets or enable both and work with both at once.


Top
 Profile  
 
 Post subject: Re: Reset on casting to float
PostPosted: Sat Oct 16, 2021 11:47 pm 
Offline

Joined: Sat Oct 16, 2021 11:57 am
Posts: 3
Thanks. I had no idea that FP computations were this involved. I will try to keep my kernel float-free. Still, does anyone know of a better c++ compiler for freestanding code? I have the creeping suspicion that mine just compiles cpp files as C files (and i'd quite like to use c++ features if at all possible). Thanks :D


Top
 Profile  
 
 Post subject: Re: Reset on casting to float
PostPosted: Sun Oct 17, 2021 2:14 am 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 4272
Location: Chichester, UK
The GNU C++ compiler is g++.


Top
 Profile  
 
 Post subject: Re: Reset on casting to float
PostPosted: Sun Oct 17, 2021 5:43 am 
Offline

Joined: Sat Oct 16, 2021 11:57 am
Posts: 3
iansjack wrote:
The GNU C++ compiler is g++.

I believe I might already be using a custom version of g++. I think what I got hung up on was the inability to use new without writing a heap manager (correct me if this is wrong please)


Top
 Profile  
 
 Post subject: Re: Reset on casting to float
PostPosted: Sun Oct 17, 2021 12:30 pm 
Offline
Member
Member

Joined: Sun Jun 23, 2019 5:36 pm
Posts: 456
Location: North Dakota, United States
skyesp wrote:
iansjack wrote:
The GNU C++ compiler is g++.

I believe I might already be using a custom version of g++. I think what I got hung up on was the inability to use new without writing a heap manager (correct me if this is wrong please)

Yes, you do need to write a memory manager and set up paging. You'll need that not just for new/delete but also for mapping devices into RAM when your scanning the PCI bus.


Top
 Profile  
 
 Post subject: Re: Reset on casting to float
PostPosted: Sun Oct 17, 2021 8:54 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 3492
Ethin wrote:
mapping devices into RAM

You probably meant either "mapping device MMIO into the virtual address space" or "allocating buffers in RAM for DMA". You can't map devices into RAM.


Top
 Profile  
 
 Post subject: Re: Reset on casting to float
PostPosted: Sun Oct 17, 2021 10:03 pm 
Offline
Member
Member

Joined: Sun Jun 23, 2019 5:36 pm
Posts: 456
Location: North Dakota, United States
Octocontrabass wrote:
Ethin wrote:
mapping devices into RAM

You probably meant either "mapping device MMIO into the virtual address space" or "allocating buffers in RAM for DMA". You can't map devices into RAM.

The former -- mapping MMIO regions into the address space -- is what I meant.


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

All times are UTC - 6 hours


Who is online

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