Reset on casting to float

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
skyesp
Posts: 15
Joined: Sat Oct 16, 2021 11:57 am

Reset on casting to float

Post by skyesp »

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!
Octocontrabass
Member
Member
Posts: 5563
Joined: Mon Mar 25, 2013 7:01 pm

Re: Reset on casting to float

Post by Octocontrabass »

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.
Ethin
Member
Member
Posts: 625
Joined: Sun Jun 23, 2019 5:36 pm
Location: North Dakota, United States

Re: Reset on casting to float

Post by Ethin »

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.
skyesp
Posts: 15
Joined: Sat Oct 16, 2021 11:57 am

Re: Reset on casting to float

Post by skyesp »

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
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Reset on casting to float

Post by iansjack »

The GNU C++ compiler is g++.
skyesp
Posts: 15
Joined: Sat Oct 16, 2021 11:57 am

Re: Reset on casting to float

Post by skyesp »

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)
Ethin
Member
Member
Posts: 625
Joined: Sun Jun 23, 2019 5:36 pm
Location: North Dakota, United States

Re: Reset on casting to float

Post by Ethin »

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.
Octocontrabass
Member
Member
Posts: 5563
Joined: Mon Mar 25, 2013 7:01 pm

Re: Reset on casting to float

Post by Octocontrabass »

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.
Ethin
Member
Member
Posts: 625
Joined: Sun Jun 23, 2019 5:36 pm
Location: North Dakota, United States

Re: Reset on casting to float

Post by Ethin »

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.
Post Reply