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!
Reset on casting to float
-
- Member
- Posts: 5563
- Joined: Mon Mar 25, 2013 7:01 pm
Re: Reset on casting to float
Beware: tutorials are usually written by beginners, and therefore full of beginner mistakes.skyesp wrote:tutorial
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.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.
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.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?
Re: Reset on casting to float
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.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!
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
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
The GNU C++ compiler is g++.
Re: Reset on casting to float
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)iansjack wrote:The GNU C++ compiler is g++.
Re: Reset on casting to float
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.skyesp wrote: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)iansjack wrote:The GNU C++ compiler is g++.
-
- Member
- Posts: 5563
- Joined: Mon Mar 25, 2013 7:01 pm
Re: Reset on casting to float
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 wrote:mapping devices into RAM
Re: Reset on casting to float
The former -- mapping MMIO regions into the address space -- is what I meant.Octocontrabass wrote: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 wrote:mapping devices into RAM