Page 1 of 1
Reset on casting to float
Posted: Sat Oct 16, 2021 12:12 pm
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!
Re: Reset on casting to float
Posted: Sat Oct 16, 2021 8:30 pm
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).
You have to write additional support code before you can use certain C++ features.
Re: Reset on casting to float
Posted: Sat Oct 16, 2021 8:46 pm
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.
Re: Reset on casting to float
Posted: Sat Oct 16, 2021 11:47 pm
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
Re: Reset on casting to float
Posted: Sun Oct 17, 2021 2:14 am
by iansjack
The GNU C++ compiler is g++.
Re: Reset on casting to float
Posted: Sun Oct 17, 2021 5:43 am
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)
Re: Reset on casting to float
Posted: Sun Oct 17, 2021 12:30 pm
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.
Re: Reset on casting to float
Posted: Sun Oct 17, 2021 8:54 pm
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.
Re: Reset on casting to float
Posted: Sun Oct 17, 2021 10:03 pm
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.